-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
streams: implement TransformStream cleanup using "transformer.cancel" #50126
Merged
nodejs-github-bot
merged 7 commits into
nodejs:main
from
debadree25:ft/transform-cancel-impl
Dec 15, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ee89ba1
stream: implement TransformStream cleanup using "transformer.cancel"
debadree25 867f64e
test: update wpts
debadree25 02205e7
fixup! replace promise resolve
debadree25 67b0944
fixup! useless import
debadree25 c627596
fixup! wrong import remove
debadree25 23e6183
expect a failure
debadree25 2557e85
linting
debadree25 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ const { | |
ObjectDefineProperties, | ||
ObjectSetPrototypeOf, | ||
PromisePrototypeThen, | ||
debadree25 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
PromiseResolve, | ||
SymbolToStringTag, | ||
Symbol, | ||
} = primordials; | ||
|
@@ -47,6 +46,7 @@ const { | |
nonOpFlush, | ||
kType, | ||
kState, | ||
nonOpCancel, | ||
} = require('internal/webstreams/util'); | ||
|
||
const { | ||
|
@@ -384,8 +384,7 @@ function initializeTransformStream( | |
return transformStreamDefaultSourcePullAlgorithm(stream); | ||
}, | ||
cancel(reason) { | ||
transformStreamErrorWritableAndUnblockWrite(stream, reason); | ||
return PromiseResolve(); | ||
return transformStreamDefaultSourceCancelAlgorithm(stream, reason); | ||
}, | ||
}, { | ||
highWaterMark: readableHighWaterMark, | ||
|
@@ -427,6 +426,10 @@ function transformStreamErrorWritableAndUnblockWrite(stream, error) { | |
writableStreamDefaultControllerErrorIfNeeded( | ||
writable[kState].controller, | ||
error); | ||
transformStreamUnblockWrite(stream); | ||
} | ||
|
||
function transformStreamUnblockWrite(stream) { | ||
if (stream[kState].backpressure) | ||
transformStreamSetBackpressure(stream, false); | ||
} | ||
|
@@ -443,13 +446,15 @@ function setupTransformStreamDefaultController( | |
stream, | ||
controller, | ||
transformAlgorithm, | ||
flushAlgorithm) { | ||
flushAlgorithm, | ||
cancelAlgorithm) { | ||
assert(isTransformStream(stream)); | ||
assert(stream[kState].controller === undefined); | ||
controller[kState] = { | ||
stream, | ||
transformAlgorithm, | ||
flushAlgorithm, | ||
cancelAlgorithm, | ||
}; | ||
stream[kState].controller = controller; | ||
} | ||
|
@@ -460,21 +465,26 @@ function setupTransformStreamDefaultControllerFromTransformer( | |
const controller = new TransformStreamDefaultController(kSkipThrow); | ||
const transform = transformer?.transform || defaultTransformAlgorithm; | ||
const flush = transformer?.flush || nonOpFlush; | ||
const cancel = transformer?.cancel || nonOpCancel; | ||
const transformAlgorithm = | ||
FunctionPrototypeBind(transform, transformer); | ||
const flushAlgorithm = | ||
FunctionPrototypeBind(flush, transformer); | ||
const cancelAlgorithm = | ||
FunctionPrototypeBind(cancel, transformer); | ||
|
||
setupTransformStreamDefaultController( | ||
stream, | ||
controller, | ||
transformAlgorithm, | ||
flushAlgorithm); | ||
flushAlgorithm, | ||
cancelAlgorithm); | ||
} | ||
|
||
function transformStreamDefaultControllerClearAlgorithms(controller) { | ||
controller[kState].transformAlgorithm = undefined; | ||
controller[kState].flushAlgorithm = undefined; | ||
controller[kState].cancelAlgorithm = undefined; | ||
} | ||
|
||
function transformStreamDefaultControllerEnqueue(controller, chunk) { | ||
|
@@ -563,7 +573,40 @@ function transformStreamDefaultSinkWriteAlgorithm(stream, chunk) { | |
} | ||
|
||
async function transformStreamDefaultSinkAbortAlgorithm(stream, reason) { | ||
transformStreamError(stream, reason); | ||
const { | ||
controller, | ||
readable, | ||
} = stream[kState]; | ||
|
||
if (controller[kState].finishPromise !== undefined) { | ||
return controller[kState].finishPromise; | ||
} | ||
|
||
const { promise, resolve, reject } = createDeferredPromise(); | ||
controller[kState].finishPromise = promise; | ||
const cancelPromise = ensureIsPromise( | ||
controller[kState].cancelAlgorithm, | ||
controller, | ||
reason); | ||
transformStreamDefaultControllerClearAlgorithms(controller); | ||
|
||
PromisePrototypeThen( | ||
cancelPromise, | ||
() => { | ||
if (readable[kState].state === 'errored') | ||
reject(readable[kState].storedError); | ||
else { | ||
readableStreamDefaultControllerError(readable[kState].controller, reason); | ||
resolve(); | ||
} | ||
}, | ||
(error) => { | ||
readableStreamDefaultControllerError(readable[kState].controller, error); | ||
reject(error); | ||
}, | ||
); | ||
|
||
return controller[kState].finishPromise; | ||
} | ||
|
||
function transformStreamDefaultSinkCloseAlgorithm(stream) { | ||
|
@@ -572,23 +615,32 @@ function transformStreamDefaultSinkCloseAlgorithm(stream) { | |
controller, | ||
} = stream[kState]; | ||
|
||
if (controller[kState].finishPromise !== undefined) { | ||
return controller[kState].finishPromise; | ||
} | ||
const { promise, resolve, reject } = createDeferredPromise(); | ||
controller[kState].finishPromise = promise; | ||
const flushPromise = | ||
ensureIsPromise( | ||
controller[kState].flushAlgorithm, | ||
controller, | ||
controller); | ||
transformStreamDefaultControllerClearAlgorithms(controller); | ||
return PromisePrototypeThen( | ||
PromisePrototypeThen( | ||
flushPromise, | ||
() => { | ||
if (readable[kState].state === 'errored') | ||
throw readable[kState].storedError; | ||
readableStreamDefaultControllerClose(readable[kState].controller); | ||
reject(readable[kState].storedError); | ||
else { | ||
readableStreamDefaultControllerClose(readable[kState].controller); | ||
resolve(); | ||
} | ||
}, | ||
(error) => { | ||
transformStreamError(stream, error); | ||
throw readable[kState].storedError; | ||
readableStreamDefaultControllerError(readable[kState].controller, error); | ||
reject(error); | ||
}); | ||
return controller[kState].finishPromise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean we are adding one more promise all the time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed yes i guess, seems like spec demands! |
||
} | ||
|
||
function transformStreamDefaultSourcePullAlgorithm(stream) { | ||
|
@@ -598,6 +650,48 @@ function transformStreamDefaultSourcePullAlgorithm(stream) { | |
return stream[kState].backpressureChange.promise; | ||
} | ||
|
||
function transformStreamDefaultSourceCancelAlgorithm(stream, reason) { | ||
const { | ||
controller, | ||
writable, | ||
} = stream[kState]; | ||
|
||
if (controller[kState].finishPromise !== undefined) { | ||
return controller[kState].finishPromise; | ||
} | ||
|
||
const { promise, resolve, reject } = createDeferredPromise(); | ||
controller[kState].finishPromise = promise; | ||
const cancelPromise = ensureIsPromise( | ||
controller[kState].cancelAlgorithm, | ||
controller, | ||
reason); | ||
transformStreamDefaultControllerClearAlgorithms(controller); | ||
|
||
PromisePrototypeThen(cancelPromise, | ||
() => { | ||
if (writable[kState].state === 'errored') | ||
reject(writable[kState].storedError); | ||
else { | ||
writableStreamDefaultControllerErrorIfNeeded( | ||
writable[kState].controller, | ||
reason); | ||
transformStreamUnblockWrite(stream); | ||
resolve(); | ||
} | ||
}, | ||
(error) => { | ||
writableStreamDefaultControllerErrorIfNeeded( | ||
writable[kState].controller, | ||
error); | ||
transformStreamUnblockWrite(stream); | ||
reject(error); | ||
}, | ||
); | ||
|
||
return controller[kState].finishPromise; | ||
} | ||
|
||
module.exports = { | ||
TransformStream, | ||
TransformStreamDefaultController, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/piping/close-propagation-backward.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/piping/close-propagation-forward.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/fixtures/wpt/streams/piping/crashtests/cross-piping.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<script type="module"> | ||
let a = new ReadableStream(); | ||
let b = self.open() | ||
let f = new b.WritableStream(); | ||
a.pipeThrough( | ||
{ "readable": a, "writable": f }, | ||
{ "signal": AbortSignal.abort() } | ||
) | ||
await new Promise(setTimeout); | ||
structuredClone(undefined, { "transfer": [f] }) | ||
</script> |
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/piping/error-propagation-backward.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/piping/error-propagation-forward.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// META: global=window,worker | ||
// META: global=window,worker,shadowrealm | ||
'use strict'; | ||
|
||
promise_test(async t => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// META: global=window,worker | ||
// META: global=window,worker,shadowrealm | ||
'use strict'; | ||
|
||
class ThrowingOptions { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// META: global=window,worker | ||
// META: global=window,worker,shadowrealm | ||
'use strict'; | ||
|
||
promise_test(() => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/readable-byte-streams/bad-buffers-and-views.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// META: global=window,worker | ||
// META: global=window,worker,shadowrealm | ||
'use strict'; | ||
|
||
promise_test(() => { | ||
|
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/readable-byte-streams/construct-byob-request.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/readable-byte-streams/enqueue-with-detached-buffer.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/readable-byte-streams/non-transferable-buffers.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// META: global=window,worker | ||
// META: global=window,worker,shadowrealm | ||
'use strict'; | ||
|
||
promise_test(async t => { | ||
|
2 changes: 1 addition & 1 deletion
2
test/fixtures/wpt/streams/readable-byte-streams/respond-after-enqueue.any.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// META: global=window,worker | ||
// META: global=window,worker,shadowrealm | ||
|
||
'use strict'; | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to align with the WHATWG spec, we need to do it in more places too Ref: #50126 (comment)