@based/server 9.2.7 → 10.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/index.js +1 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/channel/destroy.js +3 -1
- package/dist/channel/destroy.js.map +1 -1
- package/dist/channel/start.js +10 -3
- package/dist/channel/start.js.map +1 -1
- package/dist/channel/subscribe.js +13 -2
- package/dist/channel/subscribe.js.map +1 -1
- package/dist/channel/types.d.ts +1 -0
- package/dist/channel/types.js.map +1 -1
- package/dist/channel/unsub.js +24 -3
- package/dist/channel/unsub.js.map +1 -1
- package/dist/functions/index.d.ts +1 -1
- package/dist/functions/index.js +50 -20
- package/dist/functions/index.js.map +1 -1
- package/dist/incoming/http/fakeWs/function.js +8 -6
- package/dist/incoming/http/fakeWs/function.js.map +1 -1
- package/dist/incoming/http/fakeWs/handleBinary.js +4 -3
- package/dist/incoming/http/fakeWs/handleBinary.js.map +1 -1
- package/dist/incoming/http/fakeWs/query.js +6 -5
- package/dist/incoming/http/fakeWs/query.js.map +1 -1
- package/dist/incoming/http/handleRequest.js.map +1 -1
- package/dist/incoming/http/index.js +2 -0
- package/dist/incoming/http/index.js.map +1 -1
- package/dist/incoming/http/pathMatcher.d.ts +1 -1
- package/dist/incoming/http/pathMatcher.js +16 -1
- package/dist/incoming/http/pathMatcher.js.map +1 -1
- package/dist/incoming/http/payloadParser.d.ts +1 -1
- package/dist/incoming/http/payloadParser.js +1 -0
- package/dist/incoming/http/payloadParser.js.map +1 -1
- package/dist/incoming/http/query.js +3 -2
- package/dist/incoming/http/query.js.map +1 -1
- package/dist/incoming/upgrade.js +1 -0
- package/dist/incoming/upgrade.js.map +1 -1
- package/dist/incoming/ws/auth.js +9 -7
- package/dist/incoming/ws/auth.js.map +1 -1
- package/dist/incoming/ws/channelPublish.js +4 -3
- package/dist/incoming/ws/channelPublish.js.map +1 -1
- package/dist/incoming/ws/channelSubscribe.js +6 -5
- package/dist/incoming/ws/channelSubscribe.js.map +1 -1
- package/dist/incoming/ws/function.js +8 -5
- package/dist/incoming/ws/function.js.map +1 -1
- package/dist/incoming/ws/get.js +8 -7
- package/dist/incoming/ws/get.js.map +1 -1
- package/dist/incoming/ws/index.d.ts +2 -2
- package/dist/incoming/ws/index.js +14 -12
- package/dist/incoming/ws/index.js.map +1 -1
- package/dist/incoming/ws/query.js +6 -5
- package/dist/incoming/ws/query.js.map +1 -1
- package/dist/incoming/ws/stream.d.ts +1 -1
- package/dist/incoming/ws/stream.js +29 -28
- package/dist/incoming/ws/stream.js.map +1 -1
- package/dist/protocol.d.ts +25 -14
- package/dist/protocol.js +346 -158
- package/dist/protocol.js.map +1 -1
- package/dist/query/destroy.js +2 -1
- package/dist/query/destroy.js.map +1 -1
- package/dist/query/send.d.ts +1 -1
- package/dist/query/send.js +29 -7
- package/dist/query/send.js.map +1 -1
- package/dist/query/start/error.js +4 -1
- package/dist/query/start/error.js.map +1 -1
- package/dist/query/start/update.js +22 -4
- package/dist/query/start/update.js.map +1 -1
- package/dist/query/subscribe.js +15 -5
- package/dist/query/subscribe.js.map +1 -1
- package/dist/query/types.d.ts +1 -0
- package/dist/query/unsub.js +32 -8
- package/dist/query/unsub.js.map +1 -1
- package/dist/sendError.js +4 -4
- package/dist/sendError.js.map +1 -1
- package/dist/sendHttpResponse.js +6 -2
- package/dist/sendHttpResponse.js.map +1 -1
- package/package.json +2 -2
package/dist/protocol.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import zlib from 'node:zlib';
|
|
2
|
-
|
|
2
|
+
import { DECODER, ENCODER, writeUint32, writeUint24, writeUint64, readUint32, } from '@saulx/utils';
|
|
3
3
|
export const COMPRESS_FROM_BYTES = 150;
|
|
4
4
|
export const decodeHeader = (nr) => {
|
|
5
5
|
// 4 bytes
|
|
@@ -24,22 +24,8 @@ export const decodeHeader = (nr) => {
|
|
|
24
24
|
len,
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const byte = n & 0xff;
|
|
30
|
-
buff[index] = byte;
|
|
31
|
-
n = (n - byte) / 256;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
export const readUint8 = (buff, start, len) => {
|
|
35
|
-
let n = 0;
|
|
36
|
-
const s = len - 1 + start;
|
|
37
|
-
for (let i = s; i >= start; i--) {
|
|
38
|
-
n = n * 256 + buff[i];
|
|
39
|
-
}
|
|
40
|
-
return n;
|
|
41
|
-
};
|
|
42
|
-
export const encodeHeader = (type, isDeflate, len) => {
|
|
27
|
+
// add buffer
|
|
28
|
+
const encodeHeader = (type, isDeflate, len, buffer, offset) => {
|
|
43
29
|
// 4 bytes
|
|
44
30
|
// type (3 bits)
|
|
45
31
|
// 0 = functionData
|
|
@@ -54,100 +40,266 @@ export const encodeHeader = (type, isDeflate, len) => {
|
|
|
54
40
|
// len (28 bits)
|
|
55
41
|
const encodedMeta = (type << 1) + (Number(isDeflate) | 0);
|
|
56
42
|
const nr = (len << 4) + encodedMeta;
|
|
57
|
-
|
|
43
|
+
writeUint32(buffer, nr, offset);
|
|
58
44
|
};
|
|
59
|
-
export const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
45
|
+
export const CONTENT_TYPE_JSON_U8 = new Uint8Array([255]);
|
|
46
|
+
export const CONTENT_TYPE_UINT8_ARRAY_U8 = new Uint8Array([254]);
|
|
47
|
+
export const CONTENT_TYPE_STRING_U8 = new Uint8Array([253]);
|
|
48
|
+
export const CONTENT_TYPE_UNDEFINED_U8 = new Uint8Array([252]);
|
|
49
|
+
export const CONTENT_TYPE_NULL_U8 = new Uint8Array([251]);
|
|
50
|
+
export const CONTENT_TYPE_VERSION_1_U8 = new Uint8Array([250]);
|
|
51
|
+
const EMPTY_BUFFER = new Uint8Array([]);
|
|
52
|
+
export const cacheV2toV1 = (buf) => {
|
|
53
|
+
// 12 + 8
|
|
54
|
+
const isString = buf[20] === CONTENT_TYPE_STRING_U8[0];
|
|
55
|
+
if (isString) {
|
|
56
|
+
const header = decodeHeader(readUint32(buf, 0));
|
|
57
|
+
if (!header.isDeflate) {
|
|
58
|
+
const n = new Uint8Array(buf.byteLength - 1 + 2);
|
|
59
|
+
n.set(buf.subarray(0, 20), 0);
|
|
60
|
+
encodeHeader(header.type, header.isDeflate, header.len + 1, n, 0);
|
|
61
|
+
n[20] = 34; // "
|
|
62
|
+
n.set(buf.subarray(21), 21);
|
|
63
|
+
n[n.byteLength - 1] = 34; // "
|
|
64
|
+
return n;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
// very heavy rly sad..
|
|
68
|
+
const valBuffer = decodePayload(buf.subarray(20), true, false);
|
|
69
|
+
const newEncoded = valueToBufferV1(valBuffer, true);
|
|
70
|
+
const len = newEncoded.buf.byteLength;
|
|
71
|
+
const n = new Uint8Array(20 + len);
|
|
72
|
+
n.set(buf.subarray(0, 20), 0);
|
|
73
|
+
encodeHeader(header.type, header.isDeflate, 20 + len, n, 0);
|
|
74
|
+
n.set(newEncoded.buf, 20);
|
|
75
|
+
return n;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
const header = decodeHeader(readUint32(buf, 0));
|
|
80
|
+
const n = new Uint8Array(buf.byteLength - 1);
|
|
81
|
+
n.set(buf.subarray(0, 20), 0);
|
|
82
|
+
encodeHeader(header.type, header.isDeflate, header.len - 1, n, 0);
|
|
83
|
+
n.set(buf.subarray(21), 20);
|
|
84
|
+
return n;
|
|
63
85
|
}
|
|
64
|
-
// TODO: only stringify if not string...
|
|
65
|
-
return Buffer.from(JSON.stringify(payload));
|
|
66
86
|
};
|
|
67
|
-
export const
|
|
68
|
-
|
|
69
|
-
|
|
87
|
+
export const diffV2toV1 = (buf) => {
|
|
88
|
+
// totally wrong...
|
|
89
|
+
// 12 + 16
|
|
90
|
+
const n = new Uint8Array(buf.byteLength - 1);
|
|
91
|
+
n.set(buf.subarray(0, 28), 0);
|
|
92
|
+
n.set(buf.subarray(29), 28);
|
|
93
|
+
return n;
|
|
94
|
+
};
|
|
95
|
+
export const valueToBufferV1 = (payload, deflate) => {
|
|
96
|
+
let buf;
|
|
97
|
+
if (payload === undefined) {
|
|
98
|
+
buf = new Uint8Array([]);
|
|
70
99
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
100
|
+
else {
|
|
101
|
+
try {
|
|
102
|
+
buf = ENCODER.encode(JSON.stringify(payload));
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
console.log(payload);
|
|
106
|
+
buf = ENCODER.encode(payload);
|
|
107
|
+
}
|
|
74
108
|
}
|
|
75
|
-
|
|
76
|
-
|
|
109
|
+
if (deflate && buf.byteLength > COMPRESS_FROM_BYTES) {
|
|
110
|
+
return {
|
|
111
|
+
contentByte: CONTENT_TYPE_VERSION_1_U8,
|
|
112
|
+
buf: zlib.deflateRawSync(buf, {}),
|
|
113
|
+
deflate: true,
|
|
114
|
+
};
|
|
77
115
|
}
|
|
116
|
+
return {
|
|
117
|
+
contentByte: CONTENT_TYPE_VERSION_1_U8,
|
|
118
|
+
buf,
|
|
119
|
+
deflate: false,
|
|
120
|
+
};
|
|
78
121
|
};
|
|
79
|
-
|
|
122
|
+
// pass buffer and offset
|
|
123
|
+
export const valueToBuffer = (payload, deflate) => {
|
|
124
|
+
if (payload === undefined) {
|
|
125
|
+
return {
|
|
126
|
+
contentByte: CONTENT_TYPE_UNDEFINED_U8,
|
|
127
|
+
deflate: false,
|
|
128
|
+
buf: EMPTY_BUFFER,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
if (payload === null) {
|
|
132
|
+
return {
|
|
133
|
+
contentByte: CONTENT_TYPE_NULL_U8,
|
|
134
|
+
deflate: false,
|
|
135
|
+
buf: EMPTY_BUFFER,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
80
138
|
if (typeof payload === 'string') {
|
|
139
|
+
const buf = ENCODER.encode(payload);
|
|
140
|
+
if (deflate && buf.byteLength > COMPRESS_FROM_BYTES) {
|
|
141
|
+
return {
|
|
142
|
+
contentByte: CONTENT_TYPE_STRING_U8,
|
|
143
|
+
buf: zlib.deflateRawSync(buf, {}),
|
|
144
|
+
deflate: true,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
contentByte: CONTENT_TYPE_STRING_U8,
|
|
149
|
+
buf,
|
|
150
|
+
deflate: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
// mark as based db query object
|
|
154
|
+
if (payload instanceof Uint8Array) {
|
|
155
|
+
return {
|
|
156
|
+
contentByte: CONTENT_TYPE_UINT8_ARRAY_U8,
|
|
157
|
+
buf: payload,
|
|
158
|
+
deflate: false,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
const buf = ENCODER.encode(JSON.stringify(payload));
|
|
162
|
+
if (buf.byteLength > COMPRESS_FROM_BYTES) {
|
|
163
|
+
return {
|
|
164
|
+
contentByte: CONTENT_TYPE_JSON_U8,
|
|
165
|
+
buf: zlib.deflateRawSync(buf, {}),
|
|
166
|
+
deflate: true,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const result = {
|
|
170
|
+
contentByte: CONTENT_TYPE_JSON_U8,
|
|
171
|
+
buf,
|
|
172
|
+
deflate: false,
|
|
173
|
+
};
|
|
174
|
+
return result;
|
|
175
|
+
};
|
|
176
|
+
export const decodePayloadV1 = (payload, isDeflate) => {
|
|
177
|
+
if (!payload || payload.byteLength === 0) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
let p;
|
|
181
|
+
if (!isDeflate) {
|
|
182
|
+
p = DECODER.decode(payload);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
try {
|
|
186
|
+
p = zlib.inflateRawSync(payload).toString();
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
console.error('Error deflating payload', err);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (typeof p === 'string') {
|
|
81
193
|
try {
|
|
82
|
-
return JSON.parse(
|
|
194
|
+
return JSON.parse(p);
|
|
83
195
|
}
|
|
84
196
|
catch (err) { }
|
|
85
197
|
}
|
|
86
|
-
return
|
|
198
|
+
return p;
|
|
199
|
+
};
|
|
200
|
+
export const decodePayload = (payload, isDeflate, isOldClient) => {
|
|
201
|
+
if (isOldClient) {
|
|
202
|
+
return decodePayloadV1(payload, isDeflate);
|
|
203
|
+
}
|
|
204
|
+
const contentType = payload[0];
|
|
205
|
+
if (contentType === CONTENT_TYPE_UNDEFINED_U8[0]) {
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
if (contentType === CONTENT_TYPE_UINT8_ARRAY_U8[0]) {
|
|
209
|
+
return payload.subarray(1);
|
|
210
|
+
}
|
|
211
|
+
if (contentType === CONTENT_TYPE_STRING_U8[0]) {
|
|
212
|
+
if (isDeflate) {
|
|
213
|
+
return zlib.inflateRawSync(payload.subarray(1)).toString();
|
|
214
|
+
}
|
|
215
|
+
return DECODER.decode(payload.subarray(1));
|
|
216
|
+
}
|
|
217
|
+
if (contentType === CONTENT_TYPE_JSON_U8[0]) {
|
|
218
|
+
let str;
|
|
219
|
+
if (isDeflate) {
|
|
220
|
+
str = zlib.inflateRawSync(payload.subarray(1)).toString();
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
str = DECODER.decode(payload.subarray(1));
|
|
224
|
+
}
|
|
225
|
+
if (typeof str === 'string') {
|
|
226
|
+
try {
|
|
227
|
+
return JSON.parse(str);
|
|
228
|
+
}
|
|
229
|
+
catch (err) { }
|
|
230
|
+
}
|
|
231
|
+
return str;
|
|
232
|
+
}
|
|
233
|
+
if (contentType === CONTENT_TYPE_NULL_U8[0]) {
|
|
234
|
+
return null;
|
|
235
|
+
}
|
|
87
236
|
};
|
|
88
237
|
export const decodeName = (arr, start, end) => {
|
|
89
|
-
|
|
90
|
-
return textDecoder.decode(name);
|
|
238
|
+
return DECODER.decode(arr.subarray(start, end));
|
|
91
239
|
};
|
|
92
|
-
export const encodeFunctionResponse = (id,
|
|
240
|
+
export const encodeFunctionResponse = (id, val) => {
|
|
93
241
|
// Type 0
|
|
94
242
|
// | 4 header | 3 id | * payload |
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
isDeflate = true;
|
|
101
|
-
buffer = zlib.deflateRawSync(buffer, {});
|
|
102
|
-
}
|
|
103
|
-
if (chunks === 1) {
|
|
104
|
-
const headerSize = 4;
|
|
105
|
-
const idSize = 3;
|
|
106
|
-
const msgSize = idSize + buffer.length;
|
|
107
|
-
const header = encodeHeader(0, isDeflate, msgSize);
|
|
108
|
-
// not very nessecary but ok
|
|
243
|
+
const headerSize = 4;
|
|
244
|
+
const idSize = 3;
|
|
245
|
+
const isOld = val.contentByte[0] === CONTENT_TYPE_VERSION_1_U8[0];
|
|
246
|
+
if (isOld) {
|
|
247
|
+
const msgSize = idSize + val.buf.byteLength;
|
|
109
248
|
const array = new Uint8Array(headerSize + msgSize);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
if (
|
|
113
|
-
array.set(
|
|
249
|
+
encodeHeader(0, val.deflate, msgSize, array, 0);
|
|
250
|
+
writeUint24(array, id, 4);
|
|
251
|
+
if (val.buf.byteLength) {
|
|
252
|
+
array.set(val.buf, 7);
|
|
114
253
|
}
|
|
115
254
|
return array;
|
|
116
255
|
}
|
|
117
256
|
else {
|
|
118
|
-
|
|
119
|
-
|
|
257
|
+
const msgSize = idSize + val.buf.byteLength + 1;
|
|
258
|
+
const array = new Uint8Array(headerSize + msgSize);
|
|
259
|
+
encodeHeader(0, val.deflate, msgSize, array, 0);
|
|
260
|
+
writeUint24(array, id, 4);
|
|
261
|
+
array[7] = val.contentByte[0];
|
|
262
|
+
if (val.buf.byteLength) {
|
|
263
|
+
array.set(val.buf, 8);
|
|
264
|
+
}
|
|
265
|
+
return array;
|
|
120
266
|
}
|
|
121
267
|
};
|
|
122
|
-
export const encodeStreamFunctionResponse = (id,
|
|
268
|
+
export const encodeStreamFunctionResponse = (id, val) => {
|
|
123
269
|
// Type 7
|
|
124
270
|
// | 4 header | 1 subType | 3 id | * payload |
|
|
125
|
-
let isDeflate = false;
|
|
126
|
-
// TODO: implement for streams!
|
|
127
|
-
// chunk isChunk | isNotCunk | isLastchunk 0|1 (use 1 bye for now?)
|
|
128
271
|
const chunks = 1;
|
|
129
|
-
if (buffer.length > COMPRESS_FROM_BYTES) {
|
|
130
|
-
isDeflate = true;
|
|
131
|
-
buffer = zlib.deflateRawSync(buffer, {});
|
|
132
|
-
}
|
|
133
272
|
if (chunks === 1) {
|
|
134
273
|
const headerSize = 4;
|
|
135
274
|
const idSize = 3;
|
|
136
275
|
const subTypeSize = 1;
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
276
|
+
const isOld = val.contentByte[0] === CONTENT_TYPE_VERSION_1_U8[0];
|
|
277
|
+
if (isOld) {
|
|
278
|
+
const msgSize = idSize + subTypeSize + val.buf.byteLength;
|
|
279
|
+
const array = new Uint8Array(headerSize + msgSize);
|
|
280
|
+
encodeHeader(7, val.deflate, msgSize, array, 0);
|
|
281
|
+
array[4] = 1;
|
|
282
|
+
writeUint24(array, id, 5);
|
|
283
|
+
if (val.buf.byteLength) {
|
|
284
|
+
array.set(val.buf, 8);
|
|
285
|
+
}
|
|
286
|
+
return array;
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
const msgSize = idSize + subTypeSize + val.buf.byteLength + 1;
|
|
290
|
+
const array = new Uint8Array(headerSize + msgSize);
|
|
291
|
+
encodeHeader(7, val.deflate, msgSize, array, 0);
|
|
292
|
+
array[4] = 1;
|
|
293
|
+
writeUint24(array, id, 5);
|
|
294
|
+
array[8] = val.contentByte[0];
|
|
295
|
+
if (val.buf.byteLength) {
|
|
296
|
+
array.set(val.buf, 9);
|
|
297
|
+
}
|
|
298
|
+
return array;
|
|
146
299
|
}
|
|
147
|
-
return array;
|
|
148
300
|
}
|
|
149
301
|
else {
|
|
150
|
-
console.warn('
|
|
302
|
+
console.warn('Stream response to chunks not implemented yet');
|
|
151
303
|
return new Uint8Array(0);
|
|
152
304
|
}
|
|
153
305
|
};
|
|
@@ -158,114 +310,149 @@ export const encodeStreamFunctionChunkResponse = (id, seqId, code = 0, maxChunkS
|
|
|
158
310
|
if (maxChunkSize) {
|
|
159
311
|
msgSize += 3;
|
|
160
312
|
}
|
|
161
|
-
const header = encodeHeader(7, false, msgSize);
|
|
162
313
|
const array = new Uint8Array(4 + msgSize);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
314
|
+
encodeHeader(7, false, msgSize, array, 0);
|
|
315
|
+
array[4] = 2;
|
|
316
|
+
writeUint24(array, id, 5);
|
|
317
|
+
array[8] = seqId;
|
|
318
|
+
array[9] = code;
|
|
168
319
|
if (maxChunkSize) {
|
|
169
|
-
|
|
320
|
+
writeUint24(array, maxChunkSize, 10);
|
|
170
321
|
}
|
|
171
322
|
return array;
|
|
172
323
|
};
|
|
173
324
|
export const encodeGetResponse = (id) => {
|
|
174
325
|
// Type 4
|
|
175
326
|
// | 4 header | 8 id |
|
|
176
|
-
const header = encodeHeader(3, false, 8);
|
|
177
327
|
const array = new Uint8Array(12);
|
|
178
|
-
|
|
179
|
-
|
|
328
|
+
encodeHeader(3, false, 8, array, 0);
|
|
329
|
+
writeUint64(array, id, 4);
|
|
180
330
|
return array;
|
|
181
331
|
};
|
|
182
332
|
export const updateId = (payload, id) => {
|
|
183
|
-
const prevId = payload.slice(4, 12);
|
|
184
|
-
|
|
333
|
+
const prevId = payload.slice(4, 12); // does this actually copy! CHECK
|
|
334
|
+
writeUint64(payload, id, 4);
|
|
185
335
|
return prevId;
|
|
186
336
|
};
|
|
187
|
-
export const encodeObservableResponse = (id, checksum,
|
|
337
|
+
export const encodeObservableResponse = (id, checksum, val) => {
|
|
188
338
|
// Type 1 (full data)
|
|
189
339
|
// | 4 header | 8 id | 8 checksum | * payload |
|
|
190
|
-
|
|
191
|
-
if (
|
|
192
|
-
|
|
193
|
-
|
|
340
|
+
const isOld = val.contentByte[0] === CONTENT_TYPE_VERSION_1_U8[0];
|
|
341
|
+
if (isOld) {
|
|
342
|
+
const msgSize = 16 + val.buf.byteLength;
|
|
343
|
+
const array = new Uint8Array(4 + msgSize);
|
|
344
|
+
encodeHeader(1, val.deflate, msgSize, array, 0);
|
|
345
|
+
writeUint64(array, id, 4);
|
|
346
|
+
writeUint64(array, checksum, 12);
|
|
347
|
+
if (val.buf.byteLength) {
|
|
348
|
+
array.set(val.buf, 20);
|
|
349
|
+
}
|
|
350
|
+
return [array, val.deflate];
|
|
194
351
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
352
|
+
else {
|
|
353
|
+
const msgSize = 16 + val.buf.byteLength + 1;
|
|
354
|
+
const array = new Uint8Array(4 + msgSize);
|
|
355
|
+
encodeHeader(1, val.deflate, msgSize, array, 0);
|
|
356
|
+
writeUint64(array, id, 4);
|
|
357
|
+
writeUint64(array, checksum, 12);
|
|
358
|
+
array[20] = val.contentByte[0];
|
|
359
|
+
if (val.buf.byteLength) {
|
|
360
|
+
array.set(val.buf, 21);
|
|
361
|
+
}
|
|
362
|
+
return [array, val.deflate];
|
|
203
363
|
}
|
|
204
|
-
return [array, isDeflate];
|
|
205
364
|
};
|
|
206
|
-
export const encodeObservableDiffResponse = (id, checksum, previousChecksum,
|
|
365
|
+
export const encodeObservableDiffResponse = (id, checksum, previousChecksum, val) => {
|
|
207
366
|
// Type 2 (diff data)
|
|
208
367
|
// | 4 header | 8 id | 8 checksum | 8 previousChecksum | * diff |
|
|
209
|
-
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
368
|
+
const isOld = val.contentByte[0] === CONTENT_TYPE_VERSION_1_U8[0];
|
|
369
|
+
if (isOld) {
|
|
370
|
+
const msgSize = 24 + val.buf.byteLength;
|
|
371
|
+
const array = new Uint8Array(4 + msgSize);
|
|
372
|
+
encodeHeader(2, val.deflate, msgSize, array, 0);
|
|
373
|
+
writeUint64(array, id, 4);
|
|
374
|
+
writeUint64(array, checksum, 12);
|
|
375
|
+
writeUint64(array, previousChecksum, 20);
|
|
376
|
+
if (val.buf.byteLength) {
|
|
377
|
+
array.set(val.buf, 28);
|
|
378
|
+
}
|
|
213
379
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
380
|
+
else {
|
|
381
|
+
const msgSize = 24 + val.buf.byteLength + 1;
|
|
382
|
+
const array = new Uint8Array(4 + msgSize);
|
|
383
|
+
encodeHeader(2, val.deflate, msgSize, array, 0);
|
|
384
|
+
writeUint64(array, id, 4);
|
|
385
|
+
writeUint64(array, checksum, 12);
|
|
386
|
+
writeUint64(array, previousChecksum, 20);
|
|
387
|
+
array[28] = val.contentByte[0];
|
|
388
|
+
if (val.buf.byteLength) {
|
|
389
|
+
array.set(val.buf, 29);
|
|
390
|
+
}
|
|
391
|
+
return array;
|
|
223
392
|
}
|
|
224
|
-
return array;
|
|
225
393
|
};
|
|
226
|
-
const encodeSimpleResponse = (type,
|
|
394
|
+
const encodeSimpleResponse = (type, val) => {
|
|
227
395
|
// | 4 header | * payload |
|
|
228
|
-
|
|
229
|
-
if (
|
|
230
|
-
|
|
231
|
-
|
|
396
|
+
const isOld = val.contentByte[0] === CONTENT_TYPE_VERSION_1_U8[0];
|
|
397
|
+
if (isOld) {
|
|
398
|
+
const headerSize = 4;
|
|
399
|
+
const msgSize = val.buf.byteLength;
|
|
400
|
+
const array = new Uint8Array(headerSize + msgSize);
|
|
401
|
+
encodeHeader(type, val.deflate, msgSize, array, 0);
|
|
402
|
+
if (val.buf.byteLength) {
|
|
403
|
+
array.set(val.buf, 4);
|
|
404
|
+
}
|
|
405
|
+
return array;
|
|
232
406
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
407
|
+
else {
|
|
408
|
+
const headerSize = 4;
|
|
409
|
+
const msgSize = val.buf.byteLength + 1;
|
|
410
|
+
const array = new Uint8Array(headerSize + msgSize);
|
|
411
|
+
encodeHeader(type, val.deflate, msgSize, array, 0);
|
|
412
|
+
array[4] = val.contentByte[0];
|
|
413
|
+
if (val.buf.byteLength) {
|
|
414
|
+
array.set(val.buf, 5);
|
|
415
|
+
}
|
|
416
|
+
return array;
|
|
240
417
|
}
|
|
241
|
-
return array;
|
|
242
418
|
};
|
|
243
|
-
export const encodeAuthResponse = (
|
|
419
|
+
export const encodeAuthResponse = (val) => {
|
|
244
420
|
// Type 4
|
|
245
|
-
return encodeSimpleResponse(4,
|
|
421
|
+
return encodeSimpleResponse(4, val);
|
|
246
422
|
};
|
|
247
|
-
export const encodeErrorResponse = (
|
|
423
|
+
export const encodeErrorResponse = (val) => {
|
|
248
424
|
// Type 5
|
|
249
|
-
return encodeSimpleResponse(5,
|
|
425
|
+
return encodeSimpleResponse(5, val);
|
|
250
426
|
};
|
|
251
|
-
export const encodeChannelMessage = (id,
|
|
427
|
+
export const encodeChannelMessage = (id, val) => {
|
|
252
428
|
// Type 7.0 (fill data)
|
|
253
429
|
// | 4 header | 1 subType | 8 id | * payload |
|
|
254
|
-
|
|
255
|
-
if (
|
|
256
|
-
|
|
257
|
-
|
|
430
|
+
const isOld = val.contentByte[0] === CONTENT_TYPE_VERSION_1_U8[0];
|
|
431
|
+
if (isOld) {
|
|
432
|
+
const msgSize = 8 + val.buf.byteLength + 1;
|
|
433
|
+
const array = new Uint8Array(4 + msgSize);
|
|
434
|
+
// sub protocol 7.0
|
|
435
|
+
array[4] = 0;
|
|
436
|
+
encodeHeader(7, val.deflate, msgSize, array, 0);
|
|
437
|
+
writeUint64(array, id, 5);
|
|
438
|
+
if (val.buf.byteLength) {
|
|
439
|
+
array.set(val.buf, 13);
|
|
440
|
+
}
|
|
441
|
+
return array;
|
|
258
442
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
array.
|
|
443
|
+
else {
|
|
444
|
+
const msgSize = 8 + val.buf.byteLength + 2;
|
|
445
|
+
const array = new Uint8Array(4 + msgSize);
|
|
446
|
+
encodeHeader(7, val.deflate, msgSize, array, 0);
|
|
447
|
+
// sub protocol 7.0
|
|
448
|
+
array[4] = 0;
|
|
449
|
+
writeUint64(array, id, 5);
|
|
450
|
+
array[13] = val.contentByte[0];
|
|
451
|
+
if (val.buf.byteLength) {
|
|
452
|
+
array.set(val.buf, 14);
|
|
453
|
+
}
|
|
454
|
+
return array;
|
|
267
455
|
}
|
|
268
|
-
return array;
|
|
269
456
|
};
|
|
270
457
|
export const encodeReload = (type, seqId) => {
|
|
271
458
|
// Type 7.3 (fill data)
|
|
@@ -273,17 +460,17 @@ export const encodeReload = (type, seqId) => {
|
|
|
273
460
|
// 1 = browser
|
|
274
461
|
// 2 = non-browser
|
|
275
462
|
// | 4 header | 1 subType | 1 type \ 1 seqId
|
|
276
|
-
const msgSize =
|
|
277
|
-
const header = encodeHeader(7, false, msgSize);
|
|
463
|
+
const msgSize = 3;
|
|
278
464
|
const array = new Uint8Array(4 + msgSize);
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
465
|
+
encodeHeader(7, false, msgSize, array, 0);
|
|
466
|
+
array[4] = 3;
|
|
467
|
+
array[5] = type;
|
|
468
|
+
array[6] = seqId;
|
|
283
469
|
return array;
|
|
284
470
|
};
|
|
285
|
-
|
|
286
|
-
|
|
471
|
+
// FIXME
|
|
472
|
+
export const decode = (buffer, isOldClient) => {
|
|
473
|
+
const header = readUint32(buffer, 0);
|
|
287
474
|
const { isDeflate, len, type } = decodeHeader(header);
|
|
288
475
|
if (type === 1) {
|
|
289
476
|
// | 4 header | 8 id | 8 checksum | * payload |
|
|
@@ -292,7 +479,8 @@ export const decode = (buffer) => {
|
|
|
292
479
|
}
|
|
293
480
|
const start = 20;
|
|
294
481
|
const end = len + 4;
|
|
295
|
-
|
|
482
|
+
// very wrong
|
|
483
|
+
return decodePayload(buffer.slice(start, end), isDeflate, false);
|
|
296
484
|
}
|
|
297
485
|
};
|
|
298
486
|
//# sourceMappingURL=protocol.js.map
|