@powerhousedao/reactor-api 1.29.17-staging.0 → 1.29.18-dev.2

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.
@@ -0,0 +1,4 @@
1
+ export declare const config: {
2
+ basePath: string;
3
+ };
4
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM;;CAElB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export const config = {
2
+ basePath: process.env.BASE_PATH || "/",
3
+ };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/base/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAG5C,qBAAa,QAAS,YAAW,SAAS;IACxC,IAAI,SAAa;IACjB,IAAI,SAAM;IACV,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAI5B;IACF,QAAQ,EAAE,YAAY,CAIpB;IACF,OAAO,EAAE,oBAAoB,CAAC;IAC9B,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,EAAE,CAAC;gBACT,IAAI,EAAE,YAAY;IAKxB,OAAO;CAGd"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/base/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,SAAS,CAAC;AAG5C,qBAAa,QAAS,YAAW,SAAS;IACxC,IAAI,SAAa;IACjB,IAAI,SAAM;IACV,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAI5B;IACF,QAAQ,EAAE,YAAY,CAIpB;IACF,OAAO,EAAE,oBAAoB,CAAC;IAC9B,cAAc,EAAE,cAAc,CAAC;IAC/B,gBAAgB,EAAE,EAAE,CAAC;gBACT,IAAI,EAAE,YAAY;IAMxB,OAAO;CAGd"}
@@ -19,6 +19,7 @@ export class Subgraph {
19
19
  this.reactor = args.reactor;
20
20
  this.graphqlManager = args.graphqlManager;
21
21
  this.operationalStore = args.operationalStore;
22
+ this.path = args.path ?? "";
22
23
  }
23
24
  async onSetup() {
24
25
  // noop
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/drive/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAOpE,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,oDAAoD,CAAC;AAyB7F,qBAAa,aAAc,SAAQ,QAAQ;IACzC,OAAO,CAAC,MAAM,CAGX;gBAES,IAAI,EAAE,YAAY;IAM9B,IAAI,SAAc;IAClB,QAAQ,iCAsJN;IAEF,SAAS,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAoPpC;CACH"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/graphql/drive/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAClD,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAOpE,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,oDAAoD,CAAC;AAyB7F,qBAAa,aAAc,SAAQ,QAAQ;IACzC,OAAO,CAAC,MAAM,CAGX;gBAES,IAAI,EAAE,YAAY;IAS9B,IAAI,SAAc;IAClB,QAAQ,iCAsJN;IAEF,SAAS,EAAE,kBAAkB,CAAC,OAAO,CAAC,CAoPpC;CACH"}
@@ -16,6 +16,9 @@ export class DriveSubgraph extends Subgraph {
16
16
  super(args);
17
17
  this.logger.verbose(`constructor()`);
18
18
  }
19
+ // Note: This GQL declaration depends on the document drive subgraph already
20
+ // being registered. If the document drive subgraph is not registered, this
21
+ // will throw an error as it relies on those types.
19
22
  name = "d/:drive";
