bin-serde 1.2.2 → 1.4.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 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
@@ -103,7 +103,19 @@ export class Writer {
103
103
  return this;
104
104
  }
105
105
 
106
- public writeString(val: string) {
106
+ public writeStringAscii(val: string) {
107
+ if (val.length === 0) {
108
+ this.writeUInt8(0);
109
+ return this;
110
+ }
111
+ this.ensureSize(val.length);
112
+ for (let i = 0; i < val.length; i++) {
113
+ this.view.setUint8(this.pos++, val.charCodeAt(i));
114
+ }
115
+ return this;
116
+ }
117
+
118
+ public writeStringUtf8(val: string) {
107
119
  if (val.length > 0) {
108
120
  const byteSize = utf8Size(val);
109
121
  this.writeUVarint(byteSize);
@@ -201,8 +213,21 @@ export class Reader {
201
213
  return bits;
202
214
  }
203
215
 
204
- public readString() {
205
- const len = this.readUVarint();
216
+ public readStringAscii(len: number) {
217
+ if (len === 0) {
218
+ return "";
219
+ }
220
+ let val = "";
221
+ for (let i = 0; i < len; i++) {
222
+ val += String.fromCharCode(this.view.getUint8(this.pos++));
223
+ }
224
+ return val;
225
+ }
226
+
227
+ public readStringUtf8(len?: number) {
228
+ if (len === undefined) {
229
+ len = this.readUVarint();
230
+ }
206
231
  if (len === 0) {
207
232
  return "";
208
233
  }
package/lib/index.d.ts CHANGED
@@ -10,7 +10,8 @@ export declare class Writer {
10
10
  writeVarint(val: number): this;
11
11
  writeFloat(val: number): this;
12
12
  writeBits(bits: boolean[]): this;
13
- writeString(val: string): this;
13
+ writeStringAscii(val: string): this;
14
+ writeStringUtf8(val: string): this;
14
15
  writeBuffer(buf: Uint8Array): this;
15
16
  toBuffer(): Uint8Array;
16
17
  private ensureSize;
@@ -27,7 +28,8 @@ export declare class Reader {
27
28
  readVarint(): number;
28
29
  readFloat(): number;
29
30
  readBits(numBits: number): boolean[];
30
- readString(): string;
31
+ readStringAscii(len: number): string;
32
+ readStringUtf8(len?: number): string;
31
33
  readBuffer(numBytes: number): Uint8Array;
32
34
  remaining(): number;
33
35
  }
package/lib/index.js CHANGED
@@ -90,7 +90,18 @@ export class Writer {
90
90
  }
91
91
  return this;
92
92
  }
93
- writeString(val) {
93
+ writeStringAscii(val) {
94
+ if (val.length === 0) {
95
+ this.writeUInt8(0);
96
+ return this;
97
+ }
98
+ this.ensureSize(val.length);
99
+ for (let i = 0; i < val.length; i++) {
100
+ this.view.setUint8(this.pos++, val.charCodeAt(i));
101
+ }
102
+ return this;
103
+ }
104
+ writeStringUtf8(val) {
94
105
  if (val.length > 0) {
95
106
  const byteSize = utf8Size(val);
96
107
  this.writeUVarint(byteSize);
@@ -176,8 +187,20 @@ export class Reader {
176
187
  this.pos += numBytes;
177
188
  return bits;
178
189
  }
179
- readString() {
180
- const len = this.readUVarint();
190
+ readStringAscii(len) {
191
+ if (len === 0) {
192
+ return "";
193
+ }
194
+ let val = "";
195
+ for (let i = 0; i < len; i++) {
196
+ val += String.fromCharCode(this.view.getUint8(this.pos++));
197
+ }
198
+ return val;
199
+ }
200
+ readStringUtf8(len) {
201
+ if (len === undefined) {
202
+ len = this.readUVarint();
203
+ }
181
204
  if (len === 0) {
182
205
  return "";
183
206
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.2.2",
3
+ "version": "1.4.0",
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",