summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/fs.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/fs.js b/lib/fs.js
index f32ef48ad..3ed99c77f 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -52,6 +52,13 @@ var O_WRONLY = constants.O_WRONLY || 0;
var isWindows = process.platform === 'win32';
+function assertEncoding(encoding) {
+ if (encoding && !Buffer.isEncoding(encoding)) {
+ throw new Error('Unknown encoding: ' + encoding);
+ }
+}
+
+
fs.Stats = binding.Stats;
fs.Stats.prototype._checkModeProperty = function(property) {
@@ -106,6 +113,8 @@ fs.readFile = function(path, encoding_) {
var callback = arguments[arguments.length - 1];
if (typeof(callback) !== 'function') callback = function() {};
+ assertEncoding(encoding);
+
// first, stat the file, so we know the size.
var size;
var buffer; // single buffer with file data
@@ -179,6 +188,8 @@ fs.readFile = function(path, encoding_) {
};
fs.readFileSync = function(path, encoding) {
+ assertEncoding(encoding);
+
var fd = fs.openSync(path, constants.O_RDONLY, 438 /*=0666*/);
var size;
@@ -343,6 +354,9 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
// legacy string interface (fd, length, position, encoding, callback)
var cb = arguments[4],
encoding = arguments[3];
+
+ assertEncoding(encoding);
+
position = arguments[2];
length = arguments[1];
buffer = new Buffer(length);
@@ -371,6 +385,9 @@ fs.readSync = function(fd, buffer, offset, length, position) {
// legacy string interface (fd, length, position, encoding, callback)
legacy = true;
var encoding = arguments[3];
+
+ assertEncoding(encoding);
+
position = arguments[2];
length = arguments[1];
buffer = new Buffer(length);
@@ -392,6 +409,7 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
// legacy string interface (fd, data, position, encoding, callback)
callback = arguments[4];
position = arguments[2];
+ assertEncoding(arguments[3]);
buffer = new Buffer('' + arguments[1], arguments[3]);
offset = 0;
@@ -419,6 +437,7 @@ fs.writeSync = function(fd, buffer, offset, length, position) {
if (!Buffer.isBuffer(buffer)) {
// legacy string interface (fd, data, position, encoding)
position = arguments[2];
+ assertEncoding(arguments[3]);
buffer = new Buffer('' + arguments[1], arguments[3]);
offset = 0;
@@ -787,6 +806,8 @@ function writeAll(fd, buffer, offset, length, position, callback) {
fs.writeFile = function(path, data, encoding_, callback) {
var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
+ assertEncoding(encoding);
+
var callback_ = arguments[arguments.length - 1];
callback = (typeof(callback_) == 'function' ? callback_ : null);
fs.open(path, 'w', 438 /*=0666*/, function(openErr, fd) {
@@ -801,6 +822,8 @@ fs.writeFile = function(path, data, encoding_, callback) {
};
fs.writeFileSync = function(path, data, encoding) {
+ assertEncoding(encoding);
+
var fd = fs.openSync(path, 'w');
if (!Buffer.isBuffer(data)) {
data = new Buffer('' + data, encoding || 'utf8');
@@ -818,6 +841,8 @@ fs.writeFileSync = function(path, data, encoding) {
fs.appendFile = function(path, data, encoding_, callback) {
var encoding = (typeof(encoding_) == 'string' ? encoding_ : 'utf8');
+ assertEncoding(encoding);
+
var callback_ = arguments[arguments.length - 1];
callback = (typeof(callback_) == 'function' ? callback_ : null);
@@ -829,6 +854,8 @@ fs.appendFile = function(path, data, encoding_, callback) {
};
fs.appendFileSync = function(path, data, encoding) {
+ assertEncoding(encoding);
+
var fd = fs.openSync(path, 'a');
if (!Buffer.isBuffer(data)) {
data = new Buffer('' + data, encoding || 'utf8');
@@ -1288,6 +1315,8 @@ var ReadStream = fs.ReadStream = function(path, options) {
this[key] = options[key];
}
+ assertEncoding(this.encoding);
+
if (this.encoding) this.setEncoding(this.encoding);
if (this.start !== undefined) {
@@ -1331,6 +1360,7 @@ util.inherits(ReadStream, Stream);
fs.FileReadStream = fs.ReadStream; // support the legacy name
ReadStream.prototype.setEncoding = function(encoding) {
+ assertEncoding(encoding);
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder(encoding);
};
@@ -1584,6 +1614,7 @@ WriteStream.prototype.write = function(data) {
if (!Buffer.isBuffer(data)) {
var encoding = 'utf8';
if (typeof(arguments[1]) == 'string') encoding = arguments[1];
+ assertEncoding(encoding);
data = new Buffer('' + data, encoding);
}
@@ -1670,6 +1701,7 @@ SyncWriteStream.prototype.write = function(data, arg1, arg2) {
throw new Error('bad arg');
}
}
+ assertEncoding(encoding);
// Change strings to buffers. SLOW
if (typeof data == 'string') {