From f2bbe783a3569ce52edfb002bc2f44d81f3afee5 Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Mon, 22 Mar 2021 09:55:56 -0400 Subject: [PATCH 1/2] Fixes for Webpack 5 browser environments Webpack 5 no longer automatically provides polyfills for Node APIs from browser environments. (See "Automatic polyfills for native Node.js modules were removed" under the [Webpack 5 release announcement][1].) This causes problems for Bunyan's use of Node.js-only globals like `process` and `Buffer`. As I understand it, one solution is to do an explicit `require` and, if needed, [configure Webpack's resolve.fallback option][2] to specify how to resolve the `require`d modules. (Bunyan does something similar to this for the `os` and `fs` modules.) The other solution is to check whether the modules are available before continuing. I opted for the second solution in this PR, to avoid introducing a dependency on the [buffer][3] module. This means that Bunyan won't directly support Buffer objects in the browser. There are two references to `process` that I didn't update: `process.env` check and a reference to `process.stderr.write`. `process.env` is specially handled by Node.js, and `process.stderr` is addressed by #628. [1]: https://webpack.js.org/blog/2020-10-10-webpack-5-release/ [2]: https://stackoverflow.com/q/64557638/25507 [3]: https://www.npmjs.com/package/buffer --- lib/bunyan.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/bunyan.js b/lib/bunyan.js index 405cdb24..782f96e0 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -518,7 +518,7 @@ function Logger(options, _childOptions, _childSimple) { if (!fields.hostname && !self.fields.hostname) { fields.hostname = os.hostname(); } - if (!fields.pid) { + if (!fields.pid && runtimeEnv !== 'browser') { fields.pid = process.pid; } Object.keys(fields).forEach(function (k) { @@ -950,7 +950,7 @@ function mkRecord(log, minLevel, args) { // `log.(msg, ...)` fields = null; msgArgs = args.slice(); - } else if (Buffer.isBuffer(args[0])) { // `log.(buf, ...)` + } else if (typeof (Buffer) !== 'undefined' && Buffer.isBuffer(args[0])) { // `log.(buf, ...)` // Almost certainly an error, show `inspect(buf)`. See bunyan // issue #35. fields = null; From cfcd584c02899e2e59911c962f13dc107e49dc1c Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Mon, 22 Mar 2021 10:17:24 -0400 Subject: [PATCH 2/2] Fix coding style --- lib/bunyan.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/bunyan.js b/lib/bunyan.js index 782f96e0..ebe7e0f7 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -950,7 +950,8 @@ function mkRecord(log, minLevel, args) { // `log.(msg, ...)` fields = null; msgArgs = args.slice(); - } else if (typeof (Buffer) !== 'undefined' && Buffer.isBuffer(args[0])) { // `log.(buf, ...)` + } else if (typeof (Buffer) !== 'undefined' && Buffer.isBuffer(args[0])) { + // `log.(buf, ...)` // Almost certainly an error, show `inspect(buf)`. See bunyan // issue #35. fields = null;