Skip to content

Commit

Permalink
fix(twilio-run:runtime): delete from require cache on page load in li…
Browse files Browse the repository at this point in the history
…ve (#181)
  • Loading branch information
philnash authored Aug 31, 2020
1 parent 1ca1d75 commit 5108d30
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions packages/twilio-run/src/runtime/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,26 @@ function requireUncached(module: string): any {
return require(module);
}

function loadTwilioFunction(
fnPath: string,
config: StartCliConfig
): ServerlessFunctionSignature {
if (config.live) {
debug('Uncached loading of %s', fnPath);
return requireUncached(fnPath).handler;
} else {
return require(fnPath).handler;
}
function loadTwilioFunction(fnPath: string): ServerlessFunctionSignature {
return require(fnPath).handler;
}

function requireCacheCleaner(
req: ExpressRequest,
res: ExpressResponse,
next: NextFunction
) {
debug('Deleting require cache');
Object.keys(require.cache).forEach(key => {
// Entries in the cache that end with .node are compiled binaries, deleting
// those has unspecified results, so we keep them.
// Entries in the cache that include "twilio-run" are part of this module
// or its dependencies, so don't need to be cleared.
if (!(key.endsWith('.node') || key.includes('twilio-run'))) {
delete require.cache[key];
}
});
next();
}

export async function createServer(
Expand Down Expand Up @@ -77,6 +87,7 @@ export async function createServer(

if (config.live) {
app.use(nocache());
app.use(requireCacheCleaner);
}

if (config.legacyMode) {
Expand Down Expand Up @@ -160,7 +171,7 @@ export async function createServer(
}

debug('Load & route to function at "%s"', functionPath);
const twilioFunction = loadTwilioFunction(functionPath, config);
const twilioFunction = loadTwilioFunction(functionPath);
if (typeof twilioFunction !== 'function') {
return res
.status(404)
Expand Down

0 comments on commit 5108d30

Please sign in to comment.