From 1c7423dbad6c7a7d8efffadb3c854d961da17336 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 17 Jun 2021 12:40:42 -0300 Subject: [PATCH] fix support with webpack{4,5} in browser environment added missing .js extension to the imports split index use browser field in package json --- packages/taler-wallet-core/package.json | 8 ++- packages/taler-wallet-core/rollup.config.js | 35 ++++++++- .../src/crypto/primitives/nacl-fast.ts | 46 ------------ .../crypto/workers/cryptoImplementation.ts | 8 +-- .../src/crypto/workers/nodeThreadWorker.ts | 6 +- .../src/crypto/workers/synchronousWorker.ts | 6 +- .../src/headless/NodeHttpLib.ts | 10 +-- .../taler-wallet-core/src/headless/helpers.ts | 10 +-- .../taler-wallet-core/src/index.browser.ts | 72 +++++++++++++++++++ packages/taler-wallet-core/src/index.node.ts | 43 +++++++++++ packages/taler-wallet-core/src/index.ts | 9 +-- .../src/operations/backup/export.ts | 4 +- .../src/operations/backup/import.ts | 4 +- .../taler-wallet-core/src/operations/pay.ts | 2 +- .../src/util/RequestThrottler.ts | 2 +- .../taler-wallet-core/src/wallet-api-types.ts | 2 +- packages/taler-wallet-core/src/wallet.ts | 36 +++++----- 17 files changed, 200 insertions(+), 103 deletions(-) create mode 100644 packages/taler-wallet-core/src/index.browser.ts create mode 100644 packages/taler-wallet-core/src/index.node.ts diff --git a/packages/taler-wallet-core/package.json b/packages/taler-wallet-core/package.json index e14ae0dd2..0d726a6d7 100644 --- a/packages/taler-wallet-core/package.json +++ b/packages/taler-wallet-core/package.json @@ -29,9 +29,13 @@ "lib/" ], "main": "./dist/taler-wallet-core.js", - "module": "./lib/index.js", + "browser": { + "./dist/taler-wallet-core.js": "./dist/taler-wallet-core.browser.js", + "./lib/index.node.js": "./lib/index.browser.js" + }, + "module": "./lib/index.node.js", "type": "module", - "types": "./lib/index.d.ts", + "types": "./lib/index.node.d.ts", "devDependencies": { "@ava/typescript": "^1.1.1", "@gnu-taler/pogen": "workspace:*", diff --git a/packages/taler-wallet-core/rollup.config.js b/packages/taler-wallet-core/rollup.config.js index bcc8e5b26..fa5e1905c 100644 --- a/packages/taler-wallet-core/rollup.config.js +++ b/packages/taler-wallet-core/rollup.config.js @@ -6,8 +6,8 @@ import builtins from "builtin-modules"; import pkg from "./package.json"; import sourcemaps from 'rollup-plugin-sourcemaps'; -export default { - input: "lib/index.js", +const nodeEntryPoint = { + input: "lib/index.node.js", output: { file: pkg.main, format: "cjs", @@ -32,3 +32,34 @@ export default { ], } +const browserEntryPoint = { + input: "lib/index.browser.js", + output: { + file: pkg.browser[pkg.main], + format: "cjs", + sourcemap: true, + }, + external: builtins, + plugins: [ + nodeResolve({ + preferBuiltins: true, + }), + + sourcemaps(), + + commonjs({ + include: [/node_modules/, /dist/], + extensions: [".js"], + ignoreGlobal: false, + sourceMap: true, + }), + + json(), + ], +} + +export default [ + nodeEntryPoint, + browserEntryPoint +] + diff --git a/packages/taler-wallet-core/src/crypto/primitives/nacl-fast.ts b/packages/taler-wallet-core/src/crypto/primitives/nacl-fast.ts index eab4a2e5c..909c6a60a 100644 --- a/packages/taler-wallet-core/src/crypto/primitives/nacl-fast.ts +++ b/packages/taler-wallet-core/src/crypto/primitives/nacl-fast.ts @@ -5,16 +5,6 @@ // Implementation derived from TweetNaCl version 20140427. // See for details: http://tweetnacl.cr.yp.to/ -import * as mod from "module"; - -let require: any; - -if (typeof require !== "function" && mod.default && mod.default.createRequire) { - // We need this require function to synchronously - // import the "crypto" module in the CSPRNG initialization. - require = mod.default.createRequire(import.meta.url); -} - const gf = function (init: number[] = []): Float64Array { const r = new Float64Array(16); if (init) for (let i = 0; i < init.length; i++) r[i] = init[i]; @@ -2806,10 +2796,6 @@ function checkArrayTypes(...args: Uint8Array[]): void { } } -function cleanup(arr: Uint8Array): void { - for (let i = 0; i < arr.length; i++) arr[i] = 0; -} - export function randomBytes(n: number): Uint8Array { const b = new Uint8Array(n); randombytes(b, n); @@ -3031,35 +3017,3 @@ export function secretbox_open( return m.subarray(crypto_secretbox_ZEROBYTES); } -function initPRNG() { - // Initialize PRNG if environment provides CSPRNG. - // If not, methods calling randombytes will throw. - // @ts-ignore-error - const cr = typeof self !== "undefined" ? self.crypto || self.msCrypto : null; - if (cr && cr.getRandomValues) { - // Browsers. - const QUOTA = 65536; - setPRNG(function (x: Uint8Array, n: number) { - let i; - const v = new Uint8Array(n); - for (i = 0; i < n; i += QUOTA) { - cr.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))); - } - for (i = 0; i < n; i++) x[i] = v[i]; - cleanup(v); - }); - } else if (typeof require !== "undefined") { - // Node.js. - // eslint-disable-next-line @typescript-eslint/no-var-requires - const cr = require("crypto"); - if (cr && cr.randomBytes) { - setPRNG(function (x: Uint8Array, n: number) { - const v = cr.randomBytes(n); - for (let i = 0; i < n; i++) x[i] = v[i]; - cleanup(v); - }); - } - } -} - -initPRNG(); diff --git a/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts b/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts index 1969dee95..9cffef035 100644 --- a/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts +++ b/packages/taler-wallet-core/src/crypto/workers/cryptoImplementation.ts @@ -45,7 +45,7 @@ import { MakeSyncSignatureRequest, } from "@gnu-taler/taler-util"; import { AmountJson, Amounts } from "@gnu-taler/taler-util"; -import * as timer from "../../util/timer"; +import * as timer from "../../util/timer.js"; import { encodeCrock, decodeCrock, @@ -63,9 +63,9 @@ import { setupRefreshTransferPub, setupTipPlanchet, setupWithdrawPlanchet, -} from "../talerCrypto"; -import { randomBytes } from "../primitives/nacl-fast"; -import { kdf } from "../primitives/kdf"; +} from "../talerCrypto.js"; +import { randomBytes } from "../primitives/nacl-fast.js"; +import { kdf } from "../primitives/kdf.js"; import { Timestamp, timestampTruncateToSecond } from "@gnu-taler/taler-util"; import { Logger } from "@gnu-taler/taler-util"; diff --git a/packages/taler-wallet-core/src/crypto/workers/nodeThreadWorker.ts b/packages/taler-wallet-core/src/crypto/workers/nodeThreadWorker.ts index 453a46945..90016ab42 100644 --- a/packages/taler-wallet-core/src/crypto/workers/nodeThreadWorker.ts +++ b/packages/taler-wallet-core/src/crypto/workers/nodeThreadWorker.ts @@ -17,10 +17,10 @@ /** * Imports */ -import { CryptoWorkerFactory } from "./cryptoApi"; -import { CryptoWorker } from "./cryptoWorker"; +import { CryptoWorkerFactory } from "./cryptoApi.js"; +import { CryptoWorker } from "./cryptoWorker.js"; import os from "os"; -import { CryptoImplementation } from "./cryptoImplementation"; +import { CryptoImplementation } from "./cryptoImplementation.js"; import { Logger } from "@gnu-taler/taler-util"; const logger = new Logger("nodeThreadWorker.ts"); diff --git a/packages/taler-wallet-core/src/crypto/workers/synchronousWorker.ts b/packages/taler-wallet-core/src/crypto/workers/synchronousWorker.ts index 5327670bd..f6b8ac5d7 100644 --- a/packages/taler-wallet-core/src/crypto/workers/synchronousWorker.ts +++ b/packages/taler-wallet-core/src/crypto/workers/synchronousWorker.ts @@ -14,10 +14,10 @@ GNU Taler; see the file COPYING. If not, see */ -import { CryptoImplementation } from "./cryptoImplementation"; +import { CryptoImplementation } from "./cryptoImplementation.js"; -import { CryptoWorkerFactory } from "./cryptoApi"; -import { CryptoWorker } from "./cryptoWorker"; +import { CryptoWorkerFactory } from "./cryptoApi.js"; +import { CryptoWorker } from "./cryptoWorker.js"; /** * The synchronous crypto worker produced by this factory doesn't run in the diff --git a/packages/taler-wallet-core/src/headless/NodeHttpLib.ts b/packages/taler-wallet-core/src/headless/NodeHttpLib.ts index 525ac8557..1dd207345 100644 --- a/packages/taler-wallet-core/src/headless/NodeHttpLib.ts +++ b/packages/taler-wallet-core/src/headless/NodeHttpLib.ts @@ -24,13 +24,13 @@ import { HttpRequestLibrary, HttpRequestOptions, HttpResponse, -} from "../util/http"; -import { RequestThrottler } from "../util/RequestThrottler"; +} from "../util/http.js"; +import { RequestThrottler } from "../util/RequestThrottler.js"; import Axios, { AxiosResponse } from "axios"; -import { OperationFailedError, makeErrorDetails } from "../errors"; -import { URL } from "../util/url"; +import { OperationFailedError, makeErrorDetails } from "../errors.js"; +import { URL } from "../util/url.js"; import { Logger } from "@gnu-taler/taler-util"; -import { bytesToString } from "../crypto/talerCrypto"; +import { bytesToString } from "../crypto/talerCrypto.js"; import { TalerErrorCode } from "@gnu-taler/taler-util"; const logger = new Logger("NodeHttpLib.ts"); diff --git a/packages/taler-wallet-core/src/headless/helpers.ts b/packages/taler-wallet-core/src/headless/helpers.ts index c05d60497..a0053fc0f 100644 --- a/packages/taler-wallet-core/src/headless/helpers.ts +++ b/packages/taler-wallet-core/src/headless/helpers.ts @@ -27,12 +27,12 @@ import { BridgeIDBFactory, shimIndexedDB, } from "@gnu-taler/idb-bridge"; -import { openTalerDatabase } from "../db"; -import { HttpRequestLibrary } from "../util/http"; -import { NodeThreadCryptoWorkerFactory } from "../crypto/workers/nodeThreadWorker"; -import { NodeHttpLib } from "./NodeHttpLib"; +import { openTalerDatabase } from "../db.js"; +import { HttpRequestLibrary } from "../util/http.js"; +import { NodeThreadCryptoWorkerFactory } from "../crypto/workers/nodeThreadWorker.js"; +import { NodeHttpLib } from "./NodeHttpLib.js"; import { Logger } from "@gnu-taler/taler-util"; -import { SynchronousCryptoWorkerFactory } from "../crypto/workers/synchronousWorker"; +import { SynchronousCryptoWorkerFactory } from "../crypto/workers/synchronousWorker.js"; import type { IDBFactory } from "@gnu-taler/idb-bridge"; import { WalletNotification } from "@gnu-taler/taler-util"; import { InternalWalletState } from "../common.js"; diff --git a/packages/taler-wallet-core/src/index.browser.ts b/packages/taler-wallet-core/src/index.browser.ts new file mode 100644 index 000000000..d0b82d3e0 --- /dev/null +++ b/packages/taler-wallet-core/src/index.browser.ts @@ -0,0 +1,72 @@ +/* + This file is part of TALER + (C) 2019 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 + */ + +export * from "./index.js"; + +import { setPRNG } from './crypto/primitives/nacl-fast.js'; +// export default API; + +function cleanup(arr: Uint8Array): void { + for (let i = 0; i < arr.length; i++) arr[i] = 0; +} + +// Initialize PRNG if environment provides CSPRNG. +// If not, methods calling randombytes will throw. +// @ts-ignore-error +const cr = typeof self !== "undefined" ? self.crypto || self.msCrypto : null; + +const QUOTA = 65536; +setPRNG(function (x: Uint8Array, n: number) { + let i; + const v = new Uint8Array(n); + for (i = 0; i < n; i += QUOTA) { + cr.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))); + } + for (i = 0; i < n; i++) x[i] = v[i]; + cleanup(v); +}); +// function initPRNG() { +// // Initialize PRNG if environment provides CSPRNG. +// // If not, methods calling randombytes will throw. +// // @ts-ignore-error +// const cr = typeof self !== "undefined" ? self.crypto || self.msCrypto : null; +// if (cr && cr.getRandomValues) { +// // Browsers. +// const QUOTA = 65536; +// setPRNG(function (x: Uint8Array, n: number) { +// let i; +// const v = new Uint8Array(n); +// for (i = 0; i < n; i += QUOTA) { +// cr.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA))); +// } +// for (i = 0; i < n; i++) x[i] = v[i]; +// cleanup(v); +// }); +// } else if (typeof require !== "undefined") { +// // Node.js. +// // eslint-disable-next-line @typescript-eslint/no-var-requires +// const cr = require("crypto"); +// if (cr && cr.randomBytes) { +// setPRNG(function (x: Uint8Array, n: number) { +// const v = cr.randomBytes(n); +// for (let i = 0; i < n; i++) x[i] = v[i]; +// cleanup(v); +// }); +// } +// } +// } + +// initPRNG(); \ No newline at end of file diff --git a/packages/taler-wallet-core/src/index.node.ts b/packages/taler-wallet-core/src/index.node.ts new file mode 100644 index 000000000..a3be6b5f6 --- /dev/null +++ b/packages/taler-wallet-core/src/index.node.ts @@ -0,0 +1,43 @@ +/* + This file is part of TALER + (C) 2019 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 + */ + +export * from "./index.js"; + +// Utils for using the wallet under node +export { NodeHttpLib } from "./headless/NodeHttpLib.js"; +export { + getDefaultNodeWallet, + DefaultNodeWalletArgs, +} from "./headless/helpers.js"; + +import { setPRNG } from './crypto/primitives/nacl-fast.js'; +import cr from 'crypto'; + +function cleanup(arr: Uint8Array): void { + for (let i = 0; i < arr.length; i++) arr[i] = 0; +} + +// Initialize PRNG if environment provides CSPRNG. +// If not, methods calling randombytes will throw. +if (cr && cr.randomBytes) { + setPRNG(function (x: Uint8Array, n: number) { + const v = cr.randomBytes(n); + for (let i = 0; i < n; i++) x[i] = v[i]; + cleanup(v); + }); +} + +export * from "./crypto/workers/nodeThreadWorker.js"; diff --git a/packages/taler-wallet-core/src/index.ts b/packages/taler-wallet-core/src/index.ts index 850ee3a16..822c1aa2b 100644 --- a/packages/taler-wallet-core/src/index.ts +++ b/packages/taler-wallet-core/src/index.ts @@ -27,19 +27,12 @@ export * from "./util/promiseUtils.js"; export * from "./util/query.js"; export * from "./util/http.js"; -// Utils for using the wallet under node -export { NodeHttpLib } from "./headless/NodeHttpLib.js"; -export { - getDefaultNodeWallet, - DefaultNodeWalletArgs, -} from "./headless/helpers.js"; - export * from "./versions.js"; export * from "./db.js"; // Crypto and crypto workers -export * from "./crypto/workers/nodeThreadWorker.js"; +// export * from "./crypto/workers/nodeThreadWorker.js"; export { CryptoImplementation } from "./crypto/workers/cryptoImplementation.js"; export type { CryptoWorker } from "./crypto/workers/cryptoWorker.js"; export { CryptoWorkerFactory, CryptoApi } from "./crypto/workers/cryptoApi.js"; diff --git a/packages/taler-wallet-core/src/operations/backup/export.ts b/packages/taler-wallet-core/src/operations/backup/export.ts index 42cc9b651..ab2262653 100644 --- a/packages/taler-wallet-core/src/operations/backup/export.ts +++ b/packages/taler-wallet-core/src/operations/backup/export.ts @@ -50,7 +50,7 @@ import { BackupExchangeDetails, } from "@gnu-taler/taler-util"; import { InternalWalletState } from "../../common.js"; -import { provideBackupState, getWalletBackupState } from "./state"; +import { provideBackupState, getWalletBackupState } from "./state.js"; import { Amounts, getTimestampNow } from "@gnu-taler/taler-util"; import { CoinSourceType, @@ -60,7 +60,7 @@ import { ProposalStatus, WALLET_BACKUP_STATE_KEY, } from "../../db.js"; -import { encodeCrock, stringToBytes, getRandomBytes } from "../../index.js"; +import { encodeCrock, stringToBytes, getRandomBytes } from "../../crypto/talerCrypto.js"; import { canonicalizeBaseUrl, canonicalJson } from "@gnu-taler/taler-util"; export async function exportBackup( diff --git a/packages/taler-wallet-core/src/operations/backup/import.ts b/packages/taler-wallet-core/src/operations/backup/import.ts index ce4d14f1e..b33e050b7 100644 --- a/packages/taler-wallet-core/src/operations/backup/import.ts +++ b/packages/taler-wallet-core/src/operations/backup/import.ts @@ -46,9 +46,9 @@ import { WireInfo, WalletStoresV1, } from "../../db.js"; -import { PayCoinSelection } from "../../util/coinSelection"; +import { PayCoinSelection } from "../../util/coinSelection.js"; import { j2s } from "@gnu-taler/taler-util"; -import { checkDbInvariant, checkLogicInvariant } from "../../util/invariants"; +import { checkDbInvariant, checkLogicInvariant } from "../../util/invariants.js"; import { Logger } from "@gnu-taler/taler-util"; import { initRetryInfo } from "../../util/retries.js"; import { InternalWalletState } from "../../common.js"; diff --git a/packages/taler-wallet-core/src/operations/pay.ts b/packages/taler-wallet-core/src/operations/pay.ts index 6d185cae8..2a9bf154e 100644 --- a/packages/taler-wallet-core/src/operations/pay.ts +++ b/packages/taler-wallet-core/src/operations/pay.ts @@ -53,7 +53,7 @@ import { Logger, getDurationRemaining, } from "@gnu-taler/taler-util"; -import { encodeCrock, getRandomBytes } from "../crypto/talerCrypto"; +import { encodeCrock, getRandomBytes } from "../crypto/talerCrypto.js"; import { PayCoinSelection, CoinCandidateSelection, diff --git a/packages/taler-wallet-core/src/util/RequestThrottler.ts b/packages/taler-wallet-core/src/util/RequestThrottler.ts index 66a895750..56242a237 100644 --- a/packages/taler-wallet-core/src/util/RequestThrottler.ts +++ b/packages/taler-wallet-core/src/util/RequestThrottler.ts @@ -27,7 +27,7 @@ import { timestampCmp, Logger, } from "@gnu-taler/taler-util"; -import { URL } from "./url"; +import { URL } from "./url.js"; const logger = new Logger("RequestThrottler.ts"); diff --git a/packages/taler-wallet-core/src/wallet-api-types.ts b/packages/taler-wallet-core/src/wallet-api-types.ts index d1c8914d9..75121ed38 100644 --- a/packages/taler-wallet-core/src/wallet-api-types.ts +++ b/packages/taler-wallet-core/src/wallet-api-types.ts @@ -66,7 +66,7 @@ import { WithdrawTestBalanceRequest, WithdrawUriInfoResponse, } from "@gnu-taler/taler-util"; -import { AddBackupProviderRequest, BackupInfo } from "./operations/backup"; +import { AddBackupProviderRequest, BackupInfo } from "./operations/backup/index.js"; import { PendingOperationsResponse } from "./pending-types.js"; export enum WalletApiOperation { diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index a392bf4d7..a9c4c97e8 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -43,65 +43,65 @@ import { getBackupRecovery, loadBackupRecovery, runBackupCycle, -} from "./operations/backup"; -import { exportBackup } from "./operations/backup/export"; -import { getBalances } from "./operations/balance"; +} from "./operations/backup/index.js"; +import { exportBackup } from "./operations/backup/export.js"; +import { getBalances } from "./operations/balance.js"; import { createDepositGroup, processDepositGroup, trackDepositGroup, -} from "./operations/deposits"; +} from "./operations/deposits.js"; import { makeErrorDetails, OperationFailedAndReportedError, OperationFailedError, -} from "./errors"; +} from "./errors.js"; import { acceptExchangeTermsOfService, getExchangeDetails, updateExchangeFromUrl, -} from "./operations/exchanges"; +} from "./operations/exchanges.js"; import { confirmPay, preparePayForUri, processDownloadProposal, processPurchasePay, -} from "./operations/pay"; -import { getPendingOperations } from "./operations/pending"; -import { processRecoupGroup } from "./operations/recoup"; +} from "./operations/pay.js"; +import { getPendingOperations } from "./operations/pending.js"; +import { processRecoupGroup } from "./operations/recoup.js"; import { autoRefresh, createRefreshGroup, processRefreshGroup, -} from "./operations/refresh"; +} from "./operations/refresh.js"; import { abortFailedPayWithRefund, applyRefund, processPurchaseQueryRefund, -} from "./operations/refund"; +} from "./operations/refund.js"; import { createReserve, createTalerWithdrawReserve, getFundingPaytoUris, processReserve, -} from "./operations/reserves"; -import { InternalWalletState } from "./common"; +} from "./operations/reserves.js"; +import { InternalWalletState } from "./common.js"; import { runIntegrationTest, testPay, withdrawTestBalance, -} from "./operations/testing"; -import { acceptTip, prepareTip, processTip } from "./operations/tip"; +} from "./operations/testing.js"; +import { acceptTip, prepareTip, processTip } from "./operations/tip.js"; import { deleteTransaction, getTransactions, retryTransaction, -} from "./operations/transactions"; +} from "./operations/transactions.js"; import { getExchangeWithdrawalInfo, getWithdrawalDetailsForUri, processWithdrawGroup, -} from "./operations/withdraw"; +} from "./operations/withdraw.js"; import { AuditorTrustRecord, CoinSourceType, @@ -147,7 +147,7 @@ import { RefreshReason, } from "@gnu-taler/taler-util"; import { AmountJson, Amounts } from "@gnu-taler/taler-util"; -import { assertUnreachable } from "./util/assertUnreachable"; +import { assertUnreachable } from "./util/assertUnreachable.js"; import { Logger } from "@gnu-taler/taler-util"; import { setWalletDeviceId } from "./operations/backup/state.js"; import { WalletCoreApiClient } from "./wallet-api-types.js";