@opra/core 0.33.13 → 1.0.0-alpha.2

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.
Files changed (156) hide show
  1. package/cjs/augmentation/18n.augmentation.js +17 -4
  2. package/cjs/augmentation/http-controller.augmentation.js +25 -0
  3. package/cjs/constants.js +5 -0
  4. package/cjs/execution-context.js +25 -12
  5. package/cjs/{services → helpers}/logger.js +1 -2
  6. package/cjs/{services/api-service.js → helpers/service-base.js} +8 -8
  7. package/cjs/http/express-adapter.js +164 -0
  8. package/cjs/http/http-adapter.js +27 -0
  9. package/cjs/http/http-context.js +116 -0
  10. package/cjs/http/impl/asset-cache.js +21 -0
  11. package/cjs/http/impl/http-handler.js +575 -0
  12. package/cjs/http/{http-server-request.js → impl/http-incoming.host.js} +21 -46
  13. package/cjs/http/{http-server-response.js → impl/http-outgoing.host.js} +7 -26
  14. package/cjs/http/{helpers/multipart-helper.js → impl/multipart-reader.js} +24 -22
  15. package/cjs/http/impl/{http-incoming-message.host.js → node-incoming-message.host.js} +13 -54
  16. package/cjs/http/impl/{http-outgoing-message.host.js → node-outgoing-message.host.js} +11 -14
  17. package/cjs/http/interfaces/http-incoming.interface.js +25 -0
  18. package/cjs/http/interfaces/http-outgoing.interface.js +22 -0
  19. package/cjs/http/interfaces/node-incoming-message.interface.js +63 -0
  20. package/cjs/http/interfaces/node-outgoing-message.interface.js +15 -0
  21. package/cjs/http/utils/body-reader.js +215 -0
  22. package/cjs/http/{helpers → utils}/convert-to-raw-headers.js +1 -2
  23. package/cjs/http/{helpers → utils}/match-known-fields.js +11 -9
  24. package/cjs/http/utils/wrap-exception.js +34 -0
  25. package/cjs/index.js +25 -25
  26. package/cjs/platform-adapter.js +21 -0
  27. package/cjs/type-guards.js +23 -0
  28. package/esm/augmentation/18n.augmentation.js +20 -7
  29. package/esm/augmentation/http-controller.augmentation.js +23 -0
  30. package/esm/constants.js +2 -0
  31. package/esm/execution-context.js +25 -13
  32. package/esm/{services → helpers}/logger.js +1 -2
  33. package/esm/{services/api-service.js → helpers/service-base.js} +6 -6
  34. package/esm/http/express-adapter.js +159 -0
  35. package/esm/http/http-adapter.js +23 -0
  36. package/esm/http/http-context.js +111 -0
  37. package/esm/http/impl/asset-cache.js +17 -0
  38. package/esm/http/impl/http-handler.js +570 -0
  39. package/esm/http/{http-server-request.js → impl/http-incoming.host.js} +17 -43
  40. package/esm/http/{http-server-response.js → impl/http-outgoing.host.js} +6 -26
  41. package/esm/http/{helpers/multipart-helper.js → impl/multipart-reader.js} +22 -20
  42. package/esm/http/impl/{http-incoming-message.host.js → node-incoming-message.host.js} +11 -52
  43. package/esm/http/impl/{http-outgoing-message.host.js → node-outgoing-message.host.js} +9 -12
  44. package/esm/http/interfaces/http-incoming.interface.js +22 -0
  45. package/esm/http/interfaces/http-outgoing.interface.js +19 -0
  46. package/esm/http/interfaces/node-incoming-message.interface.js +60 -0
  47. package/esm/http/interfaces/node-outgoing-message.interface.js +12 -0
  48. package/esm/http/utils/body-reader.js +210 -0
  49. package/esm/http/{helpers → utils}/convert-to-headers.js +1 -1
  50. package/esm/http/{helpers → utils}/convert-to-raw-headers.js +2 -3
  51. package/esm/http/{helpers → utils}/match-known-fields.js +11 -9
  52. package/esm/http/utils/wrap-exception.js +30 -0
  53. package/esm/index.js +25 -26
  54. package/esm/platform-adapter.js +19 -1
  55. package/esm/type-guards.js +16 -0
  56. package/package.json +22 -10
  57. package/types/augmentation/18n.augmentation.d.ts +32 -2
  58. package/types/augmentation/http-controller.augmentation.d.ts +21 -0
  59. package/types/constants.d.ts +2 -0
  60. package/types/execution-context.d.ts +24 -26
  61. package/types/helpers/service-base.d.ts +10 -0
  62. package/types/http/express-adapter.d.ts +13 -0
  63. package/types/http/http-adapter.d.ts +27 -0
  64. package/types/http/http-context.d.ts +44 -0
  65. package/types/http/impl/asset-cache.d.ts +5 -0
  66. package/types/http/impl/http-handler.d.ts +73 -0
  67. package/types/http/impl/http-incoming.host.d.ts +23 -0
  68. package/types/http/impl/http-outgoing.host.d.ts +17 -0
  69. package/types/http/{helpers/multipart-helper.d.ts → impl/multipart-reader.d.ts} +8 -6
  70. package/types/http/impl/{http-incoming-message.host.d.ts → node-incoming-message.host.d.ts} +9 -22
  71. package/types/http/impl/{http-outgoing-message.host.d.ts → node-outgoing-message.host.d.ts} +11 -27
  72. package/types/http/{http-server-request.d.ts → interfaces/http-incoming.interface.d.ts} +28 -17
  73. package/types/http/{http-server-response.d.ts → interfaces/http-outgoing.interface.d.ts} +17 -10
  74. package/types/http/interfaces/node-incoming-message.interface.d.ts +38 -0
  75. package/types/http/interfaces/node-outgoing-message.interface.d.ts +29 -0
  76. package/types/http/utils/body-reader.d.ts +41 -0
  77. package/types/http/utils/wrap-exception.d.ts +2 -0
  78. package/types/index.d.ts +24 -26
  79. package/types/platform-adapter.d.ts +20 -48
  80. package/types/type-guards.d.ts +8 -0
  81. package/cjs/augmentation/collection.augmentation.js +0 -2
  82. package/cjs/augmentation/container.augmentation.js +0 -2
  83. package/cjs/augmentation/resource.augmentation.js +0 -26
  84. package/cjs/augmentation/singleton.augmentation.js +0 -2
  85. package/cjs/augmentation/storage.augmentation.js +0 -2
  86. package/cjs/execution-context.host.js +0 -46
  87. package/cjs/http/adapters/express-adapter.host.js +0 -34
  88. package/cjs/http/adapters/express-adapter.js +0 -14
  89. package/cjs/http/adapters/node-http-adapter.host.js +0 -70
  90. package/cjs/http/adapters/node-http-adapter.js +0 -14
  91. package/cjs/http/helpers/json-body-loader.js +0 -29
  92. package/cjs/http/helpers/query-parsers.js +0 -16
  93. package/cjs/http/http-adapter-host.js +0 -715
  94. package/cjs/interfaces/interceptor.interface.js +0 -2
  95. package/cjs/interfaces/request-handler.interface.js +0 -2
  96. package/cjs/platform-adapter.host.js +0 -154
  97. package/cjs/request-context.js +0 -25
  98. package/cjs/request.host.js +0 -24
  99. package/cjs/request.js +0 -2
  100. package/cjs/response.host.js +0 -22
  101. package/cjs/response.js +0 -2
  102. package/esm/augmentation/collection.augmentation.js +0 -1
  103. package/esm/augmentation/container.augmentation.js +0 -1
  104. package/esm/augmentation/resource.augmentation.js +0 -24
  105. package/esm/augmentation/singleton.augmentation.js +0 -1
  106. package/esm/augmentation/storage.augmentation.js +0 -1
  107. package/esm/execution-context.host.js +0 -42
  108. package/esm/http/adapters/express-adapter.host.js +0 -30
  109. package/esm/http/adapters/express-adapter.js +0 -11
  110. package/esm/http/adapters/node-http-adapter.host.js +0 -65
  111. package/esm/http/adapters/node-http-adapter.js +0 -11
  112. package/esm/http/helpers/json-body-loader.js +0 -24
  113. package/esm/http/helpers/query-parsers.js +0 -12
  114. package/esm/http/http-adapter-host.js +0 -710
  115. package/esm/interfaces/interceptor.interface.js +0 -1
  116. package/esm/interfaces/request-handler.interface.js +0 -1
  117. package/esm/platform-adapter.host.js +0 -149
  118. package/esm/request-context.js +0 -22
  119. package/esm/request.host.js +0 -20
  120. package/esm/request.js +0 -1
  121. package/esm/response.host.js +0 -18
  122. package/esm/response.js +0 -1
  123. package/i18n/i18n/en/error.json +0 -21
  124. package/types/augmentation/collection.augmentation.d.ts +0 -146
  125. package/types/augmentation/container.augmentation.d.ts +0 -14
  126. package/types/augmentation/resource.augmentation.d.ts +0 -38
  127. package/types/augmentation/singleton.augmentation.d.ts +0 -83
  128. package/types/augmentation/storage.augmentation.d.ts +0 -50
  129. package/types/execution-context.host.d.ts +0 -25
  130. package/types/http/adapters/express-adapter.d.ts +0 -15
  131. package/types/http/adapters/express-adapter.host.d.ts +0 -12
  132. package/types/http/adapters/node-http-adapter.d.ts +0 -17
  133. package/types/http/adapters/node-http-adapter.host.d.ts +0 -19
  134. package/types/http/helpers/json-body-loader.d.ts +0 -5
  135. package/types/http/helpers/query-parsers.d.ts +0 -1
  136. package/types/http/http-adapter-host.d.ts +0 -34
  137. package/types/interfaces/interceptor.interface.d.ts +0 -2
  138. package/types/interfaces/request-handler.interface.d.ts +0 -4
  139. package/types/platform-adapter.host.d.ts +0 -43
  140. package/types/request-context.d.ts +0 -13
  141. package/types/request.d.ts +0 -14
  142. package/types/request.host.d.ts +0 -27
  143. package/types/response.d.ts +0 -22
  144. package/types/response.host.d.ts +0 -22
  145. package/types/services/api-service.d.ts +0 -10
  146. /package/cjs/http/{helpers → utils}/common.js +0 -0
  147. /package/cjs/http/{helpers → utils}/concat-readable.js +0 -0
  148. /package/cjs/http/{helpers → utils}/convert-to-headers.js +0 -0
  149. /package/esm/http/{helpers → utils}/common.js +0 -0
  150. /package/esm/http/{helpers → utils}/concat-readable.js +0 -0
  151. /package/types/{services → helpers}/logger.d.ts +0 -0
  152. /package/types/http/{helpers → utils}/common.d.ts +0 -0
  153. /package/types/http/{helpers → utils}/concat-readable.d.ts +0 -0
  154. /package/types/http/{helpers → utils}/convert-to-headers.d.ts +0 -0
  155. /package/types/http/{helpers → utils}/convert-to-raw-headers.d.ts +0 -0
  156. /package/types/http/{helpers → utils}/match-known-fields.d.ts +0 -0
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,154 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PlatformAdapterHost = void 0;
4
- const tslib_1 = require("tslib");
5
- require("./augmentation/18n.augmentation.js");
6
- const path_1 = tslib_1.__importDefault(require("path"));
7
- const putil_varhelpers_1 = require("putil-varhelpers");
8
- const strict_typed_events_1 = require("strict-typed-events");
9
- const common_1 = require("@opra/common");
10
- const logger_js_1 = require("./services/logger.js");
11
- const resourceInitialized = Symbol.for('opra.resource.initialized');
12
- /**
13
- * @class PlatformAdapterHost
14
- */
15
- class PlatformAdapterHost extends strict_typed_events_1.AsyncEventEmitter {
16
- constructor() {
17
- super(...arguments);
18
- this._controllers = new Map();
19
- }
20
- get api() {
21
- return this._api;
22
- }
23
- get platform() {
24
- return this._platform;
25
- }
26
- get protocol() {
27
- return this._protocol;
28
- }
29
- get serviceName() {
30
- return this._serviceName;
31
- }
32
- get i18n() {
33
- return this._i18n;
34
- }
35
- async close() {
36
- const promises = [];
37
- for (const r of this._controllers.values()) {
38
- const onShutdown = r?.onShutdown;
39
- if (onShutdown)
40
- promises.push((async () => onShutdown.call(r.controller, r))());
41
- }
42
- await Promise.allSettled(promises);
43
- this._controllers.clear();
44
- }
45
- /**
46
- * Initializes the adapter
47
- */
48
- async init(api, options) {
49
- if (this._api)
50
- throw new Error(`Already initialized`);
51
- this._api = api;
52
- this._interceptors = [...(options?.interceptors || [])];
53
- this._logger = options?.logger && options.logger instanceof logger_js_1.Logger
54
- ? options.logger
55
- : new logger_js_1.Logger({ instance: options?.logger });
56
- // Assign events
57
- if (options?.on) {
58
- for (const [event, fn] of Object.entries(options.on)) {
59
- /* istanbul ignore next */
60
- if (typeof fn === 'function')
61
- this.on(event, fn);
62
- }
63
- }
64
- // Make a safe service name
65
- this._serviceName = (0, putil_varhelpers_1.pascalCase)((api.info.title || '').replace(/[^a-z0-9_ ]/ig, '')) || 'OpraService';
66
- if (!/^[a-z]/i.test(this._serviceName))
67
- this._serviceName = 'X' + this._serviceName;
68
- // Init I18n
69
- if (options?.i18n instanceof common_1.I18n)
70
- this._i18n = options.i18n;
71
- else if (typeof options?.i18n === 'function')
72
- this._i18n = await options.i18n();
73
- else
74
- this._i18n = await this._createI18n(options?.i18n);
75
- this._i18n = this._i18n || common_1.I18n.defaultInstance;
76
- if (!this._i18n.isInitialized)
77
- await this._i18n.init();
78
- // Initialize all resources
79
- await this.getController(this.api.root);
80
- }
81
- async getController(resource) {
82
- resource = typeof resource === 'object' && resource instanceof common_1.Resource
83
- ? resource : this.api.getResource(resource);
84
- let controller = this._controllers.get(resource);
85
- if (!controller) {
86
- controller = resource.controller;
87
- if (!controller && resource.ctor) {
88
- controller = new resource.ctor();
89
- }
90
- this._controllers.set(resource, controller);
91
- }
92
- if (controller && !controller[resourceInitialized]) {
93
- controller[resourceInitialized] = true;
94
- // Initialize controller
95
- if (typeof controller.onInit === 'function')
96
- await controller.onInit.call(controller);
97
- // Initialize sub resources of Container
98
- if (resource instanceof common_1.Container) {
99
- for (const r of resource.resources.values()) {
100
- await this.getController(r);
101
- }
102
- }
103
- }
104
- return controller;
105
- }
106
- async getActionHandler(resource, name) {
107
- resource = typeof resource === 'object' && resource instanceof common_1.Resource
108
- ? resource
109
- : this.api.getResource(resource);
110
- const controller = await this.getController(resource);
111
- const endpoint = resource.actions.get(name);
112
- if (endpoint) {
113
- const handler = typeof controller[endpoint.name] === 'function' ? controller[endpoint.name] : undefined;
114
- if (handler)
115
- return { controller, endpoint, handler };
116
- }
117
- throw new common_1.BadRequestError({
118
- message: (0, common_1.translate)('error:ACTION_NOT_FOUND', { resource: resource.name, action: name }),
119
- severity: 'error',
120
- code: 'ACTION_NOT_FOUND'
121
- });
122
- }
123
- async getOperationHandler(resource, name) {
124
- resource = typeof resource === 'object' && resource instanceof common_1.Resource
125
- ? resource
126
- : this.api.getResource(resource);
127
- const controller = await this.getController(resource);
128
- const endpoint = resource instanceof common_1.CrudResource && resource.operations.get(name);
129
- if (endpoint) {
130
- const handler = typeof controller[endpoint.name] === 'function' ? controller[endpoint.name] : undefined;
131
- if (handler)
132
- return { controller, endpoint, handler };
133
- }
134
- throw new common_1.ForbiddenError({
135
- message: (0, common_1.translate)('error:OPERATION_FORBIDDEN', { resource: resource.name, operation: name }),
136
- severity: 'error',
137
- code: 'OPERATION_FORBIDDEN'
138
- });
139
- }
140
- async _createI18n(options) {
141
- const opts = {
142
- ...options,
143
- };
144
- delete opts.resourceDirs;
145
- const instance = common_1.I18n.createInstance(opts);
146
- await instance.init();
147
- await instance.loadResourceDir(path_1.default.resolve((0, common_1.getStackFileName)(), '../../../i18n'));
148
- if (options?.resourceDirs)
149
- for (const dir of options.resourceDirs)
150
- await instance.loadResourceDir(dir);
151
- return instance;
152
- }
153
- }
154
- exports.PlatformAdapterHost = PlatformAdapterHost;
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RequestContext = void 0;
4
- const common_1 = require("@opra/common");
5
- const execution_context_js_1 = require("./execution-context.js");
6
- var RequestContext;
7
- (function (RequestContext) {
8
- function from(executionContext, api, request, response) {
9
- const out = {
10
- api,
11
- request,
12
- response
13
- };
14
- Object.setPrototypeOf(out, executionContext);
15
- return out;
16
- }
17
- RequestContext.from = from;
18
- function is(v) {
19
- return execution_context_js_1.ExecutionContext.is(v) &&
20
- v.api instanceof common_1.ApiDocument &&
21
- v.request &&
22
- v.response;
23
- }
24
- RequestContext.is = is;
25
- })(RequestContext || (exports.RequestContext = RequestContext = {}));
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RequestHost = void 0;
4
- class RequestHost {
5
- constructor(init) {
6
- Object.assign(this, init);
7
- this.params = this.params || {};
8
- }
9
- get resource() {
10
- return this.endpoint.resource;
11
- }
12
- switchToHttp() {
13
- if (this.http)
14
- return this.http;
15
- throw new TypeError('Not executing in an "Http" context');
16
- }
17
- switchToWs() {
18
- throw new TypeError('Not executing in an "WebSocket" context');
19
- }
20
- switchToRpc() {
21
- throw new TypeError('Not executing in an "RPC" context');
22
- }
23
- }
24
- exports.RequestHost = RequestHost;
package/cjs/request.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ResponseHost = void 0;
4
- class ResponseHost {
5
- constructor(init) {
6
- if (init)
7
- Object.assign(this, init);
8
- this.errors = this.errors || [];
9
- }
10
- switchToHttp() {
11
- if (this.http)
12
- return this.http;
13
- throw new TypeError('Not executing in an "Http" context');
14
- }
15
- switchToWs() {
16
- throw new TypeError('Not executing in an "WebSocket" context');
17
- }
18
- switchToRpc() {
19
- throw new TypeError('Not executing in an "RPC" context');
20
- }
21
- }
22
- exports.ResponseHost = ResponseHost;
package/cjs/response.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,24 +0,0 @@
1
- import { Collection, Resource, RESOURCE_METADATA, Singleton } from "@opra/common";
2
- // @ts-ignore
3
- const oldConstruct = Resource.prototype._construct;
4
- // @ts-ignore
5
- Resource.prototype._construct = function (init) {
6
- oldConstruct.call(this, init);
7
- const _this = this;
8
- _this.onInit = init.onInit;
9
- _this.onShutdown = init.onShutdown;
10
- };
11
- Collection.OnInit = Singleton.OnInit = function () {
12
- return (target, propertyKey) => {
13
- const sourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
14
- sourceMetadata.onInit = target[propertyKey];
15
- Reflect.defineMetadata(RESOURCE_METADATA, target.constructor, sourceMetadata);
16
- };
17
- };
18
- Collection.OnShutdown = Singleton.OnShutdown = function () {
19
- return (target, propertyKey) => {
20
- const sourceMetadata = (Reflect.getOwnMetadata(RESOURCE_METADATA, target.constructor) || {});
21
- sourceMetadata.onShutdown = target[propertyKey];
22
- Reflect.defineMetadata(RESOURCE_METADATA, target.constructor, sourceMetadata);
23
- };
24
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,42 +0,0 @@
1
- import { AsyncEventEmitter } from 'strict-typed-events';
2
- export class ExecutionContextHost extends AsyncEventEmitter {
3
- constructor(api, platform, protocol) {
4
- super();
5
- this.api = api;
6
- this.platform = platform;
7
- this.ws = protocol.ws;
8
- this.rpc = protocol.rpc;
9
- if (protocol.http) {
10
- this.protocol = 'http';
11
- this.http = {
12
- platform,
13
- incoming: protocol.http.incoming,
14
- outgoing: protocol.http.outgoing,
15
- switchToContext: () => this
16
- };
17
- }
18
- else if (protocol.ws) {
19
- this.protocol = 'ws';
20
- this.ws = protocol.ws;
21
- }
22
- else if (protocol.rpc) {
23
- this.protocol = 'rpc';
24
- this.rpc = protocol.rpc;
25
- }
26
- }
27
- switchToHttp() {
28
- if (this.http)
29
- return this.http;
30
- throw new TypeError('Not executing in an "Http" context');
31
- }
32
- switchToWs() {
33
- if (this.ws)
34
- return this.ws;
35
- throw new TypeError('Not executing in an "WebSocket" context');
36
- }
37
- switchToRpc() {
38
- if (this.rpc)
39
- return this.rpc;
40
- throw new TypeError('Not executing in an "RPC" context');
41
- }
42
- }
@@ -1,30 +0,0 @@
1
- import { OpraURLPath } from '@opra/common';
2
- import { HttpAdapterHost } from '../http-adapter-host.js';
3
- import { HttpServerRequest } from '../http-server-request.js';
4
- import { HttpServerResponse } from '../http-server-response.js';
5
- export class ExpressAdapterHost extends HttpAdapterHost {
6
- constructor(app) {
7
- super();
8
- this._platform = 'express';
9
- this._app = app;
10
- }
11
- get app() {
12
- return this._app;
13
- }
14
- async init(api, options) {
15
- await super.init(api, options);
16
- this._api = api;
17
- const basePath = new OpraURLPath(options?.basePath);
18
- this._app.use(basePath.toString(), (_req, _res) => {
19
- const req = HttpServerRequest.from(_req);
20
- const res = HttpServerResponse.from(_res);
21
- this.handleHttp(req, res)
22
- .catch((e) => this._logger.fatal(e));
23
- });
24
- }
25
- static async create(app, api, options) {
26
- const adapter = new ExpressAdapterHost(app);
27
- await adapter.init(api, options);
28
- return adapter;
29
- }
30
- }
@@ -1,11 +0,0 @@
1
- import { ExpressAdapterHost } from './express-adapter.host.js';
2
- /**
3
- * @namespace
4
- */
5
- export var ExpressAdapter;
6
- (function (ExpressAdapter) {
7
- async function create(app, api, options) {
8
- return ExpressAdapterHost.create(app, api, options);
9
- }
10
- ExpressAdapter.create = create;
11
- })(ExpressAdapter || (ExpressAdapter = {}));
@@ -1,65 +0,0 @@
1
- import http from 'http';
2
- import { HttpStatusCode, HttpStatusMessages, OpraURL, OpraURLPath, } from '@opra/common';
3
- import { HttpAdapterHost } from '../http-adapter-host.js';
4
- import { HttpServerRequest } from '../http-server-request.js';
5
- import { HttpServerResponse } from '../http-server-response.js';
6
- /**
7
- * @class NodeHttpAdapterHost
8
- */
9
- export class NodeHttpAdapterHost extends HttpAdapterHost {
10
- constructor() {
11
- super(...arguments);
12
- this._platform = 'http';
13
- }
14
- get basePath() {
15
- return this._basePath;
16
- }
17
- get server() {
18
- return this._server;
19
- }
20
- async close() {
21
- await super.close();
22
- if (this.server.listening)
23
- await new Promise((resolve, reject) => {
24
- this.server.close((err) => {
25
- if (err)
26
- return reject(err);
27
- resolve();
28
- });
29
- });
30
- }
31
- async init(api, options) {
32
- await super.init(api, options);
33
- this._basePath = new OpraURLPath(options?.basePath);
34
- this._server = http.createServer((incomingMessage, serverResponse) => this._serverListener(incomingMessage, serverResponse));
35
- }
36
- _serverListener(incomingMessage, serverResponse) {
37
- const originalUrl = incomingMessage.url;
38
- const parsedUrl = new OpraURL(originalUrl);
39
- const relativePath = OpraURLPath.relative(parsedUrl.path, this.basePath);
40
- if (!relativePath) {
41
- serverResponse.statusCode = HttpStatusCode.NOT_FOUND;
42
- serverResponse.statusMessage = HttpStatusMessages[HttpStatusCode.NOT_FOUND];
43
- serverResponse.end();
44
- return;
45
- }
46
- parsedUrl.path = relativePath;
47
- incomingMessage.originalUrl = originalUrl;
48
- incomingMessage.baseUrl = this.basePath.toString();
49
- incomingMessage.parsedUrl = parsedUrl;
50
- Object.defineProperty(incomingMessage, 'searchParams', {
51
- get() {
52
- return incomingMessage.parsedUrl.searchParams;
53
- }
54
- });
55
- const req = HttpServerRequest.from(incomingMessage);
56
- const res = HttpServerResponse.from(serverResponse);
57
- this.handleHttp(req, res)
58
- .catch((e) => this._logger.fatal(e));
59
- }
60
- static async create(api, options) {
61
- const adapter = new NodeHttpAdapterHost();
62
- await adapter.init(api, options);
63
- return adapter;
64
- }
65
- }
@@ -1,11 +0,0 @@
1
- import { NodeHttpAdapterHost } from './node-http-adapter.host.js';
2
- /**
3
- * @namespace NodeHttpAdapter
4
- */
5
- export var NodeHttpAdapter;
6
- (function (NodeHttpAdapter) {
7
- async function create(api, options) {
8
- return NodeHttpAdapterHost.create(api, options);
9
- }
10
- NodeHttpAdapter.create = create;
11
- })(NodeHttpAdapter || (NodeHttpAdapter = {}));
@@ -1,24 +0,0 @@
1
- import bodyParser from 'body-parser';
2
- const bodyLoaderCache = new WeakMap();
3
- export function jsonBodyLoader(options, cachePoint) {
4
- let bodyLoader = cachePoint ? bodyLoaderCache.get(cachePoint) : undefined;
5
- if (bodyLoader)
6
- return bodyLoader;
7
- const parser = bodyParser.json({
8
- ...options,
9
- type: 'json'
10
- });
11
- bodyLoader = (incoming) => {
12
- return new Promise((resolve, reject) => {
13
- const next = (error) => {
14
- if (error)
15
- return reject(error);
16
- resolve(incoming.body);
17
- };
18
- parser(incoming, {}, next);
19
- });
20
- };
21
- if (cachePoint)
22
- bodyLoaderCache.set(cachePoint, bodyLoader);
23
- return bodyLoader;
24
- }
@@ -1,12 +0,0 @@
1
- import { splitString } from 'fast-tokenizer';
2
- export function parseArrayParam(v) {
3
- if (!v)
4
- return;
5
- return splitString(v, {
6
- delimiters: ',',
7
- quotes: true,
8
- brackets: true,
9
- keepBrackets: true,
10
- keepQuotes: true
11
- }).map(x => x.trim());
12
- }