Skip to content

Commit ff8721f

Browse files
committed
fix: update for Uint8Array being generic
1 parent 843c996 commit ff8721f

File tree

6 files changed

+34
-21
lines changed

6 files changed

+34
-21
lines changed

body.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010

1111
import { createHttpError, matches, parseFormData, Status } from "./deps.ts";
12-
import type { ServerRequest } from "./types.ts";
12+
import type { ServerRequest, Uint8ArrayArrayBuffer } from "./types.ts";
1313

1414
type JsonReviver = (key: string, value: unknown) => unknown;
1515

@@ -30,13 +30,13 @@ const KNOWN_BODY_TYPES: [bodyType: BodyType, knownMediaTypes: string[]][] = [
3030
];
3131

3232
async function readBlob(
33-
body?: ReadableStream<Uint8Array<ArrayBuffer>> | null,
33+
body?: ReadableStream<Uint8ArrayArrayBuffer> | null,
3434
type?: string | null,
3535
): Promise<Blob> {
3636
if (!body) {
3737
return new Blob(undefined, type ? { type } : undefined);
3838
}
39-
const chunks: Uint8Array<ArrayBuffer>[] = [];
39+
const chunks: Uint8ArrayArrayBuffer[] = [];
4040
for await (const chunk of body) {
4141
chunks.push(chunk);
4242
}
@@ -45,7 +45,7 @@ async function readBlob(
4545

4646
/** An object which encapsulates information around a request body. */
4747
export class Body {
48-
#body?: ReadableStream<Uint8Array<ArrayBuffer>> | null;
48+
#body?: ReadableStream<Uint8ArrayArrayBuffer> | null;
4949
#memo: Promise<ArrayBuffer | Blob | FormData | string> | null = null;
5050
#memoType: "arrayBuffer" | "blob" | "formData" | "text" | null = null;
5151
#headers?: Headers;
@@ -80,7 +80,7 @@ export class Body {
8080
}
8181

8282
/** Exposes the "raw" `ReadableStream` of the body. */
83-
get stream(): ReadableStream<Uint8Array<ArrayBuffer>> | null {
83+
get stream(): ReadableStream<Uint8ArrayArrayBuffer> | null {
8484
return this.#request ? this.#request.body : this.#body!;
8585
}
8686

http_server_bun.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
ServeOptions,
1414
ServerRequest,
1515
ServeTlsOptions,
16+
Uint8ArrayArrayBuffer,
1617
} from "./types.ts";
1718
import { createPromiseWithResolvers } from "./utils/create_promise_with_resolvers.ts";
1819

@@ -94,7 +95,7 @@ class BunRequest implements ServerRequest {
9495
#resolved = false;
9596
#promise: Promise<Response>;
9697

97-
get body(): ReadableStream<Uint8Array<ArrayBuffer>> | null {
98+
get body(): ReadableStream<Uint8ArrayArrayBuffer> | null {
9899
return this.#request.body;
99100
}
100101

@@ -150,7 +151,7 @@ class BunRequest implements ServerRequest {
150151
this.#reject(reason);
151152
}
152153

153-
getBody(): ReadableStream<Uint8Array<ArrayBuffer>> | null {
154+
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
154155
return this.#request.body;
155156
}
156157

http_server_native_request.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type {
44
NetAddr,
55
ServerRequest,
6+
Uint8ArrayArrayBuffer,
67
UpgradeWebSocketFn,
78
UpgradeWebSocketOptions,
89
} from "./types.ts";
@@ -55,7 +56,7 @@ export class NativeRequest implements ServerRequest {
5556
this.#response = promise;
5657
}
5758

58-
get body(): ReadableStream<Uint8Array<ArrayBuffer>> | null {
59+
get body(): ReadableStream<Uint8ArrayArrayBuffer> | null {
5960
return this.#request.body;
6061
}
6162

@@ -102,7 +103,7 @@ export class NativeRequest implements ServerRequest {
102103
this.#resolved = true;
103104
}
104105

105-
getBody(): ReadableStream<Uint8Array<ArrayBuffer>> | null {
106+
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
106107
return this.#request.body;
107108
}
108109

http_server_node.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
ServeOptions,
1313
ServerRequest,
1414
ServeTlsOptions,
15+
Uint8ArrayArrayBuffer,
1516
} from "./types.ts";
1617
import { createPromiseWithResolvers } from "./utils/create_promise_with_resolvers.ts";
1718

@@ -90,15 +91,15 @@ export class NodeRequest implements ServerRequest {
9091
this.#responded = true;
9192
}
9293

93-
getBody(): ReadableStream<Uint8Array<ArrayBuffer>> | null {
94-
let body: ReadableStream<Uint8Array<ArrayBuffer>> | null;
94+
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
95+
let body: ReadableStream<Uint8ArrayArrayBuffer> | null;
9596
if (this.method === "GET" || this.method === "HEAD") {
9697
body = null;
9798
} else {
98-
body = new ReadableStream<Uint8Array<ArrayBuffer>>({
99+
body = new ReadableStream<Uint8ArrayArrayBuffer>({
99100
start: (controller) => {
100101
this.#request.on("data", (chunk) => {
101-
controller.enqueue(chunk as Uint8Array<ArrayBuffer>);
102+
controller.enqueue(chunk as Uint8ArrayArrayBuffer);
102103
});
103104
this.#request.on("error", (err: Error) => {
104105
controller.error(err);

send.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type { Response } from "./response.ts";
3131
import { isNode } from "./utils/type_guards.ts";
3232
import { decode } from "./utils/decode.ts";
3333
import { resolvePath } from "./utils/resolve_path.ts";
34+
import type { Uint8ArrayArrayBuffer } from "./types.ts";
3435

3536
if (isNode()) {
3637
console.warn("oak send() does not work under Node.js.");
@@ -137,9 +138,15 @@ async function getEntity(
137138
stats: Deno.FileInfo,
138139
maxbuffer: number,
139140
response: Response,
140-
): Promise<[Uint8Array<ArrayBuffer> | Deno.FsFile, Uint8Array<ArrayBuffer> | FileInfo, FileInfo]> {
141-
let body: Uint8Array<ArrayBuffer> | Deno.FsFile;
142-
let entity: Uint8Array<ArrayBuffer> | FileInfo;
141+
): Promise<
142+
[
143+
Uint8ArrayArrayBuffer | Deno.FsFile,
144+
Uint8ArrayArrayBuffer | FileInfo,
145+
FileInfo,
146+
]
147+
> {
148+
let body: Uint8ArrayArrayBuffer | Deno.FsFile;
149+
let entity: Uint8ArrayArrayBuffer | FileInfo;
143150
const fileInfo = { mtime: new Date(mtime), size: stats.size };
144151
if (stats.size < maxbuffer) {
145152
const buffer = await Deno.readFile(path);
@@ -270,8 +277,8 @@ export async function send(
270277
: contentTypes[extname(path)] ?? extname(path);
271278
}
272279

273-
let entity: Uint8Array<ArrayBuffer> | FileInfo | null = null;
274-
let body: Uint8Array<ArrayBuffer> | Deno.FsFile | null = null;
280+
let entity: Uint8ArrayArrayBuffer | FileInfo | null = null;
281+
let body: Uint8ArrayArrayBuffer | Deno.FsFile | null = null;
275282
let fileInfo: FileInfo | null = null;
276283

277284
if (request.headers.has("If-None-Match") && mtime) {

types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface ServerRequest {
2121
readonly url: string;
2222
// deno-lint-ignore no-explicit-any
2323
error(reason?: any): void;
24-
getBody(): ReadableStream<Uint8Array<ArrayBuffer>> | null;
24+
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null;
2525
respond(response: Response): void | Promise<void>;
2626
upgrade?(options?: UpgradeWebSocketOptions): WebSocket;
2727
}
@@ -38,8 +38,11 @@ export interface ServerConstructor<T extends ServerRequest> {
3838
type?: "native" | "node" | "bun";
3939
}
4040

41-
export type Data = string | number[] | ArrayBuffer | Uint8Array<ArrayBuffer>;
42-
export type Key = string | number[] | ArrayBuffer | Uint8Array<ArrayBuffer>;
41+
/** Type for backwards compatibility. Resolves to `Uint8Array<ArrayBuffer>` in
42+
* TypeScript 5.7+ and `Uint8Array` in older versions. */
43+
export type Uint8ArrayArrayBuffer = ReturnType<Uint8Array["slice"]>;
44+
export type Data = string | number[] | ArrayBuffer | Uint8ArrayArrayBuffer;
45+
export type Key = string | number[] | ArrayBuffer | Uint8ArrayArrayBuffer;
4346

4447
export interface UpgradeWebSocketOptions {
4548
protocol?: string;

0 commit comments

Comments
 (0)