-
-
Notifications
You must be signed in to change notification settings - Fork 794
/
HandlerRunner.js
148 lines (122 loc) · 3.61 KB
/
HandlerRunner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
this._funOptions = funOptions
this._options = options
this._runner = null
}
async _loadRunner() {
const {
skipCacheInvalidation,
useChildProcesses,
useWorkerThreads,
} = this._options
if (useWorkerThreads) {
// worker threads
this._verifyWorkerThreadCompatibility()
const { default: WorkerThreadRunner } = await import(
'./WorkerThreadRunner.js'
)
return new WorkerThreadRunner(
this._funOptions /* skipCacheInvalidation */,
this._env,
)
}
if (useChildProcesses) {
const { default: ChildProcessRunner } = await import(
'./ChildProcessRunner.js'
)
return new ChildProcessRunner(
this._funOptions,
this._env,
skipCacheInvalidation,
)
}
this._cacheInvalidation()
const {
functionName,
handlerName,
handlerPath,
runtime,
timeout,
} = this._funOptions
debugLog(`Loading handler... (${handlerPath})`)
if (runtime.startsWith('nodejs')) {
const { default: InProcessRunner } = await import('./InProcessRunner.js')
return new InProcessRunner(
functionName,
handlerPath,
handlerName,
this._env,
timeout,
)
}
const { default: InvokeLocalRunner } = await import(
'./InvokeLocalRunner.js'
)
return new InvokeLocalRunner(this._funOptions, this._env)
}
_verifyWorkerThreadCompatibility() {
const { node: currentVersion } = process.versions
const requiredVersionRange = '>=11.7.0'
const versionIsSatisfied = satisfiesVersionRange(
currentVersion,
requiredVersionRange,
)
// we're happy
if (!versionIsSatisfied) {
serverlessLog(
`"worker threads" require node.js version ${requiredVersionRange}, but found version ${currentVersion}.
To use this feature you have to update node.js to a later version.
`,
'serverless-offline',
{ color: 'red' },
)
throw new Error(
'"worker threads" are not supported with this node.js version',
)
}
}
_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')
return this._runner.cleanup()
}
async run(event, context, callback) {
if (this._runner == null) {
this._runner = await this._loadRunner()
}
return this._runner.run(event, context, callback)
}
}