20
23
  typeDefs = gql `
21
24
  type DriveMeta {
@@ -15,7 +15,7 @@ export declare class GraphQLManager {
15
15
  private contextFields;
16
16
  constructor(path: string, app: express.Express, reactor: IDocumentDriveServer, operationalStore: Db, analyticsStore: IAnalyticsStore, subgraphs?: Map<string, Subgraph[]>);
17
17
  init(): Promise<void>;
18
- registerSubgraph(subgraph: SubgraphClass, supergraph?: string, path?: string): Promise<void>;
18
+ registerSubgraph(subgraph: SubgraphClass, supergraph?: string): Promise<void>;
19
19
  updateRouter(): Promise<void>;
20
20
  getAdditionalContextFields: () => Record<string, any>;
21
21
  setAdditionalContextFields(fields: Record<string, any>): void;
@@ -1 +1 @@
1
- {"version":3,"file":"graphql-manager.d.ts","sourceRoot":"","sources":["../../../src/graphql/graphql-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,EAAE,MAAM,WAAW,CAAC;AAOpC,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,sCAAsC,CAAC;AAG5E,OAAO,EAAE,KAAK,oBAAoB,EAAE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;AAOnC,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,YAAY,CAAC;AAG/D,qBAAa,cAAc;;IAKvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG;IACpB,OAAO,CAAC,QAAQ,CAAC,OAAO;IACxB,OAAO,CAAC,QAAQ,CAAC,gBAAgB;IACjC,OAAO,CAAC,QAAQ,CAAC,cAAc;IAC/B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAT5B,OAAO,CAAC,aAAa,CAAqB;IAC1C,OAAO,CAAC,aAAa,CAA2B;gBAG7B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,OAAO,CAAC,OAAO,EACpB,OAAO,EAAE,oBAAoB,EAC7B,gBAAgB,EAAE,EAAE,EACpB,cAAc,EAAE,eAAe,EAC/B,SAAS,GAAE,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,CAAa;IAK3D,IAAI;IA2BJ,gBAAgB,CAAC,QAAQ,EAAE,aAAa,EAAE,UAAU,SAAK;IA6BzD,YAAY;IASlB,0BAA0B,4BAExB;IAEF,0BAA0B,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAItD,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE;CAiJxD"}
@@ -7,6 +7,7 @@ import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin
7
7
  import bodyParser from "body-parser";
8
8
  import cors from "cors";
9
9
  import { Router } from "express";
10
+ import path from "node:path";
10
11
  import { AnalyticsSubgraph } from "./analytics/index.js";
11
12
  import { AuthSubgraph } from "./auth/index.js";
12
13
  import { DriveSubgraph } from "./drive/index.js";
@@ -27,15 +28,16 @@ export class GraphQLManager {
27
28
  this.operationalStore = operationalStore;
28
29
  this.analyticsStore = analyticsStore;
29
30
  this.subgraphs = subgraphs;
30
- // Setup Default subgraphs
31
- this.registerSubgraph(AuthSubgraph, "graphql");
32
- this.registerSubgraph(SystemSubgraph, "graphql");
33
- this.registerSubgraph(AnalyticsSubgraph, "graphql");
34
- // special case for drive
35
- this.registerSubgraph(DriveSubgraph);
31
+ this.subgraphs.set("graphql", []);
36
32
  }
37
33
  async init() {
38
34
  console.log(`> Initializing Subgraph Manager...`);
35
+ // Setup Default subgraphs
36
+ await this.registerSubgraph(AuthSubgraph, "graphql");
37
+ await this.registerSubgraph(SystemSubgraph, "graphql");
38
+ await this.registerSubgraph(AnalyticsSubgraph, "graphql");
39
+ // special case for drive
40
+ await this.registerSubgraph(DriveSubgraph);
39
41
  const models = this.reactor.getDocumentModelModules();
40
42
  const driveModel = models.find((it) => it.documentModel.name === "DocumentDrive");
41
43
  if (!driveModel) {
@@ -44,15 +46,16 @@ export class GraphQLManager {
44
46
  this.reactor.on("documentModelModules", () => {
45
47
  this.updateRouter().catch((error) => console.error(error));
46
48
  });
47
- this.app.use(this.path, (req, res, next) => this.reactorRouter(req, res, next));
49
+ this.app.use("/", (req, res, next) => this.reactorRouter(req, res, next));
50
+ this.updateRouter();
48
51
  }
49
- async registerSubgraph(subgraph, supergraph = "", path = "") {
52
+ async registerSubgraph(subgraph, supergraph = "") {
50
53
  const subgraphInstance = new subgraph({
51
54
  operationalStore: this.operationalStore,
52
55
  analyticsStore: this.analyticsStore,
53
56
  reactor: this.reactor,
54
57
  graphqlManager: this,
55
- path,
58
+ path: this.path,
56
59
  });
57
60
  await subgraphInstance.onSetup();
58
61
  if (!this.subgraphs.get(supergraph)) {
@@ -62,10 +65,15 @@ export class GraphQLManager {
62
65
  this.subgraphs.set(supergraph, []);
63
66
  }
64
67
  this.subgraphs.get(supergraph)?.push(subgraphInstance);
65
- console.log(`> Registered ${supergraph ? "/" + supergraph : ""}${this.path.endsWith("/") ? this.path : this.path + "/"}${subgraphInstance.name} subgraph.`);
66
- await this.updateRouter();
68
+ // also add to global graphql supergraph
69
+ if (supergraph !== "" && supergraph !== "graphql") {
70
+ this.subgraphs.get("graphql")?.push(subgraphInstance);
71
+ }
72
+ console.log(`> Registered ${this.path.endsWith("/") ? this.path : this.path + "/"}${supergraph ? supergraph + "/" : ""}${subgraphInstance.name} subgraph.`);
73
+ // this.updateRouter();
67
74
  }
68
75
  async updateRouter() {
76
+ console.log("> Updating router");
69
77
  const newRouter = Router();
70
78
  newRouter.use(cors());
71
79
  newRouter.use(bodyParser.json());
@@ -110,9 +118,12 @@ export class GraphQLManager {
110
118
  }
111
119
  });
112
120
  }
121
+ #getSubgraphPath(subgraph, supergraph) {
122
+ return path.join(subgraph.path, supergraph, subgraph.name);
123
+ }
113
124
  async #setupSubgraphs(router) {
114
125
  for (const [supergraph, subgraphs] of this.subgraphs.entries()) {
115
- const supergraphEndpoints = {};
126
+ const supergraphEndpoints = [];
116
127
  for (const subgraph of subgraphs) {
117
128
  const subgraphConfig = this.#getLocalSubgraphConfig(subgraph.name);
118
129
  if (!subgraphConfig)
@@ -121,30 +132,39 @@ export class GraphQLManager {
121
132
  const schema = createSchema(this.reactor, subgraphConfig.resolvers, subgraphConfig.typeDefs);
122
133
  // create and start apollo server
123
134
  const server = this.#createApolloServer(schema);
124
- const path = `${subgraph.path ? "/" + subgraph.path : ""}${supergraph !== "" ? "/" + supergraph : ""}/${subgraphConfig.name}`;
135
+ const path = this.#getSubgraphPath(subgraph, supergraph);
125
136
  await server.start();
126
137
  await this.#waitForServer(server);
127
138
  this.#setupApolloExpressMiddleware(server, router, path);
128
139
  if (supergraph !== "") {
129
- supergraphEndpoints[path] = server;
140
+ supergraphEndpoints.push(this.#getSubgraphPath(subgraph, supergraph));
130
141
  }
131
142
  }
132
- if (Object.keys(supergraphEndpoints).length > 0) {
143
+ if (supergraphEndpoints.length > 0) {
144
+ await this.#sleep(1000);
133
145
  const supergraphServer = await this.#createApolloGateway(supergraphEndpoints);
134
146
  if (supergraphServer) {
135
- const path = `/${supergraph}`;
136
- this.#setupApolloExpressMiddleware(supergraphServer, router, path);
147
+ const superGraphPath = path.join(this.path, supergraph ?? "graphql");
148
+ this.#setupApolloExpressMiddleware(supergraphServer, router, superGraphPath);
137
149
  }
138
150
  }
139
151
  }
140
152
  }
153
+ #sleep(ms) {
154
+ return new Promise((resolve) => {
155
+ setTimeout(resolve, ms);
156
+ });
157
+ }
141
158
  async #createApolloGateway(endpoints) {
142
159
  try {
160
+ const herokuOrLocal = process.env.HEROKU_APP_DEFAULT_DOMAIN_NAME
161
+ ? `https://${process.env.HEROKU_APP_DEFAULT_DOMAIN_NAME}`
162
+ : `http://localhost:${process.env.PORT ?? 4001}`;
143
163
  const gateway = new ApolloGateway({
144
164
  supergraphSdl: new IntrospectAndCompose({
145
- subgraphs: Object.keys(endpoints).map((path) => ({
146
- name: path.replaceAll("/", ""),
147
- url: `http://localhost:${process.env.PORT ?? 4001}${path}`,
165
+ subgraphs: endpoints.map((endpoint) => ({
166
+ name: endpoint.replaceAll("/", ""),
167
+ url: `${herokuOrLocal}${endpoint}`,
148
168
  })),
149
169
  }),
150
170
  });
@@ -0,0 +1,14 @@
1
+ import { SubgraphClass } from "#graphql/index.js";
2
+ import { ProcessorFactory } from "document-drive/processors/types";
3
+ import { DocumentModelModule } from "document-model";
4
+ import { IPackageLoader } from "./types.js";
5
+ /**
6
+ * This class is used to load packages using the import keyword.
7
+ */
8
+ export declare class ImportPackageLoader implements IPackageLoader {
9
+ private readonly logger;
10
+ loadDocumentModels(identifier: string): Promise<DocumentModelModule[]>;
11
+ loadSubgraphs(identifier: string): Promise<SubgraphClass[]>;
12
+ loadProcessors(identifier: string): Promise<(module: any) => ProcessorFactory>;
13
+ }
14
+ //# sourceMappingURL=import-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"import-loader.d.ts","sourceRoot":"","sources":["../../../src/packages/import-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAG5C;;GAEG;AACH,qBAAa,mBAAoB,YAAW,cAAc;IACxD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiD;IAElE,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAyBtE,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAsB3D,cAAc,CAClB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,gBAAgB,CAAC;CAgB9C"}
@@ -0,0 +1,53 @@
1
+ import { childLogger } from "document-drive";
2
+ import { loadDependency } from "./util.js";
3
+ /**
4
+ * This class is used to load packages using the import keyword.
5
+ */
6
+ export class ImportPackageLoader {
7
+ logger = childLogger(["reactor-api", "import-loader"]);
8
+ async loadDocumentModels(identifier) {
9
+ this.logger.verbose("Loading document models from package:", identifier);
10
+ let pkgModule;
11
+ try {
12
+ pkgModule = (await loadDependency(identifier, "document-models"));
13
+ }
14
+ catch (e) {
15
+ this.logger.verbose(` ➜ No Document Models found: ${identifier}`);
16
+ return [];
17
+ }
18
+ if (pkgModule) {
19
+ this.logger.verbose(` ➜ Loaded Document Models from: ${identifier}`);
20
+ return Object.values(pkgModule);
21
+ }
22
+ else {
23
+ this.logger.verbose(` ➜ No Document Models found: ${identifier}`);
24
+ return [];
25
+ }
26
+ }
27
+ async loadSubgraphs(identifier) {
28
+ this.logger.verbose("Loading subgraphs from package:", identifier);
29
+ const pkgModule = (await loadDependency(identifier, "subgraphs"));
30
+ if (!pkgModule) {
31
+ this.logger.verbose(` ➜ No Subgraphs found: ${identifier}`);
32
+ return [];
33
+ }
34
+ const subgraphs = Object.values(pkgModule).map((subgraph) => {
35
+ return Object.values(subgraph);
36
+ });
37
+ this.logger.verbose(` ➜ Loaded Subgraphs from: ${identifier}`);
38
+ return subgraphs.flat();
39
+ }
40
+ async loadProcessors(identifier) {
41
+ this.logger.verbose("Loading processors from package:", identifier);
42
+ const pkgModule = (await loadDependency(identifier, "processors"));
43
+ if (pkgModule) {
44
+ this.logger.verbose(` ➜ Loaded Processor Factory from: ${identifier}`);
45
+ return pkgModule;
46
+ }
47
+ else {
48
+ this.logger.verbose(` ➜ No Processor Factory found: ${identifier}`);
49
+ }
50
+ // empty processor factory
51
+ return () => () => [];
52
+ }
53
+ }
@@ -0,0 +1,28 @@
1
+ import { type SubgraphClass } from "@powerhousedao/reactor-api";
2
+ import { ProcessorFactory } from "document-drive/processors/types";
3
+ import { type DocumentModelModule } from "document-model";
4
+ import { IPackageLoader, IPackageManager, IPackageManagerOptions, PackageManagerResult } from "./types.js";
5
+ export declare function getUniqueDocumentModels(...documentModels: DocumentModelModule[][]): DocumentModelModule[];
6
+ export declare class PackageManager implements IPackageManager {
7
+ protected options: IPackageManagerOptions;
8
+ private readonly logger;
9
+ private loaders;
10
+ private docModelsMap;
11
+ private subgraphsMap;
12
+ private processorMap;
13
+ private configWatcher;
14
+ private eventEmitter;
15
+ constructor(loaders: IPackageLoader[], options: IPackageManagerOptions);
16
+ init(): Promise<PackageManagerResult>;
17
+ private loadPackages;
18
+ private getAllPackageNames;
19
+ private getPackageNamesFromConfigFile;
20
+ private initConfigFileWatcher;
21
+ private updatePackagesMap;
22
+ private updateSubgraphsMap;
23
+ private updateProcessorsMap;
24
+ onDocumentModelsChange(handler: (documentModels: Record<string, DocumentModelModule[]>) => void): void;
25
+ onSubgraphsChange(handler: (subgraphs: Map<string, SubgraphClass[]>) => void): void;
26
+ onProcessorsChange(handler: (processors: Map<string, ((module: any) => ProcessorFactory)[]>) => void): void;
27
+ }
28
+ //# sourceMappingURL=package-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package-manager.d.ts","sourceRoot":"","sources":["../../../src/packages/package-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAA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AE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,gBAAgB,CAAC,EAAE,CAAC,KAC3D,IAAI,GACR,IAAI;CAGR"}
@@ -0,0 +1,135 @@
1
+ import { getConfig } from "@powerhousedao/config";
2
+ import { childLogger, driveDocumentModelModule } from "document-drive";
3
+ import { documentModelDocumentModelModule, } from "document-model";
4
+ import EventEmitter from "node:events";
5
+ import { watchFile } from "node:fs";
6
+ export function getUniqueDocumentModels(...documentModels) {
7
+ const uniqueModels = new Map();
8
+ for (const models of documentModels) {
9
+ for (const model of models) {
10
+ uniqueModels.set(model.documentModel.id, model);
11
+ }
12
+ }
13
+ return Array.from(uniqueModels.values());
14
+ }
15
+ export class PackageManager {
16
+ options;
17
+ logger = childLogger(["reactor-api", "package-manager"]);
18
+ loaders;
19
+ docModelsMap = new Map();
20
+ subgraphsMap = new Map();
21
+ processorMap = new Map();
22
+ configWatcher;
23
+ eventEmitter = new EventEmitter();
24
+ constructor(loaders, options) {
25
+ this.options = options;
26
+ this.loaders = loaders;
27
+ this.eventEmitter.setMaxListeners(0);
28
+ }
29
+ async init() {
30
+ if (this.options.configFile) {
31
+ this.initConfigFileWatcher(this.options.configFile);
32
+ }
33
+ return await this.loadPackages(this.getAllPackageNames());
34
+ }
35
+ async loadPackages(packages) {
36
+ this.logger.info(`Loading packages: ${packages.join(", ")}`);
37
+ const documentModelModuleMap = new Map();
38
+ const subgraphsMap = new Map();
39
+ const processorsMap = new Map();
40
+ // static prereqs
41
+ documentModelModuleMap.set("document-drive", [
42
+ driveDocumentModelModule,
43
+ ]);
44
+ documentModelModuleMap.set("document-model", [
45
+ documentModelDocumentModelModule,
46
+ ]);
47
+ for (const pkg of packages) {
48
+ const allDocumentModels = [];
49
+ const allSubgraphs = [];
50
+ const allProcessors = [];
51
+ for (const loader of this.loaders) {
52
+ const documentModels = await loader.loadDocumentModels(pkg);
53
+ const subgraphs = await loader.loadSubgraphs(pkg);
54
+ const processors = await loader.loadProcessors(pkg);
55
+ allDocumentModels.push(...documentModels);
56
+ allSubgraphs.push(...subgraphs);
57
+ allProcessors.push(processors);
58
+ }
59
+ documentModelModuleMap.set(pkg, allDocumentModels);
60
+ subgraphsMap.set(pkg, allSubgraphs);
61
+ processorsMap.set(pkg, allProcessors);
62
+ }
63
+ this.updatePackagesMap(documentModelModuleMap);
64
+ this.updateSubgraphsMap(subgraphsMap);
65
+ this.updateProcessorsMap(processorsMap);
66
+ return {
67
+ documentModels: getUniqueDocumentModels(...Array.from(documentModelModuleMap.values())),
68
+ subgraphs: subgraphsMap,
69
+ processors: processorsMap,
70
+ };
71
+ }
72
+ getAllPackageNames() {
73
+ const packageNames = this.options.packages ?? [];
74
+ if (this.options.configFile) {
75
+ packageNames.push(...this.getPackageNamesFromConfigFile(this.options.configFile));
76
+ }
77
+ return packageNames;
78
+ }
79
+ getPackageNamesFromConfigFile(configFile) {
80
+ const loadedConfig = getConfig(configFile);
81
+ return loadedConfig.packages?.map((pkg) => pkg.packageName) ?? [];
82
+ }
83
+ initConfigFileWatcher(configFile) {
84
+ if (!this.configWatcher) {
85
+ this.configWatcher = watchFile(configFile, { interval: 100 }, (curr, prev) => {
86
+ if (curr.mtime === prev.mtime) {
87
+ return;
88
+ }
89
+ void this.loadPackages(this.getAllPackageNames());
90
+ });
91
+ }
92
+ }
93
+ updatePackagesMap(packagesMap) {
94
+ const oldPackages = Array.from(this.docModelsMap.keys());
95
+ const newPackages = Array.from(packagesMap.keys());
96
+ oldPackages
97
+ .filter((pkg) => !newPackages.includes(pkg))
98
+ .forEach((pkg) => {
99
+ this.logger.info(`> Removed package: ${pkg}`);
100
+ });
101
+ this.docModelsMap = packagesMap;
102
+ this.eventEmitter.emit("documentModelsChange", Object.fromEntries(packagesMap));
103
+ }
104
+ updateSubgraphsMap(subgraphsMap) {
105
+ const oldPackages = Array.from(this.subgraphsMap.keys());
106
+ const newPackages = Array.from(subgraphsMap.keys());
107
+ oldPackages
108
+ .filter((pkg) => !newPackages.includes(pkg))
109
+ .forEach((pkg) => {
110
+ this.logger.info(`> Removed Subgraphs from: ${pkg}`);
111
+ });
112
+ this.subgraphsMap = subgraphsMap;
113
+ this.eventEmitter.emit("subgraphsChange", subgraphsMap);
114
+ }
115
+ updateProcessorsMap(processorsMap) {
116
+ const oldPackages = Array.from(this.processorMap.keys());
117
+ const newPackages = Array.from(processorsMap.keys());
118
+ oldPackages
119
+ .filter((pkg) => !newPackages.includes(pkg))
120
+ .forEach((pkg) => {
121
+ this.logger.info(`> Removed Processor Factories from: ${pkg}`);
122
+ });
123
+ this.processorMap = processorsMap;
124
+ this.eventEmitter.emit("processorsChange", processorsMap);
125
+ }
126
+ onDocumentModelsChange(handler) {
127
+ this.eventEmitter.on("documentModelsChange", handler);
128
+ }
129
+ onSubgraphsChange(handler) {
130
+ this.eventEmitter.on("subgraphsChange", handler);
131
+ }
132
+ onProcessorsChange(handler) {
133
+ this.eventEmitter.on("processorsChange", handler);
134
+ }
135
+ }
@@ -0,0 +1,27 @@
1
+ import { SubgraphClass } from "#graphql/index.js";
2
+ import { ProcessorFactory } from "document-drive/processors/types";
3
+ import { DocumentModelModule } from "document-model";
4
+ export interface IPackageLoader {
5
+ loadDocumentModels(identifier: string): Promise<DocumentModelModule[]>;
6
+ loadSubgraphs(identifier: string): Promise<SubgraphClass[]>;
7
+ loadProcessors(identifier: string): Promise<(module: any) => ProcessorFactory>;
8
+ }
9
+ export interface IPackageManager {
10
+ onDocumentModelsChange(handler: (documentModels: Record<string, DocumentModelModule[]>) => void): void;
11
+ }
12
+ export type IPackageManagerOptions = {
13
+ packages?: string[];
14
+ configFile?: string;
15
+ };
16
+ export interface PackageConfig {
17
+ packageName: string;
18
+ }
19
+ export interface PowerhouseConfig {
20
+ packages?: PackageConfig[];
21
+ }
22
+ export type PackageManagerResult = {
23
+ documentModels: DocumentModelModule[];
24
+ subgraphs: Map<string, SubgraphClass[]>;
25
+ processors: Map<string, ((module: any) => ProcessorFactory)[]>;
26
+ };
27
+ //# sourceMappingURL=types.d.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0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export declare const installPackages: (packages: string[]) => Promise<void>;
2
+ export declare const readManifest: () => string;
3
+ export declare function loadDependency(packageName: string, subPath: string): Promise<unknown>;
4
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/packages/util.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,eAAe,GAAU,UAAU,MAAM,EAAE,kBAIvD,CAAC;AAEF,eAAO,MAAM,YAAY,cAGxB,CAAC;AAEF,wBAAsB,cAAc,CAClC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,OAAO,CAAC,CAUlB"}
@@ -0,0 +1,21 @@
1
+ import { execSync } from "node:child_process";
2
+ export const installPackages = async (packages) => {
3
+ for (const packageName of packages) {
4
+ execSync(`ph install ${packageName}`);
5
+ }
6
+ };
7
+ export const readManifest = () => {
8
+ const manifest = execSync(`ph manifest`).toString();
9
+ return manifest;
10
+ };
11
+ export async function loadDependency(packageName, subPath) {
12
+ try {
13
+ const fullPath = `${packageName}/${subPath}`;
14
+ // vite does not support this, but that's okay as we have provided the
15
+ // vite-loader for this purpose
16
+ return await import(/* @vite-ignore */ fullPath);
17
+ }
18
+ catch (e) {
19
+ return null;
20
+ }
21
+ }
@@ -1,3 +1,4 @@
1
+ import { type IPackageLoader } from "#packages/types.js";
1
2
  import { type PGlite } from "@electric-sql/pglite";
2
3
  import { type IDocumentDriveServer } from "document-drive";
3
4
  import { type Express } from "express";
@@ -14,7 +15,17 @@ type Options = {
14
15
  keyPath: string;
15
16
  certPath: string;
16
17
  } | boolean | undefined;
18
+ packageLoader?: IPackageLoader;
17
19
  };
20
+ /**
21
+ * Starts the API server.
22
+ *
23
+ * @param reactor - The document drive server.
24
+ * @param options - Additional options for server configuration. These options intended to be serializable.
25
+ * @param overrides - System overrides. These overrides are intended to be object references.
26
+ *
27
+ * @returns The API server.
28
+ */
18
29
  export declare function startAPI(reactor: IDocumentDriveServer, options: Options): Promise<API>;
19
30
  export {};
20
31
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAOnD,OAAO,EAAE,KAAK,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAM3D,OAAgB,EAAE,KAAK,OAAO,EAAE,MAAM,SAAS,CAAC;AAMhD,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,IAAI,CAAC;AAC/B,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,YAAY,CAAC;AAGtC,KAAK,OAAO,GAAG;IACb,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,IAAI,GAAG,SAAS,CAAC;IAC1C,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;IACpB,KAAK,CAAC,EACF;QACE,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;KAClB,GACD,OAAO,GACP,SAAS,CAAC;CACf,CAAC;AAsKF,wBAAsB,QAAQ,CAC5B,OAAO,EAAE,oBAAoB,EAC7B,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,GAAG,CAAC,CAuDd"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAOnD,OAAO,EAGL,KAAK,oBAAoB,EAC1B,MAAM,gBAAgB,CAAC;A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