Skip to content

Commit 29005e0

Browse files
authored
Merge branch 'main' into feat/websocket-idletimeout-support
2 parents a3372f6 + 51cc817 commit 29005e0

File tree

8 files changed

+72
-53
lines changed

8 files changed

+72
-53
lines changed

README.md

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,19 @@ oak is available on both [deno.land/x](https://deno.land/x/oak/) and
4747
import { Application } from "https://deno.land/x/oak/mod.ts";
4848
```
4949

50-
To use from JSR, import into a module:
50+
To use from JSR, add it to your project:
5151

52-
```ts
53-
import { Application } from "jsr:@oak/oak";
52+
```
53+
deno add jsr:@oak/oak
5454
```
5555

56-
Or use the Deno CLI to add it to your project:
56+
Then import into a module:
5757

58-
```
59-
deno add jsr:@oak/oak
58+
```ts
59+
import { Application } from "@oak/oak";
6060
```
6161

62+
6263
### Node.js
6364

6465
oak is available for Node.js on both
@@ -157,7 +158,7 @@ processing requests with the registered middleware.
157158
A basic usage, responding to every request with _Hello World!_:
158159

159160
```ts
160-
import { Application } from "jsr:@oak/oak/application";
161+
import { Application } from "@oak/oak/application";
161162

162163
const app = new Application();
163164

@@ -184,7 +185,7 @@ context and reference to the "next" method in the stack.
184185
A more complex example:
185186

186187
```ts
187-
import { Application } from "jsr:@oak/oak/application";
188+
import { Application } from "@oak/oak/application";
188189

189190
const app = new Application();
190191

@@ -232,7 +233,7 @@ or `undefined` if the `ctx.respond === true`.
232233
An example:
233234

234235
```ts
235-
import { Application } from "jsr:@oak/oak/application";
236+
import { Application } from "@oak/oak/application";
236237

237238
const app = new Application();
238239

@@ -649,7 +650,7 @@ will fire a `"listen"` event, which can be listened for via the
649650
`.addEventListener()` method. For example:
650651

651652
```ts
652-
import { Application } from "jsr:@oak/oak/application";
653+
import { Application } from "@oak/oak/application";
653654

654655
const app = new Application();
655656

@@ -673,7 +674,7 @@ If you want to close the application, the application supports the option of an
673674
Here is an example of using the signal:
674675

675676
```ts
676-
import { Application } from "jsr:@oak/oak/application";
677+
import { Application } from "@oak/oak/application";
677678

678679
const app = new Application();
679680

@@ -700,9 +701,9 @@ handling middleware that provides a well managed response to errors would work
700701
like this:
701702

702703
```ts
703-
import { Application } from "jsr:@oak/oak/application";
704-
import { isHttpError } from "jsr:@oak/commons/http_errors";
705-
import { Status } from "jsr:@oak/commons/status";
704+
import { Application } from "@oak/oak/application";
705+
import { isHttpError } from "@oak/commons/http_errors";
706+
import { Status } from "@oak/commons/status";
706707

707708
const app = new Application();
708709

@@ -733,7 +734,7 @@ application. To listen for these errors, you would add an event handler to the
733734
application instance:
734735

735736
```ts
736-
import { Application } from "jsr:@oak/oak/application";
737+
import { Application } from "@oak/oak/application";
737738

738739
const app = new Application();
739740

@@ -762,8 +763,8 @@ The following example serves up a _RESTful_ service of a map of books, where
762763
`http://localhost:8000/book/1` would return the book with ID `"1"`:
763764

764765
```ts
765-
import { Application } from "jsr:@oak/oak/application";
766-
import { Router } from "jsr:@oak/oak/router";
766+
import { Application } from "@oak/oak/application";
767+
import { Router } from "@oak/oak/router";
767768

768769
const books = new Map<string, any>();
769770
books.set("1", {
@@ -814,8 +815,8 @@ Nesting routers is supported. The following example responds to
814815
`http://localhost:8000/forums/oak/posts/nested-routers`.
815816

816817
```typescript
817-
import { Application } from "jsr:@oak/oak/application";
818-
import { Router } from "jsr:@oak/oak/router";
818+
import { Application } from "@oak/oak/application";
819+
import { Router } from "@oak/oak/router";
819820

820821
const posts = new Router()
821822
.get("/", (ctx) => {
@@ -845,7 +846,7 @@ system relative to the root from the requested path.
845846
A basic usage would look something like this:
846847

847848
```ts
848-
import { Application } from "jsr:@oak/oak/application";
849+
import { Application } from "@oak/oak/application";
849850

850851
const app = new Application();
851852

@@ -881,8 +882,8 @@ determines if it can create an `ETag` header for that body type, and if so sets
881882
the `ETag` header on the response. Basic usage would look something like this:
882883

883884
```ts
884-
import { Application } from "jsr:@oak/oak/application";
885-
import { factory } from "jsr:@oak/oak/etag";
885+
import { Application } from "@oak/oak/application";
886+
import { factory } from "@oak/oak/etag";
886887

887888
const app = new Application();
888889

@@ -893,12 +894,12 @@ app.use(factory());
893894

894895
There is also a function which retrieves an entity for a given context based on
895896
what it logical to read into memory which can be passed to the etag calculate
896-
that is part of the Deno std library:
897+
that is part of the Deno std library (`deno add jsr:@std/http`):
897898

898899
```ts
899-
import { Application } from "jsr:@oak/oak/application";
900-
import { getEntity } from "jsr:@oak/oak/etag";
901-
import { calculate } from "jsr:@std/http/etag";
900+
import { Application } from "@oak/oak/application";
901+
import { getEntity } from "@oak/oak/etag";
902+
import { calculate } from "@std/http/etag";
902903

903904
const app = new Application();
904905

@@ -938,8 +939,8 @@ the handler to resolve with a Fetch API `Response`.
938939
An example of using `serve()` with `Application.prototype.use()`:
939940

940941
```ts
941-
import { Application } from "jsr:@oak/oak/application";
942-
import { serve } from "jsr:@oak/oak/serve";
942+
import { Application } from "@oak/oak/application";
943+
import { serve } from "@oak/oak/serve";
943944

944945
const app = new Application();
945946

@@ -955,9 +956,9 @@ And a similar solution works with `route()` where the context contains the
955956
information about the router, like the params:
956957

957958
```ts
958-
import { Application } from "jsr:@oak/oak/application";
959-
import { Router } from "jsr:@oak/oak/router";
960-
import { route } from "jsr:@oak/oak/serve";
959+
import { Application } from "@oak/oak/application";
960+
import { Router } from "@oak/oak/router";
961+
import { route } from "@oak/oak/serve";
961962

962963
const app = new Application;
963964

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> | 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[] = [];
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> | 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> | null {
83+
get stream(): ReadableStream<Uint8ArrayArrayBuffer> | null {
8484
return this.#request ? this.#request.body : this.#body!;
8585
}
8686

deno.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,10 @@
4040
"fmt": {
4141
"exclude": ["README.md"]
4242
},
43+
"lint": {
44+
"rules": {
45+
"exclude": ["no-import-prefix"]
46+
}
47+
},
4348
"lock": false
4449
}

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> | 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> | 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> | 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> | null {
106+
getBody(): ReadableStream<Uint8ArrayArrayBuffer> | null {
106107
return this.#request.body;
107108
}
108109

http_server_node.ts

Lines changed: 6 additions & 5 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> | null {
94-
let body: ReadableStream<Uint8Array> | 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>({
99+
body = new ReadableStream<Uint8ArrayArrayBuffer>({
99100
start: (controller) => {
100-
this.#request.on("data", (chunk: Uint8Array) => {
101-
controller.enqueue(chunk);
101+
this.#request.on("data", (chunk) => {
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 | Deno.FsFile, Uint8Array | FileInfo, FileInfo]> {
141-
let body: Uint8Array | Deno.FsFile;
142-
let entity: Uint8Array | 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 | FileInfo | null = null;
274-
let body: Uint8Array | 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> | 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;
42-
export type Key = string | number[] | ArrayBuffer | Uint8Array;
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)