diff --git a/lib/fs.js b/lib/fs.js index a7e8a395a6..f6059ad65c 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -14,6 +14,9 @@ const Stream = require('stream').Stream; const EventEmitter = require('events'); const FSReqWrap = binding.FSReqWrap; const FSEvent = process.binding('fs_event_wrap').FSEvent; +const internalFS = require('internal/fs'); +const assertEncoding = internalFS.assertEncoding; +const SyncWriteStream = internalFS.SyncWriteStream; Object.defineProperty(exports, 'constants', { configurable: false, @@ -96,12 +99,6 @@ function makeCallback(cb) { }; } -function assertEncoding(encoding) { - if (encoding && !Buffer.isEncoding(encoding)) { - throw new Error('Unknown encoding: ' + encoding); - } -} - function nullCheck(path, callback) { if (('' + path).indexOf('\u0000') !== -1) { var er = new Error('Path must be a string without null bytes'); @@ -2144,75 +2141,12 @@ WriteStream.prototype.close = ReadStream.prototype.close; // There is no shutdown() for files. WriteStream.prototype.destroySoon = WriteStream.prototype.end; - // SyncWriteStream is internal. DO NOT USE. -// Temporary hack for process.stdout and process.stderr when piped to files. -function SyncWriteStream(fd, options) { - Stream.call(this); - - options = options || {}; - - this.fd = fd; - this.writable = true; - this.readable = false; - this.autoClose = options.autoClose === undefined ? true : options.autoClose; -} - -util.inherits(SyncWriteStream, Stream); - - -// Export +// todo(jasnell): "Docs-only" deprecation for now. This was never documented +// so there's no documentation to modify. In the future, add a runtime +// deprecation. Object.defineProperty(fs, 'SyncWriteStream', { configurable: true, writable: true, value: SyncWriteStream }); - -SyncWriteStream.prototype.write = function(data, arg1, arg2) { - var encoding, cb; - - // parse arguments - if (arg1) { - if (typeof arg1 === 'string') { - encoding = arg1; - cb = arg2; - } else if (typeof arg1 === 'function') { - cb = arg1; - } else { - throw new Error('Bad arguments'); - } - } - assertEncoding(encoding); - - // Change strings to buffers. SLOW - if (typeof data === 'string') { - data = Buffer.from(data, encoding); - } - - fs.writeSync(this.fd, data, 0, data.length); - - if (cb) { - process.nextTick(cb); - } - - return true; -}; - - -SyncWriteStream.prototype.end = function(data, arg1, arg2) { - if (data) { - this.write(data, arg1, arg2); - } - this.destroy(); -}; - - -SyncWriteStream.prototype.destroy = function() { - if (this.autoClose) - fs.closeSync(this.fd); - this.fd = null; - this.emit('close'); - return true; -}; - -SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy; diff --git a/lib/internal/fs.js b/lib/internal/fs.js new file mode 100644 index 0000000000..dcfb4be123 --- /dev/null +++ b/lib/internal/fs.js @@ -0,0 +1,78 @@ +'use strict'; + +const Buffer = require('buffer').Buffer; +const Stream = require('stream').Stream; +const fs = require('fs'); +const util = require('util'); + +function assertEncoding(encoding) { + if (encoding && !Buffer.isEncoding(encoding)) { + throw new Error(`Unknown encoding: ${encoding}`); + } +} +exports.assertEncoding = assertEncoding; + +// Temporary hack for process.stdout and process.stderr when piped to files. +function SyncWriteStream(fd, options) { + Stream.call(this); + + options = options || {}; + + this.fd = fd; + this.writable = true; + this.readable = false; + this.autoClose = options.autoClose === undefined ? true : options.autoClose; +} + +util.inherits(SyncWriteStream, Stream); + +SyncWriteStream.prototype.write = function(data, arg1, arg2) { + var encoding, cb; + + // parse arguments + if (arg1) { + if (typeof arg1 === 'string') { + encoding = arg1; + cb = arg2; + } else if (typeof arg1 === 'function') { + cb = arg1; + } else { + throw new Error('Bad arguments'); + } + } + assertEncoding(encoding); + + // Change strings to buffers. SLOW + if (typeof data === 'string') { + data = Buffer.from(data, encoding); + } + + fs.writeSync(this.fd, data, 0, data.length); + + if (cb) { + process.nextTick(cb); + } + + return true; +}; + + +SyncWriteStream.prototype.end = function(data, arg1, arg2) { + if (data) { + this.write(data, arg1, arg2); + } + this.destroy(); +}; + + +SyncWriteStream.prototype.destroy = function() { + if (this.autoClose) + fs.closeSync(this.fd); + this.fd = null; + this.emit('close'); + return true; +}; + +SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy; + +exports.SyncWriteStream = SyncWriteStream; diff --git a/lib/internal/process/stdio.js b/lib/internal/process/stdio.js index cab1f10cd8..c8f36c5df6 100644 --- a/lib/internal/process/stdio.js +++ b/lib/internal/process/stdio.js @@ -145,7 +145,7 @@ function createWritableStdioStream(fd) { break; case 'FILE': - const fs = require('fs'); + const fs = require('internal/fs'); stream = new fs.SyncWriteStream(fd, { autoClose: false }); stream._type = 'fs'; break; diff --git a/node.gyp b/node.gyp index 0f263d1346..9cbdab063d 100644 --- a/node.gyp +++ b/node.gyp @@ -77,6 +77,7 @@ 'lib/internal/child_process.js', 'lib/internal/cluster.js', 'lib/internal/freelist.js', + 'lib/internal/fs.js', 'lib/internal/linkedlist.js', 'lib/internal/net.js', 'lib/internal/module.js',