@cloudpss/ubjson 0.5.35 → 0.5.37

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 (43) hide show
  1. package/{benchmark-string.js → benchmark-string-decode.js} +4 -4
  2. package/benchmark-string-encode.js +32 -0
  3. package/benchmark-string-size-caculation.js +9 -11
  4. package/benchmark.js +1 -0
  5. package/dist/common/decoder.js +2 -1
  6. package/dist/common/decoder.js.map +1 -1
  7. package/dist/common/encoder.d.ts +4 -2
  8. package/dist/common/encoder.js +106 -45
  9. package/dist/common/encoder.js.map +1 -1
  10. package/dist/common/errors.d.ts +4 -0
  11. package/dist/common/errors.js +14 -0
  12. package/dist/common/errors.js.map +1 -0
  13. package/dist/common/string-decoder.d.ts +5 -3
  14. package/dist/common/string-decoder.js +23 -14
  15. package/dist/common/string-decoder.js.map +1 -1
  16. package/dist/common/string-encoder.d.ts +32 -2
  17. package/dist/common/string-encoder.js +105 -12
  18. package/dist/common/string-encoder.js.map +1 -1
  19. package/dist/stream-helper/encoder.d.ts +4 -4
  20. package/dist/stream-helper/encoder.js +116 -41
  21. package/dist/stream-helper/encoder.js.map +1 -1
  22. package/package.json +3 -3
  23. package/src/common/decoder.ts +2 -1
  24. package/src/common/encoder.ts +100 -42
  25. package/src/common/errors.ts +14 -0
  26. package/src/common/string-decoder.ts +26 -17
  27. package/src/common/string-encoder.ts +103 -14
  28. package/src/stream-helper/encoder.ts +118 -39
  29. package/tests/.utils.js +10 -0
  30. package/tests/e2e/.data.js +470 -0
  31. package/tests/e2e/no-buffer-text.js +37 -0
  32. package/tests/e2e/no-buffer.js +30 -0
  33. package/tests/e2e/no-encode-into.js +32 -0
  34. package/tests/e2e/no-textencoder-decoder.js +34 -0
  35. package/tests/e2e/normal.js +27 -0
  36. package/tests/e2e/stream.js +20 -0
  37. package/tests/encode.js +11 -19
  38. package/tests/huge-string.js +7 -9
  39. package/tests/rxjs/encode.js +4 -18
  40. package/tests/stream/encode.js +0 -15
  41. package/tests/string-encoding.js +3 -2
  42. package/tests/tsconfig.json +2 -1
  43. package/tests/e2e.js +0 -415
package/tests/encode.js CHANGED
@@ -500,20 +500,22 @@ test('encode huge typed array (16K)', () => {
500
500
 
501
501
  test('encode huge typed array (~128M)', () => {
502
502
  const obj = new Uint8Array(128 * 1024 * 1024 - 10);
503
- expect(toArray(encode(obj).slice(0, 10))).toEqual(toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0));
503
+ obj[0] = 0x58;
504
+ expect(toArray(encode(obj).slice(0, 10))).toEqual(toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0x58));
504
505
  // @ts-expect-error Access private property
505
506
  expect(getEncoder().pool).toBe(poolInit);
506
507
  });
507
508
 
