@powerhousedao/reactor-local 1.27.29-staging.0 → 1.27.30-dev.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/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- export { DefaultStartServerOptions, startServer, type LocalReactor, type StartServerOptions, } from "./src/server.js";
1
+ export { startServer } from "./src/server.js";
2
+ export { DefaultStartServerOptions, type LocalReactor, type StartServerOptions, } from "./src/types.js";
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,yBAAyB,EACzB,WAAW,EACX,KAAK,YAAY,EACjB,KAAK,kBAAkB,GACxB,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EACL,yBAAyB,EACzB,KAAK,YAAY,EACjB,KAAK,kBAAkB,GACxB,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- export { DefaultStartServerOptions, startServer, } from "./src/server.js";
1
+ export { startServer } from "./src/server.js";
2
+ export { DefaultStartServerOptions, } from "./src/types.js";
@@ -1,50 +1,4 @@
1
- import { type DriveInput, type IDocumentDriveServer } from "document-drive";
2
- export type StorageOptions = {
3
- type: "filesystem" | "memory" | "postgres" | "browser";
4
- filesystemPath?: string;
5
- postgresUrl?: string;
6
- };
7
- export type StartServerOptions = {
8
- configFile?: string;
9
- dev?: boolean;
10
- port?: string | number;
11
- storage?: StorageOptions;
12
- dbPath?: string;
13
- drive?: DriveInput;
14
- packages?: string[];
15
- https?: {
16
- keyPath: string;
17
- certPath: string;
18
- } | boolean | undefined;
19
- logLevel?: "info" | "warn" | "error" | "debug" | "verbose" | "silent";
20
- };
21
- export declare const DefaultStartServerOptions: {
22
- port: number;
23
- storage: {
24
- type: "filesystem";
25
- filesystemPath: string;
26
- };
27
- dbPath: string;
28
- drive: {
29
- global: {
30
- id: string;
31
- name: string;
32
- icon: string;
33
- slug: string;
34
- };
35
- local: {
36
- availableOffline: true;
37
- listeners: never[];
38
- sharingType: string;
39
- triggers: never[];
40
- };
41
- };
42
- };
43
- export type LocalReactor = {
44
- driveUrl: string;
45
- getDocumentPath: (driveId: string, documentId: string) => string;
46
- server: IDocumentDriveServer;
47
- };
1
+ import { LocalReactor, StartServerOptions } from "./types.js";
48
2
  declare const startServer: (options?: StartServerOptions) => Promise<LocalReactor>;
49
3
  export { startServer };
50
4
  //# sourceMappingURL=server.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAOA,OAAO,EAGL,KAAK,UAAU,EACf,KAAK,oBAAoB,EAK1B,MAAM,gBAAgB,CAAC;AAyBxB,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,YAAY,GAAG,QAAQ,GAAG,UAAU,GAAG,SAAS,CAAC;IACvD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,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;IACd,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,SAAS,GAAG,QAAQ,CAAC;CACvE,CAAC;AAEF,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;CAqBR,CAAC;AAE/B,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;IACjE,MAAM,EAAE,oBAAoB,CAAC;CAC9B,CAAC;AAoCF,QAAA,MAAM,WAAW,GACf,UAAU,kBAAkB,KAC3B,OAAO,CAAC,YAAY,CAoEtB,CAAC;AAsGF,OAAO,EAAE,WAAW,EAAE,CAAC"}
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAIA,OAAO,EAEL,YAAY,EACZ,kBAAkB,EACnB,MAAM,YAAY,CAAC;AAMpB,QAAA,MAAM,WAAW,GACf,UAAU,kBAAkB,KAC3B,OAAO,CAAC,YAAY,CAsEtB,CAAC;AAEF,OAAO,EAAE,WAAW,EAAE,CAAC"}
@@ -1,67 +1,14 @@
1
- import { viteCommonjs } from "@originjs/vite-plugin-commonjs";
2
- import { isSubgraphClass, startAPI, } from "@powerhousedao/reactor-api";
3
- import { DriveAlreadyExistsError, driveDocumentModelModule, InMemoryCache, logger, MemoryStorage, ReactorBuilder, } from "document-drive";
4
- import { BrowserStorage } from "document-drive/storage/browser";
5
- import { FilesystemStorage } from "document-drive/storage/filesystem";
6
- import { PrismaStorageFactory } from "document-drive/storage/prisma/factory";
7
- import { documentModelDocumentModelModule, } from "document-model";
1
+ import { startAPI } from "@powerhousedao/reactor-api";
2
+ import { InMemoryCache, logger, ReactorBuilder } from "document-drive";
8
3
  import dotenv from "dotenv";
