@powersync/service-module-mongodb-storage 0.0.0-dev-20260114113449 → 0.0.0-dev-20260114171046

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @powersync/service-module-mongodb-storage
2
2
 
3
- ## 0.0.0-dev-20260114113449
3
+ ## 0.0.0-dev-20260114171046
4
4
 
5
5
  ### Minor Changes
6
6
 
@@ -11,10 +11,10 @@
11
11
  - Updated dependencies [781d0e3]
12
12
  - Updated dependencies [e578245]
13
13
  - Updated dependencies [3040079]
14
- - @powersync/service-core@0.0.0-dev-20260114113449
15
- - @powersync/service-sync-rules@0.0.0-dev-20260114113449
16
- - @powersync/lib-services-framework@0.0.0-dev-20260114113449
17
- - @powersync/lib-service-mongodb@0.0.0-dev-20260114113449
14
+ - @powersync/service-core@0.0.0-dev-20260114171046
15
+ - @powersync/service-sync-rules@0.0.0-dev-20260114171046
16
+ - @powersync/lib-services-framework@0.0.0-dev-20260114171046
17
+ - @powersync/lib-service-mongodb@0.0.0-dev-20260114171046
18
18
 
19
19
  ## 0.12.16
20
20
 
@@ -9,6 +9,7 @@ export declare class MongoSyncRulesLock implements storage.ReplicationLock {
9
9
  sync_rules_id: number;
10
10
  private lock_id;
11
11
  private readonly refreshInterval;
12
+ private static lockAlerted;
12
13
  static createLock(db: PowerSyncMongo, sync_rules: storage.PersistedSyncRulesContent): Promise<MongoSyncRulesLock>;
13
14
  constructor(db: PowerSyncMongo, sync_rules_id: number, lock_id: string);
14
15
  release(): Promise<void>;
@@ -1,7 +1,5 @@
1
1
  import crypto from 'crypto';
2
2
  import { ErrorCode, logger, ServiceError } from '@powersync/lib-services-framework';
3
- import { fsCache, syncLockCheck } from '../../utils/util.js';
4
- import { FsCachePaths } from '../../types/types.js';
5
3
  /**
6
4
  * Manages a lock on a sync rules document, so that only one process
7
5
  * replicates those sync rules at a time.
@@ -11,6 +9,7 @@ export class MongoSyncRulesLock {
11
9
  sync_rules_id;
12
10
  lock_id;
13
11
  refreshInterval;
12
+ static lockAlerted = false;
14
13
  static async createLock(db, sync_rules) {
15
14
  const lockId = crypto.randomBytes(8).toString('hex');
16
15
  const doc = await db.sync_rules.findOneAndUpdate({ _id: sync_rules.id, $or: [{ lock: null }, { 'lock.expires_at': { $lt: new Date() } }] }, {
@@ -28,20 +27,24 @@ export class MongoSyncRulesLock {
28
27
  // Query the existing lock to get the expiration time (best effort - it may have been released in the meantime).
29
28
  const heldLock = await db.sync_rules.findOne({ _id: sync_rules.id }, { projection: { lock: 1 } });
30
29
  if (heldLock?.lock?.expires_at) {
31
- const alert = await fsCache(FsCachePaths.SYNC_RULES_LOCK, heldLock.lock.expires_at.toISOString(), syncLockCheck);
32
- /** If the date has changed on the expires_at the alert key will be true, we want to notify that another process has a lock */
33
- if (alert) {
30
+ if (!this.lockAlerted) {
31
+ this.lockAlerted = true;
34
32
  throw new ServiceError(ErrorCode.PSYNC_S1003, `Sync rules: ${sync_rules.id} have been locked by another process for replication, expiring at ${heldLock.lock.expires_at.toISOString()}.`);
35
33
  }
36
34
  else {
37
- /** We throw undefined as it will be ignored in the catch block */
38
35
  throw undefined;
39
36
  }
40
37
  }
41
38
  else {
42
- throw new ServiceError(ErrorCode.PSYNC_S1003, `Sync rules: ${sync_rules.id} have been locked by another process for replication.`);
39
+ if (!this.lockAlerted) {
40
+ throw new ServiceError(ErrorCode.PSYNC_S1003, `Sync rules: ${sync_rules.id} have been locked by another process for replication.`);
41
+ }
42
+ else {
43
+ throw undefined;
44
+ }
43
45
  }
44
46
  }
