1
1
import { fromUtf8 , toUtf8 } from "@smithy/util-utf8" ;
2
2
3
- import { ByteVector , join } from "./ByteVector" ;
3
+ import { byteVector , join } from "./ByteVector" ;
4
4
5
5
/**
6
6
* This cbor serde implementation is derived from AWS SDK for Go's implementation.
@@ -44,7 +44,7 @@ type Int64 = bigint;
44
44
type Float16Binary = number ;
45
45
type Float32Binary = number ;
46
46
47
- type CborMajorType =
47
+ export type CborMajorType =
48
48
| typeof majorUint64
49
49
| typeof majorNegativeInt64
50
50
| typeof majorUnstructuredByteString
@@ -94,25 +94,6 @@ function demote(bigInteger: bigint): number {
94
94
return num ;
95
95
}
96
96
97
- /**
98
- * @internal
99
- * Write unsigned int into uint8 array.
100
- */
101
- export function setUnsignedInt ( bitSize : 16 | 32 | 64 , value : number | bigint , byteArray : Uint8Array ) : Uint8Array {
102
- switch ( bitSize ) {
103
- case 16 :
104
- offsetDataView ( byteArray ) . setUint16 ( 0 , Number ( value ) as number ) ;
105
- break ;
106
- case 32 :
107
- offsetDataView ( byteArray ) . setUint32 ( 0 , Number ( value ) as number ) ;
108
- break ;
109
- case 64 :
110
- offsetDataView ( byteArray ) . setBigUint64 ( 0 , BigInt ( value ) as bigint ) ;
111
- break ;
112
- }
113
- return byteArray ;
114
- }
115
-
116
97
// decode
117
98
118
99
function float16ToUint32 ( float : Float16Binary ) : Uint32 {
@@ -480,7 +461,7 @@ function decode(payload: Uint8Array, bigIntBehavior: BigIntBehavior): [CborValue
480
461
481
462
// encode
482
463
483
- function encodeHeader ( major : CborMajorType , value : Uint64 | number , byteVector : ByteVector ) : void {
464
+ function encodeHeader ( major : CborMajorType , value : Uint64 | number ) : void {
484
465
if ( value < 24 ) {
485
466
byteVector . write ( ( major << 5 ) | ( value as number ) ) ;
486
467
return ;
@@ -489,22 +470,13 @@ function encodeHeader(major: CborMajorType, value: Uint64 | number, byteVector:
489
470
byteVector . write ( value as number ) ;
490
471
return ;
491
472
} else if ( value < TWO . SIXTEEN ) {
492
- const float16Container = new Uint8Array ( 3 ) ;
493
- float16Container [ 0 ] = ( major << 5 ) | specialFloat16 ;
494
- setUnsignedInt ( 16 , value , float16Container . subarray ( 1 ) ) ;
495
- byteVector . writeSeries ( float16Container ) ;
473
+ byteVector . writeUnsignedInt ( major , 16 , value ) ;
496
474
return ;
497
475
} else if ( value < TWO . THIRTY_TWO ) {
498
- const float32Container = new Uint8Array ( 5 ) ;
499
- float32Container [ 0 ] = ( major << 5 ) | specialFloat32 ;
500
- setUnsignedInt ( 32 , value , float32Container . subarray ( 1 ) ) ;
501
- byteVector . writeSeries ( float32Container ) ;
476
+ byteVector . writeUnsignedInt ( major , 32 , value ) ;
502
477
return ;
503
478
}
504
- const float64Container = new Uint8Array ( 9 ) ;
505
- float64Container [ 0 ] = ( major << 5 ) | specialFloat64 ;
506
- setUnsignedInt ( 64 , value , float64Container . subarray ( 1 ) ) ;
507
- byteVector . writeSeries ( float64Container ) ;
479
+ byteVector . writeUnsignedInt ( major , 64 , value ) ;
508
480
return ;
509
481
}
510
482
@@ -516,7 +488,7 @@ function compose(major: CborMajorType, minor: Uint8): Uint8 {
516
488
* @param input - JS data object.
517
489
* @param byteVector - mutated, not returned.
518
490
*/
519
- function encode ( input : any , byteVector : ByteVector ) : void {
491
+ function encode ( input : any ) : void {
520
492
if ( input === null ) {
521
493
byteVector . write ( compose ( majorSpecial , specialNull ) ) ;
522
494
return ;
@@ -534,9 +506,7 @@ function encode(input: any, byteVector: ByteVector): void {
534
506
return ;
535
507
case "number" :
536
508
if ( Number . isInteger ( input ) ) {
537
- input >= 0
538
- ? encodeHeader ( majorUint64 , input , byteVector )
539
- : encodeHeader ( majorNegativeInt64 , Math . abs ( input ) - 1 , byteVector ) ;
509
+ input >= 0 ? encodeHeader ( majorUint64 , input ) : encodeHeader ( majorNegativeInt64 , Math . abs ( input ) - 1 ) ;
540
510
return ;
541
511
}
542
512
const float64Container = new Uint8Array ( 9 ) ;
@@ -545,33 +515,31 @@ function encode(input: any, byteVector: ByteVector): void {
545
515
byteVector . writeSeries ( float64Container ) ;
546
516
return ;
547
517
case "bigint" :
548
- input >= 0
549
- ? encodeHeader ( majorUint64 , input , byteVector )
550
- : encodeHeader ( majorNegativeInt64 , - input - BigInt ( 1 ) , byteVector ) ;
518
+ input >= 0 ? encodeHeader ( majorUint64 , input ) : encodeHeader ( majorNegativeInt64 , - input - BigInt ( 1 ) ) ;
551
519
return ;
552
520
case "string" :
553
- encodeHeader ( majorUtf8String , input . length , byteVector ) ;
521
+ encodeHeader ( majorUtf8String , input . length ) ;
554
522
byteVector . writeSeries ( fromUtf8 ( input ) ) ;
555
523
return ;
556
524
}
557
525
558
526
if ( Array . isArray ( input ) ) {
559
527
const _input = input . filter ( notUndef ) ;
560
- encodeHeader ( majorList , _input . length , byteVector ) ;
528
+ encodeHeader ( majorList , _input . length ) ;
561
529
for ( const vv of _input ) {
562
- encode ( vv , byteVector ) ;
530
+ encode ( vv ) ;
563
531
}
564
532
return ;
565
533
} else if ( typeof input . byteLength === "number" ) {
566
- encodeHeader ( majorUnstructuredByteString , input . length , byteVector ) ;
534
+ encodeHeader ( majorUnstructuredByteString , input . length ) ;
567
535
byteVector . writeSeries ( input ) ;
568
536
return ;
569
537
} else if ( typeof input === "object" ) {
570
538
const entries = Object . entries ( input ) . filter ( valueNotUndef ) ;
571
- encodeHeader ( majorMap , entries . length , byteVector ) ;
539
+ encodeHeader ( majorMap , entries . length ) ;
572
540
for ( const [ key , value ] of entries ) {
573
- encode ( key , byteVector ) ;
574
- encode ( value , byteVector ) ;
541
+ encode ( key ) ;
542
+ encode ( value ) ;
575
543
}
576
544
return ;
577
545
}
@@ -595,8 +563,7 @@ export const cbor = {
595
563
return decode ( payload , bigIntBehavior ) [ 0 ] ;
596
564
} ,
597
565
serialize ( input : any ) {
598
- const byteVector = new ByteVector ( ) ;
599
- encode ( input , byteVector ) ;
600
- return byteVector . data . subarray ( 0 , byteVector . cursor ) ;
566
+ encode ( input ) ;
567
+ return byteVector . toUint8Array ( ) ;
601
568
} ,
602
569
} ;
0 commit comments