11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci(() => {
41cb0ef41Sopenharmony_ci  // Create a ReadableStream that will pass the tests in
51cb0ef41Sopenharmony_ci  // testTransferredReadableStream(), below.
61cb0ef41Sopenharmony_ci  function createOriginalReadableStream() {
71cb0ef41Sopenharmony_ci    return new ReadableStream({
81cb0ef41Sopenharmony_ci      start(controller) {
91cb0ef41Sopenharmony_ci        controller.enqueue('a');
101cb0ef41Sopenharmony_ci        controller.close();
111cb0ef41Sopenharmony_ci      }
121cb0ef41Sopenharmony_ci    });
131cb0ef41Sopenharmony_ci  }
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  // Common tests to roughly determine that |rs| is a correctly transferred
161cb0ef41Sopenharmony_ci  // version of a stream created by createOriginalReadableStream().
171cb0ef41Sopenharmony_ci  function testTransferredReadableStream(rs) {
181cb0ef41Sopenharmony_ci    assert_equals(rs.constructor, ReadableStream,
191cb0ef41Sopenharmony_ci                  'rs should be a ReadableStream in this realm');
201cb0ef41Sopenharmony_ci    assert_true(rs instanceof ReadableStream,
211cb0ef41Sopenharmony_ci                'instanceof check should pass');
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci    // Perform a brand-check on |rs| in the process of calling getReader().
241cb0ef41Sopenharmony_ci    const reader = ReadableStream.prototype.getReader.call(rs);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci    return reader.read().then(({value, done}) => {
271cb0ef41Sopenharmony_ci      assert_false(done, 'done should be false');
281cb0ef41Sopenharmony_ci      assert_equals(value, 'a', 'value should be "a"');
291cb0ef41Sopenharmony_ci      return reader.read();
301cb0ef41Sopenharmony_ci    }).then(({done}) => {
311cb0ef41Sopenharmony_ci      assert_true(done, 'done should be true');
321cb0ef41Sopenharmony_ci    });
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  function testMessage(msg) {
361cb0ef41Sopenharmony_ci    assert_array_equals(msg.ports, [], 'there should be no ports in the event');
371cb0ef41Sopenharmony_ci    return testTransferredReadableStream(msg.data);
381cb0ef41Sopenharmony_ci  }
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  function testMessageEvent(target) {
411cb0ef41Sopenharmony_ci    return new Promise((resolve, reject) => {
421cb0ef41Sopenharmony_ci      target.addEventListener('message', ev => {
431cb0ef41Sopenharmony_ci        try {
441cb0ef41Sopenharmony_ci          resolve(testMessage(ev));
451cb0ef41Sopenharmony_ci        } catch (e) {
461cb0ef41Sopenharmony_ci          reject(e);
471cb0ef41Sopenharmony_ci        }
481cb0ef41Sopenharmony_ci      }, {once: true});
491cb0ef41Sopenharmony_ci    });
501cb0ef41Sopenharmony_ci  }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  function testMessageEventOrErrorMessage(target) {
531cb0ef41Sopenharmony_ci    return new Promise((resolve, reject) => {
541cb0ef41Sopenharmony_ci      target.addEventListener('message', ev => {
551cb0ef41Sopenharmony_ci        if (typeof ev.data === 'string') {
561cb0ef41Sopenharmony_ci          // Assume it's an error message and reject with it.
571cb0ef41Sopenharmony_ci          reject(ev.data);
581cb0ef41Sopenharmony_ci          return;
591cb0ef41Sopenharmony_ci        }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci        try {
621cb0ef41Sopenharmony_ci          resolve(testMessage(ev));
631cb0ef41Sopenharmony_ci        } catch (e) {
641cb0ef41Sopenharmony_ci          reject(e);
651cb0ef41Sopenharmony_ci        }
661cb0ef41Sopenharmony_ci      }, {once: true});
671cb0ef41Sopenharmony_ci    });
681cb0ef41Sopenharmony_ci  }
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  function checkTestResults(target) {
711cb0ef41Sopenharmony_ci    return new Promise((resolve, reject) => {
721cb0ef41Sopenharmony_ci      target.onmessage = msg => {
731cb0ef41Sopenharmony_ci        // testharness.js sends us objects which we need to ignore.
741cb0ef41Sopenharmony_ci        if (typeof msg.data !== 'string')
751cb0ef41Sopenharmony_ci        return;
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci        if (msg.data === 'OK') {
781cb0ef41Sopenharmony_ci          resolve();
791cb0ef41Sopenharmony_ci        } else {
801cb0ef41Sopenharmony_ci          reject(msg.data);
811cb0ef41Sopenharmony_ci        }
821cb0ef41Sopenharmony_ci      };
831cb0ef41Sopenharmony_ci    });
841cb0ef41Sopenharmony_ci  }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  // These tests assume that a transferred ReadableStream will behave the same
871cb0ef41Sopenharmony_ci  // regardless of how it was transferred. This enables us to simply transfer the
881cb0ef41Sopenharmony_ci  // stream to ourselves.
891cb0ef41Sopenharmony_ci  function createTransferredReadableStream(underlyingSource) {
901cb0ef41Sopenharmony_ci    const original = new ReadableStream(underlyingSource);
911cb0ef41Sopenharmony_ci    const promise = new Promise((resolve, reject) => {
921cb0ef41Sopenharmony_ci      addEventListener('message', msg => {
931cb0ef41Sopenharmony_ci        const rs = msg.data;
941cb0ef41Sopenharmony_ci        if (rs instanceof ReadableStream) {
951cb0ef41Sopenharmony_ci          resolve(rs);
961cb0ef41Sopenharmony_ci        } else {
971cb0ef41Sopenharmony_ci          reject(new Error(`what is this thing: "${rs}"?`));
981cb0ef41Sopenharmony_ci        }
991cb0ef41Sopenharmony_ci      }, {once: true});
1001cb0ef41Sopenharmony_ci    });
1011cb0ef41Sopenharmony_ci    postMessage(original, '*', [original]);
1021cb0ef41Sopenharmony_ci    return promise;
1031cb0ef41Sopenharmony_ci  }
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_ci  function recordingTransferredReadableStream(underlyingSource, strategy) {
1061cb0ef41Sopenharmony_ci    const original = recordingReadableStream(underlyingSource, strategy);
1071cb0ef41Sopenharmony_ci    const promise = new Promise((resolve, reject) => {
1081cb0ef41Sopenharmony_ci      addEventListener('message', msg => {
1091cb0ef41Sopenharmony_ci        const rs = msg.data;
1101cb0ef41Sopenharmony_ci        if (rs instanceof ReadableStream) {
1111cb0ef41Sopenharmony_ci          rs.events = original.events;
1121cb0ef41Sopenharmony_ci          rs.eventsWithoutPulls = original.eventsWithoutPulls;
1131cb0ef41Sopenharmony_ci          rs.controller = original.controller;
1141cb0ef41Sopenharmony_ci          resolve(rs);
1151cb0ef41Sopenharmony_ci        } else {
1161cb0ef41Sopenharmony_ci          reject(new Error(`what is this thing: "${rs}"?`));
1171cb0ef41Sopenharmony_ci        }
1181cb0ef41Sopenharmony_ci      }, {once: true});
1191cb0ef41Sopenharmony_ci    });
1201cb0ef41Sopenharmony_ci    postMessage(original, '*', [original]);
1211cb0ef41Sopenharmony_ci    return promise;
1221cb0ef41Sopenharmony_ci  }
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  self.createOriginalReadableStream = createOriginalReadableStream;
1251cb0ef41Sopenharmony_ci  self.testMessage = testMessage;
1261cb0ef41Sopenharmony_ci  self.testMessageEvent = testMessageEvent;
1271cb0ef41Sopenharmony_ci  self.testMessageEventOrErrorMessage = testMessageEventOrErrorMessage;
1281cb0ef41Sopenharmony_ci  self.checkTestResults = checkTestResults;
1291cb0ef41Sopenharmony_ci  self.createTransferredReadableStream = createTransferredReadableStream;
1301cb0ef41Sopenharmony_ci  self.recordingTransferredReadableStream = recordingTransferredReadableStream;
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci})();
133