@jsm-mit/jsm-framework 0.0.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 ADDED
@@ -0,0 +1 @@
1
+ ## JSM Framework
@@ -0,0 +1,23 @@
1
+ import { Secp256k1KeyIdentity } from "@icp-sdk/core/identity/secp256k1";
2
+ export declare abstract class ActorBase {
3
+ private readonly canisterId;
4
+ private readonly identity?;
5
+ private readonly host;
6
+ private agent;
7
+ constructor(canisterId: string, identity?: Secp256k1KeyIdentity | undefined);
8
+ reinitializeAgent(): void;
9
+ abstract initActor(): void;
10
+ private setupAgent;
11
+ protected runFunctionAndSyncTimeIfNecessaryAsyncUnsafe<T>(fnAsync: () => Promise<T>): Promise<T>;
12
+ protected handleResultErrors(functionName: string, params: any[], error: Error): {
13
+ errorKey: keyof Error;
14
+ errorMessage: any;
15
+ };
16
+ protected syncTimeAsync(throwError: boolean): Promise<void>;
17
+ protected executeFunction<T>(fnAsync: () => Promise<{
18
+ ok: T;
19
+ } | {
20
+ err: Error;
21
+ }>, errorMessage: string, functionName: string, params: any[], defaultValue: T, throwException: boolean): Promise<T>;
22
+ }
23
+ //# sourceMappingURL=actor-base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"actor-base.d.ts","sourceRoot":"","sources":["../src/actor-base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,kCAAkC,CAAC;AAIxE,8BAAsB,SAAS;IAIf,OAAO,CAAC,QAAQ,CAAC,UAAU;IAAU,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC;IAH3E,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA6B;IAClD,OAAO,CAAC,KAAK,CAAa;gBAEG,UAAU,EAAE,MAAM,EAAmB,QAAQ,CAAC,EAAE,oBAAoB,YAAA;IAK1F,iBAAiB;IAYxB,QAAQ,CAAC,SAAS,IAAI,IAAI;YAEZ,UAAU;cASR,4CAA4C,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAwBtG,SAAS,CAAC,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,KAAK,EAAE,KAAK;;;;cAQ9D,aAAa,CAAC,UAAU,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;cAQjD,eAAe,CAAC,CAAC,EAC7B,OAAO,EAAE,MAAM,OAAO,CAAC;QAAE,EAAE,EAAE,CAAC,CAAA;KAAE,GAAG;QAAE,GAAG,EAAE,KAAK,CAAA;KAAE,CAAC,EAClD,YAAY,EAAE,MAAM,EACpB,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,GAAG,EAAE,EACb,YAAY,EAAE,CAAC,EACf,cAAc,EAAE,OAAO,GACxB,OAAO,CAAC,CAAC,CAAC;CAgBhB"}
@@ -0,0 +1,85 @@
1
+ import { HttpAgent } from "@icp-sdk/core/agent";
2
+ import { Secp256k1KeyIdentity } from "@icp-sdk/core/identity/secp256k1";
3
+ import { BetterJSON, logErrorLocallyAndPublishOnErrorSubject } from "@jsm-mit/utils-package";
4
+ import { pigeon } from "@jsm-mit/pigeon-package";
5
+ export class ActorBase {
6
+ constructor(canisterId, identity) {
7
+ this.canisterId = canisterId;
8
+ this.identity = identity;
9
+ this.host = "https://icp0.io";
10
+ this.reinitializeAgent();
11
+ this.setupAgent();
12
+ }
13
+ reinitializeAgent() {
14
+ console.log("Agent reinitialization");
15
+ this.agent = HttpAgent.createSync({
16
+ host: this.host,
17
+ ingressExpiryInMinutes: 1,
18
+ identity: this.identity
19
+ });
20
+ this.initActor();
21
+ }
22
+ async setupAgent() {
23
+ try {
24
+ await this.agent.syncTime();
25
+ console.log("✅ Agent IC zsynchronizowany pomyślnie.");
26
+ }
27
+ catch (err) {
28
+ console.error("❌ Błąd podczas konfiguracji Agenta:", err);
29
+ }
30
+ }
31
+ async runFunctionAndSyncTimeIfNecessaryAsyncUnsafe(fnAsync) {
32
+ try {
33
+ return await fnAsync();
34
+ }
35
+ catch (error) {
36
+ const errorMsg = BetterJSON.stringify(error);
37
+ pigeon.debugEmailAsyncSafe("Problem at runFunctionAndSyncTimeIfNecessaryAsyncUnsafe", "RabbitMotokoActor", [], errorMsg);
38
+ if (errorMsg.includes("certificate") || errorMsg.includes("TrustError") || errorMsg.includes("ingress_expiry")) {
39
+ console.warn("⚠️ Wykryto problem z czasem. Próbuję synchronizacji...");
40
+ await this.syncTimeAsync(false);
41
+ try {
42
+ return await fnAsync();
43
+ }
44
+ catch (err) {
45
+ console.error(BetterJSON.stringify(err));
46
+ throw err;
47
+ }
48
+ }
49
+ else {
50
+ throw error;
51
+ }
52
+ }
53
+ }
54
+ handleResultErrors(functionName, params, error) {
55
+ pigeon.debugEmailAsyncSafe("debug", functionName, params, BetterJSON.stringify(error));
56
+ const errorKey = Object.keys(error)[0];
57
+ const errorMessage = error[errorKey];
58
+ return { errorKey, errorMessage };
59
+ }
60
+ async syncTimeAsync(throwError) {
61
+ try {
62
+ await this.agent.syncTime();
63
+ }
64
+ catch (err) {
65
+ if (throwError)
66
+ throw err;
67
+ }
68
+ }
69
+ async executeFunction(fnAsync, errorMessage, functionName, params, defaultValue, throwException) {
70
+ try {
71
+ const result = await this.runFunctionAndSyncTimeIfNecessaryAsyncUnsafe(fnAsync);
72
+ if ('ok' in result) {
73
+ return result.ok;
74
+ }
75
+ this.handleResultErrors(functionName, params, result.err);
76
+ return defaultValue;
77
+ }
78
+ catch (err) {
79
+ logErrorLocallyAndPublishOnErrorSubject(errorMessage, err);
80
+ if (throwException)
81
+ throw err;
82
+ return defaultValue;
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,2 @@
1
+ export { ActorBase } from "./actor-base.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { ActorBase } from "./actor-base.js";
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@jsm-mit/jsm-framework",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "homepage": "https://github.com/JSM-Common/jsm-framework#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/JSM-Common/jsm-framework/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/JSM-Common/jsm-framework.git"
12
+ },
13
+ "license": "ISC",
14
+ "author": "",
15
+ "type": "module",
16
+ "main": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
+ "exports": {
19
+ ".": {
20
+ "import": "./dist/index.js",
21
+ "types": "./dist/index.d.ts"
22
+ }
23
+ },
24
+ "files": [
25
+ "dist/"
26
+ ],
27
+ "scripts": {
28
+ "build": "tsc --build",
29
+ "clean": "rm -rf dist",
30
+ "prepare": "npm run build",
31
+ "publish-public": "npm login && npm publish --access public",
32
+ "test": "vitest",
33
+ "sandbox": "npx tsx sandbox/main.ts"
34
+ },
35
+ "peerDependencies": {
36
+ "@icp-sdk/core": "^5.2.0",
37
+ "@jsm-mit/pigeon-package": "^0.6.2",
38
+ "@jsm-mit/utils-package": "^0.2.2"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^25.5.2",
42
+ "dotenv": "^16.3.1",
43
+ "tsx": "^4.18.0",
44
+ "vitest": "^1.0.0"
45
+ }
46
+ }