9
- import { access } from "node:fs/promises";
10
4
  import path from "node:path";
11
- import { createServer as createViteServer } from "vite";
12
- const dirname = process.cwd();
5
+ import { DefaultStartServerOptions, } from "./types.js";
6
+ import { addDefaultDrive, createStorage, startViteServer } from "./util.js";
7
+ import { VitePackageLoader } from "./vite-loader.js";
13
8
  dotenv.config();
14
- export const DefaultStartServerOptions = {
15
- port: 4001,
16
- storage: {
17
- type: "filesystem",
18
- filesystemPath: path.join(dirname, ".ph/file-storage"),
19
- },
20
- dbPath: path.join(dirname, ".ph/read-model.db"),
21
- drive: {
22
- global: {
23
- id: "powerhouse",
24
- name: "Powerhouse",
25
- icon: "https://ipfs.io/ipfs/QmcaTDBYn8X2psGaXe7iQ6qd8q6oqHLgxvMX9yXf7f9uP7",
26
- slug: "powerhouse",
27
- },
28
- local: {
29
- availableOffline: true,
30
- listeners: [],
31
- sharingType: "public",
32
- triggers: [],
33
- },
34
- },
35
- };
36
- const baseDocumentModelModules = [
37
- documentModelDocumentModelModule,
38
- driveDocumentModelModule,
39
- ];
40
- const createStorage = (options, cache) => {
41
- switch (options.type) {
42
- case "filesystem":
43
- logger.info(`Initializing filesystem storage at '${options.filesystemPath}'.`);
44
- return new FilesystemStorage(options.filesystemPath);
45
- case "memory":
46
- logger.info("Initializing memory storage.");
47
- return new MemoryStorage();
48
- case "postgres": {
49
- if (!options.postgresUrl) {
50
- throw new Error("Postgres url is required");
51
- }
52
- logger.info(`Initializing postgres storage at '${options.postgresUrl}'.`);
53
- const storageFactory = new PrismaStorageFactory(options.postgresUrl, cache);
54
- const storage = storageFactory.build();
55
- return storage;
56
- }
57
- case "browser":
58
- logger.info("Initializing browser storage.");
59
- return new BrowserStorage();
60
- }
61
- };
62
9
  const startServer = async (options) => {
63
10
  process.setMaxListeners(0);
64
- const { port, storage, drive, dev, dbPath, packages, configFile, logLevel } = {
11
+ const { port, storage, drive, dev, dbPath, packages = [], configFile, logLevel, } = {
65
12
  ...DefaultStartServerOptions,
66
13
  ...options,
67
14
  };
@@ -69,43 +16,39 @@ const startServer = async (options) => {
69
16
  // be aware: this may not log anything if the log level is above info
70
17
  logger.info(`Setting log level to ${logLevel}.`);
71
18
  const serverPort = Number(process.env.PORT ?? port);
72
- // If dev load local document models
73
- let docModels = [];
74
- const vite = dev
75
- ? await startDevServer()
76
- : undefined;
19
+ // start vite server if dev
20
+ const vite = dev ? await startViteServer() : undefined;
21
+ // get paths to local document models
77
22
  if (vite) {
78
- const documentModelsPath = path.join(process.cwd(), "./document-models"); // TODO get path from powerhouse config
79
- docModels = joinDocumentModelModules((await loadDocumentModels(documentModelsPath, vite)) ?? [], baseDocumentModelModules);
23
+ // TODO get path from powerhouse config
24
+ const basePath = process.cwd();
25
+ packages.push(basePath);
80
26
  }
81
- // start document drive server with all available document models & storage
27
+ // create document drive server with all available document models & storage
82
28
  const cache = new InMemoryCache();
83
- const driveServer = new ReactorBuilder(docModels)
29
+ const driveServer = new ReactorBuilder([])
84
30
  .withCache(cache)
85
31
  .withStorage(createStorage(storage, cache))
86
32
  .build();
87
- // init drive server
33
+ // init drive server + add a default drive
88
34
  await driveServer.initialize();
89
35
  const driveUrl = await addDefaultDrive(driveServer, drive, serverPort);
36
+ // create loader
37
+ let packageLoader = vite ? new VitePackageLoader(vite) : undefined;
90
38
  // start api
91
- const packageOptions = packages?.length
92
- ? { packages }
93
- : configFile
94
- ? { configFile }
95
- : { packages: [] };
96
39
  const api = await startAPI(driveServer, {
97
40
  port: serverPort,
98
41
  dbPath,
99
42
  https: options?.https,
100
- ...packageOptions,
43
+ packageLoader,
44
+ configFile,
45
+ packages,
101
46
  });
47
+ // add vite middleware after express app is initialized if applicable
102
48
  if (vite) {
103
49
  api.app.use(vite.middlewares);
104
- // load local subgraphs
105
- const subgraphsPath = path.join(process.cwd(), "./subgraphs"); // TODO get path from powerhouse config
106
- await loadSubgraphs(subgraphsPath, vite, api.graphqlManager);
107
50
  }
108
- console.log(` ➜ Reactor: ${driveUrl}`);
51
+ logger.info(` ➜ Reactor: ${driveUrl}`);
109
52
  return {
110
53
  driveUrl,
111
54
  getDocumentPath: (driveId, documentId) => {
@@ -114,88 +57,4 @@ const startServer = async (options) => {
114
57
  server: driveServer,
115
58
  };
116
59
  };
117
- async function addDefaultDrive(driveServer, drive, serverPort) {
118
- let driveId = drive.global.slug ?? drive.global.id;
119
- try {
120
- // add default drive
121
- const driveDoc = await driveServer.addDrive(drive);
122
- driveId = driveDoc.state.global.slug ?? driveDoc.state.global.id;
123
- }
124
- catch (e) {
125
- if (e instanceof DriveAlreadyExistsError) {
126
- if (driveId) {
127
- const driveDoc = await (drive.global.slug
128
- ? driveServer.getDriveBySlug(drive.global.slug)
129
- : driveServer.getDrive(driveId));
130
- driveId = driveDoc.state.global.slug ?? driveDoc.state.global.id;
131
- }
132
- }
133
- else {
134
- throw e;
135
- }
136
- }
137
- const driveUrl = `http://localhost:${serverPort}/${driveId ? `d/${drive.global.slug ?? drive.global.id}` : ""}`;
138
- return driveUrl;
139
- }
140
- async function startDevServer() {
141
- const vite = await createViteServer({
142
- server: { middlewareMode: true, watch: null },
143
- appType: "custom",
144
- build: {
145
- rollupOptions: {
146
- input: [],
147
- },
148
- },
149
- plugins: [viteCommonjs()],
150
- });
151
- return vite;
152
- }
153
- async function loadDocumentModels(path, vite) {
154
- try {
155
- console.log("> Loading document models from", path);
156
- await access(path);
157
- const localDMs = (await vite.ssrLoadModule(path));
158
- const localDocumentModelModules = Object.values(localDMs);
159
- return localDocumentModelModules;
160
- }
161
- catch (e) {
162
- if (e.code === "ENOENT") {
163
- console.warn("No local document models found");
164
- }
165
- else {
166
- console.error("Error loading document models", e);
167
- }
168
- }
169
- }
170
- async function loadSubgraphs(path, vite, graphqlManager) {
171
- try {
172
- console.log("> Loading subgraphs from", path);
173
- await access(path);
174
- const localSubgraphs = await vite.ssrLoadModule(path);
175
- for (const [name, subgraph] of Object.entries(localSubgraphs)) {
176
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
177
- const SubgraphClass = subgraph[name];
178
- if (isSubgraphClass(SubgraphClass)) {
179
- await graphqlManager.registerSubgraph(SubgraphClass, "graphql");
180
- }
181
- }
182
- await graphqlManager.updateRouter();
183
- }
184
- catch (e) {
185
- if (e.code === "ENOENT") {
186
- console.warn("No local document models found");
187
- }
188
- else {
189
- console.error("Error loading subgraphs", e);
190
- }
191
- }
192
- }
193
- function joinDocumentModelModules(...modules) {
194
- return modules
195
- .flat()
196
- .toReversed()
197
- .reduce((acc, curr) => acc.find((dm) => dm.documentModel.id === curr.documentModel.id)
198
- ? acc
199
- : [...acc, curr], []);
200
- }
201
60
  export { startServer };
@@ -0,0 +1,48 @@
1
+ import { DriveInput, IDocumentDriveServer } from "document-drive";
2
+ export type StorageOptions = {
3
+ type: "filesystem" | "memory" | "postgres" | "browser";
4
+ filesystemPath?: string;
5
+ postgresUrl?: string;
6
+ };
7
+ export type StartServerOptions = {
8
+ configFile?: string;
9
+ dev?: boolean;
10
+ port?: string | number;
11
+ storage?: StorageOptions;
12
+ dbPath?: string;
13
+ drive?: DriveInput;
14
+ packages?: string[];
15
+ https?: {
16
+ keyPath: string;
17
+ certPath: string;
18
+ } | boolean | undefined;
19
+ logLevel?: "info" | "warn" | "error" | "debug" | "verbose" | "silent";
20
+ };
21
+ export declare const DefaultStartServerOptions: {
22
+ port: number;
23
+ storage: {
24
+ type: "filesystem";
25
+ filesystemPath: string;
26
+ };
27
+ dbPath: string;
28
+ drive: {
29
+ global: {
30
+ id: string;
31
+ name: string;
32
+ icon: string;
33
+ slug: string;
34
+ };
35
+ local: {
36
+ availableOffline: true;
37
+ listeners: never[];
38
+ sharingType: string;
39
+ triggers: never[];
40
+ };
41
+ };
42
+ };
43
+ export type LocalReactor = {
44
+ driveUrl: string;
45
+ getDocumentPath: (driveId: string, documentId: string) => string;
46
+ server: IDocumentDriveServer;
47
+ };
48
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAGlE,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,YAAY,GAAG,QAAQ,GAAG,UAAU,GAAG,SAAS,CAAC;IACvD,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,cAAc,CAAC;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,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;IACd,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,GAAG,SAAS,GAAG,QAAQ,CAAC;CACvE,CAAC;AAEF,eAAO,MAAM,yBAAyB;;;;;;;;;;;;;;;;;;;;;CAqBR,CAAC;AAE/B,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,MAAM,CAAC;IACjB,eAAe,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,KAAK,MAAM,CAAC;IACjE,MAAM,EAAE,oBAAoB,CAAC;CAC9B,CAAC"}
@@ -0,0 +1,23 @@
1
+ import path from "node:path";
2
+ export const DefaultStartServerOptions = {
3
+ port: 4001,
4
+ storage: {
5
+ type: "filesystem",
6
+ filesystemPath: path.join(process.cwd(), ".ph/file-storage"),
7
+ },
8
+ dbPath: path.join(process.cwd(), ".ph/read-model.db"),
9
+ drive: {
10
+ global: {
11
+ id: "powerhouse",
12
+ name: "Powerhouse",
13
+ icon: "https://ipfs.io/ipfs/QmcaTDBYn8X2psGaXe7iQ6qd8q6oqHLgxvMX9yXf7f9uP7",
14
+ slug: "powerhouse",
15
+ },
16
+ local: {
17
+ availableOffline: true,
18
+ listeners: [],
19
+ sharingType: "public",
20
+ triggers: [],
21
+ },
22
+ },
23
+ };
@@ -0,0 +1,8 @@
1
+ import { DriveInput, IDocumentDriveServer } from "document-drive";
2
+ import { ICache } from "document-drive/cache/types";
3
+ import { IDriveStorage } from "document-drive/storage/types";
4
+ import { StorageOptions } from "./types.js";
5
+ export declare const createStorage: (options: StorageOptions, cache: ICache) => IDriveStorage;
6
+ export declare function addDefaultDrive(driveServer: IDocumentDriveServer, drive: DriveInput, serverPort: number): Promise<string>;
7
+ export declare function startViteServer(): Promise<import("vite").ViteDevServer>;
8
+ //# sourceMappingURL=util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/util.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAC;AAIpD,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAC;AAE7D,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,eAAO,MAAM,aAAa,GACxB,SAAS,cAAc,EACvB,OAAO,MAAM,KACZ,aA2BF,CAAC;AAEF,wBAAsB,eAAe,CACnC,WAAW,EAAE,oBAAoB,EACjC,KAAK,EAAE,UAAU,EACjB,UAAU,EAAE,MAAM,mBAsBnB;AAED,wBAAsB,eAAe,0CAapC"}
@@ -0,0 +1,64 @@
1
+ import { DriveAlreadyExistsError, logger, MemoryStorage } from "document-drive";
2
+ import { viteCommonjs } from "@originjs/vite-plugin-commonjs";
3
+ import { BrowserStorage } from "document-drive/storage/browser";
4
+ import { FilesystemStorage } from "document-drive/storage/filesystem";
5
+ import { PrismaStorageFactory } from "document-drive/storage/prisma/factory";
6
+ import { createServer } from "vite";
7
+ export const createStorage = (options, cache) => {
8
+ switch (options.type) {
9
+ case "filesystem":
10
+ logger.info(`Initializing filesystem storage at '${options.filesystemPath}'.`);
11
+ return new FilesystemStorage(options.filesystemPath);
12
+ case "postgres": {
13
+ if (!options.postgresUrl) {
14
+ throw new Error("Postgres url is required");
15
+ }
16
+ logger.info(`Initializing postgres storage at '${options.postgresUrl}'.`);
17
+ const storageFactory = new PrismaStorageFactory(options.postgresUrl, cache);
18
+ const storage = storageFactory.build();
19
+ return storage;
20
+ }
21
+ case "browser":
22
+ logger.info("Initializing browser storage.");
23
+ return new BrowserStorage();
24
+ default:
25
+ logger.info("Initializing memory storage.");
26
+ return new MemoryStorage();
27
+ }
28
+ };
29
+ export async function addDefaultDrive(driveServer, drive, serverPort) {
30
+ let driveId = drive.global.slug ?? drive.global.id;
31
+ try {
32
+ // add default drive
33
+ const driveDoc = await driveServer.addDrive(drive);
34
+ driveId = driveDoc.state.global.slug ?? driveDoc.state.global.id;
35
+ }
36
+ catch (e) {
37
+ if (e instanceof DriveAlreadyExistsError) {
38
+ if (driveId) {
39
+ const driveDoc = await (drive.global.slug
40
+ ? driveServer.getDriveBySlug(drive.global.slug)
41
+ : driveServer.getDrive(driveId));
42
+ driveId = driveDoc.state.global.slug ?? driveDoc.state.global.id;
43
+ }
44
+ }
45
+ else {
46
+ throw e;
47
+ }
48
+ }
49
+ const driveUrl = `http://localhost:${serverPort}/${driveId ? `d/${drive.global.slug ?? drive.global.id}` : ""}`;
50
+ return driveUrl;
51
+ }
52
+ export async function startViteServer() {
53
+ const vite = await createServer({
54
+ server: { middlewareMode: true, watch: null },
55
+ appType: "custom",
56
+ build: {
57
+ rollupOptions: {
58
+ input: [],
59
+ },
60
+ },
61
+ plugins: [viteCommonjs()],
62
+ });
63
+ return vite;
64
+ }
@@ -0,0 +1,13 @@
1
+ import { IPackageLoader, type SubgraphClass } from "@powerhousedao/reactor-api";
2
+ import { ProcessorFactory } from "document-drive/processors/types";
3
+ import { DocumentModelModule } from "document-model";
4
+ import { ViteDevServer } from "vite";
5
+ export declare class VitePackageLoader implements IPackageLoader {
6
+ private readonly logger;
7
+ private readonly vite;
8
+ constructor(vite: ViteDevServer);
9
+ loadDocumentModels(identifier: string): Promise<DocumentModelModule[]>;
10
+ loadSubgraphs(identifier: string): Promise<SubgraphClass[]>;
11
+ loadProcessors(identifier: string): Promise<(module: any) => ProcessorFactory>;
12
+ }
13
+ //# sourceMappingURL=vite-loader.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-loader.d.ts","sourceRoot":"","sources":["../../src/vite-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAEd,KAAK,aAAa,EACnB,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAGrD,OAAO,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAErC,qBAAa,iBAAkB,YAAW,cAAc;IACtD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAiD;IAExE,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgB;gBAEzB,IAAI,EAAE,aAAa;IAIzB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,EAAE,CAAC;IAmCtE,aAAa,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,CAAC;IAgC3D,cAAc,CAClB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,gBAAgB,CAAC;CA0B9C"}
@@ -0,0 +1,74 @@
1
+ import { isSubgraphClass, } from "@powerhousedao/reactor-api";
2
+ import { childLogger } from "document-drive";
3
+ import { access } from "node:fs/promises";
4
+ import path from "node:path";
5
+ export class VitePackageLoader {
6
+ logger = childLogger(["reactor-local", "vite-loader"]);
7
+ vite;
8
+ constructor(vite) {
9
+ this.vite = vite;
10
+ }
11
+ async loadDocumentModels(identifier) {
12
+ const fullPath = path.join(identifier, "./document-models");
13
+ this.logger.verbose("Loading document models from", fullPath);
14
+ try {
15
+ await access(fullPath);
16
+ const localDMs = (await this.vite.ssrLoadModule(fullPath));
17
+ const exports = Object.values(localDMs);
18
+ // duck type
19
+ const documentModels = [];
20
+ for (const dm of exports) {
21
+ if (dm.documentModel) {
22
+ documentModels.push(dm);
23
+ }
24
+ }
25
+ this.logger.verbose(` ➜ Loaded ${documentModels.length} Document Models from: ${identifier}`);
26
+ return documentModels;
27
+ }
28
+ catch (e) {
29
+ this.logger.verbose(` ➜ No Document Models found for: ${identifier}`);
30
+ }
31
+ return [];
32
+ }
33
+ async loadSubgraphs(identifier) {
34
+ const fullPath = path.join(identifier, "./subgraphs");
35
+ this.logger.verbose("Loading subgraphs from", fullPath);
36
+ let localSubgraphs = {};
37
+ try {
38
+ await access(fullPath);
39
+ localSubgraphs = await this.vite.ssrLoadModule(fullPath);
40
+ }
41
+ catch (e) {
42
+ this.logger.verbose(` ➜ No Subgraphs found for: ${identifier}`);
43
+ return [];
44
+ }
45
+ const subgraphs = [];
46
+ for (const [name, subgraph] of Object.entries(localSubgraphs)) {
47
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
48
+ const SubgraphClass = subgraph[name];
49
+ if (isSubgraphClass(SubgraphClass)) {
50
+ subgraphs.push(SubgraphClass);
51
+ }
52
+ }
53
+ this.logger.verbose(` ➜ Loaded ${subgraphs.length} Subgraphs from: ${identifier}`);
54
+ return subgraphs;
55
+ }
56
+ async loadProcessors(identifier) {
57
+ const fullPath = path.join(identifier, "./processors");
58
+ this.logger.verbose("Loading processors from", fullPath);
59
+ try {
60
+ await access(fullPath);
61
+ const module = await this.vite.ssrLoadModule(fullPath);
62
+ if (module.processorFactory) {
63
+ this.logger.verbose(` ➜ Loaded Processor factory from: ${identifier}`);
64
+ return module.processorFactory;
65
+ }
66
+ }
67
+ catch (e) {
68
+ //
69
+ }
70
+ this.logger.verbose(` ➜ No Processor Factory found for: ${identifier}`);
71
+ // return empty processor factory
72
+ return () => () => [];
73
+ }
74
+ }