@opra/core 1.0.0-alpha.3 → 1.0.0-alpha.30

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 (63) hide show
  1. package/cjs/augmentation/18n.augmentation.js +1 -1
  2. package/cjs/constants.js +1 -2
  3. package/cjs/execution-context.js +1 -1
  4. package/cjs/http/express-adapter.js +25 -34
  5. package/cjs/http/http-adapter.js +2 -5
  6. package/cjs/http/http-context.js +20 -32
  7. package/cjs/http/{impl/http-handler.js → http-handler.js} +249 -213
  8. package/cjs/http/impl/http-incoming.host.js +3 -3
  9. package/cjs/http/impl/http-outgoing.host.js +2 -2
  10. package/cjs/http/impl/multipart-reader.js +141 -50
  11. package/cjs/http/impl/node-incoming-message.host.js +5 -3
  12. package/cjs/http/interfaces/node-incoming-message.interface.js +3 -2
  13. package/cjs/http/utils/body-reader.js +6 -5
  14. package/cjs/http/utils/common.js +6 -5
  15. package/cjs/http/utils/concat-readable.js +1 -2
  16. package/cjs/http/utils/convert-to-headers.js +2 -3
  17. package/cjs/http/utils/convert-to-raw-headers.js +1 -2
  18. package/cjs/http/utils/match-known-fields.js +2 -2
  19. package/cjs/http/utils/wrap-exception.js +1 -2
  20. package/cjs/index.js +4 -4
  21. package/cjs/platform-adapter.js +1 -4
  22. package/cjs/type-guards.js +4 -5
  23. package/esm/augmentation/18n.augmentation.js +1 -1
  24. package/esm/constants.js +0 -1
  25. package/esm/execution-context.js +1 -1
  26. package/esm/http/express-adapter.js +25 -34
  27. package/esm/http/http-adapter.js +2 -5
  28. package/esm/http/http-context.js +21 -33
  29. package/esm/http/{impl/http-handler.js → http-handler.js} +243 -207
  30. package/esm/http/impl/http-incoming.host.js +3 -3
  31. package/esm/http/impl/http-outgoing.host.js +2 -2
  32. package/esm/http/impl/multipart-reader.js +142 -51
  33. package/esm/http/impl/node-incoming-message.host.js +5 -3
  34. package/esm/http/interfaces/node-incoming-message.interface.js +3 -2
  35. package/esm/http/utils/body-reader.js +6 -5
  36. package/esm/http/utils/common.js +2 -1
  37. package/esm/index.js +4 -4
  38. package/esm/platform-adapter.js +1 -4
  39. package/package.json +21 -14
  40. package/types/augmentation/18n.augmentation.d.ts +1 -1
  41. package/types/constants.d.ts +0 -1
  42. package/types/execution-context.d.ts +2 -3
  43. package/types/http/express-adapter.d.ts +1 -1
  44. package/types/http/http-adapter.d.ts +35 -8
  45. package/types/http/http-context.d.ts +4 -4
  46. package/types/http/{impl/http-handler.d.ts → http-handler.d.ts} +11 -9
  47. package/types/http/impl/http-incoming.host.d.ts +1 -2
  48. package/types/http/impl/http-outgoing.host.d.ts +1 -1
  49. package/types/http/impl/multipart-reader.d.ts +38 -20
  50. package/types/http/impl/node-incoming-message.host.d.ts +2 -6
  51. package/types/http/impl/node-outgoing-message.host.d.ts +4 -7
  52. package/types/http/interfaces/http-incoming.interface.d.ts +1 -2
  53. package/types/http/interfaces/http-outgoing.interface.d.ts +1 -1
  54. package/types/http/interfaces/node-incoming-message.interface.d.ts +0 -2
  55. package/types/http/interfaces/node-outgoing-message.interface.d.ts +0 -2
  56. package/types/http/utils/body-reader.d.ts +2 -5
  57. package/types/http/utils/concat-readable.d.ts +0 -1
  58. package/types/http/utils/convert-to-raw-headers.d.ts +0 -1
  59. package/types/index.d.ts +4 -4
  60. package/types/platform-adapter.d.ts +1 -5
  61. package/cjs/helpers/logger.js +0 -35
  62. package/esm/helpers/logger.js +0 -31
  63. package/types/helpers/logger.d.ts +0 -14
