mirror of
https://github.com/lexmount/moli.git
synced 2026-11-18 16:08:37 +00:00
fix(streams): disturb source synchronously when piping starts
This commit is contained in:
+77
-73
@@ -3,82 +3,86 @@ async function runPipeDisturbedProbe(scenario) {
|
||||
const check = (condition, label) => {
|
||||
if (!condition) errors.push(label);
|
||||
};
|
||||
for (const byteStream of [false, true]) {
|
||||
for (const method of ['pipeTo', 'pipeThrough']) {
|
||||
const cases = scenario === 'start'
|
||||
? ['backpressure', 'pre-aborted', 'source-closed', 'source-errored', 'sink-closed', 'sink-errored']
|
||||
: ['source-locked', 'sink-locked', 'invalid-sink', 'invalid-signal', 'throwing-options'];
|
||||
for (const state of cases) {
|
||||
const label = `${byteStream ? 'bytes' : 'default'}/${method}/${state}`;
|
||||
const reason = new Error(label);
|
||||
let sourceController, sinkController, pulls = 0, writes = 0;
|
||||
const source = new ReadableStream({
|
||||
...(byteStream ? {type: 'bytes'} : {}),
|
||||
start(controller) { sourceController = controller; },
|
||||
pull() { ++pulls; }
|
||||
}, {highWaterMark: 0});
|
||||
const body = new Response(source);
|
||||
check(body.body === source, `${label}: body observes the tested source`);
|
||||
const sink = new WritableStream({
|
||||
start(controller) { sinkController = controller; },
|
||||
write() { ++writes; },
|
||||
abort() { check(body.bodyUsed, `${label}: abort observes disturbed source`); }
|
||||
}, {highWaterMark: 0});
|
||||
const output = new ReadableStream({}, {highWaterMark: 0});
|
||||
const outputBody = new Response(output);
|
||||
const abort = new AbortController();
|
||||
let options = {signal: abort.signal, preventCancel: true};
|
||||
let destination = sink, reader, writer;
|
||||
if (state === 'source-closed') sourceController.close();
|
||||
if (state === 'source-errored') sourceController.error(reason);
|
||||
if (state === 'sink-closed') await sink.close();
|
||||
if (state === 'sink-errored') sinkController.error(reason);
|
||||
if (state === 'pre-aborted') abort.abort(reason);
|
||||
if (state === 'source-locked') reader = source.getReader();
|
||||
if (state === 'sink-locked') writer = sink.getWriter();
|
||||
if (state === 'invalid-sink') destination = {};
|
||||
if (state === 'invalid-signal') options = {signal: {}};
|
||||
if (state === 'throwing-options') options = {get preventCancel() {throw reason;}};
|
||||
check(!body.bodyUsed, `${label}: initially undisturbed`);
|
||||
let completion, thrown;
|
||||
try {
|
||||
if (method === 'pipeTo') {
|
||||
completion = source.pipeTo(destination, options);
|
||||
completion.catch(() => {});
|
||||
for (const ownerKind of ['Response', 'Request']) {
|
||||
for (const byteStream of [false, true]) {
|
||||
for (const method of ['pipeTo', 'pipeThrough']) {
|
||||
const cases = scenario === 'start'
|
||||
? ['backpressure', 'pre-aborted', 'source-closed', 'source-errored', 'sink-closed', 'sink-errored']
|
||||
: ['source-locked', 'sink-locked', 'invalid-sink', 'invalid-signal', 'throwing-options'];
|
||||
for (const state of cases) {
|
||||
const label = `${ownerKind}/${byteStream ? 'bytes' : 'default'}/${method}/${state}`;
|
||||
const reason = new Error(label);
|
||||
let sourceController, sinkController, pulls = 0, writes = 0;
|
||||
const source = new ReadableStream({
|
||||
...(byteStream ? {type: 'bytes'} : {}),
|
||||
start(controller) { sourceController = controller; },
|
||||
pull() { ++pulls; }
|
||||
}, {highWaterMark: 0});
|
||||
const body = ownerKind === 'Response'
|
||||
? new Response(source)
|
||||
: new Request('https://example.test/', {method: 'POST', body: source, duplex: 'half'});
|
||||
check(body.body === source, `${label}: body observes the tested source`);
|
||||
const sink = new WritableStream({
|
||||
start(controller) { sinkController = controller; },
|
||||
write() { ++writes; },
|
||||
abort() { check(body.bodyUsed, `${label}: abort observes disturbed source`); }
|
||||
}, {highWaterMark: 0});
|
||||
const output = new ReadableStream({}, {highWaterMark: 0});
|
||||
const outputBody = new Response(output);
|
||||
const abort = new AbortController();
|
||||
let options = {signal: abort.signal, preventCancel: true};
|
||||
let destination = sink, reader, writer;
|
||||
if (state === 'source-closed') sourceController.close();
|
||||
if (state === 'source-errored') sourceController.error(reason);
|
||||
if (state === 'sink-closed') await sink.close();
|
||||
if (state === 'sink-errored') sinkController.error(reason);
|
||||
if (state === 'pre-aborted') abort.abort(reason);
|
||||
if (state === 'source-locked') reader = source.getReader();
|
||||
if (state === 'sink-locked') writer = sink.getWriter();
|
||||
if (state === 'invalid-sink') destination = {};
|
||||
if (state === 'invalid-signal') options = {signal: {}};
|
||||
if (state === 'throwing-options') options = {get preventCancel() {throw reason;}};
|
||||
check(!body.bodyUsed, `${label}: initially undisturbed`);
|
||||
let completion, thrown;
|
||||
try {
|
||||
if (method === 'pipeTo') {
|
||||
completion = source.pipeTo(destination, options);
|
||||
completion.catch(() => {});
|
||||
} else {
|
||||
const result = source.pipeThrough({readable: output, writable: destination}, options);
|
||||
check(result === output, `${label}: returns output stream`);
|
||||
}
|
||||
} catch (error) {
|
||||
thrown = error;
|
||||
}
|
||||
if (scenario === 'start') {
|
||||
check(thrown === undefined, `${label}: accepted pipe does not throw`);
|
||||
check(body.bodyUsed, `${label}: synchronously disturbed without reading`);
|
||||
check(pulls === 0 && writes === 0, `${label}: no pull or write before return`);
|
||||
check(!outputBody.bodyUsed, `${label}: output remains undisturbed`);
|
||||
abort.abort(reason);
|
||||
if (completion) await completion.catch(() => {});
|
||||
for (let turn = 0; (source.locked || sink.locked) && turn < 30; ++turn) {
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
}
|
||||
check(!source.locked && !sink.locked, `${label}: pipe releases locks`);
|
||||
check(body.bodyUsed, `${label}: remains disturbed after shutdown`);
|
||||
} else {
|
||||
const result = source.pipeThrough({readable: output, writable: destination}, options);
|
||||
check(result === output, `${label}: returns output stream`);
|
||||
if (completion) {
|
||||
check(thrown === undefined, `${label}: pipeTo returns a rejection`);
|
||||
thrown = await completion.then(() => undefined, error => error);
|
||||
}
|
||||
check(state === 'throwing-options' ? thrown === reason : thrown instanceof TypeError,
|
||||
`${label}: validation rejects with original error`);
|
||||
reader?.releaseLock();
|
||||
writer?.releaseLock();
|
||||
check(!body.bodyUsed, `${label}: rejected pipe leaves source undisturbed`);
|
||||
check(!source.locked && !sink.locked, `${label}: rejected pipe acquires no locks`);
|
||||
check(pulls === 0 && writes === 0, `${label}: rejected pipe does not read or write`);
|
||||
}
|
||||
} catch (error) {
|
||||
thrown = error;
|
||||
// Avoid invoking the observation above during test cleanup.
|
||||
await Promise.allSettled([source.cancel(), output.cancel()]);
|
||||
}
|
||||
if (scenario === 'start') {
|
||||
check(thrown === undefined, `${label}: accepted pipe does not throw`);
|
||||
check(body.bodyUsed, `${label}: synchronously disturbed without reading`);
|
||||
check(pulls === 0 && writes === 0, `${label}: no pull or write before return`);
|
||||
check(!outputBody.bodyUsed, `${label}: output remains undisturbed`);
|
||||
abort.abort(reason);
|
||||
if (completion) await completion.catch(() => {});
|
||||
for (let turn = 0; (source.locked || sink.locked) && turn < 30; ++turn) {
|
||||
await new Promise(resolve => setTimeout(resolve, 0));
|
||||
}
|
||||
check(!source.locked && !sink.locked, `${label}: pipe releases locks`);
|
||||
check(body.bodyUsed, `${label}: remains disturbed after shutdown`);
|
||||
} else {
|
||||
if (completion) {
|
||||
check(thrown === undefined, `${label}: pipeTo returns a rejection`);
|
||||
thrown = await completion.then(() => undefined, error => error);
|
||||
}
|
||||
check(state === 'throwing-options' ? thrown === reason : thrown instanceof TypeError,
|
||||
`${label}: validation rejects with original error`);
|
||||
reader?.releaseLock();
|
||||
writer?.releaseLock();
|
||||
check(!body.bodyUsed, `${label}: rejected pipe leaves source undisturbed`);
|
||||
check(!source.locked && !sink.locked, `${label}: rejected pipe acquires no locks`);
|
||||
check(pulls === 0 && writes === 0, `${label}: rejected pipe does not read or write`);
|
||||
}
|
||||
// Avoid invoking the observation above during test cleanup.
|
||||
await Promise.allSettled([source.cancel(), output.cancel()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,6 @@ mod font_queries;
|
||||
#[path = "web_apis/indexed_db_transaction.rs"]
|
||||
mod indexed_db_transaction;
|
||||
|
||||
#[path = "web_apis/pipe_disturbed.rs"]
|
||||
mod pipe_disturbed;
|
||||
|
||||
use moli_test_support as support;
|
||||
|
||||
use anyhow::Result;
|
||||
@@ -36,6 +33,8 @@ mod body_state;
|
||||
mod callback_cleanup;
|
||||
#[path = "web_apis/event_dispatch.rs"]
|
||||
mod event_dispatch;
|
||||
#[path = "web_apis/pipe_disturbed.rs"]
|
||||
mod pipe_disturbed;
|
||||
#[path = "web_apis/request_init.rs"]
|
||||
mod request_init;
|
||||
#[path = "web_apis/request_stream.rs"]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::pipe_disturbed::run_probe;
|
||||
use super::event_dispatch::run_probe;
|
||||
use super::*;
|
||||
|
||||
async fn assert_multi_entry_keys(scenario: &str) -> Result<()> {
|
||||
|
||||
@@ -11,7 +11,7 @@ async fn assert_pipe_disturbed(scenario: &str) -> Result<()> {
|
||||
for target in ["window", "child", "worker"] {
|
||||
let observed = tokio::time::timeout(
|
||||
Duration::from_secs(20),
|
||||
run_probe(&browser, &server, target, &source),
|
||||
super::event_dispatch::run_probe(&browser, &server, target, &source),
|
||||
)
|
||||
.await??;
|
||||
assert_eq!(
|
||||
@@ -33,59 +33,3 @@ async fn piping_disturbs_fetch_bodies_before_reads_and_abort_handling() -> Resul
|
||||
async fn rejected_piping_preserves_undisturbed_fetch_bodies() -> Result<()> {
|
||||
assert_pipe_disturbed("reject").await
|
||||
}
|
||||
|
||||
fn markup_url(server: &FixtureServer, markup: &str) -> String {
|
||||
let mut url = url::Url::parse(&server.url("/compat/child-dynamic-markup-document")).unwrap();
|
||||
url.query_pairs_mut().append_pair("markup", markup);
|
||||
url.into()
|
||||
}
|
||||
|
||||
pub(super) async fn run_probe(
|
||||
browser: &Browser,
|
||||
server: &FixtureServer,
|
||||
target: &str,
|
||||
source: &str,
|
||||
) -> Result<serde_json::Value> {
|
||||
let markup = if target == "worker" {
|
||||
let source = serde_json::to_string(&format!(
|
||||
"self.finish = value => postMessage(value);\n{source}"
|
||||
))?;
|
||||
format!(
|
||||
r#"<!doctype html><script>
|
||||
const worker = new Worker(URL.createObjectURL(new Blob([{source}], {{type: 'text/javascript'}})));
|
||||
window.done = new Promise(resolve => {{
|
||||
worker.onmessage = event => {{ resolve(event.data); worker.terminate(); }};
|
||||
}});
|
||||
worker.postMessage('go');
|
||||
</script>"#
|
||||
)
|
||||
} else {
|
||||
format!(
|
||||
"<!doctype html><script>self.done = new Promise(resolve => self.finish = resolve);\n{source}</script>"
|
||||
)
|
||||
};
|
||||
let url = markup_url(server, &markup);
|
||||
let url = if target == "child" {
|
||||
markup_url(
|
||||
server,
|
||||
&format!(
|
||||
"<!doctype html><iframe id=target src=\"{}\"></iframe>",
|
||||
url.replace('&', "&")
|
||||
),
|
||||
)
|
||||
} else {
|
||||
url
|
||||
};
|
||||
let mut page = browser.fetch(&url).await?;
|
||||
let expression = if target == "child" {
|
||||
"document.getElementById('target').contentWindow.done.then(JSON.stringify)"
|
||||
} else {
|
||||
"done.then(JSON.stringify)"
|
||||
};
|
||||
let value = page
|
||||
.evaluate_runtime_expression_with_await_async(expression, true)
|
||||
.await?;
|
||||
Ok(serde_json::from_str(
|
||||
value["value"].as_str().expect("callback probe result"),
|
||||
)?)
|
||||
}
|
||||
|
||||
@@ -60,9 +60,6 @@ pub(crate) fn proxy_fetch_body_stream<'s>(
|
||||
initialize_transform_stream_object(scope, transform, None, None, 1.0, None, 0.0, None);
|
||||
let readable = stream_slot_object(scope, transform, TRANSFORM_STREAM_READABLE_SLOT)?;
|
||||
let writable = stream_slot_object(scope, transform, TRANSFORM_STREAM_WRITABLE_SLOT)?;
|
||||
// A proxy disturbs its input immediately, even when backpressure delays
|
||||
// the first source read.
|
||||
super::stream_adapter::disturb_readable_stream(scope, stream);
|
||||
let pipe = readable::start_internal_readable_stream_pipe_to(scope, stream, writable)?;
|
||||
suppress_promise_unhandled_rejection(scope, pipe);
|
||||
Some(readable)
|
||||
|
||||
Reference in New Issue
Block a user