@@ -86,39 +86,41 @@ export function createImportObject(): WebAssembly.Imports {
8686}
8787
8888export interface ResponseLike {
89+ status : number ;
8990 arrayBuffer ( ) : Promise < BufferSource > ;
91+ text ( ) : Promise < string > ;
92+ headers : {
93+ get ( name : string ) : string | null ;
94+ } ;
9095}
9196
9297/**
9398 * Creates a formatter from the specified streaming source.
9499 * @remarks This is the most efficient way to create a formatter.
95100 * @param response - The streaming source to create the formatter from.
96101 */
97- export function createStreaming (
98- response : Promise < ResponseLike > ,
102+ export async function createStreaming (
103+ responsePromise : Promise < ResponseLike > | ResponseLike ,
99104) : Promise < Formatter > {
100- if ( typeof WebAssembly . instantiateStreaming === "function" ) {
105+ const response = await responsePromise ;
106+ if ( response . status !== 200 ) {
107+ throw new Error (
108+ `Unexpected status code: ${ response . status } \n${ await response . text ( ) } ` ,
109+ ) ;
110+ }
111+ if (
112+ typeof WebAssembly . instantiateStreaming === "function"
113+ && response . headers . get ( "content-type" ) === "application/wasm"
114+ ) {
101115 return WebAssembly
102116 // deno-lint-ignore no-explicit-any
103117 . instantiateStreaming ( response as any , createImportObject ( ) )
104118 . then ( ( obj ) => createFromInstance ( obj . instance ) ) ;
105119 } else {
106- // fallback for node.js
107- return getArrayBuffer ( )
120+ // fallback for node.js or when the content type isn't application/wasm
121+ return response . arrayBuffer ( )
108122 . then ( ( buffer ) => createFromBuffer ( buffer ) ) ;
109123 }
110-
111- function getArrayBuffer ( ) {
112- if ( isResponse ( response ) ) {
113- return response . arrayBuffer ( ) ;
114- } else {
115- return response . then ( ( response ) => response . arrayBuffer ( ) ) ;
116- }
117-
118- function isResponse ( response : unknown ) : response is ResponseLike {
119- return ( response as Response ) . arrayBuffer != null ;
120- }
121- }
122124}
123125
124126/**
0 commit comments