Skip to content

Commit

Permalink
chore: use safe stringify in http transport (#2043)
Browse files Browse the repository at this point in the history
* chore: use safe stringify in http transport

* chore: review feedback, use safe-stable-stringify lib for consistency with winstonjs/logform

* restored package-lock from master

* Re-run npm isntall to fix package-lock
  • Loading branch information
karlwir authored Jan 25, 2022
1 parent 8a1735b commit d18198d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
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);
});
});
});

0 comments on commit d18198d

Please sign in to comment.