Skip to content

Commit d18198d

Browse files
authored
chore: use safe stringify in http transport (#2043)
* 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
1 parent 8a1735b commit d18198d

File tree

4 files changed

+55
-10
lines changed

4 files changed

+55
-10
lines changed

lib/winston/transports/http.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const http = require('http');
1111
const https = require('https');
1212
const { Stream } = require('readable-stream');
1313
const TransportStream = require('winston-transport');
14+
const jsonStringify = require('safe-stable-stringify');
1415

1516
/**
1617
* Transport for outputting to a json-rpc server.
@@ -261,6 +262,6 @@ module.exports = class Http extends TransportStream {
261262
req.on('response', res => (
262263
res.on('end', () => callback(null, res)).resume()
263264
));
264-
req.end(Buffer.from(JSON.stringify(options), 'utf8'));
265+
req.end(Buffer.from(jsonStringify(options), 'utf8'));
265266
}
266-
};
267+
};

package-lock.json

Lines changed: 20 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
"stream"
2525
],
2626
"dependencies": {
27-
"async": "^3.2.3",
2827
"@dabh/diagnostics": "^2.0.2",
28+
"async": "^3.2.3",
2929
"is-stream": "^2.0.0",
3030
"logform": "^2.3.2",
3131
"one-time": "^1.0.0",
3232
"readable-stream": "^3.4.0",
33+
"safe-stable-stringify": "^2.3.1",
3334
"stack-trace": "0.0.x",
3435
"triple-beam": "^1.3.0",
3536
"winston-transport": "^4.4.2"

test/transports/http.test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const http = require('http');
88
const hock = require('hock');
99
const assume = require('assume');
1010
const Http = require('../../lib/winston/transports/http');
11+
const stringifyJson = require('safe-stable-stringify');
1112

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

135136
});
136137

137-
});
138+
describe('circular structure', function () {
139+
const circularLog = {
140+
level: 'error',
141+
message: 'hello',
142+
meta: {}
143+
};
144+
145+
circularLog.self = circularLog;
146+
147+
beforeEach(function (done) {
148+
context = mockHttpServer(done, stringifyJson(circularLog));
149+
server = context.server;
150+
});
151+
152+
it('should be able to handle options with circular structure', function (done) {
153+
const httpTransport = new Http({
154+
host: host,
155+
port: server.address().port,
156+
path: 'log'
157+
})
158+
.on('error', assumeError)
159+
.on('logged', function () {
160+
onLogged(context, done);
161+
});
162+
163+
httpTransport.log(circularLog, assumeError);
164+
});
165+
});
166+
});

0 commit comments

Comments
 (0)