Skip to content

sacru2red/react-native-randombytes-nodeify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-randombytes-nodeify

randombytes nodeify for react-native (metro bundler), react-native-randombytes wrapper

randombytes for dependencies on react-native runtime

What is this

// many packages use "randombytes"
var randomBytes = require('randombytes');

// and "react-native-randombytes" package support like this and similar API
import { randomBytes } from 'react-native-randombytes'
// or
var { randomBytes } = require('react-native-randombytes');

// install this package and set your project
// now, you can use randombytes same node syntax
var randomBytes = require('randombytes');

Installation

  1. add package
npm install react-native-randombytes-nodeify
// or
yarn add react-native-randombytes-nodeify
  1. install peerDependencies and babel-plugin
npm install react-native-randombytes
npm install --save-dev babel-plugin-module-resolver
// or
yarn add react-native-randombytes
yarn add --dev babel-plugin-module-resolver
  1. add a babel plugin to babel.config.js
module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    [
      require.resolve('babel-plugin-module-resolver'),
      {
        alias: {
          randombytes: './node_modules/react-native-randombytes-nodeify',
        },
      },
    ],
  ],
};

Etc.

  • If you try importing 'randombytes' before installing this package, you should clear react-native cache
npm start -- --reset-cache

Usage

// linking steps
// "randombytes" => "react-native-randombytes-nodeify" => "react-native-randombytes"'s randomBytes
var randomBytes = require('randombytes');

// get 4 random bytes
const rand = randomBytes(4)

// asynchronous API
randomBytes(4, (err, bytes) => {
  // bytes is a Buffer object(4 random bytes)
  console.log(bytes.toString('hex'))
})

And We can use node packages!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published