UNPKG

@akala/core

Version:
153 lines 6.43 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsonrpc = __importStar(require("@akala/json-rpc-ws")); const each_1 = require("../each"); const helpers_1 = require("../helpers"); const debug_1 = __importDefault(require("debug")); var log = debug_1.default('akala:metadata'); var clientLog = debug_1.default('akala:metadata:client'); class JsonRpcWs { constructor(api) { this.api = api; } createServerProxy(client) { var proxy = {}; each_1.each(this.api.serverOneWayConfig, function (config, serverKey) { if (config['jsonrpcws'] !== false) proxy[serverKey] = function (params) { log('calling ' + serverKey + ' with %o', params); return new Promise((resolve, reject) => { client.send(serverKey, params, function (error) { if (error) reject(error); else resolve(); }); }); }; }); each_1.each(this.api.serverTwoWayConfig, function (config, serverKey) { if (config['jsonrpcws'] !== false) proxy[serverKey] = function (params) { log('calling ' + serverKey + ' with %o', params); return new Promise((resolve, reject) => { client.send(serverKey, params, function (error, result) { if (error) reject(error); else resolve(result); }); }); }; }); return proxy; } createClientProxy(client) { var proxy = {}; each_1.each(this.api.clientOneWayConfig, function (config, clientKey) { if (config['jsonrpcws'] !== false) proxy[clientKey] = function (params) { log('calling ' + clientKey + ' with %o', params); return new Promise((resolve, reject) => { client.sendMethod(clientKey, params, function (error, result) { if (error) reject(error); else resolve(result); }); }); }; }); each_1.each(this.api.clientTwoWayConfig, function (config, clientKey) { if (config['jsonrpcws'] !== false) proxy[clientKey] = function (params) { log('calling ' + clientKey + ' with %o', params); return new Promise((resolve, reject) => { client.sendMethod(clientKey, params, function (error, result) { if (error) reject(error); else resolve(result); }); }); }; }); return proxy; } createClientFromAbsoluteUrl(url, clientImpl, ...rest) { var client = jsonrpc.ws.createClient(); var result = this.createClient(client, clientImpl); return new Promise((resolve, reject) => { client.connect(url, function () { resolve(result); }); }); } createLateBoundClient(clientImpl) { var client = jsonrpc.ws.createClient(); return helpers_1.extend({ $connect: function (url, connected) { client.connect(url, connected); } }, this.createClient(client, clientImpl)); } createClient(client, clientImpl, ...dummy) { dummy.unshift(clientImpl); dummy.push({ $proxy: () => { return this.createServerProxy(client); } }); var clientImplWithProxy = helpers_1.extend.apply(this, dummy); each_1.each(this.api.clientOneWayConfig, function (config, clientKey) { if (config['jsonrpcws'] !== false) client.expose(clientKey, function (params, reply) { try { Promise.resolve(clientImplWithProxy[clientKey](params)).then(function (result) { reply(null, result); }, function (e) { if (e instanceof Error) reply({ message: e.message, stack: e.stack, argv: process.argv }); else reply(e); }); } catch (e) { reply({ message: e.message, stack: e.stack, argv: process.argv }); } }); }); each_1.each(this.api.clientTwoWayConfig, function (config, clientKey) { if (config['jsonrpcws'] !== false) client.expose(clientKey, function (params, reply) { try { Promise.resolve(clientImplWithProxy[clientKey](params)).then(function (result) { reply(null, result); }, function (e) { if (e instanceof Error) reply({ message: e.message, stack: e.stack, argv: process.argv }); else reply(e); }); } catch (e) { reply({ message: e.message, stack: e.stack, argv: process.argv }); } }); }); return clientImplWithProxy; } } exports.JsonRpcWs = JsonRpcWs; helpers_1.module('$api').register('jsonrpcws', JsonRpcWs); //# sourceMappingURL=json-rpc-ws.js.map