@metamask-previews/wallet-cli 0.0.0-preview-e82188855 → 0.0.0-preview-ef063ff2d
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/CHANGELOG.md +3 -1
- package/README.md +16 -2
- package/dist/commands/daemon/call.cjs +3 -7
- package/dist/commands/daemon/call.cjs.map +1 -1
- package/dist/commands/daemon/call.d.cts.map +1 -1
- package/dist/commands/daemon/call.d.mts.map +1 -1
- package/dist/commands/daemon/call.mjs +4 -8
- package/dist/commands/daemon/call.mjs.map +1 -1
- package/dist/commands/daemon/list.cjs +48 -0
- package/dist/commands/daemon/list.cjs.map +1 -0
- package/dist/commands/daemon/list.d.cts +7 -0
- package/dist/commands/daemon/list.d.cts.map +1 -0
- package/dist/commands/daemon/list.d.mts +7 -0
- package/dist/commands/daemon/list.d.mts.map +1 -0
- package/dist/commands/daemon/list.mjs +46 -0
- package/dist/commands/daemon/list.mjs.map +1 -0
- package/dist/daemon/daemon-entry.cjs +4 -0
- package/dist/daemon/daemon-entry.cjs.map +1 -1
- package/dist/daemon/daemon-entry.mjs +4 -0
- package/dist/daemon/daemon-entry.mjs.map +1 -1
- package/dist/daemon/daemon-spawn.cjs +46 -25
- package/dist/daemon/daemon-spawn.cjs.map +1 -1
- package/dist/daemon/daemon-spawn.d.cts.map +1 -1
- package/dist/daemon/daemon-spawn.d.mts.map +1 -1
- package/dist/daemon/daemon-spawn.mjs +47 -26
- package/dist/daemon/daemon-spawn.mjs.map +1 -1
- package/dist/daemon/socket-line.cjs +8 -4
- package/dist/daemon/socket-line.cjs.map +1 -1
- package/dist/daemon/socket-line.d.cts.map +1 -1
- package/dist/daemon/socket-line.d.mts.map +1 -1
- package/dist/daemon/socket-line.mjs +8 -4
- package/dist/daemon/socket-line.mjs.map +1 -1
- package/dist/daemon/stop-daemon.cjs +23 -9
- package/dist/daemon/stop-daemon.cjs.map +1 -1
- package/dist/daemon/stop-daemon.d.cts +17 -7
- package/dist/daemon/stop-daemon.d.cts.map +1 -1
- package/dist/daemon/stop-daemon.d.mts +17 -7
- package/dist/daemon/stop-daemon.d.mts.map +1 -1
- package/dist/daemon/stop-daemon.mjs +23 -9
- package/dist/daemon/stop-daemon.mjs.map +1 -1
- package/dist/daemon/utils.cjs +52 -1
- package/dist/daemon/utils.cjs.map +1 -1
- package/dist/daemon/utils.d.cts +28 -0
- package/dist/daemon/utils.d.cts.map +1 -1
- package/dist/daemon/utils.d.mts +28 -0
- package/dist/daemon/utils.d.mts.map +1 -1
- package/dist/daemon/utils.mjs +48 -0
- package/dist/daemon/utils.mjs.map +1 -1
- package/package.json +3 -2
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Stop the daemon
|
|
3
|
-
* the socket is unresponsive, and escalates to SIGKILL if SIGTERM is ignored.
|
|
2
|
+
* Stop the daemon, preferring a graceful shutdown.
|
|
4
3
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Resolution order when a live daemon is present:
|
|
5
|
+
* 1. If the socket is responsive, request a graceful `shutdown` over it.
|
|
6
|
+
* 2. If the recorded PID is still alive, escalate to SIGTERM.
|
|
7
|
+
* 3. ...then SIGKILL.
|
|
8
|
+
*
|
|
9
|
+
* Signals (steps 2-3) are only ever sent against a PID that is observed alive.
|
|
10
|
+
* The socket-absent + alive-PID branch trades a small risk of signalling a
|
|
8
11
|
* recycled PID for the larger risk of leaving an orphan daemon holding the
|
|
9
|
-
* SQLite database — which `daemon purge` would otherwise wipe out from
|
|
10
|
-
*
|
|
12
|
+
* SQLite database — which `daemon purge` would otherwise wipe out from under
|
|
13
|
+
* it.
|
|
14
|
+
*
|
|
15
|
+
* When the socket is NOT responsive AND the recorded PID is dead (or there is
|
|
16
|
+
* no PID file), there is no live daemon: a lingering socket or PID file is
|
|
17
|
+
* stale leftovers from a daemon that already exited — typically one that
|
|
18
|
+
* crashed without running its own cleanup. Those files are removed and the
|
|
19
|
+
* stop is reported as successful, rather than failing on a daemon that is
|
|
20
|
+
* already gone.
|
|
11
21
|
*
|
|
12
22
|
* @param socketPath - The daemon socket path.
|
|
13
23
|
* @param pidPath - The daemon PID file path.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stop-daemon.d.mts","sourceRoot":"","sources":["../../src/daemon/stop-daemon.ts"],"names":[],"mappings":"AAKA
|
|
1
|
+
{"version":3,"file":"stop-daemon.d.mts","sourceRoot":"","sources":["../../src/daemon/stop-daemon.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,UAAU,CAC9B,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,GAC9B,OAAO,CAAC,OAAO,CAAC,CAiFlB"}
|
|
@@ -2,15 +2,25 @@ import { rm } from "node:fs/promises";
|
|
|
2
2
|
import { pingDaemon, sendCommand } from "./daemon-client.mjs";
|
|
3
3
|
import { isProcessAlive, readPidFile, sendSignal, waitFor } from "./utils.mjs";
|
|
4
4
|
/**
|
|
5
|
-
* Stop the daemon
|
|
6
|
-
* the socket is unresponsive, and escalates to SIGKILL if SIGTERM is ignored.
|
|
5
|
+
* Stop the daemon, preferring a graceful shutdown.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* Resolution order when a live daemon is present:
|
|
8
|
+
* 1. If the socket is responsive, request a graceful `shutdown` over it.
|
|
9
|
+
* 2. If the recorded PID is still alive, escalate to SIGTERM.
|
|
10
|
+
* 3. ...then SIGKILL.
|
|
11
|
+
*
|
|
12
|
+
* Signals (steps 2-3) are only ever sent against a PID that is observed alive.
|
|
13
|
+
* The socket-absent + alive-PID branch trades a small risk of signalling a
|
|
11
14
|
* recycled PID for the larger risk of leaving an orphan daemon holding the
|
|
12
|
-
* SQLite database — which `daemon purge` would otherwise wipe out from
|
|
13
|
-
*
|
|
15
|
+
* SQLite database — which `daemon purge` would otherwise wipe out from under
|
|
16
|
+
* it.
|
|
17
|
+
*
|
|
18
|
+
* When the socket is NOT responsive AND the recorded PID is dead (or there is
|
|
19
|
+
* no PID file), there is no live daemon: a lingering socket or PID file is
|
|
20
|
+
* stale leftovers from a daemon that already exited — typically one that
|
|
21
|
+
* crashed without running its own cleanup. Those files are removed and the
|
|
22
|
+
* stop is reported as successful, rather than failing on a daemon that is
|
|
23
|
+
* already gone.
|
|
14
24
|
*
|
|
15
25
|
* @param socketPath - The daemon socket path.
|
|
16
26
|
* @param pidPath - The daemon PID file path.
|
|
@@ -22,9 +32,13 @@ export async function stopDaemon(socketPath, pidPath, log) {
|
|
|
22
32
|
const ping = await pingDaemon(socketPath);
|
|
23
33
|
const socketObserved = ping.status === 'responsive' || ping.status === 'unreachable';
|
|
24
34
|
const processAlive = pid !== undefined && isProcessAlive(pid);
|
|
25
|
-
|
|
26
|
-
|
|
35
|
+
// Only `absent` and `refused` prove no live daemon; `permission`/`timeout`/
|
|
36
|
+
// `protocol` may be a wedged or foreign daemon, so those fall through.
|
|
37
|
+
const socketProvenGone = ping.status === 'absent' ||
|
|
38
|
+
(ping.status === 'unreachable' && ping.reason === 'refused');
|
|
39
|
+
if (socketProvenGone && !processAlive) {
|
|
27
40
|
await cleanupFile(pidPath, 'PID file', log);
|
|
41
|
+
await cleanupFile(socketPath, 'socket file', log);
|
|
28
42
|
return true;
|
|
29
43
|
}
|
|
30
44
|
log?.('Stopping daemon...');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"stop-daemon.mjs","sourceRoot":"","sources":["../../src/daemon/stop-daemon.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,yBAAyB;AAEtC,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,4BAAwB;AAC1D,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,UAAU,EAAE,OAAO,EAAE,oBAAgB;AA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,CAAC;YACH,IAAI,UAAU,CAAC,GAAG,EAAE,SAAS,CAAC,EAAE,CAAC;gBAC/B,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,IAAK,CAAC,CAAC;YAC7D,CAAC;iBAAM,CAAC;gBACN,OAAO,GAAG,IAAI,CAAC,CAAC,gCAAgC;YAClD,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,GAAG,EAAE,CAAC,mBAAmB,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QAC5C,CAAC;IACH,CAAC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,MAAM,WAAW,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,CAAC,CAAC;QAC5C,MAAM,WAAW,CAAC,UAAU,EAAE,aAAa,EAAE,GAAG,CAAC,CAAC;QAClD,GAAG,EAAE,CAAC,iBAAiB,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;;;;GAOG;AACH,KAAK,UAAU,WAAW,CACxB,IAAY,EACZ,KAAa,EACb,GAA4C;IAE5C,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;QACvD,GAAG,EAAE,CAAC,oBAAoB,KAAK,KAAK,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { rm } from 'node:fs/promises';\n\nimport { pingDaemon, sendCommand } from './daemon-client';\nimport { isProcessAlive, readPidFile, sendSignal, waitFor } from './utils';\n\n/**\n * Stop the daemon, preferring a graceful shutdown.\n *\n * Resolution order when a live daemon is present:\n * 1. If the socket is responsive, request a graceful `shutdown` over it.\n * 2. If the recorded PID is still alive, escalate to SIGTERM.\n * 3. ...then SIGKILL.\n *\n * Signals (steps 2-3) are only ever sent against a PID that is observed alive.\n * The socket-absent + alive-PID branch trades a small risk of signalling a\n * recycled PID for the larger risk of leaving an orphan daemon holding the\n * SQLite database — which `daemon purge` would otherwise wipe out from under\n * it.\n *\n * When the socket is NOT responsive AND the recorded PID is dead (or there is\n * no PID file), there is no live daemon: a lingering socket or PID file is\n * stale leftovers from a daemon that already exited — typically one that\n * crashed without running its own cleanup. Those files are removed and the\n * stop is reported as successful, rather than failing on a daemon that is\n * already gone.\n *\n * @param socketPath - The daemon socket path.\n * @param pidPath - The daemon PID file path.\n * @param log - Optional logging function for status messages.\n * @returns True if the daemon was stopped (or was not running).\n */\nexport async function stopDaemon(\n socketPath: string,\n pidPath: string,\n log?: (message: string) => void,\n): Promise<boolean> {\n const pid = await readPidFile(pidPath);\n const ping = await pingDaemon(socketPath);\n const socketObserved =\n ping.status === 'responsive' || ping.status === 'unreachable';\n const processAlive = pid !== undefined && isProcessAlive(pid);\n\n // Only `absent` and `refused` prove no live daemon; `permission`/`timeout`/\n // `protocol` may be a wedged or foreign daemon, so those fall through.\n const socketProvenGone =\n ping.status === 'absent' ||\n (ping.status === 'unreachable' && ping.reason === 'refused');\n\n if (socketProvenGone && !processAlive) {\n await cleanupFile(pidPath, 'PID file', log);\n await cleanupFile(socketPath, 'socket file', log);\n return true;\n }\n\n log?.('Stopping daemon...');\n\n let stopped = false;\n\n // Strategy 1: Graceful socket-based shutdown.\n if (ping.status === 'responsive') {\n try {\n await sendCommand({ socketPath, method: 'shutdown' });\n } catch (error) {\n log?.(`Graceful shutdown request failed: ${String(error)}`);\n }\n // A quiet socket does not prove the process exited; require the recorded\n // pid to be gone too, so a daemon that outlived its socket falls through to\n // SIGTERM/SIGKILL rather than being orphaned.\n stopped = await waitFor(\n async () =>\n (await pingDaemon(socketPath)).status !== 'responsive' &&\n (pid === undefined || !isProcessAlive(pid)),\n 5_000,\n );\n }\n\n // Strategy 2: SIGTERM. Signal when either the socket was observed or the\n // recorded PID is alive; the absent+alive case typically means someone\n // removed the socket from under a live daemon.\n if (!stopped && processAlive && pid !== undefined) {\n if (!socketObserved) {\n log?.(\n `Socket at ${socketPath} is absent but recorded pid ${pid} is alive; signalling anyway.`,\n );\n }\n try {\n if (sendSignal(pid, 'SIGTERM')) {\n stopped = await waitFor(() => !isProcessAlive(pid), 5_000);\n } else {\n stopped = true; // Process already gone (ESRCH).\n }\n } catch (error) {\n log?.(`SIGTERM failed: ${String(error)}`);\n }\n }\n\n // Strategy 3: SIGKILL.\n if (!stopped && processAlive && pid !== undefined) {\n try {\n if (sendSignal(pid, 'SIGKILL')) {\n stopped = await waitFor(() => !isProcessAlive(pid), 2_000);\n } else {\n stopped = true; // Process already gone (ESRCH).\n }\n } catch (error) {\n log?.(`SIGKILL failed: ${String(error)}`);\n }\n }\n\n if (stopped) {\n await cleanupFile(pidPath, 'PID file', log);\n await cleanupFile(socketPath, 'socket file', log);\n log?.('Daemon stopped.');\n }\n\n return stopped;\n}\n\n/**\n * Remove a file best-effort, logging any failure rather than letting it\n * propagate. ENOENT is silently ignored via `force: true`.\n *\n * @param path - The file path to remove.\n * @param label - Human-readable label for log messages.\n * @param log - Optional log sink.\n */\nasync function cleanupFile(\n path: string,\n label: string,\n log: ((message: string) => void) | undefined,\n): Promise<void> {\n await rm(path, { force: true }).catch((error: unknown) => {\n log?.(`Failed to remove ${label}: ${String(error)}`);\n });\n}\n"]}
|
package/dist/daemon/utils.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.waitFor = exports.sendSignal = exports.isProcessAlive = exports.readPidFile = exports.isErrorWithCode = void 0;
|
|
3
|
+
exports.waitFor = exports.sendSignal = exports.isProcessAlive = exports.readPidFile = exports.isStringArray = exports.formatJsonRpcError = exports.makeDaemonConnectionError = exports.isErrorWithCode = void 0;
|
|
4
4
|
const utils_1 = require("@metamask/utils");
|
|
5
5
|
const promises_1 = require("node:fs/promises");
|
|
6
6
|
/**
|
|
@@ -14,6 +14,57 @@ function isErrorWithCode(error, code) {
|
|
|
14
14
|
return (0, utils_1.isErrorWithCode)(error) && error.code === code;
|
|
15
15
|
}
|
|
16
16
|
exports.isErrorWithCode = isErrorWithCode;
|
|
17
|
+
/**
|
|
18
|
+
* Turn an error thrown while contacting the daemon socket into a user-facing
|
|
19
|
+
* message for `Command.error`, so every command reports the same failure the
|
|
20
|
+
* same way. Distinguishes a stopped daemon (`ENOENT`/`ECONNREFUSED`), a
|
|
21
|
+
* connection dropped mid-request (`ECONNRESET`), and a permission problem
|
|
22
|
+
* (`EACCES`/`EPERM`), falling back to the raw error message.
|
|
23
|
+
*
|
|
24
|
+
* @param error - The value thrown while contacting the daemon.
|
|
25
|
+
* @returns A human-readable explanation of the failure.
|
|
26
|
+
*/
|
|
27
|
+
function makeDaemonConnectionError(error) {
|
|
28
|
+
if (isErrorWithCode(error, 'ENOENT') ||
|
|
29
|
+
isErrorWithCode(error, 'ECONNREFUSED')) {
|
|
30
|
+
return 'Daemon is not running. Start it with `mm daemon start`.';
|
|
31
|
+
}
|
|
32
|
+
// A reset drops an already-established connection, so the daemon was running
|
|
33
|
+
// — most likely it crashed mid-request. Don't tell the user to start it.
|
|
34
|
+
if (isErrorWithCode(error, 'ECONNRESET')) {
|
|
35
|
+
return ('Lost the connection to the daemon; it may have crashed while ' +
|
|
36
|
+
'handling the request. Check `mm daemon status` and the daemon log.');
|
|
37
|
+
}
|
|
38
|
+
if (isErrorWithCode(error, 'EACCES') || isErrorWithCode(error, 'EPERM')) {
|
|
39
|
+
return ('Cannot connect to the daemon socket: permission denied. ' +
|
|
40
|
+
'The socket may be owned by another user, or MM_DATA_DIR ' +
|
|
41
|
+
'may point to a directory you cannot access.');
|
|
42
|
+
}
|
|
43
|
+
return error instanceof Error ? error.message : String(error);
|
|
44
|
+
}
|
|
45
|
+
exports.makeDaemonConnectionError = makeDaemonConnectionError;
|
|
46
|
+
/**
|
|
47
|
+
* Format the error of a JSON-RPC failure response into a user-facing message
|
|
48
|
+
* for `Command.error`, so every command reports RPC failures the same way.
|
|
49
|
+
*
|
|
50
|
+
* @param error - The `error` field of a JSON-RPC failure response.
|
|
51
|
+
* @returns The error message annotated with its numeric code.
|
|
52
|
+
*/
|
|
53
|
+
function formatJsonRpcError(error) {
|
|
54
|
+
return `${error.message} (code ${String(error.code)})`;
|
|
55
|
+
}
|
|
56
|
+
exports.formatJsonRpcError = formatJsonRpcError;
|
|
57
|
+
/**
|
|
58
|
+
* Check whether a value is an array of strings. Used to validate untyped RPC
|
|
59
|
+
* results (e.g. the daemon's `listActions`) before treating them as `string[]`.
|
|
60
|
+
*
|
|
61
|
+
* @param value - The value to check.
|
|
62
|
+
* @returns True if the value is an array whose every element is a string.
|
|
63
|
+
*/
|
|
64
|
+
function isStringArray(value) {
|
|
65
|
+
return (Array.isArray(value) && value.every((item) => typeof item === 'string'));
|
|
66
|
+
}
|
|
67
|
+
exports.isStringArray = isStringArray;
|
|
17
68
|
/**
|
|
18
69
|
* Read a PID from a file. The file may contain just the PID, or the PID on
|
|
19
70
|
* the first line followed by additional metadata (e.g. start time written by
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":";;;AACA,2CAAkE;AAClE,+CAA4C;AAE5C;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,KAAc,EAAE,IAAY;IAC1D,OAAO,IAAA,uBAAY,EAAC,KAAK,CAAC,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;AACpD,CAAC;AAFD,0CAEC;AAED;;;;;;;;;GASG;AACH,SAAgB,yBAAyB,CAAC,KAAc;IACtD,IACE,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC;QAChC,eAAe,CAAC,KAAK,EAAE,c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,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;IACxC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAC;QAC7B,IAAI,MAAM,KAAK,EAAE,EAAE,CAAC;YAClB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAZD,0BAYC","sourcesContent":["import type { JsonRpcError } from '@metamask/utils';\nimport { isErrorWithCode as hasErrorCode } from '@metamask/utils';\nimport { readFile } from 'node:fs/promises';\n\n/**\n * Check whether an unknown error is a Node.js system error with the given code.\n *\n * @param error - The error to check.\n * @param code - The expected error code (e.g. 'ENOENT', 'EPERM').\n * @returns True if the error matches the code.\n */\nexport function isErrorWithCode(error: unknown, code: string): boolean {\n return hasErrorCode(error) && error.code === code;\n}\n\n/**\n * Turn an error thrown while contacting the daemon socket into a user-facing\n * message for `Command.error`, so every command reports the same failure the\n * same way. Distinguishes a stopped daemon (`ENOENT`/`ECONNREFUSED`), a\n * connection dropped mid-request (`ECONNRESET`), and a permission problem\n * (`EACCES`/`EPERM`), falling back to the raw error message.\n *\n * @param error - The value thrown while contacting the daemon.\n * @returns A human-readable explanation of the failure.\n */\nexport function makeDaemonConnectionError(error: unknown): string {\n if (\n isErrorWithCode(error, 'ENOENT') ||\n isErrorWithCode(error, 'ECONNREFUSED')\n ) {\n return 'Daemon is not running. Start it with `mm daemon start`.';\n }\n // A reset drops an already-established connection, so the daemon was running\n // — most likely it crashed mid-request. Don't tell the user to start it.\n if (isErrorWithCode(error, 'ECONNRESET')) {\n return (\n 'Lost the connection to the daemon; it may have crashed while ' +\n 'handling the request. Check `mm daemon status` and the daemon log.'\n );\n }\n if (isErrorWithCode(error, 'EACCES') || isErrorWithCode(error, 'EPERM')) {\n return (\n 'Cannot connect to the daemon socket: permission denied. ' +\n 'The socket may be owned by another user, or MM_DATA_DIR ' +\n 'may point to a directory you cannot access.'\n );\n }\n return error instanceof Error ? error.message : String(error);\n}\n\n/**\n * Format the error of a JSON-RPC failure response into a user-facing message\n * for `Command.error`, so every command reports RPC failures the same way.\n *\n * @param error - The `error` field of a JSON-RPC failure response.\n * @returns The error message annotated with its numeric code.\n */\nexport function formatJsonRpcError(error: JsonRpcError): string {\n return `${error.message} (code ${String(error.code)})`;\n}\n\n/**\n * Check whether a value is an array of strings. Used to validate untyped RPC\n * results (e.g. the daemon's `listActions`) before treating them as `string[]`.\n *\n * @param value - The value to check.\n * @returns True if the value is an array whose every element is a string.\n */\nexport function isStringArray(value: unknown): value is string[] {\n return (\n Array.isArray(value) && value.every((item) => typeof item === 'string')\n );\n}\n\n/**\n * Read a PID from a file. The file may contain just the PID, or the PID on\n * the first line followed by additional metadata (e.g. start time written by\n * the daemon).\n *\n * @param pidPath - The PID file path.\n * @returns The PID, or undefined if the file is missing or its first line is\n * not a positive integer.\n */\nexport async function readPidFile(\n pidPath: string,\n): Promise<number | undefined> {\n let contents: string;\n try {\n contents = await readFile(pidPath, 'utf-8');\n } catch (error: unknown) {\n if (isErrorWithCode(error, 'ENOENT')) {\n return undefined;\n }\n throw error;\n }\n // String.prototype.split always returns at least one element, so [0] is safe.\n const pid = Number(contents.split('\\n')[0].trim());\n return Number.isInteger(pid) && pid > 0 ? pid : undefined;\n}\n\n/**\n * Check whether a process is alive by sending signal 0.\n *\n * Treats `ESRCH` as \"process is gone\", `EPERM` as \"process exists but we\n * cannot signal it\" (still alive from our perspective), and rethrows\n * anything else so the caller can surface unexpected failures rather than\n * silently assuming the process is dead.\n *\n * @param pid - The process ID to check.\n * @returns True if the process exists.\n */\nexport function isProcessAlive(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch (error: unknown) {\n if (isErrorWithCode(error, 'ESRCH')) {\n return false;\n }\n if (isErrorWithCode(error, 'EPERM')) {\n return true;\n }\n throw error;\n }\n}\n\n/**\n * Send a signal to a process. Returns true if the signal was sent, false if\n * the process does not exist (ESRCH). Re-throws on permission errors and\n * other failures.\n *\n * @param pid - The process ID.\n * @param signal - The signal to send.\n * @returns True if the signal was delivered, false if the process is gone.\n */\nexport function sendSignal(pid: number, signal: NodeJS.Signals): boolean {\n try {\n process.kill(pid, signal);\n return true;\n } catch (error: unknown) {\n if (isErrorWithCode(error, 'ESRCH')) {\n return false;\n }\n throw error;\n }\n}\n\n/**\n * Poll until a condition is met or the timeout elapses.\n *\n * @param check - A function that returns true when the condition is met.\n * @param timeoutMs - Maximum time to wait in milliseconds.\n * @returns True if the condition was met, false on timeout.\n */\nexport async function waitFor(\n check: () => boolean | Promise<boolean>,\n timeoutMs: number,\n): Promise<boolean> {\n const deadline = Date.now() + timeoutMs;\n while (Date.now() < deadline) {\n if (await check()) {\n return true;\n }\n await new Promise((resolve) => setTimeout(resolve, 250));\n }\n return false;\n}\n"]}
|
package/dist/daemon/utils.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import type { JsonRpcError } from "@metamask/utils";
|
|
2
3
|
/**
|
|
3
4
|
* Check whether an unknown error is a Node.js system error with the given code.
|
|
4
5
|
*
|
|
@@ -7,6 +8,33 @@
|
|
|
7
8
|
* @returns True if the error matches the code.
|
|
8
9
|
*/
|
|
9
10
|
export declare function isErrorWithCode(error: unknown, code: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Turn an error thrown while contacting the daemon socket into a user-facing
|
|
13
|
+
* message for `Command.error`, so every command reports the same failure the
|
|
14
|
+
* same way. Distinguishes a stopped daemon (`ENOENT`/`ECONNREFUSED`), a
|
|
15
|
+
* connection dropped mid-request (`ECONNRESET`), and a permission problem
|
|
16
|
+
* (`EACCES`/`EPERM`), falling back to the raw error message.
|
|
17
|
+
*
|
|
18
|
+
* @param error - The value thrown while contacting the daemon.
|
|
19
|
+
* @returns A human-readable explanation of the failure.
|
|
20
|
+
*/
|
|
21
|
+
export declare function makeDaemonConnectionError(error: unknown): string;
|
|
22
|
+
/**
|
|
23
|
+
* Format the error of a JSON-RPC failure response into a user-facing message
|
|
24
|
+
* for `Command.error`, so every command reports RPC failures the same way.
|
|
25
|
+
*
|
|
26
|
+
* @param error - The `error` field of a JSON-RPC failure response.
|
|
27
|
+
* @returns The error message annotated with its numeric code.
|
|
28
|
+
*/
|
|
29
|
+
export declare function formatJsonRpcError(error: JsonRpcError): string;
|
|
30
|
+
/**
|
|
31
|
+
* Check whether a value is an array of strings. Used to validate untyped RPC
|
|
32
|
+
* results (e.g. the daemon's `listActions`) before treating them as `string[]`.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to check.
|
|
35
|
+
* @returns True if the value is an array whose every element is a string.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isStringArray(value: unknown): value is string[];
|
|
10
38
|
/**
|
|
11
39
|
* Read a PID from a file. The file may contain just the PID, or the PID on
|
|
12
40
|
* the first line followed by additional metadata (e.g. start time written by
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,wBAAwB;AAIpD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAErE;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAuBhE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAE9D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,EAAE,CAI/D;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAa7B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAanD;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,OAAO,CAUvE;AAED;;;;;;GAMG;AACH,wBAAsB,OAAO,CAC3B,KAAK,EAAE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,EACvC,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,OAAO,CAAC,CASlB"}
|
package/dist/daemon/utils.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import type { JsonRpcError } from "@metamask/utils";
|
|
2
3
|
/**
|
|
3
4
|
* Check whether an unknown error is a Node.js system error with the given code.
|
|
4
5
|
*
|
|
@@ -7,6 +8,33 @@
|
|
|
7
8
|
* @returns True if the error matches the code.
|
|
8
9
|
*/
|
|
9
10
|
export declare function isErrorWithCode(error: unknown, code: string): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Turn an error thrown while contacting the daemon socket into a user-facing
|
|
13
|
+
* message for `Command.error`, so every command reports the same failure the
|
|
14
|
+
* same way. Distinguishes a stopped daemon (`ENOENT`/`ECONNREFUSED`), a
|
|
15
|
+
* connection dropped mid-request (`ECONNRESET`), and a permission problem
|
|
16
|
+
* (`EACCES`/`EPERM`), falling back to the raw error message.
|
|
17
|
+
*
|
|
18
|
+
* @param error - The value thrown while contacting the daemon.
|
|
19
|
+
* @returns A human-readable explanation of the failure.
|
|
20
|
+
*/
|
|
21
|
+
export declare function makeDaemonConnectionError(error: unknown): string;
|
|
22
|
+
/**
|
|
23
|
+
* Format the error of a JSON-RPC failure response into a user-facing message
|
|
24
|
+
* for `Command.error`, so every command reports RPC failures the same way.
|
|
25
|
+
*
|
|
26
|
+
* @param error - The `error` field of a JSON-RPC failure response.
|
|
27
|
+
* @returns The error message annotated with its numeric code.
|
|
28
|
+
*/
|
|
29
|
+
export declare function formatJsonRpcError(error: JsonRpcError): string;
|
|
30
|
+
/**
|
|
31
|
+
* Check whether a value is an array of strings. Used to validate untyped RPC
|
|
32
|
+
* results (e.g. the daemon's `listActions`) before treating them as `string[]`.
|
|
33
|
+
*
|
|
34
|
+
* @param value - The value to check.
|
|
35
|
+
* @returns True if the value is an array whose every element is a string.
|
|
36
|
+
*/
|
|
37
|
+
export declare function isStringArray(value: unknown): value is string[];
|
|
10
38
|
/**
|
|
11
39
|
* Read a PID from a file. The file may contain just the PID, or the PID on
|
|
12
40
|
* the first line followed by additional metadata (e.g. start time written by
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":";AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,wBAAwB;AAIpD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAErE;AAED;;;;;;;;;GASG;AACH,wBAAgB,yBAAyB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAuBhE;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,GAAG,MAAM,CAE9D;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,EAAE,CAI/D;AAED;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAa7B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAanD;AAED;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,OAAO,CAUvE;AAED;;;;;;GAMG;AACH,wBAAsB,OAAO,CAC3B,KAAK,EAAE,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,EACvC,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,OAAO,CAAC,CASlB"}
|
package/dist/daemon/utils.mjs
CHANGED
|
@@ -10,6 +10,54 @@ import { readFile } from "node:fs/promises";
|
|
|
10
10
|
export function isErrorWithCode(error, code) {
|
|
11
11
|
return hasErrorCode(error) && error.code === code;
|
|
12
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Turn an error thrown while contacting the daemon socket into a user-facing
|
|
15
|
+
* message for `Command.error`, so every command reports the same failure the
|
|
16
|
+
* same way. Distinguishes a stopped daemon (`ENOENT`/`ECONNREFUSED`), a
|
|
17
|
+
* connection dropped mid-request (`ECONNRESET`), and a permission problem
|
|
18
|
+
* (`EACCES`/`EPERM`), falling back to the raw error message.
|
|
19
|
+
*
|
|
20
|
+
* @param error - The value thrown while contacting the daemon.
|
|
21
|
+
* @returns A human-readable explanation of the failure.
|
|
22
|
+
*/
|
|
23
|
+
export function makeDaemonConnectionError(error) {
|
|
24
|
+
if (isErrorWithCode(error, 'ENOENT') ||
|
|
25
|
+
isErrorWithCode(error, 'ECONNREFUSED')) {
|
|
26
|
+
return 'Daemon is not running. Start it with `mm daemon start`.';
|
|
27
|
+
}
|
|
28
|
+
// A reset drops an already-established connection, so the daemon was running
|
|
29
|
+
// — most likely it crashed mid-request. Don't tell the user to start it.
|
|
30
|
+
if (isErrorWithCode(error, 'ECONNRESET')) {
|
|
31
|
+
return ('Lost the connection to the daemon; it may have crashed while ' +
|
|
32
|
+
'handling the request. Check `mm daemon status` and the daemon log.');
|
|
33
|
+
}
|
|
34
|
+
if (isErrorWithCode(error, 'EACCES') || isErrorWithCode(error, 'EPERM')) {
|
|
35
|
+
return ('Cannot connect to the daemon socket: permission denied. ' +
|
|
36
|
+
'The socket may be owned by another user, or MM_DATA_DIR ' +
|
|
37
|
+
'may point to a directory you cannot access.');
|
|
38
|
+
}
|
|
39
|
+
return error instanceof Error ? error.message : String(error);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Format the error of a JSON-RPC failure response into a user-facing message
|
|
43
|
+
* for `Command.error`, so every command reports RPC failures the same way.
|
|
44
|
+
*
|
|
45
|
+
* @param error - The `error` field of a JSON-RPC failure response.
|
|
46
|
+
* @returns The error message annotated with its numeric code.
|
|
47
|
+
*/
|
|
48
|
+
export function formatJsonRpcError(error) {
|
|
49
|
+
return `${error.message} (code ${String(error.code)})`;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Check whether a value is an array of strings. Used to validate untyped RPC
|
|
53
|
+
* results (e.g. the daemon's `listActions`) before treating them as `string[]`.
|
|
54
|
+
*
|
|
55
|
+
* @param value - The value to check.
|
|
56
|
+
* @returns True if the value is an array whose every element is a string.
|
|
57
|
+
*/
|
|
58
|
+
export function isStringArray(value) {
|
|
59
|
+
return (Array.isArray(value) && value.every((item) => typeof item === 'string'));
|
|
60
|
+
}
|
|
13
61
|
/**
|
|
14
62
|
* Read a PID from a file. The file may contain just the PID, or the PID on
|
|
15
63
|
* the first line followed by additional metadata (e.g. start time written by
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../../src/daemon/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,IAAI,YAAY,EAAE,wBAAwB;AAClE,OAAO,EAAE,QAAQ,EAAE,yBAAyB;AAE5C;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B,IAAI,eAAe,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,CAAC;YACpC,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,OAAO,CAC3B,KAAuC,EACvC,SAAiB;IAEjB,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;IACxC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAC;QAC7B,IAAI,MAAM,KAAK,EAAE,EAAE,CAAC;YAClB,OAAO,IAAI,CAAC;QACd,CAAC;QACD,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type { JsonRpcError } from '@metamask/utils';\nimport { isErrorWithCode as hasErrorCode } from '@metamask/utils';\nimport { readFile } from 'node:fs/promises';\n\n/**\n * Check whether an unknown error is a Node.js system error with the given code.\n *\n * @param error - The error to check.\n * @param code - The expected error code (e.g. 'ENOENT', 'EPERM').\n * @returns True if the error matches the code.\n */\nexport function isErrorWithCode(error: unknown, code: string): boolean {\n return hasErrorCode(error) && error.code === code;\n}\n\n/**\n * Turn an error thrown while contacting the daemon socket into a user-facing\n * message for `Command.error`, so every command reports the same failure the\n * same way. Distinguishes a stopped daemon (`ENOENT`/`ECONNREFUSED`), a\n * connection dropped mid-request (`ECONNRESET`), and a permission problem\n * (`EACCES`/`EPERM`), falling back to the raw error message.\n *\n * @param error - The value thrown while contacting the daemon.\n * @returns A human-readable explanation of the failure.\n */\nexport function makeDaemonConnectionError(error: unknown): string {\n if (\n isErrorWithCode(error, 'ENOENT') ||\n isErrorWithCode(error, 'ECONNREFUSED')\n ) {\n return 'Daemon is not running. Start it with `mm daemon start`.';\n }\n // A reset drops an already-established connection, so the daemon was running\n // — most likely it crashed mid-request. Don't tell the user to start it.\n if (isErrorWithCode(error, 'ECONNRESET')) {\n return (\n 'Lost the connection to the daemon; it may have crashed while ' +\n 'handling the request. Check `mm daemon status` and the daemon log.'\n );\n }\n if (isErrorWithCode(error, 'EACCES') || isErrorWithCode(error, 'EPERM')) {\n return (\n 'Cannot connect to the daemon socket: permission denied. ' +\n 'The socket may be owned by another user, or MM_DATA_DIR ' +\n 'may point to a directory you cannot access.'\n );\n }\n return error instanceof Error ? error.message : String(error);\n}\n\n/**\n * Format the error of a JSON-RPC failure response into a user-facing message\n * for `Command.error`, so every command reports RPC failures the same way.\n *\n * @param error - The `error` field of a JSON-RPC failure response.\n * @returns The error message annotated with its numeric code.\n */\nexport function formatJsonRpcError(error: JsonRpcError): string {\n return `${error.message} (code ${String(error.code)})`;\n}\n\n/**\n * Check whether a value is an array of strings. Used to validate untyped RPC\n * results (e.g. the daemon's `listActions`) before treating them as `string[]`.\n *\n * @param value - The value to check.\n * @returns True if the value is an array whose every element is a string.\n */\nexport function isStringArray(value: unknown): value is string[] {\n return (\n Array.isArray(value) && value.every((item) => typeof item === 'string')\n );\n}\n\n/**\n * Read a PID from a file. The file may contain just the PID, or the PID on\n * the first line followed by additional metadata (e.g. start time written by\n * the daemon).\n *\n * @param pidPath - The PID file path.\n * @returns The PID, or undefined if the file is missing or its first line is\n * not a positive integer.\n */\nexport async function readPidFile(\n pidPath: string,\n): Promise<number | undefined> {\n let contents: string;\n try {\n contents = await readFile(pidPath, 'utf-8');\n } catch (error: unknown) {\n if (isErrorWithCode(error, 'ENOENT')) {\n return undefined;\n }\n throw error;\n }\n // String.prototype.split always returns at least one element, so [0] is safe.\n const pid = Number(contents.split('\\n')[0].trim());\n return Number.isInteger(pid) && pid > 0 ? pid : undefined;\n}\n\n/**\n * Check whether a process is alive by sending signal 0.\n *\n * Treats `ESRCH` as \"process is gone\", `EPERM` as \"process exists but we\n * cannot signal it\" (still alive from our perspective), and rethrows\n * anything else so the caller can surface unexpected failures rather than\n * silently assuming the process is dead.\n *\n * @param pid - The process ID to check.\n * @returns True if the process exists.\n */\nexport function isProcessAlive(pid: number): boolean {\n try {\n process.kill(pid, 0);\n return true;\n } catch (error: unknown) {\n if (isErrorWithCode(error, 'ESRCH')) {\n return false;\n }\n if (isErrorWithCode(error, 'EPERM')) {\n return true;\n }\n throw error;\n }\n}\n\n/**\n * Send a signal to a process. Returns true if the signal was sent, false if\n * the process does not exist (ESRCH). Re-throws on permission errors and\n * other failures.\n *\n * @param pid - The process ID.\n * @param signal - The signal to send.\n * @returns True if the signal was delivered, false if the process is gone.\n */\nexport function sendSignal(pid: number, signal: NodeJS.Signals): boolean {\n try {\n process.kill(pid, signal);\n return true;\n } catch (error: unknown) {\n if (isErrorWithCode(error, 'ESRCH')) {\n return false;\n }\n throw error;\n }\n}\n\n/**\n * Poll until a condition is met or the timeout elapses.\n *\n * @param check - A function that returns true when the condition is met.\n * @param timeoutMs - Maximum time to wait in milliseconds.\n * @returns True if the condition was met, false on timeout.\n */\nexport async function waitFor(\n check: () => boolean | Promise<boolean>,\n timeoutMs: number,\n): Promise<boolean> {\n const deadline = Date.now() + timeoutMs;\n while (Date.now() < deadline) {\n if (await check()) {\n return true;\n }\n await new Promise((resolve) => setTimeout(resolve, 250));\n }\n return false;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metamask-previews/wallet-cli",
|
|
3
|
-
"version": "0.0.0-preview-
|
|
3
|
+
"version": "0.0.0-preview-ef063ff2d",
|
|
4
4
|
"description": "The CLI of @metamask/wallet",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ethereum",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"test:prepare": "./scripts/install-binaries.sh",
|
|
40
40
|
"test": "yarn test:prepare && NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
|
|
41
41
|
"test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
|
|
42
|
+
"test:e2e": "yarn test:prepare && NODE_OPTIONS=--experimental-vm-modules jest --config jest.config.e2e.js",
|
|
42
43
|
"test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
|
|
43
44
|
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
|
|
44
45
|
},
|
|
@@ -49,7 +50,7 @@
|
|
|
49
50
|
"@metamask/rpc-errors": "^7.0.2",
|
|
50
51
|
"@metamask/storage-service": "^1.0.2",
|
|
51
52
|
"@metamask/utils": "^11.11.0",
|
|
52
|
-
"@metamask/wallet": "^
|
|
53
|
+
"@metamask/wallet": "^6.0.0",
|
|
53
54
|
"@oclif/core": "^4.10.5",
|
|
54
55
|
"better-sqlite3": "^12.9.0",
|
|
55
56
|
"immer": "^9.0.6"
|