forked from squillace/hello-wasi-http-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
123 lines (97 loc) · 3.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam
import { Fields, OutgoingBody, OutgoingResponse, ResponseOutparam, WasiHttpTypes } from "@bytecodealliance/preview2-shim/types/interfaces/wasi-http-types";
/* RUST
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) {
let hdrs = Fields::new();
let resp = OutgoingResponse::new(hdrs);
let body = resp.body().expect("outgoing response");
ResponseOutparam::set(outparam, Ok(resp));
let out = body.write().expect("outgoing stream");
out.blocking_write_and_flush(b"Hello, wasi:http/proxy world!\n")
.expect("writing response");
drop(out);
OutgoingBody::finish(body, None).unwrap();
*/
/* JS client, not server
import { handle } from 'wasi:http/[email protected]';
import {
Fields,
OutgoingRequest,
OutgoingBody,
} from 'wasi:http/[email protected]';
const sendRequest = (
method,
scheme,
authority,
pathWithQuery,
body,
) => {
try {
let incomingResponse;
{
let encoder = new TextEncoder();
const req = new OutgoingRequest(
new Fields([
['User-agent', encoder.encode('WASI-HTTP/0.0.1')],
['Content-type', encoder.encode('application/json')],
])
);
req.setScheme(scheme);
req.setMethod(method);
req.setPathWithQuery(pathWithQuery);
req.setAuthority(authority);
if (body) {
const outgoingBody = req.body();
{
const bodyStream = outgoingBody.write();
bodyStream.blockingWriteAndFlush(encoder.encode(body));
}
// TODO: we should explicitly drop the bodyStream here
// when we have support for Symbol.dispose
OutgoingBody.finish(outgoingBody);
}
const futureIncomingResponse = handle(req);
futureIncomingResponse.subscribe().block();
incomingResponse = futureIncomingResponse.get().val.val;
}
const status = incomingResponse.status();
const responseHeaders = incomingResponse.headers().entries();
const decoder = new TextDecoder();
const headers = responseHeaders.map(([k, v]) => [k, decoder.decode(v)]);
let responseBody;
const incomingBody = incomingResponse.consume();
{
const bodyStream = incomingBody.stream();
bodyStream.subscribe().block();
const buf = bodyStream.read(50n);
responseBody = buf.length > 0 ? new TextDecoder().decode(buf) : undefined;
}
return JSON.stringify({
status,
headers,
body: responseBody,
});
} catch (err) {
throw new Error(err);
}
}
*/
export const incomingHandler = {
handle(_request, _response) {
let encoder = new TextEncoder();
const resp = new OutgoingResponse(
new Fields()
);
console.log("Received the request....");
const hiThere = "Hello from JavaScript thanks to @bytecodealliance/jco!";
const body = resp.body().write();
ResponseOutparam.set(_response, new WasiHttpTypes.httpErrorCode("ok"));
body.blockingWriteAndFlush(encoder.encode(hiThere));
// TODO: we should explicitly drop the bodyStream here
// when we have support for Symbol.dispose
console.info("Almost done....");
// RUST: ResponseOutparam::set(outparam, Ok(resp));
OutgoingBody.finish(body);
console.info("About to return....");
},
};