-
Notifications
You must be signed in to change notification settings - Fork 1
/
serial-buffer.mjs
183 lines (159 loc) · 4.41 KB
/
serial-buffer.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
export class SerialBuffer {
/**
* The data buffer.
* @type {Buffer}
*/
data;
/**
* The current offset in the buffer.
* @type {number}
*/
#offset = 0;
textEnc = new TextEncoder();
textDec = new TextDecoder();
constructor(data) {
this.data = data;
}
get offset() {
return this.#offset;
}
get filled() {
return this.data.subarray(0, this.#offset);
}
/**
* Reads an 8-bit unsigned integer from the buffer and advances the offset by 1 byte.
*
* @return {number} The 8-bit unsigned integer read from the buffer.
*/
readUint8() {
const value = this.data[this.#offset];
this.#offset += 1;
return value;
}
/**
* Reads a 32-bit unsigned integer from the buffer and advances the offset by 4 bytes.
*
* @return {number} The 32-bit unsigned integer read from the buffer.
*/
readUint32() {
const value = this.data.readUInt32LE(this.#offset);
this.#offset += 4;
return value;
}
/**
* Reads a 64-bit unsigned integer from the data buffer and advances the offset by 8 bytes.
*
* @returns {BigInt} The 64-bit unsigned integer read from the buffer.
*/
readUint64() {
const value = this.data.readBigUInt64LE(this.#offset);
this.#offset += 8;
return value;
}
writeUInt16(number) {
this.data.writeUInt16LE(number, this.#offset);
this.#offset += 2;
}
/**
* Writes a buffer to the data buffer.
* @param buffer {Buffer} - The buffer to write
*/
writeBuffer(buffer) {
buffer.copy(this.data, this.#offset);
this.#offset += buffer.length;
}
/**
* Writes an array of 8-bit unsigned integers to the buffer.
* @param array {Uint8Array} - The array to write
* @return {void}
*/
writeUint8Array(array) {
const buffer = Buffer.from(array);
// this.data.writeUInt32LE(buffer.length, this.#offset);
// this.#offset += 4;
buffer.copy(this.data, this.#offset);
this.#offset += buffer.length;
}
writeUInt64(number) {
this.data.writeBigUInt64LE(number, this.#offset);
this.#offset += 8;
}
writeUInt32(number) {
this.data.writeUInt32LE(number, this.#offset);
this.#offset += 4;
}
writeUInt8(number) {
this.data[this.#offset] = number;
this.#offset += 1;
}
/**
* Writes a string to the buffer.
* @param text {string} - The string to write
* @return {void}
*/
writeString(text) {
const bytes = this.textEnc.encode(text);
const buffer = Buffer.from(bytes);
this.writeVarUInt32(buffer.length, this.#offset);
buffer.copy(this.data, this.#offset);
this.#offset += buffer.length;
}
/**
* Writes a variable length unsigned 32-bit integer to the buffer.
* @param value {number} - The value to write
* @return {void}
*/
writeVarUInt32(value) {
while (true) {
if (value >>> 7) {
this.writeUInt8(0x80 | (value & 0x7f));
value = value >>> 7;
} else {
this.writeUInt8(value);
break;
}
}
}
/**
* Reads a variable length unsigned 32-bit integer from the buffer.
* @return {number}
*/
readVarUInt32() {
let v = 0;
let bit = 0;
while (true) {
const b = this.readUint8();
v |= (b & 0x7f) << bit;
bit += 7;
if (!(b & 0x80)) {
break;
}
}
return v >>> 0;
}
/**
* Reads an array of 8-bit unsigned integers
* @param number - The number of bytes to read
* @return {Buffer}
*/
readUInt8Array(number) {
const array = this.data.subarray(this.#offset, this.#offset + number);
this.#offset += number;
return array;
}
readUint16() {
const value = this.data.readUInt16LE(this.#offset);
this.#offset += 2;
return value;
}
/**
* Reads a string from the buffer.
* @return {string}
*/
readString() {
const length = this.readVarUInt32();
const buffer = this.data.subarray(this.#offset, this.#offset + length);
this.#offset += length;
return this.textDec.decode(buffer);
}
}