11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst fs = require('fs');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst fixtures = require('../common/fixtures');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
91cb0ef41Sopenharmony_citmpdir.refresh();
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci{
121cb0ef41Sopenharmony_ci  // Compat with old node.
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci  function ReadStream(...args) {
151cb0ef41Sopenharmony_ci    fs.ReadStream.call(this, ...args);
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci  Object.setPrototypeOf(ReadStream.prototype, fs.ReadStream.prototype);
181cb0ef41Sopenharmony_ci  Object.setPrototypeOf(ReadStream, fs.ReadStream);
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  ReadStream.prototype.open = common.mustCall(function() {
211cb0ef41Sopenharmony_ci    fs.open(this.path, this.flags, this.mode, (er, fd) => {
221cb0ef41Sopenharmony_ci      if (er) {
231cb0ef41Sopenharmony_ci        if (this.autoClose) {
241cb0ef41Sopenharmony_ci          this.destroy();
251cb0ef41Sopenharmony_ci        }
261cb0ef41Sopenharmony_ci        this.emit('error', er);
271cb0ef41Sopenharmony_ci        return;
281cb0ef41Sopenharmony_ci      }
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci      this.fd = fd;
311cb0ef41Sopenharmony_ci      this.emit('open', fd);
321cb0ef41Sopenharmony_ci      this.emit('ready');
331cb0ef41Sopenharmony_ci    });
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  let readyCalled = false;
371cb0ef41Sopenharmony_ci  let ticked = false;
381cb0ef41Sopenharmony_ci  const r = new ReadStream(fixtures.path('x.txt'))
391cb0ef41Sopenharmony_ci    .on('ready', common.mustCall(() => {
401cb0ef41Sopenharmony_ci      readyCalled = true;
411cb0ef41Sopenharmony_ci      // Make sure 'ready' is emitted in same tick as 'open'.
421cb0ef41Sopenharmony_ci      assert.strictEqual(ticked, false);
431cb0ef41Sopenharmony_ci    }))
441cb0ef41Sopenharmony_ci    .on('error', common.mustNotCall())
451cb0ef41Sopenharmony_ci    .on('open', common.mustCall((fd) => {
461cb0ef41Sopenharmony_ci      process.nextTick(() => {
471cb0ef41Sopenharmony_ci        ticked = true;
481cb0ef41Sopenharmony_ci        r.destroy();
491cb0ef41Sopenharmony_ci      });
501cb0ef41Sopenharmony_ci      assert.strictEqual(readyCalled, false);
511cb0ef41Sopenharmony_ci      assert.strictEqual(fd, r.fd);
521cb0ef41Sopenharmony_ci    }));
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci{
561cb0ef41Sopenharmony_ci  // Compat with old node.
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  function WriteStream(...args) {
591cb0ef41Sopenharmony_ci    fs.WriteStream.call(this, ...args);
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci  Object.setPrototypeOf(WriteStream.prototype, fs.WriteStream.prototype);
621cb0ef41Sopenharmony_ci  Object.setPrototypeOf(WriteStream, fs.WriteStream);
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci  WriteStream.prototype.open = common.mustCall(function() {
651cb0ef41Sopenharmony_ci    fs.open(this.path, this.flags, this.mode, (er, fd) => {
661cb0ef41Sopenharmony_ci      if (er) {
671cb0ef41Sopenharmony_ci        if (this.autoClose) {
681cb0ef41Sopenharmony_ci          this.destroy();
691cb0ef41Sopenharmony_ci        }
701cb0ef41Sopenharmony_ci        this.emit('error', er);
711cb0ef41Sopenharmony_ci        return;
721cb0ef41Sopenharmony_ci      }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci      this.fd = fd;
751cb0ef41Sopenharmony_ci      this.emit('open', fd);
761cb0ef41Sopenharmony_ci      this.emit('ready');
771cb0ef41Sopenharmony_ci    });
781cb0ef41Sopenharmony_ci  });
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci  let readyCalled = false;
811cb0ef41Sopenharmony_ci  let ticked = false;
821cb0ef41Sopenharmony_ci  const w = new WriteStream(`${tmpdir.path}/dummy`)
831cb0ef41Sopenharmony_ci    .on('ready', common.mustCall(() => {
841cb0ef41Sopenharmony_ci      readyCalled = true;
851cb0ef41Sopenharmony_ci      // Make sure 'ready' is emitted in same tick as 'open'.
861cb0ef41Sopenharmony_ci      assert.strictEqual(ticked, false);
871cb0ef41Sopenharmony_ci    }))
881cb0ef41Sopenharmony_ci    .on('error', common.mustNotCall())
891cb0ef41Sopenharmony_ci    .on('open', common.mustCall((fd) => {
901cb0ef41Sopenharmony_ci      process.nextTick(() => {
911cb0ef41Sopenharmony_ci        ticked = true;
921cb0ef41Sopenharmony_ci        w.destroy();
931cb0ef41Sopenharmony_ci      });
941cb0ef41Sopenharmony_ci      assert.strictEqual(readyCalled, false);
951cb0ef41Sopenharmony_ci      assert.strictEqual(fd, w.fd);
961cb0ef41Sopenharmony_ci    }));
971cb0ef41Sopenharmony_ci}
98