Repository navigation
KTOR-9595 Fix ktor-network compatibility with jsBrowser and wasmJsBrowser - #5657
Conversation
📝 WalkthroughWalkthroughKarma headless launcher adds ChangesTest Infrastructure & Karma Configuration
Node.js Dynamic Module Loading
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
Also set password store to basic
cb82ccb to
cdbd1ec
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@ktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.kt`:
- Around line 22-27: The `@JsFun` snippet uses "await import(...)" in a
non-suspend external function (loadNodeNetModule), which is invalid; change the
JS snippet to return the dynamic import Promise (remove await and return
import(...) or Promise.resolve(null) in the false branch) and make the Kotlin
side suspend-bridge by turning loadNodeNetModule into a suspend function (or
expose a Promise-returning external and add a suspend wrapper) and await the
returned Promise using the Promise→coroutine await pattern (e.g., kotlinx
coroutines Promise.await) so the module is resolved in a proper suspend context.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0b57f4c4-4abc-4c46-8784-0a4eadaf0e47
📒 Files selected for processing (6)
karma/chrome_bin.jskarma/config.jsktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.ktktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.ktktor-network/web/src/io/ktor/network/sockets/SocketEngine.web.ktktor-network/web/src/io/ktor/network/sockets/nodejs/node.net.kt
| "moduleName => ((typeof process !== 'undefined') && process.release && (process.release.name === 'node'))" + | ||
| " ? import(moduleName) : Promise.resolve(null)" |
There was a problem hiding this comment.
Could use optional chaining to clean up these functions a little process?.release?.name == 'node'
There was a problem hiding this comment.
Thanks! I've managed to replace it with (typeof process !== 'undefined') && process.release?.name === 'node', but can't get rid of typeof process !== 'undefined'
UPD: Ooops. JsFun fails to parse optional chaining :(
| val socket = nodeNet.createConnection(CreateConnectionOptions(remoteAddress, socketOptions)) | ||
| SocketContext(socket, remoteAddress, null).initiate(cont) | ||
| ): Socket { | ||
| val nodeNet = loadNodeNet() |
There was a problem hiding this comment.
Can we also use a lazy value here?
There was a problem hiding this comment.
Unfortunately not as I failed to make loadNodeNet non suspending in JS. But nodeNetPromise is lazy, so it'll be evaluated only once.
cdbd1ec to
9a71730
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@ktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.kt`:
- Around line 22-26: The JS interop currently returns a thunk because the JsFun
expression uses "((module) => () => module)(...)" so loadNodeNetModule() yields
a zero-arg function rather than the Node `net` module; update the JsFun to
return the module itself (e.g., change the wrapper to "((module) =>
module)(...)" or otherwise invoke the thunk so loadNodeNetModule() returns the
actual Node `net` object) so the Kotlin code that treats the result as NodeNet
will receive the real module; adjust the annotation on loadNodeNetModule()
accordingly and keep the external signature as returning JsAny?/NodeNet as used
elsewhere.
🪄 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: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 692f6abf-b716-4ae2-a717-0515edcb072c
📒 Files selected for processing (5)
karma/config.jsktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.ktktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.ktktor-network/web/src/io/ktor/network/sockets/SocketEngine.web.ktktor-network/web/src/io/ktor/network/sockets/nodejs/node.net.kt
💤 Files with no reviewable changes (1)
- karma/config.js
9a71730 to
4970302
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (2)
ktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.kt (1)
24-25:⚠️ Potential issue | 🟠 Major | ⚡ Quick winHarden Node detection to avoid runtime crashes outside Node.
process.release.namecan throw whenprocessexists butreleaseis missing, so the fallback path may fail before returningnull.Proposed fix
`@JsFun`( - "moduleName => ((typeof process !== 'undefined') && process.release.name === 'node')" + + "moduleName => ((typeof process !== 'undefined') && process.release?.name === 'node')" + " ? import(moduleName) : Promise.resolve(null)" )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.kt` around lines 24 - 25, The Node detection expression can throw if process.release is undefined; update the conditional that uses process.release.name (the dynamic import guard string) to first verify process.release exists and is an object (e.g., check typeof process !== 'undefined' && process.release && typeof process.release.name !== 'undefined' or use a safe typeof check for process.release.name) or wrap the check in a safe runtime guard so the ternary returns Promise.resolve(null) instead of throwing; locate the expression that builds the import string referencing process.release.name and change it to use this defensive check (or a try/catch wrapper) to avoid runtime crashes outside Node.ktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.kt (1)
22-26:⚠️ Potential issue | 🔴 Critical | ⚡ Quick win
loadNodeNetModule()currently returns a thunk instead of thenode:netmodule.
((module) => () => module)(...)returns a zero-arg function, so the casted value is notNodeNetand fails later on socket API calls.Proposed fix
`@JsFun`( - "((module) => () => module)(((typeof process !== 'undefined') && process.release.name === 'node')" + + "((module) => module)(((typeof process !== 'undefined') && process.release?.name === 'node')" + " ? await import(/* webpackIgnore: true */'node:net') : null)" )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@ktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.kt` around lines 22 - 26, The current `@JsFun` wrapper in loadNodeNetModule() returns a zero-arg thunk instead of the actual node:net module; remove the outer IIFE that produces a ()=>module and return the module (or null) directly so the external function yields the NodeNet object (matching the expected cast/use in socket APIs). Edit the `@JsFun` string expression for loadNodeNetModule to evaluate to the imported module itself (or null when not running in Node) rather than a function, so subsequent casts/uses treating the result as NodeNet succeed.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Duplicate comments:
In `@ktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.kt`:
- Around line 24-25: The Node detection expression can throw if process.release
is undefined; update the conditional that uses process.release.name (the dynamic
import guard string) to first verify process.release exists and is an object
(e.g., check typeof process !== 'undefined' && process.release && typeof
process.release.name !== 'undefined' or use a safe typeof check for
process.release.name) or wrap the check in a safe runtime guard so the ternary
returns Promise.resolve(null) instead of throwing; locate the expression that
builds the import string referencing process.release.name and change it to use
this defensive check (or a try/catch wrapper) to avoid runtime crashes outside
Node.
In `@ktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.kt`:
- Around line 22-26: The current `@JsFun` wrapper in loadNodeNetModule() returns a
zero-arg thunk instead of the actual node:net module; remove the outer IIFE that
produces a ()=>module and return the module (or null) directly so the external
function yields the NodeNet object (matching the expected cast/use in socket
APIs). Edit the `@JsFun` string expression for loadNodeNetModule to evaluate to
the imported module itself (or null when not running in Node) rather than a
function, so subsequent casts/uses treating the result as NodeNet succeed.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 60d545ea-c117-40ec-bf14-711ca8e98467
📒 Files selected for processing (5)
karma/config.jsktor-network/js/src/io/ktor/network/sockets/nodejs/node.net.js.ktktor-network/wasmJs/src/io/ktor/network/sockets/nodejs/node.net.wasmJs.ktktor-network/web/src/io/ktor/network/sockets/SocketEngine.web.ktktor-network/web/src/io/ktor/network/sockets/nodejs/node.net.kt
💤 Files with no reviewable changes (1)
- karma/config.js
Subsystem
ktor-networkMotivation
KTOR-9595 CIO: The engine imports
node:netstatically and breaks wasmJsBrowser webpack buildSolution
Use the approach with
await importsimilar to this commit.