@@ -10,6 +10,13 @@ import { fromHex } from '@exodus/bytes/hex.js'
1010import { randomValues } from '@exodus/crypto/randomBytes'
1111import { describe , test } from 'node:test'
1212
13+ const SharedArrayBuffer = globalThis . SharedArrayBuffer ?? ArrayBuffer
14+ const toShared = ( u8 ) => {
15+ const res = new Uint8Array ( new SharedArrayBuffer ( u8 . length ) )
16+ res . set ( u8 )
17+ return res
18+ }
19+
1320// invalid bytes -> string
1421const nonUtf8 = [
1522 { bytes : [ 0 , 254 , 255 ] , charcodes : [ 0 , 0xff_fd , 0xff_fd ] } ,
@@ -75,6 +82,8 @@ describe('utf8toString', () => {
7582 for ( const method of [
7683 utf8toString ,
7784 utf8toStringLoose ,
85+ ( x ) => utf8toString ( toShared ( x ) ) ,
86+ ( x ) => utf8toStringLoose ( toShared ( x ) ) ,
7887 ( x ) => js . decode ( x , false ) ,
7988 ( x ) => js . decode ( x , true ) ,
8089 ] ) {
@@ -87,7 +96,11 @@ describe('utf8toString', () => {
8796 } )
8897
8998 test ( 'non-utf8 bytes throw in utf8toString' , ( t ) => {
90- for ( const method of [ utf8toString , ( x ) => js . decode ( x , false ) ] ) {
99+ for ( const method of [
100+ utf8toString ,
101+ ( x ) => utf8toString ( toShared ( x ) ) ,
102+ ( x ) => js . decode ( x , false ) ,
103+ ] ) {
91104 for ( const { bytes } of nonUtf8 ) {
92105 t . assert . throws ( ( ) => method ( Uint8Array . of ( ...bytes ) ) )
93106
@@ -105,7 +118,11 @@ describe('utf8toString', () => {
105118 } )
106119
107120 test ( 'non-utf8 bytes get replaced in utf8toStringLoose' , ( t ) => {
108- for ( const method of [ utf8toStringLoose , ( x ) => js . decode ( x , true ) ] ) {
121+ for ( const method of [
122+ utf8toStringLoose ,
123+ ( x ) => utf8toStringLoose ( toShared ( x ) ) ,
124+ ( x ) => js . decode ( x , true ) ,
125+ ] ) {
109126 for ( const { bytes, charcodes } of nonUtf8 ) {
110127 const res = method ( Uint8Array . of ( ...bytes ) )
111128 t . assert . strictEqual ( res . length , charcodes . length )
@@ -198,6 +215,7 @@ describe('random data', () => {
198215 const NativeBuffer = globalThis . Buffer && ! globalThis . Buffer . TYPED_ARRAY_SUPPORT ? Buffer : null
199216 for ( const u8 of pool ) {
200217 const str = utf8toStringLoose ( u8 )
218+ t . assert . strictEqual ( str , utf8toStringLoose ( toShared ( u8 ) ) )
201219 t . assert . strictEqual ( str , js . decode ( u8 , true ) )
202220 if ( textDecoder ) t . assert . strictEqual ( str , textDecoder . decode ( u8 ) )
203221 if ( NativeBuffer ) t . assert . strictEqual ( str , NativeBuffer . from ( u8 ) . toString ( ) )
@@ -210,6 +228,8 @@ describe('random data', () => {
210228 for ( const u8 of poolAscii ) {
211229 const str = utf8toString ( u8 )
212230 t . assert . strictEqual ( str , utf8toStringLoose ( u8 ) )
231+ t . assert . strictEqual ( str , utf8toString ( toShared ( u8 ) ) )
232+ t . assert . strictEqual ( str , utf8toStringLoose ( toShared ( u8 ) ) )
213233 t . assert . strictEqual ( str , js . decode ( u8 , false ) )
214234 t . assert . strictEqual ( str , js . decode ( u8 , true ) )
215235 if ( textDecoder ) t . assert . strictEqual ( str , textDecoder . decode ( u8 ) )
@@ -231,11 +251,14 @@ describe('random data', () => {
231251 }
232252
233253 if ( str === undefined ) {
254+ t . assert . throws ( ( ) => utf8toString ( toShared ( u8 ) ) )
234255 t . assert . throws ( ( ) => js . decode ( u8 , false ) )
235256 if ( textDecoder ) t . assert . throws ( ( ) => textDecoder . decode ( u8 ) )
236257 } else {
237258 t . assert . strictEqual ( str , strings [ i ] )
238259 t . assert . strictEqual ( str , utf8toStringLoose ( u8 ) )
260+ t . assert . strictEqual ( str , utf8toString ( toShared ( u8 ) ) )
261+ t . assert . strictEqual ( str , utf8toStringLoose ( toShared ( u8 ) ) )
239262 t . assert . strictEqual ( str , js . decode ( u8 , false ) )
240263 t . assert . strictEqual ( str , js . decode ( u8 , true ) )
241264 if ( textDecoder ) t . assert . strictEqual ( str , textDecoder . decode ( u8 ) )
@@ -282,6 +305,8 @@ describe('random data', () => {
282305 const u8 = restored [ i ]
283306 t . assert . strictEqual ( str , utf8toString ( u8 ) )
284307 t . assert . strictEqual ( str , utf8toStringLoose ( u8 ) )
308+ t . assert . strictEqual ( str , utf8toString ( toShared ( u8 ) ) )
309+ t . assert . strictEqual ( str , utf8toStringLoose ( toShared ( u8 ) ) )
285310 t . assert . strictEqual ( str , js . decode ( u8 , false ) )
286311 t . assert . strictEqual ( str , js . decode ( u8 , true ) )
287312 if ( textDecoder ) t . assert . strictEqual ( str , textDecoder . decode ( u8 ) )
0 commit comments