routup 0.12.0 → 0.13.1

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.
Files changed (66) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +11 -10
  3. package/dist/handler/index.d.ts +1 -1
  4. package/dist/handler/utils.d.ts +2 -2
  5. package/dist/handler/utils.d.ts.map +1 -1
  6. package/dist/index.cjs +579 -0
  7. package/dist/index.cjs.map +1 -0
  8. package/dist/index.d.ts +8 -8
  9. package/dist/index.mjs +561 -0
  10. package/dist/index.mjs.map +1 -0
  11. package/dist/layer/index.d.ts +3 -3
  12. package/dist/layer/module.d.ts +15 -15
  13. package/dist/layer/module.d.ts.map +1 -1
  14. package/dist/layer/type.d.ts +6 -6
  15. package/dist/layer/type.d.ts.map +1 -1
  16. package/dist/layer/utils.d.ts +2 -2
  17. package/dist/layer/utils.d.ts.map +1 -1
  18. package/dist/path/index.d.ts +2 -2
  19. package/dist/path/matcher.d.ts +12 -12
  20. package/dist/path/matcher.d.ts.map +1 -1
  21. package/dist/path/type.d.ts +6 -6
  22. package/dist/path/type.d.ts.map +1 -1
  23. package/dist/route/index.d.ts +3 -3
  24. package/dist/route/module.d.ts +28 -26
  25. package/dist/route/module.d.ts.map +1 -1
  26. package/dist/route/type.d.ts +6 -6
  27. package/dist/route/type.d.ts.map +1 -1
  28. package/dist/route/utils.d.ts +2 -2
  29. package/dist/route/utils.d.ts.map +1 -1
  30. package/dist/router/index.d.ts +2 -2
  31. package/dist/router/module.d.ts +71 -70
  32. package/dist/router/module.d.ts.map +1 -1
  33. package/dist/router/type.d.ts +24 -24
  34. package/dist/router/type.d.ts.map +1 -1
  35. package/dist/type.d.ts +13 -13
  36. package/dist/utils/index.d.ts +4 -4
  37. package/dist/utils/is-instance.d.ts +1 -1
  38. package/dist/utils/path.d.ts +2 -2
  39. package/dist/utils/path.d.ts.map +1 -1
  40. package/dist/utils/promise.d.ts +1 -1
  41. package/dist/utils/request.d.ts +2 -2
  42. package/dist/utils/request.d.ts.map +1 -1
  43. package/package.json +17 -8
  44. package/dist/handler/index.js +0 -21
  45. package/dist/handler/utils.js +0 -29
  46. package/dist/index.js +0 -28
  47. package/dist/layer/index.js +0 -23
  48. package/dist/layer/module.js +0 -74
  49. package/dist/layer/type.js +0 -9
  50. package/dist/layer/utils.js +0 -17
  51. package/dist/path/index.js +0 -22
  52. package/dist/path/matcher.js +0 -74
  53. package/dist/path/type.js +0 -9
  54. package/dist/route/index.js +0 -23
  55. package/dist/route/module.js +0 -135
  56. package/dist/route/type.js +0 -9
  57. package/dist/route/utils.js +0 -17
  58. package/dist/router/index.js +0 -22
  59. package/dist/router/module.js +0 -294
  60. package/dist/router/type.js +0 -9
  61. package/dist/type.js +0 -9
  62. package/dist/utils/index.js +0 -24
  63. package/dist/utils/is-instance.js +0 -16
  64. package/dist/utils/path.js +0 -16
  65. package/dist/utils/promise.js +0 -19
  66. package/dist/utils/request.js +0 -33
