@cloudpss/ubjson 0.5.37 → 0.5.39

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 (91) hide show
  1. package/dist/base/decoder.d.ts +12 -0
  2. package/dist/base/decoder.js +26 -0
  3. package/dist/base/decoder.js.map +1 -0
  4. package/dist/{common → base}/encoder.d.ts +2 -6
  5. package/dist/{common → base}/encoder.js +71 -121
  6. package/dist/base/encoder.js.map +1 -0
  7. package/dist/decoder.d.ts +1 -2
  8. package/dist/decoder.js +1 -1
  9. package/dist/decoder.js.map +1 -1
  10. package/dist/encoder.d.ts +1 -1
  11. package/dist/encoder.js +11 -11
  12. package/dist/encoder.js.map +1 -1
  13. package/dist/{common → helper}/constants.js.map +1 -1
  14. package/dist/helper/decode.d.ts +49 -0
  15. package/dist/helper/decode.js +344 -0
  16. package/dist/helper/decode.js.map +1 -0
  17. package/dist/helper/encode.d.ts +20 -0
  18. package/dist/helper/encode.js +84 -0
  19. package/dist/helper/encode.js.map +1 -0
  20. package/dist/{common → helper}/errors.d.ts +4 -0
  21. package/dist/{common → helper}/errors.js +10 -2
  22. package/dist/helper/errors.js.map +1 -0
  23. package/dist/{common → helper}/string-decoder.js +1 -1
  24. package/dist/{common → helper}/string-decoder.js.map +1 -1
  25. package/dist/{common → helper}/string-encoder.js.map +1 -1
  26. package/dist/{utils.d.ts → helper/utils.d.ts} +0 -4
  27. package/dist/{utils.js → helper/utils.js} +0 -6
  28. package/dist/helper/utils.js.map +1 -0
  29. package/dist/index.d.ts +3 -5
  30. package/dist/index.js +5 -5
  31. package/dist/index.js.map +1 -1
  32. package/dist/rxjs/decoder.js +1 -1
  33. package/dist/rxjs/decoder.js.map +1 -1
  34. package/dist/rxjs/index.d.ts +1 -1
  35. package/dist/rxjs/index.js +1 -1
  36. package/dist/rxjs/index.js.map +1 -1
  37. package/dist/stream/index.d.ts +2 -2
  38. package/dist/stream/index.js +2 -2
  39. package/dist/stream/index.js.map +1 -1
  40. package/dist/stream-helper/decoder.d.ts +2 -3
  41. package/dist/stream-helper/decoder.js +3 -6
  42. package/dist/stream-helper/decoder.js.map +1 -1
  43. package/dist/stream-helper/encoder.d.ts +1 -1
  44. package/dist/stream-helper/encoder.js +15 -15
  45. package/dist/stream-helper/encoder.js.map +1 -1
  46. package/package.json +4 -4
  47. package/src/base/decoder.ts +27 -0
  48. package/src/{common → base}/encoder.ts +72 -116
  49. package/src/decoder.ts +1 -2
  50. package/src/encoder.ts +11 -11
  51. package/src/helper/decode.ts +362 -0
  52. package/src/helper/encode.ts +89 -0
  53. package/src/{common → helper}/errors.ts +12 -2
  54. package/src/{common → helper}/string-decoder.ts +1 -1
  55. package/src/{utils.ts → helper/utils.ts} +0 -7
  56. package/src/index.ts +6 -8
  57. package/src/rxjs/decoder.ts +1 -1
  58. package/src/rxjs/index.ts +1 -1
  59. package/src/stream/index.ts +3 -3
  60. package/src/stream-helper/decoder.ts +3 -6
  61. package/src/stream-helper/encoder.ts +16 -16
  62. package/tests/.utils.js +6 -4
  63. package/tests/decode.js +0 -18
  64. package/tests/e2e/no-encode-into.js +1 -1
  65. package/tests/encode.js +1 -1
  66. package/tests/huge-string.js +1 -0
  67. package/tests/rxjs/decode.js +14 -13
  68. package/tests/rxjs/encode.js +13 -12
  69. package/tests/stream/decode.js +15 -13
  70. package/tests/stream/encode.js +9 -8
  71. package/tests/stream/many.js +2 -1
  72. package/tests/string-encoding.js +4 -6
  73. package/benchmark-small.js +0 -81
  74. package/benchmark-string-decode.js +0 -41
  75. package/benchmark-string-encode.js +0 -32
  76. package/benchmark-string-size-caculation.js +0 -50
  77. package/benchmark.js +0 -50
  78. package/dist/common/decoder.d.ts +0 -45
  79. package/dist/common/decoder.js +0 -348
  80. package/dist/common/decoder.js.map +0 -1
  81. package/dist/common/encoder.js.map +0 -1
  82. package/dist/common/errors.js.map +0 -1
  83. package/dist/utils.js.map +0 -1
  84. package/src/common/decoder.ts +0 -356
  85. /package/dist/{common → helper}/constants.d.ts +0 -0
  86. /package/dist/{common → helper}/constants.js +0 -0
  87. /package/dist/{common → helper}/string-decoder.d.ts +0 -0
  88. /package/dist/{common → helper}/string-encoder.d.ts +0 -0
  89. /package/dist/{common → helper}/string-encoder.js +0 -0
  90. /package/src/{common → helper}/constants.ts +0 -0
  91. /package/src/{common → helper}/string-encoder.ts +0 -0
