@powerhousedao/reactor-local 1.3.4 → 1.4.1

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.
@@ -1,6 +1,7 @@
1
- import { fileURLToPath } from 'url';
2
- import path2 from 'path';
3
- import { startAPI, setAdditionalContextFields, registerInternalListener, addSubgraph, createSchema } from '@powerhousedao/reactor-api';
1
+ import path2 from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import dotenv from 'dotenv';
4
+ import { startAPI } from '@powerhousedao/reactor-api';
4
5
  import * as DocumentDrive from 'document-model-libs/document-drive';
5
6
  import { isFileNode, utils as utils$1, actions, documentModel } from 'document-model-libs/document-drive';
6
7
  import { utils } from 'document-model/document';
@@ -12,17 +13,12 @@ import { GraphQLError, buildSchema, GraphQLObjectType, GraphQLNonNull, GraphQLUn
12
13
  import stringify from 'json-stringify-deterministic';
13
14
  import { readdirSync, existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
14
15
  import fs from 'fs/promises';
16
+ import path from 'path';
15
17
  import sanitize from 'sanitize-filename';
16
18
  import * as DocumentModelsLibs from 'document-model-libs/document-models';
17
19
  import { module } from 'document-model/document-model';
18
- import dotenv from 'dotenv';
19
- import { drizzle } from 'drizzle-orm/connect';
20
- import * as searchListener from '@powerhousedao/general-document-indexer';
21
20
 
22
- // ../../node_modules/.pnpm/tsup@8.3.5_@swc+core@1.5.29_postcss@8.4.48_typescript@5.6.3/node_modules/tsup/assets/esm_shims.js
23
- var getFilename = () => fileURLToPath(import.meta.url);
24
- var getDirname = () => path2.dirname(getFilename());
25
- var __dirname = /* @__PURE__ */ getDirname();
21
+ // src/server.ts
26
22
 
27
23
  // ../document-drive/src/cache/memory.ts
28
24
  var InMemoryCache = class {
@@ -534,7 +530,7 @@ async function requestGraphql(...args) {
534
530
  const result = { ...response };
535
531
  if (errors?.length) {
536
532
  result.errors = errors.map(
537
- ({ message, ...options2 }) => new GraphQLError(message, options2)
533
+ ({ message, ...options }) => new GraphQLError(message, options)
538
534
  );
539
535
  }
540
536
  return result;
@@ -567,14 +563,14 @@ function getFields(type) {
567
563
  }
568
564
  return "";
569
565
  }
570
- function generateDocumentStateQueryFields(documentModel2, options2) {
566
+ function generateDocumentStateQueryFields(documentModel2, options) {
571
567
  const name = pascalCase(documentModel2.name);
572
568
  const spec = documentModel2.specifications.at(-1);
573
569
  if (!spec) {
574
570
  throw new Error("No document model specification found");
575
571
  }
576
572
  const source = `${spec.state.global.schema} type Query { ${name}: ${name}State }`;
577
- const schema = buildSchema(source, options2);
573
+ const schema = buildSchema(source, options);
578
574
  const queryType = schema.getQueryType();
579
575
  if (!queryType) {
580
576
  throw new Error("No query type found");
@@ -795,8 +791,8 @@ var ReadModeService = class {
795
791
  }
796
792
  return document;
797
793
  }
798
- async addReadDrive(url, options2) {
799
- const { id } = options2?.expectedDriveInfo ?? await requestPublicDrive(url);
794
+ async addReadDrive(url, options) {
795
+ const { id } = options?.expectedDriveInfo ?? await requestPublicDrive(url);
800
796
  const result = await this.#fetchDrive(id, url);
801
797
  if (result instanceof Error) {
802
798
  throw result;
@@ -806,7 +802,7 @@ var ReadModeService = class {
806
802
  this.#drives.set(id, {
807
803
  drive: result,
808
804
  context: {
809
- ...options2,
805
+ ...options,
810
806
  url
811
807
  }
812
808
  });
@@ -877,8 +873,8 @@ function ReadModeServer(Base) {
877
873
  getReadDriveContext(id) {
878
874
  return this.#readModeStorage.getReadDriveContext(id);
879
875
  }
880
- async addReadDrive(url, options2) {
881
- await this.#readModeStorage.addReadDrive(url, options2);
876
+ async addReadDrive(url, options) {
877
+ await this.#readModeStorage.addReadDrive(url, options);
882
878
  this.#notifyListeners(await this.#buildDrives(), "add");
883
879
  }
884
880
  fetchDrive(id) {
@@ -898,14 +894,14 @@ function ReadModeServer(Base) {
898
894
  }
899
895
  this.#notifyListeners(await this.#buildDrives(), "delete");
900
896
  }
901
- async migrateReadDrive(id, options2) {
897
+ async migrateReadDrive(id, options) {
902
898
  const result = await this.getReadDriveContext(id);
903
899
  if (result instanceof Error) {
904
900
  return result;
905
901
  }
906
902
  const { url, ...readOptions } = result;
907
903
  try {
908
- const newDrive = await this.addRemoteDrive(url, options2);
904
+ const newDrive = await this.addRemoteDrive(url, options);
909
905
  return newDrive;
910
906
  } catch (error) {
911
907
  logger.error(error);
@@ -1073,18 +1069,18 @@ function isReadModeDriveServer(obj) {
1073
1069
  return typeof obj.getReadDrives === "function";
1074
1070
  }
1075
1071
  var DefaultDrivesManager = class {
1076
- constructor(server, delegate, options2) {
1072
+ constructor(server, delegate, options) {
1077
1073
  this.server = server;
1078
1074
  this.delegate = delegate;
1079
- if (options2?.defaultDrives.remoteDrives) {
1080
- for (const defaultDrive of options2.defaultDrives.remoteDrives) {
1075
+ if (options?.defaultDrives.remoteDrives) {
1076
+ for (const defaultDrive of options.defaultDrives.remoteDrives) {
1081
1077
  this.defaultRemoteDrives.set(defaultDrive.url, {
1082
1078
  ...defaultDrive,
1083
1079
  status: "PENDING"
1084
1080
  });
1085
1081
  }
1086
1082
  }
1087
- this.removeOldRemoteDrivesConfig = options2?.defaultDrives.removeOldRemoteDrives || {
1083
+ this.removeOldRemoteDrivesConfig = options?.defaultDrives.removeOldRemoteDrives || {
1088
1084
  strategy: "preserve-all"
1089
1085
  };
1090
1086
  }
@@ -1328,10 +1324,10 @@ var BaseListenerManager = class {
1328
1324
  listenerState = /* @__PURE__ */ new Map();
1329
1325
  options;
1330
1326
  transmitters = {};
1331
- constructor(drive, listenerState = /* @__PURE__ */ new Map(), options2 = DefaultListenerManagerOptions) {
1327
+ constructor(drive, listenerState = /* @__PURE__ */ new Map(), options = DefaultListenerManagerOptions) {
1332
1328
  this.drive = drive;
1333
1329
  this.listenerState = listenerState;
1334
- this.options = { ...DefaultListenerManagerOptions, ...options2 };
1330
+ this.options = { ...DefaultListenerManagerOptions, ...options };
1335
1331
  }
1336
1332
  };
1337
1333
 
@@ -1427,11 +1423,11 @@ var PullResponderTransmitter = class _PullResponderTransmitter {
1427
1423
  this.drive = drive;
1428
1424
  this.manager = manager;
1429
1425
  }
1430
- getStrands(options2) {
1426
+ getStrands(options) {
1431
1427
  return this.manager.getStrands(
1432
1428
  this.listener.driveId,
1433
1429
  this.listener.listenerId,
1434
- options2
1430
+ options
1435
1431
  );
1436
1432
  }
1437
1433
  disconnect() {
@@ -1482,7 +1478,7 @@ var PullResponderTransmitter = class _PullResponderTransmitter {
1482
1478
  }
1483
1479
  return result.registerPullResponderListener.listenerId;
1484
1480
  }
1485
- static async pullStrands(driveId, url, listenerId, options2) {
1481
+ static async pullStrands(driveId, url, listenerId, options) {
1486
1482
  const result = await requestGraphql(
1487
1483
  url,
1488
1484
  gql`
@@ -1655,8 +1651,8 @@ var PullResponderTransmitter = class _PullResponderTransmitter {
1655
1651
  }
1656
1652
  };
1657
1653
  }
1658
- static async createPullResponderTrigger(driveId, url, options2) {
1659
- const { pullFilter, pullInterval } = options2;
1654
+ static async createPullResponderTrigger(driveId, url, options) {
1655
+ const { pullFilter, pullInterval } = options;
1660
1656
  const listenerId = await _PullResponderTransmitter.registerPullResponder(
1661
1657
  driveId,
1662
1658
  url,
@@ -2066,7 +2062,7 @@ var ListenerManager = class _ListenerManager extends BaseListenerManager {
2066
2062
  if (!listener) throw new Error("Listener not found");
2067
2063
  return Promise.resolve(listener);
2068
2064
  }
2069
- async getStrands(driveId, listenerId, options2) {
2065
+ async getStrands(driveId, listenerId, options) {
2070
2066
  const listener = await this.getListener(driveId, listenerId);
2071
2067
  const strands = [];
2072
2068
  const drive = await this.drive.getDrive(driveId);
@@ -2075,7 +2071,7 @@ var ListenerManager = class _ListenerManager extends BaseListenerManager {
2075
2071
  listenerId,
2076
2072
  drive
2077
2073
  );
2078
- const limit = options2?.limit;
2074
+ const limit = options?.limit;
2079
2075
  let operationsCount = 0;
2080
2076
  const tasks = syncUnits.map((syncUnit) => async () => {
2081
2077
  if (limit && operationsCount >= limit) {
@@ -2095,8 +2091,8 @@ var ListenerManager = class _ListenerManager extends BaseListenerManager {
2095
2091
  driveId2,
2096
2092
  syncUnit.syncId,
2097
2093
  {
2098
- since: options2?.since,
2099
- fromRevision: options2?.fromRevision ?? entry?.listenerRev,
2094
+ since: options?.since,
2095
+ fromRevision: options?.fromRevision ?? entry?.listenerRev,
2100
2096
  limit: limit ? limit - operationsCount : void 0
2101
2097
  },
2102
2098
  drive
@@ -2142,23 +2138,23 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2142
2138
  initializePromise;
2143
2139
  defaultDrivesManager;
2144
2140
  options;
2145
- constructor(documentModels, storage = new MemoryStorage(), cache = new memory_default(), queueManager = new BaseQueueManager(), options2) {
2141
+ constructor(documentModels, storage = new MemoryStorage(), cache = new memory_default(), queueManager = new BaseQueueManager(), options) {
2146
2142
  super();
2147
2143
  this.options = {
2148
- ...options2,
2144
+ ...options,
2149
2145
  defaultDrives: {
2150
- ...options2?.defaultDrives
2146
+ ...options?.defaultDrives
2151
2147
  },
2152
2148
  listenerManager: {
2153
2149
  ...DefaultListenerManagerOptions,
2154
- ...options2?.listenerManager
2150
+ ...options?.listenerManager
2155
2151
  },
2156
- taskQueueMethod: options2?.taskQueueMethod === void 0 ? RunAsap.runAsap : options2.taskQueueMethod
2152
+ taskQueueMethod: options?.taskQueueMethod === void 0 ? RunAsap.runAsap : options.taskQueueMethod
2157
2153
  };
2158
2154
  this.listenerStateManager = new ListenerManager(
2159
2155
  this,
2160
2156
  void 0,
2161
- options2?.listenerManager
2157
+ options?.listenerManager
2162
2158
  );
2163
2159
  this.documentModels = documentModels;
2164
2160
  this.storage = storage;
@@ -2167,7 +2163,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2167
2163
  this.defaultDrivesManager = new DefaultDrivesManager(
2168
2164
  this,
2169
2165
  this.defaultDrivesManagerDelegate,
2170
- options2
2166
+ options
2171
2167
  );
2172
2168
  this.storage.setStorageDelegate?.({
2173
2169
  getCachedOperations: async (drive, id) => {
@@ -2441,24 +2437,24 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2441
2437
  driveId,
2442
2438
  documentId,
2443
2439
  operations,
2444
- options: options2
2440
+ options
2445
2441
  }) => {
2446
- return documentId ? this.addOperations(driveId, documentId, operations, options2) : this.addDriveOperations(
2442
+ return documentId ? this.addOperations(driveId, documentId, operations, options) : this.addDriveOperations(
2447
2443
  driveId,
2448
2444
  operations,
2449
- options2
2445
+ options
2450
2446
  );
2451
2447
  },
2452
2448
  processActionJob: async ({
2453
2449
  driveId,
2454
2450
  documentId,
2455
2451
  actions: actions2,
2456
- options: options2
2452
+ options
2457
2453
  }) => {
2458
- return documentId ? this.addActions(driveId, documentId, actions2, options2) : this.addDriveActions(
2454
+ return documentId ? this.addActions(driveId, documentId, actions2, options) : this.addDriveActions(
2459
2455
  driveId,
2460
2456
  actions2,
2461
- options2
2457
+ options
2462
2458
  );
2463
2459
  },
2464
2460
  processJob: async (job) => {
@@ -2693,8 +2689,8 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2693
2689
  this.emit("driveAdded", document);
2694
2690
  return document;
2695
2691
  }
2696
- async addRemoteDrive(url, options2) {
2697
- const { id, name, slug, icon } = options2.expectedDriveInfo || await requestPublicDrive(url);
2692
+ async addRemoteDrive(url, options) {
2693
+ const { id, name, slug, icon } = options.expectedDriveInfo || await requestPublicDrive(url);
2698
2694
  const {
2699
2695
  pullFilter,
2700
2696
  pullInterval,
@@ -2702,7 +2698,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2702
2698
  sharingType,
2703
2699
  listeners,
2704
2700
  triggers
2705
- } = options2;
2701
+ } = options;
2706
2702
  const pullTrigger = await PullResponderTransmitter.createPullResponderTrigger(id, url, {
2707
2703
  pullFilter,
2708
2704
  pullInterval
@@ -2722,11 +2718,11 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2722
2718
  }
2723
2719
  });
2724
2720
  }
2725
- async registerPullResponderTrigger(id, url, options2) {
2721
+ async registerPullResponderTrigger(id, url, options) {
2726
2722
  const pullTrigger = await PullResponderTransmitter.createPullResponderTrigger(
2727
2723
  id,
2728
2724
  url,
2729
- options2
2725
+ options
2730
2726
  );
2731
2727
  return pullTrigger;
2732
2728
  }
@@ -2746,7 +2742,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2746
2742
  getDrives() {
2747
2743
  return this.storage.getDrives();
2748
2744
  }
2749
- async getDrive(drive, options2) {
2745
+ async getDrive(drive, options) {
2750
2746
  try {
2751
2747
  const document2 = await this.cache.getDocument("drives", drive);
2752
2748
  if (document2 && isDocumentDrive(document2)) {
@@ -2756,7 +2752,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2756
2752
  logger.error("Error getting drive from cache", e);
2757
2753
  }
2758
2754
  const driveStorage = await this.storage.getDrive(drive);
2759
- const document = this._buildDocument(driveStorage, options2);
2755
+ const document = this._buildDocument(driveStorage, options);
2760
2756
  if (!isDocumentDrive(document)) {
2761
2757
  throw new Error(`Document with id ${drive} is not a Document Drive`);
2762
2758
  } else {
@@ -2764,7 +2760,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2764
2760
  return document;
2765
2761
  }
2766
2762
  }
2767
- async getDriveBySlug(slug, options2) {
2763
+ async getDriveBySlug(slug, options) {
2768
2764
  try {
2769
2765
  const document2 = await this.cache.getDocument("drives-slug", slug);
2770
2766
  if (document2 && isDocumentDrive(document2)) {
@@ -2774,7 +2770,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2774
2770
  logger.error("Error getting drive from cache", e);
2775
2771
  }
2776
2772
  const driveStorage = await this.storage.getDriveBySlug(slug);
2777
- const document = this._buildDocument(driveStorage, options2);
2773
+ const document = this._buildDocument(driveStorage, options);
2778
2774
  if (!isDocumentDrive(document)) {
2779
2775
  throw new Error(`Document with slug ${slug} is not a Document Drive`);
2780
2776
  } else {
@@ -2782,7 +2778,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2782
2778
  return document;
2783
2779
  }
2784
2780
  }
2785
- async getDocument(drive, id, options2) {
2781
+ async getDocument(drive, id, options) {
2786
2782
  try {
2787
2783
  const document2 = await this.cache.getDocument(drive, id);
2788
2784
  if (document2) {
@@ -2792,7 +2788,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2792
2788
  logger.error("Error getting document from cache", e);
2793
2789
  }
2794
2790
  const documentStorage = await this.storage.getDocument(drive, id);
2795
- const document = this._buildDocument(documentStorage, options2);
2791
+ const document = this._buildDocument(documentStorage, options);
2796
2792
  this.cache.setDocument(drive, id, document).catch(logger.error);
2797
2793
  return document;
2798
2794
  }
@@ -2924,8 +2920,8 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2924
2920
  error
2925
2921
  };
2926
2922
  }
2927
- async _addDocumentResultingStage(document, drive, documentId, options2) {
2928
- const operations = options2?.revisions !== void 0 ? filterOperationsByRevision(document.operations, options2.revisions) : document.operations;
2923
+ async _addDocumentResultingStage(document, drive, documentId, options) {
2924
+ const operations = options?.revisions !== void 0 ? filterOperationsByRevision(document.operations, options.revisions) : document.operations;
2929
2925
  const documentOperations = utils.documentHelpers.garbageCollectDocumentOperations(
2930
2926
  operations
2931
2927
  );
@@ -2951,14 +2947,14 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2951
2947
  operations: documentOperations
2952
2948
  };
2953
2949
  }
2954
- _buildDocument(documentStorage, options2) {
2955
- if (documentStorage.state && (!options2 || options2.checkHashes === false)) {
2950
+ _buildDocument(documentStorage, options) {
2951
+ if (documentStorage.state && (!options || options.checkHashes === false)) {
2956
2952
  return documentStorage;
2957
2953
  }
2958
2954
  const documentModel2 = this.getDocumentModel(documentStorage.documentType);
2959
- const revisionOperations = options2?.revisions !== void 0 ? filterOperationsByRevision(
2955
+ const revisionOperations = options?.revisions !== void 0 ? filterOperationsByRevision(
2960
2956
  documentStorage.operations,
2961
- options2.revisions
2957
+ options.revisions
2962
2958
  ) : documentStorage.operations;
2963
2959
  const operations = utils.documentHelpers.garbageCollectDocumentOperations(
2964
2960
  revisionOperations
@@ -2971,9 +2967,9 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
2971
2967
  documentStorage,
2972
2968
  void 0,
2973
2969
  {
2974
- ...options2,
2975
- checkHashes: options2?.checkHashes ?? true,
2976
- reuseOperationResultingState: options2?.checkHashes ?? true
2970
+ ...options,
2971
+ checkHashes: options?.checkHashes ?? true,
2972
+ reuseOperationResultingState: options?.checkHashes ?? true
2977
2973
  }
2978
2974
  );
2979
2975
  }
@@ -3060,8 +3056,8 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3060
3056
  operation: appliedOperation
3061
3057
  };
3062
3058
  }
3063
- addOperation(drive, id, operation, options2) {
3064
- return this.addOperations(drive, id, [operation], options2);
3059
+ addOperation(drive, id, operation, options) {
3060
+ return this.addOperations(drive, id, [operation], options);
3065
3061
  }
3066
3062
  async _addOperations(drive, id, callback) {
3067
3063
  if (!this.storage.addDocumentOperationsWithTransaction) {
@@ -3083,8 +3079,8 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3083
3079
  );
3084
3080
  }
3085
3081
  }
3086
- queueOperation(drive, id, operation, options2) {
3087
- return this.queueOperations(drive, id, [operation], options2);
3082
+ queueOperation(drive, id, operation, options) {
3083
+ return this.queueOperations(drive, id, [operation], options);
3088
3084
  }
3089
3085
  async resultIfExistingOperations(drive, id, operations) {
3090
3086
  try {
@@ -3111,7 +3107,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3111
3107
  return void 0;
3112
3108
  }
3113
3109
  }
3114
- async queueOperations(drive, id, operations, options2) {
3110
+ async queueOperations(drive, id, operations, options) {
3115
3111
  const result = await this.resultIfExistingOperations(drive, id, operations);
3116
3112
  if (result) {
3117
3113
  return result;
@@ -3121,7 +3117,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3121
3117
  driveId: drive,
3122
3118
  documentId: id,
3123
3119
  operations,
3124
- options: options2
3120
+ options
3125
3121
  });
3126
3122
  return new Promise((resolve, reject) => {
3127
3123
  const unsubscribe = this.queueManager.on(
@@ -3150,16 +3146,16 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3150
3146
  throw error;
3151
3147
  }
3152
3148
  }
3153
- async queueAction(drive, id, action, options2) {
3154
- return this.queueActions(drive, id, [action], options2);
3149
+ async queueAction(drive, id, action, options) {
3150
+ return this.queueActions(drive, id, [action], options);
3155
3151
  }
3156
- async queueActions(drive, id, actions2, options2) {
3152
+ async queueActions(drive, id, actions2, options) {
3157
3153
  try {
3158
3154
  const jobId = await this.queueManager.addJob({
3159
3155
  driveId: drive,
3160
3156
  documentId: id,
3161
3157
  actions: actions2,
3162
- options: options2
3158
+ options
3163
3159
  });
3164
3160
  return new Promise((resolve, reject) => {
3165
3161
  const unsubscribe = this.queueManager.on(
@@ -3188,15 +3184,15 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3188
3184
  throw error;
3189
3185
  }
3190
3186
  }
3191
- async queueDriveAction(drive, action, options2) {
3192
- return this.queueDriveActions(drive, [action], options2);
3187
+ async queueDriveAction(drive, action, options) {
3188
+ return this.queueDriveActions(drive, [action], options);
3193
3189
  }
3194
- async queueDriveActions(drive, actions2, options2) {
3190
+ async queueDriveActions(drive, actions2, options) {
3195
3191
  try {
3196
3192
  const jobId = await this.queueManager.addJob({
3197
3193
  driveId: drive,
3198
3194
  actions: actions2,
3199
- options: options2
3195
+ options
3200
3196
  });
3201
3197
  return new Promise(
3202
3198
  (resolve, reject) => {
@@ -3227,7 +3223,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3227
3223
  throw error;
3228
3224
  }
3229
3225
  }
3230
- async addOperations(drive, id, operations, options2) {
3226
+ async addOperations(drive, id, operations, options) {
3231
3227
  const result = await this.resultIfExistingOperations(drive, id, operations);
3232
3228
  if (result) {
3233
3229
  return result;
@@ -3281,7 +3277,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3281
3277
  (o) => o.id === appliedOp.id && o.index === appliedOp.index && o.skip === appliedOp.skip && o.hash === appliedOp.hash
3282
3278
  )
3283
3279
  );
3284
- const source = newOp ? { type: "local" } : options2?.source ?? { type: "local" };
3280
+ const source = newOp ? { type: "local" } : options?.source ?? { type: "local" };
3285
3281
  const operationSource = this.getOperationSource(source);
3286
3282
  this.listenerStateManager.updateSynchronizationRevisions(
3287
3283
  drive,
@@ -3298,7 +3294,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3298
3294
  }
3299
3295
  },
3300
3296
  this.handleListenerError.bind(this),
3301
- options2?.forceSync ?? source.type === "local"
3297
+ options?.forceSync ?? source.type === "local"
3302
3298
  ).then((updates) => {
3303
3299
  if (updates.length) {
3304
3300
  this.updateSyncUnitStatus(drive, {
@@ -3354,8 +3350,8 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3354
3350
  };
3355
3351
  }
3356
3352
  }
3357
- addDriveOperation(drive, operation, options2) {
3358
- return this.addDriveOperations(drive, [operation], options2);
3353
+ addDriveOperation(drive, operation, options) {
3354
+ return this.addDriveOperations(drive, [operation], options);
3359
3355
  }
3360
3356
  async clearStorage() {
3361
3357
  for (const drive of await this.getDrives()) {
@@ -3379,8 +3375,8 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3379
3375
  return this.storage.addDriveOperationsWithTransaction(drive, callback);
3380
3376
  }
3381
3377
  }
3382
- queueDriveOperation(drive, operation, options2) {
3383
- return this.queueDriveOperations(drive, [operation], options2);
3378
+ queueDriveOperation(drive, operation, options) {
3379
+ return this.queueDriveOperations(drive, [operation], options);
3384
3380
  }
3385
3381
  async resultIfExistingDriveOperations(driveId, operations) {
3386
3382
  try {
@@ -3405,7 +3401,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3405
3401
  return void 0;
3406
3402
  }
3407
3403
  }
3408
- async queueDriveOperations(drive, operations, options2) {
3404
+ async queueDriveOperations(drive, operations, options) {
3409
3405
  const result = await this.resultIfExistingDriveOperations(
3410
3406
  drive,
3411
3407
  operations
@@ -3417,7 +3413,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3417
3413
  const jobId = await this.queueManager.addJob({
3418
3414
  driveId: drive,
3419
3415
  operations,
3420
- options: options2
3416
+ options
3421
3417
  });
3422
3418
  return new Promise(
3423
3419
  (resolve, reject) => {
@@ -3448,7 +3444,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3448
3444
  throw error;
3449
3445
  }
3450
3446
  }
3451
- async addDriveOperations(drive, operations, options2) {
3447
+ async addDriveOperations(drive, operations, options) {
3452
3448
  let document;
3453
3449
  const operationsApplied = [];
3454
3450
  const signals = [];
@@ -3495,7 +3491,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3495
3491
  (o) => o.id === appliedOp.id && o.index === appliedOp.index && o.skip === appliedOp.skip && o.hash === appliedOp.hash
3496
3492
  )
3497
3493
  );
3498
- const source = newOp ? { type: "local" } : options2?.source ?? { type: "local" };
3494
+ const source = newOp ? { type: "local" } : options?.source ?? { type: "local" };
3499
3495
  const operationSource = this.getOperationSource(source);
3500
3496
  this.listenerStateManager.updateSynchronizationRevisions(
3501
3497
  drive,
@@ -3518,7 +3514,7 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3518
3514
  });
3519
3515
  },
3520
3516
  this.handleListenerError.bind(this),
3521
- options2?.forceSync ?? source.type === "local"
3517
+ options?.forceSync ?? source.type === "local"
3522
3518
  ).then((updates) => {
3523
3519
  if (updates.length) {
3524
3520
  this.updateSyncUnitStatus(drive, {
@@ -3577,24 +3573,24 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3577
3573
  }
3578
3574
  return operations;
3579
3575
  }
3580
- async addAction(drive, id, action, options2) {
3581
- return this.addActions(drive, id, [action], options2);
3576
+ async addAction(drive, id, action, options) {
3577
+ return this.addActions(drive, id, [action], options);
3582
3578
  }
3583
- async addActions(drive, id, actions2, options2) {
3579
+ async addActions(drive, id, actions2, options) {
3584
3580
  const document = await this.getDocument(drive, id);
3585
3581
  const operations = this._buildOperations(document, actions2);
3586
- return this.addOperations(drive, id, operations, options2);
3582
+ return this.addOperations(drive, id, operations, options);
3587
3583
  }
3588
- async addDriveAction(drive, action, options2) {
3589
- return this.addDriveActions(drive, [action], options2);
3584
+ async addDriveAction(drive, action, options) {
3585
+ return this.addDriveActions(drive, [action], options);
3590
3586
  }
3591
- async addDriveActions(drive, actions2, options2) {
3587
+ async addDriveActions(drive, actions2, options) {
3592
3588
  const document = await this.getDrive(drive);
3593
3589
  const operations = this._buildOperations(document, actions2);
3594
- const result = await this.addDriveOperations(drive, operations, options2);
3590
+ const result = await this.addDriveOperations(drive, operations, options);
3595
3591
  return result;
3596
3592
  }
3597
- async addInternalListener(driveId, receiver, options2) {
3593
+ async addInternalListener(driveId, receiver, options) {
3598
3594
  const listener = {
3599
3595
  callInfo: {
3600
3596
  data: "",
@@ -3602,10 +3598,10 @@ var BaseDocumentDriveServer = class extends AbstractDocumentDriveServer {
3602
3598
  transmitterType: "Internal"
3603
3599
  },
3604
3600
  system: true,
3605
- ...options2
3601
+ ...options
3606
3602
  };
3607
3603
  await this.addDriveAction(driveId, actions.addListener({ listener }));
3608
- const transmitter = await this.getTransmitter(driveId, options2.listenerId);
3604
+ const transmitter = await this.getTransmitter(driveId, options.listenerId);
3609
3605
  if (!transmitter) {
3610
3606
  logger.error("Internal listener not found");
3611
3607
  throw new Error("Internal listener not found");
@@ -3698,11 +3694,11 @@ var FilesystemStorage = class _FilesystemStorage {
3698
3694
  constructor(basePath) {
3699
3695
  this.basePath = basePath;
3700
3696
  ensureDir(this.basePath);
3701
- this.drivesPath = path2.join(this.basePath, _FilesystemStorage.DRIVES_DIR);
3697
+ this.drivesPath = path.join(this.basePath, _FilesystemStorage.DRIVES_DIR);
3702
3698
  ensureDir(this.drivesPath);
3703
3699
  }
3704
3700
  _buildDocumentPath(...args) {
3705
- return `${path2.join(
3701
+ return `${path.join(
3706
3702
  this.basePath,
3707
3703
  ...args.map((arg) => sanitize(arg))
3708
3704
  )}.json`;
@@ -3710,7 +3706,7 @@ var FilesystemStorage = class _FilesystemStorage {
3710
3706
  async getDocuments(drive) {
3711
3707
  let files = [];
3712
3708
  try {
3713
- files = readdirSync(path2.join(this.basePath, drive), {
3709
+ files = readdirSync(path.join(this.basePath, drive), {
3714
3710
  withFileTypes: true
3715
3711
  });
3716
3712
  } catch (error) {
@@ -3721,7 +3717,7 @@ var FilesystemStorage = class _FilesystemStorage {
3721
3717
  const documents = [];
3722
3718
  for (const file of files.filter((file2) => file2.isFile())) {
3723
3719
  try {
3724
- const documentId = path2.parse(file.name).name;
3720
+ const documentId = path.parse(file.name).name;
3725
3721
  await this.getDocument(drive, documentId);
3726
3722
  documents.push(documentId);
3727
3723
  } catch {
@@ -3745,21 +3741,21 @@ var FilesystemStorage = class _FilesystemStorage {
3745
3741
  }
3746
3742
  async createDocument(drive, id, document) {
3747
3743
  const documentPath = this._buildDocumentPath(drive, id);
3748
- ensureDir(path2.dirname(documentPath));
3744
+ ensureDir(path.dirname(documentPath));
3749
3745
  writeFileSync(documentPath, stringify(document), {
3750
3746
  encoding: "utf-8"
3751
3747
  });
3752
3748
  return Promise.resolve();
3753
3749
  }
3754
3750
  async clearStorage() {
3755
- const drivesPath = path2.join(this.basePath, _FilesystemStorage.DRIVES_DIR);
3751
+ const drivesPath = path.join(this.basePath, _FilesystemStorage.DRIVES_DIR);
3756
3752
  const drives = (await fs.readdir(drivesPath, {
3757
3753
  withFileTypes: true,
3758
3754
  recursive: true
3759
3755
  })).filter((dirent) => !!dirent.name);
3760
3756
  await Promise.all(
3761
3757
  drives.map(async (dirent) => {
3762
- await fs.rm(path2.join(drivesPath, dirent.name), {
3758
+ await fs.rm(path.join(drivesPath, dirent.name), {
3763
3759
  recursive: true
3764
3760
  });
3765
3761
  })
@@ -3769,7 +3765,7 @@ var FilesystemStorage = class _FilesystemStorage {
3769
3765
  );
3770
3766
  await Promise.all(
3771
3767
  files.map(async (dirent) => {
3772
- await fs.rm(path2.join(this.basePath, dirent.name), {
3768
+ await fs.rm(path.join(this.basePath, dirent.name), {
3773
3769
  recursive: true
3774
3770
  });
3775
3771
  })
@@ -3797,7 +3793,7 @@ var FilesystemStorage = class _FilesystemStorage {
3797
3793
  const drives = [];
3798
3794
  for (const file of files.filter((file2) => file2.isFile())) {
3799
3795
  try {
3800
- const driveId = path2.parse(file.name).name;
3796
+ const driveId = path.parse(file.name).name;
3801
3797
  await this.getDrive(driveId);
3802
3798
  drives.push(driveId);
3803
3799
  } catch {
@@ -3882,30 +3878,32 @@ var FilesystemStorage = class _FilesystemStorage {
3882
3878
  }, []);
3883
3879
  }
3884
3880
  };
3881
+ var dirname = import.meta.dirname || path2.dirname(fileURLToPath(import.meta.url));
3885
3882
  dotenv.config();
3886
- var driveServer = new DocumentDriveServer(
3887
- [module, ...Object.values(DocumentModelsLibs)],
3888
- new FilesystemStorage(path2.join(__dirname, "../file-storage"))
3889
- );
3890
- var serverPort = process.env.PORT ? Number(process.env.PORT) : 4001;
3891
- var startServer = async () => {
3892
- const db = await drizzle("pglite", "./dev.db");
3883
+ var startServer = async (options) => {
3884
+ const serverPort = Number(options?.connect?.port ?? process.env.PORT ?? 4001);
3885
+ const storagePath = options?.reactor?.storagePath ?? path2.join(dirname, "./file-storage");
3886
+ const drive = options?.reactor?.drive ?? {
3887
+ global: {
3888
+ id: "powerhouse",
3889
+ name: "Powerhouse",
3890
+ icon: "https://ipfs.io/ipfs/QmcaTDBYn8X2psGaXe7iQ6qd8q6oqHLgxvMX9yXf7f9uP7",
3891
+ slug: "powerhouse"
3892
+ },
3893
+ local: {
3894
+ availableOffline: true,
3895
+ listeners: [],
3896
+ sharingType: "public",
3897
+ triggers: []
3898
+ }
3899
+ };
3900
+ const driveServer = new DocumentDriveServer(
3901
+ [module, ...Object.values(DocumentModelsLibs)],
3902
+ new FilesystemStorage(storagePath)
3903
+ );
3893
3904
  await driveServer.initialize();
3894
3905
  try {
3895
- await driveServer.addDrive({
3896
- global: {
3897
- id: "powerhouse",
3898
- name: "Powerhouse",
3899
- icon: "powerhouse",
3900
- slug: "powerhouse"
3901
- },
3902
- local: {
3903
- availableOffline: true,
3904
- listeners: [],
3905
- sharingType: "public",
3906
- triggers: []
3907
- }
3908
- });
3906
+ await driveServer.addDrive(drive);
3909
3907
  } catch (e) {
3910
3908
  if (e instanceof DriveAlreadyExistsError) {
3911
3909
  console.info("Default drive already exists. Skipping...");
@@ -3917,23 +3915,15 @@ var startServer = async () => {
3917
3915
  await startAPI(driveServer, {
3918
3916
  port: serverPort
3919
3917
  });
3920
- setAdditionalContextFields({ db });
3921
- await registerInternalListener({
3922
- name: "search",
3923
- options: searchListener.options,
3924
- transmit: (strands) => searchListener.transmit(strands, db)
3925
- });
3926
- await addSubgraph({
3927
- getSchema: () => createSchema(
3928
- driveServer,
3929
- searchListener.resolvers,
3930
- searchListener.typeDefs
3931
- ),
3932
- name: "search/:drive"
3933
- });
3934
3918
  } catch (e) {
3935
3919
  console.error("App crashed", e);
3936
3920
  }
3921
+ return {
3922
+ getDocumentPath: (driveId, documentId) => {
3923
+ return path2.join(storagePath, driveId, `${documentId}.json`);
3924
+ },
3925
+ addListener: (driveId, receiver, options2) => driveServer.addInternalListener(driveId, receiver, options2)
3926
+ };
3937
3927
  };
3938
3928
 
3939
3929
  export { startServer };