-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
144 lines (129 loc) · 4.22 KB
/
server.js
File metadata and controls
144 lines (129 loc) · 4.22 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
ResponseOutparam,
OutgoingBody,
OutgoingResponse,
Fields,
} from "wasi:http/types@0.2.3";
import { add } from "example:domain/adder@0.3.0";
import { chat } from "example:domain/chat@0.3.0";
import { Router, json, error, withParams } from "itty-router";
const router = Router({
before: [withParams], // upstream middleware
catch: error, // error handling
finally: [json], // downstream response formatting
});
router
.get("/hello/:name", ({ name = "World" }) => {
// simple hello world endpoint
return `Hello ${name}!`;
})
.get("/add", (request) => {
let { a, b } = request.query;
return {
// send the request to the add function, which is the Golang backend component.
result: add(Number(a), Number(b)),
};
})
.get("/chat", (request) => {
let prompt = request.query["prompt"];
let model = request.query["model"];
let chatRequest = {
model: model ?? "gpt-4o-mini",
messages: [
{
role: "user",
content: prompt,
},
],
};
// send the request to the chat function, which is the Golang backend component.
let result = chat(chatRequest);
return {
result: result,
};
});
/**
* This export represents the `wasi:http/incoming-handler` interface,
* which describes implementing a HTTP handler in WebAssembly using WASI types.
*
* This translates the WASI incoming request into a format that the `itty-router` can understand,
* then calls the router to handle the request, and finally translates the response back into
* WASI types.
*/
export const incomingHandler = {
async handle(incomingRequest, responseOutparam) {
let requestLike = {
method: incomingRequest.method().tag.toUpperCase(),
url: `http://${incomingRequest.authority()}${incomingRequest.pathWithQuery()}`,
headers: [],
};
for (const [key, value] of incomingRequest.headers().entries()) {
requestLike.headers.push([key, new TextDecoder().decode(value)]);
}
let res = await router.fetch(requestLike);
// Set the status code for the response
let outgoingResponse = new OutgoingResponse(new Fields());
outgoingResponse.setStatusCode(res.status);
// Set the headers for the response
for (const [key, value] of Object.entries(res.headers)) {
outgoingResponse
.headers()
.set(key, new Uint8Array(new TextEncoder().encode(value)));
}
// Finish the response body
let outgoingBody = outgoingResponse.body();
{
// Create a stream for the response body
let outputStream = outgoingBody.write();
ensureIterable(res);
for await (const chunk of res.body) {
outputStream.blockingWriteAndFlush(chunk);
}
outputStream[Symbol.dispose]();
}
OutgoingBody.finish(outgoingBody, undefined);
// Set the created response to an "OK" Result<T> value
ResponseOutparam.set(outgoingResponse, {
tag: "ok",
val: outgoingResponse,
});
},
};
function ensureIterable(res) {
if (!res.body[Symbol.asyncIterator]) {
res.body[Symbol.asyncIterator] = () => {
const reader = res.body.getReader();
return {
next: () => reader.read(),
};
};
}
}
// handle(incomingRequest, responseOutparam) {
// // Start building an outgoing response
// const outgoingResponse = new OutgoingResponse(new Fields());
// // Access the outgoing response body
// let outgoingBody = outgoingResponse.body();
// {
// // Create a stream for the response body
// let outputStream = outgoingBody.write();
// // Write hello world to the response stream
// let result = add(1 + 2);
// outputStream.blockingWriteAndFlush(
// new Uint8Array(
// new TextEncoder().encode(`Hello from Javascript ${result}!\n`),
// ),
// );
// // @ts-ignore: This is required in order to dispose the stream before we return
// outputStream[Symbol.dispose]();
// }
// // Set the status code for the response
// outgoingResponse.setStatusCode(200);
// // Finish the response body
// OutgoingBody.finish(outgoingBody, undefined);
// // Set the created response to an "OK" Result<T> value
// ResponseOutparam.set(outgoingResponse, {
// tag: "ok",
// val: outgoingResponse,
// });
// },