@ic-reactor/core 1.11.0 → 1.14.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/README.md CHANGED
@@ -19,7 +19,7 @@ yarn add @ic-reactor/core
19
19
  or you can use the UMD version:
20
20
 
21
21
  ```html
22
- <script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.10.3/ic-reactor-core.min.js"></script>
22
+ <script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.12.0/ic-reactor-core.min.js"></script>
23
23
  ```
24
24
 
25
25
  ### Using `createReactorCore`
@@ -14,11 +14,7 @@ export declare class ActorManager<A = BaseActor> {
14
14
  visitFunction: VisitService<A>;
15
15
  methodAttributes: MethodAttributes<A>;
16
16
  private updateState;
17
- updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<{
18
- data: ActorMethodReturnType<A[FunctionName<A>]> | undefined;
19
- loading: boolean;
20
- error: Error | undefined;
21
- }>) => void;
17
+ updateMethodState: (method: FunctionName<A>, hash: string, newState: Partial<ActorMethodState<A, typeof method>[string]>) => void;
22
18
  constructor(actorConfig: ActorManagerParameters);
23
19
  initialize: (options?: UpdateAgentParameters) => Promise<void>;
24
20
  extractInterface: () => IDL.ServiceClass;
@@ -23,12 +23,13 @@ class ActorManager {
23
23
  this.actorStore.setState((state) => (Object.assign(Object.assign({}, state), newState)), false, action);
24
24
  };
25
25
  this.updateMethodState = (method, hash, newState) => {
26
+ const actionName = `${method}:${newState.error ? "error" : newState.loading ? "loading" : "loaded"}`;
26
27
  this.actorStore.setState((state) => {
27
28
  const methodState = state.methodState[method] || {};
28
29
  const currentMethodState = methodState[hash] || DEFAULT_STATE;
29
30
  const updatedMethodState = Object.assign(Object.assign({}, methodState), { [hash]: Object.assign(Object.assign({}, currentMethodState), newState) });
30
31
  return Object.assign(Object.assign({}, state), { methodState: Object.assign(Object.assign({}, state.methodState), { [method]: updatedMethodState }) });
31
- }, false, method);
32
+ }, false, actionName);
32
33
  };
