bin-serde 1.7.5 → 1.7.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.
- package/index.ts +3 -2
- package/lib/index.js +2 -2
- package/lib/utf8-buffer.js +30 -6
- package/package.json +1 -1
- package/utf8-buffer.ts +28 -6
package/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ let slabOffset = 0;
|
|
|
9
9
|
|
|
10
10
|
const f32 = new Float32Array(1);
|
|
11
11
|
const f32u8 = new Uint8Array(f32.buffer);
|
|
12
|
+
const copyBuffer =
|
|
13
|
+
typeof Buffer !== "undefined" ? (buf: Uint8Array) => Buffer.from(buf) : (buf: Uint8Array) => buf.slice();
|
|
12
14
|
|
|
13
15
|
function allocFromSlab(size: number): Uint8Array {
|
|
14
16
|
if (size > MAX_POOLED) {
|
|
@@ -154,8 +156,7 @@ export class Writer {
|
|
|
154
156
|
}
|
|
155
157
|
|
|
156
158
|
toBuffer() {
|
|
157
|
-
|
|
158
|
-
return typeof Buffer !== "undefined" ? Buffer.from(view) : view.slice();
|
|
159
|
+
return copyBuffer(this.bytes.subarray(0, this.pos));
|
|
159
160
|
}
|
|
160
161
|
|
|
161
162
|
reset() {
|
package/lib/index.js
CHANGED
|
@@ -6,6 +6,7 @@ let slab = allocUint8Array(SLAB_SIZE);
|
|
|
6
6
|
let slabOffset = 0;
|
|
7
7
|
const f32 = new Float32Array(1);
|
|
8
8
|
const f32u8 = new Uint8Array(f32.buffer);
|
|
9
|
+
const copyBuffer = typeof Buffer !== "undefined" ? (buf) => Buffer.from(buf) : (buf) => buf.slice();
|
|
9
10
|
function allocFromSlab(size) {
|
|
10
11
|
if (size > MAX_POOLED) {
|
|
11
12
|
// Too large for pool, allocate directly
|
|
@@ -136,8 +137,7 @@ export class Writer {
|
|
|
136
137
|
return this;
|
|
137
138
|
}
|
|
138
139
|
toBuffer() {
|
|
139
|
-
|
|
140
|
-
return typeof Buffer !== "undefined" ? Buffer.from(view) : view.slice();
|
|
140
|
+
return copyBuffer(this.bytes.subarray(0, this.pos));
|
|
141
141
|
}
|
|
142
142
|
reset() {
|
|
143
143
|
this.pos = 0;
|
package/lib/utf8-buffer.js
CHANGED
|
@@ -1,17 +1,41 @@
|
|
|
1
1
|
const textDecoder = new TextDecoder();
|
|
2
2
|
const hasBuffer = typeof Buffer !== "undefined";
|
|
3
3
|
export function unpack(buffer, start = 0, end = buffer.length) {
|
|
4
|
-
|
|
4
|
+
const len = end - start;
|
|
5
|
+
if (len < 1)
|
|
5
6
|
return "";
|
|
6
|
-
//
|
|
7
|
+
// Node.js: use Buffer.toString()
|
|
7
8
|
if (hasBuffer && Buffer.isBuffer(buffer)) {
|
|
8
9
|
return buffer.toString("utf8", start, end);
|
|
9
10
|
}
|
|
10
|
-
//
|
|
11
|
-
if (
|
|
12
|
-
return textDecoder.decode(buffer);
|
|
11
|
+
// Long strings: use TextDecoder
|
|
12
|
+
if (len > 64) {
|
|
13
|
+
return textDecoder.decode(buffer.subarray(start, end));
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
// Short strings: use pure JS
|
|
16
|
+
const chunks = [];
|
|
17
|
+
let i = 0;
|
|
18
|
+
let t;
|
|
19
|
+
while (start < end) {
|
|
20
|
+
t = buffer[start++];
|
|
21
|
+
if (t < 128) {
|
|
22
|
+
chunks[i++] = t;
|
|
23
|
+
}
|
|
24
|
+
else if (t > 191 && t < 224) {
|
|
25
|
+
chunks[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
|
|
26
|
+
}
|
|
27
|
+
else if (t > 239 && t < 365) {
|
|
28
|
+
t =
|
|
29
|
+
(((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
|
|
30
|
+
0x10000;
|
|
31
|
+
chunks[i++] = 0xd800 + (t >> 10);
|
|
32
|
+
chunks[i++] = 0xdc00 + (t & 1023);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
chunks[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return String.fromCharCode.apply(String, chunks);
|
|
15
39
|
}
|
|
16
40
|
export function pack(str, buffer, index = 0) {
|
|
17
41
|
let c1, c2;
|
package/package.json
CHANGED
package/utf8-buffer.ts
CHANGED
|
@@ -2,18 +2,40 @@ const textDecoder = new TextDecoder();
|
|
|
2
2
|
const hasBuffer = typeof Buffer !== "undefined";
|
|
3
3
|
|
|
4
4
|
export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
|
|
5
|
-
|
|
5
|
+
const len = end - start;
|
|
6
|
+
if (len < 1) return "";
|
|
6
7
|
|
|
7
|
-
//
|
|
8
|
+
// Node.js: use Buffer.toString()
|
|
8
9
|
if (hasBuffer && Buffer.isBuffer(buffer)) {
|
|
9
10
|
return buffer.toString("utf8", start, end);
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
//
|
|
13
|
-
if (
|
|
14
|
-
return textDecoder.decode(buffer);
|
|
13
|
+
// Long strings: use TextDecoder
|
|
14
|
+
if (len > 64) {
|
|
15
|
+
return textDecoder.decode(buffer.subarray(start, end));
|
|
15
16
|
}
|
|
16
|
-
|
|
17
|
+
|
|
18
|
+
// Short strings: use pure JS
|
|
19
|
+
const chunks: number[] = [];
|
|
20
|
+
let i = 0;
|
|
21
|
+
let t: number;
|
|
22
|
+
while (start < end) {
|
|
23
|
+
t = buffer[start++]!;
|
|
24
|
+
if (t < 128) {
|
|
25
|
+
chunks[i++] = t;
|
|
26
|
+
} else if (t > 191 && t < 224) {
|
|
27
|
+
chunks[i++] = ((t & 31) << 6) | (buffer[start++]! & 63);
|
|
28
|
+
} else if (t > 239 && t < 365) {
|
|
29
|
+
t =
|
|
30
|
+
(((t & 7) << 18) | ((buffer[start++]! & 63) << 12) | ((buffer[start++]! & 63) << 6) | (buffer[start++]! & 63)) -
|
|
31
|
+
0x10000;
|
|
32
|
+
chunks[i++] = 0xd800 + (t >> 10);
|
|
33
|
+
chunks[i++] = 0xdc00 + (t & 1023);
|
|
34
|
+
} else {
|
|
35
|
+
chunks[i++] = ((t & 15) << 12) | ((buffer[start++]! & 63) << 6) | (buffer[start++]! & 63);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return String.fromCharCode.apply(String, chunks);
|
|
17
39
|
}
|
|
18
40
|
|
|
19
41
|
export function pack(str: string, buffer: Uint8Array, index = 0): number {
|