ovr 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app/index.d.ts +1 -1
- package/dist/app/index.d.ts.map +1 -1
- package/dist/app/index.js +5 -3
- package/dist/context/index.d.ts +4 -4
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +17 -20
- package/dist/cookie/index.js +2 -2
- package/dist/jsx/elements.d.ts +194 -2
- package/dist/jsx/elements.d.ts.map +1 -1
- package/dist/jsx/index.d.ts +6 -4
- package/dist/jsx/index.d.ts.map +1 -1
- package/dist/jsx/index.js +18 -8
- package/dist/multipart/index.d.ts.map +1 -1
- package/dist/multipart/index.js +7 -8
- package/dist/route/index.d.ts.map +1 -1
- package/dist/route/index.js +4 -4
- package/dist/util/index.d.ts +36 -3
- package/dist/util/index.d.ts.map +1 -1
- package/dist/util/index.js +58 -2
- package/package.json +1 -1
- package/src/app/index.ts +5 -4
- package/src/context/index.ts +21 -24
- package/src/cookie/index.ts +4 -4
- package/src/jsx/elements.ts +320 -64
- package/src/jsx/index.ts +21 -11
- package/src/multipart/index.ts +7 -9
- package/src/route/index.ts +7 -4
- package/src/util/index.ts +69 -4
package/dist/multipart/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Codec, Header } from "../util/index.js";
|
|
1
|
+
import { Codec, Header, Mime } from "../util/index.js";
|
|
2
2
|
/** Sequence of bytes to find within the stream */
|
|
3
3
|
class Needle extends Uint8Array {
|
|
4
4
|
/** Index of the last character in the needle */
|
|
@@ -41,6 +41,7 @@ class Part extends Request {
|
|
|
41
41
|
* @example "my-image.png"
|
|
42
42
|
*/
|
|
43
43
|
filename;
|
|
44
|
+
// this is named `type` instead of `mime` to align with `Blob.type`
|
|
44
45
|
/**
|
|
45
46
|
* Media type of the part
|
|
46
47
|
*
|
|
@@ -73,15 +74,14 @@ class Part extends Request {
|
|
|
73
74
|
// @ts-expect-error - streaming https://developer.mozilla.org/en-US/docs/Web/API/RequestInit#duplex
|
|
74
75
|
duplex: "half",
|
|
75
76
|
});
|
|
76
|
-
[this.type] = Header.shift(this.headers.get(Header.
|
|
77
|
-
({ name: this.name = null, filename: this.filename = null } = Header.params(this.headers.get(
|
|
77
|
+
[this.type] = Header.shift(this.headers.get(Header.type));
|
|
78
|
+
({ name: this.name = null, filename: this.filename = null } = Header.params(this.headers.get(Header.disposition)));
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
/** Multipart request */
|
|
81
82
|
export class Multipart extends Request {
|
|
82
83
|
static #kb = 1024;
|
|
83
84
|
static #mb = 1024 ** 2;
|
|
84
|
-
static #multipartType = "multipart/form-data";
|
|
85
85
|
/** New line needle to share across requests and parts */
|
|
86
86
|
static #newLine = new Needle("\r\n\r\n");
|
|
87
87
|
/** Parser options */
|
|
@@ -123,10 +123,9 @@ export class Multipart extends Request {
|
|
|
123
123
|
*/
|
|
124
124
|
constructor(req, options) {
|
|
125
125
|
super(req);
|
|
126
|
-
const [
|
|
127
|
-
if (
|
|
126
|
+
const [mime, params] = Header.shift(this.headers.get(Header.type));
|
|
127
|
+
if (!Mime.multipart(mime))
|
|
128
128
|
throw new TypeError("Unsupported Media Type");
|
|
129
|
-
}
|
|
130
129
|
const { boundary } = Header.params(params);
|
|
131
130
|
if (!boundary)
|
|
132
131
|
throw new TypeError("Boundary Not Found");
|
|
@@ -324,7 +323,7 @@ export class Multipart extends Request {
|
|
|
324
323
|
for await (const part of this) {
|
|
325
324
|
if (part.name) {
|
|
326
325
|
let value;
|
|
327
|
-
if (part.filename || part.type ===
|
|
326
|
+
if (part.filename || part.type === Mime.stream) {
|
|
328
327
|
const blob = await part.blob();
|
|
329
328
|
value = new File([blob], part.filename ?? "blob", {
|
|
330
329
|
type: blob.type,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/route/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAA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AO,CAAC,GACnB,QAAQ,CAAC,OAAO,CAAC,GACjB,UAAU,CAAC,OAAO,CAAC;IAgBpB;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,CAAC,GAAG,KAAK,GAAG,UAAU,GAAG,QAAQ;IACtE;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,OAAO,SAAS,MAAM,EACjC,OAAO,EAAE,OAAO,EAChB,GAAG,UAAU,EAAE,UAAU,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,EAAE,GACjD,KAAK,CAAC,OAAO,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,QAAQ,CAAC,OAAO,CAAC;CAkB3D"}
|
package/dist/route/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx } from "../jsx/index.js";
|
|
2
|
-
import { Hash } from "../util/index.js";
|
|
2
|
+
import { Hash, Method, Mime } from "../util/index.js";
|
|
3
3
|
/**
|
|
4
4
|
* Route to use in the application.
|
|
5
5
|
*
|
|
@@ -81,7 +81,7 @@ export class Route {
|
|
|
81
81
|
* @returns Route with added components
|
|
82
82
|
*/
|
|
83
83
|
static #withComponents(route) {
|
|
84
|
-
const enctype = route.method ===
|
|
84
|
+
const enctype = route.method === Method.post ? Mime.multipartFormData : undefined;
|
|
85
85
|
return Object.assign(route, {
|
|
86
86
|
Button: (({ params, search, hash, ...rest }) => jsx("button", {
|
|
87
87
|
formaction: route.url({ params, search, hash }),
|
|
@@ -104,7 +104,7 @@ export class Route {
|
|
|
104
104
|
* @returns GET `Route` with added components
|
|
105
105
|
*/
|
|
106
106
|
static get(pattern, ...middleware) {
|
|
107
|
-
const route = Route.#withComponents(new Route(
|
|
107
|
+
const route = Route.#withComponents(new Route(Method.get, pattern, ...middleware));
|
|
108
108
|
return Object.assign(route, {
|
|
109
109
|
Anchor: (({ params, search, hash, ...rest }) => jsx("a", {
|
|
110
110
|
href: route.url({ params, search, hash }),
|
|
@@ -121,6 +121,6 @@ export class Route {
|
|
|
121
121
|
middleware.unshift(patternOrMiddleware);
|
|
122
122
|
pattern = `/_p/${Hash.djb2(middleware.join())}`;
|
|
123
123
|
}
|
|
124
|
-
return Route.#withComponents(new Route(
|
|
124
|
+
return Route.#withComponents(new Route(Method.post, pattern, ...middleware));
|
|
125
125
|
}
|
|
126
126
|
}
|
package/dist/util/index.d.ts
CHANGED
|
@@ -1,12 +1,39 @@
|
|
|
1
1
|
/** General utility types */
|
|
2
2
|
export declare namespace Util {
|
|
3
|
-
type MaybePromise<T> = T | Promise<T>;
|
|
4
|
-
type MaybeFunction<T> = T | (() => T);
|
|
5
3
|
type DeepArray<T> = T | DeepArray<T>[];
|
|
6
4
|
}
|
|
5
|
+
/** Media type utils */
|
|
6
|
+
export declare class Mime {
|
|
7
|
+
#private;
|
|
8
|
+
static readonly html: "text/html";
|
|
9
|
+
static readonly text: "text/plain";
|
|
10
|
+
static readonly json: "application/json";
|
|
11
|
+
static readonly stream: "application/octet-stream";
|
|
12
|
+
static readonly multipartFormData: "multipart/form-data";
|
|
13
|
+
/**
|
|
14
|
+
* @param mime Media type
|
|
15
|
+
* @returns `true` if the mime is markup
|
|
16
|
+
*/
|
|
17
|
+
static markup(mime: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* @param mime Media type
|
|
20
|
+
* @returns `true` if the mime is multipart
|
|
21
|
+
*/
|
|
22
|
+
static multipart(mime: string | null): boolean | undefined;
|
|
23
|
+
}
|
|
7
24
|
/** Header parsing utils */
|
|
8
25
|
export declare class Header {
|
|
9
|
-
static readonly
|
|
26
|
+
static readonly type = "content-type";
|
|
27
|
+
static readonly disposition = "content-disposition";
|
|
28
|
+
static readonly etag = "etag";
|
|
29
|
+
static readonly ifNoneMatch = "if-none-match";
|
|
30
|
+
static readonly cookie = "cookie";
|
|
31
|
+
static readonly setCookie = "set-cookie";
|
|
32
|
+
/**
|
|
33
|
+
* @param mime
|
|
34
|
+
* @returns mime; charset=utf-8
|
|
35
|
+
*/
|
|
36
|
+
static utf8<M extends string>(mime: M): `${M}; charset=utf-8`;
|
|
10
37
|
/**
|
|
11
38
|
* @param header header value to parse
|
|
12
39
|
* @returns Base/first param
|
|
@@ -35,4 +62,10 @@ export declare class Codec {
|
|
|
35
62
|
/** DO NOT USE FOR STREAMS */
|
|
36
63
|
static decode: (input?: Uint8Array) => string;
|
|
37
64
|
}
|
|
65
|
+
/** HTTP methods */
|
|
66
|
+
export declare class Method {
|
|
67
|
+
static readonly get = "GET";
|
|
68
|
+
static readonly post = "POST";
|
|
69
|
+
static readonly head = "HEAD";
|
|
70
|
+
}
|
|
38
71
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/util/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,yBAAiB,IAAI,CAAC;IACrB,KAAY,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/util/index.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,yBAAiB,IAAI,CAAC;IACrB,KAAY,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC;CAC9C;AAED,uBAAuB;AACvB,qBAAa,IAAI;;IAChB,MAAM,CAAC,QAAQ,CAAC,IAAI,cAAsB;IAC1C,MAAM,CAAC,QAAQ,CAAC,IAAI,eAAuB;IAC3C,MAAM,CAAC,QAAQ,CAAC,IAAI,qBAA6B;IACjD,MAAM,CAAC,QAAQ,CAAC,MAAM,6BAAqC;IAE3D,MAAM,CAAC,QAAQ,CAAC,iBAAiB,wBAAgC;IAoBjE;;;OAGG;IACH,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM;IAQ1B;;;OAGG;IACH,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;CAGpC;AAED,2BAA2B;AAC3B,qBAAa,MAAM;IAClB,MAAM,CAAC,QAAQ,CAAC,IAAI,kBAAkB;IACtC,MAAM,CAAC,QAAQ,CAAC,WAAW,yBAAyB;IACpD,MAAM,CAAC,QAAQ,CAAC,IAAI,UAAU;IAC9B,MAAM,CAAC,QAAQ,CAAC,WAAW,mBAAmB;IAC9C,MAAM,CAAC,QAAQ,CAAC,MAAM,YAAY;IAClC,MAAM,CAAC,QAAQ,CAAC,SAAS,gBAAgB;IAEzC;;;OAGG;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC;IAIrC;;;OAGG;IACH,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,CAAC,MAAM,GAAG,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAWnE;;;OAGG;IACH,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;CAmDnC;AAED,gBAAgB;AAChB,qBAAa,IAAI;IAChB;;;;;OAKG;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,EAAE,MAAM;CAQrB;AAED,6CAA6C;AAC7C,qBAAa,KAAK;;IAIjB,MAAM,CAAC,MAAM,GAAI,GAAG,MAAM,6BAA8B;IAExD,6BAA6B;IAC7B,MAAM,CAAC,MAAM,GAAI,QAAQ,UAAU,YAAkC;CACrE;AAED,mBAAmB;AACnB,qBAAa,MAAM;IAClB,MAAM,CAAC,QAAQ,CAAC,GAAG,SAAS;IAC5B,MAAM,CAAC,QAAQ,CAAC,IAAI,UAAU;IAC9B,MAAM,CAAC,QAAQ,CAAC,IAAI,UAAU;CAC9B"}
|
package/dist/util/index.js
CHANGED
|
@@ -1,7 +1,57 @@
|
|
|
1
|
+
/** Media type utils */
|
|
2
|
+
export class Mime {
|
|
3
|
+
static html = Mime.#text("html");
|
|
4
|
+
static text = Mime.#text("plain");
|
|
5
|
+
static json = Mime.#application("json");
|
|
6
|
+
static stream = Mime.#application("octet-stream");
|
|
7
|
+
static #multipartBase = "multipart/";
|
|
8
|
+
static multipartFormData = Mime.#multipart("form-data");
|
|
9
|
+
static #text(type) {
|
|
10
|
+
return `text/${type}`;
|
|
11
|
+
}
|
|
12
|
+
static #application(type) {
|
|
13
|
+
return `application/${type}`;
|
|
14
|
+
}
|
|
15
|
+
static #multipart(type) {
|
|
16
|
+
return `${Mime.#multipartBase}${type}`;
|
|
17
|
+
}
|
|
18
|
+
static #markup = new Set([
|
|
19
|
+
Mime.html,
|
|
20
|
+
Mime.#text("xml"),
|
|
21
|
+
Mime.#application("xml"),
|
|
22
|
+
]);
|
|
23
|
+
/**
|
|
24
|
+
* @param mime Media type
|
|
25
|
+
* @returns `true` if the mime is markup
|
|
26
|
+
*/
|
|
27
|
+
static markup(mime) {
|
|
28
|
+
return (Mime.#markup.has(mime) ||
|
|
29
|
+
// covers other xml types like svg
|
|
30
|
+
mime.includes("+xml"));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @param mime Media type
|
|
34
|
+
* @returns `true` if the mime is multipart
|
|
35
|
+
*/
|
|
36
|
+
static multipart(mime) {
|
|
37
|
+
return mime?.startsWith(Mime.#multipartBase);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
1
40
|
/** Header parsing utils */
|
|
2
41
|
export class Header {
|
|
3
|
-
|
|
4
|
-
static
|
|
42
|
+
static type = "content-type";
|
|
43
|
+
static disposition = "content-disposition";
|
|
44
|
+
static etag = "etag";
|
|
45
|
+
static ifNoneMatch = "if-none-match";
|
|
46
|
+
static cookie = "cookie";
|
|
47
|
+
static setCookie = "set-cookie";
|
|
48
|
+
/**
|
|
49
|
+
* @param mime
|
|
50
|
+
* @returns mime; charset=utf-8
|
|
51
|
+
*/
|
|
52
|
+
static utf8(mime) {
|
|
53
|
+
return `${mime}; charset=utf-8`;
|
|
54
|
+
}
|
|
5
55
|
/**
|
|
6
56
|
* @param header header value to parse
|
|
7
57
|
* @returns Base/first param
|
|
@@ -86,3 +136,9 @@ export class Codec {
|
|
|
86
136
|
/** DO NOT USE FOR STREAMS */
|
|
87
137
|
static decode = (input) => Codec.#decoder.decode(input);
|
|
88
138
|
}
|
|
139
|
+
/** HTTP methods */
|
|
140
|
+
export class Method {
|
|
141
|
+
static get = "GET";
|
|
142
|
+
static post = "POST";
|
|
143
|
+
static head = "HEAD";
|
|
144
|
+
}
|
package/package.json
CHANGED
package/src/app/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { Middleware } from "../middleware/index.js";
|
|
|
3
3
|
import type { Multipart } from "../multipart/index.js";
|
|
4
4
|
import { Route } from "../route/index.js";
|
|
5
5
|
import { Trie } from "../trie/index.js";
|
|
6
|
-
import type
|
|
6
|
+
import { Method, type Util } from "../util/index.js";
|
|
7
7
|
|
|
8
8
|
export namespace App {
|
|
9
9
|
export namespace Options {
|
|
@@ -121,7 +121,8 @@ export class App {
|
|
|
121
121
|
// match
|
|
122
122
|
this.#trie.find(
|
|
123
123
|
// use GET for HEAD requests to find the correct route
|
|
124
|
-
(c.req.method ===
|
|
124
|
+
(c.req.method === Method.head ? Method.get : c.req.method) +
|
|
125
|
+
c.url.pathname,
|
|
125
126
|
),
|
|
126
127
|
),
|
|
127
128
|
this.#global.concat(c.route?.middleware ?? []),
|
|
@@ -131,8 +132,8 @@ export class App {
|
|
|
131
132
|
/** Basic CSRF middleware */
|
|
132
133
|
static #csrf(c: Middleware.Context, next: Middleware.Next) {
|
|
133
134
|
if (
|
|
134
|
-
c.req.method ===
|
|
135
|
-
c.req.method ===
|
|
135
|
+
c.req.method === Method.get ||
|
|
136
|
+
c.req.method === Method.head ||
|
|
136
137
|
c.req.headers.get("sec-fetch-site") === "same-origin" ||
|
|
137
138
|
c.req.headers.get("origin") === c.url.origin
|
|
138
139
|
) {
|
package/src/context/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { Multipart } from "../multipart/index.js";
|
|
|
5
5
|
import { Render } from "../render/index.js";
|
|
6
6
|
import { Route } from "../route/index.js";
|
|
7
7
|
import { type Trie } from "../trie/index.js";
|
|
8
|
-
import { Hash, Header } from "../util/index.js";
|
|
8
|
+
import { Hash, Header, Method, Mime } from "../util/index.js";
|
|
9
9
|
|
|
10
10
|
/** Properties to build the final `Response` with once middleware has run. */
|
|
11
11
|
class PreparedResponse {
|
|
@@ -71,17 +71,12 @@ export class Context<Params extends Trie.Params = Trie.Params> {
|
|
|
71
71
|
/** Forwarded app options */
|
|
72
72
|
readonly #options: App.Options;
|
|
73
73
|
|
|
74
|
-
// for reuse across methods
|
|
75
|
-
static readonly #textHtml = "text/html";
|
|
76
|
-
static readonly #utf8 = "charset=utf-8";
|
|
77
|
-
static readonly #htmlType = `${Context.#textHtml}; ${Context.#utf8}`;
|
|
78
|
-
|
|
79
74
|
/**
|
|
80
75
|
* Creates a new `Context` with the current `Request`.
|
|
81
76
|
*
|
|
82
77
|
* @param req Request
|
|
83
78
|
*/
|
|
84
|
-
constructor(req: Request, options: App.Options
|
|
79
|
+
constructor(req: Request, options: App.Options) {
|
|
85
80
|
this.req = req;
|
|
86
81
|
this.url = new URL(req.url);
|
|
87
82
|
this.#options = options;
|
|
@@ -96,7 +91,7 @@ export class Context<Params extends Trie.Params = Trie.Params> {
|
|
|
96
91
|
html(body: BodyInit | null, status?: number) {
|
|
97
92
|
this.res.body = body;
|
|
98
93
|
this.res.status = status;
|
|
99
|
-
this.res.headers.set(Header.
|
|
94
|
+
this.res.headers.set(Header.type, Header.utf8(Mime.html));
|
|
100
95
|
}
|
|
101
96
|
|
|
102
97
|
/**
|
|
@@ -105,22 +100,22 @@ export class Context<Params extends Trie.Params = Trie.Params> {
|
|
|
105
100
|
* @param data passed into JSON.stringify to create the body
|
|
106
101
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
107
102
|
*/
|
|
108
|
-
json(data:
|
|
103
|
+
json<D>(data: D extends bigint ? never : D, status?: number) {
|
|
109
104
|
this.res.body = JSON.stringify(data);
|
|
110
105
|
this.res.status = status;
|
|
111
|
-
this.res.headers.set(Header.
|
|
106
|
+
this.res.headers.set(Header.type, Mime.json);
|
|
112
107
|
}
|
|
113
108
|
|
|
114
109
|
/**
|
|
115
110
|
* Creates a plain text response.
|
|
116
111
|
*
|
|
117
|
-
* @param body
|
|
112
|
+
* @param body text body
|
|
118
113
|
* @param status [HTTP response status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
|
|
119
114
|
*/
|
|
120
|
-
text(body: BodyInit, status?: number) {
|
|
115
|
+
text(body: BodyInit | null, status?: number) {
|
|
121
116
|
this.res.body = body;
|
|
122
117
|
this.res.status = status;
|
|
123
|
-
this.res.headers.set(Header.
|
|
118
|
+
this.res.headers.set(Header.type, Header.utf8(Mime.text));
|
|
124
119
|
}
|
|
125
120
|
|
|
126
121
|
/**
|
|
@@ -153,9 +148,9 @@ export class Context<Params extends Trie.Params = Trie.Params> {
|
|
|
153
148
|
etag(string: string) {
|
|
154
149
|
const etag = `"${Hash.djb2(string)}"`;
|
|
155
150
|
|
|
156
|
-
this.res.headers.set(
|
|
151
|
+
this.res.headers.set(Header.etag, etag);
|
|
157
152
|
|
|
158
|
-
if (this.req.headers.get(
|
|
153
|
+
if (this.req.headers.get(Header.ifNoneMatch) === etag) {
|
|
159
154
|
this.res.body = null;
|
|
160
155
|
this.res.status = 304;
|
|
161
156
|
|
|
@@ -220,16 +215,16 @@ export class Context<Params extends Trie.Params = Trie.Params> {
|
|
|
220
215
|
}
|
|
221
216
|
} else if (r !== undefined) {
|
|
222
217
|
// something to stream
|
|
223
|
-
const
|
|
218
|
+
const [mime] = Header.shift(this.res.headers.get(Header.type));
|
|
224
219
|
|
|
225
220
|
this.res.body = Render.stream(r, {
|
|
226
221
|
// other defined types are safe
|
|
227
|
-
safe: Boolean(
|
|
222
|
+
safe: Boolean(mime && !Mime.markup(mime)),
|
|
228
223
|
});
|
|
229
224
|
|
|
230
|
-
if (!
|
|
225
|
+
if (!mime) {
|
|
231
226
|
// default to HTML
|
|
232
|
-
this.res.headers.set(Header.
|
|
227
|
+
this.res.headers.set(Header.type, Header.utf8(Mime.html));
|
|
233
228
|
}
|
|
234
229
|
|
|
235
230
|
// do not overwrite/remove status - that way user can set it before returning
|
|
@@ -246,17 +241,19 @@ export class Context<Params extends Trie.Params = Trie.Params> {
|
|
|
246
241
|
static async compose(c: Context, middleware: Middleware[]) {
|
|
247
242
|
await c.#run(middleware);
|
|
248
243
|
|
|
249
|
-
if (c.req.method ===
|
|
244
|
+
if (c.req.method === Method.head) {
|
|
250
245
|
if (c.res.body instanceof ReadableStream) {
|
|
251
246
|
// cancel unused stream to prevent leaks
|
|
252
|
-
await c.res.body.cancel(
|
|
247
|
+
await c.res.body.cancel(Method.head);
|
|
253
248
|
}
|
|
254
249
|
|
|
255
250
|
c.res.body = null;
|
|
256
251
|
}
|
|
257
252
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
253
|
+
return new Response(
|
|
254
|
+
c.res.body,
|
|
255
|
+
// prevents users from setting the response during the stream
|
|
256
|
+
Object.freeze(c.res),
|
|
257
|
+
);
|
|
261
258
|
}
|
|
262
259
|
}
|
package/src/cookie/index.ts
CHANGED
|
@@ -146,9 +146,9 @@ export class Cookie {
|
|
|
146
146
|
* @returns Value of the cookie, or `undefined` if not found
|
|
147
147
|
*/
|
|
148
148
|
get(name: string) {
|
|
149
|
-
return (this.#parsed ??= Header.params(
|
|
150
|
-
|
|
151
|
-
];
|
|
149
|
+
return (this.#parsed ??= Header.params(
|
|
150
|
+
this.#c.req.headers.get(Header.cookie),
|
|
151
|
+
))[name];
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
/**
|
|
@@ -184,6 +184,6 @@ export class Cookie {
|
|
|
184
184
|
if (options.expires)
|
|
185
185
|
cookie.push(`Expires=${options.expires.toUTCString()}`);
|
|
186
186
|
|
|
187
|
-
this.#c.res.headers.append(
|
|
187
|
+
this.#c.res.headers.append(Header.setCookie, cookie.join("; "));
|
|
188
188
|
}
|
|
189
189
|
}
|