rollup-plugin-node-globals
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -13,13 +13,8 @@ var _buffer, _slowbuffer, _INSPECT_MAX_BYTES; | ||
function init () { | ||
var i | ||
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
var len = code.length | ||
for (i = 0; i < len; i++) { | ||
for (var i = 0, len = code.length; i < len; ++i) { | ||
lookup[i] = code[i] | ||
revLookup[code.charCodeAt(i)] = i | ||
} | ||
for (i = 0; i < len; ++i) { | ||
revLookup[code.charCodeAt(i)] = i | ||
} | ||
revLookup['-'.charCodeAt(0)] = 62 | ||
@@ -56,4 +51,4 @@ revLookup['_'.charCodeAt(0)] = 63 | ||
tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)] | ||
arr[L++] = (tmp & 0xFF0000) >> 16 | ||
arr[L++] = (tmp & 0xFF00) >> 8 | ||
arr[L++] = (tmp >> 16) & 0xFF | ||
arr[L++] = (tmp >> 8) & 0xFF | ||
arr[L++] = tmp & 0xFF | ||
@@ -139,6 +134,3 @@ } | ||
_expor_.INSPECT_MAX_BYTES = 50 | ||
Buffer.poolSize = 8192 // not used by this implementation | ||
var rootParent = {} | ||
/** | ||
@@ -172,6 +164,11 @@ * If `Buffer.TYPED_ARRAY_SUPPORT`: | ||
/* | ||
* Export kMaxLength after typed array support is determined. | ||
*/ | ||
_expor_.kMaxLength = kMaxLength() | ||
function typedArraySupport () { | ||
try { | ||
var arr = new Uint8Array(1) | ||
arr.foo = function () { return 42 } | ||
arr.__proto__ = {__proto__: Uint8Array.prototype, foo: function () { return 42 }} | ||
return arr.foo() === 42 && // typed array instances can be augmented | ||
@@ -191,2 +188,21 @@ typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray` | ||
function createBuffer (that, length) { | ||
if (kMaxLength() < length) { | ||
throw new RangeError('Invalid typed array length') | ||
} | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
// Return an augmented `Uint8Array` instance, for best performance | ||
that = new Uint8Array(length) | ||
that.__proto__ = Buffer.prototype | ||
} else { | ||
// Fallback: Return an object instance of the Buffer class | ||
if (that === null) { | ||
that = new Buffer(length) | ||
} | ||
that.length = length | ||
} | ||
return that | ||
} | ||
/** | ||
@@ -201,12 +217,6 @@ * The Buffer constructor returns instances of `Uint8Array` that have their | ||
*/ | ||
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) | ||
} | ||
if (!Buffer.TYPED_ARRAY_SUPPORT) { | ||
this.length = 0 | ||
this.parent = undefined | ||
function Buffer (arg, encodingOrOffset, length) { | ||
if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) { | ||
return new Buffer(arg, encodingOrOffset, length) | ||
} | ||
@@ -216,14 +226,14 @@ | ||
if (typeof arg === 'number') { | ||
return fromNumber(this, arg) | ||
if (typeof encodingOrOffset === 'string') { | ||
throw new Error( | ||
'If encoding is specified then the first argument must be a string' | ||
) | ||
} | ||
return allocUnsafe(this, arg) | ||
} | ||
return from(this, arg, encodingOrOffset, length) | ||
} | ||
// Slightly less common case. | ||
if (typeof arg === 'string') { | ||
return fromString(this, arg, arguments.length > 1 ? arguments[1] : 'utf8') | ||
} | ||
Buffer.poolSize = 8192 // not used by this implementation | ||
// Unusual. | ||
return fromObject(this, arg) | ||
} | ||
// TODO: Legacy, not needed anymore. Remove in next major version. | ||
@@ -235,58 +245,80 @@ Buffer._augment = function (arr) { | ||
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 | ||
} | ||
function from (that, value, encodingOrOffset, length) { | ||
if (typeof value === 'number') { | ||
throw new TypeError('"value" argument must not be a number') | ||
} | ||
return that | ||
} | ||
function fromString (that, string, encoding) { | ||
if (typeof encoding !== 'string' || encoding === '') encoding = 'utf8' | ||
if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) { | ||
return fromArrayBuffer(that, value, encodingOrOffset, length) | ||
} | ||
// Assumption: byteLength() return value is always < kMaxLength. | ||
var length = byteLength(string, encoding) | 0 | ||
that = allocate(that, length) | ||
if (typeof value === 'string') { | ||
return fromString(that, value, encodingOrOffset) | ||
} | ||
that.write(string, encoding) | ||
return that | ||
return fromObject(that, value) | ||
} | ||
function fromObject (that, object) { | ||
if (Buffer.isBuffer(object)) return fromBuffer(that, object) | ||
/** | ||
* Functionally equivalent to Buffer(arg, encoding) but throws a TypeError | ||
* if value is a number. | ||
* Buffer.from(str[, encoding]) | ||
* Buffer.from(array) | ||
* Buffer.from(buffer) | ||
* Buffer.from(arrayBuffer[, byteOffset[, length]]) | ||
**/ | ||
Buffer.from = function (value, encodingOrOffset, length) { | ||
return from(null, value, encodingOrOffset, length) | ||
} | ||
if (isArray(object)) return fromArray(that, object) | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
Buffer.prototype.__proto__ = Uint8Array.prototype | ||
Buffer.__proto__ = Uint8Array | ||
if (typeof Symbol !== 'undefined' && Symbol.species && | ||
Buffer[Symbol.species] === Buffer) { | ||
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 | ||
Object.defineProperty(Buffer, Symbol.species, { | ||
value: null, | ||
configurable: true | ||
}) | ||
} | ||
} | ||
if (object == null) { | ||
throw new TypeError('must start with number, buffer, array or string') | ||
function assertSize (size) { | ||
if (typeof size !== 'number') { | ||
throw new TypeError('"size" argument must be a number') | ||
} | ||
} | ||
if (typeof ArrayBuffer !== 'undefined') { | ||
if (object.buffer instanceof ArrayBuffer) { | ||
return fromTypedArray(that, object) | ||
} | ||
if (object instanceof ArrayBuffer) { | ||
return fromArrayBuffer(that, object) | ||
} | ||
function alloc (that, size, fill, encoding) { | ||
assertSize(size) | ||
if (size <= 0) { | ||
return createBuffer(that, size) | ||
} | ||
if (object.length) return fromArrayLike(that, object) | ||
return fromJsonObject(that, object) | ||
if (fill !== undefined) { | ||
// Only pay attention to encoding if it's a string. This | ||
// prevents accidentally sending in a number that would | ||
// be interpretted as a start offset. | ||
return typeof encoding === 'string' | ||
? createBuffer(that, size).fill(fill, encoding) | ||
: createBuffer(that, size).fill(fill) | ||
} | ||
return createBuffer(that, size) | ||
} | ||
function fromBuffer (that, buffer) { | ||
var length = checked(buffer.length) | 0 | ||
that = allocate(that, length) | ||
buffer.copy(that, 0, 0, length) | ||
return that | ||
/** | ||
* Creates a new filled Buffer instance. | ||
* alloc(size[, fill[, encoding]]) | ||
**/ | ||
Buffer.alloc = function (size, fill, encoding) { | ||
return alloc(null, size, fill, encoding) | ||
} | ||
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 | ||
function allocUnsafe (that, size) { | ||
assertSize(size) | ||
that = createBuffer(that, size < 0 ? 0 : checked(size) | 0) | ||
if (!Buffer.TYPED_ARRAY_SUPPORT) { | ||
for (var i = 0; i < size; ++i) { | ||
that[i] = 0 | ||
} | ||
} | ||
@@ -296,26 +328,28 @@ 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 | ||
/** | ||
* Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance. | ||
* */ | ||
Buffer.allocUnsafe = function (size) { | ||
return allocUnsafe(null, size) | ||
} | ||
/** | ||
* Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance. | ||
*/ | ||
Buffer.allocUnsafeSlow = function (size) { | ||
return allocUnsafe(null, size) | ||
} | ||
function fromArrayBuffer (that, array) { | ||
array.byteLength // this throws if `array` is not a valid ArrayBuffer | ||
function fromString (that, string, encoding) { | ||
if (typeof encoding !== 'string' || encoding === '') { | ||
encoding = 'utf8' | ||
} | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
// Return an augmented `Uint8Array` instance, for best performance | ||
that = new Uint8Array(array) | ||
that.__proto__ = Buffer.prototype | ||
} else { | ||
// Fallback: Return an object instance of the Buffer class | ||
that = fromTypedArray(that, new Uint8Array(array)) | ||
if (!Buffer.isEncoding(encoding)) { | ||
throw new TypeError('"encoding" must be a valid string encoding') | ||
} | ||
var length = byteLength(string, encoding) | 0 | ||
that = createBuffer(that, length) | ||
that.write(string, encoding) | ||
return that | ||
@@ -326,3 +360,3 @@ } | ||
var length = checked(array.length) | 0 | ||
that = allocate(that, length) | ||
that = createBuffer(that, length) | ||
for (var i = 0; i < length; i += 1) { | ||
@@ -334,51 +368,60 @@ that[i] = array[i] & 255 | ||
// 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 | ||
function fromArrayBuffer (that, array, byteOffset, length) { | ||
array.byteLength // this throws if `array` is not a valid ArrayBuffer | ||
if (object.type === 'Buffer' && isArray(object.data)) { | ||
array = object.data | ||
length = checked(array.length) | 0 | ||
if (byteOffset < 0 || array.byteLength < byteOffset) { | ||
throw new RangeError('\'offset\' is out of bounds') | ||
} | ||
that = allocate(that, length) | ||
for (var i = 0; i < length; i += 1) { | ||
that[i] = array[i] & 255 | ||
if (array.byteLength < byteOffset + (length || 0)) { | ||
throw new RangeError('\'length\' is out of bounds') | ||
} | ||
return that | ||
} | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
Buffer.prototype.__proto__ = Uint8Array.prototype | ||
Buffer.__proto__ = Uint8Array | ||
if (typeof Symbol !== 'undefined' && Symbol.species && | ||
Buffer[Symbol.species] === Buffer) { | ||
// Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97 | ||
Object.defineProperty(Buffer, Symbol.species, { | ||
value: null, | ||
configurable: true | ||
}) | ||
if (byteOffset === undefined && length === undefined) { | ||
array = new Uint8Array(array) | ||
} else if (length === undefined) { | ||
array = new Uint8Array(array, byteOffset) | ||
} else { | ||
array = new Uint8Array(array, byteOffset, length) | ||
} | ||
} else { | ||
// pre-set for values that may exist in the future | ||
Buffer.prototype.length = undefined | ||
Buffer.prototype.parent = undefined | ||
} | ||
function allocate (that, length) { | ||
if (Buffer.TYPED_ARRAY_SUPPORT) { | ||
// Return an augmented `Uint8Array` instance, for best performance | ||
that = new Uint8Array(length) | ||
that = array | ||
that.__proto__ = Buffer.prototype | ||
} else { | ||
// Fallback: Return an object instance of the Buffer class | ||
that.length = length | ||
that = fromArrayLike(that, array) | ||
} | ||
return that | ||
} | ||
var fromPool = length !== 0 && length <= Buffer.poolSize >>> 1 | ||
if (fromPool) that.parent = rootParent | ||
function fromObject (that, obj) { | ||
if (Buffer.isBuffer(obj)) { | ||
var len = checked(obj.length) | 0 | ||
that = createBuffer(that, len) | ||
return that | ||
if (that.length === 0) { | ||
return that | ||
} | ||
obj.copy(that, 0, 0, len) | ||
return that | ||
} | ||
if (obj) { | ||
if ((typeof ArrayBuffer !== 'undefined' && | ||
obj.buffer instanceof ArrayBuffer) || 'length' in obj) { | ||
if (typeof obj.length !== 'number' || isnan(obj.length)) { | ||
return createBuffer(that, 0) | ||
} | ||
return fromArrayLike(that, obj) | ||
} | ||
if (obj.type === 'Buffer' && isArray(obj.data)) { | ||
return fromArrayLike(that, obj.data) | ||
} | ||
} | ||
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.') | ||
} | ||
@@ -396,8 +439,7 @@ | ||
function SlowBuffer (subject, encoding) { | ||
if (!(this instanceof SlowBuffer)) return new SlowBuffer(subject, encoding) | ||
var buf = new Buffer(subject, encoding) | ||
delete buf.parent | ||
return buf | ||
function SlowBuffer (length) { | ||
if (+length != length) { // eslint-disable-line eqeqeq | ||
length = 0 | ||
} | ||
return Buffer.alloc(+length) | ||
} | ||
@@ -419,15 +461,10 @@ | ||
var i = 0 | ||
var len = Math.min(x, y) | ||
while (i < len) { | ||
if (a[i] !== b[i]) break | ||
++i | ||
for (var i = 0, len = Math.min(x, y); i < len; ++i) { | ||
if (a[i] !== b[i]) { | ||
x = a[i] | ||
y = b[i] | ||
break | ||
} | ||
} | ||
if (i !== len) { | ||
x = a[i] | ||
y = b[i] | ||
} | ||
if (x < y) return -1 | ||
@@ -458,6 +495,8 @@ if (y < x) return 1 | ||
Buffer.concat = function concat (list, length) { | ||
if (!isArray(list)) throw new TypeError('list argument must be an Array of Buffers.') | ||
if (!isArray(list)) { | ||
throw new TypeError('"list" argument must be an Array of Buffers') | ||
} | ||
if (list.length === 0) { | ||
return new Buffer(0) | ||
return Buffer.alloc(0) | ||
} | ||
@@ -468,3 +507,3 @@ | ||
length = 0 | ||
for (i = 0; i < list.length; i++) { | ||
for (i = 0; i < list.length; ++i) { | ||
length += list[i].length | ||
@@ -474,14 +513,26 @@ } | ||
var buf = new Buffer(length) | ||
var buffer = Buffer.allocUnsafe(length) | ||
var pos = 0 | ||
for (i = 0; i < list.length; i++) { | ||
var item = list[i] | ||
item.copy(buf, pos) | ||
pos += item.length | ||
for (i = 0; i < list.length; ++i) { | ||
var buf = list[i] | ||
if (!Buffer.isBuffer(buf)) { | ||
throw new TypeError('"list" argument must be an Array of Buffers') | ||
} | ||
buf.copy(buffer, pos) | ||
pos += buf.length | ||
} | ||
return buf | ||
return buffer | ||
} | ||
function byteLength (string, encoding) { | ||
if (typeof string !== 'string') string = '' + string | ||
if (Buffer.isBuffer(string)) { | ||
return string.length | ||
} | ||
if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' && | ||
(ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) { | ||
return string.byteLength | ||
} | ||
if (typeof string !== 'string') { | ||
string = '' + string | ||
} | ||
@@ -497,3 +548,2 @@ var len = string.length | ||
case 'binary': | ||
// Deprecated | ||
case 'raw': | ||
@@ -504,2 +554,3 @@ case 'raws': | ||
case 'utf-8': | ||
case undefined: | ||
return utf8ToBytes(string).length | ||
@@ -527,9 +578,35 @@ case 'ucs2': | ||
start = start | 0 | ||
end = end === undefined || end === Infinity ? this.length : end | 0 | ||
// No need to verify that "this.length <= MAX_UINT32" since it's a read-only | ||
// property of a typed array. | ||
// This behaves neither like String nor Uint8Array in that we set start/end | ||
// to their upper/lower bounds if the value passed is out of range. | ||
// undefined is handled specially as per ECMA-262 6th Edition, | ||
// Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization. | ||
if (start === undefined || start < 0) { | ||
start = 0 | ||
} | ||
// Return early if start > this.length. Done here to prevent potential uint32 | ||
// coercion fail below. | ||
if (start > this.length) { | ||
return '' | ||
} | ||
if (end === undefined || end > this.length) { | ||
end = this.length | ||
} | ||
if (end <= 0) { | ||
return '' | ||
} | ||
// Force coersion to uint32. This will also coerce falsey/NaN values to 0. | ||
end >>>= 0 | ||
start >>>= 0 | ||
if (end <= start) { | ||
return '' | ||
} | ||
if (!encoding) encoding = 'utf8' | ||
if (start < 0) start = 0 | ||
if (end > this.length) end = this.length | ||
if (end <= start) return '' | ||
@@ -572,2 +649,31 @@ while (true) { | ||
function swap (b, n, m) { | ||
var i = b[n] | ||
b[n] = b[m] | ||
b[m] = i | ||
} | ||
Buffer.prototype.swap16 = function swap16 () { | ||
var len = this.length | ||
if (len % 2 !== 0) { | ||
throw new RangeError('Buffer size must be a multiple of 16-bits') | ||
} | ||
for (var i = 0; i < len; i += 2) { | ||
swap(this, i, i + 1) | ||
} | ||
return this | ||
} | ||
Buffer.prototype.swap32 = function swap32 () { | ||
var len = this.length | ||
if (len % 4 !== 0) { | ||
throw new RangeError('Buffer size must be a multiple of 32-bits') | ||
} | ||
for (var i = 0; i < len; i += 4) { | ||
swap(this, i, i + 3) | ||
swap(this, i + 1, i + 2) | ||
} | ||
return this | ||
} | ||
Buffer.prototype.toString = function toString () { | ||
@@ -596,11 +702,111 @@ var length = this.length | 0 | ||
Buffer.prototype.compare = function compare (b) { | ||
if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer') | ||
if (this === b) return 0 | ||
return Buffer.compare(this, b) | ||
Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) { | ||
if (!Buffer.isBuffer(target)) { | ||
throw new TypeError('Argument must be a Buffer') | ||
} | ||
if (start === undefined) { | ||
start = 0 | ||
} | ||
if (end === undefined) { | ||
end = target ? target.length : 0 | ||
} | ||
if (thisStart === undefined) { | ||
thisStart = 0 | ||
} | ||
if (thisEnd === undefined) { | ||
thisEnd = this.length | ||
} | ||
if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) { | ||
throw new RangeError('out of range index') | ||
} | ||
if (thisStart >= thisEnd && start >= end) { | ||
return 0 | ||
} | ||
if (thisStart >= thisEnd) { | ||
return -1 | ||
} | ||
if (start >= end) { | ||
return 1 | ||
} | ||
start >>>= 0 | ||
end >>>= 0 | ||
thisStart >>>= 0 | ||
thisEnd >>>= 0 | ||
if (this === target) return 0 | ||
var x = thisEnd - thisStart | ||
var y = end - start | ||
var len = Math.min(x, y) | ||
var thisCopy = this.slice(thisStart, thisEnd) | ||
var targetCopy = target.slice(start, end) | ||
for (var i = 0; i < len; ++i) { | ||
if (thisCopy[i] !== targetCopy[i]) { | ||
x = thisCopy[i] | ||
y = targetCopy[i] | ||
break | ||
} | ||
} | ||
if (x < y) return -1 | ||
if (y < x) return 1 | ||
return 0 | ||
} | ||
Buffer.prototype.indexOf = function indexOf (val, byteOffset) { | ||
if (byteOffset > 0x7fffffff) byteOffset = 0x7fffffff | ||
else if (byteOffset < -0x80000000) byteOffset = -0x80000000 | ||
function arrayIndexOf (arr, val, byteOffset, encoding) { | ||
var indexSize = 1 | ||
var arrLength = arr.length | ||
var valLength = val.length | ||
if (encoding !== undefined) { | ||
encoding = String(encoding).toLowerCase() | ||
if (encoding === 'ucs2' || encoding === 'ucs-2' || | ||
encoding === 'utf16le' || encoding === 'utf-16le') { | ||
if (arr.length < 2 || val.length < 2) { | ||
return -1 | ||
} | ||
indexSize = 2 | ||
arrLength /= 2 | ||
valLength /= 2 | ||
byteOffset /= 2 | ||
} | ||
} | ||
function read (buf, i) { | ||
if (indexSize === 1) { | ||
return buf[i] | ||
} else { | ||
return buf.readUInt16BE(i * indexSize) | ||
} | ||
} | ||
var foundIndex = -1 | ||
for (var i = byteOffset; i < arrLength; ++i) { | ||
if (read(arr, i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) { | ||
if (foundIndex === -1) foundIndex = i | ||
if (i - foundIndex + 1 === valLength) return foundIndex * indexSize | ||
} else { | ||
if (foundIndex !== -1) i -= i - foundIndex | ||
foundIndex = -1 | ||
} | ||
} | ||
return -1 | ||
} | ||
Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { | ||
if (typeof byteOffset === 'string') { | ||
encoding = byteOffset | ||
byteOffset = 0 | ||
} else if (byteOffset > 0x7fffffff) { | ||
byteOffset = 0x7fffffff | ||
} else if (byteOffset < -0x80000000) { | ||
byteOffset = -0x80000000 | ||
} | ||
byteOffset >>= 0 | ||
@@ -615,7 +821,11 @@ | ||
if (typeof val === 'string') { | ||
if (val.length === 0) return -1 // special case: looking for empty string always fails | ||
return String.prototype.indexOf.call(this, val, byteOffset) | ||
val = Buffer.from(val, encoding) | ||
} | ||
if (Buffer.isBuffer(val)) { | ||
return arrayIndexOf(this, val, byteOffset) | ||
// special case: looking for empty string/buffer always fails | ||
if (val.length === 0) { | ||
return -1 | ||
} | ||
return arrayIndexOf(this, val, byteOffset, encoding) | ||
} | ||
@@ -626,21 +836,12 @@ if (typeof val === 'number') { | ||
} | ||
return arrayIndexOf(this, [ val ], byteOffset) | ||
return arrayIndexOf(this, [ val ], byteOffset, encoding) | ||
} | ||
function arrayIndexOf (arr, val, byteOffset) { | ||
var foundIndex = -1 | ||
for (var i = 0; byteOffset + i < arr.length; i++) { | ||
if (arr[byteOffset + i] === val[foundIndex === -1 ? 0 : i - foundIndex]) { | ||
if (foundIndex === -1) foundIndex = i | ||
if (i - foundIndex + 1 === val.length) return byteOffset + foundIndex | ||
} else { | ||
foundIndex = -1 | ||
} | ||
} | ||
return -1 | ||
} | ||
throw new TypeError('val must be string, number or Buffer') | ||
} | ||
Buffer.prototype.includes = function includes (val, byteOffset, encoding) { | ||
return this.indexOf(val, byteOffset, encoding) !== -1 | ||
} | ||
function hexWrite (buf, string, offset, length) { | ||
@@ -665,5 +866,5 @@ offset = Number(offset) || 0 | ||
} | ||
for (var i = 0; i < length; i++) { | ||
for (var i = 0; i < length; ++i) { | ||
var parsed = parseInt(string.substr(i * 2, 2), 16) | ||
if (isNaN(parsed)) throw new Error('Invalid hex string') | ||
if (isNaN(parsed)) return i | ||
buf[offset + i] = parsed | ||
@@ -717,6 +918,5 @@ } | ||
} else { | ||
var swap = encoding | ||
encoding = offset | ||
offset = length | 0 | ||
length = swap | ||
throw new Error( | ||
'Buffer.write(string, encoding, offset[, length]) is no longer supported' | ||
) | ||
} | ||
@@ -728,3 +928,3 @@ | ||
if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) { | ||
throw new RangeError('attempt to write outside buffer bounds') | ||
throw new RangeError('Attempt to write outside buffer bounds') | ||
} | ||
@@ -883,3 +1083,3 @@ | ||
for (var i = start; i < end; i++) { | ||
for (var i = start; i < end; ++i) { | ||
ret += String.fromCharCode(buf[i] & 0x7F) | ||
@@ -894,3 +1094,3 @@ } | ||
for (var i = start; i < end; i++) { | ||
for (var i = start; i < end; ++i) { | ||
ret += String.fromCharCode(buf[i]) | ||
@@ -908,3 +1108,3 @@ } | ||
var out = '' | ||
for (var i = start; i < end; i++) { | ||
for (var i = start; i < end; ++i) { | ||
out += toHex(buf[i]) | ||
@@ -952,3 +1152,3 @@ } | ||
newBuf = new Buffer(sliceLen, undefined) | ||
for (var i = 0; i < sliceLen; i++) { | ||
for (var i = 0; i < sliceLen; ++i) { | ||
newBuf[i] = this[i + start] | ||
@@ -958,4 +1158,2 @@ } | ||
if (newBuf.length) newBuf.parent = this.parent || this | ||
return newBuf | ||
@@ -1129,5 +1327,5 @@ } | ||
function checkInt (buf, value, offset, ext, max, min) { | ||
if (!Buffer.isBuffer(buf)) throw new TypeError('buffer must be a Buffer instance') | ||
if (value > max || value < min) throw new RangeError('value is out of bounds') | ||
if (offset + ext > buf.length) throw new RangeError('index out of range') | ||
if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance') | ||
if (value > max || value < min) throw new RangeError('"value" argument is out of bounds') | ||
if (offset + ext > buf.length) throw new RangeError('Index out of range') | ||
} | ||
@@ -1139,3 +1337,6 @@ | ||
byteLength = byteLength | 0 | ||
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) | ||
if (!noAssert) { | ||
var maxBytes = Math.pow(2, 8 * byteLength) - 1 | ||
checkInt(this, value, offset, byteLength, maxBytes, 0) | ||
} | ||
@@ -1156,3 +1357,6 @@ var mul = 1 | ||
byteLength = byteLength | 0 | ||
if (!noAssert) checkInt(this, value, offset, byteLength, Math.pow(2, 8 * byteLength), 0) | ||
if (!noAssert) { | ||
var maxBytes = Math.pow(2, 8 * byteLength) - 1 | ||
checkInt(this, value, offset, byteLength, maxBytes, 0) | ||
} | ||
@@ -1180,3 +1384,3 @@ var i = byteLength - 1 | ||
if (value < 0) value = 0xffff + value + 1 | ||
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) { | ||
for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; ++i) { | ||
buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>> | ||
@@ -1215,3 +1419,3 @@ (littleEndian ? i : 1 - i) * 8 | ||
if (value < 0) value = 0xffffffff + value + 1 | ||
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) { | ||
for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; ++i) { | ||
buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff | ||
@@ -1262,5 +1466,8 @@ } | ||
var mul = 1 | ||
var sub = value < 0 ? 1 : 0 | ||
var sub = 0 | ||
this[offset] = value & 0xFF | ||
while (++i < byteLength && (mul *= 0x100)) { | ||
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) { | ||
sub = 1 | ||
} | ||
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF | ||
@@ -1283,5 +1490,8 @@ } | ||
var mul = 1 | ||
var sub = value < 0 ? 1 : 0 | ||
var sub = 0 | ||
this[offset + i] = value & 0xFF | ||
while (--i >= 0 && (mul *= 0x100)) { | ||
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) { | ||
sub = 1 | ||
} | ||
this[offset + i] = ((value / mul) >> 0) - sub & 0xFF | ||
@@ -1361,4 +1571,4 @@ } | ||
function checkIEEE754 (buf, value, offset, ext, max, min) { | ||
if (offset + ext > buf.length) throw new RangeError('index out of range') | ||
if (offset < 0) throw new RangeError('index out of range') | ||
if (offset + ext > buf.length) throw new RangeError('Index out of range') | ||
if (offset < 0) throw new RangeError('Index out of range') | ||
} | ||
@@ -1428,3 +1638,3 @@ | ||
// descending copy from end | ||
for (i = len - 1; i >= 0; i--) { | ||
for (i = len - 1; i >= 0; --i) { | ||
target[i + targetStart] = this[i + start] | ||
@@ -1434,3 +1644,3 @@ } | ||
// ascending copy from start | ||
for (i = 0; i < len; i++) { | ||
for (i = 0; i < len; ++i) { | ||
target[i + targetStart] = this[i + start] | ||
@@ -1449,27 +1659,59 @@ } | ||
// fill(value, start=0, end=buffer.length) | ||
Buffer.prototype.fill = function fill (value, start, end) { | ||
if (!value) value = 0 | ||
if (!start) start = 0 | ||
if (!end) end = this.length | ||
// Usage: | ||
// buffer.fill(number[, offset[, end]]) | ||
// buffer.fill(buffer[, offset[, end]]) | ||
// buffer.fill(string[, offset[, end]][, encoding]) | ||
Buffer.prototype.fill = function fill (val, start, end, encoding) { | ||
// Handle string cases: | ||
if (typeof val === 'string') { | ||
if (typeof start === 'string') { | ||
encoding = start | ||
start = 0 | ||
end = this.length | ||
} else if (typeof end === 'string') { | ||
encoding = end | ||
end = this.length | ||
} | ||
if (val.length === 1) { | ||
var code = val.charCodeAt(0) | ||
if (code < 256) { | ||
val = code | ||
} | ||
} | ||
if (encoding !== undefined && typeof encoding !== 'string') { | ||
throw new TypeError('encoding must be a string') | ||
} | ||
if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) { | ||
throw new TypeError('Unknown encoding: ' + encoding) | ||
} | ||
} else if (typeof val === 'number') { | ||
val = val & 255 | ||
} | ||
if (end < start) throw new RangeError('end < start') | ||
// Invalid ranges are not set to a default, so can range check early. | ||
if (start < 0 || this.length < start || this.length < end) { | ||
throw new RangeError('Out of range index') | ||
} | ||
// Fill 0 bytes; we're done | ||
if (end === start) return | ||
if (this.length === 0) return | ||
if (end <= start) { | ||
return this | ||
} | ||
if (start < 0 || start >= this.length) throw new RangeError('start out of bounds') | ||
if (end < 0 || end > this.length) throw new RangeError('end out of bounds') | ||
start = start >>> 0 | ||
end = end === undefined ? this.length : end >>> 0 | ||
if (!val) val = 0 | ||
var i | ||
if (typeof value === 'number') { | ||
for (i = start; i < end; i++) { | ||
this[i] = value | ||
if (typeof val === 'number') { | ||
for (i = start; i < end; ++i) { | ||
this[i] = val | ||
} | ||
} else { | ||
var bytes = utf8ToBytes(value.toString()) | ||
var bytes = Buffer.isBuffer(val) | ||
? val | ||
: utf8ToBytes(new Buffer(val, encoding).toString()) | ||
var len = bytes.length | ||
for (i = start; i < end; i++) { | ||
this[i] = bytes[i % len] | ||
for (i = 0; i < end - start; ++i) { | ||
this[i + start] = bytes[i % len] | ||
} | ||
@@ -1515,3 +1757,3 @@ } | ||
for (var i = 0; i < length; i++) { | ||
for (var i = 0; i < length; ++i) { | ||
codePoint = string.charCodeAt(i) | ||
@@ -1591,3 +1833,3 @@ | ||
var byteArray = [] | ||
for (var i = 0; i < str.length; i++) { | ||
for (var i = 0; i < str.length; ++i) { | ||
// Node's code seems to be doing this and not & 0x7F.. | ||
@@ -1602,3 +1844,3 @@ byteArray.push(str.charCodeAt(i) & 0xFF) | ||
var byteArray = [] | ||
for (var i = 0; i < str.length; i++) { | ||
for (var i = 0; i < str.length; ++i) { | ||
if ((units -= 2) < 0) break | ||
@@ -1621,3 +1863,3 @@ | ||
function blitBuffer (src, dst, offset, length) { | ||
for (var i = 0; i < length; i++) { | ||
for (var i = 0; i < length; ++i) { | ||
if ((i + offset >= dst.length) || (i >= src.length)) break | ||
@@ -1629,11 +1871,8 @@ dst[i + offset] = src[i] | ||
function isnan (val) { | ||
return val !== val // eslint-disable-line no-self-compare | ||
} | ||
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
},{"base64-js":1,"ieee754":4,"isarray":3}],3:[function(_dereq_,___mod,_expor_){ | ||
var toString = {}.toString; | ||
___mod.exports = Array.isArray || function (arr) { | ||
return toString.call(arr) == '[object Array]'; | ||
}; | ||
},{}],4:[function(_dereq_,___mod,_expor_){ | ||
},{"base64-js":1,"ieee754":3,"isarray":4}],3:[function(_dereq_,___mod,_expor_){ | ||
_expor_.read = function (buffer, offset, isLE, mLen, nBytes) { | ||
@@ -1724,2 +1963,9 @@ var e, m | ||
},{}],4:[function(_dereq_,___mod,_expor_){ | ||
var toString = {}.toString; | ||
___mod.exports = Array.isArray || function (arr) { | ||
return toString.call(arr) == '[object Array]'; | ||
}; | ||
},{}],5:[function(_dereq_,___mod,_expor_){ | ||
@@ -1726,0 +1972,0 @@ var buf = _dereq_('buffer'); |
@@ -12,3 +12,2 @@ 'use strict'; | ||
var GLOBAL_PATH = path.join(__dirname, '..', 'src', 'global.js'); | ||
var FILEPATH_PATH = path.join(__dirname, '..', 'src', 'filepath-placeholder.js'); | ||
@@ -18,3 +17,7 @@ function clone(obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
out[key] = obj[key]; | ||
if (Array.isArray(obj[key])) { | ||
out[key] = obj[key].slice(); | ||
} else { | ||
out[key] = obj[key]; | ||
} | ||
}); | ||
@@ -47,2 +50,5 @@ return out; | ||
var opts = clone(options); | ||
opts.exclude = opts.exclude || []; | ||
opts.exclude.push(GLOBAL_PATH); | ||
opts.exclude.push(BUFFER_PATH); | ||
opts.modules = { | ||
@@ -60,4 +66,4 @@ process: PROCESS_PATH, | ||
}; | ||
}) | ||
}); | ||
module.exports = index; |
@@ -8,3 +8,2 @@ import inject from 'rollup-plugin-inject'; | ||
var GLOBAL_PATH = join(__dirname, '..', 'src', 'global.js'); | ||
var FILEPATH_PATH = join(__dirname, '..', 'src', 'filepath-placeholder.js'); | ||
@@ -14,3 +13,7 @@ function clone(obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
out[key] = obj[key]; | ||
if (Array.isArray(obj[key])) { | ||
out[key] = obj[key].slice(); | ||
} else { | ||
out[key] = obj[key]; | ||
} | ||
}); | ||
@@ -43,2 +46,5 @@ return out; | ||
var opts = clone(options); | ||
opts.exclude = opts.exclude || []; | ||
opts.exclude.push(GLOBAL_PATH); | ||
opts.exclude.push(BUFFER_PATH); | ||
opts.modules = { | ||
@@ -56,4 +62,4 @@ process: PROCESS_PATH, | ||
}; | ||
}) | ||
}); | ||
export default index; |
@@ -5,3 +5,3 @@ { | ||
"jsnext:main": "dist/rollup-plugin-node-globals.es6.js", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "insert the same globals browserify does", | ||
@@ -18,4 +18,3 @@ "files": [ | ||
"create-buffer": "browserify src/buffer-raw.js | derequire | derequire --from module --to ___mod | derequire --from exports --to _expor_ > tmp/buffer-raw.js", | ||
"prebuild": "rm -rf dist && rm -rf tmp && mkdir tmp && mkdir dist && npm run create-buffer && cat src/buffer-prefix.js tmp/buffer-raw.js src/buffer-suffix.js > dist/buffer.js && rm -rf tmp", | ||
"prepublish": "npm test" | ||
"prebuild": "rimraf dist tmp && mkdirp tmp dist && npm run create-buffer && cat src/buffer-prefix.js tmp/buffer-raw.js src/buffer-suffix.js > dist/buffer.js && rimraf tmp" | ||
}, | ||
@@ -34,3 +33,5 @@ "author": "Calvin Metcalf <calvin.metcalf@gmail.com>", | ||
"browserify": "^13.0.0", | ||
"mkdirp": "^0.5.1", | ||
"mocha": "^2.4.5", | ||
"rimraf": "^2.5.4", | ||
"rollup": "^0.25.4", | ||
@@ -37,0 +38,0 @@ "rollup-plugin-babel": "^2.4.0" |
@@ -8,3 +8,2 @@ import inject from 'rollup-plugin-inject'; | ||
const GLOBAL_PATH = join(__dirname, '..', 'src', 'global.js'); | ||
const FILEPATH_PATH = join(__dirname, '..', 'src', 'filepath-placeholder.js'); | ||
@@ -14,3 +13,7 @@ function clone (obj) { | ||
Object.keys(obj).forEach(function (key) { | ||
out[key] = obj[key]; | ||
if (Array.isArray(obj[key])) { | ||
out[key] = obj[key].slice(); | ||
} else { | ||
out[key] = obj[key]; | ||
} | ||
}); | ||
@@ -43,2 +46,5 @@ return out; | ||
var opts = clone(options); | ||
opts.exclude = opts.exclude || []; | ||
opts.exclude.push(GLOBAL_PATH); | ||
opts.exclude.push(BUFFER_PATH); | ||
opts.modules = { | ||
@@ -45,0 +51,0 @@ process: PROCESS_PATH, |
62027
1843
7
10