Browse Source

fs: move SyncWriteStream to internal/fs

Move the implementation of SyncWriteStream to internal/fs.

PR-URL: https://github.com/nodejs/node/pull/6749
Reviewed-By: Ron Korving <ron@ronkorving.nl>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
v7.x
James M Snell 8 years ago
parent
commit
dc7277909b
  1. 78
      lib/fs.js
  2. 78
      lib/internal/fs.js
  3. 2
      lib/internal/process/stdio.js
  4. 1
      node.gyp

78
lib/fs.js

@ -14,6 +14,9 @@ const Stream = require('stream').Stream;
const EventEmitter = require('events'); const EventEmitter = require('events');
const FSReqWrap = binding.FSReqWrap; const FSReqWrap = binding.FSReqWrap;
const FSEvent = process.binding('fs_event_wrap').FSEvent; 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', { Object.defineProperty(exports, 'constants', {
configurable: false, 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) { function nullCheck(path, callback) {
if (('' + path).indexOf('\u0000') !== -1) { if (('' + path).indexOf('\u0000') !== -1) {
var er = new Error('Path must be a string without null bytes'); 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. // There is no shutdown() for files.
WriteStream.prototype.destroySoon = WriteStream.prototype.end; WriteStream.prototype.destroySoon = WriteStream.prototype.end;
// SyncWriteStream is internal. DO NOT USE. // SyncWriteStream is internal. DO NOT USE.
// Temporary hack for process.stdout and process.stderr when piped to files. // todo(jasnell): "Docs-only" deprecation for now. This was never documented
function SyncWriteStream(fd, options) { // so there's no documentation to modify. In the future, add a runtime
Stream.call(this); // deprecation.
options = options || {};
this.fd = fd;
this.writable = true;
this.readable = false;
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
}
util.inherits(SyncWriteStream, Stream);
// Export
Object.defineProperty(fs, 'SyncWriteStream', { Object.defineProperty(fs, 'SyncWriteStream', {
configurable: true, configurable: true,
writable: true, writable: true,
value: SyncWriteStream 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
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;

2
lib/internal/process/stdio.js

@ -145,7 +145,7 @@ function createWritableStdioStream(fd) {
break; break;
case 'FILE': case 'FILE':
const fs = require('fs'); const fs = require('internal/fs');
stream = new fs.SyncWriteStream(fd, { autoClose: false }); stream = new fs.SyncWriteStream(fd, { autoClose: false });
stream._type = 'fs'; stream._type = 'fs';
break; break;

1
node.gyp

@ -77,6 +77,7 @@
'lib/internal/child_process.js', 'lib/internal/child_process.js',
'lib/internal/cluster.js', 'lib/internal/cluster.js',
'lib/internal/freelist.js', 'lib/internal/freelist.js',
'lib/internal/fs.js',
'lib/internal/linkedlist.js', 'lib/internal/linkedlist.js',
'lib/internal/net.js', 'lib/internal/net.js',
'lib/internal/module.js', 'lib/internal/module.js',

Loading…
Cancel
Save