lightning-request-net
Advanced tools
Comparing version 0.0.2 to 0.1.0-beta.0
80
index.js
@@ -1,79 +0,1 @@ | ||
const Connection = require('./connection'); | ||
const httpParser = require('./http_parser'); | ||
const Pool = require('./pool'); | ||
const userAgent = 'LightningHttpClient/0.0.1'; | ||
class HttpRequestClient { | ||
constructor(options) { | ||
this.options = options || {}; | ||
this.options.poolOptions = Object.assign({}, this.options.poolOptions || {}); | ||
const factory = { | ||
create: () => { | ||
return new Connection(this.options); | ||
}, | ||
}; | ||
this.pool = new Pool(factory, this.options.poolOptions); | ||
} | ||
get defaultHeaders() { | ||
return { | ||
Host: `${this.options.host}:${this.options.port}`, | ||
'Content-Type': 'application/json', | ||
'User-Agent': userAgent, | ||
'Transfer-Encoding': 'chunked', | ||
Connection: 'keep-alive', | ||
}; | ||
} | ||
async request(options) { | ||
const method = (options.method || 'GET').toUpperCase(); | ||
const path = options.path || '/'; | ||
const headers = Object.assign({}, this.defaultHeaders, options.headers || {}); | ||
const responseType = options.responseType || 'json'; | ||
const timeout = options.timeout || 3000; | ||
const data = httpParser.encode({ | ||
method, | ||
path, | ||
headers, | ||
data: options.data, | ||
}); | ||
return new Promise((resolve, reject) => { | ||
const conn = this.pool.acquire(); | ||
let flag = false; | ||
const handler = setTimeout(() => { | ||
flag = true; | ||
reject(new Error('request timeout')); | ||
}, timeout); | ||
conn.write( | ||
data, | ||
resp => { | ||
if (flag) { | ||
return; | ||
} | ||
let result = httpParser.decode(resp); | ||
if (result.statusCode === 200 && responseType === 'json') { | ||
result.data = JSON.parse(result.data); | ||
} | ||
clearTimeout(handler); | ||
resolve(result); | ||
this.pool.release(conn); | ||
}, | ||
error => { | ||
if (flag) { | ||
return; | ||
} | ||
clearTimeout(handler); | ||
reject(error); | ||
this.pool.release(conn); | ||
} | ||
); | ||
}); | ||
} | ||
} | ||
module.exports = HttpRequestClient; | ||
module.exports = require('./src/client'); |
{ | ||
"name": "lightning-request-net", | ||
"version": "0.0.2", | ||
"version": "0.1.0-beta.0", | ||
"description": "Lightweight Node.js HTTP client based on net.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nyc ava --verbose --no-cache --timeout=1m", | ||
"test:watch": "nyc ava --verbose --watch --no-cache --timeout=1m", | ||
"report": "nyc report --reporter=html && open coverage/index.html", | ||
"release": "sh release.sh" | ||
}, | ||
"ava": { | ||
"files": [ | ||
"test/**/*.js" | ||
] | ||
}, | ||
"repository": { | ||
@@ -21,4 +29,9 @@ "type": "git", | ||
"dependencies": { | ||
"debug": "4.1.1" | ||
"debug": "4.1.1", | ||
"zan-json-parse": "1.0.2" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.5.0", | ||
"nyc": "^15.0.0" | ||
} | ||
} |
@@ -15,7 +15,7 @@ ⚡ Lightweight Node.js HTTP client based on net. | ||
``` | ||
```js | ||
const HttpRequestClient = require('lightning-request-net'); | ||
const client = new HttpRequestClient({ | ||
host: 'www.example.com', | ||
port: 8680, | ||
port: 80, | ||
}); | ||
@@ -26,3 +26,3 @@ ``` | ||
``` | ||
```js | ||
(async function() { | ||
@@ -69,2 +69,4 @@ try { | ||
responseType: 'json', // default | ||
allowBigNumberInJSON: false, // default false | ||
} | ||
@@ -71,0 +73,0 @@ ``` |
36
test.js
@@ -6,8 +6,9 @@ const HttpRequestClient = require('./index'); | ||
port: 8680, | ||
// idleTimeout: 60 * 1000, | ||
poolOptions: { | ||
initialValue: 1, | ||
initialValue: 0, | ||
}, | ||
}); | ||
async function request(serviceName, methodName, data = [], options = {}) { | ||
async function request(serviceName, methodName, data, options = {}) { | ||
try { | ||
@@ -24,5 +25,10 @@ const result = await client.request({ | ||
timeout: options.timeout || 3000, | ||
allowBigNumberInJSON: options.allowBigNumberInJSON, | ||
}); | ||
// console.log(result); | ||
if (result.statusCode !== 200) { | ||
console.log(result); | ||
} | ||
// console.log('result = '); | ||
// console.log(result); | ||
console.log(result.statusCode); | ||
// console.log('end result'); | ||
@@ -35,10 +41,22 @@ } catch (error) { | ||
(async function() { | ||
await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', []); | ||
await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', []); | ||
// for (let i = 0; i < 100; i++) { | ||
console.time('#1'); | ||
await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', [], { | ||
responseType: 'json', | ||
}); | ||
console.timeEnd('#1'); | ||
console.time('#2'); | ||
await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', [], { | ||
responseType: 'json', | ||
allowBigNumberInJSON: true, | ||
}); | ||
console.timeEnd('#2'); | ||
setTimeout(async function() { | ||
await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', []); | ||
}, 4 * 1000); | ||
await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', []); | ||
// } | ||
// setTimeout(async function() { | ||
// await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', []); | ||
// }, 4 * 1000); | ||
// await request('com.youzan.uic.auth.api.service.CountryCodeService', 'getAllCountryCode', []); | ||
// await request( | ||
@@ -45,0 +63,0 @@ // 'com.youzan.uic.session.api.service.UicSessionService', |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29911
15
412
105
2
2
3
+ Addedzan-json-parse@1.0.2
+ Addedzan-json-parse@1.0.2(transitive)