@@ -1,9 +1,8 @@
1
- import { HttpOperationResponse } from '@opra/common';
2
- import { kAssetCache } from '../../constants.js';
3
- import type { HttpAdapter } from '../http-adapter.js';
4
- import { HttpContext } from '../http-context.js';
5
- import { HttpOutgoing } from '../interfaces/http-outgoing.interface.js';
6
- import { AssetCache } from './asset-cache.js';
1
+ import { HttpOperationResponse, OpraException, OpraHttpError } from '@opra/common';
2
+ import { kAssetCache } from '../constants';
3
+ import type { HttpAdapter } from './http-adapter';
4
+ import { HttpContext } from './http-context';
5
+ import { AssetCache } from './impl/asset-cache';
7
6
  /**
8
7
  * @namespace
9
8
  */
@@ -16,6 +15,7 @@ export declare namespace HttpHandler {
16
15
  contentType?: string;
17
16
  operationResponse?: HttpOperationResponse;
18
17
  body?: any;
18
+ projection?: string[] | '*';
19
19
  }
20
20
  }
21
21
  /**
@@ -24,6 +24,7 @@ export declare namespace HttpHandler {
24
24
  export declare class HttpHandler {
25
25
  readonly adapter: HttpAdapter;
26
26
  protected [kAssetCache]: AssetCache;
27
+ onError?: (context: HttpContext, error: OpraException) => void | Promise<void>;
27
28
  constructor(adapter: HttpAdapter);
28
29
  /**
29
30
  * Main http request handler
@@ -60,7 +61,9 @@ export declare class HttpHandler {
60
61
  * @param responseValue
61
62
  * @protected
62
63
  */
63
- protected _sendResponse(context: HttpContext, responseValue: any): Promise<void>;
64
+ sendResponse(context: HttpContext, responseValue?: any): Promise<void>;
65
+ protected _sendErrorResponse(context: HttpContext): Promise<void>;
66
+ sendDocumentSchema(context: HttpContext): Promise<void>;
64
67
  /**
65
68
  *
66
69
  * @param context
@@ -68,6 +71,5 @@ export declare class HttpHandler {
68
71
  * @protected
69
72
  */
70
73
  protected _determineResponseArgs(context: HttpContext, body: any): HttpHandler.ResponseArgs;
71
- sendDocumentSchema(context: HttpContext): Promise<void>;
72
- sendErrorResponse(response: HttpOutgoing, errors: any[]): Promise<void>;
74
+ protected _wrapExceptions(exceptions: any[]): OpraHttpError[];
73
75
  }
@@ -1,6 +1,5 @@
1
- /// <reference types="node" />
2
1
  import parseRange from 'range-parser';
3
- import type { HttpIncoming } from '../interfaces/http-incoming.interface';
2
+ import type { HttpIncoming } from '../interfaces/http-incoming.interface.js';
4
3
  import { BodyReader } from '../utils/body-reader.js';
5
4
  export interface HttpIncomingHost extends HttpIncoming {
6
5
  }
@@ -1,4 +1,4 @@
1
- import type { CookieOptions, HttpOutgoing } from '../interfaces/http-outgoing.interface';
1
+ import type { CookieOptions, HttpOutgoing } from '../interfaces/http-outgoing.interface.js';
2
2
  export interface HttpOutgoingHost extends HttpOutgoing {
3
3
  }
