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_test.txt');
81cb0ef41Sopenharmony_ciconst input = 'hello world';
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_citmpdir.refresh();
111cb0ef41Sopenharmony_cifs.writeFileSync(file, input);
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifs.promises.open(file, 'r').then((handle) => {
141cb0ef41Sopenharmony_ci  handle.on('close', common.mustCall());
151cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: handle });
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci  let output = '';
181cb0ef41Sopenharmony_ci  stream.on('data', common.mustCallAtLeast((data) => {
191cb0ef41Sopenharmony_ci    output += data;
201cb0ef41Sopenharmony_ci  }));
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  stream.on('end', common.mustCall(() => {
231cb0ef41Sopenharmony_ci    assert.strictEqual(output, input);
241cb0ef41Sopenharmony_ci  }));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
271cb0ef41Sopenharmony_ci}).then(common.mustCall());
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cifs.promises.open(file, 'r').then((handle) => {
301cb0ef41Sopenharmony_ci  handle.on('close', common.mustCall());
311cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: handle });
321cb0ef41Sopenharmony_ci  stream.on('data', common.mustNotCall());
331cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  return handle.close();
361cb0ef41Sopenharmony_ci}).then(common.mustCall());
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_cifs.promises.open(file, 'r').then((handle) => {
391cb0ef41Sopenharmony_ci  handle.on('close', common.mustCall());
401cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: handle });
411cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  stream.on('data', common.mustCall(() => {
441cb0ef41Sopenharmony_ci    handle.close();
451cb0ef41Sopenharmony_ci  }));
461cb0ef41Sopenharmony_ci}).then(common.mustCall());
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_cifs.promises.open(file, 'r').then((handle) => {
491cb0ef41Sopenharmony_ci  handle.on('close', common.mustCall());
501cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: handle });
511cb0ef41Sopenharmony_ci  stream.on('close', common.mustCall());
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  stream.close();
541cb0ef41Sopenharmony_ci}).then(common.mustCall());
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_cifs.promises.open(file, 'r').then((handle) => {
571cb0ef41Sopenharmony_ci  assert.throws(() => {
581cb0ef41Sopenharmony_ci    fs.createReadStream(null, { fd: handle, fs });
591cb0ef41Sopenharmony_ci  }, {
601cb0ef41Sopenharmony_ci    code: 'ERR_METHOD_NOT_IMPLEMENTED',
611cb0ef41Sopenharmony_ci    name: 'Error',
621cb0ef41Sopenharmony_ci    message: 'The FileHandle with fs method is not implemented'
631cb0ef41Sopenharmony_ci  });
641cb0ef41Sopenharmony_ci  return handle.close();
651cb0ef41Sopenharmony_ci}).then(common.mustCall());
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_cifs.promises.open(file, 'r').then((handle) => {
681cb0ef41Sopenharmony_ci  const { read: originalReadFunction } = handle;
691cb0ef41Sopenharmony_ci  handle.read = common.mustCallAtLeast(function read() {
701cb0ef41Sopenharmony_ci    return Reflect.apply(originalReadFunction, this, arguments);
711cb0ef41Sopenharmony_ci  });
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  const stream = fs.createReadStream(null, { fd: handle });
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  let output = '';
761cb0ef41Sopenharmony_ci  stream.on('data', common.mustCallAtLeast((data) => {
771cb0ef41Sopenharmony_ci    output += data;
781cb0ef41Sopenharmony_ci  }));
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  stream.on('end', common.mustCall(() => {
811cb0ef41Sopenharmony_ci    assert.strictEqual(output, input);
821cb0ef41Sopenharmony_ci  }));
831cb0ef41Sopenharmony_ci}).then(common.mustCall());
84