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,15 +10,16 @@ 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 ) ) {
22+ console . error ( msg ) ;
2323 throw new Error ( "Expected Uint16Array" ) ;
2424 }
2525 expect ( msg ) . to . have . lengthOf ( 3 ) ;
@@ -30,7 +30,7 @@ describe("CBOR Typed Array Tagger", function () {
3030
3131 it ( "should convert tagged Uint32Array" , function ( ) {
3232 const data = hexToBuffer ( "d8464c010000000200000003000000" ) ;
33- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
33+ const msg = decode ( data ) ;
3434
3535 if ( ! ( msg instanceof Uint32Array ) ) {
3636 throw new Error ( "Expected Uint32Array" ) ;
@@ -41,24 +41,24 @@ describe("CBOR Typed Array Tagger", function () {
4141 expect ( msg [ 2 ] ) . to . equal ( 3 ) ;
4242 } ) ;
4343
44- it ( "should convert tagged Uint64Array " , function ( ) {
44+ it ( "should convert tagged BigUint64Array " , function ( ) {
4545 const data = hexToBuffer (
4646 "d8475818010000000000000002000000000000000300000000000000" ,
4747 ) ;
48- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
48+ const msg = decode ( data ) ;
4949
50- if ( ! Array . isArray ( msg ) ) {
51- throw new Error ( "Expected Array " ) ;
50+ if ( ! ( msg instanceof BigUint64Array ) ) {
51+ throw new Error ( "Expected BigUint64Array " ) ;
5252 }
5353 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 ) ;
54+ expect ( msg [ 0 ] ) . to . equal ( BigInt ( 1 ) ) ;
55+ expect ( msg [ 1 ] ) . to . equal ( BigInt ( 2 ) ) ;
56+ expect ( msg [ 2 ] ) . to . equal ( BigInt ( 3 ) ) ;
5757 } ) ;
5858
5959 it ( "should convert tagged Int8Array" , function ( ) {
6060 const data = hexToBuffer ( "d8484301fe03" ) ;
61- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
61+ const msg = decode ( data ) ;
6262
6363 if ( ! ( msg instanceof Int8Array ) ) {
6464 throw new Error ( "Expected Int8Array" ) ;
@@ -71,7 +71,7 @@ describe("CBOR Typed Array Tagger", function () {
7171
7272 it ( "should convert tagged Int16Array" , function ( ) {
7373 const data = hexToBuffer ( "d84d460100feff0300" ) ;
74- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
74+ const msg = decode ( data ) ;
7575
7676 if ( ! ( msg instanceof Int16Array ) ) {
7777 throw new Error ( "Expected Int16Array" ) ;
@@ -84,7 +84,7 @@ describe("CBOR Typed Array Tagger", function () {
8484
8585 it ( "should convert tagged Int32Array" , function ( ) {
8686 const data = hexToBuffer ( "d84e4c01000000feffffff03000000" ) ;
87- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
87+ const msg = decode ( data ) ;
8888
8989 if ( ! ( msg instanceof Int32Array ) ) {
9090 throw new Error ( "Expected Int32Array" ) ;
@@ -95,24 +95,24 @@ describe("CBOR Typed Array Tagger", function () {
9595 expect ( msg [ 2 ] ) . to . equal ( 3 ) ;
9696 } ) ;
9797
98- it ( "should convert tagged Int64Array " , function ( ) {
98+ it ( "should convert tagged BigInt64Array " , function ( ) {
9999 const data = hexToBuffer (
100100 "d84f58180100000000000000feffffffffffffff0300000000000000" ,
101101 ) ;
102- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
102+ const msg = decode ( data ) ;
103103
104- if ( ! Array . isArray ( msg ) ) {
105- throw new Error ( "Expected Array " ) ;
104+ if ( ! ( msg instanceof BigInt64Array ) ) {
105+ throw new Error ( "Expected BigInt64Array " ) ;
106106 }
107107 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 ) ;
108+ expect ( msg [ 0 ] ) . to . equal ( BigInt ( 1 ) ) ;
109+ expect ( msg [ 1 ] ) . to . equal ( BigInt ( - 2 ) ) ;
110+ expect ( msg [ 2 ] ) . to . equal ( BigInt ( 3 ) ) ;
111111 } ) ;
112112
113113 it ( "should convert tagged Float32Array" , function ( ) {
114114 const data = hexToBuffer ( "d8554ccdcc8c3fcdcc0cc033335340" ) ;
115- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
115+ const msg = decode ( data ) ;
116116
117117 if ( ! ( msg instanceof Float32Array ) ) {
118118 throw new Error ( "Expected Float32Array" ) ;
@@ -127,7 +127,7 @@ describe("CBOR Typed Array Tagger", function () {
127127 const data = hexToBuffer (
128128 "d85658189a9999999999f13f9a999999999901c06666666666660a40" ,
129129 ) ;
130- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
130+ const msg = decode ( data ) ;
131131
132132 if ( ! ( msg instanceof Float64Array ) ) {
133133 throw new Error ( "Expected Float64Array" ) ;
@@ -140,7 +140,7 @@ describe("CBOR Typed Array Tagger", function () {
140140
141141 it ( "should be able to unpack two typed arrays" , function ( ) {
142142 const data = hexToBuffer ( "82d8484308fe05d84d460100feff0300" ) ;
143- const msg = CBOR . decode ( data , cborTypedArrayTagger ) ;
143+ const msg = decode ( data ) ;
144144
145145 if ( ! Array . isArray ( msg ) ) {
146146 throw new Error ( "Expected Array" ) ;
0 commit comments