@opra/core 1.0.0-alpha.9 → 1.0.0-beta.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 (60) hide show
  1. package/cjs/augmentation/18n.augmentation.js +13 -13
  2. package/cjs/constants.js +1 -2
  3. package/cjs/execution-context.js +1 -0
  4. package/cjs/http/express-adapter.js +22 -20
  5. package/cjs/http/http-adapter.js +2 -5
  6. package/cjs/http/http-context.js +16 -31
  7. package/cjs/http/{impl/http-handler.js → http-handler.js} +194 -169
  8. package/cjs/http/impl/http-outgoing.host.js +2 -2
  9. package/cjs/http/impl/multipart-reader.js +141 -44
  10. package/cjs/http/utils/body-reader.js +0 -1
  11. package/cjs/http/utils/common.js +4 -4
  12. package/cjs/http/utils/concat-readable.js +1 -2
  13. package/cjs/http/utils/convert-to-headers.js +2 -3
  14. package/cjs/http/utils/convert-to-raw-headers.js +1 -2
  15. package/cjs/http/utils/match-known-fields.js +2 -2
  16. package/cjs/http/utils/wrap-exception.js +1 -2
  17. package/cjs/index.js +1 -1
  18. package/cjs/platform-adapter.js +0 -3
  19. package/cjs/type-guards.js +4 -5
  20. package/esm/augmentation/18n.augmentation.js +2 -2
  21. package/esm/constants.js +0 -1
  22. package/esm/execution-context.js +1 -0
  23. package/esm/http/express-adapter.js +22 -20
  24. package/esm/http/http-adapter.js +2 -5
  25. package/esm/http/http-context.js +17 -32
  26. package/esm/http/{impl/http-handler.js → http-handler.js} +195 -170
  27. package/esm/http/impl/http-outgoing.host.js +1 -1
  28. package/esm/http/impl/multipart-reader.js +142 -45
  29. package/esm/http/utils/body-reader.js +0 -1
  30. package/esm/index.js +1 -1
  31. package/esm/package.json +3 -0
  32. package/esm/platform-adapter.js +0 -3
  33. package/package.json +35 -63
  34. package/types/augmentation/http-controller.augmentation.d.ts +1 -2
  35. package/types/constants.d.ts +0 -1
  36. package/types/execution-context.d.ts +2 -1
  37. package/types/helpers/service-base.d.ts +1 -1
  38. package/types/http/express-adapter.d.ts +1 -1
  39. package/types/http/http-adapter.d.ts +35 -8
  40. package/types/http/http-context.d.ts +4 -4
  41. package/types/http/{impl/http-handler.d.ts → http-handler.d.ts} +11 -9
  42. package/types/http/impl/http-incoming.host.d.ts +1 -2
  43. package/types/http/impl/http-outgoing.host.d.ts +1 -1
  44. package/types/http/impl/multipart-reader.d.ts +38 -20
  45. package/types/http/impl/node-incoming-message.host.d.ts +3 -7
  46. package/types/http/impl/node-outgoing-message.host.d.ts +5 -8
  47. package/types/http/interfaces/http-incoming.interface.d.ts +2 -3
  48. package/types/http/interfaces/http-outgoing.interface.d.ts +2 -2
  49. package/types/http/interfaces/node-incoming-message.interface.d.ts +0 -2
  50. package/types/http/interfaces/node-outgoing-message.interface.d.ts +1 -3
  51. package/types/http/utils/body-reader.d.ts +1 -4
  52. package/types/http/utils/concat-readable.d.ts +0 -1
  53. package/types/http/utils/convert-to-raw-headers.d.ts +1 -2
  54. package/types/index.d.cts +28 -0
  55. package/types/index.d.ts +1 -1
  56. package/types/platform-adapter.d.ts +0 -4
  57. package/cjs/helpers/logger.js +0 -35
  58. package/esm/helpers/logger.js +0 -31
  59. package/i18n/i18n/en/error.json +0 -21
  60. package/types/helpers/logger.d.ts +0 -14
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
5
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
4
6
  const common_1 = require("@opra/common");
