bin-serde 1.0.0 → 1.2.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/index.ts +20 -2
- package/lib/index.d.ts +3 -0
- package/lib/index.js +22 -12
- package/package.json +2 -1
- package/tsconfig.json +2 -1
package/index.ts
CHANGED
|
@@ -4,8 +4,13 @@ const { pack, unpack } = (utf8 as any).default ?? utf8;
|
|
|
4
4
|
|
|
5
5
|
export class Writer {
|
|
6
6
|
private pos = 0;
|
|
7
|
-
private view
|
|
8
|
-
private bytes
|
|
7
|
+
private view: DataView;
|
|
8
|
+
private bytes: Uint8Array;
|
|
9
|
+
|
|
10
|
+
public constructor() {
|
|
11
|
+
this.view = new DataView(new ArrayBuffer(64));
|
|
12
|
+
this.bytes = new Uint8Array(this.view.buffer);
|
|
13
|
+
}
|
|
9
14
|
|
|
10
15
|
public writeUInt8(val: number) {
|
|
11
16
|
this.ensureSize(1);
|
|
@@ -21,6 +26,13 @@ export class Writer {
|
|
|
21
26
|
return this;
|
|
22
27
|
}
|
|
23
28
|
|
|
29
|
+
public writeUInt64(val: bigint) {
|
|
30
|
+
this.ensureSize(8);
|
|
31
|
+
this.view.setBigUint64(this.pos, val);
|
|
32
|
+
this.pos += 8;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
|
|
24
36
|
public writeUVarint(val: number) {
|
|
25
37
|
if (val < 0x80) {
|
|
26
38
|
this.ensureSize(1);
|
|
@@ -149,6 +161,12 @@ export class Reader {
|
|
|
149
161
|
return val;
|
|
150
162
|
}
|
|
151
163
|
|
|
164
|
+
public readUInt64() {
|
|
165
|
+
const val = this.view.getBigUint64(this.pos);
|
|
166
|
+
this.pos += 8;
|
|
167
|
+
return val;
|
|
168
|
+
}
|
|
169
|
+
|
|
152
170
|
public readUVarint() {
|
|
153
171
|
let val = 0;
|
|
154
172
|
while (true) {
|
package/lib/index.d.ts
CHANGED
|
@@ -2,8 +2,10 @@ export declare class Writer {
|
|
|
2
2
|
private pos;
|
|
3
3
|
private view;
|
|
4
4
|
private bytes;
|
|
5
|
+
constructor();
|
|
5
6
|
writeUInt8(val: number): this;
|
|
6
7
|
writeUInt32(val: number): this;
|
|
8
|
+
writeUInt64(val: bigint): this;
|
|
7
9
|
writeUVarint(val: number): this;
|
|
8
10
|
writeVarint(val: number): this;
|
|
9
11
|
writeFloat(val: number): this;
|
|
@@ -20,6 +22,7 @@ export declare class Reader {
|
|
|
20
22
|
constructor(buf: ArrayBufferView);
|
|
21
23
|
readUInt8(): number;
|
|
22
24
|
readUInt32(): number;
|
|
25
|
+
readUInt64(): bigint;
|
|
23
26
|
readUVarint(): number;
|
|
24
27
|
readVarint(): number;
|
|
25
28
|
readFloat(): number;
|
package/lib/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Reader = exports.Writer = void 0;
|
|
4
|
-
const utf8 = require("utf8-buffer");
|
|
5
|
-
const utf8_buffer_size_1 = require("utf8-buffer-size");
|
|
1
|
+
import * as utf8 from "utf8-buffer";
|
|
2
|
+
import utf8Size from "utf8-buffer-size";
|
|
6
3
|
const { pack, unpack } = utf8.default ?? utf8;
|
|
7
|
-
class Writer {
|
|
4
|
+
export class Writer {
|
|
8
5
|
pos = 0;
|
|
9
|
-
view
|
|
10
|
-
bytes
|
|
6
|
+
view;
|
|
7
|
+
bytes;
|
|
8
|
+
constructor() {
|
|
9
|
+
this.view = new DataView(new ArrayBuffer(64));
|
|
10
|
+
this.bytes = new Uint8Array(this.view.buffer);
|
|
11
|
+
}
|
|
11
12
|
writeUInt8(val) {
|
|
12
13
|
this.ensureSize(1);
|
|
13
14
|
this.view.setUint8(this.pos, val);
|
|
@@ -20,6 +21,12 @@ class Writer {
|
|
|
20
21
|
this.pos += 4;
|
|
21
22
|
return this;
|
|
22
23
|
}
|
|
24
|
+
writeUInt64(val) {
|
|
25
|
+
this.ensureSize(8);
|
|
26
|
+
this.view.setBigUint64(this.pos, val);
|
|
27
|
+
this.pos += 8;
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
23
30
|
writeUVarint(val) {
|
|
24
31
|
if (val < 0x80) {
|
|
25
32
|
this.ensureSize(1);
|
|
@@ -86,7 +93,7 @@ class Writer {
|
|
|
86
93
|
}
|
|
87
94
|
writeString(val) {
|
|
88
95
|
if (val.length > 0) {
|
|
89
|
-
const byteSize = (
|
|
96
|
+
const byteSize = utf8Size(val);
|
|
90
97
|
this.writeUVarint(byteSize);
|
|
91
98
|
this.ensureSize(byteSize);
|
|
92
99
|
pack(val, this.bytes, this.pos);
|
|
@@ -116,8 +123,7 @@ class Writer {
|
|
|
116
123
|
}
|
|
117
124
|
}
|
|
118
125
|
}
|
|
119
|
-
|
|
120
|
-
class Reader {
|
|
126
|
+
export class Reader {
|
|
121
127
|
pos = 0;
|
|
122
128
|
view;
|
|
123
129
|
bytes;
|
|
@@ -135,6 +141,11 @@ class Reader {
|
|
|
135
141
|
this.pos += 4;
|
|
136
142
|
return val;
|
|
137
143
|
}
|
|
144
|
+
readUInt64() {
|
|
145
|
+
const val = this.view.getBigUint64(this.pos);
|
|
146
|
+
this.pos += 8;
|
|
147
|
+
return val;
|
|
148
|
+
}
|
|
138
149
|
readUVarint() {
|
|
139
150
|
let val = 0;
|
|
140
151
|
while (true) {
|
|
@@ -184,4 +195,3 @@ class Reader {
|
|
|
184
195
|
return this.view.byteLength - this.pos;
|
|
185
196
|
}
|
|
186
197
|
}
|
|
187
|
-
exports.Reader = Reader;
|
package/package.json
CHANGED