UNPKG

bxgateway

Version:

Package for connecting to Bloxroute's gateways

52 lines (51 loc) 1.85 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LightGateway = void 0; const ws_1 = __importDefault(require("ws")); const bxgatewayBase_1 = __importDefault(require("./bxgatewayBase")); const debug_1 = require("debug"); const debug = (0, debug_1.debug)('bxgateway-go'); class LightGateway extends bxgatewayBase_1.default { constructor(url, authKey) { super(debug); this._gw = new ws_1.default(url, { headers: { 'Authorization': authKey }, rejectUnauthorized: false }); // Pass on this._gw.on('open', () => this.emit('open')); this._gw.on('close', () => this.emit('close')); this._gw.on('error', (err) => this.emit('error', err)); // Modify default messages to be more useful this._gw.on('message', (msg) => { debug.extend('message')(msg.trim()); const data = JSON.parse(msg); if (data.params) this.emit('message', data.params.result); if (data.result) this.emit('message', data.result); }); } subscribe(topic, options) { if (!this._gw.OPEN) throw new Error('Websocket connection to gateway closed'); if (topic === 'newBlocks') throw new Error('newBlocks subscription not implemented in bxgateway-go'); let req = { id: 1, method: "subscribe", params: [ topic, options ] }; debug.extend('subscribe')(JSON.stringify(req)); this._gw.send(JSON.stringify(req)); } } exports.LightGateway = LightGateway;