Giter Club home page Giter Club logo

buffer's Introduction

buffer ci npm downloads javascript style guide

The buffer module from node.js, for the browser.

saucelabs

With browserify, simply require('buffer') or use the Buffer global and you will get this module.

The goal is to provide an API that is 100% identical to node's Buffer API. Read the official docs for the full list of properties, instance methods, and class methods that are supported.

features

  • Manipulate binary data like a boss, in all browsers!
  • Super fast. Backed by Typed Arrays (Uint8Array/ArrayBuffer, not Object)
  • Extremely small bundle size (6.75KB minified + gzipped, 51.9KB with comments)
  • Excellent browser support (Chrome, Firefox, Edge, Safari 11+, iOS 11+, Android, etc.)
  • Preserves Node API exactly
  • Square-bracket buf[4] notation works!
  • Does not modify any browser prototypes or put anything on window
  • Comprehensive test suite (including all buffer tests from node.js core)

install

To use this module directly (without browserify), install it:

npm install buffer

This module was previously called native-buffer-browserify, but please use buffer from now on.

If you do not use a bundler, you can use the standalone script.

usage

The module's API is identical to node's Buffer API. Read the official docs for the full list of properties, instance methods, and class methods that are supported.

As mentioned above, require('buffer') or use the Buffer global with browserify and this module will automatically be included in your bundle. Almost any npm module will work in the browser, even if it assumes that the node Buffer API will be available.

To depend on this module explicitly (without browserify), require it like this:

var Buffer = require('buffer/').Buffer  // note: the trailing slash is important!

To require this module explicitly, use require('buffer/') which tells the node.js module lookup algorithm (also used by browserify) to use the npm module named buffer instead of the node.js core module named buffer!

how does it work?

The Buffer constructor returns instances of Uint8Array that have their prototype changed to Buffer.prototype. Furthermore, Buffer is a subclass of Uint8Array, so the returned instances will have all the node Buffer methods and the Uint8Array methods. Square bracket notation works as expected -- it returns a single octet.

The Uint8Array prototype remains unmodified.

tracking the latest node api

This module tracks the Buffer API in the latest (unstable) version of node.js. The Buffer API is considered stable in the node stability index, so it is unlikely that there will ever be breaking changes. Nonetheless, when/if the Buffer API changes in node, this module's API will change accordingly.

related packages

conversion packages

convert typed array to buffer

Use typedarray-to-buffer to convert any kind of typed array to a Buffer. Does not perform a copy, so it's super fast.

convert buffer to typed array

Buffer is a subclass of Uint8Array (which is a typed array). So there is no need to explicitly convert to typed array. Just use the buffer as a Uint8Array.

convert blob to buffer

Use blob-to-buffer to convert a Blob to a Buffer.

convert buffer to blob

To convert a Buffer to a Blob, use the Blob constructor:

var blob = new Blob([ buffer ])

Optionally, specify a mimetype:

var blob = new Blob([ buffer ], { type: 'text/html' })

convert arraybuffer to buffer

To convert an ArrayBuffer to a Buffer, use the Buffer.from function. Does not perform a copy, so it's super fast.

var buffer = Buffer.from(arrayBuffer)

convert buffer to arraybuffer

To convert a Buffer to an ArrayBuffer, use the .buffer property (which is present on all Uint8Array objects):

var arrayBuffer = buffer.buffer.slice(
  buffer.byteOffset, buffer.byteOffset + buffer.byteLength
)

Alternatively, use the to-arraybuffer module.

performance

See perf tests in /perf.

BrowserBuffer is the browser buffer module (this repo). Uint8Array is included as an additional check (since BrowserBuffer uses Uint8Array under the hood, Uint8Array will always be at least a bit faster). Finally, NodeBuffer is the node.js buffer module, which is included to compare against.

NOTE: Performance has improved since these benchmarks were taken. PR welcome to update the README.

Chrome 38

Method Operations Accuracy Sampled Fastest
BrowserBuffer#bracket-notation 11,457,464 ops/sec ±0.86% 66
Uint8Array#bracket-notation 10,824,332 ops/sec ±0.74% 65
BrowserBuffer#concat 450,532 ops/sec ±0.76% 68
Uint8Array#concat 1,368,911 ops/sec ±1.50% 62
BrowserBuffer#copy(16000) 903,001 ops/sec ±0.96% 67
Uint8Array#copy(16000) 1,422,441 ops/sec ±1.04% 66
BrowserBuffer#copy(16) 11,431,358 ops/sec ±0.46% 69
Uint8Array#copy(16) 13,944,163 ops/sec ±1.12% 68
BrowserBuffer#new(16000) 106,329 ops/sec ±6.70% 44
Uint8Array#new(16000) 131,001 ops/sec ±2.85% 31
BrowserBuffer#new(16) 1,554,491 ops/sec ±1.60% 65
Uint8Array#new(16) 6,623,930 ops/sec ±1.66% 65
BrowserBuffer#readDoubleBE 112,830 ops/sec ±0.51% 69
DataView#getFloat64 93,500 ops/sec ±0.57% 68
BrowserBuffer#readFloatBE 146,678 ops/sec ±0.95% 68
DataView#getFloat32 99,311 ops/sec ±0.41% 67
BrowserBuffer#readUInt32LE 843,214 ops/sec ±0.70% 69
DataView#getUint32 103,024 ops/sec ±0.64% 67
BrowserBuffer#slice 1,013,941 ops/sec ±0.75% 67
Uint8Array#subarray 1,903,928 ops/sec ±0.53% 67
BrowserBuffer#writeFloatBE 61,387 ops/sec ±0.90% 67
DataView#setFloat32 141,249 ops/sec ±0.40% 66

Firefox 33

Method Operations Accuracy Sampled Fastest
BrowserBuffer#bracket-notation 20,800,421 ops/sec ±1.84% 60
Uint8Array#bracket-notation 20,826,235 ops/sec ±2.02% 61
BrowserBuffer#concat 153,076 ops/sec ±2.32% 61
Uint8Array#concat 1,255,674 ops/sec ±8.65% 52
BrowserBuffer#copy(16000) 1,105,312 ops/sec ±1.16% 63
Uint8Array#copy(16000) 1,615,911 ops/sec ±0.55% 66
BrowserBuffer#copy(16) 16,357,599 ops/sec ±0.73% 68
Uint8Array#copy(16) 31,436,281 ops/sec ±1.05% 68
BrowserBuffer#new(16000) 52,995 ops/sec ±6.01% 35
Uint8Array#new(16000) 87,686 ops/sec ±5.68% 45
BrowserBuffer#new(16) 252,031 ops/sec ±1.61% 66
Uint8Array#new(16) 8,477,026 ops/sec ±0.49% 68
BrowserBuffer#readDoubleBE 99,871 ops/sec ±0.41% 69
DataView#getFloat64 285,663 ops/sec ±0.70% 68
BrowserBuffer#readFloatBE 115,540 ops/sec ±0.42% 69
DataView#getFloat32 288,722 ops/sec ±0.82% 68
BrowserBuffer#readUInt32LE 633,926 ops/sec ±1.08% 67
DataView#getUint32 294,808 ops/sec ±0.79% 64
BrowserBuffer#slice 349,425 ops/sec ±0.46% 69
Uint8Array#subarray 5,965,819 ops/sec ±0.60% 65
BrowserBuffer#writeFloatBE 59,980 ops/sec ±0.41% 67
DataView#setFloat32 317,634 ops/sec ±0.63% 68

Safari 8

Method Operations Accuracy Sampled Fastest
BrowserBuffer#bracket-notation 10,279,729 ops/sec ±2.25% 56
Uint8Array#bracket-notation 10,030,767 ops/sec ±2.23% 59
BrowserBuffer#concat 144,138 ops/sec ±1.38% 65
Uint8Array#concat 4,950,764 ops/sec ±1.70% 63
BrowserBuffer#copy(16000) 1,058,548 ops/sec ±1.51% 64
Uint8Array#copy(16000) 1,409,666 ops/sec ±1.17% 65
BrowserBuffer#copy(16) 6,282,529 ops/sec ±1.88% 58
Uint8Array#copy(16) 11,907,128 ops/sec ±2.87% 58
BrowserBuffer#new(16000) 101,663 ops/sec ±3.89% 57
Uint8Array#new(16000) 22,050,818 ops/sec ±6.51% 46
BrowserBuffer#new(16) 176,072 ops/sec ±2.13% 64
Uint8Array#new(16) 24,385,731 ops/sec ±5.01% 51
BrowserBuffer#readDoubleBE 41,341 ops/sec ±1.06% 67
DataView#getFloat64 322,280 ops/sec ±0.84% 68
BrowserBuffer#readFloatBE 46,141 ops/sec ±1.06% 65
DataView#getFloat32 337,025 ops/sec ±0.43% 69
BrowserBuffer#readUInt32LE 151,551 ops/sec ±1.02% 66
DataView#getUint32 308,278 ops/sec ±0.94% 67
BrowserBuffer#slice 197,365 ops/sec ±0.95% 66
Uint8Array#subarray 9,558,024 ops/sec ±3.08% 58
BrowserBuffer#writeFloatBE 17,518 ops/sec ±1.03% 63
DataView#setFloat32 319,751 ops/sec ±0.48% 68

Node 0.11.14

Method Operations Accuracy Sampled Fastest
BrowserBuffer#bracket-notation 10,489,828 ops/sec ±3.25% 90
Uint8Array#bracket-notation 10,534,884 ops/sec ±0.81% 92
NodeBuffer#bracket-notation 10,389,910 ops/sec ±0.97% 87
BrowserBuffer#concat 487,830 ops/sec ±2.58% 88
Uint8Array#concat 1,814,327 ops/sec ±1.28% 88
NodeBuffer#concat 1,636,523 ops/sec ±1.88% 73
BrowserBuffer#copy(16000) 1,073,665 ops/sec ±0.77% 90
Uint8Array#copy(16000) 1,348,517 ops/sec ±0.84% 89
NodeBuffer#copy(16000) 1,289,533 ops/sec ±0.82% 93
BrowserBuffer#copy(16) 12,782,706 ops/sec ±0.74% 85
Uint8Array#copy(16) 14,180,427 ops/sec ±0.93% 92
NodeBuffer#copy(16) 11,083,134 ops/sec ±1.06% 89
BrowserBuffer#new(16000) 141,678 ops/sec ±3.30% 67
Uint8Array#new(16000) 161,491 ops/sec ±2.96% 60
NodeBuffer#new(16000) 292,699 ops/sec ±3.20% 55
BrowserBuffer#new(16) 1,655,466 ops/sec ±2.41% 82
Uint8Array#new(16) 14,399,926 ops/sec ±0.91% 94
NodeBuffer#new(16) 3,894,696 ops/sec ±0.88% 92
BrowserBuffer#readDoubleBE 109,582 ops/sec ±0.75% 93
DataView#getFloat64 91,235 ops/sec ±0.81% 90
NodeBuffer#readDoubleBE 88,593 ops/sec ±0.96% 81
BrowserBuffer#readFloatBE 139,854 ops/sec ±1.03% 85
DataView#getFloat32 98,744 ops/sec ±0.80% 89
NodeBuffer#readFloatBE 92,769 ops/sec ±0.94% 93
BrowserBuffer#readUInt32LE 710,861 ops/sec ±0.82% 92
DataView#getUint32 117,893 ops/sec ±0.84% 91
NodeBuffer#readUInt32LE 851,412 ops/sec ±0.72% 93
BrowserBuffer#slice 1,673,877 ops/sec ±0.73% 94
Uint8Array#subarray 6,919,243 ops/sec ±0.67% 90
NodeBuffer#slice 4,617,604 ops/sec ±0.79% 93
BrowserBuffer#writeFloatBE 66,011 ops/sec ±0.75% 93
DataView#setFloat32 127,760 ops/sec ±0.72% 93
NodeBuffer#writeFloatBE 103,352 ops/sec ±0.83% 93

iojs 1.8.1

Method Operations Accuracy Sampled Fastest
BrowserBuffer#bracket-notation 10,990,488 ops/sec ±1.11% 91
Uint8Array#bracket-notation 11,268,757 ops/sec ±0.65% 97
NodeBuffer#bracket-notation 11,353,260 ops/sec ±0.83% 94
BrowserBuffer#concat 378,954 ops/sec ±0.74% 94
Uint8Array#concat 1,358,288 ops/sec ±0.97% 87
NodeBuffer#concat 1,934,050 ops/sec ±1.11% 78
BrowserBuffer#copy(16000) 894,538 ops/sec ±0.56% 84
Uint8Array#copy(16000) 1,442,656 ops/sec ±0.71% 96
NodeBuffer#copy(16000) 1,457,898 ops/sec ±0.53% 92
BrowserBuffer#copy(16) 12,870,457 ops/sec ±0.67% 95
Uint8Array#copy(16) 16,643,989 ops/sec ±0.61% 93
NodeBuffer#copy(16) 14,885,848 ops/sec ±0.74% 94
BrowserBuffer#new(16000) 109,264 ops/sec ±4.21% 63
Uint8Array#new(16000) 138,916 ops/sec ±1.87% 61
NodeBuffer#new(16000) 281,449 ops/sec ±3.58% 51
BrowserBuffer#new(16) 1,362,935 ops/sec ±0.56% 99
Uint8Array#new(16) 6,193,090 ops/sec ±0.64% 95
NodeBuffer#new(16) 4,745,425 ops/sec ±1.56% 90
BrowserBuffer#readDoubleBE 118,127 ops/sec ±0.59% 93
DataView#getFloat64 107,332 ops/sec ±0.65% 91
NodeBuffer#readDoubleBE 116,274 ops/sec ±0.94% 95
BrowserBuffer#readFloatBE 150,326 ops/sec ±0.58% 95
DataView#getFloat32 110,541 ops/sec ±0.57% 98
NodeBuffer#readFloatBE 121,599 ops/sec ±0.60% 87
BrowserBuffer#readUInt32LE 814,147 ops/sec ±0.62% 93
DataView#getUint32 137,592 ops/sec ±0.64% 90
NodeBuffer#readUInt32LE 931,650 ops/sec ±0.71% 96
BrowserBuffer#slice 878,590 ops/sec ±0.68% 93
Uint8Array#subarray 2,843,308 ops/sec ±1.02% 90
NodeBuffer#slice 4,998,316 ops/sec ±0.68% 90
BrowserBuffer#writeFloatBE 65,927 ops/sec ±0.74% 93
DataView#setFloat32 139,823 ops/sec ±0.97% 89
NodeBuffer#writeFloatBE 135,763 ops/sec ±0.65% 96

Testing the project

First, install the project:

npm install

Then, to run tests in Node.js, run:

npm run test-node

To test locally in a browser, you can run:

npm run test-browser-old-local # For ES5 browsers that don't support ES6
npm run test-browser-new-local # For ES6 compliant browsers

This will print out a URL that you can then open in a browser to run the tests, using airtap.

To run automated browser tests using Saucelabs, ensure that your SAUCE_USERNAME and SAUCE_ACCESS_KEY environment variables are set, then run:

npm test

This is what's run in Travis, to check against various browsers. The list of browsers is kept in the bin/airtap-es5.yml and bin/airtap-es6.yml files.

JavaScript Standard Style

This module uses JavaScript Standard Style.

JavaScript Style Guide

To test that the code conforms to the style, npm install and run:

./node_modules/.bin/standard

credit

This was originally forked from buffer-browserify.

Security Policies and Procedures

The buffer team and community take all security bugs in buffer seriously. Please see our security policies and procedures document to learn how to report issues.

license

MIT. Copyright (C) Feross Aboukhadijeh, and other contributors. Originally forked from an MIT-licensed module by Romain Beauxis.

buffer's People

Contributors

andreasmadsen avatar andrewrk avatar calvinmetcalf avatar chjj avatar daverayment avatar dcousens avatar devongovett avatar emilbayes avatar fanatid avatar feross avatar goto-bus-stop avatar greenkeeper[bot] avatar greenkeeperio-bot avatar jdalton avatar jessetane avatar jscissr avatar kawanet avatar koush avatar linusu avatar mafintosh avatar mischnic avatar mwilliamson avatar nolanlawson avatar parshap avatar peternewman avatar rdrey avatar tonistiigi avatar toots avatar ukstv avatar vmx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

buffer's Issues

Different result than with node

Hi,

I tried using specifically the function utf8slice on a byte array but I get a slightly different result than calling new Buffer(arr).toString() from node. I tried finding the utf8slice function in the node source code but didn't have much luck.

Here is the function I used from your source code:

var res = ''
    var tmp = ''
   var end = arr.length

    for (var i = 0; i < end; i++) {
        if (arr[i] <= 0x7F) {
            res += decodeUtf8Char(tmp) + String.fromCharCode(arr[i])
            tmp = ''
        } else {
            tmp += '%' + arr[i].toString(16)
        }
    }

    var str = res + decodeUtf8Char(tmp)

    function decodeUtf8Char(str) {
        try {
            return decodeURIComponent(str)
        } catch (err) {
            return String.fromCharCode(0xFFFD) // UTF 8 invalid char
        }
    }

Here is the array:

[255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, 96, 0, 96, 0, 0, 255, 219, 0, 67, 0, 8, 6, 6, 7, 6, 5, 8, 7, 7, 7, 9, 9, 8, 10, 12, 20, 13, 12, 11, 11, 12, 25, 18, 19, 15, 20, 29, 26, 31, 30, 29, 26, 28, 28, 32, 36, 46, 39, 32, 34, 44, 35, 28, 28, 40, 55, 41, 44, 48, 49, 52, 52, 52, 31, 39, 57, 61, 56, 50, 60, 46, 51, 52, 50, 255, 219, 0, 67, 1, 9, 9, 9, 12, 11, 12, 24, 13, 13, 24, 50, 33, 28, 33, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 255, 192, 0, 17, 8, 0, 102, 0, 242, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 31, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 196, 0, 31, 1, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 255, 196, 0, 181, 17, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119, 0, 1, 2, 3, 17, 4, 5, 33, 49, 6, 18, 65, 81, 7, 97, 113, 19, 34, 50, 129, 8, 20, 66, 145, 161, 177, 193, 9, 35, 51, 82, 240, 21, 98, 114, 209, 10, 22, 36, 52, 225, 37, 241, 23, 24, 25, 26, 38, 39, 40, 41, 42, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 103, 104, 105, 106, 115, 116, 117, 118, 119, 120, 121, 122, 130, 131, 132, 133, 134, 135, 136, 137, 138, 146, 147, 148, 149, 150, 151, 152, 153, 154, 162, 163, 164, 165, 166, 167, 168, 169, 170, 178, 179, 180, 181, 182, 183, 184, 185, 186, 194, 195, 196, 197, 198, 199, 200, 201, 202, 210, 211, 212, 213, 214, 215, 216, 217, 218, 226, 227, 228, 229, 230, 231, 232, 233, 234, 242, 243, 244, 245, 246, 247, 248, 249, 250, 255, 218, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, 247, 250, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 42, 148, 58, 149, 141, 197, 228, 246, 112, 94, 219, 203, 117, 6, 60, 232, 18, 85, 103, 143, 61, 55, 40, 57, 31, 141, 93, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 243, 207, 0, 248, 187, 83, 241, 31, 139, 124, 105, 167, 94, 180, 70, 223, 73, 191, 16, 91, 4, 76, 16, 187, 229, 94, 79, 127, 245, 96, 254, 38, 189, 14, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 162, 128, 10, 40, 170, 247, 55, 48, 89, 219, 73, 115, 117, 52, 112, 195, 18, 238, 121, 36, 96, 170, 163, 212, 147, 210, 128, 62, 87, 211, 117, 75, 173, 51, 246, 135, 158, 91, 89, 74, 180, 254, 32, 150, 214, 64, 122, 52, 114, 78, 81, 129, 31, 67, 199, 161, 0, 246, 175, 172, 43, 231, 79, 132, 246, 86, 126, 37, 248, 193, 226, 79, 16, 45, 186, 220, 216, 67, 44, 247, 22, 210, 200, 188, 163, 203, 54, 99, 108, 30, 251, 3, 245, 233, 245, 175, 160, 231, 18, 52, 18, 8, 164, 242, 228, 101, 33, 28, 174, 237, 167, 28, 28, 119, 250, 80, 5, 138, 42, 165, 162, 92, 69, 105, 10, 93, 76, 179, 220, 42, 42, 203, 40, 143, 96, 145, 128, 229, 130, 228, 237, 4, 243, 140, 156, 85, 186, 0, 40, 162, 138, 0, 40, 162, 138, 0, 40, 170, 211, 220, 67, 105, 109, 45, 205, 196, 139, 20, 16, 161, 146, 71, 99, 128, 170, 6, 73, 62, 192, 87, 155, 234, 95, 30, 252, 17, 98, 232, 182, 243, 223, 106, 27, 179, 184, 218, 219, 16, 19, 235, 230, 20, 253, 51, 64, 25, 63, 5, 174, 225, 212, 60, 105, 241, 18, 246, 217, 183, 193, 113, 168, 164, 177, 183, 170, 180, 151, 4, 31, 200, 215, 179, 215, 201, 255, 0, 9, 117, 207, 23, 105, 82, 234, 182, 190, 17, 208, 161, 212, 238, 46, 140, 38, 103, 152, 31, 46, 16, 130, 76, 2, 119, 40, 5, 183, 28, 101, 191, 132, 224, 26, 246, 175, 10, 107, 31, 19, 46, 245, 200, 225, 241, 63, 134, 180, 219, 61, 49, 149, 183, 207, 4, 203, 189, 8, 31, 46, 0, 149, 243, 147, 129, 208, 122, 230, 128, 61, 26, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 43, 230, 31, 11, 199, 227, 239, 24, 105, 250, 198, 165, 166, 120, 195, 84, 26, 222, 157, 114, 187, 244, 183, 153, 227, 70, 13, 146, 48, 75, 132, 7, 42, 227, 97, 92, 124, 189, 121, 160, 15, 166, 93, 214, 36, 46, 236, 21, 84, 101, 153, 142, 0, 30, 166, 188, 35, 197, 26, 237, 199, 197, 255, 0, 22, 218, 248, 71, 195, 151, 51, 199, 161, 66, 75, 223, 222, 170, 157, 146, 129, 200, 36, 119, 80, 70, 23, 61, 88, 231, 176, 53, 207, 106, 126, 60, 248, 145, 227, 86, 159, 194, 54, 182, 150, 223, 104, 230, 59, 175, 236, 193, 146, 235, 247, 72, 121, 119, 178, 5, 57, 228, 130, 7, 108, 246, 174, 131, 64, 180, 248, 191, 225, 143, 13, 38, 135, 163, 248, 79, 76, 182, 137, 3, 98, 224, 205, 19, 76, 89, 142, 75, 146, 102, 218, 91, 234, 184, 192, 3, 28, 80, 5, 255, 0, 131, 145, 89, 120, 127, 196, 191, 17, 45, 214, 68, 182, 176, 176, 189, 72, 149, 166, 147, 11, 28, 105, 37, 194, 140, 177, 246, 3, 147, 93, 126, 139, 241, 107, 195, 58, 255, 0, 139, 7, 135, 244, 215, 187, 150, 225, 247, 136, 167, 242, 128, 138, 82, 170, 88, 237, 57, 207, 69, 60, 144, 7, 21, 226, 250, 63, 129, 188, 73, 175, 252, 66, 215, 124, 39, 170, 235, 107, 103, 53, 202, 46, 163, 170, 180, 11, 189, 102, 108, 171, 129, 180, 109, 4, 134, 159, 253, 208, 115, 140, 224, 87, 75, 174, 71, 225, 191, 135, 63, 27, 124, 46, 193, 87, 78, 210, 109, 52, 182, 50, 56, 70, 115, 185, 190, 208, 187, 155, 0, 179, 18, 72, 25, 231, 242, 20, 1, 239, 146, 203, 29, 188, 77, 44, 210, 44, 113, 160, 203, 59, 176, 1, 71, 169, 38, 184, 125, 47, 226, 199, 134, 117, 159, 25, 31, 13, 89, 75, 59, 221, 23, 100, 142, 227, 98, 249, 18, 178, 130, 72, 86, 221, 147, 208, 224, 227, 7, 177, 228, 87, 207, 223, 20, 252, 108, 254, 44, 241, 101, 217, 177, 212, 174, 46, 52, 56, 202, 125, 146, 54, 220, 137, 194, 0, 205, 176, 227, 146, 219, 185, 35, 56, 165, 208, 124, 37, 170, 120, 123, 197, 30, 6, 212, 181, 40, 146, 8, 245, 93, 78, 7, 182, 140, 182, 95, 98, 203, 17, 220, 195, 176, 33, 198, 57, 207, 92, 129, 64, 30, 219, 241, 223, 88, 187, 210, 190, 29, 108, 180, 144, 198, 111, 175, 35, 182, 145, 215, 33, 130, 109, 119, 56, 35, 212, 160, 31, 66, 106, 166, 187, 227, 185, 60, 51, 240, 159, 195, 113, 218, 59, 220, 107, 250, 174, 153, 109, 21, 178, 110, 221, 32, 102, 137, 65, 148, 247, 56, 39, 143, 86, 35, 222, 184, 15, 139, 58, 246, 177, 227, 91, 205, 98, 43, 91, 53, 139, 69, 240, 173, 201, 138, 225, 252, 223, 153, 228, 119, 242, 131, 21, 207, 63, 50, 182, 48, 56, 4, 228, 243, 93, 23, 193, 159, 11, 220, 107, 215, 22, 254, 52, 215, 85, 38, 75, 104, 18, 199, 75, 137, 211, 133, 16, 170, 160, 144, 118, 227, 105, 3, 253, 173, 199, 131, 138, 0, 185, 240, 33, 181, 65, 171, 248, 198, 203, 88, 187, 158, 123, 203, 91, 152, 99, 155, 205, 152, 201, 137, 1, 153, 95, 146, 78, 121, 81, 207, 176, 175, 109, 175, 31, 248, 65, 255, 0, 37, 15, 226, 111, 253, 133, 71, 254, 141, 184, 172, 143, 137, 190, 55, 190, 241, 70, 177, 23, 129, 188, 31, 114, 175, 36, 187, 197, 236, 203, 42, 162, 182, 209, 184, 160, 114, 70, 2, 133, 98, 199, 191, 79, 80, 64, 58, 143, 132, 94, 57, 186, 241, 222, 139, 170, 38, 174, 99, 146, 242, 218, 227, 14, 137, 16, 84, 242, 100, 31, 32, 247, 229, 100, 31, 64, 51, 93, 99, 88, 120, 119, 193, 186, 109, 254, 179, 107, 164, 233, 246, 9, 4, 13, 36, 207, 107, 109, 28, 76, 202, 163, 59, 114, 160, 103, 167, 79, 90, 241, 159, 128, 23, 31, 217, 222, 55, 241, 38, 129, 20, 144, 221, 66, 208, 249, 130, 234, 34, 118, 191, 147, 38, 192, 87, 253, 150, 18, 147, 248, 10, 191, 241, 175, 197, 55, 186, 174, 160, 254, 12, 209, 188, 169, 97, 134, 213, 175, 181, 7, 71, 228, 121, 74, 242, 24, 219, 176, 1, 84, 55, 185, 42, 61, 136, 5, 255, 0, 217, 199, 76, 72, 60, 47, 171, 234, 185, 97, 37, 213, 226, 193, 130, 56, 219, 26, 2, 8, 252, 101, 97, 248, 87, 171, 77, 175, 105, 118, 250, 237, 190, 139, 45, 236, 73, 169, 92, 198, 210, 195, 108, 79, 204, 234, 51, 146, 63, 35, 249, 31, 67, 92, 159, 193, 63, 249, 36, 58, 31, 253, 188, 127, 232, 249, 43, 135, 109, 126, 223, 95, 253, 168, 116, 150, 181, 120, 228, 130, 198, 222, 75, 69, 150, 54, 220, 28, 136, 38, 118, 63, 131, 59, 47, 252, 6, 128, 61, 151, 88, 215, 116, 191, 15, 217, 139, 189, 86, 246, 43, 75, 118, 145, 98, 18, 74, 120, 44, 122, 15, 208, 254, 70, 181, 43, 193, 63, 104, 191, 17, 219, 181, 166, 155, 225, 168, 128, 121, 196, 191, 109, 159, 158, 99, 1, 89, 16, 126, 59, 152, 254, 3, 214, 189, 191, 79, 190, 135, 83, 211, 45, 53, 11, 99, 186, 11, 168, 82, 104, 207, 170, 176, 4, 126, 134, 128, 41, 107, 190, 33, 210, 252, 55, 167, 127, 104, 107, 23, 169, 107, 108, 28, 70, 29, 129, 57, 99, 208, 0, 1, 36, 240, 122, 14, 198, 175, 217, 94, 218, 234, 86, 80, 222, 89, 207, 28, 246, 211, 32, 120, 229, 141, 178, 172, 15, 112, 107, 196, 117, 155, 91, 255, 0, 140, 95, 16, 245, 29, 21, 223, 236, 90, 15, 135, 100, 146, 25, 74, 182, 93, 230, 59, 144, 54, 59, 146, 200, 113, 232, 160, 247, 53, 119, 225, 86, 175, 168, 120, 91, 197, 55, 95, 14, 117, 195, 33, 16, 25, 31, 74, 145, 161, 216, 36, 64, 206, 204, 71, 114, 24, 101, 135, 92, 97, 134, 123, 80, 7, 181, 209, 69, 120, 86, 187, 123, 226, 79, 31, 252, 64, 214, 116, 237, 15, 196, 23, 58, 6, 143, 160, 70, 209, 220, 92, 71, 51, 160, 105, 65, 108, 150, 218, 71, 86, 86, 28, 156, 0, 132, 245, 224, 128, 123, 173, 21, 228, 191, 3, 181, 239, 18, 120, 139, 64, 191, 155, 90, 187, 251, 85, 173, 188, 169, 5, 164, 239, 131, 35, 16, 9, 112, 199, 171, 1, 148, 193, 60, 242, 121, 175, 90, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 14, 103, 197, 158, 51, 209, 188, 23, 167, 165, 222, 179, 59, 168, 148, 149, 134, 40, 163, 220, 242, 17, 201, 0, 116, 252, 73, 2, 188, 1, 252, 61, 227, 111, 26, 93, 235, 158, 37, 240, 182, 145, 45, 150, 151, 174, 74, 209, 201, 110, 46, 82, 51, 50, 100, 18, 199, 121, 25, 5, 134, 73, 29, 216, 129, 198, 107, 233, 107, 189, 47, 79, 212, 140, 38, 250, 194, 214, 232, 192, 251, 226, 51, 194, 175, 229, 183, 170, 228, 112, 125, 197, 95, 160, 15, 2, 240, 174, 157, 241, 91, 193, 186, 88, 177, 210, 60, 15, 162, 162, 177, 13, 44, 210, 76, 141, 36, 173, 234, 199, 237, 31, 160, 0, 14, 194, 183, 127, 225, 33, 248, 229, 255, 0, 66, 110, 135, 255, 0, 127, 151, 255, 0, 146, 43, 216, 40, 160, 15, 33, 248, 125, 225, 255, 0, 25, 127, 194, 205, 213, 252, 91, 226, 157, 38, 218, 192, 222, 216, 8, 49, 4, 200, 202, 92, 24, 128, 192, 14, 196, 124, 177, 243, 147, 214, 189, 39, 82, 240, 246, 137, 172, 60, 111, 169, 104, 250, 125, 243, 198, 54, 163, 93, 91, 36, 165, 71, 160, 44, 14, 43, 86, 138, 0, 228, 225, 248, 119, 224, 251, 125, 100, 106, 144, 248, 114, 198, 59, 160, 0, 93, 177, 98, 53, 199, 66, 35, 251, 128, 251, 129, 154, 243, 111, 218, 15, 80, 155, 73, 213, 188, 23, 169, 91, 237, 243, 237, 39, 158, 120, 247, 12, 141, 200, 208, 48, 207, 226, 43, 221, 107, 231, 47, 137, 190, 33, 211, 60, 105, 241, 83, 195, 62, 30, 136, 173, 198, 159, 105, 123, 29, 189, 196, 136, 120, 118, 150, 84, 89, 20, 31, 64, 20, 12, 250, 230, 128, 52, 124, 23, 240, 250, 109, 123, 224, 198, 172, 240, 222, 183, 246, 167, 137, 93, 101, 121, 46, 179, 180, 121, 51, 146, 185, 192, 39, 230, 195, 18, 220, 253, 225, 233, 207, 173, 248, 47, 195, 241, 248, 91, 193, 250, 102, 140, 175, 230, 27, 88, 177, 35, 231, 33, 164, 98, 89, 200, 246, 44, 205, 143, 108, 86, 204, 16, 197, 109, 4, 112, 195, 26, 199, 28, 106, 21, 17, 87, 1, 64, 224, 0, 59, 10, 177, 64, 30, 39, 47, 194, 175, 26, 69, 226, 205, 122, 235, 73, 241, 76, 26, 94, 155, 172, 94, 181, 204, 207, 110, 95, 237, 0, 110, 118, 81, 128, 7, 77, 236, 56, 113, 154, 196, 147, 224, 15, 136, 236, 239, 230, 143, 75, 215, 172, 26, 206, 226, 17, 12, 243, 79, 27, 44, 219, 73, 5, 192, 80, 172, 6, 72, 236, 192, 144, 72, 39, 4, 231, 232, 122, 40, 3, 196, 46, 190, 14, 107, 122, 6, 179, 166, 106, 126, 3, 212, 236, 180, 249, 224, 179, 16, 79, 45, 211, 57, 51, 75, 200, 105, 54, 149, 117, 195, 2, 62, 94, 128, 129, 138, 191, 164, 124, 62, 190, 240, 183, 195, 223, 25, 92, 106, 207, 5, 246, 189, 169, 218, 93, 180, 151, 22, 251, 156, 178, 152, 155, 10, 9, 80, 114, 88, 146, 112, 57, 200, 244, 175, 96, 162, 128, 62, 102, 240, 54, 161, 226, 239, 22, 248, 82, 195, 193, 126, 27, 63, 217, 214, 22, 145, 202, 53, 13, 65, 135, 93, 242, 59, 109, 7, 168, 225, 128, 192, 228, 243, 200, 21, 216, 207, 240, 22, 11, 89, 116, 251, 157, 3, 196, 151, 122, 85, 229, 172, 6, 57, 46, 99, 136, 179, 202, 231, 118, 92, 21, 117, 41, 144, 197, 112, 51, 192, 3, 215, 62, 209, 69, 0, 120, 31, 140, 126, 24, 105, 94, 11, 248, 93, 175, 95, 188, 243, 234, 122, 196, 203, 8, 146, 250, 224, 114, 51, 113, 30, 118, 47, 59, 115, 220, 146, 79, 191, 56, 173, 93, 43, 227, 39, 133, 52, 79, 135, 90, 117, 188, 55, 198, 231, 86, 179, 210, 225, 136, 90, 136, 37, 25, 153, 99, 85, 218, 88, 174, 220, 110, 28, 156, 244, 233, 154, 245, 219, 171, 75, 107, 235, 73, 45, 174, 173, 226, 184, 183, 148, 109, 146, 41, 144, 58, 56, 244, 32, 240, 69, 100, 197, 224, 207, 11, 65, 34, 75, 15, 134, 180, 104, 228, 67, 149, 116, 176, 136, 21, 62, 160, 133, 226, 128, 60, 255, 0, 225, 189, 180, 158, 18, 240, 182, 189, 227, 191, 19, 220, 172, 45, 172, 50, 223, 205, 26, 166, 54, 46, 231, 43, 199, 247, 152, 203, 192, 247, 90, 206, 240, 197, 197, 215, 138, 252, 119, 31, 196, 221, 126, 75, 125, 27, 69, 178, 141, 173, 172, 5, 204, 138, 158, 106, 144, 234, 50, 204, 64, 255, 0, 150, 140, 115, 220, 240, 58, 26, 245, 111, 18, 120, 126, 211, 197, 58, 5, 214, 139, 126, 210, 139, 91, 165, 80, 230, 22, 10, 227, 107, 6, 4, 18, 8, 200, 42, 59, 87, 1, 109, 251, 62, 120, 58, 11, 152, 228, 150, 231, 87, 184, 68, 108, 152, 101, 157, 2, 184, 244, 59, 80, 54, 62, 132, 80, 6, 175, 138, 62, 47, 248, 115, 67, 138, 40, 244, 201, 211, 92, 191, 159, 136, 109, 180, 249, 86, 65, 158, 219, 153, 115, 143, 160, 201, 246, 175, 21, 240, 238, 149, 107, 226, 223, 21, 120, 145, 188, 77, 226, 56, 188, 51, 109, 45, 247, 159, 123, 97, 44, 226, 38, 158, 67, 36, 132, 160, 222, 192, 101, 14, 225, 146, 14, 55, 14, 57, 175, 160, 180, 31, 135, 62, 19, 240, 198, 161, 246, 237, 31, 70, 75, 123, 173, 165, 4, 173, 44, 146, 21, 7, 174, 55, 177, 199, 212, 85, 61, 71, 225, 63, 130, 117, 109, 74, 227, 80, 189, 208, 196, 151, 23, 50, 25, 37, 117, 185, 153, 55, 49, 228, 156, 43, 129, 201, 244, 20, 1, 147, 63, 143, 124, 7, 240, 243, 195, 86, 246, 58, 45, 229, 189, 236, 113, 130, 176, 90, 105, 243, 172, 238, 204, 78, 73, 102, 4, 129, 146, 122, 147, 244, 7, 165, 114, 90, 134, 129, 227, 175, 140, 115, 91, 75, 170, 91, 71, 225, 237, 14, 38, 47, 12, 114, 134, 105, 27, 56, 249, 138, 100, 22, 62, 132, 237, 24, 39, 21, 233, 154, 55, 195, 63, 7, 120, 119, 81, 91, 253, 47, 67, 138, 43, 165, 31, 36, 143, 44, 146, 148, 247, 93, 236, 112, 125, 199, 53, 216, 80, 7, 15, 240, 231, 225, 250, 120, 3, 78, 187, 182, 93, 78, 107, 249, 46, 164, 14, 238, 201, 229, 162, 227, 56, 10, 153, 56, 60, 156, 156, 243, 199, 165, 119, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 5, 20, 81, 64, 30, 65, 241, 63, 226, 59, 219, 221, 79, 224, 141, 2, 9, 229, 215, 239, 10, 91, 52, 129, 112, 145, 9, 87, 248, 78, 115, 191, 12, 189, 176, 51, 156, 228, 87, 39, 174, 120, 50, 219, 193, 26, 223, 194, 253, 57, 22, 41, 47, 31, 84, 47, 119, 114, 137, 131, 43, 249, 182, 255, 0, 142, 20, 28, 15, 166, 122, 147, 93, 63, 195, 40, 142, 163, 241, 115, 226, 6, 177, 117, 35, 73, 117, 107, 117, 246, 40, 201, 60, 121, 70, 71, 0, 99, 216, 65, 24, 31, 67, 79, 248, 247, 157, 59, 77, 240, 207, 137, 96, 63, 233, 90, 102, 168, 190, 82, 158, 135, 112, 243, 57, 252, 97, 95, 206, 128, 61, 134, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 2, 138, 40, 160, 15, 28, 240, 156, 98, 195, 246, 142, 241, 133, 141, 169, 49, 218, 203, 100, 183, 50, 70, 15, 13, 35, 121, 12, 91, 235, 153, 31, 254, 250, 52, 159, 180, 76, 139, 255, 0, 8, 54, 155, 6, 225, 231, 62, 168, 140, 137, 252, 76, 4, 82, 2, 64, 250, 176, 252, 197, 63, 198, 126, 26, 241, 141, 135, 197, 11, 127, 23, 120, 51, 75, 183, 187, 105, 44, 188, 171, 165, 154, 117, 85, 119, 229, 126, 96, 93, 73, 27, 124, 178, 48, 122, 167, 231, 14, 135, 224, 207, 23, 248, 171, 199, 176, 248, 151, 199, 214, 240, 90, 199, 96, 1, 181, 176, 138, 69, 120, 203, 129, 193, 1, 93, 176, 1, 249, 142, 78, 73, 192, 233, 64, 30, 207, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 1, 69, 20, 80, 7, 255, 217]

Can anyone help? Thanks

How to use buffer without browserify?

I'm working on a cordova project which is client-side javascript. I want to use buffer to decompress my string like this:

var s = Buffer('737AB86B719E82CBC3DDDDC9190ABE0F77CFCE54303230300200', 'hex').toString('binary');

Please help me to use your library in client javascript. Thanks you!

Bad name of variable

In this place:

347: var byte = parseInt(string.substr(i * 2, 2), 16)
348: if (isNaN(byte)) throw new Error('Invalid hex string')
349: buf[offset + i] = byte

buffer/index.js

Lines 347 to 349 in 3a22a49

var byte = parseInt(string.substr(i * 2, 2), 16)
if (isNaN(byte)) throw new Error('Invalid hex string')
buf[offset + i] = byte

used variable with name "byte", but such names are only allowed in ecmascript 5. I'm use RhinoScriptEngine in java and Rhino not complete support ecmascript 5 now.
Please change the code for this example:

347: var byte2 = parseInt(string.substr(i * 2, 2), 16)
348: if (isNaN(byte2)) throw new Error('Invalid hex string')
349: buf[offset + i] = byte2

Thank you.

buf.slice() should do parent tracking to match node api behavior

See for description of the problem: toots/buffer-browserify#19

From the node docs:

buf.slice([start], [end])#
start Number, Optional, Default: 0
end Number, Optional, Default: buffer.length
Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.

Modifying the new buffer slice will modify memory in the original buffer!

Use an Object-based implementation as a fallback

Currently, we use Uint8Array in Chrome, IE10, Opera, and Safari. We use a Proxy + Uint8Array in Firefox. And we use fake Uint8Array from typedarray in IE6-9 and Firefox 3.

There are some problems that have become clear with this approach.

  1. Firefox Proxy performance is terrible
  2. Still broken in IE6-8
  3. Large bundle size (60KB+) from too many implementations...

1. Firefox Proxy performance is terrible

Once Uint8Array instances are augmentable then performance should be awesome in Firefox. See bug report: https://bugzilla.mozilla.org/show_bug.cgi?id=695438

Using an ES6 Proxy (supported since Firefox 18) to wrap the Uint8Array instance (to simulate augmenting the instance) causes an unacceptable performance hit. Just using the typedarray implementation is also quite slow.

But the old implementation of buffer-browserify had reasonable performance in Firefox, so let's use their Object approach as the fallback behavior for all Firefoxes, and IE6-9.

On the bracket-notation perf test:
Proxy and typedarray implementations get "56,784 ops/sec ±1.68% (65 runs sampled)"
OldBuffer gets "11,787,803 ops/sec ±0.95% (66 runs sampled)"

2. Still broken in IE6-8

typedarray uses Object.defineProperty which is not exist in IE6-8. It does exist in IE9 though. So this was a good stop-gap solution for the time, but it needs to be replaced because it can't work.

3. Large bundle size (60KB+)

typedarray was really big and had a lot of code dedicated to Uint8Array/ArrayBuffer details which aren't important and just bloat the bundle size. The Proxy implementation also added some cruft.

$ browserify -r native-buffer-browserify | wc -c
   57387
$ browserify -r buffer-browserify | wc -c       
   64427

The Plan

Eliminate the Proxy and typedarray code. Fallback to using an Object when Uint8Array is not available.

Cannot set property length of [object Object]

Buffer 3.5.0 breaks in Chrome (45.0.2454.85 (Official Build) (64-bit))

What I'm getting is

Uncaught TypeError: Cannot set property length of [object Object] which has only a getter

It happens when buffer tries to set .prototype.length to undefined, here: https://github.com/feross/buffer/blob/master/index.js#L372

I discovered this while I was rolling out our app's new version. Later I'm gonna try to put together a small demo.

(I'm using browserify 11.11.0 which uses [email protected])

Missing buffer get() method?

Not sure if this is a bug, but the get method of Buffer seems to be missing in native-buffer-browserify:

b = new Buffer(1)
b.get(0)  // Node v0.10.21 returns 0, browserify throws: TypeError: Object [object Uint8Array] has no method 'get'

applies to both data view and object-backed buffers.

http://nodejs.org/api/buffer.html does not document get, which is why I am unsure whether this is a legitimate issue. But I hit this problem while attempting to browserify kriskowal/zip#12 - fixed by changing to readUInt8

Broken in Chrome 49.0.2620.0 canary (64-bit)

My chrome updated during the night and now my stuff doesn't work anymore, fun times, I get to debug!

The first thing was actually already fixed by 5f5478a, I just had to update to the latest version. @feross Was that commit intentionally fixing a Chrome bug?

The next problem is that .subarray(start, end) seems broken when the __proto__ is tampered with.

screen shot 2016-01-13 at 16 54 53

The following code fixes the problem but I'm not sure that it's the right approach:

diff --git a/index.js b/index.js
index 15d7c50..cbb1516 100644
--- a/index.js
+++ b/index.js
@@ -782,7 +782,9 @@ Buffer.prototype.slice = function slice (start, end) {

   var newBuf
   if (Buffer.TYPED_ARRAY_SUPPORT) {
+    this.__proto__ = Uint8Array.prototype
     newBuf = this.subarray(start, end)
+    this.__proto__ = Buffer.prototype
     newBuf.__proto__ = Buffer.prototype
   } else {
     var sliceLen = end - start

Convert Uint8Array to Buffer

Should we expose the augment function somehow to allow for converting an Uint8Array to a Buffer without having to do a copy (which I believe new Buffer(uint8array) does now) ?

"call stack size exceeded" for large binary strings in 3.4.2

In PouchDB we use large binary strings when we store binary data in localstorage-down and level-js, and our tests started failing recently due to buffer 3.4.1 and 3.4.2 (pouchdb/pouchdb#4164, see discussion over there). In short, 3.4.1 had a perf regression which appears fixed in 3.4.2, but 3.4.2 now has a "call stack size exceeded" error when I test in Chrome 44 (on an admittedly slow laptop, so it may be hard to reproduce on a faster one).

Glancing at the code, I think a reasonable fix would be to break up the array into decent-size chunks and then concat the string together, but I admit I'm not super familiar with the codebase.

crypto.getRandomValues and Buffer throws a TypeError on Safari iOS 7

When using https://github.com/crypto-browserify/randombytes on Safari iOS 7 it throws a TypeError.

This works:

var a = new Uint8Array(128)
crypto.getRandomValues(a)

This throws a TypeError with message "Type error":

var b = Buffer(128)
crypto.getRandomValues(b)

The second case is how randombytes implements it, see https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L17-21.

The current implementation works fine on Safari iOS 8.x, so this seems to be a bug in Safari iOS 7.

Just for fun I forked randombytes and changed it to using Uint8Array and then converting to a Buffer:
mpfluger/randombytes@711ccbc

I'm not sure if that's a workable solution or if there is something in the buffer project you can do to handle that correctly.

Slicing buffers fails in latest Microsoft Edge

slicing buffers fails in Edge 25.10586.0.0
it boils down to this:

var buf = new Buffer(100);
var arr = new Uint8Array(100);
buf instanceof Uint8Array; // true
Uint8Array.prototype.subarray.call(arr, 0, 0).length; // 0
Uint8Array.prototype.subarray.call(buf, 0, 0).length; // 100 ! 

Would you say this is an issue with Edge or with this lib?

Buffer.concat undefined is not a function

Hi,

in node doing Buffer.concat(...) of array of Buffers works but in the browser I get Buffer.concat undefined is not a function.

The code is like this:

...
var bodyArr = []
bodyArr.push(new Buffer('bla bla'));
bodyArr.push(new Buffer('bla2 bla2'));

var body = Buffer.concat(bodyArr);
...
req.write(body);
req.end();

I use this to create a POST body for http.
What could be the reason of this error?
I did checked that Buffer constructor is present after browserify build. But it looks that .concat method is not?

Corrupted file result when saved to file from base64 source

my code is like this :

  var fs = require('fs');
  var B = require('buffer/').Buffer;

  var buffin = fs.readFileSync('excel.xls');

  var b64 = buffin.toString('base64');

  var bf = new B(b64,'base64');

  fs.writeFile('excelo.xls', bf, function (err) {
           if (err) throw err;
               console.log('It\'s saved!');
  });

the excelo.xls file is cannot be open, because corrupted file.
the filesize is also different with the original file.
the original file is 1,182 kb but the result file is 1,416 kb.

kindly help, thanks.

Buffer.concat fails for lists with only one item in it

I recently tried to use telegram.link library with browsefiry and noticed that all the requests fail with Uncaught TypeError: item.copy is not a function (index.js:328).

I've tried a few workarounds and found out that if I add:

if (list.length === 1) {
  return list[0];
}

In Buffer.concat function, around line 324, then all the requests pass fine.

I'm not (yet) pro in binary conversion yet so I'd like to ask a question:
What might be wrong with Buffer object passed to Buffer.concat so item.copy fails? You might assume that it doesn't pass Buffer object but actually it does, I've checked.

And will my workaround break something in the future?

I can provide a sample repo as a playground if needed ;-)

I've made the change in my fork, feel free to check it

Writing strings that contain integers as integer fails, deviates from Node API

The following will work:

var number = "1003";

var nodeBuf = new Buffer(64);

// Succeeds
nodeBuf.writeUInt16LE(number, 0);
console.log("Written to buffer: " + nodeBuf.readUInt16LE(0));

The following will not:

var NEWBuffer = require('buffer/').Buffer;

var newBuffer = new NEWBuffer(64);

// Fails
newBuffer.writeUInt16LE(number, 0);

Node's internal buffer handles this case and parses the integer, but this buffer implementation fails in Node and throws this error:

$ node bufferDebug.js

d:\********\node_modules\buffer\index.js:1168
  if (!test) throw new Error(message || 'Failed assertion')
                   ^
Error: cannot write a non-number as a number
    at assert (d:\********\node_modules\buffer\index.js:1168:20)
    at verifuint (d:\********\node_modules\buffer\index.js:1148:3)
    at writeUInt16 (d:\********\node_modules\buffer\index.js:744:5)
    at Uint8Array.Buffer.writeUInt16LE (d:\********\node_modules\buffer\index.js:760:10)
    at Object.<anonymous> (d:\********\server\utilities\bufferDebug.js:19:11
)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

Does helper method clamp need to exist?

Quoting from the source (https://github.com/feross/buffer/blob/master/index.js#L1032), this is the given implementation:

// slice(start, end)
function clamp (index, len, defaultValue) {
  if (typeof index !== 'number') return defaultValue
  index = ~~index;  // Coerce to integer.
  if (index >= len) return len
  if (index >= 0) return index
  index += len
  if (index >= 0) return index
  return 0
}

This whole method can be realized in a more readable way, I haven't tested performance, but it should be better as multiple if conditions are saved:

Buffer.prototype.slice = function(start, end) {

    var length = this.length;

    start = typeof start === 'number' ? (start | 0) : 0;
    end   = typeof end === 'number'   ? (end   | 0) : length;

    start = Math.min(start, length);
    end   = Math.min(end,   length);

    // ... etc. etc.

};

If the Math.min calls are too heavy (which I don't expect, because they are faster than the previous implementation anyways), we can switch to a a ^ ((a ^ b) & -(a < b)) or a turnery, but for now I think that would be overkill as embedded Math.min calls get optimized anyways.

Consider matching Buffer.toJSON() more closely

> var buf = new Buffer([1,2,3])
> buf.toJSON()
[ 1, 2, 3 ]

With buffer in the browser I get {type: 'Buffer', data: [...]}. Obviously you're doing this intentionally, since the code does exactly this. However, I wonder why you've chosen to do it this way? At the very least, documenting the difference will save users problems when it works differently than they expect.

Buffer slicing not working when passed to a browser XHR

I ran into an issue, documented in nathanpeck/s3-upload-stream#20, where a Buffer that had been sliced and then passed onto the browser's native XHR object wasn't having its sliced view respected.

It looks like you might have already addressed and documented this in 500be9e? If so, it might be worth rephrasing with less low-level terminology or something? I think I understand what "parent tracking" means, and that this is the issue I'm seeing, but I'm not sure.

Remove buffer.toArrayBuffer()

Node removed the buffer.toArrayBuffer() API sometime during node 0.11. Users who rely on it will have code that doesn't work in node.js, which is unfortunate.

Let's remove buffer.toArrayBuffer() so we have parity with node.js.

assert module inflates the bundle size

Presently, this module does require('assert') which results in a rather large bundle because of downstream dependencies from assert:

$ browserify -r native-buffer-browserify | wc -c
66737

commenting out the assert, this module has a much smaller footprint:

$ browserify -r native-buffer-browserify | wc -c
35575

which is only 3k more than the index.js itself.

It seems like the asserts could be replaced with throw new Error() for a huge savings in bytes?

write() writes partial codes of multibyte characters

I've noticed that partial bytes of UCS-2 symbols are written, while they shouldn't be according to the Node.js documentation.

Here's the code taken from Node.js test:

var f = new Buffer([0, 0, 0, 0, 0]);
assert.equal(f.length, 5);
var size = f.write('あいうえお', 'ucs2');
console.error('bytes written to buffer: %d     (should be 4)', size);
assert.equal(size, 4);
assert.deepEqual(f, new Buffer([0x42, 0x30, 0x44, 0x30, 0x00]));

Base64 with new lines doesn't work.

When a base64 string contains new lines the node buffer module will behave correctly,
however the browserified version will garble the data.

Reproduce:

var Buffer = require('./node_modules/buffer').Buffer;

var data = [ 
  "LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK",
  "LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoK\nICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt",
  "LS0tCnRpdGxlOiBUaHJlZSBkYXNoZXMgbWFya3MgdGhlIHNwb3QKdGFnczoKICAtIHlhbWwKICAtIGZyb250LW1hdHRlcgogIC0gZGFzaGVzCmV4cGFuZWQt"
];

data.forEach(function(dat) {
  var d = new Buffer(dat, 'base64');
  console.log(d.toString('utf8'));
});

Output will be:


---
title: Three dashes marks the spot
tags:

---
title: Three dashes marks the spot
tags:
�(�(�)�

---
title: Three dashes marks the spot
tags:
  - yaml
  - front-matter
  - dashes
expaned-

Github seems to transfer it's content data including new lines, e.g.:

https://api.github.com/repos/rhalff/rhalff.github.io/git/blobs/9bb51606e7e8c5a63b67900fb6cab25094f4fce5

and is why I encounter this.

Drop utf8ToBytes and asciiToBytes in favor of TextEncoder

TextEncoder and TextDecoder are already supported in Chrome and Firefox (not sure about MSIE and Safari).

var buf = new TextEncoder('utf-16le').encode("I ½ ♥ 💩");
var str = new TextDecoder('utf-16le').decode(buf);

If you use them you could drop a fair amount of code and only provide your UTF-8, UTF-16, etc conversions as a separate Polyfill module when the native TextEncoder and TextDecoder don't exist.

I haven't actually tested your UTF-8 converter, but if you get the same values as you get in node, then it's probably the best that there is. The full TextEncoder Polyfill seems to have much more code than I would intuitively expect and Mozilla's UTF-8 converter is actually incorrect (well it provides a correct encoding, but it's not the encoding that node and the W3C utilities use).

Anyway, then you could drop a bit of code.

I'm not sure how this compares in terms of performance, but if you wanted the polyfill code to be even smaller you can actually do this:

function utf8ToBinaryString() {
  var escstr = encodeURIComponent(str);
  // replaces any uri escape sequence, such as %0A, with binary escape, such as 0x0A
  var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
    return String.fromCharCode('0x' + p1);
  });

  return binstr;
}

function utf8ToBuffer(str) {
  var binstr = utf8ToBinaryString(str);
  var buf = new Uint8Array(binstr.length);
  Array.prototype.forEach.call(binstr, function (ch, i) {
    buf[i] = ch.charCodeAt(0);
  });
  return buf;
}

function utf8ToBase64(str) {
  var binstr = utf8ToBinaryString(str);
  return btoa(binstr);
}

I don't know if that still works for utf-16, but I have a hunch that it would.

I know I could have just used your module here, but I've been enjoying the headache of learning the hacky, non-hacky, and best-possible ways of doing this stuff in the browser.

Buffer.isBuffer - unnecessary ._isBuffer = true flag

Why is Buffer.isBuffer solved this way?

Buffer.isBuffer = function (b) {
  return !!(b !== null && b !== undefined && b._isBuffer)
}

Why not just return b instanceof Buffer?
Why do you need to dispatch the isBuffer property to the UInt8Array anyways?

buf.toArrayBuffer on Firefox

Would it be reasonable to support toArrayBuffer on browsers where Buffer is Object-backed, but typed arrays are available? (= Firefox)

Currently toArrayBuffer is only available where Buffer is an augmented Uint8Array:

buf = new Buffer(1)
buf.toArrayBuffer  // undefined on Firefox, new Buffer(this).buffer on Chrome

added in augment():

arr.toArrayBuffer = BufferToArrayBuffer

ArrayBuffers are required by some browser APIs (at least the W3C File API), but toArrayBuffer cannot be relied on for Firefox compatibility. Workaround:

arrayBuffer = new Uint8Array(buf.length);
for (var i = 0; i < buf.length; i += 1) arrayBuffer[i] = buf[i]

(though in my specific case, I ended up porting to use bops instead so the data would be backed by Uint8Array even on Firefox)

references:
chrisdickinson/bops#7 (comment)
kriskowal/zip#13

IE6-8 support

Still broken in IE6-8.

typedarray uses Object.defineProperty which is not supported in IE6-8.

include build in release

It would be handy if the release download included a build version of the code that could be used directly in the browser for the occasional proof of concept html page put together on a tablet without a node build environment

TypeError: buf.set is not a function ([email protected] and nightly)

L86 throws for me with TypeError: buf.set is not a function in [email protected] and nightly

as far as I can see the array doesnt get augmented since L24-L31 throws, so Buffer._useTypedArrays is false. in L84 it checks against typeof Uint8Array === 'function' && subject instanceof Uint8Array although buf is actually not a uint8.

so, i think we should at least check against Buffer._useTypedArrays at L84 and not against typeof && instanceof. but using the object-fallback sucks anyway.

Remove DataView, just do bitops

Advantages:

  • less code
  • i'm guessing it will be faster
  • same code path for browsers that support typed arrays and those that don't

Android 4.x support?

I'm looking into a bug (browserify/createHash#8) effecting creating hashes in Android 4.3.1 and earlier. Some preliminary sauce lab's testing suggests it might effect ciphers too (though could have the same root cause in the hashes if it doesn't effect createCipheriv), that being said it could be a bug in buffer and I noticed that this repo doesn't test against android older than 5.0 and since I couldn't find any mention of it in the issues I thought I'd check to make sure there arn't any known issues.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.