@@ -1,294 +0,0 @@
1
- /*
2
- * Copyright (c) 2022-2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- function _export(target, all) {
11
- for(var name in all)Object.defineProperty(target, name, {
12
- enumerable: true,
13
- get: all[name]
14
- });
15
- }
16
- _export(exports, {
17
- isRouterInstance: ()=>isRouterInstance,
18
- Router: ()=>Router
19
- });
20
- const _http = require("@ebec/http");
21
- const _http1 = require("http");
22
- const _smob = require("smob");
23
- const _core = require("@routup/core");
24
- const _path = require("../path");
25
- const _utils = require("../utils");
26
- const _layer = require("../layer");
27
- const _route = require("../route");
28
- function _extends() {
29
- _extends = Object.assign || function(target) {
30
- for(var i = 1; i < arguments.length; i++){
31
- var source = arguments[i];
32
- for(var key in source){
33
- if (Object.prototype.hasOwnProperty.call(source, key)) {
34
- target[key] = source[key];
35
- }
36
- }
37
- }
38
- return target;
39
- };
40
- return _extends.apply(this, arguments);
41
- }
42
- function isRouterInstance(input) {
43
- return (0, _utils.isInstance)(input, 'Router');
44
- }
45
- class Router {
46
- // --------------------------------------------------
47
- setPathMatcherOptions(input) {
48
- this.pathMatcherOptions = input;
49
- if (this.pathMatcher) {
50
- this.pathMatcher.regexpOptions = this.pathMatcherOptions;
51
- }
52
- }
53
- setPath(value) {
54
- if (value === '/' || !(0, _utils.isPath)(value)) {
55
- this.path = '/';
56
- return;
57
- }
58
- if (typeof value === 'string') {
59
- this.path = (0, _core.withLeadingSlash)((0, _core.withoutTrailingSlash)(`${value}`));
60
- } else {
61
- this.path = value;
62
- }
63
- this.pathMatcher = new _path.PathMatcher(this.path, this.pathMatcherOptions);
64
- }
65
- // --------------------------------------------------
66
- createListener() {
67
- this.isRoot = true;
68
- return (req, res)=>{
69
- this.dispatch(req, res);
70
- };
71
- }
72
- /* istanbul ignore next */ listen(port) {
73
- const server = (0, _http1.createServer)(this.createListener());
74
- return server.listen(port);
75
- }
76
- // --------------------------------------------------
77
- matchPath(path) {
78
- if (this.pathMatcher) {
79
- return this.pathMatcher.test(path);
80
- }
81
- return true;
82
- }
83
- // --------------------------------------------------
84
- dispatch(req, res, meta, done) {
85
- let index = -1;
86
- meta = meta || {};
87
- let allowedMethods = [];
88
- if (this.isRoot && typeof this.timeout === 'number') {
89
- (0, _utils.createRequestTimeout)(res, this.timeout, done);
90
- }
91
- const fn = (err)=>{
92
- /* istanbul ignore if */ if (!this.isRoot) {
93
- if (typeof done !== 'undefined') {
94
- setImmediate(()=>done(err));
95
- }
96
- return;
97
- }
98
- if (typeof err !== 'undefined') {
99
- res.statusCode = _http.BadRequestErrorOptions.statusCode;
100
- res.statusMessage = _http.BadRequestErrorOptions.message;
101
- res.end();
102
- return;
103
- }
104
- if (req.method && req.method.toLowerCase() === _core.Method.OPTIONS) {
105
- const options = allowedMethods.map((key)=>key.toUpperCase()).join(',');
106
- res.setHeader(_core.HeaderName.ALLOW, options);
107
- (0, _core.send)(res, options);
108
- return;
109
- }
110
- res.statusCode = _http.NotFoundErrorOptions.statusCode;
111
- res.end();
112
- };
113
- let path = meta.path || (0, _core.useRequestPath)(req);
114
- if (this.pathMatcher) {
115
- const output = this.pathMatcher.exec(path);
116
- if (typeof output !== 'undefined') {
117
- meta.mountPath = (0, _core.cleanDoubleSlashes)(`${meta.mountPath || ''}/${output.path}`);
118
- if (path === output.path) {
119
- path = '/';
120
- } else {
121
- path = (0, _core.withLeadingSlash)(path.substring(output.path.length));
122
- }
123
- meta.params = (0, _smob.merge)(meta.params || {}, output.params);
124
- }
125
- }
126
- meta.path = path;
127
- if (!meta.mountPath) {
128
- meta.mountPath = '/';
129
- }
130
- const next = (err)=>{
131
- if (index >= this.stack.length) {
132
- setImmediate(fn, err);
133
- return;
134
- }
135
- let layer;
136
- let match = false;
137
- while(!match && index < this.stack.length){
138
- index++;
139
- layer = this.stack[index];
140
- if ((0, _layer.isLayerInstance)(layer)) {
141
- if (!layer.isError() && err) {
142
- continue;
143
- }
144
- match = layer.matchPath(path);
145
- }
146
- if (isRouterInstance(layer)) {
147
- match = layer.matchPath(path);
148
- }
149
- if ((0, _route.isRouteInstance)(layer)) {
150
- match = layer.matchPath(path);
151
- if (req.method && !layer.matchMethod(req.method)) {
152
- match = false;
153
- if (req.method.toLowerCase() === _core.Method.OPTIONS) {
154
- allowedMethods = (0, _smob.mergeArrays)(allowedMethods, layer.getMethods(), true);
155
- }
156
- }
157
- }
158
- }
159
- if (!match || !layer) {
160
- setImmediate(fn, err);
161
- return;
162
- }
163
- const layerMeta = _extends({}, meta);
164
- if ((0, _layer.isLayerInstance)(layer)) {
165
- const output = layer.exec(path);
166
- if (output) {
167
- layerMeta.params = (0, _smob.merge)(output.params, layerMeta.params || {});
168
- layerMeta.mountPath = (0, _core.cleanDoubleSlashes)(`${layerMeta.mountPath || ''}/${output.path}`);
169
- }
170
- }
171
- if (err) {
172
- if ((0, _layer.isLayerInstance)(layer) && layer.isError()) {
173
- layer.dispatch(req, res, layerMeta, next, err);
174
- return;
175
- }
176
- /* istanbul ignore next */ setImmediate(next, err);
177
- return;
178
- }
179
- layer.dispatch(req, res, layerMeta, next);
180
- };
181
- next();
182
- }
183
- /* istanbul ignore next */ dispatchAsync(req, res) {
184
- return new Promise((resolve, reject)=>{
185
- this.dispatch(req, res, {}, (err)=>{
186
- if (err) {
187
- reject(err);
188
- return;
189
- }
190
- resolve();
191
- });
192
- });
193
- }
194
- // --------------------------------------------------
195
- route(path) {
196
- if (typeof path === 'string' && path.length > 0) {
197
- path = (0, _core.withLeadingSlash)(path);
198
- }
199
- const index = this.stack.findIndex((item)=>(0, _route.isRouteInstance)(item) && item.path === path);
200
- if (index !== -1) {
201
- return this.stack[index];
202
- }
203
- const route = new _route.Route({
204
- path,
205
- pathMatcher: {
206
- sensitive: this.pathMatcherOptions.sensitive
207
- }
208
- });
209
- this.stack.push(route);
210
- return route;
211
- }
212
- delete(path, ...handlers) {
213
- const route = this.route(path);
214
- route.delete(...handlers);
215
- return this;
216
- }
217
- get(path, ...handlers) {
218
- const route = this.route(path);
219
- route.get(...handlers);
220
- return this;
221
- }
222
- post(path, ...handlers) {
223
- const route = this.route(path);
224
- route.post(...handlers);
225
- return this;
226
- }
227
- put(path, ...handlers) {
228
- const route = this.route(path);
229
- route.put(...handlers);
230
- return this;
231
- }
232
- patch(path, ...handlers) {
233
- const route = this.route(path);
234
- route.patch(...handlers);
235
- return this;
236
- }
237
- head(path, ...handlers) {
238
- const route = this.route(path);
239
- route.head(...handlers);
240
- return this;
241
- }
242
- options(path, ...handlers) {
243
- const route = this.route(path);
244
- route.options(...handlers);
245
- return this;
246
- }
247
- use(...input) {
248
- /* istanbul ignore next */ if (input.length === 0) {
249
- return this;
250
- }
251
- let path;
252
- if ((0, _utils.isPath)(input[0])) {
253
- path = input.shift();
254
- }
255
- for(let i = 0; i < input.length; i++){
256
- const item = input[i];
257
- if (isRouterInstance(item)) {
258
- if (path) {
259
- item.setPath(path);
260
- }
261
- item.setPathMatcherOptions(this.pathMatcherOptions);
262
- this.stack.push(item);
263
- continue;
264
- }
265
- if (typeof item === 'function') {
266
- this.stack.push(new _layer.Layer({
267
- path: path || '/',
268
- pathMatcher: {
269
- strict: false,
270
- end: false,
271
- sensitive: this.pathMatcherOptions.sensitive
272
- }
273
- }, item));
274
- }
275
- }
276
- return this;
277
- }
278
- // --------------------------------------------------
279
- constructor(ctx){
280
- this['@instanceof'] = Symbol.for('Router');
281
- /**
282
- * Array of mounted layers, routes & routers.
283
- *
284
- * @protected
285
- */ this.stack = [];
286
- ctx = ctx || {};
287
- this.pathMatcherOptions = _extends({
288
- end: false,
289
- sensitive: true
290
- }, ctx.pathMatcher || {});
291
- this.timeout = ctx.timeout;
292
- this.setPath(ctx.path || '/');
293
- }
294
- }
@@ -1,9 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
package/dist/type.js DELETED
@@ -1,9 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
@@ -1,24 +0,0 @@
1
- /*
2
- * Copyright (c) 2022-2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- _exportStar(require("./is-instance"), exports);
11
- _exportStar(require("./path"), exports);
12
- _exportStar(require("./promise"), exports);
13
- _exportStar(require("./request"), exports);
14
- function _exportStar(from, to) {
15
- Object.keys(from).forEach(function(k) {
16
- if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) Object.defineProperty(to, k, {
17
- enumerable: true,
18
- get: function() {
19
- return from[k];
20
- }
21
- });
22
- });
23
- return from;
24
- }
@@ -1,16 +0,0 @@
1
- /*
2
- * Copyright (c) 2022-2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "isInstance", {
11
- enumerable: true,
12
- get: ()=>isInstance
13
- });
14
- function isInstance(input, name) {
15
- return typeof input === 'object' && input !== null && input['@instanceof'] === Symbol.for(name);
16
- }
@@ -1,16 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "isPath", {
11
- enumerable: true,
12
- get: ()=>isPath
13
- });
14
- function isPath(input) {
15
- return typeof input === 'string' || input instanceof RegExp;
16
- }
@@ -1,19 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "isPromise", {
11
- enumerable: true,
12
- get: ()=>isPromise
13
- });
14
- const _smob = require("smob");
15
- function isPromise(p) {
16
- return (0, _smob.isObject)(p) && (p instanceof Promise || // eslint-disable-next-line @typescript-eslint/ban-ts-comment
17
- // @ts-ignore
18
- typeof p.then === 'function');
19
- }
@@ -1,33 +0,0 @@
1
- /*
2
- * Copyright (c) 2022.
3
- * Author Peter Placzek (tada5hi)
4
- * For the full copyright and license information,
5
- * view the LICENSE file that was distributed with this source code.
6
- */ "use strict";
7
- Object.defineProperty(exports, "__esModule", {
8
- value: true
9
- });
10
- Object.defineProperty(exports, "createRequestTimeout", {
11
- enumerable: true,
12
- get: ()=>createRequestTimeout
13
- });
14
- const _http = require("@ebec/http");
15
- function createRequestTimeout(res, timeout, done) {
16
- const instance = setTimeout(()=>{
17
- res.statusCode = _http.GatewayTimeoutErrorOptions.statusCode;
18
- res.statusMessage = _http.GatewayTimeoutErrorOptions.message;
19
- res.end();
20
- }, timeout);
21
- res.once('close', ()=>{
22
- clearTimeout(instance);
23
- if (typeof done === 'function') {
24
- done();
25
- }
26
- });
27
- res.once('error', (e)=>{
28
- clearTimeout(instance);
29
- if (typeof done === 'function') {
30
- done(e);
31
- }
32
- });
33
- }