From d1fd693c0753e966667a8547528742f58799badc Mon Sep 17 00:00:00 2001 From: Kitson Kelly Date: Mon, 7 Oct 2024 14:19:57 +1100 Subject: [PATCH] chore: update deps and changes for Deno 2 --- .vscode/settings.json | 2 +- context.ts | 2 +- deno.json | 12 ++++++------ request_server_bun.ts | 4 ++-- request_server_node.ts | 4 ++-- route.ts | 15 ++++++++------- routing.bench.ts | 12 ++++++++++++ utils.ts | 3 ++- uuid.bench.ts | 17 +++++++++++++++++ 9 files changed, 51 insertions(+), 20 deletions(-) create mode 100644 uuid.bench.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 27d2b22..906c1c7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,5 +5,5 @@ "respondable", "valibot" ], - "deno.unstable": true + "deno.unstable": ["kv"] } diff --git a/context.ts b/context.ts index d238b5d..0ffb3ef 100644 --- a/context.ts +++ b/context.ts @@ -321,7 +321,7 @@ export class Context< }); if (location) { if (params) { - const toPath = compile(location, { strict: true }); + const toPath = compile(location); response.headers.set("location", toPath(params)); } else { response.headers.set("location", location); diff --git a/deno.json b/deno.json index 53fe729..2d22640 100644 --- a/deno.json +++ b/deno.json @@ -15,8 +15,8 @@ "tasks": { "bench": "deno bench --allow-write --allow-read", "check": "deno check mod.ts", - "example": "deno run --allow-net --allow-env --allow-hrtime --unstable-kv _examples/server.ts", - "test": "deno test --allow-net --allow-env --allow-hrtime" + "example": "deno run --allow-net --allow-env --unstable-kv _examples/server.ts", + "test": "deno test --allow-net --allow-env" }, "imports": { "@oak/commons": "jsr:@oak/commons@^1.0", @@ -24,10 +24,10 @@ "@std/http": "jsr:@std/http@^1.0", "@std/log": "jsr:@std/log@^0.224", "@std/media-types": "jsr:@std/media-types@^1.0", - "@valibot/valibot": "jsr:@valibot/valibot@^0.39", - "hyperid": "npm:hyperid@^3.2", - "path-to-regexp": "npm:path-to-regexp@^7.1", - "qs": "npm:qs@^6.12" + "@valibot/valibot": "jsr:@valibot/valibot@^0.42", + "hyperid": "npm:hyperid@^3.3", + "path-to-regexp": "npm:path-to-regexp@^8.2", + "qs": "npm:qs@^6.13" }, "lock": false } diff --git a/request_server_bun.ts b/request_server_bun.ts index 4dcea98..2bdd332 100644 --- a/request_server_bun.ts +++ b/request_server_bun.ts @@ -3,6 +3,7 @@ import { createHttpError } from "@oak/commons/http_errors"; import { Status } from "@oak/commons/status"; import hyperid from "hyperid"; +import process from "node:process"; import type { Addr, @@ -102,8 +103,7 @@ class BunRequestEvent< } get env(): Env { - // @ts-ignore available when running under Bun - return process.env; + return process.env as Env; } get request(): Request { diff --git a/request_server_node.ts b/request_server_node.ts index ce3784d..6e8cfae 100644 --- a/request_server_node.ts +++ b/request_server_node.ts @@ -5,6 +5,7 @@ import { Status } from "@oak/commons/status"; import hyperid from "hyperid"; import type { IncomingMessage, ServerResponse } from "node:http"; import type { AddressInfo } from "node:net"; +import process from "node:process"; import type { Addr, @@ -43,8 +44,7 @@ class NodeRequestEvent> } get env(): Env { - // @ts-ignore available when running under Node.js - return process.env; + return process.env as Env; } get id(): string { diff --git a/route.ts b/route.ts index ea0c6b4..5468991 100644 --- a/route.ts +++ b/route.ts @@ -123,7 +123,7 @@ export class PathRoute< #params?: Params; #paramKeys: Key[]; #path: Path; - #regex: RegExp & { keys: Key[] }; + #regexp: RegExp; #schema: Schema; /** @@ -154,7 +154,7 @@ export class PathRoute< * The path pattern that has been converted into {@linkcode RegExp}. */ get regex(): RegExp { - return this.#regex; + return this.#regexp; } /** @@ -189,8 +189,9 @@ export class PathRoute< this.#handler = handler; this.#keys = keys; this.#expose = expose; - this.#regex = pathToRegexp(path, { ...options, strict: true }); - this.#paramKeys = this.#regex.keys; + const { regexp, keys: paramKeys } = pathToRegexp(path, { ...options }); + this.#regexp = regexp; + this.#paramKeys = paramKeys; this.#logger = getLogger("acorn.route"); this.#logger .debug(`created route with path: ${path} and methods: ${methods}`); @@ -268,7 +269,7 @@ export class PathRoute< * Determines if the request should be handled by the route. */ matches(method: HttpMethod, pathname: string): boolean | NotAllowed { - const match = pathname.match(this.#regex); + const match = pathname.match(this.#regexp); if (match) { if (!this.#methods.includes(method)) { return NOT_ALLOWED; @@ -297,7 +298,7 @@ export class PathRoute< inspect({ params: this.#params, path: this.#path, - regex: this.#regex, + regex: this.#regexp, schema: this.#schema, }) }`; @@ -321,7 +322,7 @@ export class PathRoute< inspect({ params: this.#params, path: this.#path, - regex: this.#regex, + regex: this.#regexp, schema: this.#schema, }, newOptions) }`; diff --git a/routing.bench.ts b/routing.bench.ts index d796c03..6b98e4b 100644 --- a/routing.bench.ts +++ b/routing.bench.ts @@ -1,5 +1,6 @@ import { pathToRegexp as pathToRegexp7 } from "npm:path-to-regexp@7.0.0"; import { pathToRegexp as pathToRegexp71 } from "npm:path-to-regexp@7.1.0"; +import { pathToRegexp as pathToRegexp8 } from "npm:path-to-regexp@8.2.0"; import { pathToRegexp } from "npm:path-to-regexp@6.2.1"; import { URLPattern as URLPatternPolyfill } from "npm:urlpattern-polyfill@10.0.0"; @@ -60,3 +61,14 @@ Deno.bench({ } }, }); + +const { regexp: regexp8 } = pathToRegexp8("/book/:id"); + +Deno.bench({ + name: "pathToRegexp 8", + fn() { + if (regexp8.exec("/book/1234")) { + true; + } + }, +}); diff --git a/utils.ts b/utils.ts index 0a269aa..da70e14 100644 --- a/utils.ts +++ b/utils.ts @@ -53,5 +53,6 @@ export function isBun(): boolean { /** Determines if the runtime is Node.js or not. */ export function isNode(): boolean { return "process" in globalThis && "global" in globalThis && - !("Bun" in globalThis) && !("WebSocketPair" in globalThis); + !("Bun" in globalThis) && !("WebSocketPair" in globalThis) && + !("Deno" in globalThis); } diff --git a/uuid.bench.ts b/uuid.bench.ts new file mode 100644 index 0000000..d16668d --- /dev/null +++ b/uuid.bench.ts @@ -0,0 +1,17 @@ +import hyperid from "hyperid"; + +const instance = hyperid({ urlSafe: true }); + +Deno.bench({ + name: "hyperid", + fn() { + instance(); + }, +}); + +Deno.bench({ + name: "crypto.randomUUID", + fn() { + crypto.randomUUID(); + }, +});