11import { describe , it , expect } from "vitest" ;
2- import CBOR from "cbor-js" ;
3- import cborTypedArrayTagger from "../src/util/cborTypedArrayTags.ts" ;
2+ import { decode } from "cbor2" ;
43
54/** Convert hex string to ArrayBuffer. */
65function hexToBuffer ( hex : string ) {
@@ -11,13 +10,13 @@ function hexToBuffer(hex: string) {
1110 const arr = tokens . map ( function ( t ) {
1211 return parseInt ( t , 16 ) ;
1312 } ) ;
14- return new Uint8Array ( arr ) . buffer ;
13+ return new Uint8Array ( arr ) ;
1514}
1615
1716describe ( "CBOR Typed Array Tagger" , function ( ) {
1817 it ( "should convert tagged Uint16Array" , function ( ) {
1918 const data = hexToBuffer ( "d84546010002000300" ) ;
20- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
19+ const msg = decode ( data ) ;
2120
2221 if ( ! ( msg instanceof Uint16Array ) ) {
2322 throw new Error ( "Expected Uint16Array" ) ;
@@ -30,7 +29,7 @@ describe("CBOR Typed Array Tagger", function () {
3029
3130 it ( "should convert tagged Uint32Array" , function ( ) {
3231 const data = hexToBuffer ( "d8464c010000000200000003000000" ) ;
33- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
32+ const msg = decode ( data ) ;
3433
3534 if ( ! ( msg instanceof Uint32Array ) ) {
3635 throw new Error ( "Expected Uint32Array" ) ;
@@ -41,24 +40,24 @@ describe("CBOR Typed Array Tagger", function () {
4140 expect ( msg [ 2 ] ) . to . equal ( 3 ) ;
4241 } ) ;
4342
44- it ( "should convert tagged Uint64Array " , function ( ) {
43+ it ( "should convert tagged BigUint64Array " , function ( ) {
4544 const data = hexToBuffer (
4645 "d8475818010000000000000002000000000000000300000000000000" ,
4746 ) ;
48- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
47+ const msg = decode ( data ) ;
4948
50- if ( ! Array . isArray ( msg ) ) {
51- throw new Error ( "Expected Array " ) ;
49+ if ( ! ( msg instanceof BigUint64Array ) ) {
50+ throw new Error ( "Expected BigUint64Array " ) ;
5251 }
5352 expect ( msg ) . to . have . lengthOf ( 3 ) ;
54- expect ( msg [ 0 ] ) . to . equal ( 1 ) ;
55- expect ( msg [ 1 ] ) . to . equal ( 2 ) ;
56- expect ( msg [ 2 ] ) . to . equal ( 3 ) ;
53+ expect ( msg [ 0 ] ) . to . equal ( BigInt ( 1 ) ) ;
54+ expect ( msg [ 1 ] ) . to . equal ( BigInt ( 2 ) ) ;
55+ expect ( msg [ 2 ] ) . to . equal ( BigInt ( 3 ) ) ;
5756 } ) ;
5857
5958 it ( "should convert tagged Int8Array" , function ( ) {
6059 const data = hexToBuffer ( "d8484301fe03" ) ;
61- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
60+ const msg = decode ( data ) ;
6261
6362 if ( ! ( msg instanceof Int8Array ) ) {
6463 throw new Error ( "Expected Int8Array" ) ;
@@ -71,7 +70,7 @@ describe("CBOR Typed Array Tagger", function () {
7170
7271 it ( "should convert tagged Int16Array" , function ( ) {
7372 const data = hexToBuffer ( "d84d460100feff0300" ) ;
74- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
73+ const msg = decode ( data ) ;
7574
7675 if ( ! ( msg instanceof Int16Array ) ) {
7776 throw new Error ( "Expected Int16Array" ) ;
@@ -84,7 +83,7 @@ describe("CBOR Typed Array Tagger", function () {
8483
8584 it ( "should convert tagged Int32Array" , function ( ) {
8685 const data = hexToBuffer ( "d84e4c01000000feffffff03000000" ) ;
87- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
86+ const msg = decode ( data ) ;
8887
8988 if ( ! ( msg instanceof Int32Array ) ) {
9089 throw new Error ( "Expected Int32Array" ) ;
@@ -95,24 +94,24 @@ describe("CBOR Typed Array Tagger", function () {
9594 expect ( msg [ 2 ] ) . to . equal ( 3 ) ;
9695 } ) ;
9796
98- it ( "should convert tagged Int64Array " , function ( ) {
97+ it ( "should convert tagged BigInt64Array " , function ( ) {
9998 const data = hexToBuffer (
10099 "d84f58180100000000000000feffffffffffffff0300000000000000" ,
101100 ) ;
102- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
101+ const msg = decode ( data ) ;
103102
104- if ( ! Array . isArray ( msg ) ) {
105- throw new Error ( "Expected Array " ) ;
103+ if ( ! ( msg instanceof BigInt64Array ) ) {
104+ throw new Error ( "Expected BigInt64Array " ) ;
106105 }
107106 expect ( msg ) . to . have . lengthOf ( 3 ) ;
108- expect ( msg [ 0 ] ) . to . equal ( 1 ) ;
109- expect ( msg [ 1 ] ) . to . equal ( - 2 ) ;
110- expect ( msg [ 2 ] ) . to . equal ( 3 ) ;
107+ expect ( msg [ 0 ] ) . to . equal ( BigInt ( 1 ) ) ;
108+ expect ( msg [ 1 ] ) . to . equal ( BigInt ( - 2 ) ) ;
109+ expect ( msg [ 2 ] ) . to . equal ( BigInt ( 3 ) ) ;
111110 } ) ;
112111
113112 it ( "should convert tagged Float32Array" , function ( ) {
114113 const data = hexToBuffer ( "d8554ccdcc8c3fcdcc0cc033335340" ) ;
115- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
114+ const msg = decode ( data ) ;
116115
117116 if ( ! ( msg instanceof Float32Array ) ) {
118117 throw new Error ( "Expected Float32Array" ) ;
@@ -127,7 +126,7 @@ describe("CBOR Typed Array Tagger", function () {
127126 const data = hexToBuffer (
128127 "d85658189a9999999999f13f9a999999999901c06666666666660a40" ,
129128 ) ;
130- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
129+ const msg = decode ( data ) ;
131130
132131 if ( ! ( msg instanceof Float64Array ) ) {
133132 throw new Error ( "Expected Float64Array" ) ;
@@ -140,7 +139,7 @@ describe("CBOR Typed Array Tagger", function () {
140139
141140 it ( "should be able to unpack two typed arrays" , function ( ) {
142141 const data = hexToBuffer ( "82d8484308fe05d84d460100feff0300" ) ;
143- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
142+ const msg = decode ( data ) ;
144143
145144 if ( ! Array . isArray ( msg ) ) {
146145 throw new Error ( "Expected Array" ) ;
0 commit comments