@@ -1,6 +1,7 @@
1
- import { constants } from './constants.js';
2
- import { unsupportedType, unsupportedView } from './errors.js';
3
- import { stringByteLength, encodeInto } from './string-encoder.js';
1
+ import { constants } from '../helper/constants.js';
2
+ import { type EncodeCursor, writeLength, writeTypedArrayHeader } from '../helper/encode.js';
3
+ import { unsupportedType, unsupportedView } from '../helper/errors.js';
4
+ import { stringByteLength, encodeInto } from '../helper/string-encoder.js';
4
5
 
5
6
  const LARGE_DATA_LENGTH = 65536;
6
7
 
@@ -9,7 +10,7 @@ export abstract class EncoderBase {
9
10
  /** 当前写指针位置 */
10
11
  protected length = 0;
11
12
  /** 数据 */
12
- protected buffer!: Uint8Array;
13
+ protected data!: Uint8Array;
13
14
  /** buffer 的 DataView */
14
15
  protected view!: DataView;
15
16
  /**
@@ -17,11 +18,11 @@ export abstract class EncoderBase {
17
18
  */
18
19
  protected abstract ensureCapacity(capacity: number): void;
19
20
 
20
- /** 编码至 ubjson */
21
+ /** 编码至 ubjson,对于 `undefined` 写入 NOOP */
21
22
  protected writeValue(value: unknown): void {
22
23
  if (value === undefined) {
23
24
  this.ensureCapacity(1);
24
- this.buffer[this.length++] = constants.NO_OP;
25
+ this.data[this.length++] = constants.NO_OP;
25
26
  return;
26
27
  }
27
28
  this.write(value);
@@ -30,14 +31,14 @@ export abstract class EncoderBase {
30
31
  private write(value: unknown): void {
31
32
  switch (typeof value) {
32
33
  case 'string':
33
- if (value.length === 1 && value.charCodeAt(0) < 128) {
34
+ if (value.length === 1 && value.charCodeAt(0) < 0x80) {
34
35
  // 1 byte ascii char
35
36
  this.ensureCapacity(2);
36
- this.buffer[this.length++] = constants.CHAR;
37
- this.buffer[this.length++] = value.charCodeAt(0);
37
+ this.data[this.length++] = constants.CHAR;
38
+ this.data[this.length++] = value.charCodeAt(0);
38
39
  } else {
39
40
  this.ensureCapacity(2);
40
- this.buffer[this.length++] = constants.STRING;
41
+ this.data[this.length++] = constants.STRING;
41
42
  this.writeStringData(value);
42
43
  }
43
44
  return;
@@ -46,32 +47,32 @@ export abstract class EncoderBase {
46
47
  if ((value | 0) === value) {
47
48
  if (value >= 0 && value <= 255) {
48
49
  this.ensureCapacity(2);
49
- this.buffer[this.length++] = constants.UINT8;
50
- this.buffer[this.length++] = value;
50
+ this.data[this.length++] = constants.UINT8;
51
+ this.data[this.length++] = value;
51
52
  } else if (value >= -128 && value <= 127) {
52
53
  this.ensureCapacity(2);
53
- this.buffer[this.length++] = constants.INT8;
54
- this.buffer[this.length++] = value;
54
+ this.data[this.length++] = constants.INT8;
55
+ this.data[this.length++] = value;
55
56
  } else if (value >= -32768 && value <= 32767) {
56
57
  this.ensureCapacity(3);
57
- this.buffer[this.length++] = constants.INT16;
58
- this.buffer[this.length++] = value >> 8;
59
- this.buffer[this.length++] = value & 0xff;
58
+ this.data[this.length++] = constants.INT16;
59
+ this.data[this.length++] = value >> 8;
60
+ this.data[this.length++] = value & 0xff;
60
61
  } else {
61
62
  this.ensureCapacity(5);
62
- this.buffer[this.length++] = constants.INT32;
63
+ this.data[this.length++] = constants.INT32;
63
64
  this.view.setInt32(this.length, value);
64
65
  this.length += 4;
65
66
  }
66
67
  } else if (Number.isNaN(value) || Math.fround(value) === value) {
67
68
  // 如果不会损失精度,使用 32 位浮点
68
69
  this.ensureCapacity(5);
69
- this.buffer[this.length++] = constants.FLOAT32;
70
+ this.data[this.length++] = constants.FLOAT32;
70
71
  this.view.setFloat32(this.length, value);
71
72
  this.length += 4;
72
73
  } else {
73
74
  this.ensureCapacity(9);
74
- this.buffer[this.length++] = constants.FLOAT64;
75
+ this.data[this.length++] = constants.FLOAT64;
75
76
  this.view.setFloat64(this.length, value);
76
77
  this.length += 8;
77
78
  }
@@ -79,25 +80,25 @@ export abstract class EncoderBase {
79
80
  case 'object': {
80
81
  if (value === null) {
81
82
  this.ensureCapacity(1);
82
- this.buffer[this.length++] = constants.NULL;
83
+ this.data[this.length++] = constants.NULL;
83
84
  return;
84
85
  }
85
86
  if (Array.isArray(value)) {
86
87
  this.ensureCapacity(1);
87
- this.buffer[this.length++] = constants.ARRAY;
88
+ this.data[this.length++] = constants.ARRAY;
88
89
  const size = value.length;
89
90
  for (let index = 0; index < size; index++) {
90
91
  const element = value[index] as unknown;
91
92
  // 在数组中 undefined 和 function 也被视作 null 进行序列化
92
93
  if (element == null || typeof element == 'function') {
93
94
  this.ensureCapacity(1);
94
- this.buffer[this.length++] = constants.NULL;
95
+ this.data[this.length++] = constants.NULL;
95
96
  } else {
96
97
  this.write(element);
97
98
  }
98
99
  }
99
100
  this.ensureCapacity(1);
100
- this.buffer[this.length++] = constants.ARRAY_END;
101
+ this.data[this.length++] = constants.ARRAY_END;
101
102
  return;
102
103
  }
103
104
  if (!ArrayBuffer.isView(value)) {
@@ -107,7 +108,7 @@ export abstract class EncoderBase {
107
108
  return;
108
109
  }
109
110
  this.ensureCapacity(1);
110
- this.buffer[this.length++] = constants.OBJECT;
111
+ this.data[this.length++] = constants.OBJECT;
111
112
  // 生成稳定的结果以便 hash 计算
112
113
  const keys = Object.keys(value).sort();
113
114
  const size = keys.length;
@@ -119,45 +120,24 @@ export abstract class EncoderBase {
119
120
  this.write(element);
120
121
  }
121
122
  this.ensureCapacity(1);
122
- this.buffer[this.length++] = constants.OBJECT_END;
123
+ this.data[this.length++] = constants.OBJECT_END;
123
124
  return;
124
125
  }
125
126
  if (value.byteLength > LARGE_DATA_LENGTH) {
126
127
  // ARRAY(1) + TYPE_MARKER(1) + TYPE(1) + COUNT_MARKER(1) + COUNT(5)
127
- this.ensureCapacity(9);
128
- this.buffer[this.length++] = constants.ARRAY;
129
- this.buffer[this.length++] = constants.TYPE_MARKER;
130
- if (value instanceof Uint8Array) {
131
- this.buffer[this.length++] = constants.UINT8;
132
- } else if (value instanceof Int8Array) {
133
- this.buffer[this.length++] = constants.INT8;
134
- } else if (value instanceof Int16Array) {
135
- this.buffer[this.length++] = constants.INT16;
136
- } else if (value instanceof Int32Array) {
137
- this.buffer[this.length++] = constants.INT32;
138
- } else if (value instanceof Float32Array) {
139
- this.buffer[this.length++] = constants.FLOAT32;
140
- } else if (value instanceof Float64Array) {
141
- this.buffer[this.length++] = constants.FLOAT64;
142
- } else if (value instanceof BigInt64Array) {
143
- this.buffer[this.length++] = constants.INT64;
144
- } else {
145
- unsupportedView(value);
146
- }
147
- this.buffer[this.length++] = constants.COUNT_MARKER;
148
- this.setLength(value.length);
128
+ writeTypedArrayHeader(this as this & EncodeCursor, value);
149
129
  this.writeLargeTypedArrayData(value);
150
130
  } else {
151
131
  // ARRAY(1) + TYPE_MARKER(1) + TYPE(1) + COUNT_MARKER(1) + COUNT(MAX5) + DATA
152
132
  this.ensureCapacity(9 + value.byteLength);
153
- this.buffer[this.length++] = constants.ARRAY;
154
- this.buffer[this.length++] = constants.TYPE_MARKER;
133
+ this.data[this.length++] = constants.ARRAY;
134
+ this.data[this.length++] = constants.TYPE_MARKER;
155
135
  if (value instanceof Uint8Array || value instanceof Int8Array) {
156
136
  // fast path for typed arrays with `BYTES_PER_ELEMENT` of 1
157
- this.buffer[this.length++] = value instanceof Uint8Array ? constants.UINT8 : constants.INT8;
158
- this.buffer[this.length++] = constants.COUNT_MARKER;
159
- this.setLength(value.length);
160
- this.buffer.set(value, this.length);
137
+ this.data[this.length++] = value instanceof Uint8Array ? constants.UINT8 : constants.INT8;
138
+ this.data[this.length++] = constants.COUNT_MARKER;
139
+ writeLength(this as this & EncodeCursor, value.length);
140
+ this.data.set(value, this.length);
161
141
  this.length += value.byteLength;
162
142
  return;
163
143
  }
@@ -166,42 +146,42 @@ export abstract class EncoderBase {
166
146
  .length;
167
147
  const elementSize = (value as Int16Array | Int32Array | BigInt64Array | Float32Array | Float64Array)
168
148
  .BYTES_PER_ELEMENT;
169
- if (value instanceof Int16Array) {
170
- this.buffer[this.length++] = constants.INT16;
171
- this.buffer[this.length++] = constants.COUNT_MARKER;
172
- this.setLength(arrayLength);
149
+ if (value instanceof Float64Array) {
150
+ this.data[this.length++] = constants.FLOAT64;
151
+ this.data[this.length++] = constants.COUNT_MARKER;
152
+ writeLength(this as this & EncodeCursor, arrayLength);
173
153
  for (let i = 0; i < arrayLength; i++) {
174
- this.view.setInt16(this.length, value[i]!);
154
+ this.view.setFloat64(this.length, value[i]!);
175
155
  this.length += elementSize;
176
156
  }
177
157
  } else if (value instanceof Int32Array) {
178
- this.buffer[this.length++] = constants.INT32;
179
- this.buffer[this.length++] = constants.COUNT_MARKER;
180
- this.setLength(arrayLength);
158
+ this.data[this.length++] = constants.INT32;
159
+ this.data[this.length++] = constants.COUNT_MARKER;
160
+ writeLength(this as this & EncodeCursor, arrayLength);
181
161
  for (let i = 0; i < arrayLength; i++) {
182
162
  this.view.setInt32(this.length, value[i]!);
183
163
  this.length += elementSize;
184
164
  }
185
- } else if (value instanceof Float32Array) {
186
- this.buffer[this.length++] = constants.FLOAT32;
187
- this.buffer[this.length++] = constants.COUNT_MARKER;
188
- this.setLength(arrayLength);
165
+ } else if (value instanceof Int16Array) {
166
+ this.data[this.length++] = constants.INT16;
167
+ this.data[this.length++] = constants.COUNT_MARKER;
168
+ writeLength(this as this & EncodeCursor, arrayLength);
189
169
  for (let i = 0; i < arrayLength; i++) {
190
- this.view.setFloat32(this.length, value[i]!);
170
+ this.view.setInt16(this.length, value[i]!);
191
171
  this.length += elementSize;
192
172
  }
193
- } else if (value instanceof Float64Array) {
194
- this.buffer[this.length++] = constants.FLOAT64;
195
- this.buffer[this.length++] = constants.COUNT_MARKER;
196
- this.setLength(arrayLength);
173
+ } else if (value instanceof Float32Array) {
174
+ this.data[this.length++] = constants.FLOAT32;
175
+ this.data[this.length++] = constants.COUNT_MARKER;
176
+ writeLength(this as this & EncodeCursor, arrayLength);
197
177
  for (let i = 0; i < arrayLength; i++) {
198
- this.view.setFloat64(this.length, value[i]!);
178
+ this.view.setFloat32(this.length, value[i]!);
199
179
  this.length += elementSize;
200
180
  }
201
181
  } else if (value instanceof BigInt64Array) {
202
- this.buffer[this.length++] = constants.INT64;
203
- this.buffer[this.length++] = constants.COUNT_MARKER;
204
- this.setLength(arrayLength);
182
+ this.data[this.length++] = constants.INT64;
183
+ this.data[this.length++] = constants.COUNT_MARKER;
184
+ writeLength(this as this & EncodeCursor, arrayLength);
205
185
  for (let i = 0; i < arrayLength; i++) {
206
186
  this.view.setBigInt64(this.length, value[i]!);
207
187
  this.length += elementSize;
@@ -214,7 +194,7 @@ export abstract class EncoderBase {
214
194
  }
215
195
  case 'boolean':
216
196
  this.ensureCapacity(1);
217
- this.buffer[this.length++] = value ? constants.TRUE : constants.FALSE;
197
+ this.data[this.length++] = value ? constants.TRUE : constants.FALSE;
218
198
  return;
219
199
  case 'bigint':
220
200
  // int32 range
@@ -224,7 +204,7 @@ export abstract class EncoderBase {
224
204
  // int64 range
225
205
  else if (value >= -9_223_372_036_854_775_808n && value <= 9_223_372_036_854_775_807n) {
226
206
  this.ensureCapacity(9);
227
- this.buffer[this.length++] = constants.INT64;
207
+ this.data[this.length++] = constants.INT64;
228
208
  this.view.setBigInt64(this.length, value);
229
209
  this.length += 8;
230
210
  } else {
@@ -249,24 +229,24 @@ export abstract class EncoderBase {
249
229
  this.ensureCapacity(maxUsage + 5 + 3);
250
230
 
251
231
  // 预估头部大小
252
- const headerSize = strLength < 128 ? 2 : strLength < 32768 ? 3 : 5;
232
+ const headerSize = strLength < 0x80 ? 2 : strLength < 0x8000 ? 3 : 5;
253
233
  const headerPos = this.length;
254
- const bufLength = encodeInto(value, this.buffer, this.length + headerSize);
255
- if (bufLength < 128) {
256
- this.buffer[this.length++] = constants.INT8;
257
- this.buffer[this.length++] = bufLength;
258
- } else if (bufLength < 32768) {
234
+ const bufLength = encodeInto(value, this.data, this.length + headerSize);
235
+ if (bufLength < 0x80) {
236
+ this.data[this.length++] = constants.INT8;
237
+ this.data[this.length++] = bufLength;
238
+ } else if (bufLength < 0x8000) {
259
239
  if (headerSize < 3) {
260
- this.buffer.copyWithin(headerPos + 3, headerPos + headerSize, headerPos + headerSize + bufLength);
240
+ this.data.copyWithin(headerPos + 3, headerPos + headerSize, headerPos + headerSize + bufLength);
261
241
  }
262
- this.buffer[this.length++] = constants.INT16;
263
- this.buffer[this.length++] = bufLength >> 8;
264
- this.buffer[this.length++] = bufLength & 0xff;
242
+ this.data[this.length++] = constants.INT16;
243
+ this.data[this.length++] = bufLength >> 8;
244
+ this.data[this.length++] = bufLength & 0xff;
265
245
  } else {
266
246
  if (headerSize < 5) {
267
- this.buffer.copyWithin(headerPos + 5, headerPos + headerSize, headerPos + headerSize + bufLength);
247
+ this.data.copyWithin(headerPos + 5, headerPos + headerSize, headerPos + headerSize + bufLength);
268
248
  }
269
- this.buffer[this.length++] = constants.INT32;
249
+ this.data[this.length++] = constants.INT32;
270
250
  this.view.setInt32(this.length, bufLength);
271
251
  this.length += 4;
272
252
  }
@@ -277,11 +257,11 @@ export abstract class EncoderBase {
277
257
  protected writeLargeStringData(value: string): void {
278
258
  const binLen = stringByteLength(value);
279
259
  this.ensureCapacity(5);
280
- this.buffer[this.length++] = constants.INT32;
260
+ this.data[this.length++] = constants.INT32;
281
261
  this.view.setInt32(this.length, binLen);
282
262
  this.length += 4;
283
263
  this.ensureCapacity(binLen);
284
- encodeInto(value, this.buffer, this.length);
264
+ encodeInto(value, this.data, this.length);
285
265
  this.length += binLen;
286
266
  }
287
267
 
@@ -290,7 +270,7 @@ export abstract class EncoderBase {
290
270
  this.ensureCapacity(value.byteLength);
291
271
  if (value instanceof Uint8Array || value instanceof Int8Array) {
292
272
  // fast path for typed arrays with `BYTES_PER_ELEMENT` of 1
293
- this.buffer.set(value, this.length);
273
+ this.data.set(value, this.length);
294
274
  this.length += value.byteLength;
295
275
  return;
296
276
  }
@@ -327,28 +307,4 @@ export abstract class EncoderBase {
327
307
  unsupportedView(value);
328
308
  }
329
309
  }
330
-
331
- /**
332
- * 写入整形数字,选取合适的大小,需提前分配空间
333
- */
334
- private setLength(value: number): number {
335
- // eslint-disable-next-line unicorn/prefer-math-trunc
336
- value = value | 0;
337
- if ((value & 0x7f) === value) {
338
- this.buffer[this.length++] = constants.INT8;
339
- this.buffer[this.length++] = value;
340
- return 1;
341
- }
342
- // 不使用 uint8 以保持兼容
343
- if ((value & 0x7fff) === value) {
344
- this.buffer[this.length++] = constants.INT16;
345
- this.buffer[this.length++] = value >> 8;
346
- this.buffer[this.length++] = value & 0xff;
347
- return 2;
348
- }
349
- this.buffer[this.length++] = constants.INT32;
350
- this.view.setInt32(this.length, value);
351
- this.length += 4;
352
- return 4;
353
- }
354
310
  }
package/src/decoder.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { DecoderBase } from './common/decoder.js';
2
- export type { DecoderOptions } from './common/decoder.js';
1
+ import { DecoderBase } from './base/decoder.js';
3
2
 
4
3
  /** 解码 UBJSON */
5
4
  export class Decoder extends DecoderBase {
package/src/encoder.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { EncoderBase } from './common/encoder.js';
1
+ import { EncoderBase } from './base/encoder.js';
2
2
 
3
3
  const BLOCK_SIZE = 1024 * 16; // 16 KiB
4
4
  const MAX_SIZE = 1024 * 1024 * 128; //128 MiB
@@ -17,13 +17,13 @@ export class Encoder extends EncoderBase {
17
17
  throw new Error('Buffer has exceed max size');
18
18
  }
19
19
  // 无需扩容
20
- if (this.buffer.byteLength >= this.length + capacity) return;
20
+ if (this.data.byteLength >= this.length + capacity) return;
21
21
 
22
22
  // 提交目前的数据
23
- if (this.buffer === this.pool) {
24
- this.flushedBuffers.push(this.buffer.slice(0, this.length));
23
+ if (this.data === this.pool) {
24
+ this.flushedBuffers.push(this.data.slice(0, this.length));
25
25
  } else {
26
- this.flushedBuffers.push(this.buffer.subarray(0, this.length));
26
+ this.flushedBuffers.push(this.data.subarray(0, this.length));
27
27
  }
28
28
 
29
29
  // 重新分配缓冲区
@@ -35,13 +35,13 @@ export class Encoder extends EncoderBase {
35
35
  private allocUnsafe(size: number): void {
36
36
  if (size === this.pool.byteLength) {
37
37
  // 从 pool 中获取
38
- this.buffer = this.pool;
39
- this.view = new DataView(this.buffer.buffer);
38
+ this.data = this.pool;
39
+ this.view = new DataView(this.data.buffer);
40
40
  this.length = 0;
41
41
  return;
42
42
  }
43
- this.buffer = new Uint8Array(size);
44
- this.view = new DataView(this.buffer.buffer);
43
+ this.data = new Uint8Array(size);
44
+ this.view = new DataView(this.data.buffer);
45
45
  this.length = 0;
46
46
  }
47
47
 
@@ -49,7 +49,7 @@ export class Encoder extends EncoderBase {
49
49
  private getResult(): Uint8Array {
50
50
  if (this.flushedBuffers.length === 0) {
51
51
  // 缓冲区为空,复制当前 buffer
52
- return this.buffer.slice(0, this.length);
52
+ return this.data.slice(0, this.length);
53
53
  }
54
54
 
55
55
  // 合并缓冲区
@@ -61,7 +61,7 @@ export class Encoder extends EncoderBase {
61
61
  result.set(buffer, offset);
62
62
  offset += buffer.byteLength;
63
63
  }
64
- result.set(this.buffer.subarray(0, this.length), offset);
64
+ result.set(this.data.subarray(0, this.length), offset);
65
65
  return result;
66
66
  }
67
67