1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! Canonical SSP2 encoding (SPEC.md §1.2). Re-encoding a decoded message
//! reproduces the input byte-for-byte: raw `json` strings, map entry order,
//! and unknown frames are all preserved by the model.
use crate::decode::{SSP2_MAGIC, WIRE_VERSION};
use crate::model::frame_type as ft;
use crate::model::{Frame, MediaType, Message, MsgKind, Op, OpResult, PushStatus, SubStatus};
use crate::primitives::Writer;
pub fn encode_message(msg: &Message) -> Vec<u8> {
let mut w = Writer::new();
w.raw(SSP2_MAGIC);
w.u16(WIRE_VERSION);
w.u8(match msg.msg_kind {
MsgKind::Request => 0x01,
MsgKind::Response => 0x02,
});
w.u8(0x00); // flags
for frame in &msg.frames {
let (ty, payload) = encode_frame(frame);
w.u8(ty);
w.u32(payload.len() as u32);
w.raw(&payload);
}
w.u8(ft::END);
w.u32(0);
w.into_bytes()
}
fn op_byte(op: Op) -> u8 {
match op {
Op::Upsert => 1,
Op::Delete => 2,
}
}
/// Every frame type with a defined layout in wire version 1 (§1.2 registry),
/// `END` included.
fn is_registered_frame_type(ty: u8) -> bool {
ty == ft::END || ft::REQUEST_TYPES.contains(&ty) || ft::RESPONSE_TYPES.contains(&ty)
}
fn encode_frame(frame: &Frame) -> (u8, Vec<u8>) {
let mut w = Writer::new();
let ty = match frame {
Frame::ReqHeader {
client_id,
schema_version,
} => {
w.str(client_id);
w.i32(*schema_version);
ft::REQ_HEADER
}
Frame::PushCommit {
client_commit_id,
operations,
} => {
w.str(client_commit_id);
w.u32(operations.len() as u32);
for op in operations {
w.str(&op.table);
w.str(&op.row_id);
w.u8(op_byte(op.op));
w.opt(&op.base_version, |w, v| w.i64(*v));
w.opt(&op.payload, |w, p| w.bytes(p));
}
ft::PUSH_COMMIT
}
Frame::PullHeader {
limit_commits,
limit_snapshot_rows,
max_snapshot_pages,
accept,
} => {
w.i32(*limit_commits);
w.i32(*limit_snapshot_rows);
w.i32(*max_snapshot_pages);
w.u8(*accept);
ft::PULL_HEADER
}
Frame::Subscription {
id,
table,
scopes,
params,
cursor,
bootstrap_state,
} => {
w.str(id);
w.str(table);
w.scope_map(scopes);
w.opt(params, |w, j| w.str(&j.0));
w.i64(*cursor);
w.opt(bootstrap_state, |w, j| w.str(&j.0));
ft::SUBSCRIPTION
}
Frame::RespHeader {
required_schema_version,
latest_schema_version,
} => {
w.opt(required_schema_version, |w, v| w.i32(*v));
w.opt(latest_schema_version, |w, v| w.i32(*v));
ft::RESP_HEADER
}
Frame::Lease {
lease_id,
expires_at_ms,
} => {
w.str(lease_id);
w.i64(*expires_at_ms);
ft::LEASE
}
Frame::PushResult {
client_commit_id,
status,
commit_seq,
results,
} => {
w.str(client_commit_id);
w.u8(match status {
PushStatus::Applied => 1,
PushStatus::Cached => 2,
PushStatus::Rejected => 3,
});
w.opt(commit_seq, |w, v| w.i64(*v));
w.u32(results.len() as u32);
for result in results {
match result {
OpResult::Applied { op_index } => {
w.i32(*op_index);
w.u8(1);
}
OpResult::Conflict {
op_index,
code,
message,
server_version,
server_row,
} => {
w.i32(*op_index);
w.u8(2);
w.str(code);
w.str(message);
w.i64(*server_version);
w.bytes(server_row);
}
OpResult::Error {
op_index,
code,
message,
retryable,
} => {
w.i32(*op_index);
w.u8(3);
w.str(code);
w.str(message);
w.bool(*retryable);
}
}
}
ft::PUSH_RESULT
}
Frame::SubStart {
id,
status,
reason_code,
effective_scopes,
bootstrap,
} => {
w.str(id);
w.u8(match status {
SubStatus::Active => 1,
SubStatus::Revoked => 2,
SubStatus::Reset => 3,
});
w.str(reason_code);
w.scope_map(effective_scopes);
w.bool(*bootstrap);
ft::SUB_START
}
Frame::Commit {
commit_seq,
created_at_ms,
actor_id,
tables,
changes,
} => {
w.i64(*commit_seq);
w.i64(*created_at_ms);
w.str(actor_id);
w.u32(tables.len() as u32);
for table in tables {
w.str(table);
}
w.u32(changes.len() as u32);
for change in changes {
w.u16(change.table_index);
w.str(&change.row_id);
w.u8(op_byte(change.op));
w.opt(&change.row_version, |w, v| w.i64(*v));
w.str_map(&change.scopes);
w.opt(&change.row, |w, row| w.bytes(row));
}
ft::COMMIT
}
Frame::SegmentRef {
segment_id,
media_type,
table,
byte_length,
row_count,
as_of_commit_seq,
scope_digest,
row_cursor,
next_row_cursor,
url,
url_expires_at_ms,
} => {
w.str(segment_id);
w.u8(match media_type {
MediaType::Rows => 1,
MediaType::Sqlite => 2,
});
w.str(table);
w.i64(*byte_length);
w.i64(*row_count);
w.i64(*as_of_commit_seq);
w.str(scope_digest);
w.opt(row_cursor, |w, s| w.str(s));
w.opt(next_row_cursor, |w, s| w.str(s));
w.opt(url, |w, s| w.str(s));
w.opt(url_expires_at_ms, |w, v| w.i64(*v));
ft::SEGMENT_REF
}
Frame::SegmentInline { payload } => {
w.raw(payload);
ft::SEGMENT_INLINE
}
Frame::SubEnd {
next_cursor,
bootstrap_state,
} => {
w.i64(*next_cursor);
w.opt(bootstrap_state, |w, j| w.str(&j.0));
ft::SUB_END
}
Frame::Error {
code,
message,
category,
retryable,
recommended_action,
details,
} => {
w.str(code);
w.str(message);
w.str(category);
w.bool(*retryable);
w.str(recommended_action);
w.opt(details, |w, j| w.str(&j.0));
ft::ERROR
}
Frame::Unknown {
frame_type,
payload,
} => {
// §1.2: an encoder MUST NOT emit an unknown frame under a
// frameType registered in this wire version. Preserved unknown
// frames come only from decoding, which never produces such a
// frame — hitting this is encoder misuse, not a decode error.
assert!(
!is_registered_frame_type(*frame_type),
"UNKNOWN frame type 0x{frame_type:02x} collides with a registered frame type"
);
w.raw(payload);
*frame_type
}
};
(ty, w.into_bytes())
}