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

chore: use safe stringify in http transport #2043

Merged
merged 5 commits into from
Jan 25, 2022
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
5 changes: 3 additions & 2 deletions lib/winston/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const http = require('http');
const https = require('https');
const { Stream } = require('readable-stream');
const TransportStream = require('winston-transport');
const jsonStringify = require('safe-stable-stringify');

/**
* Transport for outputting to a json-rpc server.
Expand Down Expand Up @@ -261,6 +262,6 @@ module.exports = class Http extends TransportStream {
req.on('response', res => (
res.on('end', () => callback(null, res)).resume()
));
req.end(Buffer.from(JSON.stringify(options), 'utf8'));
req.end(Buffer.from(jsonStringify(options), 'utf8'));
}
};
};
26 changes: 20 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
"stream"
],
"dependencies": {
"async": "^3.2.3",
"@dabh/diagnostics": "^2.0.2",
"async": "^3.2.3",
"is-stream": "^2.0.0",
"logform": "^2.3.2",
"one-time": "^1.0.0",
"readable-stream": "^3.4.0",
"safe-stable-stringify": "^2.3.1",
"stack-trace": "0.0.x",
"triple-beam": "^1.3.0",
"winston-transport": "^4.4.2"
Expand Down
31 changes: 30 additions & 1 deletion test/transports/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const http = require('http');
const hock = require('hock');
const assume = require('assume');
const Http = require('../../lib/winston/transports/http');
const stringifyJson = require('safe-stable-stringify');

const host = '127.0.0.1';
const port = 0;
Expand Down Expand Up @@ -134,4 +135,32 @@ describe('Http({ host, port, path })', function () {

});

});
describe('circular structure', function () {
const circularLog = {
level: 'error',
message: 'hello',
meta: {}
};

circularLog.self = circularLog;

beforeEach(function (done) {
context = mockHttpServer(done, stringifyJson(circularLog));
server = context.server;
});

it('should be able to handle options with circular structure', function (done) {
const httpTransport = new Http({
host: host,
port: server.address().port,
path: 'log'
})
.on('error', assumeError)
.on('logged', function () {
onLogged(context, done);
});

httpTransport.log(circularLog, assumeError);
});
});
});