@cloudpss/ubjson 0.4.14 → 0.4.16

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.
Files changed (40) hide show
  1. package/benchmark-string-size-caculation.js +50 -0
  2. package/benchmark-string.js +3 -3
  3. package/benchmark.js +3 -3
  4. package/dist/common/decoder.js +3 -3
  5. package/dist/common/decoder.js.map +1 -1
  6. package/dist/common/encoder.d.ts +0 -1
  7. package/dist/common/encoder.js +26 -23
  8. package/dist/common/encoder.js.map +1 -1
  9. package/dist/common/string-decoder.js +3 -3
  10. package/dist/common/string-decoder.js.map +1 -1
  11. package/dist/common/string-encoder.d.ts +2 -0
  12. package/dist/common/string-encoder.js +51 -0
  13. package/dist/common/string-encoder.js.map +1 -1
  14. package/dist/decoder.d.ts +1 -2
  15. package/dist/decoder.js +1 -2
  16. package/dist/decoder.js.map +1 -1
  17. package/dist/encoder.js.map +1 -1
  18. package/dist/stream-helper/decoder.d.ts +2 -2
  19. package/dist/stream-helper/decoder.js +2 -2
  20. package/dist/stream-helper/decoder.js.map +1 -1
  21. package/dist/utils.js +3 -0
  22. package/dist/utils.js.map +1 -1
  23. package/package.json +2 -2
  24. package/src/common/decoder.ts +3 -3
  25. package/src/common/encoder.ts +27 -25
  26. package/src/common/string-decoder.ts +3 -4
  27. package/src/common/string-encoder.ts +63 -0
  28. package/src/decoder.ts +1 -3
  29. package/src/encoder.ts +2 -4
  30. package/src/stream-helper/decoder.ts +3 -3
  31. package/src/utils.ts +3 -0
  32. package/tests/decode.js +12 -44
  33. package/tests/e2e.js +20 -21
  34. package/tests/encode.js +31 -53
  35. package/tests/huge-string.js +122 -0
  36. package/tests/rxjs/decode.js +18 -50
  37. package/tests/rxjs/encode.js +17 -61
  38. package/tests/stream/decode.js +17 -49
  39. package/tests/stream/encode.js +17 -61
  40. package/tests/string-encoding.js +4 -7
@@ -8,7 +8,6 @@ import { toArray } from '../.utils.js';
8
8
 
9
9
  /**
10
10
  * 包装为 promise
11
- *
12
11
  * @param {unknown} value 值
13
12
  */
