@cloudpss/ubjson 0.3.6 → 0.3.7

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 (50) hide show
  1. package/benchmark-string.js +18 -20
  2. package/benchmark.js +1 -0
  3. package/dist/{constants.d.ts → common/constants.d.ts} +0 -0
  4. package/dist/{constants.js → common/constants.js} +0 -0
  5. package/dist/common/constants.js.map +1 -0
  6. package/dist/common/decoder.d.ts +48 -0
  7. package/dist/{decoder.js → common/decoder.js} +13 -18
  8. package/dist/common/decoder.js.map +1 -0
  9. package/dist/common/encoder.d.ts +74 -0
  10. package/dist/common/encoder.js +282 -0
  11. package/dist/common/encoder.js.map +1 -0
  12. package/dist/common/string-decoder.d.ts +13 -0
  13. package/dist/common/string-decoder.js +106 -0
  14. package/dist/common/string-decoder.js.map +1 -0
  15. package/dist/{string-encoder.d.ts → common/string-encoder.d.ts} +0 -0
  16. package/dist/{string-encoder.js → common/string-encoder.js} +0 -0
  17. package/dist/common/string-encoder.js.map +1 -0
  18. package/dist/encoder.d.ts +10 -2
  19. package/dist/encoder.js +2 -283
  20. package/dist/encoder.js.map +1 -1
  21. package/dist/index.d.ts +4 -2
  22. package/dist/index.js +12 -2
  23. package/dist/index.js.map +1 -1
  24. package/dist/stream/encoder.d.ts +14 -0
  25. package/dist/stream/encoder.js +90 -0
  26. package/dist/stream/encoder.js.map +1 -0
  27. package/dist/stream/index.d.ts +4 -0
  28. package/dist/stream/index.js +7 -0
  29. package/dist/stream/index.js.map +1 -0
  30. package/package.json +14 -6
  31. package/src/{constants.ts → common/constants.ts} +0 -0
  32. package/src/{decoder.ts → common/decoder.ts} +13 -18
  33. package/src/common/encoder.ts +305 -0
  34. package/src/common/string-decoder.ts +107 -0
  35. package/src/{string-encoder.ts → common/string-encoder.ts} +0 -0
  36. package/src/encoder.ts +4 -305
  37. package/src/index.ts +14 -2
  38. package/src/stream/encoder.ts +87 -0
  39. package/src/stream/index.ts +8 -0
  40. package/test/encode-stream.js +450 -0
  41. package/test/string-encoding.js +13 -3
  42. package/test.js +8 -0
  43. package/dist/constants.js.map +0 -1
  44. package/dist/decoder.d.ts +0 -2
  45. package/dist/decoder.js.map +0 -1
  46. package/dist/string-decoder.d.ts +0 -9
  47. package/dist/string-decoder.js +0 -67
  48. package/dist/string-decoder.js.map +0 -1
  49. package/dist/string-encoder.js.map +0 -1
  50. package/src/string-decoder.ts +0 -69
