11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst fs = require('fs');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
71cb0ef41Sopenharmony_ciconst file = path.join(tmpdir.path, 'read_stream_filehandle_worker.txt');
81cb0ef41Sopenharmony_ciconst input = 'hello world';
91cb0ef41Sopenharmony_ciconst { Worker, isMainThread, workerData } = require('worker_threads');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciif (isMainThread || !workerData) {
121cb0ef41Sopenharmony_ci  tmpdir.refresh();
131cb0ef41Sopenharmony_ci  fs.writeFileSync(file, input);
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci  fs.promises.open(file, 'r').then((handle) => {
161cb0ef41Sopenharmony_ci    handle.on('close', common.mustNotCall());
171cb0ef41Sopenharmony_ci    new Worker(__filename, {
181cb0ef41Sopenharmony_ci      workerData: { handle },
191cb0ef41Sopenharmony_ci      transferList: [handle]
201cb0ef41Sopenharmony_ci    });
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci  fs.promises.open(file, 'r').then(async (handle) => {
231cb0ef41Sopenharmony_ci    try {
241cb0ef41Sopenharmony_ci      fs.createReadStream(null, { fd: handle });
251cb0ef41Sopenharmony_ci      assert.throws(() => {
261cb0ef41Sopenharmony_ci        new Worker(__filename, {
271cb0ef41Sopenharmony_ci          workerData: { handle },
281cb0ef41Sopenharmony_ci          transferList: [handle]
291cb0ef41Sopenharmony_ci        });
301cb0ef41Sopenharmony_ci      }, {
311cb0ef41Sopenharmony_ci        code: 25,
321cb0ef41Sopenharmony_ci        name: 'DataCloneError',
331cb0ef41Sopenharmony_ci      });
341cb0ef41Sopenharmony_ci    } finally {
351cb0ef41Sopenharmony_ci      await handle.close();
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci  });
381cb0ef41Sopenharmony_ci} else {
391cb0ef41Sopenharmony_ci  let output = '';
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci  const handle = workerData.handle;
421cb0ef41Sopenharmony_ci  handle.on('close', common.mustCall());
431cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: handle });
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci  stream.on('data', common.mustCallAtLeast((data) => {
461cb0ef41Sopenharmony_ci    output += data;
471cb0ef41Sopenharmony_ci  }));
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  stream.on('end', common.mustCall(() => {
501cb0ef41Sopenharmony_ci    handle.close();
511cb0ef41Sopenharmony_ci    assert.strictEqual(output, input);
521cb0ef41Sopenharmony_ci  }));
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
551cb0ef41Sopenharmony_ci}
56