Comparing version 3.1.2 to 3.2.0
334
index.js
@@ -67,64 +67,145 @@ /*! | ||
*/ | ||
function Buffer (subject, encoding) { | ||
var self = this | ||
if (!(self instanceof Buffer)) return new Buffer(subject, encoding) | ||
function Buffer (arg) { | ||
if (!(this instanceof Buffer)) { | ||
// Avoid going through an ArgumentsAdaptorTrampoline in the common case. | ||
if (arguments.length > 1) return new Buffer(arg, arguments[1]) | ||
return new Buffer(arg) | ||
} | ||
var type = typeof subject | ||
var length | ||
this.length = 0 | ||
this.parent = undefined | ||
if (type === 'number') { | ||
length = +subject | ||
} else if (type === 'string') { | ||
length = Buffer.byteLength(subject, encoding) | ||
} else if (type === 'object' && subject !== null) { | ||
// assume object is array-like | ||
if (subject.type === 'Buffer' && isArray(subject.data)) subject = subject.data | ||
length = +subject.length | ||
} else { | ||
// Common case. | ||
if (typeof arg === 'number') { | ||
return fromNumber(this, arg) | ||
} | ||
// Slightly less common case. | ||
if (typeof arg === 'string') { | ||
return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8') | ||
} | ||
// Unusual. | ||
return fromObject(this, arg) | ||
} | ||
function fromNumber (that, length) { | ||
that = allocate(that, length < 0 ? 0 : checked(length) | 0) | ||
if (!Buffer.TYPED_ARRAY_SUPPORT) { | ||
for (var i = 0; i < length; i++) { | ||
that[i] = 0 | ||
} | ||
} | ||
return that | ||
} | ||
function fromString (that, string, encoding) { | ||
if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8' | ||
// Assumption: byteLength() return value is always < kMaxLength. | ||
var length = byteLength(string, encoding) | 0 | ||
that = allocate(that, length) | ||
that.write(string, encoding) | 0 | ||
return that | ||
} | ||
function fromObject (that, object) { | ||
if (Buffer.isBuffer(object)) return fromBuffer(that, object) | ||
if (isArray(object)) return fromArray(that, object) | ||
if (object == null) { | ||
throw new TypeError('must start with number, buffer, array or string') | ||
} | ||
if (length > kMaxLength) { | ||
throw new RangeError('Attempt to allocate Buffer larger than maximum size: 0x' + | ||
kMaxLength.toString(16) + ' bytes') | ||
if (typeof ArrayBuffer !== 'undefined' && object.buffer instanceof ArrayBuffer) { | ||
return fromTypedArray(that, object) | ||
} | ||
if (length < 0) length = 0 | ||
else length >>>= 0 // coerce to uint32 | ||
if (object.length) return fromArrayLike(that, object) | ||
return fromJsonObject(that, object) | ||
} | ||
function fromBuffer (that, buffer) { | ||
var length = checked(buffer.length) | 0 | ||
that = allocate(that, length) | ||
buffer.copy(that, 0, 0, length) | ||
return that | ||
} | ||
function fromArray (that, array) { | ||
var length = checked(array.length) | 0 | ||
that = allocate(that, length) | ||
for (var i = 0; i < length; i += 1) { | ||
that[i] = array[i] & 255 | ||
} | ||
return that | ||
} | ||
// Duplicate of fromArray() to keep fromArray() monomorphic. | ||
function fromTypedArray (that, array) { | ||
var length = checked(array.length) | 0 | ||
that = allocate(that, length) | ||
// Truncating the elements is probably not what people expect from typed | ||
// arrays with BYTES_PER_ELEMENT > 1 but it's compatible with the behavior | ||
// of the old Buffer constructor. | ||
for (var i = 0; i < length; i += 1) { | ||
that[i] = array[i] & 255 | ||
} | ||
return that | ||
} | ||
function fromArrayLike (that, array) { | ||
var length = checked(array.length) | 0 | ||
that = allocate(that, length) | ||
for (var i = 0; i < length; i += 1) { | ||
that[i] = array[i] & 255 | ||
} | ||
return that | ||
} | ||
// Deserialize { type: 'Buffer', data: [1,2,3,...] } into a Buffer object. | ||
// Returns a zero-length buffer for inputs that don't conform to the spec. | ||
function fromJsonObject (that, object) { | ||
var array | ||
var length = 0 | ||
if (object.type === 'Buffer' && isArray(object.data)) { | ||
array = object.data | ||
length = checked(array.length) | 0 | ||
} | ||
that = allocate(that, length) | ||
for (var i = 0; i < length; i += 1) { | ||
that[i] = array[i] & 255 | ||
} | ||
return that | ||
} | ||
function allocate (that, length) { | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
// Preferred: Return an augmented `Uint8Array` instance for best performance | ||
self = Buffer._augment(new Uint8Array(length)) // eslint-disable-line consistent-this | ||
// Return an augmented `Uint8Array` instance, for best performance | ||
that = Buffer._augment(new Uint8Array(length)) | ||
} else { | ||
// Fallback: Return THIS instance of Buffer (created by `new`) | ||
self.length = length | ||
self._isBuffer = true | ||
// Fallback: Return an object instance of the Buffer class | ||
that.length = length | ||
that._isBuffer = true | ||
} | ||
var i | ||
if (Buffer.TYPED_ARRAY_SUPPORT && typeof subject.byteLength === 'number') { | ||
// Speed optimization -- use set if we're copying from a typed array | ||
self._set(subject) | ||
} else if (isArrayish(subject)) { | ||
// Treat array-ish objects as a byte array | ||
if (Buffer.isBuffer(subject)) { | ||
for (i = 0; i < length; i++) { | ||
self[i] = subject.readUInt8(i) | ||
} | ||
} else { | ||
for (i = 0; i < length; i++) { | ||
self[i] = ((subject[i] % 256) + 256) % 256 | ||
} | ||
} | ||
} else if (type === 'string') { | ||
self.write(subject, 0, encoding) | ||
} else if (type === 'number' && !Buffer.TYPED_ARRAY_SUPPORT) { | ||
for (i = 0; i < length; i++) { | ||
self[i] = 0 | ||
} | ||
} | ||
var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 | ||
if (fromPool) that.parent = rootParent | ||
if (length > 0 && length <= Buffer.poolSize) self.parent = rootParent | ||
return that | ||
} | ||
return self | ||
function checked (length) { | ||
// Note: cannot use `length < kMaxLength` here because that fails when | ||
// length is NaN (which is otherwise coerced to zero.) | ||
if (length >= kMaxLength) { | ||
throw new RangeError('Attempt to allocate Buffer larger than maximum ' + | ||
'size: 0x' + kMaxLength.toString(16) + ' bytes') | ||
} | ||
return length >>> 0 | ||
} | ||
@@ -182,3 +263,3 @@ | ||
Buffer.concat = function concat (list, totalLength) { | ||
Buffer.concat = function concat (list, length) { | ||
if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.') | ||
@@ -193,10 +274,10 @@ | ||
var i | ||
if (totalLength === undefined) { | ||
totalLength = 0 | ||
if (length === undefined) { | ||
length = 0 | ||
for (i = 0; i < list.length; i++) { | ||
totalLength += list[i].length | ||
length += list[i].length | ||
} | ||
} | ||
var buf = new Buffer(totalLength) | ||
var buf = new Buffer(length) | ||
var pos = 0 | ||
@@ -211,5 +292,7 @@ for (i = 0; i < list.length; i++) { | ||
Buffer.byteLength = function byteLength (str, encoding) { | ||
var ret | ||
str = str + '' | ||
function byteLength (string, encoding) { | ||
if (typeof string !== 'string') string = String(string) | ||
if (string.length === 0) return 0 | ||
switch (encoding || 'utf8') { | ||
@@ -219,4 +302,3 @@ case 'ascii': | ||
case 'raw': | ||
ret = str.length | ||
break | ||
return string.length | ||
case 'ucs2': | ||
@@ -226,19 +308,15 @@ case 'ucs-2': | ||
case 'utf-16le': | ||
ret = str.length * 2 | ||
break | ||
return string.length * 2 | ||
case 'hex': | ||
ret = str.length >>> 1 | ||
break | ||
return string.length >>> 1 | ||
case 'utf8': | ||
case 'utf-8': | ||
ret = utf8ToBytes(str).length | ||
break | ||
return utf8ToBytes(string).length | ||
case 'base64': | ||
ret = base64ToBytes(str).length | ||
break | ||
return base64ToBytes(string).length | ||
default: | ||
ret = str.length | ||
return string.length | ||
} | ||
return ret | ||
} | ||
Buffer.byteLength = byteLength | ||
@@ -396,9 +474,7 @@ // pre-set for values that may exist in the future | ||
function utf8Write (buf, string, offset, length) { | ||
var charsWritten = blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) | ||
return charsWritten | ||
return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length) | ||
} | ||
function asciiWrite (buf, string, offset, length) { | ||
var charsWritten = blitBuffer(asciiToBytes(string), buf, offset, length) | ||
return charsWritten | ||
return blitBuffer(asciiToBytes(string), buf, offset, length) | ||
} | ||
@@ -411,71 +487,79 @@ | ||
function base64Write (buf, string, offset, length) { | ||
var charsWritten = blitBuffer(base64ToBytes(string), buf, offset, length) | ||
return charsWritten | ||
return blitBuffer(base64ToBytes(string), buf, offset, length) | ||
} | ||
function utf16leWrite (buf, string, offset, length) { | ||
var charsWritten = blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) | ||
return charsWritten | ||
function ucs2Write (buf, string, offset, length) { | ||
return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length) | ||
} | ||
Buffer.prototype.write = function write (string, offset, length, encoding) { | ||
// Support both (string, offset, length, encoding) | ||
// and the legacy (string, encoding, offset, length) | ||
if (isFinite(offset)) { | ||
if (!isFinite(length)) { | ||
// Buffer#write(string) | ||
if (offset === undefined) { | ||
encoding = 'utf8' | ||
length = this.length | ||
offset = 0 | ||
// Buffer#write(string, encoding) | ||
} else if (length === undefined && typeof offset === 'string') { | ||
encoding = offset | ||
length = this.length | ||
offset = 0 | ||
// Buffer#write(string, offset[, length][, encoding]) | ||
} else if (isFinite(offset)) { | ||
offset = offset >>> 0 | ||
if (isFinite(length)) { | ||
length = length >>> 0 | ||
if (encoding === undefined) encoding = 'utf8' | ||
} else { | ||
encoding = length | ||
length = undefined | ||
} | ||
} else { // legacy | ||
// legacy write(string, encoding, offset, length) - remove in v0.13 | ||
} else { | ||
var swap = encoding | ||
encoding = offset | ||
offset = length | ||
offset = length >>> 0 | ||
length = swap | ||
} | ||
offset = Number(offset) || 0 | ||
var remaining = this.length - offset | ||
if (length === undefined || length > remaining) length = remaining | ||
if (length < 0 || offset < 0 || offset > this.length) { | ||
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { | ||
throw new RangeError('attempt to write outside buffer bounds') | ||
} | ||
var remaining = this.length - offset | ||
if (!length) { | ||
length = remaining | ||
} else { | ||
length = Number(length) | ||
if (length > remaining) { | ||
length = remaining | ||
if (!encoding) encoding = 'utf8' | ||
var loweredCase = false | ||
for (;;) { | ||
switch (encoding) { | ||
case 'hex': | ||
return hexWrite(this, string, offset, length) | ||
case 'utf8': | ||
case 'utf-8': | ||
return utf8Write(this, string, offset, length) | ||
case 'ascii': | ||
return asciiWrite(this, string, offset, length) | ||
case 'binary': | ||
return binaryWrite(this, string, offset, length) | ||
case 'base64': | ||
// Warning: maxLength not taken into account in base64Write | ||
return base64Write(this, string, offset, length) | ||
case 'ucs2': | ||
case 'ucs-2': | ||
case 'utf16le': | ||
case 'utf-16le': | ||
return ucs2Write(this, string, offset, length) | ||
default: | ||
if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding) | ||
encoding = ('' + encoding).toLowerCase() | ||
loweredCase = true | ||
} | ||
} | ||
encoding = String(encoding || 'utf8').toLowerCase() | ||
var ret | ||
switch (encoding) { | ||
case 'hex': | ||
ret = hexWrite(this, string, offset, length) | ||
break | ||
case 'utf8': | ||
case 'utf-8': | ||
ret = utf8Write(this, string, offset, length) | ||
break | ||
case 'ascii': | ||
ret = asciiWrite(this, string, offset, length) | ||
break | ||
case 'binary': | ||
ret = binaryWrite(this, string, offset, length) | ||
break | ||
case 'base64': | ||
ret = base64Write(this, string, offset, length) | ||
break | ||
case 'ucs2': | ||
case 'ucs-2': | ||
case 'utf16le': | ||
case 'utf-16le': | ||
ret = utf16leWrite(this, string, offset, length) | ||
break | ||
default: | ||
throw new TypeError('Unknown encoding: ' + encoding) | ||
} | ||
return ret | ||
} | ||
@@ -1204,8 +1288,2 @@ | ||
function isArrayish (subject) { | ||
return isArray(subject) || Buffer.isBuffer(subject) || | ||
subject && typeof subject === 'object' && | ||
typeof subject.length === 'number' | ||
} | ||
function toHex (n) { | ||
@@ -1212,0 +1290,0 @@ if (n < 16) return '0' + n.toString(16) |
{ | ||
"name": "buffer", | ||
"description": "Node.js Buffer API, for the browser", | ||
"version": "3.1.2", | ||
"version": "3.2.0", | ||
"author": { | ||
@@ -29,4 +29,4 @@ "name": "Feross Aboukhadijeh", | ||
"split": "^0.3.2", | ||
"standard": "^2.0.0", | ||
"tape": "^3.0.1", | ||
"standard": "^3.6.1", | ||
"tape": "^4.0.0", | ||
"through2": "^0.6.3", | ||
@@ -33,0 +33,0 @@ "zuul": "^2.0.0" |
@@ -20,4 +20,4 @@ # buffer [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][npm-url] [![gratipay][gratipay-image]][gratipay-url] | ||
The goal is to provide an API that is 100% identical to | ||
[node's Buffer API](http://nodejs.org/api/buffer.html). Read the | ||
[official docs](http://nodejs.org/api/buffer.html) for the full list of properties, | ||
[node's Buffer API](http://iojs.org/api/buffer.html). Read the | ||
[official docs](http://iojs.org/api/buffer.html) for the full list of properties, | ||
instance methods, and class methods that are supported. | ||
@@ -53,3 +53,3 @@ | ||
The module's API is identical to node's `Buffer` API. Read the | ||
[official docs](http://nodejs.org/api/buffer.html) for the full list of properties, | ||
[official docs](http://iojs.org/api/buffer.html) for the full list of properties, | ||
instance methods, and class methods that are supported. | ||
@@ -94,3 +94,3 @@ | ||
In node, the `slice()` method returns a new `Buffer` that shares underlying memory with | ||
the original Buffer. When you modify one buffer, you modify the other. [Read more.](http://nodejs.org/api/buffer.html#buffer_buf_slice_start_end) | ||
the original Buffer. When you modify one buffer, you modify the other. [Read more.](http://iojs.org/api/buffer.html#buffer_buf_slice_start_end) | ||
@@ -97,0 +97,0 @@ This works correctly in browsers with typed array support (\* with the exception of Firefox older than version 30). Browsers that lack typed arrays get an alternate buffer implementation based on `Object` which has no mechanism to point separate `Buffer`s to the same underlying slab of memory. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
128443
3350