From a5bbb60a07862b0946ed1942ef8d009c98dc2061 Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Wed, 19 Jun 2019 10:26:32 +0200 Subject: [PATCH 1/3] by default populate content-length header if missing --- index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/index.js b/index.js index 41279b7..65f2d4c 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ const fastProxy = require('fast-proxy') const restana = require('restana') +const pump = require('pump') +const toArray = require('stream-to-array') const gateway = (opts) => { opts = Object.assign({ @@ -24,6 +26,7 @@ const gateway = (opts) => { // populating required NOOPS route.hooks = route.hooks || {} route.hooks.onRequest = route.hooks.onRequest || onRequestNoOp + route.hooks.onResponse = route.hooks.onResponse || onResponse // populating pathRegex if missing route.pathRegex = undefined === route.pathRegex ? opts.pathRegex : String(route.pathRegex) @@ -52,5 +55,20 @@ const handler = (route, proxy) => async (req, res) => { } const onRequestNoOp = (req, res) => { } +const onResponse = async (req, res, stream) => { + if (!res.hasHeader('content-length')) { + try { + const resBuffer = Buffer.concat(await toArray(stream)) + res.statusCode = stream.statusCode + res.setHeader('content-length', '' + Buffer.byteLength(resBuffer)) + res.end(resBuffer) + } catch (err) { + res.send(err) + } + } else { + res.statusCode = stream.statusCode + pump(stream, res) + } +} module.exports = gateway From 1ebd99f62f06229272ddc41195f8d9113498c959 Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Wed, 19 Jun 2019 10:26:41 +0200 Subject: [PATCH 2/3] fix typo --- test/smoke.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/smoke.test.js b/test/smoke.test.js index cc101a6..2db0130 100644 --- a/test/smoke.test.js +++ b/test/smoke.test.js @@ -230,7 +230,7 @@ describe('API Gateway', () => { it('POST /users/info - 404', async () => { await request(gateway) - .post('/users/find') + .post('/users/info') .expect(404) }) From 7ad608db8ea99dc84901c953c4e71708ed666528 Mon Sep 17 00:00:00 2001 From: Rolando Santamaria Maso Date: Wed, 19 Jun 2019 10:26:48 +0200 Subject: [PATCH 3/3] v1.2.0 --- package-lock.json | 15 ++++++++++++++- package.json | 5 +++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index dad8991..1b3dde4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "fast-gateway", - "version": "1.1.2", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -184,6 +184,11 @@ "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", "dev": true }, + "any-promise": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", + "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" + }, "append-transform": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/append-transform/-/append-transform-1.0.0.tgz", @@ -3174,6 +3179,14 @@ } } }, + "stream-to-array": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/stream-to-array/-/stream-to-array-2.3.0.tgz", + "integrity": "sha1-u/azn19D7DC8cbq8s3VXrOzzQ1M=", + "requires": { + "any-promise": "^1.1.0" + } + }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", diff --git a/package.json b/package.json index 90e2faa..37cc1c3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fast-gateway", - "version": "1.1.2", + "version": "1.2.0", "description": "A Node.js API Gateway for the masses!", "main": "index.js", "scripts": { @@ -28,7 +28,8 @@ "dependencies": { "fast-proxy": "^1.1.2", "http-cache-middleware": "^1.0.0", - "restana": "^3.1.1" + "restana": "^3.1.1", + "stream-to-array": "^2.3.0" }, "devDependencies": { "chai": "^4.2.0",