@ic-reactor/core 2.0.0-alpha.0 → 2.0.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.
@@ -1,40 +1,55 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isQuery = exports.getProcessEnvNetwork = exports.isInLocalOrDevelopment = exports.importCandidDefinition = exports.createStoreWithOptionalDevtools = void 0;
13
- const agent_1 = require("@dfinity/agent");
3
+ exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.getProcessEnvNetwork = exports.isInLocalOrDevelopment = exports.importCandidDefinition = void 0;
4
+ exports.noop = noop;
5
+ exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
6
+ exports.getNetworkByHostname = getNetworkByHostname;
7
+ exports.isQuery = isQuery;
8
+ exports.createCompiledResult = createCompiledResult;
9
+ exports.extractOkResult = extractOkResult;
10
+ const sha2_1 = require("@noble/hashes/sha2");
11
+ const utils_1 = require("@noble/hashes/utils");
14
12
  const middleware_1 = require("zustand/middleware");
15
- const vanilla_1 = require("zustand/vanilla");
13
+ const zustand_1 = require("zustand");
14
+ const hash_1 = require("./hash");
15
+ const constants_1 = require("./constants");
16
+ /**
17
+ * No operation function that does nothing.
18
+ * It can be used as a placeholder or for logging purposes.
19
+ */
20
+ function noop() {
21
+ // eslint-disable-next-line no-console
22
+ console.warn("No operation function called");
23
+ }
24
+ /**
25
+ * Creates a Zustand store with optional DevTools middleware.
26
+ *
27
+ * @param initialState - The initial state of the store.
28
+ * @param config - Configuration options for DevTools.
29
+ * @returns A Zustand store with DevTools enabled if configured, otherwise a standard store.
30
+ */
16
31
  function createStoreWithOptionalDevtools(initialState, config) {
17
- if (config.withDevtools) {
18
- return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, Object.assign({ serialize: {
19
- replacer: (_, value) => typeof value === "bigint" ? value.toString() : value,
20
- } }, config)));
21
- }
22
- else {
23
- return (0, vanilla_1.createStore)(() => initialState);
24
- }
32
+ const createState = () => initialState;
33
+ return (0, zustand_1.createStore)((0, middleware_1.subscribeWithSelector)((0, middleware_1.devtools)(createState, {
34
+ enabled: !!config.withDevtools,
35
+ serialize: {
36
+ replacer: (_, value) => typeof value === "bigint" ? value.toString() : value,
37
+ },
38
+ ...config,
39
+ })));
25
40
  }
26
- exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
27
- const importCandidDefinition = (candidDef) => __awaiter(void 0, void 0, void 0, function* () {
41
+ const importCandidDefinition = async (candidDef) => {
28
42
  if (typeof window === "undefined") {
29
- // Node.js environment
43
+ // Node.js environment - use dynamic evaluation
30
44
  try {
31
- const loaderFunction = new Function(`
32
- return import("data:text/javascript;charset=utf-8, ${encodeURIComponent(candidDef)}")
33
- `);
34
- return loaderFunction();
45
+ const moduleExports = {};
46
+ const moduleCode = candidDef.replace(/export const /g, "moduleExports.");
47
+ const func = new Function("moduleExports", moduleCode);
48
+ func(moduleExports);
49
+ return moduleExports;
35
50
  }
36
51
  catch (error) {
37
- throw new Error(`Error importing candid definition in NodeJs: ${error}`);
52
+ throw new Error(`Error importing candid definition in NodeJs: ${error.message}`);
38
53
  }
39
54
  }
40
55
  else {
@@ -51,49 +66,133 @@ const importCandidDefinition = (candidDef) => __awaiter(void 0, void 0, void 0,
51
66
  throw new Error(`Error importing candid definition: ${error}`);
52
67
  }
53
68
  }
54
- });
69
+ };
55
70
  exports.importCandidDefinition = importCandidDefinition;
71
+ /**
72
+ * Checks if the current environment is local or development.
73
+ *
74
+ * @returns `true` if running in a local or development environment, otherwise `false`.
75
+ */
56
76
  const isInLocalOrDevelopment = () => {
57
77
  return typeof process !== "undefined" && process.env.DFX_NETWORK === "local";
58
78
  };
59
79
  exports.isInLocalOrDevelopment = isInLocalOrDevelopment;
