Skip to content

Commit

Permalink
chore: update to std 0.212.0, commons 0.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jan 26, 2024
1 parent 8920f73 commit 8e3ffa7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
2 changes: 1 addition & 1 deletion _examples/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
16 changes: 8 additions & 8 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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";
8 changes: 3 additions & 5 deletions deps_test.ts
Original file line number Diff line number Diff line change
@@ -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";
8 changes: 4 additions & 4 deletions router.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -87,17 +87,17 @@ Deno.test({
const { signal } = abortController;
const rp: Promise<string>[] = [];
router.get("/", () => {
const d = deferred<Response>();
const { promise, resolve } = Promise.withResolvers<Response>();
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()));
Expand Down
29 changes: 14 additions & 15 deletions router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { Context } from "./context.ts";
import {
createHttpError,
deferred,
isClientErrorStatus,
isErrorStatus,
isHttpError,
Expand Down Expand Up @@ -877,9 +876,9 @@ export class Router extends EventTarget {
async #handle(requestEvent: RequestEvent, addr: Addr): Promise<void> {
const uid = this.#uid++;
performance.mark(`${HANDLE_START} ${uid}`);
const d = deferred<Response>();
this.#handling.add(d);
requestEvent.respondWith(d).catch((error) =>
const { promise, resolve } = Promise.withResolvers<Response>();
this.#handling.add(promise);
requestEvent.respondWith(promise).catch((error) =>
this.#error(requestEvent.request, error, false)
);
const { request } = requestEvent;
Expand All @@ -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({
Expand Down Expand Up @@ -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}`,
Expand All @@ -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}`,
Expand All @@ -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}`,
Expand All @@ -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}`,
Expand Down Expand Up @@ -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<Response> {
const d = deferred<Response>();
const { promise, resolve } = Promise.withResolvers<Response>();
this.#secure = init.secure ?? false;
this.#handle({
request,
respondWith(response: Response | Promise<Response>): Promise<void> {
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
Expand Down

0 comments on commit 8e3ffa7

Please sign in to comment.