From 5beec3f106b0890cc76150d0e3b3661c576d4c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 19 Aug 2022 14:36:01 +0200 Subject: feat(unstable): change Deno.serve() API (#15498) - Merge "Deno.serve()" and "Deno.serveTls()" API - Remove first argument and use "fetch" field options instead - Update type declarations - Add more documentation --- ext/flash/01_http.js | 26 +++++++++++++------------- ext/flash/README.md | 2 +- ext/flash/lib.rs | 7 +------ 3 files changed, 15 insertions(+), 20 deletions(-) (limited to 'ext') diff --git a/ext/flash/01_http.js b/ext/flash/01_http.js index 19920da58..d850a1520 100644 --- a/ext/flash/01_http.js +++ b/ext/flash/01_http.js @@ -185,18 +185,19 @@ return hostname === "0.0.0.0" ? "localhost" : hostname; } - function serve(handler, opts = {}) { - delete opts.key; - delete opts.cert; - return serveInner(handler, opts, false); - } - - function serveTls(handler, opts = {}) { - return serveInner(handler, opts, true); - } - - function serveInner(handler, opts, useTls) { - opts = { hostname: "127.0.0.1", port: 9000, useTls, ...opts }; + function serve(opts = {}) { + if (!("fetch" in opts)) { + throw new TypeError("Options is missing 'fetch' handler"); + } + if ("cert" in opts && !("key" in opts)) { + throw new TypeError("Options is missing 'key' field"); + } + if ("key" in opts && !("cert" in opts)) { + throw new TypeError("Options is missing 'cert' field"); + } + 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) { @@ -570,6 +571,5 @@ window.__bootstrap.flash = { serve, - serveTls, }; })(this); diff --git a/ext/flash/README.md b/ext/flash/README.md index 465c60d47..bc3c12065 100644 --- a/ext/flash/README.md +++ b/ext/flash/README.md @@ -3,5 +3,5 @@ Flash is a fast HTTP/1.1 server implementation for Deno. ```js -serve((req) => new Response("Hello World")); +serve({ fetch: (req) => new Response("Hello World") }); ``` diff --git a/ext/flash/lib.rs b/ext/flash/lib.rs index 8f3cb341a..abd8502d0 100644 --- a/ext/flash/lib.rs +++ b/ext/flash/lib.rs @@ -935,7 +935,6 @@ pub struct ListenOpts { key: Option, hostname: String, port: u16, - use_tls: bool, } fn run_server( @@ -1239,11 +1238,7 @@ fn op_flash_serve

( where P: FlashPermissions + 'static, { - if opts.use_tls { - check_unstable(state, "Deno.serveTls"); - } else { - check_unstable(state, "Deno.serve"); - } + check_unstable(state, "Deno.serve"); state .borrow_mut::

() .check_net(&(&opts.hostname, Some(opts.port)))?; -- cgit v1.2.3