@@ -35,16 +35,26 @@ function requireUncached(module: string): any {
35
35
return require ( module ) ;
36
36
}
37
37
38
- function loadTwilioFunction (
39
- fnPath : string ,
40
- config : StartCliConfig
41
- ) : ServerlessFunctionSignature {
42
- if ( config . live ) {
43
- debug ( 'Uncached loading of %s' , fnPath ) ;
44
- return requireUncached ( fnPath ) . handler ;
45
- } else {
46
- return require ( fnPath ) . handler ;
47
- }
38
+ function loadTwilioFunction ( fnPath : string ) : ServerlessFunctionSignature {
39
+ return require ( fnPath ) . handler ;
40
+ }
41
+
42
+ function requireCacheCleaner (
43
+ req : ExpressRequest ,
44
+ res : ExpressResponse ,
45
+ next : NextFunction
46
+ ) {
47
+ debug ( 'Deleting require cache' ) ;
48
+ Object . keys ( require . cache ) . forEach ( key => {
49
+ // Entries in the cache that end with .node are compiled binaries, deleting
50
+ // those has unspecified results, so we keep them.
51
+ // Entries in the cache that include "twilio-run" are part of this module
52
+ // or its dependencies, so don't need to be cleared.
53
+ if ( ! ( key . endsWith ( '.node' ) || key . includes ( 'twilio-run' ) ) ) {
54
+ delete require . cache [ key ] ;
55
+ }
56
+ } ) ;
57
+ next ( ) ;
48
58
}
49
59
50
60
export async function createServer (
@@ -77,6 +87,7 @@ export async function createServer(
77
87
78
88
if ( config . live ) {
79
89
app . use ( nocache ( ) ) ;
90
+ app . use ( requireCacheCleaner ) ;
80
91
}
81
92
82
93
if ( config . legacyMode ) {
@@ -160,7 +171,7 @@ export async function createServer(
160
171
}
161
172
162
173
debug ( 'Load & route to function at "%s"' , functionPath ) ;
163
- const twilioFunction = loadTwilioFunction ( functionPath , config ) ;
174
+ const twilioFunction = loadTwilioFunction ( functionPath ) ;
164
175
if ( typeof twilioFunction !== 'function' ) {
165
176
return res
166
177
. status ( 404 )
0 commit comments