Skip to content

Commit

Permalink
Remove require cache deletion and --skipCacheInvalidation option, fixes
Browse files Browse the repository at this point in the history
#766, fixes #798, and plenty others
  • Loading branch information
dnalborczyk committed Sep 2, 2019
1 parent a3cd25a commit 72d4db0
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 64 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default {
port: 3000,
printOutput: false,
resourceRoutes: false,
skipCacheInvalidation: false,
useChildProcess: false,
useWorkerThreads: false,
websocketPort: 3001,
Expand Down
5 changes: 0 additions & 5 deletions src/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
5 changes: 1 addition & 4 deletions src/lambda/handler-runner/ChildProcessRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ 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
this._functionName = functionName
this._handlerName = handlerName
this._handlerPath = handlerPath
this._timeout = timeout

// TODO
this._skipCacheInvalidation = skipCacheInvalidation
}

// no-op
Expand Down
56 changes: 3 additions & 53 deletions src/lambda/handler-runner/HandlerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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<void>
cleanup() {
// TODO console.log('handler runner cleanup')
Expand Down

0 comments on commit 72d4db0

Please sign in to comment.