@@ -2,23 +2,43 @@ import { shown } from './options.ts';
22/** Data accepted wherever binary input is expected */
33export type BinaryData = ArrayBuffer | ArrayBufferView ;
44
5+ // eslint-disable-next-line @typescript-eslint/unbound-method -- called on the value, as a check
6+ const byteLengthOf = Object . getOwnPropertyDescriptor ( ArrayBuffer . prototype , 'byteLength' ) ! . get ! ;
7+ // eslint-disable-next-line @typescript-eslint/unbound-method -- called on the value, as a check
8+ const sharedByteLengthOf = typeof SharedArrayBuffer === 'undefined' ? null : Object . getOwnPropertyDescriptor ( SharedArrayBuffer . prototype , 'byteLength' ) ! . get ! ;
9+
10+ /** Whether a value is a buffer (an ArrayBuffer or a SharedArrayBuffer), also one of another realm */
11+ function isBuffer ( value : unknown ) : value is ArrayBuffer {
12+ // (by the byteLength getter's own check, not instanceof: a buffer of another realm, an iframe's or a vm context's,
13+ // isn't an instance here; nor by the tag, which any object can claim)
14+ if ( typeof value !== 'object' || value === null ) return false ;
15+ for ( const getter of [ byteLengthOf , sharedByteLengthOf ] ) {
16+ try {
17+ getter ?. call ( value ) ;
18+ if ( getter ) return true ;
19+ } catch { /* another kind */ }
20+ }
21+ return false ;
22+ }
23+
524/** Whether a value is binary data (an ArrayBuffer, a SharedArrayBuffer or a view), also one of another realm */
625export function isBinaryData ( value : unknown ) : value is BinaryData {
7- if ( ArrayBuffer . isView ( value ) ) return true ;
8- const tag = Object . prototype . toString . call ( value ) ;
9- return tag === '[object ArrayBuffer]' || tag === '[object SharedArrayBuffer]' ;
26+ return ArrayBuffer . isView ( value ) || isBuffer ( value ) ;
1027}
1128
1229/**
1330 * Returns a Uint8Array view of the given buffer without copying
1431 */
1532export function view ( buffer : BinaryData ) : Uint8Array {
33+ // (a buffer transferred away, to a worker for one, has no data: said so instead of the engine's construct error, or
34+ // the "unsupported type" of 0 bytes)
35+ const data = ArrayBuffer . isView ( buffer ) ? buffer . buffer : buffer ;
36+ if ( isBuffer ( data ) && ( data as { detached ?: boolean } ) . detached === true )
37+ throw new TypeError ( 'Binary data has to be readable, not of a detached ArrayBuffer (one transferred, to a worker for one)' ) ;
1638 // subclasses like node's Buffer are unwrapped, their slice() doesn't copy
1739 if ( buffer instanceof Uint8Array && buffer . constructor === Uint8Array ) return buffer ;
1840 if ( ArrayBuffer . isView ( buffer ) ) return new Uint8Array ( buffer . buffer , buffer . byteOffset , buffer . byteLength ) ;
19- // (by its tag, not instanceof: a buffer of another realm, an iframe's or a vm context's, isn't an instance here)
20- const tag = Object . prototype . toString . call ( buffer ) ;
21- if ( tag === '[object ArrayBuffer]' || tag === '[object SharedArrayBuffer]' ) return new Uint8Array ( buffer ) ;
41+ if ( isBuffer ( buffer ) ) return new Uint8Array ( buffer ) ;
2242
2343 throw new TypeError ( `Binary data has to be an ArrayBuffer or a typed array like Uint8Array, not ${ shown ( buffer ) } ` ) ;
2444}
0 commit comments