4
4
  export declare class HttpOutgoingHost {
@@ -1,28 +1,46 @@
1
- /// <reference types="node" />
1
+ import { HttpMediaType } from '@opra/common';
2
+ import busboy from 'busboy';
2
3
  import { EventEmitter } from 'events';
3
- import formidable from 'formidable';
4
- import type IncomingForm from 'formidable/Formidable.js';
5
- import type { NodeIncomingMessage } from '../interfaces/node-incoming-message.interface.js';
6
- export type MultipartFile = formidable.File;
7
- export type MultipartItem = {
8
- fieldName: string;
9
- type: 'field' | 'file';
10
- value?: string;
11
- file?: MultipartFile;
12
- };
4
+ import { StrictOmit } from 'ts-gems';
5
+ import { HttpContext } from '../http-context';
6
+ export declare namespace MultipartReader {
7
+ interface Options extends StrictOmit<busboy.BusboyConfig, 'headers'> {
8
+ tempDirectory?: string;
9
+ }
10
+ interface FieldInfo {
11
+ kind: 'field';
12
+ field: string;
13
+ value?: any;
14
+ mimeType?: string;
15
+ encoding?: string;
16
+ }
17
+ interface FileInfo {
18
+ kind: 'file';
19
+ field: string;
20
+ filename: string;
21
+ storedPath: string;
22
+ mimeType?: string;
23
+ encoding?: string;
24
+ }
25
+ type Item = FieldInfo | FileInfo;
26
+ }
13
27
  export declare class MultipartReader extends EventEmitter {
14
- protected _incoming: NodeIncomingMessage;
28
+ protected context: HttpContext;
29
+ protected mediaType?: HttpMediaType | undefined;
15
30
  protected _started: boolean;
31
+ protected _finished: boolean;
16
32
  protected _cancelled: boolean;
17
- protected _form: IncomingForm;
18
- protected _items: MultipartItem[];
19
- protected _stack: MultipartItem[];
20
- constructor(incoming: NodeIncomingMessage, options?: formidable.Options);
21
- get items(): MultipartItem[];
22
- getNext(): Promise<MultipartItem | undefined>;
23
- getAll(): Promise<MultipartItem[]>;
33
+ protected _form: busboy.Busboy;
34
+ protected _items: MultipartReader.Item[];
35
+ protected _stack: MultipartReader.Item[];
36
+ protected tempDirectory: string;
37
+ constructor(context: HttpContext, options?: MultipartReader.Options, mediaType?: HttpMediaType | undefined);
38
+ get items(): MultipartReader.Item[];
39
+ getNext(): Promise<MultipartReader.Item | undefined>;
40
+ getAll(): Promise<MultipartReader.Item[]>;
41
+ getAll_(): Promise<MultipartReader.Item[]>;
24
42
  cancel(): void;
25
43
  resume(): void;
26
44
  pause(): void;
27
- deleteTempFiles(): Promise<PromiseSettledResult<any>[]>;
45
+ purge(): Promise<PromiseSettledResult<any>[]>;
28
46
  }
@@ -1,11 +1,7 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
- /// <reference types="node" />
1
+ import { HTTPParserJS } from '@browsery/http-parser';
5
2
  import { IncomingHttpHeaders } from 'http';
6
3
  import { Duplex, Readable } from 'stream';
7
- import { HTTPParserJS } from '@browsery/http-parser';
8
- import type { NodeIncomingMessage } from '../interfaces/node-incoming-message.interface';
4
+ import type { NodeIncomingMessage } from '../interfaces/node-incoming-message.interface.js';
9
5
  export declare const CRLF: Buffer;
10
6
  export declare const kHeaders: unique symbol;
11
7
  export declare const kHeadersDistinct: unique symbol;
@@ -1,10 +1,7 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
1
  import { OutgoingHttpHeaders } from 'http';
5
2
  import { Duplex } from 'stream';
6
- import type { NodeIncomingMessage } from '../interfaces/node-incoming-message.interface';
7
- import type { NodeOutgoingMessage } from '../interfaces/node-outgoing-message.interface';
3
+ import type { NodeIncomingMessage } from '../interfaces/node-incoming-message.interface.js';
4
+ import type { NodeOutgoingMessage } from '../interfaces/node-outgoing-message.interface.js';
8
5
  export declare const kOutHeaders: unique symbol;
9
6
  export declare const kOutTrailers: unique symbol;
10
7
  declare global {
@@ -18,8 +15,8 @@ declare global {
18
15
  * @class NodeOutgoingMessageHost
19
16
  */
20
17
  export declare class NodeOutgoingMessageHost extends Duplex implements NodeOutgoingMessage {
21
- protected [kOutHeaders]: Record<string, any>;
22
- protected [kOutTrailers]: Record<string, any>;
18
+ protected [kOutHeaders]?: Record<string, any>;
19
+ protected [kOutTrailers]?: Record<string, any>;
23
20
  protected _headersSent: boolean;
24
21
  protected _httpVersionMajor?: number;
25
22
  protected _httpVersionMinor?: number;
@@ -1,7 +1,6 @@
1
- /// <reference types="node" />
2
1
  import { Options as RangeParserOptions, Ranges as RangeParserRanges, Result as RangeParserResult } from 'range-parser';
3
2
  import { BodyReader } from '../utils/body-reader.js';
4
- import type { HttpOutgoing } from './http-outgoing.interface';
3
+ import type { HttpOutgoing } from './http-outgoing.interface.js';
5
4
  import { NodeIncomingMessage } from './node-incoming-message.interface.js';
6
5
  /**
7
6
  * @interface HttpIncoming
@@ -1,5 +1,5 @@
1
1
  import { StrictOmit } from 'ts-gems';
2
- import type { HttpIncoming } from './http-incoming.interface';
2
+ import type { HttpIncoming } from './http-incoming.interface.js';
3
3
  import { NodeOutgoingMessage } from './node-outgoing-message.interface.js';
4
4
  export interface HttpOutgoing extends StrictOmit<NodeOutgoingMessage, 'req' | 'appendHeader' | 'setHeader'> {
5
5
  req: HttpIncoming;
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import http from 'http';
4
2
  import { Readable } from 'stream';
5
3
  /**
@@ -1,5 +1,3 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
1
  import http, { OutgoingHttpHeaders } from 'http';
4
2
  import { Writable } from 'stream';
5
3
  import { NodeIncomingMessage } from './node-incoming-message.interface.js';
@@ -1,8 +1,5 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- /// <reference types="node" />
4
- import { EventEmitter } from 'events';
5
1
  import nodeStream from 'node:stream';
2
+ import { EventEmitter } from 'events';
6
3
  import type { HttpIncoming } from '../interfaces/http-incoming.interface.js';
7
4
  /**
8
5
  *
@@ -21,7 +18,7 @@ type Callback = (...args: any[]) => any;
21
18
  export declare class BodyReader extends EventEmitter {
22
19
  readonly req: HttpIncoming;
23
20
  limit?: number;
24
- protected _stream: nodeStream.Readable;
21
+ protected _stream?: nodeStream.Readable;
25
22
  protected _buffer?: string | Buffer[];
26
23
  protected _completed?: boolean | undefined;
27
24
  protected _receivedSize: number;
@@ -1,3 +1,2 @@
1
- /// <reference types="node" />
2
1
  import { Readable } from 'stream';
3
2
  export declare function concatReadable(...streams: Readable[]): Readable;
@@ -1,3 +1,2 @@
1
- /// <reference types="node" />
2
1
  import { IncomingHttpHeaders } from 'http';
3
2
  export declare function convertToRawHeaders(src: IncomingHttpHeaders | Record<string, any>): string[];
package/types/index.d.ts CHANGED
@@ -6,20 +6,20 @@ import * as HttpOutgoingHost_ from './http/impl/http-outgoing.host.js';
6
6
  import * as NodeIncomingMessageHost_ from './http/impl/node-incoming-message.host.js';
7
7
  import * as NodeOutgoingMessageHost_ from './http/impl/node-outgoing-message.host.js';
8
8
  export * from './execution-context.js';
9
- export * from './platform-adapter.js';
10
- export * from './type-guards.js';
11
- export * from './helpers/logger.js';
12
9
  export * from './helpers/service-base.js';
13
10
  export * from './http/express-adapter.js';
14
11
  export * from './http/http-adapter.js';
15
12
  export * from './http/http-context.js';
13
+ export * from './http/http-handler.js';
14
+ export * from './http/impl/multipart-reader.js';
16
15
  export * from './http/interfaces/http-incoming.interface.js';
17
16
  export * from './http/interfaces/http-outgoing.interface.js';
18
17
  export * from './http/interfaces/node-incoming-message.interface.js';
19
18
  export * from './http/interfaces/node-outgoing-message.interface.js';
20
- export * from './http/impl/multipart-reader.js';
21
19
  export * from './http/utils/wrap-exception.js';
22
20
  export * from './interfaces/logger.interface.js';
21
+ export * from './platform-adapter.js';
22
+ export * from './type-guards.js';
23
23
  export declare namespace classes {
24
24
  export import HttpIncomingHost = HttpIncomingHost_.HttpIncomingHost;
25
25
  export import HttpOutgoingHost = HttpOutgoingHost_.HttpOutgoingHost;
@@ -1,17 +1,14 @@
1
1
  import './augmentation/18n.augmentation.js';
2
- import { AsyncEventEmitter } from 'strict-typed-events';
3
2
  import { ApiDocument, I18n, OpraSchema } from '@opra/common';
3
+ import { AsyncEventEmitter } from 'strict-typed-events';
4
4
  import { kAssetCache } from './constants.js';
5
- import { Logger } from './helpers/logger.js';
6
5
  import { AssetCache } from './http/impl/asset-cache.js';
7
- import type { ILogger } from './interfaces/logger.interface';
8
6
  /**
9
7
  * @namespace PlatformAdapter
10
8
  */
11
9
  export declare namespace PlatformAdapter {
12
10
  interface Options {
13
11
  i18n?: I18n;
14
- logger?: ILogger;
15
12
  }
16
13
  }
17
14
  /**
@@ -21,7 +18,6 @@ export declare abstract class PlatformAdapter extends AsyncEventEmitter {
21
18
  protected [kAssetCache]: AssetCache;
22
19
  readonly document: ApiDocument;
23
20
  abstract readonly protocol: OpraSchema.Protocol;
24
- logger: Logger;
25
21
  i18n: I18n;
26
22
  protected constructor(document: ApiDocument, options?: PlatformAdapter.Options);
27
23
  abstract close(): Promise<void>;
@@ -1,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Logger = void 0;
4
- class Logger {
5
- constructor(options = {}) {
6
- this._instance = options.instance || (!(process.env.NODE_ENV || '').includes('test') ? globalThis.console : {});
7
- }
8
- info(message, ...optionalParams) {
9
- (this._instance.info || this._instance.log)?.(message, ...optionalParams);
10
- }
11
- error(message, ...optionalParams) {
12
- if (this._instance.error) {
13
- this._instance.error(message, ...optionalParams);
14
- return;
15
- }
16
- this.info(message, ...optionalParams);
17
- }
18
- fatal(message, ...optionalParams) {
19
- if (this._instance.fatal) {
20
- this._instance.fatal(message, ...optionalParams);
21
- return;
22
- }
23
- this.error(message, ...optionalParams);
24
- }
25
- warn(message, ...optionalParams) {
26
- this._instance.warn?.(message, ...optionalParams);
27
- }
28
- debug(message, ...optionalParams) {
29
- this._instance.debug?.(message, ...optionalParams);
30
- }
31
- verbose(message, ...optionalParams) {
32
- this._instance.verbose?.(message, ...optionalParams);
33
- }
34
- }
35
- exports.Logger = Logger;
@@ -1,31 +0,0 @@
1
- export class Logger {
2
- constructor(options = {}) {
3
- this._instance = options.instance || (!(process.env.NODE_ENV || '').includes('test') ? globalThis.console : {});
4
- }
5
- info(message, ...optionalParams) {
6
- (this._instance.info || this._instance.log)?.(message, ...optionalParams);
7
- }
8
- error(message, ...optionalParams) {
9
- if (this._instance.error) {
10
- this._instance.error(message, ...optionalParams);
11
- return;
12
- }
13
- this.info(message, ...optionalParams);
14
- }
15
- fatal(message, ...optionalParams) {
16
- if (this._instance.fatal) {
17
- this._instance.fatal(message, ...optionalParams);
18
- return;
19
- }
20
- this.error(message, ...optionalParams);
21
- }
22
- warn(message, ...optionalParams) {
23
- this._instance.warn?.(message, ...optionalParams);
24
- }
25
- debug(message, ...optionalParams) {
26
- this._instance.debug?.(message, ...optionalParams);
27
- }
28
- verbose(message, ...optionalParams) {
29
- this._instance.verbose?.(message, ...optionalParams);
30
- }
31
- }
@@ -1,14 +0,0 @@
1
- import { ILogger } from '../interfaces/logger.interface.js';
2
- export interface LoggerOptions {
3
- instance?: ILogger;
4
- }
5
- export declare class Logger implements ILogger {
6
- protected _instance: ILogger;
7
- constructor(options?: LoggerOptions);
8
- info(message: any, ...optionalParams: any[]): void;
9
- error(message: any, ...optionalParams: any[]): void;
10
- fatal(message: any, ...optionalParams: any[]): void;
11
- warn(message: any, ...optionalParams: any[]): void;
12
- debug(message: any, ...optionalParams: any[]): void;
13
- verbose(message: any, ...optionalParams: any[]): void;
14
- }