@consento/sync-randombytes

中文文档
1.0.5发布于 2020/05/07MITCommonJS近 30 天下载 1.8万

Synchronous randombytes function that works in node, the browser & react-native!

原始使用文档
文档目录4 节
  1. Usage
  2. Why?
  3. How it works?
  4. License

sync-randombytes

Build Status JavaScript Style Guide

sync-randombytes is a polyfill for crypto.randomBytes that works in node, browsers, react-native (with support for versions < 0.60) and expo.

npm i sync-randombytes --save

Usage

You can use it like an other randomBytes function by calling:

const randomBytes = require('sync-randombytes')
const randomUint8Array = randomBytes(new Uint8Array(16))

Why?

  • React-native < 0.60 does not support crypto.randomBytes and you need a custom solution to implement a sync random method
  • Metro bundlers complain when you try to use react-native-randombytes as it's dependencies try to require('crypto') - which is not available in react-native.

How it works?

  • It uses window.crypto.getRandomBytes if available.
  • If 'crypto' can be "required" it will use
    • from node > 6.13: crypo.randomFill - or a polyfill that implements it using crypto.randomBytes for node < 6.13
  • .. else it will use seedrandom
    • with a randomseed from the native environment.
    • if not available: with a custom random-seed if expo is available. Random quality degraded
    • else the default random-seed if. Random quality degraded

License

MIT

with some source code adopted from react-native-randombytes

版本与发布记录

@consento/sync-randombytes 共发布 6 个版本,首次发布于 2019/09/30;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.5latest2020/05/076 年前
1.0.42020/04/216 年前
1.0.32019/12/276 年前
1.0.22019/10/027 年前
1.0.12019/10/027 年前
1.0.02019/09/307 年前

关于 @consento/sync-randombytes 的常见问题

@consento/sync-randombytes 是什么?

Synchronous randombytes function that works in node, the browser & react-native!

如何安装 @consento/sync-randombytes?

在项目目录运行 npm install @consento/sync-randombytes,或使用 pnpm add / yarn add / bun add。

@consento/sync-randombytes 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/consento__sync-randombytes 是否存在。

@consento/sync-randombytes 支持 ESM 还是 CommonJS?

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

@consento/sync-randombytes 可以商用吗?

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

@consento/sync-randombytes 的下载量有多少?

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

@consento/sync-randombytes 还在维护吗?

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