httprpc.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. This file is part of ethereum.js.
  3. ethereum.js is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. ethereum.js is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file httprpc.js
  15. * @authors:
  16. * Marek Kotewicz <marek@ethdev.com>
  17. * Marian Oancea <marian@ethdev.com>
  18. * @date 2014
  19. */
  20. // TODO: work out which of the following two lines it is supposed to be...
  21. //if (process.env.NODE_ENV !== 'build') {
  22. if ("build" !== "build") {/*
  23. var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
  24. */}
  25. var HttpRpcProvider = function (host) {
  26. this.handlers = [];
  27. this.host = host;
  28. };
  29. function formatJsonRpcObject(object) {
  30. return {
  31. jsonrpc: '2.0',
  32. method: object.call,
  33. params: object.args,
  34. id: object._id
  35. };
  36. }
  37. function formatJsonRpcMessage(message) {
  38. var object = JSON.parse(message);
  39. return {
  40. _id: object.id,
  41. data: object.result,
  42. error: object.error
  43. };
  44. }
  45. HttpRpcProvider.prototype.sendRequest = function (payload, cb) {
  46. var data = formatJsonRpcObject(payload);
  47. var request = new XMLHttpRequest();
  48. request.open("POST", this.host, true);
  49. request.send(JSON.stringify(data));
  50. request.onreadystatechange = function () {
  51. if (request.readyState === 4 && cb) {
  52. cb(request);
  53. }
  54. };
  55. };
  56. HttpRpcProvider.prototype.send = function (payload) {
  57. var self = this;
  58. this.sendRequest(payload, function (request) {
  59. self.handlers.forEach(function (handler) {
  60. handler.call(self, formatJsonRpcMessage(request.responseText));
  61. });
  62. });
  63. };
  64. HttpRpcProvider.prototype.poll = function (payload, id) {
  65. var self = this;
  66. this.sendRequest(payload, function (request) {
  67. var parsed = JSON.parse(request.responseText);
  68. if (parsed.error || (parsed.result instanceof Array ? parsed.result.length === 0 : !parsed.result)) {
  69. return;
  70. }
  71. self.handlers.forEach(function (handler) {
  72. handler.call(self, {_event: payload.call, _id: id, data: parsed.result});
  73. });
  74. });
  75. };
  76. Object.defineProperty(HttpRpcProvider.prototype, "onmessage", {
  77. set: function (handler) {
  78. this.handlers.push(handler);
  79. }
  80. });
  81. module.exports = HttpRpcProvider;