80
+ /**
81
+ * Retrieves the network from the process environment variables.
82
+ *
83
+ * @returns The network name, defaulting to "ic" if not specified.
84
+ */
60
85
  const getProcessEnvNetwork = () => {
61
- var _a;
62
86
  if (typeof process === "undefined")
63
87
  return "ic";
64
88
  else
65
- return (_a = process.env.DFX_NETWORK) !== null && _a !== void 0 ? _a : "ic";
89
+ return process.env.DFX_NETWORK ?? "ic";
66
90
  };
67
91
  exports.getProcessEnvNetwork = getProcessEnvNetwork;
92
+ /**
93
+ * Determines the network type based on the provided hostname.
94
+ *
95
+ * @param hostname - The hostname to evaluate.
96
+ * @returns A string indicating the network type: "local", "remote", or "ic".
97
+ */
98
+ function getNetworkByHostname(hostname) {
99
+ if (constants_1.LOCAL_HOSTS.some((host) => hostname.endsWith(host))) {
100
+ return "local";
101
+ }
102
+ else if (constants_1.REMOTE_HOSTS.some((host) => hostname.endsWith(host))) {
103
+ return "remote";
104
+ }
105
+ else {
106
+ return "ic";
107
+ }
108
+ }
109
+ /**
110
+ * Checks if a given IDL function is a query.
111
+ *
112
+ * @param func - The IDL function to check.
113
+ * @returns `true` if the function is a query or composite query, otherwise `false`.
114
+ */
68
115
  function isQuery(func) {
69
116
  return (func.annotations.includes("query") ||
70
117
  func.annotations.includes("composite_query"));
71
118
  }
72
- exports.isQuery = isQuery;
73
119
  const jsonToString = (json, space = 2) => {
74
120
  return JSON.stringify(json, (_, value) => (typeof value === "bigint" ? `BigInt(${value})` : value), space);
75
121
  };
76
122
  exports.jsonToString = jsonToString;
77
123
  const generateRequestHash = (args = []) => {
78
- const serializedArgs = (0, agent_1.hashValue)(args);
79
- return `0x${(0, agent_1.toHex)(serializedArgs)}`;
124
+ const serializedArgs = (0, hash_1.createSimpleHash)(args);
125
+ return `0x${serializedArgs}`;
80
126
  };
81
127
  exports.generateRequestHash = generateRequestHash;
82
128
  const generateHash = (field) => {
83
129
  const serializedArgs = JSON.stringify(field);
84
- return (0, exports.stringToHash)(serializedArgs !== null && serializedArgs !== void 0 ? serializedArgs : "");
130
+ return (0, exports.stringToHash)(serializedArgs ?? "");
85
131
  };
86
132
  exports.generateHash = generateHash;
87
133
  const generateActorHash = (actor) => {
88
134
  const serializedArgs = JSON.stringify(actor);
89
- return (0, exports.stringToHash)(serializedArgs !== null && serializedArgs !== void 0 ? serializedArgs : "");
135
+ return (0, exports.stringToHash)(serializedArgs ?? "");
90
136
  };
91
137
  exports.generateActorHash = generateActorHash;
92
138
  const stringToHash = (str) => {
93
- const hashBytes = (0, agent_1.hash)(new TextEncoder().encode(str));
94
- return `0x${toHexString(hashBytes)}`;
139
+ const hashBytes = (0, sha2_1.sha256)(str);
140
+ return `0x${(0, utils_1.bytesToHex)(hashBytes)}`;
95
141
  };
96
142
  exports.stringToHash = stringToHash;
