ic-mops 0.38.1 → 0.38.2

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.
@@ -3,7 +3,7 @@ import {unzipSync} from 'node:zlib';
3
3
  import {chmodSync} from 'node:fs';
4
4
  import fs from 'fs-extra';
5
5
  import decompress from 'decompress';
6
- import decompressTarxz from 'decomp-tarxz';
6
+ // import decompressTarxz from 'decomp-tarxz';
7
7
  import {deleteSync} from 'del';
8
8
  import {Octokit} from 'octokit';
9
9
  import tar from 'tar';
@@ -30,8 +30,9 @@ export let downloadAndExtract = async (url: string, dest: string) => {
30
30
  fs.mkdirSync(dest, {recursive: true});
31
31
 
32
32
  if (archive.endsWith('.xz')) {
33
+ let decompressTarxz = await import('decomp-tarxz');
33
34
  await decompress(archive, tmpDir, {
34
- plugins: [decompressTarxz()],
35
+ plugins: [decompressTarxz.default()],
35
36
  }).catch(() => {
36
37
  deleteSync([tmpDir]);
37
38
  });
@@ -0,0 +1,36 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ import { ChildProcess, ChildProcessWithoutNullStreams } from 'node:child_process';
3
+ import { PocketIc } from 'pic-ic';
4
+ export declare class Replica {
5
+ type: 'dfx' | 'pocket-ic';
6
+ cwd: string;
7
+ verbose: boolean;
8
+ canisters: Record<string, {
9
+ cwd: string;
10
+ canisterId: string;
11
+ actor: any;
12
+ }>;
13
+ pocketIc?: PocketIc;
14
+ process?: ChildProcess;
15
+ constructor(type: 'dfx' | 'pocket-ic', cwd: string, verbose?: boolean);
16
+ start(): Promise<ChildProcessWithoutNullStreams>;
17
+ stop(): Promise<void>;
18
+ deploy(name: string, wasm: string, cwd?: string): Promise<void>;
19
+ getActor(name: string): unknown;
20
+ getCanisterId(name: string): string;
21
+ dfxJson(canisterName: string): {
22
+ version: number;
23
+ canisters: Record<string, any>;
24
+ defaults: {
25
+ build: {
26
+ packtool: string;
27
+ };
28
+ };
29
+ networks: {
30
+ local: {
31
+ type: string;
32
+ bind: string;
33
+ };
34
+ };
35
+ };
36
+ }
@@ -0,0 +1,103 @@
1
+ import { execSync, spawn } from 'node:child_process';
2
+ import path from 'node:path';
3
+ import fs from 'node:fs';
4
+ import { execaCommand } from 'execa';
5
+ import { PocketIc } from 'pic-ic';
6
+ import { readConfig } from '../mops.js';
7
+ import { createActor, idlFactory } from '../declarations/bench/index.js';
8
+ import { toolchain } from './toolchain/index.js';
9
+ export class Replica {
10
+ constructor(type, cwd, verbose = false) {
11
+ this.verbose = false;
12
+ this.canisters = {};
13
+ this.type = type;
14
+ this.verbose = verbose;
15
+ this.cwd = cwd;
16
+ }
17
+ async start() {
18
+ console.log(`Starting ${this.type} replica...`);
19
+ if (this.type == 'dfx') {
20
+ await this.stop();
21
+ fs.writeFileSync(path.join(this.cwd, 'dfx.json'), JSON.stringify(this.dfxJson(''), null, 2));
22
+ // this.process = spawn('dfx start --background --clean --artificial-delay 0' + (this.verbose ? '' : ' -qqqq'), {cwd: this.cwd, stdio: ['inherit', this.verbose ? 'inherit' : 'ignore', 'inherit']});
23
+ this.process = spawn('dfx', ['start', '--background', '--clean', '--artificial-delay=0', this.verbose ? '' : ' -qqqq'].filter(x => x), { cwd: this.cwd });
24
+ }
25
+ else {
26
+ let pocketIcBin = await toolchain.bin('pocket-ic');
27
+ let config = readConfig();
28
+ if (config.toolchain?.['pocket-ic'] !== '1.0.0') {
29
+ console.error('Currently only pocket-ic 1.0.0 is supported');
30
+ process.exit(1);
31
+ }
32
+ this.pocketIc = await PocketIc.create(pocketIcBin);
33
+ // @ts-ignore
34
+ this.process = this.pocketIc.server.serverProcess;
35
+ }
36
+ await new Promise((resolve, reject) => {
37
+ console.log(1);
38
+ this.process?.on('data', (data) => {
39
+ console.log(111, data);
40
+ });
41
+ });
42
+ return this.process;
43
+ }
44
+ async stop() {
45
+ if (this.type == 'dfx') {
46
+ execSync('dfx stop' + (this.verbose ? '' : ' -qqqq'), { cwd: this.cwd, stdio: ['pipe', this.verbose ? 'inherit' : 'ignore', 'pipe'] });
47
+ }
48
+ else if (this.pocketIc) {
49
+ await this.pocketIc.tearDown();
50
+ }
51
+ }
52
+ async deploy(name, wasm, cwd = process.cwd()) {
53
+ if (this.type === 'dfx') {
54
+ await execaCommand(`dfx deploy ${name} --mode reinstall --yes --identity anonymous`, { cwd, stdio: this.verbose ? 'pipe' : ['pipe', 'ignore', 'pipe'] });
55
+ let canisterId = execSync(`dfx canister id ${name}`, { cwd }).toString().trim();
56
+ let actor = await createActor(canisterId, {
57
+ agentOptions: {
58
+ host: 'http://127.0.0.1:4944',
59
+ },
60
+ });
61
+ this.canisters[name] = { cwd, canisterId, actor };
62
+ }
63
+ else if (this.pocketIc) {
64
+ let { canisterId, actor } = await this.pocketIc.setupCanister(idlFactory, wasm);
65
+ this.canisters[name] = {
66
+ cwd,
67
+ canisterId: canisterId.toText(),
68
+ actor,
69
+ };
70
+ }
71
+ }
72
+ getActor(name) {
73
+ return this.canisters[name]?.actor;
74
+ }
75
+ getCanisterId(name) {
76
+ return this.canisters[name]?.canisterId || '';
77
+ }
78
+ dfxJson(canisterName) {
79
+ let canisters = {};
80
+ if (canisterName) {
81
+ canisters[canisterName] = {
82
+ type: 'custom',
83
+ wasm: 'canister.wasm',
84
+ candid: 'canister.did',
85
+ };
86
+ }
87
+ return {
88
+ version: 1,
89
+ canisters,
90
+ defaults: {
91
+ build: {
92
+ packtool: 'mops sources',
93
+ },
94
+ },
95
+ networks: {
96
+ local: {
97
+ type: 'ephemeral',
98
+ bind: '127.0.0.1:4944',
99
+ },
100
+ },
101
+ };
102
+ }
103
+ }
@@ -3,7 +3,7 @@ import { unzipSync } from 'node:zlib';
3
3
  import { chmodSync } from 'node:fs';
4
4
  import fs from 'fs-extra';
5
5
  import decompress from 'decompress';
6
- import decompressTarxz from 'decomp-tarxz';
6
+ // import decompressTarxz from 'decomp-tarxz';
7
7
  import { deleteSync } from 'del';
8
8
  import { Octokit } from 'octokit';
9
9
  import tar from 'tar';
@@ -22,8 +22,9 @@ export let downloadAndExtract = async (url, dest) => {
22
22
  fs.writeFileSync(archive, buffer);
23
23
  fs.mkdirSync(dest, { recursive: true });
24
24
  if (archive.endsWith('.xz')) {
25
+ let decompressTarxz = await import('decomp-tarxz');
25
26
  await decompress(archive, tmpDir, {
26
- plugins: [decompressTarxz()],
27
+ plugins: [decompressTarxz.default()],
27
28
  }).catch(() => {
28
29
  deleteSync([tmpDir]);
29
30
  });
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "0.38.1",
3
+ "version": "0.38.2",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/bin/mops.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "0.38.1",
3
+ "version": "0.38.2",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/bin/mops.js",