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 3 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
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 || 10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't change the current default behavior though -- looks like default is Infinity per https://github.com/BridgeAR/safe-stable-stringify/blob/46f24495221803b7a945883cb8fb262b14f1ea52/index.js#L125 so I think that is what we should use here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Can't quite use Infinity due to the checks safe-stable-stringify has in place -- try using ES6 spreading to conditionally set the prop in the config object if it's passed in as an option)

DABH marked this conversation as resolved.
Show resolved Hide resolved
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({
maximumDepth: this.maximumDepth
DABH marked this conversation as resolved.
Show resolved Hide resolved
});
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);
});
});
});
Loading