Skip to content

Commit 91cc099

Browse files
fix(std): request creation code for hono integration
1 parent 7481af1 commit 91cc099

File tree

4 files changed

+64
-57
lines changed

4 files changed

+64
-57
lines changed

packages/jco-std/src/wasi/0.2.6/http/types/request.ts

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
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
*/
2726
export 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;

packages/jco-std/src/wasi/0.2.x/http/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,15 @@ export function wasiHTTPMethodToString(wasiMethod: WASIHTTPMethodLike): HTTPMeth
4848
);
4949
}
5050
}
51+
52+
/** Arguments to `requestShouldHaveBody()` */
53+
interface ReqeustShouldHaveBodyArgs {
54+
method: string,
55+
}
56+
57+
/** Helper for determining whether a request should have a body */
58+
export function requestShouldHaveBody(args: ReqeustShouldHaveBodyArgs): boolean {
59+
const { method } = args;
60+
if (method === "GET" || method === "HEAD") { return false; }
61+
return true;
62+
}

packages/jco-std/test/fixtures/apps/wasi-http-hono/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { fire } from '@bytecodealliance/jco-std/wasi/0.2.6/http/adapters/hono';
1212

1313
const app = new Hono();
1414
app.get("/", (c) => {
15-
return c.text("Hello World!");
15+
return c.text("Hello World!!!!");
1616
});
1717

1818
fire(app);

packages/jco-std/test/fixtures/apps/wasi-http-hono/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export const config = {
88

99
export async function test({ server }) {
1010
const req = await fetch(server.url);
11-
assert.strictEqual('Hello World!', await req.text());
11+
assert.strictEqual("Hello World!!!!", await req.text());
1212
}

0 commit comments

Comments
 (0)