Skip to content

Commit e90cfe5

Browse files
authored
fix(ckb-debugger): lazy-load WASI module to suppress ExperimentalWarning (#407)
* fix(ckb-debugger): lazy-load WASI module to suppress ExperimentalWarning Convert static import of node:wasi to dynamic import with caching. This prevents the ExperimentalWarning from being emitted when running non-debugger commands like 'offckb accounts' or 'offckb config list'. The WASI module is now only loaded when debugger functionality is actually executed. Fixes #405 * chore: add changeset for WASI lazy-load fix
1 parent 6700502 commit e90cfe5

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

.changeset/lazy-wasi-warning.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@offckb/cli': patch
3+
---
4+
5+
fix(ckb-debugger): lazy-load WASI module to suppress ExperimentalWarning
6+
7+
Convert static import of node:wasi to dynamic import with caching.
8+
This prevents the ExperimentalWarning from being emitted when running
9+
non-debugger commands like 'offckb accounts' or 'offckb config list'.
10+
11+
The WASI module is now only loaded when debugger functionality is
12+
actually executed.
13+
14+
Fixes #405

src/tools/ckb-debugger-wasm.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424

2525
import * as fs from 'node:fs';
26-
import * as wasi from 'node:wasi';
2726
import * as path from 'node:path';
2827

2928
/**
@@ -60,6 +59,7 @@ export class CkbDebuggerWasi {
6059
private env: Record<string, string>;
6160
private captureOutput: boolean;
6261
private wasm: WebAssembly.Module | null = null;
62+
private static wasiModule: typeof import('node:wasi') | null = null;
6363

6464
constructor(options: CkbDebuggerOptions = {}) {
6565
this.wasmPath = options.wasmPath || './ckb-debugger.wasm';
@@ -77,6 +77,17 @@ export class CkbDebuggerWasi {
7777
}
7878
}
7979

80+
private async getWasiModule(): Promise<typeof import('node:wasi')> {
81+
if (!CkbDebuggerWasi.wasiModule) {
82+
try {
83+
CkbDebuggerWasi.wasiModule = await import('node:wasi');
84+
} catch (_error) {
85+
throw new Error('Failed to load WASI module. Node.js >= 20.0.0 is required for WASM debugger support.');
86+
}
87+
}
88+
return CkbDebuggerWasi.wasiModule;
89+
}
90+
8091
private extractFilePathPreopens(args: string[]): Record<string, string> {
8192
const additionalPreopens: Record<string, string> = {};
8293
for (let i = 0; i < args.length - 1; i++) {
@@ -99,6 +110,7 @@ export class CkbDebuggerWasi {
99110

100111
async run(args: string[] = [], preopens: Record<string, string> = {}): Promise<CkbDebuggerResult> {
101112
await this.initialize();
113+
const wasiModule = await this.getWasiModule();
102114

103115
return new Promise((resolve, reject) => {
104116
try {
@@ -136,7 +148,7 @@ export class CkbDebuggerWasi {
136148
// For output capture, we'll handle it differently since WASI expects file descriptors
137149
// We'll use the default stdout/stderr and capture via different means if needed
138150

139-
const wasihost = new wasi.WASI(wasiOptions);
151+
const wasihost = new wasiModule.WASI(wasiOptions);
140152

141153
// Instantiate the WebAssembly module
142154
WebAssembly.instantiate(this.wasm!, wasihost.getImportObject() as WebAssembly.Imports)

0 commit comments

Comments
 (0)