Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
fix: revert "fs: stop lazy loading stream constructors"
Browse files Browse the repository at this point in the history
Fixes electron/electron#15543
by reverting 484140e.

This commit was added to retain backwards compatibility in the 10.x
branch and shouldn't be an issue in Node 11 / Electron based on 11.x.

More info can be found at nodejs/node#21489
  • Loading branch information
ckerr committed Nov 21, 2018
1 parent 4d44266 commit 1e7f829
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
60 changes: 53 additions & 7 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ const {
} = errors.codes;

const { FSReqWrap, statValues } = binding;
const { ReadStream, WriteStream } = require('internal/fs/streams');
const internalFS = require('internal/fs/utils');
const { getPathFromURL } = require('internal/url');
const internalUtil = require('internal/util');
Expand Down Expand Up @@ -94,6 +93,13 @@ let fs;
let promises;
let watchers;
let ReadFileContext;
let ReadStream;
let WriteStream;

// These have to be separate because of how graceful-fs happens to do it's
// monkeypatching.
let FileReadStream;
let FileWriteStream;

const isWindows = process.platform === 'win32';

Expand Down Expand Up @@ -1702,11 +1708,20 @@ function copyFileSync(src, dest, flags) {
handleErrorFromBinding(ctx);
}

function lazyLoadStreams() {
if (!ReadStream) {
({ ReadStream, WriteStream } = require('internal/fs/streams'));
[ FileReadStream, FileWriteStream ] = [ ReadStream, WriteStream ];
}
}

function createReadStream(path, options) {
lazyLoadStreams();
return new ReadStream(path, options);
}

function createWriteStream(path, options) {
lazyLoadStreams();
return new WriteStream(path, options);
}

Expand Down Expand Up @@ -1790,12 +1805,43 @@ module.exports = fs = {
Dirent,
Stats,

// Stream constructors
ReadStream,
WriteStream,
// Legacy names...
FileReadStream: ReadStream,
FileWriteStream: WriteStream,
get ReadStream() {
lazyLoadStreams();
return ReadStream;
},

set ReadStream(val) {
ReadStream = val;
},

get WriteStream() {
lazyLoadStreams();
return WriteStream;
},

set WriteStream(val) {
WriteStream = val;
},

// Legacy names... these have to be separate because of how graceful-fs
// (and possibly other) modules monkey patch the values.
get FileReadStream() {
lazyLoadStreams();
return FileReadStream;
},

set FileReadStream(val) {
FileReadStream = val;
},

get FileWriteStream() {
lazyLoadStreams();
return FileWriteStream;
},

set FileWriteStream(val) {
FileWriteStream = val;
},

// For tests
_toUnixTimestamp: toUnixTimestamp
Expand Down
18 changes: 6 additions & 12 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const fs = require('fs');
const { Buffer } = require('buffer');
const {
copyObject,
Expand All @@ -17,13 +18,6 @@ const { Readable, Writable } = require('stream');
const { getPathFromURL } = require('internal/url');
const util = require('util');

let fs;
function lazyFs() {
if (fs === undefined)
fs = require('fs');
return fs;
}

const kMinPoolSpace = 128;

let pool;
Expand Down Expand Up @@ -107,7 +101,7 @@ function ReadStream(path, options) {
util.inherits(ReadStream, Readable);

ReadStream.prototype.open = function() {
lazyFs().open(this.path, this.flags, this.mode, (er, fd) => {
fs.open(this.path, this.flags, this.mode, (er, fd) => {
if (er) {
if (this.autoClose) {
this.destroy();
Expand Down Expand Up @@ -157,7 +151,7 @@ ReadStream.prototype._read = function(n) {
return this.push(null);

// the actual read.
lazyFs().read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => {
fs.read(this.fd, pool, pool.used, toRead, this.pos, (er, bytesRead) => {
if (er) {
if (this.autoClose) {
this.destroy();
Expand Down Expand Up @@ -200,7 +194,7 @@ ReadStream.prototype._destroy = function(err, cb) {
};

function closeFsStream(stream, cb, err) {
lazyFs().close(stream.fd, (er) => {
fs.close(stream.fd, (er) => {
er = er || err;
cb(er);
stream.closed = true;
Expand Down Expand Up @@ -265,7 +259,7 @@ WriteStream.prototype._final = function(callback) {
};

WriteStream.prototype.open = function() {
lazyFs().open(this.path, this.flags, this.mode, (er, fd) => {
fs.open(this.path, this.flags, this.mode, (er, fd) => {
if (er) {
if (this.autoClose) {
this.destroy();
Expand Down Expand Up @@ -293,7 +287,7 @@ WriteStream.prototype._write = function(data, encoding, cb) {
});
}

lazyFs().write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
if (er) {
if (this.autoClose) {
this.destroy();
Expand Down

0 comments on commit 1e7f829

Please sign in to comment.