From 300415bd75415d9b8ac17b9b153f37f71cd3aa6c Mon Sep 17 00:00:00 2001 From: Eric Satterwhite Date: Mon, 27 Apr 2020 13:08:51 -0500 Subject: [PATCH] transports: replaces request with phin The request packages was deprecated. this replaces it with the phin package in the http transport Semver: minor Ref: #51 --- .travis.yml | 2 +- packages/skyring/lib/transports/http.js | 25 +++++++++++-------- packages/skyring/package.json | 1 - packages/skyring/test/main.spec.js | 2 -- .../skyring/test/unit/transport.http.spec.js | 5 ++-- packages/skyring/test/unit/validator.spec.js | 12 +++------ 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5156dc84..1dc5d367 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,7 @@ before_script: - mkdir -p coverage script: npm run test:coverage after_script: - - npm run nyc report --reporter=text-lcov > coverage/lcov.info + - npm run nyc report -- --reporter=text-lcov > coverage/lcov.info - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT - docker-compose -f compose/nats.yml down install: diff --git a/packages/skyring/lib/transports/http.js b/packages/skyring/lib/transports/http.js index 09761e57..b83df5dc 100644 --- a/packages/skyring/lib/transports/http.js +++ b/packages/skyring/lib/transports/http.js @@ -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 @@ -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}`) @@ -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) diff --git a/packages/skyring/package.json b/packages/skyring/package.json index e93e86d2..8ee4ba11 100644 --- a/packages/skyring/package.json +++ b/packages/skyring/package.json @@ -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" diff --git a/packages/skyring/test/main.spec.js b/packages/skyring/test/main.spec.js index 7d5c9c8b..c50d45d3 100644 --- a/packages/skyring/test/main.spec.js +++ b/packages/skyring/test/main.spec.js @@ -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() @@ -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() }) diff --git a/packages/skyring/test/unit/transport.http.spec.js b/packages/skyring/test/unit/transport.http.spec.js index 4b266a0a..3432fbb0 100644 --- a/packages/skyring/test/unit/transport.http.spec.js +++ b/packages/skyring/test/unit/transport.http.spec.js @@ -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') } } diff --git a/packages/skyring/test/unit/validator.spec.js b/packages/skyring/test/unit/validator.spec.js index cc9f30ad..e6b189f7 100644 --- a/packages/skyring/test/unit/validator.spec.js +++ b/packages/skyring/test/unit/validator.spec.js @@ -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() }) }) @@ -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() }) })