New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

kitejs

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kitejs - npm Package Compare versions

Comparing version 1.0.3 to 1.0.4

src/balance/random.ts

7

package.json
{
"name": "kitejs",
"version": "1.0.3",
"version": "1.0.4",
"description": "the rpc framework Kite for Node.js",

@@ -8,4 +8,3 @@ "main": "index.js",

"test": "mocha --compilers ts:ts-node/register,tsx:ts-node/register test/*",
"build": "tsc -p ./src",
"prepublishOnly": "tsc -p ./src"
"build": "tsc -p ./src"
},

@@ -20,2 +19,4 @@ "keywords": [

"dependencies": {
"consul": "^0.29.0",
"debug": "^2.6.8",
"thrift": "^0.10.0"

@@ -22,0 +23,0 @@ },

@@ -5,5 +5,9 @@ /**

*/
import {Address, Config, Protocol} from './config';
import {Map} from './types/lang';
import {Config} from './config';
import {Address, Protocol as ProtocolInterface} from './protocol/protocol';
import {Http as HttpClient} from './protocol/http';
import {Thrift as ThriftClient} from './protocol/thrift';
import * as thrift from 'thrift';
import {random as BalanceRandom} from './balance/random';
import {resolve} from 'path';

@@ -16,37 +20,33 @@

connection: any;
protocolArr: Map<any> = {};
constructor(config: Config) {
this.config = config;
this.register('HTTP', HttpClient);
this.register('THRIFT', ThriftClient);
}
public createClient(cb) {
if (!this.service) {
return cb(new Error('invalid service or not given, please use .loadService(<path>) given it.'));
public createClient(cb: (err: any, service: any, conn?:any) => void) {
var handle = this.protocolArr[this.config.getProtocol()]
if (!handle) {
return cb(new Error(`protocol ${this.config.getProtocol()} not support`), null);
}
var that = this;
return this.config.getHosts(function (err, host) {
if (err) {
return cb(err);
}
var options: Map<any> = this.config.options;
options['service'] = this.service;
var callback = function (err, hosts) {
if (err) throw err;
let con = that.createConnection(host);
let ins = new handle(that.getAddress(hosts), options);
ins.createClient(cb);
};
cb (null, thrift.createClient(that.service, con), con);
});
return this.config.getHosts(callback);
}
public createClientSync() {
if (!this.service) {
throw new Error('invalid service or not given, please use .loadService(<path>) given it.');
}
let con = this.createConnection(this.getAddress());
let work = thrift.createClient(this.service, con);
return {
client: thrift.createClient(this.service, con),
connection: con
};
public request(cb: (err: any, service: any, conn?:any) => void) {
return this.createClient(cb);
}

@@ -62,15 +62,4 @@

protected createConnection(address: Address) {
let transport, protocol;
if (this.config.getProtocol() == Protocol.BINARY) {
transport = thrift.TBufferedTransport;
protocol = thrift.TBinaryProtocol;
} else if (0) {
}
return this.connection = thrift.createConnection(address.getHost(), address.getPort(), {
transport: transport,
protocol: protocol
});
public register(protocol: string, handle: any) {
this.protocolArr[protocol] = handle;
}

@@ -83,11 +72,8 @@

*/
private balance() {
private getAddress(hosts: Array<Address>) : Address {
let balance_ = this.config.balance || 'random';
let hosts = this.config.getHostsSync();
switch (balance_) {
case 'random':
let len = hosts.length;
return hosts[Math.floor(len * Math.random())];
return BalanceRandom(hosts);
case 'rate_with_idc':

@@ -98,13 +84,6 @@

return fn(this.config.getHostsWithIDC());
default:
return new Address();
}
}
private getAddress() {
if (this.balance) {
return this.balance();
}
// default address
return new Address();
}
}

@@ -8,30 +8,9 @@ /**

import {consul} from './consul';
import {Map} from './types/lang';
import {Address} from './protocol/protocol';
export enum Protocol {
BINARY = 1,
}
export class Address {
host: string;
port: number;
constructor(host: string = '127.0.0.1', port: number = 5099) {
this.host = host;
this.port = port;
}
public getHost() {
return this.host;
}
public getPort() {
return this.port;
}
}
/**
/**
* The Config Class
*/
export class Config {
ctx: any;

@@ -41,14 +20,10 @@ encoding: string;

/**
* protocol: (consul | local | http | https)
* protocol: (THRIFT | HTTP | HTTPS)
*/
protocol: Protocol;
protocol: string;
searchHostType: string = 'LOCAL'; // or 'CONSUL'
/**
* the connect server timeout settings.
*/
timeout: any = {};
_timeout: number;
connectTimeout: number;
readTimeout: number;
writeTimeout: number;
timeout: number = 2000;

@@ -60,8 +35,5 @@ /**

retry: number;
/**
* connect address
*/
service: string;
consul: { host: string, port: number };
/**

@@ -86,32 +58,73 @@ * {

address: string;
options: Map<any>;
constructor (public service: string | Map<any>, public idc?: string, public ctx?: any) {
if (typeof service != 'string') {
this.create(service);
} else {
this.loadFile(<string> this.service);
}
}
public getHosts(cb: (err: any, hosts: Array<Address>) => void): void {
if (this.searchHostType == 'LOCAL') {
let hosts = this.hosts[this.idc]
|| [new Address(this.host, this.port)];
cb(null, hosts);
} else if (this.searchHostType == 'CONSUL') {
this.getHostsWithConsul(cb);
} else {
throw new Error(`unsupport '${this.searchHostType}' searchHostType`);
}
}
/**
* Server Information
* git the host list with `idc`
*/
idc: string;
public getHostsWithIDC() {
return [];
}
constructor (service: any, idc?: string, ctx?: any) {
this.service = service;
this.idc = idc;
this.ctx = ctx;
public getProtocol() {
if (!this.protocol) {
this.protocol = 'HTTP';
}
if (Object.prototype.toString.call(service) != '[object String]') {
this.create(service);
} else {
this.loadFile(this.service);
return this.protocol;
}
public getConsulAddress() {
if (!this.consul) {
throw new Error('invalid consul address');
}
if (!this.consul) {
this.consul = {
host: 'consul.service.byted.org',
port: 2280
};
}
return new Address(
this.consul.host,
this.consul.port
);
}
/**
* create a instance of the Config from a Map.
*
* @param config Map<string, []>
* @param config Map<any>
*/
private create(config: any) {
if (config.timeout) {
this._timeout = config.timeout.connectTimeout
+ config.timeout.readTimeout
+ config.time.writeTimeout;
private create(config: Map<any>) {
if (config['timeout']) {
config['timeout'] = this.timeFromString(config['timeout']);
} else {
// default 2s
config['timeout'] = this.timeout;
}
this.options = config;
if (config.hosts) {

@@ -130,3 +143,3 @@ for (var idc in config.hosts) {

for (var key in config) {
if (config.hasOwnProperty(key) && ['timeout', 'hosts'].indexOf(key) == -1) {
if (config.hasOwnProperty(key) && ['hosts'].indexOf(key) == -1) {
this[key] = config[key];

@@ -136,4 +149,9 @@ }

}
loadFile(file: string) {
/**
* config information load from a config file
* @param {string} file
* @return void
*/
protected loadFile(file: string) {
let config = {};

@@ -153,18 +171,16 @@

}
public getHosts(cb) : any {
return this.getHostsWithConsul(cb);
}
public getHostsSync() {
this.getHostsWithAddress();
return this.hosts[this.idc] || [new Address(this.host, this.port)];
}
/**
* git the host list with `idc`
* the time 1s\1ms to \d+(ms)
*
* @param s string | any
*/
public getHostsWithIDC() {
return [];
private timeFromString(s: string) {
if (/([\d.]+)ms/i.test(s)) {
return parseFloat(RegExp.$1);
} else if (/([\d.]+)s/i.test(s)) {
return parseFloat(RegExp.$1) * 1000;
} else {
return parseFloat(s) * 1000;
}
}

@@ -177,3 +193,3 @@

// require consul get hosts.
return consul.get(this.service, cb);
return consul.get(this.getConsulAddress(), <string>this.service, cb);
}

@@ -192,3 +208,3 @@

private getHostsWithAddress() : boolean {
if (Object.prototype.toString.call(this.address) != '[object String]') {
if (typeof this.address != 'string') {
return false;

@@ -213,9 +229,2 @@ }

getProtocol() {
if (!this.protocol) {
this.protocol = Protocol.BINARY;
}
return this.protocol;
}
};

@@ -0,3 +1,9 @@

import {Address} from './protocol/protocol';
import * as Debug from 'debug';
import * as NConsul from 'consul';
const debug = Debug('consul');
export class Consul {
public get(service: string, callback, idc: string = '') {
public get(serAddress: Address, service: string, callback, idc: string = '') {
if (!service) {

@@ -7,6 +13,27 @@ return callback(new Error('unknown service name.'), null);

callback(service);
debug(`start_request host: ${serAddress.getHost} port: ${serAddress.getPort()}`);
const consul = NConsul({
host: serAddress.getHost(),
port: serAddress.getPort()
});
consul.catalog.service.nodes(
{
service: service
}, function (err, nodes) {
if (err) return callback(err);
var hosts: Array<Address> = [];
nodes.forEach(node => {
hosts.push(new Address(
node.Address || node.ServiceAddress,
node.ServicePort
));
});
callback(null, hosts);
}
);
}
}
export var consul = new Consul();
export var consul = new Consul();
import {expect} from 'chai';
import {Protocol, Config} from '../../src/config';
import {Config} from '../../src/config';
import {Client} from '../../src/kite';
import {Map} from '../../src/types/lang';
import {Address} from '../../src/protocol/protocol';
describe('new config', function () {
it('#1 config hosts', function () {
it('#1 config hosts', function (ok) {
let config = new Config({

@@ -19,10 +21,16 @@ 'hosts': {

let hosts = config.getHostsSync();
config.getHosts(function (hosts: Array<Address>) {
expect(hosts.length).to.be.equal(1);
var host = hosts[0];
expect(hosts[0].getHost()).to.be.equal('127.0.0.1');
expect(hosts[0].getPort()).to.be.equal(10200);
ok();
});
expect(hosts.length).to.be.equal(1);
expect(hosts[0].getHost()).to.be.equal('127.0.0.1');
expect(hosts[0].getPort()).to.be.equal(10200);
});
it('#2 config host', function () {
it('#2 config host', function (ok) {
let config = new Config({

@@ -33,7 +41,10 @@ 'host': '127.0.0.1',

let hosts = config.getHostsSync();
expect(hosts.length).to.be.equal(1);
expect(hosts[0].getHost()).to.be.equal('127.0.0.1');
expect(hosts[0].getPort()).to.be.equal(50022);
config.getHosts(function (hosts: Array<Address>) {
expect(hosts.length).to.be.equal(1);
expect(hosts[0].getHost()).to.be.equal('127.0.0.1');
expect(hosts[0].getPort()).to.be.equal(50022);
ok();
});
})

@@ -43,7 +54,7 @@ });

describe('new client', function () {
it ('#1 new client', function (ok) {
it ('#1 new client Thrift', function (ok) {
var client = new Client(new Config({
'host': '127.0.0.1',
'port': 9090,
'protocol': Protocol.BINARY
'protocol': 'THRIFT'
}));

@@ -53,10 +64,25 @@

.loadService(__dirname + '/../thrift/gen-nodejs/Calculator')
.createClientSync();
.createClient(function (err, service, conn) {
service.ping(function (err, response) {
console.log('ping()');
});
conn.client.ping(function (err, response) {
console.log('ping()');
});
service.add(1, 1, function (err, response) {
expect(response).to.be.equal(2);
ok();
});
});
conn.client.add(1, 1, function (err, response) {
expect(response).to.be.equal(2);
});
it ('#2 new client HTTP', function (ok) {
var client = new Client(new Config({
'host': '127.0.0.1',
'port': 8080,
'protocol': 'HTTP'
}));
client.createClient(function (err, data) {
console.log(data);
ok();

@@ -63,0 +89,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc