Skip to content

Commit

Permalink
- Update to Deno 2.1.3
Browse files Browse the repository at this point in the history
- Update to latest `@std` deps
- [@http/route] allow `asURLPattern` to accept a full URL pattern string, by
  attempting to parse the pattern directly with `new URLPattern` before falling
  back to treating it as `pathname` of a `URLPatternInit`
- [@http/route] allow `byPattern`, `bySubPattern`, `cascade` and `lazy` handlers
  to return something other than a `Response` if desired
- [@http/discovery] remove use of `@std/url` in tests
- [@http/host-deno-local], [@http/host-deno-deploy] fix `Deno.serve` related
  types
- [@http/response] allow `prependDocType` to accept `Iterable<Uint8Array>` which
  is now a valid type of `BodyInit`
- fix caught error types to be explicitly `unknown`
  • Loading branch information
jollytoad committed Dec 11, 2024
1 parent 2bc6926 commit 205dc25
Show file tree
Hide file tree
Showing 36 changed files with 209 additions and 97 deletions.
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.importMap": "./import_map_local.json",
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.tabSize": 2,
"deno.codeLens.testArgs": [
Expand Down
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ and this project adheres to

This changelog will need to be split between individual packages

## [0.25.0]

### Changed

- Update to Deno 2.1.3
- Update to latest `@std` deps
- [@http/route] allow `asURLPattern` to accept a full URL pattern string, by
attempting to parse the pattern directly with `new URLPattern` before falling
back to treating it as `pathname` of a `URLPatternInit`
- [@http/route] allow `byPattern`, `bySubPattern`, `cascade` and `lazy` handlers
to return something other than a `Response` if desired

### Fixed

- [@http/discovery] remove use of `@std/url` in tests
- [@http/host-deno-local], [@http/host-deno-deploy] fix `Deno.serve` related
types
- [@http/response] allow `prependDocType` to accept `Iterable<Uint8Array>` which
is now a valid type of `BodyInit`
- fix caught error types to be explicitly `unknown`

## [0.24.0]

### Added
Expand Down
6 changes: 3 additions & 3 deletions _tools/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export const rootPath = import.meta.dirname
export async function readJson(path: string) {
try {
return JSON.parse(await Deno.readTextFile(path));
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
} catch (error: unknown) {
if (error instanceof Deno.errors.NotFound) {
return undefined;
} else {
throw e;
throw error;
}
}
}
Expand Down
45 changes: 22 additions & 23 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,30 @@
},
"imports": {
"@dprint/formatter": "jsr:@dprint/formatter@^0.4.1",
"@http/assert": "jsr:@http/assert@^0.24.0",
"@http/discovery": "jsr:@http/discovery@^0.24.0",
"@http/examples": "jsr:@http/examples@^0.24.0",
"@http/fs": "jsr:@http/fs@^0.24.0",
"@http/generate": "jsr:@http/generate@^0.24.0",
"@http/host-bun-local": "jsr:@http/host-bun-local@^0.24.0",
"@http/host-cloudflare-worker": "jsr:@http/host-cloudflare-worker@^0.24.0",
"@http/host-deno-deploy": "jsr:@http/host-deno-deploy@^0.24.0",
"@http/host-deno-local": "jsr:@http/host-deno-local@^0.24.0",
"@http/interceptor": "jsr:@http/interceptor@^0.24.0",
"@http/request": "jsr:@http/request@^0.24.0",
"@http/response": "jsr:@http/response@^0.24.0",
"@http/route": "jsr:@http/route@^0.24.0",
"@std/assert": "jsr:@std/assert@^1.0.5",
"@std/bytes": "jsr:@std/bytes@^1.0.2",
"@std/collections": "jsr:@std/collections@^1.0.5",
"@http/assert": "jsr:@http/assert@^0.25.0",
"@http/discovery": "jsr:@http/discovery@^0.25.0",
"@http/examples": "jsr:@http/examples@^0.25.0",
"@http/fs": "jsr:@http/fs@^0.25.0",
"@http/generate": "jsr:@http/generate@^0.25.0",
"@http/host-bun-local": "jsr:@http/host-bun-local@^0.25.0",
"@http/host-cloudflare-worker": "jsr:@http/host-cloudflare-worker@^0.25.0",
"@http/host-deno-deploy": "jsr:@http/host-deno-deploy@^0.25.0",
"@http/host-deno-local": "jsr:@http/host-deno-local@^0.25.0",
"@http/interceptor": "jsr:@http/interceptor@^0.25.0",
"@http/request": "jsr:@http/request@^0.25.0",
"@http/response": "jsr:@http/response@^0.25.0",
"@http/route": "jsr:@http/route@^0.25.0",
"@std/assert": "jsr:@std/assert@^1.0.9",
"@std/bytes": "jsr:@std/bytes@^1.0.4",
"@std/collections": "jsr:@std/collections@^1.0.9",
"@std/datetime": "jsr:@std/datetime@^0.225.2",
"@std/fs": "jsr:@std/fs@^1.0.3",
"@std/http": "jsr:@std/http@^1.0.6",
"@std/media-types": "jsr:@std/media-types@^1.0.3",
"@std/fs": "jsr:@std/fs@^1.0.6",
"@std/http": "jsr:@std/http@^1.0.12",
"@std/media-types": "jsr:@std/media-types@^1.1.0",
"@std/net": "jsr:@std/net@^1.0.4",
"@std/path": "jsr:@std/path@^1.0.6",
"@std/streams": "jsr:@std/streams@^1.0.5",
"@std/testing": "jsr:@std/testing@^1.0.2",
"@std/url": "jsr:@std/url@^1.0.0-rc.3",
"@std/path": "jsr:@std/path@^1.0.8",
"@std/streams": "jsr:@std/streams@^1.0.8",
"@std/testing": "jsr:@std/testing@^1.0.6",
"$test/generate/": "./packages/generate/_test/"
},
"workspace": [
Expand Down
2 changes: 1 addition & 1 deletion packages/assert/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/assert",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./header": "./header.ts",
".": "./mod.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/discovery/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/discovery",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./as-serializable-pattern": "./as_serializable_pattern.ts",
"./cascading-handler-mapper": "./cascading_handler_mapper.ts",
Expand Down
5 changes: 3 additions & 2 deletions packages/discovery/fresh_path_mapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals, fail } from "@std/assert";
import { discoverRoutes } from "./discover_routes.ts";
import freshPathMapper from "./fresh_path_mapper.ts";
import { join } from "@std/url/join";
import { join } from "@std/path/posix/join";
import { asSerializablePattern } from "./as_serializable_pattern.ts";

