File tree Expand file tree Collapse file tree 4 files changed +69
-0
lines changed
Expand file tree Collapse file tree 4 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ import {
2+ IncomingRequest ,
3+ ResponseOutparam ,
4+ OutgoingBody ,
5+ OutgoingResponse ,
6+ Fields ,
7+ } from 'wasi:http/types@0.2.3' ;
8+
9+ export const incomingHandler = {
10+ handle ( incomingRequest , responseOutparam ) {
11+ const outgoingResponse = new OutgoingResponse ( new Fields ( ) ) ;
12+ let outgoingBody = outgoingResponse . body ( ) ;
13+ {
14+ let outputStream = outgoingBody . write ( ) ;
15+ outputStream . blockingWriteAndFlush (
16+ new Uint8Array ( new TextEncoder ( ) . encode ( 'Hello world!' ) ) ,
17+ ) ;
18+ outputStream [ Symbol . dispose ] ( ) ;
19+ }
20+ outgoingResponse . setStatusCode ( 200 ) ;
21+ OutgoingBody . finish ( outgoingBody , undefined ) ;
22+ ResponseOutparam . set ( outgoingResponse , {
23+ tag : 'ok' ,
24+ val : outgoingResponse ,
25+ } ) ;
26+ } ,
27+ } ;
Original file line number Diff line number Diff line change 1+ import { strictEqual } from 'node:assert' ;
2+
3+ import { HTTPServer } from '@bytecodealliance/preview2-shim/http' ;
4+
5+ import { getRandomPort } from '../../util.js' ;
6+
7+ export const enableFeatures = [ 'http' ] ;
8+ export const worldName = 'test3' ;
9+
10+ export async function test ( instance ) {
11+ const server = new HTTPServer ( instance . incomingHandler ) ;
12+ let port = await getRandomPort ( ) ;
13+ server . listen ( port ) ;
14+
15+ try {
16+ const resp = await fetch ( `http://localhost:${ port } ` ) ;
17+ const text = await resp . text ( ) ;
18+ strictEqual ( text , 'Hello world!' ) ;
19+ } finally {
20+ server . stop ( ) ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ import { createServer } from 'node:net' ;
2+
3+ // Utility function for getting a random port
4+ export async function getRandomPort ( ) {
5+ return await new Promise ( ( resolve ) => {
6+ const server = createServer ( ) ;
7+ server . listen ( 0 , function ( ) {
8+ const port = this . address ( ) . port ;
9+ server . on ( 'close' , ( ) => resolve ( port ) ) ;
10+ server . close ( ) ;
11+ } ) ;
12+ } ) ;
13+ }
Original file line number Diff line number Diff line change @@ -64,3 +64,10 @@ world test2 {
6464
6565 export get-result : func () -> string ;
6666}
67+
68+ world test3 {
69+ import wasi :cli /environment @ 0.2.3 ;
70+ import wasi :http /types @ 0.2.3 ;
71+
72+ export wasi :http /incoming-handler @ 0.2.3 ;
73+ }
You can’t perform that action at this time.
0 commit comments