bin-serde 1.2.1 → 1.3.0
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/LICENSE +21 -0
- package/index.ts +4 -2
- package/lib/index.d.ts +1 -1
- package/lib/index.js +4 -2
- package/lib/utf8-buffer.d.ts +28 -0
- package/lib/utf8-buffer.js +150 -0
- package/package.json +1 -1
- package/tsconfig.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Hathora Inc
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.ts
CHANGED
|
@@ -201,8 +201,10 @@ export class Reader {
|
|
|
201
201
|
return bits;
|
|
202
202
|
}
|
|
203
203
|
|
|
204
|
-
public readString() {
|
|
205
|
-
|
|
204
|
+
public readString(len?: number) {
|
|
205
|
+
if (len === undefined) {
|
|
206
|
+
len = this.readUVarint();
|
|
207
|
+
}
|
|
206
208
|
if (len === 0) {
|
|
207
209
|
return "";
|
|
208
210
|
}
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -176,8 +176,10 @@ export class Reader {
|
|
|
176
176
|
this.pos += numBytes;
|
|
177
177
|
return bits;
|
|
178
178
|
}
|
|
179
|
-
readString() {
|
|
180
|
-
|
|
179
|
+
readString(len) {
|
|
180
|
+
if (len === undefined) {
|
|
181
|
+
len = this.readUVarint();
|
|
182
|
+
}
|
|
181
183
|
if (len === 0) {
|
|
182
184
|
return "";
|
|
183
185
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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;
|
|
@@ -0,0 +1,150 @@
|
|
|
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
|
+
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);
|
|
56
|
+
}
|
|
57
|
+
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
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return str;
|
|
105
|
+
}
|
|
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
|
+
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++;
|
|
121
|
+
}
|
|
122
|
+
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
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return index;
|
|
150
|
+
}
|
package/package.json
CHANGED