@opra/http 1.25.4 → 1.25.6

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 (2) hide show
  1. package/http-context.js +62 -57
  2. package/package.json +3 -3
package/http-context.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import typeIs from '@browsery/type-is';
2
- import { InternalServerError, MimeTypes, NotAcceptableError, } from '@opra/common';
2
+ import { BadRequestError, InternalServerError, MimeTypes, NotAcceptableError, } from '@opra/common';
3
3
  import { ExecutionContext, kAssetCache } from '@opra/core';
4
4
  import toml from 'toml';
5
5
  import yaml from 'yaml';
@@ -75,65 +75,70 @@ export class HttpContext extends ExecutionContext {
75
75
  async getBody() {
76
76
  if (this._body !== undefined)
77
77
  return this._body;
78
- const { request, __oprDef, mediaType } = this;
79
- if (this.isMultipart) {
80
- const reader = await this.getMultipartReader();
81
- /** Retrieve all fields */
82
- const parts = await reader.getAll();
83
- /** Filter fields according to configuration */
84
- this._body = [...parts];
85
- return this._body;
86
- }
87
- this._body = await this.request.readBody({
88
- limit: __oprDef?.requestBody?.maxContentSize,
89
- });
90
- if (this._body != null) {
91
- const encoding = request.characterEncoding();
92
- if (encoding)
93
- this._body = this._body.toString(encoding);
94
- if (Buffer.isBuffer(this._body) &&
95
- request.is([
96
- 'json',
97
- 'xml',
98
- 'txt',
99
- 'text',
100
- 'yaml',
101
- 'toml',
102
- MimeTypes.yaml2,
103
- MimeTypes.toml2,
104
- ])) {
105
- this._body = this._body.toString('utf-8');
78
+ try {
79
+ const { request, __oprDef, mediaType } = this;
80
+ if (this.isMultipart) {
81
+ const reader = await this.getMultipartReader();
82
+ /** Retrieve all fields */
83
+ const parts = await reader.getAll();
84
+ /** Filter fields according to configuration */
85
+ this._body = [...parts];
86
+ return this._body;
106
87
  }
107
- // Transform text to Object if media is JSON
108
- if (typeof this._body === 'string' && request.is(['json']))
109
- this._body = JSON.parse(this._body);
110
- // Transform text to Object if media is YAML
111
- if (typeof this._body === 'string' &&
112
- request.is(['yaml', MimeTypes.yaml2]))
113
- this._body = yaml.parse(this._body);
114
- // Transform text to Object if media is YAML
115
- if (typeof this._body === 'string' &&
116
- request.is(['toml', MimeTypes.toml2]))
117
- this._body = toml.parse(this._body);
118
- }
119
- if (mediaType) {
120
- // Decode/Validate the data object according to data model
121
- if (this._body && mediaType.type) {
122
- let decode = this.__adapter[kAssetCache].get(mediaType, 'decode');
123
- if (!decode) {
124
- decode = mediaType.generateCodec('decode', {
125
- scope: this.__adapter.scope,
126
- partial: __oprDef?.requestBody?.partial,
127
- projection: '*',
128
- ignoreReadonlyFields: true,
129
- allowPatchOperators: __oprDef?.requestBody?.allowPatchOperators,
130
- keepKeyFields: __oprDef?.requestBody?.keepKeyFields,
131
- });
132
- this.__adapter[kAssetCache].set(mediaType, 'decode', decode);
88
+ this._body = await this.request.readBody({
89
+ limit: __oprDef?.requestBody?.maxContentSize,
90
+ });
91
+ if (this._body != null) {
92
+ const encoding = request.characterEncoding();
93
+ if (encoding)
94
+ this._body = this._body.toString(encoding);
95
+ if (Buffer.isBuffer(this._body) &&
96
+ request.is([
97
+ 'json',
98
+ 'xml',
99
+ 'txt',
100
+ 'text',
101
+ 'yaml',
102
+ 'toml',
103
+ MimeTypes.yaml2,
104
+ MimeTypes.toml2,
105
+ ])) {
106
+ this._body = this._body.toString('utf-8');
133
107
  }
134
- this._body = decode(this._body);
108
+ // Transform text to Object if media is JSON
109
+ if (typeof this._body === 'string' && request.is(['json']))
110
+ this._body = JSON.parse(this._body);
111
+ // Transform text to Object if media is YAML
112
+ if (typeof this._body === 'string' &&
113
+ request.is(['yaml', MimeTypes.yaml2]))
114
+ this._body = yaml.parse(this._body);
115
+ // Transform text to Object if media is YAML
116
+ if (typeof this._body === 'string' &&
117
+ request.is(['toml', MimeTypes.toml2]))
118
+ this._body = toml.parse(this._body);
135
119
  }
120
+ if (mediaType) {
121
+ // Decode/Validate the data object according to data model
122
+ if (this._body && mediaType.type) {
123
+ let decode = this.__adapter[kAssetCache].get(mediaType, 'decode');
124
+ if (!decode) {
125
+ decode = mediaType.generateCodec('decode', {
126
+ scope: this.__adapter.scope,
127
+ partial: __oprDef?.requestBody?.partial,
128
+ projection: '*',
129
+ ignoreReadonlyFields: true,
130
+ allowPatchOperators: __oprDef?.requestBody?.allowPatchOperators,
131
+ keepKeyFields: __oprDef?.requestBody?.keepKeyFields,
132
+ });
133
+ this.__adapter[kAssetCache].set(mediaType, 'decode', decode);
134
+ }
135
+ this._body = decode(this._body);
136
+ }
137
+ }
138
+ return this._body;
139
+ }
140
+ catch (error) {
141
+ throw new BadRequestError(error);
136
142
  }
137
- return this._body;
138
143
  }
139
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/http",
3
- "version": "1.25.4",
3
+ "version": "1.25.6",
4
4
  "description": "Opra Http Server Adapter",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -38,8 +38,8 @@
38
38
  "yaml": "^2.8.3"
39
39
  },
40
40
  "peerDependencies": {
41
- "@opra/common": "^1.25.4",
42
- "@opra/core": "^1.25.4"
41
+ "@opra/common": "^1.25.6",
42
+ "@opra/core": "^1.25.6"
43
43
  },
44
44
  "optionalDependencies": {
45
45
  "express": "^4.0.0 || ^5.0.0",