@kaito-http/core 2.2.6 → 2.3.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.
- 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 +205 -176
- package/dist/kaito-http-core.cjs.prod.js +205 -176
- package/dist/kaito-http-core.esm.js +181 -178
- 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) {
|
|
@@ -119,7 +153,17 @@ class WrappedError extends Error {
|
|
|
119
153
|
}
|
|
120
154
|
|
|
121
155
|
}
|
|
156
|
+
class KaitoError extends Error {
|
|
157
|
+
constructor(status, message) {
|
|
158
|
+
super(message);
|
|
159
|
+
this.status = status;
|
|
160
|
+
}
|
|
122
161
|
|
|
162
|
+
}
|
|
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,141 +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 newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
269
|
-
var [path, proc] = entry;
|
|
270
|
-
var newPath = "".concat(prefix).concat(path);
|
|
271
|
-
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
272
|
-
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
273
|
-
path: newPath,
|
|
274
|
-
pattern: Router.patternize(newPath)
|
|
275
|
-
})
|
|
276
|
-
});
|
|
277
|
-
}, {});
|
|
278
|
-
|
|
279
|
-
var mergedProcs = _objectSpread2(_objectSpread2({}, this.procs), newProcs);
|
|
280
|
-
|
|
281
|
-
return new Router(mergedProcs);
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
_defineProperty(this, "get", this.create('GET'));
|
|
285
|
-
|
|
286
|
-
_defineProperty(this, "post", this.create('POST'));
|
|
287
|
-
|
|
288
|
-
_defineProperty(this, "put", this.create('PUT'));
|
|
289
|
-
|
|
290
|
-
_defineProperty(this, "patch", this.create('PATCH'));
|
|
291
|
-
|
|
292
|
-
_defineProperty(this, "delete", this.create('DELETE'));
|
|
293
|
-
|
|
294
|
-
_defineProperty(this, "head", this.create('HEAD'));
|
|
295
|
-
|
|
296
|
-
_defineProperty(this, "options", this.create('OPTIONS'));
|
|
297
|
-
|
|
298
|
-
_defineProperty(this, "connect", this.create('CONNECT'));
|
|
299
|
-
|
|
300
|
-
_defineProperty(this, "trace", this.create('TRACE'));
|
|
301
|
-
|
|
302
|
-
_defineProperty(this, "acl", this.create('ACL'));
|
|
303
|
-
|
|
304
|
-
_defineProperty(this, "bind", this.create('BIND'));
|
|
305
|
-
|
|
306
|
-
this.procs = procs;
|
|
307
|
-
this._procsArray = Object.values(procs);
|
|
294
|
+
static create() {
|
|
295
|
+
return new Router({});
|
|
308
296
|
}
|
|
309
297
|
|
|
310
|
-
|
|
311
|
-
return
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
find(method, url) {
|
|
315
|
-
for (var proc of this._procsArray) {
|
|
316
|
-
if (proc.method !== method) {
|
|
317
|
-
continue;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (proc.pattern.test(url)) {
|
|
321
|
-
return proc;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
return null;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
class KaitoError extends Error {
|
|
330
|
-
constructor(status, message, cause) {
|
|
331
|
-
super(message);
|
|
332
|
-
this.status = status;
|
|
333
|
-
this.cause = cause;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
}
|
|
337
|
-
function createRouter() {
|
|
338
|
-
return new Router({});
|
|
339
|
-
}
|
|
340
|
-
function createServer(config) {
|
|
341
|
-
var log = message => {
|
|
342
|
-
if (config.log === undefined) {
|
|
343
|
-
console.log(message);
|
|
344
|
-
} else if (config.log) {
|
|
345
|
-
config.log(message);
|
|
346
|
-
}
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
return http__default["default"].createServer( /*#__PURE__*/function () {
|
|
350
|
-
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse) {
|
|
351
|
-
var start = Date.now();
|
|
352
|
-
var req = new KaitoRequest(incomingMessage);
|
|
353
|
-
var res = new KaitoResponse(serverResponse);
|
|
354
|
-
|
|
298
|
+
static handle(server, options) {
|
|
299
|
+
return _asyncToGenerator(function* () {
|
|
355
300
|
try {
|
|
356
|
-
var
|
|
357
|
-
|
|
358
|
-
var handler = config.router.find(req.method, req.url.pathname);
|
|
301
|
+
var _options$route$input$, _options$route$input;
|
|
359
302
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
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);
|
|
365
|
-
var context = yield config.getContext(req, res);
|
|
366
|
-
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({
|
|
367
307
|
ctx: context,
|
|
368
|
-
input
|
|
308
|
+
input,
|
|
309
|
+
params: options.params
|
|
369
310
|
});
|
|
370
|
-
res.json({
|
|
311
|
+
options.res.status(200).json({
|
|
371
312
|
success: true,
|
|
372
|
-
data,
|
|
313
|
+
data: result,
|
|
373
314
|
message: 'OK'
|
|
374
315
|
});
|
|
375
|
-
} catch (
|
|
316
|
+
} catch (e) {
|
|
317
|
+
var error = WrappedError.maybe(e);
|
|
318
|
+
|
|
376
319
|
if (error instanceof KaitoError) {
|
|
377
|
-
res.status(error.status).json({
|
|
320
|
+
options.res.status(error.status).json({
|
|
378
321
|
success: false,
|
|
379
322
|
data: null,
|
|
380
323
|
message: error.message
|
|
@@ -383,35 +326,121 @@ function createServer(config) {
|
|
|
383
326
|
}
|
|
384
327
|
|
|
385
328
|
var {
|
|
386
|
-
status
|
|
387
|
-
message
|
|
388
|
-
} = yield
|
|
389
|
-
error
|
|
390
|
-
req,
|
|
391
|
-
res
|
|
329
|
+
status,
|
|
330
|
+
message
|
|
331
|
+
} = yield server.onError({
|
|
332
|
+
error,
|
|
333
|
+
req: options.req,
|
|
334
|
+
res: options.res
|
|
392
335
|
}).catch(() => ({
|
|
393
336
|
status: 500,
|
|
394
|
-
message: '
|
|
337
|
+
message: 'Internal Server Error'
|
|
395
338
|
}));
|
|
396
|
-
res.status(
|
|
339
|
+
options.res.status(status).json({
|
|
397
340
|
success: false,
|
|
398
341
|
data: null,
|
|
399
|
-
message
|
|
342
|
+
message
|
|
400
343
|
});
|
|
401
|
-
} finally {
|
|
402
|
-
var finish = Date.now();
|
|
403
|
-
log("".concat(req.method, " ").concat(req.fullURL, " ").concat(res.raw.statusCode, " ").concat(finish - start, "ms"));
|
|
404
344
|
}
|
|
405
|
-
});
|
|
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;
|
|
372
|
+
|
|
373
|
+
var instance = fmw__default["default"]({
|
|
374
|
+
ignoreTrailingSlash: true,
|
|
406
375
|
|
|
407
|
-
|
|
408
|
-
|
|
376
|
+
defaultRoute(req, serverResponse) {
|
|
377
|
+
var _req$url;
|
|
378
|
+
|
|
379
|
+
var res = new KaitoResponse(serverResponse);
|
|
380
|
+
res.status(404).json({
|
|
381
|
+
success: false,
|
|
382
|
+
data: null,
|
|
383
|
+
message: "Cannot ".concat(req.method, " ").concat((_req$url = req.url) !== null && _req$url !== void 0 ? _req$url : '/')
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
}); // eslint-disable-next-line guard-for-in
|
|
388
|
+
|
|
389
|
+
var _loop = function _loop(path) {
|
|
390
|
+
var route = _this.routes[path];
|
|
391
|
+
instance.on(route.method, path, /*#__PURE__*/function () {
|
|
392
|
+
var _ref2 = _asyncToGenerator(function* (incomingMessage, serverResponse, params) {
|
|
393
|
+
var req = new KaitoRequest(incomingMessage);
|
|
394
|
+
var res = new KaitoResponse(serverResponse);
|
|
395
|
+
yield Router.handle(server, {
|
|
396
|
+
route,
|
|
397
|
+
params,
|
|
398
|
+
req,
|
|
399
|
+
res
|
|
400
|
+
});
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
return function (_x, _x2, _x3) {
|
|
404
|
+
return _ref2.apply(this, arguments);
|
|
405
|
+
};
|
|
406
|
+
}());
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
for (var path in this.routes) {
|
|
410
|
+
_loop(path);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return instance;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
make(method) {
|
|
417
|
+
return (path, route) => {
|
|
418
|
+
var mergedRoute = _objectSpread2(_objectSpread2({}, route), {}, {
|
|
419
|
+
method
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
return new Router(_objectSpread2(_objectSpread2({}, this.routes), {}, {
|
|
423
|
+
[path]: mergedRoute
|
|
424
|
+
}));
|
|
409
425
|
};
|
|
410
|
-
}
|
|
426
|
+
}
|
|
427
|
+
|
|
411
428
|
}
|
|
429
|
+
/**
|
|
430
|
+
* @deprecated Please use Router#create instead
|
|
431
|
+
*/
|
|
432
|
+
|
|
433
|
+
var createRouter = Router.create;
|
|
412
434
|
|
|
413
435
|
exports.KaitoError = KaitoError;
|
|
436
|
+
exports.KaitoRequest = KaitoRequest;
|
|
437
|
+
exports.KaitoResponse = KaitoResponse;
|
|
414
438
|
exports.Router = Router;
|
|
439
|
+
exports.WrappedError = WrappedError;
|
|
440
|
+
exports.createFMWServer = createFMWServer;
|
|
415
441
|
exports.createGetContext = createGetContext;
|
|
416
442
|
exports.createRouter = createRouter;
|
|
417
443
|
exports.createServer = createServer;
|
|
444
|
+
exports.getInput = getInput;
|
|
445
|
+
exports.getLastEntryInMultiHeaderValue = getLastEntryInMultiHeaderValue;
|
|
446
|
+
exports.normalizePath = normalizePath;
|