@cloudpss/ubjson 0.3.6 → 0.3.9
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/benchmark-string.js +18 -20
- package/benchmark.js +1 -0
- package/dist/{constants.d.ts → common/constants.d.ts} +0 -0
- package/dist/{constants.js → common/constants.js} +0 -0
- package/dist/common/constants.js.map +1 -0
- package/dist/common/decoder.d.ts +48 -0
- package/dist/{decoder.js → common/decoder.js} +13 -18
- package/dist/common/decoder.js.map +1 -0
- package/dist/common/encoder.d.ts +74 -0
- package/dist/common/encoder.js +282 -0
- package/dist/common/encoder.js.map +1 -0
- package/dist/common/string-decoder.d.ts +13 -0
- package/dist/common/string-decoder.js +106 -0
- package/dist/common/string-decoder.js.map +1 -0
- package/dist/{string-encoder.d.ts → common/string-encoder.d.ts} +0 -0
- package/dist/{string-encoder.js → common/string-encoder.js} +0 -0
- package/dist/common/string-encoder.js.map +1 -0
- package/dist/encoder.d.ts +10 -2
- package/dist/encoder.js +2 -283
- package/dist/encoder.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/stream/encoder.d.ts +14 -0
- package/dist/stream/encoder.js +86 -0
- package/dist/stream/encoder.js.map +1 -0
- package/dist/stream/index.d.ts +4 -0
- package/dist/stream/index.js +7 -0
- package/dist/stream/index.js.map +1 -0
- package/jest.config.js +3 -0
- package/package.json +18 -8
- package/src/{constants.ts → common/constants.ts} +0 -0
- package/src/{decoder.ts → common/decoder.ts} +13 -18
- package/src/common/encoder.ts +305 -0
- package/src/common/string-decoder.ts +107 -0
- package/src/{string-encoder.ts → common/string-encoder.ts} +0 -0
- package/src/encoder.ts +4 -305
- package/src/index.ts +14 -2
- package/src/stream/encoder.ts +83 -0
- package/src/stream/index.ts +8 -0
- package/tests/decode.js +433 -0
- package/{test → tests}/e2e.js +19 -27
- package/tests/encode.js +354 -0
- package/tests/stream/encode.js +380 -0
- package/tests/string-encoding.js +71 -0
- package/tests/tsconfig.json +7 -0
- package/tsconfig.json +1 -2
- package/dist/constants.js.map +0 -1
- package/dist/decoder.d.ts +0 -2
- package/dist/decoder.js.map +0 -1
- package/dist/string-decoder.d.ts +0 -9
- package/dist/string-decoder.js +0 -67
- package/dist/string-decoder.js.map +0 -1
- package/dist/string-encoder.js.map +0 -1
- package/src/string-decoder.ts +0 -69
- package/test/decode.js +0 -491
- package/test/encode.js +0 -432
- package/test/string-encoding.js +0 -62
package/tests/encode.js
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests from https://bitbucket.org/shelacek/ubjson
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { encode, decode } from '../dist/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {[ArrayBuffer] | [Uint8Array] | (number | string)[]} args
|
|
10
|
+
* @returns {number[]}
|
|
11
|
+
*/
|
|
12
|
+
function toArray(...args) {
|
|
13
|
+
if (args[0] instanceof ArrayBuffer) {
|
|
14
|
+
return Array.from(new Uint8Array(args[0]));
|
|
15
|
+
}
|
|
16
|
+
if (args[0] instanceof Uint8Array) {
|
|
17
|
+
return Array.from(args[0]);
|
|
18
|
+
}
|
|
19
|
+
return args.map((x) => (typeof x == 'number' ? x : /** @type {string} */ (x).charCodeAt(0)));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
test('encode function', () => {
|
|
23
|
+
expect(() => encode(function () {})).toThrow();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('encode bigint', () => {
|
|
27
|
+
expect(() => encode(1n)).toThrow();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('encode symbol', () => {
|
|
31
|
+
expect(() => encode(Symbol('sym'))).toThrow();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('encode undefined', () => {
|
|
35
|
+
expect(toArray(encode(undefined))).toEqual(toArray('N'));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test('encode null', () => {
|
|
39
|
+
expect(toArray(encode(null))).toEqual(toArray('Z'));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('encode true', () => {
|
|
43
|
+
expect(toArray(encode(true))).toEqual(toArray('T'));
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('encode false', () => {
|
|
47
|
+
expect(toArray(encode(false))).toEqual(toArray('F'));
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('encode int8', () => {
|
|
51
|
+
expect(toArray(encode(-1))).toEqual(toArray('i', 255));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('encode uint8', () => {
|
|
55
|
+
expect(toArray(encode(200))).toEqual(toArray('U', 200));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('encode int16', () => {
|
|
59
|
+
expect(toArray(encode(0x1234))).toEqual(toArray('I', 0x12, 0x34));
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('encode int32', () => {
|
|
63
|
+
expect(toArray(encode(0x12345678))).toEqual(toArray('l', 0x12, 0x34, 0x56, 0x78));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('encode float32', () => {
|
|
67
|
+
expect(toArray(encode(1.00390625))).toEqual(toArray('d', 0x3f, 0x80, 0x80, 0x00));
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('encode float32 (too large integer)', () => {
|
|
71
|
+
expect(toArray(encode(2147483648))).toEqual(toArray('d', 0x4f, 0x00, 0x00, 0x00));
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('encode float64', () => {
|
|
75
|
+
expect(toArray(encode(100000.00390625))).toEqual(toArray('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00));
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('encode char', () => {
|
|
79
|
+
expect(toArray(encode('a'))).toEqual(toArray('C', 'a'));
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('encode char 128', () => {
|
|
83
|
+
expect(toArray(encode('\xcc'))).toEqual(toArray('C', '\xcc'));
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('encode char 257', () => {
|
|
87
|
+
expect(toArray(encode('\u0123'))).toEqual(toArray('S', 'i', 2, 196, 163));
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('encode char emoji', () => {
|
|
91
|
+
expect(toArray(encode('💖'))).toEqual(toArray('S', 'i', 4, 240, 159, 146, 150));
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('encode string', () => {
|
|
95
|
+
expect(toArray(encode('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test('encode string (no encodeInto)', () => {
|
|
99
|
+
const encodeInto = TextEncoder.prototype.encodeInto;
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
TextEncoder.prototype.encodeInto = undefined;
|
|
102
|
+
expect(toArray(encode('ubjson'))).toEqual(toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
|
|
103
|
+
|
|
104
|
+
TextEncoder.prototype.encodeInto = encodeInto;
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('encode array', () => {
|
|
108
|
+
expect(toArray(encode([1, 2, 3, -1]))).toEqual(toArray('[', 'U', 1, 'U', 2, 'U', 3, 'i', 255, ']'));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('encode array (empty)', () => {
|
|
112
|
+
expect(toArray(encode([]))).toEqual(toArray('[', ']'));
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test('encode array (undefined)', () => {
|
|
116
|
+
expect(toArray(encode([undefined]))).toEqual(toArray('[', 'Z', ']'));
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('encode array (spares)', () => {
|
|
120
|
+
const array = new Array(3);
|
|
121
|
+
array[1] = true;
|
|
122
|
+
expect(toArray(encode(array))).toEqual(toArray('[', 'Z', 'T', 'Z', ']'));
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
test('encode array (mixed)', () => {
|
|
126
|
+
expect(toArray(encode([1, 'a', true]))).toEqual(toArray('[', 'U', 1, 'C', 'a', 'T', ']'));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
test('encode array (int8)', () => {
|
|
130
|
+
expect(toArray(encode([-1, 2, 3]))).toEqual(toArray('[', 'i', 255, 'U', 2, 'U', 3, ']'));
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('encode array (int16)', () => {
|
|
134
|
+
expect(toArray(encode([255, -1]))).toEqual(toArray('[', 'U', 0xff, 'i', 0xff, ']'));
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('encode array (only null values)', () => {
|
|
138
|
+
expect(toArray(encode([null, null, null]))).toEqual(toArray('[', 'Z', 'Z', 'Z', ']'));
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test('encode N-D array', () => {
|
|
142
|
+
expect(
|
|
143
|
+
decode(
|
|
144
|
+
encode([
|
|
145
|
+
[1, 2, 3],
|
|
146
|
+
[4, 5, 6],
|
|
147
|
+
]),
|
|
148
|
+
),
|
|
149
|
+
).toEqual([
|
|
150
|
+
[1, 2, 3],
|
|
151
|
+
[4, 5, 6],
|
|
152
|
+
]);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test('encode array of objects', () => {
|
|
156
|
+
expect(
|
|
157
|
+
decode(
|
|
158
|
+
encode([
|
|
159
|
+
{ a: 1, b: 2, c: 3 },
|
|
160
|
+
{ d: 4, e: 5, f: 6 },
|
|
161
|
+
]),
|
|
162
|
+
),
|
|
163
|
+
).toEqual([
|
|
164
|
+
{ a: 1, b: 2, c: 3 },
|
|
165
|
+
{ d: 4, e: 5, f: 6 },
|
|
166
|
+
]);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('encode array of objects of arrays', () => {
|
|
170
|
+
expect(
|
|
171
|
+
decode(
|
|
172
|
+
encode([
|
|
173
|
+
{ a: [1, 2], b: [3, 4] },
|
|
174
|
+
{ c: [5, 6], d: [7, 8] },
|
|
175
|
+
]),
|
|
176
|
+
),
|
|
177
|
+
).toEqual([
|
|
178
|
+
{ a: [1, 2], b: [3, 4] },
|
|
179
|
+
{ c: [5, 6], d: [7, 8] },
|
|
180
|
+
]);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('encode array (int8 typed array)', () => {
|
|
184
|
+
expect(toArray(encode(Int8Array.from([18, -2])))).toEqual(toArray('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe));
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test('encode array (uint8 typed array)', () => {
|
|
188
|
+
expect(toArray(encode(Uint8Array.from([18, 254])))).toEqual(toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('encode array (int16 typed array)', () => {
|
|
192
|
+
expect(toArray(encode(Int16Array.from([4660, -292])))).toEqual(
|
|
193
|
+
toArray('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc),
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
test('encode array (int32 typed array)', () => {
|
|
198
|
+
expect(toArray(encode(Int32Array.from([305419896, -19088744])))).toEqual(
|
|
199
|
+
toArray('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98),
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
test('encode array (float32 typed array)', () => {
|
|
204
|
+
expect(toArray(encode(Float32Array.from([0.25, 0.125])))).toEqual(
|
|
205
|
+
toArray('[', '$', 'd', '#', 'i', 2, 0x3e, 0x80, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00),
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('encode array (float64 typed array)', () => {
|
|
210
|
+
expect(toArray(encode(Float64Array.from([0.25, 0.125])))).toEqual(
|
|
211
|
+
toArray(
|
|
212
|
+
'[',
|
|
213
|
+
'$',
|
|
214
|
+
'D',
|
|
215
|
+
'#',
|
|
216
|
+
'i',
|
|
217
|
+
2,
|
|
218
|
+
0x3f,
|
|
219
|
+
0xd0,
|
|
220
|
+
0x00,
|
|
221
|
+
0x00,
|
|
222
|
+
0x00,
|
|
223
|
+
0x00,
|
|
224
|
+
0x00,
|
|
225
|
+
0x00,
|
|
226
|
+
0x3f,
|
|
227
|
+
0xc0,
|
|
228
|
+
0x00,
|
|
229
|
+
0x00,
|
|
230
|
+
0x00,
|
|
231
|
+
0x00,
|
|
232
|
+
0x00,
|
|
233
|
+
0x00,
|
|
234
|
+
),
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
test('encode array (uint8clamped typed array)', () => {
|
|
239
|
+
expect(() => encode(new Uint8ClampedArray())).toThrow();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
test('encode array (uint16 typed array)', () => {
|
|
243
|
+
expect(() => encode(new Uint16Array())).toThrow();
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test('encode array (uint32 typed array)', () => {
|
|
247
|
+
expect(() => encode(new Uint32Array())).toThrow();
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
test('encode array (uint64 typed array)', () => {
|
|
251
|
+
expect(() => encode(new BigUint64Array())).toThrow();
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
test('encode array (int64 typed array)', () => {
|
|
255
|
+
expect(() => encode(new BigInt64Array())).toThrow();
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test('encode object', () => {
|
|
259
|
+
expect(toArray(encode({ a: 1, b: 2, c: 3 }))).toEqual(
|
|
260
|
+
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'),
|
|
261
|
+
);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('encode object (empty)', () => {
|
|
265
|
+
expect(toArray(encode({}))).toEqual(toArray('{', '}'));
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test('encode object (empty key)', () => {
|
|
269
|
+
expect(toArray(encode({ '': '' }))).toEqual(toArray('{', 'i', 0, 'S', 'i', 0, '}'));
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
test('encode object (mixed)', () => {
|
|
273
|
+
expect(toArray(encode({ a: 1, b: 'a', c: true }))).toEqual(
|
|
274
|
+
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
|
|
275
|
+
);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
test('encode object (only null values)', () => {
|
|
279
|
+
expect(toArray(encode({ a: null, b: null, c: null }))).toEqual(
|
|
280
|
+
toArray('{', 'i', 1, 'a', 'Z', 'i', 1, 'b', 'Z', 'i', 1, 'c', 'Z', '}'),
|
|
281
|
+
);
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
test('encode object (skip prototype)', () => {
|
|
285
|
+
const obj = Object.create({ a: 2, x: 'xx' });
|
|
286
|
+
obj.a = 1;
|
|
287
|
+
obj.b = 'a';
|
|
288
|
+
obj.c = true;
|
|
289
|
+
expect(toArray(encode(obj))).toEqual(
|
|
290
|
+
toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
|
|
291
|
+
);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('encode object (skip symbol)', () => {
|
|
295
|
+
const obj = { [Symbol()]: true, a: 1 };
|
|
296
|
+
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
test('encode object (skip non-enumerable)', () => {
|
|
300
|
+
const obj = {};
|
|
301
|
+
Object.defineProperty(obj, 'a', { value: 1, configurable: true, writable: true });
|
|
302
|
+
expect(toArray(encode(obj))).toEqual(toArray('{', '}'));
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
test('encode object (include getter)', () => {
|
|
306
|
+
const obj = {};
|
|
307
|
+
Object.defineProperty(obj, 'a', { get: () => 1, enumerable: true });
|
|
308
|
+
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, '}'));
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('encode object (skip undefined)', () => {
|
|
312
|
+
const obj = { a: undefined };
|
|
313
|
+
expect(toArray(encode(obj))).toEqual(toArray('{', '}'));
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
test('encode object (function) [error]', () => {
|
|
317
|
+
const obj = { a: () => {} };
|
|
318
|
+
expect(() => encode(obj)).toThrow(/Unsupported type \[object Function\]/);
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
test('encode object (include null)', () => {
|
|
322
|
+
const obj = { a: null };
|
|
323
|
+
expect(toArray(encode(obj))).toEqual(toArray('{', 'i', 1, 'a', 'Z', '}'));
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
test('encode huge typed array (16K)', () => {
|
|
327
|
+
const obj = new Uint8Array(16 * 1024);
|
|
328
|
+
expect(toArray(encode(obj).slice(0, 8))).toEqual(toArray('[', '$', 'U', '#', 'I', 0x40, 0x00, 0));
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
test('encode huge typed array (~128M)', () => {
|
|
332
|
+
const obj = new Uint8Array(128 * 1024 * 1024 - 10);
|
|
333
|
+
expect(toArray(encode(obj).slice(0, 10))).toEqual(toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0));
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
test('encode huge typed array (128M) [error]', () => {
|
|
337
|
+
const obj = new Uint8Array(128 * 1024 * 1024);
|
|
338
|
+
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
test('encode huge typed array (3G) [error]', () => {
|
|
342
|
+
const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
|
|
343
|
+
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
test('encode huge data (~128M)', () => {
|
|
347
|
+
const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
|
|
348
|
+
expect(toArray(encode(obj).slice(0, 11))).toEqual(toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0));
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
test('encode huge data (128M) [error]', () => {
|
|
352
|
+
const obj = [new Uint8Array(128 * 1024 * 1024)];
|
|
353
|
+
expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
|
|
354
|
+
});
|