From 8e3ffa715a5cdca15468a1025016f46028ad96f0 Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Fri, 26 Jan 2024 20:15:00 +1100 Subject: [PATCH] chore: update to std 0.212.0, commons 0.5.0 --- _examples/server.ts | 2 +- deps.ts | 16 ++++++++-------- deps_test.ts | 8 +++----- router.test.ts | 8 ++++---- router.ts | 29 ++++++++++++++--------------- 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/_examples/server.ts b/_examples/server.ts index b34d2ae..6d1c9f3 100644 --- a/_examples/server.ts +++ b/_examples/server.ts @@ -3,7 +3,7 @@ import { ServerSentEvent, ServerSentEventStreamTarget, -} from "https://deno.land/std@0.194.0/http/server_sent_event.ts"; +} from "https://deno.land/x/oak_commons@0.5.0/server_sent_event.ts"; import { auth, immutable, Router } from "../mod.ts"; import { createHttpError, Status } from "../deps.ts"; diff --git a/deps.ts b/deps.ts index b7b8d39..8133412 100644 --- a/deps.ts +++ b/deps.ts @@ -1,17 +1,20 @@ // Copyright 2022-2023 the oak authors. All rights reserved. -export { deferred } from "https://deno.land/std@0.194.0/async/deferred.ts"; export { type Data as SigningData, type Key as SigningKey, -} from "https://deno.land/std@0.194.0/crypto/keystack.ts"; -export { SecureCookieMap } from "https://deno.land/std@0.194.0/http/cookie_map.ts"; +} from "https://deno.land/std@0.212.0/crypto/unstable_keystack.ts"; +export { accepts } from "https://deno.land/std@0.212.0/http/negotiation.ts"; +export { UserAgent } from "https://deno.land/std@0.212.0/http/user_agent.ts"; +export { contentType } from "https://deno.land/std@0.212.0/media_types/content_type.ts"; + +export { SecureCookieMap } from "https://deno.land/x/oak_commons@0.5.0/cookie_map.ts"; export { createHttpError, errors, type HttpError, isHttpError, -} from "https://deno.land/std@0.194.0/http/http_errors.ts"; +} from "https://deno.land/x/oak_commons@0.5.0/http_errors.ts"; export { isClientErrorStatus, isErrorStatus, @@ -21,7 +24,4 @@ export { isSuccessfulStatus, Status, STATUS_TEXT, -} from "https://deno.land/std@0.194.0/http/http_status.ts"; -export { accepts } from "https://deno.land/std@0.194.0/http/negotiation.ts"; -export { UserAgent } from "https://deno.land/std@0.194.0/http/user_agent.ts"; -export { contentType } from "https://deno.land/std@0.194.0/media_types/mod.ts"; +} from "https://deno.land/x/oak_commons@0.5.0/status.ts"; diff --git a/deps_test.ts b/deps_test.ts index 9d48a33..92eefc6 100644 --- a/deps_test.ts +++ b/deps_test.ts @@ -1,7 +1,5 @@ // Copyright 2022-2023 the oak authors. All rights reserved. -export { - assert, - assertEquals, - assertRejects, -} from "https://deno.land/std@0.194.0/testing/asserts.ts"; +export { assert } from "https://deno.land/std@0.212.0/assert/assert.ts"; +export { assertEquals } from "https://deno.land/std@0.212.0/assert/assert_equals.ts"; +export { assertRejects } from "https://deno.land/std@0.212.0/assert/assert_rejects.ts"; diff --git a/router.test.ts b/router.test.ts index 7ae2199..fc0296f 100644 --- a/router.test.ts +++ b/router.test.ts @@ -1,6 +1,6 @@ // Copyright 2022-2023 the oak authors. All rights reserved. -import { deferred, Status } from "./deps.ts"; +import { Status } from "./deps.ts"; import { assertEquals } from "./deps_test.ts"; import { Router, RouterRequestEvent } from "./router.ts"; import { assert } from "./util.ts"; @@ -87,17 +87,17 @@ Deno.test({ const { signal } = abortController; const rp: Promise[] = []; router.get("/", () => { - const d = deferred(); + const { promise, resolve } = Promise.withResolvers(); setTimeout( () => - d.resolve( + resolve( new Response(JSON.stringify({ hello: "world" }), { headers: { "content-type": "application/json" }, }), ), 200, ); - return d; + return promise; }); router.addEventListener("listen", ({ port, hostname }) => { rp.push(fetch(`http://${hostname}:${port}/`).then((r) => r.text())); diff --git a/router.ts b/router.ts index 4aa6f45..b0468b1 100644 --- a/router.ts +++ b/router.ts @@ -3,7 +3,6 @@ import { Context } from "./context.ts"; import { createHttpError, - deferred, isClientErrorStatus, isErrorStatus, isHttpError, @@ -877,9 +876,9 @@ export class Router extends EventTarget { async #handle(requestEvent: RequestEvent, addr: Addr): Promise { const uid = this.#uid++; performance.mark(`${HANDLE_START} ${uid}`); - const d = deferred(); - this.#handling.add(d); - requestEvent.respondWith(d).catch((error) => + const { promise, resolve } = Promise.withResolvers(); + this.#handling.add(promise); + requestEvent.respondWith(promise).catch((error) => this.#error(requestEvent.request, error, false) ); const { request } = requestEvent; @@ -894,7 +893,7 @@ export class Router extends EventTarget { } catch { // deal with a request dropping before the headers can be read which can // occur under heavy load - this.#handling.delete(d); + this.#handling.delete(promise); return; } const routerRequestEvent = new RouterRequestEvent({ @@ -923,7 +922,7 @@ export class Router extends EventTarget { response.headers, response, ); - d.resolve(result ?? response); + resolve(result ?? response); const measure = performance.measure( `handle ${uid}`, `${HANDLE_START} ${uid}`, @@ -948,8 +947,8 @@ export class Router extends EventTarget { if (!response) { response = this.#error(request, error, true); } - d.resolve(response); - this.#handling.delete(d); + resolve(response); + this.#handling.delete(promise); const measure = performance.measure( `handle ${uid}`, `${HANDLE_START} ${uid}`, @@ -970,8 +969,8 @@ export class Router extends EventTarget { responseHeaders, response, ); - d.resolve(result ?? response); - this.#handling.delete(d); + resolve(result ?? response); + this.#handling.delete(promise); const measure = performance.measure( `handle ${uid}`, `${HANDLE_START} ${uid}`, @@ -988,8 +987,8 @@ export class Router extends EventTarget { if (!response) { response = this.#notFound(request); } - d.resolve(response); - this.#handling.delete(d); + resolve(response); + this.#handling.delete(promise); const measure = performance.measure( `handle ${uid}`, `${HANDLE_START} ${uid}`, @@ -1416,16 +1415,16 @@ export class Router extends EventTarget { * This is intended to be used when the router isn't managing opening the * server and listening for requests. */ handle(request: Request, init: RouterHandleInit): Promise { - const d = deferred(); + const { promise, resolve } = Promise.withResolvers(); this.#secure = init.secure ?? false; this.#handle({ request, respondWith(response: Response | Promise): Promise { - d.resolve(response); + resolve(response); return Promise.resolve(); }, }, init.addr); - return d; + return promise; } /** Open a server to listen for requests and handle them by matching against