@opra/core 0.25.5 → 0.26.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/cjs/augmentation/container.augmentation.js +2 -0
- package/cjs/http/adapters/express-adapter.host.js +34 -0
- package/cjs/http/{express-adapter.js → adapters/express-adapter.js} +1 -3
- package/cjs/http/{http-adapter.host.js → adapters/node-http-adapter.host.js} +30 -22
- package/cjs/http/adapters/node-http-adapter.js +14 -0
- package/cjs/http/helpers/json-body-loader.js +29 -0
- package/cjs/http/http-adapter-host.js +678 -0
- package/cjs/index.js +4 -3
- package/cjs/platform-adapter.host.js +74 -45
- package/cjs/{endpoint-context.js → request-context.js} +5 -5
- package/cjs/request.host.js +3 -0
- package/esm/augmentation/container.augmentation.js +1 -0
- package/esm/http/adapters/express-adapter.host.js +30 -0
- package/esm/http/{express-adapter.js → adapters/express-adapter.js} +1 -3
- package/esm/http/{http-adapter.host.js → adapters/node-http-adapter.host.js} +28 -20
- package/esm/http/adapters/node-http-adapter.js +11 -0
- package/esm/http/helpers/json-body-loader.js +24 -0
- package/esm/http/http-adapter-host.js +673 -0
- package/esm/index.js +4 -3
- package/esm/platform-adapter.host.js +75 -46
- package/esm/{endpoint-context.js → request-context.js} +4 -4
- package/esm/request.host.js +3 -0
- package/i18n/en/error.json +1 -2
- package/package.json +3 -3
- package/types/augmentation/collection.augmentation.d.ts +19 -16
- package/types/augmentation/container.augmentation.d.ts +13 -0
- package/types/augmentation/resource.augmentation.d.ts +2 -2
- package/types/augmentation/singleton.augmentation.d.ts +13 -9
- package/types/augmentation/storage.augmentation.d.ts +11 -14
- package/types/http/{express-adapter.d.ts → adapters/express-adapter.d.ts} +3 -3
- package/types/http/adapters/express-adapter.host.d.ts +12 -0
- package/types/http/{http-adapter.d.ts → adapters/node-http-adapter.d.ts} +5 -5
- package/types/http/adapters/node-http-adapter.host.d.ts +19 -0
- package/types/http/helpers/json-body-loader.d.ts +5 -0
- package/types/http/http-adapter-host.d.ts +34 -0
- package/types/index.d.ts +4 -3
- package/types/interfaces/request-handler.interface.d.ts +1 -1
- package/types/platform-adapter.d.ts +2 -2
- package/types/platform-adapter.host.d.ts +18 -14
- package/types/{endpoint-context.d.ts → request-context.d.ts} +3 -3
- package/types/request.d.ts +7 -2
- package/types/request.host.d.ts +5 -2
- package/cjs/http/express-adapter.host.js +0 -24
- package/cjs/http/http-adapter-base.js +0 -138
- package/cjs/http/http-adapter.js +0 -16
- package/cjs/http/request-handlers/entity-request-handler.js +0 -429
- package/cjs/http/request-handlers/parse-batch-request.js +0 -169
- package/cjs/http/request-handlers/request-handler-base.js +0 -37
- package/cjs/http/request-handlers/storage-request-handler.js +0 -139
- package/esm/http/express-adapter.host.js +0 -20
- package/esm/http/http-adapter-base.js +0 -134
- package/esm/http/http-adapter.js +0 -13
- package/esm/http/request-handlers/entity-request-handler.js +0 -424
- package/esm/http/request-handlers/parse-batch-request.js +0 -169
- package/esm/http/request-handlers/request-handler-base.js +0 -33
- package/esm/http/request-handlers/storage-request-handler.js +0 -134
- package/types/http/express-adapter.host.d.ts +0 -11
- package/types/http/http-adapter-base.d.ts +0 -23
- package/types/http/http-adapter.host.d.ts +0 -18
- package/types/http/request-handlers/entity-request-handler.d.ts +0 -24
- package/types/http/request-handlers/parse-batch-request.d.ts +0 -0
- package/types/http/request-handlers/request-handler-base.d.ts +0 -16
- package/types/http/request-handlers/storage-request-handler.d.ts +0 -23
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import fs from 'fs/promises';
|
|
2
|
-
import os from 'os';
|
|
3
|
-
import { BadRequestError, HttpStatusCodes, isReadable, OpraException, Storage, uid } from '@opra/common';
|
|
4
|
-
import { EndpointContext } from '../../endpoint-context.js';
|
|
5
|
-
import { RequestHost } from '../../request.host.js';
|
|
6
|
-
import { ResponseHost } from '../../response.host.js';
|
|
7
|
-
import { MultipartIterator } from '../helpers/multipart-helper.js';
|
|
8
|
-
import { RequestHandlerBase } from './request-handler-base.js';
|
|
9
|
-
/**
|
|
10
|
-
* @class StorageRequestHandler
|
|
11
|
-
*/
|
|
12
|
-
export class StorageRequestHandler extends RequestHandlerBase {
|
|
13
|
-
constructor(adapter, options) {
|
|
14
|
-
super(adapter);
|
|
15
|
-
this.adapter = adapter;
|
|
16
|
-
this._uploadDir = options?.uploadDir || os.tmpdir();
|
|
17
|
-
}
|
|
18
|
-
async processRequest(executionContext) {
|
|
19
|
-
const { incoming, outgoing } = executionContext.switchToHttp();
|
|
20
|
-
// Parse incoming message and create Request object
|
|
21
|
-
const request = await this.parseRequest(executionContext, incoming);
|
|
22
|
-
if (!request)
|
|
23
|
-
return;
|
|
24
|
-
const response = new ResponseHost({ http: outgoing });
|
|
25
|
-
const context = EndpointContext.from(executionContext, request, response);
|
|
26
|
-
await this.callEndpoint(context);
|
|
27
|
-
if (response.errors.length) {
|
|
28
|
-
context.errors.push(...response.errors);
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
await this.sendResponse(context);
|
|
32
|
-
}
|
|
33
|
-
async parseRequest(executionContext, incoming) {
|
|
34
|
-
const contentId = incoming.headers['content-id'];
|
|
35
|
-
const p = incoming.parsedUrl.path[0];
|
|
36
|
-
const resource = this.adapter.api.getResource(p.resource);
|
|
37
|
-
try {
|
|
38
|
-
if (!(resource instanceof Storage))
|
|
39
|
-
return;
|
|
40
|
-
switch (incoming.method) {
|
|
41
|
-
case 'GET': {
|
|
42
|
-
const endpointMeta = await this.assertEndpoint(resource, 'get');
|
|
43
|
-
return new RequestHost({
|
|
44
|
-
controller: endpointMeta.controller,
|
|
45
|
-
http: incoming,
|
|
46
|
-
resource,
|
|
47
|
-
endpoint: 'get',
|
|
48
|
-
contentId,
|
|
49
|
-
path: incoming.parsedUrl.path.slice(1).toString().substring(1),
|
|
50
|
-
params: this.parseParameters(incoming.parsedUrl, endpointMeta)
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
case 'DELETE': {
|
|
54
|
-
const endpointMeta = await this.assertEndpoint(resource, 'delete');
|
|
55
|
-
return new RequestHost({
|
|
56
|
-
controller: endpointMeta.controller,
|
|
57
|
-
http: incoming,
|
|
58
|
-
resource,
|
|
59
|
-
endpoint: 'delete',
|
|
60
|
-
contentId,
|
|
61
|
-
path: incoming.parsedUrl.path.slice(1).toString().substring(1),
|
|
62
|
-
params: this.parseParameters(incoming.parsedUrl, endpointMeta)
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
case 'POST': {
|
|
66
|
-
const endpointMeta = await this.assertEndpoint(resource, 'post');
|
|
67
|
-
await fs.mkdir(this._uploadDir, { recursive: true });
|
|
68
|
-
const multipartIterator = new MultipartIterator(incoming, {
|
|
69
|
-
...endpointMeta,
|
|
70
|
-
filename: () => this.adapter.serviceName + '_p' + process.pid +
|
|
71
|
-
't' + String(Date.now()).substring(8) + 'r' + uid(12)
|
|
72
|
-
});
|
|
73
|
-
multipartIterator.pause();
|
|
74
|
-
// Add an hook to clean up files after request finished
|
|
75
|
-
executionContext.on('finish', async () => {
|
|
76
|
-
multipartIterator.cancel();
|
|
77
|
-
await multipartIterator.deleteFiles().catch(() => void 0);
|
|
78
|
-
});
|
|
79
|
-
return new RequestHost({
|
|
80
|
-
controller: endpointMeta.controller,
|
|
81
|
-
http: incoming,
|
|
82
|
-
resource,
|
|
83
|
-
endpoint: 'post',
|
|
84
|
-
contentId,
|
|
85
|
-
parts: multipartIterator,
|
|
86
|
-
path: incoming.parsedUrl.path.slice(1).toString().substring(1),
|
|
87
|
-
params: this.parseParameters(incoming.parsedUrl, endpointMeta)
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
catch (e) {
|
|
93
|
-
if (e instanceof OpraException)
|
|
94
|
-
throw e;
|
|
95
|
-
throw new BadRequestError(e);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
async callEndpoint(context) {
|
|
99
|
-
const request = context.request;
|
|
100
|
-
const { response } = context;
|
|
101
|
-
// Call endpoint handler method
|
|
102
|
-
let value;
|
|
103
|
-
try {
|
|
104
|
-
value = await request.controller[request.endpoint].call(request.controller, context);
|
|
105
|
-
if (response.value == null)
|
|
106
|
-
response.value = value;
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
response.errors.push(error);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
async sendResponse(context) {
|
|
113
|
-
const { response } = context;
|
|
114
|
-
const outgoing = response.switchToHttp();
|
|
115
|
-
outgoing.statusCode = outgoing.statusCode || HttpStatusCodes.OK;
|
|
116
|
-
if (response.value != null) {
|
|
117
|
-
if (typeof response.value === 'string') {
|
|
118
|
-
if (!outgoing.hasHeader('content-type'))
|
|
119
|
-
outgoing.setHeader('content-type', 'text/plain');
|
|
120
|
-
outgoing.send(response.value);
|
|
121
|
-
}
|
|
122
|
-
else if (Buffer.isBuffer(response.value) || isReadable(response.value)) {
|
|
123
|
-
if (!outgoing.hasHeader('content-type'))
|
|
124
|
-
outgoing.setHeader('content-type', 'application/octet-stream');
|
|
125
|
-
outgoing.send(response.value);
|
|
126
|
-
}
|
|
127
|
-
else {
|
|
128
|
-
outgoing.setHeader('content-type', 'application/json; charset=utf-8');
|
|
129
|
-
outgoing.send(JSON.stringify(response.value));
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
outgoing.end();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import type { Application } from 'express';
|
|
2
|
-
import { ApiDocument } from '@opra/common';
|
|
3
|
-
import type { ExpressAdapter } from './express-adapter.js';
|
|
4
|
-
import { HttpAdapterBase } from './http-adapter-base.js';
|
|
5
|
-
export declare class ExpressAdapterHost extends HttpAdapterBase implements ExpressAdapter {
|
|
6
|
-
_platform: string;
|
|
7
|
-
_options: ExpressAdapter.Options;
|
|
8
|
-
_app: Application;
|
|
9
|
-
constructor(app: Application, api: ApiDocument, options?: ExpressAdapter.Options);
|
|
10
|
-
get app(): Application;
|
|
11
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { ExecutionContext } from '../execution-context.js';
|
|
2
|
-
import { RequestHandler } from '../interfaces/request-handler.interface.js';
|
|
3
|
-
import { PlatformAdapterHost } from '../platform-adapter.host.js';
|
|
4
|
-
import type { Protocol } from '../platform-adapter.js';
|
|
5
|
-
import { HttpServerRequest } from './http-server-request.js';
|
|
6
|
-
import { HttpServerResponse } from './http-server-response.js';
|
|
7
|
-
/**
|
|
8
|
-
*
|
|
9
|
-
* @class HttpAdapterBase
|
|
10
|
-
*/
|
|
11
|
-
export declare abstract class HttpAdapterBase extends PlatformAdapterHost {
|
|
12
|
-
_protocol: Protocol;
|
|
13
|
-
_requestHandlers: RequestHandler[];
|
|
14
|
-
/**
|
|
15
|
-
* Main http request handler
|
|
16
|
-
* @param incoming
|
|
17
|
-
* @param outgoing
|
|
18
|
-
* @protected
|
|
19
|
-
*/
|
|
20
|
-
protected handleIncoming(incoming: HttpServerRequest, outgoing: HttpServerResponse): Promise<void>;
|
|
21
|
-
processRequest(context: ExecutionContext): Promise<void>;
|
|
22
|
-
protected handleError(context: ExecutionContext): Promise<void>;
|
|
23
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import http from 'http';
|
|
3
|
-
import { ApiDocument, OpraURLPath } from '@opra/common';
|
|
4
|
-
import type { HttpAdapter } from './http-adapter.js';
|
|
5
|
-
import { HttpAdapterBase } from './http-adapter-base.js';
|
|
6
|
-
/**
|
|
7
|
-
* @class HttpAdapterHost
|
|
8
|
-
*/
|
|
9
|
-
export declare class HttpAdapterHost extends HttpAdapterBase implements HttpAdapter {
|
|
10
|
-
_basePath: OpraURLPath;
|
|
11
|
-
_options: HttpAdapter.Options;
|
|
12
|
-
_server: http.Server;
|
|
13
|
-
constructor(api: ApiDocument, options?: HttpAdapter.Options);
|
|
14
|
-
get basePath(): OpraURLPath;
|
|
15
|
-
get server(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
16
|
-
protected _serverListener(incomingMessage: http.IncomingMessage, serverResponse: http.ServerResponse): void;
|
|
17
|
-
close(): Promise<void>;
|
|
18
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Collection, OpraSchema, Singleton } from '@opra/common';
|
|
2
|
-
import { EndpointContext } from '../../endpoint-context.js';
|
|
3
|
-
import { ExecutionContext } from '../../execution-context.js';
|
|
4
|
-
import { Request } from '../../request.js';
|
|
5
|
-
import type { HttpAdapterBase } from '../http-adapter-base.js';
|
|
6
|
-
import { HttpServerRequest } from '../http-server-request.js';
|
|
7
|
-
import { RequestHandlerBase } from './request-handler-base.js';
|
|
8
|
-
type BodyLoaderFunction = (incoming: HttpServerRequest) => Promise<any>;
|
|
9
|
-
/**
|
|
10
|
-
* @class EntityRequestHandler
|
|
11
|
-
*/
|
|
12
|
-
export declare class EntityRequestHandler extends RequestHandlerBase {
|
|
13
|
-
readonly adapter: HttpAdapterBase;
|
|
14
|
-
readonly bodyLoaders: WeakMap<Object, BodyLoaderFunction>;
|
|
15
|
-
constructor(adapter: HttpAdapterBase);
|
|
16
|
-
processRequest(executionContext: ExecutionContext): Promise<void>;
|
|
17
|
-
parseRequest(incoming: HttpServerRequest): Promise<Request | undefined>;
|
|
18
|
-
callEndpoint(context: EndpointContext): Promise<void>;
|
|
19
|
-
sendResponse(context: EndpointContext): Promise<void>;
|
|
20
|
-
parseCollectionRequest(resource: Collection, incoming: HttpServerRequest): Promise<Request>;
|
|
21
|
-
parseSingletonRequest(resource: Singleton, incoming: HttpServerRequest): Promise<Request>;
|
|
22
|
-
getBodyLoader(endpoint: OpraSchema.Collection.CreateEndpoint | OpraSchema.Collection.UpdateEndpoint | OpraSchema.Collection.UpdateManyEndpoint): BodyLoaderFunction;
|
|
23
|
-
}
|
|
24
|
-
export {};
|
|
File without changes
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { OpraSchema, OpraURL, Resource } from '@opra/common';
|
|
2
|
-
import type { ExecutionContext } from '../../execution-context.js';
|
|
3
|
-
import type { RequestHandler } from '../../interfaces/request-handler.interface.js';
|
|
4
|
-
import type { HttpAdapterBase } from '../http-adapter-base.js';
|
|
5
|
-
/**
|
|
6
|
-
* @class RequestHandlerBase
|
|
7
|
-
*/
|
|
8
|
-
export declare abstract class RequestHandlerBase implements RequestHandler {
|
|
9
|
-
readonly adapter: HttpAdapterBase;
|
|
10
|
-
protected constructor(adapter: HttpAdapterBase);
|
|
11
|
-
abstract processRequest(executionContext: ExecutionContext): Promise<void>;
|
|
12
|
-
assertEndpoint<T extends OpraSchema.Endpoint = OpraSchema.Endpoint>(resource: Resource, endpoint: string): Promise<T & {
|
|
13
|
-
controller: any;
|
|
14
|
-
}>;
|
|
15
|
-
protected parseParameters(parsedUrl: OpraURL, metadata: OpraSchema.Endpoint): any;
|
|
16
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { EndpointContext } from '../../endpoint-context.js';
|
|
2
|
-
import type { ExecutionContext } from '../../execution-context.js';
|
|
3
|
-
import { Request } from '../../request.js';
|
|
4
|
-
import type { HttpAdapterBase } from '../http-adapter-base.js';
|
|
5
|
-
import { HttpServerRequest } from '../http-server-request.js';
|
|
6
|
-
import { RequestHandlerBase } from './request-handler-base.js';
|
|
7
|
-
export declare namespace StorageRequestHandler {
|
|
8
|
-
interface Options {
|
|
9
|
-
uploadDir?: string;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* @class StorageRequestHandler
|
|
14
|
-
*/
|
|
15
|
-
export declare class StorageRequestHandler extends RequestHandlerBase {
|
|
16
|
-
readonly adapter: HttpAdapterBase;
|
|
17
|
-
_uploadDir: string;
|
|
18
|
-
constructor(adapter: HttpAdapterBase, options?: StorageRequestHandler.Options);
|
|
19
|
-
processRequest(executionContext: ExecutionContext): Promise<void>;
|
|
20
|
-
parseRequest(executionContext: ExecutionContext, incoming: HttpServerRequest): Promise<Request | undefined>;
|
|
21
|
-
callEndpoint(context: EndpointContext): Promise<void>;
|
|
22
|
-
sendResponse(context: EndpointContext): Promise<void>;
|
|
23
|
-
}
|