@ocap/statedb 1.29.3 → 1.29.5

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/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  Defines the abstract interface of OCAP StateDB, must be implemented with an actual statedb, please checkout following packages:
4
4
 
5
5
  - `@ocap/statedb-memory`
6
- - `@ocap/statedb-fs`
7
6
  - `@ocap/statedb-dolt`
8
7
 
9
8
  ## Usage
package/esm/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
1
  import StateDB from "./db.mjs";
2
2
  import StateDBTable from "./table.mjs";
3
- export { StateDB, StateDBTable };
3
+ import { followMigrationChain, wouldCreateMigrationCycle } from "./migration-utils.mjs";
4
+ export { StateDB, StateDBTable, followMigrationChain, wouldCreateMigrationCycle };
package/esm/index.mjs CHANGED
@@ -1,4 +1,5 @@
1
1
  import db_default from "./db.mjs";
2
2
  import table_default from "./table.mjs";
3
+ import { followMigrationChain, wouldCreateMigrationCycle } from "./migration-utils.mjs";
3
4
 
4
- export { db_default as StateDB, table_default as StateDBTable };
5
+ export { db_default as StateDB, table_default as StateDBTable, followMigrationChain, wouldCreateMigrationCycle };
@@ -0,0 +1,28 @@
1
+ //#region src/migration-utils.d.ts
2
+ /**
3
+ * Migration utility functions for StateDB
4
+ * Shared logic for circular migration detection
5
+ */
6
+ type AccountWithMigration = {
7
+ migratedTo?: string[];
8
+ };
9
+ /**
10
+ * Check if adding migration targets would create a cycle
11
+ * @param sourceAddress - The address that will have migratedTo set
12
+ * @param targets - The migration targets to check
13
+ * @param getAccount - Function to get account by address (without following migration)
14
+ * @param normalizeAddress - Function to normalize address format
15
+ * @returns true if a cycle would be created
16
+ */
17
+ declare function wouldCreateMigrationCycle<T extends AccountWithMigration>(sourceAddress: string, targets: string[], getAccount: (address: string) => Promise<T | null>, normalizeAddress: (addr: string) => string): Promise<boolean>;
18
+ /**
19
+ * Follow migration chain with cycle detection
20
+ * @param startAddress - Starting address to look up
21
+ * @param getAccount - Function to get account by address
22
+ * @param normalizeAddress - Function to normalize address format
23
+ * @param visited - Set of already visited addresses (for cycle detection)
24
+ * @returns The final account after following migration chain, or null if cycle/missing
25
+ */
26
+ declare function followMigrationChain<T extends AccountWithMigration>(startAddress: string, getAccount: (address: string) => Promise<T | null>, normalizeAddress: (addr: string) => string, visited?: Set<string>): Promise<T | null>;
27
+ //#endregion
28
+ export { followMigrationChain, wouldCreateMigrationCycle };
@@ -0,0 +1,45 @@
1
+ //#region src/migration-utils.ts
2
+ /**
3
+ * Check if adding migration targets would create a cycle
4
+ * @param sourceAddress - The address that will have migratedTo set
5
+ * @param targets - The migration targets to check
6
+ * @param getAccount - Function to get account by address (without following migration)
7
+ * @param normalizeAddress - Function to normalize address format
8
+ * @returns true if a cycle would be created
9
+ */
10
+ async function wouldCreateMigrationCycle(sourceAddress, targets, getAccount, normalizeAddress) {
11
+ const normalizedSource = normalizeAddress(sourceAddress);
12
+ const visited = new Set([normalizedSource]);
13
+ const checkTarget = async (target) => {
14
+ const normalizedTarget = normalizeAddress(target);
15
+ if (normalizedTarget === normalizedSource) return true;
16
+ if (visited.has(normalizedTarget)) return false;
17
+ visited.add(normalizedTarget);
18
+ const targetAccount = await getAccount(normalizedTarget);
19
+ if (targetAccount?.migratedTo?.length) {
20
+ for (const nextTarget of targetAccount.migratedTo) if (await checkTarget(nextTarget)) return true;
21
+ }
22
+ return false;
23
+ };
24
+ for (const target of targets) if (await checkTarget(target)) return true;
25
+ return false;
26
+ }
27
+ /**
28
+ * Follow migration chain with cycle detection
29
+ * @param startAddress - Starting address to look up
30
+ * @param getAccount - Function to get account by address
31
+ * @param normalizeAddress - Function to normalize address format
32
+ * @param visited - Set of already visited addresses (for cycle detection)
33
+ * @returns The final account after following migration chain, or null if cycle/missing
34
+ */
35
+ async function followMigrationChain(startAddress, getAccount, normalizeAddress, visited = /* @__PURE__ */ new Set()) {
36
+ const normalizedAddress = normalizeAddress(startAddress);
37
+ if (visited.has(normalizedAddress)) return null;
38
+ visited.add(normalizedAddress);
39
+ const account = await getAccount(normalizedAddress);
40
+ if (account?.migratedTo?.length) return followMigrationChain(account.migratedTo[0], getAccount, normalizeAddress, visited);
41
+ return account;
42
+ }
43
+
44
+ //#endregion
45
+ export { followMigrationChain, wouldCreateMigrationCycle };
package/esm/table.mjs CHANGED
@@ -28,7 +28,7 @@ var StateDBTable = class extends Ready {
28
28
  "reset"
29
29
  ].forEach((x) => {
30
30
  Object.defineProperty(this, x, { value: async (...args) => {
31
- if (typeof args[0] === "string" && isValid(args[0])) args[0] = toAddress(args[0]);
31
+ if (typeof args[0] === "string" && (isValid(args[0]) || args[0].startsWith("did:abt:"))) args[0] = toAddress(args[0]);
32
32
  hooks.execPreSync(x, args);
33
33
  const result = await this[`_${x}`](...args);
34
34
  hooks.execPostSync(x, result);
package/lib/index.cjs CHANGED
@@ -1,5 +1,8 @@
1
1
  const require_db = require('./db.cjs');
2
2
  const require_table = require('./table.cjs');
3
+ const require_migration_utils = require('./migration-utils.cjs');
3
4
 
4
5
  exports.StateDB = require_db.default;
5
- exports.StateDBTable = require_table.default;
6
+ exports.StateDBTable = require_table.default;
7
+ exports.followMigrationChain = require_migration_utils.followMigrationChain;
8
+ exports.wouldCreateMigrationCycle = require_migration_utils.wouldCreateMigrationCycle;
package/lib/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
1
  import StateDB from "./db.cjs";
2
2
  import StateDBTable from "./table.cjs";
3
- export { StateDB, StateDBTable };
3
+ import { followMigrationChain, wouldCreateMigrationCycle } from "./migration-utils.cjs";
4
+ export { StateDB, StateDBTable, followMigrationChain, wouldCreateMigrationCycle };
@@ -0,0 +1,47 @@
1
+
2
+ //#region src/migration-utils.ts
3
+ /**
4
+ * Check if adding migration targets would create a cycle
5
+ * @param sourceAddress - The address that will have migratedTo set
6
+ * @param targets - The migration targets to check
7
+ * @param getAccount - Function to get account by address (without following migration)
8
+ * @param normalizeAddress - Function to normalize address format
9
+ * @returns true if a cycle would be created
10
+ */
11
+ async function wouldCreateMigrationCycle(sourceAddress, targets, getAccount, normalizeAddress) {
12
+ const normalizedSource = normalizeAddress(sourceAddress);
13
+ const visited = new Set([normalizedSource]);
14
+ const checkTarget = async (target) => {
15
+ const normalizedTarget = normalizeAddress(target);
16
+ if (normalizedTarget === normalizedSource) return true;
17
+ if (visited.has(normalizedTarget)) return false;
18
+ visited.add(normalizedTarget);
19
+ const targetAccount = await getAccount(normalizedTarget);
20
+ if (targetAccount?.migratedTo?.length) {
21
+ for (const nextTarget of targetAccount.migratedTo) if (await checkTarget(nextTarget)) return true;
22
+ }
23
+ return false;
24
+ };
25
+ for (const target of targets) if (await checkTarget(target)) return true;
26
+ return false;
27
+ }
28
+ /**
29
+ * Follow migration chain with cycle detection
30
+ * @param startAddress - Starting address to look up
31
+ * @param getAccount - Function to get account by address
32
+ * @param normalizeAddress - Function to normalize address format
33
+ * @param visited - Set of already visited addresses (for cycle detection)
34
+ * @returns The final account after following migration chain, or null if cycle/missing
35
+ */
36
+ async function followMigrationChain(startAddress, getAccount, normalizeAddress, visited = /* @__PURE__ */ new Set()) {
37
+ const normalizedAddress = normalizeAddress(startAddress);
38
+ if (visited.has(normalizedAddress)) return null;
39
+ visited.add(normalizedAddress);
40
+ const account = await getAccount(normalizedAddress);
41
+ if (account?.migratedTo?.length) return followMigrationChain(account.migratedTo[0], getAccount, normalizeAddress, visited);
42
+ return account;
43
+ }
44
+
45
+ //#endregion
46
+ exports.followMigrationChain = followMigrationChain;
47
+ exports.wouldCreateMigrationCycle = wouldCreateMigrationCycle;
@@ -0,0 +1,28 @@
1
+ //#region src/migration-utils.d.ts
2
+ /**
3
+ * Migration utility functions for StateDB
4
+ * Shared logic for circular migration detection
5
+ */
6
+ type AccountWithMigration = {
7
+ migratedTo?: string[];
8
+ };
9
+ /**
10
+ * Check if adding migration targets would create a cycle
11
+ * @param sourceAddress - The address that will have migratedTo set
12
+ * @param targets - The migration targets to check
13
+ * @param getAccount - Function to get account by address (without following migration)
14
+ * @param normalizeAddress - Function to normalize address format
15
+ * @returns true if a cycle would be created
16
+ */
17
+ declare function wouldCreateMigrationCycle<T extends AccountWithMigration>(sourceAddress: string, targets: string[], getAccount: (address: string) => Promise<T | null>, normalizeAddress: (addr: string) => string): Promise<boolean>;
18
+ /**
19
+ * Follow migration chain with cycle detection
20
+ * @param startAddress - Starting address to look up
21
+ * @param getAccount - Function to get account by address
22
+ * @param normalizeAddress - Function to normalize address format
23
+ * @param visited - Set of already visited addresses (for cycle detection)
24
+ * @returns The final account after following migration chain, or null if cycle/missing
25
+ */
26
+ declare function followMigrationChain<T extends AccountWithMigration>(startAddress: string, getAccount: (address: string) => Promise<T | null>, normalizeAddress: (addr: string) => string, visited?: Set<string>): Promise<T | null>;
27
+ //#endregion
28
+ export { followMigrationChain, wouldCreateMigrationCycle };
package/lib/table.cjs CHANGED
@@ -34,7 +34,7 @@ var StateDBTable = class extends _ocap_util_lib_ready.Ready {
34
34
  "reset"
35
35
  ].forEach((x) => {
36
36
  Object.defineProperty(this, x, { value: async (...args) => {
37
- if (typeof args[0] === "string" && (0, _arcblock_did.isValid)(args[0])) args[0] = (0, _ocap_util.toAddress)(args[0]);
37
+ if (typeof args[0] === "string" && ((0, _arcblock_did.isValid)(args[0]) || args[0].startsWith("did:abt:"))) args[0] = (0, _ocap_util.toAddress)(args[0]);
38
38
  hooks.execPreSync(x, args);
39
39
  const result = await this[`_${x}`](...args);
40
40
  hooks.execPostSync(x, result);
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.29.3",
6
+ "version": "1.29.5",
7
7
  "description": "Defines the basic interface for OCAP StateDB",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -43,10 +43,10 @@
43
43
  "license": "MIT",
44
44
  "devDependencies": {},
45
45
  "dependencies": {
46
- "@arcblock/did": "1.29.3",
47
- "@ocap/state": "1.29.3",
48
- "@ocap/types": "1.29.3",
49
- "@ocap/util": "1.29.3",
46
+ "@arcblock/did": "1.29.5",
47
+ "@ocap/state": "1.29.5",
48
+ "@ocap/types": "1.29.5",
49
+ "@ocap/util": "1.29.5",
50
50
  "kareem": "^2.4.1",
51
51
  "lodash": "^4.17.23"
52
52
  }