native-buffer-browserify
Advanced tools
Comparing version 0.5.0 to 1.0.0
249
index.js
@@ -1,3 +0,1 @@ | ||
var assert | ||
exports.Buffer = Buffer | ||
@@ -8,4 +6,20 @@ exports.SlowBuffer = Buffer | ||
// TODO: throw the correct exception type on errors (look at node source) | ||
var assert | ||
var browserSupport | ||
/** | ||
* Class: Buffer | ||
* ============= | ||
* | ||
* The Buffer constructor returns instances of `Uint8Array` that are augmented | ||
* with function properties for all the node `Buffer` API functions. We use | ||
* `Uint8Array` so that square bracket notation works as expected -- it returns | ||
* a single octet. | ||
* | ||
* By augmenting the instances, we can avoid modifying the `Uint8Array` | ||
* prototype. | ||
* | ||
* Firefox is a special case because it doesn't allow augmenting "native" object | ||
* instances. See `ProxyBuffer` below for more details. | ||
*/ | ||
function Buffer (subject, encoding) { | ||
@@ -77,3 +91,3 @@ if (!assert) assert = require('assert') | ||
Buffer.isBuffer = function isBuffer (b) { | ||
return b instanceof Uint8Array && b._isBuffer | ||
return b && b._isBuffer | ||
} | ||
@@ -236,5 +250,11 @@ | ||
function BufferToString (encoding, start, end) { | ||
var self = (this instanceof ProxyBuffer) | ||
? this._proxy | ||
: this | ||
encoding = String(encoding || 'utf8').toLowerCase() | ||
start = Number(start) || 0 | ||
end = Number(end) || this.length | ||
end = (end !== undefined) | ||
? Number(end) | ||
: end = self.length | ||
@@ -247,16 +267,16 @@ // Fastpath empty strings | ||
case 'hex': | ||
return _hexSlice(this, start, end) | ||
return _hexSlice(self, start, end) | ||
case 'utf8': | ||
case 'utf-8': | ||
return _utf8Slice(this, start, end) | ||
return _utf8Slice(self, start, end) | ||
case 'ascii': | ||
return _asciiSlice(this, start, end) | ||
return _asciiSlice(self, start, end) | ||
case 'binary': | ||
return _binarySlice(this, start, end) | ||
return _binarySlice(self, start, end) | ||
case 'base64': | ||
return _base64Slice(this, start, end) | ||
return _base64Slice(self, start, end) | ||
@@ -269,3 +289,6 @@ default: | ||
function BufferToJSON () { | ||
return Array.prototype.slice.call(this, 0) | ||
return { | ||
type: 'Buffer', | ||
data: Array.prototype.slice.call(this, 0) | ||
} | ||
} | ||
@@ -823,51 +846,171 @@ | ||
function augment (arr) { | ||
// Augment the Uint8Array *instance* (not the class!) with Buffer methods | ||
arr.write = BufferWrite | ||
/** | ||
* Check to see if the browser supports augmenting a `Uint8Array` instance. | ||
* @return {boolean} | ||
*/ | ||
function _browserSupport () { | ||
var arr = new Uint8Array(0) | ||
arr.foo = function () { return 42 } | ||
arr.toString = BufferToString | ||
arr.toLocaleString = BufferToString | ||
arr.toJSON = BufferToJSON | ||
arr.copy = BufferCopy | ||
arr.slice = BufferSlice | ||
arr.readUInt8 = BufferReadUInt8 | ||
arr.readUInt16LE = BufferReadUInt16LE | ||
arr.readUInt16BE = BufferReadUInt16BE | ||
arr.readUInt32LE = BufferReadUInt32LE | ||
arr.readUInt32BE = BufferReadUInt32BE | ||
arr.readInt8 = BufferReadInt8 | ||
arr.readInt16LE = BufferReadInt16LE | ||
arr.readInt16BE = BufferReadInt16BE | ||
arr.readInt32LE = BufferReadInt32LE | ||
arr.readInt32BE = BufferReadInt32BE | ||
arr.readFloatLE = BufferReadFloatLE | ||
arr.readFloatBE = BufferReadFloatBE | ||
arr.readDoubleLE = BufferReadDoubleLE | ||
arr.readDoubleBE = BufferReadDoubleBE | ||
arr.writeUInt8 = BufferWriteUInt8 | ||
arr.writeUInt16LE = BufferWriteUInt16LE | ||
arr.writeUInt16BE = BufferWriteUInt16BE | ||
arr.writeUInt32LE = BufferWriteUInt32LE | ||
arr.writeUInt32BE = BufferWriteUInt32BE | ||
arr.writeInt8 = BufferWriteInt8 | ||
arr.writeInt16LE = BufferWriteInt16LE | ||
arr.writeInt16BE = BufferWriteInt16BE | ||
arr.writeInt32LE = BufferWriteInt32LE | ||
arr.writeInt32BE = BufferWriteInt32BE | ||
arr.writeFloatLE = BufferWriteFloatLE | ||
arr.writeFloatBE = BufferWriteFloatBE | ||
arr.writeDoubleLE = BufferWriteDoubleLE | ||
arr.writeDoubleBE = BufferWriteDoubleBE | ||
arr.fill = BufferFill | ||
arr.inspect = BufferInspect | ||
arr.toArrayBuffer = BufferToArrayBuffer | ||
try { | ||
return (42 === arr.foo()) | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
arr._isBuffer = true | ||
/** | ||
* Class: ProxyBuffer | ||
* ================== | ||
* | ||
* Only used in Firefox, since Firefox does not allow augmenting "native" | ||
* objects (like Uint8Array instances) with new properties for some unknown | ||
* (probably silly) reason. So we'll use an ES6 Proxy (supported since | ||
* Firefox 18) to wrap the Uint8Array instance without actually adding any | ||
* properties to it. | ||
* | ||
* Instances of this "fake" Buffer class are the "target" of the | ||
* ES6 Proxy (see `augment` function). | ||
* | ||
* We couldn't just use the `Uint8Array` as the target of the `Proxy` because | ||
* Proxies have an important limitation on trapping the `toString` method. | ||
* `Object.prototype.toString.call(proxy)` gets called whenever something is | ||
* implicitly cast to a String. Unfortunately, with a `Proxy` this | ||
* unconditionally returns `Object.prototype.toString.call(target)` which would | ||
* always return "[object Uint8Array]" if we used the `Uint8Array` instance as | ||
* the target. And, remember, in Firefox we cannot redefine the `Uint8Array` | ||
* instance's `toString` method. | ||
* | ||
* So, we use this `ProxyBuffer` class as the proxy's "target". Since this class | ||
* has its own custom `toString` method, it will get called whenever `toString` | ||
* gets called, implicitly or explicitly, on the `Proxy` instance. | ||
* | ||
* We also have to define the Uint8Array methods `subarray` and `set` on | ||
* `ProxyBuffer` because if we didn't then `proxy.subarray(0)` would have its | ||
* `this` set to `proxy` (a `Proxy` instance) which throws an exception in | ||
* Firefox which expects it to be a `TypedArray` instance. | ||
*/ | ||
function ProxyBuffer (arr) { | ||
this._arr = arr | ||
if (arr.byteLength !== 0) | ||
arr._dataview = new DataView(arr.buffer, arr.byteOffset, arr.byteLength) | ||
this._dataview = new DataView(arr.buffer, arr.byteOffset, arr.byteLength) | ||
} | ||
return arr | ||
ProxyBuffer.prototype.write = BufferWrite | ||
ProxyBuffer.prototype.toString = BufferToString | ||
ProxyBuffer.prototype.toLocaleString = BufferToString | ||
ProxyBuffer.prototype.toJSON = BufferToJSON | ||
ProxyBuffer.prototype.copy = BufferCopy | ||
ProxyBuffer.prototype.slice = BufferSlice | ||
ProxyBuffer.prototype.readUInt8 = BufferReadUInt8 | ||
ProxyBuffer.prototype.readUInt16LE = BufferReadUInt16LE | ||
ProxyBuffer.prototype.readUInt16BE = BufferReadUInt16BE | ||
ProxyBuffer.prototype.readUInt32LE = BufferReadUInt32LE | ||
ProxyBuffer.prototype.readUInt32BE = BufferReadUInt32BE | ||
ProxyBuffer.prototype.readInt8 = BufferReadInt8 | ||
ProxyBuffer.prototype.readInt16LE = BufferReadInt16LE | ||
ProxyBuffer.prototype.readInt16BE = BufferReadInt16BE | ||
ProxyBuffer.prototype.readInt32LE = BufferReadInt32LE | ||
ProxyBuffer.prototype.readInt32BE = BufferReadInt32BE | ||
ProxyBuffer.prototype.readFloatLE = BufferReadFloatLE | ||
ProxyBuffer.prototype.readFloatBE = BufferReadFloatBE | ||
ProxyBuffer.prototype.readDoubleLE = BufferReadDoubleLE | ||
ProxyBuffer.prototype.readDoubleBE = BufferReadDoubleBE | ||
ProxyBuffer.prototype.writeUInt8 = BufferWriteUInt8 | ||
ProxyBuffer.prototype.writeUInt16LE = BufferWriteUInt16LE | ||
ProxyBuffer.prototype.writeUInt16BE = BufferWriteUInt16BE | ||
ProxyBuffer.prototype.writeUInt32LE = BufferWriteUInt32LE | ||
ProxyBuffer.prototype.writeUInt32BE = BufferWriteUInt32BE | ||
ProxyBuffer.prototype.writeInt8 = BufferWriteInt8 | ||
ProxyBuffer.prototype.writeInt16LE = BufferWriteInt16LE | ||
ProxyBuffer.prototype.writeInt16BE = BufferWriteInt16BE | ||
ProxyBuffer.prototype.writeInt32LE = BufferWriteInt32LE | ||
ProxyBuffer.prototype.writeInt32BE = BufferWriteInt32BE | ||
ProxyBuffer.prototype.writeFloatLE = BufferWriteFloatLE | ||
ProxyBuffer.prototype.writeFloatBE = BufferWriteFloatBE | ||
ProxyBuffer.prototype.writeDoubleLE = BufferWriteDoubleLE | ||
ProxyBuffer.prototype.writeDoubleBE = BufferWriteDoubleBE | ||
ProxyBuffer.prototype.fill = BufferFill | ||
ProxyBuffer.prototype.inspect = BufferInspect | ||
ProxyBuffer.prototype.toArrayBuffer = BufferToArrayBuffer | ||
ProxyBuffer.prototype._isBuffer = true | ||
ProxyBuffer.prototype.subarray = function () { | ||
return this._arr.subarray.apply(this._arr, arguments) | ||
} | ||
ProxyBuffer.prototype.set = function () { | ||
return this._arr.set.apply(this._arr, arguments) | ||
} | ||
var ProxyHandler = { | ||
get: function (target, name) { | ||
if (name in target) return target[name] | ||
else return target._arr[name] | ||
}, | ||
set: function (target, name, value) { | ||
target._arr[name] = value | ||
} | ||
} | ||
function augment (arr) { | ||
if (browserSupport === undefined) { | ||
browserSupport = _browserSupport() | ||
} | ||
if (browserSupport) { | ||
// Augment the Uint8Array *instance* (not the class!) with Buffer methods | ||
arr.write = BufferWrite | ||
arr.toString = BufferToString | ||
arr.toLocaleString = BufferToString | ||
arr.toJSON = BufferToJSON | ||
arr.copy = BufferCopy | ||
arr.slice = BufferSlice | ||
arr.readUInt8 = BufferReadUInt8 | ||
arr.readUInt16LE = BufferReadUInt16LE | ||
arr.readUInt16BE = BufferReadUInt16BE | ||
arr.readUInt32LE = BufferReadUInt32LE | ||
arr.readUInt32BE = BufferReadUInt32BE | ||
arr.readInt8 = BufferReadInt8 | ||
arr.readInt16LE = BufferReadInt16LE | ||
arr.readInt16BE = BufferReadInt16BE | ||
arr.readInt32LE = BufferReadInt32LE | ||
arr.readInt32BE = BufferReadInt32BE | ||
arr.readFloatLE = BufferReadFloatLE | ||
arr.readFloatBE = BufferReadFloatBE | ||
arr.readDoubleLE = BufferReadDoubleLE | ||
arr.readDoubleBE = BufferReadDoubleBE | ||
arr.writeUInt8 = BufferWriteUInt8 | ||
arr.writeUInt16LE = BufferWriteUInt16LE | ||
arr.writeUInt16BE = BufferWriteUInt16BE | ||
arr.writeUInt32LE = BufferWriteUInt32LE | ||
arr.writeUInt32BE = BufferWriteUInt32BE | ||
arr.writeInt8 = BufferWriteInt8 | ||
arr.writeInt16LE = BufferWriteInt16LE | ||
arr.writeInt16BE = BufferWriteInt16BE | ||
arr.writeInt32LE = BufferWriteInt32LE | ||
arr.writeInt32BE = BufferWriteInt32BE | ||
arr.writeFloatLE = BufferWriteFloatLE | ||
arr.writeFloatBE = BufferWriteFloatBE | ||
arr.writeDoubleLE = BufferWriteDoubleLE | ||
arr.writeDoubleBE = BufferWriteDoubleBE | ||
arr.fill = BufferFill | ||
arr.inspect = BufferInspect | ||
arr.toArrayBuffer = BufferToArrayBuffer | ||
arr._isBuffer = true | ||
if (arr.byteLength !== 0) | ||
arr._dataview = new DataView(arr.buffer, arr.byteOffset, arr.byteLength) | ||
return arr | ||
} else { | ||
// This is a browser that doesn't support augmenting the `Uint8Array` | ||
// instance (*ahem* Firefox) so use an ES6 `Proxy`. | ||
var proxyBuffer = new ProxyBuffer(arr) | ||
var proxy = new Proxy(proxyBuffer, ProxyHandler) | ||
proxyBuffer._proxy = proxy | ||
return proxy | ||
} | ||
} | ||
// slice(start, end) | ||
@@ -874,0 +1017,0 @@ function clamp (index, len, defaultValue) { |
{ | ||
"name": "native-buffer-browserify", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "buffer module compatibility for browserify (backed by ArrayBuffer so its fast!)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
var benchmark = require('benchmark') | ||
var suite = new benchmark.Suite() | ||
global.NewBuffer = require('../').Buffer // buffer-browserify-fast | ||
global.NewBuffer = require('../').Buffer // native-buffer-browserify | ||
global.OldBuffer = require('buffer-browserify').Buffer // buffer-browserify | ||
@@ -6,0 +6,0 @@ |
@@ -21,3 +21,3 @@ native-buffer-browserify | ||
- All tests from the original `buffer-browserify` project pass. | ||
- Requires browsers to have `Uint8Array` support (IE9 and up) | ||
- Requires browsers to have `Uint8Array` and `DataView` support (all modern browsers, IE10+) | ||
@@ -31,3 +31,2 @@ | ||
For browsers that don't support `Uint8Array`, we fallback to the (old) `buffer-browserify`. | ||
@@ -34,0 +33,0 @@ |
@@ -41,3 +41,3 @@ var B = require('../index.js').Buffer | ||
new B(data).toJSON(), | ||
new Buffer(data).toJSON() | ||
{ type: 'Buffer', data: [1,2,3,4] } | ||
) | ||
@@ -44,0 +44,0 @@ t.end() |
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
57280
1578
1
127