zkcloudworker 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. package/lib/ts/src/api/api.js +14 -6
  2. package/lib/ts/src/index.d.ts +3 -2
  3. package/lib/ts/src/index.js +3 -2
  4. package/lib/ts/src/utils/base64.d.ts +5 -0
  5. package/lib/ts/src/utils/base64.js +90 -0
  6. package/lib/ts/src/{fetch.js → utils/fetch.js} +24 -6
  7. package/lib/ts/src/utils/fields.d.ts +3 -0
  8. package/lib/ts/src/utils/fields.js +33 -0
  9. package/lib/ts/tsconfig.tsbuildinfo +1 -1
  10. package/lib/web/src/api/api.js +260 -275
  11. package/lib/web/src/api/api.js.map +1 -1
  12. package/lib/web/src/cloud/cloud.js +12 -25
  13. package/lib/web/src/cloud/cloud.js.map +1 -1
  14. package/lib/web/src/cloud/files.js +59 -68
  15. package/lib/web/src/cloud/files.js.map +1 -1
  16. package/lib/web/src/cloud/local.js +221 -259
  17. package/lib/web/src/cloud/local.js.map +1 -1
  18. package/lib/web/src/encryption/encryption.js +2 -5
  19. package/lib/web/src/encryption/encryption.js.map +1 -1
  20. package/lib/web/src/encryption/messages.js +24 -29
  21. package/lib/web/src/encryption/messages.js.map +1 -1
  22. package/lib/web/src/encryption/nats-client.js +67 -83
  23. package/lib/web/src/encryption/nats-client.js.map +1 -1
  24. package/lib/web/src/fee.js +3 -6
  25. package/lib/web/src/fee.js.map +1 -1
  26. package/lib/web/src/index.d.ts +3 -2
  27. package/lib/web/src/index.js +3 -2
  28. package/lib/web/src/index.js.map +1 -1
  29. package/lib/web/src/mina.js +60 -67
  30. package/lib/web/src/mina.js.map +1 -1
  31. package/lib/web/src/utils/base64.d.ts +5 -0
  32. package/lib/web/src/utils/base64.js +84 -0
  33. package/lib/web/src/utils/base64.js.map +1 -0
  34. package/lib/web/src/utils/fetch.js +79 -0
  35. package/lib/web/src/utils/fetch.js.map +1 -0
  36. package/lib/web/src/utils/fields.d.ts +3 -0
  37. package/lib/web/src/utils/fields.js +29 -0
  38. package/lib/web/src/utils/fields.js.map +1 -0
  39. package/lib/web/tsconfig.web.tsbuildinfo +1 -1
  40. package/package.json +1 -1
  41. package/lib/ts/src/api/client-api.d.ts +0 -147
  42. package/lib/ts/src/api/client-api.js +0 -122
  43. package/lib/web/src/api/client-api.d.ts +0 -147
  44. package/lib/web/src/api/client-api.js +0 -134
  45. package/lib/web/src/api/client-api.js.map +0 -1
  46. package/lib/web/src/fetch.js +0 -68
  47. package/lib/web/src/fetch.js.map +0 -1
  48. /package/lib/ts/src/{fetch.d.ts → utils/fetch.d.ts} +0 -0
  49. /package/lib/web/src/{fetch.d.ts → utils/fetch.d.ts} +0 -0
@@ -1,4 +1,3 @@
1
- import { __awaiter } from "tslib";
2
1
  import axios from "axios";
3
2
  import chalk from "chalk";
4
3
  import { sleep } from "../mina";
