From 80fe04b8a4a79f4b4160c4767520b9dc80a003a7 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Fri, 8 Mar 2024 20:38:14 -0500 Subject: [PATCH 1/6] fix(http): allow passing maximumDepth option --- lib/winston/transports/http.js | 6 +++++- lib/winston/transports/index.d.ts | 2 ++ test/unit/winston/transports/http.test.js | 5 +++-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index 0653d78bc..8b7962514 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 || 1; 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({ + 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..19b9fbd96 100644 --- a/test/unit/winston/transports/http.test.js +++ b/test/unit/winston/transports/http.test.js @@ -32,7 +32,8 @@ function mockHttpServer(done, expectedLog) { return { server, mock }; } -function assumeError(err) { +function assumeError(err, hello) { + console.log(err, hello) if (err) { assume(err).falsy(); } @@ -150,7 +151,7 @@ describe('Http({ host, port, path })', function () { server = context.server; }); - it('should be able to handle options with circular structure', function (done) { + it.only('should be able to handle options with circular structure', function (done) { const httpTransport = new Http({ host: host, port: server.address().port, From 13e766205b858424f9ae005a432101aee399c82a Mon Sep 17 00:00:00 2001 From: roggervalf Date: Fri, 8 Mar 2024 20:41:43 -0500 Subject: [PATCH 2/6] test(http): add test case --- lib/winston/transports/http.js | 2 +- test/unit/winston/transports/http.test.js | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index 8b7962514..cecd12d49 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -35,7 +35,7 @@ module.exports = class Http extends TransportStream { this.port = options.port; this.auth = options.auth; this.path = options.path || ''; - this.maximumDepth = options.maximumDepth || 1; + this.maximumDepth = options.maximumDepth || 10; this.agent = options.agent; this.headers = options.headers || {}; this.headers['content-type'] = 'application/json'; diff --git a/test/unit/winston/transports/http.test.js b/test/unit/winston/transports/http.test.js index 19b9fbd96..0b770fbbd 100644 --- a/test/unit/winston/transports/http.test.js +++ b/test/unit/winston/transports/http.test.js @@ -32,8 +32,7 @@ function mockHttpServer(done, expectedLog) { return { server, mock }; } -function assumeError(err, hello) { - console.log(err, hello) +function assumeError(err) { if (err) { assume(err).falsy(); } @@ -137,7 +136,7 @@ describe('Http({ host, port, path })', function () { }); - describe('circular structure', function () { + describe.only('circular structure', function () { const circularLog = { level: 'error', message: 'hello', @@ -151,7 +150,7 @@ describe('Http({ host, port, path })', function () { server = context.server; }); - it.only('should be able to handle options with circular structure', function (done) { + it('should be able to handle options with circular structure', function (done) { const httpTransport = new Http({ host: host, port: server.address().port, @@ -164,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); + }); }); }); From a8f91382b1e37fbcdf69ddbf577f8f319cf0dea6 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Fri, 8 Mar 2024 20:43:24 -0500 Subject: [PATCH 3/6] chore: remove .only statement --- test/unit/winston/transports/http.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/winston/transports/http.test.js b/test/unit/winston/transports/http.test.js index 0b770fbbd..5edbd6d02 100644 --- a/test/unit/winston/transports/http.test.js +++ b/test/unit/winston/transports/http.test.js @@ -136,7 +136,7 @@ describe('Http({ host, port, path })', function () { }); - describe.only('circular structure', function () { + describe('circular structure', function () { const circularLog = { level: 'error', message: 'hello', From f277edfa3edfffa682de29800b717352290f2927 Mon Sep 17 00:00:00 2001 From: David Hyde Date: Sun, 24 Mar 2024 16:10:58 -0500 Subject: [PATCH 4/6] Update lib/winston/transports/http.js --- lib/winston/transports/http.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index cecd12d49..430fc06a7 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -35,7 +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; + this.maximumDepth = options.maximumDepth || Infinity; this.agent = options.agent; this.headers = options.headers || {}; this.headers['content-type'] = 'application/json'; From 3cd460856d322bda620e5448fff1ac80fb79d2ce Mon Sep 17 00:00:00 2001 From: David Hyde Date: Sun, 24 Mar 2024 16:20:59 -0500 Subject: [PATCH 5/6] Update lib/winston/transports/http.js --- lib/winston/transports/http.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index 430fc06a7..9018a8049 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -35,7 +35,7 @@ module.exports = class Http extends TransportStream { this.port = options.port; this.auth = options.auth; this.path = options.path || ''; - this.maximumDepth = options.maximumDepth || Infinity; + this.maximumDepth = options.maximumDepth; this.agent = options.agent; this.headers = options.headers || {}; this.headers['content-type'] = 'application/json'; From a1ad58072a08e41b56439909e053c9c4c5e5e0fb Mon Sep 17 00:00:00 2001 From: David Hyde Date: Sun, 24 Mar 2024 16:21:05 -0500 Subject: [PATCH 6/6] Update lib/winston/transports/http.js --- lib/winston/transports/http.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index 9018a8049..17f661af4 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -255,7 +255,7 @@ module.exports = class Http extends TransportStream { res.on('end', () => callback(null, res)).resume() )); const jsonStringify = configure({ - maximumDepth: this.maximumDepth + ...(this.maximumDepth && { maximumDepth: this.maximumDepth }) }); req.end(Buffer.from(jsonStringify(options, this.options.replacer), 'utf8')); }