@@ -75,9 +75,9 @@ class Packer {
75
75
* @param x the value to pack
76
76
* @returns Function
77
77
*/
78
- packable ( x , dehydrate = functional . identity ) {
78
+ packable ( x , dehydrateStruct = functional . identity ) {
79
79
try {
80
- x = dehydrate ( x )
80
+ x = dehydrateStruct ( x )
81
81
} catch ( ex ) {
82
82
return ( ) => ex
83
83
}
@@ -102,15 +102,15 @@ class Packer {
102
102
return ( ) => {
103
103
this . packListHeader ( x . length )
104
104
for ( let i = 0 ; i < x . length ; i ++ ) {
105
- this . packable ( x [ i ] === undefined ? null : x [ i ] ) ( )
105
+ this . packable ( x [ i ] === undefined ? null : x [ i ] , dehydrateStruct ) ( )
106
106
}
107
107
}
108
108
} else if ( isIterable ( x ) ) {
109
- return this . packableIterable ( x )
109
+ return this . packableIterable ( x , dehydrateStruct )
110
110
} else if ( x instanceof Structure ) {
111
111
const packableFields = [ ]
112
112
for ( let i = 0 ; i < x . fields . length ; i ++ ) {
113
- packableFields [ i ] = this . packable ( x . fields [ i ] )
113
+ packableFields [ i ] = this . packable ( x . fields [ i ] , dehydrateStruct )
114
114
}
115
115
return ( ) => this . packStruct ( x . signature , packableFields )
116
116
} else if ( typeof x === 'object' ) {
@@ -128,7 +128,7 @@ class Packer {
128
128
const key = keys [ i ]
129
129
if ( x [ key ] !== undefined ) {
130
130
this . packString ( key )
131
- this . packable ( x [ key ] ) ( )
131
+ this . packable ( x [ key ] , dehydrateStruct ) ( )
132
132
}
133
133
}
134
134
}
@@ -137,10 +137,10 @@ class Packer {
137
137
}
138
138
}
139
139
140
- packableIterable ( iterable ) {
140
+ packableIterable ( iterable , dehydrateStruct ) {
141
141
try {
142
142
const array = Array . from ( iterable )
143
- return this . packable ( array )
143
+ return this . packable ( array , dehydrateStruct )
144
144
} catch ( e ) {
145
145
// handle errors from iterable to array conversion
146
146
throw newError ( `Cannot pack given iterable, ${ e . message } : ${ iterable } ` )
@@ -331,63 +331,58 @@ class Unpacker {
331
331
this . _useBigInt = useBigInt
332
332
}
333
333
334
- unpack ( buffer , hydrate = functional . identity ) {
335
- const deserialize = ( ) => {
336
- const marker = buffer . readUInt8 ( )
337
- const markerHigh = marker & 0xf0
338
- const markerLow = marker & 0x0f
334
+ unpack ( buffer , hydrateStructure = functional . identity ) {
335
+ const marker = buffer . readUInt8 ( )
336
+ const markerHigh = marker & 0xf0
337
+ const markerLow = marker & 0x0f
339
338
340
- if ( marker === NULL ) {
341
- return null
342
- }
339
+ if ( marker === NULL ) {
340
+ return null
341
+ }
343
342
344
- const boolean = this . _unpackBoolean ( marker )
345
- if ( boolean !== null ) {
346
- return boolean
347
- }
343
+ const boolean = this . _unpackBoolean ( marker )
344
+ if ( boolean !== null ) {
345
+ return boolean
346
+ }
348
347
349
- const numberOrInteger = this . _unpackNumberOrInteger ( marker , buffer )
350
- if ( numberOrInteger !== null ) {
351
- if ( isInt ( numberOrInteger ) ) {
352
- if ( this . _useBigInt ) {
353
- return numberOrInteger . toBigInt ( )
354
- } else if ( this . _disableLosslessIntegers ) {
355
- return numberOrInteger . toNumberOrInfinity ( )
356
- }
348
+ const numberOrInteger = this . _unpackNumberOrInteger ( marker , buffer )
349
+ if ( numberOrInteger !== null ) {
350
+ if ( isInt ( numberOrInteger ) ) {
351
+ if ( this . _useBigInt ) {
352
+ return numberOrInteger . toBigInt ( )
353
+ } else if ( this . _disableLosslessIntegers ) {
354
+ return numberOrInteger . toNumberOrInfinity ( )
357
355
}
358
- return numberOrInteger
359
- }
360
-
361
- const string = this . _unpackString ( marker , markerHigh , markerLow , buffer )
362
- if ( string !== null ) {
363
- return string
364
356
}
357
+ return numberOrInteger
358
+ }
365
359
366
- const list = this . _unpackList ( marker , markerHigh , markerLow , buffer )
367
- if ( list !== null ) {
368
- return list
369
- }
360
+ const string = this . _unpackString ( marker , markerHigh , markerLow , buffer )
361
+ if ( string !== null ) {
362
+ return string
363
+ }
370
364
371
- const byteArray = this . _unpackByteArray ( marker , buffer )
372
- if ( byteArray !== null ) {
373
- return byteArray
374
- }
365
+ const list = this . _unpackList ( marker , markerHigh , markerLow , buffer , hydrateStructure )
366
+ if ( list !== null ) {
367
+ return list
368
+ }
375
369
376
- const map = this . _unpackMap ( marker , markerHigh , markerLow , buffer )
377
- if ( map !== null ) {
378
- return map
379
- }
370
+ const byteArray = this . _unpackByteArray ( marker , buffer )
371
+ if ( byteArray !== null ) {
372
+ return byteArray
373
+ }
380
374
381
- const struct = this . _unpackStruct ( marker , markerHigh , markerLow , buffer )
382
- if ( struct !== null ) {
383
- return struct
384
- }
375
+ const map = this . _unpackMap ( marker , markerHigh , markerLow , buffer , hydrateStructure )
376
+ if ( map !== null ) {
377
+ return map
378
+ }
385
379
386
- throw newError ( 'Unknown packed value with marker ' + marker . toString ( 16 ) )
380
+ const struct = this . _unpackStruct ( marker , markerHigh , markerLow , buffer , hydrateStructure )
381
+ if ( struct !== null ) {
382
+ return struct
387
383
}
388
384
389
- const deserialized = deserialize ( )
390
- return hydrate ( deserialized )
385
+ throw newError ( 'Unknown packed value with marker ' + marker . toString ( 16 ) )
391
386
}
392
387
393
388
unpackInteger ( buffer ) {
@@ -454,24 +449,24 @@ class Unpacker {
454
449
}
455
450
}
456
451
457
- _unpackList ( marker , markerHigh , markerLow , buffer ) {
452
+ _unpackList ( marker , markerHigh , markerLow , buffer , hydrateStructure ) {
458
453
if ( markerHigh === TINY_LIST ) {
459
- return this . _unpackListWithSize ( markerLow , buffer )
454
+ return this . _unpackListWithSize ( markerLow , buffer , hydrateStructure )
460
455
} else if ( marker === LIST_8 ) {
461
- return this . _unpackListWithSize ( buffer . readUInt8 ( ) , buffer )
456
+ return this . _unpackListWithSize ( buffer . readUInt8 ( ) , buffer , hydrateStructure )
462
457
} else if ( marker === LIST_16 ) {
463
- return this . _unpackListWithSize ( buffer . readUInt16 ( ) , buffer )
458
+ return this . _unpackListWithSize ( buffer . readUInt16 ( ) , buffer , hydrateStructure )
464
459
} else if ( marker === LIST_32 ) {
465
- return this . _unpackListWithSize ( buffer . readUInt32 ( ) , buffer )
460
+ return this . _unpackListWithSize ( buffer . readUInt32 ( ) , buffer , hydrateStructure )
466
461
} else {
467
462
return null
468
463
}
469
464
}
470
465
471
- _unpackListWithSize ( size , buffer ) {
466
+ _unpackListWithSize ( size , buffer , hydrateStructure ) {
472
467
const value = [ ]
473
468
for ( let i = 0 ; i < size ; i ++ ) {
474
- value . push ( this . unpack ( buffer ) )
469
+ value . push ( this . unpack ( buffer , hydrateStructure ) )
475
470
}
476
471
return value
477
472
}
@@ -496,48 +491,49 @@ class Unpacker {
496
491
return value
497
492
}
498
493
499
- _unpackMap ( marker , markerHigh , markerLow , buffer ) {
494
+ _unpackMap ( marker , markerHigh , markerLow , buffer , hydrateStructure ) {
500
495
if ( markerHigh === TINY_MAP ) {
501
- return this . _unpackMapWithSize ( markerLow , buffer )
496
+ return this . _unpackMapWithSize ( markerLow , buffer , hydrateStructure )
502
497
} else if ( marker === MAP_8 ) {
503
- return this . _unpackMapWithSize ( buffer . readUInt8 ( ) , buffer )
498
+ return this . _unpackMapWithSize ( buffer . readUInt8 ( ) , buffer , hydrateStructure )
504
499
} else if ( marker === MAP_16 ) {
505
- return this . _unpackMapWithSize ( buffer . readUInt16 ( ) , buffer )
500
+ return this . _unpackMapWithSize ( buffer . readUInt16 ( ) , buffer , hydrateStructure )
506
501
} else if ( marker === MAP_32 ) {
507
- return this . _unpackMapWithSize ( buffer . readUInt32 ( ) , buffer )
502
+ return this . _unpackMapWithSize ( buffer . readUInt32 ( ) , buffer , hydrateStructure )
508
503
} else {
509
504
return null
510
505
}
511
506
}
512
507
513
- _unpackMapWithSize ( size , buffer ) {
508
+ _unpackMapWithSize ( size , buffer , hydrateStructure ) {
514
509
const value = { }
515
510
for ( let i = 0 ; i < size ; i ++ ) {
516
- const key = this . unpack ( buffer )
517
- value [ key ] = this . unpack ( buffer )
511
+ const key = this . unpack ( buffer , hydrateStructure )
512
+ value [ key ] = this . unpack ( buffer , hydrateStructure )
518
513
}
519
514
return value
520
515
}
521
516
522
- _unpackStruct ( marker , markerHigh , markerLow , buffer ) {
517
+ _unpackStruct ( marker , markerHigh , markerLow , buffer , hydrateStructure ) {
523
518
if ( markerHigh === TINY_STRUCT ) {
524
- return this . _unpackStructWithSize ( markerLow , buffer )
519
+ return this . _unpackStructWithSize ( markerLow , buffer , hydrateStructure )
525
520
} else if ( marker === STRUCT_8 ) {
526
- return this . _unpackStructWithSize ( buffer . readUInt8 ( ) , buffer )
521
+ return this . _unpackStructWithSize ( buffer . readUInt8 ( ) , buffer , hydrateStructure )
527
522
} else if ( marker === STRUCT_16 ) {
528
- return this . _unpackStructWithSize ( buffer . readUInt16 ( ) , buffer )
523
+ return this . _unpackStructWithSize ( buffer . readUInt16 ( ) , buffer , hydrateStructure )
529
524
} else {
530
525
return null
531
526
}
532
527
}
533
528
534
- _unpackStructWithSize ( structSize , buffer ) {
529
+ _unpackStructWithSize ( structSize , buffer , hydrateStructure ) {
535
530
const signature = buffer . readUInt8 ( )
536
531
const structure = new Structure ( signature , [ ] )
537
532
for ( let i = 0 ; i < structSize ; i ++ ) {
538
- structure . fields . push ( this . unpack ( buffer ) )
533
+ structure . fields . push ( this . unpack ( buffer , hydrateStructure ) )
539
534
}
540
- return structure
535
+
536
+ return hydrateStructure ( structure )
541
537
}
542
538
}
543
539
@@ -548,4 +544,4 @@ function isIterable (obj) {
548
544
return typeof obj [ Symbol . iterator ] === 'function'
549
545
}
550
546
551
- export { Packer , Unpacker , Structure }
547
+ export { Packer , Unpacker }
0 commit comments