-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
HandlerRunner.js
98 lines (82 loc) · 2.47 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
import debugLog from '../../debugLog.js'
import serverlessLog from '../../serverlessLog.js'
import { satisfiesVersionRange } from '../../utils/index.js'
export default class HandlerRunner {
constructor(funOptions, options, env) {
this._env = env
this._funOptions = funOptions
this._options = options
this._runner = null
}
async _loadRunner() {
const { useChildProcess, useWorkerThreads } = this._options
if (useWorkerThreads) {
// worker threads
this._verifyWorkerThreadCompatibility()
const { default: WorkerThreadRunner } = await import(
'./WorkerThreadRunner.js'
)
return new WorkerThreadRunner(this._funOptions, this._env)
}
if (useChildProcess) {
const { default: ChildProcessRunner } = await import(
'./ChildProcessRunner.js'
)
return new ChildProcessRunner(this._funOptions, this._env)
}
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',
)
}
}
// () => 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)
}
}