Skip to content

Commit

Permalink
fix(http): allow passing maximumDepth to prevent big object being str…
Browse files Browse the repository at this point in the history
…ingified (#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>
  • Loading branch information
roggervalf and DABH authored Mar 24, 2024
1 parent b5eecf0 commit a237865
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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);
});
});
});

0 comments on commit a237865

Please sign in to comment.