From 2f59d40a68c6a86d08c67b36b1101b1e77f95783 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Wed, 10 May 2023 18:05:42 +0200 Subject: [PATCH 1/6] upgrade lc39 to v7 --- CHANGELOG.md | 10 ++++++++++ index.js | 2 +- package.json | 26 +++++++++++++------------- tests/index.test.js | 5 +++++ tests/services/plain-custom-service.js | 1 + 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3bc8dc..782c1ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ 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 + +### Upgraded + +- upgrade lc39 to v7 and fastify to v4 + ## v5.1.7 - 2023-03-13 ### Fixed diff --git a/index.js b/index.js index 73c3858..6b02a82 100644 --- a/index.js +++ b/index.js @@ -274,7 +274,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) diff --git a/package.json b/package.json index c994e6f..2ca4d9d 100644 --- a/package.json +++ b/package.json @@ -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-rc.2", + "@types/node": "^18.15.3", + "ajv": "^8.12.0", "ajv-formats": "^2.1.1", "axios": "^0.27.2", - "fastify-plugin": "^3.0.1", + "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", + "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" diff --git a/tests/index.test.js b/tests/index.test.js index 500f4b5..1866312 100644 --- a/tests/index.test.js +++ b/tests/index.test.js @@ -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({ @@ -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({ @@ -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({ @@ -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() @@ -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() diff --git a/tests/services/plain-custom-service.js b/tests/services/plain-custom-service.js index d2de526..575a945 100644 --- a/tests/services/plain-custom-service.js +++ b/tests/services/plain-custom-service.js @@ -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')) } From e83cf4af6e14c5d6dce47ba782dc508b3d1297d7 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Wed, 10 May 2023 18:38:04 +0200 Subject: [PATCH 2/6] upgrade axios to 1.4 --- lib/httpClient.js | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/httpClient.js b/lib/httpClient.js index 61e09fb..d022ae1 100644 --- a/lib/httpClient.js +++ b/lib/httpClient.js @@ -108,7 +108,7 @@ class HttpClient { }) const responseBody = { statusCode: response.status, - headers: response.headers, + headers: { ...response.headers.toJSON() }, payload: response.data, duration: response.duration, } @@ -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 diff --git a/package.json b/package.json index 2ca4d9d..307acae 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "@types/node": "^18.15.3", "ajv": "^8.12.0", "ajv-formats": "^2.1.1", - "axios": "^0.27.2", + "axios": "^1.4.0", "fastify-plugin": "^4.5.0", "http-errors": "^2.0.0", "pino": "^8.0.0", From 661d7da426b84fbfc398d8c70669da0daeb822fa Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Wed, 10 May 2023 19:03:37 +0200 Subject: [PATCH 3/6] upgrade proxy --- package.json | 2 +- tests/httpClient.test.js | 26 ++++++++++---------------- tests/serviceBuilder.test.js | 26 ++++++++++---------------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 307acae..96b99bc 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "hpagent": "^1.2.0", "nock": "^13.3.1", "pre-commit": "^1.2.2", - "proxy": "^1.0.2", + "proxy": "^2.0.1", "split2": "^4.2.0", "swagger-parser": "^10.0.3", "tap": "^16.3.4", diff --git a/tests/httpClient.test.js b/tests/httpClient.test.js index ddb2014..808d32d 100644 --- a/tests/httpClient.test.js +++ b/tests/httpClient.test.js @@ -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') @@ -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}`) @@ -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() @@ -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}`) @@ -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() @@ -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}`) @@ -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) diff --git a/tests/serviceBuilder.test.js b/tests/serviceBuilder.test.js index 9c9cbdb..ac88e61 100644 --- a/tests/serviceBuilder.test.js +++ b/tests/serviceBuilder.test.js @@ -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') @@ -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) @@ -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() @@ -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) @@ -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() @@ -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) @@ -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) From 9880e213e539931277756f6235c948efbe36e3b1 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Wed, 10 May 2023 19:11:54 +0200 Subject: [PATCH 4/6] docs: update CHANGELOG --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 782c1ee..191d74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,9 +9,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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 - -### Upgraded - - upgrade lc39 to v7 and fastify to v4 ## v5.1.7 - 2023-03-13 From 611d55debecfc86d23f3f3b8dd442db4ffd15d74 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Wed, 10 May 2023 19:37:38 +0200 Subject: [PATCH 5/6] fix: fix getHttpClient fn --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6b02a82..f8f62ee 100644 --- a/index.js +++ b/index.js @@ -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 } = {}) { @@ -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) } From d556cfc58b30a6aecbb6e3b97eaf2882047e11f6 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Thu, 11 May 2023 14:30:18 +0200 Subject: [PATCH 6/6] uprade lc39 and add publish workflow --- .github/workflows/node.js.yml | 11 +++++++++++ package.json | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index 1e64602..e9c07f5 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -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' diff --git a/package.json b/package.json index 96b99bc..be0ad01 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "dependencies": { "@fastify/env": "^4.2.0", "@fastify/formbody": "^7.0.0", - "@mia-platform/lc39": "^7.0.0-rc.2", + "@mia-platform/lc39": "^7.0.0", "@types/node": "^18.15.3", "ajv": "^8.12.0", "ajv-formats": "^2.1.1",