h3 0.8.2 → 0.8.4
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/README.md +1 -1
- package/dist/index.cjs +13 -10
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +13 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -101,7 +101,7 @@ app.use('/1', eventHandler(() => '<h1>Hello world!</h1>'))
|
|
|
101
101
|
.use('/2', eventHandler(() => '<h1>Goodbye!</h1>'))
|
|
102
102
|
|
|
103
103
|
// Legacy middleware with 3rd argument are automatically promisified
|
|
104
|
-
app.use(fromNodeMiddleware((req, res, next) => { req.setHeader('
|
|
104
|
+
app.use(fromNodeMiddleware((req, res, next) => { req.setHeader('x-foo', 'bar'); next() }))
|
|
105
105
|
|
|
106
106
|
// Lazy loaded routes using { lazy: true }
|
|
107
107
|
app.use('/big', () => import('./big-handler'), { lazy: true })
|
package/dist/index.cjs
CHANGED
|
@@ -88,7 +88,7 @@ function sendError(event, error, debug) {
|
|
|
88
88
|
if (h3Error.statusMessage) {
|
|
89
89
|
event.res.statusMessage = h3Error.statusMessage;
|
|
90
90
|
}
|
|
91
|
-
event.res.setHeader("
|
|
91
|
+
event.res.setHeader("content-type", MIMES.json);
|
|
92
92
|
event.res.end(JSON.stringify(responseBody, null, 2));
|
|
93
93
|
}
|
|
94
94
|
function isError(input) {
|
|
@@ -155,6 +155,9 @@ function readRawBody(event, encoding = "utf-8") {
|
|
|
155
155
|
if ("body" in event.req) {
|
|
156
156
|
return Promise.resolve(event.req.body);
|
|
157
157
|
}
|
|
158
|
+
if (!parseInt(event.req.headers["content-length"] || "")) {
|
|
159
|
+
return Promise.resolve(void 0);
|
|
160
|
+
}
|
|
158
161
|
const promise = event.req[RawBodySymbol] = new Promise((resolve, reject) => {
|
|
159
162
|
const bodyData = [];
|
|
160
163
|
event.req.on("error", (err) => {
|
|
@@ -192,7 +195,7 @@ function handleCacheHeaders(event, opts) {
|
|
|
192
195
|
if (opts.modifiedTime) {
|
|
193
196
|
const modifiedTime = new Date(opts.modifiedTime);
|
|
194
197
|
const ifModifiedSince = event.req.headers["if-modified-since"];
|
|
195
|
-
event.res.setHeader("
|
|
198
|
+
event.res.setHeader("last-modified", modifiedTime.toUTCString());
|
|
196
199
|
if (ifModifiedSince) {
|
|
197
200
|
if (new Date(ifModifiedSince) >= opts.modifiedTime) {
|
|
198
201
|
cacheMatched = true;
|
|
@@ -200,16 +203,16 @@ function handleCacheHeaders(event, opts) {
|
|
|
200
203
|
}
|
|
201
204
|
}
|
|
202
205
|
if (opts.etag) {
|
|
203
|
-
event.res.setHeader("
|
|
206
|
+
event.res.setHeader("etag", opts.etag);
|
|
204
207
|
const ifNonMatch = event.req.headers["if-none-match"];
|
|
205
208
|
if (ifNonMatch === opts.etag) {
|
|
206
209
|
cacheMatched = true;
|
|
207
210
|
}
|
|
208
211
|
}
|
|
209
|
-
event.res.setHeader("
|
|
212
|
+
event.res.setHeader("cache-control", cacheControls.join(", "));
|
|
210
213
|
if (cacheMatched) {
|
|
211
214
|
event.res.statusCode = 304;
|
|
212
|
-
event.res.end(
|
|
215
|
+
event.res.end();
|
|
213
216
|
return true;
|
|
214
217
|
}
|
|
215
218
|
return false;
|
|
@@ -233,13 +236,13 @@ function send(event, data, type) {
|
|
|
233
236
|
});
|
|
234
237
|
}
|
|
235
238
|
function defaultContentType(event, type) {
|
|
236
|
-
if (type && !event.res.getHeader("
|
|
237
|
-
event.res.setHeader("
|
|
239
|
+
if (type && !event.res.getHeader("content-type")) {
|
|
240
|
+
event.res.setHeader("content-type", type);
|
|
238
241
|
}
|
|
239
242
|
}
|
|
240
243
|
function sendRedirect(event, location, code = 302) {
|
|
241
244
|
event.res.statusCode = code;
|
|
242
|
-
event.res.setHeader("
|
|
245
|
+
event.res.setHeader("location", location);
|
|
243
246
|
const encodedLoc = location.replace(/"/g, "%22");
|
|
244
247
|
const html = `<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=${encodedLoc}"></head></html>`;
|
|
245
248
|
return send(event, html, MIMES.html);
|
|
@@ -309,7 +312,7 @@ function writeEarlyHints(event, hints, cb = noop) {
|
|
|
309
312
|
let hint = "HTTP/1.1 103 Early Hints";
|
|
310
313
|
if (hints.link) {
|
|
311
314
|
hint += `\r
|
|
312
|
-
Link: ${hints.link.join("
|
|
315
|
+
Link: ${hints.link.join(", ")}`;
|
|
313
316
|
}
|
|
314
317
|
for (const [header, value] of headers) {
|
|
315
318
|
if (header === "link") {
|
|
@@ -438,7 +441,7 @@ class H3Event {
|
|
|
438
441
|
this.res.statusMessage = response.statusText;
|
|
439
442
|
}
|
|
440
443
|
if (response.redirected) {
|
|
441
|
-
this.res.setHeader("
|
|
444
|
+
this.res.setHeader("location", response.url);
|
|
442
445
|
}
|
|
443
446
|
if (!response._body) {
|
|
444
447
|
return this.res.end();
|
package/dist/index.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ declare type LazyEventHandler = () => EventHandler | Promise<EventHandler>;
|
|
|
16
16
|
|
|
17
17
|
declare type NodeListener = (req: IncomingMessage, res: ServerResponse) => void;
|
|
18
18
|
declare type NodePromisifiedHandler = (req: IncomingMessage, res: ServerResponse) => Promise<any>;
|
|
19
|
-
declare type NodeMiddleware = (req: IncomingMessage, res: ServerResponse, next
|
|
19
|
+
declare type NodeMiddleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any;
|
|
20
20
|
declare const defineNodeListener: (handler: NodeListener) => NodeListener;
|
|
21
21
|
declare const defineNodeMiddleware: (middleware: NodeMiddleware) => NodeMiddleware;
|
|
22
22
|
declare function fromNodeMiddleware(handler: NodeListener | NodeMiddleware): EventHandler;
|
|
@@ -159,7 +159,7 @@ declare function useBase(base: string, handler: EventHandler): EventHandler;
|
|
|
159
159
|
*
|
|
160
160
|
* @return {String|Buffer} Encoded raw string or raw Buffer of the body
|
|
161
161
|
*/
|
|
162
|
-
declare function readRawBody(event: H3Event, encoding?: Encoding): Encoding extends false ? Buffer : Promise<string | Buffer>;
|
|
162
|
+
declare function readRawBody(event: H3Event, encoding?: Encoding): Encoding extends false ? Buffer : Promise<string | Buffer | undefined>;
|
|
163
163
|
/** @deprecated Use `h3.readRawBody` */
|
|
164
164
|
declare const useRawBody: typeof readRawBody;
|
|
165
165
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -86,7 +86,7 @@ function sendError(event, error, debug) {
|
|
|
86
86
|
if (h3Error.statusMessage) {
|
|
87
87
|
event.res.statusMessage = h3Error.statusMessage;
|
|
88
88
|
}
|
|
89
|
-
event.res.setHeader("
|
|
89
|
+
event.res.setHeader("content-type", MIMES.json);
|
|
90
90
|
event.res.end(JSON.stringify(responseBody, null, 2));
|
|
91
91
|
}
|
|
92
92
|
function isError(input) {
|
|
@@ -153,6 +153,9 @@ function readRawBody(event, encoding = "utf-8") {
|
|
|
153
153
|
if ("body" in event.req) {
|
|
154
154
|
return Promise.resolve(event.req.body);
|
|
155
155
|
}
|
|
156
|
+
if (!parseInt(event.req.headers["content-length"] || "")) {
|
|
157
|
+
return Promise.resolve(void 0);
|
|
158
|
+
}
|
|
156
159
|
const promise = event.req[RawBodySymbol] = new Promise((resolve, reject) => {
|
|
157
160
|
const bodyData = [];
|
|
158
161
|
event.req.on("error", (err) => {
|
|
@@ -190,7 +193,7 @@ function handleCacheHeaders(event, opts) {
|
|
|
190
193
|
if (opts.modifiedTime) {
|
|
191
194
|
const modifiedTime = new Date(opts.modifiedTime);
|
|
192
195
|
const ifModifiedSince = event.req.headers["if-modified-since"];
|
|
193
|
-
event.res.setHeader("
|
|
196
|
+
event.res.setHeader("last-modified", modifiedTime.toUTCString());
|
|
194
197
|
if (ifModifiedSince) {
|
|
195
198
|
if (new Date(ifModifiedSince) >= opts.modifiedTime) {
|
|
196
199
|
cacheMatched = true;
|
|
@@ -198,16 +201,16 @@ function handleCacheHeaders(event, opts) {
|
|
|
198
201
|
}
|
|
199
202
|
}
|
|
200
203
|
if (opts.etag) {
|
|
201
|
-
event.res.setHeader("
|
|
204
|
+
event.res.setHeader("etag", opts.etag);
|
|
202
205
|
const ifNonMatch = event.req.headers["if-none-match"];
|
|
203
206
|
if (ifNonMatch === opts.etag) {
|
|
204
207
|
cacheMatched = true;
|
|
205
208
|
}
|
|
206
209
|
}
|
|
207
|
-
event.res.setHeader("
|
|
210
|
+
event.res.setHeader("cache-control", cacheControls.join(", "));
|
|
208
211
|
if (cacheMatched) {
|
|
209
212
|
event.res.statusCode = 304;
|
|
210
|
-
event.res.end(
|
|
213
|
+
event.res.end();
|
|
211
214
|
return true;
|
|
212
215
|
}
|
|
213
216
|
return false;
|
|
@@ -231,13 +234,13 @@ function send(event, data, type) {
|
|
|
231
234
|
});
|
|
232
235
|
}
|
|
233
236
|
function defaultContentType(event, type) {
|
|
234
|
-
if (type && !event.res.getHeader("
|
|
235
|
-
event.res.setHeader("
|
|
237
|
+
if (type && !event.res.getHeader("content-type")) {
|
|
238
|
+
event.res.setHeader("content-type", type);
|
|
236
239
|
}
|
|
237
240
|
}
|
|
238
241
|
function sendRedirect(event, location, code = 302) {
|
|
239
242
|
event.res.statusCode = code;
|
|
240
|
-
event.res.setHeader("
|
|
243
|
+
event.res.setHeader("location", location);
|
|
241
244
|
const encodedLoc = location.replace(/"/g, "%22");
|
|
242
245
|
const html = `<!DOCTYPE html><html><head><meta http-equiv="refresh" content="0; url=${encodedLoc}"></head></html>`;
|
|
243
246
|
return send(event, html, MIMES.html);
|
|
@@ -307,7 +310,7 @@ function writeEarlyHints(event, hints, cb = noop) {
|
|
|
307
310
|
let hint = "HTTP/1.1 103 Early Hints";
|
|
308
311
|
if (hints.link) {
|
|
309
312
|
hint += `\r
|
|
310
|
-
Link: ${hints.link.join("
|
|
313
|
+
Link: ${hints.link.join(", ")}`;
|
|
311
314
|
}
|
|
312
315
|
for (const [header, value] of headers) {
|
|
313
316
|
if (header === "link") {
|
|
@@ -436,7 +439,7 @@ class H3Event {
|
|
|
436
439
|
this.res.statusMessage = response.statusText;
|
|
437
440
|
}
|
|
438
441
|
if (response.redirected) {
|
|
439
|
-
this.res.setHeader("
|
|
442
|
+
this.res.setHeader("location", response.url);
|
|
440
443
|
}
|
|
441
444
|
if (!response._body) {
|
|
442
445
|
return this.res.end();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "h3",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"description": "Tiny JavaScript Server",
|
|
5
5
|
"repository": "unjs/h3",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"unbuild": "^0.9.4",
|
|
46
46
|
"vitest": "^0.24.3"
|
|
47
47
|
},
|
|
48
|
-
"packageManager": "pnpm@7.13.
|
|
48
|
+
"packageManager": "pnpm@7.13.5",
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "unbuild",
|
|
51
51
|
"dev": "vitest",
|