fast-gateway
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -34,2 +34,7 @@ const gateway = require('./../index') | ||
.get('/hi', (req, res) => res.send('Hello World!')) | ||
.get('/hi-chunked', (req, res) => { | ||
res.write('Hello ') | ||
res.write('World!') | ||
res.end() | ||
}) | ||
.start(3000).then(() => console.log('Public service listening on 3000 port!')) | ||
@@ -36,0 +41,0 @@ |
36
index.js
@@ -106,8 +106,28 @@ /* eslint-disable no-useless-call */ | ||
const onResponse = async (req, res, stream) => { | ||
if (!res.hasHeader('content-length')) { | ||
const TRANSFER_ENCODING_HEADER_NAME = 'transfer-encoding' | ||
const chunked = stream.headers[TRANSFER_ENCODING_HEADER_NAME] | ||
? stream.headers[TRANSFER_ENCODING_HEADER_NAME].endsWith('chunked') | ||
: false | ||
if (req.headers.connection === 'close' && chunked) { | ||
try { | ||
const resBuffer = Buffer.concat(await toArray(stream)) | ||
res.setHeader('content-length', '' + Buffer.byteLength(resBuffer)) | ||
res.statusCode = stream.statusCode | ||
res.end(resBuffer) | ||
// remove transfer-encoding header | ||
const transferEncoding = stream.headers[TRANSFER_ENCODING_HEADER_NAME].replace(/(,( )?)?chunked/, '') | ||
if (transferEncoding) { | ||
res.setHeader(TRANSFER_ENCODING_HEADER_NAME, transferEncoding) | ||
} else { | ||
res.removeHeader(TRANSFER_ENCODING_HEADER_NAME) | ||
} | ||
if (!stream.headers['content-length']) { | ||
// pack all pieces into 1 buffer to calculate content length | ||
const resBuffer = Buffer.concat(await toArray(stream)) | ||
// add content-length header and send the merged response buffer | ||
res.setHeader('content-length', '' + Buffer.byteLength(resBuffer)) | ||
res.statusCode = stream.statusCode | ||
res.end(resBuffer) | ||
return | ||
} | ||
} catch (err) { | ||
@@ -117,8 +137,8 @@ res.statusCode = 500 | ||
} | ||
} else { | ||
res.statusCode = stream.statusCode | ||
pump(stream, res) | ||
} | ||
res.statusCode = stream.statusCode | ||
pump(stream, res) | ||
} | ||
module.exports = gateway |
{ | ||
"name": "fast-gateway", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "A Node.js API Gateway for the masses!", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nyc mocha test/*.test.js --exit", | ||
"test": "nyc mocha test/*.test.js", | ||
"format": "npx standard --fix", | ||
@@ -42,4 +42,4 @@ "lint": "npx standard" | ||
"helmet": "^3.21.2", | ||
"mocha": "^6.2.2", | ||
"nyc": "^14.1.1", | ||
"mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"opossum": "^4.2.4", | ||
@@ -46,0 +46,0 @@ "request-ip": "^2.1.3", |
@@ -114,15 +114,36 @@ # fast-gateway | ||
const onResponse = async (req, res, stream) => { | ||
if (!res.hasHeader('content-length')) { | ||
const TRANSFER_ENCODING_HEADER_NAME = 'transfer-encoding' | ||
const chunked = stream.headers[TRANSFER_ENCODING_HEADER_NAME] | ||
? stream.headers[TRANSFER_ENCODING_HEADER_NAME].endsWith('chunked') | ||
: false | ||
if (req.headers.connection === 'close' && chunked) { | ||
try { | ||
const resBuffer = Buffer.concat(await toArray(stream)) | ||
res.statusCode = stream.statusCode | ||
res.setHeader('content-length', '' + Buffer.byteLength(resBuffer)) | ||
res.end(resBuffer) | ||
// remove transfer-encoding header | ||
const transferEncoding = stream.headers[TRANSFER_ENCODING_HEADER_NAME].replace(/(,( )?)?chunked/, '') | ||
if (transferEncoding) { | ||
res.setHeader(TRANSFER_ENCODING_HEADER_NAME, transferEncoding) | ||
} else { | ||
res.removeHeader(TRANSFER_ENCODING_HEADER_NAME) | ||
} | ||
if (!stream.headers['content-length']) { | ||
// pack all pieces into 1 buffer to calculate content length | ||
const resBuffer = Buffer.concat(await toArray(stream)) | ||
// add content-length header and send the merged response buffer | ||
res.setHeader('content-length', '' + Buffer.byteLength(resBuffer)) | ||
res.statusCode = stream.statusCode | ||
res.end(resBuffer) | ||
return | ||
} | ||
} catch (err) { | ||
res.send(err) | ||
res.statusCode = 500 | ||
res.end(err.message) | ||
} | ||
} else { | ||
res.statusCode = stream.statusCode | ||
pump(stream, res) | ||
} | ||
res.statusCode = stream.statusCode | ||
pump(stream, res) | ||
} | ||
@@ -129,0 +150,0 @@ ``` |
@@ -22,2 +22,7 @@ /* global describe, it */ | ||
})) | ||
remote.get('/chunked', (req, res) => { | ||
res.write('user') | ||
res.write('1') | ||
res.end() | ||
}) | ||
remote.get('/cache', (req, res) => { | ||
@@ -283,3 +288,24 @@ res.setHeader('x-cache-timeout', '1 second') | ||
it('close', async () => { | ||
it('(Connection: close) chunked transfer-encoding support', async () => { | ||
await request(gateway) | ||
.get('/users/chunked') | ||
.set({ Connection: 'close' }) | ||
.expect(200) | ||
.then((response) => { | ||
expect(response.text).to.equal('user1') | ||
}) | ||
}) | ||
it('(Connection: keep-alive) chunked transfer-encoding support', async () => { | ||
await request(gateway) | ||
.get('/users/chunked') | ||
.set('Connection', 'keep-alive') | ||
.then((res) => { | ||
expect(res.text).to.equal('user1') | ||
}) | ||
}) | ||
it('close', async function () { | ||
this.timeout(10 * 1000) | ||
await remote.close() | ||
@@ -286,0 +312,0 @@ await gateway.close() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33646
648
323