h3 0.3.5 → 0.3.9
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/index.cjs +42 -39
- package/dist/index.d.ts +10 -4
- package/dist/index.mjs +42 -39
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -195,29 +195,61 @@ function destr(val) {
|
|
|
195
195
|
}
|
|
196
196
|
}
|
|
197
197
|
|
|
198
|
+
function useQuery(req) {
|
|
199
|
+
return getQuery(req.url || "");
|
|
200
|
+
}
|
|
201
|
+
function useMethod(req, defaultMethod = "GET") {
|
|
202
|
+
return (req.method || defaultMethod).toUpperCase();
|
|
203
|
+
}
|
|
204
|
+
function isMethod(req, expected, allowHead) {
|
|
205
|
+
const method = useMethod(req);
|
|
206
|
+
if (allowHead && method === "HEAD") {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
if (typeof expected === "string") {
|
|
210
|
+
if (method === expected) {
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
} else if (expected.includes(method)) {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
function assertMethod(req, expected, allowHead) {
|
|
219
|
+
if (!isMethod(req, expected, allowHead)) {
|
|
220
|
+
throw createError({
|
|
221
|
+
statusCode: 405,
|
|
222
|
+
statusMessage: "HTTP method is not allowed."
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
198
227
|
const RawBodySymbol = Symbol("h3RawBody");
|
|
199
228
|
const ParsedBodySymbol = Symbol("h3RawBody");
|
|
229
|
+
const PayloadMethods = ["PATCH", "POST", "PUT", "DELETE"];
|
|
200
230
|
function useRawBody(req, encoding = "utf-8") {
|
|
201
|
-
|
|
202
|
-
|
|
231
|
+
assertMethod(req, PayloadMethods);
|
|
232
|
+
if (RawBodySymbol in req) {
|
|
233
|
+
const promise2 = Promise.resolve(req[RawBodySymbol]);
|
|
234
|
+
return encoding ? promise2.then((buff) => buff.toString(encoding)) : promise2;
|
|
203
235
|
}
|
|
204
|
-
if (req
|
|
205
|
-
return Promise.resolve(req.
|
|
236
|
+
if ("body" in req) {
|
|
237
|
+
return Promise.resolve(req.body);
|
|
206
238
|
}
|
|
207
|
-
|
|
239
|
+
const promise = req[RawBodySymbol] = new Promise((resolve, reject) => {
|
|
208
240
|
const bodyData = [];
|
|
209
241
|
req.on("error", (err) => {
|
|
210
242
|
reject(err);
|
|
211
243
|
}).on("data", (chunk) => {
|
|
212
244
|
bodyData.push(chunk);
|
|
213
245
|
}).on("end", () => {
|
|
214
|
-
|
|
215
|
-
resolve(encoding ? req[RawBodySymbol].toString(encoding) : req[RawBodySymbol]);
|
|
246
|
+
resolve(Buffer.concat(bodyData));
|
|
216
247
|
});
|
|
217
248
|
});
|
|
249
|
+
return encoding ? promise.then((buff) => buff.toString(encoding)) : promise;
|
|
218
250
|
}
|
|
219
251
|
async function useBody(req) {
|
|
220
|
-
if (req
|
|
252
|
+
if (ParsedBodySymbol in req) {
|
|
221
253
|
return req[ParsedBodySymbol];
|
|
222
254
|
}
|
|
223
255
|
const body = await useRawBody(req);
|
|
@@ -473,35 +505,6 @@ function setCookie(res, name, value, serializeOptions) {
|
|
|
473
505
|
appendHeader(res, "Set-Cookie", cookieStr);
|
|
474
506
|
}
|
|
475
507
|
|
|
476
|
-
function useQuery(req) {
|
|
477
|
-
return getQuery(req.url || "");
|
|
478
|
-
}
|
|
479
|
-
function useMethod(req, defaultMethod = "GET") {
|
|
480
|
-
return (req.method || defaultMethod).toUpperCase();
|
|
481
|
-
}
|
|
482
|
-
function isMethod(req, expected, allowHead) {
|
|
483
|
-
const method = useMethod(req);
|
|
484
|
-
if (allowHead && method === "HEAD") {
|
|
485
|
-
return true;
|
|
486
|
-
}
|
|
487
|
-
if (typeof expected === "string") {
|
|
488
|
-
if (method === expected) {
|
|
489
|
-
return true;
|
|
490
|
-
}
|
|
491
|
-
} else if (expected.includes(method)) {
|
|
492
|
-
return true;
|
|
493
|
-
}
|
|
494
|
-
return false;
|
|
495
|
-
}
|
|
496
|
-
function assertMethod(req, expected, allowHead) {
|
|
497
|
-
if (!isMethod(req, expected, allowHead)) {
|
|
498
|
-
throw createError({
|
|
499
|
-
statusCode: 405,
|
|
500
|
-
statusMessage: "HTTP method is not allowed."
|
|
501
|
-
});
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
508
|
class H3Error extends Error {
|
|
506
509
|
constructor() {
|
|
507
510
|
super(...arguments);
|
|
@@ -592,7 +595,7 @@ function createHandle(stack, options) {
|
|
|
592
595
|
if (!reqUrl.startsWith(layer.route)) {
|
|
593
596
|
continue;
|
|
594
597
|
}
|
|
595
|
-
req.url = reqUrl.
|
|
598
|
+
req.url = reqUrl.slice(layer.route.length) || "/";
|
|
596
599
|
} else {
|
|
597
600
|
req.url = reqUrl;
|
|
598
601
|
}
|
|
@@ -626,7 +629,7 @@ function normalizeLayer(layer) {
|
|
|
626
629
|
layer.promisify = layer.handle.length > 2;
|
|
627
630
|
}
|
|
628
631
|
return {
|
|
629
|
-
route: withoutTrailingSlash(layer.route)
|
|
632
|
+
route: withoutTrailingSlash(layer.route),
|
|
630
633
|
match: layer.match,
|
|
631
634
|
handle: layer.lazy ? lazyHandle(layer.handle, layer.promisify) : layer.promisify ? promisifyHandle(layer.handle) : layer.handle
|
|
632
635
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -73,8 +73,8 @@ declare class H3Error extends Error {
|
|
|
73
73
|
*/
|
|
74
74
|
declare function createError(input: Partial<H3Error>): H3Error;
|
|
75
75
|
/**
|
|
76
|
-
*
|
|
77
|
-
* H3 internally uses this
|
|
76
|
+
* Receive an error and return the corresponding response.<br>
|
|
77
|
+
* H3 internally uses this function to handle unhandled errors.<br>
|
|
78
78
|
* Note that calling this function will close the connection and no other data will be sent to client afterwards.
|
|
79
79
|
*
|
|
80
80
|
* @param res {ServerResponse} The ServerResponse object is passed as the second parameter in the handler function
|
|
@@ -84,6 +84,12 @@ declare function createError(input: Partial<H3Error>): H3Error;
|
|
|
84
84
|
*/
|
|
85
85
|
declare function sendError(res: ServerResponse, error: Error | H3Error, debug?: boolean): void;
|
|
86
86
|
|
|
87
|
+
declare const RawBodySymbol: unique symbol;
|
|
88
|
+
interface _IncomingMessage extends IncomingMessage {
|
|
89
|
+
[RawBodySymbol]?: Promise<Buffer>;
|
|
90
|
+
ParsedBodySymbol?: any;
|
|
91
|
+
body?: any;
|
|
92
|
+
}
|
|
87
93
|
/**
|
|
88
94
|
* Reads body of the request and returns encoded raw string (default) or `Buffer` if encoding if falsy.
|
|
89
95
|
* @param req {IncomingMessage} An IncomingMessage object is created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
|
|
@@ -91,7 +97,7 @@ declare function sendError(res: ServerResponse, error: Error | H3Error, debug?:
|
|
|
91
97
|
*
|
|
92
98
|
* @return {String|Buffer} Encoded raw string or raw Buffer of the body
|
|
93
99
|
*/
|
|
94
|
-
declare function useRawBody(req:
|
|
100
|
+
declare function useRawBody(req: _IncomingMessage, encoding?: Encoding): Encoding extends false ? Buffer : Promise<string>;
|
|
95
101
|
/**
|
|
96
102
|
* Reads request body and try to safely parse using [destr](https://github.com/unjs/destr)
|
|
97
103
|
* @param req {IncomingMessage} An IncomingMessage object created by [http.Server](https://nodejs.org/api/http.html#http_class_http_server)
|
|
@@ -103,7 +109,7 @@ declare function useRawBody(req: IncomingMessage, encoding?: Encoding): Encoding
|
|
|
103
109
|
* const body = await useBody(req)
|
|
104
110
|
* ```
|
|
105
111
|
*/
|
|
106
|
-
declare function useBody<T = any>(req:
|
|
112
|
+
declare function useBody<T = any>(req: _IncomingMessage): Promise<T>;
|
|
107
113
|
|
|
108
114
|
declare const MIMES: {
|
|
109
115
|
html: string;
|
package/dist/index.mjs
CHANGED
|
@@ -191,29 +191,61 @@ function destr(val) {
|
|
|
191
191
|
}
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
function useQuery(req) {
|
|
195
|
+
return getQuery(req.url || "");
|
|
196
|
+
}
|
|
197
|
+
function useMethod(req, defaultMethod = "GET") {
|
|
198
|
+
return (req.method || defaultMethod).toUpperCase();
|
|
199
|
+
}
|
|
200
|
+
function isMethod(req, expected, allowHead) {
|
|
201
|
+
const method = useMethod(req);
|
|
202
|
+
if (allowHead && method === "HEAD") {
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
if (typeof expected === "string") {
|
|
206
|
+
if (method === expected) {
|
|
207
|
+
return true;
|
|
208
|
+
}
|
|
209
|
+
} else if (expected.includes(method)) {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
function assertMethod(req, expected, allowHead) {
|
|
215
|
+
if (!isMethod(req, expected, allowHead)) {
|
|
216
|
+
throw createError({
|
|
217
|
+
statusCode: 405,
|
|
218
|
+
statusMessage: "HTTP method is not allowed."
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
194
223
|
const RawBodySymbol = Symbol("h3RawBody");
|
|
195
224
|
const ParsedBodySymbol = Symbol("h3RawBody");
|
|
225
|
+
const PayloadMethods = ["PATCH", "POST", "PUT", "DELETE"];
|
|
196
226
|
function useRawBody(req, encoding = "utf-8") {
|
|
197
|
-
|
|
198
|
-
|
|
227
|
+
assertMethod(req, PayloadMethods);
|
|
228
|
+
if (RawBodySymbol in req) {
|
|
229
|
+
const promise2 = Promise.resolve(req[RawBodySymbol]);
|
|
230
|
+
return encoding ? promise2.then((buff) => buff.toString(encoding)) : promise2;
|
|
199
231
|
}
|
|
200
|
-
if (req
|
|
201
|
-
return Promise.resolve(req.
|
|
232
|
+
if ("body" in req) {
|
|
233
|
+
return Promise.resolve(req.body);
|
|
202
234
|
}
|
|
203
|
-
|
|
235
|
+
const promise = req[RawBodySymbol] = new Promise((resolve, reject) => {
|
|
204
236
|
const bodyData = [];
|
|
205
237
|
req.on("error", (err) => {
|
|
206
238
|
reject(err);
|
|
207
239
|
}).on("data", (chunk) => {
|
|
208
240
|
bodyData.push(chunk);
|
|
209
241
|
}).on("end", () => {
|
|
210
|
-
|
|
211
|
-
resolve(encoding ? req[RawBodySymbol].toString(encoding) : req[RawBodySymbol]);
|
|
242
|
+
resolve(Buffer.concat(bodyData));
|
|
212
243
|
});
|
|
213
244
|
});
|
|
245
|
+
return encoding ? promise.then((buff) => buff.toString(encoding)) : promise;
|
|
214
246
|
}
|
|
215
247
|
async function useBody(req) {
|
|
216
|
-
if (req
|
|
248
|
+
if (ParsedBodySymbol in req) {
|
|
217
249
|
return req[ParsedBodySymbol];
|
|
218
250
|
}
|
|
219
251
|
const body = await useRawBody(req);
|
|
@@ -469,35 +501,6 @@ function setCookie(res, name, value, serializeOptions) {
|
|
|
469
501
|
appendHeader(res, "Set-Cookie", cookieStr);
|
|
470
502
|
}
|
|
471
503
|
|
|
472
|
-
function useQuery(req) {
|
|
473
|
-
return getQuery(req.url || "");
|
|
474
|
-
}
|
|
475
|
-
function useMethod(req, defaultMethod = "GET") {
|
|
476
|
-
return (req.method || defaultMethod).toUpperCase();
|
|
477
|
-
}
|
|
478
|
-
function isMethod(req, expected, allowHead) {
|
|
479
|
-
const method = useMethod(req);
|
|
480
|
-
if (allowHead && method === "HEAD") {
|
|
481
|
-
return true;
|
|
482
|
-
}
|
|
483
|
-
if (typeof expected === "string") {
|
|
484
|
-
if (method === expected) {
|
|
485
|
-
return true;
|
|
486
|
-
}
|
|
487
|
-
} else if (expected.includes(method)) {
|
|
488
|
-
return true;
|
|
489
|
-
}
|
|
490
|
-
return false;
|
|
491
|
-
}
|
|
492
|
-
function assertMethod(req, expected, allowHead) {
|
|
493
|
-
if (!isMethod(req, expected, allowHead)) {
|
|
494
|
-
throw createError({
|
|
495
|
-
statusCode: 405,
|
|
496
|
-
statusMessage: "HTTP method is not allowed."
|
|
497
|
-
});
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
504
|
class H3Error extends Error {
|
|
502
505
|
constructor() {
|
|
503
506
|
super(...arguments);
|
|
@@ -588,7 +591,7 @@ function createHandle(stack, options) {
|
|
|
588
591
|
if (!reqUrl.startsWith(layer.route)) {
|
|
589
592
|
continue;
|
|
590
593
|
}
|
|
591
|
-
req.url = reqUrl.
|
|
594
|
+
req.url = reqUrl.slice(layer.route.length) || "/";
|
|
592
595
|
} else {
|
|
593
596
|
req.url = reqUrl;
|
|
594
597
|
}
|
|
@@ -622,7 +625,7 @@ function normalizeLayer(layer) {
|
|
|
622
625
|
layer.promisify = layer.handle.length > 2;
|
|
623
626
|
}
|
|
624
627
|
return {
|
|
625
|
-
route: withoutTrailingSlash(layer.route)
|
|
628
|
+
route: withoutTrailingSlash(layer.route),
|
|
626
629
|
match: layer.match,
|
|
627
630
|
handle: layer.lazy ? lazyHandle(layer.handle, layer.promisify) : layer.promisify ? promisifyHandle(layer.handle) : layer.handle
|
|
628
631
|
};
|