diff --git a/packages/fether-react/package.json b/packages/fether-react/package.json index 27d1b2ffab5511c4c2d6cfbf10eb39fe50887b77..ff0f1c193de77b9cb44a3ced2ce852ab8db06482 100644 --- a/packages/fether-react/package.json +++ b/packages/fether-react/package.json @@ -35,7 +35,7 @@ }, "dependencies": { "@parity/api": "^2.1.22", - "@parity/light.js": "https://github.com/parity-js/light.js#9646ce15d9dd9c4cf11776ddd613d5bd86016f94", + "@parity/light.js": "https://github.com/parity-js/light.js#4520ff71ff46cef1f5fa8b3c7f6ec97968d", "@parity/shared": "^3.0.2", "bignumber.js": "^4.1.0", "debounce-promise": "^3.1.0", diff --git a/packages/parity-electron/README.md b/packages/parity-electron/README.md index f88909a69d72e400f6e4bcfb57ef3d2e25204d4b..9b194f7e34028891cd8bbdf5b8387fcbe5cfdf9e 100644 --- a/packages/parity-electron/README.md +++ b/packages/parity-electron/README.md @@ -16,8 +16,7 @@ import parityElectron, { isParityRunning } from '@parity/electron'; // Optional: override default options parityElectron({ cli: myOwnCliObject, - logger: myCustomLoggerFunction, - parityChannel: 'nightly' + logger: myCustomLoggerFunction }) isParityRunning() diff --git a/packages/parity-electron/babel.config.js b/packages/parity-electron/babel.config.js deleted file mode 100644 index 9fae1bf22ee619049cb6aab469aef25705e3afe6..0000000000000000000000000000000000000000 --- a/packages/parity-electron/babel.config.js +++ /dev/null @@ -1,19 +0,0 @@ -module.exports = { - plugins: [ - '@babel/plugin-proposal-class-properties', - [ - '@babel/plugin-transform-runtime', - { - helpers: false, - polyfill: false, - regenerator: true, - moduleName: '@babel/runtime' - } - ] - ], - presets: [ - '@babel/preset-env', - '@babel/preset-react', - ['@babel/preset-stage-0', { decoratorsLegacy: true }] - ] -}; diff --git a/packages/parity-electron/package.json b/packages/parity-electron/package.json index 94bdd4792d92656126cf71d5054fb2c73e441ddb..fe706c2b265cd0031f94dac69ca233937f9754c6 100644 --- a/packages/parity-electron/package.json +++ b/packages/parity-electron/package.json @@ -27,7 +27,7 @@ "main": "lib/index.js", "scripts": { "prebuild": "rimraf lib", - "build": "babel src --out-dir lib", + "build": "tsc", "start": "yarn build --watch" }, "dependencies": { @@ -40,8 +40,8 @@ "promise-any": "^0.2.0" }, "devDependencies": { - "@babel/plugin-transform-runtime": "^7.0.0-beta.51", - "electron": "^2.0.2" + "electron": "^2.0.2", + "typescript": "^2.9.2" }, "peerDependencies": { "electron": "^2.0.3" diff --git a/packages/parity-electron/src/fetchParity.js b/packages/parity-electron/src/fetchParity.ts similarity index 68% rename from packages/parity-electron/src/fetchParity.js rename to packages/parity-electron/src/fetchParity.ts index fbc3da0249c7c73f6353d575d0bea279f13716b1..c10a857532d47295a721bc3b4c0581f0ee7e9549 100644 --- a/packages/parity-electron/src/fetchParity.js +++ b/packages/parity-electron/src/fetchParity.ts @@ -3,21 +3,21 @@ // // SPDX-License-Identifier: BSD-3-Clause -import { app } from 'electron'; +import { app, BrowserWindow } from 'electron'; import axios from 'axios'; -import cs from 'checksum'; +import { file } from 'checksum'; import { download } from 'electron-dl'; -import fs from 'fs'; +import { chmod, stat, unlink } from 'fs'; import { promisify } from 'util'; -import retry from 'async-retry'; +import * as retry from 'async-retry'; import { defaultParityPath, getParityPath } from './getParityPath'; import logger from './utils/logger'; -const checksum = promisify(cs.file); -const fsChmod = promisify(fs.chmod); -const fsStat = promisify(fs.stat); -const fsUnlink = promisify(fs.unlink); +const checksum = promisify(file); +const fsChmod = promisify(chmod); +const fsStat = promisify(stat); +const fsUnlink = promisify(unlink); const VANITY_URL = 'https://vanity-service.parity.io/parity-binaries'; @@ -53,7 +53,7 @@ const getOs = () => { }; /** - * Remove parity binary in the userData folder + * Remove parity binary in the userData folder, if it exists. */ export const deleteParity = async () => { try { @@ -63,16 +63,23 @@ export const deleteParity = async () => { } catch (e) {} }; -// Fetch parity from https://vanity-service.parity.io/parity-binaries -export const fetchParity = ( - mainWindow, - { onProgress, parityChannel } = { +/** + * Downloads Parity, saves it to Electron's `userData` folder, and returns the + * path to the downloaded binary once finished. + */ +export const fetchParity = async ( + mainWindow: BrowserWindow, + { + onProgress, + parityChannel + }: { onProgress: (progress: number) => void; parityChannel: string } = { + onProgress: () => {}, parityChannel: 'beta' } ) => { try { - return retry( - async (_, attempt) => { + const parityPath: string = retry( + async (_, attempt: number) => { if (attempt > 1) { logger()('@parity/electron:main')('Retrying.'); } @@ -86,7 +93,10 @@ export const fetchParity = ( const { data } = await axios.get(metadataUrl); // Get the binary's url - const { downloadUrl, checksum: expectedChecksum } = data[0].files.find( + const { + downloadUrl, + checksum: expectedChecksum + }: { downloadUrl: string; checksum: string } = data[0].files.find( ({ name }) => name === 'parity' || name === 'parity.exe' ); @@ -95,11 +105,11 @@ export const fetchParity = ( directory: app.getPath('userData'), onProgress }); - const downloadPath = downloadItem.getSavePath(); // Equal to defaultParityPath + const downloadPath: string = downloadItem.getSavePath(); // Equal to defaultParityPath // Once downloaded, we check the sha256 checksum // Calculate the actual checksum - const actualChecksum = await checksum(downloadPath, { + const actualChecksum: string = await checksum(downloadPath, { algorithm: 'sha256' }); // The 2 checksums should of course match @@ -113,16 +123,16 @@ export const fetchParity = ( await fsChmod(downloadPath, '755'); // Double-check that Parity exists now. - const parityPath = await getParityPath(); - return parityPath; + return await getParityPath(); }, { retries: 3 } ); + + return parityPath; } catch (err) { - return deleteParity().then(() => { - Promise.reject(err); - }); + await deleteParity(); + throw err; } }; diff --git a/packages/parity-electron/src/getParityPath.js b/packages/parity-electron/src/getParityPath.ts similarity index 83% rename from packages/parity-electron/src/getParityPath.js rename to packages/parity-electron/src/getParityPath.ts index 82be6a3ddcab88f29ac2d8d5a0106dfd761485c2..851d436ad725386a81dbefba062e4a19b2d2fbf0 100644 --- a/packages/parity-electron/src/getParityPath.js +++ b/packages/parity-electron/src/getParityPath.ts @@ -5,16 +5,20 @@ import { app } from 'electron'; import commandExists from 'command-exists'; -import fs from 'fs'; +import { stat } from 'fs'; import promiseAny from 'promise-any'; import { promisify } from 'util'; import logger from './utils/logger'; -const fsStat = promisify(fs.stat); +const fsStat = promisify(stat); -// The default path to install parity, in case there's no other instance found -// on the machine. +/** + * The default path to install parity, in case there's no other instance found + * on the machine. + * + * @ignore + */ export const defaultParityPath = () => Promise.resolve( `${app.getPath('userData')}/parity${ @@ -22,10 +26,12 @@ export const defaultParityPath = () => }` ); -let parityPath; // The real parity path, will be populated after doesParityExist Promise resolves +let parityPath: string; // The real parity path, will be populated after doesParityExist Promise resolves /** * Test if `parity` command is in $PATH. + * + * @ignore */ const isParityInPath = async () => { const parityCommandExists = await commandExists('parity'); @@ -37,8 +43,10 @@ const isParityInPath = async () => { /** * Test if Parity is in the common OS locations. + * + * @ignore */ -const isParityInOs = async () => { +const isParityInOs = async (): Promise => { // OS locations to test if parity binary exists const locations = { linux: ['/bin/parity', '/usr/bin/parity', '/usr/local/bin/parity'], @@ -54,6 +62,8 @@ const isParityInOs = async () => { /** * Test is Parity is already downloaded in electron app's userData folder. + * + * @ignore */ const isParityInUserData = async () => { const parityPath = await defaultParityPath(); @@ -68,7 +78,8 @@ const isParityInUserData = async () => { * - finally check parity-ui's own userData folder * This function should run in node env. * - * @return Promise - Resolves to a string which is the command to run parity. + * @ignore + * @return Promise - Resolves to a string which is the command to run parity. */ const doesParityExist = async () => { try { @@ -89,6 +100,9 @@ const doesParityExist = async () => { } }; +/** + * Returns the path to Parity, or throws if parity is not found. + */ export const getParityPath = async () => { if (parityPath) { return parityPath; diff --git a/packages/parity-electron/src/index.js b/packages/parity-electron/src/index.ts similarity index 72% rename from packages/parity-electron/src/index.js rename to packages/parity-electron/src/index.ts index bd384f3e6d00200d5a704955369bb7310e659039..3c8c1863299df5a92ad2ea79b38e354df1b8b1a8 100644 --- a/packages/parity-electron/src/index.js +++ b/packages/parity-electron/src/index.ts @@ -3,6 +3,7 @@ // // SPDX-License-Identifier: BSD-3-Clause +import { CliObject, LoggerFunction } from './types'; import { setCli } from './utils/cli'; import { setLogger } from './utils/logger'; @@ -12,8 +13,10 @@ export * from './isParityRunning'; export * from './runParity'; export * from './signerNewToken'; -// Set default options for @parity/electron -export default opts => { +/** + * Set default options for @parity/electron. + */ +export default (opts: { cli: CliObject; logger: LoggerFunction }) => { if (opts.cli) { setCli(opts.cli); } diff --git a/packages/parity-electron/src/isParityRunning.js b/packages/parity-electron/src/isParityRunning.ts similarity index 82% rename from packages/parity-electron/src/isParityRunning.js rename to packages/parity-electron/src/isParityRunning.ts index 9deb4cdb56e60acc2234b6e1e7120cd6ce8b6947..73d40c35708af1a5205ee9babf462f8cdf7ad10b 100644 --- a/packages/parity-electron/src/isParityRunning.js +++ b/packages/parity-electron/src/isParityRunning.ts @@ -4,12 +4,14 @@ // SPDX-License-Identifier: BSD-3-Clause import axios from 'axios'; -import retry from 'async-retry'; +import * as retry from 'async-retry'; import { cli } from './utils/cli'; import logger from './utils/logger'; -// Try to ping these hosts +/** + * Try to ping these hosts to test if Parity is running. + */ const hostsToPing = ['http://127.0.0.1:8545', 'http://127.0.0.1:8546']; if (cli.wsInterface || cli.wsPort) { // Also try custom host/port if a --ws-interface or --ws-port flag is passed @@ -20,15 +22,13 @@ if (cli.wsInterface || cli.wsPort) { /** * Detect if another instance of parity is already running or not. To achieve - * that, we just ping on the common hosts, see hostsToPing array. - * - * @return [Promise] - Promise that resolves to true or false. + * that, we just ping on the common hosts, see {@link hostsToPing} array. */ export const isParityRunning = async () => { try { // Retry to ping as many times as there are hosts in `hostsToPing` await retry( - async (_, attempt) => { + async (_, attempt: number) => { const host = hostsToPing[attempt - 1]; // Attempt starts with 1 await axios.get(host); logger()('@parity/electron:main')( diff --git a/packages/parity-electron/src/runParity.js b/packages/parity-electron/src/runParity.ts similarity index 89% rename from packages/parity-electron/src/runParity.js rename to packages/parity-electron/src/runParity.ts index 0e328cc1e0a52feb5f747637ab640e3ab1231d33..76745fb00f07cb750d2ec8cc52aa1b3272a80063 100644 --- a/packages/parity-electron/src/runParity.js +++ b/packages/parity-electron/src/runParity.ts @@ -4,7 +4,7 @@ // SPDX-License-Identifier: BSD-3-Clause import { app } from 'electron'; -import fs from 'fs'; +import { chmod } from 'fs'; import { spawn } from 'child_process'; import { promisify } from 'util'; @@ -14,7 +14,7 @@ import { isParityRunning } from './isParityRunning'; import logCommand from './utils/logCommand'; import logger from './utils/logger'; -const fsChmod = promisify(fs.chmod); +const fsChmod = promisify(chmod); let parity = null; // Will hold the running parity instance @@ -26,7 +26,15 @@ const catchableErrors = [ 'IO error: While lock file:' ]; -export const runParity = async (additionalFlags, onParityError) => { +/** + * Spawns a child process to run Parity. If some cli flags are passed into the + * options in parityElectron, then those flags will be passed down to Parity + * itself. + */ +export const runParity = async ( + additionalFlags: string[], + onParityError: (error: Error) => void = () => {} +) => { // Do not run parity with --no-run-parity if (cli.runParity === false) { return; diff --git a/packages/parity-electron/src/signerNewToken.js b/packages/parity-electron/src/signerNewToken.ts similarity index 96% rename from packages/parity-electron/src/signerNewToken.js rename to packages/parity-electron/src/signerNewToken.ts index fdbc4a0ec6200f316600e333fdd4f9f537f613be..6e404d0e578d452576c9ce0f66947c0995e2c16f 100644 --- a/packages/parity-electron/src/signerNewToken.js +++ b/packages/parity-electron/src/signerNewToken.ts @@ -12,7 +12,7 @@ import logger from './utils/logger'; /** * Launch a parity instance to get a secure token. */ -export const signerNewToken = () => +export const signerNewToken = (): Promise => new Promise(async (resolve, reject) => { logger()('@parity/electron:main')('Requesting new token.'); diff --git a/packages/parity-electron/src/types.d.ts b/packages/parity-electron/src/types.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..4efe9a064017c21db44e91a63a03b9b839eb65dc --- /dev/null +++ b/packages/parity-electron/src/types.d.ts @@ -0,0 +1,16 @@ +// Copyright 2015-2018 Parity Technologies (UK) Ltd. +// This file is part of Parity. +// +// SPDX-License-Identifier: BSD-3-Clause + +export type CliObject = { + rawArgs?: string[]; + [key: string]: string | boolean | string[]; +}; + +export type LoggerFunction = (namespace: string) => (log: string) => void; + +export type Options = { + cli: CliObject; + logger: LoggerFunction; +}; diff --git a/packages/parity-electron/src/utils/cli.js b/packages/parity-electron/src/utils/cli.ts similarity index 91% rename from packages/parity-electron/src/utils/cli.js rename to packages/parity-electron/src/utils/cli.ts index f2ca25d620d9154ca394881da5642b580919c566..1f61f8f2573702b99c4b65473574b118c10a6115 100644 --- a/packages/parity-electron/src/utils/cli.js +++ b/packages/parity-electron/src/utils/cli.ts @@ -3,25 +3,25 @@ // // SPDX-License-Identifier: BSD-3-Clause -let cli = {}; +import { CliObject } from '../types'; + +let cli: CliObject = {}; /** * Set custom cli options. * * @param {*} cliOptions */ -export const setCli = cliObject => { +export const setCli = (cliObject: CliObject) => { cli = cliObject; }; /** * Camel-case the given `flag` * - * @param {String} flag - * @return {String} * @see https://github.com/tj/commander.js/blob/dcddf698c5463795401ad3d6382f5ec5ec060478/index.js#L1160-L1172 */ -const camelcase = flag => +const camelcase = (flag: string) => flag .split('-') .reduce((str, word) => str + word[0].toUpperCase() + word.slice(1)); diff --git a/packages/parity-electron/src/utils/logCommand.js b/packages/parity-electron/src/utils/logCommand.ts similarity index 71% rename from packages/parity-electron/src/utils/logCommand.js rename to packages/parity-electron/src/utils/logCommand.ts index 7f9eaa5e31ce4e50ffd81d4ee08afeb5727b4c02..f3a7ac122ab8a3e2d56b5e0e7b878f37d2db458c 100644 --- a/packages/parity-electron/src/utils/logCommand.js +++ b/packages/parity-electron/src/utils/logCommand.ts @@ -6,11 +6,8 @@ /** * Given a command and its args, returns a nice string to be logged. The * arguments to this function are the same as the ones you would pass to spawn. - * - * @param {String} command - The command to be run. - * @param {String} args - The args of the above command. */ -const logCommand = (command, args) => +const logCommand = (command: string, args: string[]) => `Running "${command.replace(' ', '\\ ')} ${args.join(' ')}".`; export default logCommand; diff --git a/packages/parity-electron/src/utils/logger.js b/packages/parity-electron/src/utils/logger.ts similarity index 60% rename from packages/parity-electron/src/utils/logger.js rename to packages/parity-electron/src/utils/logger.ts index acdb56669adc9857c2a4831781892202edf8e150..2f9d48bd17676b508fa552e6951bf2f425843677 100644 --- a/packages/parity-electron/src/utils/logger.js +++ b/packages/parity-electron/src/utils/logger.ts @@ -5,9 +5,11 @@ import debug from 'debug'; -let logger = debug; +import { LoggerFunction } from '../types'; -export const setLogger = _logger => { +let logger: LoggerFunction = debug; + +export const setLogger = (_logger: LoggerFunction) => { logger = _logger; }; diff --git a/packages/parity-electron/tsconfig.json b/packages/parity-electron/tsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..c95edce2c9861dd16501da1eaf6e43b142152b4f --- /dev/null +++ b/packages/parity-electron/tsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "outDir": "./lib", + "lib": ["es2015", "dom"], + "target": "es5" + }, + "include": ["./src/**/*"] +} diff --git a/yarn.lock b/yarn.lock index 20e3797cc0c252ec560d40394b4f99d7cb735177..67801d85f4871e462ea0acd9167f8a42374faa12 100644 --- a/yarn.lock +++ b/yarn.lock @@ -706,13 +706,6 @@ dependencies: regenerator-transform "^0.12.4" -"@babel/plugin-transform-runtime@^7.0.0-beta.51": - version "7.0.0-beta.51" - resolved "https://registry.yarnpkg.com/@babel/plugin-transform-runtime/-/plugin-transform-runtime-7.0.0-beta.51.tgz#0c9cab174f4e3e131659fd65c5ce8e3d73376820" - dependencies: - "@babel/helper-module-imports" "7.0.0-beta.51" - "@babel/helper-plugin-utils" "7.0.0-beta.51" - "@babel/plugin-transform-shorthand-properties@7.0.0-beta.51": version "7.0.0-beta.51" resolved "https://registry.yarnpkg.com/@babel/plugin-transform-shorthand-properties/-/plugin-transform-shorthand-properties-7.0.0-beta.51.tgz#ddbc0b1ae1ddb3bcfe6969f2c968103f11e32bd9" @@ -984,11 +977,10 @@ u2f-api "0.0.9" u2f-api-polyfill "0.4.3" -"@parity/light.js@https://github.com/parity-js/light.js#9646ce15d9dd9c4cf11776ddd613d5bd86016f94": +"@parity/light.js@https://github.com/parity-js/light.js#4520ff71ff46cef1f5fa8b3c7f6ec97968d": version "1.0.0" - resolved "https://github.com/parity-js/light.js#9646ce15d9dd9c4cf11776ddd613d5bd86016f94" + resolved "https://github.com/parity-js/light.js#4520ff71ff46cef1f5fa8b3c7f6ec97968dd1fd2" dependencies: - "@babel/runtime" "^7.0.0-beta.49" "@parity/api" "^2.1.23" json-prune "^1.1.0" memoizee "^0.4.12" @@ -11483,6 +11475,10 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" +typescript@^2.9.2: + version "2.9.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c" + u2f-api-polyfill@0.4.3: version "0.4.3" resolved "https://registry.yarnpkg.com/u2f-api-polyfill/-/u2f-api-polyfill-0.4.3.tgz#b7ad165a6f962558517a867c5c4bf9399fcf7e98"