From d381226f21f1d0605d06ccae56c38ab6b12f88f0 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Sat, 27 May 2017 18:43:11 +0200 Subject: [PATCH] Simplify loading of the emscripten lib. This removes an ugly hack and makes it possible to access the emscripten compiled library from within nodejs test cases more easily. --- emscripten/README | 4 ++ .../taler-emscripten-lib.js | 0 gulpfile.js | 4 +- src/{ => crypto}/cryptoApi-test.ts | 2 +- src/{ => crypto}/cryptoApi.ts | 4 +- src/{ => crypto}/cryptoWorker.ts | 39 +++++++++++-------- .../emscInterface-test.ts} | 2 +- .../emscInterface.ts} | 9 +++-- .../emscLoader.d.ts} | 26 +++++++------ src/crypto/emscLoader.js | 38 ++++++++++++++++++ src/wallet.ts | 2 +- tsconfig.json | 17 ++++---- webpack.config.js | 7 +--- 13 files changed, 104 insertions(+), 50 deletions(-) create mode 100644 emscripten/README rename {src/emscripten => emscripten}/taler-emscripten-lib.js (100%) rename src/{ => crypto}/cryptoApi-test.ts (99%) rename src/{ => crypto}/cryptoApi.ts (99%) rename src/{ => crypto}/cryptoWorker.ts (96%) rename src/{emscriptif-test.ts => crypto/emscInterface-test.ts} (99%) rename src/{emscriptif.ts => crypto/emscInterface.ts} (99%) rename src/{emscripten/taler-emscripten-lib.d.ts => crypto/emscLoader.d.ts} (63%) create mode 100644 src/crypto/emscLoader.js diff --git a/emscripten/README b/emscripten/README new file mode 100644 index 000000000..e13ea8ed1 --- /dev/null +++ b/emscripten/README @@ -0,0 +1,4 @@ +The taler-emscripten-lib.js is compiled from C using emscripten. + +See https://git.taler.net/libtalerutil-emscripten.git for automated build +instructions and the functions exported from this module. diff --git a/src/emscripten/taler-emscripten-lib.js b/emscripten/taler-emscripten-lib.js similarity index 100% rename from src/emscripten/taler-emscripten-lib.js rename to emscripten/taler-emscripten-lib.js diff --git a/gulpfile.js b/gulpfile.js index ec8321dc0..568300614 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -53,7 +53,7 @@ const po2json = require("po2json"); const paths = { ts: { src: [ - "src/**/*.{ts,tsx}", + "src/**/*.{ts,tsx,js}", "!src/**/*-test*.ts", ], decl: [ @@ -112,6 +112,8 @@ const tsBaseArgs = { strict: true, outDir: "build/src/", noImplicitAny: true, + allowJs: true, + checkJs: true, }; diff --git a/src/cryptoApi-test.ts b/src/crypto/cryptoApi-test.ts similarity index 99% rename from src/cryptoApi-test.ts rename to src/crypto/cryptoApi-test.ts index 8350defbc..89b74d776 100644 --- a/src/cryptoApi-test.ts +++ b/src/crypto/cryptoApi-test.ts @@ -1,5 +1,5 @@ import {CryptoApi} from "./cryptoApi"; -import {ReserveRecord, DenominationRecord, DenominationStatus} from "./types"; +import {ReserveRecord, DenominationRecord, DenominationStatus} from "../types"; import {test, TestLib} from "talertest"; let masterPub1: string = "CQQZ9DY3MZ1ARMN5K1VKDETS04Y2QCKMMCFHZSWJWWVN82BTTH00"; diff --git a/src/cryptoApi.ts b/src/crypto/cryptoApi.ts similarity index 99% rename from src/cryptoApi.ts rename to src/crypto/cryptoApi.ts index 45efb63e3..a386eab42 100644 --- a/src/cryptoApi.ts +++ b/src/crypto/cryptoApi.ts @@ -33,11 +33,11 @@ import { RefreshSessionRecord, WireFee, PayCoinInfo, -} from "./types"; +} from "../types"; import { OfferRecord, CoinWithDenom, -} from "./wallet"; +} from "../wallet"; /** diff --git a/src/cryptoWorker.ts b/src/crypto/cryptoWorker.ts similarity index 96% rename from src/cryptoWorker.ts rename to src/crypto/cryptoWorker.ts index bf802a32b..36b3b924a 100644 --- a/src/cryptoWorker.ts +++ b/src/crypto/cryptoWorker.ts @@ -22,27 +22,34 @@ /** * Imports. */ -import * as native from "./emscriptif"; import { - PreCoinRecord, - PayCoinInfo, AmountJson, - RefreshSessionRecord, - RefreshPreCoinRecord, - ReserveRecord, + Amounts, + CoinPaySig, + CoinRecord, CoinStatus, + DenominationRecord, + PayCoinInfo, PaybackRequest, -} from "./types"; + PreCoinRecord, + RefreshPreCoinRecord, + RefreshSessionRecord, + ReserveRecord, + WireFee, +} from "../types"; import create = chrome.alarms.create; -import {OfferRecord} from "./wallet"; -import {CoinWithDenom} from "./wallet"; -import {CoinPaySig, CoinRecord} from "./types"; -import {DenominationRecord, Amounts, WireFee} from "./types"; -import {Amount} from "./emscriptif"; -import {HashContext} from "./emscriptif"; -import {RefreshMeltCoinAffirmationPS} from "./emscriptif"; -import {EddsaPublicKey} from "./emscriptif"; -import {HashCode} from "./emscriptif"; +import { + CoinWithDenom, + OfferRecord, +} from "../wallet"; +import * as native from "./emscInterface"; +import { + Amount, + EddsaPublicKey, + HashCode, + HashContext, + RefreshMeltCoinAffirmationPS, +} from "./emscInterface"; namespace RpcFunctions { diff --git a/src/emscriptif-test.ts b/src/crypto/emscInterface-test.ts similarity index 99% rename from src/emscriptif-test.ts rename to src/crypto/emscInterface-test.ts index 8b7a9eff4..4f57bf802 100644 --- a/src/emscriptif-test.ts +++ b/src/crypto/emscInterface-test.ts @@ -1,5 +1,5 @@ import {test, TestLib} from "talertest"; -import * as native from "./emscriptif"; +import * as native from "./emscInterface"; test("string hashing", (t: TestLib) => { let x = native.ByteArray.fromStringWithNull("hello taler"); diff --git a/src/emscriptif.ts b/src/crypto/emscInterface.ts similarity index 99% rename from src/emscriptif.ts rename to src/crypto/emscInterface.ts index 60653b66e..52c6c965e 100644 --- a/src/emscriptif.ts +++ b/src/crypto/emscInterface.ts @@ -26,8 +26,11 @@ /** * Imports. */ -import {AmountJson} from "./types"; -import * as emscLib from "./emscripten/taler-emscripten-lib"; +import {AmountJson} from "../types"; +import {getLib, EmscFunGen} from "./emscLoader"; + + +const emscLib = getLib(); /** @@ -45,7 +48,7 @@ const GNUNET_SYSERR = -1; /** * Get an emscripten-compiled function. */ -const getEmsc: emscLib.EmscFunGen = (name: string, ret: any, argTypes: any[]) => { +const getEmsc: EmscFunGen = (name: string, ret: any, argTypes: any[]) => { return (...args: any[]) => { return emscLib.ccall(name, ret, argTypes, args); } diff --git a/src/emscripten/taler-emscripten-lib.d.ts b/src/crypto/emscLoader.d.ts similarity index 63% rename from src/emscripten/taler-emscripten-lib.d.ts rename to src/crypto/emscLoader.d.ts index 25cfc623e..e46ed7f13 100644 --- a/src/emscripten/taler-emscripten-lib.d.ts +++ b/src/crypto/emscLoader.d.ts @@ -14,6 +14,9 @@ TALER; see the file COPYING. If not, see */ + +declare function getLib(): EmscLib; + export interface EmscFunGen { (name: string, ret: string, @@ -30,23 +33,22 @@ export interface EmscFunGen { } -export var cwrap: EmscFunGen; +interface EmscLib { + cwrap: EmscFunGen; -export function ccall(name: string, ret:"number"|"string", argTypes: any[], args: any[]): any + ccall(name: string, ret:"number"|"string", argTypes: any[], args: any[]): any -export function stringToUTF8(s: string, addr: number, maxLength: number): void + stringToUTF8(s: string, addr: number, maxLength: number): void -export function _free(ptr: number): void; + _free(ptr: number): void; -export function _malloc(n: number): number; + _malloc(n: number): number; -export function Pointer_stringify(p: number, len?: number): string; + Pointer_stringify(p: number, len?: number): string; -export function getValue(ptr: number, type: string, noSafe?: boolean): number; + getValue(ptr: number, type: string, noSafe?: boolean): number; -export function setValue(ptr: number, value: number, type: string, - noSafe?: boolean): void; + setValue(ptr: number, value: number, type: string, noSafe?: boolean): void; -export function writeStringToMemory(s: string, - buffer: number, - dontAddNull?: boolean): void; + writeStringToMemory(s: string, buffer: number, dontAddNull?: boolean): void; +} diff --git a/src/crypto/emscLoader.js b/src/crypto/emscLoader.js new file mode 100644 index 000000000..723b8ae36 --- /dev/null +++ b/src/crypto/emscLoader.js @@ -0,0 +1,38 @@ +/* + This file is part of TALER + (C) 2017 Inria and GNUnet e.V. + + TALER is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + TALER is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + TALER; see the file COPYING. If not, see + */ + + +// @ts-nocheck + +/** + * Load the taler emscripten lib. + * + * If in a WebWorker, importScripts is used. Inside a browser, + * the module must be globally available. + */ +export default function getLib() { + if (window.TalerEmscriptenLib) { + return TalerEmscriptenLib; + } + if (importScripts) { + importScripts('/src/emscripten/taler-emscripten-lib.js') + if (TalerEmscriptenLib) { + throw Error("can't import TalerEmscriptenLib"); + } + return TalerEmscriptenLib + } + throw Error("Can't find TalerEmscriptenLib."); +} diff --git a/src/wallet.ts b/src/wallet.ts index f48fcc76e..a2a9e83f5 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -72,7 +72,7 @@ import { flatMap, getTalerStampSec, } from "./helpers"; -import {CryptoApi} from "./cryptoApi"; +import {CryptoApi} from "./crypto/cryptoApi"; import URI = require("urijs"); diff --git a/tsconfig.json b/tsconfig.json index 7409f2b81..0c684214e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,9 @@ "noFallthroughCasesInSwitch": true, "strict": true, "outDir": "build/src/", - "noImplicitAny": true + "noImplicitAny": true, + "allowJs": true, + "checkJs": true }, "files": [ "decl/chrome/chrome.d.ts", @@ -25,12 +27,13 @@ "src/chromeBadge.ts", "src/components.ts", "src/content_scripts/notify.ts", - "src/cryptoApi-test.ts", - "src/cryptoApi.ts", - "src/cryptoWorker.ts", - "src/emscripten/taler-emscripten-lib.d.ts", - "src/emscriptif-test.ts", - "src/emscriptif.ts", + "src/crypto/cryptoApi-test.ts", + "src/crypto/cryptoApi.ts", + "src/crypto/cryptoWorker.ts", + "src/crypto/emscInterface-test.ts", + "src/crypto/emscInterface.ts", + "src/crypto/emscLoader.d.ts", + "src/crypto/emscLoader.js", "src/helpers-test.ts", "src/helpers.ts", "src/http.ts", diff --git a/webpack.config.js b/webpack.config.js index a99d2a7c8..deca2ae76 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -34,13 +34,8 @@ module.exports = function (env) { base.plugins.push(new webpack.LoaderOptionsPlugin({minimize: true})); } const configWebWorker = { - entry: {"cryptoWorker": "./src/cryptoWorker.ts"}, + entry: {"cryptoWorker": "./src/crypto/cryptoWorker.ts"}, target: "webworker", - externals: { - // A big hack to load taler-emscripten-lib from the environment, - // because we exclude it from the bundle. - "./emscripten/taler-emscripten-lib": "(self.TalerEmscriptenLib = {}, importScripts('/src/emscripten/taler-emscripten-lib.js'), TalerEmscriptenLib)", - }, }; const configBackground = {