From aeda9625a6e50139d5bb42b79f6afc7c90f65257 Mon Sep 17 00:00:00 2001 From: Lam Wei Li Date: Thu, 14 Apr 2022 14:51:36 +0800 Subject: [PATCH] chore(dep): temporary fix for fs-extra issue (to be reverted when fs-extra patches it) --- lib/RollingFileWriteStream.js | 8 ++++++- lib/moveAndMaybeCompressFile.js | 8 ++++++- test/fs-extra-test.js | 42 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 test/fs-extra-test.js diff --git a/lib/RollingFileWriteStream.js b/lib/RollingFileWriteStream.js index 05dc40b..47b1232 100644 --- a/lib/RollingFileWriteStream.js +++ b/lib/RollingFileWriteStream.js @@ -1,5 +1,11 @@ const debug = require("debug")("streamroller:RollingFileWriteStream"); -const fs = require("fs-extra"); + +const realFs = require('fs'); +const current = realFs.realpath.native; +if (!realFs.realpath.native) realFs.realpath.native = realFs.realpath; +const fs = require('fs-extra'); +realFs.realpath.native = current; + const path = require("path"); const newNow = require("./now"); const format = require("date-format"); diff --git a/lib/moveAndMaybeCompressFile.js b/lib/moveAndMaybeCompressFile.js index dd2b103..497933b 100644 --- a/lib/moveAndMaybeCompressFile.js +++ b/lib/moveAndMaybeCompressFile.js @@ -1,5 +1,11 @@ const debug = require('debug')('streamroller:moveAndMaybeCompressFile'); -const fs = require('fs-extra'); + +const realFs = require('fs'); +const current = realFs.realpath.native; +if (!realFs.realpath.native) realFs.realpath.native = realFs.realpath; +let fs = require('fs-extra'); +realFs.realpath.native = current; + const zlib = require('zlib'); const _parseOption = function(rawOptions){ diff --git a/test/fs-extra-test.js b/test/fs-extra-test.js new file mode 100644 index 0000000..c98e9fd --- /dev/null +++ b/test/fs-extra-test.js @@ -0,0 +1,42 @@ +const should = require("should"); +const path = require("path"); + +describe("simulate fs monkey-patch", function() { + const fs = require("fs"); + const realpathNativeBackup = fs.realpath.native; + + beforeEach(function() { + // simulate fs monkey-patch + delete fs.realpath.native; + }); + + afterEach(function() { + // reset require.cache + delete require.cache[require.resolve("../lib/RollingFileWriteStream")]; + delete require.cache[require.resolve("../lib/moveAndMaybeCompressFile")]; + const fsePath = path.dirname(require.resolve("fs-extra")); + for (const entry in require.cache) { + if (entry.startsWith(fsePath)) { + delete require.cache[entry] + } + } + // reinstate fs.realpath.native + fs.realpath.native = realpathNativeBackup; + }); + + it("RollingFileWriteStream should not throw error", function() { + console.log(Object.keys(require.cache).length); + should.doesNotThrow(function() { + require("../lib/RollingFileWriteStream"); + }); + console.log(Object.keys(require.cache).length); + }); + + it("moveAndMaybeCompressFile should not throw error", function() { + console.log(Object.keys(require.cache).length); + should.doesNotThrow(function() { + require("../lib/moveAndMaybeCompressFile"); + }); + console.log(Object.keys(require.cache).length); + }); +}); \ No newline at end of file