Skip to content

Commit 042034f

Browse files
committed
fix: updates to support Deno 2
1 parent de20780 commit 042034f

File tree

9 files changed

+23
-110
lines changed

9 files changed

+23
-110
lines changed

application.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -643,29 +643,29 @@ export class Application<AS extends State = Record<string, any>>
643643

644644
/** Add an event listener for a `"close"` event which occurs when the
645645
* application is closed and no longer listening or handling requests. */
646-
addEventListener<S extends AS>(
646+
override addEventListener<S extends AS>(
647647
type: "close",
648648
listener: ApplicationCloseEventListenerOrEventListenerObject | null,
649649
options?: boolean | AddEventListenerOptions,
650650
): void;
651651
/** Add an event listener for an `"error"` event which occurs when an
652652
* un-caught error occurs when processing the middleware or during processing
653653
* of the response. */
654-
addEventListener<S extends AS>(
654+
override addEventListener<S extends AS>(
655655
type: "error",
656656
listener: ApplicationErrorEventListenerOrEventListenerObject<S, AS> | null,
657657
options?: boolean | AddEventListenerOptions,
658658
): void;
659659
/** Add an event listener for a `"listen"` event which occurs when the server
660660
* has successfully opened but before any requests start being processed. */
661-
addEventListener(
661+
override addEventListener(
662662
type: "listen",
663663
listener: ApplicationListenEventListenerOrEventListenerObject | null,
664664
options?: boolean | AddEventListenerOptions,
665665
): void;
666666
/** Add an event listener for an event. Currently valid event types are
667667
* `"error"` and `"listen"`. */
668-
addEventListener(
668+
override addEventListener(
669669
type: "close" | "error" | "listen",
670670
listener: EventListenerOrEventListenerObject | null,
671671
options?: boolean | AddEventListenerOptions,

docs/_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
theme: jekyll-theme-minimal
1+
theme: jekyll-theme-minimal

docs/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<meta charset="utf-8">
2+
<meta charset="utf-8" />
33
<title>Redirecting to https://oakserver.org/</title>
4-
<meta http-equiv="refresh" content="0; URL=https://oakserver.org/">
5-
<link rel="canonical" href="https://oakserver.org/">
4+
<meta http-equiv="refresh" content="0; URL=https://oakserver.org/" />
5+
<link rel="canonical" href="https://oakserver.org/" />

examples/static/index.html

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
<!DOCTYPE html>
22
<html>
3+
<head></head>
34

4-
<head></head>
5-
6-
<body>
7-
<h1>Hello Static World!</h1>
8-
<img src="deno_logo.png" width="150" />
9-
</body>
10-
11-
</html>
5+
<body>
6+
<h1>Hello Static World!</h1>
7+
<img src="deno_logo.png" width="150" />
8+
</body>
9+
</html>

middleware/etag.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,7 @@ import type { Context } from "../context.ts";
1111
import { eTag, type ETagOptions } from "../deps.ts";
1212
import type { Middleware } from "../middleware.ts";
1313
import { BODY_TYPES } from "../utils/consts.ts";
14-
import { isAsyncIterable, isReader } from "../utils/type_guards.ts";
15-
16-
// This is to work around issue introduced in Deno 1.40
17-
// See: https://github.com/denoland/deno/issues/22115
18-
function isFsFile(value: unknown): value is Deno.FsFile {
19-
return !!(value && typeof value === "object" && "stat" in value &&
20-
typeof value.stat === "function");
21-
}
14+
import { isAsyncIterable, isFsFile } from "../utils/type_guards.ts";
2215

2316
/** For a given Context, try to determine the response body entity that an ETag
2417
* can be calculated from. */
@@ -36,7 +29,7 @@ export function getEntity<S extends State = Record<string, any>>(
3629
if (BODY_TYPES.includes(typeof body)) {
3730
return Promise.resolve(String(body));
3831
}
39-
if (isAsyncIterable(body) || isReader(body)) {
32+
if (isAsyncIterable(body)) {
4033
return Promise.resolve(undefined);
4134
}
4235
if (typeof body === "object" && body !== null) {

response.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
import { contentType, isRedirectStatus, Status, STATUS_TEXT } from "./deps.ts";
1212
import { DomResponse } from "./http_server_native_request.ts";
1313
import type { Request } from "./request.ts";
14-
import { isAsyncIterable, isHtml, isReader } from "./utils/type_guards.ts";
14+
import { isAsyncIterable, isFsFile, isHtml } from "./utils/type_guards.ts";
1515
import { BODY_TYPES } from "./utils/consts.ts";
1616
import { encodeUrl } from "./utils/encode_url.ts";
1717
import {
1818
readableStreamFromAsyncIterable,
19-
readableStreamFromReader,
2019
Uint8ArrayTransformStream,
2120
} from "./utils/streams.ts";
2221

@@ -64,8 +63,8 @@ async function convertBodyToBodyInit(
6463
if (BODY_TYPES.includes(typeof body)) {
6564
result = String(body);
6665
type = type ?? (isHtml(result) ? "html" : "text/plain");
67-
} else if (isReader(body)) {
68-
result = readableStreamFromReader(body);
66+
} else if (isFsFile(body)) {
67+
result = body.readable;
6968
} else if (
7069
ArrayBuffer.isView(body) || body instanceof ArrayBuffer ||
7170
body instanceof Blob || body instanceof URLSearchParams

send.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export async function send(
179179
root,
180180
} = options;
181181
const trailingSlash = path[path.length - 1] === "/";
182-
path = decodeComponent(path.substr(parse(path).root.length));
182+
path = decodeComponent(path.substring(parse(path).root.length));
183183
if (index && trailingSlash) {
184184
path += index;
185185
}

utils/streams.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,8 @@
22

33
import { BODY_TYPES } from "./consts.ts";
44

5-
interface Reader {
6-
read(p: Uint8Array): Promise<number | null>;
7-
}
8-
9-
interface Closer {
10-
close(): void;
11-
}
12-
13-
interface ReadableStreamFromReaderOptions {
14-
/** If the `reader` is also a `Closer`, automatically close the `reader`
15-
* when `EOF` is encountered, or a read error occurs.
16-
*
17-
* Defaults to `true`. */
18-
autoClose?: boolean;
19-
20-
/** The size of chunks to allocate to read, the default is ~16KiB, which is
21-
* the maximum size that Deno operations can currently support. */
22-
chunkSize?: number;
23-
24-
/** The queuing strategy to create the `ReadableStream` with. */
25-
strategy?: { highWaterMark?: number | undefined; size?: undefined };
26-
}
27-
28-
function isCloser(value: unknown): value is Deno.Closer {
29-
return typeof value === "object" && value != null && "close" in value &&
30-
// deno-lint-ignore no-explicit-any
31-
typeof (value as Record<string, any>)["close"] === "function";
32-
}
33-
34-
const DEFAULT_CHUNK_SIZE = 16_640; // 17 Kib
35-
365
const encoder = new TextEncoder();
376

38-
/**
39-
* Create a `ReadableStream<Uint8Array>` from a `Reader`.
40-
*
41-
* When the pull algorithm is called on the stream, a chunk from the reader
42-
* will be read. When `null` is returned from the reader, the stream will be
43-
* closed along with the reader (if it is also a `Closer`).
44-
*/
45-
export function readableStreamFromReader(
46-
reader: Reader | (Reader & Closer),
47-
options: ReadableStreamFromReaderOptions = {},
48-
): ReadableStream<Uint8Array> {
49-
const {
50-
autoClose = true,
51-
chunkSize = DEFAULT_CHUNK_SIZE,
52-
strategy,
53-
} = options;
54-
55-
return new ReadableStream({
56-
async pull(controller) {
57-
const chunk = new Uint8Array(chunkSize);
58-
try {
59-
const read = await reader.read(chunk);
60-
if (read === null) {
61-
if (isCloser(reader) && autoClose) {
62-
reader.close();
63-
}
64-
controller.close();
65-
return;
66-
}
67-
controller.enqueue(chunk.subarray(0, read));
68-
} catch (e) {
69-
controller.error(e);
70-
if (isCloser(reader)) {
71-
reader.close();
72-
}
73-
}
74-
},
75-
cancel() {
76-
if (isCloser(reader) && autoClose) {
77-
reader.close();
78-
}
79-
},
80-
}, strategy);
81-
}
82-
837
/**
848
* Create a `ReadableStream<Uint8Array>` from an `AsyncIterable`.
859
*/

utils/type_guards.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,9 @@ export function isNode(): boolean {
4444
!("Bun" in globalThis) && !("WebSocketPair" in globalThis);
4545
}
4646

47-
/** Guard for `Deno.Reader`. */
48-
export function isReader(value: unknown): value is Deno.Reader {
49-
return typeof value === "object" && value !== null && "read" in value &&
50-
typeof (value as Record<string, unknown>).read === "function";
47+
export function isFsFile(value: unknown): value is Deno.FsFile {
48+
return !!(value && typeof value === "object" && "stat" in value &&
49+
typeof value.stat === "function");
5150
}
5251

5352
export function isRouterContext<

0 commit comments

Comments
 (0)