bin-serde 1.6.4 → 1.6.6

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 CHANGED
@@ -224,11 +224,10 @@ export class Writer {
224
224
  export class Reader {
225
225
  private pos = 0;
226
226
  private bytes: Uint8Array;
227
- private view: DataView;
227
+ private _view: DataView | null = null; // lazily allocated
228
228
 
229
229
  constructor(buf: Uint8Array) {
230
230
  this.bytes = buf;
231
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
232
231
  }
233
232
 
234
233
  readUInt8() {
@@ -315,4 +314,11 @@ export class Reader {
315
314
  remaining() {
316
315
  return this.bytes.length - this.pos;
317
316
  }
317
+
318
+ private get view(): DataView {
319
+ if (!this._view) {
320
+ this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
321
+ }
322
+ return this._view;
323
+ }
318
324
  }
package/lib/index.d.ts CHANGED
@@ -22,7 +22,7 @@ export declare class Writer {
22
22
  export declare class Reader {
23
23
  private pos;
24
24
  private bytes;
25
- private view;
25
+ private _view;
26
26
  constructor(buf: Uint8Array);
27
27
  readUInt8(): number;
28
28
  readUInt16(): number;
@@ -35,4 +35,5 @@ export declare class Reader {
35
35
  readStringUtf8(len?: number): string;
36
36
  readBuffer(numBytes: number): Uint8Array;
37
37
  remaining(): number;
38
+ private get view();
38
39
  }
package/lib/index.js CHANGED
@@ -199,10 +199,9 @@ export class Writer {
199
199
  export class Reader {
200
200
  pos = 0;
201
201
  bytes;
202
- view;
202
+ _view = null; // lazily allocated
203
203
  constructor(buf) {
204
204
  this.bytes = buf;
205
- this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
206
205
  }
207
206
  readUInt8() {
208
207
  return this.bytes[this.pos++];
@@ -278,4 +277,10 @@ export class Reader {
278
277
  remaining() {
279
278
  return this.bytes.length - this.pos;
280
279
  }
280
+ get view() {
281
+ if (!this._view) {
282
+ this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
283
+ }
284
+ return this._view;
285
+ }
281
286
  }
@@ -1,28 +1,2 @@
1
- /**
2
- * @fileoverview Functions to serialize and deserialize UTF-8 strings.
3
- * @see https://github.com/rochars/utf8-buffer
4
- * @see https://encoding.spec.whatwg.org/#the-encoding
5
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
6
- */
7
- /** @module utf8-buffer */
8
- /**
9
- * Read a string of UTF-8 characters from a byte buffer.
10
- * Invalid characters are replaced with 'REPLACEMENT CHARACTER' (U+FFFD).
11
- * @see https://encoding.spec.whatwg.org/#the-encoding
12
- * @see https://stackoverflow.com/a/34926911
13
- * @param {!Uint8Array|!Array<number>} buffer A byte buffer.
14
- * @param {number=} start The buffer index to start reading.
15
- * @param {?number=} end The buffer index to stop reading.
16
- * Assumes the buffer length if undefined.
17
- * @return {string}
18
- */
19
- export function unpack(buffer: Uint8Array | Array<number>, start?: number | undefined, end?: (number | null) | undefined): string;
20
- /**
21
- * Write a string of UTF-8 characters to a byte buffer.
22
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
23
- * @param {string} str The string to pack.
24
- * @param {!Uint8Array|!Array<number>} buffer The buffer to pack the string to.
25
- * @param {number=} index The buffer index to start writing.
26
- * @return {number} The next index to write in the buffer.
27
- */
28
- export function pack(str: string, buffer: Uint8Array | Array<number>, index?: number | undefined): number;
1
+ export declare function unpack(buffer: Uint8Array, start?: number, end?: number): string;
2
+ export declare function pack(str: string, buffer: Uint8Array, index?: number): number;
@@ -1,149 +1,61 @@
1
- /*
2
- * Copyright (c) 2018 Rafael da Silva Rocha.
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining
5
- * a copy of this software and associated documentation files (the
6
- * "Software"), to deal in the Software without restriction, including
7
- * without limitation the rights to use, copy, modify, merge, publish,
8
- * distribute, sublicense, and/or sell copies of the Software, and to
9
- * permit persons to whom the Software is furnished to do so, subject to
10
- * the following conditions:
11
- *
12
- * The above copyright notice and this permission notice shall be
13
- * included in all copies or substantial portions of the Software.
14
- *
15
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- *
23
- */
24
- /**
25
- * @fileoverview Functions to serialize and deserialize UTF-8 strings.
26
- * @see https://github.com/rochars/utf8-buffer
27
- * @see https://encoding.spec.whatwg.org/#the-encoding
28
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
29
- */
30
- /** @module utf8-buffer */
31
- /**
32
- * Read a string of UTF-8 characters from a byte buffer.
33
- * Invalid characters are replaced with 'REPLACEMENT CHARACTER' (U+FFFD).
34
- * @see https://encoding.spec.whatwg.org/#the-encoding
35
- * @see https://stackoverflow.com/a/34926911
36
- * @param {!Uint8Array|!Array<number>} buffer A byte buffer.
37
- * @param {number=} start The buffer index to start reading.
38
- * @param {?number=} end The buffer index to stop reading.
39
- * Assumes the buffer length if undefined.
40
- * @return {string}
41
- */
42
1
  export function unpack(buffer, start = 0, end = buffer.length) {
43
- /** @type {string} */
44
- let str = "";
45
- for (let index = start; index < end;) {
46
- /** @type {number} */
47
- let lowerBoundary = 0x80;
48
- /** @type {number} */
49
- let upperBoundary = 0xbf;
50
- /** @type {boolean} */
51
- let replace = false;
52
- /** @type {number} */
53
- let charCode = buffer[index++];
54
- if (charCode >= 0x00 && charCode <= 0x7f) {
55
- str += String.fromCharCode(charCode);
2
+ const len = end - start;
3
+ if (len < 1)
4
+ return "";
5
+ const chunks = [];
6
+ const chunk = [];
7
+ let i = 0;
8
+ while (start < end) {
9
+ let t = buffer[start++];
10
+ if (t < 128) {
11
+ chunk[i++] = t;
12
+ }
13
+ else if (t > 191 && t < 224) {
14
+ chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
15
+ }
16
+ else if (t > 239 && t < 245) {
17
+ t =
18
+ (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
19
+ 0x10000;
20
+ chunk[i++] = 0xd800 + (t >> 10);
21
+ chunk[i++] = 0xdc00 + (t & 1023);
56
22
  }
57
23
  else {
58
- /** @type {number} */
59
- let count = 0;
60
- if (charCode >= 0xc2 && charCode <= 0xdf) {
61
- count = 1;
62
- }
63
- else if (charCode >= 0xe0 && charCode <= 0xef) {
64
- count = 2;
65
- if (buffer[index] === 0xe0) {
66
- lowerBoundary = 0xa0;
67
- }
68
- if (buffer[index] === 0xed) {
69
- upperBoundary = 0x9f;
70
- }
71
- }
72
- else if (charCode >= 0xf0 && charCode <= 0xf4) {
73
- count = 3;
74
- if (buffer[index] === 0xf0) {
75
- lowerBoundary = 0x90;
76
- }
77
- if (buffer[index] === 0xf4) {
78
- upperBoundary = 0x8f;
79
- }
80
- }
81
- else {
82
- replace = true;
83
- }
84
- charCode = charCode & ((1 << (8 - count - 1)) - 1);
85
- for (let i = 0; i < count; i++) {
86
- if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) {
87
- replace = true;
88
- }
89
- charCode = (charCode << 6) | (buffer[index] & 0x3f);
90
- index++;
91
- }
92
- if (replace) {
93
- str += String.fromCharCode(0xfffd);
94
- }
95
- else if (charCode <= 0xffff) {
96
- str += String.fromCharCode(charCode);
97
- }
98
- else {
99
- charCode -= 0x10000;
100
- str += String.fromCharCode(((charCode >> 10) & 0x3ff) + 0xd800, (charCode & 0x3ff) + 0xdc00);
101
- }
24
+ chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
25
+ }
26
+ if (i > 8191) {
27
+ chunks.push(String.fromCharCode.apply(String, chunk));
28
+ i = 0;
102
29
  }
103
30
  }
104
- return str;
31
+ if (i > 0) {
32
+ chunks.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
33
+ }
34
+ return chunks.join("");
105
35
  }
106
- /**
107
- * Write a string of UTF-8 characters to a byte buffer.
108
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
109
- * @param {string} str The string to pack.
110
- * @param {!Uint8Array|!Array<number>} buffer The buffer to pack the string to.
111
- * @param {number=} index The buffer index to start writing.
112
- * @return {number} The next index to write in the buffer.
113
- */
114
36
  export function pack(str, buffer, index = 0) {
115
- for (let i = 0, len = str.length; i < len; i++) {
116
- /** @type {number} */
117
- let codePoint = str.codePointAt(i);
118
- if (codePoint < 128) {
119
- buffer[index] = codePoint;
120
- index++;
37
+ let c1, c2;
38
+ for (let i = 0; i < str.length; i++) {
39
+ c1 = str.charCodeAt(i);
40
+ if (c1 < 128) {
41
+ buffer[index++] = c1;
42
+ }
43
+ else if (c1 < 2048) {
44
+ buffer[index++] = (c1 >> 6) | 192;
45
+ buffer[index++] = (c1 & 63) | 128;
46
+ }
47
+ else if ((c1 & 0xfc00) === 0xd800 && ((c2 = str.charCodeAt(i + 1)) & 0xfc00) === 0xdc00) {
48
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
49
+ i++;
50
+ buffer[index++] = (c1 >> 18) | 240;
51
+ buffer[index++] = ((c1 >> 12) & 63) | 128;
52
+ buffer[index++] = ((c1 >> 6) & 63) | 128;
53
+ buffer[index++] = (c1 & 63) | 128;
121
54
  }
122
55
  else {
123
- /** @type {number} */
124
- let count = 0;
125
- /** @type {number} */
126
- let offset = 0;
127
- if (codePoint <= 0x07ff) {
128
- count = 1;
129
- offset = 0xc0;
130
- }
131
- else if (codePoint <= 0xffff) {
132
- count = 2;
133
- offset = 0xe0;
134
- }
135
- else if (codePoint <= 0x10ffff) {
136
- count = 3;
137
- offset = 0xf0;
138
- i++;
139
- }
140
- buffer[index] = (codePoint >> (6 * count)) + offset;
141
- index++;
142
- while (count > 0) {
143
- buffer[index] = 0x80 | ((codePoint >> (6 * (count - 1))) & 0x3f);
144
- index++;
145
- count--;
146
- }
56
+ buffer[index++] = (c1 >> 12) | 224;
57
+ buffer[index++] = ((c1 >> 6) & 63) | 128;
58
+ buffer[index++] = (c1 & 63) | 128;
147
59
  }
148
60
  }
149
61
  return index;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.6.4",
3
+ "version": "1.6.6",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
package/tsconfig.json CHANGED
@@ -4,7 +4,6 @@
4
4
  "module": "esnext",
5
5
  "moduleResolution": "node",
6
6
  "declaration": true,
7
- "allowJs": true,
8
7
  "outDir": "./lib",
9
8
  "strict": true
10
9
  }
package/utf8-buffer.ts ADDED
@@ -0,0 +1,59 @@
1
+ export function unpack(buffer: Uint8Array, start = 0, end = buffer.length): string {
2
+ const len = end - start;
3
+ if (len < 1) return "";
4
+
5
+ const chunks: string[] = [];
6
+ const chunk: number[] = [];
7
+ let i = 0;
8
+
9
+ while (start < end) {
10
+ let t = buffer[start++];
11
+ if (t < 128) {
12
+ chunk[i++] = t;
13
+ } else if (t > 191 && t < 224) {
14
+ chunk[i++] = ((t & 31) << 6) | (buffer[start++] & 63);
15
+ } else if (t > 239 && t < 245) {
16
+ t =
17
+ (((t & 7) << 18) | ((buffer[start++] & 63) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63)) -
18
+ 0x10000;
19
+ chunk[i++] = 0xd800 + (t >> 10);
20
+ chunk[i++] = 0xdc00 + (t & 1023);
21
+ } else {
22
+ chunk[i++] = ((t & 15) << 12) | ((buffer[start++] & 63) << 6) | (buffer[start++] & 63);
23
+ }
24
+ if (i > 8191) {
25
+ chunks.push(String.fromCharCode.apply(String, chunk));
26
+ i = 0;
27
+ }
28
+ }
29
+
30
+ if (i > 0) {
31
+ chunks.push(String.fromCharCode.apply(String, chunk.slice(0, i)));
32
+ }
33
+ return chunks.join("");
34
+ }
35
+
36
+ export function pack(str: string, buffer: Uint8Array, index = 0): number {
37
+ let c1: number, c2: number;
38
+ for (let i = 0; i < str.length; i++) {
39
+ c1 = str.charCodeAt(i);
40
+ if (c1 < 128) {
41
+ buffer[index++] = c1;
42
+ } else if (c1 < 2048) {
43
+ buffer[index++] = (c1 >> 6) | 192;
44
+ buffer[index++] = (c1 & 63) | 128;
45
+ } else if ((c1 & 0xfc00) === 0xd800 && ((c2 = str.charCodeAt(i + 1)) & 0xfc00) === 0xdc00) {
46
+ c1 = 0x10000 + ((c1 & 0x03ff) << 10) + (c2 & 0x03ff);
47
+ i++;
48
+ buffer[index++] = (c1 >> 18) | 240;
49
+ buffer[index++] = ((c1 >> 12) & 63) | 128;
50
+ buffer[index++] = ((c1 >> 6) & 63) | 128;
51
+ buffer[index++] = (c1 & 63) | 128;
52
+ } else {
53
+ buffer[index++] = (c1 >> 12) | 224;
54
+ buffer[index++] = ((c1 >> 6) & 63) | 128;
55
+ buffer[index++] = (c1 & 63) | 128;
56
+ }
57
+ }
58
+ return index;
59
+ }
package/utf8-buffer.d.ts DELETED
@@ -1,27 +0,0 @@
1
- // Type definitions for utf8-buffer 1.0.0
2
- // Project: https://github.com/rochars/byte-data
3
- // Definitions by: Rafael da Silva Rocha <https://github.com/rochars>
4
- // Definitions: https://github.com/rochars/utf8-buffer
5
-
6
- /**
7
- * Read a string of UTF-8 characters from a byte buffer.
8
- * Invalid characters are replaced with 'REPLACEMENT CHARACTER' (U+FFFD).
9
- * @see https://encoding.spec.whatwg.org/#the-encoding
10
- * @see https://stackoverflow.com/a/34926911
11
- * @param {!Uint8Array|!Array<number>} buffer A byte buffer.
12
- * @param {number=} start The buffer index to start reading.
13
- * @param {?number=} end The buffer index to stop reading.
14
- * Assumes the buffer length if undefined.
15
- * @return {string}
16
- */
17
- export function unpack(buffer: Uint8Array | number[], start: number, end?: number): string;
18
-
19
- /**
20
- * Write a string of UTF-8 characters to a byte buffer.
21
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
22
- * @param {string} str The string to pack.
23
- * @param {!Uint8Array|!Array<number>} buffer The buffer to pack the string to.
24
- * @param {number=} index The buffer index to start writing.
25
- * @return {number} The next index to write in the buffer.
26
- */
27
- export function pack(str: string, buffer: Uint8Array | number[], index?: number): number;
package/utf8-buffer.js DELETED
@@ -1,145 +0,0 @@
1
- /*
2
- * Copyright (c) 2018 Rafael da Silva Rocha.
3
- *
4
- * Permission is hereby granted, free of charge, to any person obtaining
5
- * a copy of this software and associated documentation files (the
6
- * "Software"), to deal in the Software without restriction, including
7
- * without limitation the rights to use, copy, modify, merge, publish,
8
- * distribute, sublicense, and/or sell copies of the Software, and to
9
- * permit persons to whom the Software is furnished to do so, subject to
10
- * the following conditions:
11
- *
12
- * The above copyright notice and this permission notice shall be
13
- * included in all copies or substantial portions of the Software.
14
- *
15
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
- *
23
- */
24
-
25
- /**
26
- * @fileoverview Functions to serialize and deserialize UTF-8 strings.
27
- * @see https://github.com/rochars/utf8-buffer
28
- * @see https://encoding.spec.whatwg.org/#the-encoding
29
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
30
- */
31
-
32
- /** @module utf8-buffer */
33
-
34
- /**
35
- * Read a string of UTF-8 characters from a byte buffer.
36
- * Invalid characters are replaced with 'REPLACEMENT CHARACTER' (U+FFFD).
37
- * @see https://encoding.spec.whatwg.org/#the-encoding
38
- * @see https://stackoverflow.com/a/34926911
39
- * @param {!Uint8Array|!Array<number>} buffer A byte buffer.
40
- * @param {number=} start The buffer index to start reading.
41
- * @param {?number=} end The buffer index to stop reading.
42
- * Assumes the buffer length if undefined.
43
- * @return {string}
44
- */
45
- export function unpack(buffer, start = 0, end = buffer.length) {
46
- /** @type {string} */
47
- let str = "";
48
- for (let index = start; index < end; ) {
49
- /** @type {number} */
50
- let lowerBoundary = 0x80;
51
- /** @type {number} */
52
- let upperBoundary = 0xbf;
53
- /** @type {boolean} */
54
- let replace = false;
55
- /** @type {number} */
56
- let charCode = buffer[index++];
57
- if (charCode >= 0x00 && charCode <= 0x7f) {
58
- str += String.fromCharCode(charCode);
59
- } else {
60
- /** @type {number} */
61
- let count = 0;
62
- if (charCode >= 0xc2 && charCode <= 0xdf) {
63
- count = 1;
64
- } else if (charCode >= 0xe0 && charCode <= 0xef) {
65
- count = 2;
66
- if (buffer[index] === 0xe0) {
67
- lowerBoundary = 0xa0;
68
- }
69
- if (buffer[index] === 0xed) {
70
- upperBoundary = 0x9f;
71
- }
72
- } else if (charCode >= 0xf0 && charCode <= 0xf4) {
73
- count = 3;
74
- if (buffer[index] === 0xf0) {
75
- lowerBoundary = 0x90;
76
- }
77
- if (buffer[index] === 0xf4) {
78
- upperBoundary = 0x8f;
79
- }
80
- } else {
81
- replace = true;
82
- }
83
- charCode = charCode & ((1 << (8 - count - 1)) - 1);
84
- for (let i = 0; i < count; i++) {
85
- if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) {
86
- replace = true;
87
- }
88
- charCode = (charCode << 6) | (buffer[index] & 0x3f);
89
- index++;
90
- }
91
- if (replace) {
92
- str += String.fromCharCode(0xfffd);
93
- } else if (charCode <= 0xffff) {
94
- str += String.fromCharCode(charCode);
95
- } else {
96
- charCode -= 0x10000;
97
- str += String.fromCharCode(((charCode >> 10) & 0x3ff) + 0xd800, (charCode & 0x3ff) + 0xdc00);
98
- }
99
- }
100
- }
101
- return str;
102
- }
103
-
104
- /**
105
- * Write a string of UTF-8 characters to a byte buffer.
106
- * @see https://encoding.spec.whatwg.org/#utf-8-encoder
107
- * @param {string} str The string to pack.
108
- * @param {!Uint8Array|!Array<number>} buffer The buffer to pack the string to.
109
- * @param {number=} index The buffer index to start writing.
110
- * @return {number} The next index to write in the buffer.
111
- */
112
- export function pack(str, buffer, index = 0) {
113
- for (let i = 0, len = str.length; i < len; i++) {
114
- /** @type {number} */
115
- let codePoint = str.codePointAt(i);
116
- if (codePoint < 128) {
117
- buffer[index] = codePoint;
118
- index++;
119
- } else {
120
- /** @type {number} */
121
- let count = 0;
122
- /** @type {number} */
123
- let offset = 0;
124
- if (codePoint <= 0x07ff) {
125
- count = 1;
126
- offset = 0xc0;
127
- } else if (codePoint <= 0xffff) {
128
- count = 2;
129
- offset = 0xe0;
130
- } else if (codePoint <= 0x10ffff) {
131
- count = 3;
132
- offset = 0xf0;
133
- i++;
134
- }
135
- buffer[index] = (codePoint >> (6 * count)) + offset;
136
- index++;
137
- while (count > 0) {
138
- buffer[index] = 0x80 | ((codePoint >> (6 * (count - 1))) & 0x3f);
139
- index++;
140
- count--;
141
- }
142
- }
143
- }
144
- return index;
145
- }