@ic-reactor/core 1.10.3 → 1.12.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 +1 -1
- package/dist/types.d.ts +33 -0
- package/dist/utils/hash.d.ts +12 -0
- package/dist/utils/hash.js +71 -0
- package/dist/utils/helper.d.ts +2 -1
- package/dist/utils/helper.js +33 -3
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/package.json +3 -3
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.
|
|
22
|
+
<script src="https://github.com/B3Pay/ic-reactor/releases/download/v1.11.0/ic-reactor-core.min.js"></script>
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
### Using `createReactorCore`
|
package/dist/types.d.ts
CHANGED
|
@@ -55,3 +55,36 @@ export interface CreateReactorCoreReturnType<A = BaseActor> extends AgentManager
|
|
|
55
55
|
queryCall: ActorQuery<A>;
|
|
56
56
|
updateCall: ActorUpdate<A>;
|
|
57
57
|
}
|
|
58
|
+
export type UnwrapResult<T> = T extends {
|
|
59
|
+
Ok: infer U;
|
|
60
|
+
} ? U : T extends {
|
|
61
|
+
Err: infer E;
|
|
62
|
+
} ? E : T;
|
|
63
|
+
type ExtractOkErr<T> = T extends {
|
|
64
|
+
Ok: infer U;
|
|
65
|
+
} ? {
|
|
66
|
+
OkType: U;
|
|
67
|
+
ErrType: never;
|
|
68
|
+
} : T extends {
|
|
69
|
+
Err: infer E;
|
|
70
|
+
} ? {
|
|
71
|
+
OkType: never;
|
|
72
|
+
ErrType: E;
|
|
73
|
+
} : {
|
|
74
|
+
OkType: T;
|
|
75
|
+
ErrType: never;
|
|
76
|
+
};
|
|
77
|
+
export type CompiledResult<T> = ExtractOkErr<T> extends {
|
|
78
|
+
OkType: infer U;
|
|
79
|
+
ErrType: infer E;
|
|
80
|
+
} ? {
|
|
81
|
+
isOk: true;
|
|
82
|
+
isErr: false;
|
|
83
|
+
value: U;
|
|
84
|
+
error: null;
|
|
85
|
+
} | {
|
|
86
|
+
isOk: false;
|
|
87
|
+
isErr: true;
|
|
88
|
+
value: null;
|
|
89
|
+
error: E;
|
|
90
|
+
} : never;
|
|
@@ -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,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createSimpleHash = exports.stringifyStable = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a stable string representation of any JavaScript value
|
|
6
|
+
* Handles circular references and maintains consistent object key ordering
|
|
7
|
+
*/
|
|
8
|
+
function stringifyStable(value) {
|
|
9
|
+
const seen = new WeakSet();
|
|
10
|
+
return JSON.stringify(value, (_, value) => {
|
|
11
|
+
if (value === undefined)
|
|
12
|
+
return "[undefined]";
|
|
13
|
+
if (value === null)
|
|
14
|
+
return "[null]";
|
|
15
|
+
if (Number.isNaN(value))
|
|
16
|
+
return "[NaN]";
|
|
17
|
+
if (value === Infinity)
|
|
18
|
+
return "[Infinity]";
|
|
19
|
+
if (value === -Infinity)
|
|
20
|
+
return "[-Infinity]";
|
|
21
|
+
if (typeof value === "bigint")
|
|
22
|
+
return value.toString();
|
|
23
|
+
if (typeof value === "function")
|
|
24
|
+
return value.toString();
|
|
25
|
+
if (value instanceof Date)
|
|
26
|
+
return value.toISOString();
|
|
27
|
+
if (value instanceof RegExp)
|
|
28
|
+
return value.toString();
|
|
29
|
+
if (ArrayBuffer.isView(value)) {
|
|
30
|
+
return Array.from(value).join(",");
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === "object" && value !== null) {
|
|
33
|
+
if (seen.has(value))
|
|
34
|
+
return "[Circular]";
|
|
35
|
+
seen.add(value);
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
const sortedObj = {};
|
|
40
|
+
const sortedKeys = Object.keys(value).sort();
|
|
41
|
+
for (const key of sortedKeys) {
|
|
42
|
+
sortedObj[key] = value[key];
|
|
43
|
+
}
|
|
44
|
+
return sortedObj;
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
exports.stringifyStable = stringifyStable;
|
|
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
|
+
}
|
|
71
|
+
exports.createSimpleHash = createSimpleHash;
|
package/dist/utils/helper.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DevtoolsOptions } from "zustand/middleware";
|
|
2
|
-
import type { BaseActor, CandidDefenition, IDL } from "../types";
|
|
2
|
+
import type { CompiledResult, BaseActor, CandidDefenition, IDL } from "../types";
|
|
3
3
|
export declare function createStoreWithOptionalDevtools<T>(initialState: T, config: DevtoolsOptions): Omit<import("zustand/vanilla").StoreApi<T>, "setState"> & {
|
|
4
4
|
setState<A extends string | {
|
|
5
5
|
type: string;
|
|
@@ -14,3 +14,4 @@ export declare const generateRequestHash: (args?: unknown[]) => `0x${string}`;
|
|
|
14
14
|
export declare const generateHash: (field?: unknown) => `0x${string}`;
|
|
15
15
|
export declare const generateActorHash: (actor: BaseActor) => `0x${string}`;
|
|
16
16
|
export declare const stringToHash: (str: string) => `0x${string}`;
|
|
17
|
+
export declare function createCompiledResult<T>(result: T): CompiledResult<T>;
|
package/dist/utils/helper.js
CHANGED
|
@@ -9,10 +9,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
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;
|
|
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;
|
|
13
13
|
const agent_1 = require("@dfinity/agent");
|
|
14
14
|
const middleware_1 = require("zustand/middleware");
|
|
15
15
|
const vanilla_1 = require("zustand/vanilla");
|
|
16
|
+
const hash_1 = require("./hash");
|
|
16
17
|
function createStoreWithOptionalDevtools(initialState, config) {
|
|
17
18
|
if (config.withDevtools) {
|
|
18
19
|
return (0, vanilla_1.createStore)((0, middleware_1.devtools)(() => initialState, Object.assign({ serialize: {
|
|
@@ -75,8 +76,8 @@ const jsonToString = (json, space = 2) => {
|
|
|
75
76
|
};
|
|
76
77
|
exports.jsonToString = jsonToString;
|
|
77
78
|
const generateRequestHash = (args = []) => {
|
|
78
|
-
const serializedArgs = (0,
|
|
79
|
-
return `0x${
|
|
79
|
+
const serializedArgs = (0, hash_1.createSimpleHash)(args);
|
|
80
|
+
return `0x${serializedArgs}`;
|
|
80
81
|
};
|
|
81
82
|
exports.generateRequestHash = generateRequestHash;
|
|
82
83
|
const generateHash = (field) => {
|
|
@@ -97,3 +98,32 @@ exports.stringToHash = stringToHash;
|
|
|
97
98
|
function toHexString(bytes) {
|
|
98
99
|
return (0, agent_1.toHex)(bytes);
|
|
99
100
|
}
|
|
101
|
+
/// Helper function for extracting the value from a compiled result { Ok: T } or { Err: E }
|
|
102
|
+
function createCompiledResult(result) {
|
|
103
|
+
if (result && typeof result === "object" && "Ok" in result) {
|
|
104
|
+
return {
|
|
105
|
+
isOk: true,
|
|
106
|
+
isErr: false,
|
|
107
|
+
value: result.Ok,
|
|
108
|
+
error: null,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
else if (result && typeof result === "object" && "Err" in result) {
|
|
112
|
+
return {
|
|
113
|
+
isOk: false,
|
|
114
|
+
isErr: true,
|
|
115
|
+
value: null,
|
|
116
|
+
error: result.Err,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
// For non-Result types
|
|
121
|
+
return {
|
|
122
|
+
isOk: false,
|
|
123
|
+
isErr: false,
|
|
124
|
+
value: undefined,
|
|
125
|
+
error: null,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.createCompiledResult = createCompiledResult;
|
package/dist/utils/index.d.ts
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -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.
|
|
3
|
+
"version": "1.12.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",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"zustand": "4.5.5"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@ic-reactor/parser": "0.4.
|
|
44
|
+
"@ic-reactor/parser": "^0.4.1"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"engines": {
|
|
57
57
|
"node": ">=10"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "8f8b4be20c0c89a58110e782f0d613cef9e7a04f"
|
|
60
60
|
}
|