如何在 nativescript 中使用ripple-lib [英] How to use ripple-lib with nativescript

查看:52
本文介绍了如何在 nativescript 中使用ripple-lib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的 stackoverflowers,

Dear stackoverflowers,

我在 nativescript 中遇到了加密问题.我想使用 NPM 包ripple-lib,但发现没有使用nativescript-nodeify.我怎样才能让这个包与 nativescript vuejs 一起工作.我还需要使用加密包.

i'm having troubles with crypto in nativescript. I want to use a NPM package ripple-lib but found no luck with nativescript-nodeify. How can I make this package work with nativescript vuejs. I Need te make use of the crypto package aswell.

如果我需要这个包,我首先会收到一个错误 ->错误是:找不到模块/Websites/repo/tests/FirebaseVuejs/platforms/ios/FirebaseVuejs/app/tns_modules/nativescript-nodeify/patch-npm-packages.js".

If i require the package I first get an error -> Error is: Cannot find module '/Websites/repo/tests/FirebaseVuejs/platforms/ios/FirebaseVuejs/app/tns_modules/nativescript-nodeify/patch-npm-packages.js'.

如果我重建没有错误,包仍然给我同样的错误,因为我在没有 nativescript-nodeify 的情况下运行它:

than if i rebuild there is no error and the package is still giving me the same error as i ran it without nativescript-nodeify:

控制台错误文件:///app/bundle.js:950:22:类型错误:crypto.randomBytes 不是函数.(在 'crypto.randomBytes(16)' 中,'crypto.randomBytes' 未定义)

CONSOLE ERROR file:///app/bundle.js:950:22: TypeError: crypto.randomBytes is not a function. (In 'crypto.randomBytes(16)', 'crypto.randomBytes' is undefined)

推荐答案

ripple-lib正在使用某些依赖项,这些依赖项使用 node.js 特定的或全局可用的模块,并通过 require 或直接从 global 上下文访问它们.实际上,NativeScript 环境与 node.js 和浏览器都不同,因此我们必须确保满足所有依赖项并且可以在 {N} 环境中运行.

ripple-lib is using certain dependencies, that use node.js specific or globally available modules and access them via require or directly from global context. In reality, NativeScript environment is different from both node.js and browser, so we have to make sure all dependencies are met and are available to run in {N} environment.

对于许多用例,nativescript-nodeify 插件可以完成这项工作,但使用情况ripple-lib,它不能解决问题,所以我们必须手动处理兼容性:

For many use-cases nativescript-nodeify plugin does the job, but case of using ripple-lib, it does not resolve the issue so we have to take care of compatibilities manually:

1) ripple-lib 的依赖 bignumber.js 正在使用本机节点库 crypto.由于它在 {N} 运行时不可用,我们必须使用专门设计的模块 nativescript-randombytes 并使用 webpack 的 providePlugin 使其可在全球范围内访问:

1) ripple-lib's dependency bignumber.js is using native node library crypto. Since it is not available in {N} runtime, we have to use specially designed module nativescript-randombytes and make it accessible globally with webpack's providePlugin:

  1. 将 NativeScript 插件添加到项目中:

  1. Add NativeScript plugin to the project:

tns plugin add nativescript-randombytes

  • 创建一个文件,作为 crypto 模块的部分实现:

    // Example path: project/app/shims/crypto.js
    
    module.exports.randomBytes = require('nativescript-randombytes')
    

  • 将其添加到插件配置中的webpack.config.js:

    plugins: [
      ..., // other plugins
      new webpack.ProvidePlugin({
            crypto: resolve(__dirname, 'app/shims/crypto.js')
        })
    ]
    

  • webpack.config.js中为我们的crypto版本添加resolve.alias,所以子依赖require 我们的 crypto 实现:

  • Add resolve.alias for our version of crypto in webpack.config.js, so child dependencies require our crypto implementation:

    alias: {
      ..., // Other aliases
      'crypto': resolve(__dirname, 'app/shims/crypto.js')
    }
    

  • 2) 缺少一些必需的模块,但我们可以手动安装它们,因为它们与 NativeScript 运行时兼容:

    2) There are few required modules that are missing, though we can install them manually, as they are compatible with NativeScript runtime:

    npm i -S lodash bufferutil tls utf-8-validate
    

    3) ripple-lib 似乎不适用于可用的 websockets 实现,所以我们必须使用 NativeScript 的跨平台解决方案 - nativescript-websockets

    3) ripple-lib doesn't seem to work with available websockets implementation, so we have to use cross-platform solution for NativeScript - nativescript-websockets

    1. 向项目添加插件:

    1. Add plugin to the project:

    tns plugin add nativescript-websockets
    

  • 并在ripple-lib导入之前导入:

    import 'nativescript-websockets'
    import { RippleAPI } from 'ripple-lib'
    

  • 4) net node.js 模块也需要建立连接,可以用 webpack 模拟 node 模拟配置.在 webpack.config.js 中的 node 配置选项下添加以下内容:

    4) net node.js module is also required to establish connection, which can be mocked with webpack node mocks configuration. Add the following under node config options in webpack.config.js:

       node: {
            ..., // Other default NativeScript shims 
            "net": 'mock',
        },
    

    5) 一些库对它们运行的​​环境感到困惑并默认使用 Node,这使得它们请求不可用的模块.要修复它,请将 resolve.aliasFields 设置为浏览器",如下所示:

    5) Some libraries are confused about the environment they are running in and default to Node, which makes them request non-available modules. To fix it set resolve.aliasFields to 'browser', as following:

       resolve: {
         // other options, aliases, etc
         aliasFields: ['browser']
       }
    

    6) 另一个无法自行运行的库,因为它依赖于 crypto 模块是 create-hash.好消息是它附带了一个浏览器版本,我们可以用它来修复它.在 resolve 选项中为 webpack.config.js 添加另一个 alias:

    6) Another library that can't run itself, as it depends on crypto module is create-hash. Good thing is that it is shipped with a browser build which we can use to fix it. Add another alias in resolve options for webpack.config.js:

      alias: {
        ..., // Other aliases
        'create-hash': 'create-hash/browser'
      }
    

    7) 完成所有步骤后,确保清理项目文件.执行以下操作:

    7) After all steps are done make sure to clean up project files. Do the following:

    1. 删除文件夹:

    1. Delete folders:

    rm -rf platforms/android # or ios
    rm -rf hooks
    rm -rf node_modules
    

  • 重新安装软件包并添加平台:

  • Reinstall packages and add platforms:

    npm i
    tns platform add android # or ios       
    

  • 这篇关于如何在 nativescript 中使用ripple-lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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