Comparing version 1.0.8 to 1.0.9
"use strict"; | ||
/** | ||
* @author xiangshouding | ||
* @date 2017-07-06 14:34:17 | ||
*/ | ||
exports.__esModule = true; | ||
var debug_ = require("debug"); | ||
var path_1 = require("path"); | ||
var config_1 = require("./config"); | ||
@@ -8,3 +14,4 @@ var protocol_1 = require("./protocol/protocol"); | ||
var random_1 = require("./balance/random"); | ||
var path_1 = require("path"); | ||
var logger_1 = require("./logger"); | ||
var debug = debug_('client'); | ||
var Client = (function () { | ||
@@ -16,2 +23,6 @@ function Client(options, idc, ctx) { | ||
this.register('THRIFT', thrift_1.Thrift); | ||
this.logger = new logger_1.Logger({ | ||
logFile: this.config.getLogFile(), | ||
level: this.config.getLogLevel() | ||
}); | ||
return this; | ||
@@ -33,3 +44,3 @@ } | ||
} | ||
var that = this; | ||
var __this = this; | ||
// merge options and this.config.options | ||
@@ -43,6 +54,35 @@ for (var key in this.config.options) { | ||
var callback = function (err, hosts) { | ||
if (err) | ||
throw err; | ||
var ins = new handle(that.getAddress(hosts), options); | ||
ins.request(cb); | ||
if (err) { | ||
__this.logger.fatal(__this.logToString({ | ||
error: err.message | ||
})); | ||
return cb(err, null); | ||
} | ||
var host = __this.getAddress(hosts); | ||
var remoteAddr = host.getHost() + ":" + host.getPort(); | ||
__this.logger.notice(__this.logToString({ | ||
command: 'REQUEST_START', | ||
remote_addr: remoteAddr | ||
})); | ||
var client = new handle(host, options); | ||
client.request(function (err, service, conn) { | ||
if (err) { | ||
__this.logger.fatal(__this.logToString({ | ||
command: 'REQUEST_END', | ||
remote_addr: remoteAddr, | ||
error: err.message | ||
})); | ||
} | ||
else { | ||
__this.logger.notice(__this.logToString({ | ||
command: 'REQUEST_END', | ||
remote_addr: remoteAddr, | ||
response: (typeof service === 'string') | ||
? service : "service connected.", | ||
response_size: (typeof service === 'string') | ||
? service.length : -1 | ||
})); | ||
} | ||
cb(err, service, conn); | ||
}); | ||
}; | ||
@@ -71,4 +111,3 @@ return this.config.getHosts(callback); | ||
case 'rate_with_idc': | ||
var fn = require('./balance/rateWithIDC'); | ||
return fn(this.config.getHostsWithIDC()); | ||
return require('./balance/rateWithIDC')(this.config.getHostsWithIDC()); | ||
default: | ||
@@ -78,4 +117,13 @@ return new protocol_1.Address(); | ||
}; | ||
Client.prototype.logToString = function (obj) { | ||
var log = ''; | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
log += key + "=[" + obj[key] + "] "; | ||
} | ||
} | ||
return log; | ||
}; | ||
return Client; | ||
}()); | ||
exports.Client = Client; |
@@ -49,4 +49,10 @@ "use strict"; | ||
if (this.searchHostType == 'LOCAL') { | ||
var hosts = this.hosts[this.idc] | ||
|| [new protocol_1.Address(this.host, this.port)]; | ||
var hosts = this.hosts[this.idc]; | ||
//if idc hosts not exist, return `hosts` or `[{host:, port:}]` | ||
if (!hosts && Array.isArray(this.hosts)) { | ||
hosts = this.hosts; | ||
} | ||
else { | ||
hosts = [new protocol_1.Address(this.host, this.port)]; | ||
} | ||
cb(null, hosts); | ||
@@ -58,3 +64,3 @@ } | ||
else { | ||
throw new Error("unsupport '" + this.searchHostType + "' searchHostType"); | ||
cb(new Error("unsupport '" + this.searchHostType + "' searchHostType"), []); | ||
} | ||
@@ -86,3 +92,28 @@ }; | ||
}; | ||
Config.prototype.getLogFile = function () { | ||
return this.log.logFile; | ||
}; | ||
Config.prototype.getLogLevel = function () { | ||
return this.log.logLevel; | ||
}; | ||
/** | ||
* config information load from a config file | ||
* @param {string} file | ||
* @return void | ||
*/ | ||
Config.prototype.loadFile = function (file) { | ||
var config = {}; | ||
/** | ||
* if set ctx, read from ctx. | ||
* it's config is preload to context. | ||
*/ | ||
if (this.ctx && this.ctx[file]) { | ||
config = this.ctx[file]; | ||
} | ||
else { | ||
config = require(file); | ||
} | ||
this.create(config); | ||
}; | ||
/** | ||
* create a instance of the Config from a Map. | ||
@@ -118,21 +149,2 @@ * | ||
/** | ||
* config information load from a config file | ||
* @param {string} file | ||
* @return void | ||
*/ | ||
Config.prototype.loadFile = function (file) { | ||
var config = {}; | ||
/** | ||
* if set ctx, read from ctx. | ||
* it's config is preload to context. | ||
*/ | ||
if (this.ctx && this.ctx[file]) { | ||
config = this.ctx[file]; | ||
} | ||
else { | ||
config = require(file); | ||
} | ||
this.create(config); | ||
}; | ||
/** | ||
* the time 1s\1ms to \d+(ms) | ||
@@ -139,0 +151,0 @@ * |
{ | ||
"name": "kitejs", | ||
"version": "1.0.8", | ||
"version": "1.0.9", | ||
"description": "the rpc framework Kite for Node.js", | ||
@@ -19,4 +19,6 @@ "main": "index.js", | ||
"dependencies": { | ||
"caller": "^1.0.1", | ||
"consul": "^0.29.0", | ||
"debug": "^2.6.8", | ||
"moment": "^2.18.1", | ||
"thrift": "^0.10.0" | ||
@@ -23,0 +25,0 @@ }, |
@@ -5,2 +5,6 @@ /** | ||
*/ | ||
import * as thrift from 'thrift'; | ||
import * as debug_ from 'debug'; | ||
import {resolve} from 'path'; | ||
import {Map} from './types/lang'; | ||
@@ -11,6 +15,7 @@ import {Config} from './config'; | ||
import {Thrift as ThriftClient} from './protocol/thrift'; | ||
import * as thrift from 'thrift'; | ||
import {random as BalanceRandom} from './balance/random'; | ||
import {resolve} from 'path'; | ||
import {Logger} from './logger'; | ||
const debug = debug_('client'); | ||
export class Client { | ||
@@ -22,2 +27,3 @@ transport: any; | ||
provider: Map<any> = {}; | ||
logger: any; | ||
@@ -29,2 +35,7 @@ constructor(options: Map<any>, idc?: string, ctx?: any) { | ||
this.logger = new Logger({ | ||
logFile: this.config.getLogFile(), | ||
level: this.config.getLogLevel() | ||
}); | ||
return this; | ||
@@ -49,3 +60,3 @@ } | ||
var that = this; | ||
var __this = this; | ||
@@ -62,6 +73,40 @@ // merge options and this.config.options | ||
var callback = function (err, hosts) { | ||
if (err) throw err; | ||
if (err) { | ||
__this.logger.fatal(__this.logToString({ | ||
error: err.message, | ||
})); | ||
let ins = new handle(that.getAddress(hosts), options); | ||
ins.request(cb); | ||
return cb(err, null); | ||
} | ||
let host: Address = __this.getAddress(hosts); | ||
let remoteAddr: string = `${host.getHost()}:${host.getPort()}`; | ||
__this.logger.notice(__this.logToString({ | ||
command: 'REQUEST_START', | ||
remote_addr: remoteAddr | ||
})); | ||
let client = new handle(host, options); | ||
client.request((err, service, conn) => { | ||
if (err) { | ||
__this.logger.fatal(__this.logToString({ | ||
command: 'REQUEST_END', | ||
remote_addr: remoteAddr, | ||
error: err.message | ||
})); | ||
} else { | ||
__this.logger.notice(__this.logToString({ | ||
command: 'REQUEST_END', | ||
remote_addr: remoteAddr, | ||
response: (typeof service === 'string') | ||
? service : `service connected.`, | ||
response_size: (typeof service === 'string') | ||
? service.length : -1 | ||
})); | ||
} | ||
cb(err, service, conn); | ||
}); | ||
}; | ||
@@ -96,6 +141,5 @@ | ||
case 'rate_with_idc': | ||
let fn = require('./balance/rateWithIDC'); | ||
return fn(this.config.getHostsWithIDC()); | ||
return require('./balance/rateWithIDC')( | ||
this.config.getHostsWithIDC() | ||
); | ||
default: | ||
@@ -105,2 +149,14 @@ return new Address(); | ||
} | ||
private logToString(obj: Map<any>) { | ||
let log = ''; | ||
for(var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
log += `${key}=[${obj[key]}] `; | ||
} | ||
} | ||
return log; | ||
} | ||
} |
@@ -57,2 +57,4 @@ /** | ||
log: {logFile: string, logLevel: string}; | ||
constructor (public service: string | Map<any>, public idc?: string, public ctx?: any) { | ||
@@ -68,4 +70,11 @@ if (typeof service != 'string') { | ||
if (this.searchHostType == 'LOCAL') { | ||
let hosts = this.hosts[this.idc] | ||
|| [new Address(this.host, this.port)]; | ||
let hosts = this.hosts[this.idc]; | ||
//if idc hosts not exist, return `hosts` or `[{host:, port:}]` | ||
if (!hosts && Array.isArray(this.hosts)) { | ||
hosts = this.hosts; | ||
} else { | ||
hosts = [new Address(this.host, this.port)]; | ||
} | ||
cb(null, hosts); | ||
@@ -75,3 +84,3 @@ } else if (this.searchHostType == 'CONSUL') { | ||
} else { | ||
throw new Error(`unsupport '${this.searchHostType}' searchHostType`); | ||
cb(new Error(`unsupport '${this.searchHostType}' searchHostType`), []); | ||
} | ||
@@ -113,4 +122,32 @@ } | ||
public getLogFile() { | ||
return this.log.logFile; | ||
} | ||
public getLogLevel() { | ||
return this.log.logLevel; | ||
} | ||
/** | ||
* config information load from a config file | ||
* @param {string} file | ||
* @return void | ||
*/ | ||
protected loadFile(file: string) { | ||
let config = {}; | ||
/** | ||
* if set ctx, read from ctx. | ||
* it's config is preload to context. | ||
*/ | ||
if (this.ctx && this.ctx[file]) { | ||
config = this.ctx[file]; | ||
} else { | ||
config = require(file); | ||
} | ||
this.create(config); | ||
} | ||
/** | ||
* create a instance of the Config from a Map. | ||
@@ -148,24 +185,3 @@ * | ||
} | ||
/** | ||
* config information load from a config file | ||
* @param {string} file | ||
* @return void | ||
*/ | ||
protected loadFile(file: string) { | ||
let config = {}; | ||
/** | ||
* if set ctx, read from ctx. | ||
* it's config is preload to context. | ||
*/ | ||
if (this.ctx && this.ctx[file]) { | ||
config = this.ctx[file]; | ||
} else { | ||
config = require(file); | ||
} | ||
this.create(config); | ||
} | ||
/** | ||
@@ -172,0 +188,0 @@ * the time 1s\1ms to \d+(ms) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49982
37
1298
5
4
+ Addedcaller@^1.0.1
+ Addedmoment@^2.18.1
+ Addedcaller@1.1.0(transitive)
+ Addedmoment@2.30.1(transitive)