Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions body.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import { createHttpError, matches, parseFormData, Status } from "./deps.ts";
import type { ServerRequest } from "./types.ts";
import type { ServerRequest, Uint8ArrayArrayBuffer } from "./types.ts";

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

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

async function readBlob(
body?: ReadableStream<Uint8Array> | null,
body?: ReadableStream<Uint8ArrayArrayBuffer> | null,
type?: string | null,
): Promise<Blob> {
if (!body) {
return new Blob(undefined, type ? { type } : undefined);
}
const chunks: Uint8Array[] = [];
const chunks: Uint8ArrayArrayBuffer[] = [];
for await (const chunk of body) {
chunks.push(chunk);
}
Expand All @@ -45,7 +45,7 @@ async function readBlob(

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

/** Exposes the "raw" `ReadableStream` of the body. */
get stream(): ReadableStream<Uint8Array> | null {
get stream(): ReadableStream<Uint8ArrayArrayBuffer> | null {
return this.#request ? this.#request.body : this.#body!;
}

Expand Down
5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@
"fmt": {
"exclude": ["README.md"]
},
"lint": {
"rules": {
"exclude": ["no-import-prefix"]
}
},
"lock": false
}
5 changes: 3 additions & 2 deletions http_server_bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
ServeOptions,
ServerRequest,
ServeTlsOptions,
Uint8ArrayArrayBuffer,
} from "./types.ts";
import { createPromiseWithResolvers } from "./utils/create_promise_with_resolvers.ts";

Expand Down Expand Up @@ -94,7 +95,7 @@ class BunRequest implements ServerRequest {
#resolved = false;
#promise: Promise<Response>;

get body(): ReadableStream<Uint8Array> | null {
get body(): ReadableStream<Uint8ArrayArrayBuffer> | null {
return this.#request.body;
}

Expand Down Expand Up @@ -150,7 +151,7 @@ class BunRequest implements ServerRequest {
this.#reject(reason);
}

getBody(): ReadableStream<Uint8Array> | null {
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
return this.#request.body;
}

Expand Down
5 changes: 3 additions & 2 deletions http_server_native_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type {
NetAddr,
ServerRequest,
Uint8ArrayArrayBuffer,
UpgradeWebSocketFn,
UpgradeWebSocketOptions,
} from "./types.ts";
Expand Down Expand Up @@ -55,7 +56,7 @@ export class NativeRequest implements ServerRequest {
this.#response = promise;
}

get body(): ReadableStream<Uint8Array> | null {
get body(): ReadableStream<Uint8ArrayArrayBuffer> | null {
return this.#request.body;
}

Expand Down Expand Up @@ -102,7 +103,7 @@ export class NativeRequest implements ServerRequest {
this.#resolved = true;
}

getBody(): ReadableStream<Uint8Array> | null {
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
return this.#request.body;
}

Expand Down
11 changes: 6 additions & 5 deletions http_server_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
ServeOptions,
ServerRequest,
ServeTlsOptions,
Uint8ArrayArrayBuffer,
} from "./types.ts";
import { createPromiseWithResolvers } from "./utils/create_promise_with_resolvers.ts";

Expand Down Expand Up @@ -90,15 +91,15 @@ export class NodeRequest implements ServerRequest {
this.#responded = true;
}

getBody(): ReadableStream<Uint8Array> | null {
let body: ReadableStream<Uint8Array> | null;
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
let body: ReadableStream<Uint8ArrayArrayBuffer> | null;
if (this.method === "GET" || this.method === "HEAD") {
body = null;
} else {
body = new ReadableStream<Uint8Array>({
body = new ReadableStream<Uint8ArrayArrayBuffer>({
start: (controller) => {
this.#request.on("data", (chunk: Uint8Array) => {
controller.enqueue(chunk);
this.#request.on("data", (chunk) => {
controller.enqueue(chunk as Uint8ArrayArrayBuffer);
});
this.#request.on("error", (err: Error) => {
controller.error(err);
Expand Down
17 changes: 12 additions & 5 deletions send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type { Response } from "./response.ts";
import { isNode } from "./utils/type_guards.ts";
import { decode } from "./utils/decode.ts";
import { resolvePath } from "./utils/resolve_path.ts";
import type { Uint8ArrayArrayBuffer } from "./types.ts";

if (isNode()) {
console.warn("oak send() does not work under Node.js.");
Expand Down Expand Up @@ -137,9 +138,15 @@ async function getEntity(
stats: Deno.FileInfo,
maxbuffer: number,
response: Response,
): Promise<[Uint8Array | Deno.FsFile, Uint8Array | FileInfo, FileInfo]> {
let body: Uint8Array | Deno.FsFile;
let entity: Uint8Array | FileInfo;
): Promise<
[
Uint8ArrayArrayBuffer | Deno.FsFile,
Uint8ArrayArrayBuffer | FileInfo,
FileInfo,
]
> {
let body: Uint8ArrayArrayBuffer | Deno.FsFile;
let entity: Uint8ArrayArrayBuffer | FileInfo;
const fileInfo = { mtime: new Date(mtime), size: stats.size };
if (stats.size < maxbuffer) {
const buffer = await Deno.readFile(path);
Expand Down Expand Up @@ -270,8 +277,8 @@ export async function send(
: contentTypes[extname(path)] ?? extname(path);
}

let entity: Uint8Array | FileInfo | null = null;
let body: Uint8Array | Deno.FsFile | null = null;
let entity: Uint8ArrayArrayBuffer | FileInfo | null = null;
let body: Uint8ArrayArrayBuffer | Deno.FsFile | null = null;
let fileInfo: FileInfo | null = null;

if (request.headers.has("If-None-Match") && mtime) {
Expand Down
9 changes: 6 additions & 3 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ServerRequest {
readonly url: string;
// deno-lint-ignore no-explicit-any
error(reason?: any): void;
getBody(): ReadableStream<Uint8Array> | null;
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null;
respond(response: Response): void | Promise<void>;
upgrade?(options?: UpgradeWebSocketOptions): WebSocket;
}
Expand All @@ -38,8 +38,11 @@ export interface ServerConstructor<T extends ServerRequest> {
type?: "native" | "node" | "bun";
}

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

export interface UpgradeWebSocketOptions {
protocol?: string;
Expand Down