@opra/core 1.0.0-beta.2 → 1.0.0-beta.4
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/cjs/{http/impl/asset-cache.js → asset-cache.js} +6 -0
- package/cjs/execution-context.js +3 -14
- package/cjs/index.js +3 -25
- package/cjs/platform-adapter.js +3 -3
- package/cjs/{helpers/service-base.js → service-base.js} +9 -4
- package/esm/{http/impl/asset-cache.js → asset-cache.js} +6 -0
- package/esm/execution-context.js +2 -13
- package/esm/index.js +3 -24
- package/esm/platform-adapter.js +2 -2
- package/esm/{helpers/service-base.js → service-base.js} +9 -4
- package/package.json +5 -39
- package/types/{http/impl/asset-cache.d.ts → asset-cache.d.ts} +1 -0
- package/types/execution-context.d.ts +4 -8
- package/types/index.d.cts +3 -23
- package/types/index.d.ts +3 -23
- package/types/interfaces/logger.interface.d.ts +1 -1
- package/types/platform-adapter.d.ts +3 -3
- package/types/service-base.d.ts +11 -0
- package/cjs/augmentation/http-controller.augmentation.js +0 -25
- package/cjs/http/express-adapter.js +0 -155
- package/cjs/http/http-adapter.js +0 -24
- package/cjs/http/http-context.js +0 -104
- package/cjs/http/http-handler.js +0 -609
- package/cjs/http/impl/http-incoming.host.js +0 -112
- package/cjs/http/impl/http-outgoing.host.js +0 -207
- package/cjs/http/impl/multipart-reader.js +0 -196
- package/cjs/http/impl/node-incoming-message.host.js +0 -109
- package/cjs/http/impl/node-outgoing-message.host.js +0 -195
- package/cjs/http/interfaces/http-incoming.interface.js +0 -25
- package/cjs/http/interfaces/http-outgoing.interface.js +0 -22
- package/cjs/http/interfaces/node-incoming-message.interface.js +0 -64
- package/cjs/http/interfaces/node-outgoing-message.interface.js +0 -15
- package/cjs/http/utils/body-reader.js +0 -216
- package/cjs/http/utils/common.js +0 -67
- package/cjs/http/utils/concat-readable.js +0 -19
- package/cjs/http/utils/convert-to-headers.js +0 -64
- package/cjs/http/utils/convert-to-raw-headers.js +0 -23
- package/cjs/http/utils/match-known-fields.js +0 -49
- package/cjs/http/utils/wrap-exception.js +0 -33
- package/cjs/type-guards.js +0 -22
- package/esm/augmentation/http-controller.augmentation.js +0 -23
- package/esm/http/express-adapter.js +0 -150
- package/esm/http/http-adapter.js +0 -20
- package/esm/http/http-context.js +0 -99
- package/esm/http/http-handler.js +0 -604
- package/esm/http/impl/http-incoming.host.js +0 -107
- package/esm/http/impl/http-outgoing.host.js +0 -202
- package/esm/http/impl/multipart-reader.js +0 -191
- package/esm/http/impl/node-incoming-message.host.js +0 -105
- package/esm/http/impl/node-outgoing-message.host.js +0 -191
- package/esm/http/interfaces/http-incoming.interface.js +0 -22
- package/esm/http/interfaces/http-outgoing.interface.js +0 -19
- package/esm/http/interfaces/node-incoming-message.interface.js +0 -61
- package/esm/http/interfaces/node-outgoing-message.interface.js +0 -12
- package/esm/http/utils/body-reader.js +0 -211
- package/esm/http/utils/common.js +0 -61
- package/esm/http/utils/concat-readable.js +0 -16
- package/esm/http/utils/convert-to-headers.js +0 -60
- package/esm/http/utils/convert-to-raw-headers.js +0 -20
- package/esm/http/utils/match-known-fields.js +0 -45
- package/esm/http/utils/wrap-exception.js +0 -30
- package/esm/type-guards.js +0 -16
- package/types/augmentation/http-controller.augmentation.d.ts +0 -20
- package/types/helpers/service-base.d.ts +0 -10
- package/types/http/express-adapter.d.ts +0 -13
- package/types/http/http-adapter.d.ts +0 -54
- package/types/http/http-context.d.ts +0 -44
- package/types/http/http-handler.d.ts +0 -75
- package/types/http/impl/http-incoming.host.d.ts +0 -22
- package/types/http/impl/http-outgoing.host.d.ts +0 -17
- package/types/http/impl/multipart-reader.d.ts +0 -46
- package/types/http/impl/node-incoming-message.host.d.ts +0 -45
- package/types/http/impl/node-outgoing-message.host.d.ts +0 -49
- package/types/http/interfaces/http-incoming.interface.d.ts +0 -192
- package/types/http/interfaces/http-outgoing.interface.d.ts +0 -144
- package/types/http/interfaces/node-incoming-message.interface.d.ts +0 -36
- package/types/http/interfaces/node-outgoing-message.interface.d.ts +0 -27
- package/types/http/utils/body-reader.d.ts +0 -38
- package/types/http/utils/common.d.ts +0 -17
- package/types/http/utils/concat-readable.d.ts +0 -2
- package/types/http/utils/convert-to-headers.d.ts +0 -2
- package/types/http/utils/convert-to-raw-headers.d.ts +0 -2
- package/types/http/utils/match-known-fields.d.ts +0 -6
- package/types/http/utils/wrap-exception.d.ts +0 -2
- package/types/type-guards.d.ts +0 -8
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToHeaders = convertToHeaders;
|
|
4
|
-
exports.convertToHeadersDistinct = convertToHeadersDistinct;
|
|
5
|
-
const match_known_fields_js_1 = require("./match-known-fields.js");
|
|
6
|
-
function convertToHeaders(src, dst, joinDuplicateHeaders) {
|
|
7
|
-
for (let n = 0; n < src.length; n += 2) {
|
|
8
|
-
addHeaderLine(src[n], src[n + 1], dst, joinDuplicateHeaders);
|
|
9
|
-
}
|
|
10
|
-
return dst;
|
|
11
|
-
}
|
|
12
|
-
function convertToHeadersDistinct(src, dst) {
|
|
13
|
-
const count = src.length % 2;
|
|
14
|
-
for (let n = 0; n < count; n += 2) {
|
|
15
|
-
addHeaderLineDistinct(src[n], src[n + 1], dst);
|
|
16
|
-
}
|
|
17
|
-
return dst;
|
|
18
|
-
}
|
|
19
|
-
function addHeaderLine(field, value, dest, joinDuplicateHeaders) {
|
|
20
|
-
if (value == null)
|
|
21
|
-
return;
|
|
22
|
-
field = field.toLowerCase();
|
|
23
|
-
const [, flag] = (0, match_known_fields_js_1.matchKnownFields)(field);
|
|
24
|
-
// comma(0) or semicolon(2) delimited field
|
|
25
|
-
if (flag === match_known_fields_js_1.COMMA_DELIMITED_FIELD || flag === match_known_fields_js_1.SEMICOLON_DELIMITED_FIELD) {
|
|
26
|
-
// Make a delimited list
|
|
27
|
-
if (typeof dest[field] === 'string') {
|
|
28
|
-
dest[field] += (flag === match_known_fields_js_1.COMMA_DELIMITED_FIELD ? ', ' : '; ') + value;
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
dest[field] = value;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
else if (flag === match_known_fields_js_1.ARRAY_FIELD) {
|
|
35
|
-
// Array header -- only Set-Cookie at the moment
|
|
36
|
-
if (dest['set-cookie'] !== undefined) {
|
|
37
|
-
dest['set-cookie'].push(value);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
dest['set-cookie'] = [value];
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
else if (joinDuplicateHeaders) {
|
|
44
|
-
if (dest[field] === undefined) {
|
|
45
|
-
dest[field] = value;
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
dest[field] += ', ' + value;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else if (dest[field] === undefined) {
|
|
52
|
-
// Drop duplicates
|
|
53
|
-
dest[field] = value;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
function addHeaderLineDistinct(field, value, dest) {
|
|
57
|
-
field = field.toLowerCase();
|
|
58
|
-
if (!dest[field]) {
|
|
59
|
-
dest[field] = [value];
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
dest[field].push(value);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertToRawHeaders = convertToRawHeaders;
|
|
4
|
-
const match_known_fields_js_1 = require("./match-known-fields.js");
|
|
5
|
-
function convertToRawHeaders(src) {
|
|
6
|
-
return Object.entries(src).reduce((a, [field, v]) => {
|
|
7
|
-
const [name, flag] = (0, match_known_fields_js_1.matchKnownFields)(field);
|
|
8
|
-
if (flag === match_known_fields_js_1.ARRAY_FIELD) {
|
|
9
|
-
if (Array.isArray(v))
|
|
10
|
-
v.forEach(x => a.push(name, String(x)));
|
|
11
|
-
else
|
|
12
|
-
a.push(name, String(v));
|
|
13
|
-
return a;
|
|
14
|
-
}
|
|
15
|
-
if (flag === match_known_fields_js_1.COMMA_DELIMITED_FIELD || flag === match_known_fields_js_1.SEMICOLON_DELIMITED_FIELD) {
|
|
16
|
-
v = Array.isArray(v) ? v.join(flag === match_known_fields_js_1.COMMA_DELIMITED_FIELD ? ', ' : '; ') : String(v);
|
|
17
|
-
}
|
|
18
|
-
else
|
|
19
|
-
v = Array.isArray(v) ? String(v[0]) : String(v);
|
|
20
|
-
a.push(name, v);
|
|
21
|
-
return a;
|
|
22
|
-
}, []);
|
|
23
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ARRAY_FIELD = exports.SEMICOLON_DELIMITED_FIELD = exports.COMMA_DELIMITED_FIELD = exports.NO_DUPLICATES_FIELD = void 0;
|
|
4
|
-
exports.matchKnownFields = matchKnownFields;
|
|
5
|
-
const common_1 = require("@opra/common");
|
|
6
|
-
exports.NO_DUPLICATES_FIELD = 0;
|
|
7
|
-
exports.COMMA_DELIMITED_FIELD = 1;
|
|
8
|
-
exports.SEMICOLON_DELIMITED_FIELD = 2;
|
|
9
|
-
exports.ARRAY_FIELD = 3;
|
|
10
|
-
const ARRAY_HEADERS = ['set-cookie'];
|
|
11
|
-
const NO_DUPLICATES_HEADERS = [
|
|
12
|
-
'age',
|
|
13
|
-
'from',
|
|
14
|
-
'etag',
|
|
15
|
-
'server',
|
|
16
|
-
'referer',
|
|
17
|
-
'referrer',
|
|
18
|
-
'expires',
|
|
19
|
-
'location',
|
|
20
|
-
'user-agent',
|
|
21
|
-
'retry-after',
|
|
22
|
-
'content-type',
|
|
23
|
-
'content-length',
|
|
24
|
-
'max-forwards',
|
|
25
|
-
'last-modified',
|
|
26
|
-
'authorization',
|
|
27
|
-
'proxy-authorization',
|
|
28
|
-
'if-modified-since',
|
|
29
|
-
'if-unmodified-since',
|
|
30
|
-
];
|
|
31
|
-
const SEMICOLON_DELIMITED_HEADERS = ['cookie'];
|
|
32
|
-
const KNOWN_FIELDS = Object.values(common_1.HttpHeaderCodes).reduce((o, k) => {
|
|
33
|
-
const n = k.toLowerCase();
|
|
34
|
-
o[n] = [
|
|
35
|
-
k,
|
|
36
|
-
NO_DUPLICATES_HEADERS.includes(n)
|
|
37
|
-
? exports.NO_DUPLICATES_FIELD
|
|
38
|
-
: ARRAY_HEADERS.includes(n)
|
|
39
|
-
? exports.ARRAY_FIELD
|
|
40
|
-
: SEMICOLON_DELIMITED_HEADERS.includes(n)
|
|
41
|
-
? exports.SEMICOLON_DELIMITED_FIELD
|
|
42
|
-
: exports.COMMA_DELIMITED_FIELD,
|
|
43
|
-
];
|
|
44
|
-
return o;
|
|
45
|
-
}, {});
|
|
46
|
-
function matchKnownFields(field) {
|
|
47
|
-
const x = KNOWN_FIELDS[field.toLowerCase()];
|
|
48
|
-
return x ? x : [field, exports.COMMA_DELIMITED_FIELD];
|
|
49
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wrapException = wrapException;
|
|
4
|
-
const common_1 = require("@opra/common");
|
|
5
|
-
function wrapException(error) {
|
|
6
|
-
if (error instanceof common_1.OpraHttpError)
|
|
7
|
-
return error;
|
|
8
|
-
let status = 500;
|
|
9
|
-
if (typeof error.status === 'number')
|
|
10
|
-
status = error.status;
|
|
11
|
-
else if (typeof error.getStatus === 'function')
|
|
12
|
-
status = error.getStatus();
|
|
13
|
-
switch (status) {
|
|
14
|
-
case 400:
|
|
15
|
-
return new common_1.BadRequestError(error);
|
|
16
|
-
case 401:
|
|
17
|
-
return new common_1.UnauthorizedError(error);
|
|
18
|
-
case 403:
|
|
19
|
-
return new common_1.ForbiddenError(error);
|
|
20
|
-
case 404:
|
|
21
|
-
return new common_1.NotFoundError(error);
|
|
22
|
-
case 405:
|
|
23
|
-
return new common_1.MethodNotAllowedError(error);
|
|
24
|
-
case 406:
|
|
25
|
-
return new common_1.NotAcceptableError(error);
|
|
26
|
-
case 422:
|
|
27
|
-
return new common_1.UnprocessableEntityError(error);
|
|
28
|
-
case 424:
|
|
29
|
-
return new common_1.FailedDependencyError(error);
|
|
30
|
-
default:
|
|
31
|
-
return new common_1.InternalServerError(error);
|
|
32
|
-
}
|
|
33
|
-
}
|
package/cjs/type-guards.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isNodeIncomingMessage = isNodeIncomingMessage;
|
|
4
|
-
exports.isHttpIncoming = isHttpIncoming;
|
|
5
|
-
exports.isNodeOutgoingMessage = isNodeOutgoingMessage;
|
|
6
|
-
exports.isHttpOutgoing = isHttpOutgoing;
|
|
7
|
-
const common_1 = require("@opra/common");
|
|
8
|
-
function isNodeIncomingMessage(v) {
|
|
9
|
-
return v && typeof v.method === 'string' && Array.isArray(v.rawHeaders) && (0, common_1.isReadable)(v);
|
|
10
|
-
}
|
|
11
|
-
function isHttpIncoming(v) {
|
|
12
|
-
return (isNodeIncomingMessage(v) &&
|
|
13
|
-
typeof v.header === 'function' &&
|
|
14
|
-
typeof v.acceptsLanguages === 'function' &&
|
|
15
|
-
typeof v.readBody === 'function');
|
|
16
|
-
}
|
|
17
|
-
function isNodeOutgoingMessage(v) {
|
|
18
|
-
return v && typeof v.getHeaders === 'function' && (0, common_1.isStream)(v);
|
|
19
|
-
}
|
|
20
|
-
function isHttpOutgoing(v) {
|
|
21
|
-
return isNodeOutgoingMessage(v) && typeof v.clearCookie === 'function' && typeof v.cookie === 'function';
|
|
22
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { HTTP_CONTROLLER_METADATA, HttpController } from '@opra/common';
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
const oldInitialize = HttpController.prototype._initialize;
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
HttpController.prototype._initialize = function (initArgs) {
|
|
6
|
-
oldInitialize?.call(this, initArgs);
|
|
7
|
-
this.onInit = initArgs.onInit;
|
|
8
|
-
this.onShutdown = initArgs.onShutdown;
|
|
9
|
-
};
|
|
10
|
-
HttpController.OnInit = function () {
|
|
11
|
-
return (target, propertyKey) => {
|
|
12
|
-
const sourceMetadata = (Reflect.getOwnMetadata(HTTP_CONTROLLER_METADATA, target.constructor) || {});
|
|
13
|
-
sourceMetadata.onInit = target[propertyKey];
|
|
14
|
-
Reflect.defineMetadata(HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
HttpController.OnShutdown = function () {
|
|
18
|
-
return (target, propertyKey) => {
|
|
19
|
-
const sourceMetadata = (Reflect.getOwnMetadata(HTTP_CONTROLLER_METADATA, target.constructor) || {});
|
|
20
|
-
sourceMetadata.onShutdown = target[propertyKey];
|
|
21
|
-
Reflect.defineMetadata(HTTP_CONTROLLER_METADATA, target.constructor, sourceMetadata);
|
|
22
|
-
};
|
|
23
|
-
};
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import * as nodePath from 'node:path';
|
|
2
|
-
import { HttpApi, NotFoundError } from '@opra/common';
|
|
3
|
-
import { Router } from 'express';
|
|
4
|
-
import { HttpAdapter } from './http-adapter.js';
|
|
5
|
-
import { HttpContext } from './http-context.js';
|
|
6
|
-
import { HttpIncoming } from './interfaces/http-incoming.interface.js';
|
|
7
|
-
import { HttpOutgoing } from './interfaces/http-outgoing.interface.js';
|
|
8
|
-
import { wrapException } from './utils/wrap-exception.js';
|
|
9
|
-
export class ExpressAdapter extends HttpAdapter {
|
|
10
|
-
constructor(app, document, options) {
|
|
11
|
-
super(document, options);
|
|
12
|
-
this._controllerInstances = new Map();
|
|
13
|
-
this.app = app;
|
|
14
|
-
if (!(this.document.api instanceof HttpApi))
|
|
15
|
-
throw new TypeError('document.api must be instance of HttpApi');
|
|
16
|
-
for (const c of this.api.controllers.values())
|
|
17
|
-
this._createControllers(c);
|
|
18
|
-
this._initRouter(options?.basePath);
|
|
19
|
-
}
|
|
20
|
-
get platform() {
|
|
21
|
-
return 'express';
|
|
22
|
-
}
|
|
23
|
-
async close() {
|
|
24
|
-
const processResource = async (resource) => {
|
|
25
|
-
if (resource.controllers.size) {
|
|
26
|
-
const subResources = Array.from(resource.controllers.values());
|
|
27
|
-
subResources.reverse();
|
|
28
|
-
for (const subResource of subResources) {
|
|
29
|
-
await processResource(subResource);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (resource.onShutdown) {
|
|
33
|
-
const instance = this._controllerInstances.get(resource) || resource.instance;
|
|
34
|
-
if (instance) {
|
|
35
|
-
try {
|
|
36
|
-
await resource.onShutdown.call(instance, resource);
|
|
37
|
-
}
|
|
38
|
-
catch (e) {
|
|
39
|
-
if (this.listenerCount('error'))
|
|
40
|
-
this.emit('error', wrapException(e));
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
for (const c of this.api.controllers.values())
|
|
46
|
-
await processResource(c);
|
|
47
|
-
this._controllerInstances.clear();
|
|
48
|
-
}
|
|
49
|
-
getControllerInstance(controllerPath) {
|
|
50
|
-
const controller = this.api.findController(controllerPath);
|
|
51
|
-
return controller && this._controllerInstances.get(controller);
|
|
52
|
-
}
|
|
53
|
-
_initRouter(basePath) {
|
|
54
|
-
const router = Router();
|
|
55
|
-
if (basePath) {
|
|
56
|
-
if (!basePath.startsWith('/'))
|
|
57
|
-
basePath = '/' + basePath;
|
|
58
|
-
if (basePath)
|
|
59
|
-
this.app.use(basePath, router);
|
|
60
|
-
}
|
|
61
|
-
else
|
|
62
|
-
this.app.use(router);
|
|
63
|
-
const createContext = async (_req, _res, args) => {
|
|
64
|
-
const request = HttpIncoming.from(_req);
|
|
65
|
-
const response = HttpOutgoing.from(_res);
|
|
66
|
-
const ctx = new HttpContext({
|
|
67
|
-
adapter: this,
|
|
68
|
-
platform: this.platform,
|
|
69
|
-
request,
|
|
70
|
-
response,
|
|
71
|
-
controller: args?.controller,
|
|
72
|
-
controllerInstance: args?.controllerInstance,
|
|
73
|
-
operation: args?.operation,
|
|
74
|
-
operationHandler: args?.operationHandler,
|
|
75
|
-
});
|
|
76
|
-
await this.emitAsync('createContext', ctx);
|
|
77
|
-
return ctx;
|
|
78
|
-
};
|
|
79
|
-
/** Add an endpoint that returns document schema */
|
|
80
|
-
router.get('/\\$schema', (_req, _res, next) => {
|
|
81
|
-
createContext(_req, _res)
|
|
82
|
-
.then(ctx => this.handler.sendDocumentSchema(ctx).catch(next))
|
|
83
|
-
.catch(next);
|
|
84
|
-
});
|
|
85
|
-
/** Add operation endpoints */
|
|
86
|
-
if (this.api.controllers.size) {
|
|
87
|
-
const processResource = (controller, currentPath) => {
|
|
88
|
-
currentPath = nodePath.join(currentPath, controller.path);
|
|
89
|
-
for (const operation of controller.operations.values()) {
|
|
90
|
-
const routePath = currentPath + (operation.path || '');
|
|
91
|
-
const controllerInstance = this._controllerInstances.get(controller);
|
|
92
|
-
const operationHandler = controllerInstance[operation.name];
|
|
93
|
-
if (!operationHandler)
|
|
94
|
-
continue;
|
|
95
|
-
/** Define router callback */
|
|
96
|
-
router[operation.method.toLowerCase()](routePath, (_req, _res, _next) => {
|
|
97
|
-
createContext(_req, _res, {
|
|
98
|
-
controller,
|
|
99
|
-
controllerInstance,
|
|
100
|
-
operation,
|
|
101
|
-
operationHandler,
|
|
102
|
-
})
|
|
103
|
-
.then(ctx => this.handler.handleRequest(ctx))
|
|
104
|
-
.then(() => {
|
|
105
|
-
if (!_res.headersSent)
|
|
106
|
-
_next();
|
|
107
|
-
})
|
|
108
|
-
.catch((e) => this.emit('error', e));
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
if (controller.controllers.size) {
|
|
112
|
-
for (const child of controller.controllers.values())
|
|
113
|
-
processResource(child, currentPath);
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
for (const c of this.api.controllers.values())
|
|
117
|
-
processResource(c, '/');
|
|
118
|
-
}
|
|
119
|
-
/** Add an endpoint that returns 404 error at last */
|
|
120
|
-
router.use('*', (_req, _res, next) => {
|
|
121
|
-
createContext(_req, _res)
|
|
122
|
-
.then(ctx => {
|
|
123
|
-
ctx.errors.push(new NotFoundError({
|
|
124
|
-
message: `No endpoint found at [${_req.method}]${_req.baseUrl}`,
|
|
125
|
-
details: {
|
|
126
|
-
path: _req.baseUrl,
|
|
127
|
-
method: _req.method,
|
|
128
|
-
},
|
|
129
|
-
}));
|
|
130
|
-
this.handler.sendResponse(ctx).catch(next);
|
|
131
|
-
})
|
|
132
|
-
.catch(next);
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
_createControllers(controller) {
|
|
136
|
-
let instance = controller.instance;
|
|
137
|
-
if (!instance && controller.ctor)
|
|
138
|
-
instance = new controller.ctor();
|
|
139
|
-
if (instance) {
|
|
140
|
-
if (typeof instance.onInit === 'function')
|
|
141
|
-
instance.onInit.call(instance, this);
|
|
142
|
-
this._controllerInstances.set(controller, instance);
|
|
143
|
-
// Initialize sub resources
|
|
144
|
-
for (const r of controller.controllers.values()) {
|
|
145
|
-
this._createControllers(r);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return instance;
|
|
149
|
-
}
|
|
150
|
-
}
|
package/esm/http/http-adapter.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { HttpApi } from '@opra/common';
|
|
2
|
-
import { PlatformAdapter } from '../platform-adapter.js';
|
|
3
|
-
import { HttpHandler } from './http-handler.js';
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
* @class HttpAdapter
|
|
7
|
-
*/
|
|
8
|
-
export class HttpAdapter extends PlatformAdapter {
|
|
9
|
-
constructor(document, options) {
|
|
10
|
-
super(document, options);
|
|
11
|
-
this.protocol = 'http';
|
|
12
|
-
if (!(document.api instanceof HttpApi))
|
|
13
|
-
throw new TypeError(`The document does not expose an HTTP Api`);
|
|
14
|
-
this.handler = new HttpHandler(this);
|
|
15
|
-
this.interceptors = [...(options?.interceptors || [])];
|
|
16
|
-
}
|
|
17
|
-
get api() {
|
|
18
|
-
return this.document.api;
|
|
19
|
-
}
|
|
20
|
-
}
|
package/esm/http/http-context.js
DELETED
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import typeIs from '@browsery/type-is';
|
|
2
|
-
import { InternalServerError, NotAcceptableError, } from '@opra/common';
|
|
3
|
-
import { vg } from 'valgen';
|
|
4
|
-
import { kAssetCache } from '../constants.js';
|
|
5
|
-
import { ExecutionContext } from '../execution-context.js';
|
|
6
|
-
import { MultipartReader } from './impl/multipart-reader.js';
|
|
7
|
-
export class HttpContext extends ExecutionContext {
|
|
8
|
-
constructor(init) {
|
|
9
|
-
super({ ...init, document: init.adapter.document, protocol: 'http' });
|
|
10
|
-
this.adapter = init.adapter;
|
|
11
|
-
this.protocol = 'http';
|
|
12
|
-
if (init.controller)
|
|
13
|
-
this.controller = init.controller;
|
|
14
|
-
if (init.controllerInstance)
|
|
15
|
-
this.controllerInstance = init.controllerInstance;
|
|
16
|
-
if (init.operation)
|
|
17
|
-
this.operation = init.operation;
|
|
18
|
-
if (init.operationHandler)
|
|
19
|
-
this.operationHandler = init.operationHandler;
|
|
20
|
-
this.request = init.request;
|
|
21
|
-
this.response = init.response;
|
|
22
|
-
this.mediaType = init.mediaType;
|
|
23
|
-
this.cookies = init.cookies || {};
|
|
24
|
-
this.headers = init.headers || {};
|
|
25
|
-
this.pathParams = init.pathParams || {};
|
|
26
|
-
this.queryParams = init.queryParams || {};
|
|
27
|
-
this._body = init.body;
|
|
28
|
-
this.on('finish', () => {
|
|
29
|
-
if (this._multipartReader)
|
|
30
|
-
this._multipartReader.purge().catch(() => undefined);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
get isMultipart() {
|
|
34
|
-
return !!this.request.is('multipart');
|
|
35
|
-
}
|
|
36
|
-
async getMultipartReader() {
|
|
37
|
-
if (!this.isMultipart)
|
|
38
|
-
throw new InternalServerError('Request content is not a multipart content');
|
|
39
|
-
if (this._multipartReader)
|
|
40
|
-
return this._multipartReader;
|
|
41
|
-
const { mediaType } = this;
|
|
42
|
-
if (mediaType?.contentType) {
|
|
43
|
-
const arr = Array.isArray(mediaType.contentType) ? mediaType.contentType : [mediaType.contentType];
|
|
44
|
-
const contentType = arr.find(ct => typeIs.is(ct, ['multipart']));
|
|
45
|
-
if (!contentType)
|
|
46
|
-
throw new NotAcceptableError('This endpoint does not accept multipart requests');
|
|
47
|
-
}
|
|
48
|
-
const reader = new MultipartReader(this, {
|
|
49
|
-
limits: {
|
|
50
|
-
fields: mediaType?.maxFields,
|
|
51
|
-
fieldSize: mediaType?.maxFieldsSize,
|
|
52
|
-
files: mediaType?.maxFiles,
|
|
53
|
-
fileSize: mediaType?.maxFileSize,
|
|
54
|
-
},
|
|
55
|
-
}, mediaType);
|
|
56
|
-
this._multipartReader = reader;
|
|
57
|
-
return reader;
|
|
58
|
-
}
|
|
59
|
-
async getBody() {
|
|
60
|
-
if (this._body !== undefined)
|
|
61
|
-
return this._body;
|
|
62
|
-
const { request, operation, mediaType } = this;
|
|
63
|
-
if (this.isMultipart) {
|
|
64
|
-
const reader = await this.getMultipartReader();
|
|
65
|
-
/** Retrieve all fields */
|
|
66
|
-
const parts = await reader.getAll();
|
|
67
|
-
/** Filter fields according to configuration */
|
|
68
|
-
this._body = [...parts];
|
|
69
|
-
return this._body;
|
|
70
|
-
}
|
|
71
|
-
this._body = await this.request.readBody({ limit: operation?.requestBody?.maxContentSize });
|
|
72
|
-
if (this._body != null) {
|
|
73
|
-
// Convert Buffer to string if media is text
|
|
74
|
-
if (Buffer.isBuffer(this._body) && request.is(['json', 'xml', 'txt', 'text'])) {
|
|
75
|
-
this._body = this._body.toString('utf-8');
|
|
76
|
-
}
|
|
77
|
-
// Transform text to Object if media is JSON
|
|
78
|
-
if (typeof this._body === 'string' && request.is(['json']))
|
|
79
|
-
this._body = JSON.parse(this._body);
|
|
80
|
-
}
|
|
81
|
-
if (mediaType) {
|
|
82
|
-
// Decode/Validate the data object according to data model
|
|
83
|
-
if (this._body && mediaType.type) {
|
|
84
|
-
let decode = this.adapter[kAssetCache].get(mediaType, 'decode');
|
|
85
|
-
if (!decode) {
|
|
86
|
-
decode =
|
|
87
|
-
mediaType.type?.generateCodec('decode', {
|
|
88
|
-
partial: operation?.requestBody?.partial,
|
|
89
|
-
projection: '*',
|
|
90
|
-
ignoreReadonlyFields: true,
|
|
91
|
-
}) || vg.isAny();
|
|
92
|
-
this.adapter[kAssetCache].set(mediaType, 'decode', decode);
|
|
93
|
-
}
|
|
94
|
-
this._body = decode(this._body);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return this._body;
|
|
98
|
-
}
|
|
99
|
-
}
|