ic-mops 0.33.0 → 0.34.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/api/actors.ts +45 -0
- package/api/downloadPackageFiles.ts +78 -0
- package/api/getHighestVersion.ts +6 -0
- package/api/index.ts +4 -0
- package/api/network.ts +24 -0
- package/api/resolveVersion.ts +12 -0
- package/cli.ts +18 -4
- package/commands/add.ts +2 -1
- package/commands/available-updates.ts +1 -1
- package/commands/init.ts +2 -1
- package/commands/install.ts +26 -49
- package/commands/publish.ts +4 -2
- package/commands/search.ts +2 -3
- package/commands/transfer-ownership.ts +4 -2
- package/commands/user.ts +5 -2
- package/dist/api/actors.d.ts +6 -0
- package/dist/api/actors.js +32 -0
- package/dist/api/downloadPackageFiles.d.ts +12 -0
- package/dist/api/downloadPackageFiles.js +62 -0
- package/dist/api/getHighestVersion.d.ts +1 -0
- package/dist/api/getHighestVersion.js +5 -0
- package/dist/api/index.d.ts +4 -0
- package/dist/api/index.js +4 -0
- package/dist/api/network.d.ts +5 -0
- package/dist/api/network.js +23 -0
- package/dist/api/resolveVersion.d.ts +1 -0
- package/dist/api/resolveVersion.js +11 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +11 -5
- package/dist/commands/add.js +2 -1
- package/dist/commands/available-updates.js +1 -1
- package/dist/commands/init.js +2 -1
- package/dist/commands/install.js +22 -43
- package/dist/commands/publish.js +4 -2
- package/dist/commands/search.js +2 -3
- package/dist/commands/transfer-ownership.js +4 -2
- package/dist/commands/user.js +4 -2
- package/dist/helpers/download-package-files.d.ts +12 -0
- package/dist/helpers/download-package-files.js +62 -0
- package/dist/helpers/resolve-version.d.ts +1 -0
- package/dist/helpers/resolve-version.js +11 -0
- package/dist/integrity.js +2 -1
- package/dist/mops.d.ts +6 -12
- package/dist/mops.js +15 -70
- package/dist/notify-installs.js +2 -1
- package/dist/package.json +1 -1
- package/integrity.ts +2 -1
- package/mops.ts +24 -85
- package/notify-installs.ts +2 -1
- package/package.json +1 -1
package/api/actors.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {Actor, HttpAgent, Identity} from '@dfinity/agent';
|
|
2
|
+
import {Principal} from '@dfinity/principal';
|
|
3
|
+
|
|
4
|
+
import {idlFactory} from '../declarations/main/index.js';
|
|
5
|
+
import {_SERVICE} from '../declarations/main/main.did.js';
|
|
6
|
+
import {idlFactory as storageIdlFactory} from '../declarations/storage/index.js';
|
|
7
|
+
import {_SERVICE as _STORAGE_SERVICE} from '../declarations/storage/storage.did.js';
|
|
8
|
+
|
|
9
|
+
import {getEndpoint} from './network.js';
|
|
10
|
+
import {getNetwork} from './network.js';
|
|
11
|
+
|
|
12
|
+
export let mainActor = async (identity?: Identity): Promise<_SERVICE> => {
|
|
13
|
+
let network = getNetwork();
|
|
14
|
+
let host = getEndpoint(network).host;
|
|
15
|
+
let canisterId = getEndpoint(network).canisterId;
|
|
16
|
+
|
|
17
|
+
// @ts-ignore exactOptionalPropertyTypes
|
|
18
|
+
let agent = new HttpAgent({host, identity});
|
|
19
|
+
|
|
20
|
+
if (network === 'local') {
|
|
21
|
+
await agent.fetchRootKey();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return Actor.createActor(idlFactory, {
|
|
25
|
+
agent,
|
|
26
|
+
canisterId,
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export let storageActor = async (storageId: Principal, identity?: Identity): Promise<_STORAGE_SERVICE> => {
|
|
31
|
+
let network = getNetwork();
|
|
32
|
+
let host = getEndpoint(network).host;
|
|
33
|
+
|
|
34
|
+
// @ts-ignore exactOptionalPropertyTypes
|
|
35
|
+
let agent = new HttpAgent({host, identity});
|
|
36
|
+
|
|
37
|
+
if (network === 'local') {
|
|
38
|
+
await agent.fetchRootKey();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return Actor.createActor(storageIdlFactory, {
|
|
42
|
+
agent,
|
|
43
|
+
canisterId: storageId,
|
|
44
|
+
});
|
|
45
|
+
};
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {Principal} from '@dfinity/principal';
|
|
2
|
+
import {mainActor, storageActor} from './actors.js';
|
|
3
|
+
import {resolveVersion} from './resolveVersion.js';
|
|
4
|
+
import {parallel} from '../parallel.js';
|
|
5
|
+
import {Storage} from '../declarations/storage/storage.did.js';
|
|
6
|
+
|
|
7
|
+
export async function downloadPackageFiles(pkg: string, version = '', threads = 8, onLoad = (_fileIds: string[], _fileId: string) => {}): Promise<Map<string, Array<number>>> {
|
|
8
|
+
version = await resolveVersion(pkg, version);
|
|
9
|
+
|
|
10
|
+
let {storageId, fileIds} = await getPackageFilesInfo(pkg, version);
|
|
11
|
+
let storage = await storageActor(storageId);
|
|
12
|
+
|
|
13
|
+
let filesData = new Map<string, Array<number>>();
|
|
14
|
+
await parallel(threads, fileIds, async (fileId: string) => {
|
|
15
|
+
let {path, data} = await downloadFile(storage, fileId);
|
|
16
|
+
filesData.set(path, data);
|
|
17
|
+
onLoad(fileIds, fileId);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return filesData;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// get package files meta
|
|
24
|
+
export async function getPackageFilesInfo(pkg: string, version: string): Promise<{ storageId: Principal, fileIds: string[] }> {
|
|
25
|
+
let actor = await mainActor();
|
|
26
|
+
|
|
27
|
+
let [packageDetailsRes, fileIds] = await Promise.all([
|
|
28
|
+
actor.getPackageDetails(pkg, version),
|
|
29
|
+
getFileIds(pkg, version),
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
if ('err' in packageDetailsRes) {
|
|
33
|
+
throw packageDetailsRes.err;
|
|
34
|
+
}
|
|
35
|
+
let packageDetails = packageDetailsRes.ok;
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
storageId: packageDetails.publication.storage,
|
|
39
|
+
fileIds,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// get package files ids
|
|
44
|
+
export async function getFileIds(pkg: string, version: string): Promise<string[]> {
|
|
45
|
+
let actor = await mainActor();
|
|
46
|
+
let fileIdsRes = await actor.getFileIds(pkg, version);
|
|
47
|
+
|
|
48
|
+
if ('err' in fileIdsRes) {
|
|
49
|
+
throw fileIdsRes.err;
|
|
50
|
+
}
|
|
51
|
+
let filesIds = fileIdsRes.ok;
|
|
52
|
+
|
|
53
|
+
return filesIds;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// download single file
|
|
57
|
+
export async function downloadFile(storage: Storage, fileId: string): Promise<{ path: string, data: Array<number> }> {
|
|
58
|
+
let fileMetaRes = await storage.getFileMeta(fileId);
|
|
59
|
+
if ('err' in fileMetaRes) {
|
|
60
|
+
throw fileMetaRes.err;
|
|
61
|
+
}
|
|
62
|
+
let fileMeta = fileMetaRes.ok;
|
|
63
|
+
|
|
64
|
+
let data: Array<number> = [];
|
|
65
|
+
for (let i = 0n; i < fileMeta.chunkCount; i++) {
|
|
66
|
+
let chunkRes = await storage.downloadChunk(fileId, i);
|
|
67
|
+
if ('err' in chunkRes) {
|
|
68
|
+
throw chunkRes.err;
|
|
69
|
+
}
|
|
70
|
+
let chunk = chunkRes.ok;
|
|
71
|
+
data = [...data, ...chunk];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
path: fileMeta.path,
|
|
76
|
+
data: data,
|
|
77
|
+
};
|
|
78
|
+
}
|
package/api/index.ts
ADDED
package/api/network.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export function getNetwork() {
|
|
2
|
+
return globalThis.MOPS_NETWORK || 'ic';
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function getEndpoint(network: string) {
|
|
6
|
+
if (network === 'staging') {
|
|
7
|
+
return {
|
|
8
|
+
host: 'https://icp-api.io',
|
|
9
|
+
canisterId: '2d2zu-vaaaa-aaaak-qb6pq-cai',
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
else if (network === 'ic') {
|
|
13
|
+
return {
|
|
14
|
+
host: 'https://icp-api.io',
|
|
15
|
+
canisterId: 'oknww-riaaa-aaaam-qaf6a-cai',
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return {
|
|
20
|
+
host: 'http://127.0.0.1:4943',
|
|
21
|
+
canisterId: '2d2zu-vaaaa-aaaak-qb6pq-cai',
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {getHighestVersion} from './getHighestVersion.js';
|
|
2
|
+
|
|
3
|
+
export async function resolveVersion(pkg: string, version = ''): Promise<string> {
|
|
4
|
+
if (!version) {
|
|
5
|
+
let versionRes = await getHighestVersion(pkg);
|
|
6
|
+
if ('err' in versionRes) {
|
|
7
|
+
throw versionRes.err;
|
|
8
|
+
}
|
|
9
|
+
version = versionRes.ok;
|
|
10
|
+
}
|
|
11
|
+
return version;
|
|
12
|
+
}
|
package/cli.ts
CHANGED
|
@@ -9,7 +9,9 @@ import {init} from './commands/init.js';
|
|
|
9
9
|
import {publish} from './commands/publish.js';
|
|
10
10
|
import {importPem} from './commands/import-identity.js';
|
|
11
11
|
import {sources} from './commands/sources.js';
|
|
12
|
-
import {checkApiCompatibility,
|
|
12
|
+
import {checkApiCompatibility, setNetwork, apiVersion, checkConfigFile, getNetworkFile, getIdentity} from './mops.js';
|
|
13
|
+
import {getNetwork} from './api/network.js';
|
|
14
|
+
import {mainActor} from './api/actors.js';
|
|
13
15
|
import {whoami} from './commands/whoami.js';
|
|
14
16
|
import {installAll} from './commands/install-all.js';
|
|
15
17
|
import {search} from './commands/search.js';
|
|
@@ -28,6 +30,17 @@ import {bench} from './commands/bench.js';
|
|
|
28
30
|
import {transferOwnership} from './commands/transfer-ownership.js';
|
|
29
31
|
// import {docs} from './commands/docs.js';
|
|
30
32
|
|
|
33
|
+
declare global {
|
|
34
|
+
// eslint-disable-next-line no-var
|
|
35
|
+
var MOPS_NETWORK: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let networkFile = getNetworkFile();
|
|
39
|
+
if (fs.existsSync(networkFile)) {
|
|
40
|
+
globalThis.MOPS_NETWORK = fs.readFileSync(networkFile).toString() || 'ic';
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
|
|
31
44
|
program.name('mops');
|
|
32
45
|
|
|
33
46
|
// --version
|
|
@@ -132,7 +145,7 @@ program
|
|
|
132
145
|
.alias('gn')
|
|
133
146
|
.description('Get network')
|
|
134
147
|
.action(async () => {
|
|
135
|
-
console.log(getNetwork()
|
|
148
|
+
console.log(getNetwork());
|
|
136
149
|
});
|
|
137
150
|
|
|
138
151
|
// import-identity
|
|
@@ -272,11 +285,12 @@ program
|
|
|
272
285
|
}
|
|
273
286
|
});
|
|
274
287
|
|
|
275
|
-
// airdrop
|
|
288
|
+
// temp: airdrop
|
|
276
289
|
program
|
|
277
290
|
.command('airdrop <check|claim> [canister]')
|
|
278
291
|
.action(async (sub, canister) => {
|
|
279
|
-
let
|
|
292
|
+
let identity = await getIdentity();
|
|
293
|
+
let main = await mainActor(identity);
|
|
280
294
|
if (sub === 'check') {
|
|
281
295
|
let amount = await main.getAirdropAmount();
|
|
282
296
|
if (amount === 0n) {
|
package/commands/add.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
3
|
import logUpdate from 'log-update';
|
|
4
|
-
import {checkConfigFile, getGithubCommit,
|
|
4
|
+
import {checkConfigFile, getGithubCommit, parseGithubURL, readConfig, writeConfig} from '../mops.js';
|
|
5
|
+
import {getHighestVersion} from '../api/getHighestVersion.js';
|
|
5
6
|
import {installFromGithub} from '../vessel.js';
|
|
6
7
|
import {install} from './install.js';
|
|
7
8
|
import {notifyInstalls} from '../notify-installs.js';
|
package/commands/init.ts
CHANGED
|
@@ -4,7 +4,8 @@ import {existsSync, readFileSync, writeFileSync} from 'node:fs';
|
|
|
4
4
|
import chalk from 'chalk';
|
|
5
5
|
import prompts from 'prompts';
|
|
6
6
|
|
|
7
|
-
import {checkApiCompatibility,
|
|
7
|
+
import {checkApiCompatibility, writeConfig} from '../mops.js';
|
|
8
|
+
import {mainActor} from '../api/actors.js';
|
|
8
9
|
import {installAll} from './install-all.js';
|
|
9
10
|
import {VesselConfig, readVesselConfig} from '../vessel.js';
|
|
10
11
|
import {Config, Dependencies} from '../types.js';
|
package/commands/install.ts
CHANGED
|
@@ -2,10 +2,13 @@ import path from 'node:path';
|
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import logUpdate from 'log-update';
|
|
4
4
|
import chalk from 'chalk';
|
|
5
|
-
import {checkConfigFile, formatDir,
|
|
5
|
+
import {checkConfigFile, formatDir, progressBar, readConfig} from '../mops.js';
|
|
6
|
+
import {getHighestVersion} from '../api/getHighestVersion.js';
|
|
7
|
+
import {storageActor} from '../api/actors.js';
|
|
6
8
|
import {parallel} from '../parallel.js';
|
|
7
9
|
import {installFromGithub} from '../vessel.js';
|
|
8
10
|
import {addCache, copyCache, isCached} from '../cache.js';
|
|
11
|
+
import {downloadFile, getPackageFilesInfo} from '../api/downloadPackageFiles.js';
|
|
9
12
|
|
|
10
13
|
export async function install(pkg: string, version = '', {verbose = false, silent = false, dep = false} = {}): Promise<Record<string, string> | false> {
|
|
11
14
|
if (!checkConfigFile()) {
|
|
@@ -31,7 +34,6 @@ export async function install(pkg: string, version = '', {verbose = false, silen
|
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
let dir = formatDir(pkg, version);
|
|
34
|
-
let actor = await mainActor();
|
|
35
37
|
let alreadyInstalled = false;
|
|
36
38
|
|
|
37
39
|
// already installed
|
|
@@ -46,28 +48,6 @@ export async function install(pkg: string, version = '', {verbose = false, silen
|
|
|
46
48
|
}
|
|
47
49
|
// download
|
|
48
50
|
else {
|
|
49
|
-
let [packageDetailsRes, filesIdsRes] = await Promise.all([
|
|
50
|
-
actor.getPackageDetails(pkg, version),
|
|
51
|
-
actor.getFileIds(pkg, version),
|
|
52
|
-
]);
|
|
53
|
-
|
|
54
|
-
if ('err' in packageDetailsRes) {
|
|
55
|
-
console.log(chalk.red('Error: ') + packageDetailsRes.err);
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
let packageDetails = packageDetailsRes.ok;
|
|
59
|
-
|
|
60
|
-
if ('err' in filesIdsRes) {
|
|
61
|
-
console.log(chalk.red('Error: ') + filesIdsRes.err);
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
let filesIds = filesIdsRes.ok;
|
|
65
|
-
total = filesIds.length + 2;
|
|
66
|
-
|
|
67
|
-
let storage = await storageActor(packageDetails.publication.storage);
|
|
68
|
-
|
|
69
|
-
// download files
|
|
70
|
-
let filesData = new Map;
|
|
71
51
|
let threads = 16;
|
|
72
52
|
|
|
73
53
|
// GitHub Actions fails with "fetch failed" if there are multiple concurrent actions
|
|
@@ -75,32 +55,29 @@ export async function install(pkg: string, version = '', {verbose = false, silen
|
|
|
75
55
|
threads = 4;
|
|
76
56
|
}
|
|
77
57
|
|
|
78
|
-
|
|
79
|
-
let
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
let
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
58
|
+
try {
|
|
59
|
+
let {storageId, fileIds} = await getPackageFilesInfo(pkg, version);
|
|
60
|
+
|
|
61
|
+
total = fileIds.length + 2;
|
|
62
|
+
|
|
63
|
+
let filesData = new Map;
|
|
64
|
+
let storage = await storageActor(storageId);
|
|
65
|
+
|
|
66
|
+
await parallel(threads, fileIds, async (fileId: string) => {
|
|
67
|
+
let {path, data} = await downloadFile(storage, fileId);
|
|
68
|
+
filesData.set(path, data);
|
|
69
|
+
progress();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// write files to disk
|
|
73
|
+
for (let [filePath, data] of filesData.entries()) {
|
|
74
|
+
fs.mkdirSync(path.join(dir, path.dirname(filePath)), {recursive: true});
|
|
75
|
+
fs.writeFileSync(path.join(dir, filePath), Buffer.from(data));
|
|
95
76
|
}
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
// write files to disk
|
|
101
|
-
for (let [filePath, buffer] of filesData.entries()) {
|
|
102
|
-
fs.mkdirSync(path.join(dir, path.dirname(filePath)), {recursive: true});
|
|
103
|
-
fs.writeFileSync(path.join(dir, filePath), buffer);
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.error(chalk.red('Error: ') + err);
|
|
80
|
+
return false;
|
|
104
81
|
}
|
|
105
82
|
|
|
106
83
|
// add to cache
|
package/commands/publish.ts
CHANGED
|
@@ -8,7 +8,8 @@ import prompts from 'prompts';
|
|
|
8
8
|
import {fromMarkdown} from 'mdast-util-from-markdown';
|
|
9
9
|
import {toMarkdown} from 'mdast-util-to-markdown';
|
|
10
10
|
|
|
11
|
-
import {checkConfigFile,
|
|
11
|
+
import {checkConfigFile, getIdentity, getRootDir, progressBar, readConfig} from '../mops.js';
|
|
12
|
+
import {mainActor} from '../api/actors.js';
|
|
12
13
|
import {parallel} from '../parallel.js';
|
|
13
14
|
import {docs} from './docs.js';
|
|
14
15
|
import {DependencyV2, PackageConfigV2} from '../declarations/main/main.did.js';
|
|
@@ -266,7 +267,8 @@ export async function publish(options: {docs?: boolean, test?: boolean} = {}) {
|
|
|
266
267
|
}
|
|
267
268
|
|
|
268
269
|
// upload config
|
|
269
|
-
let
|
|
270
|
+
let identity = await getIdentity();
|
|
271
|
+
let actor = await mainActor(identity);
|
|
270
272
|
|
|
271
273
|
progress();
|
|
272
274
|
let publishing = await actor.startPublish(backendPkgConfig);
|
package/commands/search.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import asTable from 'as-table';
|
|
2
2
|
import chalk from 'chalk';
|
|
3
|
-
import {mainActor} from '../
|
|
3
|
+
import {mainActor} from '../api/actors.js';
|
|
4
4
|
|
|
5
5
|
export async function search(text: string) {
|
|
6
6
|
let actor = await mainActor();
|
|
7
|
-
let
|
|
8
|
-
let packages = res[0];
|
|
7
|
+
let [packages, _pageCount] = await actor.search(text, [], []);
|
|
9
8
|
|
|
10
9
|
if (!packages.length) {
|
|
11
10
|
console.log('Packages not found');
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
-
import {checkConfigFile,
|
|
2
|
+
import {checkConfigFile, getIdentity, readConfig} from '../mops.js';
|
|
3
|
+
import {mainActor} from '../api/actors.js';
|
|
3
4
|
import {Principal} from '@dfinity/principal';
|
|
4
5
|
import prompts from 'prompts';
|
|
5
6
|
|
|
@@ -30,7 +31,8 @@ export async function transferOwnership(toPrincipal: string) {
|
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
let
|
|
34
|
+
let identity = await getIdentity();
|
|
35
|
+
let actor = await mainActor(identity);
|
|
34
36
|
|
|
35
37
|
let res = await actor.transferOwnership(config.package?.name || '', principal);
|
|
36
38
|
if ('ok' in res) {
|
package/commands/user.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
|
-
import {getIdentity
|
|
2
|
+
import {getIdentity} from '../mops.js';
|
|
3
|
+
import {mainActor} from '../api/actors.js';
|
|
3
4
|
|
|
4
5
|
export async function getUserProp(prop: string) {
|
|
5
6
|
let actor = await mainActor();
|
|
@@ -14,7 +15,9 @@ export async function getUserProp(prop: string) {
|
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export async function setUserProp(prop: string, value: string) {
|
|
17
|
-
let
|
|
18
|
+
let identity = await getIdentity();
|
|
19
|
+
let actor = await mainActor(identity);
|
|
20
|
+
|
|
18
21
|
let res = await actor.setUserProp(prop, value);
|
|
19
22
|
if ('ok' in res) {
|
|
20
23
|
console.log(chalk.green('Success!'));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Identity } from '@dfinity/agent';
|
|
2
|
+
import { Principal } from '@dfinity/principal';
|
|
3
|
+
import { _SERVICE } from '../declarations/main/main.did.js';
|
|
4
|
+
import { _SERVICE as _STORAGE_SERVICE } from '../declarations/storage/storage.did.js';
|
|
5
|
+
export declare let mainActor: (identity?: Identity) => Promise<_SERVICE>;
|
|
6
|
+
export declare let storageActor: (storageId: Principal, identity?: Identity) => Promise<_STORAGE_SERVICE>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Actor, HttpAgent } from '@dfinity/agent';
|
|
2
|
+
import { idlFactory } from '../declarations/main/index.js';
|
|
3
|
+
import { idlFactory as storageIdlFactory } from '../declarations/storage/index.js';
|
|
4
|
+
import { getEndpoint } from './network.js';
|
|
5
|
+
import { getNetwork } from './network.js';
|
|
6
|
+
export let mainActor = async (identity) => {
|
|
7
|
+
let network = getNetwork();
|
|
8
|
+
let host = getEndpoint(network).host;
|
|
9
|
+
let canisterId = getEndpoint(network).canisterId;
|
|
10
|
+
// @ts-ignore exactOptionalPropertyTypes
|
|
11
|
+
let agent = new HttpAgent({ host, identity });
|
|
12
|
+
if (network === 'local') {
|
|
13
|
+
await agent.fetchRootKey();
|
|
14
|
+
}
|
|
15
|
+
return Actor.createActor(idlFactory, {
|
|
16
|
+
agent,
|
|
17
|
+
canisterId,
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
export let storageActor = async (storageId, identity) => {
|
|
21
|
+
let network = getNetwork();
|
|
22
|
+
let host = getEndpoint(network).host;
|
|
23
|
+
// @ts-ignore exactOptionalPropertyTypes
|
|
24
|
+
let agent = new HttpAgent({ host, identity });
|
|
25
|
+
if (network === 'local') {
|
|
26
|
+
await agent.fetchRootKey();
|
|
27
|
+
}
|
|
28
|
+
return Actor.createActor(storageIdlFactory, {
|
|
29
|
+
agent,
|
|
30
|
+
canisterId: storageId,
|
|
31
|
+
});
|
|
32
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Principal } from '@dfinity/principal';
|
|
2
|
+
import { Storage } from '../declarations/storage/storage.did.js';
|
|
3
|
+
export declare function downloadPackageFiles(pkg: string, version?: string, threads?: number, onLoad?: (_fileIds: string[], _fileId: string) => void): Promise<Map<string, Array<number>>>;
|
|
4
|
+
export declare function getPackageFilesInfo(pkg: string, version: string): Promise<{
|
|
5
|
+
storageId: Principal;
|
|
6
|
+
fileIds: string[];
|
|
7
|
+
}>;
|
|
8
|
+
export declare function getFileIds(pkg: string, version: string): Promise<string[]>;
|
|
9
|
+
export declare function downloadFile(storage: Storage, fileId: string): Promise<{
|
|
10
|
+
path: string;
|
|
11
|
+
data: Array<number>;
|
|
12
|
+
}>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { mainActor, storageActor } from './actors.js';
|
|
2
|
+
import { resolveVersion } from './resolveVersion.js';
|
|
3
|
+
import { parallel } from '../parallel.js';
|
|
4
|
+
export async function downloadPackageFiles(pkg, version = '', threads = 8, onLoad = (_fileIds, _fileId) => { }) {
|
|
5
|
+
version = await resolveVersion(pkg, version);
|
|
6
|
+
let { storageId, fileIds } = await getPackageFilesInfo(pkg, version);
|
|
7
|
+
let storage = await storageActor(storageId);
|
|
8
|
+
let filesData = new Map();
|
|
9
|
+
await parallel(threads, fileIds, async (fileId) => {
|
|
10
|
+
let { path, data } = await downloadFile(storage, fileId);
|
|
11
|
+
filesData.set(path, data);
|
|
12
|
+
onLoad(fileIds, fileId);
|
|
13
|
+
});
|
|
14
|
+
return filesData;
|
|
15
|
+
}
|
|
16
|
+
// get package files meta
|
|
17
|
+
export async function getPackageFilesInfo(pkg, version) {
|
|
18
|
+
let actor = await mainActor();
|
|
19
|
+
let [packageDetailsRes, fileIds] = await Promise.all([
|
|
20
|
+
actor.getPackageDetails(pkg, version),
|
|
21
|
+
getFileIds(pkg, version),
|
|
22
|
+
]);
|
|
23
|
+
if ('err' in packageDetailsRes) {
|
|
24
|
+
throw packageDetailsRes.err;
|
|
25
|
+
}
|
|
26
|
+
let packageDetails = packageDetailsRes.ok;
|
|
27
|
+
return {
|
|
28
|
+
storageId: packageDetails.publication.storage,
|
|
29
|
+
fileIds,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
// get package files ids
|
|
33
|
+
export async function getFileIds(pkg, version) {
|
|
34
|
+
let actor = await mainActor();
|
|
35
|
+
let fileIdsRes = await actor.getFileIds(pkg, version);
|
|
36
|
+
if ('err' in fileIdsRes) {
|
|
37
|
+
throw fileIdsRes.err;
|
|
38
|
+
}
|
|
39
|
+
let filesIds = fileIdsRes.ok;
|
|
40
|
+
return filesIds;
|
|
41
|
+
}
|
|
42
|
+
// download single file
|
|
43
|
+
export async function downloadFile(storage, fileId) {
|
|
44
|
+
let fileMetaRes = await storage.getFileMeta(fileId);
|
|
45
|
+
if ('err' in fileMetaRes) {
|
|
46
|
+
throw fileMetaRes.err;
|
|
47
|
+
}
|
|
48
|
+
let fileMeta = fileMetaRes.ok;
|
|
49
|
+
let data = [];
|
|
50
|
+
for (let i = 0n; i < fileMeta.chunkCount; i++) {
|
|
51
|
+
let chunkRes = await storage.downloadChunk(fileId, i);
|
|
52
|
+
if ('err' in chunkRes) {
|
|
53
|
+
throw chunkRes.err;
|
|
54
|
+
}
|
|
55
|
+
let chunk = chunkRes.ok;
|
|
56
|
+
data = [...data, ...chunk];
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
path: fileMeta.path,
|
|
60
|
+
data: data,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getHighestVersion(pkgName: string): Promise<import("../declarations/main/main.did.js").Result_5>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function getNetwork() {
|
|
2
|
+
return globalThis.MOPS_NETWORK || 'ic';
|
|
3
|
+
}
|
|
4
|
+
export function getEndpoint(network) {
|
|
5
|
+
if (network === 'staging') {
|
|
6
|
+
return {
|
|
7
|
+
host: 'https://icp-api.io',
|
|
8
|
+
canisterId: '2d2zu-vaaaa-aaaak-qb6pq-cai',
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
else if (network === 'ic') {
|
|
12
|
+
return {
|
|
13
|
+
host: 'https://icp-api.io',
|
|
14
|
+
canisterId: 'oknww-riaaa-aaaam-qaf6a-cai',
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
return {
|
|
19
|
+
host: 'http://127.0.0.1:4943',
|
|
20
|
+
canisterId: '2d2zu-vaaaa-aaaak-qb6pq-cai',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function resolveVersion(pkg: string, version?: string): Promise<string>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getHighestVersion } from './getHighestVersion.js';
|
|
2
|
+
export async function resolveVersion(pkg, version = '') {
|
|
3
|
+
if (!version) {
|
|
4
|
+
let versionRes = await getHighestVersion(pkg);
|
|
5
|
+
if ('err' in versionRes) {
|
|
6
|
+
throw versionRes.err;
|
|
7
|
+
}
|
|
8
|
+
version = versionRes.ok;
|
|
9
|
+
}
|
|
10
|
+
return version;
|
|
11
|
+
}
|