@ic-reactor/core 1.5.4-beta.2 → 1.6.0-beta.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.
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.5.3/ic-reactor-core.min.js"></script>
22
+ <script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.5.4-beta.2/ic-reactor-core.min.js"></script>
23
23
  ```
24
24
 
25
25
  ### Using `createReactorCore`
@@ -3,13 +3,15 @@ import type { CanisterId, CandidAdapterParameters, CandidDefenition } from "../.
3
3
  export declare class CandidAdapter {
4
4
  agent: HttpAgent;
5
5
  didjsCanisterId: string;
6
+ private parserModule?;
6
7
  unsubscribeAgent: () => void;
7
8
  constructor({ agentManager, agent, didjsCanisterId, }: CandidAdapterParameters);
9
+ initializeParser(): Promise<void>;
8
10
  private getDefaultDidJsId;
9
11
  getCandidDefinition(canisterId: CanisterId): Promise<CandidDefenition>;
10
12
  getFromMetadata(canisterId: CanisterId): Promise<string | undefined>;
11
13
  getFromTmpHack(canisterId: CanisterId): Promise<string | undefined>;
12
- evaluateJs(data: string): Promise<CandidDefenition>;
14
+ dynamicEvalJs(data: string): Promise<CandidDefenition>;
13
15
  fetchDidTojs(candidSource: string): Promise<[string]>;
14
- parseDidToJs(candidSource: string): Promise<string>;
16
+ parseDidToJs(candidSource: string): string;
15
17
  }
@@ -35,6 +35,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
35
35
  exports.CandidAdapter = void 0;
36
36
  const agent_1 = require("@dfinity/agent");
37
37
  const constants_1 = require("../../utils/constants");
38
+ const utils_1 = require("../../utils");
38
39
  class CandidAdapter {
39
40
  constructor({ agentManager, agent, didjsCanisterId, }) {
40
41
  this.unsubscribeAgent = () => { };
@@ -53,6 +54,17 @@ class CandidAdapter {
53
54
  }
54
55
  this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
55
56
  }
57
+ initializeParser() {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ try {
60
+ this.parserModule = yield Promise.resolve().then(() => __importStar(require("@ic-reactor/parser")));
61
+ yield this.parserModule.default();
62
+ }
63
+ catch (error) {
64
+ throw new Error(`Error initializing parser: ${error}`);
65
+ }
66
+ });
67
+ }
56
68
  getDefaultDidJsId() {
57
69
  var _a, _b;
58
70
  return ((_b = (_a = this.agent).isLocal) === null || _b === void 0 ? void 0 : _b.call(_a)) === true
@@ -67,13 +79,13 @@ class CandidAdapter {
67
79
  return undefined;
68
80
  });
69
81
  if (fromMetadata)
70
- return this.evaluateJs(fromMetadata);
82
+ return this.dynamicEvalJs(fromMetadata);
71
83
  // Second attempt: Try the temporary hack method
72
84
  const fromTmpHack = yield this.getFromTmpHack(canisterId).catch(() => {
73
85
  return undefined;
74
86
  });
75
87
  if (fromTmpHack)
76
- return this.evaluateJs(fromTmpHack);
88
+ return this.dynamicEvalJs(fromTmpHack);
77
89
  // If both attempts fail, throw an error
78
90
  throw "Failed to retrieve Candid definition by any method.";
79
91
  }
@@ -104,12 +116,12 @@ class CandidAdapter {
104
116
  return (yield actor.__get_candid_interface_tmp_hack());
105
117
  });
106
118
  }
107
- evaluateJs(data) {
119
+ dynamicEvalJs(data) {
108
120
  return __awaiter(this, void 0, void 0, function* () {
109
121
  try {
110
122
  let candidDef = "";
111
123
  try {
112
- candidDef = yield this.parseDidToJs(data);
124
+ candidDef = this.parseDidToJs(data);
113
125
  }
114
126
  catch (error) {
115
127
  candidDef = (yield this.fetchDidTojs(data))[0];
@@ -117,11 +129,10 @@ class CandidAdapter {
117
129
  if (JSON.stringify(candidDef) === JSON.stringify([])) {
118
130
  throw new Error("Cannot compile Candid to JavaScript");
119
131
  }
120
- const dataUri = "data:text/javascript;charset=utf-8," + encodeURIComponent(candidDef);
121
- return eval('import("' + dataUri + '")');
132
+ return (0, utils_1.importCandidDefinition)(candidDef);
122
133
  }
123
134
  catch (error) {
124
- throw new Error("Error evaluating Candid definition");
135
+ throw new Error(`Error evaluating Candid definition: ${error}`);
125
136
  }
126
137
  });
127
138
  }
@@ -138,17 +149,10 @@ class CandidAdapter {
138
149
  });
139
150
  }
140
151
  parseDidToJs(candidSource) {
141
- return __awaiter(this, void 0, void 0, function* () {
142
- try {
143
- // This is a dynamic import if the module is available in the environment
144
- const parser = yield Promise.resolve().then(() => __importStar(require("@ic-reactor/parser")));
145
- yield parser.default();
146
- return parser.did_to_js(candidSource);
147
- }
148
- catch (error) {
149
- throw new Error("@ic-reactor/parser module is not available");
150
- }
151
- });
152
+ if (!this.parserModule) {
153
+ throw new Error("Parser module not available");
154
+ }
155
+ return this.parserModule.did_to_js(candidSource);
152
156
  }
153
157
  }
154
158
  exports.CandidAdapter = CandidAdapter;
@@ -5,6 +5,7 @@ export declare function createStoreWithOptionalDevtools<T>(initialState: T, conf
5
5
  type: string;
6
6
  }>(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: boolean | undefined, action?: A | undefined): void;
7
7
  };
8
+ export declare const importCandidDefinition: (candidDef: string) => any;
8
9
  export declare const isInLocalOrDevelopment: () => boolean;
9
10
  export declare const getProcessEnvNetwork: () => string;
10
11
  export declare function isQuery(func: IDL.FuncClass): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isQuery = exports.getProcessEnvNetwork = exports.isInLocalOrDevelopment = exports.createStoreWithOptionalDevtools = void 0;
3
+ exports.stringToHash = exports.generateActorHash = exports.generateHash = exports.generateRequestHash = exports.jsonToString = exports.isQuery = exports.getProcessEnvNetwork = exports.isInLocalOrDevelopment = exports.importCandidDefinition = exports.createStoreWithOptionalDevtools = void 0;
4
4
  const agent_1 = require("@dfinity/agent");
5
5
  const middleware_1 = require("zustand/middleware");
6
6
  const vanilla_1 = require("zustand/vanilla");
@@ -15,6 +15,16 @@ function createStoreWithOptionalDevtools(initialState, config) {
15
15
  }
16
16
  }
17
17
  exports.createStoreWithOptionalDevtools = createStoreWithOptionalDevtools;
18
+ const importCandidDefinition = (candidDef) => {
19
+ try {
20
+ const dataUri = "data:text/javascript;charset=utf-8," + encodeURIComponent(candidDef);
21
+ return eval('import("' + dataUri + '")');
22
+ }
23
+ catch (error) {
24
+ throw new Error("Error importing Candid definition");
25
+ }
26
+ };
27
+ exports.importCandidDefinition = importCandidDefinition;
18
28
  const isInLocalOrDevelopment = () => {
19
29
  return typeof process !== "undefined" && process.env.DFX_NETWORK === "local";
20
30
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ic-reactor/core",
3
- "version": "1.5.4-beta.2",
3
+ "version": "1.6.0-beta.1",
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",
@@ -40,6 +40,9 @@
40
40
  "@dfinity/principal": "^1.2",
41
41
  "zustand": "^4.5"
42
42
  },
43
+ "devDependencies": {
44
+ "@ic-reactor/parser": "^0.1.0-beta.1"
45
+ },
43
46
  "scripts": {
44
47
  "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" npx jest",
45
48
  "start": "tsc watch",
@@ -52,5 +55,5 @@
52
55
  "engines": {
53
56
  "node": ">=10"
54
57
  },
55
- "gitHead": "bf632e49a5a4d1128531a6fc50b5c2f97dee58c9"
58
+ "gitHead": "8ca72485b7651d5c8edfdcc9bcd2675f454856f1"
56
59
  }