14
13
  function encodeAsync(value) {
@@ -60,21 +59,19 @@ test('encode int16', async () => {
60
59
  });
61
60
 
62
61
  test('encode int32', async () => {
63
- expect(toArray(await encodeAsync(0x12345678))).toEqual(toArray('l', 0x12, 0x34, 0x56, 0x78));
62
+ expect(toArray(await encodeAsync(0x1234_5678))).toEqual(toArray('l', 0x12, 0x34, 0x56, 0x78));
64
63
  });
65
64
 
66
65
  test('encode float32', async () => {
67
- expect(toArray(await encodeAsync(1.00390625))).toEqual(toArray('d', 0x3f, 0x80, 0x80, 0x00));
66
+ expect(toArray(await encodeAsync(1.003_906_25))).toEqual(toArray('d', 0x3f, 0x80, 0x80, 0x00));
68
67
  });
69
68
 
70
69
  test('encode float32 (too large integer)', async () => {
71
- expect(toArray(await encodeAsync(2147483648))).toEqual(toArray('d', 0x4f, 0x00, 0x00, 0x00));
70
+ expect(toArray(await encodeAsync(2_147_483_648))).toEqual(toArray('d', 0x4f, 0x00, 0x00, 0x00));
72
71
  });
73
72
 
74
73
  test('encode float64', async () => {
75
- expect(toArray(await encodeAsync(100000.00390625))).toEqual(
76
- toArray('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00),
77
- );
74
+ expect(toArray(await encodeAsync(100_000.003_906_25))).toEqual(toArray('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00));
78
75
  });
79
76
 
80
77
  test('encode char', async () => {
@@ -82,7 +79,7 @@ test('encode char', async () => {
82
79
  });
83
80
 
84
81
  test('encode char 128', async () => {
85
- expect(toArray(await encodeAsync('\xcc'))).toEqual(toArray('C', '\xcc'));
82
+ expect(toArray(await encodeAsync('\u00CC'))).toEqual(toArray('C', '\u00CC'));
86
83
  });
87
84
 
88
85
  test('encode char 257', async () => {
@@ -126,7 +123,7 @@ test('encode array (undefined)', async () => {
126
123
  });
127
124
 
128
125
  test('encode array (spares)', async () => {
129
- const array = new Array(3);
126
+ const array = Array.from({ length: 3 });
130
127
  array[1] = true;
131
128
  expect(toArray(await encodeAsync(array))).toEqual(toArray('[', 'Z', 'T', 'Z', ']'));
132
129
  });
@@ -190,25 +187,19 @@ test('encode array of objects of arrays', async () => {
190
187
  });
191
188
 
192
189
  test('encode array (int8 typed array)', async () => {
193
- expect(toArray(await encodeAsync(Int8Array.from([18, -2])))).toEqual(
194
- toArray('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe),
195
- );
190
+ expect(toArray(await encodeAsync(Int8Array.from([18, -2])))).toEqual(toArray('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe));
196
191
  });
197
192
 
198
193
  test('encode array (uint8 typed array)', async () => {
199
- expect(toArray(await encodeAsync(Uint8Array.from([18, 254])))).toEqual(
200
- toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe),
201
- );
194
+ expect(toArray(await encodeAsync(Uint8Array.from([18, 254])))).toEqual(toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe));
202
195
  });
203
196
 
204
197
  test('encode array (int16 typed array)', async () => {
205
- expect(toArray(await encodeAsync(Int16Array.from([4660, -292])))).toEqual(
206
- toArray('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc),
207
- );
198
+ expect(toArray(await encodeAsync(Int16Array.from([4660, -292])))).toEqual(toArray('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc));
208
199
  });
209
200
 
210
201
  test('encode array (int32 typed array)', async () => {
211
- expect(toArray(await encodeAsync(Int32Array.from([305419896, -19088744])))).toEqual(
202
+ expect(toArray(await encodeAsync(Int32Array.from([305_419_896, -19_088_744])))).toEqual(
212
203
  toArray('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98),
213
204
  );
214
205
  });
@@ -221,30 +212,7 @@ test('encode array (float32 typed array)', async () => {
221
212
 
222
213
  test('encode array (float64 typed array)', async () => {
223
214
  expect(toArray(await encodeAsync(Float64Array.from([0.25, 0.125])))).toEqual(
224
- toArray(
225
- '[',
226
- '$',
227
- 'D',
228
- '#',
229
- 'i',
230
- 2,
231
- 0x3f,
232
- 0xd0,
233
- 0x00,
234
- 0x00,
235
- 0x00,
236
- 0x00,
237
- 0x00,
238
- 0x00,
239
- 0x3f,
240
- 0xc0,
241
- 0x00,
242
- 0x00,
243
- 0x00,
244
- 0x00,
245
- 0x00,
246
- 0x00,
247
- ),
215
+ toArray('[', '$', 'D', '#', 'i', 2, 0x3f, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
248
216
  );
249
217
  });
250
218
 
@@ -269,9 +237,7 @@ test('encode array (int64 typed array)', async () => {
269
237
  });
270
238
 
271
239
  test('encode object', async () => {
272
- expect(toArray(await encodeAsync({ a: 1, b: 2, c: 3 }))).toEqual(
273
- toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'),
274
- );
240
+ expect(toArray(await encodeAsync({ a: 1, b: 2, c: 3 }))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'));
275
241
  });
276
242
 
277
243
  test('encode object (empty)', async () => {
@@ -283,15 +249,11 @@ test('encode object (empty key)', async () => {
283
249
  });
284
250
 
285
251
  test('encode object (mixed)', async () => {
286
- expect(toArray(await encodeAsync({ a: 1, b: 'a', c: true }))).toEqual(
287
- toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
288
- );
252
+ expect(toArray(await encodeAsync({ a: 1, b: 'a', c: true }))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'));
289
253
  });
290
254
 
291
255
  test('encode object (only null values)', async () => {
292
- expect(toArray(await encodeAsync({ a: null, b: null, c: null }))).toEqual(
293
- toArray('{', 'i', 1, 'a', 'Z', 'i', 1, 'b', 'Z', 'i', 1, 'c', 'Z', '}'),
294
- );
256
+ expect(toArray(await encodeAsync({ a: null, b: null, c: null }))).toEqual(toArray('{', 'i', 1, 'a', 'Z', 'i', 1, 'b', 'Z', 'i', 1, 'c', 'Z', '}'));
295
257
  });
296
258
 
297
259
  test('encode object (skip prototype)', async () => {
@@ -299,9 +261,7 @@ test('encode object (skip prototype)', async () => {
299
261
  obj.a = 1;
300
262
  obj.b = 'a';
301
263
  obj.c = true;
302
- expect(toArray(await encodeAsync(obj))).toEqual(
303
- toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
304
- );
264
+ expect(toArray(await encodeAsync(obj))).toEqual(toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'));
305
265
  });
306
266
 
307
267
  test('encode object (skip symbol)', async () => {
@@ -347,9 +307,7 @@ test('encode huge typed array (16K)', async () => {
347
307
 
348
308
  test('encode huge typed array (~128M)', async () => {
349
309
  const obj = new Uint8Array(128 * 1024 * 1024 - 10);
350
- expect(toArray((await encodeAsync(obj)).slice(0, 10))).toEqual(
351
- toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0),
352
- );
310
+ expect(toArray((await encodeAsync(obj)).slice(0, 10))).toEqual(toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0));
353
311
  });
354
312
 
355
313
  test('encode huge typed array (256M + 1) [error]', async () => {
@@ -364,9 +322,7 @@ test('encode huge typed array (3G) [error]', async () => {
364
322
 
365
323
  test('encode huge data (~128M)', async () => {
366
324
  const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
367
- expect(toArray((await encodeAsync(obj)).slice(0, 11))).toEqual(
368
- toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0),
369
- );
325
+ expect(toArray((await encodeAsync(obj)).subarray(0, 11))).toEqual(toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0));
370
326
  });
371
327
 
372
328
  test('encode huge data (256M + 1) [error]', async () => {
@@ -4,10 +4,7 @@ import { StringDecoder, decodeJs } from '../dist/common/string-decoder.js';
4
4
  /**
5
5
  * 测试编码
6
6
  */
7
- function testEncoding(
8
- /** @type {Pick<TextEncoder, 'encode'>} */ encoder,
9
- /** @type {Pick<TextDecoder, 'decode'>} */ decoder,
10
- ) {
7
+ function testEncoding(/** @type {Pick<TextEncoder, 'encode'>} */ encoder, /** @type {Pick<TextDecoder, 'decode'>} */ decoder) {
11
8
  expect(decoder.decode(encoder.encode(''))).toEqual('');
12
9
  expect(decoder.decode(encoder.encode('p4'))).toEqual('p4');
13
10
  expect(decoder.decode(encoder.encode('t0'))).toEqual('t0');
@@ -33,7 +30,7 @@ function testEncoding(
33
30
 
34
31
  {
35
32
  // 检查所有代理项
36
- for (let index = 0x10000; index < 0x10ffff; index++) {
33
+ for (let index = 0x1_0000; index < 0x10_ffff; index++) {
37
34
  const expected = String.fromCodePoint(index);
38
35
  const actual = decoder.decode(encoder.encode(expected));
39
36
  if (expected !== actual) expect(actual).toEqual(expected);
@@ -53,7 +50,7 @@ function testEncoding(
53
50
  }
54
51
 
55
52
  test('encode string', () => {
56
- testEncoding(new StringEncoder(), new TextDecoder('utf-8', { ignoreBOM: true, fatal: false }));
53
+ testEncoding(new StringEncoder(), new TextDecoder('utf8', { ignoreBOM: true, fatal: false }));
57
54
  });
58
55
 
59
56
  test('decode string', () => {
@@ -80,5 +77,5 @@ test('decode string js', () => {
80
77
 
81
78
  test('decode malformed', () => {
82
79
  const decoder = new StringDecoder();
83
- expect(decoder.decode(new Uint8Array([0xff, 'a'.charCodeAt(0)]), 0, 2)).toEqual('\ufffda');
80
+ expect(decoder.decode(new Uint8Array([0xff, 'a'.charCodeAt(0)]), 0, 2)).toEqual('\uFFFDa');
84
81
  });