routup 1.0.0-alpha.2 → 1.0.0-alpha.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 +7 -1
- package/dist/helpers/index.d.ts +0 -1
- package/dist/helpers/request/cookie.d.ts +6 -3
- package/dist/helpers/request/query.d.ts +6 -3
- package/dist/helpers/response/send-format.d.ts +4 -3
- package/dist/index.cjs +59 -45
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +56 -46
- package/dist/index.mjs.map +1 -1
- package/dist/utils/etag/index.d.ts +1 -0
- package/package.json +2 -1
- package/dist/helpers/type.d.ts +0 -3
package/README.md
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
[](https://routup.net)
|
|
4
|
+
|
|
5
|
+
</div>
|
|
6
|
+
|
|
7
|
+
# Routup 🧙
|
|
2
8
|
|
|
3
9
|
[](https://badge.fury.io/js/routup)
|
|
4
10
|
[](https://github.com/Tada5hi/routup/actions/workflows/main.yml)
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { IncomingMessage } from 'node:http';
|
|
3
|
-
import type {
|
|
4
|
-
|
|
3
|
+
import type { Request } from '../../type';
|
|
4
|
+
type RequestCookieFn = (req: Request) => Record<string, string>;
|
|
5
|
+
export declare function setRequestCookieFn(fn: RequestCookieFn): void;
|
|
5
6
|
export declare function useRequestCookies(req: IncomingMessage): Record<string, string>;
|
|
6
7
|
export declare function hasRequestCookies(req: IncomingMessage): boolean;
|
|
7
8
|
export declare function useRequestCookie(req: IncomingMessage, name: string): string | undefined;
|
|
8
|
-
export declare function setRequestCookies(req: IncomingMessage, record: Record<string, any
|
|
9
|
+
export declare function setRequestCookies(req: IncomingMessage, record: Record<string, any>): void;
|
|
10
|
+
export declare function extendRequestCookies(req: IncomingMessage, record: Record<string, any>): void;
|
|
11
|
+
export {};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import type { Request } from '../../type';
|
|
2
|
-
|
|
3
|
-
export declare function setRequestQueryFn(fn:
|
|
2
|
+
type RequestQueryFn = (req: Request) => Record<string, any>;
|
|
3
|
+
export declare function setRequestQueryFn(fn: RequestQueryFn): void;
|
|
4
4
|
export declare function useRequestQuery(req: Request): Record<string, any>;
|
|
5
5
|
export declare function useRequestQuery(req: Request, key: string): any;
|
|
6
6
|
export declare function hasRequestQuery(req: Request): boolean;
|
|
7
7
|
export declare function setRequestQuery(req: Request, key: string, value: unknown): void;
|
|
8
|
-
export declare function setRequestQuery(req: Request, record: Record<string, any
|
|
8
|
+
export declare function setRequestQuery(req: Request, record: Record<string, any>): void;
|
|
9
|
+
export declare function extendRequestQuery(req: Request, key: string, value: unknown): void;
|
|
10
|
+
export declare function extendRequestQuery(req: Request, record: Record<string, any>): void;
|
|
11
|
+
export {};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Response } from '../../type';
|
|
2
|
-
|
|
3
|
-
[key: string]: () => void;
|
|
2
|
+
type ResponseFormats = {
|
|
4
3
|
default: () => void;
|
|
4
|
+
[key: string]: () => void;
|
|
5
5
|
};
|
|
6
|
-
export declare function sendFormat(
|
|
6
|
+
export declare function sendFormat(res: Response, input: ResponseFormats): void;
|
|
7
|
+
export {};
|
package/dist/index.cjs
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
var continu = require('continu');
|
|
4
4
|
var process = require('node:process');
|
|
5
5
|
var zod = require('zod');
|
|
6
|
-
var smob = require('smob');
|
|
7
6
|
var crypto = require('node:crypto');
|
|
8
7
|
var node_fs = require('node:fs');
|
|
8
|
+
var smob = require('smob');
|
|
9
9
|
var proxyAddr = require('proxy-addr');
|
|
10
10
|
var mimeExplorer = require('mime-explorer');
|
|
11
11
|
var http = require('@ebec/http');
|
|
@@ -15,15 +15,6 @@ var path = require('node:path');
|
|
|
15
15
|
var pathToRegexp = require('path-to-regexp');
|
|
16
16
|
var node_http = require('node:http');
|
|
17
17
|
|
|
18
|
-
/*
|
|
19
|
-
* Copyright (c) 2022-2023.
|
|
20
|
-
* Author Peter Placzek (tada5hi)
|
|
21
|
-
* For the full copyright and license information,
|
|
22
|
-
* view the LICENSE file that was distributed with this source code.
|
|
23
|
-
*/ function isObject(item) {
|
|
24
|
-
return !!item && typeof item === 'object' && !Array.isArray(item);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
18
|
/**
|
|
28
19
|
* Determine if object is a Stats object.
|
|
29
20
|
*
|
|
@@ -31,8 +22,7 @@ var node_http = require('node:http');
|
|
|
31
22
|
* @return {boolean}
|
|
32
23
|
* @api private
|
|
33
24
|
*/ function isStatsObject(obj) {
|
|
34
|
-
|
|
35
|
-
if (typeof node_fs.Stats === 'function' && obj instanceof node_fs.Stats) {
|
|
25
|
+
/* istanbul ignore next */ if (typeof node_fs.Stats === 'function' && obj instanceof node_fs.Stats) {
|
|
36
26
|
return true;
|
|
37
27
|
}
|
|
38
28
|
// quack quack
|
|
@@ -65,6 +55,15 @@ var node_http = require('node:http');
|
|
|
65
55
|
return weak ? `W/${tag}` : tag;
|
|
66
56
|
}
|
|
67
57
|
|
|
58
|
+
/*
|
|
59
|
+
* Copyright (c) 2022-2023.
|
|
60
|
+
* Author Peter Placzek (tada5hi)
|
|
61
|
+
* For the full copyright and license information,
|
|
62
|
+
* view the LICENSE file that was distributed with this source code.
|
|
63
|
+
*/ function isObject(item) {
|
|
64
|
+
return !!item && typeof item === 'object' && !Array.isArray(item);
|
|
65
|
+
}
|
|
66
|
+
|
|
68
67
|
function buildEtagFn(input) {
|
|
69
68
|
if (typeof input === 'function') {
|
|
70
69
|
return input;
|
|
@@ -156,11 +155,11 @@ function isPromise(p) {
|
|
|
156
155
|
}, timeout);
|
|
157
156
|
res.once('close', ()=>{
|
|
158
157
|
clearTimeout(instance);
|
|
159
|
-
if (typeof done === 'function') {
|
|
158
|
+
/* istanbul ignore next */ if (typeof done === 'function') {
|
|
160
159
|
done();
|
|
161
160
|
}
|
|
162
161
|
});
|
|
163
|
-
res.once('error', (e)=>{
|
|
162
|
+
/* istanbul ignore next */ res.once('error', (e)=>{
|
|
164
163
|
clearTimeout(instance);
|
|
165
164
|
if (typeof done === 'function') {
|
|
166
165
|
done(e);
|
|
@@ -366,7 +365,7 @@ function setRequestCookieFn(fn) {
|
|
|
366
365
|
requestFn$1 = fn;
|
|
367
366
|
}
|
|
368
367
|
function useRequestCookies(req) {
|
|
369
|
-
if (!(CookieSymbol in req) && typeof requestFn$1
|
|
368
|
+
if (!(CookieSymbol in req) && typeof requestFn$1 === 'function') {
|
|
370
369
|
req[CookieSymbol] = requestFn$1(req);
|
|
371
370
|
}
|
|
372
371
|
if (CookieSymbol in req) {
|
|
@@ -380,11 +379,12 @@ function hasRequestCookies(req) {
|
|
|
380
379
|
function useRequestCookie(req, name) {
|
|
381
380
|
return useRequestCookies(req)[name];
|
|
382
381
|
}
|
|
383
|
-
function setRequestCookies(req, record
|
|
382
|
+
function setRequestCookies(req, record) {
|
|
383
|
+
req[CookieSymbol] = record;
|
|
384
|
+
}
|
|
385
|
+
function extendRequestCookies(req, record) {
|
|
384
386
|
if (CookieSymbol in req) {
|
|
385
|
-
|
|
386
|
-
req[CookieSymbol] = smob.merge(req[CookieSymbol], record);
|
|
387
|
-
}
|
|
387
|
+
req[CookieSymbol] = smob.merge(req[CookieSymbol], record);
|
|
388
388
|
return;
|
|
389
389
|
}
|
|
390
390
|
req[CookieSymbol] = record;
|
|
@@ -613,7 +613,7 @@ function setRequestMountPath(req, basePath) {
|
|
|
613
613
|
* view the LICENSE file that was distributed with this source code.
|
|
614
614
|
*/ const ParamsSymbol = Symbol.for('ReqParams');
|
|
615
615
|
function useRequestParams(req) {
|
|
616
|
-
if ('params' in req) {
|
|
616
|
+
/* istanbul ignore next */ if ('params' in req) {
|
|
617
617
|
return req.params;
|
|
618
618
|
}
|
|
619
619
|
if (ParamsSymbol in req) {
|
|
@@ -700,25 +700,28 @@ function hasRequestQuery(req) {
|
|
|
700
700
|
return QuerySymbol in req && isObject(req[QuerySymbol]);
|
|
701
701
|
}
|
|
702
702
|
function setRequestQuery(req, key, value) {
|
|
703
|
+
if (isObject(key)) {
|
|
704
|
+
req[QuerySymbol] = key;
|
|
705
|
+
return;
|
|
706
|
+
}
|
|
707
|
+
req[QuerySymbol] = {
|
|
708
|
+
[key]: value
|
|
709
|
+
};
|
|
710
|
+
}
|
|
711
|
+
function extendRequestQuery(req, key, value) {
|
|
703
712
|
if (QuerySymbol in req) {
|
|
704
|
-
if (
|
|
705
|
-
|
|
706
|
-
req[QuerySymbol] = smob.merge(req[QuerySymbol], key);
|
|
707
|
-
} else {
|
|
708
|
-
req[QuerySymbol] = key;
|
|
709
|
-
}
|
|
713
|
+
if (isObject(key)) {
|
|
714
|
+
req[QuerySymbol] = smob.merge(req[QuerySymbol], key);
|
|
710
715
|
} else {
|
|
711
716
|
req[QuerySymbol][key] = value;
|
|
712
717
|
}
|
|
713
718
|
return;
|
|
714
719
|
}
|
|
715
|
-
if (
|
|
716
|
-
req
|
|
720
|
+
if (isObject(key)) {
|
|
721
|
+
setRequestQuery(req, key);
|
|
717
722
|
return;
|
|
718
723
|
}
|
|
719
|
-
req
|
|
720
|
-
[key]: value
|
|
721
|
-
};
|
|
724
|
+
setRequestQuery(req, key, value);
|
|
722
725
|
}
|
|
723
726
|
|
|
724
727
|
/*
|
|
@@ -807,20 +810,20 @@ function setResponseContentTypeByFileName(res, fileName) {
|
|
|
807
810
|
}
|
|
808
811
|
}
|
|
809
812
|
}
|
|
810
|
-
function onResponseFinished(res, cb) {
|
|
813
|
+
/* istanbul ignore next */ function onResponseFinished(res, cb) {
|
|
811
814
|
let called;
|
|
812
815
|
const callCallback = (err)=>{
|
|
813
816
|
if (called) return;
|
|
814
817
|
called = true;
|
|
815
818
|
cb(err);
|
|
816
819
|
};
|
|
817
|
-
res.on('finish',
|
|
820
|
+
res.on('finish', ()=>{
|
|
818
821
|
callCallback();
|
|
819
822
|
});
|
|
820
|
-
res.on('close',
|
|
823
|
+
res.on('close', ()=>{
|
|
821
824
|
callCallback();
|
|
822
825
|
});
|
|
823
|
-
res.on('error',
|
|
826
|
+
res.on('error', (err)=>{
|
|
824
827
|
callCallback(err);
|
|
825
828
|
});
|
|
826
829
|
}
|
|
@@ -947,7 +950,7 @@ function sendCreated(res, chunk) {
|
|
|
947
950
|
stream.on('open', ()=>{
|
|
948
951
|
stream.pipe(res);
|
|
949
952
|
});
|
|
950
|
-
stream.on('error', (err)=>{
|
|
953
|
+
/* istanbul ignore next */ stream.on('error', (err)=>{
|
|
951
954
|
if (typeof fn === 'function') {
|
|
952
955
|
fn(err);
|
|
953
956
|
} else {
|
|
@@ -990,7 +993,7 @@ function sendFile(res, filePath, fn) {
|
|
|
990
993
|
setResponseContentTypeByFileName(res, fileName);
|
|
991
994
|
}
|
|
992
995
|
resolveStats(options, (err, stats)=>{
|
|
993
|
-
if (err) {
|
|
996
|
+
/* istanbul ignore next */ if (err) {
|
|
994
997
|
if (typeof fn === 'function') {
|
|
995
998
|
fn(err);
|
|
996
999
|
} else {
|
|
@@ -1005,7 +1008,10 @@ function sendFile(res, filePath, fn) {
|
|
|
1005
1008
|
const [x, y] = rangeHeader.replace('bytes=', '').split('-');
|
|
1006
1009
|
streamOptions.end = Math.min(parseInt(y, 10) || stats.size - 1, stats.size - 1);
|
|
1007
1010
|
streamOptions.start = parseInt(x, 10) || 0;
|
|
1008
|
-
if (streamOptions.
|
|
1011
|
+
if (streamOptions.end >= stats.size) {
|
|
1012
|
+
streamOptions.end = stats.size - 1;
|
|
1013
|
+
}
|
|
1014
|
+
if (streamOptions.start >= stats.size) {
|
|
1009
1015
|
res.setHeader(exports.HeaderName.CONTENT_RANGE, `bytes */${stats.size}`);
|
|
1010
1016
|
res.statusCode = 416;
|
|
1011
1017
|
res.end();
|
|
@@ -1019,16 +1025,20 @@ function sendFile(res, filePath, fn) {
|
|
|
1019
1025
|
res.setHeader(exports.HeaderName.ACCEPT_RANGES, 'bytes');
|
|
1020
1026
|
res.setHeader(exports.HeaderName.LAST_MODIFIED, stats.mtime.toUTCString());
|
|
1021
1027
|
res.setHeader(exports.HeaderName.ETag, `W/"${stats.size}-${stats.mtime.getTime()}"`);
|
|
1022
|
-
sendStream(res, node_fs.createReadStream(options
|
|
1028
|
+
sendStream(res, node_fs.createReadStream(options.filePath, streamOptions), fn);
|
|
1023
1029
|
});
|
|
1024
1030
|
}
|
|
1025
1031
|
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
+
function sendFormat(res, input) {
|
|
1033
|
+
const { default: formatDefault , ...formats } = input;
|
|
1034
|
+
const contentTypes = Object.keys(formats);
|
|
1035
|
+
const contentType = getRequestAcceptableContentType(res.req, contentTypes);
|
|
1036
|
+
if (contentType) {
|
|
1037
|
+
formats[contentType]();
|
|
1038
|
+
return;
|
|
1039
|
+
}
|
|
1040
|
+
formatDefault();
|
|
1041
|
+
}
|
|
1032
1042
|
|
|
1033
1043
|
function sendRedirect(res, location, statusCode = 302) {
|
|
1034
1044
|
res.statusCode = statusCode;
|
|
@@ -1556,7 +1566,11 @@ exports.buildConfig = buildConfig;
|
|
|
1556
1566
|
exports.buildEtagFn = buildEtagFn;
|
|
1557
1567
|
exports.buildTrustProxyFn = buildTrustProxyFn;
|
|
1558
1568
|
exports.cleanDoubleSlashes = cleanDoubleSlashes;
|
|
1569
|
+
exports.createEtag = createEtag;
|
|
1559
1570
|
exports.createRequestTimeout = createRequestTimeout;
|
|
1571
|
+
exports.extendRequestCookies = extendRequestCookies;
|
|
1572
|
+
exports.extendRequestQuery = extendRequestQuery;
|
|
1573
|
+
exports.generateETag = generateETag;
|
|
1560
1574
|
exports.getCharsetForMimeType = getCharsetForMimeType;
|
|
1561
1575
|
exports.getConfigOption = getConfigOption;
|
|
1562
1576
|
exports.getMimeType = getMimeType;
|