@furystack/rest-service 13.0.0 → 14.0.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/CHANGELOG.md +17 -0
- package/README.md +47 -28
- package/esm/actions/get-current-user.spec.js +10 -6
- package/esm/actions/get-current-user.spec.js.map +1 -1
- package/esm/actions/is-authenticated.spec.js +10 -6
- package/esm/actions/is-authenticated.spec.js.map +1 -1
- package/esm/actions/logout-action.spec.js +11 -9
- package/esm/actions/logout-action.spec.js.map +1 -1
- package/esm/actions/logout.js +1 -1
- package/esm/actions/logout.js.map +1 -1
- package/esm/actions/password-login-action.js +1 -1
- package/esm/actions/password-login-action.js.map +1 -1
- package/esm/actions/password-login-action.spec.js +23 -28
- package/esm/actions/password-login-action.spec.js.map +1 -1
- package/esm/authenticate.js +1 -1
- package/esm/authenticate.js.map +1 -1
- package/esm/authenticate.spec.js +41 -33
- package/esm/authenticate.spec.js.map +1 -1
- package/esm/authorize.spec.js +34 -29
- package/esm/authorize.spec.js.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.d.ts +6 -11
- package/esm/endpoint-generators/create-delete-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.js +7 -11
- package/esm/endpoint-generators/create-delete-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-delete-endpoint.spec.js +14 -21
- package/esm/endpoint-generators/create-delete-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.d.ts +11 -11
- package/esm/endpoint-generators/create-get-collection-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.js +13 -12
- package/esm/endpoint-generators/create-get-collection-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js +43 -70
- package/esm/endpoint-generators/create-get-collection-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.d.ts +6 -11
- package/esm/endpoint-generators/create-get-entity-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.js +7 -11
- package/esm/endpoint-generators/create-get-entity-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js +16 -33
- package/esm/endpoint-generators/create-get-entity-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.d.ts +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.js +1 -1
- package/esm/endpoint-generators/create-get-openapi-document-action.js.map +1 -1
- package/esm/endpoint-generators/create-get-schema-action.d.ts +1 -1
- package/esm/endpoint-generators/create-get-schema-action.d.ts.map +1 -1
- package/esm/endpoint-generators/create-get-schema-action.js.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.d.ts +7 -13
- package/esm/endpoint-generators/create-patch-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.js +8 -13
- package/esm/endpoint-generators/create-patch-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-patch-endpoint.spec.js +10 -17
- package/esm/endpoint-generators/create-patch-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.d.ts +7 -13
- package/esm/endpoint-generators/create-post-endpoint.d.ts.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.js +8 -13
- package/esm/endpoint-generators/create-post-endpoint.js.map +1 -1
- package/esm/endpoint-generators/create-post-endpoint.spec.js +41 -14
- package/esm/endpoint-generators/create-post-endpoint.spec.js.map +1 -1
- package/esm/endpoint-generators/utils.d.ts +17 -2
- package/esm/endpoint-generators/utils.d.ts.map +1 -1
- package/esm/endpoint-generators/utils.js +33 -20
- package/esm/endpoint-generators/utils.js.map +1 -1
- package/esm/get-schema-from-api.d.ts +1 -1
- package/esm/get-schema-from-api.d.ts.map +1 -1
- package/esm/get-schema-from-api.spec.js.map +1 -1
- package/esm/helpers.d.ts +53 -87
- package/esm/helpers.d.ts.map +1 -1
- package/esm/helpers.js +91 -92
- package/esm/helpers.js.map +1 -1
- package/esm/helpers.spec.d.ts +2 -1
- package/esm/helpers.spec.d.ts.map +1 -1
- package/esm/helpers.spec.js +41 -38
- package/esm/helpers.spec.js.map +1 -1
- package/esm/http-authentication-settings.d.ts +35 -12
- package/esm/http-authentication-settings.d.ts.map +1 -1
- package/esm/http-authentication-settings.js +21 -29
- package/esm/http-authentication-settings.js.map +1 -1
- package/esm/http-server-pool.d.ts +64 -0
- package/esm/http-server-pool.d.ts.map +1 -0
- package/esm/http-server-pool.js +95 -0
- package/esm/http-server-pool.js.map +1 -0
- package/esm/http-server-pool.spec.d.ts +2 -0
- package/esm/http-server-pool.spec.d.ts.map +1 -0
- package/esm/http-server-pool.spec.js +193 -0
- package/esm/http-server-pool.spec.js.map +1 -0
- package/esm/http-user-context.d.ts +31 -44
- package/esm/http-user-context.d.ts.map +1 -1
- package/esm/http-user-context.js +76 -95
- package/esm/http-user-context.js.map +1 -1
- package/esm/http-user-context.spec.d.ts +8 -2
- package/esm/http-user-context.spec.d.ts.map +1 -1
- package/esm/http-user-context.spec.js +189 -337
- package/esm/http-user-context.spec.js.map +1 -1
- package/esm/index.d.ts +6 -4
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +6 -4
- package/esm/index.js.map +1 -1
- package/esm/login-response-strategy.d.ts +11 -10
- package/esm/login-response-strategy.d.ts.map +1 -1
- package/esm/login-response-strategy.js +7 -6
- package/esm/login-response-strategy.js.map +1 -1
- package/esm/login-response-strategy.spec.js +25 -29
- package/esm/login-response-strategy.spec.js.map +1 -1
- package/esm/proxy-manager.spec.js +152 -176
- package/esm/proxy-manager.spec.js.map +1 -1
- package/esm/proxy-runtime.d.ts +27 -0
- package/esm/proxy-runtime.d.ts.map +1 -0
- package/esm/proxy-runtime.js +45 -0
- package/esm/proxy-runtime.js.map +1 -0
- package/esm/proxy-runtime.spec.d.ts +2 -0
- package/esm/proxy-runtime.spec.d.ts.map +1 -0
- package/esm/proxy-runtime.spec.js +39 -0
- package/esm/proxy-runtime.spec.js.map +1 -0
- package/esm/rest-api-runtime.d.ts +76 -0
- package/esm/rest-api-runtime.d.ts.map +1 -0
- package/esm/rest-api-runtime.js +131 -0
- package/esm/rest-api-runtime.js.map +1 -0
- package/esm/rest-api-runtime.spec.d.ts +2 -0
- package/esm/rest-api-runtime.spec.d.ts.map +1 -0
- package/esm/rest-api-runtime.spec.js +58 -0
- package/esm/rest-api-runtime.spec.js.map +1 -0
- package/esm/rest-service.integration.spec.d.ts +1 -1
- package/esm/rest-service.integration.spec.d.ts.map +1 -1
- package/esm/rest-service.integration.spec.js +57 -80
- package/esm/rest-service.integration.spec.js.map +1 -1
- package/esm/rest.integration.test.js +26 -84
- package/esm/rest.integration.test.js.map +1 -1
- package/esm/server-telemetry.d.ts +57 -0
- package/esm/server-telemetry.d.ts.map +1 -0
- package/esm/server-telemetry.js +26 -0
- package/esm/server-telemetry.js.map +1 -0
- package/esm/server-telemetry.spec.d.ts +2 -0
- package/esm/server-telemetry.spec.d.ts.map +1 -0
- package/esm/server-telemetry.spec.js +52 -0
- package/esm/server-telemetry.spec.js.map +1 -0
- package/esm/static-server-manager.spec.js +60 -125
- package/esm/static-server-manager.spec.js.map +1 -1
- package/esm/static-site-runtime.d.ts +21 -0
- package/esm/static-site-runtime.d.ts.map +1 -0
- package/esm/static-site-runtime.js +48 -0
- package/esm/static-site-runtime.js.map +1 -0
- package/esm/static-site-runtime.spec.d.ts +2 -0
- package/esm/static-site-runtime.spec.d.ts.map +1 -0
- package/esm/static-site-runtime.spec.js +30 -0
- package/esm/static-site-runtime.spec.js.map +1 -0
- package/esm/user-store.d.ts +39 -0
- package/esm/user-store.d.ts.map +1 -0
- package/esm/user-store.js +62 -0
- package/esm/user-store.js.map +1 -0
- package/esm/user-store.spec.d.ts +2 -0
- package/esm/user-store.spec.d.ts.map +1 -0
- package/esm/user-store.spec.js +72 -0
- package/esm/user-store.spec.js.map +1 -0
- package/esm/validate.integration.spec.js +26 -28
- package/esm/validate.integration.spec.js.map +1 -1
- package/package.json +8 -8
- package/src/actions/get-current-user.spec.ts +11 -10
- package/src/actions/is-authenticated.spec.ts +12 -7
- package/src/actions/logout-action.spec.ts +12 -12
- package/src/actions/logout.ts +1 -1
- package/src/actions/password-login-action.spec.ts +30 -30
- package/src/actions/password-login-action.ts +1 -1
- package/src/authenticate.spec.ts +45 -51
- package/src/authenticate.ts +1 -1
- package/src/authorize.spec.ts +37 -39
- package/src/endpoint-generators/create-delete-endpoint.spec.ts +16 -24
- package/src/endpoint-generators/create-delete-endpoint.ts +11 -17
- package/src/endpoint-generators/create-get-collection-endpoint.spec.ts +45 -78
- package/src/endpoint-generators/create-get-collection-endpoint.ts +17 -19
- package/src/endpoint-generators/create-get-entity-endpoint.spec.ts +17 -33
- package/src/endpoint-generators/create-get-entity-endpoint.ts +11 -17
- package/src/endpoint-generators/create-get-openapi-document-action.ts +2 -2
- package/src/endpoint-generators/create-get-schema-action.ts +1 -1
- package/src/endpoint-generators/create-patch-endpoint.spec.ts +11 -18
- package/src/endpoint-generators/create-patch-endpoint.ts +12 -23
- package/src/endpoint-generators/create-post-endpoint.spec.ts +45 -14
- package/src/endpoint-generators/create-post-endpoint.ts +13 -23
- package/src/endpoint-generators/utils.ts +36 -28
- package/src/get-schema-from-api.spec.ts +1 -1
- package/src/get-schema-from-api.ts +1 -1
- package/src/helpers.spec.ts +51 -41
- package/src/helpers.ts +140 -109
- package/src/http-authentication-settings.ts +51 -25
- package/src/http-server-pool.spec.ts +220 -0
- package/src/http-server-pool.ts +169 -0
- package/src/http-user-context.spec.ts +203 -383
- package/src/http-user-context.ts +114 -91
- package/src/index.ts +6 -4
- package/src/login-response-strategy.spec.ts +32 -31
- package/src/login-response-strategy.ts +13 -12
- package/src/proxy-manager.spec.ts +152 -182
- package/src/proxy-runtime.spec.ts +50 -0
- package/src/proxy-runtime.ts +75 -0
- package/src/rest-api-runtime.spec.ts +77 -0
- package/src/rest-api-runtime.ts +204 -0
- package/src/rest-service.integration.spec.ts +65 -85
- package/src/rest.integration.test.ts +27 -87
- package/src/server-telemetry.spec.ts +56 -0
- package/src/server-telemetry.ts +51 -0
- package/src/static-server-manager.spec.ts +63 -151
- package/src/static-site-runtime.spec.ts +39 -0
- package/src/static-site-runtime.ts +75 -0
- package/src/user-store.spec.ts +86 -0
- package/src/user-store.ts +68 -0
- package/src/validate.integration.spec.ts +39 -35
- package/esm/api-manager.d.ts +0 -104
- package/esm/api-manager.d.ts.map +0 -1
- package/esm/api-manager.js +0 -179
- package/esm/api-manager.js.map +0 -1
- package/esm/proxy-manager.d.ts +0 -53
- package/esm/proxy-manager.d.ts.map +0 -1
- package/esm/proxy-manager.js +0 -84
- package/esm/proxy-manager.js.map +0 -1
- package/esm/server-manager.d.ts +0 -48
- package/esm/server-manager.d.ts.map +0 -1
- package/esm/server-manager.js +0 -100
- package/esm/server-manager.js.map +0 -1
- package/esm/static-server-manager.d.ts +0 -19
- package/esm/static-server-manager.d.ts.map +0 -1
- package/esm/static-server-manager.js +0 -65
- package/esm/static-server-manager.js.map +0 -1
- package/src/api-manager.ts +0 -304
- package/src/proxy-manager.ts +0 -102
- package/src/server-manager.ts +0 -135
- package/src/static-server-manager.ts +0 -90
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server-manager.d.ts","sourceRoot":"","sources":["../src/server-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,KAAK,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACtE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,MAAM,CAAA;AAGnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAEpC,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,SAAS;IACxB,GAAG,EAAE,eAAe,CAAA;IACpB,GAAG,E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orate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var ServerManager_1;
|
|
8
|
-
import { Injectable } from '@furystack/inject';
|
|
9
|
-
import { EventHub } from '@furystack/utils';
|
|
10
|
-
import { createServer } from 'http';
|
|
11
|
-
let ServerManager = class ServerManager extends EventHub {
|
|
12
|
-
static { ServerManager_1 = this; }
|
|
13
|
-
static DEFAULT_HOST = 'localhost';
|
|
14
|
-
servers = new Map();
|
|
15
|
-
openedSockets = new Set();
|
|
16
|
-
pendingCreates = new Map();
|
|
17
|
-
getHostUrl = (options) => `http://${options.hostName || ServerManager_1.DEFAULT_HOST}:${options.port}`;
|
|
18
|
-
onConnection = (socket) => {
|
|
19
|
-
this.openedSockets.add(socket);
|
|
20
|
-
socket.once('close', () => this.openedSockets.delete(socket));
|
|
21
|
-
};
|
|
22
|
-
async [Symbol.asyncDispose]() {
|
|
23
|
-
await Promise.allSettled([...this.pendingCreates.values()]);
|
|
24
|
-
this.openedSockets.forEach((s) => s.destroy());
|
|
25
|
-
await Promise.allSettled([...this.servers.entries()].map(([url, s]) => new Promise((resolve, reject) => {
|
|
26
|
-
s.server.close((err) => {
|
|
27
|
-
if (err) {
|
|
28
|
-
reject(err);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
this.emit('onServerClosed', { url });
|
|
32
|
-
resolve();
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
s.server.off('connection', this.onConnection);
|
|
36
|
-
})));
|
|
37
|
-
this.servers.clear();
|
|
38
|
-
super[Symbol.dispose]?.();
|
|
39
|
-
}
|
|
40
|
-
async getOrCreate(options) {
|
|
41
|
-
const url = this.getHostUrl(options);
|
|
42
|
-
const existing = this.servers.get(url);
|
|
43
|
-
if (existing) {
|
|
44
|
-
return existing;
|
|
45
|
-
}
|
|
46
|
-
const pending = this.pendingCreates.get(url);
|
|
47
|
-
if (pending) {
|
|
48
|
-
return pending;
|
|
49
|
-
}
|
|
50
|
-
const createPromise = this.createServer(url, options);
|
|
51
|
-
this.pendingCreates.set(url, createPromise);
|
|
52
|
-
return createPromise;
|
|
53
|
-
}
|
|
54
|
-
async createServer(url, options) {
|
|
55
|
-
const apis = [];
|
|
56
|
-
const server = createServer((req, res) => {
|
|
57
|
-
const apiMatch = apis.find((api) => api.shouldExec({ req, res }));
|
|
58
|
-
if (apiMatch) {
|
|
59
|
-
apiMatch.onRequest({ req, res }).catch((error) => {
|
|
60
|
-
this.emit('onRequestFailed', [error, req, res]);
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
res.destroy();
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
server.on('upgrade', (req, socket, head) => {
|
|
68
|
-
const apiMatch = apis.find((api) => api.shouldExec({ req, res: {} }));
|
|
69
|
-
if (apiMatch?.onUpgrade) {
|
|
70
|
-
apiMatch.onUpgrade({ req, socket, head }).catch((error) => {
|
|
71
|
-
this.emit('onRequestFailed', [error, req, {}]);
|
|
72
|
-
socket.destroy();
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
socket.destroy();
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
server.on('connection', this.onConnection);
|
|
80
|
-
try {
|
|
81
|
-
await new Promise((resolve, reject) => {
|
|
82
|
-
server.on('listening', () => resolve());
|
|
83
|
-
server.on('error', (err) => reject(err));
|
|
84
|
-
server.listen(options.port, options.hostName);
|
|
85
|
-
});
|
|
86
|
-
const record = { server, apis };
|
|
87
|
-
this.servers.set(url, record);
|
|
88
|
-
this.emit('onServerListening', { url, port: options.port, hostName: options.hostName });
|
|
89
|
-
return record;
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
this.pendingCreates.delete(url);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
ServerManager = ServerManager_1 = __decorate([
|
|
97
|
-
Injectable({ lifetime: 'singleton' })
|
|
98
|
-
], ServerManager);
|
|
99
|
-
export { ServerManager };
|
|
100
|
-
//# sourceMappingURL=server-manager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server-manager.js","sourceRoot":"","sources":["../src/server-manager.ts"],"names":[],"mappings":";;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,QAAQ,EAA6B,MAAM,kBAAkB,CAAA;AAEtE,OAAO,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAgC5B,IAAM,aAAa,GAAnB,MAAM,aACX,SAAQ,QAKN;;IAGK,MAAM,CAAC,YAAY,GAAG,WAAW,CAAA;IACjC,OAAO,GAAG,IAAI,GAAG,EAAwB,CAAA;IACxC,aAAa,GAAG,IAAI,GAAG,EAAU,CAAA;IACxB,cAAc,GAAG,IAAI,GAAG,EAAiC,CAAA;IAClE,UAAU,GAAG,CAAC,OAAsB,EAAE,EAAE,CAC9C,UAAU,OAAO,CAAC,QAAQ,IAAI,eAAa,CAAC,YAAY,IAAI,OAAO,CAAC,IAAI,EAAE,CAAA;IAEpE,YAAY,GAAG,CAAC,MAAc,EAAE,EAAE;QACxC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;QAC9B,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;IAC/D,CAAC,CAAA;IACM,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AO,MAAM,CAAA;QACf,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACjC,CAAC;IACH,CAAC;;AAlGU,aAAa;IADzB,UAAU,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;GACzB,aAAa,CAmGzB"}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, OutgoingHttpHeaders } from 'http';
|
|
2
|
-
export interface StaticServerOptions {
|
|
3
|
-
baseUrl: string;
|
|
4
|
-
path: string;
|
|
5
|
-
hostName?: string;
|
|
6
|
-
port: number;
|
|
7
|
-
fallback?: string;
|
|
8
|
-
headers?: OutgoingHttpHeaders;
|
|
9
|
-
}
|
|
10
|
-
export declare class StaticServerManager {
|
|
11
|
-
private readonly serverManager;
|
|
12
|
-
private sendFile;
|
|
13
|
-
shouldExec: (baseUrl: string) => ({ req }: {
|
|
14
|
-
req: Pick<IncomingMessage, "url" | "method">;
|
|
15
|
-
}) => boolean;
|
|
16
|
-
private onRequest;
|
|
17
|
-
addStaticSite(options: StaticServerOptions): Promise<void>;
|
|
18
|
-
}
|
|
19
|
-
//# sourceMappingURL=static-server-manager.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"static-server-manager.d.ts","sourceRoot":"","sources":["../src/static-server-manager.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,eAAe,EAAE,mBAAmB,EAAkB,MAAM,MAAM,CAAA;AAKhF,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,mBAAmB,CAAA;CAC9B;AAED,qBACa,mBAAmB;IAC9B,iBACyB,aAAa,CAAe;YAEvC,QAAQ;IAwBf,UAAU,GACd,SAAS,MAAM,MACf,SAAS;QAAE,GAAG,EAAE,IAAI,CAAC,eAAe,EAAE,KAAK,GAAG,QAAQ,CAAC,CAAA;KAAE,aACoD;IAEhH,OAAO,CAAC,SAAS,CA2BhB;IAEY,aAAa,CAAC,OAAO,EAAE,mBAAmB;CAQxD"}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { Injectable, Injected } from '@furystack/inject';
|
|
11
|
-
import { PathHelper } from '@furystack/utils';
|
|
12
|
-
import { createReadStream } from 'fs';
|
|
13
|
-
import { access, stat } from 'fs/promises';
|
|
14
|
-
import { join, normalize, sep } from 'path';
|
|
15
|
-
import { getMimeForFile } from './mime-types.js';
|
|
16
|
-
import { ServerManager } from './server-manager.js';
|
|
17
|
-
let StaticServerManager = class StaticServerManager {
|
|
18
|
-
async sendFile({ fullPath, headers, res, }) {
|
|
19
|
-
const { size } = await stat(fullPath);
|
|
20
|
-
await access(fullPath);
|
|
21
|
-
const head = {
|
|
22
|
-
...headers,
|
|
23
|
-
'Content-Length': size,
|
|
24
|
-
'Content-Type': getMimeForFile(fullPath),
|
|
25
|
-
};
|
|
26
|
-
Object.entries(head).map(([key, value]) => res.setHeader(key, value));
|
|
27
|
-
await new Promise((resolve, reject) => createReadStream(fullPath, { autoClose: true }).once('finish', resolve).once('error', reject).pipe(res));
|
|
28
|
-
}
|
|
29
|
-
shouldExec = (baseUrl) => ({ req }) => req.url && req.method?.toUpperCase() === 'GET' && PathHelper.matchesBaseUrl(req.url, baseUrl) ? true : false;
|
|
30
|
-
onRequest = ({ path, baseUrl, fallback, headers, }) => {
|
|
31
|
-
return async ({ req, res }) => {
|
|
32
|
-
const extractedPath = PathHelper.extractPath(req.url, baseUrl);
|
|
33
|
-
const filePath = (extractedPath || '/').replaceAll('/', sep);
|
|
34
|
-
const fullPath = normalize(join(path, filePath));
|
|
35
|
-
try {
|
|
36
|
-
await this.sendFile({ fullPath, res, headers });
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
if (fallback) {
|
|
40
|
-
await this.sendFile({ fullPath: normalize(join(path, fallback)), res, headers });
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
44
|
-
res.end('Not found');
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
async addStaticSite(options) {
|
|
50
|
-
const server = await this.serverManager.getOrCreate({ hostName: options.hostName, port: options.port });
|
|
51
|
-
server.apis.push({
|
|
52
|
-
shouldExec: this.shouldExec(options.baseUrl),
|
|
53
|
-
onRequest: this.onRequest({ ...options }),
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
__decorate([
|
|
58
|
-
Injected(ServerManager),
|
|
59
|
-
__metadata("design:type", ServerManager)
|
|
60
|
-
], StaticServerManager.prototype, "serverManager", void 0);
|
|
61
|
-
StaticServerManager = __decorate([
|
|
62
|
-
Injectable({ lifetime: 'singleton' })
|
|
63
|
-
], StaticServerManager);
|
|
64
|
-
export { StaticServerManager };
|
|
65
|
-
//# sourceMappingURL=static-server-manager.js.map
|
|
@@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Y,EAAE,CAAC,CAAA;oBACpD,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;gBACtB,CAAC;YACH,CAAC;QACH,CAAC,CAAA;IACH,CAAC,CAAA;IAEM,KAAK,CAAC,aAAa,CAAC,OAA4B;QACrD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,CAAA;QAEvG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC;YACf,UAAU,EAAE,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC;YAC5C,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC;SAC1C,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AApE0B;IADxB,QAAQ,CAAC,aAAa,CAAC;8BACgB,aAAa;0DAAA;AAF1C,mBAAmB;IAD/B,UAAU,CAAC,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;GACzB,mBAAmB,CAsE/B"}
|
package/src/api-manager.ts
DELETED
|
@@ -1,304 +0,0 @@
|
|
|
1
|
-
import type { User } from '@furystack/core'
|
|
2
|
-
import { IdentityContext } from '@furystack/core'
|
|
3
|
-
import type { Injector } from '@furystack/inject'
|
|
4
|
-
import { Injectable, Injected } from '@furystack/inject'
|
|
5
|
-
import type { Method, RestApi } from '@furystack/rest'
|
|
6
|
-
import { RequestError, deserializeQueryString } from '@furystack/rest'
|
|
7
|
-
import { PathHelper, usingAsync } from '@furystack/utils'
|
|
8
|
-
import type { MatchFunction } from 'path-to-regexp'
|
|
9
|
-
import { match } from 'path-to-regexp'
|
|
10
|
-
import { ErrorAction } from './actions/error-action.js'
|
|
11
|
-
import { NotFoundAction } from './actions/not-found-action.js'
|
|
12
|
-
import { addCorsHeaders } from './add-cors-header.js'
|
|
13
|
-
import { CreateGetSchemaAction } from './endpoint-generators/create-get-schema-action.js'
|
|
14
|
-
import { CreateGetOpenApiDocumentAction } from './endpoint-generators/create-get-openapi-document-action.js'
|
|
15
|
-
import { HttpUserContext } from './http-user-context.js'
|
|
16
|
-
import type { CorsOptions } from './models/cors-options.js'
|
|
17
|
-
import { readPostBody } from './read-post-body.js'
|
|
18
|
-
import type { RequestAction } from './request-action-implementation.js'
|
|
19
|
-
import type { OnRequest, ServerApi } from './server-manager.js'
|
|
20
|
-
import { ServerManager } from './server-manager.js'
|
|
21
|
-
import './server-response-extensions.js'
|
|
22
|
-
|
|
23
|
-
export type RestApiImplementation<T extends RestApi> = {
|
|
24
|
-
[TMethod in keyof T]: {
|
|
25
|
-
[TUrl in keyof T[TMethod]]: T[TMethod][TUrl] extends { result: unknown } ? RequestAction<T[TMethod][TUrl]> : never
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface ImplementApiOptions<T extends RestApi> {
|
|
30
|
-
/**
|
|
31
|
-
* The structure of the implemented API.
|
|
32
|
-
*/
|
|
33
|
-
api: RestApiImplementation<T>
|
|
34
|
-
/**
|
|
35
|
-
* The Injector instance to use for dependency injection in the API actions.
|
|
36
|
-
*/
|
|
37
|
-
injector: Injector
|
|
38
|
-
/**
|
|
39
|
-
* The host name for the API Server. If not provided, the default host (ServerManager.DEFAULT_HOST) will be used.
|
|
40
|
-
*/
|
|
41
|
-
hostName?: string
|
|
42
|
-
/**
|
|
43
|
-
* The root path for the API. This will be prepended to all API paths.
|
|
44
|
-
*/
|
|
45
|
-
root: string
|
|
46
|
-
/**
|
|
47
|
-
* The port on which the API server will listen.
|
|
48
|
-
*/
|
|
49
|
-
port: number
|
|
50
|
-
/**
|
|
51
|
-
* CORS options to configure Cross-Origin Resource Sharing for the API.
|
|
52
|
-
*/
|
|
53
|
-
cors?: CorsOptions
|
|
54
|
-
/**
|
|
55
|
-
* An optional function to deserialize query parameters from the URL.
|
|
56
|
-
* This function should take a query string (e.g., "?key=value") and return an object with the parsed parameters.
|
|
57
|
-
* If not provided, the default deserialization will be used.
|
|
58
|
-
*/
|
|
59
|
-
deserializeQueryParams?: (param: string) => any
|
|
60
|
-
/**
|
|
61
|
-
* Adds an additional 'GET /schema' endpoint that returns the schema definitions of the API.
|
|
62
|
-
* Also adds a 'GET /openapi.json' endpoint that returns the API schema in OpenAPI 3.1 format.
|
|
63
|
-
*/
|
|
64
|
-
enableGetSchema?: boolean
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Optional name for the API, used in the generated schema.
|
|
68
|
-
* This can be useful for documentation or identification purposes.
|
|
69
|
-
*/
|
|
70
|
-
name?: string
|
|
71
|
-
/**
|
|
72
|
-
* Optional description for the API, used in the generated schema.
|
|
73
|
-
* This can provide additional context or information about the API's purpose.
|
|
74
|
-
*/
|
|
75
|
-
description?: string
|
|
76
|
-
/**
|
|
77
|
-
* Optional version for the API, used in the generated schema.
|
|
78
|
-
* This can help in versioning the API and tracking changes over time.
|
|
79
|
-
*/
|
|
80
|
-
version?: string
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export type NewCompiledApiEntry = {
|
|
84
|
-
method: Method
|
|
85
|
-
fullPath: string
|
|
86
|
-
matcher: MatchFunction<Partial<Record<string, string | string[]>>>
|
|
87
|
-
action: RequestAction<any>
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export type NewCompiledApi = {
|
|
91
|
-
[K in Method]?: NewCompiledApiEntry[]
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export type OnRequestOptions = OnRequest & {
|
|
95
|
-
compiledApi: NewCompiledApi
|
|
96
|
-
hostName?: string
|
|
97
|
-
port: number
|
|
98
|
-
rootApiPath: string
|
|
99
|
-
injector: Injector
|
|
100
|
-
cors?: CorsOptions
|
|
101
|
-
supportedMethods: string[]
|
|
102
|
-
deserializeQueryParams?: (param: string) => Record<string, unknown>
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
@Injectable({ lifetime: 'singleton' })
|
|
106
|
-
export class ApiManager {
|
|
107
|
-
private getSuportedMethods(api: RestApiImplementation<RestApi>): Method[] {
|
|
108
|
-
return Object.keys(api) as Method[]
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
private compileApi<T extends RestApi>(api: RestApiImplementation<T>, root: string) {
|
|
112
|
-
const supportedMethods = this.getSuportedMethods(api)
|
|
113
|
-
|
|
114
|
-
const compiledApi: NewCompiledApi = {}
|
|
115
|
-
supportedMethods.forEach((method: Method) => {
|
|
116
|
-
compiledApi[method] = [
|
|
117
|
-
...Object.entries(api[method as keyof typeof api]).map(([path, action]) => {
|
|
118
|
-
const fullPath = `/${PathHelper.normalize(PathHelper.joinPaths(root, path))}`
|
|
119
|
-
const matcher = match(fullPath, { decode: decodeURIComponent })
|
|
120
|
-
return { method, fullPath, matcher, action: action as RequestAction<any> }
|
|
121
|
-
}),
|
|
122
|
-
]
|
|
123
|
-
})
|
|
124
|
-
return compiledApi
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
public async addApi<T extends RestApi>({
|
|
128
|
-
api,
|
|
129
|
-
hostName,
|
|
130
|
-
port,
|
|
131
|
-
root,
|
|
132
|
-
cors,
|
|
133
|
-
injector,
|
|
134
|
-
deserializeQueryParams,
|
|
135
|
-
enableGetSchema,
|
|
136
|
-
name,
|
|
137
|
-
description,
|
|
138
|
-
version,
|
|
139
|
-
}: ImplementApiOptions<T>) {
|
|
140
|
-
const extendedApi: typeof api = enableGetSchema
|
|
141
|
-
? {
|
|
142
|
-
...api,
|
|
143
|
-
GET: {
|
|
144
|
-
...api.GET,
|
|
145
|
-
'/schema': CreateGetSchemaAction(api, name, description, version),
|
|
146
|
-
'/openapi.json': CreateGetOpenApiDocumentAction(api, name, description, version),
|
|
147
|
-
},
|
|
148
|
-
}
|
|
149
|
-
: api
|
|
150
|
-
|
|
151
|
-
const supportedMethods = this.getSuportedMethods(extendedApi)
|
|
152
|
-
const rootApiPath = PathHelper.normalize(root)
|
|
153
|
-
const server = await this.serverManager.getOrCreate({ hostName, port })
|
|
154
|
-
const compiledApi = this.compileApi(extendedApi, root)
|
|
155
|
-
const serverApi = {
|
|
156
|
-
shouldExec: (msg) =>
|
|
157
|
-
this.shouldExecRequest({
|
|
158
|
-
...msg,
|
|
159
|
-
method: msg.req.method?.toUpperCase() as Method,
|
|
160
|
-
rootApiPath,
|
|
161
|
-
supportedMethods,
|
|
162
|
-
url: PathHelper.normalize(msg.req.url || ''),
|
|
163
|
-
}),
|
|
164
|
-
onRequest: (msg) =>
|
|
165
|
-
this.onMessage({
|
|
166
|
-
...msg,
|
|
167
|
-
compiledApi,
|
|
168
|
-
rootApiPath,
|
|
169
|
-
port,
|
|
170
|
-
supportedMethods,
|
|
171
|
-
cors,
|
|
172
|
-
injector,
|
|
173
|
-
hostName,
|
|
174
|
-
deserializeQueryParams,
|
|
175
|
-
}),
|
|
176
|
-
} satisfies ServerApi
|
|
177
|
-
server.apis.push(serverApi)
|
|
178
|
-
return serverApi
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
public shouldExecRequest(options: {
|
|
182
|
-
method?: Method
|
|
183
|
-
url?: string
|
|
184
|
-
rootApiPath: string
|
|
185
|
-
supportedMethods: Method[]
|
|
186
|
-
}): boolean {
|
|
187
|
-
return options.method &&
|
|
188
|
-
options.url &&
|
|
189
|
-
(options.supportedMethods.includes(options.method) || options.method === 'OPTIONS') &&
|
|
190
|
-
PathHelper.matchesBaseUrl(options.url, options.rootApiPath)
|
|
191
|
-
? true
|
|
192
|
-
: false
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
private getActionFromEndpoint(compiledEndpoint: NewCompiledApi, fullUrl: URL, method: Method) {
|
|
196
|
-
let resolvedParams: unknown
|
|
197
|
-
const action = compiledEndpoint[method]?.find((route) => {
|
|
198
|
-
const result = route.matcher(fullUrl.pathname)
|
|
199
|
-
if (result) {
|
|
200
|
-
resolvedParams = result.params
|
|
201
|
-
}
|
|
202
|
-
return result
|
|
203
|
-
})
|
|
204
|
-
return (
|
|
205
|
-
action && {
|
|
206
|
-
...action,
|
|
207
|
-
params: resolvedParams,
|
|
208
|
-
}
|
|
209
|
-
)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
private async executeAction({
|
|
213
|
-
injector,
|
|
214
|
-
req,
|
|
215
|
-
res,
|
|
216
|
-
fullUrl,
|
|
217
|
-
action,
|
|
218
|
-
deserializeQueryParams,
|
|
219
|
-
params,
|
|
220
|
-
}: OnRequestOptions & {
|
|
221
|
-
fullUrl: URL
|
|
222
|
-
action: RequestAction<{ body: object; result: object; query: object; url: object; headers: object }>
|
|
223
|
-
params: unknown
|
|
224
|
-
}) {
|
|
225
|
-
await usingAsync(injector.createChild({ owner: action }), async (i) => {
|
|
226
|
-
const httpUserContext = i.getInstance(HttpUserContext)
|
|
227
|
-
i.setExplicitInstance<IdentityContext>(
|
|
228
|
-
{
|
|
229
|
-
getCurrentUser: <TUser extends User>() => httpUserContext.getCurrentUser(req) as Promise<TUser>,
|
|
230
|
-
isAuthorized: (...roles) => httpUserContext.isAuthorized(req, ...roles),
|
|
231
|
-
isAuthenticated: () => httpUserContext.isAuthenticated(req),
|
|
232
|
-
},
|
|
233
|
-
IdentityContext,
|
|
234
|
-
)
|
|
235
|
-
try {
|
|
236
|
-
const actionResult = await action({
|
|
237
|
-
request: req,
|
|
238
|
-
response: res,
|
|
239
|
-
injector: i,
|
|
240
|
-
getBody: () => readPostBody<any>(req),
|
|
241
|
-
headers: req.headers,
|
|
242
|
-
getQuery: () =>
|
|
243
|
-
deserializeQueryParams ? deserializeQueryParams(fullUrl.search) : deserializeQueryString(fullUrl.search),
|
|
244
|
-
getUrlParams: () => {
|
|
245
|
-
return params as object
|
|
246
|
-
},
|
|
247
|
-
})
|
|
248
|
-
res.sendActionResult(actionResult)
|
|
249
|
-
} catch (error) {
|
|
250
|
-
const errorActionResult = await ErrorAction({
|
|
251
|
-
request: req,
|
|
252
|
-
response: res,
|
|
253
|
-
injector: i,
|
|
254
|
-
getBody: async () => error,
|
|
255
|
-
})
|
|
256
|
-
res.sendActionResult(errorActionResult)
|
|
257
|
-
}
|
|
258
|
-
return
|
|
259
|
-
})
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
private async onMessage(options: OnRequestOptions) {
|
|
263
|
-
const protocol = 'http://'
|
|
264
|
-
const host = `${options.hostName || ServerManager.DEFAULT_HOST}:${options.port}`
|
|
265
|
-
const fullUrl = new URL(PathHelper.joinUrl(`${protocol}${host}`, options.req.url as string))
|
|
266
|
-
|
|
267
|
-
if (options.cors) {
|
|
268
|
-
addCorsHeaders(options.cors, options.req, options.res)
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (options.req.method === 'OPTIONS') {
|
|
272
|
-
options.res.writeHead(200)
|
|
273
|
-
options.res.end()
|
|
274
|
-
return
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
let action: ReturnType<typeof this.getActionFromEndpoint>
|
|
278
|
-
try {
|
|
279
|
-
action = this.getActionFromEndpoint(options.compiledApi, fullUrl, options.req.method?.toUpperCase() as Method)
|
|
280
|
-
} catch (error) {
|
|
281
|
-
const responseError =
|
|
282
|
-
error instanceof URIError ? new RequestError('Failed to decode URL path parameter', 400) : (error as Error)
|
|
283
|
-
const errorActionResult = await ErrorAction({
|
|
284
|
-
request: options.req,
|
|
285
|
-
response: options.res,
|
|
286
|
-
injector: options.injector,
|
|
287
|
-
getBody: async () => responseError,
|
|
288
|
-
})
|
|
289
|
-
options.res.sendActionResult(errorActionResult)
|
|
290
|
-
return
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (action) {
|
|
294
|
-
await this.executeAction({ ...options, ...action, fullUrl })
|
|
295
|
-
} else {
|
|
296
|
-
options.res.sendActionResult(
|
|
297
|
-
await NotFoundAction({ injector: options.injector, request: options.req, response: options.res }),
|
|
298
|
-
)
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
@Injected(ServerManager)
|
|
303
|
-
declare private readonly serverManager: ServerManager
|
|
304
|
-
}
|
package/src/proxy-manager.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { Injectable, Injected } from '@furystack/inject'
|
|
2
|
-
import { EventHub, PathHelper, type ListenerErrorPayload } from '@furystack/utils'
|
|
3
|
-
import type { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'http'
|
|
4
|
-
import type { Duplex } from 'stream'
|
|
5
|
-
import { HttpProxyHandler } from './http-proxy-handler.js'
|
|
6
|
-
import { PathProcessor } from './path-processor.js'
|
|
7
|
-
import { ServerManager } from './server-manager.js'
|
|
8
|
-
import { WebSocketProxyHandler } from './websocket-proxy-handler.js'
|
|
9
|
-
|
|
10
|
-
export interface ProxyOptions {
|
|
11
|
-
sourceBaseUrl: string
|
|
12
|
-
targetBaseUrl: string
|
|
13
|
-
pathRewrite?: (sourcePath: string) => string
|
|
14
|
-
sourceHostName?: string
|
|
15
|
-
sourcePort: number
|
|
16
|
-
headers?: (originalHeaders: OutgoingHttpHeaders) => OutgoingHttpHeaders
|
|
17
|
-
cookies?: (originalCookies: string[]) => string[]
|
|
18
|
-
responseCookies?: (responseCookies: string[]) => string[]
|
|
19
|
-
timeout?: number
|
|
20
|
-
enableWebsockets?: boolean
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Manages HTTP and WebSocket proxy configurations and routing
|
|
25
|
-
*/
|
|
26
|
-
@Injectable({ lifetime: 'singleton' })
|
|
27
|
-
export class ProxyManager extends EventHub<{
|
|
28
|
-
onProxyFailed: { from: string; to: string; error: unknown }
|
|
29
|
-
onWebSocketProxyFailed: { from: string; to: string; error: unknown }
|
|
30
|
-
onListenerError: ListenerErrorPayload
|
|
31
|
-
}> {
|
|
32
|
-
@Injected(ServerManager)
|
|
33
|
-
declare private readonly serverManager: ServerManager
|
|
34
|
-
|
|
35
|
-
private readonly pathProcessor = new PathProcessor()
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Creates a function that determines if a request should be handled by this proxy
|
|
39
|
-
*/
|
|
40
|
-
public shouldExec =
|
|
41
|
-
(sourceBaseUrl: string) =>
|
|
42
|
-
({ req }: { req: Pick<IncomingMessage, 'url' | 'method'> }) =>
|
|
43
|
-
req.url ? PathHelper.matchesBaseUrl(req.url, sourceBaseUrl) : false
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Creates an HTTP request handler for the proxy
|
|
47
|
-
*/
|
|
48
|
-
private createRequestHandler(options: ProxyOptions) {
|
|
49
|
-
const handler = new HttpProxyHandler({
|
|
50
|
-
sourceBaseUrl: options.sourceBaseUrl,
|
|
51
|
-
targetBaseUrl: options.targetBaseUrl,
|
|
52
|
-
pathRewrite: options.pathRewrite,
|
|
53
|
-
headers: options.headers,
|
|
54
|
-
cookies: options.cookies,
|
|
55
|
-
responseCookies: options.responseCookies,
|
|
56
|
-
timeout: options.timeout,
|
|
57
|
-
onError: (error) => this.emit('onProxyFailed', error),
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
return async ({ req, res }: { req: IncomingMessage; res: ServerResponse }) => {
|
|
61
|
-
await handler.handle(req, res)
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Creates a WebSocket upgrade handler for the proxy
|
|
67
|
-
*/
|
|
68
|
-
private createUpgradeHandler(options: ProxyOptions) {
|
|
69
|
-
const handler = new WebSocketProxyHandler({
|
|
70
|
-
sourceBaseUrl: options.sourceBaseUrl,
|
|
71
|
-
targetBaseUrl: options.targetBaseUrl,
|
|
72
|
-
pathRewrite: options.pathRewrite,
|
|
73
|
-
headers: options.headers,
|
|
74
|
-
timeout: options.timeout,
|
|
75
|
-
onError: (error) => this.emit('onWebSocketProxyFailed', error),
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
return async ({ req, socket, head }: { req: IncomingMessage; socket: Duplex; head: Buffer }) => {
|
|
79
|
-
await handler.handle(req, socket, head)
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Adds a new proxy configuration
|
|
85
|
-
* @throws Error if targetBaseUrl is invalid or uses non-HTTP/HTTPS protocol
|
|
86
|
-
*/
|
|
87
|
-
public async addProxy(options: ProxyOptions): Promise<void> {
|
|
88
|
-
// Validate targetBaseUrl format
|
|
89
|
-
const url = this.pathProcessor.validateUrl(options.targetBaseUrl, 'targetBaseUrl')
|
|
90
|
-
this.pathProcessor.validateHttpProtocol(url)
|
|
91
|
-
|
|
92
|
-
const server = await this.serverManager.getOrCreate({ hostName: options.sourceHostName, port: options.sourcePort })
|
|
93
|
-
|
|
94
|
-
const api = {
|
|
95
|
-
shouldExec: this.shouldExec(options.sourceBaseUrl),
|
|
96
|
-
onRequest: this.createRequestHandler(options),
|
|
97
|
-
...(options.enableWebsockets ? { onUpgrade: this.createUpgradeHandler(options) } : {}),
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
server.apis.push(api)
|
|
101
|
-
}
|
|
102
|
-
}
|