11cb0ef41Sopenharmony_ci// META: global=window,worker
21cb0ef41Sopenharmony_ci// META: script=../resources/test-utils.js
31cb0ef41Sopenharmony_ci// META: script=../resources/rs-test-templates.js
41cb0ef41Sopenharmony_ci'use strict';
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// Run the readable stream test templates against readable streams created directly using the constructor
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst theError = { name: 'boo!' };
91cb0ef41Sopenharmony_ciconst chunks = ['a', 'b'];
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_citemplatedRSEmpty('ReadableStream (empty)', () => {
121cb0ef41Sopenharmony_ci  return new ReadableStream();
131cb0ef41Sopenharmony_ci});
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_citemplatedRSEmptyReader('ReadableStream (empty) reader', () => {
161cb0ef41Sopenharmony_ci  return streamAndDefaultReader(new ReadableStream());
171cb0ef41Sopenharmony_ci});
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_citemplatedRSClosed('ReadableStream (closed via call in start)', () => {
201cb0ef41Sopenharmony_ci  return new ReadableStream({
211cb0ef41Sopenharmony_ci    start(c) {
221cb0ef41Sopenharmony_ci      c.close();
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci});
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_citemplatedRSClosedReader('ReadableStream reader (closed before getting reader)', () => {
281cb0ef41Sopenharmony_ci  let controller;
291cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
301cb0ef41Sopenharmony_ci    start(c) {
311cb0ef41Sopenharmony_ci      controller = c;
321cb0ef41Sopenharmony_ci    }
331cb0ef41Sopenharmony_ci  });
341cb0ef41Sopenharmony_ci  controller.close();
351cb0ef41Sopenharmony_ci  const result = streamAndDefaultReader(stream);
361cb0ef41Sopenharmony_ci  return result;
371cb0ef41Sopenharmony_ci});
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_citemplatedRSClosedReader('ReadableStream reader (closed after getting reader)', () => {
401cb0ef41Sopenharmony_ci  let controller;
411cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
421cb0ef41Sopenharmony_ci    start(c) {
431cb0ef41Sopenharmony_ci      controller = c;
441cb0ef41Sopenharmony_ci    }
451cb0ef41Sopenharmony_ci  });
461cb0ef41Sopenharmony_ci  const result = streamAndDefaultReader(stream);
471cb0ef41Sopenharmony_ci  controller.close();
481cb0ef41Sopenharmony_ci  return result;
491cb0ef41Sopenharmony_ci});
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_citemplatedRSClosed('ReadableStream (closed via cancel)', () => {
521cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
531cb0ef41Sopenharmony_ci  stream.cancel();
541cb0ef41Sopenharmony_ci  return stream;
551cb0ef41Sopenharmony_ci});
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_citemplatedRSClosedReader('ReadableStream reader (closed via cancel after getting reader)', () => {
581cb0ef41Sopenharmony_ci  const stream = new ReadableStream();
591cb0ef41Sopenharmony_ci  const result = streamAndDefaultReader(stream);
601cb0ef41Sopenharmony_ci  result.reader.cancel();
611cb0ef41Sopenharmony_ci  return result;
621cb0ef41Sopenharmony_ci});
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_citemplatedRSErrored('ReadableStream (errored via call in start)', () => {
651cb0ef41Sopenharmony_ci  return new ReadableStream({
661cb0ef41Sopenharmony_ci    start(c) {
671cb0ef41Sopenharmony_ci      c.error(theError);
681cb0ef41Sopenharmony_ci    }
691cb0ef41Sopenharmony_ci  });
701cb0ef41Sopenharmony_ci}, theError);
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_citemplatedRSErroredSyncOnly('ReadableStream (errored via call in start)', () => {
731cb0ef41Sopenharmony_ci  return new ReadableStream({
741cb0ef41Sopenharmony_ci    start(c) {
751cb0ef41Sopenharmony_ci      c.error(theError);
761cb0ef41Sopenharmony_ci    }
771cb0ef41Sopenharmony_ci  });
781cb0ef41Sopenharmony_ci}, theError);
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_citemplatedRSErrored('ReadableStream (errored via returning a rejected promise in start)', () => {
811cb0ef41Sopenharmony_ci  return new ReadableStream({
821cb0ef41Sopenharmony_ci    start() {
831cb0ef41Sopenharmony_ci      return Promise.reject(theError);
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci  });
861cb0ef41Sopenharmony_ci}, theError);
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_citemplatedRSErroredReader('ReadableStream (errored via returning a rejected promise in start) reader', () => {
891cb0ef41Sopenharmony_ci  return streamAndDefaultReader(new ReadableStream({
901cb0ef41Sopenharmony_ci    start() {
911cb0ef41Sopenharmony_ci      return Promise.reject(theError);
921cb0ef41Sopenharmony_ci    }
931cb0ef41Sopenharmony_ci  }));
941cb0ef41Sopenharmony_ci}, theError);
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_citemplatedRSErroredReader('ReadableStream reader (errored before getting reader)', () => {
971cb0ef41Sopenharmony_ci  let controller;
981cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
991cb0ef41Sopenharmony_ci    start(c) {
1001cb0ef41Sopenharmony_ci      controller = c;
1011cb0ef41Sopenharmony_ci    }
1021cb0ef41Sopenharmony_ci  });
1031cb0ef41Sopenharmony_ci  controller.error(theError);
1041cb0ef41Sopenharmony_ci  return streamAndDefaultReader(stream);
1051cb0ef41Sopenharmony_ci}, theError);
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_citemplatedRSErroredReader('ReadableStream reader (errored after getting reader)', () => {
1081cb0ef41Sopenharmony_ci  let controller;
1091cb0ef41Sopenharmony_ci  const result = streamAndDefaultReader(new ReadableStream({
1101cb0ef41Sopenharmony_ci    start(c) {
1111cb0ef41Sopenharmony_ci      controller = c;
1121cb0ef41Sopenharmony_ci    }
1131cb0ef41Sopenharmony_ci  }));
1141cb0ef41Sopenharmony_ci  controller.error(theError);
1151cb0ef41Sopenharmony_ci  return result;
1161cb0ef41Sopenharmony_ci}, theError);
1171cb0ef41Sopenharmony_ci
1181cb0ef41Sopenharmony_citemplatedRSTwoChunksOpenReader('ReadableStream (two chunks enqueued, still open) reader', () => {
1191cb0ef41Sopenharmony_ci  return streamAndDefaultReader(new ReadableStream({
1201cb0ef41Sopenharmony_ci    start(c) {
1211cb0ef41Sopenharmony_ci      c.enqueue(chunks[0]);
1221cb0ef41Sopenharmony_ci      c.enqueue(chunks[1]);
1231cb0ef41Sopenharmony_ci    }
1241cb0ef41Sopenharmony_ci  }));
1251cb0ef41Sopenharmony_ci}, chunks);
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_citemplatedRSTwoChunksClosedReader('ReadableStream (two chunks enqueued, then closed) reader', () => {
1281cb0ef41Sopenharmony_ci  let doClose;
1291cb0ef41Sopenharmony_ci  const stream = new ReadableStream({
1301cb0ef41Sopenharmony_ci    start(c) {
1311cb0ef41Sopenharmony_ci      c.enqueue(chunks[0]);
1321cb0ef41Sopenharmony_ci      c.enqueue(chunks[1]);
1331cb0ef41Sopenharmony_ci      doClose = c.close.bind(c);
1341cb0ef41Sopenharmony_ci    }
1351cb0ef41Sopenharmony_ci  });
1361cb0ef41Sopenharmony_ci  const result = streamAndDefaultReader(stream);
1371cb0ef41Sopenharmony_ci  doClose();
1381cb0ef41Sopenharmony_ci  return result;
1391cb0ef41Sopenharmony_ci}, chunks);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_cifunction streamAndDefaultReader(stream) {
1421cb0ef41Sopenharmony_ci  return { stream, reader: stream.getReader() };
1431cb0ef41Sopenharmony_ci}
144