mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.0 KiB
122 lines
3.0 KiB
'use strict';
|
|
|
|
const Buffer = require('buffer').Buffer;
|
|
const Stream = require('stream').Stream;
|
|
const fs = require('fs');
|
|
const util = require('util');
|
|
const constants = process.binding('constants').fs;
|
|
|
|
const O_APPEND = constants.O_APPEND | 0;
|
|
const O_CREAT = constants.O_CREAT | 0;
|
|
const O_EXCL = constants.O_EXCL | 0;
|
|
const O_RDONLY = constants.O_RDONLY | 0;
|
|
const O_RDWR = constants.O_RDWR | 0;
|
|
const O_SYNC = constants.O_SYNC | 0;
|
|
const O_TRUNC = constants.O_TRUNC | 0;
|
|
const O_WRONLY = constants.O_WRONLY | 0;
|
|
|
|
function assertEncoding(encoding) {
|
|
if (encoding && !Buffer.isEncoding(encoding)) {
|
|
throw new Error(`Unknown encoding: ${encoding}`);
|
|
}
|
|
}
|
|
exports.assertEncoding = assertEncoding;
|
|
|
|
function stringToFlags(flag) {
|
|
if (typeof flag === 'number') {
|
|
return flag;
|
|
}
|
|
|
|
switch (flag) {
|
|
case 'r' : return O_RDONLY;
|
|
case 'rs' : // Fall through.
|
|
case 'sr' : return O_RDONLY | O_SYNC;
|
|
case 'r+' : return O_RDWR;
|
|
case 'rs+' : // Fall through.
|
|
case 'sr+' : return O_RDWR | O_SYNC;
|
|
|
|
case 'w' : return O_TRUNC | O_CREAT | O_WRONLY;
|
|
case 'wx' : // Fall through.
|
|
case 'xw' : return O_TRUNC | O_CREAT | O_WRONLY | O_EXCL;
|
|
|
|
case 'w+' : return O_TRUNC | O_CREAT | O_RDWR;
|
|
case 'wx+': // Fall through.
|
|
case 'xw+': return O_TRUNC | O_CREAT | O_RDWR | O_EXCL;
|
|
|
|
case 'a' : return O_APPEND | O_CREAT | O_WRONLY;
|
|
case 'ax' : // Fall through.
|
|
case 'xa' : return O_APPEND | O_CREAT | O_WRONLY | O_EXCL;
|
|
|
|
case 'a+' : return O_APPEND | O_CREAT | O_RDWR;
|
|
case 'ax+': // Fall through.
|
|
case 'xa+': return O_APPEND | O_CREAT | O_RDWR | O_EXCL;
|
|
}
|
|
|
|
throw new Error('Unknown file open flag: ' + flag);
|
|
}
|
|
exports.stringToFlags = stringToFlags;
|
|
|
|
// 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;
|
|
|