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 old node. 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() { 21 fs.open(this.path, this.flags, this.mode, (er, fd) => { 22 if (er) { 23 if (this.autoClose) { 24 this.destroy(); 25 } 26 this.emit('error', er); 27 return; 28 } 29 30 this.fd = fd; 31 this.emit('open', fd); 32 this.emit('ready'); 33 }); 34 }); 35 36 let readyCalled = false; 37 let ticked = false; 38 const r = new ReadStream(fixtures.path('x.txt')) 39 .on('ready', common.mustCall(() => { 40 readyCalled = true; 41 // Make sure 'ready' is emitted in same tick as 'open'. 42 assert.strictEqual(ticked, false); 43 })) 44 .on('error', common.mustNotCall()) 45 .on('open', common.mustCall((fd) => { 46 process.nextTick(() => { 47 ticked = true; 48 r.destroy(); 49 }); 50 assert.strictEqual(readyCalled, false); 51 assert.strictEqual(fd, r.fd); 52 })); 53} 54 55{ 56 // Compat with old node. 57 58 function WriteStream(...args) { 59 fs.WriteStream.call(this, ...args); 60 } 61 Object.setPrototypeOf(WriteStream.prototype, fs.WriteStream.prototype); 62 Object.setPrototypeOf(WriteStream, fs.WriteStream); 63 64 WriteStream.prototype.open = common.mustCall(function() { 65 fs.open(this.path, this.flags, this.mode, (er, fd) => { 66 if (er) { 67 if (this.autoClose) { 68 this.destroy(); 69 } 70 this.emit('error', er); 71 return; 72 } 73 74 this.fd = fd; 75 this.emit('open', fd); 76 this.emit('ready'); 77 }); 78 }); 79 80 let readyCalled = false; 81 let ticked = false; 82 const w = new WriteStream(`${tmpdir.path}/dummy`) 83 .on('ready', common.mustCall(() => { 84 readyCalled = true; 85 // Make sure 'ready' is emitted in same tick as 'open'. 86 assert.strictEqual(ticked, false); 87 })) 88 .on('error', common.mustNotCall()) 89 .on('open', common.mustCall((fd) => { 90 process.nextTick(() => { 91 ticked = true; 92 w.destroy(); 93 }); 94 assert.strictEqual(readyCalled, false); 95 assert.strictEqual(fd, w.fd); 96 })); 97} 98