bin-serde 1.2.0 → 1.2.1

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
@@ -1,6 +1,5 @@
1
- import * as utf8 from "utf8-buffer";
1
+ import { pack, unpack } from "./utf8-buffer.js";
2
2
  import utf8Size from "utf8-buffer-size";
3
- const { pack, unpack } = (utf8 as any).default ?? utf8;
4
3
 
5
4
  export class Writer {
6
5
  private pos = 0;
package/lib/index.d.ts CHANGED
@@ -27,7 +27,7 @@ export declare class Reader {
27
27
  readVarint(): number;
28
28
  readFloat(): number;
29
29
  readBits(numBits: number): boolean[];
30
- readString(): any;
30
+ readString(): string;
31
31
  readBuffer(numBytes: number): Uint8Array;
32
32
  remaining(): number;
33
33
  }
package/lib/index.js CHANGED
@@ -1,6 +1,5 @@
1
- import * as utf8 from "utf8-buffer";
1
+ import { pack, unpack } from "./utf8-buffer.js";
2
2
  import utf8Size from "utf8-buffer-size";
3
- const { pack, unpack } = utf8.default ?? utf8;
4
3
  export class Writer {
5
4
  pos = 0;
6
5
  view;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
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",
@@ -27,7 +27,6 @@
27
27
  },
28
28
  "homepage": "https://github.com/hathora/bin-serde#readme",
29
29
  "dependencies": {
30
- "utf8-buffer": "^1.0.0",
31
30
  "utf8-buffer-size": "^0.0.4"
32
31
  },
33
32
  "devDependencies": {
@@ -0,0 +1,27 @@
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 ADDED
@@ -0,0 +1,145 @@
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
+ }