Gathering detailed insights and metrics for @teamteanpm2024/modi-dolorem-aspernatur
Gathering detailed insights and metrics for @teamteanpm2024/modi-dolorem-aspernatur
Gathering detailed insights and metrics for @teamteanpm2024/modi-dolorem-aspernatur
Gathering detailed insights and metrics for @teamteanpm2024/modi-dolorem-aspernatur
npm install @teamteanpm2024/modi-dolorem-aspernatur
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
41
npm i @teamteanpm2024/modi-dolorem-aspernatur
Atom | Type | Size [B] | Aliases | Notes |
---|---|---|---|---|
b8 | boolean | 1 | bool8 bool BOOL | |
b16 | boolean | 2 | bool16 | |
b32 | boolean | 4 | bool32 | |
b64 | boolean | 8 | bool64 | |
u8 | unsigned char | 1 | uint8 uint8_t BYTE | |
u16 | unsigned int | 2 | uint16 uint16_t WORD | |
u32 | unsigned long | 4 | uint32 uint32_t DWORD | |
u64 | unsigned long long | 8 | uint64 uint64_t LWORD QWORD | |
i8 | signed char | 1 | int8 int8_t SINT | |
i16 | signed int | 2 | int16 int16_t INT | |
i32 | signed long | 4 | int32 int32_t DINT | |
i64 | signed long long | 8 | int64 int64_t LINT QINT | |
f | float | 4 | float float32 float32_t REAL single | |
d | double | 4 | double float64 float64_t LREAL | |
sN | string | N | string | N= 0+ |
bufN | buffer | N | buffer | N= 1+ |
jN | json | N | json any | N= 0+ |
The main concept is to first create a model of your data structure and then utilize it to read from and write to a buffer.
For exchanging data between JavaScript and C/C++ you can use the following classes and their methods:
Both classes uses the same methods and have the same functionality.
Dynamic methods uses Object/Array/String model/types to exchange data.
Static methods are designed to use decorators to define model/types.
MAKE
When using make
method it returns { buffer, offset, size }
object.
make(struct: T): CStructWriteResult;
WRITE
When using write
method it returns { buffer, offset, size }
object.
write(buffer: Buffer, struct: T, offset?: number): CStructWriteResult;
Write is different from make because it uses existing buffer and writes data to it.
Also write allows to pass offset
to pin start point from any offset in the buffer.
READ
When using read
method it returns { struct, offset, size }
object.
read<T>(buffer: Buffer, offset?: number): CStructReadResult<T>;
Read uses existing buffer and reads data from it.
And also read allows to pass offset
to pin start point from any offset in the buffer.
OFFSET
Offset can be used in different scenario as we want to read/write from/to buffer from any offset.
Which allows binary parser to split data into different parts and read/write them separately.
DECORATORS
Decorators are used to define model/types for static methods.
@CStructClass
- defines model/types for class.
@CStructProperty
- defines type for property.
Static make
creates new instance of provided class and fills it with parsed data.
NOTE
When using @CStructClass
decorator with {model: ... }
it can override @CStructProperty
decorators.
Atomic types instead pure string types
1const {CStructBE,CStructLE, hexToBuffer, AtomTypes} = require('@teamteanpm2024/modi-dolorem-aspernatur'); 2const {U16, I16, STRING} = AtomTypes; // You can also use 'u16', 'i16', 'string' / 's' as before 3 4// JavaScript Example 1 5{ 6 // Model with two fields. 7 const model = {a: U16, b: I16}; // = {a: 'u16', b: 'i16'}; 8 // Create CStruct from model. Precompile. 9 const cStruct = CStructBE.fromModelTypes(model); 10 // Data to transfer. Make buffer from data. Transfer buffer. 11 const data = {a: 10, b: -10}; 12 // Buffer made. 13 const buffer = cStruct.make(data).buffer; 14 console.log(buffer.toString('hex')); 15 // 000afff6 16 // {a: 000a, b: fff6} 17 18 // Read buffer. Receive data. 19 const result = cStruct.read(buffer); 20 console.log(result.struct); 21 // { a: 10, b: -10 } 22} 23 24// JavaScript Example 2 25{ 26 // Sensor type. ID and value. 27 const types = { 28 Sensor: {id: U16, value: I16}, // Sensor: {id: 'u16', value: 'i16'}, 29 } 30 // Model with IOT name and two sensors. 31 const model = { 32 iotName: STRING(0), // iotName: 's0', 33 sensors: 'Sensor[2]', 34 }; 35 // Create CStruct from model and types. Precompile. 36 const cStruct = CStructBE.fromModelTypes(model, types); 37 // Data to transfer. Make buffer from data. Transfer buffer. 38 const data = { 39 iotName: 'iot-1', 40 sensors: [ 41 {id: 1, value: -10}, 42 {id: 2, value: -20} 43 ] 44 }; 45 // Buffer made. 46 const buffer = cStruct.make(data).buffer; 47 console.log(buffer.toString('hex')); 48 // 696f742d31000001fff60002ffec 49 // '696f742d31'00 [{0001, fff6}, {0002, ffec}] 50 51 // Read buffer. Receive data. 52 const result = cStruct.read(buffer); 53 console.log(result.struct); 54 // { iotName: 'iot-1', sensors: [ { id: 1, value: -10 }, { id: 2, value: -20 } ] } 55}
Make example
1import { CStructBE, CStructProperty } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3class MyClass { 4 @CStructProperty({type: 'u8'}) 5 public propertyA: number; 6 7 @CStructProperty({type: 'i8'}) 8 public propertyB: number; 9} 10 11const myClass = new MyClass(); 12myClass.propertyA = 10; 13myClass.propertyB = -10; 14 15const bufferMake = CStructBE.make(myClass).buffer; 16console.log(bufferMake.toString('hex')); 17// 0af6 18// 0a f6
Read example
1import { CStructBE, CStructProperty } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3class MyClass { 4 @CStructProperty({type: 'u8'}) 5 public propertyA: number; 6 7 @CStructProperty({type: 'i8'}) 8 public propertyB: number; 9} 10 11const buffer = Buffer.from('0af6', 'hex'); 12const myClass = CStructBE.read(MyClass, buffer).struct; 13 14console.log(myClass); 15// MyClass { propertyA: 10, propertyB: -10 } 16console.log(myClass instanceof MyClass); 17// true
CStructClass example
1import { CStructBE, CStructClass } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3@CStructClass({ 4 model: { 5 propertyA: 'u8', 6 propertyB: 'i8', 7 } 8}) 9class MyClass { 10 public propertyA: number; 11 public propertyB: number; 12} 13 14const buffer = Buffer.from('0af6', 'hex'); 15const myClass = CStructBE.read(MyClass, buffer).struct; 16 17console.log(myClass); 18// MyClass { propertyA: 10, propertyB: -10 } 19console.log(myClass instanceof MyClass); 20// true
Read example with offset, and model and types described as string in CStructClass decorator
1import { CStructBE, CStructClass } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3@CStructClass({ 4 model: `{propertyA: U8, propertyB: I8}`, 5 types: '{U8: uint8, I8: int8}', 6}) 7class MyClass { 8 public propertyA: number; 9 public propertyB: number; 10} 11 12const buffer = Buffer.from('77770af6', 'hex'); 13const myClass = CStructBE.read(MyClass, buffer, 2).struct; 14 15console.log(myClass); 16// MyClass { propertyA: 10, propertyB: -10 } 17console.log(myClass instanceof MyClass); 18// true
Off course types: '{U8: uint8, I8: int8}'
shows only some idea how to use types.
Types can be much more complex.
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Make BE buffer from struct based on model 4const model = { a: 'u16', b: 'i16' }; 5const cStruct = CStructBE.fromModelTypes(model); 6 7const data = { a: 10, b: -10 }; 8const buffer = cStruct.make(data).buffer; 9 10console.log(buffer.toString('hex')); 11// 000afff6 12// 000a fff6
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Make BE buffer from struct based on model 4const cStruct = CStructBE.fromModelTypes({ error: {code: 'u16', message: 's20'} }); 5 6const { buffer, offset, size } = cStruct.make({ error: { code: 10, message: 'xyz' } }); 7 8console.log(buffer.toString('hex')); 9// 000a78797a0000000000000000000000000000000000 10console.log(offset); 11// 22 12console.log(size); 13// 22
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Read BE buffer to struct based on model 4const buffer = hexToBuffer('000F 6162630000_0000000000_0000000000'); 5console.log(buffer.toString('hex')); 6// 000f616263000000000000000000000000 7 8const cStruct = CStructBE.fromModelTypes({ error: {code: 'u16', message: 's20'} }); 9 10const struct = cStruct.read(buffer).struct; 11 12console.log(struct); 13// { error: { code: 15, message: 'abc' } }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Read BE buffer to struct based on model 4const buffer = hexToBuffer('000F 6162630000_0000000000_0000000000'); 5console.log(buffer.toString('hex')); 6// 000f616263000000000000000000000000 7 8const cStruct = CStructBE.fromModelTypes({ error: {code: 'u16', message: 's20'} }); 9 10const { struct, offset, size } = cStruct.read(buffer); 11 12console.log(struct); 13// { error: { code: 15, message: 'abc' } } 14console.log(offset); 15// 17 16console.log(size); 17// 17
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3class MyClass { 4 public propertyA: number; 5 public propertyB: number; 6} 7 8const myClass = new MyClass(); 9myClass.propertyA = 10; 10myClass.propertyB = -10; 11 12const cStruct = CStructBE.from({ 13 model: `{propertyA: U16, propertyB: I16}`, 14 types: '{U16: uint16, I16: int16}', 15}); 16const make = cStruct.make(myClass); 17console.log(make.buffer.toString('hex')); 18// 000afff6 19// 000a fff6
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3@CStructClass({ 4 model: `{propertyA: U16, propertyB: I16}`, 5 types: '{U16: uint16, I16: int16}', 6}) 7class MyClass { 8 public propertyA: number; 9 public propertyB: number; 10} 11 12const myClass = new MyClass(); 13myClass.propertyA = 10; 14myClass.propertyB = -10; 15 16const cStruct = CStructBE.from(MyClass); 17const make = cStruct.make(myClass); 18console.log(make.buffer.toString('hex')); 19// 000afff6 20// 000a fff6
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Model and Types for Sender & Receiver 4const types = { 5 Sensor: { 6 id: 'u32', 7 type: 'u8', 8 value: 'd', 9 timestamp: 'u64', 10 } 11}; 12const iotModel = { 13 iotName: 's20', 14 sensor: 'Sensor', 15}; 16 17// IOT Sender 18const sender = CStructBE.fromModelTypes(iotModel, types); 19const senderData = { 20 iotName: 'IOT-1', 21 sensor: { 22 id: 123456789, 23 type: 0x01, 24 value: 123.456, 25 timestamp: 1677277903685n, 26 } 27}; 28const { buffer: senderFrame } = sender.make(senderData); 29 30// Transmitting frame 31console.log(senderFrame.toString('hex')); 32 33// IOT Receiver 34const receiver = CStructBE.fromModelTypes(iotModel, types); 35const { struct: receiverData } = receiver.read(senderFrame); 36console.log(receiverData); 37// { 38// iotName: 'IOT-1', 39// sensor: { 40// id: 123456789, 41// type: 1, 42// value: 123.456, 43// timestamp: 1677277903685 44// } 45// }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Make buffer from struct based on model and types 4const cStruct = CStructBE.fromModelTypes(`{errors: [Error, Error]}`, `{Error: {code: u16, message: s10}}`); 5 6const {buffer, offset, size} = cStruct.make({ 7 errors: [ 8 {code: 0x12, message: 'message1'}, 9 {code: 0x34, message: 'message2'}, 10 ] 11}); 12 13console.log(buffer.toString('hex')); 14// 00126d65737361676531000000346d657373616765320000 15console.log(offset); 16// 24 17console.log(size); 18// 24
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Mixed approach for model and types 4const cStruct = CStructBE.fromModelTypes({errors: `[Error, Error]`}, {Error: `{code: u16, message: s10}`}); 5 6const {buffer, offset, size} = cStruct.make({ 7 errors: [ 8 {code: 0x12, message: 'message1'}, 9 {code: 0x34, message: 'message2'}, 10 ] 11}); 12 13console.log(buffer.toString('hex')); 14// 00126d65737361676531000000346d657373616765320000 15console.log(offset); 16// 24 17console.log(size); 18// 24
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// C-kind fields {u8 a,b;} into {a:u8,b:u8} 4const model = `{u8 a,b;}`; 5const cStruct = CStructBE.fromModelTypes(model); 6 7const makeStruct = { a: 1, b: 2 }; 8const { buffer: structBuffer } = cStruct.make( 9 makeStruct 10); 11console.log(structBuffer.toString('hex')); 12// 0102 13 14const { struct: readStruct } = cStruct.read(structBuffer); 15console.log(readStruct); 16// { a: 1, b: 2 }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Dynamic (length) array with types 4const model = { 5 ab: "Ab[i16]", 6}; 7 8const types = { 9 Ab: {a: 'i8', b: 'i8'} 10}; 11 12const cStruct = CStructBE.fromModelTypes(model, types); 13 14console.log(cStruct.modelClone); 15// { 'ab.i16': { a: 'i8', b: 'i8' } } 16 17const data = { 18 ab: [ 19 {a: '-1', b: '+1'}, 20 {a: '-2', b: '+2'}, 21 ] 22}; 23const {buffer} = cStruct.make(data); 24 25console.log(buffer.toString('hex')); 26// 0002ff01fe02 27 28const {struct: extractedData} = cStruct.read(buffer); 29console.log(extractedData); 30// { ab: [ { a: -1, b: 1 }, { a: -2, b: 2 } ] }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Dynamic (length) string 4const model = { 5 txt1: "s[i16]", 6 txt2: "string[i16]", 7}; 8 9const cStruct = CStructBE.fromModelTypes(model); 10 11console.log(cStruct.modelClone); 12// { 'txt1.i16': 's', 'txt2.i16': 's' } 13 14const data = { 15 txt1: "ABCDE", 16 txt2: "AB" 17}; 18const {buffer} = cStruct.make(data); 19 20console.log(buffer.toString('hex')); 21// 0005414243444500024142 22// 0005_4142434445 0002_4142 23 24const {struct: extractedData} = cStruct.read(buffer); 25console.log(extractedData); 26// { txt1: 'ABCDE', txt2: 'AB' }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3const model = {b: 'BYTE', w: 'WORD', f: 'BOOL'}; 4 5const cStruct = CStructBE.fromModelTypes(model); 6 7console.log(cStruct.modelClone); 8// { b: 'BYTE', w: 'WORD', f: 'BOOL' } 9 10const struct = {b: 0x12, w: 0x3456, f: true}; 11const {buffer} = cStruct.make(struct); 12 13console.log(buffer.toString('hex')); 14// 12345601 15// 12 3456 01 16 17const {struct: extractedData} = cStruct.read(buffer); 18console.log(extractedData); 19// { b: 18, w: 13398, f: true } 20// { b: 0x12, w: 0x3456, f: true }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// C struct types 4const model = { 5 xyzs: "Xyx[2]", 6}; 7const types = `{ 8 typedef struct { 9 uint8_t x; 10 uint8_t y; 11 uint8_t z; 12 } Xyx; 13}`; 14 15const cStruct = CStructBE.fromModelTypes(model, types); 16 17const data = { 18 xyzs: [ 19 {x: 1, y: 2, z: 3}, 20 {x: 4, y: 5, z: 6}, 21 ] 22}; 23const {buffer: makeBuffer} = cStruct.make(data); 24 25console.log(makeBuffer.toString('hex')); 26// 010203040506 27 28const {struct: readStruct} = cStruct.read(makeBuffer); 29console.log(readStruct); 30// { xyzs: [ { x: 1, y: 2, z: 3 }, { x: 4, y: 5, z: 6 } ] }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// C struct types 4const types = `{ 5 // 1st approach 6 typedef struct { 7 u8 x,y,z; 8 } Xyz; 9 10 // 2nd approach 11 struct Ab { 12 i8 x,y; 13 }; 14 15 // As you noticed, comments are allowed 16}`; 17const model = `{ 18 ab: Ab, 19 xyz: Xyz, 20 21 // As you noticed, comments are allowed 22}`; 23 24const cStruct = CStructBE.fromModelTypes(model, types); 25 26const data = { 27 ab: { x: -2, y: -1 }, 28 xyz: { x: 0, y: 1, z: 2 } 29}; 30const {buffer: makeBuffer} = cStruct.make(data); 31 32console.log(makeBuffer.toString('hex')); 33// feff000102 34 35const {struct: readStruct} = cStruct.read(makeBuffer); 36console.log(readStruct); 37// { ab: { x: -2, y: -1 }, xyz: { x: 0, y: 1, z: 2 } }
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Value, static array, dynamic array 4const model = `[ 5 i8, // 1 byte 6 i8[2], // 2 bytes static array 7 i8[i16] // i16 bytes dynamic array 8]`; 9 10const cStruct = CStructBE.fromModelTypes(model); 11 12console.log(cStruct.jsonModel); 13// ["i8","i8.2","i8.i16"] 14console.log(cStruct.modelClone); 15// [ 'i8', 'i8.2', 'i8.i16' ] 16 17const data = [ 18 0x01, 19 [0x02, 0x03], 20 [0x04, 0x05, 0x06, 0x07], 21]; 22const {buffer} = cStruct.make(data); 23 24console.log(buffer.toString('hex')); 25// 010203000404050607 26// 01 02_03 0004_04_05_06_07 27 28const {struct: extractedData} = cStruct.read(buffer); 29console.log(extractedData); 30// [ 1, [ 2, 3 ], [ 4, 5, 6, 7 ] ]
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3class Undecorated { 4 a: number; 5 b: number; 6} 7const undecorated = new Undecorated(); 8undecorated.a = -1; 9undecorated.b = -2; 10 11const undecoratedStruct = CStructBE.from({ 12 model: '{a:float,b:double}', 13}); 14const undecoratedBuffer = undecoratedStruct.make(undecorated).buffer; 15console.log(undecoratedBuffer.toString('hex')); 16// bf800000c000000000000000 17// bf800000 c000000000000000
TypeScript's decorators to serialize/deserialize class object to/from binary
NOTE Take a look on '/examples/decorators.ts'
MUST enable in tsconfig.json
or jsconfig.json
1{ 2 "compilerOptions": { 3 "experimentalDecorators": true 4 } 5}
1import { CStructBE, CStructClass, CStructModelProperty } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3// Decorators - Serialize any class with CStructClass and CStructModelProperty decorator, model and type 4class MyClass { 5 public a: number; 6 public b: number; 7} 8 9@CStructClass({ 10 types: {MyClass: {a: 'u16', b: 'i16'}} 11}) 12class MyData { 13 @CStructModelProperty('MyClass') 14 public myClass: MyClass; 15} 16 17const myData = new MyData(); 18myData.myClass = new MyClass(); 19myData.myClass.a = 10; 20myData.myClass.b = -10; 21 22// MAKE 23const bufferMake = CStructBE.make(myData).buffer; 24console.log(bufferMake.toString('hex')); 25// 000afff6 26// 000a fff6 27 28// READ 29const myDataRead = new MyData(); 30myDataRead.myClass = new MyClass(); 31CStructBE.read(myDataRead, bufferMake); 32console.log(myDataRead); 33// MyData { myClass: MyClass { a: 10, b: -10 } } 34 35// WRITE 36const bufferWrite = Buffer.alloc(4); 37CStructBE.write(myData, bufferWrite); 38console.log(bufferWrite.toString('hex')); 39// 000afff6 40// 000a fff6
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2import * as fs from "fs"; 3 4interface GeoAltitude { 5 lat: number; 6 long: number; 7 alt: number; 8} 9 10@CStructClass({ 11 types: `{ GeoAltitude: { lat:double, long:double, alt:double }}` 12}) 13class GeoAltitudesFile { 14 @CStructProperty({type: 'string30'}) 15 public fileName: string = 'GeoAltitudesFile v1.0'; 16 17 @CStructProperty({type: 'GeoAltitude[i32]'}) 18 public geoAltitudes: GeoAltitude[] = []; 19} 20 21(async () => { 22 // Make random data 23 const geoAltitudesFile = new GeoAltitudesFile(); 24 for (let i = 0; i < 1e6; i++) { 25 let randomLat = Math.random() * (90 - -90) + -90; 26 let randomLong = Math.random() * (180 - -180) + -180; 27 let randomAlt = 6.4e6 * Math.random() * (8e3 - -4e3) + -4e3; 28 const geo = {lat: randomLat, long: randomLong, alt: randomAlt}; 29 geoAltitudesFile.geoAltitudes.push(geo); 30 } 31 console.log('Write data length,', geoAltitudesFile.geoAltitudes.length); 32 33 // Make buffer 34 console.time('make'); 35 const writeFile = CStructBE.make(geoAltitudesFile).buffer; 36 console.timeEnd('make'); 37 38 // Write to file 39 console.log('Write file length,', writeFile.length); 40 await fs.promises.writeFile('geoAltitudesFile.bin', writeFile); 41 42 // Read from file 43 const readFile = await fs.promises.readFile('geoAltitudesFile.bin'); 44 console.log('Read file length,', readFile.length); 45 46 // Read data 47 console.time('read'); 48 const readGeoAltitudesFile = CStructBE.read(GeoAltitudesFile, readFile).struct; 49 console.timeEnd('read'); 50 51 console.log('Read fileName,', readGeoAltitudesFile.fileName); 52 console.log('Read data length,', readGeoAltitudesFile.geoAltitudes.length); 53})();
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3@CStructClass({ 4 model: { 5 any1: 'j[i8]', 6 any2: 'json[i8]', 7 any3: 'any[i8]', 8 } 9}) 10class MyClass { 11 any1: any; 12 any2: any; 13 any3: any; 14} 15 16const myClass = new MyClass(); 17myClass.any1 = {a: 1}; 18myClass.any2 = {b: "B"}; 19myClass.any3 = [1, 3, 5]; 20 21const buffer = CStructBE.make(myClass).buffer; 22console.log(buffer.toString('hex')); 23// 077b2261223a317d097b2262223a2242227d075b312c332c355d 24// 07_7b2261223a317d 09_7b2262223a2242227d 07_5b312c332c355d 25const myClass2 = CStructBE.read(MyClass, buffer).struct; 26console.log(myClass2); 27// MyClass { 28// any1: { a: 1 }, 29// any2: { b: 'B' }, 30// any3: [ 1, 3, 5 ] 31// }
This library has support for trailing zero for "string" and "json" types.
When you use it "json" and "string" will be written as full unknown length and '\0' will be added at the end.
That ending zero helps to read data from binary file without knowing the length of the string / json.
We can not use that trick with buffer as it may contain zeros at any place.
1import { CStructBE } from '@teamteanpm2024/modi-dolorem-aspernatur'; 2 3const model = { 4 any1: 'j[0]', // or 'json[0]' or 'any[0]' 5 any2: 's[0]', // or 'string[0]' 6}; 7 8const cStruct = CStructBE.fromModelTypes(model); 9 10const data = { 11 any1: [1, 2, 3], 12 any2: 'abc', 13}; 14 15const buffer = cStruct.make(data).buffer; 16console.log(buffer.toString('hex')); 17// 5b312c322c335d0061626300 18// 5b_31_2c_32_2c_33_5d_00 616263_00 19// [ 1 , 2 , 3 ] \0 a b c \0 20 21const extractedData = cStruct.read(buffer).struct; 22console.log(extractedData); 23// { any1: [ 1, 2, 3 ], any2: 'abc' } 24 25console.log(JSON.stringify(data) === JSON.stringify(extractedData)); 26// true
If you have any questions or suggestions, please contact me at
mrhiden@outlook.com
No vulnerabilities found.
No security vulnerabilities found.