h3 0.3.4 → 0.3.8
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 +41 -35
- package/dist/index.d.ts +8 -2
- package/dist/index.mjs +41 -35
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -195,26 +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
|
-
|
|
236
|
+
if ("body" in req) {
|
|
237
|
+
return Promise.resolve(req.body);
|
|
238
|
+
}
|
|
239
|
+
const promise = req[RawBodySymbol] = new Promise((resolve, reject) => {
|
|
205
240
|
const bodyData = [];
|
|
206
241
|
req.on("error", (err) => {
|
|
207
242
|
reject(err);
|
|
208
243
|
}).on("data", (chunk) => {
|
|
209
244
|
bodyData.push(chunk);
|
|
210
245
|
}).on("end", () => {
|
|
211
|
-
|
|
212
|
-
resolve(encoding ? req[RawBodySymbol].toString(encoding) : req[RawBodySymbol]);
|
|
246
|
+
resolve(Buffer.concat(bodyData));
|
|
213
247
|
});
|
|
214
248
|
});
|
|
249
|
+
return encoding ? promise.then((buff) => buff.toString(encoding)) : promise;
|
|
215
250
|
}
|
|
216
251
|
async function useBody(req) {
|
|
217
|
-
if (req
|
|
252
|
+
if (ParsedBodySymbol in req) {
|
|
218
253
|
return req[ParsedBodySymbol];
|
|
219
254
|
}
|
|
220
255
|
const body = await useRawBody(req);
|
|
@@ -470,35 +505,6 @@ function setCookie(res, name, value, serializeOptions) {
|
|
|
470
505
|
appendHeader(res, "Set-Cookie", cookieStr);
|
|
471
506
|
}
|
|
472
507
|
|
|
473
|
-
function useQuery(req) {
|
|
474
|
-
return getQuery(req.url || "");
|
|
475
|
-
}
|
|
476
|
-
function useMethod(req, defaultMethod = "GET") {
|
|
477
|
-
return (req.method || defaultMethod).toUpperCase();
|
|
478
|
-
}
|
|
479
|
-
function isMethod(req, expected, allowHead) {
|
|
480
|
-
const method = useMethod(req);
|
|
481
|
-
if (allowHead && method === "HEAD") {
|
|
482
|
-
return true;
|
|
483
|
-
}
|
|
484
|
-
if (typeof expected === "string") {
|
|
485
|
-
if (method === expected) {
|
|
486
|
-
return true;
|
|
487
|
-
}
|
|
488
|
-
} else if (expected.includes(method)) {
|
|
489
|
-
return true;
|
|
490
|
-
}
|
|
491
|
-
return false;
|
|
492
|
-
}
|
|
493
|
-
function assertMethod(req, expected, allowHead) {
|
|
494
|
-
if (!isMethod(req, expected, allowHead)) {
|
|
495
|
-
throw createError({
|
|
496
|
-
statusCode: 405,
|
|
497
|
-
statusMessage: "HTTP method is not allowed."
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
508
|
class H3Error extends Error {
|
|
503
509
|
constructor() {
|
|
504
510
|
super(...arguments);
|
package/dist/index.d.ts
CHANGED
|
@@ -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,26 +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
|
-
|
|
232
|
+
if ("body" in req) {
|
|
233
|
+
return Promise.resolve(req.body);
|
|
234
|
+
}
|
|
235
|
+
const promise = req[RawBodySymbol] = new Promise((resolve, reject) => {
|
|
201
236
|
const bodyData = [];
|
|
202
237
|
req.on("error", (err) => {
|
|
203
238
|
reject(err);
|
|
204
239
|
}).on("data", (chunk) => {
|
|
205
240
|
bodyData.push(chunk);
|
|
206
241
|
}).on("end", () => {
|
|
207
|
-
|
|
208
|
-
resolve(encoding ? req[RawBodySymbol].toString(encoding) : req[RawBodySymbol]);
|
|
242
|
+
resolve(Buffer.concat(bodyData));
|
|
209
243
|
});
|
|
210
244
|
});
|
|
245
|
+
return encoding ? promise.then((buff) => buff.toString(encoding)) : promise;
|
|
211
246
|
}
|
|
212
247
|
async function useBody(req) {
|
|
213
|
-
if (req
|
|
248
|
+
if (ParsedBodySymbol in req) {
|
|
214
249
|
return req[ParsedBodySymbol];
|
|
215
250
|
}
|
|
216
251
|
const body = await useRawBody(req);
|
|
@@ -466,35 +501,6 @@ function setCookie(res, name, value, serializeOptions) {
|
|
|
466
501
|
appendHeader(res, "Set-Cookie", cookieStr);
|
|
467
502
|
}
|
|
468
503
|
|
|
469
|
-
function useQuery(req) {
|
|
470
|
-
return getQuery(req.url || "");
|
|
471
|
-
}
|
|
472
|
-
function useMethod(req, defaultMethod = "GET") {
|
|
473
|
-
return (req.method || defaultMethod).toUpperCase();
|
|
474
|
-
}
|
|
475
|
-
function isMethod(req, expected, allowHead) {
|
|
476
|
-
const method = useMethod(req);
|
|
477
|
-
if (allowHead && method === "HEAD") {
|
|
478
|
-
return true;
|
|
479
|
-
}
|
|
480
|
-
if (typeof expected === "string") {
|
|
481
|
-
if (method === expected) {
|
|
482
|
-
return true;
|
|
483
|
-
}
|
|
484
|
-
} else if (expected.includes(method)) {
|
|
485
|
-
return true;
|
|
486
|
-
}
|
|
487
|
-
return false;
|
|
488
|
-
}
|
|
489
|
-
function assertMethod(req, expected, allowHead) {
|
|
490
|
-
if (!isMethod(req, expected, allowHead)) {
|
|
491
|
-
throw createError({
|
|
492
|
-
statusCode: 405,
|
|
493
|
-
statusMessage: "HTTP method is not allowed."
|
|
494
|
-
});
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
504
|
class H3Error extends Error {
|
|
499
505
|
constructor() {
|
|
500
506
|
super(...arguments);
|