From 72d4db020dff859567a0b6517ab8bba084fdacb7 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sun, 1 Sep 2019 21:24:48 -0400 Subject: [PATCH] Remove require cache deletion and --skipCacheInvalidation option, fixes #766, fixes #798, and plenty others --- README.md | 1 - src/config/defaults.js | 1 - src/config/options.js | 5 -- .../handler-runner/ChildProcessRunner.js | 5 +- src/lambda/handler-runner/HandlerRunner.js | 56 +------------------ 5 files changed, 4 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 37f9cc3c3..610abb74c 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,6 @@ All CLI options are optional: --port -P Port to listen on. Default: 3000 --printOutput Turns on logging of your lambda outputs in the terminal. --resourceRoutes Turns on loading of your HTTP proxy settings from serverless.yml ---skipCacheInvalidation -c Tells the plugin to skip require cache invalidation. A script reloading tool like Nodemon might then be needed --useChildProcess Run handlers in a child process --useWorkerThreads Uses worker threads for handlers. Requires node.js v11.7.0 or higher --websocketPort WebSocket port to listen on. Default: 3001 diff --git a/src/config/defaults.js b/src/config/defaults.js index 44c8297a4..1fd9fea91 100644 --- a/src/config/defaults.js +++ b/src/config/defaults.js @@ -14,7 +14,6 @@ export default { port: 3000, printOutput: false, resourceRoutes: false, - skipCacheInvalidation: false, useChildProcess: false, useWorkerThreads: false, websocketPort: 3001, diff --git a/src/config/options.js b/src/config/options.js index 4b139c2c5..5cb878f4e 100644 --- a/src/config/options.js +++ b/src/config/options.js @@ -58,11 +58,6 @@ export default { resourceRoutes: { usage: 'Turns on loading of your HTTP proxy settings from serverless.yml.', }, - skipCacheInvalidation: { - shortcut: 'c', - usage: - 'Tells the plugin to skip require cache invalidation. A script reloading tool like Nodemon might then be needed', - }, useChildProcess: { usage: 'Uses separate node processes for handlers', }, diff --git a/src/lambda/handler-runner/ChildProcessRunner.js b/src/lambda/handler-runner/ChildProcessRunner.js index 83cd3f2a0..6c3d7e984 100644 --- a/src/lambda/handler-runner/ChildProcessRunner.js +++ b/src/lambda/handler-runner/ChildProcessRunner.js @@ -4,7 +4,7 @@ import { node } from 'execa' const childProcessHelperPath = resolve(__dirname, 'childProcessHelper.js') export default class ChildProcessRunner { - constructor(funOptions, env, skipCacheInvalidation) { + constructor(funOptions, env) { const { functionName, handlerName, handlerPath, timeout } = funOptions this._env = env @@ -12,9 +12,6 @@ export default class ChildProcessRunner { this._handlerName = handlerName this._handlerPath = handlerPath this._timeout = timeout - - // TODO - this._skipCacheInvalidation = skipCacheInvalidation } // no-op diff --git a/src/lambda/handler-runner/HandlerRunner.js b/src/lambda/handler-runner/HandlerRunner.js index 27665c11a..cd5dd60e9 100644 --- a/src/lambda/handler-runner/HandlerRunner.js +++ b/src/lambda/handler-runner/HandlerRunner.js @@ -2,8 +2,6 @@ import debugLog from '../../debugLog.js' import serverlessLog from '../../serverlessLog.js' import { satisfiesVersionRange } from '../../utils/index.js' -const { keys } = Object - export default class HandlerRunner { constructor(funOptions, options, env) { this._env = env @@ -13,11 +11,7 @@ export default class HandlerRunner { } async _loadRunner() { - const { - skipCacheInvalidation, - useChildProcess, - useWorkerThreads, - } = this._options + const { useChildProcess, useWorkerThreads } = this._options if (useWorkerThreads) { // worker threads @@ -26,25 +20,16 @@ export default class HandlerRunner { const { default: WorkerThreadRunner } = await import( './WorkerThreadRunner.js' ) - return new WorkerThreadRunner( - this._funOptions /* skipCacheInvalidation */, - this._env, - ) + return new WorkerThreadRunner(this._funOptions, this._env) } if (useChildProcess) { const { default: ChildProcessRunner } = await import( './ChildProcessRunner.js' ) - return new ChildProcessRunner( - this._funOptions, - this._env, - skipCacheInvalidation, - ) + return new ChildProcessRunner(this._funOptions, this._env) } - this._cacheInvalidation() - const { functionName, handlerName, @@ -97,41 +82,6 @@ export default class HandlerRunner { } } - _cacheInvalidation() { - const { cacheInvalidationRegex, skipCacheInvalidation } = this._options - - if (!skipCacheInvalidation) { - debugLog('Invalidating cache...') - - const regExp = new RegExp(cacheInvalidationRegex) - - keys(require.cache).forEach((key) => { - // Require cache invalidation, brutal and fragile. - // Might cause errors, if so please submit an issue. - if (!key.match(regExp)) { - delete require.cache[key] - } - }) - - const currentFilePath = __filename - - if ( - require.cache[currentFilePath] && - require.cache[currentFilePath].children - ) { - const nextChildren = [] - - require.cache[currentFilePath].children.forEach((moduleCache) => { - if (moduleCache.filename.match(regExp)) { - nextChildren.push(moduleCache) - } - }) - - require.cache[currentFilePath].children = nextChildren - } - } - } - // () => Promise cleanup() { // TODO console.log('handler runner cleanup')