11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst {
71cb0ef41Sopenharmony_ci  readFileSync,
81cb0ef41Sopenharmony_ci} = require('fs');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst {
111cb0ef41Sopenharmony_ci  open,
121cb0ef41Sopenharmony_ci} = require('fs/promises');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst check = readFileSync(__filename, { encoding: 'utf8' });
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// Make sure the ReadableStream works...
171cb0ef41Sopenharmony_ci(async () => {
181cb0ef41Sopenharmony_ci  const dec = new TextDecoder();
191cb0ef41Sopenharmony_ci  const file = await open(__filename);
201cb0ef41Sopenharmony_ci  let data = '';
211cb0ef41Sopenharmony_ci  for await (const chunk of file.readableWebStream())
221cb0ef41Sopenharmony_ci    data += dec.decode(chunk);
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci  assert.strictEqual(check, data);
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  assert.throws(() => file.readableWebStream(), {
271cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci  await file.close();
311cb0ef41Sopenharmony_ci})().then(common.mustCall());
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci// Make sure that acquiring a ReadableStream fails if the
341cb0ef41Sopenharmony_ci// FileHandle is already closed.
351cb0ef41Sopenharmony_ci(async () => {
361cb0ef41Sopenharmony_ci  const file = await open(__filename);
371cb0ef41Sopenharmony_ci  await file.close();
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  assert.throws(() => file.readableWebStream(), {
401cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci})().then(common.mustCall());
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci// Make sure that acquiring a ReadableStream fails if the
451cb0ef41Sopenharmony_ci// FileHandle is already closing.
461cb0ef41Sopenharmony_ci(async () => {
471cb0ef41Sopenharmony_ci  const file = await open(__filename);
481cb0ef41Sopenharmony_ci  file.close();
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  assert.throws(() => file.readableWebStream(), {
511cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
521cb0ef41Sopenharmony_ci  });
531cb0ef41Sopenharmony_ci})().then(common.mustCall());
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci// Make sure the ReadableStream is closed when the underlying
561cb0ef41Sopenharmony_ci// FileHandle is closed.
571cb0ef41Sopenharmony_ci(async () => {
581cb0ef41Sopenharmony_ci  const file = await open(__filename);
591cb0ef41Sopenharmony_ci  const readable = file.readableWebStream();
601cb0ef41Sopenharmony_ci  const reader = readable.getReader();
611cb0ef41Sopenharmony_ci  file.close();
621cb0ef41Sopenharmony_ci  await reader.closed;
631cb0ef41Sopenharmony_ci})().then(common.mustCall());
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci// Make sure the ReadableStream is closed when the underlying
661cb0ef41Sopenharmony_ci// FileHandle is closed.
671cb0ef41Sopenharmony_ci(async () => {
681cb0ef41Sopenharmony_ci  const file = await open(__filename);
691cb0ef41Sopenharmony_ci  const readable = file.readableWebStream();
701cb0ef41Sopenharmony_ci  file.close();
711cb0ef41Sopenharmony_ci  const reader = readable.getReader();
721cb0ef41Sopenharmony_ci  await reader.closed;
731cb0ef41Sopenharmony_ci})().then(common.mustCall());
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci// Make sure that the FileHandle is properly marked "in use"
761cb0ef41Sopenharmony_ci// when a ReadableStream has been acquired for it.
771cb0ef41Sopenharmony_ci(async () => {
781cb0ef41Sopenharmony_ci  const file = await open(__filename);
791cb0ef41Sopenharmony_ci  file.readableWebStream();
801cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
811cb0ef41Sopenharmony_ci  mc.port1.onmessage = common.mustNotCall();
821cb0ef41Sopenharmony_ci  assert.throws(() => mc.port2.postMessage(file, [file]), {
831cb0ef41Sopenharmony_ci    code: 25,
841cb0ef41Sopenharmony_ci    name: 'DataCloneError',
851cb0ef41Sopenharmony_ci  });
861cb0ef41Sopenharmony_ci  mc.port1.close();
871cb0ef41Sopenharmony_ci  await file.close();
881cb0ef41Sopenharmony_ci})().then(common.mustCall());
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci// Make sure 'bytes' stream works
911cb0ef41Sopenharmony_ci(async () => {
921cb0ef41Sopenharmony_ci  const file = await open(__filename);
931cb0ef41Sopenharmony_ci  const dec = new TextDecoder();
941cb0ef41Sopenharmony_ci  const readable = file.readableWebStream({ type: 'bytes' });
951cb0ef41Sopenharmony_ci  const reader = readable.getReader({ mode: 'byob' });
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  let data = '';
981cb0ef41Sopenharmony_ci  let result;
991cb0ef41Sopenharmony_ci  do {
1001cb0ef41Sopenharmony_ci    const buff = new ArrayBuffer(100);
1011cb0ef41Sopenharmony_ci    result = await reader.read(new DataView(buff));
1021cb0ef41Sopenharmony_ci    if (result.value !== undefined) {
1031cb0ef41Sopenharmony_ci      data += dec.decode(result.value);
1041cb0ef41Sopenharmony_ci      assert.ok(result.value.byteLength <= 100);
1051cb0ef41Sopenharmony_ci    }
1061cb0ef41Sopenharmony_ci  } while (!result.done);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  assert.strictEqual(check, data);
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci  assert.throws(() => file.readableWebStream(), {
1111cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
1121cb0ef41Sopenharmony_ci  });
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ci  await file.close();
1151cb0ef41Sopenharmony_ci})().then(common.mustCall());
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci// Make sure that acquiring a ReadableStream 'bytes' stream
1181cb0ef41Sopenharmony_ci// fails if the FileHandle is already closed.
1191cb0ef41Sopenharmony_ci(async () => {
1201cb0ef41Sopenharmony_ci  const file = await open(__filename);
1211cb0ef41Sopenharmony_ci  await file.close();
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  assert.throws(() => file.readableWebStream({ type: 'bytes' }), {
1241cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
1251cb0ef41Sopenharmony_ci  });
1261cb0ef41Sopenharmony_ci})().then(common.mustCall());
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ci// Make sure that acquiring a ReadableStream 'bytes' stream
1291cb0ef41Sopenharmony_ci// fails if the FileHandle is already closing.
1301cb0ef41Sopenharmony_ci(async () => {
1311cb0ef41Sopenharmony_ci  const file = await open(__filename);
1321cb0ef41Sopenharmony_ci  file.close();
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ci  assert.throws(() => file.readableWebStream({ type: 'bytes' }), {
1351cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_STATE',
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci})().then(common.mustCall());
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci// Make sure the 'bytes' ReadableStream is closed when the underlying
1401cb0ef41Sopenharmony_ci// FileHandle is closed.
1411cb0ef41Sopenharmony_ci(async () => {
1421cb0ef41Sopenharmony_ci  const file = await open(__filename);
1431cb0ef41Sopenharmony_ci  const readable = file.readableWebStream({ type: 'bytes' });
1441cb0ef41Sopenharmony_ci  const reader = readable.getReader({ mode: 'byob' });
1451cb0ef41Sopenharmony_ci  file.close();
1461cb0ef41Sopenharmony_ci  await reader.closed;
1471cb0ef41Sopenharmony_ci})().then(common.mustCall());
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci// Make sure the 'bytes' ReadableStream is closed when the underlying
1501cb0ef41Sopenharmony_ci// FileHandle is closed.
1511cb0ef41Sopenharmony_ci(async () => {
1521cb0ef41Sopenharmony_ci  const file = await open(__filename);
1531cb0ef41Sopenharmony_ci  const readable = file.readableWebStream({ type: 'bytes' });
1541cb0ef41Sopenharmony_ci  file.close();
1551cb0ef41Sopenharmony_ci  const reader = readable.getReader({ mode: 'byob' });
1561cb0ef41Sopenharmony_ci  await reader.closed;
1571cb0ef41Sopenharmony_ci})().then(common.mustCall());
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci// Make sure that the FileHandle is properly marked "in use"
1601cb0ef41Sopenharmony_ci// when a 'bytes' ReadableStream has been acquired for it.
1611cb0ef41Sopenharmony_ci(async () => {
1621cb0ef41Sopenharmony_ci  const file = await open(__filename);
1631cb0ef41Sopenharmony_ci  file.readableWebStream({ type: 'bytes' });
1641cb0ef41Sopenharmony_ci  const mc = new MessageChannel();
1651cb0ef41Sopenharmony_ci  mc.port1.onmessage = common.mustNotCall();
1661cb0ef41Sopenharmony_ci  assert.throws(() => mc.port2.postMessage(file, [file]), {
1671cb0ef41Sopenharmony_ci    code: 25,
1681cb0ef41Sopenharmony_ci    name: 'DataCloneError',
1691cb0ef41Sopenharmony_ci  });
1701cb0ef41Sopenharmony_ci  mc.port1.close();
1711cb0ef41Sopenharmony_ci  await file.close();
1721cb0ef41Sopenharmony_ci})().then(common.mustCall());
173