h3 0.3.3 → 0.3.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 +5 -0
- package/dist/index.cjs +35 -7
- package/dist/index.d.ts +7 -1
- package/dist/index.mjs +31 -8
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -98,6 +98,11 @@ Instead of adding helpers to `req` and `res`, h3 exposes them as composable util
|
|
|
98
98
|
- `appendHeader(res, name, value)`
|
|
99
99
|
- `createError({ statusCode, statusMessage, data? }`
|
|
100
100
|
- `sendError(res, error, debug?)`
|
|
101
|
+
- `defineHandle(handle)`
|
|
102
|
+
- `defineMiddleware(middlware)`
|
|
103
|
+
- `useMethod(req, default?)`
|
|
104
|
+
- `isMethod(req, expected, allowHead?)`
|
|
105
|
+
- `assertMethod(req, expected, allowHead?)`
|
|
101
106
|
|
|
102
107
|
👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).
|
|
103
108
|
|
package/dist/index.cjs
CHANGED
|
@@ -83,8 +83,8 @@ function parseURL(input = "", defaultProto) {
|
|
|
83
83
|
return defaultProto ? parseURL(defaultProto + input) : parsePath(input);
|
|
84
84
|
}
|
|
85
85
|
const [protocol = "", auth, hostAndPath] = (input.match(/([^:/]+:)?\/\/([^/@]+@)?(.*)/) || []).splice(1);
|
|
86
|
-
const [host = "", path = ""] = (hostAndPath.match(/([
|
|
87
|
-
const {pathname, search, hash} = parsePath(path);
|
|
86
|
+
const [host = "", path = ""] = (hostAndPath.match(/([^/?]*)(.*)?/) || []).splice(1);
|
|
87
|
+
const { pathname, search, hash } = parsePath(path);
|
|
88
88
|
return {
|
|
89
89
|
protocol,
|
|
90
90
|
auth: auth ? auth.substr(0, auth.length - 1) : "",
|
|
@@ -103,6 +103,8 @@ function parsePath(input = "") {
|
|
|
103
103
|
};
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
const defineHandle = (handler) => handler;
|
|
107
|
+
const defineMiddleware = (middleware) => middleware;
|
|
106
108
|
function promisifyHandle(handle) {
|
|
107
109
|
return function(req, res) {
|
|
108
110
|
return callHandle(handle, req, res);
|
|
@@ -346,11 +348,7 @@ function serialize(name, val, options) {
|
|
|
346
348
|
|
|
347
349
|
if (null != opt.maxAge) {
|
|
348
350
|
var maxAge = opt.maxAge - 0;
|
|
349
|
-
|
|
350
|
-
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
351
|
-
throw new TypeError('option maxAge is invalid')
|
|
352
|
-
}
|
|
353
|
-
|
|
351
|
+
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
|
|
354
352
|
str += '; Max-Age=' + Math.floor(maxAge);
|
|
355
353
|
}
|
|
356
354
|
|
|
@@ -475,6 +473,31 @@ function setCookie(res, name, value, serializeOptions) {
|
|
|
475
473
|
function useQuery(req) {
|
|
476
474
|
return getQuery(req.url || "");
|
|
477
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
|
+
}
|
|
478
501
|
|
|
479
502
|
class H3Error extends Error {
|
|
480
503
|
constructor() {
|
|
@@ -609,11 +632,15 @@ function normalizeLayer(layer) {
|
|
|
609
632
|
exports.H3Error = H3Error;
|
|
610
633
|
exports.MIMES = MIMES;
|
|
611
634
|
exports.appendHeader = appendHeader;
|
|
635
|
+
exports.assertMethod = assertMethod;
|
|
612
636
|
exports.callHandle = callHandle;
|
|
613
637
|
exports.createApp = createApp;
|
|
614
638
|
exports.createError = createError;
|
|
615
639
|
exports.createHandle = createHandle;
|
|
616
640
|
exports.defaultContentType = defaultContentType;
|
|
641
|
+
exports.defineHandle = defineHandle;
|
|
642
|
+
exports.defineMiddleware = defineMiddleware;
|
|
643
|
+
exports.isMethod = isMethod;
|
|
617
644
|
exports.lazyHandle = lazyHandle;
|
|
618
645
|
exports.promisifyHandle = promisifyHandle;
|
|
619
646
|
exports.send = send;
|
|
@@ -625,5 +652,6 @@ exports.useBase = useBase;
|
|
|
625
652
|
exports.useBody = useBody;
|
|
626
653
|
exports.useCookie = useCookie;
|
|
627
654
|
exports.useCookies = useCookies;
|
|
655
|
+
exports.useMethod = useMethod;
|
|
628
656
|
exports.useQuery = useQuery;
|
|
629
657
|
exports.useRawBody = useRawBody;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ declare type Handle<T = any> = (req: IncomingMessage, res: ServerResponse) => T;
|
|
|
7
7
|
declare type PHandle = Handle<Promise<any>>;
|
|
8
8
|
declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any;
|
|
9
9
|
declare type LazyHandle = () => Handle | Promise<Handle>;
|
|
10
|
+
declare const defineHandle: <T>(handler: Handle<T>) => Handle<T>;
|
|
11
|
+
declare const defineMiddleware: (middleware: Middleware) => Middleware;
|
|
10
12
|
declare function promisifyHandle(handle: Handle | Middleware): PHandle;
|
|
11
13
|
declare function callHandle(handle: Middleware, req: IncomingMessage, res: ServerResponse): Promise<unknown>;
|
|
12
14
|
declare function lazyHandle(handle: LazyHandle, promisify?: boolean): PHandle;
|
|
@@ -226,10 +228,14 @@ declare function useCookie(req: IncomingMessage, name: string): string | undefin
|
|
|
226
228
|
declare function setCookie(res: ServerResponse, name: string, value: string, serializeOptions?: CookieSerializeOptions): void;
|
|
227
229
|
|
|
228
230
|
declare function useQuery(req: IncomingMessage): ufo.QueryObject;
|
|
231
|
+
declare type HTTPMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE';
|
|
232
|
+
declare function useMethod(req: IncomingMessage, defaultMethod?: HTTPMethod): HTTPMethod;
|
|
233
|
+
declare function isMethod(req: IncomingMessage, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean): boolean;
|
|
234
|
+
declare function assertMethod(req: IncomingMessage, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean): void;
|
|
229
235
|
|
|
230
236
|
declare function send(res: ServerResponse, data: any, type?: string): Promise<unknown>;
|
|
231
237
|
declare function defaultContentType(res: ServerResponse, type?: string): void;
|
|
232
238
|
declare function sendRedirect(res: ServerResponse, location: string, code?: number): Promise<unknown>;
|
|
233
239
|
declare function appendHeader(res: ServerResponse, name: string, value: string): void;
|
|
234
240
|
|
|
235
|
-
export { App, AppOptions, AppUse, H3Error, Handle, InputLayer, InputStack, Layer, LazyHandle, MIMES, Matcher, Middleware, PHandle, Stack, appendHeader, callHandle, createApp, createError, createHandle, defaultContentType, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useQuery, useRawBody };
|
|
241
|
+
export { App, AppOptions, AppUse, H3Error, HTTPMethod, Handle, InputLayer, InputStack, Layer, LazyHandle, MIMES, Matcher, Middleware, PHandle, Stack, appendHeader, assertMethod, callHandle, createApp, createError, createHandle, defaultContentType, defineHandle, defineMiddleware, isMethod, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useMethod, useQuery, useRawBody };
|
package/dist/index.mjs
CHANGED
|
@@ -79,8 +79,8 @@ function parseURL(input = "", defaultProto) {
|
|
|
79
79
|
return defaultProto ? parseURL(defaultProto + input) : parsePath(input);
|
|
80
80
|
}
|
|
81
81
|
const [protocol = "", auth, hostAndPath] = (input.match(/([^:/]+:)?\/\/([^/@]+@)?(.*)/) || []).splice(1);
|
|
82
|
-
const [host = "", path = ""] = (hostAndPath.match(/([
|
|
83
|
-
const {pathname, search, hash} = parsePath(path);
|
|
82
|
+
const [host = "", path = ""] = (hostAndPath.match(/([^/?]*)(.*)?/) || []).splice(1);
|
|
83
|
+
const { pathname, search, hash } = parsePath(path);
|
|
84
84
|
return {
|
|
85
85
|
protocol,
|
|
86
86
|
auth: auth ? auth.substr(0, auth.length - 1) : "",
|
|
@@ -99,6 +99,8 @@ function parsePath(input = "") {
|
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
const defineHandle = (handler) => handler;
|
|
103
|
+
const defineMiddleware = (middleware) => middleware;
|
|
102
104
|
function promisifyHandle(handle) {
|
|
103
105
|
return function(req, res) {
|
|
104
106
|
return callHandle(handle, req, res);
|
|
@@ -342,11 +344,7 @@ function serialize(name, val, options) {
|
|
|
342
344
|
|
|
343
345
|
if (null != opt.maxAge) {
|
|
344
346
|
var maxAge = opt.maxAge - 0;
|
|
345
|
-
|
|
346
|
-
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
347
|
-
throw new TypeError('option maxAge is invalid')
|
|
348
|
-
}
|
|
349
|
-
|
|
347
|
+
if (isNaN(maxAge)) throw new Error('maxAge should be a Number');
|
|
350
348
|
str += '; Max-Age=' + Math.floor(maxAge);
|
|
351
349
|
}
|
|
352
350
|
|
|
@@ -471,6 +469,31 @@ function setCookie(res, name, value, serializeOptions) {
|
|
|
471
469
|
function useQuery(req) {
|
|
472
470
|
return getQuery(req.url || "");
|
|
473
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
|
+
}
|
|
474
497
|
|
|
475
498
|
class H3Error extends Error {
|
|
476
499
|
constructor() {
|
|
@@ -602,4 +625,4 @@ function normalizeLayer(layer) {
|
|
|
602
625
|
};
|
|
603
626
|
}
|
|
604
627
|
|
|
605
|
-
export { H3Error, MIMES, appendHeader, callHandle, createApp, createError, createHandle, defaultContentType, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useQuery, useRawBody };
|
|
628
|
+
export { H3Error, MIMES, appendHeader, assertMethod, callHandle, createApp, createError, createHandle, defaultContentType, defineHandle, defineMiddleware, isMethod, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useMethod, useQuery, useRawBody };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "h3",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"description": "Tiny JavaScript Server",
|
|
5
5
|
"repository": "unjs/h3",
|
|
6
6
|
"license": "MIT",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"build": "siroc build",
|
|
22
22
|
"dev": "jiti test/playground",
|
|
23
23
|
"lint": "eslint --ext ts .",
|
|
24
|
-
"release": "yarn test && yarn build && standard-version && npm publish && git push --follow-tags",
|
|
25
24
|
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./hello.js",
|
|
25
|
+
"release": "yarn test && yarn build && standard-version && npm publish && git push --follow-tags",
|
|
26
26
|
"test": "yarn lint && jest"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"@types/supertest": "latest",
|
|
36
36
|
"autocannon": "latest",
|
|
37
37
|
"connect": "latest",
|
|
38
|
-
"cookie": "latest",
|
|
38
|
+
"cookie-es": "latest",
|
|
39
39
|
"destr": "latest",
|
|
40
40
|
"eslint": "latest",
|
|
41
41
|
"express": "latest",
|
|
42
|
-
"get-port": "
|
|
42
|
+
"get-port": "^5.0.0",
|
|
43
43
|
"jest": "latest",
|
|
44
44
|
"jiti": "latest",
|
|
45
45
|
"listhen": "latest",
|