const fileRootUrl = import.meta.resolve("./_test/_fresh_routes");
Expand Down Expand Up @@ -39,7 +39,8 @@ Deno.test("freshPathMapper", async (t) => {
}

function assertRoute(expectedPattern: string, modulePath: string) {
const expectedModule = join(fileRootUrl, modulePath);
const expectedModule = new URL(fileRootUrl);
expectedModule.pathname = join(expectedModule.pathname, modulePath);
for (const { pattern, module } of routes) {
if (asSerializablePattern(pattern) === expectedPattern) {
assertEquals(module, expectedModule);
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/examples",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./by-media-type": "./by_media_type.ts",
"./by-method": "./by_method.ts",
Expand Down
20 changes: 10 additions & 10 deletions packages/fs/_testdata/hello.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Hello World</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Hello World</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
2 changes: 1 addition & 1 deletion packages/fs/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/fs",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./file-body": "./file_body.ts",
"./file-body-bun": "./file_body_bun.ts",
Expand Down
4 changes: 2 additions & 2 deletions packages/fs/serve_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function serveDir(
let fileInfo: FileStats;
try {
fileInfo = await stat(fsPath);
} catch (error) {
} catch (error: unknown) {
if (fileNotFound(error)) {
return notFound();
}
Expand Down Expand Up @@ -138,7 +138,7 @@ export async function serveDir(
etagDefault,
});
}
} catch (error) {
} catch (error: unknown) {
if (!fileNotFound(error)) {
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/generate/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/generate",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./code-builder/generate": "./code-builder/generate.ts",
"./code-builder": "./code-builder/mod.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/host-bun-local/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/host-bun-local",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./init": "./init.ts",
"./load-key-and-cert": "./load_key_and_cert.ts",
Expand Down
5 changes: 3 additions & 2 deletions packages/host-bun-local/load_key_and_cert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ export async function loadKeyAndCert(): Promise<KeyAndCert | undefined> {
key: await Bun.file(KEY_FILE).text(),
cert: await Bun.file(CERT_FILE).text(),
};
} catch (error) {
if (error.code !== "ENOENT") {
} catch (error: unknown) {
// deno-lint-ignore no-explicit-any
if ((error as any)?.code !== "ENOENT") {
throw error;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/host-cloudflare-worker/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/host-cloudflare-worker",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./asset-handler": "./asset_handler.ts",
"./init": "./init.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/host-deno-deploy/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/host-deno-deploy",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./deno-deploy-etag": "./deno_deploy_etag.ts",
"./init": "./init.ts",
Expand Down
6 changes: 4 additions & 2 deletions packages/host-deno-deploy/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import type { Awaitable, Interceptors } from "@http/interceptor/types";
export default function initDeploy(
handler: (
req: Request,
info: Deno.ServeHandlerInfo,
info: Deno.ServeHandlerInfo<Deno.NetAddr>,
) => Awaitable<Response | null>,
...interceptors: Interceptors<unknown[], Response>[]
): Deno.ServeInit & (Deno.ServeOptions | Deno.ServeTlsOptions) {
):
& Deno.ServeInit<Deno.NetAddr>
& (Deno.ServeTcpOptions | Deno.TlsCertifiedKeyPem) {
return {
handler: intercept(withFallback(handler), logging(), ...interceptors),
};
Expand Down
2 changes: 1 addition & 1 deletion packages/host-deno-local/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/host-deno-local",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./init": "./init.ts",
"./load-key-and-cert": "./load_key_and_cert.ts",
Expand Down
9 changes: 7 additions & 2 deletions packages/host-deno-local/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import type { Interceptors } from "@http/interceptor/types";
export default async function initLocalhost(
handler: (
req: Request,
info: Deno.ServeHandlerInfo,
info: Deno.ServeHandlerInfo<Deno.NetAddr>,
) => Awaitable<Response | null>,
...interceptors: Interceptors<unknown[], Response>[]
): Promise<Deno.ServeInit & (Deno.ServeOptions | Deno.ServeTlsOptions)> {
): Promise<
& Deno.ServeInit<Deno.NetAddr>
& (Deno.ServeTcpOptions | Deno.TlsCertifiedKeyPem)
> {
const keyAndCert = await loadKeyAndCert();
return {
handler: intercept(withFallback(handler), logging(), ...interceptors),
Expand All @@ -26,3 +29,5 @@ export default async function initLocalhost(
onListen: logServerUrl(keyAndCert),
};
}

Deno.serve;
14 changes: 8 additions & 6 deletions packages/host-deno-local/load_key_and_cert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const KEY_FILE = "localhost-key.pem";
const CERT_FILE = "localhost-cert.pem";

export interface KeyAndCert {
key: string;
cert: string;
}
/**
* @deprecated use Deno.TlsCertifiedKeyPem instead
*/
export type KeyAndCert = Deno.TlsCertifiedKeyPem;

/**
* Allow serving over HTTPS on localhost, by load the TLS key and certificate for localhost from the files
Expand All @@ -21,7 +21,9 @@ export interface KeyAndCert {
*
* @returns options that can be added to the `Deno.serve` options
*/
export async function loadKeyAndCert(): Promise<KeyAndCert | undefined> {
export async function loadKeyAndCert(): Promise<
Deno.TlsCertifiedKeyPem | undefined
> {
if (Deno.args.includes("--http")) {
return;
}
Expand All @@ -31,7 +33,7 @@ export async function loadKeyAndCert(): Promise<KeyAndCert | undefined> {
key: await Deno.readTextFile(KEY_FILE),
cert: await Deno.readTextFile(CERT_FILE),
};
} catch (error) {
} catch (error: unknown) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/interceptor/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/interceptor",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./apply-forwarded-headers": "./apply_forwarded_headers.ts",
"./catch-response": "./catch_response.ts",
Expand Down
16 changes: 8 additions & 8 deletions packages/interceptor/intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export function intercept<A extends unknown[], R extends Response | null>(
for (const interceptor of flatten("finally", true)) {
try {
interceptor(req, res, reason);
} catch (e: unknown) {
console.error("Error during finally interceptor", e);
} catch (error: unknown) {
console.error("Error during finally interceptor", error);
}
}
}
Expand All @@ -127,22 +127,22 @@ export function intercept<A extends unknown[], R extends Response | null>(

try {
await applyRequestInterceptors();
} catch (e: unknown) {
await applyErrorInterceptors(e);
} catch (error: unknown) {
await applyErrorInterceptors(error);
}

if (!res) {
try {
res = await handler(req, ...args);
} catch (e: unknown) {
await applyErrorInterceptors(e);
} catch (error: unknown) {
await applyErrorInterceptors(error);
}
}

try {
await applyResponseInterceptors();
} catch (e: unknown) {
await applyErrorInterceptors(e);
} catch (error: unknown) {
await applyErrorInterceptors(error);
}

return res;
Expand Down
8 changes: 4 additions & 4 deletions packages/request/body_as_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export async function getBodyAsObject<T>(
case "application/json":
try {
body = await req.json();
} catch (e) {
throw badRequest(`Invalid request body: ${e.message}`);
} catch (error: unknown) {
throw badRequest(`Invalid request body: ${(error as Error)?.message}`);
}
if (!body || typeof body !== "object") {
throw badRequest(
Expand All @@ -44,8 +44,8 @@ export async function getBodyAsObject<T>(
if (processForm) {
body = processForm(body, form);
}
} catch (e) {
throw badRequest(`Invalid request body: ${e.message}`);
} catch (error: unknown) {
throw badRequest(`Invalid request body: ${(error as Error)?.message}`);
}
break;

Expand Down
2 changes: 1 addition & 1 deletion packages/request/deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@http/request",
"version": "0.24.0",
"version": "0.25.0",
"exports": {
"./body-as-object": "./body_as_object.ts",
"./memoize": "./memoize.ts",
Expand Down
Loading

0 comments on commit 205dc25

Please sign in to comment.