Skip to content

Commit

Permalink
fs: move SyncWriteStream to internal/fs
Browse files Browse the repository at this point in the history
Move the implementation of SyncWriteStream to internal/fs.

PR-URL: #6749
Reviewed-By: Ron Korving <ron@ronkorving.nl>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
  • Loading branch information
jasnell committed Sep 2, 2016
1 parent 2c9a86f commit dc72779
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 73 deletions.
78 changes: 6 additions & 72 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
78 changes: 78 additions & 0 deletions lib/internal/fs.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit dc72779

Please sign in to comment.