47
+ this.lockAlerted = false;
45
48
  return new MongoSyncRulesLock(db, sync_rules.id, lockId);
46
49
  }
47
50
  constructor(db, sync_rules_id, lock_id) {
@@ -1 +1 @@
1
- {"version":3,"file":"MongoSyncRulesLock.js","sourceRoot":"","sources":["../../../src/storage/implementation/MongoSyncRulesLock.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,Q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;QACpC,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAC/C;YACE,GAAG,EAAE,IAAI,CAAC,aAAa;YACvB,SAAS,EAAE,IAAI,CAAC,OAAO;SACxB,EACD;YACE,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE;SACpB,CACF,CAAC;QACF,IAAI,MAAM,CAAC,aAAa,IAAI,CAAC,EAAE,CAAC;YAC9B,iBAAiB;YACjB,MAAM,CAAC,IAAI,CAAC,0BAA0B,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9E,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,gBAAgB,CACtD;YACE,GAAG,EAAE,IAAI,CAAC,aAAa;YACvB,SAAS,EAAE,IAAI,CAAC,OAAO;SACxB,EACD;YACE,IAAI,EAAE,EAAE,iBAAiB,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,EAAE;SAC9D,EACD,EAAE,cAAc,EAAE,OAAO,EAAE,CAC5B,CAAC;QACF,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAClF,CAAC;IACH,CAAC;CACF"}
1
+ {"version":3,"file":"MongoSyncRulesLock.js","sourceRoot":"","sources":["../../../src/storage/implementation/MongoSyncRulesLock.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,mCAAmC,CAAC;AAIpF;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAsDnB;IACD;IACC;IAvDO,eAAe,CAAiB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E,CAAC,EAAE;SACpB,CACF,CAAC;QACF,IAAI,MAAM,CAAC,aAAa,IAAI,CAAC,EAAE,CAAC;YAC9B,iBAAiB;YACjB,MAAM,CAAC,IAAI,CAAC,0BAA0B,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9E,CAAC;IACH,CAAC;IAEO,KAAK,CAAC,OAAO;QACnB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,gBAAgB,CACtD;YACE,GAAG,EAAE,IAAI,CAAC,aAAa;YACvB,SAAS,EAAE,IAAI,CAAC,OAAO;SACxB,EACD;YACE,IAAI,EAAE,EAAE,iBAAiB,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC,EAAE;SAC9D,EACD,EAAE,cAAc,EAAE,OAAO,EAAE,CAC5B,CAAC;QACF,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,0BAA0B,IAAI,CAAC,aAAa,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAClF,CAAC;IACH,CAAC"}
@@ -1,8 +1,5 @@
1
1
  import * as service_types from '@powersync/service-types';
2
2
  import * as t from 'ts-codec';
3
- export declare enum FsCachePaths {
4
- SYNC_RULES_LOCK = "sync_rules_lock"
5
- }
6
3
  export declare const MongoStorageConfig: t.Intersection<t.Codec<{
7
4
  uri: string;
8
5
  type: "mongodb";
@@ -1,9 +1,5 @@
1
1
  import * as lib_mongo from '@powersync/lib-service-mongodb';
2
2
  import * as t from 'ts-codec';
3
- export var FsCachePaths;
4
- (function (FsCachePaths) {
5
- FsCachePaths["SYNC_RULES_LOCK"] = "sync_rules_lock";
6
- })(FsCachePaths || (FsCachePaths = {}));
7
3
  export const MongoStorageConfig = lib_mongo.BaseMongoConfig.and(t.object({
8
4
  // Add any mongo specific storage settings here in future
9
5
  }));
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,gCAAgC,CAAC;AAE5D,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B,MAAM,CAAN,IAAY,YAEX;AAFD,WAAY,YAAY;IACtB,mDAAmC,CAAA;AACrC,CAAC,EAFW,YAAY,KAAZ,YAAY,QAEvB;AAED,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAC,eAAe,CAAC,GAAG,CAC7D,CAAC,CAAC,MAAM,CAAC;AACP,yDAAyD;CAC1D,CAAC,CACH,CAAC;AAKF,MAAM,UAAU,oBAAoB,CAClC,MAAqD;IAErD,OAAO,MAAM,CAAC,IAAI,IAAI,SAAS,CAAC,qBAAqB,CAAC;AACxD,CAAC"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,SAAS,MAAM,gCAAgC,CAAC;AAE5D,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B,MAAM,CAAC,MAAM,kBAAkB,GAAG,SAAS,CAAC,eAAe,CAAC,GAAG,CAC7D,CAAC,CAAC,MAAM,CAAC;AACP,yDAAyD;CAC1D,CAAC,CACH,CAAC;AAKF,MAAM,UAAU,oBAAoB,CAClC,MAAqD;IAErD,OAAO,MAAM,CAAC,IAAI,IAAI,SAAS,CAAC,qBAAqB,CAAC;AACxD,CAAC"}
@@ -2,7 +2,6 @@ import * as bson from 'bson';
2
2
  import { mongo } from '@powersync/lib-service-mongodb';
3
3
  import { storage, utils } from '@powersync/service-core';
4
4
  import { BucketDataDocument } from '../storage/implementation/models.js';
5
- import { FsCachePaths } from '../types/types.js';
6
5
  export declare function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T>;
7
6
  export declare function generateSlotName(prefix: string, sync_rules_id: number): string;
8
7
  /**
@@ -30,25 +29,3 @@ export declare const createPaginatedConnectionQuery: <T extends mongo.Document>(
30
29
  cursor: any;
31
30
  more: boolean;
32
31
  }>;
33
- /**
34
- * Caches data in a file and processes it using a provided function.
35
- *
36
- * This function checks if a file exists at the specified `filename` path. If the file exists,
37
- * it reads the cached data from the file and passes it, along with the provided `text`,
38
- * to the `func` callback for processing. If the file does not exist, it writes the `text`
39
- * to the file and then calls the `func` with the `text` as both the cache and input.
40
- *
41
- * @template T - The return type of the `func` callback.
42
- * @param {FsCachePaths} filename - The path to the cache file.
43
- * @param {string} text - The text to cache or compare against the cached data.
44
- * @param {(cache: string, text: string) => Promise<T>} func - A callback function that processes the cached data
45
- * and the provided text. It receives the cached data (or the `text` if no cache exists) as the first argument
46
- * and the `text` as the second argument.
47
- * @returns {Promise<T>} - A promise that resolves to the result of the `func` callback.
48
- */
49
- export declare function fsCache<T>(filename: FsCachePaths, text: string, func: (cache: string, text: string) => Promise<T>): Promise<T>;
50
- /**
51
- * Compares cached text with new text and updates the cache file if they differ.
52
- * Returns true if the cache was updated, false otherwise.
53
- */
54
- export declare function syncLockCheck(cache: string, text: string): Promise<boolean>;
@@ -1,11 +1,8 @@
1
1
  import * as bson from 'bson';
2
2
  import * as crypto from 'crypto';
3
3
  import * as uuid from 'uuid';
4
- import fs from 'fs';
5
4
  import { storage, utils } from '@powersync/service-core';
6
5
  import { ServiceAssertionError } from '@powersync/lib-services-framework';
7
- import { promisify } from 'node:util';
8
- import { FsCachePaths } from '../types/types.js';
9
6
  export function idPrefixFilter(prefix, rest) {
10
7
  let filter = {
11
8
  $gte: {
@@ -141,46 +138,4 @@ export const createPaginatedConnectionQuery = async (query, collection, limit, c
141
138
  more: !(count !== limit)
142
139
  };
143
140
  };
144
- /**
145
- * Caches data in a file and processes it using a provided function.
146
- *
147
- * This function checks if a file exists at the specified `filename` path. If the file exists,
148
- * it reads the cached data from the file and passes it, along with the provided `text`,
149
- * to the `func` callback for processing. If the file does not exist, it writes the `text`
150
- * to the file and then calls the `func` with the `text` as both the cache and input.
151
- *
152
- * @template T - The return type of the `func` callback.
153
- * @param {FsCachePaths} filename - The path to the cache file.
154
- * @param {string} text - The text to cache or compare against the cached data.
155
- * @param {(cache: string, text: string) => Promise<T>} func - A callback function that processes the cached data
156
- * and the provided text. It receives the cached data (or the `text` if no cache exists) as the first argument
157
- * and the `text` as the second argument.
158
- * @returns {Promise<T>} - A promise that resolves to the result of the `func` callback.
159
- */
160
- export async function fsCache(filename, text, func) {
161
- const readFilePromise = promisify(fs.readFile);
162
- const checkFilePromise = promisify(fs.exists);
163
- const writeFilePromise = promisify(fs.writeFile);
164
- /** Checks to see if the file exists */
165
- if (await checkFilePromise(filename)) {
166
- const cache = await readFilePromise(filename, 'utf-8');
167
- return func(cache, text);
168
- }
169
- else {
170
- await writeFilePromise(filename, text, 'utf-8');
171
- return func(text, text);
172
- }
173
- }
174
- /**
175
- * Compares cached text with new text and updates the cache file if they differ.
176
- * Returns true if the cache was updated, false otherwise.
177
- */
178
- export async function syncLockCheck(cache, text) {
179
- if (cache === text) {
180
- return false;
181
- }
182
- const writeFilePromise = promisify(fs.writeFile);
183
- await writeFilePromise(FsCachePaths.SYNC_RULES_LOCK, text, 'utf-8');
184
- return true;
185
- }
186
141
  //# sourceMappingURL=util.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/utils/util.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAE,MAAM,IAAI,CAAC;AAEpB,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAE1E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,m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EAAE,CAAC;YACZ,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,YAAY,GAAG,KAAK,CAAC,YAAY;YACrC,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE;YAC1D,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9B,OAAO;YACL,GAAG,KAAK;YACR,YAAY;SACM,CAAC;IACvB,CAAC,CAAC;IAEF,MAAM,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE;QACtD,IAAI,EAAE;YACJ,6FAA6F;YAC7F,YAAY,EAAE,CAAC,CAAC;SACjB;KACF,CAAC,CAAC;IAEH,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,OAAO,EAAE,CAAC;IACtD,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC;IAC3B;;SAEK;IACL,OAAO;QACL,KAAK;QACL,KAAK;QACL,+EAA+E;QAC/E,MAAM,EAAE,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,SAAS;QACxF,IAAI,EAAE,CAAC,CAAC,KAAK,KAAK,KAAK,CAAC;KACzB,CAAC;AACJ,CAAC,CAAC"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@powersync/service-module-mongodb-storage",
3
3
  "repository": "https://github.com/powersync-ja/powersync-service",
4
4
  "types": "dist/index.d.ts",
5
- "version": "0.0.0-dev-20260114113449",
5
+ "version": "0.0.0-dev-20260114171046",
6
6
  "main": "dist/index.js",
7
7
  "license": "FSL-1.1-ALv2",
8
8
  "type": "module",
@@ -27,15 +27,15 @@
27
27
  "lru-cache": "^10.2.2",
28
28
  "ts-codec": "^1.3.0",
29
29
  "uuid": "^11.1.0",
30
- "@powersync/lib-service-mongodb": "0.0.0-dev-20260114113449",
31
- "@powersync/lib-services-framework": "0.0.0-dev-20260114113449",
32
- "@powersync/service-core": "0.0.0-dev-20260114113449",
30
+ "@powersync/lib-service-mongodb": "0.0.0-dev-20260114171046",
31
+ "@powersync/lib-services-framework": "0.0.0-dev-20260114171046",
32
+ "@powersync/service-core": "0.0.0-dev-20260114171046",
33
33
  "@powersync/service-jsonbig": "0.17.12",
34
- "@powersync/service-sync-rules": "0.0.0-dev-20260114113449",
34
+ "@powersync/service-sync-rules": "0.0.0-dev-20260114171046",
35
35
  "@powersync/service-types": "0.13.3"
36
36
  },
37
37
  "devDependencies": {
38
- "@powersync/service-core-tests": "0.0.0-dev-20260114113449"
38
+ "@powersync/service-core-tests": "0.0.0-dev-20260114171046"
39
39
  },
40
40
  "scripts": {
41
41
  "build": "tsc -b",
@@ -3,8 +3,6 @@ import crypto from 'crypto';
3
3
  import { ErrorCode, logger, ServiceError } from '@powersync/lib-services-framework';
4
4
  import { storage } from '@powersync/service-core';
5
5
  import { PowerSyncMongo } from './db.js';
6
- import { fsCache, syncLockCheck } from '../../utils/util.js';
7
- import { FsCachePaths } from '../../types/types.js';
8
6
 
9
7
  /**
10
8
  * Manages a lock on a sync rules document, so that only one process
@@ -12,6 +10,7 @@ import { FsCachePaths } from '../../types/types.js';
12
10
  */
13
11
  export class MongoSyncRulesLock implements storage.ReplicationLock {
14
12
  private readonly refreshInterval: NodeJS.Timeout;
13
+ private static lockAlerted: boolean = false;
15
14
 
16
15
  static async createLock(
17
16
  db: PowerSyncMongo,
@@ -38,28 +37,27 @@ export class MongoSyncRulesLock implements storage.ReplicationLock {
38
37
  // Query the existing lock to get the expiration time (best effort - it may have been released in the meantime).
39
38
  const heldLock = await db.sync_rules.findOne({ _id: sync_rules.id }, { projection: { lock: 1 } });
40
39
  if (heldLock?.lock?.expires_at) {
41
- const alert = await fsCache(
42
- FsCachePaths.SYNC_RULES_LOCK,
43
- heldLock.lock.expires_at.toISOString(),
44
- syncLockCheck
45
- );
46
- /** If the date has changed on the expires_at the alert key will be true, we want to notify that another process has a lock */
47
- if (alert) {
40
+ if (!this.lockAlerted) {
41
+ this.lockAlerted = true;
48
42
  throw new ServiceError(
49
43
  ErrorCode.PSYNC_S1003,
50
44
  `Sync rules: ${sync_rules.id} have been locked by another process for replication, expiring at ${heldLock.lock.expires_at.toISOString()}.`
51
45
  );
52
46
  } else {
53
- /** We throw undefined as it will be ignored in the catch block */
54
47
  throw undefined;
55
48
  }
56
49
  } else {
57
- throw new ServiceError(
58
- ErrorCode.PSYNC_S1003,
59
- `Sync rules: ${sync_rules.id} have been locked by another process for replication.`
60
- );
50
+ if (!this.lockAlerted) {
51
+ throw new ServiceError(
52
+ ErrorCode.PSYNC_S1003,
53
+ `Sync rules: ${sync_rules.id} have been locked by another process for replication.`
54
+ );
55
+ } else {
56
+ throw undefined;
57
+ }
61
58
  }
62
59
  }
60
+ this.lockAlerted = false;
63
61
  return new MongoSyncRulesLock(db, sync_rules.id, lockId);
64
62
  }
65
63
 
@@ -2,10 +2,6 @@ import * as lib_mongo from '@powersync/lib-service-mongodb';
2
2
  import * as service_types from '@powersync/service-types';
3
3
  import * as t from 'ts-codec';
4
4
 
5
- export enum FsCachePaths {
6
- SYNC_RULES_LOCK = 'sync_rules_lock'
7
- }
8
-
9
5
  export const MongoStorageConfig = lib_mongo.BaseMongoConfig.and(
10
6
  t.object({
11
7
  // Add any mongo specific storage settings here in future
package/src/utils/util.ts CHANGED
@@ -1,13 +1,10 @@
1
1
  import * as bson from 'bson';
2
2
  import * as crypto from 'crypto';
3
3
  import * as uuid from 'uuid';
4
- import fs from 'fs';
5
4
  import { mongo } from '@powersync/lib-service-mongodb';
6
5
  import { storage, utils } from '@powersync/service-core';
7
6
  import { ServiceAssertionError } from '@powersync/lib-services-framework';
8
7
  import { BucketDataDocument } from '../storage/implementation/models.js';
9
- import { promisify } from 'node:util';
10
- import { FsCachePaths } from '../types/types.js';
11
8
 
12
9
  export function idPrefixFilter<T>(prefix: Partial<T>, rest: (keyof T)[]): mongo.Condition<T> {
13
10
  let filter = {
@@ -156,51 +153,3 @@ export const createPaginatedConnectionQuery = async <T extends mongo.Document>(
156
153
  more: !(count !== limit)
157
154
  };
158
155
  };
159
-
160
- /**
161
- * Caches data in a file and processes it using a provided function.
162
- *
163
- * This function checks if a file exists at the specified `filename` path. If the file exists,
164
- * it reads the cached data from the file and passes it, along with the provided `text`,
165
- * to the `func` callback for processing. If the file does not exist, it writes the `text`
166
- * to the file and then calls the `func` with the `text` as both the cache and input.
167
- *
168
- * @template T - The return type of the `func` callback.
169
- * @param {FsCachePaths} filename - The path to the cache file.
170
- * @param {string} text - The text to cache or compare against the cached data.
171
- * @param {(cache: string, text: string) => Promise<T>} func - A callback function that processes the cached data
172
- * and the provided text. It receives the cached data (or the `text` if no cache exists) as the first argument
173
- * and the `text` as the second argument.
174
- * @returns {Promise<T>} - A promise that resolves to the result of the `func` callback.
175
- */
176
- export async function fsCache<T>(
177
- filename: FsCachePaths,
178
- text: string,
179
- func: (cache: string, text: string) => Promise<T>
180
- ): Promise<T> {
181
- const readFilePromise = promisify(fs.readFile);
182
- const checkFilePromise = promisify(fs.exists);
183
- const writeFilePromise = promisify(fs.writeFile);
184
-
185
- /** Checks to see if the file exists */
186
- if (await checkFilePromise(filename)) {
187
- const cache = await readFilePromise(filename, 'utf-8');
188
- return func(cache, text);
189
- } else {
190
- await writeFilePromise(filename, text, 'utf-8');
191
- return func(text, text);
192
- }
193
- }
194
-
195
- /**
196
- * Compares cached text with new text and updates the cache file if they differ.
197
- * Returns true if the cache was updated, false otherwise.
198
- */
199
- export async function syncLockCheck(cache: string, text: string): Promise<boolean> {
200
- if (cache === text) {
201
- return false;
202
- }
203
- const writeFilePromise = promisify(fs.writeFile);
204
- await writeFilePromise(FsCachePaths.SYNC_RULES_LOCK, text, 'utf-8');
205
- return true;
206
- }