swarmNode.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const EventEmitter = require('events').EventEmitter;
  2. const Q = require('bluebird');
  3. const path = require('path');
  4. const fs = require('fs');
  5. const os = require('os');
  6. const clientBinaries = require('./../clientBinaries.json');
  7. import Settings from './settings';
  8. import Swarm from 'swarm-js';
  9. let instance = null;
  10. class SwarmNode extends EventEmitter {
  11. constructor() {
  12. super();
  13. if (!instance) {
  14. instance = this;
  15. }
  16. return instance;
  17. }
  18. getKeyPath() {
  19. // Gets Swarm Key path
  20. const swarmKeyDir = path.join(Settings.userDataPath, 'swarmjs');
  21. const swarmKeyPath = path.join(swarmKeyDir, 'swarmKey');
  22. // Generates the key if not there
  23. if (!fs.existsSync(swarmKeyDir)) {
  24. fs.mkdirSync(swarmKeyDir);
  25. }
  26. if (!fs.existsSync(swarmKeyPath)) {
  27. fs.writeFileSync(swarmKeyPath, web3.utils.randomHex(32));
  28. }
  29. return swarmKeyPath;
  30. }
  31. startUsingLocalNode() {
  32. const totalSize = 7406937; // TODO: hardcoded & innacurate, use archives.json instead
  33. let totalDownloaded = 0;
  34. const swarmBinDir = path.join(Settings.userDataPath, 'swarmjs', 'bin');
  35. const swarmBinExt = os.platform() === 'win32' ? '.exe' : '';
  36. const swarmBinPath = path.join(swarmBinDir, `swarm${swarmBinExt}`);
  37. const config = {
  38. privateKey: this.getKeyPath(),
  39. dataDir: path.join(Settings.userDataPath, 'swarmjs'),
  40. ensApi: Settings.rpcIpcPath,
  41. binPath: swarmBinPath,
  42. onProgress: size =>
  43. this.emit('downloadProgress', (totalDownloaded += size) / totalSize),
  44. archives: clientBinaries.swarm.archives
  45. };
  46. return new Q((resolve, reject) => {
  47. Swarm.local(config)(
  48. swarm =>
  49. new Q(stop => {
  50. this.emit('started', true);
  51. this._stop = stop;
  52. this._swarm = swarm;
  53. resolve(this);
  54. })
  55. ).catch(reject);
  56. });
  57. }
  58. startUsingGateway() {
  59. return new Q((resolve, reject) => {
  60. this.emit('started', false);
  61. this._swarm = Swarm.at(Settings.swarmURL);
  62. this._stop = () => {};
  63. resolve(this);
  64. });
  65. }
  66. init() {
  67. this.emit('starting');
  68. if (Settings.swarmURL === 'http://localhost:8500') {
  69. return this.startUsingLocalNode();
  70. }
  71. return this.startUsingGateway();
  72. }
  73. stop() {
  74. if (!this._swarm) {
  75. return Q.reject(new Error('Swarm not initialized, unable to stop.'));
  76. }
  77. this.emit('stopping');
  78. this._stop();
  79. this.emit('stopped');
  80. }
  81. upload(arg) {
  82. if (!this._swarm) {
  83. return Q.reject(new Error('Swarm not initialized, unable to upload.'));
  84. }
  85. return this._swarm.upload(arg);
  86. }
  87. }
  88. module.exports = new SwarmNode();