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

fix(http): allow passing maximumDepth to prevent big object being stringified #2425

Merged
merged 6 commits into from
Mar 24, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/winston/transports/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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');
const { configure } = require('safe-stable-stringify');

/**
* Transport for outputting to a json-rpc server.
Expand All @@ -35,6 +35,7 @@ module.exports = class Http extends TransportStream {
this.port = options.port;
this.auth = options.auth;
this.path = options.path || '';
this.maximumDepth = options.maximumDepth;
this.agent = options.agent;
this.headers = options.headers || {};
this.headers['content-type'] = 'application/json';
Expand Down Expand Up @@ -253,6 +254,9 @@ module.exports = class Http extends TransportStream {
req.on('response', res => (
res.on('end', () => callback(null, res)).resume()
));
const jsonStringify = configure({
...(this.maximumDepth && { maximumDepth: this.maximumDepth })
});
req.end(Buffer.from(jsonStringify(options, this.options.replacer), 'utf8'));
}
};
2 changes: 2 additions & 0 deletions lib/winston/transports/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ declare namespace winston {
batchInterval?: number;
batchCount?: number;
replacer?: (key: string, value: any) => any;
maximumDepth?: number;
}

interface HttpTransportInstance extends Transport {
name: string;
ssl: boolean;
host: string;
maximumDepth: number;
port: number;
auth?: { username?: string | undefined, password?: string | undefined, bearer?: string | undefined };
path: string;
Expand Down
15 changes: 15 additions & 0 deletions test/unit/winston/transports/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,20 @@ describe('Http({ host, port, path })', function () {

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

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

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