-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5faa4d1
commit 4e29466
Showing
6 changed files
with
94 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 4 additions & 69 deletions
73
src/lambda/handler-runner/in-process-runner/InProcessRunner.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/lambda/handler-runner/in-process-runner/clearModule.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { readdirSync } from 'node:fs' | ||
import { createRequire } from 'node:module' | ||
import { dirname, resolve } from 'node:path' | ||
|
||
const { keys } = Object | ||
|
||
const require = createRequire(import.meta.url) | ||
|
||
export default function clearModule(fP, opts) { | ||
const options = opts ?? {} | ||
let filePath = fP | ||
if (!require.cache[filePath]) { | ||
const dirName = dirname(filePath) | ||
for (const fn of readdirSync(dirName)) { | ||
const fullPath = resolve(dirName, fn) | ||
if ( | ||
fullPath.substr(0, filePath.length + 1) === `${filePath}.` && | ||
require.cache[fullPath] | ||
) { | ||
filePath = fullPath | ||
break | ||
} | ||
} | ||
} | ||
if (require.cache[filePath]) { | ||
// Remove file from parent cache | ||
if (require.cache[filePath].parent) { | ||
let i = require.cache[filePath].parent.children.length | ||
if (i) { | ||
do { | ||
i -= 1 | ||
if (require.cache[filePath].parent.children[i].id === filePath) { | ||
require.cache[filePath].parent.children.splice(i, 1) | ||
} | ||
} while (i) | ||
} | ||
} | ||
const cld = require.cache[filePath].children | ||
delete require.cache[filePath] | ||
for (const c of cld) { | ||
// Unload any non node_modules and non-binary children | ||
if ( | ||
!c.filename.match(/\/node_modules\//i) && | ||
!c.filename.match(/\.node$/i) | ||
) { | ||
clearModule(c.id, { ...options, cleanup: false }) | ||
} | ||
} | ||
if (opts.cleanup) { | ||
// Cleanup any node_modules that are orphans | ||
let cleanup = false | ||
do { | ||
cleanup = false | ||
for (const fn of keys(require.cache)) { | ||
if ( | ||
require.cache[fn] && | ||
require.cache[fn].id !== '.' && | ||
require.cache[fn].parent && | ||
require.cache[fn].parent.id !== '.' && | ||
!require.cache[require.cache[fn].parent.id] && | ||
!fn.match(/\/node_modules\//i) && | ||
!fn.match(/\.node$/i) | ||
) { | ||
delete require.cache[fn] | ||
cleanup = true | ||
} | ||
} | ||
} while (cleanup) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters