-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
416 lines (362 loc) · 14.2 KB
/
server.js
File metadata and controls
416 lines (362 loc) · 14.2 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import { createServer } from 'node:http';
import { createSecureServer } from 'node:http2';
import { existsSync } from 'node:fs';
import { HTTPRequest } from './HTTPRequest.js';
import { RequestCookieMap } from './RequestCookieMap.js';
import { HTTPError } from './HTTPError.js';
import { getFileURL, resolveStaticPath, respondWithFile, resolveModulePath } from './utils.js';
const _noop = () => undefined;
const _hasKeys = ({ key, cert }) => key instanceof Blob && cert instanceof Blob && key.type === 'application/pkcs8' && cert.type === 'application/x-pem-file';
const _isImmutableResponse = resp => resp.status > 299 && resp.status < 309 && resp.headers.has('Location');
async function _importMiddleware(src, index, list) {
if (typeof src !== 'function') {
const handler = await import(resolveModulePath(src));
if (handler.default instanceof Function) {
list[index] = handler.default;
} else {
throw new TypeError(`No default export for ${src}.`);
}
}
}
const _createServer = async (callback, {
key,
cert,
allowHTTP1 = true,
ALPNProtocols = ['h2', 'http/1.1'],
} = {}) => _hasKeys({ key, cert })
? createSecureServer({
key: await key.bytes(),
cert: await cert.bytes(),
allowHTTP1,
ALPNProtocols,
}, callback)
: createServer(callback);
const _isTransformStream = (result) =>
result instanceof TransformStream
|| result instanceof CompressionStream
|| (typeof result === 'object' && result.readable instanceof ReadableStream && result.writable instanceof WritableStream);
async function _open(url) {
if (typeof url === 'string') {
return await _open(URL.parse(url));
} else if (! (url instanceof URL)) {
throw new TypeError('URL must be a string or URL.');
} else if (url.protocol === 'http:' || url.protocol === 'https:') {
const { exec } = await import('node:child_process');
const { promise, resolve, reject } = Promise.withResolvers();
switch(process.platform) {
case 'darwin':
exec(`open "${url}"`, reject, resolve);
break;
case 'win32':
exec(`start "${url}"`, reject, resolve);
break;
default:
exec(`xdg-open "${url}"`, reject, resolve);
}
return await promise;
} else {
throw new TypeError(`Invalid URL: ${url.href}`);
}
}
/**
*
* @param {Response} resp
* @param {import('node:http').ServerResponse} respMessage
* @param {object} details
* @param {Function[]} [details.responsePostprocessors]
* @param {object} [details.context]
* @param {Request} [details.request]
*/
async function _send(resp, respMessage, { responsePostprocessors = [], context = {}, request } = {}) {
if (context.signal.aborted) {
if (! respMessage.headersSent) {
respMessage.setHeader('Content-Type', 'application/json');
respMessage.writeHead(408);
}
if (respMessage.writable) {
const cause = context.signal.reason instanceof Error ? context.signal.reason : new Error(context.signal.reason);
respMessage.write(JSON.stringify(new HTTPError(cause.message, { status: 408, cause })));
}
respMessage.end();
} else if (resp instanceof Response) {
const signal = context.signal;
// Skip handling responses from `Response.redirect()`, which are immutable
const body = _isImmutableResponse(resp)
? resp.body
: await Promise.allSettled(responsePostprocessors
.map(postProcessor => Promise.try(() => postProcessor(resp, { request, context })))
).then(results => resp.body instanceof ReadableStream
? results
.filter(result => _isTransformStream(result.value))
.map(result => result.value)
.reduce((stream, pipe) => stream.pipeThrough(pipe, { signal }), resp.body)
: null
);
if (! respMessage.headersSent) {
resp.headers.forEach((value, key) => {
if (key !== 'set-cookie') {
respMessage.setHeader(key, value);
}
});
resp.headers.getSetCookie().forEach(cookie => respMessage.appendHeader('Set-Cookie', cookie));
if (resp.headers.get('Connection') === 'Upgrade') {
respMessage.writeHead(101);
} else {
respMessage.writeHead(resp.status === 0 ? 500 : resp.status);
}
}
if (context.signal.aborted) {
if (! respMessage.headersSent) {
respMessage.setHeader('Content-Type', 'application/json');
respMessage.writeHead(408);
}
if (respMessage.writable) {
const cause = context.signal.reason instanceof Error ? context.signal.reason : new Error(context.signal.reason);
respMessage.write(JSON.stringify(new HTTPError(cause.message, { status: 408, cause })));
}
respMessage.end();
} else if (respMessage.writable && body instanceof ReadableStream && ! body.locked) {
const reader = body.getReader();
const cancel = reason => {
if (body.locked) {
reader.closed.then(() => {
body.cancel(reason).catch(_noop);
}).catch(_noop);
reader.releaseLock();
} else {
body.cancel(reason).catch(_noop);
}
};
try {
signal.addEventListener('abort', ({ target }) => cancel(target.reason), { once: true });
if (body.locked) {
while (! signal.aborted) {
const { done, value } = await reader.read();
if (done) {
reader.releaseLock();
respMessage.end();
body.cancel('Done').catch(_noop);
break;
} else {
respMessage.write(value);
}
}
} else {
respMessage.destroySoon();
}
} catch(err) {
respMessage.destroy(err);
cancel(err);
}
} else if (resp.headers.get('Connection') !== 'Upgrade') {
respMessage.end();
} else {
// IDK why this works...
respMessage.end();
}
} else if (typeof resp === 'object') {
const { headers: respHeaders = {}, body = null, status = 200, statusText } = resp;
await _send(new Response(body, {
status,
statusText,
headers: Array.isArray(respHeaders) ? Object.fromEntries(respHeaders) : respHeaders,
}), respMessage, { responsePostprocessors, context, request });
} else {
await _send(new HTTPError('Invalid response.'), respMessage, { responsePostprocessors, context, request });
}
}
/**
* Starts a development server.
*
* @param {object} config Configuration options for the server.
* @param {string} [config.hostname="localhost"] The hostname to listen on.
* @param {string} [config.staticRoot="/"] The path to the directory containing static files.
* @param {number} [config.port=8080] The port to listen on.
* @param {Blob & { type: "application/pkcs8" }} [config.key] The private key in PEM format (PKCS#8 encoded).
* @param {Blob & { type: "application/x-pem-file" }} [config.cert] The certificate in PEM format (X.509 encoded).
* @param {string} [config.pathname="/"] The URL path to serve the application on.
* @param {object} [config.routes={}] A map of URL patterns to route handlers.
* @param {Function} [config.logger=console.error] A function to log messages.
* @param {boolean} [config.open=false] Whether to open the application in the browser.
* @param {Function[]} [config.requestPreprocessors] Functions run before request handling, capable of modifying context, logging, validating, or aborting requests.
* @param {Function[]} [config.responsePostprocessors] Functions to modify a response after being created but before being sent.
* @param {AbortSignal} [config.signal] A signal to abort the server.
* @param {number} [config.timeout=1000] Max length to keep the incoming connection alive before aborting.
* @param {boolean} [config.allowHTTP1] Whether or not to revert to HTTP 1.1 if HTTP 2 is not supported by the client.
* @returns {Promise<{server: Server<typeof IncomingMessage, typeof ServerResponse>, url: string, whenServerClosed: Promise<void>}>} An object containing the server instance, the URL it is listening on, and a promise that resolves when the server is closed.
*/
export async function serve({
hostname = 'localhost',
staticRoot = '/',
port = 8080,
key,
cert,
pathname = '/',
routes = {},
logger = console.error,
open = false,
requestPreprocessors = [],
responsePostprocessors = [],
signal: passedSignal,
timeout = 1000,
allowHTTP1 = true,
} = {}) {
const { promise: whenServerClosed, resolve: resolveClosed } = Promise.withResolvers();
const schema = _hasKeys({ key, cert }) ? 'https:' : 'http:';
const url = new URL(pathname, `${schema}//${hostname}:${port}`).href;
const ROUTES = new Map(Object.entries(routes).map(([pattern, module]) => {
// Only interested in the `pathname` & `search`, so parse those out. Origin should not matter.
const { pathname, search } = new URLPattern(pattern, url);
return [new URLPattern({ pathname, search }), module];
}));
await Promise.all([
Promise.all(requestPreprocessors.map(_importMiddleware)),
Promise.all(responsePostprocessors.map(_importMiddleware)),
]);
const server = await _createServer(
/**
* Handles incoming HTTP requests.
*
* @param {import('node:http').IncomingMessage} incomingMessage - The HTTP request.
* @param {import('node:http').ServerResponse} serverResponse - The HTTP response.
*/
async function(incomingMessage, serverResponse) {
const stack = new DisposableStack();
const controller = stack.adopt(
new AbortController(),
controller => controller.abort(new DOMException('Stack disposed', 'AbortError'))
);
const signal = passedSignal instanceof AbortSignal
? AbortSignal.any([passedSignal, controller.signal])
: controller.signal;
const request = HTTPRequest.createFromIncomingMessage(incomingMessage, { signal });
const url = new URL(request.url);
const fileURL = getFileURL(url, staticRoot);
const pattern = ROUTES.keys().find(pattern => pattern.test(request.url));
const cookies = new RequestCookieMap(request);
const { resolve: resolveRequest, reject: rejectRequest, promise } = Promise.withResolvers();
let settled = false;
if (typeof timeout === 'number' && ! Number.isNaN(timeout) && request.body instanceof ReadableStream) {
const timeoutHandle = setTimeout(() => {
const err = new HTTPError('Connection timed out.', { status: 408 });
controller.abort(err);
}, timeout);
incomingMessage.once('end', () => clearTimeout(timeoutHandle));
}
const resolve = result => {
if (! settled) {
resolveRequest(result);
settled = true;
}
};
const reject = reason => {
if (! settled) {
rejectRequest(reason);
settled = true;
}
};
const matches = pattern instanceof URLPattern ? pattern.exec(request.url) : undefined;
const params = typeof matches === 'object'
? {
...matches.protocol.groups, ...matches.username.groups, ...matches.password.groups, ...matches.hostname.groups,
...matches.port.groups, ...matches.pathname.groups, ...matches.search.groups, ...matches.hash.groups,
} : {};
delete params['0'];
const context = {
url,
searchParams: url.searchParams,
matches: pattern instanceof URLPattern ? Object.freeze(matches) : null,
params: Object.freeze(params),
cookies,
ip: incomingMessage.socket.remoteAddress,
controller,
signal: controller.signal,
stack,
socket: incomingMessage.socket,
resolve,
reject,
};
try {
incomingMessage.socket.once('close', () => controller.abort('Socket closed'));
signal.addEventListener('abort', () => incomingMessage.removeAllListeners(), { once: true });
const hookErrs = await Promise.allSettled(requestPreprocessors.map(plugin => plugin(request, context)))
.then(results => results.filter(result => result.status === 'rejected').map(result => result.reason));
Object.freeze(context);
if (signal.aborted) {
reject(signal.reason);
} else if (hookErrs.length === 1) {
reject(hookErrs[0]);
} else if (hookErrs.length !== 0) {
reject(new AggregateError(hookErrs));
} else if (controller.signal.aborted) {
// Controller would be aborted if any of the pre-hooks aborted it.
reject(controller.signal.reason);
} else if (! settled && pattern instanceof URLPattern) {
const moduleSpecifier = ROUTES.get(pattern);
const module = moduleSpecifier instanceof Function
? { default: moduleSpecifier }
: await import(moduleSpecifier).catch(err => err);
if (module instanceof Error) {
reject(module);
} else if (! (module.default instanceof Function)) {
reject(HTTPError('There was an error handling the request.', {
status: 500,
cause: new Error(`${moduleSpecifier} is missing a default export.`),
}));
} else {
const resp = await Promise.try(() => module.default(request, context)).catch(err => err);
if (resp instanceof Response) {
resolve(resp);
} else if (resp instanceof URL) {
resolve(Response.redirect(resp));
} else if (resp instanceof Error) {
reject(resp);
} else {
reject(new TypeError(`${moduleSpecifier} did not return a response.`));
}
}
} else if (! settled && existsSync(fileURL)) {
const resolved = await resolveStaticPath(fileURL.pathname);
if (typeof resolved === 'string') {
const resp = await respondWithFile(resolved);
resolve(resp);
} else {
reject(new HTTPError(`<${request.url}> not found.`, { status: 404 }));
}
} else if (! settled) {
reject(new HTTPError(`<${request.url}> not found.`, { status: 404 }));
}
} catch(err) {
reject(err);
}
await promise
.then(resp => _send(resp, serverResponse, { responsePostprocessors, request, context }))
.catch(async err => {
if (logger instanceof Function) {
Promise.try(() => logger(err));
}
if (err instanceof HTTPError) {
await _send(err.response, serverResponse, { responsePostprocessors, request, context });
} else {
await _send(new HTTPError('An unknown error occured', { cause: err, status: 500 }).response, serverResponse, { responsePostprocessors, context });
}
}).finally(() => stack.dispose());
},
{ key, cert, allowHTTP1 }
);
server.listen(port, hostname);
if (passedSignal instanceof AbortSignal) {
passedSignal.addEventListener('abort', () => server.close(), { once: true });
}
await new Promise(resolve => server.once('listening', resolve));
server.once('close', resolveClosed);
// Check if a given signal aborted during server start-up
if (passedSignal instanceof AbortSignal && passedSignal.aborted) {
server.close();
throw passedSignal.reason;
}
if (open) {
_open(url).catch(console.error);
}
return Object.freeze({ server, url, whenServerClosed });
}