Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
buffer: no warning when encoding isn't passed
Browse files Browse the repository at this point in the history
Buffer#write() was showing the deprecation warning when only
buf.write('string') was passed. This is incorrect since the encoding is
always optional.

Argument order should follow:
  Buffer#write(string[, offset[, length]][, encoding])

(yeah, not confusing at all)
  • Loading branch information
trevnorris committed Nov 15, 2013
1 parent 85c8eeb commit a263aba
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/buffer.js
Expand Up @@ -75,8 +75,10 @@ function Buffer(subject, encoding) {

if (!util.isNumber(subject)) {
if (util.isString(subject)) {
// FIXME: the number of bytes hasn't changed, so why change the length?
this.length = this.write(subject, 0, encoding);
// In the case of base64 it's possible that the size of the buffer
// allocated was slightly too large. In this case we need to rewrite
// the length to the actual length written.
this.length = this.write(subject, encoding);
} else {
if (util.isBuffer(subject))
subject.copy(this, 0, 0, this.length);
Expand Down Expand Up @@ -281,18 +283,25 @@ Buffer.prototype.set = util.deprecate(function set(offset, v) {
// write(string, offset = 0, length = buffer.length, encoding = 'utf8')
var writeWarned = false;
var writeMsg = '.write(string, encoding, offset, length) is deprecated.' +
' Use write(string, offset, length, encoding) instead.';
' Use write(string[, offset[, length]][, encoding]) instead.';
Buffer.prototype.write = function(string, offset, length, encoding) {
// allow write(string, encoding)
if (util.isString(offset) && util.isUndefined(length)) {
// Buffer#write(string);
if (util.isUndefined(offset)) {
offset = 0;
encoding = 'utf8';

// Buffer#write(string, encoding)
} else if (util.isUndefined(length) && util.isString(offset)) {
encoding = offset;
offset = 0;

// allow write(string, offset[, length], encoding)
// Buffer#write(string, offset[, length][, encoding])
} else if (isFinite(offset)) {
offset = ~~offset;
if (isFinite(length)) {
length = ~~length;
if (util.isUndefined(encoding))
encoding = 'utf8';
} else {
encoding = length;
length = undefined;
Expand Down

0 comments on commit a263aba

Please sign in to comment.