@opra/http 1.0.0-beta.3
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/LICENSE +21 -0
- package/README.md +3 -0
- package/cjs/express-adapter.js +155 -0
- package/cjs/http-adapter.js +24 -0
- package/cjs/http-context.js +103 -0
- package/cjs/http-handler.js +609 -0
- package/cjs/impl/http-incoming.host.js +112 -0
- package/cjs/impl/http-outgoing.host.js +207 -0
- package/cjs/impl/multipart-reader.js +196 -0
- package/cjs/impl/node-incoming-message.host.js +109 -0
- package/cjs/impl/node-outgoing-message.host.js +195 -0
- package/cjs/index.js +27 -0
- package/cjs/interfaces/http-incoming.interface.js +25 -0
- package/cjs/interfaces/http-outgoing.interface.js +22 -0
- package/cjs/interfaces/node-incoming-message.interface.js +64 -0
- package/cjs/interfaces/node-outgoing-message.interface.js +15 -0
- package/cjs/package.json +3 -0
- package/cjs/type-guards.js +22 -0
- package/cjs/utils/body-reader.js +216 -0
- package/cjs/utils/common.js +67 -0
- package/cjs/utils/concat-readable.js +19 -0
- package/cjs/utils/convert-to-headers.js +64 -0
- package/cjs/utils/convert-to-raw-headers.js +23 -0
- package/cjs/utils/match-known-fields.js +49 -0
- package/cjs/utils/wrap-exception.js +33 -0
- package/esm/express-adapter.js +150 -0
- package/esm/http-adapter.js +20 -0
- package/esm/http-context.js +98 -0
- package/esm/http-handler.js +604 -0
- package/esm/impl/http-incoming.host.js +107 -0
- package/esm/impl/http-outgoing.host.js +202 -0
- package/esm/impl/multipart-reader.js +191 -0
- package/esm/impl/node-incoming-message.host.js +105 -0
- package/esm/impl/node-outgoing-message.host.js +191 -0
- package/esm/index.js +23 -0
- package/esm/interfaces/http-incoming.interface.js +22 -0
- package/esm/interfaces/http-outgoing.interface.js +19 -0
- package/esm/interfaces/node-incoming-message.interface.js +61 -0
- package/esm/interfaces/node-outgoing-message.interface.js +12 -0
- package/esm/package.json +3 -0
- package/esm/type-guards.js +16 -0
- package/esm/utils/body-reader.js +211 -0
- package/esm/utils/common.js +61 -0
- package/esm/utils/concat-readable.js +16 -0
- package/esm/utils/convert-to-headers.js +60 -0
- package/esm/utils/convert-to-raw-headers.js +20 -0
- package/esm/utils/match-known-fields.js +45 -0
- package/esm/utils/wrap-exception.js +30 -0
- package/i18n/en/error.json +21 -0
- package/package.json +89 -0
- package/types/express-adapter.d.ts +13 -0
- package/types/http-adapter.d.ts +32 -0
- package/types/http-context.d.ts +44 -0
- package/types/http-handler.d.ts +74 -0
- package/types/impl/http-incoming.host.d.ts +22 -0
- package/types/impl/http-outgoing.host.d.ts +17 -0
- package/types/impl/multipart-reader.d.ts +46 -0
- package/types/impl/node-incoming-message.host.d.ts +45 -0
- package/types/impl/node-outgoing-message.host.d.ts +49 -0
- package/types/index.d.cts +22 -0
- package/types/index.d.ts +22 -0
- package/types/interfaces/http-incoming.interface.d.ts +192 -0
- package/types/interfaces/http-outgoing.interface.d.ts +144 -0
- package/types/interfaces/node-incoming-message.interface.d.ts +36 -0
- package/types/interfaces/node-outgoing-message.interface.d.ts +27 -0
- package/types/type-guards.d.ts +8 -0
- package/types/utils/body-reader.d.ts +38 -0
- package/types/utils/common.d.ts +17 -0
- package/types/utils/concat-readable.d.ts +2 -0
- package/types/utils/convert-to-headers.d.ts +2 -0
- package/types/utils/convert-to-raw-headers.d.ts +2 -0
- package/types/utils/match-known-fields.d.ts +6 -0
- package/types/utils/wrap-exception.d.ts +2 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Panates
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExpressAdapter = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const nodePath = tslib_1.__importStar(require("node:path"));
|
|
6
|
+
const common_1 = require("@opra/common");
|
|
7
|
+
const express_1 = require("express");
|
|
8
|
+
const http_adapter_js_1 = require("./http-adapter.js");
|
|
9
|
+
const http_context_js_1 = require("./http-context.js");
|
|
10
|
+
const http_incoming_interface_js_1 = require("./interfaces/http-incoming.interface.js");
|
|
11
|
+
const http_outgoing_interface_js_1 = require("./interfaces/http-outgoing.interface.js");
|
|
12
|
+
const wrap_exception_js_1 = require("./utils/wrap-exception.js");
|
|
13
|
+
class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
|
|
14
|
+
constructor(app, document, options) {
|
|
15
|
+
super(document, options);
|
|
16
|
+
this._controllerInstances = new Map();
|
|
17
|
+
this.app = app;
|
|
18
|
+
if (!(this.document.api instanceof common_1.HttpApi))
|
|
19
|
+
throw new TypeError('document.api must be instance of HttpApi');
|
|
20
|
+
for (const c of this.api.controllers.values())
|
|
21
|
+
this._createControllers(c);
|
|
22
|
+
this._initRouter(options?.basePath);
|
|
23
|
+
}
|
|
24
|
+
get platform() {
|
|
25
|
+
return 'express';
|
|
26
|
+
}
|
|
27
|
+
async close() {
|
|
28
|
+
const processInstance = async (controller) => {
|
|
29
|
+
if (controller.controllers.size) {
|
|
30
|
+
const subResources = Array.from(controller.controllers.values());
|
|
31
|
+
subResources.reverse();
|
|
32
|
+
for (const subResource of subResources) {
|
|
33
|
+
await processInstance(subResource);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (controller.onShutdown) {
|
|
37
|
+
const instance = this._controllerInstances.get(controller) || controller.instance;
|
|
38
|
+
if (instance) {
|
|
39
|
+
try {
|
|
40
|
+
await controller.onShutdown.call(instance, controller);
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
if (this.listenerCount('error'))
|
|
44
|
+
this.emit('error', (0, wrap_exception_js_1.wrapException)(e));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
for (const c of this.api.controllers.values())
|
|
50
|
+
await processInstance(c);
|
|
51
|
+
this._controllerInstances.clear();
|
|
52
|
+
}
|
|
53
|
+
getControllerInstance(controllerPath) {
|
|
54
|
+
const controller = this.api.findController(controllerPath);
|
|
55
|
+
return controller && this._controllerInstances.get(controller);
|
|
56
|
+
}
|
|
57
|
+
_initRouter(basePath) {
|
|
58
|
+
const router = (0, express_1.Router)();
|
|
59
|
+
if (basePath) {
|
|
60
|
+
if (!basePath.startsWith('/'))
|
|
61
|
+
basePath = '/' + basePath;
|
|
62
|
+
if (basePath)
|
|
63
|
+
this.app.use(basePath, router);
|
|
64
|
+
}
|
|
65
|
+
else
|
|
66
|
+
this.app.use(router);
|
|
67
|
+
const createContext = async (_req, _res, args) => {
|
|
68
|
+
const request = http_incoming_interface_js_1.HttpIncoming.from(_req);
|
|
69
|
+
const response = http_outgoing_interface_js_1.HttpOutgoing.from(_res);
|
|
70
|
+
const ctx = new http_context_js_1.HttpContext({
|
|
71
|
+
adapter: this,
|
|
72
|
+
platform: this.platform,
|
|
73
|
+
request,
|
|
74
|
+
response,
|
|
75
|
+
controller: args?.controller,
|
|
76
|
+
controllerInstance: args?.controllerInstance,
|
|
77
|
+
operation: args?.operation,
|
|
78
|
+
operationHandler: args?.operationHandler,
|
|
79
|
+
});
|
|
80
|
+
await this.emitAsync('createContext', ctx);
|
|
81
|
+
return ctx;
|
|
82
|
+
};
|
|
83
|
+
/** Add an endpoint that returns document schema */
|
|
84
|
+
router.get('/\\$schema', (_req, _res, next) => {
|
|
85
|
+
createContext(_req, _res)
|
|
86
|
+
.then(ctx => this.handler.sendDocumentSchema(ctx).catch(next))
|
|
87
|
+
.catch(next);
|
|
88
|
+
});
|
|
89
|
+
/** Add operation endpoints */
|
|
90
|
+
if (this.api.controllers.size) {
|
|
91
|
+
const processResource = (controller, currentPath) => {
|
|
92
|
+
currentPath = nodePath.join(currentPath, controller.path);
|
|
93
|
+
for (const operation of controller.operations.values()) {
|
|
94
|
+
const routePath = currentPath + (operation.path || '');
|
|
95
|
+
const controllerInstance = this._controllerInstances.get(controller);
|
|
96
|
+
const operationHandler = controllerInstance[operation.name];
|
|
97
|
+
if (!operationHandler)
|
|
98
|
+
continue;
|
|
99
|
+
/** Define router callback */
|
|
100
|
+
router[operation.method.toLowerCase()](routePath, (_req, _res, _next) => {
|
|
101
|
+
createContext(_req, _res, {
|
|
102
|
+
controller,
|
|
103
|
+
controllerInstance,
|
|
104
|
+
operation,
|
|
105
|
+
operationHandler,
|
|
106
|
+
})
|
|
107
|
+
.then(ctx => this.handler.handleRequest(ctx))
|
|
108
|
+
.then(() => {
|
|
109
|
+
if (!_res.headersSent)
|
|
110
|
+
_next();
|
|
111
|
+
})
|
|
112
|
+
.catch((e) => this.emit('error', e));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
if (controller.controllers.size) {
|
|
116
|
+
for (const child of controller.controllers.values())
|
|
117
|
+
processResource(child, currentPath);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
for (const c of this.api.controllers.values())
|
|
121
|
+
processResource(c, '/');
|
|
122
|
+
}
|
|
123
|
+
/** Add an endpoint that returns 404 error at last */
|
|
124
|
+
router.use('*', (_req, _res, next) => {
|
|
125
|
+
createContext(_req, _res)
|
|
126
|
+
.then(ctx => {
|
|
127
|
+
ctx.errors.push(new common_1.NotFoundError({
|
|
128
|
+
message: `No endpoint found at [${_req.method}]${_req.baseUrl}`,
|
|
129
|
+
details: {
|
|
130
|
+
path: _req.baseUrl,
|
|
131
|
+
method: _req.method,
|
|
132
|
+
},
|
|
133
|
+
}));
|
|
134
|
+
this.handler.sendResponse(ctx).catch(next);
|
|
135
|
+
})
|
|
136
|
+
.catch(next);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
_createControllers(controller) {
|
|
140
|
+
let instance = controller.instance;
|
|
141
|
+
if (!instance && controller.ctor)
|
|
142
|
+
instance = new controller.ctor();
|
|
143
|
+
if (instance) {
|
|
144
|
+
if (typeof controller.onInit === 'function')
|
|
145
|
+
controller.onInit.call(instance, controller);
|
|
146
|
+
this._controllerInstances.set(controller, instance);
|
|
147
|
+
// Initialize sub resources
|
|
148
|
+
for (const r of controller.controllers.values()) {
|
|
149
|
+
this._createControllers(r);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return instance;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.ExpressAdapter = ExpressAdapter;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpAdapter = void 0;
|
|
4
|
+
const common_1 = require("@opra/common");
|
|
5
|
+
const core_1 = require("@opra/core");
|
|
6
|
+
const http_handler_js_1 = require("./http-handler.js");
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @class HttpAdapter
|
|
10
|
+
*/
|
|
11
|
+
class HttpAdapter extends core_1.PlatformAdapter {
|
|
12
|
+
constructor(document, options) {
|
|
13
|
+
super(document, options);
|
|
14
|
+
this.protocol = 'http';
|
|
15
|
+
if (!(document.api instanceof common_1.HttpApi))
|
|
16
|
+
throw new TypeError(`The document does not expose an HTTP Api`);
|
|
17
|
+
this.handler = new http_handler_js_1.HttpHandler(this);
|
|
18
|
+
this.interceptors = [...(options?.interceptors || [])];
|
|
19
|
+
}
|
|
20
|
+
get api() {
|
|
21
|
+
return this.document.httpApi;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.HttpAdapter = HttpAdapter;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpContext = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const type_is_1 = tslib_1.__importDefault(require("@browsery/type-is"));
|
|
6
|
+
const common_1 = require("@opra/common");
|
|
7
|
+
const core_1 = require("@opra/core");
|
|
8
|
+
const valgen_1 = require("valgen");
|
|
9
|
+
const multipart_reader_js_1 = require("./impl/multipart-reader.js");
|
|
10
|
+
class HttpContext extends core_1.ExecutionContext {
|
|
11
|
+
constructor(init) {
|
|
12
|
+
super({ ...init, document: init.adapter.document, protocol: 'http' });
|
|
13
|
+
this.adapter = init.adapter;
|
|
14
|
+
this.protocol = 'http';
|
|
15
|
+
if (init.controller)
|
|
16
|
+
this.controller = init.controller;
|
|
17
|
+
if (init.controllerInstance)
|
|
18
|
+
this.controllerInstance = init.controllerInstance;
|
|
19
|
+
if (init.operation)
|
|
20
|
+
this.operation = init.operation;
|
|
21
|
+
if (init.operationHandler)
|
|
22
|
+
this.operationHandler = init.operationHandler;
|
|
23
|
+
this.request = init.request;
|
|
24
|
+
this.response = init.response;
|
|
25
|
+
this.mediaType = init.mediaType;
|
|
26
|
+
this.cookies = init.cookies || {};
|
|
27
|
+
this.headers = init.headers || {};
|
|
28
|
+
this.pathParams = init.pathParams || {};
|
|
29
|
+
this.queryParams = init.queryParams || {};
|
|
30
|
+
this._body = init.body;
|
|
31
|
+
this.on('finish', () => {
|
|
32
|
+
if (this._multipartReader)
|
|
33
|
+
this._multipartReader.purge().catch(() => undefined);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
get isMultipart() {
|
|
37
|
+
return !!this.request.is('multipart');
|
|
38
|
+
}
|
|
39
|
+
async getMultipartReader() {
|
|
40
|
+
if (!this.isMultipart)
|
|
41
|
+
throw new common_1.InternalServerError('Request content is not a multipart content');
|
|
42
|
+
if (this._multipartReader)
|
|
43
|
+
return this._multipartReader;
|
|
44
|
+
const { mediaType } = this;
|
|
45
|
+
if (mediaType?.contentType) {
|
|
46
|
+
const arr = Array.isArray(mediaType.contentType) ? mediaType.contentType : [mediaType.contentType];
|
|
47
|
+
const contentType = arr.find(ct => type_is_1.default.is(ct, ['multipart']));
|
|
48
|
+
if (!contentType)
|
|
49
|
+
throw new common_1.NotAcceptableError('This endpoint does not accept multipart requests');
|
|
50
|
+
}
|
|
51
|
+
const reader = new multipart_reader_js_1.MultipartReader(this, {
|
|
52
|
+
limits: {
|
|
53
|
+
fields: mediaType?.maxFields,
|
|
54
|
+
fieldSize: mediaType?.maxFieldsSize,
|
|
55
|
+
files: mediaType?.maxFiles,
|
|
56
|
+
fileSize: mediaType?.maxFileSize,
|
|
57
|
+
},
|
|
58
|
+
}, mediaType);
|
|
59
|
+
this._multipartReader = reader;
|
|
60
|
+
return reader;
|
|
61
|
+
}
|
|
62
|
+
async getBody() {
|
|
63
|
+
if (this._body !== undefined)
|
|
64
|
+
return this._body;
|
|
65
|
+
const { request, operation, mediaType } = this;
|
|
66
|
+
if (this.isMultipart) {
|
|
67
|
+
const reader = await this.getMultipartReader();
|
|
68
|
+
/** Retrieve all fields */
|
|
69
|
+
const parts = await reader.getAll();
|
|
70
|
+
/** Filter fields according to configuration */
|
|
71
|
+
this._body = [...parts];
|
|
72
|
+
return this._body;
|
|
73
|
+
}
|
|
74
|
+
this._body = await this.request.readBody({ limit: operation?.requestBody?.maxContentSize });
|
|
75
|
+
if (this._body != null) {
|
|
76
|
+
// Convert Buffer to string if media is text
|
|
77
|
+
if (Buffer.isBuffer(this._body) && request.is(['json', 'xml', 'txt', 'text'])) {
|
|
78
|
+
this._body = this._body.toString('utf-8');
|
|
79
|
+
}
|
|
80
|
+
// Transform text to Object if media is JSON
|
|
81
|
+
if (typeof this._body === 'string' && request.is(['json']))
|
|
82
|
+
this._body = JSON.parse(this._body);
|
|
83
|
+
}
|
|
84
|
+
if (mediaType) {
|
|
85
|
+
// Decode/Validate the data object according to data model
|
|
86
|
+
if (this._body && mediaType.type) {
|
|
87
|
+
let decode = this.adapter[core_1.kAssetCache].get(mediaType, 'decode');
|
|
88
|
+
if (!decode) {
|
|
89
|
+
decode =
|
|
90
|
+
mediaType.type?.generateCodec('decode', {
|
|
91
|
+
partial: operation?.requestBody?.partial,
|
|
92
|
+
projection: '*',
|
|
93
|
+
ignoreReadonlyFields: true,
|
|
94
|
+
}) || valgen_1.vg.isAny();
|
|
95
|
+
this.adapter[core_1.kAssetCache].set(mediaType, 'decode', decode);
|
|
96
|
+
}
|
|
97
|
+
this._body = decode(this._body);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return this._body;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
exports.HttpContext = HttpContext;
|