@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.
- package/dist/base/decoder.d.ts +12 -0
- package/dist/base/decoder.js +26 -0
- package/dist/base/decoder.js.map +1 -0
- package/dist/{common → base}/encoder.d.ts +2 -6
- package/dist/{common → base}/encoder.js +71 -121
- package/dist/base/encoder.js.map +1 -0
- package/dist/decoder.d.ts +1 -2
- package/dist/decoder.js +1 -1
- package/dist/decoder.js.map +1 -1
- package/dist/encoder.d.ts +1 -1
- package/dist/encoder.js +11 -11
- package/dist/encoder.js.map +1 -1
- package/dist/{common → helper}/constants.js.map +1 -1
- package/dist/helper/decode.d.ts +49 -0
- package/dist/helper/decode.js +344 -0
- package/dist/helper/decode.js.map +1 -0
- package/dist/helper/encode.d.ts +20 -0
- package/dist/helper/encode.js +84 -0
- package/dist/helper/encode.js.map +1 -0
- package/dist/{common → helper}/errors.d.ts +4 -0
- package/dist/{common → helper}/errors.js +10 -2
- package/dist/helper/errors.js.map +1 -0
- package/dist/{common → helper}/string-decoder.js +1 -1
- package/dist/{common → helper}/string-decoder.js.map +1 -1
- package/dist/{common → helper}/string-encoder.js.map +1 -1
- package/dist/{utils.d.ts → helper/utils.d.ts} +0 -4
- package/dist/{utils.js → helper/utils.js} +0 -6
- package/dist/helper/utils.js.map +1 -0
- package/dist/index.d.ts +3 -5
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/rxjs/decoder.js +1 -1
- package/dist/rxjs/decoder.js.map +1 -1
- package/dist/rxjs/index.d.ts +1 -1
- package/dist/rxjs/index.js +1 -1
- package/dist/rxjs/index.js.map +1 -1
- package/dist/stream/index.d.ts +2 -2
- package/dist/stream/index.js +2 -2
- package/dist/stream/index.js.map +1 -1
- package/dist/stream-helper/decoder.d.ts +2 -3
- package/dist/stream-helper/decoder.js +3 -6
- package/dist/stream-helper/decoder.js.map +1 -1
- package/dist/stream-helper/encoder.d.ts +1 -1
- package/dist/stream-helper/encoder.js +15 -15
- package/dist/stream-helper/encoder.js.map +1 -1
- package/package.json +4 -4
- package/src/base/decoder.ts +27 -0
- package/src/{common → base}/encoder.ts +72 -116
- package/src/decoder.ts +1 -2
- package/src/encoder.ts +11 -11
- package/src/helper/decode.ts +362 -0
- package/src/helper/encode.ts +89 -0
- package/src/{common → helper}/errors.ts +12 -2
- package/src/{common → helper}/string-decoder.ts +1 -1
- package/src/{utils.ts → helper/utils.ts} +0 -7
- package/src/index.ts +6 -8
- package/src/rxjs/decoder.ts +1 -1
- package/src/rxjs/index.ts +1 -1
- package/src/stream/index.ts +3 -3
- package/src/stream-helper/decoder.ts +3 -6
- package/src/stream-helper/encoder.ts +16 -16
- package/tests/.utils.js +6 -4
- package/tests/decode.js +0 -18
- package/tests/e2e/no-encode-into.js +1 -1
- package/tests/encode.js +1 -1
- package/tests/huge-string.js +1 -0
- package/tests/rxjs/decode.js +14 -13
- package/tests/rxjs/encode.js +13 -12
- package/tests/stream/decode.js +15 -13
- package/tests/stream/encode.js +9 -8
- package/tests/stream/many.js +2 -1
- package/tests/string-encoding.js +4 -6
- package/benchmark-small.js +0 -81
- package/benchmark-string-decode.js +0 -41
- package/benchmark-string-encode.js +0 -32
- package/benchmark-string-size-caculation.js +0 -50
- package/benchmark.js +0 -50
- package/dist/common/decoder.d.ts +0 -45
- package/dist/common/decoder.js +0 -348
- package/dist/common/decoder.js.map +0 -1
- package/dist/common/encoder.js.map +0 -1
- package/dist/common/errors.js.map +0 -1
- package/dist/utils.js.map +0 -1
- package/src/common/decoder.ts +0 -356
- /package/dist/{common → helper}/constants.d.ts +0 -0
- /package/dist/{common → helper}/constants.js +0 -0
- /package/dist/{common → helper}/string-decoder.d.ts +0 -0
- /package/dist/{common → helper}/string-encoder.d.ts +0 -0
- /package/dist/{common → helper}/string-encoder.js +0 -0
- /package/src/{common → helper}/constants.ts +0 -0
- /package/src/{common → helper}/string-encoder.ts +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { constants } from '
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
|
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.
|
|
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) <
|
|
34
|
+
if (value.length === 1 && value.charCodeAt(0) < 0x80) {
|
|
34
35
|
// 1 byte ascii char
|
|
35
36
|
this.ensureCapacity(2);
|
|
36
|
-
this.
|
|
37
|
-
this.
|
|
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.
|
|
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.
|
|
50
|
-
this.
|
|
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.
|
|
54
|
-
this.
|
|
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.
|
|
58
|
-
this.
|
|
59
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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
|
|
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.
|
|
154
|
-
this.
|
|
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.
|
|
158
|
-
this.
|
|
159
|
-
this
|
|
160
|
-
this.
|
|
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
|
|
170
|
-
this.
|
|
171
|
-
this.
|
|
172
|
-
this
|
|
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.
|
|
154
|
+
this.view.setFloat64(this.length, value[i]!);
|
|
175
155
|
this.length += elementSize;
|
|
176
156
|
}
|
|
177
157
|
} else if (value instanceof Int32Array) {
|
|
178
|
-
this.
|
|
179
|
-
this.
|
|
180
|
-
this
|
|
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
|
|
186
|
-
this.
|
|
187
|
-
this.
|
|
188
|
-
this
|
|
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.
|
|
170
|
+
this.view.setInt16(this.length, value[i]!);
|
|
191
171
|
this.length += elementSize;
|
|
192
172
|
}
|
|
193
|
-
} else if (value instanceof
|
|
194
|
-
this.
|
|
195
|
-
this.
|
|
196
|
-
this
|
|
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.
|
|
178
|
+
this.view.setFloat32(this.length, value[i]!);
|
|
199
179
|
this.length += elementSize;
|
|
200
180
|
}
|
|
201
181
|
} else if (value instanceof BigInt64Array) {
|
|
202
|
-
this.
|
|
203
|
-
this.
|
|
204
|
-
this
|
|
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.
|
|
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.
|
|
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 <
|
|
232
|
+
const headerSize = strLength < 0x80 ? 2 : strLength < 0x8000 ? 3 : 5;
|
|
253
233
|
const headerPos = this.length;
|
|
254
|
-
const bufLength = encodeInto(value, this.
|
|
255
|
-
if (bufLength <
|
|
256
|
-
this.
|
|
257
|
-
this.
|
|
258
|
-
} else if (bufLength <
|
|
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.
|
|
240
|
+
this.data.copyWithin(headerPos + 3, headerPos + headerSize, headerPos + headerSize + bufLength);
|
|
261
241
|
}
|
|
262
|
-
this.
|
|
263
|
-
this.
|
|
264
|
-
this.
|
|
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.
|
|
247
|
+
this.data.copyWithin(headerPos + 5, headerPos + headerSize, headerPos + headerSize + bufLength);
|
|
268
248
|
}
|
|
269
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
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
package/src/encoder.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EncoderBase } from './
|
|
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.
|
|
20
|
+
if (this.data.byteLength >= this.length + capacity) return;
|
|
21
21
|
|
|
22
22
|
// 提交目前的数据
|
|
23
|
-
if (this.
|
|
24
|
-
this.flushedBuffers.push(this.
|
|
23
|
+
if (this.data === this.pool) {
|
|
24
|
+
this.flushedBuffers.push(this.data.slice(0, this.length));
|
|
25
25
|
} else {
|
|
26
|
-
this.flushedBuffers.push(this.
|
|
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.
|
|
39
|
-
this.view = new DataView(this.
|
|
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.
|
|
44
|
-
this.view = new DataView(this.
|
|
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.
|
|
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.
|
|
64
|
+
result.set(this.data.subarray(0, this.length), offset);
|
|
65
65
|
return result;
|
|
66
66
|
}
|
|
67
67
|
|