New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

node-http-proxy-json

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-http-proxy-json - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

174

index.js

@@ -1,5 +0,7 @@

var zlib = require('zlib');
var concatStream = require('concat-stream');
var BufferHelper = require('bufferhelper');
'use strict';
const zlib = require('zlib');
const concatStream = require('concat-stream');
const BufferHelper = require('bufferhelper');
/**

@@ -12,30 +14,30 @@ * Modify the response of json

module.exports = function modifyResponse(res, contentEncoding, callback) {
var unzip, zip;
// Now only deal with the gzip/deflate/undefined content-encoding.
switch (contentEncoding) {
case 'gzip':
unzip = zlib.Gunzip();
zip = zlib.Gzip();
break;
case 'deflate':
unzip = zlib.Inflate();
zip = zlib.Deflate();
break;
}
let unzip, zip;
// Now only deal with the gzip/deflate/undefined content-encoding.
switch (contentEncoding) {
case 'gzip':
unzip = zlib.Gunzip();
zip = zlib.Gzip();
break;
case 'deflate':
unzip = zlib.Inflate();
zip = zlib.Deflate();
break;
}
// The cache response method can be called after the modification.
var _write = res.write;
var _end = res.end;
// The cache response method can be called after the modification.
let _write = res.write;
let _end = res.end;
if (unzip) {
unzip.on('error', function (e) {
console.log('Unzip error: ', e);
_end.call(res);
});
handleCompressed(res, _write, _end, unzip, zip, callback);
} else if (!contentEncoding) {
handleUncompressed(res, _write, _end, callback);
} else {
console.log('Not supported content-encoding: ' + contentEncoding);
}
if (unzip) {
unzip.on('error', function(e) {
console.log('Unzip error: ', e);
_end.call(res);
});
handleCompressed(res, _write, _end, unzip, zip, callback);
} else if (!contentEncoding) {
handleUncompressed(res, _write, _end, callback);
} else {
console.log('Not supported content-encoding: ' + contentEncoding);
}
};

@@ -47,42 +49,42 @@

function handleCompressed(res, _write, _end, unzip, zip, callback) {
// The rewrite response method is replaced by unzip stream.
res.write = function (data) {
unzip.write(data);
};
// The rewrite response method is replaced by unzip stream.
res.write = data => unzip.write(data);
res.end = function () {
unzip.end();
};
res.end = () => unzip.end();
// Concat the unzip stream.
var concatWrite = concatStream(function (data) {
var body;
try {
body = JSON.parse(data.toString());
} catch (e) {
body = data.toString();
console.log('JSON.parse error:', e);
}
// Concat the unzip stream.
let concatWrite = concatStream(data => {
let body;
try {
body = JSON.parse(data.toString());
} catch (e) {
body = data.toString();
console.log('JSON.parse error:', e);
}
// Custom modified logic
if (typeof callback === 'function') {
body = callback(body);
}
// Custom modified logic
if (typeof callback === 'function') {
body = callback(body);
}
// Converts the JSON to buffer.
body = new Buffer(JSON.stringify(body));
let finish = _body => {
// Converts the JSON to buffer.
let body = new Buffer(JSON.stringify(_body));
// Call the response method and recover the content-encoding.
zip.on('data', function (chunk) {
_write.call(res, chunk);
});
zip.on('end', function () {
_end.call(res);
});
// Call the response method and recover the content-encoding.
zip.on('data', chunk => _write.call(res, chunk));
zip.on('end', () => _end.call(res));
zip.write(body);
zip.end();
});
zip.write(body);
zip.end();
};
unzip.pipe(concatWrite);
if (body && body.then) {
body.then(finish);
} else {
finish(body);
}
});
unzip.pipe(concatWrite);
}

@@ -94,28 +96,34 @@

function handleUncompressed(res, _write, _end, callback) {
var buffer = new BufferHelper();
// Rewrite response method and get the content.
res.write = function (data) {
buffer.concat(data);
};
let buffer = new BufferHelper();
// Rewrite response method and get the content.
res.write = data => buffer.concat(data);
res.end = function () {
var body;
try {
body = JSON.parse(buffer.toBuffer().toString());
} catch (e) {
console.log('JSON.parse error:', e);
}
res.end = () => {
let body;
try {
body = JSON.parse(buffer.toBuffer().toString());
} catch (e) {
console.log('JSON.parse error:', e);
}
// Custom modified logic
if (typeof callback === 'function') {
body = callback(body);
}
// Custom modified logic
if (typeof callback === 'function') {
body = callback(body);
}
// Converts the JSON to buffer.
body = new Buffer(JSON.stringify(body));
let finish = _body => {
// Converts the JSON to buffer.
let body = new Buffer(JSON.stringify(_body));
// Call the response method
_write.call(res, body);
_end.call(res);
// Call the response method
_write.call(res, body);
_end.call(res);
};
if (body && body.then) {
body.then(finish);
} else {
finish(body);
}
};
}
{
"name": "node-http-proxy-json",
"version": "0.1.3",
"version": "0.1.4",
"description": "for node-http-proxy transform the response json from the proxied server.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -20,3 +20,3 @@ # node-http-proxy-json [![Build Status](https://travis-ci.org/langjt/node-http-proxy-json.svg?branch=master)](https://travis-ci.org/langjt/node-http-proxy-json)

```
```js
var zlib = require('zlib');

@@ -40,3 +40,3 @@ var http = require('http');

}
return body;
return body; // return value can be a promise
});

@@ -80,3 +80,3 @@ });

```
```js
var zlib = require('zlib');

@@ -100,3 +100,3 @@ var http = require('http');

}
return body;
return body; // return value can be a promise
});

@@ -140,3 +140,3 @@ });

```
```js
var http = require('http');

@@ -159,3 +159,3 @@ var httpProxy = require('http-proxy');

}
return body;
return body; // return value can be a promise
});

@@ -162,0 +162,0 @@ });

@@ -0,1 +1,2 @@

'use strict';
/**

@@ -5,82 +6,136 @@ * Test content-encoding for deflate

var assert = require('chai').assert;
const assert = require('chai').assert;
var zlib = require('zlib');
var http = require('http');
var httpProxy = require('http-proxy');
var modifyResponse = require('../');
const zlib = require('zlib');
const http = require('http');
const httpProxy = require('http-proxy');
const modifyResponse = require('../');
var SERVER_PORT = 5002;
var TARGET_SERVER_PORT = 5003;
const SERVER_PORT = 5002;
const TARGET_SERVER_PORT = 5003;
// Create a proxy server
var proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + TARGET_SERVER_PORT
});
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return body;
describe('modifyResponse--deflate', () => {
let proxy, server, targetServer;
beforeEach(() => {
// Create a proxy server
proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + TARGET_SERVER_PORT,
});
});
// Create your server and then proxies the request
var server = http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(SERVER_PORT);
// Create your server and then proxies the request
server = http
.createServer((req, res) => proxy.web(req, res))
.listen(SERVER_PORT);
// Create your target server
var targetServer = http.createServer(function (req, res) {
// Create your target server
targetServer = http
.createServer(function(req, res) {
// Create deflated content
let deflate = zlib.Deflate();
let _write = res.write;
let _end = res.end;
// Create deflated content
var deflate = zlib.Deflate();
var _write = res.write;
var _end = res.end;
deflate.on('data', buf => _write.call(res, buf));
deflate.on('end', () => _end.call(res));
deflate.on('data', function (buf) {
_write.call(res, buf);
});
deflate.on('end', function () {
_end.call(res);
});
res.write = data => deflate.write(data);
res.end = () => deflate.end();
res.write = function (data) {
deflate.write(data);
};
res.end = function () {
deflate.end();
};
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Encoding': 'deflate',
});
res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'deflate'});
res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'}));
res.end();
}).listen(TARGET_SERVER_PORT);
res.write(
JSON.stringify({
name: 'node-http-proxy-json',
age: 1,
version: '1.0.0',
})
);
describe("modifyResponse--deflate", function () {
it('deflate: modify response json successfully', function (done) {
// Test server
http.get('http://localhost:' + SERVER_PORT, function (res) {
var body = '';
var inflate = zlib.Inflate();
res.pipe(inflate);
res.end();
})
.listen(TARGET_SERVER_PORT);
});
inflate.on('data', function (chunk) {
body += chunk;
}).on('end', function () {
assert.equal(JSON.stringify({name: 'node-http-proxy-json', age: 2}), body);
afterEach(() => {
proxy.close();
server.close();
targetServer.close();
});
proxy.close();
server.close();
targetServer.close();
describe('callback returns data', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return body;
});
});
});
it('deflate: modify response json successfully', done => {
// Test server
http.get('http://localhost:' + SERVER_PORT, res => {
let body = '';
let inflate = zlib.Inflate();
res.pipe(inflate);
done();
});
inflate
.on('data', function(chunk) {
body += chunk;
})
.on('end', function() {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
);
done();
});
});
});
});
describe('callback returns a promise', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return Promise.resolve(body);
});
});
});
it('deflate: modify response json successfully', done => {
// Test server
http.get('http://localhost:' + SERVER_PORT, res => {
let body = '';
let inflate = zlib.Inflate();
res.pipe(inflate);
inflate
.on('data', function(chunk) {
body += chunk;
})
.on('end', function() {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
);
done();
});
});
});
});
});

@@ -0,1 +1,2 @@

'use strict';
/**

@@ -5,85 +6,139 @@ * Test content-encoding for gzip

var assert = require('chai').assert;
const assert = require('chai').assert;
var zlib = require('zlib');
var http = require('http');
var httpProxy = require('http-proxy');
var modifyResponse = require('../');
const zlib = require('zlib');
const http = require('http');
const httpProxy = require('http-proxy');
const modifyResponse = require('../');
var SERVER_PORT = 5000;
var TARGET_SERVER_PORT = 5001;
const SERVER_PORT = 5000;
const TARGET_SERVER_PORT = 5001;
// Create a proxy server
var proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + TARGET_SERVER_PORT
});
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return body;
describe('modifyResponse--gzip', function() {
let proxy, server, targetServer;
beforeEach(() => {
// Create a proxy server
proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + TARGET_SERVER_PORT,
});
});
// Create your server and then proxies the request
var server = http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(SERVER_PORT);
// Create your server and then proxies the request
server = http
.createServer((req, res) => {
proxy.web(req, res);
})
.listen(SERVER_PORT);
// Create your target server
var targetServer = http.createServer(function (req, res) {
// Create your target server
targetServer = http
.createServer((req, res) => {
// Create gzipped content
let gzip = zlib.Gzip();
let _write = res.write;
let _end = res.end;
// Create gzipped content
var gzip = zlib.Gzip();
var _write = res.write;
var _end = res.end;
gzip.on('data', buf => _write.call(res, buf));
gzip.on('end', () => _end.call(res));
gzip.on('data', function (buf) {
_write.call(res, buf);
});
gzip.on('end', function () {
_end.call(res);
});
res.write = data => gzip.write(data);
res.end = () => gzip.end();
res.write = function (data) {
gzip.write(data);
};
res.end = function () {
gzip.end();
};
res.writeHead(200, {
'Content-Type': 'application/json',
'Content-Encoding': 'gzip',
});
res.writeHead(200, {'Content-Type': 'application/json', 'Content-Encoding': 'gzip'});
res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'}));
res.end();
}).listen(TARGET_SERVER_PORT);
res.write(
JSON.stringify({
name: 'node-http-proxy-json',
age: 1,
version: '1.0.0',
})
);
res.end();
})
.listen(TARGET_SERVER_PORT);
});
describe("modifyResponse--gzip", function () {
it('gzip: modify response json successfully', function (done) {
// Test server
http.get('http://localhost:' + SERVER_PORT, function (res) {
var body = '';
var gunzip = zlib.Gunzip();
res.pipe(gunzip);
afterEach(() => {
proxy.close();
server.close();
targetServer.close();
});
gunzip.on('data', function (chunk) {
body += chunk;
}).on('end', function () {
assert.equal(JSON.stringify({name: 'node-http-proxy-json', age: 2}), body);
describe('callback returns data', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return body;
});
});
});
proxy.close();
server.close();
targetServer.close();
it('gzip: modify response json successfully', done => {
// Test server
http.get('http://localhost:' + SERVER_PORT, res => {
let body = '';
let gunzip = zlib.Gunzip();
res.pipe(gunzip);
done();
});
gunzip
.on('data', chunk => {
body += chunk;
})
.on('end', () => {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
);
done();
});
});
});
});
describe('callback returns a promise', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return Promise.resolve(body);
});
});
});
});
it('gzip: modify response json successfully', done => {
// Test server
http.get('http://localhost:' + SERVER_PORT, res => {
let body = '';
let gunzip = zlib.Gunzip();
res.pipe(gunzip);
gunzip
.on('data', chunk => {
body += chunk;
})
.on('end', () => {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
);
done();
});
});
});
});
});

@@ -0,1 +1,3 @@

'use strict';
/**

@@ -5,59 +7,106 @@ * Test content-encoding for uncompressed

var assert = require('chai').assert;
const assert = require('chai').assert;
var http = require('http');
var httpProxy = require('http-proxy');
var modifyResponse = require('../');
const http = require('http');
const httpProxy = require('http-proxy');
const modifyResponse = require('../');
var SERVER_PORT = 5004;
var TARGET_SERVER_PORT = 5005;
const SERVER_PORT = 5004;
const TARGET_SERVER_PORT = 5005;
// Create a proxy server
var proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + TARGET_SERVER_PORT
});
describe('modifyResponse--uncompressed', function() {
let proxy, server, targetServer;
beforeEach(() => {
// Create a proxy server
proxy = httpProxy.createProxyServer({
target: 'http://localhost:' + TARGET_SERVER_PORT,
});
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function (proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], function (body) {
if (body) {
// Create your server and then proxies the request
server = http
.createServer((req, res) => proxy.web(req, res))
.listen(SERVER_PORT);
// Create your target server
targetServer = http
.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(
JSON.stringify({
name: 'node-http-proxy-json',
age: 1,
version: '1.0.0',
})
);
res.end();
})
.listen(TARGET_SERVER_PORT);
});
afterEach(() => {
proxy.close();
server.close();
targetServer.close();
});
describe('callback returns data', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', function(proxyRes, req, res) {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return body;
}
return body;
});
});
});
});
// Create your server and then proxies the request
var server = http.createServer(function (req, res) {
proxy.web(req, res);
}).listen(SERVER_PORT);
it('uncompressed: modify response json successfully', done => {
// Test server
http.get('http://localhost:' + SERVER_PORT, res => {
let body = '';
res.on('data', chunk => (body += chunk)).on('end', () => {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
);
done();
});
});
});
});
// Create your target server
var targetServer = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify({name: 'node-http-proxy-json', age: 1, version: '1.0.0'}));
res.end();
}).listen(TARGET_SERVER_PORT);
describe('callback returns a promise', () => {
beforeEach(() => {
// Listen for the `proxyRes` event on `proxy`.
proxy.on('proxyRes', (proxyRes, req, res) => {
modifyResponse(res, proxyRes.headers['content-encoding'], body => {
if (body) {
// modify some information
body.age = 2;
delete body.version;
}
return Promise.resolve(body);
});
});
});
describe("modifyResponse--uncompressed", function () {
it('uncompressed: modify response json successfully', function (done) {
// Test server
http.get('http://localhost:' + SERVER_PORT, function (res) {
var body = '';
res.on('data', function (chunk) {
body += chunk;
}).on('end', function () {
assert.equal(JSON.stringify({name: 'node-http-proxy-json', age: 2}), body);
it('uncompressed: modify response json successfully', done => {
// Test server
http.get('http://localhost:' + SERVER_PORT, res => {
let body = '';
res.on('data', chunk => (body += chunk)).on('end', () => {
assert.equal(
JSON.stringify({ name: 'node-http-proxy-json', age: 2 }),
body
);
proxy.close();
server.close();
targetServer.close();
done();
});
done();
});
});
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc