6.0.5 • Published 2 years ago

@craftzdog/react-native-buffer v6.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@craftzdog/react-native-buffer

The buffer module from node.js, for React Native.

Simply require('@craftzdog/react-native-buffer') or use the Buffer global and you will get this module. It internally uses react-native-quick-base64, which is a performant native implementation of base64.

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, with one minor difference (see below)
  • 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

npm install @craftzdog/react-native-buffer react-native-quick-base64
cd ios && pod install

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.

To depend on this module explicitly, require it like this:

var Buffer = require("@craftzdog/react-native-buffer").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 a sanity 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

MethodOperationsAccuracySampledFastest
BrowserBuffer#bracket-notation11,457,464 ops/sec±0.86%66
Uint8Array#bracket-notation10,824,332 ops/sec±0.74%65
BrowserBuffer#concat450,532 ops/sec±0.76%68
Uint8Array#concat1,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#readDoubleBE112,830 ops/sec±0.51%69
DataView#getFloat6493,500 ops/sec±0.57%68
BrowserBuffer#readFloatBE146,678 ops/sec±0.95%68
DataView#getFloat3299,311 ops/sec±0.41%67
BrowserBuffer#readUInt32LE843,214 ops/sec±0.70%69
DataView#getUint32103,024 ops/sec±0.64%67
BrowserBuffer#slice1,013,941 ops/sec±0.75%67
Uint8Array#subarray1,903,928 ops/sec±0.53%67
BrowserBuffer#writeFloatBE61,387 ops/sec±0.90%67
DataView#setFloat32141,249 ops/sec±0.40%66

Firefox 33

MethodOperationsAccuracySampledFastest
BrowserBuffer#bracket-notation20,800,421 ops/sec±1.84%60
Uint8Array#bracket-notation20,826,235 ops/sec±2.02%61
BrowserBuffer#concat153,076 ops/sec±2.32%61
Uint8Array#concat1,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#readDoubleBE99,871 ops/sec±0.41%69
DataView#getFloat64285,663 ops/sec±0.70%68
BrowserBuffer#readFloatBE115,540 ops/sec±0.42%69
DataView#getFloat32288,722 ops/sec±0.82%68
BrowserBuffer#readUInt32LE633,926 ops/sec±1.08%67
DataView#getUint32294,808 ops/sec±0.79%64
BrowserBuffer#slice349,425 ops/sec±0.46%69
Uint8Array#subarray5,965,819 ops/sec±0.60%65
BrowserBuffer#writeFloatBE59,980 ops/sec±0.41%67
DataView#setFloat32317,634 ops/sec±0.63%68

Safari 8

MethodOperationsAccuracySampledFastest
BrowserBuffer#bracket-notation10,279,729 ops/sec±2.25%56
Uint8Array#bracket-notation10,030,767 ops/sec±2.23%59
BrowserBuffer#concat144,138 ops/sec±1.38%65
Uint8Array#concat4,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#readDoubleBE41,341 ops/sec±1.06%67
DataView#getFloat64322,280 ops/sec±0.84%68
BrowserBuffer#readFloatBE46,141 ops/sec±1.06%65
DataView#getFloat32337,025 ops/sec±0.43%69
BrowserBuffer#readUInt32LE151,551 ops/sec±1.02%66
DataView#getUint32308,278 ops/sec±0.94%67
BrowserBuffer#slice197,365 ops/sec±0.95%66
Uint8Array#subarray9,558,024 ops/sec±3.08%58
BrowserBuffer#writeFloatBE17,518 ops/sec±1.03%63
DataView#setFloat32319,751 ops/sec±0.48%68

Node 0.11.14

MethodOperationsAccuracySampledFastest
BrowserBuffer#bracket-notation10,489,828 ops/sec±3.25%90
Uint8Array#bracket-notation10,534,884 ops/sec±0.81%92
NodeBuffer#bracket-notation10,389,910 ops/sec±0.97%87
BrowserBuffer#concat487,830 ops/sec±2.58%88
Uint8Array#concat1,814,327 ops/sec±1.28%88
NodeBuffer#concat1,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#readDoubleBE109,582 ops/sec±0.75%93
DataView#getFloat6491,235 ops/sec±0.81%90
NodeBuffer#readDoubleBE88,593 ops/sec±0.96%81
BrowserBuffer#readFloatBE139,854 ops/sec±1.03%85
DataView#getFloat3298,744 ops/sec±0.80%89
NodeBuffer#readFloatBE92,769 ops/sec±0.94%93
BrowserBuffer#readUInt32LE710,861 ops/sec±0.82%92
DataView#getUint32117,893 ops/sec±0.84%91
NodeBuffer#readUInt32LE851,412 ops/sec±0.72%93
BrowserBuffer#slice1,673,877 ops/sec±0.73%94
Uint8Array#subarray6,919,243 ops/sec±0.67%90
NodeBuffer#slice4,617,604 ops/sec±0.79%93
BrowserBuffer#writeFloatBE66,011 ops/sec±0.75%93
DataView#setFloat32127,760 ops/sec±0.72%93
NodeBuffer#writeFloatBE103,352 ops/sec±0.83%93

iojs 1.8.1

MethodOperationsAccuracySampledFastest
BrowserBuffer#bracket-notation10,990,488 ops/sec±1.11%91
Uint8Array#bracket-notation11,268,757 ops/sec±0.65%97
NodeBuffer#bracket-notation11,353,260 ops/sec±0.83%94
BrowserBuffer#concat378,954 ops/sec±0.74%94
Uint8Array#concat1,358,288 ops/sec±0.97%87
NodeBuffer#concat1,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#readDoubleBE118,127 ops/sec±0.59%93
DataView#getFloat64107,332 ops/sec±0.65%91
NodeBuffer#readDoubleBE116,274 ops/sec±0.94%95
BrowserBuffer#readFloatBE150,326 ops/sec±0.58%95
DataView#getFloat32110,541 ops/sec±0.57%98
NodeBuffer#readFloatBE121,599 ops/sec±0.60%87
BrowserBuffer#readUInt32LE814,147 ops/sec±0.62%93
DataView#getUint32137,592 ops/sec±0.64%90
NodeBuffer#readUInt32LE931,650 ops/sec±0.71%96
BrowserBuffer#slice878,590 ops/sec±0.68%93
Uint8Array#subarray2,843,308 ops/sec±1.02%90
NodeBuffer#slice4,998,316 ops/sec±0.68%90
BrowserBuffer#writeFloatBE65,927 ops/sec±0.74%93
DataView#setFloat32139,823 ops/sec±0.97%89
NodeBuffer#writeFloatBE135,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-es5-local # For ES5 browsers that don't support ES6
npm run test-browser-es6-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.