From dc7277909b0d842ea3f559628d6d7c7887bfd05b Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 30 Aug 2016 09:53:50 -0700 Subject: [PATCH] fs: move SyncWriteStream to internal/fs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move the implementation of SyncWriteStream to internal/fs. PR-URL: https://github.com/nodejs/node/pull/6749 Reviewed-By: Ron Korving Reviewed-By: Сковорода Никита Андреевич --- lib/fs.js | 78 +++-------------------------------- lib/internal/fs.js | 78 +++++++++++++++++++++++++++++++++++ lib/internal/process/stdio.js | 2 +- node.gyp | 1 + 4 files changed, 86 insertions(+), 73 deletions(-) create mode 100644 lib/internal/fs.js diff --git a/lib/fs.js b/lib/fs.js index a7e8a395a68017..f6059ad65c2a04 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 00000000000000..dcfb4be1236530 --- /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 cab1f10cd87ebc..c8f36c5df60c94 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 0f263d1346f1b5..9cbdab063dc829 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',