This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.ts
200 lines (176 loc) · 5.5 KB
/
index.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { extname, join } from 'path'
import { copyFile } from 'cp-file'
import { INVOCATION_MODE } from '../../function.js'
import { Priority } from '../../priority.js'
import getInternalValue from '../../utils/get_internal_value.js'
import { GetSrcFilesFunction, Runtime, RUNTIME, ZipFunction } from '../runtime.js'
import { getBundler, getBundlerName } from './bundlers/index.js'
import { NODE_BUNDLER } from './bundlers/types.js'
import { findFunctionsInPaths, findFunctionInPath } from './finder.js'
import { parseFile } from './in_source_config/index.js'
import { MODULE_FORMAT, MODULE_FILE_EXTENSION } from './utils/module_format.js'
import { getNodeRuntime, getNodeRuntimeForV2 } from './utils/node_runtime.js'
import { createAliases as createPluginsModulesPathAliases, getPluginsModulesPath } from './utils/plugin_modules_path.js'
import { zipNodeJs } from './utils/zip.js'
// A proxy for the `getSrcFiles` that calls `getSrcFiles` on the bundler
const getSrcFilesWithBundler: GetSrcFilesFunction = async (parameters) => {
const { config, extension, featureFlags, mainFile, runtimeAPIVersion, srcDir } = parameters
const pluginsModulesPath = await getPluginsModulesPath(srcDir)
const bundlerName = await getBundlerName({
config,
extension,
featureFlags,
mainFile,
runtimeAPIVersion,
})
const bundler = getBundler(bundlerName)
const result = await bundler.getSrcFiles({ ...parameters, pluginsModulesPath })
return result.srcFiles
}
const zipFunction: ZipFunction = async function ({
archiveFormat,
basePath,
cache,
config = {},
destFolder,
extension,
featureFlags,
filename,
isInternal,
logger,
mainFile,
name,
repositoryRoot,
runtime,
srcDir,
srcPath,
stat,
}) {
// If the file is a zip, we assume the function is bundled and ready to go.
// We simply copy it to the destination path with no further processing.
if (extension === '.zip') {
const destPath = join(destFolder, filename)
await copyFile(srcPath, destPath)
return { config, path: destPath, entryFilename: '' }
}
const staticAnalysisResult = await parseFile(mainFile, { functionName: name })
const runtimeAPIVersion = staticAnalysisResult.runtimeAPIVersion === 2 ? 2 : 1
const pluginsModulesPath = await getPluginsModulesPath(srcDir)
const bundlerName = await getBundlerName({
config,
extension,
featureFlags,
mainFile,
runtimeAPIVersion,
})
const bundler = getBundler(bundlerName)
const {
aliases = new Map(),
cleanupFunction,
basePath: finalBasePath,
bundlerWarnings,
includedFiles,
inputs,
mainFile: finalMainFile = mainFile,
moduleFormat,
nativeNodeModules,
rewrites = new Map(),
srcFiles,
} = await bundler.bundle({
basePath,
cache,
config,
extension,
featureFlags,
filename,
logger,
mainFile,
name,
pluginsModulesPath,
repositoryRoot,
runtime,
runtimeAPIVersion,
srcDir,
srcPath,
stat,
})
createPluginsModulesPathAliases(srcFiles, pluginsModulesPath, aliases, finalBasePath)
const zipPath = await zipNodeJs({
aliases,
archiveFormat,
basePath: finalBasePath,
cache,
destFolder,
extension,
featureFlags,
filename,
mainFile: finalMainFile,
moduleFormat,
name,
repositoryRoot,
rewrites,
runtimeAPIVersion,
srcFiles,
})
await cleanupFunction?.()
// Getting the invocation mode from ISC, in case the function is using the
// `stream` helper.
let { invocationMode } = staticAnalysisResult
// If we're using the V2 API, force the invocation to "stream".
if (runtimeAPIVersion === 2) {
invocationMode = INVOCATION_MODE.Stream
}
// If this is a background function, set the right `invocationMode` value.
if (name.endsWith('-background')) {
invocationMode = INVOCATION_MODE.Background
}
const { trafficRules } = staticAnalysisResult
const outputModuleFormat =
extname(finalMainFile) === MODULE_FILE_EXTENSION.MJS ? MODULE_FORMAT.ESM : MODULE_FORMAT.COMMONJS
const priority = isInternal ? Priority.GeneratedFunction : Priority.UserFunction
return {
bundler: bundlerName,
bundlerWarnings,
config,
displayName: config?.name,
entryFilename: zipPath.entryFilename,
generator: config?.generator || getInternalValue(isInternal),
inputs,
includedFiles,
staticAnalysisResult,
invocationMode,
outputModuleFormat,
nativeNodeModules,
path: zipPath.path,
priority,
trafficRules,
runtimeVersion:
runtimeAPIVersion === 2 ? getNodeRuntimeForV2(config.nodeVersion) : getNodeRuntime(config.nodeVersion),
}
}
const zipWithFunctionWithFallback: ZipFunction = async ({ config = {}, ...parameters }) => {
// If a specific JS bundler version is specified, we'll use it.
if (config.nodeBundler !== NODE_BUNDLER.ESBUILD_ZISI) {
return zipFunction({ ...parameters, config })
}
// Otherwise, we'll try to bundle with esbuild and, if that fails, fallback
// to zisi.
try {
return await zipFunction({ ...parameters, config: { ...config, nodeBundler: NODE_BUNDLER.ESBUILD } })
} catch (esbuildError) {
try {
const data = await zipFunction({ ...parameters, config: { ...config, nodeBundler: NODE_BUNDLER.ZISI } })
return { ...data, bundlerErrors: esbuildError.errors }
} catch {
throw esbuildError
}
}
}
const runtime: Runtime = {
findFunctionsInPaths,
findFunctionInPath,
getSrcFiles: getSrcFilesWithBundler,
name: RUNTIME.JAVASCRIPT,
zipFunction: zipWithFunctionWithFallback,
}
export default runtime