routup 4.0.1 → 4.1.0
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 +139 -124
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +76 -64
- package/dist/index.mjs.map +1 -1
- package/package.json +26 -24
package/dist/index.cjs
CHANGED
|
@@ -10,8 +10,7 @@ var readableStream = require('readable-stream');
|
|
|
10
10
|
var http = require('@ebec/http');
|
|
11
11
|
var pathToRegexp = require('path-to-regexp');
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
(function(MethodName) {
|
|
13
|
+
var MethodName = /*#__PURE__*/ function(MethodName) {
|
|
15
14
|
MethodName["GET"] = "GET";
|
|
16
15
|
MethodName["POST"] = "POST";
|
|
17
16
|
MethodName["PUT"] = "PUT";
|
|
@@ -19,9 +18,9 @@ exports.MethodName = void 0;
|
|
|
19
18
|
MethodName["DELETE"] = "DELETE";
|
|
20
19
|
MethodName["OPTIONS"] = "OPTIONS";
|
|
21
20
|
MethodName["HEAD"] = "HEAD";
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
return MethodName;
|
|
22
|
+
}({});
|
|
23
|
+
var HeaderName = /*#__PURE__*/ function(HeaderName) {
|
|
25
24
|
HeaderName["ACCEPT"] = "accept";
|
|
26
25
|
HeaderName["ACCEPT_CHARSET"] = "accept-charset";
|
|
27
26
|
HeaderName["ACCEPT_ENCODING"] = "accept-encoding";
|
|
@@ -53,10 +52,11 @@ exports.HeaderName = void 0;
|
|
|
53
52
|
HeaderName["X_FORWARDED_HOST"] = "x-forwarded-host";
|
|
54
53
|
HeaderName["X_FORWARDED_FOR"] = "x-forwarded-for";
|
|
55
54
|
HeaderName["X_FORWARDED_PROTO"] = "x-forwarded-proto";
|
|
56
|
-
|
|
55
|
+
return HeaderName;
|
|
56
|
+
}({});
|
|
57
57
|
|
|
58
58
|
function isRequestCacheable(req, modifiedTime) {
|
|
59
|
-
const modifiedSince = req.headers[
|
|
59
|
+
const modifiedSince = req.headers[HeaderName.IF_MODIFIED_SINCE];
|
|
60
60
|
if (!modifiedSince) {
|
|
61
61
|
return false;
|
|
62
62
|
}
|
|
@@ -411,7 +411,7 @@ function getRequestAcceptableContentType(req, input) {
|
|
|
411
411
|
if (items.length === 0) {
|
|
412
412
|
return getRequestAcceptableContentTypes(req).shift();
|
|
413
413
|
}
|
|
414
|
-
const header = getRequestHeader(req,
|
|
414
|
+
const header = getRequestHeader(req, HeaderName.ACCEPT);
|
|
415
415
|
if (!header) {
|
|
416
416
|
return items[0];
|
|
417
417
|
}
|
|
@@ -485,7 +485,7 @@ function getRequestAcceptableLanguage(req, input) {
|
|
|
485
485
|
}
|
|
486
486
|
|
|
487
487
|
function matchRequestContentType(req, contentType) {
|
|
488
|
-
const header = getRequestHeader(req,
|
|
488
|
+
const header = getRequestHeader(req, HeaderName.CONTENT_TYPE);
|
|
489
489
|
if (!header) {
|
|
490
490
|
return true;
|
|
491
491
|
}
|
|
@@ -538,9 +538,9 @@ function getRequestHostName(req, options) {
|
|
|
538
538
|
} else {
|
|
539
539
|
trustProxy = findRouterOption('trustProxy', useRequestRouterPath(req));
|
|
540
540
|
}
|
|
541
|
-
let hostname = req.headers[
|
|
541
|
+
let hostname = req.headers[HeaderName.X_FORWARDED_HOST];
|
|
542
542
|
if (!hostname || !req.socket.remoteAddress || !trustProxy(req.socket.remoteAddress, 0)) {
|
|
543
|
-
hostname = req.headers[
|
|
543
|
+
hostname = req.headers[HeaderName.HOST];
|
|
544
544
|
} else {
|
|
545
545
|
hostname = Array.isArray(hostname) ? hostname.pop() : hostname;
|
|
546
546
|
if (hostname && hostname.indexOf(',') !== -1) {
|
|
@@ -627,7 +627,7 @@ function getRequestProtocol(req, options) {
|
|
|
627
627
|
if (!req.socket.remoteAddress || !trustProxy(req.socket.remoteAddress, 0)) {
|
|
628
628
|
return protocol;
|
|
629
629
|
}
|
|
630
|
-
let header = req.headers[
|
|
630
|
+
let header = req.headers[HeaderName.X_FORWARDED_PROTO];
|
|
631
631
|
/* istanbul ignore next */ if (Array.isArray(header)) {
|
|
632
632
|
header = header.pop();
|
|
633
633
|
}
|
|
@@ -781,6 +781,15 @@ function serializeEventStreamMessage(message) {
|
|
|
781
781
|
}
|
|
782
782
|
|
|
783
783
|
class EventStream {
|
|
784
|
+
constructor(response){
|
|
785
|
+
this.response = response;
|
|
786
|
+
this.passThrough = new readableStream.PassThrough({
|
|
787
|
+
encoding: 'utf-8'
|
|
788
|
+
});
|
|
789
|
+
this.flushed = false;
|
|
790
|
+
this.eventHandlers = {};
|
|
791
|
+
this.open();
|
|
792
|
+
}
|
|
784
793
|
open() {
|
|
785
794
|
this.response.req.on('close', ()=>this.end());
|
|
786
795
|
this.response.req.on('error', (err)=>{
|
|
@@ -793,11 +802,11 @@ class EventStream {
|
|
|
793
802
|
this.end();
|
|
794
803
|
});
|
|
795
804
|
this.passThrough.on('close', ()=>this.end());
|
|
796
|
-
this.response.setHeader(
|
|
797
|
-
this.response.setHeader(
|
|
798
|
-
this.response.setHeader(
|
|
805
|
+
this.response.setHeader(HeaderName.CONTENT_TYPE, 'text/event-stream');
|
|
806
|
+
this.response.setHeader(HeaderName.CACHE_CONTROL, 'private, no-cache, no-store, no-transform, must-revalidate, max-age=0');
|
|
807
|
+
this.response.setHeader(HeaderName.X_ACCEL_BUFFERING, 'no');
|
|
799
808
|
if (!isRequestHTTP2(this.response.req)) {
|
|
800
|
-
this.response.setHeader(
|
|
809
|
+
this.response.setHeader(HeaderName.CONNECTION, 'keep-alive');
|
|
801
810
|
}
|
|
802
811
|
this.response.statusCode = 200;
|
|
803
812
|
}
|
|
@@ -837,15 +846,6 @@ class EventStream {
|
|
|
837
846
|
listeners[i].apply(this, args);
|
|
838
847
|
}
|
|
839
848
|
}
|
|
840
|
-
constructor(response){
|
|
841
|
-
this.response = response;
|
|
842
|
-
this.passThrough = new readableStream.PassThrough({
|
|
843
|
-
encoding: 'utf-8'
|
|
844
|
-
});
|
|
845
|
-
this.flushed = false;
|
|
846
|
-
this.eventHandlers = {};
|
|
847
|
-
this.open();
|
|
848
|
-
}
|
|
849
849
|
}
|
|
850
850
|
|
|
851
851
|
function createEventStream(response) {
|
|
@@ -909,7 +909,7 @@ function setResponseContentTypeByFileName(res, fileName) {
|
|
|
909
909
|
if (charset) {
|
|
910
910
|
type += `; charset=${charset}`;
|
|
911
911
|
}
|
|
912
|
-
res.setHeader(
|
|
912
|
+
res.setHeader(HeaderName.CONTENT_TYPE, type);
|
|
913
913
|
}
|
|
914
914
|
}
|
|
915
915
|
}
|
|
@@ -918,19 +918,19 @@ function setResponseHeaderAttachment(res, filename) {
|
|
|
918
918
|
if (typeof filename === 'string') {
|
|
919
919
|
setResponseContentTypeByFileName(res, filename);
|
|
920
920
|
}
|
|
921
|
-
res.setHeader(
|
|
921
|
+
res.setHeader(HeaderName.CONTENT_DISPOSITION, `attachment${filename ? `; filename="${filename}"` : ''}`);
|
|
922
922
|
}
|
|
923
923
|
|
|
924
924
|
function setResponseHeaderContentType(res, input, ifNotExists) {
|
|
925
925
|
if (ifNotExists) {
|
|
926
|
-
const header = res.getHeader(
|
|
926
|
+
const header = res.getHeader(HeaderName.CONTENT_TYPE);
|
|
927
927
|
if (header) {
|
|
928
928
|
return;
|
|
929
929
|
}
|
|
930
930
|
}
|
|
931
931
|
const contentType = getMimeType(input);
|
|
932
932
|
if (contentType) {
|
|
933
|
-
res.setHeader(
|
|
933
|
+
res.setHeader(HeaderName.CONTENT_TYPE, contentType);
|
|
934
934
|
}
|
|
935
935
|
}
|
|
936
936
|
|
|
@@ -983,7 +983,7 @@ async function sendWebBlob(res, blob) {
|
|
|
983
983
|
|
|
984
984
|
async function sendWebResponse(res, webResponse) {
|
|
985
985
|
if (webResponse.redirected) {
|
|
986
|
-
res.setHeader(
|
|
986
|
+
res.setHeader(HeaderName.LOCATION, webResponse.url);
|
|
987
987
|
}
|
|
988
988
|
if (webResponse.status) {
|
|
989
989
|
res.statusCode = webResponse.status;
|
|
@@ -992,7 +992,7 @@ async function sendWebResponse(res, webResponse) {
|
|
|
992
992
|
res.statusMessage = webResponse.statusText;
|
|
993
993
|
}
|
|
994
994
|
webResponse.headers.forEach((value, key)=>{
|
|
995
|
-
if (key ===
|
|
995
|
+
if (key === HeaderName.SET_COOKIE) {
|
|
996
996
|
res.appendHeader(key, splitCookiesString(value));
|
|
997
997
|
} else {
|
|
998
998
|
res.setHeader(key, value);
|
|
@@ -1045,8 +1045,8 @@ async function send(res, chunk) {
|
|
|
1045
1045
|
}
|
|
1046
1046
|
let encoding;
|
|
1047
1047
|
if (typeof chunk === 'string') {
|
|
1048
|
-
res.setHeader(
|
|
1049
|
-
appendResponseHeaderDirective(res,
|
|
1048
|
+
res.setHeader(HeaderName.CONTENT_ENCODING, 'utf-8');
|
|
1049
|
+
appendResponseHeaderDirective(res, HeaderName.CONTENT_TYPE, 'charset=utf-8');
|
|
1050
1050
|
encoding = 'utf-8';
|
|
1051
1051
|
}
|
|
1052
1052
|
// populate Content-Length
|
|
@@ -1064,7 +1064,7 @@ async function send(res, chunk) {
|
|
|
1064
1064
|
encoding = undefined;
|
|
1065
1065
|
len = chunk.length;
|
|
1066
1066
|
}
|
|
1067
|
-
res.setHeader(
|
|
1067
|
+
res.setHeader(HeaderName.CONTENT_LENGTH, `${len}`);
|
|
1068
1068
|
}
|
|
1069
1069
|
if (typeof len !== 'undefined') {
|
|
1070
1070
|
const etagFn = findRouterOption('etag', useRequestRouterPath(res.req));
|
|
@@ -1073,22 +1073,22 @@ async function send(res, chunk) {
|
|
|
1073
1073
|
return;
|
|
1074
1074
|
}
|
|
1075
1075
|
if (typeof chunkHash === 'string') {
|
|
1076
|
-
res.setHeader(
|
|
1077
|
-
if (res.req.headers[
|
|
1076
|
+
res.setHeader(HeaderName.ETag, chunkHash);
|
|
1077
|
+
if (res.req.headers[HeaderName.IF_NONE_MATCH] === chunkHash) {
|
|
1078
1078
|
res.statusCode = 304;
|
|
1079
1079
|
}
|
|
1080
1080
|
}
|
|
1081
1081
|
}
|
|
1082
1082
|
// strip irrelevant headers
|
|
1083
1083
|
if (res.statusCode === 204 || res.statusCode === 304) {
|
|
1084
|
-
res.removeHeader(
|
|
1085
|
-
res.removeHeader(
|
|
1086
|
-
res.removeHeader(
|
|
1084
|
+
res.removeHeader(HeaderName.CONTENT_TYPE);
|
|
1085
|
+
res.removeHeader(HeaderName.CONTENT_LENGTH);
|
|
1086
|
+
res.removeHeader(HeaderName.TRANSFER_ENCODING);
|
|
1087
1087
|
}
|
|
1088
1088
|
// alter headers for 205
|
|
1089
1089
|
if (res.statusCode === 205) {
|
|
1090
|
-
res.setHeader(
|
|
1091
|
-
res.removeHeader(
|
|
1090
|
+
res.setHeader(HeaderName.CONTENT_LENGTH, 0);
|
|
1091
|
+
res.removeHeader(HeaderName.TRANSFER_ENCODING);
|
|
1092
1092
|
}
|
|
1093
1093
|
if (isResponseGone(res)) {
|
|
1094
1094
|
return;
|
|
@@ -1138,7 +1138,7 @@ async function sendFile(res, options, next) {
|
|
|
1138
1138
|
if (name) {
|
|
1139
1139
|
const fileName = basename(name);
|
|
1140
1140
|
if (options.attachment) {
|
|
1141
|
-
const dispositionHeader = res.getHeader(
|
|
1141
|
+
const dispositionHeader = res.getHeader(HeaderName.CONTENT_DISPOSITION);
|
|
1142
1142
|
if (!dispositionHeader) {
|
|
1143
1143
|
setResponseHeaderAttachment(res, fileName);
|
|
1144
1144
|
}
|
|
@@ -1148,7 +1148,7 @@ async function sendFile(res, options, next) {
|
|
|
1148
1148
|
}
|
|
1149
1149
|
const contentOptions = {};
|
|
1150
1150
|
if (stats.size) {
|
|
1151
|
-
const rangeHeader = res.req.headers[
|
|
1151
|
+
const rangeHeader = res.req.headers[HeaderName.RANGE];
|
|
1152
1152
|
if (rangeHeader) {
|
|
1153
1153
|
const [x, y] = rangeHeader.replace('bytes=', '').split('-');
|
|
1154
1154
|
contentOptions.end = Math.min(parseInt(y, 10) || stats.size - 1, stats.size - 1);
|
|
@@ -1157,21 +1157,21 @@ async function sendFile(res, options, next) {
|
|
|
1157
1157
|
contentOptions.end = stats.size - 1;
|
|
1158
1158
|
}
|
|
1159
1159
|
if (contentOptions.start >= stats.size) {
|
|
1160
|
-
res.setHeader(
|
|
1160
|
+
res.setHeader(HeaderName.CONTENT_RANGE, `bytes */${stats.size}`);
|
|
1161
1161
|
res.statusCode = 416;
|
|
1162
1162
|
res.end();
|
|
1163
1163
|
return Promise.resolve();
|
|
1164
1164
|
}
|
|
1165
|
-
res.setHeader(
|
|
1166
|
-
res.setHeader(
|
|
1165
|
+
res.setHeader(HeaderName.CONTENT_RANGE, `bytes ${contentOptions.start}-${contentOptions.end}/${stats.size}`);
|
|
1166
|
+
res.setHeader(HeaderName.CONTENT_LENGTH, contentOptions.end - contentOptions.start + 1);
|
|
1167
1167
|
} else {
|
|
1168
|
-
res.setHeader(
|
|
1168
|
+
res.setHeader(HeaderName.CONTENT_LENGTH, stats.size);
|
|
1169
1169
|
}
|
|
1170
|
-
res.setHeader(
|
|
1170
|
+
res.setHeader(HeaderName.ACCEPT_RANGES, 'bytes');
|
|
1171
1171
|
if (stats.mtime) {
|
|
1172
1172
|
const mtime = new Date(stats.mtime);
|
|
1173
|
-
res.setHeader(
|
|
1174
|
-
res.setHeader(
|
|
1173
|
+
res.setHeader(HeaderName.LAST_MODIFIED, mtime.toUTCString());
|
|
1174
|
+
res.setHeader(HeaderName.ETag, `W/"${stats.size}-${mtime.getTime()}"`);
|
|
1175
1175
|
}
|
|
1176
1176
|
}
|
|
1177
1177
|
try {
|
|
@@ -1273,7 +1273,7 @@ function createResponse(request) {
|
|
|
1273
1273
|
finished: false,
|
|
1274
1274
|
addTrailers (_headers) {},
|
|
1275
1275
|
appendHeader (name, value) {
|
|
1276
|
-
if (name ===
|
|
1276
|
+
if (name === HeaderName.SET_COOKIE) {
|
|
1277
1277
|
value = splitCookiesString(value);
|
|
1278
1278
|
}
|
|
1279
1279
|
name = name.toLowerCase();
|
|
@@ -1308,12 +1308,24 @@ function createResponse(request) {
|
|
|
1308
1308
|
delete headers[name.toLowerCase()];
|
|
1309
1309
|
},
|
|
1310
1310
|
setHeader (name, value) {
|
|
1311
|
-
if (name ===
|
|
1311
|
+
if (name === HeaderName.SET_COOKIE && typeof value !== 'number') {
|
|
1312
1312
|
value = splitCookiesString(value);
|
|
1313
1313
|
}
|
|
1314
1314
|
headers[name.toLowerCase()] = value;
|
|
1315
1315
|
return this;
|
|
1316
1316
|
},
|
|
1317
|
+
setHeaders (headers) {
|
|
1318
|
+
if (headers instanceof Map) {
|
|
1319
|
+
headers.entries().forEach(([key, value])=>{
|
|
1320
|
+
this.setHeader(key, value);
|
|
1321
|
+
});
|
|
1322
|
+
return this;
|
|
1323
|
+
}
|
|
1324
|
+
headers.forEach((value, key)=>{
|
|
1325
|
+
this.setHeader(key, value);
|
|
1326
|
+
});
|
|
1327
|
+
return this;
|
|
1328
|
+
},
|
|
1317
1329
|
setTimeout (_msecs, _callback) {
|
|
1318
1330
|
return this;
|
|
1319
1331
|
},
|
|
@@ -1418,16 +1430,10 @@ function isDispatcherErrorEvent(event) {
|
|
|
1418
1430
|
}
|
|
1419
1431
|
|
|
1420
1432
|
class DispatchEvent {
|
|
1421
|
-
get dispatched() {
|
|
1422
|
-
return this._dispatched || this.response.writableEnded || this.response.headersSent;
|
|
1423
|
-
}
|
|
1424
|
-
set dispatched(value) {
|
|
1425
|
-
this._dispatched = value;
|
|
1426
|
-
}
|
|
1427
1433
|
constructor(context){
|
|
1428
1434
|
this.request = context.request;
|
|
1429
1435
|
this.response = context.response;
|
|
1430
|
-
this.method = context.method ||
|
|
1436
|
+
this.method = context.method || MethodName.GET;
|
|
1431
1437
|
this.methodsAllowed = [];
|
|
1432
1438
|
this.mountPath = '/';
|
|
1433
1439
|
this.params = {};
|
|
@@ -1435,6 +1441,12 @@ class DispatchEvent {
|
|
|
1435
1441
|
this.routerPath = [];
|
|
1436
1442
|
this.next = nextPlaceholder;
|
|
1437
1443
|
}
|
|
1444
|
+
get dispatched() {
|
|
1445
|
+
return this._dispatched || this.response.writableEnded || this.response.headersSent;
|
|
1446
|
+
}
|
|
1447
|
+
set dispatched(value) {
|
|
1448
|
+
this._dispatched = value;
|
|
1449
|
+
}
|
|
1438
1450
|
}
|
|
1439
1451
|
|
|
1440
1452
|
class DispatchErrorEvent extends DispatchEvent {
|
|
@@ -1445,7 +1457,7 @@ async function dispatchNodeRequest(router, request, response) {
|
|
|
1445
1457
|
request,
|
|
1446
1458
|
response,
|
|
1447
1459
|
path: useRequestPath(request),
|
|
1448
|
-
method: toMethodName(request.method,
|
|
1460
|
+
method: toMethodName(request.method, MethodName.GET)
|
|
1449
1461
|
});
|
|
1450
1462
|
await router.dispatch(event);
|
|
1451
1463
|
if (event.dispatched) {
|
|
@@ -1497,7 +1509,7 @@ function transformHeadersToTuples(input) {
|
|
|
1497
1509
|
}
|
|
1498
1510
|
|
|
1499
1511
|
async function dispatchRawRequest(router, request) {
|
|
1500
|
-
const method = toMethodName(request.method,
|
|
1512
|
+
const method = toMethodName(request.method, MethodName.GET);
|
|
1501
1513
|
const req = createRequest({
|
|
1502
1514
|
url: request.path,
|
|
1503
1515
|
method,
|
|
@@ -1555,7 +1567,7 @@ async function dispatchWebRequest(router, request) {
|
|
|
1555
1567
|
request.headers.forEach((value, key)=>{
|
|
1556
1568
|
headers[key] = value;
|
|
1557
1569
|
});
|
|
1558
|
-
const method = toMethodName(request.method,
|
|
1570
|
+
const method = toMethodName(request.method, MethodName.GET);
|
|
1559
1571
|
const res = await dispatchRawRequest(router, {
|
|
1560
1572
|
method,
|
|
1561
1573
|
path: url.pathname + url.search,
|
|
@@ -1563,7 +1575,7 @@ async function dispatchWebRequest(router, request) {
|
|
|
1563
1575
|
body: request.body
|
|
1564
1576
|
});
|
|
1565
1577
|
let body;
|
|
1566
|
-
if (method ===
|
|
1578
|
+
if (method === MethodName.HEAD || res.status === 304 || res.status === 101 || res.status === 204 || res.status === 205) {
|
|
1567
1579
|
body = null;
|
|
1568
1580
|
} else {
|
|
1569
1581
|
body = res.body;
|
|
@@ -1578,24 +1590,28 @@ function createWebDispatcher(router) {
|
|
|
1578
1590
|
return async (request)=>dispatchWebRequest(router, request);
|
|
1579
1591
|
}
|
|
1580
1592
|
|
|
1581
|
-
|
|
1582
|
-
(function(HandlerType) {
|
|
1593
|
+
var HandlerType = /*#__PURE__*/ function(HandlerType) {
|
|
1583
1594
|
HandlerType["CORE"] = "core";
|
|
1584
1595
|
HandlerType["ERROR"] = "error";
|
|
1585
|
-
|
|
1596
|
+
return HandlerType;
|
|
1597
|
+
}({});
|
|
1586
1598
|
const HandlerSymbol = Symbol.for('Handler');
|
|
1587
1599
|
|
|
1588
|
-
var HookName
|
|
1589
|
-
(function(HookName) {
|
|
1600
|
+
var HookName = /*#__PURE__*/ function(HookName) {
|
|
1590
1601
|
HookName["ERROR"] = "error";
|
|
1591
1602
|
HookName["DISPATCH_START"] = "dispatchStart";
|
|
1592
1603
|
HookName["DISPATCH_END"] = "dispatchEnd";
|
|
1593
1604
|
HookName["CHILD_MATCH"] = "childMatch";
|
|
1594
1605
|
HookName["CHILD_DISPATCH_BEFORE"] = "childDispatchBefore";
|
|
1595
1606
|
HookName["CHILD_DISPATCH_AFTER"] = "childDispatchAfter";
|
|
1596
|
-
|
|
1607
|
+
return HookName;
|
|
1608
|
+
}({});
|
|
1597
1609
|
|
|
1598
1610
|
class HookManager {
|
|
1611
|
+
// --------------------------------------------------
|
|
1612
|
+
constructor(){
|
|
1613
|
+
this.items = {};
|
|
1614
|
+
}
|
|
1599
1615
|
// --------------------------------------------------
|
|
1600
1616
|
addListener(name, fn) {
|
|
1601
1617
|
this.items[name] = this.items[name] || [];
|
|
@@ -1671,10 +1687,6 @@ class HookManager {
|
|
|
1671
1687
|
isErrorListenerHook(input) {
|
|
1672
1688
|
return input === HookName.ERROR;
|
|
1673
1689
|
}
|
|
1674
|
-
// --------------------------------------------------
|
|
1675
|
-
constructor(){
|
|
1676
|
-
this.items = {};
|
|
1677
|
-
}
|
|
1678
1690
|
}
|
|
1679
1691
|
|
|
1680
1692
|
function decodeParam(val) {
|
|
@@ -1684,6 +1696,14 @@ function decodeParam(val) {
|
|
|
1684
1696
|
return decodeURIComponent(val);
|
|
1685
1697
|
}
|
|
1686
1698
|
class PathMatcher {
|
|
1699
|
+
constructor(path, options){
|
|
1700
|
+
this.regexpKeys = [];
|
|
1701
|
+
this.path = path;
|
|
1702
|
+
this.regexpOptions = options || {};
|
|
1703
|
+
const regexp = pathToRegexp.pathToRegexp(path, options);
|
|
1704
|
+
this.regexp = regexp.regexp;
|
|
1705
|
+
this.regexpKeys = regexp.keys;
|
|
1706
|
+
}
|
|
1687
1707
|
test(path) {
|
|
1688
1708
|
return this.regexp.test(path);
|
|
1689
1709
|
}
|
|
@@ -1720,14 +1740,6 @@ class PathMatcher {
|
|
|
1720
1740
|
params
|
|
1721
1741
|
};
|
|
1722
1742
|
}
|
|
1723
|
-
constructor(path, options){
|
|
1724
|
-
this.regexpKeys = [];
|
|
1725
|
-
this.path = path;
|
|
1726
|
-
this.regexpOptions = options || {};
|
|
1727
|
-
const regexp = pathToRegexp.pathToRegexp(path, options);
|
|
1728
|
-
this.regexp = regexp.regexp;
|
|
1729
|
-
this.regexpKeys = regexp.keys;
|
|
1730
|
-
}
|
|
1731
1743
|
}
|
|
1732
1744
|
|
|
1733
1745
|
function isPath(input) {
|
|
@@ -1735,6 +1747,14 @@ function isPath(input) {
|
|
|
1735
1747
|
}
|
|
1736
1748
|
|
|
1737
1749
|
class Handler {
|
|
1750
|
+
// --------------------------------------------------
|
|
1751
|
+
constructor(handler){
|
|
1752
|
+
this['@instanceof'] = HandlerSymbol;
|
|
1753
|
+
this.config = handler;
|
|
1754
|
+
this.hookManager = new HookManager();
|
|
1755
|
+
this.mountHooks();
|
|
1756
|
+
this.setPath(handler.path);
|
|
1757
|
+
}
|
|
1738
1758
|
// --------------------------------------------------
|
|
1739
1759
|
get type() {
|
|
1740
1760
|
return this.config.type;
|
|
@@ -1766,7 +1786,7 @@ class Handler {
|
|
|
1766
1786
|
}
|
|
1767
1787
|
try {
|
|
1768
1788
|
event.dispatched = await dispatch(event, (done)=>{
|
|
1769
|
-
if (this.config.type ===
|
|
1789
|
+
if (this.config.type === HandlerType.ERROR) {
|
|
1770
1790
|
if (event.error) {
|
|
1771
1791
|
return this.config.fn(event.error, event.request, event.response, done);
|
|
1772
1792
|
}
|
|
@@ -1810,7 +1830,7 @@ class Handler {
|
|
|
1810
1830
|
}
|
|
1811
1831
|
// --------------------------------------------------
|
|
1812
1832
|
matchMethod(method) {
|
|
1813
|
-
return !this.method || method === this.method || method ===
|
|
1833
|
+
return !this.method || method === this.method || method === MethodName.HEAD && this.method === MethodName.GET;
|
|
1814
1834
|
}
|
|
1815
1835
|
setMethod(input) {
|
|
1816
1836
|
const method = toMethodName(input);
|
|
@@ -1829,25 +1849,17 @@ class Handler {
|
|
|
1829
1849
|
this.hookManager.addListener(HookName.ERROR, this.config.onError);
|
|
1830
1850
|
}
|
|
1831
1851
|
}
|
|
1832
|
-
// --------------------------------------------------
|
|
1833
|
-
constructor(handler){
|
|
1834
|
-
this['@instanceof'] = HandlerSymbol;
|
|
1835
|
-
this.config = handler;
|
|
1836
|
-
this.hookManager = new HookManager();
|
|
1837
|
-
this.mountHooks();
|
|
1838
|
-
this.setPath(handler.path);
|
|
1839
|
-
}
|
|
1840
1852
|
}
|
|
1841
1853
|
|
|
1842
1854
|
function coreHandler(input) {
|
|
1843
1855
|
if (typeof input === 'function') {
|
|
1844
1856
|
return new Handler({
|
|
1845
|
-
type:
|
|
1857
|
+
type: HandlerType.CORE,
|
|
1846
1858
|
fn: input
|
|
1847
1859
|
});
|
|
1848
1860
|
}
|
|
1849
1861
|
return new Handler({
|
|
1850
|
-
type:
|
|
1862
|
+
type: HandlerType.CORE,
|
|
1851
1863
|
...input
|
|
1852
1864
|
});
|
|
1853
1865
|
}
|
|
@@ -1855,12 +1867,12 @@ function coreHandler(input) {
|
|
|
1855
1867
|
function errorHandler(input) {
|
|
1856
1868
|
if (typeof input === 'function') {
|
|
1857
1869
|
return new Handler({
|
|
1858
|
-
type:
|
|
1870
|
+
type: HandlerType.ERROR,
|
|
1859
1871
|
fn: input
|
|
1860
1872
|
});
|
|
1861
1873
|
}
|
|
1862
1874
|
return new Handler({
|
|
1863
|
-
type:
|
|
1875
|
+
type: HandlerType.ERROR,
|
|
1864
1876
|
...input
|
|
1865
1877
|
});
|
|
1866
1878
|
}
|
|
@@ -1893,15 +1905,15 @@ function transformRouterOptions(input) {
|
|
|
1893
1905
|
}
|
|
1894
1906
|
|
|
1895
1907
|
const RouterSymbol = Symbol.for('Router');
|
|
1896
|
-
var RouterPipelineStep
|
|
1897
|
-
(function(RouterPipelineStep) {
|
|
1908
|
+
var RouterPipelineStep = /*#__PURE__*/ function(RouterPipelineStep) {
|
|
1898
1909
|
RouterPipelineStep[RouterPipelineStep["START"] = 0] = "START";
|
|
1899
1910
|
RouterPipelineStep[RouterPipelineStep["LOOKUP"] = 1] = "LOOKUP";
|
|
1900
1911
|
RouterPipelineStep[RouterPipelineStep["CHILD_BEFORE"] = 2] = "CHILD_BEFORE";
|
|
1901
1912
|
RouterPipelineStep[RouterPipelineStep["CHILD_DISPATCH"] = 3] = "CHILD_DISPATCH";
|
|
1902
1913
|
RouterPipelineStep[RouterPipelineStep["CHILD_AFTER"] = 4] = "CHILD_AFTER";
|
|
1903
1914
|
RouterPipelineStep[RouterPipelineStep["FINISH"] = 5] = "FINISH";
|
|
1904
|
-
|
|
1915
|
+
return RouterPipelineStep;
|
|
1916
|
+
}({});
|
|
1905
1917
|
|
|
1906
1918
|
let nextId = 0;
|
|
1907
1919
|
function generateRouterID() {
|
|
@@ -1912,6 +1924,20 @@ function isRouterInstance(input) {
|
|
|
1912
1924
|
}
|
|
1913
1925
|
|
|
1914
1926
|
class Router {
|
|
1927
|
+
// --------------------------------------------------
|
|
1928
|
+
constructor(options = {}){
|
|
1929
|
+
this['@instanceof'] = RouterSymbol;
|
|
1930
|
+
/**
|
|
1931
|
+
* Array of mounted layers, routes & routers.
|
|
1932
|
+
*
|
|
1933
|
+
* @protected
|
|
1934
|
+
*/ this.stack = [];
|
|
1935
|
+
this.id = generateRouterID();
|
|
1936
|
+
this.name = options.name;
|
|
1937
|
+
this.hookManager = new HookManager();
|
|
1938
|
+
this.setPath(options.path);
|
|
1939
|
+
setRouterOptions(this.id, transformRouterOptions(options));
|
|
1940
|
+
}
|
|
1915
1941
|
// --------------------------------------------------
|
|
1916
1942
|
matchPath(path) {
|
|
1917
1943
|
if (this.pathMatcher) {
|
|
@@ -1976,7 +2002,7 @@ class Router {
|
|
|
1976
2002
|
let match;
|
|
1977
2003
|
const item = this.stack[context.stackIndex];
|
|
1978
2004
|
if (isHandler(item)) {
|
|
1979
|
-
if (context.event.error && item.type ===
|
|
2005
|
+
if (context.event.error && item.type === HandlerType.CORE || !context.event.error && item.type === HandlerType.ERROR) {
|
|
1980
2006
|
context.stackIndex++;
|
|
1981
2007
|
return this.executePipelineStepLookup(context);
|
|
1982
2008
|
}
|
|
@@ -2050,13 +2076,13 @@ class Router {
|
|
|
2050
2076
|
if (context.event.error || context.event.dispatched) {
|
|
2051
2077
|
return this.hookManager.trigger(HookName.DISPATCH_END, context.event);
|
|
2052
2078
|
}
|
|
2053
|
-
if (!context.event.dispatched && context.event.routerPath.length === 1 && context.event.method && context.event.method ===
|
|
2054
|
-
if (context.event.methodsAllowed.indexOf(
|
|
2055
|
-
context.event.methodsAllowed.push(
|
|
2079
|
+
if (!context.event.dispatched && context.event.routerPath.length === 1 && context.event.method && context.event.method === MethodName.OPTIONS) {
|
|
2080
|
+
if (context.event.methodsAllowed.indexOf(MethodName.GET) !== -1) {
|
|
2081
|
+
context.event.methodsAllowed.push(MethodName.HEAD);
|
|
2056
2082
|
}
|
|
2057
2083
|
smob.distinctArray(context.event.methodsAllowed);
|
|
2058
2084
|
const options = context.event.methodsAllowed.map((key)=>key.toUpperCase()).join(',');
|
|
2059
|
-
context.event.response.setHeader(
|
|
2085
|
+
context.event.response.setHeader(HeaderName.ALLOW, options);
|
|
2060
2086
|
await send(context.event.response, options);
|
|
2061
2087
|
context.event.dispatched = true;
|
|
2062
2088
|
}
|
|
@@ -2090,31 +2116,31 @@ class Router {
|
|
|
2090
2116
|
});
|
|
2091
2117
|
}
|
|
2092
2118
|
delete(...input) {
|
|
2093
|
-
this.useForMethod(
|
|
2119
|
+
this.useForMethod(MethodName.DELETE, ...input);
|
|
2094
2120
|
return this;
|
|
2095
2121
|
}
|
|
2096
2122
|
get(...input) {
|
|
2097
|
-
this.useForMethod(
|
|
2123
|
+
this.useForMethod(MethodName.GET, ...input);
|
|
2098
2124
|
return this;
|
|
2099
2125
|
}
|
|
2100
2126
|
post(...input) {
|
|
2101
|
-
this.useForMethod(
|
|
2127
|
+
this.useForMethod(MethodName.POST, ...input);
|
|
2102
2128
|
return this;
|
|
2103
2129
|
}
|
|
2104
2130
|
put(...input) {
|
|
2105
|
-
this.useForMethod(
|
|
2131
|
+
this.useForMethod(MethodName.PUT, ...input);
|
|
2106
2132
|
return this;
|
|
2107
2133
|
}
|
|
2108
2134
|
patch(...input) {
|
|
2109
|
-
this.useForMethod(
|
|
2135
|
+
this.useForMethod(MethodName.PATCH, ...input);
|
|
2110
2136
|
return this;
|
|
2111
2137
|
}
|
|
2112
2138
|
head(...input) {
|
|
2113
|
-
this.useForMethod(
|
|
2139
|
+
this.useForMethod(MethodName.HEAD, ...input);
|
|
2114
2140
|
return this;
|
|
2115
2141
|
}
|
|
2116
2142
|
options(...input) {
|
|
2117
|
-
this.useForMethod(
|
|
2143
|
+
this.useForMethod(MethodName.OPTIONS, ...input);
|
|
2118
2144
|
return this;
|
|
2119
2145
|
}
|
|
2120
2146
|
// --------------------------------------------------
|
|
@@ -2205,20 +2231,6 @@ class Router {
|
|
|
2205
2231
|
this.hookManager.removeListener(name, fn);
|
|
2206
2232
|
return this;
|
|
2207
2233
|
}
|
|
2208
|
-
// --------------------------------------------------
|
|
2209
|
-
constructor(options = {}){
|
|
2210
|
-
this['@instanceof'] = RouterSymbol;
|
|
2211
|
-
/**
|
|
2212
|
-
* Array of mounted layers, routes & routers.
|
|
2213
|
-
*
|
|
2214
|
-
* @protected
|
|
2215
|
-
*/ this.stack = [];
|
|
2216
|
-
this.id = generateRouterID();
|
|
2217
|
-
this.name = options.name;
|
|
2218
|
-
this.hookManager = new HookManager();
|
|
2219
|
-
this.setPath(options.path);
|
|
2220
|
-
setRouterOptions(this.id, transformRouterOptions(options));
|
|
2221
|
-
}
|
|
2222
2234
|
}
|
|
2223
2235
|
|
|
2224
2236
|
exports.DispatchErrorEvent = DispatchErrorEvent;
|
|
@@ -2226,6 +2238,9 @@ exports.DispatchEvent = DispatchEvent;
|
|
|
2226
2238
|
exports.EventStream = EventStream;
|
|
2227
2239
|
exports.Handler = Handler;
|
|
2228
2240
|
exports.HandlerSymbol = HandlerSymbol;
|
|
2241
|
+
exports.HandlerType = HandlerType;
|
|
2242
|
+
exports.HeaderName = HeaderName;
|
|
2243
|
+
exports.MethodName = MethodName;
|
|
2229
2244
|
exports.PathMatcher = PathMatcher;
|
|
2230
2245
|
exports.Router = Router;
|
|
2231
2246
|
exports.RoutupError = RoutupError;
|