97
- function toHexString(bytes) {
98
- return (0, agent_1.toHex)(bytes);
143
+ /**
144
+ * Helper function for extracting the value from a compiled result { Ok: T } or { Err: E }
145
+ *
146
+ * @param result - The compiled result to extract from.
147
+ * @returns A `CompiledResult` object indicating success or failure.
148
+ */
149
+ function createCompiledResult(result) {
150
+ if (result && typeof result === "object" && "Ok" in result) {
151
+ return {
152
+ isOk: true,
153
+ isErr: false,
154
+ value: result.Ok,
155
+ error: null,
156
+ };
157
+ }
158
+ else if (result && typeof result === "object" && "Err" in result) {
159
+ return {
160
+ isOk: false,
161
+ isErr: true,
162
+ value: null,
163
+ error: result.Err,
164
+ };
165
+ }
166
+ else if (result) {
167
+ // For non-Result types
168
+ return {
169
+ isOk: true,
170
+ isErr: false,
171
+ value: result,
172
+ error: null,
173
+ };
174
+ }
175
+ else {
176
+ // For non-Result types
177
+ return {
178
+ isOk: false,
179
+ isErr: false,
180
+ value: undefined,
181
+ error: null,
182
+ };
183
+ }
184
+ }
185
+ /**
186
+ * Helper function for extracting the value from a compiled result { Ok: T } or throw the error if { Err: E }
187
+ *
188
+ * @param result - The compiled result to extract from.
189
+ * @returns The extracted value from the compiled result.
190
+ * @throws The error from the compiled result.
191
+ */
192
+ function extractOkResult(result) {
193
+ const compiledResult = createCompiledResult(result);
194
+ if (compiledResult.isErr) {
195
+ throw compiledResult.error;
196
+ }
197
+ return compiledResult.value;
99
198
  }
@@ -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";
@@ -18,17 +18,28 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
18
18
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
19
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
20
  };
21
- var __importStar = (this && this.__importStar) || function (mod) {
22
- if (mod && mod.__esModule) return mod;
23
- var result = {};
24
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25
- __setModuleDefault(result, mod);
26
- return result;
27
- };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
28
38
  Object.defineProperty(exports, "__esModule", { value: true });
29
39
  exports.agent = exports.principal = exports.candid = void 0;
30
40
  __exportStar(require("./helper"), exports);
31
41
  __exportStar(require("./constants"), exports);
42
+ __exportStar(require("./hash"), exports);
32
43
  // Re-export the peerDependencies
33
44
  /// https://agent-js.icp.xyz/candid
34
45
  exports.candid = __importStar(require("./candid"));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "2.0.0-alpha.0",
3
+ "version": "2.0.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,36 +25,36 @@
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",
41
- "zustand": "4.5.5"
29
+ "@dfinity/agent": "^3.1.0",
30
+ "@dfinity/auth-client": "^3.1.0",
31
+ "@dfinity/candid": "^3.1.0",
32
+ "@dfinity/identity": "^3.1.0",
33
+ "@dfinity/principal": "^3.1.0",
34
+ "zustand": "5.0.6"
35
+ },
36
+ "peerDependencies": {
37
+ "@dfinity/agent": ">=3.1.0",
38
+ "@dfinity/auth-client": ">=3.1.0",
39
+ "@dfinity/candid": ">=3.1.0",
40
+ "@dfinity/identity": ">=3.1.0",
41
+ "@dfinity/principal": ">=3.1.0"
42
42
  },
43
43
  "devDependencies": {
44
- "@ic-reactor/parser": "0.4.0"
44
+ "@ic-reactor/parser": "0.4.5"
45
45
  },
46
46
  "scripts": {
47
- "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
48
- "start": "tsc watch",
49
- "bundle": "yarn bundle:dev && yarn bundle:prod",
50
- "bundle:dev": "npx webpack-cli --mode development",
51
- "bundle:prod": "npx webpack-cli --mode production",
52
- "build:tsc": "tsc",
53
- "build": "yarn build:tsc && yarn bundle",
54
- "clean": "npx rimraf dist && npx rimraf umd && npx rimraf node_modules"
47
+ "test": "bun test",
48
+ "start": "bun run tsc --watch",
49
+ "build:tsc": "bun run tsc",
50
+ "build": "bun run build:tsc && bun run bundle",
51
+ "bundle": "bun run bundle:dev && bun run bundle:prod",
52
+ "bundle:dev": "bun run webpack-cli --mode development",
53
+ "bundle:prod": "bun run webpack-cli --mode production",
54
+ "clean": "bun run rimraf dist && bun run rimraf umd && bun run rimraf node_modules"
55
55
  },
56
56
  "engines": {
57
- "node": ">=10"
57
+ "node": ">=22.0.0"
58
58
  },
59
- "gitHead": "081197439510ef0327c5a807e0e35ec7aa841fd4"
59
+ "gitHead": "2677a090df726dc4f4216eb406b135a32334507b"
60
60
  }