1'use strict'; 2 3const common = require('../common'); 4const fs = require('fs'); 5const assert = require('assert'); 6const fixtures = require('../common/fixtures'); 7 8const tmpdir = require('../common/tmpdir'); 9tmpdir.refresh(); 10 11{ 12 // Compat with graceful-fs. 13 14 function ReadStream(...args) { 15 fs.ReadStream.call(this, ...args); 16 } 17 Object.setPrototypeOf(ReadStream.prototype, fs.ReadStream.prototype); 18 Object.setPrototypeOf(ReadStream, fs.ReadStream); 19 20 ReadStream.prototype.open = common.mustCall(function ReadStream$open() { 21 const that = this; 22 fs.open(that.path, that.flags, that.mode, (err, fd) => { 23 if (err) { 24 if (that.autoClose) 25 that.destroy(); 26 27 that.emit('error', err); 28 } else { 29 that.fd = fd; 30 that.emit('open', fd); 31 that.read(); 32 } 33 }); 34 }); 35 36 const r = new ReadStream(fixtures.path('x.txt')) 37 .on('open', common.mustCall((fd) => { 38 assert.strictEqual(fd, r.fd); 39 r.destroy(); 40 })); 41} 42 43{ 44 // Compat with graceful-fs. 45 46 function WriteStream(...args) { 47 fs.WriteStream.call(this, ...args); 48 } 49 Object.setPrototypeOf(WriteStream.prototype, fs.WriteStream.prototype); 50 Object.setPrototypeOf(WriteStream, fs.WriteStream); 51 52 WriteStream.prototype.open = common.mustCall(function WriteStream$open() { 53 const that = this; 54 fs.open(that.path, that.flags, that.mode, function(err, fd) { 55 if (err) { 56 that.destroy(); 57 that.emit('error', err); 58 } else { 59 that.fd = fd; 60 that.emit('open', fd); 61 } 62 }); 63 }); 64 65 const w = new WriteStream(`${tmpdir.path}/dummy`) 66 .on('open', common.mustCall((fd) => { 67 assert.strictEqual(fd, w.fd); 68 w.destroy(); 69 })); 70} 71