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
171 lines (145 loc) · 4.39 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
import type { Stats } from 'fs'
import { join, extname, dirname, basename } from 'path'
import { FeatureFlags } from '../../feature_flags.js'
import { SourceFile } from '../../function.js'
import type { RuntimeCache } from '../../utils/cache.js'
import { cachedLstat, cachedReaddir } from '../../utils/fs.js'
import { nonNullable } from '../../utils/non_nullable.js'
import { zipBinary } from '../../zip_binary.js'
import { detectBinaryRuntime } from '../detect_runtime.js'
import {
FindFunctionsInPathsFunction,
FindFunctionInPathFunction,
Runtime,
ZipFunction,
RuntimeType,
} from '../runtime.js'
import { build } from './builder.js'
import { MANIFEST_NAME } from './constants.js'
const detectRustFunction = async ({ cache, path }: { cache: RuntimeCache; path: string }) => {
const stat = await cachedLstat(cache.lstatCache, path)
if (!stat.isDirectory()) {
return
}
const files = await cachedReaddir(cache.readdirCache, path)
const hasCargoManifest = files.includes(MANIFEST_NAME)
if (!hasCargoManifest) {
return
}
const mainFilePath = join(path, 'src', 'main.rs')
try {
const mainFile = await cachedLstat(cache.lstatCache, mainFilePath)
if (mainFile.isFile()) {
return mainFilePath
}
} catch {
// no-op
}
}
const findFunctionsInPaths: FindFunctionsInPathsFunction = async function ({
cache,
featureFlags,
paths,
}: {
cache: RuntimeCache
featureFlags: FeatureFlags
paths: string[]
}) {
const functions = await Promise.all(paths.map((path) => findFunctionInPath({ cache, featureFlags, path })))
return functions.filter(nonNullable)
}
const findFunctionInPath: FindFunctionInPathFunction = async function ({ cache, featureFlags, path }) {
const runtime = await detectBinaryRuntime({ path })
if (runtime === RuntimeType.RUST) {
return processBinary({ cache, path })
}
if (featureFlags.buildRustSource !== true) {
return
}
const rustSourceFile = await detectRustFunction({ cache, path })
if (rustSourceFile) {
return processSource({ cache, mainFile: rustSourceFile, path })
}
}
const processBinary = async ({ cache, path }: { cache: RuntimeCache; path: string }): Promise<SourceFile> => {
const stat = (await cachedLstat(cache.lstatCache, path)) as Stats
const filename = basename(path)
const extension = extname(path)
const name = basename(path, extension)
return {
extension,
filename,
mainFile: path,
name,
srcDir: dirname(path),
srcPath: path,
stat,
}
}
const processSource = async ({
cache,
mainFile,
path,
}: {
cache: RuntimeCache
mainFile: string
path: string
}): Promise<SourceFile> => {
// TODO: This `stat` value is not going to be used, but we need it to satisfy
// the `FunctionSource` interface. We should revisit whether `stat` should be
// part of that interface in the first place, or whether we could compute it
// downstream when needed (maybe using the FS cache as an optimisation).
const stat = (await cachedLstat(cache.lstatCache, path)) as Stats
const filename = basename(path)
const extension = extname(path)
const name = basename(path, extension)
return {
extension,
filename,
mainFile,
name,
srcDir: path,
srcPath: path,
stat,
}
}
// The name of the binary inside the zip file must always be `bootstrap`
// because they include the Lambda runtime, and that's the name that AWS
// expects for those kind of functions.
const zipFunction: ZipFunction = async function ({
cache,
config,
destFolder,
filename,
mainFile,
runtime,
srcDir,
srcPath,
stat,
isInternal,
}) {
const destPath = join(destFolder, `${filename}.zip`)
const isSource = extname(mainFile) === '.rs'
const zipOptions = {
destPath,
filename: 'bootstrap',
runtime,
}
// If we're building from source, we first need to build the source and zip
// the resulting binary. Otherwise, we're dealing with a binary so we zip it
// directly.
if (isSource) {
const { path: binaryPath, stat: binaryStat } = await build({ cache, config, name: filename, srcDir })
await zipBinary({ ...zipOptions, srcPath: binaryPath, stat: binaryStat })
} else {
await zipBinary({ ...zipOptions, srcPath, stat })
}
return {
config,
path: destPath,
displayName: config?.name,
isInternal,
}
}
const runtime: Runtime = { findFunctionsInPaths, findFunctionInPath, name: RuntimeType.RUST, zipFunction }
export default runtime