@zzzeros0/nbsp 0.1.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 +3 -0
- package/README.md +344 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/memory.d.ts +7 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +195 -0
- package/dist/memory.js.map +1 -0
- package/dist/size.d.ts +4 -0
- package/dist/size.d.ts.map +1 -0
- package/dist/size.js +46 -0
- package/dist/size.js.map +1 -0
- package/dist/structure.d.ts +50 -0
- package/dist/structure.d.ts.map +1 -0
- package/dist/structure.js +228 -0
- package/dist/structure.js.map +1 -0
- package/dist/type.d.ts +47 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +32 -0
- package/dist/type.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
# NBSP
|
|
2
|
+
|
|
3
|
+
## NodeJS Binary Structure Payloads
|
|
4
|
+
|
|
5
|
+
This package facilitates the creation and management of fixed binary payloads in NodeJS.
|
|
6
|
+
|
|
7
|
+
## Why NBSP?
|
|
8
|
+
|
|
9
|
+
NBSP is designed for:
|
|
10
|
+
|
|
11
|
+
- Binary network protocols (MQTT, CAN, custom TCP/UDP protocols)
|
|
12
|
+
- Fixed-size payloads
|
|
13
|
+
- Embedded / low-level communication
|
|
14
|
+
- Interoperability with C/C++ structures
|
|
15
|
+
|
|
16
|
+
It avoids:
|
|
17
|
+
|
|
18
|
+
- JSON serialization overhead
|
|
19
|
+
- Dynamic allocations
|
|
20
|
+
- Hidden copies
|
|
21
|
+
|
|
22
|
+
### Example
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
interface AckPacket {
|
|
26
|
+
id: string;
|
|
27
|
+
} // domain AckPacket
|
|
28
|
+
|
|
29
|
+
interface Packet {
|
|
30
|
+
header: number;
|
|
31
|
+
id: string;
|
|
32
|
+
method: string;
|
|
33
|
+
content: string;
|
|
34
|
+
} // domain Packet
|
|
35
|
+
|
|
36
|
+
const AckPacketStructure = structure<AckPacket>(
|
|
37
|
+
{
|
|
38
|
+
id: charDataType(6),
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
packed: true,
|
|
42
|
+
},
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const PacketStructure = structure<Packet>(
|
|
46
|
+
{
|
|
47
|
+
header: DataType.UINT32LE,
|
|
48
|
+
id: charDataType(6),
|
|
49
|
+
method: charDataType(4),
|
|
50
|
+
content: charDataType(64),
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
packed: true,
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
console.log(AckPacketStructure.size); // 6 (Unpacked: 10)
|
|
58
|
+
console.log(PacketStructure.size); // 78 (Unpacked: 128)
|
|
59
|
+
|
|
60
|
+
mqttClient.on("message", (topic, msg) => {
|
|
61
|
+
const packet = PacketStructure.from(msg);
|
|
62
|
+
|
|
63
|
+
processMessage(
|
|
64
|
+
topic,
|
|
65
|
+
toString(packet.id),
|
|
66
|
+
toString(packet.method),
|
|
67
|
+
toString(packet.content),
|
|
68
|
+
).then(() => {
|
|
69
|
+
const ackMessage = new AckPacketStructure({
|
|
70
|
+
id: packet.id,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
mqttClient.publish("ack", ackMessage.data());
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Usage
|
|
79
|
+
|
|
80
|
+
1. Create a domain interface.
|
|
81
|
+
|
|
82
|
+
- This will represent your data.
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
interface Person {
|
|
86
|
+
age: number;
|
|
87
|
+
name: string;
|
|
88
|
+
} // domain Person
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
2. Create a Structure for your domain interface. Calling `structure` will return a new class; it is not intended to be extended.
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
const PersonStructure = structure<Person>({
|
|
95
|
+
age: DataType.UINT8,
|
|
96
|
+
name: charDataType(4), // strings are arrays of UINT8. charDataType(n) is equivalent to [DataType.UINT8, n]
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
3. Now, you can instantiate the structure by using `new` or the `StructureConstructor`'s static method `from`. Instance exposes getters and setters to update and retrieve data directly from the fields of the structure in the buffer as well as some other methods to manage it:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const person1 = new PersonStructure({
|
|
104
|
+
age: 24,
|
|
105
|
+
name: toUint8Array("Dave"), // Transform the string to UTF-8 UINT8 array
|
|
106
|
+
});
|
|
107
|
+
console.log("Name: %s, age: %d", toString(person1.name), person1.age); // Name: Dave, age: 24
|
|
108
|
+
|
|
109
|
+
const person2 = PersonStructure.from(Buffer.from("526f73655a000000", "hex"));
|
|
110
|
+
|
|
111
|
+
console.log("Name: %s, age: %d", toString(person2.name), person2.age); // Name: Rose, age: 90
|
|
112
|
+
|
|
113
|
+
console.log(person2.data()); // <Buffer 52 6f 73 65 5a 00 00 00>
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
4. You can create a plain object using `toJson` instance or static methods:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
console.log(person2.toJson()); // { name: [ 82, 111, 115, 101 ], age: 90 }
|
|
120
|
+
|
|
121
|
+
const person3 = PersonStructure.toJson(Buffer.from("4a616b655a000000", "hex"));
|
|
122
|
+
|
|
123
|
+
console.log("Name: %s, age: %d", toString(person3.name), person3.age); // Name: Jake, age: 90
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Packing
|
|
127
|
+
|
|
128
|
+
When creating a `Structure`, you can provide the argument `packed` (default is `false`):
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
const PacketStructure = structure<Packet>(
|
|
132
|
+
{
|
|
133
|
+
header: DataType.UINT32LE,
|
|
134
|
+
id: charDataType(6),
|
|
135
|
+
content: charDataType(64),
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
packed: true,
|
|
139
|
+
},
|
|
140
|
+
);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
When `packed` is `true`, no padding or alignment will be applied to the fields. This is really important especially for binary communications, improving performance and usage.
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
console.log(PersonStructure.size); // Unpacked: 8 bytes
|
|
147
|
+
console.log(PersonStructure.size); // Packed: 5 bytes
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Strings
|
|
151
|
+
|
|
152
|
+
NBSP does not store strings dynamically.
|
|
153
|
+
Strings are represented as fixed-length arrays of `UINT8`. You can use `charDataType` as a shorthand:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
charDataType(6); // returns [DataType.UINT8, 6]
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
This is intentional and ensures:
|
|
160
|
+
|
|
161
|
+
- Deterministic payload size
|
|
162
|
+
- Protocol compatibility
|
|
163
|
+
- Zero dynamic allocation
|
|
164
|
+
|
|
165
|
+
### Working with hex
|
|
166
|
+
|
|
167
|
+
You can serialize/retrieve hex data:
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
interface User {
|
|
171
|
+
id: string;
|
|
172
|
+
role: number;
|
|
173
|
+
} // domain Person
|
|
174
|
+
|
|
175
|
+
const UserStructure = structure<User>({
|
|
176
|
+
id: charDataType(4),
|
|
177
|
+
role: DataType.UINT8,
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
const instance = new UserStructure({
|
|
181
|
+
id: toUint8Array("1a2b4c6d", true), // Transform to hex array
|
|
182
|
+
role: 2,
|
|
183
|
+
});
|
|
184
|
+
// HEX string length 8 -> HEX array length 4
|
|
185
|
+
|
|
186
|
+
console.log(instance.name); // Prints [ 26, 43, 76, 109 ]
|
|
187
|
+
console.log(toString(instance.name, true)); // "1a2b4c6d"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Arrays
|
|
191
|
+
|
|
192
|
+
`Arrays` are defined with a `type` and a fixed `length` of items:
|
|
193
|
+
|
|
194
|
+
```ts
|
|
195
|
+
interface Person {
|
|
196
|
+
name: string;
|
|
197
|
+
} // domain Person
|
|
198
|
+
|
|
199
|
+
const PersonStructure = structure<Person>({
|
|
200
|
+
name: charDataType(4), // [ DataType.UINT8, 4 ]
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const person = new PersonStructure({
|
|
204
|
+
name: toUint8Array("Jake"),
|
|
205
|
+
});
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
This specifies that there will be `4` items in this field, each of them of type `DataType.UINT8` (strings will always be stored as arrays of `DataType.UINT8`).
|
|
209
|
+
|
|
210
|
+
### Nested Structures
|
|
211
|
+
|
|
212
|
+
You can nest `Structures`:
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
interface DeviceConfig {
|
|
216
|
+
mode: number;
|
|
217
|
+
factor: number;
|
|
218
|
+
} // domain DeviceConfig
|
|
219
|
+
|
|
220
|
+
interface Device {
|
|
221
|
+
id: string;
|
|
222
|
+
config: DeviceConfig; // Provide the domain type
|
|
223
|
+
} // domain Device
|
|
224
|
+
|
|
225
|
+
const DeviceConfigStructure = structure<DeviceConfig>({
|
|
226
|
+
mode: DataType.UINT8,
|
|
227
|
+
factor: DataType.UINT8,
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const DeviceStructure = structure<Device>({
|
|
231
|
+
id: charDataType(6),
|
|
232
|
+
config: DeviceConfigStructure, // Provide the structure as type
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
const instance = new DeviceStructure({
|
|
236
|
+
id: Array.from(randomBytes(6)),
|
|
237
|
+
config: {
|
|
238
|
+
// Create a plain object
|
|
239
|
+
// DO NOT use DeviceConfigStructure constructor
|
|
240
|
+
// it will cause unnecessary allocations
|
|
241
|
+
mode: 4,
|
|
242
|
+
factor: 8,
|
|
243
|
+
},
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
console.log(instance.config.factor); // Prints '8'
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
You can also store `Structures` or `Arrays` inside of `Arrays`:
|
|
250
|
+
|
|
251
|
+
```ts
|
|
252
|
+
interface Group {
|
|
253
|
+
people: Person[];
|
|
254
|
+
} // domain Group
|
|
255
|
+
|
|
256
|
+
const GroupStructure = structure<Group>({
|
|
257
|
+
people: [PersonStructure, 100],
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const groupInstance = new GroupStructure({
|
|
261
|
+
people: [
|
|
262
|
+
// Create a plain object
|
|
263
|
+
// DO NOT use PersonStructure constructor
|
|
264
|
+
// it will cause unnecessary allocations
|
|
265
|
+
{
|
|
266
|
+
name: toUint8Array("Jack"),
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
name: toUint8Array("Rose"),
|
|
270
|
+
},
|
|
271
|
+
{
|
|
272
|
+
name: toUint8Array("Dave"),
|
|
273
|
+
},
|
|
274
|
+
...
|
|
275
|
+
],
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
console.log(groupInstance.people); // Array<100> [ { name: [Getter/Setter] }, ... ]
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
> **Important**
|
|
282
|
+
>
|
|
283
|
+
> Structures must be instantiated with plain objects when are nested, `do not` use the `Structure's constructor`; doing so will make unnecessary allocations.
|
|
284
|
+
|
|
285
|
+
### Buffer and copying
|
|
286
|
+
|
|
287
|
+
You can access the `buffer` of the `instance` with `data` method:
|
|
288
|
+
|
|
289
|
+
```ts
|
|
290
|
+
instance.data(); // Returns the internal Buffer (no copy)
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
You can reset the `buffer` of the `instance` with `reset` method:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
instance.reset(); // Resets the internal Buffer (0)
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
You can `copy` the data from other `instance` or `buffer`:
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
instance.copy(instanceOrBuffer);
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
You can create a `new instance` of the `Structure`, copying the data `from` other `buffer` or `instance`:
|
|
306
|
+
|
|
307
|
+
```ts
|
|
308
|
+
const copiedInstance = PersonStructure.from(instanceOrBuffer);
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
> **Important**
|
|
312
|
+
>
|
|
313
|
+
> Buffers must be the same size when copying.
|
|
314
|
+
>
|
|
315
|
+
> Each instance has its own buffer.
|
|
316
|
+
|
|
317
|
+
### JSON
|
|
318
|
+
|
|
319
|
+
Instances can be converted to plain JavaScript object with all the resolved properties and nested attributes of your structure using `toJson` method:
|
|
320
|
+
|
|
321
|
+
```ts
|
|
322
|
+
personInstance.toJson(); // { name: [ 68, 97, 118, 101 ], age: 24 }
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
You can serialize directly to a plain JavaScript object from a buffer by using the static method `toJson`:
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
PersonStructure.toJson(Buffer.from("4a616b655a000000", "hex")); // { name: [ 68, 97, 118, 101 ], age: 24 }
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
> **Important**
|
|
332
|
+
>
|
|
333
|
+
> Strings are still represented as numeric arrays in JSON output.
|
|
334
|
+
|
|
335
|
+
### Endianness
|
|
336
|
+
|
|
337
|
+
`DataType` difference between `LE` and `BE` types:
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
structure({
|
|
341
|
+
property1: DataType.UINT32BE,
|
|
342
|
+
property2: DataType.INT64LE,
|
|
343
|
+
});
|
|
344
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/memory.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type AlignedData } from "./type.js";
|
|
2
|
+
export declare function alloc(s: number): Buffer<ArrayBuffer>;
|
|
3
|
+
export declare function read(data: AlignedData, buffer: Buffer, offset: number): number | bigint;
|
|
4
|
+
export declare function write(data: AlignedData, buffer: Buffer, value: number, offset: number): void;
|
|
5
|
+
export declare function toUint8Array(s: string, hex?: boolean): number[];
|
|
6
|
+
export declare function toString(n: number[], hex?: boolean): string;
|
|
7
|
+
//# sourceMappingURL=memory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,WAAW,EAAE,MAAM,WAAW,CAAC;AA4BvD,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,uBAG9B;AAED,wBAAgB,IAAI,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mBA2CrE;AAED,wBAAgB,KAAK,CACnB,IAAI,EAAE,WAAW,EACjB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,QA0Ff;AAyBD,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,GAAE,OAAe,GAAG,MAAM,EAAE,CAMtE;AACD,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,GAAG,GAAE,OAAe,GAAG,MAAM,CAKlE"}
|
package/dist/memory.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { DataType } from "./type.js";
|
|
2
|
+
function assertInteger(value, min, max, type) {
|
|
3
|
+
if (!Number.isInteger(value)) {
|
|
4
|
+
throw new Error(`${type}: value is not an integer`);
|
|
5
|
+
}
|
|
6
|
+
if (value < min || value > max) {
|
|
7
|
+
throw new Error(`${type}: value ${value} out of range [${min}, ${max}]`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function assertFinite(value, type) {
|
|
11
|
+
if (!Number.isFinite(value)) {
|
|
12
|
+
throw new Error(`${type}: value is not finite`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function assertBigIntRange(value, min, max, type) {
|
|
16
|
+
if (value < min || value > max) {
|
|
17
|
+
throw new Error(`${type}: value ${value} out of range [${min}, ${max}]`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function alloc(s) {
|
|
21
|
+
// console.log("Alloc", s);
|
|
22
|
+
return Buffer.alloc(s);
|
|
23
|
+
}
|
|
24
|
+
export function read(data, buffer, offset) {
|
|
25
|
+
// console.log("read", data, offset + data.offset);
|
|
26
|
+
// if (typeof data.type === "number") {
|
|
27
|
+
switch (data.type) {
|
|
28
|
+
case DataType.INT8:
|
|
29
|
+
return buffer.readInt8(offset + data.offset);
|
|
30
|
+
case DataType.UINT8:
|
|
31
|
+
return buffer.readUInt8(offset + data.offset);
|
|
32
|
+
case DataType.INT16LE:
|
|
33
|
+
return buffer.readInt16LE(offset + data.offset);
|
|
34
|
+
case DataType.INT16BE:
|
|
35
|
+
return buffer.readInt16BE(offset + data.offset);
|
|
36
|
+
case DataType.UINT16LE:
|
|
37
|
+
return buffer.readUInt16LE(offset + data.offset);
|
|
38
|
+
case DataType.UINT16BE:
|
|
39
|
+
return buffer.readUInt16BE(offset + data.offset);
|
|
40
|
+
case DataType.INT32LE:
|
|
41
|
+
return buffer.readInt32LE(offset + data.offset);
|
|
42
|
+
case DataType.INT32BE:
|
|
43
|
+
return buffer.readInt32BE(offset + data.offset);
|
|
44
|
+
case DataType.UINT32LE:
|
|
45
|
+
return buffer.readUInt32LE(offset + data.offset);
|
|
46
|
+
case DataType.UINT32BE:
|
|
47
|
+
return buffer.readUInt32BE(offset + data.offset);
|
|
48
|
+
case DataType.INT64LE:
|
|
49
|
+
return buffer.readBigInt64LE(offset + data.offset);
|
|
50
|
+
case DataType.INT64BE:
|
|
51
|
+
return buffer.readBigInt64BE(offset + data.offset);
|
|
52
|
+
case DataType.UINT64LE:
|
|
53
|
+
return buffer.readBigUInt64LE(offset + data.offset);
|
|
54
|
+
case DataType.UINT64BE:
|
|
55
|
+
return buffer.readBigUInt64BE(offset + data.offset);
|
|
56
|
+
case DataType.FLOAT32LE:
|
|
57
|
+
return buffer.readFloatLE(offset + data.offset);
|
|
58
|
+
case DataType.FLOAT32BE:
|
|
59
|
+
return buffer.readFloatBE(offset + data.offset);
|
|
60
|
+
case DataType.FLOAT64LE:
|
|
61
|
+
return buffer.readDoubleLE(offset + data.offset);
|
|
62
|
+
case DataType.FLOAT64BE:
|
|
63
|
+
return buffer.readDoubleBE(offset + data.offset);
|
|
64
|
+
default:
|
|
65
|
+
throw new Error("Invalid type");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export function write(data, buffer, value, offset) {
|
|
69
|
+
// console.log("Write", data.offset + offset, value);
|
|
70
|
+
// if (typeof data.type === "number") {
|
|
71
|
+
// if (Array.isArray(value) || typeof value !== "number")
|
|
72
|
+
// throw new Error("Invalid value");
|
|
73
|
+
switch (data.type) {
|
|
74
|
+
case DataType.INT8:
|
|
75
|
+
assertInteger(value, -128, 127, "INT8");
|
|
76
|
+
buffer.writeInt8(value, offset + data.offset);
|
|
77
|
+
break;
|
|
78
|
+
case DataType.UINT8:
|
|
79
|
+
assertInteger(value, 0, 0xff, "UINT8");
|
|
80
|
+
buffer.writeUInt8(value, offset + data.offset);
|
|
81
|
+
break;
|
|
82
|
+
case DataType.INT16LE:
|
|
83
|
+
assertInteger(value, -0x8000, 0x7fff, "INT16");
|
|
84
|
+
buffer.writeInt16LE(value, offset + data.offset);
|
|
85
|
+
break;
|
|
86
|
+
case DataType.INT16BE:
|
|
87
|
+
assertInteger(value, -0x8000, 0x7fff, "INT16");
|
|
88
|
+
buffer.writeInt16BE(value, offset + data.offset);
|
|
89
|
+
break;
|
|
90
|
+
case DataType.UINT16LE:
|
|
91
|
+
assertInteger(value, 0, 0xffff, "UINT16");
|
|
92
|
+
buffer.writeUInt16LE(value, offset + data.offset);
|
|
93
|
+
break;
|
|
94
|
+
case DataType.UINT16BE:
|
|
95
|
+
assertInteger(value, 0, 0xffff, "UINT16");
|
|
96
|
+
buffer.writeUInt16BE(value, offset + data.offset);
|
|
97
|
+
break;
|
|
98
|
+
case DataType.INT32LE:
|
|
99
|
+
assertInteger(value, -0x80000000, 0x7fffffff, "INT32");
|
|
100
|
+
buffer.writeInt32LE(value, offset + data.offset);
|
|
101
|
+
break;
|
|
102
|
+
case DataType.INT32BE:
|
|
103
|
+
assertInteger(value, -0x80000000, 0x7fffffff, "INT32");
|
|
104
|
+
buffer.writeInt32BE(value, offset + data.offset);
|
|
105
|
+
break;
|
|
106
|
+
case DataType.UINT32LE:
|
|
107
|
+
assertInteger(value, 0, 0xffffffff, "UINT32");
|
|
108
|
+
buffer.writeUInt32LE(value, offset + data.offset);
|
|
109
|
+
break;
|
|
110
|
+
case DataType.UINT32BE:
|
|
111
|
+
assertInteger(value, 0, 0xffffffff, "UINT32");
|
|
112
|
+
buffer.writeUInt32BE(value, offset + data.offset);
|
|
113
|
+
break;
|
|
114
|
+
case DataType.INT64LE: {
|
|
115
|
+
const bigint = BigInt(value);
|
|
116
|
+
assertBigIntRange(bigint, -(1n << 63n), (1n << 63n) - 1n, "INT64");
|
|
117
|
+
buffer.writeBigInt64LE(bigint, offset + data.offset);
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
case DataType.INT64BE: {
|
|
121
|
+
const bigint = BigInt(value);
|
|
122
|
+
assertBigIntRange(bigint, -(1n << 63n), (1n << 63n) - 1n, "INT64");
|
|
123
|
+
buffer.writeBigInt64BE(bigint, offset + data.offset);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
case DataType.UINT64LE: {
|
|
127
|
+
const bigint = BigInt(value);
|
|
128
|
+
assertBigIntRange(bigint, 0n, (1n << 64n) - 1n, "UINT64");
|
|
129
|
+
buffer.writeBigUInt64LE(bigint, offset + data.offset);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
case DataType.UINT64BE: {
|
|
133
|
+
const bigint = BigInt(value);
|
|
134
|
+
assertBigIntRange(bigint, 0n, (1n << 64n) - 1n, "UINT64");
|
|
135
|
+
buffer.writeBigUInt64BE(bigint, offset + data.offset);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case DataType.FLOAT32LE:
|
|
139
|
+
assertFinite(value, "FLOAT32");
|
|
140
|
+
buffer.writeFloatLE(value, offset + data.offset);
|
|
141
|
+
break;
|
|
142
|
+
case DataType.FLOAT32BE:
|
|
143
|
+
assertFinite(value, "FLOAT32");
|
|
144
|
+
buffer.writeFloatBE(value, offset + data.offset);
|
|
145
|
+
break;
|
|
146
|
+
case DataType.FLOAT64LE:
|
|
147
|
+
assertFinite(value, "FLOAT64");
|
|
148
|
+
buffer.writeDoubleLE(value, offset + data.offset);
|
|
149
|
+
break;
|
|
150
|
+
case DataType.FLOAT64BE:
|
|
151
|
+
assertFinite(value, "FLOAT64");
|
|
152
|
+
buffer.writeDoubleBE(value, offset + data.offset);
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
throw new Error("Invalid type");
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
let encoder, decoder;
|
|
159
|
+
function hexToBytes(hex) {
|
|
160
|
+
let bytes = [];
|
|
161
|
+
for (let c = 0; c < hex.length; c += 2)
|
|
162
|
+
bytes.push(parseInt(hex.slice(c, c + 2), 16));
|
|
163
|
+
// console.log("Hex:", hex, bytes);
|
|
164
|
+
return bytes;
|
|
165
|
+
}
|
|
166
|
+
function bytesToHex(bytes) {
|
|
167
|
+
let hex = [];
|
|
168
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
169
|
+
const byte = bytes[i];
|
|
170
|
+
if (byte == undefined)
|
|
171
|
+
throw new Error("Invalid byte");
|
|
172
|
+
let current = byte < 0 ? byte + 256 : byte;
|
|
173
|
+
hex.push((current >>> 4).toString(16));
|
|
174
|
+
hex.push((current & 0xf).toString(16));
|
|
175
|
+
}
|
|
176
|
+
return hex.join("");
|
|
177
|
+
}
|
|
178
|
+
export function toUint8Array(s, hex = false) {
|
|
179
|
+
if (hex)
|
|
180
|
+
return hexToBytes(s);
|
|
181
|
+
if (!encoder)
|
|
182
|
+
encoder = new TextEncoder(); // UTF-8 por defecto
|
|
183
|
+
const bytes = encoder.encode(s);
|
|
184
|
+
// console.log("Raw", bytes);
|
|
185
|
+
return Array.from(bytes);
|
|
186
|
+
}
|
|
187
|
+
export function toString(n, hex = false) {
|
|
188
|
+
// console.log("String", n);
|
|
189
|
+
if (hex)
|
|
190
|
+
return bytesToHex(n);
|
|
191
|
+
if (!decoder)
|
|
192
|
+
decoder = new TextDecoder();
|
|
193
|
+
return decoder.decode(new Uint8Array(n));
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAoB,MAAM,WAAW,CAAC;AAEvD,SAAS,aAAa,C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G;QAAE,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC;IAC9B,IAAI,CAAC,OAAO;QAAE,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC,CAAC,oBAAoB;IAC/D,MAAM,KAAK,GAAe,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC5C,6BAA6B;IAC7B,OAAO,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAC3B,CAAC;AACD,MAAM,UAAU,QAAQ,CAAC,CAAW,EAAE,MAAe,KAAK;IACxD,4BAA4B;IAC5B,IAAI,GAAG;QAAE,OAAO,UAAU,CAAC,CAAC,CAAC,CAAC;IAC9B,IAAI,CAAC,OAAO;QAAE,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;IAC1C,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AAC3C,CAAC"}
|
package/dist/size.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"size.d.ts","sourceRoot":"","sources":["../src/size.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAgC,KAAK,IAAI,EAAE,MAAM,WAAW,CAAC;AAsCpE,wBAAgB,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,oBAAoB,GAAG,MAAM,CAQhE"}
|
package/dist/size.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {} from "./structure.js";
|
|
2
|
+
import { DataType } from "./type.js";
|
|
3
|
+
function getDataTypeSize(type) {
|
|
4
|
+
switch (type) {
|
|
5
|
+
case DataType.INT8:
|
|
6
|
+
case DataType.UINT8:
|
|
7
|
+
return 1;
|
|
8
|
+
case DataType.INT16LE:
|
|
9
|
+
case DataType.INT16BE:
|
|
10
|
+
case DataType.UINT16LE:
|
|
11
|
+
case DataType.UINT16BE:
|
|
12
|
+
return 2;
|
|
13
|
+
case DataType.INT32BE:
|
|
14
|
+
case DataType.INT32LE:
|
|
15
|
+
case DataType.UINT32LE:
|
|
16
|
+
case DataType.UINT32BE:
|
|
17
|
+
return 4;
|
|
18
|
+
case DataType.INT64LE:
|
|
19
|
+
case DataType.INT64BE:
|
|
20
|
+
case DataType.UINT64LE:
|
|
21
|
+
case DataType.UINT64BE:
|
|
22
|
+
return 8;
|
|
23
|
+
case DataType.FLOAT32LE:
|
|
24
|
+
case DataType.FLOAT32BE:
|
|
25
|
+
return 4;
|
|
26
|
+
case DataType.FLOAT64LE:
|
|
27
|
+
case DataType.FLOAT64BE:
|
|
28
|
+
return 8;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function getStructureDataSize(structure) {
|
|
32
|
+
return structure.size;
|
|
33
|
+
}
|
|
34
|
+
function getArrrayDataSize(type) {
|
|
35
|
+
return sizeof(type[0]) * type[1];
|
|
36
|
+
}
|
|
37
|
+
export function sizeof(type) {
|
|
38
|
+
return typeof type === "number"
|
|
39
|
+
? getDataTypeSize(type)
|
|
40
|
+
: typeof type === "string"
|
|
41
|
+
? 0
|
|
42
|
+
: Array.isArray(type)
|
|
43
|
+
? getArrrayDataSize(type)
|
|
44
|
+
: getStructureDataSize(type);
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=size.js.map
|
package/dist/size.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"size.js","sourceRoot":"","sources":["../src/size.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAiC,MAAM,WAAW,CAAC;AAEpE,SAAS,eAAe,CAAC,IAAc;IACrC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,QAAQ,CAAC,IAAI,CAAC;QACnB,KAAK,QAAQ,CAAC,KAAK;YACjB,OAAO,CAAC,CAAC;QACX,KAAK,QAAQ,CAAC,OAAO,CAAC;QACtB,KAAK,QAAQ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-0,0 +1,50 @@
|
|
|
1
|
+
import { type AlignedData, type BindedType, type DomainObject, type StructureDefinitionDataType, type Type } from "./type.js";
|
|
2
|
+
export type StructureFields<T extends DomainObject = DomainObject> = {
|
|
3
|
+
[K in keyof Record<keyof T, Type>]: AlignedData;
|
|
4
|
+
};
|
|
5
|
+
export interface StructureConstructor<T extends DomainObject = DomainObject> {
|
|
6
|
+
readonly size: number;
|
|
7
|
+
readonly fields: StructureFields;
|
|
8
|
+
/**
|
|
9
|
+
* Copys the contents of the buffer. Returns a new Instance.
|
|
10
|
+
* @param buffer
|
|
11
|
+
*/
|
|
12
|
+
from(buffer: Buffer): Structure<BindedType<T>>;
|
|
13
|
+
from(buffer: Structure<BindedType<T>>): Structure<BindedType<T>>;
|
|
14
|
+
toJson(buffer: Buffer): BindedType<T>;
|
|
15
|
+
new (args: BindedType<T>): Structure<BindedType<T>>;
|
|
16
|
+
}
|
|
17
|
+
export interface StructureMethods<T extends DomainObject> {
|
|
18
|
+
/**
|
|
19
|
+
* Copies the contents of the buffer
|
|
20
|
+
* @param buffer
|
|
21
|
+
*/
|
|
22
|
+
copy(buffer: Buffer): void;
|
|
23
|
+
/**
|
|
24
|
+
* Copies the contents of the structure buffer
|
|
25
|
+
* @param buffer
|
|
26
|
+
*/
|
|
27
|
+
copy(structure: Structure<T>): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the buffer
|
|
30
|
+
*/
|
|
31
|
+
data(): Buffer;
|
|
32
|
+
/**
|
|
33
|
+
* Sets the contents of the buffer to 0
|
|
34
|
+
*/
|
|
35
|
+
reset(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Returns a plain object with the content of the structure
|
|
38
|
+
*/
|
|
39
|
+
toJson(): T;
|
|
40
|
+
}
|
|
41
|
+
export type Structure<T extends DomainObject> = T & StructureMethods<T>;
|
|
42
|
+
export declare function alignFields<T extends Record<string, Type>>(data: T, packed?: boolean): {
|
|
43
|
+
fields: StructureFields<T>;
|
|
44
|
+
size: number;
|
|
45
|
+
};
|
|
46
|
+
export declare function defineProxyProperty(target: Record<string, any>, key: string, field: AlignedData, buffer: Buffer, offset?: number): void;
|
|
47
|
+
export declare function structure<T extends DomainObject>(data: StructureDefinitionDataType<T>, opts?: {
|
|
48
|
+
packed?: boolean;
|
|
49
|
+
}): StructureConstructor<T>;
|
|
50
|
+
//# sourceMappingURL=structure.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structure.d.ts","sourceRoot":"","sources":["../src/structure.ts"],"names":[],"mappings":"AAEA,OAAO,EAIL,KAAK,WAAW,EAEhB,KAAK,UAAU,EAEf,KAAK,YAAY,EACjB,KAAK,2BAA2B,EAChC,KAAK,IAAI,EACV,MAAM,WAAW,CAAC;AAEnB,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY,IAAI;KAClE,CAAC,IAAI,MAAM,MAAM,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,GAAG,WAAW;CAChD,CAAC;AACF,MAAM,WAAW,oBAAoB,CAAC,CAAC,SAAS,YAAY,GAAG,YAAY;IACzE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC;IACjC;;;OAGG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/C,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;IACjE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IACtC,KAAK,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;CACrD;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,YAAY;IACtD;;;OAGG;IACH,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B;;;OAGG;IACH,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;IACpC;;OAEG;IACH,IAAI,IAAI,MAAM,CAAC;IACf;;OAEG;IACH,KAAK,IAAI,IAAI,CAAC;IACd;;OAEG;IACH,MAAM,IAAI,CAAC,CAAC;CACb;AAED,MAAM,MAAM,SAAS,CAAC,CAAC,SAAS,YAAY,IAAI,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC;AAKxE,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,EACxD,IAAI,EAAE,CAAC,EACP,MAAM,GAAE,OAAe,GACtB;IAAE,MAAM,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAsB9C;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,GAAG,EAAE,MAAM,EACX,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,MAAM,EACd,MAAM,GAAE,MAAU,GACjB,IAAI,CA0BN;AA8MD,wBAAgB,SAAS,CAAC,CAAC,SAAS,YAAY,EAC9C,IAAI,EAAE,2BAA2B,CAAC,CAAC,CAAC,EACpC,IAAI,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,GAC1B,oBAAoB,CAAC,CAAC,CAAC,CAsEzB"}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { alloc, read, write } from "./memory.js";
|
|
2
|
+
import { sizeof } from "./size.js";
|
|
3
|
+
import { DataType, isArrayDataType, isStructureDataType, } from "./type.js";
|
|
4
|
+
function alignUp(n, align) {
|
|
5
|
+
return (n + align - 1) & ~(align - 1);
|
|
6
|
+
}
|
|
7
|
+
export function alignFields(data, packed = false) {
|
|
8
|
+
const fields = {};
|
|
9
|
+
let offset = 0;
|
|
10
|
+
let maxAlign = 1;
|
|
11
|
+
const mnames = new Set();
|
|
12
|
+
for (const [k, m] of Object.entries(data)) {
|
|
13
|
+
const size = sizeof(m);
|
|
14
|
+
if (!packed) {
|
|
15
|
+
offset = alignUp(offset, size);
|
|
16
|
+
maxAlign = Math.max(maxAlign, size);
|
|
17
|
+
}
|
|
18
|
+
fields[k] = {
|
|
19
|
+
type: m,
|
|
20
|
+
size,
|
|
21
|
+
offset,
|
|
22
|
+
};
|
|
23
|
+
offset += size;
|
|
24
|
+
if (mnames.has(k))
|
|
25
|
+
throw new Error("Duplicate name");
|
|
26
|
+
mnames.add(k);
|
|
27
|
+
}
|
|
28
|
+
const structSize = packed ? offset : alignUp(offset, maxAlign);
|
|
29
|
+
return { fields: Object.freeze(fields), size: structSize };
|
|
30
|
+
}
|
|
31
|
+
export function defineProxyProperty(target, key, field, buffer, offset = 0) {
|
|
32
|
+
// console.log("Define property", target, key, field);
|
|
33
|
+
const isStructField = isStructureDataType(field.type);
|
|
34
|
+
const isArrayField = isArrayDataType(field.type);
|
|
35
|
+
Object.defineProperty(target, key, {
|
|
36
|
+
get() {
|
|
37
|
+
// console.log("Get", key, field, isStructField);
|
|
38
|
+
if (isStructField)
|
|
39
|
+
return target[key];
|
|
40
|
+
else if (isArrayField)
|
|
41
|
+
return readArray(field, buffer, offset);
|
|
42
|
+
return read(field, buffer, offset);
|
|
43
|
+
},
|
|
44
|
+
set(v) {
|
|
45
|
+
// console.log("Set", key, isStructField);
|
|
46
|
+
if (isStructField)
|
|
47
|
+
return writeStructure(field, buffer, v, offset);
|
|
48
|
+
else if (isArrayField)
|
|
49
|
+
writeArray(field, v, buffer, offset);
|
|
50
|
+
else
|
|
51
|
+
write(field, buffer, v, offset);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function writeStructure(data, buffer, value, offset) {
|
|
56
|
+
// console.log("Write structure", data, offset, value);
|
|
57
|
+
for (const [k, field] of Object.entries(data.type.fields)) {
|
|
58
|
+
if (isStructureDataType(field.type)) {
|
|
59
|
+
writeStructure(field, buffer, value[k], offset + data.offset);
|
|
60
|
+
}
|
|
61
|
+
else if (Array.isArray(field.type)) {
|
|
62
|
+
writeArray(field, value[k], buffer, offset + data.offset);
|
|
63
|
+
}
|
|
64
|
+
else
|
|
65
|
+
write(field, buffer, value[k], offset + data.offset);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function writeArray(data, arr, buffer, offset) {
|
|
69
|
+
// console.log("Write array", data, offset, arr);
|
|
70
|
+
const [type, length] = data.type;
|
|
71
|
+
const isStructure = isStructureDataType(type);
|
|
72
|
+
const size = sizeof(type);
|
|
73
|
+
for (let i = 0; i < length; i++) {
|
|
74
|
+
const value = arr[i];
|
|
75
|
+
if (isStructure) {
|
|
76
|
+
writeStructure({
|
|
77
|
+
offset: i * size,
|
|
78
|
+
size: size,
|
|
79
|
+
type,
|
|
80
|
+
}, buffer, value, offset + data.offset);
|
|
81
|
+
}
|
|
82
|
+
else if (isArrayDataType(type)) {
|
|
83
|
+
writeArray({
|
|
84
|
+
type: type,
|
|
85
|
+
offset: i * sizeof(type),
|
|
86
|
+
size: size,
|
|
87
|
+
}, value, buffer, offset + data.offset);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
write({
|
|
91
|
+
type,
|
|
92
|
+
size: size,
|
|
93
|
+
offset: i * size,
|
|
94
|
+
}, buffer, value, offset + data.offset);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function readArray(data, buffer, offset = 0, mutable = true) {
|
|
99
|
+
// console.log("Read array", data, offset);
|
|
100
|
+
const t = [];
|
|
101
|
+
const [type, length] = data.type;
|
|
102
|
+
const size = sizeof(type);
|
|
103
|
+
const isStructure = isStructureDataType(type);
|
|
104
|
+
const isArray = isArrayDataType(type);
|
|
105
|
+
for (let i = 0; i < length; i++) {
|
|
106
|
+
if (isStructure) {
|
|
107
|
+
t.push(readStructure({
|
|
108
|
+
type,
|
|
109
|
+
offset: i * type.size,
|
|
110
|
+
size: type.size,
|
|
111
|
+
}, buffer, offset + data.offset, mutable));
|
|
112
|
+
}
|
|
113
|
+
else if (isArray) {
|
|
114
|
+
t.push(readArray({
|
|
115
|
+
type: type,
|
|
116
|
+
offset: i * size,
|
|
117
|
+
size: size,
|
|
118
|
+
}, buffer, offset + data.offset, mutable));
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
t.push(read({
|
|
122
|
+
offset: i * size,
|
|
123
|
+
size,
|
|
124
|
+
type,
|
|
125
|
+
}, buffer, offset + data.offset));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return t;
|
|
129
|
+
}
|
|
130
|
+
function readStructure(data, buffer, offset = 0, mutable = true) {
|
|
131
|
+
// console.log("readStructure", offset);
|
|
132
|
+
const t = {};
|
|
133
|
+
for (const [k, field] of Object.entries(data.type.fields)) {
|
|
134
|
+
if (mutable)
|
|
135
|
+
defineProxyProperty(t, k, field, buffer, data.offset + offset);
|
|
136
|
+
else {
|
|
137
|
+
if (isStructureDataType(field.type)) {
|
|
138
|
+
t[k] = readStructure(field, buffer, data.offset + offset, mutable);
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (isArrayDataType(field.type)) {
|
|
142
|
+
t[k] = readArray(field, buffer, data.offset + offset, mutable);
|
|
143
|
+
}
|
|
144
|
+
else
|
|
145
|
+
t[k] = read(field, buffer, data.offset + offset);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return t;
|
|
149
|
+
}
|
|
150
|
+
function construct(target, fields, args, buffer, offset = 0, writeData = true) {
|
|
151
|
+
// console.log("Construct", target, offset);
|
|
152
|
+
for (const [k, field] of Object.entries(fields)) {
|
|
153
|
+
const arg = args[k];
|
|
154
|
+
if (writeData) {
|
|
155
|
+
if (isStructureDataType(field.type)) {
|
|
156
|
+
target[k] = {};
|
|
157
|
+
construct(target[k], field.type.fields, arg ?? {}, buffer, offset + field.offset);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (Array.isArray(field.type)) {
|
|
161
|
+
writeArray(field, arg ?? [], buffer, offset);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
if (arg)
|
|
165
|
+
write(field, buffer, arg, offset);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
defineProxyProperty(target, k, field, buffer, offset);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
export function structure(data, opts) {
|
|
172
|
+
const { fields, size } = alignFields(data, opts?.packed);
|
|
173
|
+
let writeData = true;
|
|
174
|
+
const t = class {
|
|
175
|
+
static fields = fields;
|
|
176
|
+
static size = size;
|
|
177
|
+
__buff__ = alloc(size);
|
|
178
|
+
static from(arg) {
|
|
179
|
+
writeData = false;
|
|
180
|
+
if (arg instanceof Buffer) {
|
|
181
|
+
if (arg.length !== size)
|
|
182
|
+
throw new Error("Invalid buffer size");
|
|
183
|
+
const inst = new this({});
|
|
184
|
+
arg.copy(inst.data());
|
|
185
|
+
return inst;
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
const inst = new this({});
|
|
189
|
+
arg.data().copy(inst.data());
|
|
190
|
+
return inst;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
static toJson(buffer) {
|
|
194
|
+
return readStructure({
|
|
195
|
+
type: this,
|
|
196
|
+
offset: 0,
|
|
197
|
+
size,
|
|
198
|
+
}, buffer, 0, false);
|
|
199
|
+
}
|
|
200
|
+
constructor(args) {
|
|
201
|
+
construct(this, fields, args, this.__buff__, 0, writeData);
|
|
202
|
+
writeData = true;
|
|
203
|
+
}
|
|
204
|
+
copy(target) {
|
|
205
|
+
if (target instanceof Buffer) {
|
|
206
|
+
target.copy(this.__buff__);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
else
|
|
210
|
+
target.data().copy(this.__buff__);
|
|
211
|
+
}
|
|
212
|
+
data() {
|
|
213
|
+
return this.__buff__;
|
|
214
|
+
}
|
|
215
|
+
reset() {
|
|
216
|
+
this.__buff__.fill(0);
|
|
217
|
+
}
|
|
218
|
+
toJson() {
|
|
219
|
+
return readStructure({
|
|
220
|
+
type: t,
|
|
221
|
+
offset: 0,
|
|
222
|
+
size,
|
|
223
|
+
}, this.__buff__, 0, false);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
return t;
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=structure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"structure.js","sourceRoot":"","sources":["../src/structure.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EACL,QAAQ,EACR,eAAe,EACf,mBAAmB,GAQpB,MAAM,WAAW,CAAC;AA6CnB,SAAS,OAAO,CAAC,CAAS,EAAE,KAAa;IACvC,OAAO,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AACxC,CAAC;AACD,MAAM,UAAU,WAAW,CACzB,IAAO,EACP,SAAkB,KAAK;IAEvB,MAAM,MAAM,GAAG,EAAqC,CAAC;IACrD,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,QAAQ,GAAG,CAAC,CAAC;IACjB,MAAM,MAAM,GAAG,IAAI,GAAG,EAAE,CAAC;IACzB,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAA0B,EAAE,CAAC;QACnE,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;QACvB,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE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gBAC7B,OAAO,IAAgC,CAAC;YAC1C,CAAC;QACH,CAAC;QACM,MAAM,CAAC,MAAM,CAAC,MAAc;YACjC,OAAO,aAAa,CAClB;gBACE,IAAI,EAAE,IAA4B;gBAClC,MAAM,EAAE,CAAC;gBACT,IAAI;aACL,EACD,MAAM,EACN,CAAC,EACD,KAAK,CACN,CAAC;QACJ,CAAC;QACD,YAAY,IAAmB;YAC7B,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,SAAS,CAAC,CAAC;YAC3D,SAAS,GAAG,IAAI,CAAC;QACnB,CAAC;QAGM,IAAI,CAAC,MAAW;YACrB,IAAI,MAAM,YAAY,MAAM,EAAE,CAAC;gBAC7B,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;gBAC3B,OAAO;YACT,CAAC;;gBAAM,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC3C,CAAC;QACM,IAAI;YACT,OAAO,IAAI,CAAC,QAAQ,CAAC;QACvB,CAAC;QACM,KAAK;YACV,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACxB,CAAC;QACM,MAAM;YACX,OAAO,aAAa,CAClB;gBACE,IAAI,EAAE,CAAyB;gBAC/B,MAAM,EAAE,CAAC;gBACT,IAAI;aACL,EACD,IAAI,CAAC,QAAQ,EACb,CAAC,EACD,KAAK,CACN,CAAC;QACJ,CAAC;KACgC,CAAC;IACpC,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/dist/type.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type StructureConstructor } from "./structure.js";
|
|
2
|
+
export declare enum DataType {
|
|
3
|
+
INT8 = 0,
|
|
4
|
+
UINT8 = 1,
|
|
5
|
+
INT16LE = 2,
|
|
6
|
+
INT16BE = 3,
|
|
7
|
+
UINT16LE = 4,
|
|
8
|
+
UINT16BE = 5,
|
|
9
|
+
INT32LE = 6,
|
|
10
|
+
INT32BE = 7,
|
|
11
|
+
UINT32LE = 8,
|
|
12
|
+
UINT32BE = 9,
|
|
13
|
+
INT64LE = 10,
|
|
14
|
+
INT64BE = 11,
|
|
15
|
+
UINT64LE = 12,
|
|
16
|
+
UINT64BE = 13,
|
|
17
|
+
FLOAT32LE = 14,
|
|
18
|
+
FLOAT32BE = 15,
|
|
19
|
+
FLOAT64LE = 16,
|
|
20
|
+
FLOAT64BE = 17
|
|
21
|
+
}
|
|
22
|
+
export type BigIntDataType = DataType.INT64LE | DataType.INT64BE | DataType.UINT64LE | DataType.UINT64BE;
|
|
23
|
+
export type NumericArrayDataType = [type: DataType, size: number];
|
|
24
|
+
export type StructureArrayDataType<T extends Record<string, any>> = [
|
|
25
|
+
type: StructureConstructor<T>,
|
|
26
|
+
size: number
|
|
27
|
+
];
|
|
28
|
+
export type DomainObject = Record<string, any>;
|
|
29
|
+
export type InferArray<T extends any> = T extends number ? NumericArrayDataType : T extends DomainObject ? StructureArrayDataType<T> : never;
|
|
30
|
+
export type ArrayDataType = NumericArrayDataType | StructureArrayDataType<DomainObject>;
|
|
31
|
+
export type Type<D extends DomainObject = DomainObject> = DataType | ArrayDataType | StructureConstructor<D>;
|
|
32
|
+
export type DataVale = number | Array<number> | bigint | object;
|
|
33
|
+
export type BindedType<T extends Record<string, any>> = {
|
|
34
|
+
[K in keyof T]: T[K] extends string ? number[] : T[K] extends DomainObject ? BindedType<T[K]> : T[K];
|
|
35
|
+
};
|
|
36
|
+
export type StructureDefinitionDataType<T extends DomainObject> = {
|
|
37
|
+
[K in keyof T]: T[K] extends readonly (infer P)[] ? InferArray<P> : T[K] extends number ? DataType : T[K] extends bigint ? BigIntDataType : T[K] extends string ? NumericArrayDataType : T[K] extends DomainObject ? StructureConstructor<T[K]> : never;
|
|
38
|
+
};
|
|
39
|
+
export interface AlignedData<T extends Type = Type> {
|
|
40
|
+
readonly type: T;
|
|
41
|
+
readonly offset: number;
|
|
42
|
+
readonly size: number;
|
|
43
|
+
}
|
|
44
|
+
export declare function isStructureDataType(t: Type): t is StructureConstructor;
|
|
45
|
+
export declare function isArrayDataType(t: Type): t is ArrayDataType;
|
|
46
|
+
export declare function charDataType(length: number): NumericArrayDataType;
|
|
47
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,oBAAY,QAAQ;IAClB,IAAI,IAAI;IACR,KAAK,IAAA;IACL,OAAO,IAAA;IACP,OAAO,IAAA;IACP,QAAQ,IAAA;IACR,QAAQ,IAAA;IACR,OAAO,IAAA;IACP,OAAO,IAAA;IACP,QAAQ,IAAA;IACR,QAAQ,IAAA;IACR,OAAO,KAAA;IACP,OAAO,KAAA;IACP,QAAQ,KAAA;IACR,QAAQ,KAAA;IACR,SAAS,KAAA;IACT,SAAS,KAAA;IACT,SAAS,KAAA;IACT,SAAS,KAAA;CACV;AACD,MAAM,MAAM,cAAc,GACtB,QAAQ,CAAC,OAAO,GAChB,QAAQ,CAAC,OAAO,GAChB,QAAQ,CAAC,QAAQ,GACjB,QAAQ,CAAC,QAAQ,CAAC;AACtB,MAAM,MAAM,oBAAoB,GAAG,CAAC,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AAClE,MAAM,MAAM,sBAAsB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI;IAClE,IAAI,EAAE,oBAAoB,CAAC,CAAC,CAAC;IAC7B,IAAI,EAAE,MAAM;CACb,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC/C,MAAM,MAAM,UAAU,CAAC,C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C,CAAC,SAAS,IAAI,GAAG,IAAI;IAChD,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AACD,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,IAAI,oBAAoB,CAEtE;AAED,wBAAgB,eAAe,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,IAAI,aAAa,CAE3D;AACD,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,oBAAoB,CAEjE"}
|
package/dist/type.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {} from "./structure.js";
|
|
2
|
+
export var DataType;
|
|
3
|
+
(function (DataType) {
|
|
4
|
+
DataType[DataType["INT8"] = 0] = "INT8";
|
|
5
|
+
DataType[DataType["UINT8"] = 1] = "UINT8";
|
|
6
|
+
DataType[DataType["INT16LE"] = 2] = "INT16LE";
|
|
7
|
+
DataType[DataType["INT16BE"] = 3] = "INT16BE";
|
|
8
|
+
DataType[DataType["UINT16LE"] = 4] = "UINT16LE";
|
|
9
|
+
DataType[DataType["UINT16BE"] = 5] = "UINT16BE";
|
|
10
|
+
DataType[DataType["INT32LE"] = 6] = "INT32LE";
|
|
11
|
+
DataType[DataType["INT32BE"] = 7] = "INT32BE";
|
|
12
|
+
DataType[DataType["UINT32LE"] = 8] = "UINT32LE";
|
|
13
|
+
DataType[DataType["UINT32BE"] = 9] = "UINT32BE";
|
|
14
|
+
DataType[DataType["INT64LE"] = 10] = "INT64LE";
|
|
15
|
+
DataType[DataType["INT64BE"] = 11] = "INT64BE";
|
|
16
|
+
DataType[DataType["UINT64LE"] = 12] = "UINT64LE";
|
|
17
|
+
DataType[DataType["UINT64BE"] = 13] = "UINT64BE";
|
|
18
|
+
DataType[DataType["FLOAT32LE"] = 14] = "FLOAT32LE";
|
|
19
|
+
DataType[DataType["FLOAT32BE"] = 15] = "FLOAT32BE";
|
|
20
|
+
DataType[DataType["FLOAT64LE"] = 16] = "FLOAT64LE";
|
|
21
|
+
DataType[DataType["FLOAT64BE"] = 17] = "FLOAT64BE";
|
|
22
|
+
})(DataType || (DataType = {}));
|
|
23
|
+
export function isStructureDataType(t) {
|
|
24
|
+
return typeof t === "function";
|
|
25
|
+
}
|
|
26
|
+
export function isArrayDataType(t) {
|
|
27
|
+
return Array.isArray(t);
|
|
28
|
+
}
|
|
29
|
+
export function charDataType(length) {
|
|
30
|
+
return [DataType.UINT8, length];
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=type.js.map
|
package/dist/type.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,MAAM,gBAAgB,CAAC;AAE3D,MAAM,CAAN,IAAY,QAmBX;AAnBD,WAAY,QAAQ;IAClB,uCAAQ,CAAA;IACR,yCAAK,CAAA;IACL,6CAAO,CAAA;IACP,6CAAO,CAAA;IACP,+CAAQ,CAAA;IACR,+CAAQ,CAAA;IACR,6CAAO,CAAA;IACP,6CAAO,CAAA;IACP,+CAAQ,CAAA;IACR,+CAAQ,CAAA;IACR,8CAAO,CAAA;IACP,8CAAO,CAAA;IACP,gDAAQ,CAAA;IACR,gDAAQ,CAAA;IACR,kDAAS,CAAA;IACT,kDAAS,CAAA;IACT,kDAAS,CAAA;IACT,kDAAS,CAAA;AACX,CAAC,EAnBW,QAAQ,KAAR,QAAQ,QAmBnB;AAqDD,MAAM,UAAU,mBAAmB,CAAC,CAAO;IACzC,OAAO,OAAO,CAAC,KAAK,UAAU,CAAC;AACjC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,CAAO;IACrC,OAAO,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1B,CAAC;AACD,MAAM,UAAU,YAAY,CAAC,MAAc;IACzC,OAAO,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AAClC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zzzeros0/nbsp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-copy fixed-size binary structures for NodeJS TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepublishOnly": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"files": ["dist"],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"binary",
|
|
21
|
+
"hex",
|
|
22
|
+
"buffer",
|
|
23
|
+
"array",
|
|
24
|
+
"json",
|
|
25
|
+
"data",
|
|
26
|
+
"raw",
|
|
27
|
+
"data-structure",
|
|
28
|
+
"data-protocol",
|
|
29
|
+
"struct",
|
|
30
|
+
"API",
|
|
31
|
+
"structure",
|
|
32
|
+
"communication",
|
|
33
|
+
"protocol",
|
|
34
|
+
"network",
|
|
35
|
+
"zero-copy",
|
|
36
|
+
"zero-copy-structure",
|
|
37
|
+
"zero-copy-protocol",
|
|
38
|
+
"zero-copy-communication",
|
|
39
|
+
"zero-copy-network",
|
|
40
|
+
"cpp",
|
|
41
|
+
"c++",
|
|
42
|
+
"arduino",
|
|
43
|
+
"esp32",
|
|
44
|
+
"mqtt",
|
|
45
|
+
"amqp",
|
|
46
|
+
"ts",
|
|
47
|
+
"typescript"
|
|
48
|
+
],
|
|
49
|
+
"author": "zzzeros0",
|
|
50
|
+
"license": "MIT",
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "https://github.com/zzzeros0/nbsp.git"
|
|
54
|
+
},
|
|
55
|
+
"homepage": "https://github.com/zzzeros0/nbsp.git#readme",
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/node": "^25.2.3",
|
|
58
|
+
"typescript": "^5.9.3"
|
|
59
|
+
}
|
|
60
|
+
}
|