@cloudpss/ubjson 0.3.0-alpha.21

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/test/decode.js ADDED
@@ -0,0 +1,423 @@
1
+ /**
2
+ * Tests from https://bitbucket.org/shelacek/ubjson
3
+ */
4
+
5
+ import test from 'tape';
6
+ import * as ubjson from '../dist/index.js';
7
+
8
+ function toBuffer(...args) {
9
+ return Uint8Array.from(args, (x) => (x === +x ? x : x.charCodeAt()));
10
+ }
11
+
12
+ test('decode unsupported type', (t) => {
13
+ t.throws(() => ubjson.decode(toBuffer('!')));
14
+ t.end();
15
+ });
16
+
17
+ test('decode undefined data', (t) => {
18
+ t.throws(() => ubjson.decode(undefined));
19
+ t.end();
20
+ });
21
+
22
+ test('decode undefined', (t) => {
23
+ t.equal(ubjson.decode(toBuffer('N')), undefined);
24
+ t.end();
25
+ });
26
+
27
+ test('decode undefined (multiple noop)', (t) => {
28
+ t.equal(ubjson.decode(toBuffer('N', 'N', 'N')), undefined);
29
+ t.end();
30
+ });
31
+
32
+ test('decode undefined (empty buffer)', (t) => {
33
+ t.equal(ubjson.decode(toBuffer()), undefined);
34
+ t.end();
35
+ });
36
+
37
+ test('decode null', (t) => {
38
+ t.equal(ubjson.decode(toBuffer('Z')), null);
39
+ t.end();
40
+ });
41
+
42
+ test('decode true', (t) => {
43
+ t.equal(ubjson.decode(toBuffer('T')), true);
44
+ t.end();
45
+ });
46
+
47
+ test('decode false', (t) => {
48
+ t.equal(ubjson.decode(toBuffer('F')), false);
49
+ t.end();
50
+ });
51
+
52
+ test('decode int8', (t) => {
53
+ t.equal(ubjson.decode(toBuffer('i', 100)), 100);
54
+ t.end();
55
+ });
56
+
57
+ test('decode uint8', (t) => {
58
+ t.equal(ubjson.decode(toBuffer('U', 200)), 200);
59
+ t.end();
60
+ });
61
+
62
+ test('decode int16', (t) => {
63
+ t.equal(ubjson.decode(toBuffer('I', 0x12, 0x34)), 0x1234);
64
+ t.end();
65
+ });
66
+
67
+ test('decode int32', (t) => {
68
+ t.equal(ubjson.decode(toBuffer('l', 0x12, 0x34, 0x56, 0x78)), 0x12345678);
69
+ t.end();
70
+ });
71
+
72
+ test('decode int64 [error]', (t) => {
73
+ t.throws(() => ubjson.decode(toBuffer('L', 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0)));
74
+ t.end();
75
+ });
76
+
77
+ test('decode float32', (t) => {
78
+ t.equal(ubjson.decode(toBuffer('d', 0x3f, 0x80, 0x80, 0x00)), 1.00390625);
79
+ t.end();
80
+ });
81
+
82
+ test('decode float64', (t) => {
83
+ t.equal(ubjson.decode(toBuffer('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00)), 100000.00390625);
84
+ t.end();
85
+ });
86
+
87
+ test('decode high-precision number [error]', (t) => {
88
+ t.throws(() => ubjson.decode(toBuffer('H', 'i', 3, '1', '.', '1')));
89
+ t.end();
90
+ });
91
+
92
+ test('decode char', (t) => {
93
+ t.equal(ubjson.decode(toBuffer('C', 'a')), 'a');
94
+ t.end();
95
+ });
96
+
97
+ test('decode string', (t) => {
98
+ t.equal(ubjson.decode(toBuffer('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n')), 'ubjson');
99
+ t.end();
100
+ });
101
+
102
+ test('decode array', (t) => {
103
+ t.deepEqual(ubjson.decode(toBuffer('[', 'i', 1, 'i', 2, 'i', 3, ']')), [1, 2, 3]);
104
+ t.end();
105
+ });
106
+
107
+ test('decode array (with no-op)', (t) => {
108
+ t.deepEqual(ubjson.decode(toBuffer('[', 'i', 1, 'N', 'i', 2, 'i', 3, 'N', ']')), [1, 2, 3]);
109
+ t.end();
110
+ });
111
+
112
+ test('decode array (empty, optimized)', (t) => {
113
+ t.deepEqual(ubjson.decode(toBuffer('[', '#', 'i', 0)), []);
114
+ t.end();
115
+ });
116
+
117
+ test('decode array (mixed, optimized)', (t) => {
118
+ t.deepEqual(ubjson.decode(toBuffer('[', '#', 'i', 3, 'i', 1, 'C', 'a', 'T')), [1, 'a', true]);
119
+ t.end();
120
+ });
121
+
122
+ test('decode array (strongly typed, optimized)', (t) => {
123
+ t.deepEqual(ubjson.decode(toBuffer('[', '$', 'i', '#', 'i', 3, 1, 2, 3)), new Int8Array([1, 2, 3]));
124
+ t.end();
125
+ });
126
+
127
+ test('decode array (strongly typed, empty, optimized)', (t) => {
128
+ t.deepEqual(ubjson.decode(toBuffer('[', '$', 'i', '#', 'i', 0)), new Int8Array([]));
129
+ t.end();
130
+ });
131
+
132
+ test('decode N-D array (strongly typed, optimized)', (t) => {
133
+ t.deepEqual(
134
+ ubjson.decode(
135
+ toBuffer('[', '$', '[', '#', 'i', 2, '$', 'i', '#', 'i', 3, 1, 2, 3, '$', 'i', '#', 'i', 3, 4, 5, 6),
136
+ ),
137
+ [new Int8Array([1, 2, 3]), new Int8Array([4, 5, 6])],
138
+ );
139
+ t.end();
140
+ });
141
+
142
+ test('decode array of objects (optimized)', (t) => {
143
+ t.deepEqual(
144
+ ubjson.decode(
145
+ toBuffer(
146
+ '[',
147
+ '$',
148
+ '{',
149
+ '#',
150
+ 'i',
151
+ 2,
152
+ '$',
153
+ 'i',
154
+ '#',
155
+ 'i',
156
+ 3,
157
+ 'i',
158
+ 1,
159
+ 'a',
160
+ 1,
161
+ 'i',
162
+ 1,
163
+ 'b',
164
+ 2,
165
+ 'i',
166
+ 1,
167
+ 'c',
168
+ 3,
169
+ '$',
170
+ 'i',
171
+ '#',
172
+ 'i',
173
+ 3,
174
+ 'i',
175
+ 1,
176
+ 'd',
177
+ 4,
178
+ 'i',
179
+ 1,
180
+ 'e',
181
+ 5,
182
+ 'i',
183
+ 1,
184
+ 'f',
185
+ 6,
186
+ ),
187
+ ),
188
+ [
189
+ { a: 1, b: 2, c: 3 },
190
+ { d: 4, e: 5, f: 6 },
191
+ ],
192
+ );
193
+ t.end();
194
+ });
195
+
196
+ test('decode array of objects of arrays (optimized)', (t) => {
197
+ t.deepEqual(
198
+ ubjson.decode(
199
+ toBuffer(
200
+ '[',
201
+ '$',
202
+ '{',
203
+ '#',
204
+ 'i',
205
+ 2,
206
+ '$',
207
+ '[',
208
+ '#',
209
+ 'i',
210
+ 2,
211
+ 'i',
212
+ 1,
213
+ 'a',
214
+ '$',
215
+ 'i',
216
+ '#',
217
+ 'i',
218
+ 2,
219
+ 1,
220
+ 2,
221
+ 'i',
222
+ 1,
223
+ 'b',
224
+ '$',
225
+ 'i',
226
+ '#',
227
+ 'i',
228
+ 2,
229
+ 3,
230
+ 4,
231
+ '$',
232
+ '[',
233
+ '#',
234
+ 'i',
235
+ 2,
236
+ 'i',
237
+ 1,
238
+ 'c',
239
+ '$',
240
+ 'i',
241
+ '#',
242
+ 'i',
243
+ 2,
244
+ 5,
245
+ 6,
246
+ 'i',
247
+ 1,
248
+ 'd',
249
+ '$',
250
+ 'i',
251
+ '#',
252
+ 'i',
253
+ 2,
254
+ 7,
255
+ 8,
256
+ ),
257
+ ),
258
+ [
259
+ { a: new Int8Array([1, 2]), b: new Int8Array([3, 4]) },
260
+ { c: new Int8Array([5, 6]), d: new Int8Array([7, 8]) },
261
+ ],
262
+ );
263
+ t.end();
264
+ });
265
+
266
+ test('decode array (strongly typed, unexpected eof, optimized)', (t) => {
267
+ t.throws(() => ubjson.decode(toBuffer('[', '$', 'i', '#', 'i', 3, 1, 2)));
268
+ t.end();
269
+ });
270
+
271
+ test('decode array (strongly typed, invalid length value, optimized)', (t) => {
272
+ t.throws(() => ubjson.decode(toBuffer('[', '$', 'i', '#', 'i', -1)));
273
+ t.end();
274
+ });
275
+
276
+ test('decode array (strongly typed, invalid length type, optimized)', (t) => {
277
+ t.throws(() => ubjson.decode(toBuffer('[', '$', 'i', '#', 'C', '0')));
278
+ t.end();
279
+ });
280
+
281
+ test('decode array (strongly typed, malformed, optimized)', (t) => {
282
+ t.throws(() => ubjson.decode(toBuffer('[', '$', 'i', 1, 2, 3, ']')));
283
+ t.end();
284
+ });
285
+
286
+ test('decode array (only null values, optimized)', (t) => {
287
+ t.deepEqual(ubjson.decode(toBuffer('[', '$', 'Z', '#', 'i', 3)), [null, null, null]);
288
+ t.end();
289
+ });
290
+
291
+ test('decode array (only true values, optimized)', (t) => {
292
+ t.deepEqual(ubjson.decode(toBuffer('[', '$', 'T', '#', 'i', 3)), [true, true, true]);
293
+ t.end();
294
+ });
295
+
296
+ test('decode array (only false values, optimized)', (t) => {
297
+ t.deepEqual(ubjson.decode(toBuffer('[', '$', 'F', '#', 'i', 3)), [false, false, false]);
298
+ t.end();
299
+ });
300
+
301
+ test('decode array (int8, strongly typed, optimized) [use typed array]', (t) => {
302
+ const actual = ubjson.decode(toBuffer('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe), { useTypedArrays: true });
303
+ t.assert(actual.constructor.name === 'Int8Array');
304
+ t.deepEqual(actual, Int8Array.from([18, -2]));
305
+ t.end();
306
+ });
307
+
308
+ test('decode array (uint8, strongly typed, optimized) [use typed array]', (t) => {
309
+ const actual = ubjson.decode(toBuffer('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe), { useTypedArrays: true });
310
+ t.assert(actual.constructor.name === 'Uint8Array');
311
+ t.deepEqual(actual, Uint8Array.from([18, 254]));
312
+ t.end();
313
+ });
314
+
315
+ test('decode array (int16, strongly typed, optimized) [use typed array]', (t) => {
316
+ const actual = ubjson.decode(toBuffer('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc), {
317
+ useTypedArrays: true,
318
+ });
319
+ t.assert(actual.constructor.name === 'Int16Array');
320
+ t.deepEqual(actual, Int16Array.from([4660, -292]));
321
+ t.end();
322
+ });
323
+
324
+ test('decode array (int32, strongly typed, optimized) [use typed array]', (t) => {
325
+ const actual = ubjson.decode(toBuffer('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98), {
326
+ useTypedArrays: true,
327
+ });
328
+ t.assert(actual.constructor.name === 'Int32Array');
329
+ t.deepEqual(actual, Int32Array.from([305419896, -19088744]));
330
+ t.end();
331
+ });
332
+
333
+ test('decode array (float32, strongly typed, optimized) [use typed array]', (t) => {
334
+ const actual = ubjson.decode(toBuffer('[', '$', 'd', '#', 'i', 2, 0x3e, 0x80, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00), {
335
+ useTypedArrays: true,
336
+ });
337
+ t.assert(actual.constructor.name === 'Float32Array');
338
+ t.deepEqual(actual, Float32Array.from([0.25, 0.125]));
339
+ t.end();
340
+ });
341
+
342
+ test('decode array (float64, strongly typed, optimized) [use typed array]', (t) => {
343
+ const actual = ubjson.decode(
344
+ toBuffer(
345
+ '[',
346
+ '$',
347
+ 'D',
348
+ '#',
349
+ 'i',
350
+ 2,
351
+ 0x3f,
352
+ 0xd0,
353
+ 0x00,
354
+ 0x00,
355
+ 0x00,
356
+ 0x00,
357
+ 0x00,
358
+ 0x00,
359
+ 0x3f,
360
+ 0xc0,
361
+ 0x00,
362
+ 0x00,
363
+ 0x00,
364
+ 0x00,
365
+ 0x00,
366
+ 0x00,
367
+ ),
368
+ { useTypedArrays: true },
369
+ );
370
+ t.assert(actual.constructor.name === 'Float64Array');
371
+ t.deepEqual(actual, Float64Array.from([0.25, 0.125]));
372
+ t.end();
373
+ });
374
+
375
+ test('decode object', (t) => {
376
+ t.deepEqual(ubjson.decode(toBuffer('{', 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'i', 2, 'i', 1, 'c', 'i', 3, '}')), {
377
+ a: 1,
378
+ b: 2,
379
+ c: 3,
380
+ });
381
+ t.end();
382
+ });
383
+
384
+ test('decode object (with no-op)', (t) => {
385
+ t.deepEqual(
386
+ ubjson.decode(
387
+ toBuffer('N', '{', 'N', 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'N', 'i', 2, 'i', 1, 'c', 'i', 3, 'N', '}', 'N'),
388
+ ),
389
+ { a: 1, b: 2, c: 3 },
390
+ );
391
+ t.end();
392
+ });
393
+
394
+ test('decode array (empty, optimized)', (t) => {
395
+ t.deepEqual(ubjson.decode(toBuffer('{', '#', 'i', 0)), {});
396
+ t.end();
397
+ });
398
+
399
+ test('decode object (mixed, optimized)', (t) => {
400
+ t.deepEqual(
401
+ ubjson.decode(toBuffer('{', '#', 'i', 3, 'i', 1, 'a', 'i', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T')),
402
+ { a: 1, b: 'a', c: true },
403
+ );
404
+ t.end();
405
+ });
406
+
407
+ test('decode object (strongly typed, optimized)', (t) => {
408
+ t.deepEqual(ubjson.decode(toBuffer('{', '$', 'i', '#', 'i', 3, 'i', 1, 'a', 1, 'i', 1, 'b', 2, 'i', 1, 'c', 3)), {
409
+ a: 1,
410
+ b: 2,
411
+ c: 3,
412
+ });
413
+ t.end();
414
+ });
415
+
416
+ test('decode object (only null values, optimized)', (t) => {
417
+ t.deepEqual(ubjson.decode(toBuffer('{', '$', 'Z', '#', 'i', 3, 'i', 1, 'a', 'i', 1, 'b', 'i', 1, 'c')), {
418
+ a: null,
419
+ b: null,
420
+ c: null,
421
+ });
422
+ t.end();
423
+ });
package/test/e2e.js ADDED
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Tests from https://bitbucket.org/shelacek/ubjson
3
+ */
4
+
5
+ import test from 'tape';
6
+ import * as ubjson from '../dist/index.js';
7
+
8
+ test('encode/decode complex object', (t) => {
9
+ const expected = {
10
+ hello: 'world',
11
+ from: ['UBJSON'],
12
+ colors: [[255, 255, 255], [0, 0, 0], [64, 64, 96], new Uint8Array([255, 0, 1, 2, 3, 127])],
13
+ domains: {
14
+ com: 'commercial',
15
+ org: 'organization',
16
+ net: 'network',
17
+ },
18
+ entires: [
19
+ {
20
+ id: 1,
21
+ name: 'test',
22
+ content: null,
23
+ timestamp: 1532432408.008,
24
+ published: false,
25
+ },
26
+ {
27
+ id: 2,
28
+ name: 'lorem',
29
+ content: 'Lorem ipsum...',
30
+ timestamp: 1532432416.346,
31
+ published: true,
32
+ },
33
+ ],
34
+ plots: {
35
+ traces: [
36
+ {
37
+ name: 'test',
38
+ x: new Float64Array([1, 2, 3, -Infinity, Infinity, NaN, Number.MAX_VALUE, Number.MIN_VALUE]),
39
+ y: new Int8Array([0, 4, 5, 6, -128, -1, 127]),
40
+ },
41
+ {
42
+ name: 'test2',
43
+ x: new Float32Array([1, 2, 3, -Infinity, Infinity, NaN, Number.MAX_VALUE, Number.MIN_VALUE]),
44
+ y: new Int16Array([4, 5, 6, -32768, 32767]),
45
+ },
46
+ {
47
+ name: 'test233',
48
+ x: new Int32Array([1, 2, 3, 0, -1]),
49
+ y: new Int32Array([4, 5, 6, -(2 ** 31), 2 ** 31 - 1]),
50
+ },
51
+ ],
52
+ },
53
+ description1: 'medium text'.repeat(100),
54
+ description2: 'medium text'.repeat(1000),
55
+ document1: 'long text'.repeat(10000),
56
+ document2: 'long text'.repeat(100000),
57
+ };
58
+ const encoded = ubjson.encode(expected);
59
+ const actual = ubjson.decode(encoded);
60
+ t.deepEqual(actual, expected);
61
+ t.end();
62
+ });
63
+
64
+ test('encode/decode complex object (no encodeInto)', (t) => {
65
+ const encodeInto = TextEncoder.prototype.encodeInto;
66
+ TextEncoder.prototype.encodeInto = undefined;
67
+ const expected = {
68
+ hello: 'world',
69
+ from: ['UBJSON'],
70
+ colors: [[255, 255, 255], [0, 0, 0], [64, 64, 96], new Uint8Array([255, 0, 1, 2, 3, 127])],
71
+ domains: {
72
+ com: 'commercial',
73
+ org: 'organization',
74
+ net: 'network',
75
+ },
76
+ entires: [
77
+ {
78
+ id: 1,
79
+ name: 'test',
80
+ content: null,
81
+ timestamp: 1532432408.008,
82
+ published: false,
83
+ },
84
+ {
85
+ id: 2,
86
+ name: 'lorem',
87
+ content: 'Lorem ipsum...',
88
+ timestamp: 1532432416.346,
89
+ published: true,
90
+ },
91
+ ],
92
+ plots: {
93
+ traces: [
94
+ {
95
+ name: 'test',
96
+ x: new Float64Array([1, 2, 3, -Infinity, Infinity, NaN, Number.MAX_VALUE, Number.MIN_VALUE]),
97
+ y: new Int8Array([0, 4, 5, 6, -128, -1, 127]),
98
+ },
99
+ {
100
+ name: 'test2',
101
+ x: new Float32Array([1, 2, 3, -Infinity, Infinity, NaN, Number.MAX_VALUE, Number.MIN_VALUE]),
102
+ y: new Int16Array([4, 5, 6, -32768, 32767]),
103
+ },
104
+ {
105
+ name: 'test233',
106
+ x: new Int32Array([1, 2, 3, 0, -1]),
107
+ y: new Int32Array([4, 5, 6, -(2 ** 31), 2 ** 31 - 1]),
108
+ },
109
+ ],
110
+ },
111
+ description1: 'medium text'.repeat(100),
112
+ description2: 'medium text'.repeat(1000),
113
+ document1: 'long text'.repeat(10000),
114
+ document2: 'long text'.repeat(100000),
115
+ };
116
+ const encoded = ubjson.encode(expected);
117
+ const actual = ubjson.decode(encoded);
118
+ t.deepEqual(actual, expected);
119
+ t.end();
120
+ TextEncoder.prototype.encodeInto = encodeInto;
121
+ });
122
+
123
+ test('encode/decode large array', (t) => {
124
+ t.deepEqual(ubjson.decode(ubjson.encode(new Array(100000))), Array.from({ length: 100000 }).fill(null));
125
+ t.end();
126
+ });
127
+
128
+ test('encode/decode object with undefined values', (t) => {
129
+ t.deepEqual(ubjson.decode(ubjson.encode({ a: 1, b: undefined, c: { d: 2, e: undefined, f: null } })), {
130
+ a: 1,
131
+ c: { d: 2, f: null },
132
+ });
133
+ t.end();
134
+ });