@@ -31,6 +31,10 @@ interface ParserOptions {
31
31
offset ?: number | string | ( ( item : any ) => number ) ;
32
32
}
33
33
34
+ interface EncoderOptions {
35
+ bitEndianess ?: boolean ;
36
+ }
37
+
34
38
type Types = PrimitiveTypes | ComplexTypes ;
35
39
36
40
type ComplexTypes =
@@ -175,12 +179,16 @@ export class Parser {
175
179
constructorFn : Function | null = null ;
176
180
alias : string | null = null ;
177
181
smartBufferSize : number ;
182
+ encoderOpts : EncoderOptions ;
178
183
179
184
constructor ( opts ?: any ) {
180
185
this . smartBufferSize =
181
186
opts && typeof opts === 'object' && opts . smartBufferSize
182
187
? opts . smartBufferSize
183
188
: 256 ;
189
+ this . encoderOpts = {
190
+ bitEndianess : false ,
191
+ } ;
184
192
}
185
193
186
194
static start ( opts ?: any ) {
@@ -200,7 +208,7 @@ export class Parser {
200
208
const typeName = CAPITILIZED_TYPE_NAMES [ type ] ;
201
209
202
210
ctx . pushCode (
203
- `smartBuffer.write${ typeName } (${ ctx . generateVariable ( this . varName ) } );` ,
211
+ `smartBuffer.write${ typeName } (${ ctx . generateVariable ( this . varName ) } );`
204
212
) ;
205
213
}
206
214
@@ -268,9 +276,7 @@ export class Parser {
268
276
const [ major ] = process . version . replace ( 'v' , '' ) . split ( '.' ) ;
269
277
if ( Number ( major ) < 12 ) {
270
278
throw new Error (
271
- `The methods readBigInt64BE, readBigInt64BE, readBigInt64BE, readBigInt64BE are not avilable in your version of nodejs: ${
272
- process . version
273
- } , you must use v12 or greater`
279
+ `The methods readBigInt64BE, readBigInt64BE, readBigInt64BE, readBigInt64BE are not avilable in your version of nodejs: ${ process . version } , you must use v12 or greater`
274
280
) ;
275
281
}
276
282
}
@@ -590,6 +596,11 @@ export class Parser {
590
596
return this ;
591
597
}
592
598
599
+ encoderSetOptions ( opts : EncoderOptions ) {
600
+ Object . assign ( this . encoderOpts , opts ) ;
601
+ return this ;
602
+ }
603
+
593
604
create ( constructorFn : Function ) {
594
605
if ( ! ( constructorFn instanceof Function ) ) {
595
606
throw new Error ( 'Constructor must be a Function object.' ) ;
@@ -658,8 +669,7 @@ export class Parser {
658
669
private addRawCodeEncode ( ctx : Context ) {
659
670
ctx . pushCode ( 'var vars = obj;' ) ;
660
671
ctx . pushCode (
661
- `var smartBuffer = SmartBuffer.fromOptions({size: ${
662
- this . smartBufferSize } , encoding: "utf8"});`
672
+ `var smartBuffer = SmartBuffer.fromOptions({size: ${ this . smartBufferSize } , encoding: "utf8"});`
663
673
) ;
664
674
665
675
this . generateEncode ( ctx ) ;
@@ -694,8 +704,7 @@ export class Parser {
694
704
695
705
ctx . pushCode ( 'var vars = obj;' ) ;
696
706
ctx . pushCode (
697
- `var smartBuffer = SmartBuffer.fromOptions({size: ${
698
- this . smartBufferSize } , encoding: "utf8"});`
707
+ `var smartBuffer = SmartBuffer.fromOptions({size: ${ this . smartBufferSize } , encoding: "utf8"});`
699
708
) ;
700
709
701
710
this . generateEncode ( ctx ) ;
@@ -808,6 +817,7 @@ export class Parser {
808
817
parser . varName = varName ;
809
818
parser . options = options || parser . options ;
810
819
parser . endian = this . endian ;
820
+ parser . encoderOpts = this . encoderOpts ;
811
821
812
822
if ( this . head ) {
813
823
this . head . next = parser ;
@@ -1083,14 +1093,25 @@ export class Parser {
1083
1093
) ;
1084
1094
}
1085
1095
1086
-
1096
+ const isBitLittleEndian =
1097
+ this . endian === 'le' && this . encoderOpts . bitEndianess ;
1087
1098
const tmpVal = ctx . generateTmpVariable ( ) ;
1099
+ const boundVal = ctx . generateTmpVariable ( ) ;
1088
1100
ctx . pushCode ( `var ${ tmpVal } = 0;` ) ;
1101
+ ctx . pushCode ( `var ${ boundVal } = 0;` ) ;
1089
1102
let bitOffset = 0 ;
1090
1103
ctx . bitFields . forEach ( parser => {
1091
1104
ctx . pushCode (
1092
- `${ tmpVal } |= (${ parser . varName } << ${
1093
- sum - ( parser . options . length as number ) - bitOffset } );`
1105
+ `${ boundVal } = (${ parser . varName } & ${ ( 1 <<
1106
+ ( parser . options . length as number ) ) -
1107
+ 1 } );`
1108
+ ) ;
1109
+ ctx . pushCode (
1110
+ `${ tmpVal } |= (${ boundVal } << ${
1111
+ isBitLittleEndian
1112
+ ? bitOffset
1113
+ : sum - ( parser . options . length as number ) - bitOffset
1114
+ } );`
1094
1115
) ;
1095
1116
ctx . pushCode ( `${ tmpVal } = ${ tmpVal } >>> 0;` ) ;
1096
1117
bitOffset += parser . options . length as number ;
@@ -1206,7 +1227,7 @@ export class Parser {
1206
1227
}
1207
1228
} else {
1208
1229
ctx . pushCode (
1209
- `smartBuffer.writeString(${ name } , "${ this . options . encoding } ");` ,
1230
+ `smartBuffer.writeString(${ name } , "${ this . options . encoding } ");`
1210
1231
) ;
1211
1232
}
1212
1233
if ( this . options . zeroTerminated ) {
@@ -1342,15 +1363,16 @@ export class Parser {
1342
1363
// Compute the desired count of array items to encode (min of array size
1343
1364
// and length option)
1344
1365
if ( length !== undefined ) {
1345
- ctx . pushCode ( `${ maxItems } = ${ maxItems } > ${ length } ? ${ length } : ${ maxItems } ` ) ;
1366
+ ctx . pushCode (
1367
+ `${ maxItems } = ${ maxItems } > ${ length } ? ${ length } : ${ maxItems } `
1368
+ ) ;
1346
1369
}
1347
1370
1348
1371
// Save current encoding smartBuffer and allocate a new one
1349
1372
const savSmartBuffer = ctx . generateTmpVariable ( ) ;
1350
1373
ctx . pushCode (
1351
1374
`var ${ savSmartBuffer } = smartBuffer; ` +
1352
- `smartBuffer = SmartBuffer.fromOptions({size: ${
1353
- this . smartBufferSize } , encoding: "utf8"});`,
1375
+ `smartBuffer = SmartBuffer.fromOptions({size: ${ this . smartBufferSize } , encoding: "utf8"});`
1354
1376
) ;
1355
1377
1356
1378
ctx . pushCode ( `if(${ maxItems } > 0) {` ) ;
@@ -1370,7 +1392,9 @@ export class Parser {
1370
1392
1371
1393
if ( typeof type === 'string' ) {
1372
1394
if ( ! aliasRegistry [ type ] ) {
1373
- ctx . pushCode ( `smartBuffer.write${ CAPITILIZED_TYPE_NAMES [ type as Types ] } (${ item } );` ) ;
1395
+ ctx . pushCode (
1396
+ `smartBuffer.write${ CAPITILIZED_TYPE_NAMES [ type as Types ] } (${ item } );`
1397
+ ) ;
1374
1398
} else {
1375
1399
ctx . pushCode (
1376
1400
`smartBuffer.writeBuffer(${ FUNCTION_ENCODE_PREFIX + type } (${ item } ));`
@@ -1389,13 +1413,11 @@ export class Parser {
1389
1413
1390
1414
if ( typeof this . options . encodeUntil === 'function' ) {
1391
1415
ctx . pushCode (
1392
- ` while (${ itemCounter } < ${ maxItems } && !(${
1393
- this . options . encodeUntil } ).call(this, ${ item } , vars));`
1416
+ ` while (${ itemCounter } < ${ maxItems } && !(${ this . options . encodeUntil } ).call(this, ${ item } , vars));`
1394
1417
) ;
1395
1418
} else if ( typeof this . options . readUntil === 'function' ) {
1396
1419
ctx . pushCode (
1397
- ` while (${ itemCounter } < ${ maxItems } && !(${
1398
- this . options . readUntil } ).call(this, ${ item } , ${ savSmartBuffer } .toBuffer()));`
1420
+ ` while (${ itemCounter } < ${ maxItems } && !(${ this . options . readUntil } ).call(this, ${ item } , ${ savSmartBuffer } .toBuffer()));`
1399
1421
) ;
1400
1422
}
1401
1423
ctx . pushCode ( '}' ) ; // End of 'if(...) {'
@@ -1446,14 +1468,15 @@ export class Parser {
1446
1468
if ( typeof type === 'string' ) {
1447
1469
if ( ! aliasRegistry [ type ] ) {
1448
1470
ctx . pushCode (
1449
- `smartBuffer.write${ CAPITILIZED_TYPE_NAMES [ type as Types ] } (${
1450
- ctx . generateVariable ( this . varName ) } );`
1471
+ `smartBuffer.write${
1472
+ CAPITILIZED_TYPE_NAMES [ type as Types ]
1473
+ } (${ ctx . generateVariable ( this . varName ) } );`
1451
1474
) ;
1452
1475
} else {
1453
1476
const tempVar = ctx . generateTmpVariable ( ) ;
1454
1477
ctx . pushCode (
1455
- `var ${ tempVar } = ${ FUNCTION_ENCODE_PREFIX + type } ( ${
1456
- ctx . generateVariable ( this . varName ) } );`
1478
+ `var ${ tempVar } = ${ FUNCTION_ENCODE_PREFIX +
1479
+ type } ( ${ ctx . generateVariable ( this . varName ) } );`
1457
1480
) ;
1458
1481
ctx . pushCode ( `smartBuffer.writeBuffer(${ tempVar } );` ) ;
1459
1482
if ( type !== this . alias ) ctx . addReference ( type ) ;
@@ -1542,7 +1565,8 @@ export class Parser {
1542
1565
} else if ( aliasRegistry [ this . options . type ] ) {
1543
1566
var tempVar = ctx . generateTmpVariable ( ) ;
1544
1567
ctx . pushCode (
1545
- `var ${ tempVar } = ${ FUNCTION_ENCODE_PREFIX + this . options . type } (${ nestVar } );` ,
1568
+ `var ${ tempVar } = ${ FUNCTION_ENCODE_PREFIX +
1569
+ this . options . type } (${ nestVar } );`
1546
1570
) ;
1547
1571
ctx . pushCode ( `smartBuffer.writeBuffer(${ tempVar } );` ) ;
1548
1572
if ( this . options . type !== this . alias ) {
@@ -1557,15 +1581,13 @@ export class Parser {
1557
1581
formatter : Function
1558
1582
) {
1559
1583
if ( typeof formatter === 'function' ) {
1560
- ctx . pushCode ( `${ varName } = (${ formatter } ).call(this, ${ varName } , buffer, offset);` ) ;
1584
+ ctx . pushCode (
1585
+ `${ varName } = (${ formatter } ).call(this, ${ varName } , buffer, offset);`
1586
+ ) ;
1561
1587
}
1562
1588
}
1563
1589
1564
- private generateEncoder (
1565
- ctx : Context ,
1566
- varName : string ,
1567
- encoder ?: Function
1568
- ) {
1590
+ private generateEncoder ( ctx : Context , varName : string , encoder ?: Function ) {
1569
1591
if ( typeof encoder === 'function' ) {
1570
1592
ctx . pushCode ( `${ varName } = (${ encoder } ).call(this, ${ varName } , vars);` ) ;
1571
1593
}
0 commit comments