Skip to content

Commit 7fbd77c

Browse files
committed
fs: move SyncWriteStream to internal/fs
Move the implementation of SyncWriteStream to internal/fs.
1 parent 2c9a86f commit 7fbd77c

File tree

4 files changed

+86
-73
lines changed

4 files changed

+86
-73
lines changed

lib/fs.js

+6-72
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const Stream = require('stream').Stream;
1414
const EventEmitter = require('events');
1515
const FSReqWrap = binding.FSReqWrap;
1616
const FSEvent = process.binding('fs_event_wrap').FSEvent;
17+
const internalFS = require('internal/fs');
18+
const assertEncoding = internalFS.assertEncoding;
19+
const SyncWriteStream = internalFS.SyncWriteStream;
1720

1821
Object.defineProperty(exports, 'constants', {
1922
configurable: false,
@@ -96,12 +99,6 @@ function makeCallback(cb) {
9699
};
97100
}
98101

99-
function assertEncoding(encoding) {
100-
if (encoding && !Buffer.isEncoding(encoding)) {
101-
throw new Error('Unknown encoding: ' + encoding);
102-
}
103-
}
104-
105102
function nullCheck(path, callback) {
106103
if (('' + path).indexOf('\u0000') !== -1) {
107104
var er = new Error('Path must be a string without null bytes');
@@ -2144,75 +2141,12 @@ WriteStream.prototype.close = ReadStream.prototype.close;
21442141
// There is no shutdown() for files.
21452142
WriteStream.prototype.destroySoon = WriteStream.prototype.end;
21462143

2147-
21482144
// SyncWriteStream is internal. DO NOT USE.
2149-
// Temporary hack for process.stdout and process.stderr when piped to files.
2150-
function SyncWriteStream(fd, options) {
2151-
Stream.call(this);
2152-
2153-
options = options || {};
2154-
2155-
this.fd = fd;
2156-
this.writable = true;
2157-
this.readable = false;
2158-
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
2159-
}
2160-
2161-
util.inherits(SyncWriteStream, Stream);
2162-
2163-
2164-
// Export
2145+
// todo(jasnell): "Docs-only" deprecation for now. This was never documented
2146+
// so there's no documentation to modify. In the future, add a runtime
2147+
// deprecation.
21652148
Object.defineProperty(fs, 'SyncWriteStream', {
21662149
configurable: true,
21672150
writable: true,
21682151
value: SyncWriteStream
21692152
});
2170-
2171-
SyncWriteStream.prototype.write = function(data, arg1, arg2) {
2172-
var encoding, cb;
2173-
2174-
// parse arguments
2175-
if (arg1) {
2176-
if (typeof arg1 === 'string') {
2177-
encoding = arg1;
2178-
cb = arg2;
2179-
} else if (typeof arg1 === 'function') {
2180-
cb = arg1;
2181-
} else {
2182-
throw new Error('Bad arguments');
2183-
}
2184-
}
2185-
assertEncoding(encoding);
2186-
2187-
// Change strings to buffers. SLOW
2188-
if (typeof data === 'string') {
2189-
data = Buffer.from(data, encoding);
2190-
}
2191-
2192-
fs.writeSync(this.fd, data, 0, data.length);
2193-
2194-
if (cb) {
2195-
process.nextTick(cb);
2196-
}
2197-
2198-
return true;
2199-
};
2200-
2201-
2202-
SyncWriteStream.prototype.end = function(data, arg1, arg2) {
2203-
if (data) {
2204-
this.write(data, arg1, arg2);
2205-
}
2206-
this.destroy();
2207-
};
2208-
2209-
2210-
SyncWriteStream.prototype.destroy = function() {
2211-
if (this.autoClose)
2212-
fs.closeSync(this.fd);
2213-
this.fd = null;
2214-
this.emit('close');
2215-
return true;
2216-
};
2217-
2218-
SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;

lib/internal/fs.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict';
2+
3+
const Buffer = require('buffer').Buffer;
4+
const Stream = require('stream').Stream;
5+
const fs = require('fs');
6+
const util = require('util');
7+
8+
function assertEncoding(encoding) {
9+
if (encoding && !Buffer.isEncoding(encoding)) {
10+
throw new Error(`Unknown encoding: ${encoding}`);
11+
}
12+
}
13+
exports.assertEncoding = assertEncoding;
14+
15+
// Temporary hack for process.stdout and process.stderr when piped to files.
16+
function SyncWriteStream(fd, options) {
17+
Stream.call(this);
18+
19+
options = options || {};
20+
21+
this.fd = fd;
22+
this.writable = true;
23+
this.readable = false;
24+
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
25+
}
26+
27+
util.inherits(SyncWriteStream, Stream);
28+
29+
SyncWriteStream.prototype.write = function(data, arg1, arg2) {
30+
var encoding, cb;
31+
32+
// parse arguments
33+
if (arg1) {
34+
if (typeof arg1 === 'string') {
35+
encoding = arg1;
36+
cb = arg2;
37+
} else if (typeof arg1 === 'function') {
38+
cb = arg1;
39+
} else {
40+
throw new Error('Bad arguments');
41+
}
42+
}
43+
assertEncoding(encoding);
44+
45+
// Change strings to buffers. SLOW
46+
if (typeof data === 'string') {
47+
data = Buffer.from(data, encoding);
48+
}
49+
50+
fs.writeSync(this.fd, data, 0, data.length);
51+
52+
if (cb) {
53+
process.nextTick(cb);
54+
}
55+
56+
return true;
57+
};
58+
59+
60+
SyncWriteStream.prototype.end = function(data, arg1, arg2) {
61+
if (data) {
62+
this.write(data, arg1, arg2);
63+
}
64+
this.destroy();
65+
};
66+
67+
68+
SyncWriteStream.prototype.destroy = function() {
69+
if (this.autoClose)
70+
fs.closeSync(this.fd);
71+
this.fd = null;
72+
this.emit('close');
73+
return true;
74+
};
75+
76+
SyncWriteStream.prototype.destroySoon = SyncWriteStream.prototype.destroy;
77+
78+
exports.SyncWriteStream = SyncWriteStream;

lib/internal/process/stdio.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function createWritableStdioStream(fd) {
145145
break;
146146

147147
case 'FILE':
148-
const fs = require('fs');
148+
const fs = require('internal/fs');
149149
stream = new fs.SyncWriteStream(fd, { autoClose: false });
150150
stream._type = 'fs';
151151
break;

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
'lib/internal/child_process.js',
7878
'lib/internal/cluster.js',
7979
'lib/internal/freelist.js',
80+
'lib/internal/fs.js',
8081
'lib/internal/linkedlist.js',
8182
'lib/internal/net.js',
8283
'lib/internal/module.js',

0 commit comments

Comments
 (0)