11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<meta charset="utf-8">
31cb0ef41Sopenharmony_ci<script src="/resources/testharness.js"></script>
41cb0ef41Sopenharmony_ci<script src="/resources/testharnessreport.js"></script>
51cb0ef41Sopenharmony_ci<script src="../resources/test-utils.js"></script>
61cb0ef41Sopenharmony_ci<script>
71cb0ef41Sopenharmony_ci'use strict';
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_cipromise_test(t => {
101cb0ef41Sopenharmony_ci  const orig = new TransformStream();
111cb0ef41Sopenharmony_ci  const promise = new Promise(resolve => {
121cb0ef41Sopenharmony_ci    addEventListener('message', t.step_func(evt => {
131cb0ef41Sopenharmony_ci      const transferred = evt.data;
141cb0ef41Sopenharmony_ci      assert_equals(transferred.constructor, TransformStream,
151cb0ef41Sopenharmony_ci                    'transferred should be a TransformStream in this realm');
161cb0ef41Sopenharmony_ci      assert_true(transferred instanceof TransformStream,
171cb0ef41Sopenharmony_ci                  'instanceof check should pass');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci      // Perform a brand-check on |transferred|.
201cb0ef41Sopenharmony_ci      const readableGetter = Object.getOwnPropertyDescriptor(
211cb0ef41Sopenharmony_ci        TransformStream.prototype, 'readable').get;
221cb0ef41Sopenharmony_ci      assert_true(readableGetter.call(transferred) instanceof ReadableStream,
231cb0ef41Sopenharmony_ci                 'brand check should pass and readable stream should result');
241cb0ef41Sopenharmony_ci      const writableGetter = Object.getOwnPropertyDescriptor(
251cb0ef41Sopenharmony_ci        TransformStream.prototype, 'writable').get;
261cb0ef41Sopenharmony_ci      assert_true(writableGetter.call(transferred) instanceof WritableStream,
271cb0ef41Sopenharmony_ci                 'brand check should pass and writable stream should result');
281cb0ef41Sopenharmony_ci      resolve();
291cb0ef41Sopenharmony_ci    }), {once: true});
301cb0ef41Sopenharmony_ci  });
311cb0ef41Sopenharmony_ci  postMessage(orig, '*', [orig]);
321cb0ef41Sopenharmony_ci  assert_true(orig.readable.locked, 'the readable side should be locked');
331cb0ef41Sopenharmony_ci  assert_true(orig.writable.locked, 'the writable side should be locked');
341cb0ef41Sopenharmony_ci  return promise;
351cb0ef41Sopenharmony_ci}, 'window.postMessage should be able to transfer a TransformStream');
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_citest(() => {
381cb0ef41Sopenharmony_ci  const ts = new TransformStream();
391cb0ef41Sopenharmony_ci  const writer = ts.writable.getWriter();
401cb0ef41Sopenharmony_ci  assert_throws_dom('DataCloneError', () => postMessage(ts, '*', [ts]),
411cb0ef41Sopenharmony_ci                    'postMessage should throw');
421cb0ef41Sopenharmony_ci  assert_false(ts.readable.locked, 'readable side should not get locked');
431cb0ef41Sopenharmony_ci}, 'a TransformStream with a locked writable should not be transferable');
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_citest(() => {
461cb0ef41Sopenharmony_ci  const ts = new TransformStream();
471cb0ef41Sopenharmony_ci  const reader = ts.readable.getReader();
481cb0ef41Sopenharmony_ci  assert_throws_dom('DataCloneError', () => postMessage(ts, '*', [ts]),
491cb0ef41Sopenharmony_ci                    'postMessage should throw');
501cb0ef41Sopenharmony_ci  assert_false(ts.writable.locked, 'writable side should not get locked');
511cb0ef41Sopenharmony_ci}, 'a TransformStream with a locked readable should not be transferable');
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_citest(() => {
541cb0ef41Sopenharmony_ci  const ts = new TransformStream();
551cb0ef41Sopenharmony_ci  const reader = ts.readable.getReader();
561cb0ef41Sopenharmony_ci  const writer = ts.writable.getWriter();
571cb0ef41Sopenharmony_ci  assert_throws_dom('DataCloneError', () => postMessage(ts, '*', [ts]),
581cb0ef41Sopenharmony_ci                    'postMessage should throw');
591cb0ef41Sopenharmony_ci}, 'a TransformStream with both sides locked should not be transferable');
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_cipromise_test(t => {
621cb0ef41Sopenharmony_ci  const source = new ReadableStream({
631cb0ef41Sopenharmony_ci    start(controller) {
641cb0ef41Sopenharmony_ci      controller.enqueue('hello ');
651cb0ef41Sopenharmony_ci      controller.enqueue('there ');
661cb0ef41Sopenharmony_ci      controller.close();
671cb0ef41Sopenharmony_ci    }
681cb0ef41Sopenharmony_ci  });
691cb0ef41Sopenharmony_ci  let resolve;
701cb0ef41Sopenharmony_ci  const ready = new Promise(r => resolve = r);
711cb0ef41Sopenharmony_ci  let result = '';
721cb0ef41Sopenharmony_ci  const sink = new WritableStream({
731cb0ef41Sopenharmony_ci    write(chunk) {
741cb0ef41Sopenharmony_ci      if (result) {
751cb0ef41Sopenharmony_ci        resolve();
761cb0ef41Sopenharmony_ci      }
771cb0ef41Sopenharmony_ci      result += chunk;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci  });
801cb0ef41Sopenharmony_ci  const transform1 = new TransformStream({
811cb0ef41Sopenharmony_ci    transform(chunk, controller) {
821cb0ef41Sopenharmony_ci      controller.enqueue(chunk.toUpperCase());
831cb0ef41Sopenharmony_ci    }
841cb0ef41Sopenharmony_ci  });
851cb0ef41Sopenharmony_ci  const transform2 = new TransformStream({
861cb0ef41Sopenharmony_ci    transform(chunk, controller) {
871cb0ef41Sopenharmony_ci      controller.enqueue(chunk + chunk);
881cb0ef41Sopenharmony_ci    }
891cb0ef41Sopenharmony_ci  });
901cb0ef41Sopenharmony_ci  const promise = new Promise(resolve => {
911cb0ef41Sopenharmony_ci    addEventListener('message', t.step_func(evt => {
921cb0ef41Sopenharmony_ci      const data = evt.data;
931cb0ef41Sopenharmony_ci      resolve(data.source
941cb0ef41Sopenharmony_ci                .pipeThrough(data.transform1)
951cb0ef41Sopenharmony_ci                .pipeThrough(data.transform2)
961cb0ef41Sopenharmony_ci                .pipeTo(data.sink));
971cb0ef41Sopenharmony_ci    }));
981cb0ef41Sopenharmony_ci  });
991cb0ef41Sopenharmony_ci  postMessage({source, sink, transform1, transform2}, '*',
1001cb0ef41Sopenharmony_ci              [source, transform1, sink, transform2]);
1011cb0ef41Sopenharmony_ci  return ready
1021cb0ef41Sopenharmony_ci    .then(() => {
1031cb0ef41Sopenharmony_ci      assert_equals(result, 'HELLO HELLO THERE THERE ',
1041cb0ef41Sopenharmony_ci                    'transforms should have been applied');
1051cb0ef41Sopenharmony_ci    });
1061cb0ef41Sopenharmony_ci}, 'piping through transferred transforms should work');
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci</script>
109