zkcloudworker 0.6.1 → 0.6.3
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/lib/ts/src/api/api.js +3 -3
- package/lib/ts/src/cloud/cache.d.ts +97 -0
- package/lib/ts/src/cloud/cache.js +98 -0
- package/lib/ts/src/cloud/cloud.d.ts +1 -1
- package/lib/ts/src/cloud/index.d.ts +1 -0
- package/lib/ts/src/cloud/index.js +1 -0
- package/lib/ts/src/cloud/local.d.ts +1 -1
- package/lib/ts/src/cloud/local.js +4 -3
- package/lib/ts/src/utils/fetch.d.ts +2 -2
- package/lib/ts/src/utils/fetch.js +3 -3
- package/lib/ts/src/utils/index.d.ts +1 -0
- package/lib/ts/src/utils/index.js +1 -0
- package/lib/ts/src/utils/mina.d.ts +1 -9
- package/lib/ts/src/utils/mina.js +9 -65
- package/lib/ts/src/utils/utils.d.ts +8 -0
- package/lib/ts/src/utils/utils.js +67 -0
- package/lib/ts/tsconfig.tsbuildinfo +1 -1
- package/lib/web/src/api/api.js +1 -1
- package/lib/web/src/api/api.js.map +1 -1
- package/lib/web/src/cloud/cache.d.ts +97 -0
- package/lib/web/src/cloud/cache.js +97 -0
- package/lib/web/src/cloud/cache.js.map +1 -0
- package/lib/web/src/cloud/cloud.d.ts +1 -1
- package/lib/web/src/cloud/index.d.ts +1 -0
- package/lib/web/src/cloud/index.js +1 -0
- package/lib/web/src/cloud/index.js.map +1 -1
- package/lib/web/src/cloud/local.d.ts +1 -1
- package/lib/web/src/cloud/local.js +3 -2
- package/lib/web/src/cloud/local.js.map +1 -1
- package/lib/web/src/utils/fetch.d.ts +2 -2
- package/lib/web/src/utils/fetch.js +1 -1
- package/lib/web/src/utils/fetch.js.map +1 -1
- package/lib/web/src/utils/index.d.ts +1 -0
- package/lib/web/src/utils/index.js +1 -0
- package/lib/web/src/utils/index.js.map +1 -1
- package/lib/web/src/utils/mina.d.ts +1 -9
- package/lib/web/src/utils/mina.js +9 -61
- package/lib/web/src/utils/mina.js.map +1 -1
- package/lib/web/src/utils/utils.d.ts +8 -0
- package/lib/web/src/utils/utils.js +61 -0
- package/lib/web/src/utils/utils.js.map +1 -0
- package/lib/web/tsconfig.web.tsbuildinfo +1 -1
- package/package.json +1 -1
package/lib/ts/src/api/api.js
CHANGED
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.zkCloudWorkerClient = void 0;
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
8
8
|
const chalk_1 = __importDefault(require("chalk"));
|
9
|
-
const
|
9
|
+
const utils_1 = require("../utils/utils");
|
10
10
|
const local_1 = require("../cloud/local");
|
11
11
|
const config_1 = __importDefault(require("../config"));
|
12
12
|
const { ZKCLOUDWORKER_AUTH, ZKCLOUDWORKER_API } = config_1.default;
|
@@ -312,7 +312,7 @@ class zkCloudWorkerClient {
|
|
312
312
|
result: undefined,
|
313
313
|
};
|
314
314
|
}
|
315
|
-
await (0,
|
315
|
+
await (0, utils_1.sleep)(errorDelay * errors);
|
316
316
|
}
|
317
317
|
else {
|
318
318
|
if (this.isError(result.data))
|
@@ -335,7 +335,7 @@ class zkCloudWorkerClient {
|
|
335
335
|
result: result.data,
|
336
336
|
};
|
337
337
|
}
|
338
|
-
await (0,
|
338
|
+
await (0, utils_1.sleep)(interval);
|
339
339
|
}
|
340
340
|
attempts++;
|
341
341
|
}
|
@@ -0,0 +1,97 @@
|
|
1
|
+
export { Cache };
|
2
|
+
/**
|
3
|
+
* Interface for storing and retrieving values, for caching.
|
4
|
+
* `read()` and `write()` can just throw errors on failure.
|
5
|
+
*
|
6
|
+
* The data that will be passed to the cache for writing is exhaustively described by the {@link CacheHeader} type.
|
7
|
+
* It represents one of the following:
|
8
|
+
* - The SRS. This is a deterministic lists of curve points (one per curve) that needs to be generated just once,
|
9
|
+
* to be used for polynomial commitments.
|
10
|
+
* - Lagrange basis commitments. Similar to the SRS, this will be created once for every power-of-2 circuit size.
|
11
|
+
* - Prover and verifier keys for every compiled circuit.
|
12
|
+
*
|
13
|
+
* Per smart contract or ZkProgram, several different keys are created:
|
14
|
+
* - a step prover key (`step-pk`) and verification key (`step-vk`) _for every method_.
|
15
|
+
* - a wrap prover key (`wrap-pk`) and verification key (`wrap-vk`) for the entire contract.
|
16
|
+
*/
|
17
|
+
type Cache = {
|
18
|
+
/**
|
19
|
+
* Read a value from the cache.
|
20
|
+
*
|
21
|
+
* @param header A small header to identify what is read from the cache.
|
22
|
+
*/
|
23
|
+
read(header: CacheHeader): Uint8Array | undefined;
|
24
|
+
/**
|
25
|
+
* Write a value to the cache.
|
26
|
+
*
|
27
|
+
* @param header A small header to identify what is written to the cache. This will be used by `read()` to retrieve the data.
|
28
|
+
* @param value The value to write to the cache, as a byte array.
|
29
|
+
*/
|
30
|
+
write(header: CacheHeader, value: Uint8Array): void;
|
31
|
+
/**
|
32
|
+
* Indicates whether the cache is writable.
|
33
|
+
*/
|
34
|
+
canWrite: boolean;
|
35
|
+
/**
|
36
|
+
* If `debug` is toggled, `read()` and `write()` errors are logged to the console.
|
37
|
+
*
|
38
|
+
* By default, cache errors are silent, because they don't necessarily represent an error condition,
|
39
|
+
* but could just be a cache miss, or file system permissions incompatible with writing data.
|
40
|
+
*/
|
41
|
+
debug?: boolean;
|
42
|
+
};
|
43
|
+
type CommonHeader = {
|
44
|
+
/**
|
45
|
+
* Header version to avoid parsing incompatible headers.
|
46
|
+
*/
|
47
|
+
version: number;
|
48
|
+
/**
|
49
|
+
* An identifier that is persistent even as versions of the data change. Safe to use as a file path.
|
50
|
+
*/
|
51
|
+
persistentId: string;
|
52
|
+
/**
|
53
|
+
* A unique identifier for the data to be read. Safe to use as a file path.
|
54
|
+
*/
|
55
|
+
uniqueId: string;
|
56
|
+
/**
|
57
|
+
* Specifies whether the data to be read is a utf8-encoded string or raw binary data. This was added
|
58
|
+
* because node's `fs.readFileSync` returns garbage when reading string files without specifying the encoding.
|
59
|
+
*/
|
60
|
+
dataType: "string" | "bytes";
|
61
|
+
};
|
62
|
+
type StepKeyHeader<Kind> = {
|
63
|
+
kind: Kind;
|
64
|
+
programName: string;
|
65
|
+
methodName: string;
|
66
|
+
methodIndex: number;
|
67
|
+
hash: string;
|
68
|
+
};
|
69
|
+
type WrapKeyHeader<Kind> = {
|
70
|
+
kind: Kind;
|
71
|
+
programName: string;
|
72
|
+
hash: string;
|
73
|
+
};
|
74
|
+
type PlainHeader<Kind> = {
|
75
|
+
kind: Kind;
|
76
|
+
};
|
77
|
+
/**
|
78
|
+
* A header that is passed to the caching layer, to support rich caching strategies.
|
79
|
+
*
|
80
|
+
* Both `uniqueId` and `programId` can safely be used as a file path.
|
81
|
+
*/
|
82
|
+
type CacheHeader = (StepKeyHeader<"step-pk"> | StepKeyHeader<"step-vk"> | WrapKeyHeader<"wrap-pk"> | WrapKeyHeader<"wrap-vk"> | PlainHeader<"srs"> | PlainHeader<"lagrange-basis">) & CommonHeader;
|
83
|
+
declare const Cache: {
|
84
|
+
/**
|
85
|
+
* Store data on the file system, in a directory of your choice.
|
86
|
+
*
|
87
|
+
* Data will be stored in two files per cache entry: a data file and a `.header` file.
|
88
|
+
* The header file just contains a unique string which is used to determine whether we can use the cached data.
|
89
|
+
*
|
90
|
+
* Note: this {@link Cache} only caches data in Node.js.
|
91
|
+
*/
|
92
|
+
FileSystem: (cacheDirectory: string, debug?: boolean) => Cache;
|
93
|
+
/**
|
94
|
+
* Don't store anything.
|
95
|
+
*/
|
96
|
+
None: Cache;
|
97
|
+
};
|
@@ -0,0 +1,98 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Cache = void 0;
|
4
|
+
const node_fs_1 = require("node:fs");
|
5
|
+
const node_path_1 = require("node:path");
|
6
|
+
const jsEnvironment = "node";
|
7
|
+
const cacheHeaderVersion = 1;
|
8
|
+
function withVersion(header, version = cacheHeaderVersion) {
|
9
|
+
let uniqueId = `${header.uniqueId}-${version}`;
|
10
|
+
return { ...header, version, uniqueId };
|
11
|
+
}
|
12
|
+
function readCache(cache, header, transform) {
|
13
|
+
try {
|
14
|
+
let result = cache.read(header);
|
15
|
+
if (result === undefined) {
|
16
|
+
if (cache.debug)
|
17
|
+
console.trace("cache miss");
|
18
|
+
return undefined;
|
19
|
+
}
|
20
|
+
if (transform === undefined)
|
21
|
+
return result;
|
22
|
+
return transform(result);
|
23
|
+
}
|
24
|
+
catch (e) {
|
25
|
+
if (cache.debug)
|
26
|
+
console.log("Failed to read cache", e);
|
27
|
+
return undefined;
|
28
|
+
}
|
29
|
+
}
|
30
|
+
function writeCache(cache, header, value) {
|
31
|
+
if (!cache.canWrite)
|
32
|
+
return false;
|
33
|
+
try {
|
34
|
+
cache.write(header, value);
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
catch (e) {
|
38
|
+
if (cache.debug)
|
39
|
+
console.log("Failed to write cache", e);
|
40
|
+
return false;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
const None = {
|
44
|
+
read() {
|
45
|
+
throw Error("not available");
|
46
|
+
},
|
47
|
+
write() {
|
48
|
+
throw Error("not available");
|
49
|
+
},
|
50
|
+
canWrite: false,
|
51
|
+
};
|
52
|
+
const FileSystem = (cacheDirectory, debug) => ({
|
53
|
+
read({ persistentId, uniqueId, dataType }) {
|
54
|
+
if (jsEnvironment !== "node")
|
55
|
+
throw Error("file system not available");
|
56
|
+
// read current uniqueId, return data if it matches
|
57
|
+
let currentId = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(cacheDirectory, `${persistentId}.header`), "utf8");
|
58
|
+
if (currentId !== uniqueId)
|
59
|
+
return undefined;
|
60
|
+
if (dataType === "string") {
|
61
|
+
let string = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(cacheDirectory, persistentId), "utf8");
|
62
|
+
return new TextEncoder().encode(string);
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
let buffer = (0, node_fs_1.readFileSync)((0, node_path_1.resolve)(cacheDirectory, persistentId));
|
66
|
+
return new Uint8Array(buffer.buffer);
|
67
|
+
}
|
68
|
+
},
|
69
|
+
write({ persistentId, uniqueId, dataType }, data) {
|
70
|
+
if (jsEnvironment !== "node")
|
71
|
+
throw Error("file system not available");
|
72
|
+
(0, node_fs_1.mkdirSync)(cacheDirectory, { recursive: true });
|
73
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.resolve)(cacheDirectory, `${persistentId}.header`), uniqueId, {
|
74
|
+
encoding: "utf8",
|
75
|
+
});
|
76
|
+
(0, node_fs_1.writeFileSync)((0, node_path_1.resolve)(cacheDirectory, persistentId), data, {
|
77
|
+
encoding: dataType === "string" ? "utf8" : undefined,
|
78
|
+
});
|
79
|
+
},
|
80
|
+
canWrite: jsEnvironment === "node",
|
81
|
+
debug,
|
82
|
+
});
|
83
|
+
const Cache = {
|
84
|
+
/**
|
85
|
+
* Store data on the file system, in a directory of your choice.
|
86
|
+
*
|
87
|
+
* Data will be stored in two files per cache entry: a data file and a `.header` file.
|
88
|
+
* The header file just contains a unique string which is used to determine whether we can use the cached data.
|
89
|
+
*
|
90
|
+
* Note: this {@link Cache} only caches data in Node.js.
|
91
|
+
*/
|
92
|
+
FileSystem,
|
93
|
+
/**
|
94
|
+
* Don't store anything.
|
95
|
+
*/
|
96
|
+
None,
|
97
|
+
};
|
98
|
+
exports.Cache = Cache;
|
@@ -2,8 +2,9 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.LocalStorage = exports.LocalCloud = void 0;
|
4
4
|
const o1js_1 = require("o1js");
|
5
|
+
const cache_1 = require("./cache");
|
5
6
|
const cloud_1 = require("./cloud");
|
6
|
-
const
|
7
|
+
const utils_1 = require("../utils/utils");
|
7
8
|
const files_1 = require("./files");
|
8
9
|
class LocalCloud extends cloud_1.Cloud {
|
9
10
|
constructor(params) {
|
@@ -14,7 +15,7 @@ class LocalCloud extends cloud_1.Cloud {
|
|
14
15
|
jobId: jobId,
|
15
16
|
stepId: stepId ?? "stepId",
|
16
17
|
taskId: taskId ?? "taskId",
|
17
|
-
cache: cache ??
|
18
|
+
cache: cache ?? cache_1.Cache.FileSystem("./cache"),
|
18
19
|
developer: developer,
|
19
20
|
repo: repo,
|
20
21
|
task: task,
|
@@ -68,7 +69,7 @@ class LocalCloud extends cloud_1.Cloud {
|
|
68
69
|
throw new Error("Method not implemented.");
|
69
70
|
}
|
70
71
|
static generateId() {
|
71
|
-
return "local." + Date.now().toString() + "." + (0,
|
72
|
+
return "local." + Date.now().toString() + "." + (0, utils_1.makeString)(32);
|
72
73
|
}
|
73
74
|
static async addTransactions(transactions) {
|
74
75
|
const timeReceived = Date.now();
|
@@ -4,10 +4,10 @@ export declare function fetchMinaAccount(params: {
|
|
4
4
|
tokenId?: string | Field | undefined;
|
5
5
|
force?: boolean;
|
6
6
|
}): Promise<{
|
7
|
+
account: undefined;
|
8
|
+
} | {
|
7
9
|
account: import("o1js/dist/node/bindings/mina-transaction/gen/transaction").Account;
|
8
10
|
error: undefined;
|
9
|
-
} | {
|
10
|
-
account: undefined;
|
11
11
|
}>;
|
12
12
|
export declare function fetchMinaActions(publicKey: PublicKey, fromActionState: Field, endActionState?: Field): Promise<{
|
13
13
|
actions: string[][];
|
@@ -2,7 +2,7 @@
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.checkMinaZkappTransaction = exports.fetchMinaActions = exports.fetchMinaAccount = void 0;
|
4
4
|
const o1js_1 = require("o1js");
|
5
|
-
const
|
5
|
+
const utils_1 = require("./utils");
|
6
6
|
async function fetchMinaAccount(params) {
|
7
7
|
const { publicKey, tokenId, force } = params;
|
8
8
|
const timeout = 1000 * 60 * 2; // 2 minutes
|
@@ -34,7 +34,7 @@ async function fetchMinaAccount(params) {
|
|
34
34
|
return result;
|
35
35
|
}
|
36
36
|
}
|
37
|
-
await (0,
|
37
|
+
await (0, utils_1.sleep)(1000 * 5);
|
38
38
|
}
|
39
39
|
if (force === true)
|
40
40
|
throw new Error(`fetchMinaAccount timeout
|
@@ -65,7 +65,7 @@ async function fetchMinaActions(publicKey, fromActionState, endActionState) {
|
|
65
65
|
catch (error) {
|
66
66
|
console.log("Error in fetchMinaActions", error.toString().substring(0, 300));
|
67
67
|
}
|
68
|
-
await (0,
|
68
|
+
await (0, utils_1.sleep)(1000 * 60 * 2);
|
69
69
|
}
|
70
70
|
console.log("Timeout in fetchMinaActions");
|
71
71
|
return undefined;
|
@@ -1,4 +1,4 @@
|
|
1
|
-
export { initBlockchain,
|
1
|
+
export { initBlockchain, accountBalance, accountBalanceMina, MinaNetworkInstance, currentNetwork, getNetworkIdHash, getCurrentNetwork, getDeployer, };
|
2
2
|
import { Mina, PublicKey, UInt64, Field } from "o1js";
|
3
3
|
import { blockchain, MinaNetwork } from "../networks";
|
4
4
|
interface MinaNetworkInstance {
|
@@ -13,11 +13,3 @@ declare function getDeployer(): Mina.TestPublicKey | undefined;
|
|
13
13
|
declare function initBlockchain(instance: blockchain, deployersNumber?: number): Promise<MinaNetworkInstance>;
|
14
14
|
declare function accountBalance(address: PublicKey): Promise<UInt64>;
|
15
15
|
declare function accountBalanceMina(address: PublicKey): Promise<number>;
|
16
|
-
declare function sleep(ms: number): Promise<unknown>;
|
17
|
-
declare function makeString(length: number): string;
|
18
|
-
declare function formatTime(ms: number): string;
|
19
|
-
declare class Memory {
|
20
|
-
static rss: number;
|
21
|
-
constructor();
|
22
|
-
static info(description?: string, fullInfo?: boolean): void;
|
23
|
-
}
|
package/lib/ts/src/utils/mina.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.getDeployer = exports.getCurrentNetwork = exports.getNetworkIdHash = exports.currentNetwork = exports.
|
3
|
+
exports.getDeployer = exports.getCurrentNetwork = exports.getNetworkIdHash = exports.currentNetwork = exports.accountBalanceMina = exports.accountBalance = exports.initBlockchain = void 0;
|
4
4
|
const o1js_1 = require("o1js");
|
5
5
|
const networks_1 = require("../networks");
|
6
6
|
let currentNetwork = undefined;
|
@@ -32,6 +32,14 @@ async function initBlockchain(instance, deployersNumber = 0) {
|
|
32
32
|
if (instance === "mainnet") {
|
33
33
|
throw new Error("Mainnet is not supported yet by zkApps");
|
34
34
|
}
|
35
|
+
if (currentNetwork !== undefined) {
|
36
|
+
if (currentNetwork?.network.chainId === instance) {
|
37
|
+
return currentNetwork;
|
38
|
+
}
|
39
|
+
else {
|
40
|
+
throw new Error(`Network is already initialized to different chain ${currentNetwork.network.chainId}, cannot initialize to ${instance}`);
|
41
|
+
}
|
42
|
+
}
|
35
43
|
const networkIdHash = o1js_1.Poseidon.hash(o1js_1.Encoding.stringToFields(instance));
|
36
44
|
// await used for compatibility with future versions of o1js
|
37
45
|
if (instance === "local") {
|
@@ -100,67 +108,3 @@ async function accountBalanceMina(address) {
|
|
100
108
|
return Number((await accountBalance(address)).toBigInt()) / 1e9;
|
101
109
|
}
|
102
110
|
exports.accountBalanceMina = accountBalanceMina;
|
103
|
-
function sleep(ms) {
|
104
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
105
|
-
}
|
106
|
-
exports.sleep = sleep;
|
107
|
-
function makeString(length) {
|
108
|
-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
109
|
-
let outString = ``;
|
110
|
-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
111
|
-
const inOptions = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`;
|
112
|
-
for (let i = 0; i < length; i++) {
|
113
|
-
outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));
|
114
|
-
}
|
115
|
-
return outString;
|
116
|
-
}
|
117
|
-
exports.makeString = makeString;
|
118
|
-
function formatTime(ms) {
|
119
|
-
if (ms === undefined)
|
120
|
-
return "";
|
121
|
-
if (ms < 1000)
|
122
|
-
return ms.toString() + " ms";
|
123
|
-
if (ms < 60 * 1000)
|
124
|
-
return parseInt((ms / 1000).toString()).toString() + " sec";
|
125
|
-
if (ms < 60 * 60 * 1000) {
|
126
|
-
const minutes = parseInt((ms / 1000 / 60).toString());
|
127
|
-
const seconds = parseInt(((ms - minutes * 60 * 1000) / 1000).toString());
|
128
|
-
return minutes.toString() + " min " + seconds.toString() + " sec";
|
129
|
-
}
|
130
|
-
else {
|
131
|
-
const hours = parseInt((ms / 1000 / 60 / 60).toString());
|
132
|
-
const minutes = parseInt(((ms - hours * 60 * 60 * 1000) / 1000 / 60).toString());
|
133
|
-
return hours.toString() + " h " + minutes.toString() + " min";
|
134
|
-
}
|
135
|
-
}
|
136
|
-
exports.formatTime = formatTime;
|
137
|
-
class Memory {
|
138
|
-
constructor() {
|
139
|
-
Memory.rss = 0;
|
140
|
-
}
|
141
|
-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
142
|
-
static info(description = ``, fullInfo = false) {
|
143
|
-
const memoryData = process.memoryUsage();
|
144
|
-
const formatMemoryUsage = (data) => `${Math.round(data / 1024 / 1024)} MB`;
|
145
|
-
const oldRSS = Memory.rss;
|
146
|
-
Memory.rss = Math.round(memoryData.rss / 1024 / 1024);
|
147
|
-
const memoryUsage = fullInfo
|
148
|
-
? {
|
149
|
-
step: `${description}:`,
|
150
|
-
rssDelta: `${(oldRSS === 0
|
151
|
-
? 0
|
152
|
-
: Memory.rss - oldRSS).toString()} MB -> Resident Set Size memory change`,
|
153
|
-
rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated`,
|
154
|
-
heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
|
155
|
-
heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
|
156
|
-
external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
|
157
|
-
}
|
158
|
-
: `RSS memory ${description}: ${formatMemoryUsage(memoryData.rss)}${oldRSS === 0
|
159
|
-
? ``
|
160
|
-
: `, changed by ` + (Memory.rss - oldRSS).toString() + ` MB`}`;
|
161
|
-
console.log(memoryUsage);
|
162
|
-
}
|
163
|
-
}
|
164
|
-
exports.Memory = Memory;
|
165
|
-
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
166
|
-
Memory.rss = 0;
|
@@ -0,0 +1,8 @@
|
|
1
|
+
export declare function sleep(ms: number): Promise<unknown>;
|
2
|
+
export declare function makeString(length: number): string;
|
3
|
+
export declare function formatTime(ms: number): string;
|
4
|
+
export declare class Memory {
|
5
|
+
static rss: number;
|
6
|
+
constructor();
|
7
|
+
static info(description?: string, fullInfo?: boolean): void;
|
8
|
+
}
|
@@ -0,0 +1,67 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.Memory = exports.formatTime = exports.makeString = exports.sleep = void 0;
|
4
|
+
function sleep(ms) {
|
5
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
6
|
+
}
|
7
|
+
exports.sleep = sleep;
|
8
|
+
function makeString(length) {
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
10
|
+
let outString = ``;
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
12
|
+
const inOptions = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`;
|
13
|
+
for (let i = 0; i < length; i++) {
|
14
|
+
outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));
|
15
|
+
}
|
16
|
+
return outString;
|
17
|
+
}
|
18
|
+
exports.makeString = makeString;
|
19
|
+
function formatTime(ms) {
|
20
|
+
if (ms === undefined)
|
21
|
+
return "";
|
22
|
+
if (ms < 1000)
|
23
|
+
return ms.toString() + " ms";
|
24
|
+
if (ms < 60 * 1000)
|
25
|
+
return parseInt((ms / 1000).toString()).toString() + " sec";
|
26
|
+
if (ms < 60 * 60 * 1000) {
|
27
|
+
const minutes = parseInt((ms / 1000 / 60).toString());
|
28
|
+
const seconds = parseInt(((ms - minutes * 60 * 1000) / 1000).toString());
|
29
|
+
return minutes.toString() + " min " + seconds.toString() + " sec";
|
30
|
+
}
|
31
|
+
else {
|
32
|
+
const hours = parseInt((ms / 1000 / 60 / 60).toString());
|
33
|
+
const minutes = parseInt(((ms - hours * 60 * 60 * 1000) / 1000 / 60).toString());
|
34
|
+
return hours.toString() + " h " + minutes.toString() + " min";
|
35
|
+
}
|
36
|
+
}
|
37
|
+
exports.formatTime = formatTime;
|
38
|
+
class Memory {
|
39
|
+
constructor() {
|
40
|
+
Memory.rss = 0;
|
41
|
+
}
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
43
|
+
static info(description = ``, fullInfo = false) {
|
44
|
+
const memoryData = process.memoryUsage();
|
45
|
+
const formatMemoryUsage = (data) => `${Math.round(data / 1024 / 1024)} MB`;
|
46
|
+
const oldRSS = Memory.rss;
|
47
|
+
Memory.rss = Math.round(memoryData.rss / 1024 / 1024);
|
48
|
+
const memoryUsage = fullInfo
|
49
|
+
? {
|
50
|
+
step: `${description}:`,
|
51
|
+
rssDelta: `${(oldRSS === 0
|
52
|
+
? 0
|
53
|
+
: Memory.rss - oldRSS).toString()} MB -> Resident Set Size memory change`,
|
54
|
+
rss: `${formatMemoryUsage(memoryData.rss)} -> Resident Set Size - total memory allocated`,
|
55
|
+
heapTotal: `${formatMemoryUsage(memoryData.heapTotal)} -> total size of the allocated heap`,
|
56
|
+
heapUsed: `${formatMemoryUsage(memoryData.heapUsed)} -> actual memory used during the execution`,
|
57
|
+
external: `${formatMemoryUsage(memoryData.external)} -> V8 external memory`,
|
58
|
+
}
|
59
|
+
: `RSS memory ${description}: ${formatMemoryUsage(memoryData.rss)}${oldRSS === 0
|
60
|
+
? ``
|
61
|
+
: `, changed by ` + (Memory.rss - oldRSS).toString() + ` MB`}`;
|
62
|
+
console.log(memoryUsage);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
exports.Memory = Memory;
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-inferrable-types
|
67
|
+
Memory.rss = 0;
|