ic-mops 0.33.0 → 0.34.0-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.
@@ -2,10 +2,11 @@ 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, getHighestVersion, mainActor, progressBar, readConfig, storageActor} from '../mops.js';
5
+ import {checkConfigFile, formatDir, getHighestVersion, progressBar, readConfig, storageActor} from '../mops.js';
6
6
  import {parallel} from '../parallel.js';
7
7
  import {installFromGithub} from '../vessel.js';
8
8
  import {addCache, copyCache, isCached} from '../cache.js';
9
+ import {downloadFile, getPackageFilesInfo} from '../helpers/download-package-files.js';
9
10
 
10
11
  export async function install(pkg: string, version = '', {verbose = false, silent = false, dep = false} = {}): Promise<Record<string, string> | false> {
11
12
  if (!checkConfigFile()) {
@@ -31,7 +32,6 @@ export async function install(pkg: string, version = '', {verbose = false, silen
31
32
  }
32
33
 
33
34
  let dir = formatDir(pkg, version);
34
- let actor = await mainActor();
35
35
  let alreadyInstalled = false;
36
36
 
37
37
  // already installed
@@ -46,28 +46,6 @@ export async function install(pkg: string, version = '', {verbose = false, silen
46
46
  }
47
47
  // download
48
48
  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
49
  let threads = 16;
72
50
 
73
51
  // GitHub Actions fails with "fetch failed" if there are multiple concurrent actions
@@ -75,32 +53,29 @@ export async function install(pkg: string, version = '', {verbose = false, silen
75
53
  threads = 4;
76
54
  }
77
55
 
78
- await parallel(threads, filesIds, async (fileId: string) => {
79
- let fileMetaRes = await storage.getFileMeta(fileId);
80
- if ('err' in fileMetaRes) {
81
- console.log(chalk.red('ERR: ') + fileMetaRes.err);
82
- return;
83
- }
84
- let fileMeta = fileMetaRes.ok;
85
-
86
- let buffer = Buffer.from([]);
87
- for (let i = 0n; i < fileMeta.chunkCount; i++) {
88
- let chunkRes = await storage.downloadChunk(fileId, i);
89
- if ('err' in chunkRes) {
90
- console.log(chalk.red('ERR: ') + chunkRes.err);
91
- return;
92
- }
93
- let chunk = chunkRes.ok;
94
- buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
56
+ try {
57
+ let {storageId, fileIds} = await getPackageFilesInfo(pkg, version);
58
+
59
+ total = fileIds.length + 2;
60
+
61
+ let filesData = new Map;
62
+ let storage = await storageActor(storageId);
63
+
64
+ await parallel(threads, fileIds, async (fileId: string) => {
65
+ let {path, data} = await downloadFile(storage, fileId);
66
+ filesData.set(path, data);
67
+ progress();
68
+ });
69
+
70
+ // write files to disk
71
+ for (let [filePath, data] of filesData.entries()) {
72
+ fs.mkdirSync(path.join(dir, path.dirname(filePath)), {recursive: true});
73
+ fs.writeFileSync(path.join(dir, filePath), Buffer.from(data));
95
74
  }
96
- filesData.set(fileMeta.path, buffer);
97
- progress();
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);
75
+ }
76
+ catch (err) {
77
+ console.error(chalk.red('Error: ') + err);
78
+ return false;
104
79
  }
105
80
 
106
81
  // add to cache
@@ -4,8 +4,7 @@ import {mainActor} from '../mops.js';
4
4
 
5
5
  export async function search(text: string) {
6
6
  let actor = await mainActor();
7
- let res = await actor.search(text, [], []);
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');
@@ -2,10 +2,11 @@ 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, getHighestVersion, mainActor, progressBar, readConfig, storageActor } from '../mops.js';
5
+ import { checkConfigFile, formatDir, getHighestVersion, progressBar, readConfig, storageActor } from '../mops.js';
6
6
  import { parallel } from '../parallel.js';
7
7
  import { installFromGithub } from '../vessel.js';
8
8
  import { addCache, copyCache, isCached } from '../cache.js';
9
+ import { downloadFile, getPackageFilesInfo } from '../helpers/download-package-files.js';
9
10
  export async function install(pkg, version = '', { verbose = false, silent = false, dep = false } = {}) {
10
11
  if (!checkConfigFile()) {
11
12
  return false;
@@ -27,7 +28,6 @@ export async function install(pkg, version = '', { verbose = false, silent = fal
27
28
  version = versionRes.ok;
28
29
  }
29
30
  let dir = formatDir(pkg, version);
30
- let actor = await mainActor();
31
31
  let alreadyInstalled = false;
32
32
  // already installed
33
33
  if (fs.existsSync(dir)) {
@@ -41,53 +41,30 @@ export async function install(pkg, version = '', { verbose = false, silent = fal
41
41
  }
42
42
  // download
43
43
  else {
44
- let [packageDetailsRes, filesIdsRes] = await Promise.all([
45
- actor.getPackageDetails(pkg, version),
46
- actor.getFileIds(pkg, version),
47
- ]);
48
- if ('err' in packageDetailsRes) {
49
- console.log(chalk.red('Error: ') + packageDetailsRes.err);
50
- return false;
51
- }
52
- let packageDetails = packageDetailsRes.ok;
53
- if ('err' in filesIdsRes) {
54
- console.log(chalk.red('Error: ') + filesIdsRes.err);
55
- return false;
56
- }
57
- let filesIds = filesIdsRes.ok;
58
- total = filesIds.length + 2;
59
- let storage = await storageActor(packageDetails.publication.storage);
60
- // download files
61
- let filesData = new Map;
62
44
  let threads = 16;
63
45
  // GitHub Actions fails with "fetch failed" if there are multiple concurrent actions
64
46
  if (process.env.GITHUB_ENV) {
65
47
  threads = 4;
66
48
  }
67
- await parallel(threads, filesIds, async (fileId) => {
68
- let fileMetaRes = await storage.getFileMeta(fileId);
69
- if ('err' in fileMetaRes) {
70
- console.log(chalk.red('ERR: ') + fileMetaRes.err);
71
- return;
49
+ try {
50
+ let { storageId, fileIds } = await getPackageFilesInfo(pkg, version);
51
+ total = fileIds.length + 2;
52
+ let filesData = new Map;
53
+ let storage = await storageActor(storageId);
54
+ await parallel(threads, fileIds, async (fileId) => {
55
+ let { path, data } = await downloadFile(storage, fileId);
56
+ filesData.set(path, data);
57
+ progress();
58
+ });
59
+ // write files to disk
60
+ for (let [filePath, data] of filesData.entries()) {
61
+ fs.mkdirSync(path.join(dir, path.dirname(filePath)), { recursive: true });
62
+ fs.writeFileSync(path.join(dir, filePath), Buffer.from(data));
72
63
  }
73
- let fileMeta = fileMetaRes.ok;
74
- let buffer = Buffer.from([]);
75
- for (let i = 0n; i < fileMeta.chunkCount; i++) {
76
- let chunkRes = await storage.downloadChunk(fileId, i);
77
- if ('err' in chunkRes) {
78
- console.log(chalk.red('ERR: ') + chunkRes.err);
79
- return;
80
- }
81
- let chunk = chunkRes.ok;
82
- buffer = Buffer.concat([buffer, Buffer.from(chunk)]);
83
- }
84
- filesData.set(fileMeta.path, buffer);
85
- progress();
86
- });
87
- // write files to disk
88
- for (let [filePath, buffer] of filesData.entries()) {
89
- fs.mkdirSync(path.join(dir, path.dirname(filePath)), { recursive: true });
90
- fs.writeFileSync(path.join(dir, filePath), buffer);
64
+ }
65
+ catch (err) {
66
+ console.error(chalk.red('Error: ') + err);
67
+ return false;
91
68
  }
92
69
  // add to cache
93
70
  await addCache(`${pkg}@${version}`, dir);
@@ -3,8 +3,7 @@ import chalk from 'chalk';
3
3
  import { mainActor } from '../mops.js';
4
4
  export async function search(text) {
5
5
  let actor = await mainActor();
6
- let res = await actor.search(text, [], []);
7
- let packages = res[0];
6
+ let [packages, _pageCount] = await actor.search(text, [], []);
8
7
  if (!packages.length) {
9
8
  console.log('Packages not found');
10
9
  return;
@@ -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, string>>;
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 '../mops.js';
2
+ import { parallel } from '../parallel.js';
3
+ import { resolveVersion } from './resolve-version.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 resolveVersion(pkg: string, version?: string): Promise<string>;
@@ -0,0 +1,11 @@
1
+ import { getHighestVersion } from '../mops.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
+ }
package/dist/mops.d.ts CHANGED
@@ -3,6 +3,7 @@ import { _SERVICE } from './declarations/main/main.did.js';
3
3
  import { _SERVICE as _STORAGE_SERVICE } from './declarations/storage/storage.did.js';
4
4
  import { Config } from './types.js';
5
5
  import { Principal } from '@dfinity/principal';
6
+ import { downloadPackageFiles } from './helpers/download-package-files.js';
6
7
  export declare let apiVersion: string;
7
8
  export declare let globalConfigDir: string;
8
9
  export declare let globalCacheDir: string;
@@ -34,3 +35,4 @@ export declare function formatDir(name: string, version: string): string;
34
35
  export declare function formatGithubDir(name: string, repo: string): string;
35
36
  export declare function readDfxJson(): any;
36
37
  export declare function checkApiCompatibility(): Promise<boolean>;
38
+ export { downloadPackageFiles };
package/dist/mops.js CHANGED
@@ -9,6 +9,7 @@ import fetch from 'node-fetch';
9
9
  import { idlFactory } from './declarations/main/index.js';
10
10
  import { idlFactory as storageIdlFactory } from './declarations/storage/index.js';
11
11
  import { decodeFile } from './pem.js';
12
+ import { downloadPackageFiles } from './helpers/download-package-files.js';
12
13
  if (!global.fetch) {
13
14
  global.fetch = fetch;
14
15
  }
@@ -292,3 +293,4 @@ export async function checkApiCompatibility() {
292
293
  }
293
294
  return true;
294
295
  }
296
+ export { downloadPackageFiles };
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "0.33.0",
3
+ "version": "0.34.0-0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/cli.js"
@@ -0,0 +1,78 @@
1
+ import {mainActor, storageActor} from '../mops.js';
2
+ import {parallel} from '../parallel.js';
3
+ import {Principal} from '@dfinity/principal';
4
+ import {resolveVersion} from './resolve-version.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, string>> {
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;
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
+ }
@@ -0,0 +1,12 @@
1
+ import {getHighestVersion} from '../mops.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/mops.ts CHANGED
@@ -14,6 +14,7 @@ import {_SERVICE as _STORAGE_SERVICE} from './declarations/storage/storage.did.j
14
14
  import {decodeFile} from './pem.js';
15
15
  import {Config} from './types.js';
16
16
  import {Principal} from '@dfinity/principal';
17
+ import {downloadPackageFiles} from './helpers/download-package-files.js';
17
18
 
18
19
 
19
20
  if (!global.fetch) {
@@ -338,4 +339,6 @@ export async function checkApiCompatibility() {
338
339
  console.log('-'.repeat(50));
339
340
  }
340
341
  return true;
341
- }
342
+ }
343
+
344
+ export {downloadPackageFiles};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "0.33.0",
3
+ "version": "0.34.0-0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/cli.js"