-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: move SyncWriteStream to internal/fs
Move the implementation of SyncWriteStream to internal/fs. PR-URL: #6749 Reviewed-By: Ron Korving <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]>
- Loading branch information
Showing
4 changed files
with
86 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters