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.
npm install @craftzdog/react-native-buffer react-native-quick-base64
cd ios && pod install
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:
import { Buffer } from "@craftzdog/react-native-buffer";
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.
Use typedarray-to-buffer
to convert any kind of typed array to a Buffer
. Does not perform a copy, so it's super fast.
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
.
Use blob-to-buffer
to convert a Blob
to a Buffer
.
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" });
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);
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.
4x faster than base64-js on an iPhone 11 Pro when dealing with base64, thanks to react-native-quick-base64.
This was originally forked from feross/buffer.
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.
MIT. Copyright (C) Takuya Matsuyama, and other contributors. Originally forked from an MIT-licensed module by Feross Aboukhadijeh and Romain Beauxis.