【问题标题】:Using Openpgp lib, I get: Failed to find module "stream"使用 Openpgp lib,我得到: 找不到模块“流”
【发布时间】:2019-03-25 13:36:36
【问题描述】:

我在我的项目中安装了 openpgp 库并运行了一个使用 Android 模拟器的服务。然后它会抛出以下错误:

“主”线程发生未捕获的异常。
java.lang.RuntimeException: 无法创建应用程序 com.tns.NativeScriptApplication: com.tns.NativeScriptException:
调用模块函数时出错
调用模块函数时出错
调用模块函数时出错
调用模块函数时出错
错误:com.tns.NativeScriptException:找不到模块:“流”,相对于:app/tns_modules/

在此问题的其他解决方案中,建议从 package.json 文件中删除对 @angular/platform-server 的调用,但我的 package.json 没有这个。

以下是我的代码的相关部分:

rsa.key.service:

import { Injectable } from '@angular/core';
//import * as openpgp from 'openpgp';
  import * as openpgp from 'openpgp';

@Injectable()
export class RsaKeyService {

  constructor() { }

  generateKey () {
    const name: string = this.hashGen(7);
    const email: string = this.hashGen(7) + '@test.com';
    const keyOptions = {
        userIds: [{ name: name, email: email }],
        passphrase: this.hashGen(10),
        curve: 'ed25519',
        compression: openpgp.enums.compression.zip
    }
    const user = {};
    return new Promise((resolve) => {
        openpgp.generateKey(keyOptions)
        .then((key) => {
            user['privateKey'] = key.privateKeyArmored;
            user['publicKey'] = key.publicKeyArmored;
            localStorage.setItem('RSAKey', JSON.stringify(user));
            resolve(true);
        });
    })

    }

    async encrypt(stringToEncrypt: string, receiverPublicKey) {
        const options: any = {
            data: stringToEncrypt,
            publicKeys: await openpgp.key.readArmored(receiverPublicKey).then((data) => {return data.keys})
        };
        return await openpgp.encrypt(options)
          .then((cipherText) => {
              return cipherText.data;
          }).catch((error) => {
              console.log('Error', error);
        })
    }

   async decrypt(encryptedMessage: string, dataAccess) {
        const privateKey = await openpgp.key.readArmored(dataAccess['privateKey']).then((data) => {return data.keys[0]});
        privateKey.decrypt(dataAccess['passphrase']);
            return openpgp.decrypt({
                privateKeys: [privateKey],
                message: await openpgp.message.readArmored(encryptedMessage)
            }).then((decryptedData) => {
                return decryptedData.data;
            })
    }

    hashGen(length) {
        let text = '';
        const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        for (let i = 0; i < length; i++) {
          text += possible.charAt(Math.floor(Math.random() * possible.length));
        }
        return text;
    }

}

应用模块:

import { NgModule, NO_ERRORS_SCHEMA } from "@angular/core";
import { NativeScriptModule } from "nativescript-angular/nativescript.module";

import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";

// Uncomment and add to NgModule imports if you need to use two-way binding
import { NativeScriptFormsModule } from "nativescript-angular/forms";

// Uncomment and add to NgModule imports if you need to use the HttpClient wrapper
import { NativeScriptHttpClientModule } from "nativescript-angular/http-client";
import { LoginComponent } from "./modules/components/login/login.component";
import { UserServiceService } from "./core/services/UserService.service";
import { HttpClientModule, HTTP_INTERCEPTORS } from "@angular/common/http";
import { HttpErrorInterceptor } from "./core/interceptors/http.interseptor";
import { HomeComponent } from "./modules/components/home/home.component";
import { AuthGuardService } from "./core/guards/guards.services";
import { token } from "./core/authentication/token";
import { HttpService } from "./core/services/http.service";
import { HttpModule } from "@angular/http";
import { HttpHeadersInterceptor } from "./core/interceptors/http.header.interceptor";
import { NativeScriptHttpModule } from "nativescript-angular/http";
import { RsaKeyService } from "./core/services/rsa.key.service";


@NgModule({
    bootstrap: [
        AppComponent
    ],
    imports: [
        NativeScriptModule,
        AppRoutingModule,
        NativeScriptFormsModule,
        HttpClientModule,
        HttpModule,
        NativeScriptHttpModule
    ],
    declarations: [
        AppComponent,
        LoginComponent,
        HomeComponent,
    ],
    providers: [
        HttpService,
        UserServiceService,
        AuthGuardService,
        RsaKeyService,
        token,
        {
            provide: HTTP_INTERCEPTORS,
            useClass: HttpErrorInterceptor,
            multi: true
        },
        {
            provide: HTTP_INTERCEPTORS,
            useClass: HttpHeadersInterceptor,
            multi: true
        }
    ],
    schemas: [
        NO_ERRORS_SCHEMA
    ]
})
/*
Pass your application module to the bootstrapModule function located in main.ts to start your app
*/
export class AppModule { }

package.json:

{
  "nativescript": {
    "id": "org.nativescript.CriptoZoneApp",
    "tns-ios": {
      "version": "5.2.0"
    },
    "tns-android": {
      "version": "5.2.1"
    }
  },
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "nativescript-angular": "~7.2.1",
    "nativescript-localstorage": "^2.0.0",
    "nativescript-theme-core": "~1.0.4",
    "openpgp": "^4.4.10",
    "reflect-metadata": "~0.1.12",
    "rxjs": "~6.3.0",
    "rxjs-compat": "^6.4.0",
    "tns-core-modules": "~5.2.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~7.2.0",
    "@nativescript/schematics": "~0.5.0",
    "@ngtools/webpack": "~7.2.0",
    "nativescript-dev-typescript": "~0.8.0",
    "nativescript-dev-webpack": "~0.20.0"
  },
  "gitHead": "f548ec926e75201ab1b7c4a3a7ceefe7a4db15af",
  "readme": "NativeScript Application"
}

【问题讨论】:

    标签: angular nativescript nativescript-angular openpgp


    【解决方案1】:

    该库似乎使用了仅在节点环境中可用的stream 模块。只能使用纯 JavaScript api 编写的 NPM 模块,否则可能无法在 {N} 环境下工作。

    【讨论】:

    • 我明白了。 thx 但是如何在 NS 中使用 lib openpgp.js?如果他们有任何想法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 2020-10-22
    • 2021-05-02
    • 2020-11-11
    相关资源
    最近更新 更多