Repository navigation
feat(DEP0207): migrate HTTP/2 'aborted' listeners to 'close' - #580
FrustratedBoy420 wants to merge 2 commits into
Conversation
Add the http2-aborted-to-close recipe. It applies one chosen strategy
("run this handler when the writable side did not finish"): an inline
'aborted' listener with no parameters on a locally resolved
Http2Stream becomes a 'close' listener wrapped in
`if (!stream.writableFinished)`.
Resolved streams: the first parameter of a 'stream' listener on a
createServer()/createSecureServer() server, and session.request()
results on a connect() session.
Reported, not rewritten: .aborted property reads, named listeners or
listeners with parameters, and streams whose 'aborted' listeners are
removed, counted or listed. The compatibility API and read-side
'error' handling are left alone.
Closes nodejs#558
Assisted-by: Claude Code (claude-opus-5-5)
AugustinMauroy
left a comment
There was a problem hiding this comment.
still a lot of kind() === pattern
and query can be stronger by using better query.
| } | ||
|
|
||
| if (!edits.length) return null; | ||
| return rootNode.commitEdits(edits); |
There was a problem hiding this comment.
| return rootNode.commitEdits(edits); | |
| return rootNode.commitEdits(edits); |
There was a problem hiding this comment.
Done in bbdee9b: the early return null is gone and it always returns rootNode.commitEdits(edits).
| !listener || | ||
| !FUNCTION_KINDS.has(listener.kind()) || | ||
| countParameters(listener) !== 0 || | ||
| parent?.kind() !== 'expression_statement' |
There was a problem hiding this comment.
| parent?.kind() !== 'expression_statement' | |
| !parent?.is('expression_statement') |
There was a problem hiding this comment.
Done in bbdee9b: it uses call.parent()?.is('expression_statement') now, and every other kind() === check is replaced too.
|
|
||
| const streams: StreamBinding[] = []; | ||
| for (const call of rootNode.findAll({ | ||
| rule: { pattern: '$SERVER.$METHOD($EVENT, $LISTENER)' }, |
There was a problem hiding this comment.
there are serval ether like this on the codemod I think we can do a stronger AST query
There was a problem hiding this comment.
Done in bbdee9b: servers, 'stream' listeners, sessions and 'aborted' listeners are now found with rules + constraints (method regex, inline function kinds, server/session names), so the code no longer filters nodes by hand.
| function stringValue(node: SgNode<Js> | undefined): string | undefined { | ||
| if (node?.kind() !== 'string') return undefined; | ||
| return node.text().slice(1, -1); | ||
| } |
There was a problem hiding this comment.
this can be replace by node.find() with rule for string_fragment
There was a problem hiding this comment.
Done in bbdee9b: the event name is matched with a string_fragment rule, and the rewrite replaces that fragment, so the original quotes stay as they were.
Address review on nodejs#580: - Match listeners, servers and sessions with ast-grep rules and `constraints` (method regex, string_fragment for the event name, inline function kinds) instead of filtering nodes in code. - Use `is()` instead of `kind() ===`. - Rewrite the event name through its `string_fragment`, so the original quotes are kept without slicing text. - Always return `commitEdits(edits)`. Assisted-by: Claude Code (claude-opus-5-5)
Closes #558
What it does
Adds the
http2-aborted-to-closerecipe for DEP0207. It applies one chosen strategy, "run this handler when the writable side did not finish before the stream closed", which matches the write-side example in the DEP0207 docs:server.on('stream', (stream) => { - stream.on('aborted', () => { - recordIncompleteResponse(); + stream.on('close', () => { + if (!stream.writableFinished) { + recordIncompleteResponse(); + } }); });Scope follows the issue:
'stream'listener on acreateServer()/createSecureServer()server, andsession.request()results on aconnect()session. Works withhttp2andnode:http2fromrequireorimport(default, namespace, named and aliased).on,once,addListener,prependListener,prependOnceListener). Block and expression bodies are both handled..abortedproperty reads, named listeners or listeners with parameters, and every'aborted'listener on a stream whose'aborted'listeners are also removed, counted or listed (off,removeListener,removeAllListeners,listeners,rawListeners,listenerCount).createServer((req, res) => …)) and read-side'error'handling.Tests
9 cases: CJS server, ESM aliased
createSecureServerwithonceand afunctionlistener, clientrequest()with an expression body, chainedcreateServer().on('stream', …)with 2-space indentation, TypeScript typed parameters, and four "unchanged" cases (named/parameterised listeners plus a.abortedread,removeAllListeners('aborted'), the compat API, and a file withouthttp2).npm testin the recipe,biome lintandtsc --noEmitpass.Runtime check and a question
The issue asks to validate the rewrite against normal completion, peer reset, local destruction, and writes ended but not finished. I ran those four cases on Node.js 25 (the only version I have locally), recording whether
'aborted'fired and whether!writableFinishedwas true at'close':'aborted'close&&!writableFinisheddestroy()end()called, not finished, then peer resetOn Node 25 the new pattern misses a peer reset, because the writable side is already finished by
'close'. As far as I can tell, nodejs/node#63249 (Node 27) changes this reset handling, and that is why the docs recommend this pattern, but I could not test Node 27 here. The README says to run the codemod when moving to Node.js 27. Could someone confirm the peer-reset row on 27?AI assistance
I used Claude Code (claude-opus-5-5) to help write the transform, tests and README, and to run the runtime check above. I reviewed every line and can explain it. The commit has an
Assisted-bytrailer, following the OpenJS AI coding assistants policy.