MCPcopy Create free account
hub / github.com/bytecodealliance/ComponentizeJS / getResult

Function getResult

test/cases/http-request/source.js:7–49  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

5const decoder = new TextDecoder();
6
7export function getResult() {
8 let incomingResponse;
9
10 const req = new OutgoingRequest(
11 new Fields([
12 ['User-agent', encoder.encode('WASI-HTTP/0.0.1')],
13 ['Content-type', encoder.encode('application/json')],
14 ]),
15 );
16
17 req.setScheme({ tag: 'HTTPS' });
18 req.setMethod({ tag: 'get' });
19 req.setPathWithQuery('/');
20 req.setAuthority('webassembly.org');
21
22 const futureIncomingResponse = handle(req);
23 futureIncomingResponse.subscribe().block();
24 incomingResponse = futureIncomingResponse.get().val.val;
25
26 const status = incomingResponse.status();
27 const responseHeaders = incomingResponse.headers().entries();
28
29 const headers = Object.fromEntries(
30 responseHeaders.map(([k, v]) => [k, decoder.decode(v)]),
31 );
32
33 let responseBody;
34
35 const incomingBody = incomingResponse.consume();
36 {
37 const bodyStream = incomingBody.stream();
38 // const bodyStreamPollable = bodyStream.subscribe();
39 const buf = bodyStream.blockingRead(500n);
40 // TODO: actual streaming
41 responseBody = buf.length > 0 ? decoder.decode(buf) : undefined;
42 bodyStream[Symbol.dispose]();
43 }
44
45 return `
46STATUS: ${status}
47HEADERS: ${JSON.stringify(headers)}
48BODY: ${responseBody}`;
49}

Callers

nothing calls this directly

Calls 2

handleFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected