Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade lc39 to v7 #351

Merged
merged 6 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ jobs:
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

publish:
runs-on: ubuntu-latest
needs: [build]
if: ${{ startsWith(github.ref, 'refs/tags/') }}
steps:
- uses: actions/checkout@v3
- uses: JS-DevTools/npm-publish@v2
with:
token: ${{ secrets.NPM_TOKEN }}
access: 'public'
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### BREAKING CHANGES

- when returns stream, content-type `application/octet-stream` is not added by default anymore. So, you should add it on your own. See [here](https://github.com/fastify/fastify/pull/3086) for reference
- upgrade lc39 to v7 and fastify to v4

## v5.1.7 - 2023-03-13

### Fixed
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,12 @@ function getHttpClientFromRequest(url, baseOptions = {}) {
return new HttpClient(url, serviceHeaders, options, this.httpClientMetrics)
}

function getHttpClient(url, baseOptions = {}) {
return new HttpClient(url, {}, baseOptions, this.httpClientMetrics)
function getHttpClient(url, baseOptions = {}, httpClientMetrics = {}) {
return new HttpClient(url, {}, baseOptions, httpClientMetrics)
}

function getHttpClientFastifyDecoration(url, baseOptions = {}) {
return getHttpClient(url, baseOptions, this.httpClientMetrics)
}

function getHeadersToProxy({ isMiaHeaderInjected = true } = {}) {
Expand Down Expand Up @@ -274,7 +278,7 @@ function decorateFastify(fastify) {
fastify.decorateRequest('getDirectServiceProxy', getDirectlyServiceBuilderFromRequest)
fastify.decorateRequest('getServiceProxy', getServiceBuilderFromRequest)
fastify.decorateRequest('getHttpClient', getHttpClientFromRequest)
fastify.decorateRequest('httpClientMetrics', httpClientMetrics)
fastify.decorateRequest('httpClientMetrics', { getter: () => httpClientMetrics })

fastify.decorate(MICROSERVICE_GATEWAY_SERVICE_NAME, config[MICROSERVICE_GATEWAY_SERVICE_NAME])
fastify.decorate('addRawCustomPlugin', addRawCustomPlugin)
Expand All @@ -283,7 +287,7 @@ function decorateFastify(fastify) {

fastify.decorate('getDirectServiceProxy', getDirectlyServiceBuilderFromService)
fastify.decorate('getServiceProxy', getServiceBuilderFromService)
fastify.decorate('getHttpClient', getHttpClient)
fastify.decorate('getHttpClient', getHttpClientFastifyDecoration)
fastify.decorate('httpClientMetrics', httpClientMetrics)
}

Expand Down
4 changes: 2 additions & 2 deletions lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class HttpClient {
})
const responseBody = {
statusCode: response.status,
headers: response.headers,
headers: { ...response.headers.toJSON() },
payload: response.data,
duration: response.duration,
}
Expand All @@ -128,7 +128,7 @@ class HttpClient {
if (error.response) {
const errorMessage = getErrorMessage(error.response, options.returnAs, errorMessageKey)
const responseError = new Error(errorMessage)
responseError.headers = error.response.headers
responseError.headers = { ...error.response.headers.toJSON() }
responseError.statusCode = error.response.status
responseError.payload = error.response.data
responseError.duration = error.duration
Expand Down
30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@
"version": "./scripts/update-version.sh ${npm_package_version} && git add CHANGELOG.md"
},
"dependencies": {
"@fastify/env": "^3.0.0",
"@fastify/formbody": "^6.0.0",
"@mia-platform/lc39": "^6.0.0",
"@types/node": "^16.7.1",
"ajv": "^8.11.0",
"@fastify/env": "^4.2.0",
"@fastify/formbody": "^7.0.0",
"@mia-platform/lc39": "^7.0.0",
"@types/node": "^18.15.3",
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"axios": "^0.27.2",
"fastify-plugin": "^3.0.1",
"axios": "^1.4.0",
"fastify-plugin": "^4.5.0",
"http-errors": "^2.0.0",
"pino": "^8.0.0",
"simple-get": "^4.0.1"
},
"devDependencies": {
"@fastify/routes": "^4.0.0",
"@fastify/routes": "^5.1.0",
"@mia-platform/eslint-config-mia": "^3.0.0",
"eslint": "^8.14.0",
"hpagent": "^1.0.0",
"nock": "^13.2.4",
"eslint": "^8.40.0",
"hpagent": "^1.2.0",
"nock": "^13.3.1",
"pre-commit": "^1.2.2",
"proxy": "^1.0.2",
"split2": "^4.1.0",
"proxy": "^2.0.1",
"split2": "^4.2.0",
"swagger-parser": "^10.0.3",
"tap": "^16.1.0",
"typescript": "^4.6.4"
"tap": "^16.3.4",
"typescript": "^5.0.4"
},
"engines": {
"node": ">=14"
Expand Down
26 changes: 10 additions & 16 deletions tests/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const tap = require('tap')
const nock = require('nock')
const { Readable } = require('stream')
const http = require('http')
const proxy = require('proxy')
const { createProxy: proxy } = require('proxy')
const fs = require('fs')
const https = require('https')
const split = require('split2')
Expand Down Expand Up @@ -1716,10 +1716,9 @@ tap.test('httpClient', test => {
res.end('{"status": "ok"}')
})
const serverProxy = await createProxy()
let proxyCalled = false
serverProxy.authenticate = (req, fn) => {
proxyCalled = true
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
serverProxy.authenticate = (req) => {
assert.strictSame(req.headers['proxy-authorization'], `Basic ${Buffer.from('hello:world').toString('base64')}`)
return true
}

const service = new HttpClient(`http://${server.address().address}:${server.address().port}`)
Expand All @@ -1738,7 +1737,6 @@ tap.test('httpClient', test => {

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload, { status: 'ok' })
assert.ok(proxyCalled)

server.close()
serverProxy.close()
Expand All @@ -1752,10 +1750,9 @@ tap.test('httpClient', test => {
res.end('OK')
})
const serverProxy = await createProxy()
let proxyCalled = false
serverProxy.authenticate = (req, fn) => {
proxyCalled = true
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
serverProxy.authenticate = (req) => {
assert.strictSame(req.headers['proxy-authorization'], `Basic ${Buffer.from('hello:world').toString('base64')}`)
return true
}

const service = new HttpClient(`http://${server.address().address}:${server.address().port}`)
Expand All @@ -1775,7 +1772,6 @@ tap.test('httpClient', test => {

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload.toString('utf-8'), 'OK')
assert.ok(proxyCalled)

server.close()
serverProxy.close()
Expand All @@ -1789,10 +1785,9 @@ tap.test('httpClient', test => {
res.end(JSON.stringify({ the: 'response' }))
})
const serverProxy = await createProxy()
let proxyCalled = false
serverProxy.authenticate = (req, fn) => {
proxyCalled = true
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
serverProxy.authenticate = (req) => {
assert.strictSame(req.headers['proxy-authorization'], `Basic ${Buffer.from('hello:world').toString('base64')}`)
return true
}

const service = new HttpClient(`http://${server.address().address}:${server.address().port}`)
Expand All @@ -1812,7 +1807,6 @@ tap.test('httpClient', test => {

assert.equal(response.statusCode, 200)
assert.ok(response.headers['content-length'])
assert.ok(proxyCalled)

await wait(200)

Expand Down
5 changes: 5 additions & 0 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ tap.test('Plain Custom Service', test => {
statusCode: 400,
message: 'body must NOT have additional properties',
error: 'Bad Request',
code: 'FST_ERR_VALIDATION',
}, 'bad response')

const badResponse1 = await fastify.inject({
Expand All @@ -215,6 +216,7 @@ tap.test('Plain Custom Service', test => {
statusCode: 400,
error: 'Bad Request',
message: 'body.foobar must be equal to one of the allowed values',
code: 'FST_ERR_VALIDATION',
}, 'enum validation')

const badResponse2 = await fastify.inject({
Expand All @@ -233,6 +235,7 @@ tap.test('Plain Custom Service', test => {
statusCode: 400,
error: 'Bad Request',
message: 'body.nested.field must be string',
code: 'FST_ERR_VALIDATION',
}, 'nested fields validation')

const badResponse3 = await fastify.inject({
Expand All @@ -247,6 +250,7 @@ tap.test('Plain Custom Service', test => {
statusCode: 400,
error: 'Bad Request',
message: 'querystring.some must be number',
code: 'FST_ERR_VALIDATION',
}, 'nested fields validation')

assert.end()
Expand Down Expand Up @@ -472,6 +476,7 @@ tap.test('Service with API formats', t => {
statusCode: 400,
error: 'Bad Request',
message: 'body.someDate must match format "date-time"',
code: 'FST_ERR_VALIDATION',
})

t.end()
Expand Down
26 changes: 10 additions & 16 deletions tests/serviceBuilder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const serviceBuilder = require('../lib/serviceBuilder')
const reqheaders = { 'content-type': 'application/json;charset=utf-8' }
const { Readable } = require('stream')
const http = require('http')
const proxy = require('proxy')
const { createProxy: proxy } = require('proxy')
const { HttpProxyAgent } = require('hpagent')
const fs = require('fs')
const https = require('https')
Expand Down Expand Up @@ -1331,10 +1331,9 @@ tap.test('serviceBuilder', test => {
res.end('{"status": "ok"}')
})
const serverProxy = await createProxy()
let proxyCalled = false
serverProxy.authenticate = (req, fn) => {
proxyCalled = true
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
serverProxy.authenticate = req => {
assert.strictSame(req.headers['proxy-authorization'], `Basic ${Buffer.from('hello:world').toString('base64')}`)
return true
}

const service = serviceBuilder(server.address().address)
Expand All @@ -1349,7 +1348,6 @@ tap.test('serviceBuilder', test => {

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload, { status: 'ok' })
assert.ok(proxyCalled)

server.close()
serverProxy.close()
Expand All @@ -1363,10 +1361,9 @@ tap.test('serviceBuilder', test => {
res.end('OK')
})
const serverProxy = await createProxy()
let proxyCalled = false
serverProxy.authenticate = (req, fn) => {
proxyCalled = true
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
serverProxy.authenticate = (req) => {
assert.strictSame(req.headers['proxy-authorization'], `Basic ${Buffer.from('hello:world').toString('base64')}`)
return true
}

const service = serviceBuilder(server.address().address)
Expand All @@ -1381,7 +1378,6 @@ tap.test('serviceBuilder', test => {

assert.equal(response.statusCode, 200)
assert.strictSame(response.payload.toString('utf-8'), 'OK')
assert.ok(proxyCalled)

server.close()
serverProxy.close()
Expand All @@ -1395,10 +1391,9 @@ tap.test('serviceBuilder', test => {
res.end(JSON.stringify({ the: 'response' }))
})
const serverProxy = await createProxy()
let proxyCalled = false
serverProxy.authenticate = (req, fn) => {
proxyCalled = true
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('hello:world').toString('base64')}`)
serverProxy.authenticate = (req) => {
assert.strictSame(req.headers['proxy-authorization'], `Basic ${Buffer.from('hello:world').toString('base64')}`)
return true
}

const service = serviceBuilder(server.address().address)
Expand All @@ -1413,7 +1408,6 @@ tap.test('serviceBuilder', test => {

assert.equal(response.statusCode, 200)
assert.ok(response.headers['content-length'])
assert.ok(proxyCalled)

await wait(200)

Expand Down
1 change: 1 addition & 0 deletions tests/services/plain-custom-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ module.exports = customService(async function clientGroups(service) {
}

function handlerStream(request, reply) {
reply.header('Content-Type', 'application/octet-stream')
reply.send(fs.createReadStream('./tests/services/plain-custom-service.js'))
}

Expand Down