@powerhousedao/reactor-api 1.3.3 → 1.4.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 +7 -6
- package/dist/index.d.ts +285 -10
- package/dist/index.js +95 -15673
- package/dist/index.js.map +1 -1
- package/package.json +11 -8
- package/src/internal-listener-manager.ts +10 -13
- package/src/router.ts +10 -12
- package/src/server.ts +3 -3
- package/src/subgraphs/drive/subgraph.ts +2 -2
- package/src/subgraphs/system/subgraph.ts +2 -2
- package/src/subgraphs/types.ts +3 -3
- package/src/types.ts +2 -2
- package/src/utils/create-schema.ts +7 -9
- package/test/router.test.ts +7 -7
- package/tsconfig.json +4 -1
- package/tsup.config.ts +1 -0
- package/vitest.config.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powerhousedao/reactor-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,28 +13,31 @@
|
|
|
13
13
|
"author": "",
|
|
14
14
|
"license": "AGPL-3.0-only",
|
|
15
15
|
"devDependencies": {
|
|
16
|
+
"@powerhousedao/scalars": "1.9.0",
|
|
16
17
|
"@types/body-parser": "^1.19.5",
|
|
17
18
|
"@types/cors": "^2.8.17",
|
|
18
19
|
"@types/express": "^5.0.0",
|
|
19
|
-
"document-drive": "^1.
|
|
20
|
+
"document-drive": "^1.4.0",
|
|
20
21
|
"document-model": "^2.6.0",
|
|
21
22
|
"esbuild": "^0.24.0",
|
|
22
|
-
"graphql": "^16.9.0",
|
|
23
23
|
"graphql-tag": "^2.12.6",
|
|
24
24
|
"tsup": "^8.3.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"document-
|
|
28
|
-
"
|
|
27
|
+
"document-model": "^2.3.1",
|
|
28
|
+
"@powerhousedao/scalars": "^1.9.0"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"@apollo/server": "^4.11.0",
|
|
32
32
|
"@apollo/subgraph": "^2.9.2",
|
|
33
33
|
"body-parser": "^1.20.3",
|
|
34
34
|
"cors": "^2.8.5",
|
|
35
|
-
"document-model-libs": "^1.
|
|
36
|
-
"
|
|
37
|
-
"
|
|
35
|
+
"document-model-libs": "^1.110.1",
|
|
36
|
+
"express": "^4.21.1",
|
|
37
|
+
"graphql": "^16.9.0",
|
|
38
|
+
"graphql-request": "^6.1.0",
|
|
39
|
+
"nanoevents": "^9.0.0",
|
|
40
|
+
"uuid": "^9.0.1"
|
|
38
41
|
},
|
|
39
42
|
"scripts": {
|
|
40
43
|
"build": "tsup",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
IDocumentDriveServer,
|
|
3
3
|
InternalTransmitter,
|
|
4
4
|
InternalTransmitterUpdate,
|
|
5
5
|
Listener,
|
|
@@ -13,10 +13,10 @@ export type InternalListenerModule = {
|
|
|
13
13
|
};
|
|
14
14
|
|
|
15
15
|
export class InternalListenerManager {
|
|
16
|
-
private driveServer:
|
|
16
|
+
private driveServer: IDocumentDriveServer;
|
|
17
17
|
private modules: InternalListenerModule[] = [];
|
|
18
18
|
|
|
19
|
-
constructor(driveServer:
|
|
19
|
+
constructor(driveServer: IDocumentDriveServer) {
|
|
20
20
|
this.driveServer = driveServer;
|
|
21
21
|
driveServer.on("driveAdded", this.#onDriveAdded.bind(this));
|
|
22
22
|
}
|
|
@@ -32,30 +32,27 @@ export class InternalListenerManager {
|
|
|
32
32
|
return Promise.resolve();
|
|
33
33
|
},
|
|
34
34
|
},
|
|
35
|
-
{ ...module.options, label: module.options.label ?? "" }
|
|
36
|
-
)
|
|
37
|
-
)
|
|
35
|
+
{ ...module.options, label: module.options.label ?? "" },
|
|
36
|
+
),
|
|
37
|
+
),
|
|
38
38
|
);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
async init() {
|
|
42
42
|
const drives = await this.driveServer.getDrives();
|
|
43
43
|
|
|
44
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
45
44
|
for (const { options, transmit } of this.modules) {
|
|
46
|
-
console.log(options, transmit);
|
|
47
45
|
if (!options || !transmit) {
|
|
48
46
|
continue;
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
// eslint-disable-next-line no-restricted-syntax
|
|
52
49
|
for (const driveId of drives) {
|
|
53
50
|
try {
|
|
54
51
|
const { listenerId } = options;
|
|
55
52
|
const drive = await this.driveServer.getDrive(driveId);
|
|
56
53
|
const moduleRegistered =
|
|
57
54
|
drive.state.local.listeners.filter(
|
|
58
|
-
(l) => l.listenerId === listenerId
|
|
55
|
+
(l) => l.listenerId === listenerId,
|
|
59
56
|
).length > 0;
|
|
60
57
|
if (!moduleRegistered) {
|
|
61
58
|
await this.driveServer.addInternalListener(
|
|
@@ -69,7 +66,7 @@ export class InternalListenerManager {
|
|
|
69
66
|
filter: options.filter,
|
|
70
67
|
label: options.label!,
|
|
71
68
|
listenerId,
|
|
72
|
-
}
|
|
69
|
+
},
|
|
73
70
|
);
|
|
74
71
|
|
|
75
72
|
return;
|
|
@@ -77,7 +74,7 @@ export class InternalListenerManager {
|
|
|
77
74
|
|
|
78
75
|
const transmitter = await this.driveServer.getTransmitter(
|
|
79
76
|
driveId,
|
|
80
|
-
listenerId
|
|
77
|
+
listenerId,
|
|
81
78
|
);
|
|
82
79
|
if (transmitter instanceof InternalTransmitter) {
|
|
83
80
|
transmitter.setReceiver({
|
|
@@ -94,7 +91,7 @@ export class InternalListenerManager {
|
|
|
94
91
|
} catch (e) {
|
|
95
92
|
console.error(
|
|
96
93
|
`Error while initializing listener ${options.listenerId} for drive ${driveId}`,
|
|
97
|
-
e
|
|
94
|
+
e,
|
|
98
95
|
);
|
|
99
96
|
}
|
|
100
97
|
}
|
package/src/router.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { expressMiddleware } from "@apollo/server/express4";
|
|
|
3
3
|
import { ApolloServerPluginInlineTraceDisabled } from "@apollo/server/plugin/disabled";
|
|
4
4
|
import bodyParser from "body-parser";
|
|
5
5
|
import cors from "cors";
|
|
6
|
-
import {
|
|
6
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
7
7
|
import express, { IRouter, Router } from "express";
|
|
8
8
|
import {
|
|
9
9
|
InternalListenerManager,
|
|
@@ -18,9 +18,7 @@ const getLocalSubgraphConfig = (subgraphName: string) =>
|
|
|
18
18
|
|
|
19
19
|
let listenerManager: InternalListenerManager | undefined;
|
|
20
20
|
|
|
21
|
-
export const getListenerManager = async (
|
|
22
|
-
driveServer: BaseDocumentDriveServer
|
|
23
|
-
) => {
|
|
21
|
+
export const getListenerManager = async (driveServer: IDocumentDriveServer) => {
|
|
24
22
|
if (!listenerManager) {
|
|
25
23
|
listenerManager = new InternalListenerManager(driveServer);
|
|
26
24
|
await listenerManager.init();
|
|
@@ -28,7 +26,7 @@ export const getListenerManager = async (
|
|
|
28
26
|
return listenerManager;
|
|
29
27
|
};
|
|
30
28
|
|
|
31
|
-
export const updateRouter = async (driveServer:
|
|
29
|
+
export const updateRouter = async (driveServer: IDocumentDriveServer) => {
|
|
32
30
|
const newRouter = Router();
|
|
33
31
|
newRouter.use(cors());
|
|
34
32
|
newRouter.use(bodyParser.json());
|
|
@@ -62,7 +60,7 @@ export const updateRouter = async (driveServer: BaseDocumentDriveServer) => {
|
|
|
62
60
|
driveServer,
|
|
63
61
|
...getAdditionalContextFields(),
|
|
64
62
|
}),
|
|
65
|
-
})
|
|
63
|
+
}),
|
|
66
64
|
);
|
|
67
65
|
console.log(`Setting up [${subgraphConfig.name}] subgraph at ${path}`);
|
|
68
66
|
}
|
|
@@ -72,16 +70,16 @@ export const updateRouter = async (driveServer: BaseDocumentDriveServer) => {
|
|
|
72
70
|
console.log("All subgraphs started.");
|
|
73
71
|
};
|
|
74
72
|
|
|
75
|
-
let docDriveServer:
|
|
73
|
+
let docDriveServer: IDocumentDriveServer;
|
|
76
74
|
export const initReactorRouter = async (
|
|
77
75
|
path: string,
|
|
78
76
|
app: express.Express,
|
|
79
|
-
driveServer:
|
|
77
|
+
driveServer: IDocumentDriveServer,
|
|
80
78
|
) => {
|
|
81
79
|
docDriveServer = driveServer;
|
|
82
80
|
const models = driveServer.getDocumentModels();
|
|
83
81
|
const driveModel = models.find(
|
|
84
|
-
(it) => it.documentModel.name === "DocumentDrive"
|
|
82
|
+
(it) => it.documentModel.name === "DocumentDrive",
|
|
85
83
|
);
|
|
86
84
|
|
|
87
85
|
if (!driveModel) {
|
|
@@ -90,21 +88,21 @@ export const initReactorRouter = async (
|
|
|
90
88
|
|
|
91
89
|
await updateRouter(driveServer);
|
|
92
90
|
driveServer.on("documentModels", () => {
|
|
93
|
-
updateRouter(driveServer);
|
|
91
|
+
updateRouter(driveServer).catch((error: unknown) => console.error(error));
|
|
94
92
|
});
|
|
95
93
|
|
|
96
94
|
app.use(path, (req, res, next) => reactorRouter(req, res, next));
|
|
97
95
|
};
|
|
98
96
|
|
|
99
97
|
export const addSubgraph = async (
|
|
100
|
-
subgraph: (typeof SUBGRAPH_REGISTRY)[number]
|
|
98
|
+
subgraph: (typeof SUBGRAPH_REGISTRY)[number],
|
|
101
99
|
) => {
|
|
102
100
|
SUBGRAPH_REGISTRY.unshift(subgraph);
|
|
103
101
|
await updateRouter(docDriveServer);
|
|
104
102
|
};
|
|
105
103
|
|
|
106
104
|
export const registerInternalListener = async (
|
|
107
|
-
module: InternalListenerModule
|
|
105
|
+
module: InternalListenerModule,
|
|
108
106
|
) => {
|
|
109
107
|
if (!listenerManager) {
|
|
110
108
|
throw new Error("Listener manager not initialized");
|
package/src/server.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
2
2
|
import express, { Express } from "express";
|
|
3
3
|
import { initReactorRouter } from "./router";
|
|
4
4
|
type Options = {
|
|
@@ -9,8 +9,8 @@ type Options = {
|
|
|
9
9
|
const DEFAULT_PORT = 4000;
|
|
10
10
|
|
|
11
11
|
export async function startAPI(
|
|
12
|
-
reactor:
|
|
13
|
-
options: Options
|
|
12
|
+
reactor: IDocumentDriveServer,
|
|
13
|
+
options: Options,
|
|
14
14
|
) {
|
|
15
15
|
const port = options.port ?? DEFAULT_PORT;
|
|
16
16
|
const app = options.express ?? express();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GraphQLResolverMap } from "@apollo/subgraph/dist/schema-helper";
|
|
2
|
-
import {
|
|
2
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
4
|
import { dirname, resolve } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
@@ -12,5 +12,5 @@ const __dirname =
|
|
|
12
12
|
|
|
13
13
|
const typeDefs = readFileSync(resolve(__dirname, schemaPath), "utf8");
|
|
14
14
|
|
|
15
|
-
export const getSchema = (driveServer:
|
|
15
|
+
export const getSchema = (driveServer: IDocumentDriveServer) =>
|
|
16
16
|
createSchema(driveServer, resolvers as GraphQLResolverMap, typeDefs);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GraphQLResolverMap } from "@apollo/subgraph/dist/schema-helper";
|
|
2
|
-
import {
|
|
2
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
3
3
|
import { readFileSync } from "fs";
|
|
4
4
|
import { dirname, resolve } from "path";
|
|
5
5
|
import { fileURLToPath } from "url";
|
|
@@ -12,5 +12,5 @@ const __dirname =
|
|
|
12
12
|
|
|
13
13
|
const typeDefs = readFileSync(resolve(__dirname, schemaPath), "utf8");
|
|
14
14
|
|
|
15
|
-
export const getSchema = (driveServer:
|
|
15
|
+
export const getSchema = (driveServer: IDocumentDriveServer) =>
|
|
16
16
|
createSchema(driveServer, resolvers as GraphQLResolverMap, typeDefs);
|
package/src/subgraphs/types.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { IncomingHttpHeaders } from "
|
|
2
|
-
import {
|
|
1
|
+
import type { IncomingHttpHeaders } from "http";
|
|
2
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
3
3
|
|
|
4
4
|
export type Context = {
|
|
5
|
-
driveServer:
|
|
5
|
+
driveServer: IDocumentDriveServer;
|
|
6
6
|
driveId?: string;
|
|
7
7
|
headers: IncomingHttpHeaders;
|
|
8
8
|
db: unknown;
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
2
2
|
import { IncomingHttpHeaders } from "http";
|
|
3
3
|
|
|
4
4
|
export interface Context {
|
|
5
5
|
headers: IncomingHttpHeaders;
|
|
6
6
|
driveId: string | undefined;
|
|
7
|
-
driveServer:
|
|
7
|
+
driveServer: IDocumentDriveServer;
|
|
8
8
|
}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { buildSubgraphSchema } from "@apollo/subgraph";
|
|
2
|
-
import {
|
|
3
|
-
import { readFileSync } from "node:fs";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { IDocumentDriveServer } from "document-drive";
|
|
5
3
|
import { GraphQLResolverMap } from "@apollo/subgraph/dist/schema-helper";
|
|
6
4
|
import { parse } from "graphql";
|
|
7
5
|
|
|
8
6
|
export const createSchema = (
|
|
9
|
-
documentDriveServer:
|
|
7
|
+
documentDriveServer: IDocumentDriveServer,
|
|
10
8
|
resolvers: GraphQLResolverMap,
|
|
11
|
-
typeDefs: string
|
|
9
|
+
typeDefs: string,
|
|
12
10
|
) =>
|
|
13
11
|
buildSubgraphSchema([
|
|
14
12
|
{
|
|
@@ -18,8 +16,8 @@ export const createSchema = (
|
|
|
18
16
|
]);
|
|
19
17
|
|
|
20
18
|
export const getDocumentModelTypeDefs = (
|
|
21
|
-
documentDriveServer:
|
|
22
|
-
typeDefs: string
|
|
19
|
+
documentDriveServer: IDocumentDriveServer,
|
|
20
|
+
typeDefs: string,
|
|
23
21
|
) => {
|
|
24
22
|
const documentModels = documentDriveServer.getDocumentModels();
|
|
25
23
|
let dmSchema = "";
|
|
@@ -32,7 +30,7 @@ export const getDocumentModelTypeDefs = (
|
|
|
32
30
|
.replaceAll(`: Account`, `: ${documentModel.name}Account`)
|
|
33
31
|
.replaceAll(`[Account!]!`, `[${documentModel.name}Account!]!`)
|
|
34
32
|
.replaceAll("scalar DateTime", "")
|
|
35
|
-
.replaceAll(/input (.*?) {[\s\S]*?}/g, "")
|
|
33
|
+
.replaceAll(/input (.*?) {[\s\S]*?}/g, ""),
|
|
36
34
|
)
|
|
37
35
|
.join("\n")};
|
|
38
36
|
|
|
@@ -46,7 +44,7 @@ export const getDocumentModelTypeDefs = (
|
|
|
46
44
|
.replaceAll(/input (.*?) {[\s\S]*?}/g, "")
|
|
47
45
|
.replaceAll("type AccountSnapshotLocalState", "")
|
|
48
46
|
.replaceAll("type BudgetStatementLocalState", "")
|
|
49
|
-
.replaceAll("type ScopeFrameworkLocalState", "")
|
|
47
|
+
.replaceAll("type ScopeFrameworkLocalState", ""),
|
|
50
48
|
)
|
|
51
49
|
.join("\n")};
|
|
52
50
|
|
package/test/router.test.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
1
3
|
import { buildSubgraphSchema } from "@apollo/subgraph";
|
|
2
4
|
import * as DocumentModelsLibs from "document-model-libs/document-models";
|
|
3
5
|
import { DocumentModel } from "document-model/document";
|
|
4
6
|
import { module as DocumentModelLib } from "document-model/document-model";
|
|
5
|
-
import { describe, expect, it } from "vitest";
|
|
6
7
|
import {
|
|
7
|
-
|
|
8
|
+
IDocumentDriveServer,
|
|
8
9
|
DocumentDriveServer,
|
|
9
10
|
} from "../../document-drive/src/server";
|
|
10
11
|
import { addSubgraph } from "../src/index";
|
|
11
|
-
import { initReactorRouter, reactorRouter
|
|
12
|
-
import { getDocumentModelTypeDefs } from "../src/utils/
|
|
13
|
-
import express from "express";
|
|
12
|
+
import { initReactorRouter, reactorRouter } from "../src/router";
|
|
13
|
+
import { getDocumentModelTypeDefs } from "../src/utils/create-schema";
|
|
14
14
|
|
|
15
15
|
const documentModels = [
|
|
16
16
|
DocumentModelLib,
|
|
@@ -33,7 +33,7 @@ describe("Reactor Router", () => {
|
|
|
33
33
|
await driveServer.initialize();
|
|
34
34
|
const newSubgraph = {
|
|
35
35
|
name: "newSubgraph",
|
|
36
|
-
getSchema: (documentDriveServer:
|
|
36
|
+
getSchema: (documentDriveServer: IDocumentDriveServer) =>
|
|
37
37
|
buildSubgraphSchema([
|
|
38
38
|
{
|
|
39
39
|
typeDefs: getDocumentModelTypeDefs(
|
|
@@ -42,7 +42,7 @@ describe("Reactor Router", () => {
|
|
|
42
42
|
type Query {
|
|
43
43
|
hello: String
|
|
44
44
|
}
|
|
45
|
-
|
|
45
|
+
`,
|
|
46
46
|
),
|
|
47
47
|
resolvers: { Query: { hello: () => "world" } },
|
|
48
48
|
},
|
package/tsconfig.json
CHANGED
package/tsup.config.ts
CHANGED
package/vitest.config.ts
CHANGED
|
@@ -10,7 +10,7 @@ export default defineConfig({
|
|
|
10
10
|
// Resolve the path relative to the the file that imports the .graphql file
|
|
11
11
|
return resolve(
|
|
12
12
|
dirname(importer || ""),
|
|
13
|
-
`${source.startsWith("/") ? `.${source}` : source}.graphql
|
|
13
|
+
`${source.startsWith("/") ? `.${source}` : source}.graphql`,
|
|
14
14
|
);
|
|
15
15
|
}
|
|
16
16
|
return null; // Let other resolvers handle other imports
|