找不到变量:缓冲区 [英] Can't find variable: Buffer

查看:447
本文介绍了找不到变量:缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的反应本地应用使用节点模块,以及我服用 ReactNativify 方法在这里。

现在我全部建立起来了,我得到了加密包来加载。但是,当我在 eth-lightwallet 中添加内容时,事情变得很奇怪。



每次我添加该包后,npm都没有安装任何依赖关系。这意味着我不得不手动添加它们。每次我安装一个与eth-lightwallet相关的依赖项时,该模块都会被卸载。尽管乏味且令人讨厌,但我希望能够解决我目前的问题。



现在我遇到 Can not找到变量:Buffer ,它被抛出到标准库的util文件夹中。我查看了代码,它从全局命名空间访问Buffer。事情是,我正在将Buffer导入全局命名空间。这里看看我的global.js

  //将节点全局注入到React Native全局作用域中。 
global.Buffer = require('buffer')。Buffer;
global.process = require('process');
global.process.env.NODE_ENV = __DEV__? '发展':'生产';

//需要这样'stream-http'选择正确的默认协议。
global.location = {
protocol:'file:',
};

//不要在生产中这样做。您将要在
中修补// https://github.com/mvayngrib/react-native-randombytes或类似内容。
global.crypto = {
getRandomValues(字节阵列){
为(令i = 0; I< byteArray.length;我++){
的ByteArray [I] =数学。 floor(256 * Math.random());
}
},
};

我的猜测是在这个全局被加载之前正在评估标准库,因此错误被抛出。

解决方案

回到此处留下一个解决方案,以防止任何人遇到此问题。解决的办法是在不同的时间内尝试在不同的包装中垫片以改变加载顺序。

当TYPED_ARRAY_SUPPORT的处理方式不同时,我们尝试回到不同的版本,并且Buffer更依赖于它。在旧版本中,我们尝试了很多不同的东西,最终通过将缓冲区更新到最新版本来放弃和回溯,最后一切正常。



我的意思是说我们不确定我们是如何解决它的,但它是通过随机更改加载顺序直到我们运气好。不是一个好的答案,我知道,但我可以提供这个问题最好的。



以下是我们的global.js在最后的样子

  //将节点全局注入React Native全局作用域。 
//需要bitcoinjs-lib,web3等的加密功能。

global.Buffer = require('buffer')。Buffer;
//global.Buffer.TYPED_ARRAY_SUPPORT = false;

global.process = require('process');
global.process.env.NODE_ENV = __DEV__? '发展':'生产';

var getRandomValues = function(byteArray){
var bytes = crypto.rng.randomBytes(byteArray.length);
for(let i = 0; i< byteArray.length; i ++){
byteArray [i] = bytes [i];
}
};
//但扎克,你不是两次做同样的事吗?
//是的。初始化crypto-browserify模块最终需要
// crypto.getRandomValues存在,所以我们必须在这里添加一次。
//然而,crypto-browserify不支持getRandomValues,所以我们
//必须在加载模块后重新添加它。
global.crypto = {getRandomValues};
global.crypto.rng = require('react-native-randombytes');
global.crypto = require('crypto');
global.crypto.getRandomValues = getRandomValues;
global.crypto.rng = require('react-native-randombytes');
crypto.rng.seedSJCL();

//需要这样'stream-http'选择正确的默认协议。
global.location = {
protocol:'file:'
};


I'm trying to use node modules in my react-native app, and I'm taking the ReactNativify approach here.

I'm all set up now, and I got the crypto package to load in fine. However when I added in eth-lightwallet things have been getting weird.

Every since I added that package in, npm hasn't been installing dependancies of anything. Meaning I've had to add them in manually. And everytime I install a dependency somehow related to eth-lightwallet, that module is uninstalled. Although tedious and annoying, I'm hoping it can shed light onto my current problem.

Right now I'm running into a Can't find variable: Buffer which is being thrown in a util folder in the standard library. I've taken a look at the code and it's accessing Buffer from a global namespace. Thing is, I'm importing Buffer into the global namespace. Here's a look at my global.js

// Inject node globals into React Native global scope.
global.Buffer = require('buffer').Buffer;
global.process = require('process');
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';

// Needed so that 'stream-http' chooses the right default protocol.
global.location = {
    protocol: 'file:',
};

// Don't do this in production. You're going to want to patch in
// https://github.com/mvayngrib/react-native-randombytes or similar.
global.crypto = {
    getRandomValues(byteArray) {
        for (let i = 0; i < byteArray.length; i++) {
            byteArray[i] = Math.floor(256 * Math.random());
        }
    },
};

My guess is the standard library is being evaluated before this global is being loaded in, and thus the error is thrown.

解决方案

Coming back to this to leave a solution in case anyone is stuck on this. The solution was to essentially try to shim in different packages in different times to change the load order.

We tried going back to a different version when TYPED_ARRAY_SUPPORT was being treated differently and Buffer was more dependent on it. While on the older version we tried a lot of different things and eventually gave up and backtracked by updating buffer to the most recent version and finally everything worked.

What I mean to say is we're not sure how we fixed it, but it was by randomly changing the load order until we got lucky. Not a good answer, I'm aware, but the best I can provide for this issue.

Here's what our global.js looked like at the end

// Inject node globals into React Native global scope.
// Required for crypto functionality for bitcoinjs-lib, web3, etc.

global.Buffer = require('buffer').Buffer;
//global.Buffer.TYPED_ARRAY_SUPPORT = false;

global.process = require('process');
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';

var getRandomValues = function(byteArray) {
  var bytes = crypto.rng.randomBytes(byteArray.length);
  for (let i = 0; i < byteArray.length; i++) {
    byteArray[i] = bytes[i];
  }
};
// "But Zach, aren't you just doing the same thing twice?"
// Yes. Initializing the crypto-browserify module eventually requires
// crypto.getRandomValues to exist, so we must add it here once.
// However, crypto-browserify does not support getRandomValues, so we
// must re-add it after loading the module.
global.crypto = { getRandomValues };
global.crypto.rng = require('react-native-randombytes');
global.crypto = require('crypto');
global.crypto.getRandomValues = getRandomValues;
global.crypto.rng = require('react-native-randombytes');
crypto.rng.seedSJCL();

// Needed so that 'stream-http' chooses the right default protocol.
global.location = {
  protocol: 'file:'
};

这篇关于找不到变量:缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