@ic-reactor/core 1.5.4-beta.1 → 1.6.0-beta.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.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`
@@ -1,13 +1,18 @@
1
+ /// <reference path="../../../src/classes/candid/parser.d.ts" />
1
2
  import { HttpAgent } from "@dfinity/agent";
2
3
  import type { CanisterId, CandidAdapterParameters, CandidDefenition } from "../../types";
3
4
  export declare class CandidAdapter {
4
5
  agent: HttpAgent;
5
6
  didjsCanisterId: string;
7
+ parserModule?: typeof import("@ic-reactor/parser");
6
8
  unsubscribeAgent: () => void;
7
9
  constructor({ agentManager, agent, didjsCanisterId, }: CandidAdapterParameters);
10
+ initializeParser(): Promise<void>;
8
11
  private getDefaultDidJsId;
9
12
  getCandidDefinition(canisterId: CanisterId): Promise<CandidDefenition>;
10
- getFromMetadata(canisterId: CanisterId): Promise<CandidDefenition | undefined>;
11
- getFromTmpHack(canisterId: CanisterId): Promise<CandidDefenition | undefined>;
12
- didTojs(candidSource: string): Promise<CandidDefenition>;
13
+ getFromMetadata(canisterId: CanisterId): Promise<string | undefined>;
14
+ getFromTmpHack(canisterId: CanisterId): Promise<string | undefined>;
15
+ dynamicEvalJs(data: string): Promise<CandidDefenition>;
16
+ fetchDidTojs(candidSource: string): Promise<[string]>;
17
+ parseDidToJs(candidSource: string): string;
13
18
  }
@@ -1,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
26
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
27
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -12,6 +35,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
35
  exports.CandidAdapter = void 0;
13
36
  const agent_1 = require("@dfinity/agent");
14
37
  const constants_1 = require("../../utils/constants");
38
+ const utils_1 = require("../../utils");
15
39
  class CandidAdapter {
16
40
  constructor({ agentManager, agent, didjsCanisterId, }) {
17
41
  this.unsubscribeAgent = () => { };
@@ -30,6 +54,17 @@ class CandidAdapter {
30
54
  }
31
55
  this.didjsCanisterId = didjsCanisterId || this.getDefaultDidJsId();
32
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
+ }
33
68
  getDefaultDidJsId() {
34
69
  var _a, _b;
35
70
  return ((_b = (_a = this.agent).isLocal) === null || _b === void 0 ? void 0 : _b.call(_a)) === true
@@ -44,13 +79,13 @@ class CandidAdapter {
44
79
  return undefined;
45
80
  });
46
81
  if (fromMetadata)
47
- return fromMetadata;
82
+ return this.dynamicEvalJs(fromMetadata);
48
83
  // Second attempt: Try the temporary hack method
49
84
  const fromTmpHack = yield this.getFromTmpHack(canisterId).catch(() => {
50
85
  return undefined;
51
86
  });
52
87
  if (fromTmpHack)
53
- return fromTmpHack;
88
+ return this.dynamicEvalJs(fromTmpHack);
54
89
  // If both attempts fail, throw an error
55
90
  throw "Failed to retrieve Candid definition by any method.";
56
91
  }
@@ -66,8 +101,7 @@ class CandidAdapter {
66
101
  canisterId: canisterId,
67
102
  paths: ["candid"],
68
103
  });
69
- const did = status.get("candid");
70
- return did ? this.didTojs(did) : undefined;
104
+ return status.get("candid");
71
105
  });
72
106
  }
73
107
  getFromTmpHack(canisterId) {
@@ -79,11 +113,30 @@ class CandidAdapter {
79
113
  agent: this.agent,
80
114
  canisterId,
81
115
  });
82
- const data = (yield actor.__get_candid_interface_tmp_hack());
83
- return data ? this.didTojs(data) : undefined;
116
+ return (yield actor.__get_candid_interface_tmp_hack());
84
117
  });
85
118
  }
86
- didTojs(candidSource) {
119
+ dynamicEvalJs(data) {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ try {
122
+ let candidDef = "";
123
+ try {
124
+ candidDef = this.parseDidToJs(data);
125
+ }
126
+ catch (error) {
127
+ candidDef = (yield this.fetchDidTojs(data))[0];
128
+ }
129
+ if (JSON.stringify(candidDef) === JSON.stringify([])) {
130
+ throw new Error("Cannot compile Candid to JavaScript");
131
+ }
132
+ return (0, utils_1.importCandidDefinition)(candidDef);
133
+ }
134
+ catch (error) {
135
+ throw new Error(`Error evaluating Candid definition: ${error}`);
136
+ }
137
+ });
138
+ }
139
+ fetchDidTojs(candidSource) {
87
140
  return __awaiter(this, void 0, void 0, function* () {
88
141
  const didjsInterface = ({ IDL }) => IDL.Service({
89
142
  did_to_js: IDL.Func([IDL.Text], [IDL.Opt(IDL.Text)], ["query"]),
@@ -92,14 +145,14 @@ class CandidAdapter {
92
145
  agent: this.agent,
93
146
  canisterId: this.didjsCanisterId,
94
147
  });
95
- const js = yield didjs.did_to_js(candidSource);
96
- if (JSON.stringify(js) === JSON.stringify([])) {
97
- throw new Error("Cannot compile Candid to JavaScript");
98
- }
99
- const dataUri = "data:text/javascript;charset=utf-8," +
100
- encodeURIComponent(js[0]);
101
- return eval('import("' + dataUri + '")');
148
+ return didjs.did_to_js(candidSource);
102
149
  });
103
150
  }
151
+ parseDidToJs(candidSource) {
152
+ if (!this.parserModule) {
153
+ throw new Error("Parser module not available");
154
+ }
155
+ return this.parserModule.did_to_js(candidSource);
156
+ }
104
157
  }
105
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.1",
3
+ "version": "1.6.0-beta.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",
@@ -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.0"
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": "8ebd455fbb68d838b5bd5a477c004306560397bb"
58
+ "gitHead": "4911f0639b420406550150f789f34ecf3e6167c1"
56
59
  }