508
- test('encode huge typed array (128M) [error]', () => {
509
+ test('encode huge typed array (128M)', () => {
509
510
  const obj = new Uint8Array(128 * 1024 * 1024);
510
- expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
511
+ obj[0] = 0x58;
512
+ expect(toArray(encode(obj).slice(0, 10))).toEqual(toArray('[', '$', 'U', '#', 'l', 0x8, 0x00, 0x00, 0x00, 0x58));
511
513
  // @ts-expect-error Access private property
512
514
  expect(getEncoder().pool).toBe(poolInit);
513
515
  });
514
516
 
515
- test('encode huge typed array (3G) [error]', () => {
516
- const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
517
+ test('encode huge typed array (1G) [error]', () => {
518
+ const obj = new Uint8Array(1 * 1024 * 1024 * 1024);
517
519
  expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
518
520
  // @ts-expect-error Access private property
519
521
  expect(getEncoder().pool).toBe(poolInit);
@@ -530,27 +532,17 @@ test('encode huge data (~128M)', () => {
530
532
  expect(getEncoder().pool).toBe(poolInit);
531
533
  });
532
534
 
533
- test('encode huge data (128M - 9)', () => {
534
- const obj = [new Uint8Array(128 * 1024 * 1024 - 9)];
535
+ test('encode huge data (128M)', () => {
536
+ const obj = [new Uint8Array(128 * 1024 * 1024)];
535
537
  obj[0][0] = 0x12;
536
538
  obj[0][1] = 0x34;
537
- const encoded = encode(obj);
538
- expect(encoded.length).toBe(128 * 1024 * 1024 + 2);
539
- // (128 * 1024 * 1024 - 9) === 0x7fffff7
540
- expect(toArray(encoded.slice(0, 12))).toEqual(
541
- toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf7, 0x12, 0x34),
539
+ expect(toArray(encode(obj).slice(0, 12))).toEqual(
540
+ toArray('[', '[', '$', 'U', '#', 'l', 0x8, 0x00, 0x00, 0x00, 0x12, 0x34),
542
541
  );
543
542
  // @ts-expect-error Access private property
544
543
  expect(getEncoder().pool).toBe(poolInit);
545
544
  });
546
545
 
547
- test('encode huge data (128M - 8) [error]', () => {
548
- const obj = [new Uint8Array(128 * 1024 * 1024 - 8)];
549
- expect(() => encode(obj)).toThrow(/Buffer has exceed max size/);
550
- // @ts-expect-error Access private property
551
- expect(getEncoder().pool).toBe(poolInit);
552
- });
553
-
554
546
  test('encode huge data (256M)', () => {
555
547
  const obj = [new Uint8Array(128 * 1024 * 1024 - 9), new Uint8Array(128 * 1024 * 1024 - 9)];
556
548
  obj[0][0] = 0x12;
@@ -1,6 +1,6 @@
1
+ import { Buffer } from 'node:buffer';
1
2
  import { encode } from '../dist/index.js';
2
- import { resetEncoder } from '../dist/encoder.js';
3
- import { toArray } from './.utils.js';
3
+ import { toArray, resetEnv } from './.utils.js';
4
4
 
5
5
  const STR_BYTE_LENGTH = 128 * 1024 * 1024 - 20;
6
6
 
@@ -24,7 +24,7 @@ const [str4Data, str4] = makeString('😀', STR_BYTE_LENGTH);
24
24
  // bad surrogate pair
25
25
  const [strXData, strX] = makeString('😀'[0], STR_BYTE_LENGTH);
26
26
 
27
- const strErr = 'a'.repeat(128 * 1024 * 1024);
27
+ const strErr = 'a'.repeat(128 * 1024 * 1024 + 1);
28
28
 
29
29
  const ENCODED_PREFIX_LENGTH = 6;
30
30
 
@@ -70,17 +70,15 @@ describe(`encode huge string`, () => {
70
70
  });
71
71
  });
72
72
  describe(`without node buffer`, () => {
73
- let buffer = Buffer;
74
73
  beforeAll(() => {
75
- buffer = global.Buffer;
76
74
  // @ts-expect-error remove buffer
77
- delete global.Buffer;
78
- resetEncoder();
75
+ global.Buffer = undefined;
76
+ resetEnv();
79
77
  });
80
78
 
81
79
  afterAll(() => {
82
- global.Buffer = buffer;
83
- resetEncoder();
80
+ global.Buffer = Buffer;
81
+ resetEnv();
84
82
  });
85
83
 
86
84
  it(`~128M [1]`, () => {
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Tests from https://bitbucket.org/shelacek/ubjson
3
3
  */
4
- import { firstValueFrom, of, reduce, Subject } from 'rxjs';
4
+ import { firstValueFrom, of, map, reduce, Subject, toArray as rxjsToArray } from 'rxjs';
5
5
  import { encode } from '../../dist/rxjs/index.js';
6
6
  import { decode, encode as encodeRef } from '../../dist/index.js';
7
7
  import { toArray } from '../.utils.js';
@@ -14,7 +14,8 @@ function encodeAsync(value) {
14
14
  return firstValueFrom(
15
15
  of(value).pipe(
16
16
  encode(),
17
- reduce((a, b) => Buffer.concat([a, b])),
17
+ rxjsToArray(),
18
+ map((data) => Buffer.concat(data)),
18
19
  ),
19
20
  );
20
21
  }
@@ -357,28 +358,13 @@ test('encode huge typed array (~128M)', async () => {
357
358
  );
358
359
  });
359
360
 
360
- test('encode huge typed array (256M + 1) [error]', async () => {
361
- const obj = new Uint8Array(256 * 1024 * 1024 + 1);
362
- await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
363
- });
364
-
365
- test('encode huge typed array (3G) [error]', async () => {
366
- const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
367
- await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
368
- });
369
-
370
361
  test('encode huge data (~128M)', async () => {
371
362
  const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
372
- expect(toArray((await encodeAsync(obj)).slice(0, 11))).toEqual(
363
+ expect(toArray((await encodeAsync(obj)).subarray(0, 11))).toEqual(
373
364
  toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0),
374
365
  );
375
366
  });
376
367
 
377
- test('encode huge data (256M + 1) [error]', async () => {
378
- const obj = [new Uint8Array(256 * 1024 * 1024 + 1)];
379
- await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
380
- });
381
-
382
368
  test('encode stream', async () => {
383
369
  const stream = new Subject();
384
370
  const result = firstValueFrom(
@@ -354,16 +354,6 @@ test('encode huge typed array (~128M)', async () => {
354
354
  );
355
355
  });
356
356
 
357
- test('encode huge typed array (256M + 1) [error]', async () => {
358
- const obj = new Uint8Array(256 * 1024 * 1024 + 1);
359
- await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
360
- });
361
-
362
- test('encode huge typed array (3G) [error]', async () => {
363
- const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
364
- await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
365
- });
366
-
367
357
  test('encode huge data (~128M)', async () => {
368
358
  const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
369
359
  expect(toArray((await encodeAsync(obj)).subarray(0, 11))).toEqual(
@@ -371,11 +361,6 @@ test('encode huge data (~128M)', async () => {
371
361
  );
372
362
  });
373
363
 
374
- test('encode huge data (256M + 1) [error]', async () => {
375
- const obj = [new Uint8Array(256 * 1024 * 1024 + 1)];
376
- await expect(() => encodeAsync(obj)).rejects.toThrow(/Buffer has exceed max size/);
377
- });
378
-
379
364
  test('encode stream', async () => {
380
365
  const stream = encoder();
381
366
  stream.write(undefined);
@@ -1,5 +1,5 @@
1
1
  import { Encoder } from '../dist/encoder.js';
2
- import { decode, decodeKey, decodeJs } from '../dist/common/string-decoder.js';
2
+ import { decode, decodeKey, jsDecode } from '../dist/common/string-decoder.js';
3
3
  import '../dist/common/constants.js';
4
4
 
5
5
  /**
@@ -126,11 +126,12 @@ test('decode string js', () => {
126
126
  testEncoding(new TextEncoder(), {
127
127
  decode(buffer) {
128
128
  if (!(buffer instanceof Uint8Array)) return '';
129
- return decodeJs(buffer, 0, buffer.byteLength);
129
+ return jsDecode(buffer, 0, buffer.byteLength);
130
130
  },
131
131
  });
132
132
  });
133
133
 
134
134
  test('decode malformed', () => {
135
135
  expect(decode(new Uint8Array([0xff, 'a'.charCodeAt(0)]), 0, 2)).toEqual('\uFFFDa');
136
+ expect(jsDecode(new Uint8Array([0xff, 'a'.charCodeAt(0)]), 0, 2)).toEqual('\uFFFDa');
136
137
  });
@@ -3,6 +3,7 @@
3
3
  "include": ["./"],
4
4
  "compilerOptions": {
5
5
  "checkJs": true,
6
- "noEmit": true
6
+ "noEmit": true,
7
+ "noUncheckedIndexedAccess": false
7
8
  }
8
9
  }
package/tests/e2e.js DELETED
@@ -1,415 +0,0 @@
1
- /**
2
- * Tests from https://bitbucket.org/shelacek/ubjson
3
- */
4
- import { encode, decode } from '../dist/index.js';
5
- import { resetEncoder } from '../dist/encoder.js';
6
-
7
- test('encode/decode complex object', () => {
8
- const expected = {
9
- hello: 'world',
10
- from: ['UBJSON'],
11
- colors: [[255, 255, 255], [0, 0, 0], [64, 64, 96], new Uint8Array([255, 0, 1, 2, 3, 127])],
12
- domains: {
13
- '': '',
14
- com: 'commercial',
15
- org: 'organization',
16
- net: 'network',
17
- },
18
- entires: [
19
- {
20
- id: 1,
21
- name: 'test',
22
- content: null,
23
- timestamp: 1_532_432_408.008,
24
- published: false,
25
- },
26
- {
27
- id: 2,
28
- name: 'lorem',
29
- content: 'Lorem ipsum...',
30
- timestamp: 1_532_432_416.346,
31
- published: true,
32
- },
33
- ],
34
- plots: {
35
- traces: [
36
- {
37
- name: 'test',
38
- x: new Float64Array([
39
- 1,
40
- 2,
41
- 3,
42
- Number.NEGATIVE_INFINITY,
43
- Number.POSITIVE_INFINITY,
44
- Number.NaN,
45
- Number.MAX_VALUE,
46
- Number.MIN_VALUE,
47
- ]),
48
- y: new Int8Array([0, 4, 5, 6, -128, -1, 127]),
49
- },
50
- {
51
- name: 'test2',
52
- x: new Float32Array([
53
- 1,
54
- 2,
55
- 3,
56
- Number.NEGATIVE_INFINITY,
57
- Number.POSITIVE_INFINITY,
58
- Number.NaN,
59
- Number.MAX_VALUE,
60
- Number.MIN_VALUE,
61
- ]),
62
- y: new Int16Array([4, 5, 6, -32768, 32767]),
63
- },
64
- {
65
- name: 'test233',
66
- x: new Int32Array([1, 2, 3, 0, -1]),
67
- y: new Int32Array([4, 5, 6, -(2 ** 31), 2 ** 31 - 1]),
68
- },
69
- ],
70
- },
71
- description1: 'medium text'.repeat(100),
72
- description2: 'medium text'.repeat(1000),
73
- document1: 'long text'.repeat(10000),
74
- document2: 'long text'.repeat(100_000),
75
- };
76
- const encoded = encode(expected);
77
- const actual = decode(Buffer.from(encoded));
78
- expect(actual).toEqual(expected);
79
- });
80
-
81
- test('encode/decode model', () => {
82
- const expected = {
83
- rid: 'project/CloudPSS/_SubSysPort',
84
- name: '模块端口',
85
- description: '',
86
- tags: [
87
- 'project:component',
88
- 'type:30000',
89
- 'project-group:模块-基础:101',
90
- 'project-category:component',
91
- 'project-support:job-definition/cloudpss/emtp',
92
- 'project-support:job-definition/cloudpss/sfemt',
93
- 'project-support:job-definition/cloudpss/power-flow',
94
- ],
95
- revision: {
96
- graphic: {
97
- pins: {
98
- input: {
99
- position: {
100
- x: 0.999_999_9,
101
- y: 0.5,
102
- },
103
- },
104
- output: {
105
- position: {
106
- x: 0,
107
- y: 0.5,
108
- },
109
- },
110
- electrical: {
111
- position: {
112
- x: 0.999_999_9,
113
- y: 0.5,
114
- },
115
- },
116
- },
117
- attrs: {
118
- p0: {
119
- fill: 'var(--fill)',
120
- stroke: 'var(--stroke)',
121
- refPath: {
122
- d: 'm90000 0h-60000a10000 10000 0 0 0 0 20000h60000zm0 10000h20000',
123
- h: 20000,
124
- w: 110_000,
125
- },
126
- fillOpacity: 'var(--fill-opacity)',
127
- strokeWidth: 'var(--stroke-width)',
128
- strokeOpacity: 'var(--stroke-opacity)',
129
- },
130
- p1: {
131
- fill: 'var(--fill)',
132
- stroke: 'var(--stroke)',
133
- refPath: {
134
- d: 'm90000 0h-60000a10000 10000 0 0 0 0 20000h60000zm0 10000h20000',
135
- h: 20000,
136
- w: 110_000,
137
- },
138
- fillOpacity: 'var(--fill-opacity)',
139
- strokeWidth: 'var(--stroke-width)',
140
- strokeOpacity: 'var(--stroke-opacity)',
141
- },
142
- p2: {
143
- fill: 'transparent',
144
- stroke: 'var(--stroke)',
145
- refPath: {
146
- d: 'm20000 10000m-5000 -5000l5000 5000l-5000 5000',
147
- h: 20000,
148
- w: 110_000,
149
- },
150
- fillOpacity: 'var(--fill-opacity)',
151
- strokeWidth: 'var(--stroke-width)',
152
- strokeOpacity: 'var(--stroke-opacity)',
153
- },
154
- p3: {
155
- fill: 'var(--fill)',
156
- stroke: 'var(--stroke)',
157
- refPath: {
158
- d: 'm20000 0h60000a10000 10000 0 0 1 0 20000h-60000zm0 10000h-20000',
159
- h: 20000,
160
- w: 110_000,
161
- },
162
- fillOpacity: 'var(--fill-opacity)',
163
- strokeWidth: 'var(--stroke-width)',
164
- strokeOpacity: 'var(--stroke-opacity)',
165
- },
166
- p4: {
167
- fill: 'transparent',
168
- stroke: 'var(--stroke)',
169
- refPath: {
170
- d: 'm110000 10000m-5000 -5000l5000 5000l-5000 5000',
171
- h: 20000,
172
- w: 110_000,
173
- },
174
- fillOpacity: 'var(--fill-opacity)',
175
- strokeWidth: 'var(--stroke-width)',
176
- strokeOpacity: 'var(--stroke-opacity)',
177
- },
178
- t0: {
179
- fill: 'var(--spectrum-global-color-orange-600)',
180
- refX: 0.5,
181
- refY: 0.5,
182
- stroke: 'transparent',
183
- fontSize: 12,
184
- textAnchor: 'middle',
185
- },
186
- },
187
- width: 110,
188
- height: 20,
189
- markup: [
190
- {
191
- tagName: 'path',
192
- selector: 'p0',
193
- condition:
194
- "=finder(p, index, array)= equalText(p.key, Key);\n p = $$.revision.pins.find(finder);\n p == undefined ? true : (not equalText(p.connection, 'input') and not equalText(p.connection, 'output'))",
195
- },
196
- {
197
- tagName: 'path',
198
- selector: 'p1',
199
- condition:
200
- "=finder(p, index, array) = equalText(p.key, Key);\n p = $$.revision.pins.find(finder);\n p != undefined ? equalText(p.connection, 'input') :false",
201
- },
202
- {
203
- tagName: 'path',
204
- selector: 'p2',
205
- condition:
206
- "=finder(p, index, array) = equalText(p.key, Key);\n p = $$.revision.pins.find(finder);\n p != undefined ? equalText(p.connection, 'output') : false",
207
- },
208
- {
209
- tagName: 'path',
210
- selector: 'p3',
211
- condition:
212
- "=finder(p, index, array) = equalText(p.key, Key);\n p = $$.revision.pins.find(finder);\n p != undefined ? equalText(p.connection, 'output') : false",
213
- },
214
- {
215
- tagName: 'path',
216
- selector: 'p4',
217
- condition:
218
- "=finder(p, index, array) = equalText(p.key, Key);\n p = $$.revision.pins.find(finder);\n p != undefined ? equalText(p.connection, 'input') :false",
219
- },
220
- {
221
- attrs: {
222
- y: '0.35em',
223
- },
224
- tagName: 'text',
225
- selector: 't0',
226
- condition: '',
227
- textContent: 'Port: $Key',
228
- },
229
- ],
230
- generator: 'x6',
231
- },
232
- parameters: [
233
- {
234
- name: 'Configuration',
235
- items: [
236
- {
237
- key: 'Key',
238
- name: 'Pin Key',
239
- type: 'choice',
240
- value: '',
241
- choices:
242
- '=mapper(p,index,array) = {key: p.key, name: p.key, description: p.description};\n$$.revision.pins.map(mapper)',
243
- condition: 'true',
244
- description: '绑定端口',
245
- },
246
- ],
247
- condition: 'true',
248
- description: 'Configuration',
249
- },
250
- ],
251
- pins: [
252
- {
253
- dim: [
254
- "=finder(p, index, array) = equalText(p.key, 'a');\np = $$.revision.pins.find(finder);\np!=undefined?p.dim[1]:''",
255
- "=finder(p, index, array) = equalText(p.key, 'a');\np = $$.revision.pins.find(finder);\np!=undefined?p.dim[2]:''",
256
- ],
257
- key: 'input',
258
- data: 'real',
259
- name: 'Port',
260
- visible: true,
261
- condition:
262
- "=finder(p, index, array) = equalText(p.key, Key);\np = $$.revision.pins.find(finder);\np != undefined ? equalText(p.connection, 'input') :false",
263
- connection: 'output',
264
- },
265
- {
266
- dim: [
267
- "=finder(p, index, array) = equalText(p.key, 'a');\np = $$.revision.pins.find(finder);\np!=undefined?p.dim[2]:''",
268
- "=finder(p, index, array) = equalText(p.key, 'a');\np = $$.revision.pins.find(finder);\np!=undefined?p.dim[2]:''",
269
- ],
270
- key: 'output',
271
- data: 'real',
272
- name: 'Port',
273
- visible: true,
274
- condition:
275
- "=finder(p, index, array) = equalText(p.key, Key);\np = $$.revision.pins.find(finder);\np != undefined ? equalText(p.connection, 'output') :false",
276
- connection: 'input',
277
- },
278
- {
279
- dim: [
280
- "=finder(p, index, array) = equalText(p.key, 'a');\np = $$.revision.pins.find(finder);\np!=undefined?p.dim[2]:''",
281
- "=finder(p, index, array) = equalText(p.key, 'a');\np = $$.revision.pins.find(finder);\np!=undefined?p.dim[2]:''",
282
- ],
283
- key: 'electrical',
284
- data: 'real',
285
- name: 'Port',
286
- visible: true,
287
- condition:
288
- "=finder(p, index, array)= equalText(p.key, Key);\np = $$.revision.pins.find(finder);\np == undefined ? true : (not equalText(p.connection, 'input') and not equalText(p.connection, 'output'))",
289
- connection: 'electrical',
290
- },
291
- ],
292
- documentation: '@[docs](http://docs.cloudpss.net/components/comp_PSS/comp_PSSSystem/BasicComp/SystemPort)',
293
- },
294
- };
295
- const encoded = encode(expected);
296
- const actual = decode(encoded);
297
- expect(actual).toEqual(expected);
298
- });
299
-
300
- test('encode/decode complex object (no encodeInto)', () => {
301
- // eslint-disable-next-line @typescript-eslint/unbound-method
302
- const encodeInto = TextEncoder.prototype.encodeInto;
303
- // @ts-expect-error 移除 encodeInto 以测试兼容性
304
- TextEncoder.prototype.encodeInto = undefined;
305
- resetEncoder();
306
- const expected = {
307
- hello: 'world',
308
- from: ['UBJSON'],
309
- colors: [[255, 255, 255], [0, 0, 0], [64, 64, 96], new Uint8Array([255, 0, 1, 2, 3, 127])],
310
- domains: {
311
- com: 'commercial',
312
- org: 'organization',
313
- net: 'network',
314
- },
315
- entires: [
316
- {
317
- id: 1,
318
- name: 'test',
319
- content: null,
320
- timestamp: 1_532_432_408.008,
321
- published: false,
322
- },
323
- {
324
- id: 2,
325
- name: 'lorem',
326
- content: 'Lorem ipsum...',
327
- timestamp: 1_532_432_416.346,
328
- published: true,
329
- },
330
- ],
331
- plots: {
332
- traces: [
333
- {
334
- name: 'test',
335
- x: new Float64Array([
336
- 1,
337
- 2,
338
- 3,
339
- Number.NEGATIVE_INFINITY,
340
- Number.POSITIVE_INFINITY,
341
- Number.NaN,
342
- Number.MAX_VALUE,
343
- Number.MIN_VALUE,
344
- ]),
345
- y: new Int8Array([0, 4, 5, 6, -128, -1, 127]),
346
- },
347
- {
348
- name: 'test2',
349
- x: new Float32Array([
350
- 1,
351
- 2,
352
- 3,
353
- Number.NEGATIVE_INFINITY,
354
- Number.POSITIVE_INFINITY,
355
- Number.NaN,
356
- Number.MAX_VALUE,
357
- Number.MIN_VALUE,
358
- ]),
359
- y: new Int16Array([4, 5, 6, -32768, 32767]),
360
- },
361
- {
362
- name: 'test233',
363
- x: new Int32Array([1, 2, 3, 0, -1]),
364
- y: new Int32Array([4, 5, 6, -(2 ** 31), 2 ** 31 - 1]),
365
- },
366
- ],
367
- },
368
- description1: 'medium text'.repeat(100),
369
- description2: 'medium text'.repeat(1000),
370
- document1: 'long text'.repeat(10000),
371
- document2: 'long text'.repeat(100_000),
372
- };
373
- const encoded = encode(expected);
374
- const actual = decode(encoded);
375
- expect(actual).toEqual(expected);
376
- TextEncoder.prototype.encodeInto = encodeInto;
377
- resetEncoder();
378
- });
379
-
380
- test('encode/decode large array', () => {
381
- const array = Array.from({ length: 100_000 });
382
- array[12345] = 'item';
383
-
384
- const expected = Array.from({ length: 100_000 }).fill(null);
385
- expected[12345] = 'item';
386
-
387
- expect(decode(encode(array))).toEqual(expected);
388
- });
389
-
390
- test('encode/decode object with undefined values', () => {
391
- const obj = { a: 1, b: undefined, c: { d: 2, e: undefined, f: null } };
392
- expect(decode(encode(obj))).toEqual(obj);
393
- });
394
-
395
- test('decode ArrayBuffer', () => {
396
- const obj = { a: 1, b: undefined, c: { d: 2, e: undefined, f: null } };
397
- expect(decode(encode(obj).buffer)).toEqual(obj);
398
- });
399
-
400
- test('decode Int8Array', () => {
401
- const obj = { a: 1, b: undefined, c: { d: 2, e: undefined, f: null } };
402
- expect(decode(new Int8Array(encode(obj).buffer))).toEqual(obj);
403
- });
404
-
405
- test('decode invalid __proto__', () => {
406
- const obj = decode(encode(JSON.parse('{"__proto__":"xxx"}')));
407
- expect(obj).toEqual(JSON.parse('{"__proto__":"xxx"}'));
408
- expect(Object.getPrototypeOf(obj)).toBe(Object.prototype);
409
- });
410
-
411
- test('decode null __proto__', () => {
412
- const obj = decode(encode(JSON.parse('{"__proto__":null}')));
413
- expect(obj).toEqual(JSON.parse('{"__proto__":null}'));
414
- expect(Object.getPrototypeOf(obj)).toBe(Object.prototype);
415
- });