33
34
  this.initialize = (options) => __awaiter(this, void 0, void 0, function* () {
34
35
  yield this._agentManager.updateAgent(options);
@@ -122,9 +122,9 @@ class AgentManager {
122
122
  yield this._auth.login(Object.assign(Object.assign({ identityProvider: this.getIsLocal()
123
123
  ? constants_1.LOCAL_INTERNET_IDENTITY_PROVIDER
124
124
  : constants_1.IC_INTERNET_IDENTITY_PROVIDER }, options), { onSuccess: (msg) => __awaiter(this, void 0, void 0, function* () {
125
- var _c;
125
+ var _a;
126
126
  yield this.authenticate();
127
- (_c = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _c === void 0 ? void 0 : _c.call(options, msg);
127
+ (_a = options === null || options === void 0 ? void 0 : options.onSuccess) === null || _a === void 0 ? void 0 : _a.call(options, msg);
128
128
  }) }));
129
129
  });
130
130
  this.logout = (options) => __awaiter(this, void 0, void 0, function* () {
@@ -150,15 +150,7 @@ class AgentManager {
150
150
  };
151
151
  this.getNetwork = () => {
152
152
  const hostname = this.getAgentHostName();
153
- if (constants_1.LOCAL_HOSTS.some((host) => hostname.endsWith(host))) {
154
- return "local";
155
- }
156
- else if (constants_1.REMOTE_HOSTS.some((host) => hostname.endsWith(host))) {
157
- return "remote";
158
- }
159
- else {
160
- return "ic";
161
- }
153
+ return (0, helper_1.getNetworkByHostname)(hostname);
162
154
  };
163
155
  this.getAgentState = () => {
164
156
  return this.agentStore.getState();
package/dist/types.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { ActorMethod, ActorSubclass, HttpAgentOptions, HttpAgent, Identity, CallConfig } from "@dfinity/agent";
3
2
  import type { Principal } from "@dfinity/principal";
4
3
  import type { IDL } from "@dfinity/candid";
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Creates a stable string representation of any JavaScript value
3
+ * Handles circular references and maintains consistent object key ordering
4
+ */
5
+ export declare function stringifyStable(value: unknown): string;
6
+ /**
7
+ * Creates a simple numeric hash code and returns it as a hex string
8
+ * @param value - Any JavaScript value
9
+ * @param length - Desired length of the hex string (default: 8)
10
+ * @returns string - Hex string of specified length
11
+ */
12
+ export declare function createSimpleHash(value: unknown, length?: number): string;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.stringifyStable = stringifyStable;
4
+ exports.createSimpleHash = createSimpleHash;
5
+ /**
6
+ * Creates a stable string representation of any JavaScript value
7
+ * Handles circular references and maintains consistent object key ordering
8
+ */
9
+ function stringifyStable(value) {
10
+ const seen = new WeakSet();
11
+ return JSON.stringify(value, (_, value) => {
12
+ if (value === undefined)
13
+ return "[undefined]";
14
+ if (value === null)
15
+ return "[null]";
16
+ if (Number.isNaN(value))
17
+ return "[NaN]";
18
+ if (value === Infinity)
19
+ return "[Infinity]";
20
+ if (value === -Infinity)
21
+ return "[-Infinity]";
22
+ if (typeof value === "bigint")
23
+ return value.toString();
24
+ if (typeof value === "function")
25
+ return value.toString();
26
+ if (value instanceof Date)
27
+ return value.toISOString();
28
+ if (value instanceof RegExp)
29
+ return value.toString();
30
+ if (ArrayBuffer.isView(value)) {
31
+ return Array.from(value).join(",");
32
+ }
33
+ if (typeof value === "object" && value !== null) {
34
+ if (seen.has(value))
35
+ return "[Circular]";
36
+ seen.add(value);
37
+ if (Array.isArray(value)) {
38
+ return value;
39
+ }
40
+ const sortedObj = {};
41
+ const sortedKeys = Object.keys(value).sort();
42
+ for (const key of sortedKeys) {
43
+ sortedObj[key] = value[key];
44
+ }
45
+ return sortedObj;
46
+ }
47
+ return value;
48
+ });
49
+ }
50
+ /**
51
+ * Creates a simple numeric hash code and returns it as a hex string
52
+ * @param value - Any JavaScript value
53
+ * @param length - Desired length of the hex string (default: 8)
54
+ * @returns string - Hex string of specified length
55
+ */
56
+ function createSimpleHash(value, length = 8) {
57
+ const str = stringifyStable(value);
58
+ let hash = 0;
59
+ // Generate a more distributed hash
60
+ for (let i = 0; i < str.length; i++) {
61
+ const char = str.charCodeAt(i);
62
+ hash = (hash << 5) - hash + char;
63
+ hash = hash & hash; // Convert to 32-bit integer
64
+ }
65
+ // Convert to positive hex string and ensure proper length
66
+ const positiveHash = Math.abs(hash);
67
+ const hexString = positiveHash.toString(16);
68
+ // Pad with zeros to match desired length
69
+ return hexString.padStart(length, "0");
70
+ }
@@ -8,6 +8,7 @@ export declare function createStoreWithOptionalDevtools<T>(initialState: T, conf
8
8
  export declare const importCandidDefinition: (candidDef: string) => Promise<CandidDefenition>;
9
9
  export declare const isInLocalOrDevelopment: () => boolean;
10
10
  export declare const getProcessEnvNetwork: () => string;
11
+ export declare function getNetworkByHostname(hostname: string): "local" | "remote" | "ic";
11
12
  export declare function isQuery(func: IDL.FuncClass): boolean;
12
13
  export declare const jsonToString: (json: unknown, space?: number) => string;
13
14
  export declare const generateRequestHash: (args?: unknown[]) => `0x${string}`;
@@ -9,10 +9,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.createCompiledResult = exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isQuery = exports.getProcessEnvNetwork = exports.isInLocalOrDevelopment = exports.importCandidDefinition = exports.createStoreWithOptionalDevtools = void 0;
12
+ exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.getProcessEnvNetwork = exports.isInLocalOrDevelopment = exports.importCandidDefinition = void 0;
13
+ exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
14
+ exports.getNetworkByHostname = getNetworkByHostname;
15
+ exports.isQuery = isQuery;
16
+ exports.createCompiledResult = createCompiledResult;
13
17
  const agent_1 = require("@dfinity/agent");
14
18
  const middleware_1 = require("zustand/middleware");
15
19
  const vanilla_1 = require("zustand/vanilla");
20
+ const hash_1 = require("./hash");
21
+ const constants_1 = require("./constants");
16
22
  function createStoreWithOptionalDevtools(initialState, config) {
17
23
  if (config.withDevtools) {
18
24
  return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, Object.assign({ serialize: {
@@ -23,7 +29,6 @@ function createStoreWithOptionalDevtools(initialState, config) {
23
29
  return (0, vanilla_1.createStore)(() => initialState);
24
30
  }
25
31
  }
26
- exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
27
32
  const importCandidDefinition = (candidDef) => __awaiter(void 0, void 0, void 0, function* () {
28
33
  if (typeof window === "undefined") {
29
34
  // Node.js environment
@@ -65,18 +70,28 @@ const getProcessEnvNetwork = () => {
65
70
  return (_a = process.env.DFX_NETWORK) !== null && _a !== void 0 ? _a : "ic";
66
71
  };
67
72
  exports.getProcessEnvNetwork = getProcessEnvNetwork;
73
+ function getNetworkByHostname(hostname) {
74
+ if (constants_1.LOCAL_HOSTS.some((host) => hostname.endsWith(host))) {
75
+ return "local";
76
+ }
77
+ else if (constants_1.REMOTE_HOSTS.some((host) => hostname.endsWith(host))) {
78
+ return "remote";
79
+ }
80
+ else {
81
+ return "ic";
82
+ }
83
+ }
68
84
  function isQuery(func) {
69
85
  return (func.annotations.includes("query") ||
70
86
  func.annotations.includes("composite_query"));
71
87
  }
72
- exports.isQuery = isQuery;
73
88
  const jsonToString = (json, space = 2) => {
74
89
  return JSON.stringify(json, (_, value) => (typeof value === "bigint" ? `BigInt(${value})` : value), space);
75
90
  };
76
91
  exports.jsonToString = jsonToString;
77
92
  const generateRequestHash = (args = []) => {
78
- const serializedArgs = (0, agent_1.hashValue)(args);
79
- return `0x${(0, agent_1.toHex)(serializedArgs)}`;
93
+ const serializedArgs = (0, hash_1.createSimpleHash)(args);
94
+ return `0x${serializedArgs}`;
80
95
  };
81
96
  exports.generateRequestHash = generateRequestHash;
82
97
  const generateHash = (field) => {
@@ -125,4 +140,3 @@ function createCompiledResult(result) {
125
140
  };
126
141
  }
127
142
  }
128
- exports.createCompiledResult = createCompiledResult;
@@ -1,5 +1,6 @@
1
1
  export * from "./helper";
2
2
  export * from "./constants";
3
+ export * from "./hash";
3
4
  export * as candid from "./candid";
4
5
  export * as principal from "./principal";
5
6
  export * as agent from "./agent";
@@ -29,6 +29,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.agent = exports.principal = exports.candid = void 0;
30
30
  __exportStar(require("./helper"), exports);
31
31
  __exportStar(require("./constants"), exports);
32
+ __exportStar(require("./hash"), exports);
32
33
  // Re-export the peerDependencies
33
34
  /// https://agent-js.icp.xyz/candid
34
35
  exports.candid = __importStar(require("./candid"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.11.0",
3
+ "version": "1.14.0",
4
4
  "description": "A library for intracting with the Internet Computer canisters",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -25,23 +25,24 @@
25
25
  "url": "https://github.com/b3hr4d/ic-reactor/issues"
26
26
  },
27
27
  "homepage": "https://b3pay.github.io/ic-reactor/modules/core.html",
28
- "peerDependencies": {
29
- "@dfinity/agent": ">=2.1.2",
30
- "@dfinity/auth-client": ">=2.1.2",
31
- "@dfinity/candid": ">=2.1.2",
32
- "@dfinity/identity": ">=2.1.2",
33
- "@dfinity/principal": ">=2.1.2"
34
- },
35
28
  "dependencies": {
36
- "@dfinity/agent": ">=2.0.0",
37
- "@dfinity/auth-client": ">=2.0.0",
38
- "@dfinity/candid": ">=2.0.0",
39
- "@dfinity/identity": ">=2.0.0",
40
- "@dfinity/principal": ">=2.0.0",
29
+ "@dfinity/agent": ">=2.1",
30
+ "@dfinity/auth-client": ">=2.1",
31
+ "@dfinity/candid": ">=2.1",
32
+ "@dfinity/identity": ">=2.1",
33
+ "@dfinity/principal": ">=2.1",
41
34
  "zustand": "4.5.5"
42
35
  },
36
+ "peerDependencies": {
37
+ "@dfinity/agent": ">=2.1",
38
+ "@dfinity/auth-client": ">=2.1",
39
+ "@dfinity/candid": ">=2.1",
40
+ "@dfinity/identity": ">=2.1",
41
+ "@dfinity/principal": ">=2.1"
42
+ },
43
43
  "devDependencies": {
44
- "@ic-reactor/parser": "^0.4.1"
44
+ "@ic-reactor/parser": "^0.4.3",
45
+ "@types/node": "^22.9.0"
45
46
  },
46
47
  "scripts": {
47
48
  "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
@@ -56,5 +57,5 @@
56
57
  "engines": {
57
58
  "node": ">=10"
58
59
  },
59
- "gitHead": "03b85c107e062dc52e4feee4864ed78db5a97294"
60
+ "gitHead": "fee4b92574ba3fd295e28fb49105f0a07c6fdb61"
60
61
  }