web3Http.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const Q = require('bluebird');
  2. const EventEmitter = require('events').EventEmitter;
  3. const got = require('got');
  4. const SocketBase = require('./base');
  5. const STATE = SocketBase.STATE;
  6. const Web3SocketBase = require('./web3Base');
  7. class HttpSocket extends EventEmitter {
  8. constructor(_parentSocket) {
  9. super();
  10. this._log = _parentSocket._log.create('HttpSocket');
  11. }
  12. connect(connectConfig) {
  13. this._log.trace('Connect', connectConfig);
  14. this._hostPort = connectConfig.hostPort;
  15. const payload = JSON.stringify({
  16. jsonrpc: '2.0',
  17. id: 0,
  18. method: 'eth_accounts',
  19. params: []
  20. });
  21. this._call(payload)
  22. .then(() => {
  23. this._log.trace('Connection successful');
  24. this.emit('connect');
  25. })
  26. .catch(err => {
  27. this._log.trace('Connection failed', err);
  28. this.emit.bind(this, new Error('Unable to connect to HTTP RPC'));
  29. });
  30. }
  31. destroy() {
  32. this._log.trace('Destroy');
  33. this._hostPort = null;
  34. this.emit('close');
  35. }
  36. write(data) {
  37. this._log.trace('Write data', data);
  38. this._call(data)
  39. .then(body => {
  40. this._log.trace('Got response', body);
  41. this.emit('data', body);
  42. })
  43. .catch(this.emit.bind(this, 'error'));
  44. }
  45. setEncoding(enc) {
  46. this._log.trace('Set encoding', enc);
  47. this._encoding = enc;
  48. }
  49. _call(dataStr) {
  50. return got
  51. .post(this._hostPort, {
  52. encoding: this._encoding,
  53. headers: {
  54. 'Content-Type': 'application/json'
  55. },
  56. body: dataStr
  57. })
  58. .then(res => {
  59. return res.body;
  60. });
  61. }
  62. }
  63. module.exports = class Web3HttpSocket extends Web3SocketBase {
  64. /**
  65. * Reset socket.
  66. */
  67. _resetSocket() {
  68. this._log.debug('Resetting socket');
  69. return Q.try(() => {
  70. if (STATE.CONNECTED === this._state) {
  71. this._log.debug('Disconnecting prior to reset');
  72. return this.disconnect();
  73. }
  74. }).then(() => {
  75. this._socket = new HttpSocket(this);
  76. this._socket.setEncoding('utf8');
  77. this._socket.on('close', hadError => {
  78. // if we did the disconnection then all good
  79. if (STATE.DISCONNECTING === this._state) {
  80. return;
  81. }
  82. this.emit('close', hadError);
  83. });
  84. this._socket.on('data', data => {
  85. this._log.trace('Got data');
  86. this.emit('data', data);
  87. });
  88. this._socket.on('error', err => {
  89. // connection errors will be handled in connect() code
  90. if (STATE.CONNECTING === this._state) {
  91. return;
  92. }
  93. this._log.error(err);
  94. this.emit('error', err);
  95. });
  96. });
  97. }
  98. };