UNPKG

@yhiot/utils

Version:
101 lines (93 loc) 2.58 kB
import { getType } from './getType'; import console from 'console'; const jayson = require('jayson/promise'); export function replacer(key, value) { if (value instanceof RegExp) return '__REGEXP ' + value.toString(); else return value; } export function reviver(key, value) { if (getType(value) === 'string') { if (value.toString().indexOf('__REGEXP ') == 0) { let m = value.split('__REGEXP ')[1].match(/\/(.*)\/(.*)?/); return new RegExp(m[1], m[2] || ''); } } return value; } export default class Client { /** * 构造函数 * @param {array} ops 操作列表 * @param {json} cfg 配置{protocol:'tcp/http',host:'localhost',port} protocol默认为tcp,host默认为localhost */ constructor(ops, cfg) { this.cfg = cfg; let client = null; switch (cfg.protocol) { case 'http': client = jayson.client.http({ port: cfg.port, host: cfg.host || 'localhost', reviver, replacer, }); break; case 'tcp': default: client = jayson.client.tcp({ port: cfg.port, host: cfg.host || 'localhost', reviver, replacer, }); break; } this.client = client; for (let i = 0; i < ops.length; i++) { let opname = ops[i]; this[opname] = async function () { let ret = null; try { let argsToArray = Array.prototype.slice.apply(arguments); // console.log('jaysonClient ', { // opname, // args: argsToArray, // client: !!client, // }); ret = await client.request(opname, argsToArray); // ret = await client.request(opname, arguments) } catch (e) { console.error('jaysonClient error:', opname, arguments, e); throw { errcode: -1, message: e.message }; } let result = ret && ret.result; if (result && result.errcode) { throw result; } return result; }.bind(this); } } } // 供初始化jayson 客户端. let clients = {}; /** * 初始化微服务列表 * @param {json} cfgs {iotdevice: {ops, protocol, port, host}} */ export function init(cfgs) { let names = Object.getOwnPropertyNames(cfgs); for (let i = 0; i < names.length; i++) { let name = names[i]; let cfg = cfgs[name]; clients[name] = new Client(cfg.ops, { protocol: cfg.protocol, port: cfg.port, host: cfg.host, }); } return clients; } export function $rpc(name) { return clients[name]; }