@craftzdog/react-native-buffer

中文文档
6.1.2发布于 2026/05/08MITTypeScriptCommonJS近 30 天下载 159.4万

Node.js Buffer API, for React Native

原始使用文档
文档目录17 节
  1. features
  2. install
  3. usage
  4. how does it work?
  5. conversion packages
  6. convert typed array to buffer
  7. convert buffer to typed array
  8. convert blob to buffer
  9. convert buffer to blob
  10. convert arraybuffer to buffer
  11. convert buffer to arraybuffer
  12. performance
  13. Testing the project
  14. JavaScript Standard Style
  15. Credit
  16. Security Policies and Procedures
  17. license

@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
  • 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:

import { Buffer } from "@craftzdog/react-native-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.

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

4x faster than base64-js on an iPhone 11 Pro when dealing with base64, thanks to react-native-quick-base64.

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:

Credit

This was originally forked from feross/buffer.

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) Takuya Matsuyama, and other contributors. Originally forked from an MIT-licensed module by Feross Aboukhadijeh and Romain Beauxis.

版本与发布记录

@craftzdog/react-native-buffer 共发布 6 个版本,首次发布于 2021/02/09;过去 12 个月发布了 1 个版本。

版本发布日期距今
6.1.2latest2026/05/086 个月前
6.1.12025/10/031.1 年前
6.1.02025/05/271.5 年前
6.0.52022/07/224 年前
6.0.42022/01/214 年前
6.0.32021/02/095 年前

关于 @craftzdog/react-native-buffer 的常见问题

@craftzdog/react-native-buffer 是什么?

Node.js Buffer API, for React Native

如何安装 @craftzdog/react-native-buffer?

在项目目录运行 npm install @craftzdog/react-native-buffer,或使用 pnpm add / yarn add / bun add。

@craftzdog/react-native-buffer 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@craftzdog/react-native-buffer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@craftzdog/react-native-buffer 可以商用吗?

@craftzdog/react-native-buffer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@craftzdog/react-native-buffer 的下载量有多少?

根据 npm 官方下载统计,@craftzdog/react-native-buffer 在 2026/10/11 – 2026/11/09 这 30 天内被下载 1,593,882 次,日均约 53,129 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@craftzdog/react-native-buffer 还在维护吗?

最新版本 6.1.2 发布于 2026/05/08(6 个月前);至今共发布 6 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/02/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。