@@ -0,0 +1,450 @@
1
+ /**
2
+ * Tests from https://bitbucket.org/shelacek/ubjson
3
+ */
4
+
5
+ import test from 'tape';
6
+ import * as ubjsonBase from '../dist/index.js';
7
+ import * as ubjsonStream from '../dist/stream/index.js';
8
+
9
+ const ubjson = {
10
+ decode: ubjsonBase.decode,
11
+ encode: (value) => {
12
+ const stream = ubjsonStream.encode(value);
13
+ let ex;
14
+ stream.on('error', (e) => (ex = e));
15
+ let buffer = Buffer.alloc(0);
16
+ while (buffer) {
17
+ const chunk = stream.read();
18
+ if (ex) throw ex;
19
+ if (chunk === null) break;
20
+ buffer = Buffer.concat([buffer, chunk]);
21
+ }
22
+ return buffer;
23
+ },
24
+ };
25
+
26
+ function toArray(...args) {
27
+ if (args[0] instanceof ArrayBuffer) {
28
+ return Array.from(new Uint8Array(args[0]));
29
+ }
30
+ if (args[0] instanceof Uint8Array) {
31
+ return Array.from(args[0]);
32
+ }
33
+ return args.map((x) => (x === +x ? x : x.charCodeAt()));
34
+ }
35
+
36
+ test('encode function', (t) => {
37
+ t.throws(() => ubjson.encode(function () {}));
38
+ t.end();
39
+ });
40
+
41
+ test('encode bigint', (t) => {
42
+ t.throws(() => ubjson.encode(1n));
43
+ t.end();
44
+ });
45
+
46
+ test('encode symbol', (t) => {
47
+ t.throws(() => ubjson.encode(Symbol('sym')));
48
+ t.end();
49
+ });
50
+
51
+ test('encode undefined', (t) => {
52
+ t.deepEqual(toArray(ubjson.encode(undefined)), toArray('N'));
53
+ t.end();
54
+ });
55
+
56
+ test('encode null', (t) => {
57
+ t.deepEqual(toArray(ubjson.encode(null)), toArray('Z'));
58
+ t.end();
59
+ });
60
+
61
+ test('encode true', (t) => {
62
+ t.deepEqual(toArray(ubjson.encode(true)), toArray('T'));
63
+ t.end();
64
+ });
65
+
66
+ test('encode false', (t) => {
67
+ t.deepEqual(toArray(ubjson.encode(false)), toArray('F'));
68
+ t.end();
69
+ });
70
+
71
+ test('encode int8', (t) => {
72
+ t.deepEqual(toArray(ubjson.encode(-1)), toArray('i', 255));
73
+ t.end();
74
+ });
75
+
76
+ test('encode uint8', (t) => {
77
+ t.deepEqual(toArray(ubjson.encode(200)), toArray('U', 200));
78
+ t.end();
79
+ });
80
+
81
+ test('encode int16', (t) => {
82
+ t.deepEqual(toArray(ubjson.encode(0x1234)), toArray('I', 0x12, 0x34));
83
+ t.end();
84
+ });
85
+
86
+ test('encode int32', (t) => {
87
+ t.deepEqual(toArray(ubjson.encode(0x12345678)), toArray('l', 0x12, 0x34, 0x56, 0x78));
88
+ t.end();
89
+ });
90
+
91
+ test('encode float32', (t) => {
92
+ t.deepEqual(toArray(ubjson.encode(1.00390625)), toArray('d', 0x3f, 0x80, 0x80, 0x00));
93
+ t.end();
94
+ });
95
+
96
+ test('encode float32 (too large integer)', (t) => {
97
+ t.deepEqual(toArray(ubjson.encode(2147483648)), toArray('d', 0x4f, 0x00, 0x00, 0x00));
98
+ t.end();
99
+ });
100
+
101
+ test('encode float64', (t) => {
102
+ t.deepEqual(toArray(ubjson.encode(100000.00390625)), toArray('D', 0x40, 0xf8, 0x6a, 0x00, 0x10, 0x00, 0x00, 0x00));
103
+ t.end();
104
+ });
105
+
106
+ test('encode char', (t) => {
107
+ t.deepEqual(toArray(ubjson.encode('a')), toArray('C', 'a'));
108
+ t.end();
109
+ });
110
+
111
+ test('encode char 128', (t) => {
112
+ t.deepEqual(toArray(ubjson.encode('\xcc')), toArray('C', '\xcc'));
113
+ t.end();
114
+ });
115
+
116
+ test('encode char 257', (t) => {
117
+ t.deepEqual(toArray(ubjson.encode('\u0123')), toArray('S', 'i', 2, 196, 163));
118
+ t.end();
119
+ });
120
+
121
+ test('encode char emoji', (t) => {
122
+ t.deepEqual(toArray(ubjson.encode('💖')), toArray('S', 'i', 4, 240, 159, 146, 150));
123
+ t.end();
124
+ });
125
+
126
+ test('encode string', (t) => {
127
+ t.deepEqual(toArray(ubjson.encode('ubjson')), toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
128
+ t.end();
129
+ });
130
+
131
+ test('encode string (no encodeInto)', (t) => {
132
+ const encodeInto = TextEncoder.prototype.encodeInto;
133
+ TextEncoder.prototype.encodeInto = undefined;
134
+ t.deepEqual(toArray(ubjson.encode('ubjson')), toArray('S', 'i', 6, 'u', 'b', 'j', 's', 'o', 'n'));
135
+ t.end();
136
+ TextEncoder.prototype.encodeInto = encodeInto;
137
+ });
138
+
139
+ test('encode array', (t) => {
140
+ t.deepEqual(toArray(ubjson.encode([1, 2, 3, -1])), toArray('[', 'U', 1, 'U', 2, 'U', 3, 'i', 255, ']'));
141
+ t.end();
142
+ });
143
+
144
+ test('encode array (empty)', (t) => {
145
+ t.deepEqual(toArray(ubjson.encode([])), toArray('[', ']'));
146
+ t.end();
147
+ });
148
+
149
+ test('encode array (undefined)', (t) => {
150
+ t.deepEqual(toArray(ubjson.encode([undefined])), toArray('[', 'Z', ']'));
151
+ t.end();
152
+ });
153
+
154
+ test('encode array (spares)', (t) => {
155
+ const array = new Array(3);
156
+ array[1] = true;
157
+ t.deepEqual(toArray(ubjson.encode(array)), toArray('[', 'Z', 'T', 'Z', ']'));
158
+ t.end();
159
+ });
160
+
161
+ test('encode array (mixed)', (t) => {
162
+ t.deepEqual(toArray(ubjson.encode([1, 'a', true])), toArray('[', 'U', 1, 'C', 'a', 'T', ']'));
163
+ t.end();
164
+ });
165
+
166
+ test('encode array (int8)', (t) => {
167
+ t.deepEqual(toArray(ubjson.encode([-1, 2, 3])), toArray('[', 'i', 255, 'U', 2, 'U', 3, ']'));
168
+ t.end();
169
+ });
170
+
171
+ test('encode array (int16)', (t) => {
172
+ t.deepEqual(toArray(ubjson.encode([255, -1])), toArray('[', 'U', 0xff, 'i', 0xff, ']'));
173
+ t.end();
174
+ });
175
+
176
+ test('encode array (only null values)', (t) => {
177
+ t.deepEqual(toArray(ubjson.encode([null, null, null])), toArray('[', 'Z', 'Z', 'Z', ']'));
178
+ t.end();
179
+ });
180
+
181
+ test('encode N-D array', (t) => {
182
+ t.deepEqual(
183
+ ubjson.decode(
184
+ ubjson.encode([
185
+ [1, 2, 3],
186
+ [4, 5, 6],
187
+ ]),
188
+ ),
189
+ [
190
+ [1, 2, 3],
191
+ [4, 5, 6],
192
+ ],
193
+ );
194
+ t.end();
195
+ });
196
+
197
+ test('encode array of objects', (t) => {
198
+ t.deepEqual(
199
+ ubjson.decode(
200
+ ubjson.encode([
201
+ { a: 1, b: 2, c: 3 },
202
+ { d: 4, e: 5, f: 6 },
203
+ ]),
204
+ ),
205
+ [
206
+ { a: 1, b: 2, c: 3 },
207
+ { d: 4, e: 5, f: 6 },
208
+ ],
209
+ );
210
+ t.end();
211
+ });
212
+
213
+ test('encode array of objects of arrays', (t) => {
214
+ t.deepEqual(
215
+ ubjson.decode(
216
+ ubjson.encode([
217
+ { a: [1, 2], b: [3, 4] },
218
+ { c: [5, 6], d: [7, 8] },
219
+ ]),
220
+ ),
221
+ [
222
+ { a: [1, 2], b: [3, 4] },
223
+ { c: [5, 6], d: [7, 8] },
224
+ ],
225
+ );
226
+ t.end();
227
+ });
228
+
229
+ test('encode array (int8 typed array)', (t) => {
230
+ t.deepEqual(toArray(ubjson.encode(Int8Array.from([18, -2]))), toArray('[', '$', 'i', '#', 'i', 2, 0x12, 0xfe));
231
+ t.end();
232
+ });
233
+
234
+ test('encode array (uint8 typed array)', (t) => {
235
+ t.deepEqual(toArray(ubjson.encode(Uint8Array.from([18, 254]))), toArray('[', '$', 'U', '#', 'i', 2, 0x12, 0xfe));
236
+ t.end();
237
+ });
238
+
239
+ test('encode array (int16 typed array)', (t) => {
240
+ t.deepEqual(
241
+ toArray(ubjson.encode(Int16Array.from([4660, -292]))),
242
+ toArray('[', '$', 'I', '#', 'i', 2, 0x12, 0x34, 0xfe, 0xdc),
243
+ );
244
+ t.end();
245
+ });
246
+
247
+ test('encode array (int32 typed array)', (t) => {
248
+ t.deepEqual(
249
+ toArray(ubjson.encode(Int32Array.from([305419896, -19088744]))),
250
+ toArray('[', '$', 'l', '#', 'i', 2, 0x12, 0x34, 0x56, 0x78, 0xfe, 0xdc, 0xba, 0x98),
251
+ );
252
+ t.end();
253
+ });
254
+
255
+ test('encode array (float32 typed array)', (t) => {
256
+ t.deepEqual(
257
+ toArray(ubjson.encode(Float32Array.from([0.25, 0.125]))),
258
+ toArray('[', '$', 'd', '#', 'i', 2, 0x3e, 0x80, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00),
259
+ );
260
+ t.end();
261
+ });
262
+
263
+ test('encode array (float64 typed array)', (t) => {
264
+ t.deepEqual(
265
+ toArray(ubjson.encode(Float64Array.from([0.25, 0.125]))),
266
+ toArray(
267
+ '[',
268
+ '$',
269
+ 'D',
270
+ '#',
271
+ 'i',
272
+ 2,
273
+ 0x3f,
274
+ 0xd0,
275
+ 0x00,
276
+ 0x00,
277
+ 0x00,
278
+ 0x00,
279
+ 0x00,
280
+ 0x00,
281
+ 0x3f,
282
+ 0xc0,
283
+ 0x00,
284
+ 0x00,
285
+ 0x00,
286
+ 0x00,
287
+ 0x00,
288
+ 0x00,
289
+ ),
290
+ );
291
+ t.end();
292
+ });
293
+
294
+ test('encode array (uint8clamped typed array)', (t) => {
295
+ t.throws(() => {
296
+ ubjson.encode(new Uint8ClampedArray());
297
+ });
298
+ t.end();
299
+ });
300
+
301
+ test('encode array (uint16 typed array)', (t) => {
302
+ t.throws(() => {
303
+ ubjson.encode(new Uint16Array());
304
+ });
305
+ t.end();
306
+ });
307
+
308
+ test('encode array (uint32 typed array)', (t) => {
309
+ t.throws(() => {
310
+ ubjson.encode(new Uint32Array());
311
+ });
312
+ t.end();
313
+ });
314
+
315
+ test('encode array (uint64 typed array)', (t) => {
316
+ t.throws(() => {
317
+ ubjson.encode(new BigUint64Array());
318
+ });
319
+ t.end();
320
+ });
321
+
322
+ test('encode array (int64 typed array)', (t) => {
323
+ t.throws(() => {
324
+ ubjson.encode(new BigInt64Array());
325
+ });
326
+ t.end();
327
+ });
328
+
329
+ test('encode object', (t) => {
330
+ t.deepEqual(
331
+ toArray(ubjson.encode({ a: 1, b: 2, c: 3 })),
332
+ toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'U', 2, 'i', 1, 'c', 'U', 3, '}'),
333
+ );
334
+ t.end();
335
+ });
336
+
337
+ test('encode object (empty)', (t) => {
338
+ t.deepEqual(toArray(ubjson.encode({})), toArray('{', '}'));
339
+ t.end();
340
+ });
341
+
342
+ test('encode object (empty key)', (t) => {
343
+ t.deepEqual(toArray(ubjson.encode({ '': '' })), toArray('{', 'i', 0, 'S', 'i', 0, '}'));
344
+ t.end();
345
+ });
346
+
347
+ test('encode object (mixed)', (t) => {
348
+ t.deepEqual(
349
+ toArray(ubjson.encode({ a: 1, b: 'a', c: true })),
350
+ toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
351
+ );
352
+ t.end();
353
+ });
354
+
355
+ test('encode object (only null values)', (t) => {
356
+ t.deepEqual(
357
+ toArray(ubjson.encode({ a: null, b: null, c: null })),
358
+ toArray('{', 'i', 1, 'a', 'Z', 'i', 1, 'b', 'Z', 'i', 1, 'c', 'Z', '}'),
359
+ );
360
+ t.end();
361
+ });
362
+
363
+ test('encode object (skip prototype)', (t) => {
364
+ const obj = Object.create({ a: 2, x: 'xx' });
365
+ obj.a = 1;
366
+ obj.b = 'a';
367
+ obj.c = true;
368
+ t.deepEqual(
369
+ toArray(ubjson.encode(obj)),
370
+ toArray('{', 'i', 1, 'a', 'U', 1, 'i', 1, 'b', 'C', 'a', 'i', 1, 'c', 'T', '}'),
371
+ );
372
+ t.end();
373
+ });
374
+
375
+ test('encode object (skip symbol)', (t) => {
376
+ const obj = { [Symbol()]: true, a: 1 };
377
+ t.deepEqual(toArray(ubjson.encode(obj)), toArray('{', 'i', 1, 'a', 'U', 1, '}'));
378
+ t.end();
379
+ });
380
+
381
+ test('encode object (skip non-enumerable)', (t) => {
382
+ const obj = {};
383
+ Object.defineProperty(obj, 'a', { value: 1, configurable: true, writable: true });
384
+ t.deepEqual(toArray(ubjson.encode(obj)), toArray('{', '}'));
385
+ t.end();
386
+ });
387
+
388
+ test('encode object (include getter)', (t) => {
389
+ const obj = {};
390
+ Object.defineProperty(obj, 'a', { get: () => 1, enumerable: true });
391
+ t.deepEqual(toArray(ubjson.encode(obj)), toArray('{', 'i', 1, 'a', 'U', 1, '}'));
392
+ t.end();
393
+ });
394
+
395
+ test('encode object (skip undefined)', (t) => {
396
+ const obj = { a: undefined };
397
+ t.deepEqual(toArray(ubjson.encode(obj)), toArray('{', '}'));
398
+ t.end();
399
+ });
400
+
401
+ test('encode object (function) [error]', (t) => {
402
+ const obj = { a: () => {} };
403
+ t.throws(() => ubjson.encode(obj), /Unsupported type \[object Function\]/);
404
+ t.end();
405
+ });
406
+
407
+ test('encode object (include null)', (t) => {
408
+ const obj = { a: null };
409
+ t.deepEqual(toArray(ubjson.encode(obj)), toArray('{', 'i', 1, 'a', 'Z', '}'));
410
+ t.end();
411
+ });
412
+
413
+ test('encode huge typed array (16K)', (t) => {
414
+ const obj = new Uint8Array(16 * 1024);
415
+ t.deepEqual(toArray(ubjson.encode(obj).slice(0, 8)), toArray('[', '$', 'U', '#', 'I', 0x40, 0x00, 0));
416
+ t.end();
417
+ });
418
+
419
+ test('encode huge typed array (~128M)', (t) => {
420
+ const obj = new Uint8Array(128 * 1024 * 1024 - 10);
421
+ t.deepEqual(toArray(ubjson.encode(obj).slice(0, 10)), toArray('[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xf6, 0));
422
+ t.end();
423
+ });
424
+
425
+ test('encode huge typed array (128M) [error]', (t) => {
426
+ const obj = new Uint8Array(128 * 1024 * 1024);
427
+ t.throws(() => ubjson.encode(obj), /Buffer has exceed max size/);
428
+ t.end();
429
+ });
430
+
431
+ test('encode huge typed array (3G) [error]', (t) => {
432
+ const obj = new Uint8Array(3 * 1024 * 1024 * 1024);
433
+ t.throws(() => ubjson.encode(obj), /Buffer has exceed max size/);
434
+ t.end();
435
+ });
436
+
437
+ test('encode huge data (~128M)', (t) => {
438
+ const obj = [new Uint8Array(128 * 1024 * 1024 - 20)];
439
+ t.deepEqual(
440
+ toArray(ubjson.encode(obj).slice(0, 11)),
441
+ toArray('[', '[', '$', 'U', '#', 'l', 0x7, 0xff, 0xff, 0xec, 0),
442
+ );
443
+ t.end();
444
+ });
445
+
446
+ test('encode huge data (256M + 1) [error]', (t) => {
447
+ const obj = [new Uint8Array(256 * 1024 * 1024 + 1)];
448
+ t.throws(() => ubjson.encode(obj), /Buffer has exceed max size/);
449
+ t.end();
450
+ });
@@ -1,6 +1,6 @@
1
1
  import test from 'tape';
2
- import { StringEncoder } from '../dist/string-encoder.js';
3
- import { StringDecoder } from '../dist/string-decoder.js';
2
+ import { StringEncoder } from '../dist/common/string-encoder.js';
3
+ import { StringDecoder } from '../dist/common/string-decoder.js';
4
4
 
5
5
  function testEncoding(t, encoder, decoder) {
6
6
  t.equal(decoder.decode(encoder.encode('')), '');
@@ -12,6 +12,8 @@ function testEncoding(t, encoder, decoder) {
12
12
  t.equal(decoder.decode(encoder.encode('123465')), '123465');
13
13
  t.equal(decoder.decode(encoder.encode('1234651')), '1234651');
14
14
  t.equal(decoder.decode(encoder.encode('1234651')), '1234651');
15
+ t.equal(decoder.decode(encoder.encode('abc 你好 😊🤣')), 'abc 你好 😊🤣');
16
+ t.equal(decoder.decode(encoder.encode('abc 你好 😊🤣'.repeat(1000))), 'abc 你好 😊🤣'.repeat(1000));
15
17
 
16
18
  {
17
19
  // 检查所有单字节
@@ -57,6 +59,14 @@ test('encode string', (t) => {
57
59
  });
58
60
 
59
61
  test('decode string', (t) => {
60
- testEncoding(t, new TextEncoder(), new StringDecoder());
62
+ testEncoding(
63
+ t,
64
+ new TextEncoder(),
65
+ new (class extends StringDecoder {
66
+ decode(buffer) {
67
+ return super.decode(buffer, 0, buffer.byteLength);
68
+ }
69
+ })(),
70
+ );
61
71
  t.end();
62
72
  });
package/test.js ADDED
@@ -0,0 +1,8 @@
1
+ import { encode } from './dist/stream/index.js';
2
+ import { setTimeout } from 'node:timers/promises';
3
+
4
+ const s = encode(1);
5
+ await setTimeout(100);
6
+ console.log(s.read());
7
+ console.log(s.read());
8
+ console.log(s.read());
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,2BAA2B;AAE3B,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACzC,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACzC,MAAM,CAAC,MAAM,qBAAqB,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvD,MAAM,CAAC,MAAM,IAAI,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACtC,MAAM,CAAC,MAAM,MAAM,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAExC,MAAM,CAAC,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC3C,MAAM,CAAC,MAAM,MAAM,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AACxC,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAE5C,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC7C,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC"}
package/dist/decoder.d.ts DELETED
@@ -1,2 +0,0 @@
1
- /** 解码 */
2
- export declare function decode(value: Uint8Array): unknown;
@@ -1 +0,0 @@
1
- {"version":3,"file":"decoder.js","sourceRoot":"","sources":["../src/decoder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,cAAc;AACd,MAAM,OAAO;IAIT,YAAqB,IAAgB;QAAhB,SAAI,GAAJ,IAAI,CAAY;QAFrC,cAAc;QACN,WAAM,GAAG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AC,MAAM,CAAC,CAAC;IACtC,CAAC;CACJ;AAED,SAAS;AACT,MAAM,UAAU,MAAM,CAAC,KAAiB;IACpC,MAAM,OAAO,GAAG,IAAI,OAAO,CAAC,KAAK,CAAC,CAAC;IACnC,OAAO,OAAO,CAAC,MAAM,EAAE,CAAC;AAC5B,CAAC"}
@@ -1,9 +0,0 @@
1
- export declare const textDecoder: TextDecoder;
2
- export declare const decodeAscii: (array: Uint8Array) => string;
3
- /** 特别优化字符串解码速度 */
4
- export declare class StringDecoder {
5
- /** 小字符串缓存 */
6
- private readonly cache;
7
- /** 字符串解码 */
8
- decode(data: Uint8Array): string;
9
- }
@@ -1,67 +0,0 @@
1
- export const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: false });
2
- export const decodeAscii = (() => {
3
- try {
4
- const fastDecodeAscii = String.fromCharCode.apply.bind(String.fromCharCode, String);
5
- // 检查能否直接向 `String.fromCharCode.apply` 传入 `Uint8Array`
6
- if ('\0\x01\x7f' === fastDecodeAscii(Uint8Array.from([0, 1, 0x7f])))
7
- return fastDecodeAscii;
8
- /* c8 ignore next 1 */
9
- }
10
- catch {
11
- /* c8 ignore next 2 */
12
- }
13
- return (array) => String.fromCharCode(...array);
14
- })();
15
- /** 解码 */
16
- function decode(data) {
17
- if (data.byteLength < 50 && // 只有小字符串有优化价值,见 benchmark-string.js
18
- data.every((b) => b < 128)) {
19
- // 为 ASCII 字符串优化
20
- return decodeAscii(data);
21
- }
22
- // 使用系统解码
23
- return textDecoder.decode(data);
24
- }
25
- /** 特别优化字符串解码速度 */
26
- export class StringDecoder {
27
- constructor() {
28
- /** 小字符串缓存 */
29
- this.cache = [
30
- undefined,
31
- undefined,
32
- new Map(),
33
- new Map(),
34
- new Map(),
35
- new Map(),
36
- new Map(), // 6 字节
37
- ];
38
- }
39
- /** 字符串解码 */
40
- decode(data) {
41
- const length = data.byteLength;
42
- // 这里,length 类型为 Uint32
43
- if (length > 6)
44
- return decode(data);
45
- // 这里,length 类型为 0 | 1 | 2 | 3 | 4 | 5 | 6
46
- // 为小字符串优化
47
- if (length === 0)
48
- return '';
49
- if (length === 1)
50
- return String.fromCharCode(data[0]);
51
- // number 最多存储 6 字节数据
52
- const cache = this.cache[length];
53
- // 计算缓存 key
54
- let key = 0;
55
- for (const byte of data) {
56
- key *= 256;
57
- key += byte;
58
- }
59
- const match = cache.get(key);
60
- if (match != null)
61
- return match;
62
- const string = decode(data);
63
- cache.set(key, string);
64
- return string;
65
- }
66
- }
67
- //# sourceMappingURL=string-decoder.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"string-decoder.js","sourceRoot":"","sources":["../src/string-decoder.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;AAEvF,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,GAAG,EAAE;IAC7B,IAAI;QACA,MAAM,eAAe,GAAkC,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,IAAI,CACjF,MAAM,CAAC,YAAY,EACnB,MAAM,CAC8B,CAAC;QAEzC,sDAAsD;QACtD,IAAI,YAAY,KAAK,eAAe,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC;YAAE,OAAO,eAAe,CAAC;QAC5F,sBAAsB;KACzB;IAAC,MAAM;QACJ,sBAAsB;KACzB;IACD,OAAO,CAAC,KAAiB,EAAU,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,CAAC;AACxE,CAAC,CAAC,EAAE,CAAC;AAEL,SAAS;AACT,SAAS,MAAM,CAAC,IAAgB;IAC5B,IACI,IAAI,CAAC,UAAU,GAAG,EAAE,IAAI,oCAAoC;QAC5D,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC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,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;SAC9B;QACD,OAAO,GAAG,CAAC;IACf,CAAC;CACJ"}
@@ -1,69 +0,0 @@
1
- export const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: false });
2
-
3
- export const decodeAscii = (() => {
4
- try {
5
- const fastDecodeAscii: (array: Uint8Array) => string = String.fromCharCode.apply.bind(
6
- String.fromCharCode,
7
- String,
8
- ) as (array: Iterable<number>) => string;
9
-
10
- // 检查能否直接向 `String.fromCharCode.apply` 传入 `Uint8Array`
11
- if ('\0\x01\x7f' === fastDecodeAscii(Uint8Array.from([0, 1, 0x7f]))) return fastDecodeAscii;
12
- /* c8 ignore next 1 */
13
- } catch {
14
- /* c8 ignore next 2 */
15
- }
16
- return (array: Uint8Array): string => String.fromCharCode(...array);
17
- })();
18
-
19
- /** 解码 */
20
- function decode(data: Uint8Array): string {
21
- if (
22
- data.byteLength < 50 && // 只有小字符串有优化价值,见 benchmark-string.js
23
- data.every((b) => b < 128)
24
- ) {
25
- // 为 ASCII 字符串优化
26
- return decodeAscii(data);
27
- }
28
- // 使用系统解码
29
- return textDecoder.decode(data);
30
- }
31
-
32
- /** 特别优化字符串解码速度 */
33
- export class StringDecoder {
34
- /** 小字符串缓存 */
35
- private readonly cache = [
36
- undefined, // 0 字节字符串,直接返回
37
- undefined, // 1 字节字符串,使用 String.fromCharCode,兼容 UTF8
38
- new Map<number, string>(), // 2 字节
39
- new Map<number, string>(), // 3 字节
40
- new Map<number, string>(), // 4 字节
41
- new Map<number, string>(), // 5 字节
42
- new Map<number, string>(), // 6 字节
43
- ] as const;
44
-
45
- /** 字符串解码 */
46
- decode(data: Uint8Array): string {
47
- const length = data.byteLength as 0 | 1 | 2 | 3 | 4 | 5 | 6;
48
- // 这里,length 类型为 Uint32
49
- if (length > 6) return decode(data);
50
-
51
- // 这里,length 类型为 0 | 1 | 2 | 3 | 4 | 5 | 6
52
- // 为小字符串优化
53
- if (length === 0) return '';
54
- if (length === 1) return String.fromCharCode(data[0]);
55
- // number 最多存储 6 字节数据
56
- const cache = this.cache[length];
57
- // 计算缓存 key
58
- let key = 0;
59
- for (const byte of data) {
60
- key *= 256;
61
- key += byte;
62
- }
63
- const match = cache.get(key);
64
- if (match != null) return match;
65
- const string = decode(data);
66
- cache.set(key, string);
67
- return string;
68
- }
69
- }