Skip to content

Commit

Permalink
transports: replaces request with phin
Browse files Browse the repository at this point in the history
The request packages was deprecated. this replaces it with the phin
package in the http transport

Semver: minor
Ref: #51
  • Loading branch information
Eric Satterwhite committed Apr 27, 2020
1 parent 7721d3e commit 0c5818e
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
25 changes: 15 additions & 10 deletions packages/skyring/lib/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/

const STATUS_CODES = require('http').STATUS_CODES
const request = require('request')
const phin = require('phin').unpromisified
const debug = require('debug')('skyring:transport:http')
const Transport = require('./transport')
const {name, version} = require('../../package.json')
const method_exp = /^(post|put|patch|delete|get|options|head)$/i
const kType = Symbol.for('SkyringTransport')
const TRANSPORT = 'httptransport'
const USER_AGENT = `${name}/${version}`

/**
* Dispatches an http request
* @function
Expand All @@ -39,14 +42,8 @@ class Http extends Transport {
}

exec(method, url, payload, id, cache) {
const isJSON = typeof payload === 'object'
const _method = method.toLowerCase()
const options = {
json: isJSON
, body: payload || ''
}

if (!method_exp.test(method) || typeof request[_method] !== 'function') {
const body = payload || ''
if (!method_exp.test(method)) {
const pending = cache.get(id)
pending && clearTimeout(pending.timer)
const err = new Error(`Invalid http verb ${method}`)
Expand All @@ -57,7 +54,15 @@ class Http extends Transport {
}

debug('executing http transport %s', id, method)
request[_method](url, options, (err, res, body) => {

phin({
url: url
, data: body
, method: method
, headers: {
'User-Agent': USER_AGENT
}
}, (err, res) => {
if (err) {
debug('timer err', err)
return cache.failure(id, err)
Expand Down
1 change: 0 additions & 1 deletion packages/skyring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"nats": "^1.4.8",
"path-to-regexp": "^3.0.0",
"phin": "^3.4.1",
"request": "^2.88.0",
"seeli": "^9.0.0",
"tchannel": "^4.0.0",
"uuid": "^3.2.1"
Expand Down
2 changes: 0 additions & 2 deletions packages/skyring/test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ test('server starts when executed directly', (t) => {

let buf = ''
const cases = { listen: false }

child.once('error', t.threw)
child.stderr.on('data', (chunk) => {
buf += chunk.toString()
Expand All @@ -42,7 +41,6 @@ test('server starts when executed directly', (t) => {
}

child.on('close', (code, signal) => {
console.log('close', code)
t.equal(code, 0)
t.end()
})
Expand Down
5 changes: 3 additions & 2 deletions packages/skyring/test/unit/transport.http.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ test('http transport', async (t) => {
const addr = `http://localhost:${state.server.address().port}/r1`
const mock_store = {
success: () => {
tt.pass('timer success')}
tt.pass('timer success')
}
, failure: () => {
tt.fail('timer fail')
tt.fail('timer failure')
}
}

Expand Down
12 changes: 4 additions & 8 deletions packages/skyring/test/unit/validator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ test('timer payload validation', async (t) => {
timeout: MAX + 1
}, (err) => {
tt.type(err, Error, 'error is of type Error')
tt.match(err, {
statusCode: 400
, message: new RegExp(`less than or equal to ${MAX}`, 'ig')
})
tt.match(err.statusCode, 400, 'error statusCode')
tt.match(err.message, new RegExp(`less than or equal to ${MAX}`, 'ig'), 'error message')
tt.end()
})
})
Expand All @@ -63,10 +61,8 @@ test('timer payload validation', async (t) => {
, data: false
}, (err) => {
tt.type(err, Error, 'error is of type Error')
tt.match(err, {
statusCode: 400
, message: /must be a string or object/ig
})
tt.match(err.statusCode, 400, 'error statusCode')
tt.match(err.message, /must be a string or object/ig, 'error message')
tt.end()
})
})
Expand Down

0 comments on commit 0c5818e

Please sign in to comment.