@kaito-http/core 2.2.7 → 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 +2 -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 -71
- package/dist/declarations/src/util.d.ts +9 -1
- package/dist/kaito-http-core.cjs.dev.js +194 -175
- package/dist/kaito-http-core.cjs.prod.js +194 -175
- package/dist/kaito-http-core.esm.js +170 -177
- 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
|
}
|
|
@@ -247,142 +290,34 @@ class KaitoResponse {
|
|
|
247
290
|
|
|
248
291
|
}
|
|
249
292
|
|
|
250
|
-
function createGetContext(getContext) {
|
|
251
|
-
return getContext;
|
|
252
|
-
}
|
|
253
293
|
class Router {
|
|
254
|
-
static
|
|
255
|
-
|
|
256
|
-
return new RegExp("^".concat(normalized, "/?$"), 'i');
|
|
294
|
+
static create() {
|
|
295
|
+
return new Router({});
|
|
257
296
|
}
|
|
258
297
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
var pattern = Router.patternize(path);
|
|
262
|
-
|
|
263
|
-
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
264
|
-
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
265
|
-
method,
|
|
266
|
-
path,
|
|
267
|
-
pattern
|
|
268
|
-
})
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
return new Router(merged);
|
|
272
|
-
});
|
|
273
|
-
|
|
274
|
-
_defineProperty(this, "merge", (prefix, router) => {
|
|
275
|
-
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
276
|
-
var [path, proc] = entry;
|
|
277
|
-
var newPath = "".concat(prefix).concat(path);
|
|
278
|
-
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
279
|
-
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
280
|
-
path: newPath,
|
|
281
|
-
pattern: Router.patternize(newPath)
|
|
282
|
-
})
|
|
283
|
-
});
|
|
284
|
-
}, {});
|
|
285
|
-
|
|
286
|
-
var mergedProcs = _objectSpread2(_objectSpread2({}, this.procs), newProcs);
|
|
287
|
-
|
|
288
|
-
return new Router(mergedProcs);
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
_defineProperty(this, "get", this.create('GET'));
|
|
292
|
-
|
|
293
|
-
_defineProperty(this, "post", this.create('POST'));
|
|
294
|
-
|
|
295
|
-
_defineProperty(this, "put", this.create('PUT'));
|
|
296
|
-
|
|
297
|
-
_defineProperty(this, "patch", this.create('PATCH'));
|
|
298
|
-
|
|
299
|
-
_defineProperty(this, "delete", this.create('DELETE'));
|
|
300
|
-
|
|
301
|
-
_defineProperty(this, "head", this.create('HEAD'));
|
|
302
|
-
|
|
303
|
-
_defineProperty(this, "options", this.create('OPTIONS'));
|
|
304
|
-
|
|
305
|
-
_defineProperty(this, "connect", this.create('CONNECT'));
|
|
306
|
-
|
|
307
|
-
_defineProperty(this, "trace", this.create('TRACE'));
|
|
308
|
-
|
|
309
|
-
_defineProperty(this, "acl", this.create('ACL'));
|
|
310
|
-
|
|
311
|
-
_defineProperty(this, "bind", this.create('BIND'));
|
|
312
|
-
|
|
313
|
-
this.procs = procs;
|
|
314
|
-
this._procsArray = Object.values(procs);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
getProcs() {
|
|
318
|
-
return this.procs;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
find(method, url) {
|
|
322
|
-
for (var proc of this._procsArray) {
|
|
323
|
-
if (proc.method !== method) {
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
if (proc.pattern.test(url)) {
|
|
328
|
-
return proc;
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
return null;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
class KaitoError extends Error {
|
|
337
|
-
constructor(status, message, cause) {
|
|
338
|
-
super(message);
|
|
339
|
-
this.status = status;
|
|
340
|
-
this.cause = cause;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
}
|
|
344
|
-
function createRouter() {
|
|
345
|
-
return new Router({});
|
|
346
|
-
}
|
|
347
|
-
function createServer(config) {
|
|
348
|
-
var log = message => {
|
|
349
|
-
if (config.log === undefined) {
|
|
350
|
-
console.log(message);
|
|
351
|
-
} else if (config.log) {
|
|
352
|
-
config.log(message);
|
|
353
|
-
}
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
return http__default["default"].createServer( /*#__PURE__*/function () {
|
|
357
|
-
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse) {
|
|
358
|
-
var start = Date.now();
|
|
359
|
-
var req = new KaitoRequest(incomingMessage);
|
|
360
|
-
var res = new KaitoResponse(serverResponse);
|
|
361
|
-
|
|
298
|
+
static handle(server, options) {
|
|
299
|
+
return _asyncToGenerator(function* () {
|
|
362
300
|
try {
|
|
363
|
-
var
|
|
364
|
-
|
|
365
|
-
var handler = config.router.find(req.method, req.raw.url);
|
|
301
|
+
var _options$route$input$, _options$route$input;
|
|
366
302
|
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
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);
|
|
372
|
-
var context = yield config.getContext(req, res);
|
|
373
|
-
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({
|
|
374
307
|
ctx: context,
|
|
375
308
|
input,
|
|
376
|
-
params:
|
|
309
|
+
params: options.params
|
|
377
310
|
});
|
|
378
|
-
res.json({
|
|
311
|
+
options.res.status(200).json({
|
|
379
312
|
success: true,
|
|
380
|
-
data,
|
|
313
|
+
data: result,
|
|
381
314
|
message: 'OK'
|
|
382
315
|
});
|
|
383
|
-
} catch (
|
|
316
|
+
} catch (e) {
|
|
317
|
+
var error = WrappedError.maybe(e);
|
|
318
|
+
|
|
384
319
|
if (error instanceof KaitoError) {
|
|
385
|
-
res.status(error.status).json({
|
|
320
|
+
options.res.status(error.status).json({
|
|
386
321
|
success: false,
|
|
387
322
|
data: null,
|
|
388
323
|
message: error.message
|
|
@@ -391,35 +326,119 @@ function createServer(config) {
|
|
|
391
326
|
}
|
|
392
327
|
|
|
393
328
|
var {
|
|
394
|
-
status
|
|
395
|
-
message
|
|
396
|
-
} = yield
|
|
397
|
-
error
|
|
398
|
-
req,
|
|
399
|
-
res
|
|
329
|
+
status,
|
|
330
|
+
message
|
|
331
|
+
} = yield server.onError({
|
|
332
|
+
error,
|
|
333
|
+
req: options.req,
|
|
334
|
+
res: options.res
|
|
400
335
|
}).catch(() => ({
|
|
401
336
|
status: 500,
|
|
402
|
-
message: '
|
|
337
|
+
message: 'Internal Server Error'
|
|
403
338
|
}));
|
|
404
|
-
res.status(
|
|
339
|
+
options.res.status(status).json({
|
|
405
340
|
success: false,
|
|
406
341
|
data: null,
|
|
407
|
-
message
|
|
342
|
+
message
|
|
408
343
|
});
|
|
409
|
-
} finally {
|
|
410
|
-
var finish = Date.now();
|
|
411
|
-
log("".concat(req.method, " ").concat(req.fullURL, " ").concat(res.raw.statusCode, " ").concat(finish - start, "ms"));
|
|
412
344
|
}
|
|
413
|
-
});
|
|
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;
|
|
414
372
|
|
|
415
|
-
|
|
416
|
-
|
|
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
|
+
}));
|
|
417
423
|
};
|
|
418
|
-
}
|
|
424
|
+
}
|
|
425
|
+
|
|
419
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
* @deprecated Please use Router#create instead
|
|
429
|
+
*/
|
|
430
|
+
|
|
431
|
+
var createRouter = Router.create;
|
|
420
432
|
|
|
421
433
|
exports.KaitoError = KaitoError;
|
|
434
|
+
exports.KaitoRequest = KaitoRequest;
|
|
435
|
+
exports.KaitoResponse = KaitoResponse;
|
|
422
436
|
exports.Router = Router;
|
|
437
|
+
exports.WrappedError = WrappedError;
|
|
438
|
+
exports.createFMWServer = createFMWServer;
|
|
423
439
|
exports.createGetContext = createGetContext;
|
|
424
440
|
exports.createRouter = createRouter;
|
|
425
441
|
exports.createServer = createServer;
|
|
442
|
+
exports.getInput = getInput;
|
|
443
|
+
exports.getLastEntryInMultiHeaderValue = getLastEntryInMultiHeaderValue;
|
|
444
|
+
exports.normalizePath = normalizePath;
|