5
- const fs_1 = tslib_1.__importDefault(require("fs"));
6
- const path_1 = tslib_1.__importDefault(require("path"));
7
7
  common_1.I18n.load = async function (options) {
8
8
  const opts = {
9
9
  ...options,
@@ -11,7 +11,7 @@ common_1.I18n.load = async function (options) {
11
11
  delete opts.resourceDirs;
12
12
  const instance = common_1.I18n.createInstance(opts);
13
13
  await instance.init();
14
- await instance.loadResourceDir(path_1.default.resolve((0, common_1.getStackFileName)(), '../../../i18n'));
14
+ await instance.loadResourceDir(node_path_1.default.resolve((0, common_1.getStackFileName)(), '../../../i18n'));
15
15
  if (options?.resourceDirs)
16
16
  for (const dir of options.resourceDirs)
17
17
  await instance.loadResourceDir(dir);
@@ -23,7 +23,7 @@ common_1.I18n.prototype.loadResourceBundle = async function (lang, ns, filePath,
23
23
  obj = (await fetch(filePath, { headers: { accept: 'application/json' } })).json();
24
24
  }
25
25
  else {
26
- const content = fs_1.default.readFileSync(filePath, 'utf8');
26
+ const content = node_fs_1.default.readFileSync(filePath, 'utf8');
27
27
  obj = JSON.parse(content);
28
28
  }
29
29
  this.addResourceBundle(lang, ns, obj, deep, overwrite);
@@ -31,18 +31,18 @@ common_1.I18n.prototype.loadResourceBundle = async function (lang, ns, filePath,
31
31
  common_1.I18n.prototype.loadResourceDir = async function (dirnames, deep, overwrite) {
32
32
  for (const dirname of Array.isArray(dirnames) ? dirnames : [dirnames]) {
33
33
  /* istanbul ignore next */
34
- if (!fs_1.default.existsSync(dirname))
34
+ if (!node_fs_1.default.existsSync(dirname))
35
35
  continue;
36
- const languageDirs = fs_1.default.readdirSync(dirname);
36
+ const languageDirs = node_fs_1.default.readdirSync(dirname);
37
37
  for (const lang of languageDirs) {
38
- const langDir = path_1.default.join(dirname, lang);
39
- if (fs_1.default.statSync(langDir).isDirectory()) {
40
- const nsDirs = fs_1.default.readdirSync(langDir);
38
+ const langDir = node_path_1.default.join(dirname, lang);
39
+ if (node_fs_1.default.statSync(langDir).isDirectory()) {
40
+ const nsDirs = node_fs_1.default.readdirSync(langDir);
41
41
  for (const nsfile of nsDirs) {
42
- const nsFilePath = path_1.default.join(langDir, nsfile);
43
- const ext = path_1.default.extname(nsfile);
44
- if (ext === '.json' && fs_1.default.statSync(nsFilePath).isFile()) {
45
- const ns = path_1.default.basename(nsfile, ext);
42
+ const nsFilePath = node_path_1.default.join(langDir, nsfile);
43
+ const ext = node_path_1.default.extname(nsfile);
44
+ if (ext === '.json' && node_fs_1.default.statSync(nsFilePath).isFile()) {
45
+ const ns = node_path_1.default.basename(nsfile, ext);
46
46
  await this.loadResourceBundle(lang, ns, nsFilePath, deep, overwrite);
47
47
  }
48
48
  }
package/cjs/constants.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.kAssetCache = exports.kHandler = void 0;
4
- exports.kHandler = Symbol.for('kHandler');
3
+ exports.kAssetCache = void 0;
5
4
  exports.kAssetCache = Symbol.for('kAssetCache');
@@ -8,6 +8,7 @@ const strict_typed_events_1 = require("strict-typed-events");
8
8
  class ExecutionContext extends strict_typed_events_1.AsyncEventEmitter {
9
9
  constructor(init) {
10
10
  super();
11
+ this.errors = [];
11
12
  this.document = init.document;
12
13
  this.protocol = init.protocol;
13
14
  this.platform = init.platform;
@@ -2,14 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExpressAdapter = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const nodePath = tslib_1.__importStar(require("node:path"));
5
6
  const common_1 = require("@opra/common");
6
7
  const express_1 = require("express");
7
- const nodePath = tslib_1.__importStar(require("path"));
8
- const constants_js_1 = require("../constants.js");
9
8
  const http_adapter_js_1 = require("./http-adapter.js");
10
9
  const http_context_js_1 = require("./http-context.js");
11
10
  const http_incoming_interface_js_1 = require("./interfaces/http-incoming.interface.js");
12
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
13
  class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
14
14
  constructor(app, document, options) {
15
15
  super(document, options);
@@ -40,7 +40,8 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
40
40
  await resource.onShutdown.call(instance, resource);
41
41
  }
42
42
  catch (e) {
43
- this.logger.error(e);
43
+ if (this.listenerCount('error'))
44
+ this.emit('error', (0, wrap_exception_js_1.wrapException)(e));
44
45
  }
45
46
  }
46
47
  }
@@ -63,10 +64,10 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
63
64
  }
64
65
  else
65
66
  this.app.use(router);
66
- const createContext = (_req, _res, args) => {
67
+ const createContext = async (_req, _res, args) => {
67
68
  const request = http_incoming_interface_js_1.HttpIncoming.from(_req);
68
69
  const response = http_outgoing_interface_js_1.HttpOutgoing.from(_res);
69
- return new http_context_js_1.HttpContext({
70
+ const ctx = new http_context_js_1.HttpContext({
70
71
  adapter: this,
71
72
  platform: this.platform,
72
73
  request,
@@ -76,11 +77,14 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
76
77
  operation: args?.operation,
77
78
  operationHandler: args?.operationHandler,
78
79
  });
80
+ await this.emitAsync('createContext', ctx);
81
+ return ctx;
79
82
  };
80
83
  /** Add an endpoint that returns document schema */
81
84
  router.get('/\\$schema', (_req, _res, next) => {
82
- const context = createContext(_req, _res);
83
- this[constants_js_1.kHandler].sendDocumentSchema(context).catch(next);
85
+ createContext(_req, _res)
86
+ .then(ctx => this.handler.sendDocumentSchema(ctx).catch(next))
87
+ .catch(next);
84
88
  });
85
89
  /** Add operation endpoints */
86
90
  if (this.api.controllers.size) {
@@ -94,19 +98,18 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
94
98
  continue;
95
99
  /** Define router callback */
96
100
  router[operation.method.toLowerCase()](routePath, (_req, _res, _next) => {
97
- const context = createContext(_req, _res, {
101
+ createContext(_req, _res, {
98
102
  controller,
99
103
  controllerInstance,
100
104
  operation,
101
105
  operationHandler,
102
- });
103
- this[constants_js_1.kHandler]
104
- .handleRequest(context)
106
+ })
107
+ .then(ctx => this.handler.handleRequest(ctx))
105
108
  .then(() => {
106
109
  if (!_res.headersSent)
107
110
  _next();
108
111
  })
109
- .catch((e) => this.logger.fatal(e));
112
+ .catch((e) => this.emit('error', e));
110
113
  });
111
114
  }
112
115
  if (controller.controllers.size) {
@@ -119,18 +122,17 @@ class ExpressAdapter extends http_adapter_js_1.HttpAdapter {
119
122
  }
120
123
  /** Add an endpoint that returns 404 error at last */
121
124
  router.use('*', (_req, _res, next) => {
122
- const res = http_outgoing_interface_js_1.HttpOutgoing.from(_res);
123
- // const url = new URL(_req.originalUrl, '')
124
- this[constants_js_1.kHandler]
125
- .sendErrorResponse(res, [
126
- new common_1.NotFoundError({
127
- message: `No endpoint found for [${_req.method}]${_req.baseUrl}`,
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}`,
128
129
  details: {
129
130
  path: _req.baseUrl,
130
131
  method: _req.method,
131
132
  },
132
- }),
133
- ])
133
+ }));
134
+ this.handler.sendResponse(ctx).catch(next);
135
+ })
134
136
  .catch(next);
135
137
  });
136
138
  }
@@ -2,9 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HttpAdapter = void 0;
4
4
  const common_1 = require("@opra/common");
5
- const constants_js_1 = require("../constants.js");
6
5
  const platform_adapter_js_1 = require("../platform-adapter.js");
7
- const http_handler_js_1 = require("./impl/http-handler.js");
6
+ const http_handler_js_1 = require("./http-handler.js");
8
7
  /**
9
8
  *
10
9
  * @class HttpAdapter
@@ -15,10 +14,8 @@ class HttpAdapter extends platform_adapter_js_1.PlatformAdapter {
15
14
  this.protocol = 'http';
16
15
  if (!(document.api instanceof common_1.HttpApi))
17
16
  throw new TypeError(`The document does not expose an HTTP Api`);
18
- this[constants_js_1.kHandler] = new http_handler_js_1.HttpHandler(this);
17
+ this.handler = new http_handler_js_1.HttpHandler(this);
19
18
  this.interceptors = [...(options?.interceptors || [])];
20
- if (options?.onRequest)
21
- this.on('request', options.onRequest);
22
19
  }
23
20
  get api() {
24
21
  return this.document.api;
@@ -29,6 +29,10 @@ class HttpContext extends execution_context_js_1.ExecutionContext {
29
29
  this.pathParams = init.pathParams || {};
30
30
  this.queryParams = init.queryParams || {};
31
31
  this._body = init.body;
32
+ this.on('finish', () => {
33
+ if (this._multipartReader)
34
+ this._multipartReader.purge().catch(() => undefined);
35
+ });
32
36
  }
33
37
  get isMultipart() {
34
38
  return !!this.request.is('multipart');
@@ -38,21 +42,21 @@ class HttpContext extends execution_context_js_1.ExecutionContext {
38
42
  throw new common_1.InternalServerError('Request content is not a multipart content');
39
43
  if (this._multipartReader)
40
44
  return this._multipartReader;
41
- const { request, mediaType } = this;
45
+ const { mediaType } = this;
42
46
  if (mediaType?.contentType) {
43
47
  const arr = Array.isArray(mediaType.contentType) ? mediaType.contentType : [mediaType.contentType];
44
48
  const contentType = arr.find(ct => type_is_1.default.is(ct, ['multipart']));
45
49
  if (!contentType)
46
50
  throw new common_1.NotAcceptableError('This endpoint does not accept multipart requests');
47
51
  }
48
- const reader = new multipart_reader_js_1.MultipartReader(request, {
49
- maxFields: mediaType?.maxFields,
50
- maxFieldsSize: mediaType?.maxFieldsSize,
51
- maxFiles: mediaType?.maxFiles,
52
- maxFileSize: mediaType?.maxFileSize,
53
- maxTotalFileSize: mediaType?.maxTotalFileSize,
54
- minFileSize: mediaType?.minFileSize,
55
- });
52
+ const reader = new multipart_reader_js_1.MultipartReader(this, {
53
+ limits: {
54
+ fields: mediaType?.maxFields,
55
+ fieldSize: mediaType?.maxFieldsSize,
56
+ files: mediaType?.maxFiles,
57
+ fileSize: mediaType?.maxFileSize,
58
+ },
59
+ }, mediaType);
56
60
  this._multipartReader = reader;
57
61
  return reader;
58
62
  }
@@ -65,29 +69,10 @@ class HttpContext extends execution_context_js_1.ExecutionContext {
65
69
  /** Retrieve all fields */
66
70
  const parts = await reader.getAll();
67
71
  /** Filter fields according to configuration */
68
- this._body = [];
69
- const multipartFields = mediaType?.multipartFields;
70
- if (mediaType && multipartFields?.length) {
71
- const fieldsFound = new Map();
72
- for (const item of parts) {
73
- const field = mediaType.findMultipartField(item.fieldName, item.type);
74
- if (field) {
75
- fieldsFound.set(field, true);
76
- this._body.push(item);
77
- }
78
- }
79
- /** Check required fields */
80
- for (const field of multipartFields) {
81
- if (field.required && !fieldsFound.get(field)) {
82
- throw new common_1.BadRequestError({
83
- message: `Multipart field (${field.fieldName}) is required`,
84
- });
85
- }
86
- }
87
- }
72
+ this._body = [...parts];
88
73
  return this._body;
89
74
  }
90
- this._body = await this.request.readBody({ limit: operation.requestBody?.maxContentSize });
75
+ this._body = await this.request.readBody({ limit: operation?.requestBody?.maxContentSize });
91
76
  if (this._body != null) {
92
77
  // Convert Buffer to string if media is text
93
78
  if (Buffer.isBuffer(this._body) && request.is(['json', 'xml', 'txt', 'text'])) {
@@ -104,7 +89,7 @@ class HttpContext extends execution_context_js_1.ExecutionContext {
104
89
  if (!decode) {
105
90
  decode =
106
91
  mediaType.type?.generateCodec('decode', {
107
- partial: operation.requestBody?.partial,
92
+ partial: operation?.requestBody?.partial,
108
93
  projection: '*',
109
94
  ignoreReadonlyFields: true,
110
95
  }) || valgen_1.vg.isAny();