Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: joyent/node
base: 0285dae26a
...
head fork: joyent/node
compare: 985e3a25cb
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
7 doc/api/buffer.markdown
@@ -65,6 +65,13 @@ Allocates a new buffer using an `array` of octets.
Allocates a new buffer containing the given `str`.
`encoding` defaults to `'utf8'`.
+### Class Method: Buffer.isEncoding(encoding)
+
+* `encoding` {String} The encoding string to test
+
+Returns true if the `encoding` is a valid encoding argument, or false
+otherwise.
+
### buf.write(string, [offset], [length], [encoding])
* `string` String - data to be written to buffer
View
23 lib/buffer.js
@@ -274,6 +274,29 @@ function isArrayIsh(subject) {
exports.SlowBuffer = SlowBuffer;
exports.Buffer = Buffer;
+
+Buffer.isEncoding = function(encoding) {
+ switch (encoding && encoding.toLowerCase()) {
+ case 'hex':
+ case 'utf8':
+ case 'utf-8':
+ case 'ascii':
+ case 'binary':
+ case 'base64':
+ case 'ucs2':
+ case 'ucs-2':
+ case 'utf16le':
+ case 'utf-16le':
+ case 'raw':
+ return true;
+
+ default:
+ return false;
+ }
+};
+
+
+
Buffer.poolSize = 8 * 1024;
var pool;
View
32 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') {
View
2  lib/path.js
@@ -450,4 +450,4 @@ if (isWindows) {
exports._makeLong = function(path) {
return path;
};
-}
+}
View
2  lib/timers.js
@@ -308,7 +308,7 @@ function processImmediate() {
if (immediate.domain) immediate.domain.exit();
}
-};
+}
exports.setImmediate = function(callback) {
View
4 src/node.js
@@ -580,13 +580,13 @@
wrap.unref();
- wrap.onsignal = function () { process.emit(type); };
+ wrap.onsignal = function() { process.emit(type); };
var signum = startup.lazyConstants()[type];
var r = wrap.start(signum);
if (r) {
wrap.close();
- throw errnoException(errno, "uv_signal_start");
+ throw errnoException(errno, 'uv_signal_start');
}
signalWraps[type] = wrap;
View
21 test/simple/test-buffer.js
@@ -727,3 +727,24 @@ assert.equal(b.toString(), 'xxx');
// issue GH-3416
Buffer(Buffer(0), 0, 0);
+
+
+[ 'hex',
+ 'utf8',
+ 'utf-8',
+ 'ascii',
+ 'binary',
+ 'base64',
+ 'ucs2',
+ 'ucs-2',
+ 'utf16le',
+ 'utf-16le' ].forEach(function(enc) {
+ assert.equal(Buffer.isEncoding(enc), true);
+ });
+
+[ 'utf9',
+ 'utf-7',
+ 'Unicode-FTW',
+ 'new gnu gun' ].forEach(function(enc) {
+ assert.equal(Buffer.isEncoding(enc), false);
+ });

No commit comments for this range

Something went wrong with that request. Please try again.