summaryrefslogtreecommitdiff
path: root/ext/flash/01_http.js
diff options
context:
space:
mode:
authorLuca Casonato <hello@lcas.dev>2022-08-24 00:08:56 +0200
committerGitHub <noreply@github.com>2022-08-24 00:08:56 +0200
commit4ef08a58dfbcf893f25fd59917aa946f455e85f2 (patch)
tree7b1e07869fd23f1ce65029ab3725b9ef054f7576 /ext/flash/01_http.js
parentf0993f413b86997674ea4bc6812af7d2c529ec96 (diff)
feat: update `Deno.serve` function signature (#15563)
This commit changes the `Deno.serve` function signature to be more versatile and easier to use. It is now a drop in replacement for std/http's `serve`. The input validation has also been reworked.
Diffstat (limited to 'ext/flash/01_http.js')
-rw-r--r--ext/flash/01_http.js82
1 files changed, 56 insertions, 26 deletions
diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js
index 9fda597c7..a9ae9d85a 100644
--- a/ext/flash/01_http.js
+++ b/ext/flash/01_http.js
@@ -25,7 +25,9 @@
} = window.__bootstrap.webSocket;
const { _ws } = window.__bootstrap.http;
const {
+ Function,
ObjectPrototypeIsPrototypeOf,
+ PromiseResolve,
TypedArrayPrototypeSubarray,
TypeError,
Uint8Array,
@@ -185,48 +187,76 @@
return hostname === "0.0.0.0" ? "localhost" : hostname;
}
- function serve(opts = {}) {
- if (!("fetch" in opts)) {
- throw new TypeError("Options is missing 'fetch' handler");
+ async function serve(arg1, arg2) {
+ let options = undefined;
+ let handler = undefined;
+ if (arg1 instanceof Function) {
+ handler = arg1;
+ options = arg2;
+ } else if (arg2 instanceof Function) {
+ handler = arg2;
+ options = arg1;
+ } else {
+ options = arg1;
+ }
+ if (handler === undefined) {
+ if (options === undefined) {
+ throw new TypeError(
+ "No handler was provided, so an options bag is mandatory.",
+ );
+ }
+ handler = options.handler;
}
- if ("cert" in opts && !("key" in opts)) {
- throw new TypeError("Options is missing 'key' field");
+ if (!(handler instanceof Function)) {
+ throw new TypeError("A handler function must be provided.");
}
- if ("key" in opts && !("cert" in opts)) {
- throw new TypeError("Options is missing 'cert' field");
+ if (options === undefined) {
+ options = {};
}
- opts = { hostname: "127.0.0.1", port: 9000, ...opts };
- const handler = opts.fetch;
- delete opts.fetch;
- const signal = opts.signal;
- delete opts.signal;
- const onError = opts.onError ?? function (error) {
+
+ const signal = options.signal;
+
+ const onError = options.onError ?? function (error) {
console.error(error);
return new Response("Internal Server Error", { status: 500 });
};
- delete opts.onError;
- const onListen = opts.onListen ?? function ({ port }) {
+
+ const onListen = options.onListen ?? function ({ port }) {
console.log(
- `Listening on http://${hostnameForDisplay(opts.hostname)}:${port}/`,
+ `Listening on http://${
+ hostnameForDisplay(listenOpts.hostname)
+ }:${port}/`,
);
};
- delete opts.onListen;
- const serverId = core.ops.op_flash_serve(opts);
+
+ const listenOpts = {
+ hostname: options.hostname ?? "127.0.0.1",
+ port: options.port ?? 9000,
+ };
+ if (options.cert || options.key) {
+ if (!options.cert || !options.key) {
+ throw new TypeError(
+ "Both cert and key must be provided to enable HTTPS.",
+ );
+ }
+ listenOpts.cert = options.cert;
+ listenOpts.key = options.key;
+ }
+
+ const serverId = core.ops.op_flash_serve(listenOpts);
const serverPromise = core.opAsync("op_flash_drive_server", serverId);
core.opAsync("op_flash_wait_for_listening", serverId).then((port) => {
- onListen({ hostname: opts.hostname, port });
+ onListen({ hostname: listenOpts.hostname, port });
});
const server = {
id: serverId,
- transport: opts.cert && opts.key ? "https" : "http",
- hostname: opts.hostname,
- port: opts.port,
+ transport: listenOpts.cert && listenOpts.key ? "https" : "http",
+ hostname: listenOpts.hostname,
+ port: listenOpts.port,
closed: false,
- finished: (async () => {
- return await serverPromise;
- })(),
+ finished: PromiseResolve(serverPromise),
async close() {
if (server.closed) {
return;
@@ -520,7 +550,7 @@
}, 1000);
}
- return server.serve().catch(console.error);
+ return await server.serve().catch(console.error);
}
function createRequestBodyStream(serverId, token) {