diff --git a/streams/readable-streams/owning-type-message-port.any.js b/streams/readable-streams/owning-type-message-port.any.js index 282c1f411485c0..e9961ce042256a 100644 --- a/streams/readable-streams/owning-type-message-port.any.js +++ b/streams/readable-streams/owning-type-message-port.any.js @@ -1,4 +1,4 @@ -// META: global=window,worker,shadowrealm +// META: global=window,worker // META: script=../resources/test-utils.js // META: script=../resources/rs-utils.js 'use strict'; diff --git a/streams/transferable/readable-stream.html b/streams/transferable/readable-stream.any.js similarity index 93% rename from streams/transferable/readable-stream.html rename to streams/transferable/readable-stream.any.js index b1ede4695bf4cd..f885a48e4a5454 100644 --- a/streams/transferable/readable-stream.html +++ b/streams/transferable/readable-stream.any.js @@ -1,11 +1,8 @@ - - - - - - - - diff --git a/streams/transferable/resources/helpers.js b/streams/transferable/resources/helpers.js index 12504537f91eab..30c6abb4b83815 100644 --- a/streams/transferable/resources/helpers.js +++ b/streams/transferable/resources/helpers.js @@ -98,7 +98,11 @@ } }, {once: true}); }); - postMessage(original, '*', [original]); + if (GLOBAL.isShadowRealm()) { + structuredClone(original, {transfer: [original]}); + } else { + postMessage(original, '*', [original]); + } return promise; } @@ -117,7 +121,11 @@ } }, {once: true}); }); - postMessage(original, '*', [original]); + if (GLOBAL.isShadowRealm()) { + structuredClone(original, {transfer: [original]}); + } else { + postMessage(original, '*', [original]); + } return promise; } diff --git a/streams/transferable/transform-stream.any.js b/streams/transferable/transform-stream.any.js new file mode 100644 index 00000000000000..86a68e93dfe21e --- /dev/null +++ b/streams/transferable/transform-stream.any.js @@ -0,0 +1,117 @@ +// META: global=window,dedicatedworker,shadowrealm +// META: script=../resources/test-utils.js + +'use strict'; + +function transfer(t, obj, transfers) { + if (GLOBAL.isShadowRealm()) { + const transferred = structuredClone(obj, {transfer: transfers}); + return Promise.resolve(transferred); + } + return new Promise(resolve => { + addEventListener('message', t.step_func(evt => { + const transferred = evt.data; + resolve(transferred); + }), {once: true}); + postMessage(obj, '*', transfers); + }); +} + +function failToTransfer(obj) { + if (GLOBAL.isShadowRealm()) { + structuredClone(obj, {transfer: [obj]}); + } else { + postMessage(obj, '*', [obj]); + } +} + +promise_test(t => { + const orig = new TransformStream(); + const promise = transfer(t, orig, [orig]); + assert_true(orig.readable.locked, 'the readable side should be locked'); + assert_true(orig.writable.locked, 'the writable side should be locked'); + return promise.then(transferred => { + assert_equals(transferred.constructor, TransformStream, + 'transferred should be a TransformStream in this realm'); + assert_true(transferred instanceof TransformStream, + 'instanceof check should pass'); + + // Perform a brand-check on |transferred|. + const readableGetter = Object.getOwnPropertyDescriptor( + TransformStream.prototype, 'readable').get; + assert_true(readableGetter.call(transferred) instanceof ReadableStream, + 'brand check should pass and readable stream should result'); + const writableGetter = Object.getOwnPropertyDescriptor( + TransformStream.prototype, 'writable').get; + assert_true(writableGetter.call(transferred) instanceof WritableStream, + 'brand check should pass and writable stream should result'); + }); +}, `should be able to transfer a TransformStream`); + +test(() => { + const ts = new TransformStream(); + const writer = ts.writable.getWriter(); + assert_throws_dom('DataCloneError', () => failToTransfer(ts), + 'transferring should throw'); + assert_false(ts.readable.locked, 'readable side should not get locked'); +}, 'a TransformStream with a locked writable should not be transferable'); + +test(() => { + const ts = new TransformStream(); + const reader = ts.readable.getReader(); + assert_throws_dom('DataCloneError', () => failToTransfer(ts), + 'transferring should throw'); + assert_false(ts.writable.locked, 'writable side should not get locked'); +}, 'a TransformStream with a locked readable should not be transferable'); + +test(() => { + const ts = new TransformStream(); + const reader = ts.readable.getReader(); + const writer = ts.writable.getWriter(); + assert_throws_dom('DataCloneError', () => failToTransfer(ts), + 'transferring should throw'); +}, 'a TransformStream with both sides locked should not be transferable'); + +promise_test(t => { + const source = new ReadableStream({ + start(controller) { + controller.enqueue('hello '); + controller.enqueue('there '); + controller.close(); + } + }); + let resolve; + const ready = new Promise(r => resolve = r); + let result = ''; + const sink = new WritableStream({ + write(chunk) { + if (result) { + resolve(); + } + result += chunk; + } + }); + const transform1 = new TransformStream({ + transform(chunk, controller) { + controller.enqueue(chunk.toUpperCase()); + } + }); + const transform2 = new TransformStream({ + transform(chunk, controller) { + controller.enqueue(chunk + chunk); + } + }); + return transfer(t, {source, sink, transform1, transform2}, + [source, transform1, sink, transform2]) + .then(data => { + resolve(data.source + .pipeThrough(data.transform1) + .pipeThrough(data.transform2) + .pipeTo(data.sink)); + }) + .then(() => ready) + .then(() => { + assert_equals(result, 'HELLO HELLO THERE THERE ', + 'transforms should have been applied'); + }); +}, 'piping through transferred transforms should work'); diff --git a/streams/transferable/transform-stream.html b/streams/transferable/transform-stream.html deleted file mode 100644 index 355d5d807433d7..00000000000000 --- a/streams/transferable/transform-stream.html +++ /dev/null @@ -1,108 +0,0 @@ - - - - - - diff --git a/streams/transferable/writable-stream.html b/streams/transferable/writable-stream.any.js similarity index 59% rename from streams/transferable/writable-stream.html rename to streams/transferable/writable-stream.any.js index 7e25dad94d4cfd..4d57ecba973d08 100644 --- a/streams/transferable/writable-stream.html +++ b/streams/transferable/writable-stream.any.js @@ -1,65 +1,78 @@ - - - - - - - -