Skip to content

Commit a237865

Browse files
roggervalfDABH
andauthoredMar 24, 2024··
fix(http): allow passing maximumDepth to prevent big object being stringified (#2425)
* fix(http): allow passing maximumDepth option * test(http): add test case * chore: remove .only statement * Update lib/winston/transports/http.js * Update lib/winston/transports/http.js * Update lib/winston/transports/http.js --------- Co-authored-by: David Hyde <DABH@users.noreply.github.com>
1 parent b5eecf0 commit a237865

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed
 

‎lib/winston/transports/http.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +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');
14+
const { configure } = require('safe-stable-stringify');
1515

1616
/**
1717
* Transport for outputting to a json-rpc server.
@@ -35,6 +35,7 @@ module.exports = class Http extends TransportStream {
3535
this.port = options.port;
3636
this.auth = options.auth;
3737
this.path = options.path || '';
38+
this.maximumDepth = options.maximumDepth;
3839
this.agent = options.agent;
3940
this.headers = options.headers || {};
4041
this.headers['content-type'] = 'application/json';
@@ -253,6 +254,9 @@ module.exports = class Http extends TransportStream {
253254
req.on('response', res => (
254255
res.on('end', () => callback(null, res)).resume()
255256
));
257+
const jsonStringify = configure({
258+
...(this.maximumDepth && { maximumDepth: this.maximumDepth })
259+
});
256260
req.end(Buffer.from(jsonStringify(options, this.options.replacer), 'utf8'));
257261
}
258262
};

‎lib/winston/transports/index.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ declare namespace winston {
6565
batchInterval?: number;
6666
batchCount?: number;
6767
replacer?: (key: string, value: any) => any;
68+
maximumDepth?: number;
6869
}
6970

7071
interface HttpTransportInstance extends Transport {
7172
name: string;
7273
ssl: boolean;
7374
host: string;
75+
maximumDepth: number;
7476
port: number;
7577
auth?: { username?: string | undefined, password?: string | undefined, bearer?: string | undefined };
7678
path: string;

‎test/unit/winston/transports/http.test.js

+15
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,20 @@ describe('Http({ host, port, path })', function () {
163163

164164
httpTransport.log(circularLog, assumeError);
165165
});
166+
167+
it('should be able to handle options with circular structure when passing maximumDepth', function (done) {
168+
const httpTransport = new Http({
169+
host: host,
170+
maximumDepth: 5,
171+
port: server.address().port,
172+
path: 'log'
173+
})
174+
.on('error', assumeError)
175+
.on('logged', function () {
176+
onLogged(context, done);
177+
});
178+
179+
httpTransport.log(circularLog, assumeError);
180+
});
166181
});
167182
});

0 commit comments

Comments
 (0)
Please sign in to comment.