@kaito-http/core 2.2.5 → 2.3.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/declarations/src/error.d.ts +4 -0
- package/dist/declarations/src/index.d.ts +6 -0
- package/dist/declarations/src/req.d.ts +3 -2
- package/dist/declarations/src/route.d.ts +13 -0
- package/dist/declarations/src/router.d.ts +28 -0
- package/dist/declarations/src/server.d.ts +15 -65
- package/dist/declarations/src/util.d.ts +9 -1
- package/dist/kaito-http-core.cjs.dev.js +203 -177
- package/dist/kaito-http-core.cjs.prod.js +203 -177
- package/dist/kaito-http-core.esm.js +179 -179
- package/package.json +2 -1
|
@@ -3,49 +3,47 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var http = require('http');
|
|
6
|
+
var fmw = require('find-my-way');
|
|
6
7
|
var tls = require('tls');
|
|
7
8
|
var getRawBody = require('raw-body');
|
|
8
9
|
|
|
9
10
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
args = arguments;
|
|
34
|
-
return new Promise(function (resolve, reject) {
|
|
35
|
-
var gen = fn.apply(self, args);
|
|
36
|
-
|
|
37
|
-
function _next(value) {
|
|
38
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function _throw(err) {
|
|
42
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
43
|
-
}
|
|
30
|
+
var http__namespace = /*#__PURE__*/_interopNamespace(http);
|
|
31
|
+
var fmw__default = /*#__PURE__*/_interopDefault(fmw);
|
|
32
|
+
var getRawBody__default = /*#__PURE__*/_interopDefault(getRawBody);
|
|
44
33
|
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
function createFMWServer(config) {
|
|
35
|
+
var fmw = config.router.toFindMyWay(config);
|
|
36
|
+
var server = http__namespace.createServer((req, res) => {
|
|
37
|
+
fmw.lookup(req, res);
|
|
38
|
+
});
|
|
39
|
+
return {
|
|
40
|
+
server,
|
|
41
|
+
fmw
|
|
47
42
|
};
|
|
48
43
|
}
|
|
44
|
+
function createServer(config) {
|
|
45
|
+
return createFMWServer(config).server;
|
|
46
|
+
}
|
|
49
47
|
|
|
50
48
|
function _defineProperty(obj, key, value) {
|
|
51
49
|
if (key in obj) {
|
|
@@ -100,6 +98,42 @@ function _objectSpread2(target) {
|
|
|
100
98
|
return target;
|
|
101
99
|
}
|
|
102
100
|
|
|
101
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
102
|
+
try {
|
|
103
|
+
var info = gen[key](arg);
|
|
104
|
+
var value = info.value;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
reject(error);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (info.done) {
|
|
111
|
+
resolve(value);
|
|
112
|
+
} else {
|
|
113
|
+
Promise.resolve(value).then(_next, _throw);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function _asyncToGenerator(fn) {
|
|
118
|
+
return function () {
|
|
119
|
+
var self = this,
|
|
120
|
+
args = arguments;
|
|
121
|
+
return new Promise(function (resolve, reject) {
|
|
122
|
+
var gen = fn.apply(self, args);
|
|
123
|
+
|
|
124
|
+
function _next(value) {
|
|
125
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function _throw(err) {
|
|
129
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
_next(undefined);
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
103
137
|
class WrappedError extends Error {
|
|
104
138
|
static maybe(maybeError) {
|
|
105
139
|
if (maybeError instanceof Error) {
|
|
@@ -118,8 +152,18 @@ class WrappedError extends Error {
|
|
|
118
152
|
this.data = data;
|
|
119
153
|
}
|
|
120
154
|
|
|
155
|
+
}
|
|
156
|
+
class KaitoError extends Error {
|
|
157
|
+
constructor(status, message) {
|
|
158
|
+
super(message);
|
|
159
|
+
this.status = status;
|
|
160
|
+
}
|
|
161
|
+
|
|
121
162
|
}
|
|
122
163
|
|
|
164
|
+
function createGetContext(callback) {
|
|
165
|
+
return callback;
|
|
166
|
+
}
|
|
123
167
|
function getLastEntryInMultiHeaderValue(headerValue) {
|
|
124
168
|
var normalized = Array.isArray(headerValue) ? headerValue.join(',') : headerValue;
|
|
125
169
|
var lastIndex = normalized.lastIndexOf(',');
|
|
@@ -137,8 +181,7 @@ function normalizePath(path) {
|
|
|
137
181
|
}
|
|
138
182
|
|
|
139
183
|
return result;
|
|
140
|
-
}
|
|
141
|
-
|
|
184
|
+
}
|
|
142
185
|
function getInput(_x) {
|
|
143
186
|
return _getInput.apply(this, arguments);
|
|
144
187
|
}
|
|
@@ -174,6 +217,8 @@ function _getInput() {
|
|
|
174
217
|
|
|
175
218
|
class KaitoRequest {
|
|
176
219
|
constructor(raw) {
|
|
220
|
+
_defineProperty(this, "_url", null);
|
|
221
|
+
|
|
177
222
|
this.raw = raw;
|
|
178
223
|
}
|
|
179
224
|
|
|
@@ -184,7 +229,12 @@ class KaitoRequest {
|
|
|
184
229
|
}
|
|
185
230
|
|
|
186
231
|
get url() {
|
|
187
|
-
|
|
232
|
+
if (this._url) {
|
|
233
|
+
return this._url;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
this._url = new URL(this.fullURL);
|
|
237
|
+
return this._url;
|
|
188
238
|
}
|
|
189
239
|
|
|
190
240
|
get method() {
|
|
@@ -240,142 +290,34 @@ class KaitoResponse {
|
|
|
240
290
|
|
|
241
291
|
}
|
|
242
292
|
|
|
243
|
-
function createGetContext(getContext) {
|
|
244
|
-
return getContext;
|
|
245
|
-
}
|
|
246
293
|
class Router {
|
|
247
|
-
static
|
|
248
|
-
|
|
249
|
-
return new RegExp("^".concat(normalized, "/?$"), 'i');
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
constructor(procs) {
|
|
253
|
-
_defineProperty(this, "create", method => (path, proc) => {
|
|
254
|
-
var pattern = Router.patternize(path);
|
|
255
|
-
|
|
256
|
-
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
257
|
-
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
258
|
-
method,
|
|
259
|
-
path,
|
|
260
|
-
pattern
|
|
261
|
-
})
|
|
262
|
-
});
|
|
263
|
-
|
|
264
|
-
return new Router(merged);
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
_defineProperty(this, "merge", (_prefix, router) => {
|
|
268
|
-
var prefix = normalizePath(_prefix);
|
|
269
|
-
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
270
|
-
var [path, proc] = entry;
|
|
271
|
-
var newPath = "".concat(prefix).concat(normalizePath(path));
|
|
272
|
-
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
273
|
-
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
274
|
-
path: newPath,
|
|
275
|
-
pattern: Router.patternize(newPath)
|
|
276
|
-
})
|
|
277
|
-
});
|
|
278
|
-
}, {});
|
|
279
|
-
|
|
280
|
-
var mergedProcs = _objectSpread2(_objectSpread2({}, this.procs), newProcs);
|
|
281
|
-
|
|
282
|
-
return new Router(mergedProcs);
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
_defineProperty(this, "get", this.create('GET'));
|
|
286
|
-
|
|
287
|
-
_defineProperty(this, "post", this.create('POST'));
|
|
288
|
-
|
|
289
|
-
_defineProperty(this, "put", this.create('PUT'));
|
|
290
|
-
|
|
291
|
-
_defineProperty(this, "patch", this.create('PATCH'));
|
|
292
|
-
|
|
293
|
-
_defineProperty(this, "delete", this.create('DELETE'));
|
|
294
|
-
|
|
295
|
-
_defineProperty(this, "head", this.create('HEAD'));
|
|
296
|
-
|
|
297
|
-
_defineProperty(this, "options", this.create('OPTIONS'));
|
|
298
|
-
|
|
299
|
-
_defineProperty(this, "connect", this.create('CONNECT'));
|
|
300
|
-
|
|
301
|
-
_defineProperty(this, "trace", this.create('TRACE'));
|
|
302
|
-
|
|
303
|
-
_defineProperty(this, "acl", this.create('ACL'));
|
|
304
|
-
|
|
305
|
-
_defineProperty(this, "bind", this.create('BIND'));
|
|
306
|
-
|
|
307
|
-
this.procs = procs;
|
|
308
|
-
this._procsArray = Object.values(procs);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
getProcs() {
|
|
312
|
-
return this.procs;
|
|
294
|
+
static create() {
|
|
295
|
+
return new Router({});
|
|
313
296
|
}
|
|
314
297
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
if (proc.method !== method) {
|
|
318
|
-
continue;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
if (proc.pattern.test(url)) {
|
|
322
|
-
return proc;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
return null;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
class KaitoError extends Error {
|
|
331
|
-
constructor(status, message, cause) {
|
|
332
|
-
super(message);
|
|
333
|
-
this.status = status;
|
|
334
|
-
this.cause = cause;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
function createRouter() {
|
|
339
|
-
return new Router({});
|
|
340
|
-
}
|
|
341
|
-
function createServer(config) {
|
|
342
|
-
var log = message => {
|
|
343
|
-
if (config.log === undefined) {
|
|
344
|
-
console.log(message);
|
|
345
|
-
} else if (config.log) {
|
|
346
|
-
config.log(message);
|
|
347
|
-
}
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
return http__default["default"].createServer( /*#__PURE__*/function () {
|
|
351
|
-
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse) {
|
|
352
|
-
var start = Date.now();
|
|
353
|
-
var req = new KaitoRequest(incomingMessage);
|
|
354
|
-
var res = new KaitoResponse(serverResponse);
|
|
355
|
-
|
|
298
|
+
static handle(server, options) {
|
|
299
|
+
return _asyncToGenerator(function* () {
|
|
356
300
|
try {
|
|
357
|
-
var
|
|
358
|
-
|
|
359
|
-
var handler = config.router.find(req.method, req.url.pathname);
|
|
301
|
+
var _options$route$input$, _options$route$input;
|
|
360
302
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
var input = (_handler$input = handler.input) === null || _handler$input === void 0 ? void 0 : _handler$input.parse((_yield$getInput = yield getInput(req)) !== null && _yield$getInput !== void 0 ? _yield$getInput : undefined);
|
|
366
|
-
var context = yield config.getContext(req, res);
|
|
367
|
-
var data = yield handler.run({
|
|
303
|
+
var context = yield server.getContext(options.req, options.res);
|
|
304
|
+
var body = yield getInput(options.req);
|
|
305
|
+
var input = (_options$route$input$ = (_options$route$input = options.route.input) === null || _options$route$input === void 0 ? void 0 : _options$route$input.parse(body)) !== null && _options$route$input$ !== void 0 ? _options$route$input$ : undefined;
|
|
306
|
+
var result = yield options.route.run({
|
|
368
307
|
ctx: context,
|
|
369
|
-
input
|
|
308
|
+
input,
|
|
309
|
+
params: options.params
|
|
370
310
|
});
|
|
371
|
-
res.json({
|
|
311
|
+
options.res.status(200).json({
|
|
372
312
|
success: true,
|
|
373
|
-
data,
|
|
313
|
+
data: result,
|
|
374
314
|
message: 'OK'
|
|
375
315
|
});
|
|
376
|
-
} catch (
|
|
316
|
+
} catch (e) {
|
|
317
|
+
var error = WrappedError.maybe(e);
|
|
318
|
+
|
|
377
319
|
if (error instanceof KaitoError) {
|
|
378
|
-
res.status(error.status).json({
|
|
320
|
+
options.res.status(error.status).json({
|
|
379
321
|
success: false,
|
|
380
322
|
data: null,
|
|
381
323
|
message: error.message
|
|
@@ -384,35 +326,119 @@ function createServer(config) {
|
|
|
384
326
|
}
|
|
385
327
|
|
|
386
328
|
var {
|
|
387
|
-
status
|
|
388
|
-
message
|
|
389
|
-
} = yield
|
|
390
|
-
error
|
|
391
|
-
req,
|
|
392
|
-
res
|
|
329
|
+
status,
|
|
330
|
+
message
|
|
331
|
+
} = yield server.onError({
|
|
332
|
+
error,
|
|
333
|
+
req: options.req,
|
|
334
|
+
res: options.res
|
|
393
335
|
}).catch(() => ({
|
|
394
336
|
status: 500,
|
|
395
|
-
message: '
|
|
337
|
+
message: 'Internal Server Error'
|
|
396
338
|
}));
|
|
397
|
-
res.status(
|
|
339
|
+
options.res.status(status).json({
|
|
398
340
|
success: false,
|
|
399
341
|
data: null,
|
|
400
|
-
message
|
|
342
|
+
message
|
|
401
343
|
});
|
|
402
|
-
} finally {
|
|
403
|
-
var finish = Date.now();
|
|
404
|
-
log("".concat(req.method, " ").concat(req.fullURL, " ").concat(res.raw.statusCode, " ").concat(finish - start, "ms"));
|
|
405
344
|
}
|
|
406
|
-
});
|
|
345
|
+
})();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
constructor(routes) {
|
|
349
|
+
_defineProperty(this, "get", this.make('GET'));
|
|
350
|
+
|
|
351
|
+
_defineProperty(this, "post", this.make('POST'));
|
|
352
|
+
|
|
353
|
+
_defineProperty(this, "head", this.make('HEAD'));
|
|
354
|
+
|
|
355
|
+
_defineProperty(this, "put", this.make('PUT'));
|
|
356
|
+
|
|
357
|
+
_defineProperty(this, "patch", this.make('PATCH'));
|
|
358
|
+
|
|
359
|
+
this.routes = routes;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
merge(prefix, router) {
|
|
363
|
+
var newRoutes = Object.fromEntries(Object.entries(router.routes).map(_ref => {
|
|
364
|
+
var [k, v] = _ref;
|
|
365
|
+
return ["".concat(prefix).concat(k), v];
|
|
366
|
+
}));
|
|
367
|
+
return new Router(_objectSpread2(_objectSpread2({}, this.routes), newRoutes));
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
toFindMyWay(server) {
|
|
371
|
+
var _this = this;
|
|
407
372
|
|
|
408
|
-
|
|
409
|
-
|
|
373
|
+
var instance = fmw__default["default"]({
|
|
374
|
+
defaultRoute(req, serverResponse) {
|
|
375
|
+
var _req$url;
|
|
376
|
+
|
|
377
|
+
var res = new KaitoResponse(serverResponse);
|
|
378
|
+
res.status(404).json({
|
|
379
|
+
success: false,
|
|
380
|
+
data: null,
|
|
381
|
+
message: "Cannot ".concat(req.method, " ").concat((_req$url = req.url) !== null && _req$url !== void 0 ? _req$url : '/')
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
}); // eslint-disable-next-line guard-for-in
|
|
386
|
+
|
|
387
|
+
var _loop = function _loop(path) {
|
|
388
|
+
var route = _this.routes[path];
|
|
389
|
+
instance.on(route.method, path, /*#__PURE__*/function () {
|
|
390
|
+
var _ref2 = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
391
|
+
var req = new KaitoRequest(incomingMessage);
|
|
392
|
+
var res = new KaitoResponse(serverResponse);
|
|
393
|
+
yield Router.handle(server, {
|
|
394
|
+
route,
|
|
395
|
+
params,
|
|
396
|
+
req,
|
|
397
|
+
res
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
return function (_x, _x2, _x3) {
|
|
402
|
+
return _ref2.apply(this, arguments);
|
|
403
|
+
};
|
|
404
|
+
}());
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
for (var path in this.routes) {
|
|
408
|
+
_loop(path);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
return instance;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
make(method) {
|
|
415
|
+
return (path, route) => {
|
|
416
|
+
var mergedRoute = _objectSpread2(_objectSpread2({}, route), {}, {
|
|
417
|
+
method
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
return new Router(_objectSpread2(_objectSpread2({}, this.routes), {}, {
|
|
421
|
+
[path]: mergedRoute
|
|
422
|
+
}));
|
|
410
423
|
};
|
|
411
|
-
}
|
|
424
|
+
}
|
|
425
|
+
|
|
412
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
* @deprecated Please use Router#create instead
|
|
429
|
+
*/
|
|
430
|
+
|
|
431
|
+
var createRouter = Router.create;
|
|
413
432
|
|
|
414
433
|
exports.KaitoError = KaitoError;
|
|
434
|
+
exports.KaitoRequest = KaitoRequest;
|
|
435
|
+
exports.KaitoResponse = KaitoResponse;
|
|
415
436
|
exports.Router = Router;
|
|
437
|
+
exports.WrappedError = WrappedError;
|
|
438
|
+
exports.createFMWServer = createFMWServer;
|
|
416
439
|
exports.createGetContext = createGetContext;
|
|
417
440
|
exports.createRouter = createRouter;
|
|
418
441
|
exports.createServer = createServer;
|
|
442
|
+
exports.getInput = getInput;
|
|
443
|
+
exports.getLastEntryInMultiHeaderValue = getLastEntryInMultiHeaderValue;
|
|
444
|
+
exports.normalizePath = normalizePath;
|