44 * @see : https://github.com/WebAssembly/wasi-http
55 */
66
7- import { IncomingRequest } from "wasi:http/types@0.2.6" ;
8- // import { IncomingBody, IncomingRequest } from "wasi:http/types@0.2.6";
9- // import { Pollable } from "wasi:io/poll@0.2.6";
10- // import { InputStream } from "wasi:io/streams@0.2.6";
7+ import { IncomingBody , IncomingRequest } from "wasi:http/types@0.2.6" ;
8+ import { Pollable } from "wasi:io/poll@0.2.6" ;
9+ import { InputStream } from "wasi:io/streams@0.2.6" ;
1110
12- // import { ensureGlobalReadableStream, ensureGlobalRequest } from "../../../globals.js";
13- import { wasiHTTPMethodToString } from "../../../0.2.x/http/index.js" ;
14- // import { DEFAULT_INCOMING_BODY_READ_MAX_BYTES } from "../../../constants.js";
11+ import { ensureGlobalReadableStream , ensureGlobalRequest } from "../../../globals.js" ;
12+ import { wasiHTTPMethodToString , requestShouldHaveBody } from "../../../0.2.x/http/index.js" ;
13+ import { DEFAULT_INCOMING_BODY_READ_MAX_BYTES } from "../../../constants.js" ;
1514
1615/**
1716 * Create a web-platform `Request` from a `wasi:http/incoming-handler` `incoming-request`.
@@ -25,8 +24,6 @@ import { wasiHTTPMethodToString } from "../../../0.2.x/http/index.js";
2524 * @see https://github.com/WebAssembly/wasi-http
2625 */
2726export async function readWASIRequest ( wasiIncomingRequest : IncomingRequest ) : Promise < Request > {
28- // TODO: reuse bytes for subsequent web requests, doing pruning/growing where necessary
29- // TODO: trailer support
3027 if ( ! wasiIncomingRequest ) {
3128 throw new TypeError ( 'WASI incoming request not provided' ) ;
3229 }
@@ -53,62 +50,60 @@ export async function readWASIRequest(wasiIncomingRequest: IncomingRequest): Pro
5350 return [ k , decoder . decode ( valueBytes ) ] ;
5451 } )
5552 ) ;
56- // const Request = ensureGlobalRequest();
57- // const ReadableStream = ensureGlobalReadableStream();
53+ const Request = ensureGlobalRequest ( ) ;
54+ const ReadableStream = ensureGlobalReadableStream ( ) ;
5855
59- // let incomingBody: IncomingBody;
60- // let incomingBodyStream: InputStream;
61- // let incomingBodyPollable: Pollable;
56+ let incomingBody : IncomingBody ;
57+ let incomingBodyStream : InputStream ;
58+ let incomingBodyPollable : Pollable ;
6259
63- // const body = new ReadableStream({
64- // async start(controller) {
65- // if (!incomingBody) {
66- // incomingBody = wasiIncomingRequest.consume();
67- // incomingBodyStream = incomingBody.stream();
68- // incomingBodyPollable = incomingBodyStream.subscribe();
69- // }
70- // },
60+ let body : ReadableStream ;
61+ if ( requestShouldHaveBody ( { method } ) ) {
62+ body = new ReadableStream ( {
63+ start ( controller ) {
64+ if ( ! incomingBody ) {
65+ incomingBody = wasiIncomingRequest . consume ( ) ;
66+ incomingBodyStream = incomingBody . stream ( ) ;
67+ incomingBodyPollable = incomingBodyStream . subscribe ( ) ;
68+ }
69+ } ,
7170
72- // async pull(controller) {
73- // // Read all information coming from the request
74- // while (true) {
75- // // Wait until the pollable is ready
76- // if (!incomingBodyPollable.ready()) {
77- // incomingBodyPollable.block();
78- // }
71+ pull ( controller ) {
72+ // Read all information coming from the request
73+ while ( true ) {
74+ // Wait until the pollable is ready
75+ if ( ! incomingBodyPollable . ready ( ) ) {
76+ incomingBodyPollable . block ( ) ;
77+ }
7978
80- // try {
81- // console.error("BEFORE READ!");
82- // const bytes = incomingBodyStream.read(
83- // DEFAULT_INCOMING_BODY_READ_MAX_BYTES
84- // );
85- // if (bytes.length === 0) {
86- // break;
87- // } else {
88- // controller.enqueue(bytes);
89- // }
90- // } catch (err) {
91- // // If the channel is closed, we can break out
92- // if (err.payload.tag === 'closed') { break; }
93- // throw err;
94- // }
95- // }
79+ try {
80+ const bytes = incomingBodyStream . read ( DEFAULT_INCOMING_BODY_READ_MAX_BYTES ) ;
81+ if ( bytes . length === 0 ) {
82+ break ;
83+ }
84+ controller . enqueue ( bytes ) ;
85+ } catch ( err ) {
86+ if ( err . payload . tag === 'closed' ) { break ; }
87+ throw err ;
88+ }
89+ }
9690
97- // incomingBodyPollable[Symbol.dispose]();
98- // incomingBodyStream[Symbol.dispose]();
99- // incomingBody[Symbol.dispose]();
100- // wasiIncomingRequest[Symbol.dispose]();
101- // controller.close();
102- // },
103- // });
91+ // Once information has all been read we can clean up
92+ incomingBodyPollable [ Symbol . dispose ] ( ) ;
93+ incomingBodyStream [ Symbol . dispose ] ( ) ;
94+ IncomingBody . finish ( incomingBody ) ;
95+ wasiIncomingRequest [ Symbol . dispose ] ( ) ;
96+ controller . close ( ) ;
97+ } ,
98+ } ) ;
99+
100+ }
104101
105- // TODO: unfortunately, Request`s don't seem to work properly with StarlingMonkey.
106- // we may have to do some sort of polyfill.
107102 const url = `${ scheme } ://${ authority } ${ pathWithQuery } ` ;
108103 const req = new Request ( url , {
109104 method,
110105 headers,
111- // body, <--- using any kind of body will break
106+ body,
112107 } ) ;
113108
114109 return req ;
0 commit comments