@@ -19,7 +18,7 @@ export class zkCloudWorkerClient {
19
18
  const { jwt, zkcloudworker, chain, webhook } = params;
20
19
  this.jwt = jwt;
21
20
  this.endpoint = ZKCLOUDWORKER_API;
22
- this.chain = chain !== null && chain !== void 0 ? chain : "devnet";
21
+ this.chain = chain ?? "devnet";
23
22
  this.webhook = webhook;
24
23
  if (jwt === "local") {
25
24
  if (zkcloudworker === undefined)
@@ -43,21 +42,19 @@ export class zkCloudWorkerClient {
43
42
  * The developers repo should provide a BackupPlugin with the name task
44
43
  * that can be called with the given parameters
45
44
  */
46
- recursiveProof(data) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- const result = yield this.apiHub("recursiveProof", data);
49
- if (result.data === "error")
50
- return {
51
- success: false,
52
- error: result.error,
53
- };
54
- else
55
- return {
56
- success: result.success,
57
- jobId: result.data,
58
- error: result.error,
59
- };
60
- });
45
+ async recursiveProof(data) {
46
+ const result = await this.apiHub("recursiveProof", data);
47
+ if (result.data === "error")
48
+ return {
49
+ success: false,
50
+ error: result.error,
51
+ };
52
+ else
53
+ return {
54
+ success: result.success,
55
+ jobId: result.data,
56
+ error: result.error,
57
+ };
61
58
  }
62
59
  /**
63
60
  * Starts a new job for the function call using serverless api call
@@ -75,34 +72,31 @@ export class zkCloudWorkerClient {
75
72
  * @returns { success: boolean, error?: string, jobId?: string }
76
73
  * where jonId is the jobId of the job
77
74
  */
78
- execute(data) {
79
- return __awaiter(this, void 0, void 0, function* () {
80
- var _a, _b, _c, _d, _e;
81
- const result = yield this.apiHub("execute", data);
82
- if (result.data === "error" ||
83
- (typeof result.data === "string" && result.data.startsWith("error")))
84
- return {
85
- success: false,
86
- error: result.error,
87
- };
88
- else if (result.success === false || ((_a = result.data) === null || _a === void 0 ? void 0 : _a.success) === false)
89
- return {
90
- success: false,
91
- error: (_d = (_b = result.error) !== null && _b !== void 0 ? _b : (_c = result.data) === null || _c === void 0 ? void 0 : _c.error) !== null && _d !== void 0 ? _d : "execute call failed",
92
- };
93
- else if (result.success === true && ((_e = result.data) === null || _e === void 0 ? void 0 : _e.success) === true)
94
- return {
95
- success: result.success,
96
- jobId: data.mode === "sync" ? undefined : result.data.jobId,
97
- result: data.mode === "sync" ? result.data : undefined,
98
- error: result.error,
99
- };
100
- else
101
- return {
102
- success: false,
103
- error: "execute call error",
104
- };
105
- });
75
+ async execute(data) {
76
+ const result = await this.apiHub("execute", data);
77
+ if (result.data === "error" ||
78
+ (typeof result.data === "string" && result.data.startsWith("error")))
79
+ return {
80
+ success: false,
81
+ error: result.error,
82
+ };
83
+ else if (result.success === false || result.data?.success === false)
84
+ return {
85
+ success: false,
86
+ error: result.error ?? result.data?.error ?? "execute call failed",
87
+ };
88
+ else if (result.success === true && result.data?.success === true)
89
+ return {
90
+ success: result.success,
91
+ jobId: data.mode === "sync" ? undefined : result.data.jobId,
92
+ result: data.mode === "sync" ? result.data : undefined,
93
+ error: result.error,
94
+ };
95
+ else
96
+ return {
97
+ success: false,
98
+ error: "execute call error",
99
+ };
106
100
  }
107
101
  /**
108
102
  * Starts a new job for the function call using serverless api call
@@ -116,22 +110,20 @@ export class zkCloudWorkerClient {
116
110
  * @returns { success: boolean, error?: string, jobId?: string }
117
111
  * where jonId is the jobId of the job
118
112
  */
119
- sendTransactions(data) {
120
- return __awaiter(this, void 0, void 0, function* () {
121
- const result = yield this.apiHub("sendTransactions", data);
122
- if (result.data === "error")
123
- // TODO: check if this is correct in AWS code
124
- return {
125
- success: false,
126
- error: result.error,
127
- };
128
- else
129
- return {
130
- success: result.success,
131
- txId: result.data,
132
- error: result.error,
133
- };
134
- });
113
+ async sendTransactions(data) {
114
+ const result = await this.apiHub("sendTransactions", data);
115
+ if (result.data === "error")
116
+ // TODO: check if this is correct in AWS code
117
+ return {
118
+ success: false,
119
+ error: result.error,
120
+ };
121
+ else
122
+ return {
123
+ success: result.success,
124
+ txId: result.data,
125
+ error: result.error,
126
+ };
135
127
  }
136
128
  /**
137
129
  * Gets the result of the job using serverless api call
@@ -145,22 +137,20 @@ export class zkCloudWorkerClient {
145
137
  * if the job is finished, the result will be set and error will be undefined
146
138
  * if the job is not found, the result will be undefined and error will be set
147
139
  */
148
- jobResult(data) {
149
- return __awaiter(this, void 0, void 0, function* () {
150
- const result = yield this.apiHub("jobResult", data);
151
- if (this.isError(result.data))
152
- return {
153
- success: false,
154
- error: result.error,
155
- result: result.data,
156
- };
157
- else
158
- return {
159
- success: result.success,
160
- error: result.error,
161
- result: result.data,
162
- };
163
- });
140
+ async jobResult(data) {
141
+ const result = await this.apiHub("jobResult", data);
142
+ if (this.isError(result.data))
143
+ return {
144
+ success: false,
145
+ error: result.error,
146
+ result: result.data,
147
+ };
148
+ else
149
+ return {
150
+ success: result.success,
151
+ error: result.error,
152
+ result: result.data,
153
+ };
164
154
  }
165
155
  /**
166
156
  * Gets the result of the job using serverless api call
@@ -173,73 +163,66 @@ export class zkCloudWorkerClient {
173
163
  * if the job is finished, the result will be set and error will be undefined
174
164
  * if the job is not found, the result will be undefined and error will be set
175
165
  */
176
- deploy(data) {
177
- return __awaiter(this, void 0, void 0, function* () {
178
- var _a, _b;
179
- // TODO: encrypt env.json
180
- const { repo, developer, packageManager } = data;
181
- const result = yield this.apiHub("deploy", {
182
- developer,
183
- repo,
184
- args: packageManager,
185
- });
186
- if (result.data === "error" ||
187
- (typeof result.data === "string" && result.data.startsWith("error")))
188
- return {
189
- success: false,
190
- error: result.error,
191
- };
192
- else
193
- return {
194
- success: result.success && ((_a = result.data) === null || _a === void 0 ? void 0 : _a.success),
195
- jobId: (_b = result.data) === null || _b === void 0 ? void 0 : _b.jobId,
196
- error: result.error,
197
- };
166
+ async deploy(data) {
167
+ // TODO: encrypt env.json
168
+ const { repo, developer, packageManager } = data;
169
+ const result = await this.apiHub("deploy", {
170
+ developer,
171
+ repo,
172
+ args: packageManager,
198
173
  });
174
+ if (result.data === "error" ||
175
+ (typeof result.data === "string" && result.data.startsWith("error")))
176
+ return {
177
+ success: false,
178
+ error: result.error,
179
+ };
180
+ else
181
+ return {
182
+ success: result.success && result.data?.success,
183
+ jobId: result.data?.jobId,
184
+ error: result.error,
185
+ };
199
186
  }
200
187
  /**
201
188
  * Gets the billing report for the jobs sent using JWT
202
189
  * @returns { success: boolean, error?: string, result?: any }
203
190
  * where result is the billing report
204
191
  */
205
- queryBilling() {
206
- return __awaiter(this, void 0, void 0, function* () {
207
- const result = yield this.apiHub("queryBilling", {});
208
- if (this.isError(result.data))
209
- return {
210
- success: false,
211
- error: result.error,
212
- result: result.data,
213
- };
214
- else
215
- return {
216
- success: result.success,
217
- error: result.error,
218
- result: result.data,
219
- };
220
- });
192
+ async queryBilling() {
193
+ const result = await this.apiHub("queryBilling", {});
194
+ if (this.isError(result.data))
195
+ return {
196
+ success: false,
197
+ error: result.error,
198
+ result: result.data,
199
+ };
200
+ else
201
+ return {
202
+ success: result.success,
203
+ error: result.error,
204
+ result: result.data,
205
+ };
221
206
  }
222
207
  /**
223
208
  * Gets the remaining balance
224
209
  * @returns { success: boolean, error?: string, result?: any }
225
210
  * where result is the billing report
226
211
  */
227
- getBalance() {
228
- return __awaiter(this, void 0, void 0, function* () {
229
- const result = yield this.apiHub("getBalance", {});
230
- if (this.isError(result.data))
231
- return {
232
- success: false,
233
- error: result.error,
234
- result: result.data,
235
- };
236
- else
237
- return {
238
- success: result.success,
239
- error: result.error,
240
- result: result.data,
241
- };
242
- });
212
+ async getBalance() {
213
+ const result = await this.apiHub("getBalance", {});
214
+ if (this.isError(result.data))
215
+ return {
216
+ success: false,
217
+ error: result.error,
218
+ result: result.data,
219
+ };
220
+ else
221
+ return {
222
+ success: result.success,
223
+ error: result.error,
224
+ result: result.data,
225
+ };
243
226
  }
244
227
  /**
245
228
  * Waits for the job to finish
@@ -252,186 +235,188 @@ export class zkCloudWorkerClient {
252
235
  * @returns { success: boolean, error?: string, result?: any }
253
236
  * where result is the result of the job
254
237
  */
255
- waitForJobResult(data) {
256
- return __awaiter(this, void 0, void 0, function* () {
257
- var _a, _b, _c, _d, _e, _f, _g, _h;
258
- const maxAttempts = (_a = data === null || data === void 0 ? void 0 : data.maxAttempts) !== null && _a !== void 0 ? _a : 360; // 2 hours
259
- const interval = (_b = data === null || data === void 0 ? void 0 : data.interval) !== null && _b !== void 0 ? _b : 10000;
260
- const maxErrors = (_c = data === null || data === void 0 ? void 0 : data.maxErrors) !== null && _c !== void 0 ? _c : 10;
261
- const errorDelay = 30000; // 30 seconds
262
- const printedLogs = [];
263
- const printLogs = (_d = data.printLogs) !== null && _d !== void 0 ? _d : true;
264
- function print(logs) {
265
- logs.forEach((log) => {
266
- if (printedLogs.includes(log) === false) {
267
- printedLogs.push(log);
238
+ async waitForJobResult(data) {
239
+ const maxAttempts = data?.maxAttempts ?? 360; // 1 hour
240
+ const interval = data?.interval ?? 10000;
241
+ const maxErrors = data?.maxErrors ?? 10;
242
+ const errorDelay = 30000; // 30 seconds
243
+ const printedLogs = [];
244
+ const printLogs = data.printLogs ?? true;
245
+ function isAllLogsFetched() {
246
+ if (printLogs === false)
247
+ return true;
248
+ // search for "Billed Duration" in the logs and return true if found
249
+ return printedLogs.some((log) => log.includes("Billed Duration"));
250
+ }
251
+ function print(logs) {
252
+ logs.forEach((log) => {
253
+ if (printedLogs.includes(log) === false) {
254
+ printedLogs.push(log);
255
+ if (printLogs) {
268
256
  // replace all occurrences of "error" with red color
269
257
  const text = log.replace(/error/gi, (matched) => chalk.red(matched));
270
258
  console.log(text);
271
259
  }
272
- });
260
+ }
261
+ });
262
+ }
263
+ let attempts = 0;
264
+ let errors = 0;
265
+ while (attempts < maxAttempts) {
266
+ const result = await this.apiHub("jobResult", {
267
+ jobId: data.jobId,
268
+ includeLogs: printLogs,
269
+ });
270
+ if (printLogs === true &&
271
+ result?.data?.logs !== undefined &&
272
+ result?.data?.logs !== null &&
273
+ Array.isArray(result.data.logs) === true)
274
+ print(result.data.logs);
275
+ if (result.success === false) {
276
+ errors++;
277
+ if (errors > maxErrors) {
278
+ return {
279
+ success: false,
280
+ error: "Too many network errors",
281
+ result: undefined,
282
+ };
283
+ }
284
+ await sleep(errorDelay * errors);
273
285
  }
274
- let attempts = 0;
275
- let errors = 0;
276
- while (attempts < maxAttempts) {
277
- const result = yield this.apiHub("jobResult", {
278
- jobId: data.jobId,
279
- includeLogs: printLogs,
280
- });
281
- if (printLogs === true &&
282
- ((_e = result === null || result === void 0 ? void 0 : result.data) === null || _e === void 0 ? void 0 : _e.logs) !== undefined &&
283
- ((_f = result === null || result === void 0 ? void 0 : result.data) === null || _f === void 0 ? void 0 : _f.logs) !== null &&
284
- Array.isArray(result.data.logs) === true)
285
- print(result.data.logs);
286
- if (result.success === false) {
287
- errors++;
288
- if (errors > maxErrors) {
289
- return {
290
- success: false,
291
- error: "Too many network errors",
292
- result: undefined,
293
- };
294
- }
295
- yield sleep(errorDelay * errors);
286
+ else {
287
+ if (this.isError(result.data))
288
+ return {
289
+ success: false,
290
+ error: result.error,
291
+ result: result.data,
292
+ };
293
+ else if (result.data?.result !== undefined && isAllLogsFetched()) {
294
+ return {
295
+ success: result.success,
296
+ error: result.error,
297
+ result: result.data,
298
+ };
296
299
  }
297
- else {
298
- if (this.isError(result.data))
299
- return {
300
- success: false,
301
- error: result.error,
302
- result: result.data,
303
- };
304
- else if (((_g = result.data) === null || _g === void 0 ? void 0 : _g.result) !== undefined) {
305
- return {
306
- success: result.success,
307
- error: result.error,
308
- result: result.data,
309
- };
310
- }
311
- else if (((_h = result.data) === null || _h === void 0 ? void 0 : _h.jobStatus) === "failed") {
312
- return {
313
- success: false,
314
- error: "Job failed",
315
- result: result.data,
316
- };
317
- }
318
- yield sleep(interval);
300
+ else if (result.data?.jobStatus === "failed" && isAllLogsFetched()) {
301
+ return {
302
+ success: false,
303
+ error: "Job failed",
304
+ result: result.data,
305
+ };
319
306
  }
320
- attempts++;
307
+ await sleep(interval);
321
308
  }
322
- return {
323
- success: false,
324
- error: "Timeout",
325
- result: undefined,
326
- };
327
- });
309
+ attempts++;
310
+ }
311
+ return {
312
+ success: false,
313
+ error: "Timeout",
314
+ result: undefined,
315
+ };
328
316
  }
329
317
  /**
330
318
  * Calls the serverless API
331
319
  * @param command the command of the API
332
320
  * @param data the data of the API
333
321
  * */
334
- apiHub(command,
322
+ async apiHub(command,
335
323
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
336
324
  data
337
325
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
338
326
  ) {
339
- return __awaiter(this, void 0, void 0, function* () {
340
- var _a;
341
- if (this.jwt === "local") {
342
- if (this.localWorker === undefined)
343
- throw new Error("localWorker is undefined");
344
- switch (command) {
345
- case "recursiveProof": {
346
- const jobId = yield LocalCloud.run({
347
- command: "recursiveProof",
348
- data,
349
- chain: this.chain,
350
- localWorker: this.localWorker,
351
- });
327
+ if (this.jwt === "local") {
328
+ if (this.localWorker === undefined)
329
+ throw new Error("localWorker is undefined");
330
+ switch (command) {
331
+ case "recursiveProof": {
332
+ const jobId = await LocalCloud.run({
333
+ command: "recursiveProof",
334
+ data,
335
+ chain: this.chain,
336
+ localWorker: this.localWorker,
337
+ });
338
+ return {
339
+ success: true,
340
+ data: jobId,
341
+ };
342
+ }
343
+ case "execute": {
344
+ const jobId = await LocalCloud.run({
345
+ command: "execute",
346
+ data,
347
+ chain: this.chain,
348
+ localWorker: this.localWorker,
349
+ });
350
+ if (data.mode === "sync")
351
+ return { success: true, data: LocalStorage.jobs[jobId].result };
352
+ else
352
353
  return {
353
354
  success: true,
354
355
  data: jobId,
355
356
  };
356
- }
357
- case "execute": {
358
- const jobId = yield LocalCloud.run({
359
- command: "execute",
360
- data,
361
- chain: this.chain,
362
- localWorker: this.localWorker,
363
- });
364
- if (data.mode === "sync")
365
- return { success: true, data: LocalStorage.jobs[jobId].result };
366
- else
367
- return {
368
- success: true,
369
- data: jobId,
370
- };
371
- }
372
- case "jobResult": {
373
- const job = LocalStorage.jobs[data.jobId];
374
- if (job === undefined) {
375
- return {
376
- success: false,
377
- error: "local job not found",
378
- };
379
- }
380
- else {
381
- return {
382
- success: true,
383
- data: job,
384
- };
385
- }
386
- }
387
- case "sendTransactions": {
357
+ }
358
+ case "jobResult": {
359
+ const job = LocalStorage.jobs[data.jobId];
360
+ if (job === undefined) {
388
361
  return {
389
- success: true,
390
- data: yield LocalCloud.addTransactions(data.transactions),
362
+ success: false,
363
+ error: "local job not found",
391
364
  };
392
365
  }
393
- case "deploy":
394
- return {
395
- success: true,
396
- data: "local_deploy",
397
- };
398
- case "queryBilling":
366
+ else {
399
367
  return {
400
368
  success: true,
401
- data: "local_queryBilling",
402
- };
403
- default:
404
- return {
405
- success: false,
406
- error: "local_error",
369
+ data: job,
407
370
  };
371
+ }
408
372
  }
409
- }
410
- else {
411
- const apiData = {
412
- auth: ZKCLOUDWORKER_AUTH,
413
- command: command,
414
- jwtToken: this.jwt,
415
- data: data,
416
- chain: this.chain,
417
- webhook: this.webhook, // TODO: implement webhook code on AWS
418
- };
419
- try {
420
- const response = yield axios.post(this.endpoint, apiData);
421
- return { success: true, data: response.data };
422
- }
423
- catch (error) {
424
- console.error("apiHub error:", (_a = error.message) !== null && _a !== void 0 ? _a : error);
425
- return { success: false, error: error };
373
+ case "sendTransactions": {
374
+ return {
375
+ success: true,
376
+ data: await LocalCloud.addTransactions(data.transactions),
377
+ };
426
378
  }
379
+ case "deploy":
380
+ return {
381
+ success: true,
382
+ data: "local_deploy",
383
+ };
384
+ case "queryBilling":
385
+ return {
386
+ success: true,
387
+ data: "local_queryBilling",
388
+ };
389
+ default:
390
+ return {
391
+ success: false,
392
+ error: "local_error",
393
+ };
427
394
  }
428
- });
395
+ }
396
+ else {
397
+ const apiData = {
398
+ auth: ZKCLOUDWORKER_AUTH,
399
+ command: command,
400
+ jwtToken: this.jwt,
401
+ data: data,
402
+ chain: this.chain,
403
+ webhook: this.webhook, // TODO: implement webhook code on AWS
404
+ };
405
+ try {
406
+ const response = await axios.post(this.endpoint, apiData);
407
+ return { success: true, data: response.data };
408
+ }
409
+ catch (error) {
410
+ console.error("apiHub error:", error.message ?? error);
411
+ return { success: false, error: error };
412
+ }
413
+ }
429
414
  }
430
415
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
431
416
  isError(data) {
432
417
  if (data === "error")
433
418
  return true;
434
- if ((data === null || data === void 0 ? void 0 : data.jobStatus) === "failed")
419
+ if (data?.jobStatus === "failed")
435
420
  return true;
436
421
  if (typeof data === "string" && data.toLowerCase().startsWith("error"))
437
422
  return true;