From a2378657fc52eabc4a177bf4f02cf38a388de579 Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Sun, 24 Mar 2024 15:23:36 -0600 Subject: [PATCH] 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 --- lib/winston/transports/http.js | 6 +++++- lib/winston/transports/index.d.ts | 2 ++ test/unit/winston/transports/http.test.js | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index 0653d78bc..17f661af4 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -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. @@ -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'; @@ -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')); } }; diff --git a/lib/winston/transports/index.d.ts b/lib/winston/transports/index.d.ts index c3eb45045..2eb7f1122 100644 --- a/lib/winston/transports/index.d.ts +++ b/lib/winston/transports/index.d.ts @@ -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; diff --git a/test/unit/winston/transports/http.test.js b/test/unit/winston/transports/http.test.js index f70011c7a..5edbd6d02 100644 --- a/test/unit/winston/transports/http.test.js +++ b/test/unit/winston/transports/http.test.js @@ -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); + }); }); });