1- import { Buffer } from "node:buffer"
2-
31import { describe , expect , it } from "vitest"
42
53// eslint-disable-next-line antfu/no-import-dist
64import { Decoder } from "../dist/decoder.mjs"
75
86describe ( ".currentOffset" , ( ) => {
97 it ( "example works as described" , ( ) => {
10- const buffer = Buffer . from ( [ 0x01 , 0x01 , 0x00 , 0x00 ] )
8+ const buffer = new Uint8Array ( [ 0x01 , 0x01 , 0x00 , 0x00 ] )
119 const decoder = new Decoder ( buffer )
1210 expect ( decoder . readUint32 ( ) ) . toBe ( 257 )
1311 expect ( decoder . currentOffset ) . toBe ( 4 )
@@ -16,7 +14,7 @@ describe(".currentOffset", () => {
1614
1715describe ( ".endianness" , ( ) => {
1816 it ( "example works as described" , ( ) => {
19- const buffer = Buffer . from ( [ 0x00 , 0x00 , 0x01 , 0x01 ] )
17+ const buffer = new Uint8Array ( [ 0x00 , 0x00 , 0x01 , 0x01 ] )
2018 const decoder = new Decoder ( buffer )
2119 decoder . endianness ( "big" )
2220 expect ( decoder . readUint32 ( ) ) . toBe ( 257 )
@@ -25,7 +23,7 @@ describe(".endianness", () => {
2523
2624describe ( ".seek" , ( ) => {
2725 it ( "example works as described" , ( ) => {
28- const buffer = Buffer . from ( [ 0xff , 0xff , 0x01 , 0x00 ] )
26+ const buffer = new Uint8Array ( [ 0xff , 0xff , 0x01 , 0x00 ] )
2927 const decoder = new Decoder ( buffer )
3028 expect ( decoder . seek ( 2 ) ) . toBe ( 0 )
3129 expect ( decoder . readUint16 ( ) ) . toBe ( 1 )
@@ -34,7 +32,7 @@ describe(".seek", () => {
3432
3533describe ( ".goto" , ( ) => {
3634 it ( "example works as described" , ( ) => {
37- const buffer = Buffer . from ( [ 0xff , 0xff , 0x01 , 0x00 ] )
35+ const buffer = new Uint8Array ( [ 0xff , 0xff , 0x01 , 0x00 ] )
3836 const decoder = new Decoder ( buffer )
3937 expect ( decoder . readUint16 ( ) ) . toBe ( 65535 )
4038 expect ( decoder . goto ( 0 ) ) . toBe ( 2 )
@@ -45,14 +43,14 @@ describe(".goto", () => {
4543
4644describe ( ".alignTo" , ( ) => {
4745 it ( "skips forwards correctly if diff if positive" , ( ) => {
48- const decoder = new Decoder ( Buffer . alloc ( 10 ) )
46+ const decoder = new Decoder ( new Uint8Array ( 10 ) )
4947 decoder . seek ( 12 )
5048 decoder . alignTo ( 16 )
5149 expect ( decoder . currentOffset ) . toBe ( 16 )
5250 } )
5351
5452 it ( "skips forwards correctly if diff if negative" , ( ) => {
55- const decoder = new Decoder ( Buffer . alloc ( 10 ) )
53+ const decoder = new Decoder ( new Uint8Array ( 10 ) )
5654 decoder . seek ( 4 )
5755 decoder . alignTo ( 16 )
5856 expect ( decoder . currentOffset ) . toBe ( 16 )
@@ -61,7 +59,7 @@ describe(".alignTo", () => {
6159 it ( "example works as described" , ( ) => {
6260 // prettier-ignore
6361 // Assume we have a header 5 bytes long, then 3 bytes of padding up to 8 bytes in, then more data
64- const buffer = Buffer . from ( [
62+ const buffer = new Uint8Array ( [
6563 0x74 , 0xee , 0xb2 , 0x36 , 0x0c , 0x00 , 0x00 , 0x00 ,
6664 0x01 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
6765 ] )
@@ -78,13 +76,13 @@ describe(".alignTo", () => {
7876describe ( "integers" , ( ) => {
7977 describe ( ".readInt8" , ( ) => {
8078 it ( "example works as described" , ( ) => {
81- const decoder = new Decoder ( Buffer . from ( [ 0xff ] ) )
79+ const decoder = new Decoder ( new Uint8Array ( [ 0xff ] ) )
8280 expect ( decoder . readInt8 ( ) ) . toBe ( - 1 )
8381 } )
8482
8583 it ( "pointer example works as described" , ( ) => {
86- // (3 + 0) | pad | -1
87- const decoder = new Decoder ( Buffer . from ( [ 0x03 , 0x00 , 0x00 , 0xff ] ) )
84+ // (3 + 0) | pad | -1
85+ const decoder = new Decoder ( new Uint8Array ( [ 0x03 , 0x00 , 0x00 , 0xff ] ) )
8886 const pointer = decoder . readUint16 ( )
8987 expect ( pointer ) . toBe ( 3 )
9088 expect ( decoder . readInt8 ( { pointer } ) ) . toBe ( - 1 )
@@ -93,13 +91,13 @@ describe("integers", () => {
9391
9492 describe ( ".readUint8" , ( ) => {
9593 it ( "example works as described" , ( ) => {
96- const decoder = new Decoder ( Buffer . from ( [ 0xff ] ) )
94+ const decoder = new Decoder ( new Uint8Array ( [ 0xff ] ) )
9795 expect ( decoder . readUint8 ( ) ) . toBe ( 255 )
9896 } )
9997
10098 it ( "pointer example works as described" , ( ) => {
101- // (3 + 0) | pad | -1
102- const decoder = new Decoder ( Buffer . from ( [ 0x03 , 0x00 , 0x00 , 0xff ] ) )
99+ // (3 + 0) | pad | -1
100+ const decoder = new Decoder ( new Uint8Array ( [ 0x03 , 0x00 , 0x00 , 0xff ] ) )
103101 const pointer = decoder . readUint16 ( )
104102 expect ( pointer ) . toBe ( 3 )
105103 expect ( decoder . readUint8 ( { pointer } ) ) . toBe ( 255 )
@@ -108,12 +106,12 @@ describe("integers", () => {
108106
109107 describe ( ".readInt16" , ( ) => {
110108 it ( "example works as described" , ( ) => {
111- const decoder = new Decoder ( Buffer . from ( [ 0xff , 0xff ] ) )
109+ const decoder = new Decoder ( new Uint8Array ( [ 0xff , 0xff ] ) )
112110 expect ( decoder . readInt16 ( ) ) . toBe ( - 1 )
113111 } )
114112
115113 it ( "pointer example works as described" , ( ) => {
116- const decoder = new Decoder ( Buffer . from ( [ 0x03 , 0x00 , 0x00 , 0xff , 0xff ] ) )
114+ const decoder = new Decoder ( new Uint8Array ( [ 0x03 , 0x00 , 0x00 , 0xff , 0xff ] ) )
117115 const pointer = decoder . readUint16 ( )
118116 expect ( pointer ) . toBe ( 3 )
119117 expect ( decoder . readInt16 ( { pointer } ) ) . toBe ( - 1 )
@@ -122,13 +120,13 @@ describe("integers", () => {
122120
123121 describe ( ".readUint16" , ( ) => {
124122 it ( "decodes a value correctly" , ( ) => {
125- const decoder = new Decoder ( Buffer . from ( [ 0xff , 0xff ] ) )
123+ const decoder = new Decoder ( new Uint8Array ( [ 0xff , 0xff ] ) )
126124 expect ( decoder . readUint16 ( ) ) . toBe ( 65535 )
127125 } )
128126
129127 it ( "decodes a value via a pointer correctly" , ( ) => {
130- // (3 + 0) | pad | -1
131- const decoder = new Decoder ( Buffer . from ( [ 0x03 , 0x00 , 0x00 , 0xff , 0xff ] ) )
128+ // (3 + 0) | pad | -1
129+ const decoder = new Decoder ( new Uint8Array ( [ 0x03 , 0x00 , 0x00 , 0xff , 0xff ] ) )
132130 const pointer = decoder . readUint16 ( )
133131 expect ( pointer ) . toBe ( 3 )
134132 expect ( decoder . readUint16 ( { pointer } ) ) . toBe ( 65535 )
@@ -138,7 +136,7 @@ describe("integers", () => {
138136
139137describe ( ".readString" , ( ) => {
140138 it ( "should parse strings correctly" , ( ) => {
141- const data = Buffer . from ( "testhelloworld" )
139+ const data = new TextEncoder ( ) . encode ( "testhelloworld" )
142140
143141 const decoder = new Decoder ( data )
144142 expect ( decoder . readString ( { length : 4 } ) ) . toBe ( "test" )
@@ -147,7 +145,7 @@ describe(".readString", () => {
147145 } )
148146
149147 it ( "should parse zeroed strings correctly" , ( ) => {
150- const data = Buffer . from ( "test\x00hello\x00world\x00" )
148+ const data = new TextEncoder ( ) . encode ( "test\x00hello\x00world\x00" )
151149
152150 const decoder = new Decoder ( data )
153151 expect ( decoder . readString ( { zeroed : true } ) ) . toBe ( "test" )
@@ -157,7 +155,7 @@ describe(".readString", () => {
157155} )
158156
159157it ( "should parse the README example" , ( ) => {
160- const data = Buffer . from ( [
158+ const data = new Uint8Array ( [
161159 0x48 , 0x65 , 0x6c , 0x6c , 0x6f , 0x2c , 0x20 , 0x57 , 0x6f , 0x72 , 0x6c , 0x64 , 0x21 ,
162160 ] )
163161 const decoder = new Decoder ( data )
@@ -167,7 +165,9 @@ it("should parse the README example", () => {
167165 expect ( decoder . readString ( { length : 7 } ) ) . toBe ( ", World" )
168166
169167 // Or you can use it more ergonomically when possible
170- const decoder2 = new Decoder ( Buffer . alloc ( 2 , 2 ) )
168+ const filledArray = new Uint8Array ( 2 )
169+ filledArray . fill ( 2 )
170+ const decoder2 = new Decoder ( filledArray )
171171 const result = {
172172 a : decoder2 . readUint8 ( ) ,
173173 b : decoder2 . readUint8 ( ) ,
@@ -177,7 +177,7 @@ it("should parse the README example", () => {
177177} )
178178
179179it ( "should allow parsing simple data" , ( ) => {
180- const testBuffer = Buffer . alloc ( 17 )
180+ const testBuffer = new Uint8Array ( 17 )
181181 const values = [ 3257 , 3263 , 6483 , 9773 ]
182182 const view = new DataView ( testBuffer . buffer )
183183 view . setInt8 ( 0 , 4 )
0 commit comments