@opra/http 1.23.3 → 1.24.0

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/http-context.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import typeIs from '@browsery/type-is';
2
2
  import { InternalServerError, NotAcceptableError, } from '@opra/common';
3
3
  import { ExecutionContext, kAssetCache } from '@opra/core';
4
+ import toml from 'toml';
5
+ import yaml from 'yaml';
4
6
  import { MultipartReader } from './impl/multipart-reader.js';
5
7
  export class HttpContext extends ExecutionContext {
6
8
  _body;
@@ -21,12 +23,12 @@ export class HttpContext extends ExecutionContext {
21
23
  init.__adapter.document.node,
22
24
  transport: 'http',
23
25
  });
24
- if (init.__contDef)
25
- this.__contDef = init.__contDef;
26
26
  if (init.__controller)
27
27
  this.__controller = init.__controller;
28
+ if (init.__contDef)
29
+ this.__contDef = Object.create(init.__contDef);
28
30
  if (init.__oprDef)
29
- this.__oprDef = init.__oprDef;
31
+ this.__oprDef = Object.create(init.__oprDef);
30
32
  if (init.__handler)
31
33
  this.__handler = init.__handler;
32
34
  this.request = init.request;
@@ -86,14 +88,22 @@ export class HttpContext extends ExecutionContext {
86
88
  limit: __oprDef?.requestBody?.maxContentSize,
87
89
  });
88
90
  if (this._body != null) {
89
- // Convert Buffer to string if media is text
91
+ const encoding = request.characterEncoding();
92
+ if (encoding)
93
+ this._body = this._body.toString(encoding);
90
94
  if (Buffer.isBuffer(this._body) &&
91
- request.is(['json', 'xml', 'txt', 'text'])) {
95
+ request.is(['json', 'xml', 'txt', 'text', 'yaml', 'toml'])) {
92
96
  this._body = this._body.toString('utf-8');
93
97
  }
94
98
  // Transform text to Object if media is JSON
95
99
  if (typeof this._body === 'string' && request.is(['json']))
96
100
  this._body = JSON.parse(this._body);
101
+ // Transform text to Object if media is YAML
102
+ if (typeof this._body === 'string' && request.is(['yaml']))
103
+ this._body = yaml.parse(this._body);
104
+ // Transform text to Object if media is YAML
105
+ if (typeof this._body === 'string' && request.is(['toml']))
106
+ this._body = toml.parse(this._body);
97
107
  }
98
108
  if (mediaType) {
99
109
  // Decode/Validate the data object according to data model
package/http-handler.js CHANGED
@@ -279,9 +279,17 @@ export class HttpHandler {
279
279
  let mediaType;
280
280
  let contentType = request.header('content-type');
281
281
  if (contentType) {
282
- contentType = parseContentType(contentType).type;
282
+ const ct = parseContentType(contentType);
283
+ contentType = ct.type;
283
284
  mediaType = __oprDef.requestBody.content.find(mc => mc.contentType &&
284
285
  typeIs.is(contentType, Array.isArray(mc.contentType) ? mc.contentType : [mc.contentType]));
286
+ if (mediaType)
287
+ mediaType = Object.create(mediaType);
288
+ if (ct && mediaType) {
289
+ mediaType.contentType = contentType;
290
+ mediaType.contentEncoding =
291
+ ct.parameters?.['charset'] || mediaType.contentEncoding;
292
+ }
285
293
  }
286
294
  if (!mediaType) {
287
295
  const contentTypes = __oprDef.requestBody.content
@@ -16,6 +16,7 @@ export declare class HttpIncomingHost implements HttpIncoming {
16
16
  acceptsCharsets(...charsets: any): any;
17
17
  acceptsEncodings(...encoding: any): any;
18
18
  acceptsLanguages(...lang: any): any;
19
+ characterEncoding(): string | undefined;
19
20
  is(type: string | string[], ...otherTypes: string[]): string | false | null;
20
21
  range(size: number, options: any): parseRange.Ranges | parseRange.Result | undefined;
21
22
  readBody(options: BodyReader.Options): Promise<string | Buffer | undefined>;
@@ -7,6 +7,7 @@ import accepts from 'accepts';
7
7
  import fresh from 'fresh';
8
8
  import parseRange from 'range-parser';
9
9
  import { BodyReader } from '../utils/body-reader.js';
10
+ const ENCODING_PATTERN = /encoding=([^;]+)/;
10
11
  export class HttpIncomingHost {
11
12
  body;
12
13
  get protocol() {
@@ -87,6 +88,10 @@ export class HttpIncomingHost {
87
88
  // eslint-disable-next-line prefer-spread
88
89
  return accept.languages.apply(accept, lang);
89
90
  }
91
+ characterEncoding() {
92
+ const contentType = this.header('content-type');
93
+ return contentType ? ENCODING_PATTERN.exec(contentType)?.[1] : undefined;
94
+ }
90
95
  is(type, ...otherTypes) {
91
96
  const types = Array.isArray(type) ? type : [type];
92
97
  if (otherTypes.length)
@@ -152,6 +152,7 @@ export interface HttpIncoming extends NodeIncomingMessage {
152
152
  acceptsLanguages(lang: string): string | false;
153
153
  acceptsLanguages(lang: string[]): string | false;
154
154
  acceptsLanguages(...lang: string[]): string | false;
155
+ characterEncoding(): string | undefined;
155
156
  /**
156
157
  * Check if the incoming request contains the "Content-Type"
157
158
  * header field, and it contains the give mime `type`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/http",
3
- "version": "1.23.3",
3
+ "version": "1.24.0",
4
4
  "description": "Opra Http Server Adapter",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -8,7 +8,7 @@
8
8
  "@browsery/antlr4": "^4.13.3-r4",
9
9
  "@browsery/http-parser": "^0.5.10-r2",
10
10
  "@browsery/type-is": "^2.0.1",
11
- "@jsopen/objects": "^2.1.1",
11
+ "@jsopen/objects": "^2.2.0",
12
12
  "accepts": "^1.3.8",
13
13
  "base64-stream": "^1.0.0",
14
14
  "busboy": "^1.6.0",
@@ -18,7 +18,7 @@
18
18
  "cookie": "^1.1.1",
19
19
  "cookie-signature": "^1.2.2",
20
20
  "encodeurl": "^2.0.0",
21
- "expect": "^30.2.0",
21
+ "expect": "^30.3.0",
22
22
  "fast-tokenizer": "^1.9.0",
23
23
  "fresh": "^0.5.2",
24
24
  "iconv-lite": "^0.7.2",
@@ -30,13 +30,15 @@
30
30
  "raw-body": "^3.0.2",
31
31
  "reflect-metadata": "^0.2.2",
32
32
  "super-fast-md5": "^1.0.3",
33
+ "toml": "^3.0.0",
33
34
  "tslib": "^2.8.1",
34
35
  "valgen": "^5.19.5",
35
- "vary": "^1.1.2"
36
+ "vary": "^1.1.2",
37
+ "yaml": "^2.8.2"
36
38
  },
37
39
  "peerDependencies": {
38
- "@opra/common": "^1.23.3",
39
- "@opra/core": "^1.23.3"
40
+ "@opra/common": "^1.24.0",
41
+ "@opra/core": "^1.24.0"
40
42
  },
41
43
  "optionalDependencies": {
42
44
  "express": "^4.0.0 || ^5.0.0",