Repository navigation
fix(cli): harden wasm runtime registry lookup - #3193
Conversation
Signed-off-by: Kamat, Trivikram <[email protected]>
📝 WalkthroughWalkthroughAdded Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cli/src/api/create-npm-dirs.ts`:
- Around line 28-35: The code currently hardcodes WASM_RUNTIME_REGISTRY_URL and
always queries the public host; update getLatestWasmRuntimeVersion to derive the
registry base dynamically from the active npm configuration (first check
process.env.NPM_CONFIG_REGISTRY, then fall back to running `npm config get
registry` or equivalent programmatic lookup), construct the package registry URL
for `@napi-rs/wasm-runtime` from that registry base, and only use
'https://registry.npmjs.org/@napi-rs/wasm-runtime' as a last-resort fallback;
keep the existing fetch logic and error handling in getLatestWasmRuntimeVersion
but replace references to the hardcoded WASM_RUNTIME_REGISTRY_URL with the
computed registry-derived URL.
- Around line 60-65: The check for latestVersion is too permissive—inspect
packageMeta['dist-tags']?.latest and ensure it is a non-empty string (e.g.,
typeof latestVersion === 'string' && latestVersion.trim().length > 0); if not,
throw the Error that currently references WASM_RUNTIME_REGISTRY_URL so
downstream code (using latestVersion as a dependency spec) never receives
malformed values. Locate the code around latestVersion and the existing throw
and replace the truthiness check with this explicit string validation.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 16a8f2a2-9c98-4c8f-9521-d27bff051db5
📒 Files selected for processing (1)
cli/src/api/create-npm-dirs.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cli/src/api/__tests__/create-npm-dirs.spec.ts (1)
345-381: Potential resource leak if second server startup fails.If
startRegistryServer()succeeds on line 349 but fails on line 350,registryServerwon't be closed. Consider nesting the server startups or using a cleanup array.♻️ Suggested fix using nested try/finally
const { tmpDir, packageJsonPath } = t.context const registryServer = await startRegistryServer() - const envRegistryServer = await startRegistryServer() - process.env.npm_config_registry = `${envRegistryServer.origin}/env` + try { + const envRegistryServer = await startRegistryServer() - const packageJson = { - name: 'test-wasm-publish-registry', - version: '1.0.0', - publishConfig: { - registry: `${registryServer.origin}/custom`, - }, - napi: { - binaryName: 'test-wasm-publish-registry', - targets: ['wasm32-wasi-preview1-threads'], - }, - } + process.env.npm_config_registry = `${envRegistryServer.origin}/env` - await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) + const packageJson = { + name: 'test-wasm-publish-registry', + version: '1.0.0', + publishConfig: { + registry: `${registryServer.origin}/custom`, + }, + napi: { + binaryName: 'test-wasm-publish-registry', + targets: ['wasm32-wasi-preview1-threads'], + }, + } - try { - await createNpmDirs({ - cwd: tmpDir, - packageJsonPath: 'package.json', - }) + await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2)) - t.deepEqual(registryServer.requests, []) - t.deepEqual(envRegistryServer.requests, ['/env/@napi-rs/wasm-runtime']) + try { + await createNpmDirs({ + cwd: tmpDir, + packageJsonPath: 'package.json', + }) + + t.deepEqual(registryServer.requests, []) + t.deepEqual(envRegistryServer.requests, ['/env/@napi-rs/wasm-runtime']) + } finally { + await envRegistryServer.close() + } } finally { await registryServer.close() - await envRegistryServer.close() }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cli/src/api/__tests__/create-npm-dirs.spec.ts` around lines 345 - 381, The test starts two servers with startRegistryServer() into registryServer and envRegistryServer but if the second call throws the first server is never closed; wrap the second start in a try/finally (or push servers into a cleanup array) so that if startRegistryServer() for envRegistryServer fails you still call await registryServer.close()—apply this change around the startRegistryServer() calls in the test (references: startRegistryServer, registryServer, envRegistryServer, the test callback) to guarantee both servers are always closed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@cli/src/api/__tests__/create-npm-dirs.spec.ts`:
- Around line 345-381: The test starts two servers with startRegistryServer()
into registryServer and envRegistryServer but if the second call throws the
first server is never closed; wrap the second start in a try/finally (or push
servers into a cleanup array) so that if startRegistryServer() for
envRegistryServer fails you still call await registryServer.close()—apply this
change around the startRegistryServer() calls in the test (references:
startRegistryServer, registryServer, envRegistryServer, the test callback) to
guarantee both servers are always closed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 523a7216-febe-42fc-a205-d967b401e3df
📒 Files selected for processing (2)
cli/src/api/__tests__/create-npm-dirs.spec.tscli/src/api/create-npm-dirs.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- cli/src/api/create-npm-dirs.ts
Summary
This hardens the WASM runtime version lookup used by
create-npm-dirs.Previously, the CLI fetched
https://registry.npmjs.org/@napi-rs/wasm-runtimeinline and immediately parsed the response body without checkingres.ok. That made offline and registry-failure cases noisier and harder to diagnose.Changes
response.okbefore parsing registry metadataSummary by CodeRabbit