@kaito-http/core 2.0.2 → 2.2.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 +6 -0
- package/dist/declarations/src/req.d.ts +13 -0
- package/dist/declarations/src/res.d.ts +21 -0
- package/dist/declarations/src/server.d.ts +92 -51
- package/dist/declarations/src/util.d.ts +9 -0
- package/dist/kaito-http-core.cjs.dev.js +254 -86
- package/dist/kaito-http-core.cjs.prod.js +254 -86
- package/dist/kaito-http-core.esm.js +253 -86
- package/package.json +5 -8
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var http = require('http');
|
|
6
|
+
var tls = require('tls');
|
|
7
|
+
var getRawBody = require('raw-body');
|
|
6
8
|
|
|
7
9
|
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
8
10
|
|
|
9
|
-
var
|
|
11
|
+
var http__default = /*#__PURE__*/_interopDefault(http);
|
|
12
|
+
var getRawBody__default = /*#__PURE__*/_interopDefault(getRawBody);
|
|
10
13
|
|
|
11
14
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
12
15
|
try {
|
|
@@ -97,35 +100,174 @@ function _objectSpread2(target) {
|
|
|
97
100
|
return target;
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
|
|
103
|
+
class WrappedError extends Error {
|
|
104
|
+
static maybe(maybeError) {
|
|
105
|
+
if (maybeError instanceof Error) {
|
|
106
|
+
return maybeError;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return WrappedError.from(maybeError);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static from(data) {
|
|
113
|
+
return new WrappedError(data);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
constructor(data) {
|
|
117
|
+
super('Something was thrown, but it was not an instance of Error, so a WrappedError was created.');
|
|
118
|
+
this.data = data;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function getLastEntryInMultiHeaderValue(headerValue) {
|
|
124
|
+
var normalized = Array.isArray(headerValue) ? headerValue.join(',') : headerValue;
|
|
125
|
+
var lastIndex = normalized.lastIndexOf(',');
|
|
126
|
+
return lastIndex === -1 ? normalized.trim() : normalized.slice(lastIndex + 1).trim();
|
|
127
|
+
}
|
|
128
|
+
function normalizePath(path) {
|
|
129
|
+
var result = path;
|
|
130
|
+
|
|
131
|
+
if (!result.startsWith('/')) {
|
|
132
|
+
result = "/".concat(result);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (result.endsWith('/')) {
|
|
136
|
+
result = result.slice(-1);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return result;
|
|
140
|
+
} // Type for import('http').METHODS
|
|
141
|
+
|
|
142
|
+
function getInput(_x) {
|
|
143
|
+
return _getInput.apply(this, arguments);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function _getInput() {
|
|
147
|
+
_getInput = _asyncToGenerator(function* (req) {
|
|
148
|
+
if (req.method === 'GET') {
|
|
149
|
+
var input = req.url.searchParams.get('input');
|
|
150
|
+
|
|
151
|
+
if (!input) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return JSON.parse(input);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
var buffer = yield getRawBody__default["default"](req.raw);
|
|
159
|
+
|
|
160
|
+
switch (req.headers['content-type']) {
|
|
161
|
+
case 'application/json':
|
|
162
|
+
{
|
|
163
|
+
return JSON.parse(buffer.toString());
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
default:
|
|
167
|
+
{
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
return _getInput.apply(this, arguments);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
class KaitoRequest {
|
|
176
|
+
constructor(raw) {
|
|
177
|
+
this.raw = raw;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
get fullURL() {
|
|
181
|
+
var _this$raw$url;
|
|
182
|
+
|
|
183
|
+
return "".concat(this.protocol, "://").concat(this.hostname).concat((_this$raw$url = this.raw.url) !== null && _this$raw$url !== void 0 ? _this$raw$url : '');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
get url() {
|
|
187
|
+
return new URL(this.fullURL);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
get method() {
|
|
191
|
+
if (!this.raw.method) {
|
|
192
|
+
throw new Error('Request method is not defined, somehow...');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return this.raw.method;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
get protocol() {
|
|
199
|
+
if (this.raw.socket instanceof tls.TLSSocket) {
|
|
200
|
+
return this.raw.socket.encrypted ? 'https' : 'http';
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return 'http';
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
get headers() {
|
|
207
|
+
return this.raw.headers;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
get hostname() {
|
|
211
|
+
var _this$raw$headers$hos, _this$raw$headers$Au;
|
|
212
|
+
|
|
213
|
+
return (_this$raw$headers$hos = this.raw.headers.host) !== null && _this$raw$headers$hos !== void 0 ? _this$raw$headers$hos : getLastEntryInMultiHeaderValue((_this$raw$headers$Au = this.raw.headers[':authority']) !== null && _this$raw$headers$Au !== void 0 ? _this$raw$headers$Au : []);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
}
|
|
101
217
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
218
|
+
class KaitoResponse {
|
|
219
|
+
constructor(raw) {
|
|
220
|
+
this.raw = raw;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
header(key, value) {
|
|
224
|
+
this.raw.setHeader(key, value);
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
status(code) {
|
|
229
|
+
this.raw.statusCode = code;
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
json(data) {
|
|
234
|
+
var json = JSON.stringify(data);
|
|
235
|
+
this.raw.setHeader('Content-Type', 'application/json');
|
|
236
|
+
this.raw.setHeader('Content-Length', Buffer.byteLength(json));
|
|
237
|
+
this.raw.end(json);
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
}
|
|
108
242
|
|
|
109
243
|
function createGetContext(getContext) {
|
|
110
244
|
return getContext;
|
|
111
245
|
}
|
|
112
246
|
class Router {
|
|
113
247
|
constructor(procs) {
|
|
114
|
-
_defineProperty(this, "create", method => (
|
|
115
|
-
|
|
116
|
-
|
|
248
|
+
_defineProperty(this, "create", method => (path, proc) => {
|
|
249
|
+
var stripped = normalizePath(path);
|
|
250
|
+
var pattern = new RegExp("^".concat(stripped, "/?$"), 'i');
|
|
251
|
+
|
|
252
|
+
var merged = _objectSpread2(_objectSpread2({}, this.procs), {}, {
|
|
253
|
+
[path]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
117
254
|
method,
|
|
118
|
-
|
|
255
|
+
path,
|
|
256
|
+
pattern
|
|
119
257
|
})
|
|
120
|
-
})
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
return new Router(merged);
|
|
121
261
|
});
|
|
122
262
|
|
|
123
|
-
_defineProperty(this, "merge", (
|
|
263
|
+
_defineProperty(this, "merge", (_prefix, router) => {
|
|
264
|
+
var prefix = normalizePath(_prefix);
|
|
124
265
|
var newProcs = Object.entries(router.getProcs()).reduce((all, entry) => {
|
|
125
|
-
var [
|
|
266
|
+
var [_path, proc] = entry;
|
|
267
|
+
var path = normalizePath(_path);
|
|
126
268
|
return _objectSpread2(_objectSpread2({}, all), {}, {
|
|
127
|
-
["".concat(prefix).concat(
|
|
128
|
-
|
|
269
|
+
["".concat(prefix).concat(path)]: _objectSpread2(_objectSpread2({}, proc), {}, {
|
|
270
|
+
path: "".concat(prefix).concat(path)
|
|
129
271
|
})
|
|
130
272
|
});
|
|
131
273
|
}, {});
|
|
@@ -135,26 +277,55 @@ class Router {
|
|
|
135
277
|
return new Router(mergedProcs);
|
|
136
278
|
});
|
|
137
279
|
|
|
138
|
-
_defineProperty(this, "get", this.create(
|
|
280
|
+
_defineProperty(this, "get", this.create('GET'));
|
|
281
|
+
|
|
282
|
+
_defineProperty(this, "post", this.create('POST'));
|
|
139
283
|
|
|
140
|
-
_defineProperty(this, "
|
|
284
|
+
_defineProperty(this, "put", this.create('PUT'));
|
|
141
285
|
|
|
142
|
-
_defineProperty(this, "patch", this.create(
|
|
286
|
+
_defineProperty(this, "patch", this.create('PATCH'));
|
|
143
287
|
|
|
144
|
-
_defineProperty(this, "delete", this.create(
|
|
288
|
+
_defineProperty(this, "delete", this.create('DELETE'));
|
|
289
|
+
|
|
290
|
+
_defineProperty(this, "head", this.create('HEAD'));
|
|
291
|
+
|
|
292
|
+
_defineProperty(this, "options", this.create('OPTIONS'));
|
|
293
|
+
|
|
294
|
+
_defineProperty(this, "connect", this.create('CONNECT'));
|
|
295
|
+
|
|
296
|
+
_defineProperty(this, "trace", this.create('TRACE'));
|
|
297
|
+
|
|
298
|
+
_defineProperty(this, "acl", this.create('ACL'));
|
|
299
|
+
|
|
300
|
+
_defineProperty(this, "bind", this.create('BIND'));
|
|
145
301
|
|
|
146
302
|
this.procs = procs;
|
|
303
|
+
this._procsArray = Object.values(procs);
|
|
147
304
|
}
|
|
148
305
|
|
|
149
306
|
getProcs() {
|
|
150
307
|
return this.procs;
|
|
151
308
|
}
|
|
152
309
|
|
|
310
|
+
find(method, url) {
|
|
311
|
+
for (var proc of this._procsArray) {
|
|
312
|
+
if (proc.method !== method) {
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (proc.pattern.test(url)) {
|
|
317
|
+
return proc;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
|
|
153
324
|
}
|
|
154
325
|
class KaitoError extends Error {
|
|
155
|
-
constructor(
|
|
326
|
+
constructor(status, message, cause) {
|
|
156
327
|
super(message);
|
|
157
|
-
this.
|
|
328
|
+
this.status = status;
|
|
158
329
|
this.cause = cause;
|
|
159
330
|
}
|
|
160
331
|
|
|
@@ -163,79 +334,76 @@ function createRouter() {
|
|
|
163
334
|
return new Router({});
|
|
164
335
|
}
|
|
165
336
|
function createServer(config) {
|
|
166
|
-
var
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
data: null,
|
|
174
|
-
message: error.message
|
|
175
|
-
});
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
var {
|
|
180
|
-
code,
|
|
181
|
-
message
|
|
182
|
-
} = yield config.onError({
|
|
183
|
-
error,
|
|
184
|
-
req,
|
|
185
|
-
res
|
|
186
|
-
}).catch(() => ({
|
|
187
|
-
code: 500,
|
|
188
|
-
message: 'Something went wrong'
|
|
189
|
-
}));
|
|
190
|
-
yield res.status(code).send({
|
|
191
|
-
success: false,
|
|
192
|
-
data: null,
|
|
193
|
-
message
|
|
194
|
-
});
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
return function (_x, _x2, _x3) {
|
|
198
|
-
return _ref.apply(this, arguments);
|
|
199
|
-
};
|
|
200
|
-
}());
|
|
201
|
-
app.all('*', /*#__PURE__*/function () {
|
|
202
|
-
var _ref2 = _asyncToGenerator(function* (req, res) {
|
|
203
|
-
var _handler$input$parse, _handler$input;
|
|
204
|
-
|
|
205
|
-
var logMessage = "".concat(req.hostname, " ").concat(req.method, " ").concat(req.url);
|
|
337
|
+
var log = message => {
|
|
338
|
+
if (config.log === undefined) {
|
|
339
|
+
console.log(message);
|
|
340
|
+
} else if (config.log) {
|
|
341
|
+
config.log(message);
|
|
342
|
+
}
|
|
343
|
+
};
|
|
206
344
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
345
|
+
return http__default["default"].createServer( /*#__PURE__*/function () {
|
|
346
|
+
var _ref = _asyncToGenerator(function* (incomingMessage, serverResponse) {
|
|
347
|
+
var start = Date.now();
|
|
348
|
+
var req = new KaitoRequest(incomingMessage);
|
|
349
|
+
var res = new KaitoResponse(serverResponse);
|
|
212
350
|
|
|
213
|
-
|
|
214
|
-
|
|
351
|
+
try {
|
|
352
|
+
var _handler$input, _yield$getInput;
|
|
215
353
|
|
|
216
|
-
|
|
217
|
-
throw new KaitoError(404, "Cannot ".concat(req.method, " this route."));
|
|
218
|
-
}
|
|
354
|
+
var handler = config.router.find(req.method, req.url.pathname);
|
|
219
355
|
|
|
220
|
-
|
|
356
|
+
if (!handler) {
|
|
357
|
+
throw new KaitoError(404, "Cannot ".concat(req.method, " this route."));
|
|
358
|
+
}
|
|
221
359
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
226
|
-
data: yield handler.run({
|
|
360
|
+
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);
|
|
361
|
+
var context = yield config.getContext(req, res);
|
|
362
|
+
var data = yield handler.run({
|
|
227
363
|
ctx: context,
|
|
228
364
|
input
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
|
|
365
|
+
});
|
|
366
|
+
res.json({
|
|
367
|
+
success: true,
|
|
368
|
+
data,
|
|
369
|
+
message: 'OK'
|
|
370
|
+
});
|
|
371
|
+
} catch (error) {
|
|
372
|
+
if (error instanceof KaitoError) {
|
|
373
|
+
res.status(error.status).json({
|
|
374
|
+
success: false,
|
|
375
|
+
data: null,
|
|
376
|
+
message: error.message
|
|
377
|
+
});
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
var {
|
|
382
|
+
status: _status,
|
|
383
|
+
message: _message
|
|
384
|
+
} = yield config.onError({
|
|
385
|
+
error: WrappedError.maybe(error),
|
|
386
|
+
req,
|
|
387
|
+
res
|
|
388
|
+
}).catch(() => ({
|
|
389
|
+
status: 500,
|
|
390
|
+
message: 'Something went wrong'
|
|
391
|
+
}));
|
|
392
|
+
res.status(_status).json({
|
|
393
|
+
success: false,
|
|
394
|
+
data: null,
|
|
395
|
+
message: _message
|
|
396
|
+
});
|
|
397
|
+
} finally {
|
|
398
|
+
var finish = Date.now();
|
|
399
|
+
log("".concat(req.method, " ").concat(req.fullURL, " ").concat(res.raw.statusCode, " ").concat(finish - start, "ms"));
|
|
400
|
+
}
|
|
232
401
|
});
|
|
233
402
|
|
|
234
|
-
return function (
|
|
235
|
-
return
|
|
403
|
+
return function (_x, _x2) {
|
|
404
|
+
return _ref.apply(this, arguments);
|
|
236
405
|
};
|
|
237
406
|
}());
|
|
238
|
-
return app;
|
|
239
407
|
}
|
|
240
408
|
|
|
241
409
|
exports.KaitoError = KaitoError;
|