@xyo-network/bridge-http-express 6.0.3 → 6.1.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/README.md +3 -1
- package/package.json +21 -27
- package/dist/browser/HttpBridge.d.ts +0 -46
- package/dist/browser/HttpBridge.d.ts.map +0 -1
- package/dist/browser/index.d.ts +0 -2
- package/dist/browser/index.d.ts.map +0 -1
- package/dist/browser/index.mjs +0 -193
- package/dist/browser/index.mjs.map +0 -7
- package/dist/neutral/HttpBridge.d.ts +0 -46
- package/dist/neutral/HttpBridge.d.ts.map +0 -1
- package/dist/neutral/index.d.ts +0 -2
- package/dist/neutral/index.d.ts.map +0 -1
- package/dist/neutral/index.mjs +0 -193
- package/dist/neutral/index.mjs.map +0 -7
package/README.md
CHANGED
|
@@ -38,10 +38,12 @@ bun add {{name}}
|
|
|
38
38
|
|
|
39
39
|
See the [LICENSE](LICENSE) file for license rights and limitations (LGPL-3.0-only).
|
|
40
40
|
|
|
41
|
+
## Credits
|
|
41
42
|
|
|
43
|
+
[Made with 🔥 and ❄️ by XYO Foundation](https://xyo.network)
|
|
42
44
|
|
|
43
|
-
[logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
|
|
44
45
|
[npm-badge]: https://img.shields.io/npm/v/@xyo-network/bridge-http-express.svg
|
|
45
46
|
[npm-link]: https://www.npmjs.com/package/@xyo-network/bridge-http-express
|
|
46
47
|
[license-badge]: https://img.shields.io/npm/l/@xyo-network/bridge-http-express.svg
|
|
47
48
|
[license-link]: https://github.com/xylabs/sdk-js/blob/main/LICENSE
|
|
49
|
+
[logo]: https://cdn.xy.company/img/brand/XYO_full_colored.png
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xyo-network/bridge-http-express",
|
|
3
|
-
"version": "6.0
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"description": "Primary SDK for using XYO Protocol 2.0",
|
|
5
5
|
"homepage": "https://xyo.network",
|
|
6
6
|
"bugs": {
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"package.json"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@xyo-network/bridge-
|
|
37
|
-
"@xyo-network/bridge-
|
|
38
|
-
"@xyo-network/module-
|
|
39
|
-
"@xyo-network/module-
|
|
36
|
+
"@xyo-network/bridge-http": "~6.1.0",
|
|
37
|
+
"@xyo-network/bridge-model": "~6.1.0",
|
|
38
|
+
"@xyo-network/module-resolver": "~6.1.0",
|
|
39
|
+
"@xyo-network/module-model": "~6.1.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@bitauth/libauth": "~3.0.0",
|
|
@@ -47,20 +47,18 @@
|
|
|
47
47
|
"@scure/base": "~2.2.0",
|
|
48
48
|
"@scure/bip39": "~2.2.0",
|
|
49
49
|
"@types/express": "^5.0.6",
|
|
50
|
-
"@types/node": "^25.9.
|
|
51
|
-
"@xylabs/express": "^6.0
|
|
52
|
-
"@xylabs/geo": "^6.0
|
|
53
|
-
"@xylabs/sdk-js": "^6.0
|
|
54
|
-
"@xylabs/threads": "^6.0
|
|
55
|
-
"@xylabs/toolchain": "
|
|
56
|
-
"@xylabs/tsconfig": "
|
|
57
|
-
"@xylabs/vitest-extended": "
|
|
58
|
-
"@xyo-network/sdk-protocol-js": "
|
|
50
|
+
"@types/node": "^25.9.2",
|
|
51
|
+
"@xylabs/express": "^6.1.0",
|
|
52
|
+
"@xylabs/geo": "^6.1.0",
|
|
53
|
+
"@xylabs/sdk-js": "^6.1.0",
|
|
54
|
+
"@xylabs/threads": "^6.1.0",
|
|
55
|
+
"@xylabs/toolchain": "^8.1.20",
|
|
56
|
+
"@xylabs/tsconfig": "^8.1.20",
|
|
57
|
+
"@xylabs/vitest-extended": "^6.1.0",
|
|
58
|
+
"@xyo-network/sdk-protocol-js": "^6.1.0",
|
|
59
59
|
"ajv": "^8.20.0",
|
|
60
60
|
"async-mutex": "^0.5.0",
|
|
61
|
-
"bn.js": "^5.2.3",
|
|
62
61
|
"body-parser": "^2.2.2",
|
|
63
|
-
"buffer": "^6.0.3",
|
|
64
62
|
"connect": "^3.7.0",
|
|
65
63
|
"debug": "~4.4.3",
|
|
66
64
|
"eslint": "^10.4.1",
|
|
@@ -72,7 +70,6 @@
|
|
|
72
70
|
"idb": "^8.0.3",
|
|
73
71
|
"lru-cache": "^11.5.1",
|
|
74
72
|
"observable-fns": "~0.6.1",
|
|
75
|
-
"pako": "~2.1.0",
|
|
76
73
|
"rollbar": "^3.1.0",
|
|
77
74
|
"tslib": "^2.8.1",
|
|
78
75
|
"typescript": "~6.0.3",
|
|
@@ -82,12 +79,12 @@
|
|
|
82
79
|
"winston": "^3.19.0",
|
|
83
80
|
"winston-transport": "^4.9.0",
|
|
84
81
|
"zod": "^4.4.3",
|
|
85
|
-
"@xyo-network/archivist-model": "~6.0
|
|
86
|
-
"@xyo-network/bridge-abstract": "~6.0
|
|
87
|
-
"@xyo-network/bridge-pub-sub": "~6.0
|
|
88
|
-
"@xyo-network/
|
|
89
|
-
"@xyo-network/
|
|
90
|
-
"@xyo-network/node-memory": "~6.0
|
|
82
|
+
"@xyo-network/archivist-model": "~6.1.0",
|
|
83
|
+
"@xyo-network/bridge-abstract": "~6.1.0",
|
|
84
|
+
"@xyo-network/bridge-pub-sub": "~6.1.0",
|
|
85
|
+
"@xyo-network/node-model": "~6.1.0",
|
|
86
|
+
"@xyo-network/diviner-model": "~6.1.0",
|
|
87
|
+
"@xyo-network/node-memory": "~6.1.0"
|
|
91
88
|
},
|
|
92
89
|
"peerDependencies": {
|
|
93
90
|
"@bitauth/libauth": "~3.0",
|
|
@@ -101,12 +98,10 @@
|
|
|
101
98
|
"@xylabs/geo": "^6.0",
|
|
102
99
|
"@xylabs/sdk-js": "^6.0",
|
|
103
100
|
"@xylabs/threads": "^6.0",
|
|
104
|
-
"@xyo-network/sdk-protocol-js": "
|
|
101
|
+
"@xyo-network/sdk-protocol-js": "^6.1",
|
|
105
102
|
"ajv": "^8.20",
|
|
106
103
|
"async-mutex": "^0.5",
|
|
107
|
-
"bn.js": "^5.2",
|
|
108
104
|
"body-parser": "^2.2",
|
|
109
|
-
"buffer": "^6.0",
|
|
110
105
|
"connect": "^3.7",
|
|
111
106
|
"debug": "~4.4",
|
|
112
107
|
"ethers": "^6.16",
|
|
@@ -116,7 +111,6 @@
|
|
|
116
111
|
"idb": "^8.0",
|
|
117
112
|
"lru-cache": "^11.3",
|
|
118
113
|
"observable-fns": "~0.6",
|
|
119
|
-
"pako": "~2.1",
|
|
120
114
|
"rollbar": "^3.1",
|
|
121
115
|
"webextension-polyfill": "^0.12",
|
|
122
116
|
"winston": "^3.19",
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import type { Server } from 'node:http';
|
|
2
|
-
import type { HttpBridgeConfig } from '@xyo-network/bridge-http';
|
|
3
|
-
import { HttpBridge } from '@xyo-network/bridge-http';
|
|
4
|
-
import type { BridgeExposeOptions, BridgeParams, BridgeUnexposeOptions } from '@xyo-network/bridge-model';
|
|
5
|
-
import type { AnyConfigSchema, ModuleInstance, ModuleQueryResult } from '@xyo-network/module-model';
|
|
6
|
-
import type { Payload, QueryBoundWitness, Schema, XyoAddress } from '@xyo-network/sdk-protocol-js';
|
|
7
|
-
import type { Application, Request, Response } from 'express';
|
|
8
|
-
/**
|
|
9
|
-
* The type of the path parameters for the address path.
|
|
10
|
-
*/
|
|
11
|
-
interface AddressPathParams {
|
|
12
|
-
address: XyoAddress;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* The type of the request body for the address path.
|
|
16
|
-
*/
|
|
17
|
-
type PostAddressRequestBody = [QueryBoundWitness, undefined | Payload[]];
|
|
18
|
-
export declare const HttpBridgeExpressConfigSchema: "network.xyo.bridge.http.express.config" & {
|
|
19
|
-
readonly __schema: true;
|
|
20
|
-
};
|
|
21
|
-
export type HttpBridgeExpressConfigSchema = typeof HttpBridgeExpressConfigSchema;
|
|
22
|
-
export interface HttpBridgeExpressConfig extends HttpBridgeConfig<{}, HttpBridgeExpressConfigSchema> {
|
|
23
|
-
}
|
|
24
|
-
export interface HttpBridgeExpressParams extends BridgeParams<AnyConfigSchema<HttpBridgeExpressConfig>> {
|
|
25
|
-
}
|
|
26
|
-
export declare class HttpBridgeExpress<TParams extends HttpBridgeExpressParams> extends HttpBridge<TParams> {
|
|
27
|
-
static readonly configSchemas: Schema[];
|
|
28
|
-
protected _app?: Application;
|
|
29
|
-
protected _exposedModules: WeakRef<ModuleInstance>[];
|
|
30
|
-
protected _server?: Server;
|
|
31
|
-
protected get app(): Application;
|
|
32
|
-
exposeChild(mod: ModuleInstance, options?: BridgeExposeOptions): Promise<ModuleInstance[]>;
|
|
33
|
-
exposeHandler(address: XyoAddress, options?: BridgeExposeOptions): Promise<ModuleInstance[]>;
|
|
34
|
-
exposedHandler(): XyoAddress[];
|
|
35
|
-
startHandler(): Promise<void>;
|
|
36
|
-
stopHandler(_timeout?: number): Promise<void>;
|
|
37
|
-
unexposeHandler(address: XyoAddress, options?: BridgeUnexposeOptions): Promise<ModuleInstance[]>;
|
|
38
|
-
protected callLocalModule(address: XyoAddress, query: QueryBoundWitness, payloads: Payload[]): Promise<ModuleQueryResult | null>;
|
|
39
|
-
protected handleGet(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response): Promise<void>;
|
|
40
|
-
protected handlePost(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response): Promise<Response<any, Record<string, any>> | undefined>;
|
|
41
|
-
protected initializeApp(): Application;
|
|
42
|
-
protected startHttpServer(): Promise<boolean>;
|
|
43
|
-
protected stopHttpServer(): Promise<boolean>;
|
|
44
|
-
}
|
|
45
|
-
export {};
|
|
46
|
-
//# sourceMappingURL=HttpBridge.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"HttpBridge.d.ts","sourceRoot":"","sources":["../../src/HttpBridge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,MAAM,CAAA;IAE1B,SAAS,KAAK,GAAG,gBAGhB;IAEK,WAAW,CAAC,GAAG,EAAE,cAAc,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAgBjF,aAAa,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;IAYlG,cAAc,IAAI,UAAU,EAAE;IAIxB,YAAY;IAKZ,WAAW,CAAC,QAAQ,CAAC,EAAE,MAAM;IAK7B,eAAe,CAAC,OAAO,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC;cAkB/F,eAAe,CAAC,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,iBAAiB,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC;cAKtH,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,sBAAsB,CAAC,EAAE,GAAG,EAAE,QAAQ;cAoBnG,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,sBAAsB,CAAC,EAAE,GAAG,EAAE,QAAQ;IA4BpH,SAAS,CAAC,aAAa,IAAI,WAAW;IAgCtC,SAAS,CAAC,eAAe,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ7C,SAAS,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;CAiB7C"}
|
package/dist/browser/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
package/dist/browser/index.mjs
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
-
var __reflectGet = Reflect.get;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
7
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
8
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
9
|
-
if (decorator = decorators[i])
|
|
10
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
11
|
-
if (kind && result) __defProp(target, key, result);
|
|
12
|
-
return result;
|
|
13
|
-
};
|
|
14
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
15
|
-
var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
|
|
16
|
-
|
|
17
|
-
// src/HttpBridge.ts
|
|
18
|
-
import {
|
|
19
|
-
asyncHandler,
|
|
20
|
-
customPoweredByHeader,
|
|
21
|
-
disableCaseSensitiveRouting,
|
|
22
|
-
disableExpressDefaultPoweredByHeader,
|
|
23
|
-
jsonBodyParser,
|
|
24
|
-
responseProfiler,
|
|
25
|
-
useRequestCounters
|
|
26
|
-
} from "@xylabs/express";
|
|
27
|
-
import {
|
|
28
|
-
assertEx,
|
|
29
|
-
exists,
|
|
30
|
-
toSafeJsonString
|
|
31
|
-
} from "@xylabs/sdk-js";
|
|
32
|
-
import { HttpBridge } from "@xyo-network/bridge-http";
|
|
33
|
-
import { creatableModule } from "@xyo-network/module-model";
|
|
34
|
-
import { resolveAddressToInstanceUp } from "@xyo-network/module-resolver";
|
|
35
|
-
import { asSchema, isQueryBoundWitness } from "@xyo-network/sdk-protocol-js";
|
|
36
|
-
import express from "express";
|
|
37
|
-
import { StatusCodes } from "http-status-codes";
|
|
38
|
-
var HttpBridgeExpressConfigSchema = asSchema("network.xyo.bridge.http.express.config", true);
|
|
39
|
-
var HttpBridgeExpress = class extends HttpBridge {
|
|
40
|
-
_app;
|
|
41
|
-
_exposedModules = [];
|
|
42
|
-
_server;
|
|
43
|
-
get app() {
|
|
44
|
-
this._app ??= this.initializeApp();
|
|
45
|
-
return assertEx(this._app, () => "App not initialized");
|
|
46
|
-
}
|
|
47
|
-
async exposeChild(mod, options) {
|
|
48
|
-
const { maxDepth = 5 } = options ?? {};
|
|
49
|
-
assertEx(this.config.host, () => "Not configured as a host");
|
|
50
|
-
this._exposedModules.push(new WeakRef(mod));
|
|
51
|
-
const children = maxDepth > 0 ? await mod.publicChildren?.() ?? [] : [];
|
|
52
|
-
this.logger?.log(`childrenToExpose [${mod.id}][${mod.address}]: ${toSafeJsonString(children.map((child) => child.id))}`);
|
|
53
|
-
const exposedChildren = (await Promise.all(children.map((child) => this.exposeChild(child, { maxDepth: maxDepth - 1, required: false })))).flat().filter(exists);
|
|
54
|
-
const allExposed = [mod, ...exposedChildren];
|
|
55
|
-
for (const exposedMod of allExposed) this.logger?.log(`exposed: ${exposedMod.address} [${mod.id}]`);
|
|
56
|
-
return allExposed;
|
|
57
|
-
}
|
|
58
|
-
async exposeHandler(address, options) {
|
|
59
|
-
const { required = true } = options ?? {};
|
|
60
|
-
const mod = await resolveAddressToInstanceUp(this, address);
|
|
61
|
-
if (required && !mod) {
|
|
62
|
-
throw new Error(`Unable to find required module: ${address}`);
|
|
63
|
-
}
|
|
64
|
-
if (mod) {
|
|
65
|
-
return this.exposeChild(mod, options);
|
|
66
|
-
}
|
|
67
|
-
return [];
|
|
68
|
-
}
|
|
69
|
-
exposedHandler() {
|
|
70
|
-
return this._exposedModules.map((ref) => ref.deref()?.address).filter(exists);
|
|
71
|
-
}
|
|
72
|
-
async startHandler() {
|
|
73
|
-
await super.startHandler();
|
|
74
|
-
await this.startHttpServer();
|
|
75
|
-
}
|
|
76
|
-
async stopHandler(_timeout) {
|
|
77
|
-
await super.stopHandler();
|
|
78
|
-
await this.stopHttpServer();
|
|
79
|
-
}
|
|
80
|
-
async unexposeHandler(address, options) {
|
|
81
|
-
const { maxDepth = 2, required = true } = options ?? {};
|
|
82
|
-
assertEx(this.config.host, () => "Not configured as a host");
|
|
83
|
-
const mod = this._exposedModules.find((ref) => ref.deref()?.address === address)?.deref();
|
|
84
|
-
assertEx(!required || mod, () => `Module not exposed: ${address}`);
|
|
85
|
-
this._exposedModules = this._exposedModules.filter((ref) => ref.deref()?.address !== address);
|
|
86
|
-
if (mod) {
|
|
87
|
-
const children = maxDepth > 0 ? await mod.publicChildren?.() ?? [] : [];
|
|
88
|
-
const exposedChildren = (await Promise.all(children.map((child) => this.unexposeHandler(child.address, { maxDepth: maxDepth - 1, required: false })))).flat().filter(exists);
|
|
89
|
-
return [mod, ...exposedChildren];
|
|
90
|
-
}
|
|
91
|
-
return [];
|
|
92
|
-
}
|
|
93
|
-
async callLocalModule(address, query, payloads) {
|
|
94
|
-
const mod = this._exposedModules.find((ref) => ref.deref()?.address === address)?.deref();
|
|
95
|
-
return mod ? await mod.query(query, payloads) : null;
|
|
96
|
-
}
|
|
97
|
-
async handleGet(req, res) {
|
|
98
|
-
const { address } = req.params;
|
|
99
|
-
try {
|
|
100
|
-
if (address == this.address) {
|
|
101
|
-
res.json(await this.stateQuery(this.account));
|
|
102
|
-
} else {
|
|
103
|
-
const mod = this._exposedModules.find((ref) => ref.deref()?.address === address)?.deref();
|
|
104
|
-
if (mod) {
|
|
105
|
-
res.json(await mod.stateQuery(this.account));
|
|
106
|
-
} else {
|
|
107
|
-
res.status(StatusCodes.NOT_FOUND).json({ error: "Module not found" });
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
} catch (ex) {
|
|
111
|
-
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: ex.message });
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async handlePost(req, res) {
|
|
115
|
-
const { address } = req.params;
|
|
116
|
-
const [bw, payloads = []] = Array.isArray(req.body) ? req.body : [];
|
|
117
|
-
const query = isQueryBoundWitness(bw) ? bw : void 0;
|
|
118
|
-
if (!query) {
|
|
119
|
-
res.status(StatusCodes.BAD_REQUEST).json({ error: "No query provided" });
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
if (address == this.address) {
|
|
124
|
-
const result = await this.query(query, payloads);
|
|
125
|
-
return res.json(result);
|
|
126
|
-
} else {
|
|
127
|
-
const result = await this.callLocalModule(address, query, payloads);
|
|
128
|
-
if (result === null) {
|
|
129
|
-
res.status(StatusCodes.NOT_FOUND).json({ error: "Module not found" });
|
|
130
|
-
} else {
|
|
131
|
-
res.json(result);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
} catch (ex) {
|
|
135
|
-
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: ex.message });
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
initializeApp() {
|
|
139
|
-
const app = express();
|
|
140
|
-
app.use(responseProfiler);
|
|
141
|
-
app.use(jsonBodyParser);
|
|
142
|
-
disableExpressDefaultPoweredByHeader(app);
|
|
143
|
-
app.use(customPoweredByHeader);
|
|
144
|
-
disableCaseSensitiveRouting(app);
|
|
145
|
-
useRequestCounters(app);
|
|
146
|
-
app.get("/", (_req, res) => res.redirect(StatusCodes.MOVED_TEMPORARILY, `/${this.address}`));
|
|
147
|
-
app.post("/", (_req, res) => res.redirect(StatusCodes.TEMPORARY_REDIRECT, `/${this.address}`));
|
|
148
|
-
app.get(
|
|
149
|
-
"/:address",
|
|
150
|
-
asyncHandler(async (req, res) => await this.handleGet(req, res))
|
|
151
|
-
);
|
|
152
|
-
app.post(
|
|
153
|
-
"/:address",
|
|
154
|
-
asyncHandler(async (req, res) => {
|
|
155
|
-
await this.handlePost(req, res);
|
|
156
|
-
})
|
|
157
|
-
);
|
|
158
|
-
return app;
|
|
159
|
-
}
|
|
160
|
-
startHttpServer() {
|
|
161
|
-
if (this.config.host) {
|
|
162
|
-
assertEx(!this._server, () => "Server already started");
|
|
163
|
-
this._server = this.app.listen(this.config.host?.port ?? 3030);
|
|
164
|
-
}
|
|
165
|
-
return Promise.resolve(true);
|
|
166
|
-
}
|
|
167
|
-
stopHttpServer() {
|
|
168
|
-
if (this.config.host) {
|
|
169
|
-
return new Promise((resolve, reject) => {
|
|
170
|
-
if (this._server) {
|
|
171
|
-
this._server.close((err) => {
|
|
172
|
-
if (err) {
|
|
173
|
-
reject(err);
|
|
174
|
-
} else {
|
|
175
|
-
this._server = void 0;
|
|
176
|
-
resolve(true);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return Promise.resolve(true);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
__publicField(HttpBridgeExpress, "configSchemas", [...__superGet(HttpBridgeExpress, HttpBridgeExpress, "configSchemas"), HttpBridgeExpressConfigSchema]);
|
|
186
|
-
HttpBridgeExpress = __decorateClass([
|
|
187
|
-
creatableModule()
|
|
188
|
-
], HttpBridgeExpress);
|
|
189
|
-
export {
|
|
190
|
-
HttpBridgeExpress,
|
|
191
|
-
HttpBridgeExpressConfigSchema
|
|
192
|
-
};
|
|
193
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/HttpBridge.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Server } from 'node:http'\n\nimport {\n asyncHandler,\n customPoweredByHeader,\n disableCaseSensitiveRouting,\n disableExpressDefaultPoweredByHeader,\n jsonBodyParser,\n responseProfiler,\n useRequestCounters,\n} from '@xylabs/express'\nimport {\n assertEx,\n exists, toSafeJsonString,\n} from '@xylabs/sdk-js'\nimport type { HttpBridgeConfig } from '@xyo-network/bridge-http'\nimport { HttpBridge } from '@xyo-network/bridge-http'\nimport type {\n BridgeExposeOptions, BridgeParams, BridgeUnexposeOptions,\n} from '@xyo-network/bridge-model'\n// import { standardResponses } from '@xyo-network/express-node-middleware'\nimport type {\n AnyConfigSchema, ModuleInstance, ModuleQueryResult,\n} from '@xyo-network/module-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { resolveAddressToInstanceUp } from '@xyo-network/module-resolver'\nimport type {\n Payload, QueryBoundWitness, Schema, XyoAddress,\n} from '@xyo-network/sdk-protocol-js'\nimport { asSchema, isQueryBoundWitness } from '@xyo-network/sdk-protocol-js'\nimport type {\n Application, Request, Response,\n} from 'express'\nimport express from 'express'\nimport { StatusCodes } from 'http-status-codes'\n\n/**\n * The type of the path parameters for the address path.\n */\ninterface AddressPathParams {\n address: XyoAddress\n}\n\n/**\n * The type of the request body for the address path.\n */\ntype PostAddressRequestBody = [QueryBoundWitness, undefined | Payload[]]\n\n// TODO: This does not match the error response shape of the legacy bridge BUT it its the\n// shape this bridge is currently returning. Massage this into the standard\n// error shape constructed via middleware.\n/* type ErrorResponseBody = {\n error: string\n} */\n\nexport const HttpBridgeExpressConfigSchema = asSchema('network.xyo.bridge.http.express.config', true)\nexport type HttpBridgeExpressConfigSchema = typeof HttpBridgeExpressConfigSchema\n\nexport interface HttpBridgeExpressConfig extends HttpBridgeConfig<{}, HttpBridgeExpressConfigSchema> {}\n\nexport interface HttpBridgeExpressParams extends BridgeParams<AnyConfigSchema<HttpBridgeExpressConfig>> {}\n\n@creatableModule()\nexport class HttpBridgeExpress<TParams extends HttpBridgeExpressParams> extends HttpBridge<TParams> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, HttpBridgeExpressConfigSchema]\n protected _app?: Application\n protected _exposedModules: WeakRef<ModuleInstance>[] = []\n protected _server?: Server\n\n protected get app() {\n this._app ??= this.initializeApp()\n return assertEx(this._app, () => 'App not initialized')\n }\n\n async exposeChild(mod: ModuleInstance, options?: BridgeExposeOptions): Promise<ModuleInstance[]> {\n const { maxDepth = 5 } = options ?? {}\n assertEx(this.config.host, () => 'Not configured as a host')\n this._exposedModules.push(new WeakRef(mod))\n const children = maxDepth > 0 ? ((await mod.publicChildren?.()) ?? []) : []\n this.logger?.log(`childrenToExpose [${mod.id}][${mod.address}]: ${toSafeJsonString(children.map(child => child.id))}`)\n const exposedChildren = (await Promise.all(children.map(child => this.exposeChild(child, { maxDepth: maxDepth - 1, required: false }))))\n .flat()\n .filter(exists)\n const allExposed = [mod, ...exposedChildren]\n\n for (const exposedMod of allExposed) this.logger?.log(`exposed: ${exposedMod.address} [${mod.id}]`)\n\n return allExposed\n }\n\n override async exposeHandler(address: XyoAddress, options?: BridgeExposeOptions): Promise<ModuleInstance[]> {\n const { required = true } = options ?? {}\n const mod = await resolveAddressToInstanceUp(this, address)\n if (required && !mod) {\n throw new Error(`Unable to find required module: ${address}`)\n }\n if (mod) {\n return this.exposeChild(mod, options)\n }\n return []\n }\n\n override exposedHandler(): XyoAddress[] {\n return this._exposedModules.map(ref => ref.deref()?.address).filter(exists)\n }\n\n override async startHandler() {\n await super.startHandler()\n await this.startHttpServer()\n }\n\n override async stopHandler(_timeout?: number) {\n await super.stopHandler()\n await this.stopHttpServer()\n }\n\n override async unexposeHandler(address: XyoAddress, options?: BridgeUnexposeOptions): Promise<ModuleInstance[]> {\n const { maxDepth = 2, required = true } = options ?? {}\n assertEx(this.config.host, () => 'Not configured as a host')\n const mod = this._exposedModules.find(ref => ref.deref()?.address === address)?.deref()\n assertEx(!required || mod, () => `Module not exposed: ${address}`)\n this._exposedModules = this._exposedModules.filter(ref => ref.deref()?.address !== address)\n if (mod) {\n const children = maxDepth > 0 ? ((await mod.publicChildren?.()) ?? []) : []\n const exposedChildren = (\n await Promise.all(children.map(child => this.unexposeHandler(child.address, { maxDepth: maxDepth - 1, required: false })))\n )\n .flat()\n .filter(exists)\n return [mod, ...exposedChildren]\n }\n return []\n }\n\n protected async callLocalModule(address: XyoAddress, query: QueryBoundWitness, payloads: Payload[]): Promise<ModuleQueryResult | null> {\n const mod = this._exposedModules.find(ref => ref.deref()?.address === address)?.deref()\n return mod ? await mod.query(query, payloads) : null\n }\n\n protected async handleGet(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response) {\n const { address } = req.params\n try {\n if (address == this.address) {\n res.json(await this.stateQuery(this.account))\n } else {\n const mod = this._exposedModules.find(ref => ref.deref()?.address === address)?.deref()\n // TODO: Use standard errors middleware\n if (mod) {\n res.json(await mod.stateQuery(this.account))\n } else {\n res.status(StatusCodes.NOT_FOUND).json({ error: 'Module not found' })\n }\n }\n } catch (ex) {\n // TODO: Sanitize message\n res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: (ex as Error).message })\n }\n }\n\n protected async handlePost(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response) {\n const { address } = req.params\n const [bw, payloads = []] = Array.isArray(req.body) ? req.body : []\n const query = isQueryBoundWitness(bw) ? bw : undefined\n if (!query) {\n // TODO: Use standard errors middleware\n res.status(StatusCodes.BAD_REQUEST).json({ error: 'No query provided' })\n return\n }\n try {\n if (address == this.address) {\n const result = await this.query(query, payloads)\n return res.json(result)\n } else {\n const result = await this.callLocalModule(address, query, payloads)\n // TODO: Use standard errors middleware\n if (result === null) {\n res.status(StatusCodes.NOT_FOUND).json({ error: 'Module not found' })\n } else {\n res.json(result)\n }\n }\n } catch (ex) {\n // TODO: Sanitize message\n res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: (ex as Error).message })\n }\n }\n\n protected initializeApp(): Application {\n // Create the express app\n const app = express()\n\n // Add middleware\n app.use(responseProfiler)\n app.use(jsonBodyParser)\n // removed for now since this causes a cycle\n // app.use(standardResponses)\n disableExpressDefaultPoweredByHeader(app)\n app.use(customPoweredByHeader)\n disableCaseSensitiveRouting(app)\n useRequestCounters(app)\n\n // Add routes\n // Redirect all requests to the root to this module's address\n app.get('/', (_req, res) => res.redirect(StatusCodes.MOVED_TEMPORARILY, `/${this.address}`))\n app.post('/', (_req, res) => res.redirect(StatusCodes.TEMPORARY_REDIRECT, `/${this.address}`))\n\n app.get<AddressPathParams, ModuleQueryResult>(\n '/:address',\n\n asyncHandler(async (req, res) => await this.handleGet(req, res)),\n )\n app.post<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>(\n '/:address',\n\n asyncHandler(async (req, res) => { await this.handlePost(req, res) }),\n )\n return app\n }\n\n protected startHttpServer(): Promise<boolean> {\n if (this.config.host) {\n assertEx(!this._server, () => 'Server already started')\n this._server = this.app.listen(this.config.host?.port ?? 3030)\n }\n return Promise.resolve(true)\n }\n\n protected stopHttpServer(): Promise<boolean> {\n if (this.config.host) {\n return new Promise((resolve, reject) => {\n if (this._server) {\n this._server.close((err) => {\n if (err) {\n reject(err)\n } else {\n this._server = undefined\n resolve(true)\n }\n })\n }\n })\n }\n return Promise.resolve(true)\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EAAQ;AAAA,OACH;AAEP,SAAS,kBAAkB;AAQ3B,SAAS,uBAAuB;AAChC,SAAS,kCAAkC;AAI3C,SAAS,UAAU,2BAA2B;AAI9C,OAAO,aAAa;AACpB,SAAS,mBAAmB;AAqBrB,IAAM,gCAAgC,SAAS,0CAA0C,IAAI;AAQ7F,IAAM,oBAAN,cAAyE,WAAoB;AAAA,EAExF;AAAA,EACA,kBAA6C,CAAC;AAAA,EAC9C;AAAA,EAEV,IAAc,MAAM;AAClB,SAAK,SAAS,KAAK,cAAc;AACjC,WAAO,SAAS,KAAK,MAAM,MAAM,qBAAqB;AAAA,EACxD;AAAA,EAEA,MAAM,YAAY,KAAqB,SAA0D;AAC/F,UAAM,EAAE,WAAW,EAAE,IAAI,WAAW,CAAC;AACrC,aAAS,KAAK,OAAO,MAAM,MAAM,0BAA0B;AAC3D,SAAK,gBAAgB,KAAK,IA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
import { HttpBridge } from '@xyo-network/bridge-http';
|
|
4
|
-
import type { BridgeExposeOptions, BridgeParams, BridgeUnexposeOptions } from '@xyo-network/bridge-model';
|
|
5
|
-
import type { AnyConfigSchema, ModuleInstance, ModuleQueryResult } from '@xyo-network/module-model';
|
|
6
|
-
import type { Payload, QueryBoundWitness, Schema, XyoAddress } from '@xyo-network/sdk-protocol-js';
|
|
7
|
-
import type { Application, Request, Response } from 'express';
|
|
8
|
-
/**
|
|
9
|
-
* The type of the path parameters for the address path.
|
|
10
|
-
*/
|
|
11
|
-
interface AddressPathParams {
|
|
12
|
-
address: XyoAddress;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* The type of the request body for the address path.
|
|
16
|
-
*/
|
|
17
|
-
type PostAddressRequestBody = [QueryBoundWitness, undefined | Payload[]];
|
|
18
|
-
export declare const HttpBridgeExpressConfigSchema: "network.xyo.bridge.http.express.config" & {
|
|
19
|
-
readonly __schema: true;
|
|
20
|
-
};
|
|
21
|
-
export type HttpBridgeExpressConfigSchema = typeof HttpBridgeExpressConfigSchema;
|
|
22
|
-
export interface HttpBridgeExpressConfig extends HttpBridgeConfig<{}, HttpBridgeExpressConfigSchema> {
|
|
23
|
-
}
|
|
24
|
-
export interface HttpBridgeExpressParams extends BridgeParams<AnyConfigSchema<HttpBridgeExpressConfig>> {
|
|
25
|
-
}
|
|
26
|
-
export declare class HttpBridgeExpress<TParams extends HttpBridgeExpressParams> extends HttpBridge<TParams> {
|
|
27
|
-
static readonly configSchemas: Schema[];
|
|
28
|
-
protected _app?: Application;
|
|
29
|
-
protected _exposedModules: WeakRef<ModuleInstance>[];
|
|
30
|
-
protected _server?: Server;
|
|
31
|
-
protected get app(): Application;
|
|
32
|
-
exposeChild(mod: ModuleInstance, options?: BridgeExposeOptions): Promise<ModuleInstance[]>;
|
|
33
|
-
exposeHandler(address: XyoAddress, options?: BridgeExposeOptions): Promise<ModuleInstance[]>;
|
|
34
|
-
exposedHandler(): XyoAddress[];
|
|
35
|
-
startHandler(): Promise<void>;
|
|
36
|
-
stopHandler(_timeout?: number): Promise<void>;
|
|
37
|
-
unexposeHandler(address: XyoAddress, options?: BridgeUnexposeOptions): Promise<ModuleInstance[]>;
|
|
38
|
-
protected callLocalModule(address: XyoAddress, query: QueryBoundWitness, payloads: Payload[]): Promise<ModuleQueryResult | null>;
|
|
39
|
-
protected handleGet(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response): Promise<void>;
|
|
40
|
-
protected handlePost(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response): Promise<Response<any, Record<string, any>> | undefined>;
|
|
41
|
-
protected initializeApp(): Application;
|
|
42
|
-
protected startHttpServer(): Promise<boolean>;
|
|
43
|
-
protected stopHttpServer(): Promise<boolean>;
|
|
44
|
-
}
|
|
45
|
-
export {};
|
|
46
|
-
//# sourceMappingURL=HttpBridge.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"HttpBridge.d.ts","sourceRoot":"","sources":["../../src/HttpBridge.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAevC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAA;AAChE,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AACrD,OAAO,KAAK,EACV,mBAAmB,EAAE,YAAY,EAAE,qBAAqB,EACzD,MAAM,2BAA2B,CAAA;AAElC,OAAO,KAAK,EACV,eAAe,EAAE,cAAc,EAAE,iBAAiB,EACnD,MAAM,2BAA2B,CAAA;AAGlC,OAAO,KAAK,EACV,OAAO,EAAE,iBAAiB,EAAE,MAAM,EAAE,UAAU,EAC/C,MAAM,8BAA8B,CAAA;AAErC,OAAO,KAAK,EACV,WAAW,EAAE,OAAO,EAAE,QAAQ,EAC/B,MAAM,SAAS,CAAA;AAIhB;;GAEG;AACH,UAAU,iBAAiB;IACzB,OAAO,EA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iB,GAAG,IAAI,CAAC;cAKtH,SAAS,CAAC,GAAG,EAAE,OAAO,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,sBAAsB,CAAC,EAAE,GAAG,EAAE,QAAQ;cAoBnG,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,sBAAsB,CAAC,EAAE,GAAG,EAAE,QAAQ;IA4BpH,SAAS,CAAC,aAAa,IAAI,WAAW;IAgCtC,SAAS,CAAC,eAAe,IAAI,OAAO,CAAC,OAAO,CAAC;IAQ7C,SAAS,CAAC,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;CAiB7C"}
|
package/dist/neutral/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
package/dist/neutral/index.mjs
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
-
var __reflectGet = Reflect.get;
|
|
5
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
-
var __decorateClass = (decorators, target, key, kind) => {
|
|
7
|
-
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
8
|
-
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
9
|
-
if (decorator = decorators[i])
|
|
10
|
-
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
11
|
-
if (kind && result) __defProp(target, key, result);
|
|
12
|
-
return result;
|
|
13
|
-
};
|
|
14
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
15
|
-
var __superGet = (cls, obj, key) => __reflectGet(__getProtoOf(cls), key, obj);
|
|
16
|
-
|
|
17
|
-
// src/HttpBridge.ts
|
|
18
|
-
import {
|
|
19
|
-
asyncHandler,
|
|
20
|
-
customPoweredByHeader,
|
|
21
|
-
disableCaseSensitiveRouting,
|
|
22
|
-
disableExpressDefaultPoweredByHeader,
|
|
23
|
-
jsonBodyParser,
|
|
24
|
-
responseProfiler,
|
|
25
|
-
useRequestCounters
|
|
26
|
-
} from "@xylabs/express";
|
|
27
|
-
import {
|
|
28
|
-
assertEx,
|
|
29
|
-
exists,
|
|
30
|
-
toSafeJsonString
|
|
31
|
-
} from "@xylabs/sdk-js";
|
|
32
|
-
import { HttpBridge } from "@xyo-network/bridge-http";
|
|
33
|
-
import { creatableModule } from "@xyo-network/module-model";
|
|
34
|
-
import { resolveAddressToInstanceUp } from "@xyo-network/module-resolver";
|
|
35
|
-
import { asSchema, isQueryBoundWitness } from "@xyo-network/sdk-protocol-js";
|
|
36
|
-
import express from "express";
|
|
37
|
-
import { StatusCodes } from "http-status-codes";
|
|
38
|
-
var HttpBridgeExpressConfigSchema = asSchema("network.xyo.bridge.http.express.config", true);
|
|
39
|
-
var HttpBridgeExpress = class extends HttpBridge {
|
|
40
|
-
_app;
|
|
41
|
-
_exposedModules = [];
|
|
42
|
-
_server;
|
|
43
|
-
get app() {
|
|
44
|
-
this._app ??= this.initializeApp();
|
|
45
|
-
return assertEx(this._app, () => "App not initialized");
|
|
46
|
-
}
|
|
47
|
-
async exposeChild(mod, options) {
|
|
48
|
-
const { maxDepth = 5 } = options ?? {};
|
|
49
|
-
assertEx(this.config.host, () => "Not configured as a host");
|
|
50
|
-
this._exposedModules.push(new WeakRef(mod));
|
|
51
|
-
const children = maxDepth > 0 ? await mod.publicChildren?.() ?? [] : [];
|
|
52
|
-
this.logger?.log(`childrenToExpose [${mod.id}][${mod.address}]: ${toSafeJsonString(children.map((child) => child.id))}`);
|
|
53
|
-
const exposedChildren = (await Promise.all(children.map((child) => this.exposeChild(child, { maxDepth: maxDepth - 1, required: false })))).flat().filter(exists);
|
|
54
|
-
const allExposed = [mod, ...exposedChildren];
|
|
55
|
-
for (const exposedMod of allExposed) this.logger?.log(`exposed: ${exposedMod.address} [${mod.id}]`);
|
|
56
|
-
return allExposed;
|
|
57
|
-
}
|
|
58
|
-
async exposeHandler(address, options) {
|
|
59
|
-
const { required = true } = options ?? {};
|
|
60
|
-
const mod = await resolveAddressToInstanceUp(this, address);
|
|
61
|
-
if (required && !mod) {
|
|
62
|
-
throw new Error(`Unable to find required module: ${address}`);
|
|
63
|
-
}
|
|
64
|
-
if (mod) {
|
|
65
|
-
return this.exposeChild(mod, options);
|
|
66
|
-
}
|
|
67
|
-
return [];
|
|
68
|
-
}
|
|
69
|
-
exposedHandler() {
|
|
70
|
-
return this._exposedModules.map((ref) => ref.deref()?.address).filter(exists);
|
|
71
|
-
}
|
|
72
|
-
async startHandler() {
|
|
73
|
-
await super.startHandler();
|
|
74
|
-
await this.startHttpServer();
|
|
75
|
-
}
|
|
76
|
-
async stopHandler(_timeout) {
|
|
77
|
-
await super.stopHandler();
|
|
78
|
-
await this.stopHttpServer();
|
|
79
|
-
}
|
|
80
|
-
async unexposeHandler(address, options) {
|
|
81
|
-
const { maxDepth = 2, required = true } = options ?? {};
|
|
82
|
-
assertEx(this.config.host, () => "Not configured as a host");
|
|
83
|
-
const mod = this._exposedModules.find((ref) => ref.deref()?.address === address)?.deref();
|
|
84
|
-
assertEx(!required || mod, () => `Module not exposed: ${address}`);
|
|
85
|
-
this._exposedModules = this._exposedModules.filter((ref) => ref.deref()?.address !== address);
|
|
86
|
-
if (mod) {
|
|
87
|
-
const children = maxDepth > 0 ? await mod.publicChildren?.() ?? [] : [];
|
|
88
|
-
const exposedChildren = (await Promise.all(children.map((child) => this.unexposeHandler(child.address, { maxDepth: maxDepth - 1, required: false })))).flat().filter(exists);
|
|
89
|
-
return [mod, ...exposedChildren];
|
|
90
|
-
}
|
|
91
|
-
return [];
|
|
92
|
-
}
|
|
93
|
-
async callLocalModule(address, query, payloads) {
|
|
94
|
-
const mod = this._exposedModules.find((ref) => ref.deref()?.address === address)?.deref();
|
|
95
|
-
return mod ? await mod.query(query, payloads) : null;
|
|
96
|
-
}
|
|
97
|
-
async handleGet(req, res) {
|
|
98
|
-
const { address } = req.params;
|
|
99
|
-
try {
|
|
100
|
-
if (address == this.address) {
|
|
101
|
-
res.json(await this.stateQuery(this.account));
|
|
102
|
-
} else {
|
|
103
|
-
const mod = this._exposedModules.find((ref) => ref.deref()?.address === address)?.deref();
|
|
104
|
-
if (mod) {
|
|
105
|
-
res.json(await mod.stateQuery(this.account));
|
|
106
|
-
} else {
|
|
107
|
-
res.status(StatusCodes.NOT_FOUND).json({ error: "Module not found" });
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
} catch (ex) {
|
|
111
|
-
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: ex.message });
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
async handlePost(req, res) {
|
|
115
|
-
const { address } = req.params;
|
|
116
|
-
const [bw, payloads = []] = Array.isArray(req.body) ? req.body : [];
|
|
117
|
-
const query = isQueryBoundWitness(bw) ? bw : void 0;
|
|
118
|
-
if (!query) {
|
|
119
|
-
res.status(StatusCodes.BAD_REQUEST).json({ error: "No query provided" });
|
|
120
|
-
return;
|
|
121
|
-
}
|
|
122
|
-
try {
|
|
123
|
-
if (address == this.address) {
|
|
124
|
-
const result = await this.query(query, payloads);
|
|
125
|
-
return res.json(result);
|
|
126
|
-
} else {
|
|
127
|
-
const result = await this.callLocalModule(address, query, payloads);
|
|
128
|
-
if (result === null) {
|
|
129
|
-
res.status(StatusCodes.NOT_FOUND).json({ error: "Module not found" });
|
|
130
|
-
} else {
|
|
131
|
-
res.json(result);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
} catch (ex) {
|
|
135
|
-
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: ex.message });
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
initializeApp() {
|
|
139
|
-
const app = express();
|
|
140
|
-
app.use(responseProfiler);
|
|
141
|
-
app.use(jsonBodyParser);
|
|
142
|
-
disableExpressDefaultPoweredByHeader(app);
|
|
143
|
-
app.use(customPoweredByHeader);
|
|
144
|
-
disableCaseSensitiveRouting(app);
|
|
145
|
-
useRequestCounters(app);
|
|
146
|
-
app.get("/", (_req, res) => res.redirect(StatusCodes.MOVED_TEMPORARILY, `/${this.address}`));
|
|
147
|
-
app.post("/", (_req, res) => res.redirect(StatusCodes.TEMPORARY_REDIRECT, `/${this.address}`));
|
|
148
|
-
app.get(
|
|
149
|
-
"/:address",
|
|
150
|
-
asyncHandler(async (req, res) => await this.handleGet(req, res))
|
|
151
|
-
);
|
|
152
|
-
app.post(
|
|
153
|
-
"/:address",
|
|
154
|
-
asyncHandler(async (req, res) => {
|
|
155
|
-
await this.handlePost(req, res);
|
|
156
|
-
})
|
|
157
|
-
);
|
|
158
|
-
return app;
|
|
159
|
-
}
|
|
160
|
-
startHttpServer() {
|
|
161
|
-
if (this.config.host) {
|
|
162
|
-
assertEx(!this._server, () => "Server already started");
|
|
163
|
-
this._server = this.app.listen(this.config.host?.port ?? 3030);
|
|
164
|
-
}
|
|
165
|
-
return Promise.resolve(true);
|
|
166
|
-
}
|
|
167
|
-
stopHttpServer() {
|
|
168
|
-
if (this.config.host) {
|
|
169
|
-
return new Promise((resolve, reject) => {
|
|
170
|
-
if (this._server) {
|
|
171
|
-
this._server.close((err) => {
|
|
172
|
-
if (err) {
|
|
173
|
-
reject(err);
|
|
174
|
-
} else {
|
|
175
|
-
this._server = void 0;
|
|
176
|
-
resolve(true);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return Promise.resolve(true);
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
__publicField(HttpBridgeExpress, "configSchemas", [...__superGet(HttpBridgeExpress, HttpBridgeExpress, "configSchemas"), HttpBridgeExpressConfigSchema]);
|
|
186
|
-
HttpBridgeExpress = __decorateClass([
|
|
187
|
-
creatableModule()
|
|
188
|
-
], HttpBridgeExpress);
|
|
189
|
-
export {
|
|
190
|
-
HttpBridgeExpress,
|
|
191
|
-
HttpBridgeExpressConfigSchema
|
|
192
|
-
};
|
|
193
|
-
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": 3,
|
|
3
|
-
"sources": ["../../src/HttpBridge.ts"],
|
|
4
|
-
"sourcesContent": ["import type { Server } from 'node:http'\n\nimport {\n asyncHandler,\n customPoweredByHeader,\n disableCaseSensitiveRouting,\n disableExpressDefaultPoweredByHeader,\n jsonBodyParser,\n responseProfiler,\n useRequestCounters,\n} from '@xylabs/express'\nimport {\n assertEx,\n exists, toSafeJsonString,\n} from '@xylabs/sdk-js'\nimport type { HttpBridgeConfig } from '@xyo-network/bridge-http'\nimport { HttpBridge } from '@xyo-network/bridge-http'\nimport type {\n BridgeExposeOptions, BridgeParams, BridgeUnexposeOptions,\n} from '@xyo-network/bridge-model'\n// import { standardResponses } from '@xyo-network/express-node-middleware'\nimport type {\n AnyConfigSchema, ModuleInstance, ModuleQueryResult,\n} from '@xyo-network/module-model'\nimport { creatableModule } from '@xyo-network/module-model'\nimport { resolveAddressToInstanceUp } from '@xyo-network/module-resolver'\nimport type {\n Payload, QueryBoundWitness, Schema, XyoAddress,\n} from '@xyo-network/sdk-protocol-js'\nimport { asSchema, isQueryBoundWitness } from '@xyo-network/sdk-protocol-js'\nimport type {\n Application, Request, Response,\n} from 'express'\nimport express from 'express'\nimport { StatusCodes } from 'http-status-codes'\n\n/**\n * The type of the path parameters for the address path.\n */\ninterface AddressPathParams {\n address: XyoAddress\n}\n\n/**\n * The type of the request body for the address path.\n */\ntype PostAddressRequestBody = [QueryBoundWitness, undefined | Payload[]]\n\n// TODO: This does not match the error response shape of the legacy bridge BUT it its the\n// shape this bridge is currently returning. Massage this into the standard\n// error shape constructed via middleware.\n/* type ErrorResponseBody = {\n error: string\n} */\n\nexport const HttpBridgeExpressConfigSchema = asSchema('network.xyo.bridge.http.express.config', true)\nexport type HttpBridgeExpressConfigSchema = typeof HttpBridgeExpressConfigSchema\n\nexport interface HttpBridgeExpressConfig extends HttpBridgeConfig<{}, HttpBridgeExpressConfigSchema> {}\n\nexport interface HttpBridgeExpressParams extends BridgeParams<AnyConfigSchema<HttpBridgeExpressConfig>> {}\n\n@creatableModule()\nexport class HttpBridgeExpress<TParams extends HttpBridgeExpressParams> extends HttpBridge<TParams> {\n static override readonly configSchemas: Schema[] = [...super.configSchemas, HttpBridgeExpressConfigSchema]\n protected _app?: Application\n protected _exposedModules: WeakRef<ModuleInstance>[] = []\n protected _server?: Server\n\n protected get app() {\n this._app ??= this.initializeApp()\n return assertEx(this._app, () => 'App not initialized')\n }\n\n async exposeChild(mod: ModuleInstance, options?: BridgeExposeOptions): Promise<ModuleInstance[]> {\n const { maxDepth = 5 } = options ?? {}\n assertEx(this.config.host, () => 'Not configured as a host')\n this._exposedModules.push(new WeakRef(mod))\n const children = maxDepth > 0 ? ((await mod.publicChildren?.()) ?? []) : []\n this.logger?.log(`childrenToExpose [${mod.id}][${mod.address}]: ${toSafeJsonString(children.map(child => child.id))}`)\n const exposedChildren = (await Promise.all(children.map(child => this.exposeChild(child, { maxDepth: maxDepth - 1, required: false }))))\n .flat()\n .filter(exists)\n const allExposed = [mod, ...exposedChildren]\n\n for (const exposedMod of allExposed) this.logger?.log(`exposed: ${exposedMod.address} [${mod.id}]`)\n\n return allExposed\n }\n\n override async exposeHandler(address: XyoAddress, options?: BridgeExposeOptions): Promise<ModuleInstance[]> {\n const { required = true } = options ?? {}\n const mod = await resolveAddressToInstanceUp(this, address)\n if (required && !mod) {\n throw new Error(`Unable to find required module: ${address}`)\n }\n if (mod) {\n return this.exposeChild(mod, options)\n }\n return []\n }\n\n override exposedHandler(): XyoAddress[] {\n return this._exposedModules.map(ref => ref.deref()?.address).filter(exists)\n }\n\n override async startHandler() {\n await super.startHandler()\n await this.startHttpServer()\n }\n\n override async stopHandler(_timeout?: number) {\n await super.stopHandler()\n await this.stopHttpServer()\n }\n\n override async unexposeHandler(address: XyoAddress, options?: BridgeUnexposeOptions): Promise<ModuleInstance[]> {\n const { maxDepth = 2, required = true } = options ?? {}\n assertEx(this.config.host, () => 'Not configured as a host')\n const mod = this._exposedModules.find(ref => ref.deref()?.address === address)?.deref()\n assertEx(!required || mod, () => `Module not exposed: ${address}`)\n this._exposedModules = this._exposedModules.filter(ref => ref.deref()?.address !== address)\n if (mod) {\n const children = maxDepth > 0 ? ((await mod.publicChildren?.()) ?? []) : []\n const exposedChildren = (\n await Promise.all(children.map(child => this.unexposeHandler(child.address, { maxDepth: maxDepth - 1, required: false })))\n )\n .flat()\n .filter(exists)\n return [mod, ...exposedChildren]\n }\n return []\n }\n\n protected async callLocalModule(address: XyoAddress, query: QueryBoundWitness, payloads: Payload[]): Promise<ModuleQueryResult | null> {\n const mod = this._exposedModules.find(ref => ref.deref()?.address === address)?.deref()\n return mod ? await mod.query(query, payloads) : null\n }\n\n protected async handleGet(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response) {\n const { address } = req.params\n try {\n if (address == this.address) {\n res.json(await this.stateQuery(this.account))\n } else {\n const mod = this._exposedModules.find(ref => ref.deref()?.address === address)?.deref()\n // TODO: Use standard errors middleware\n if (mod) {\n res.json(await mod.stateQuery(this.account))\n } else {\n res.status(StatusCodes.NOT_FOUND).json({ error: 'Module not found' })\n }\n }\n } catch (ex) {\n // TODO: Sanitize message\n res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: (ex as Error).message })\n }\n }\n\n protected async handlePost(req: Request<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>, res: Response) {\n const { address } = req.params\n const [bw, payloads = []] = Array.isArray(req.body) ? req.body : []\n const query = isQueryBoundWitness(bw) ? bw : undefined\n if (!query) {\n // TODO: Use standard errors middleware\n res.status(StatusCodes.BAD_REQUEST).json({ error: 'No query provided' })\n return\n }\n try {\n if (address == this.address) {\n const result = await this.query(query, payloads)\n return res.json(result)\n } else {\n const result = await this.callLocalModule(address, query, payloads)\n // TODO: Use standard errors middleware\n if (result === null) {\n res.status(StatusCodes.NOT_FOUND).json({ error: 'Module not found' })\n } else {\n res.json(result)\n }\n }\n } catch (ex) {\n // TODO: Sanitize message\n res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({ error: (ex as Error).message })\n }\n }\n\n protected initializeApp(): Application {\n // Create the express app\n const app = express()\n\n // Add middleware\n app.use(responseProfiler)\n app.use(jsonBodyParser)\n // removed for now since this causes a cycle\n // app.use(standardResponses)\n disableExpressDefaultPoweredByHeader(app)\n app.use(customPoweredByHeader)\n disableCaseSensitiveRouting(app)\n useRequestCounters(app)\n\n // Add routes\n // Redirect all requests to the root to this module's address\n app.get('/', (_req, res) => res.redirect(StatusCodes.MOVED_TEMPORARILY, `/${this.address}`))\n app.post('/', (_req, res) => res.redirect(StatusCodes.TEMPORARY_REDIRECT, `/${this.address}`))\n\n app.get<AddressPathParams, ModuleQueryResult>(\n '/:address',\n\n asyncHandler(async (req, res) => await this.handleGet(req, res)),\n )\n app.post<AddressPathParams, ModuleQueryResult, PostAddressRequestBody>(\n '/:address',\n\n asyncHandler(async (req, res) => { await this.handlePost(req, res) }),\n )\n return app\n }\n\n protected startHttpServer(): Promise<boolean> {\n if (this.config.host) {\n assertEx(!this._server, () => 'Server already started')\n this._server = this.app.listen(this.config.host?.port ?? 3030)\n }\n return Promise.resolve(true)\n }\n\n protected stopHttpServer(): Promise<boolean> {\n if (this.config.host) {\n return new Promise((resolve, reject) => {\n if (this._server) {\n this._server.close((err) => {\n if (err) {\n reject(err)\n } else {\n this._server = undefined\n resolve(true)\n }\n })\n }\n })\n }\n return Promise.resolve(true)\n }\n}\n"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAEA;AAAA,EACE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