-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction.ts
More file actions
143 lines (126 loc) · 3.78 KB
/
Copy pathfunction.ts
File metadata and controls
143 lines (126 loc) · 3.78 KB
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
import * as fs from 'fs';
import * as path from 'path';
import {
Architecture,
FunctionOptions,
Runtime,
RuntimeFamily,
Function,
} from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import { Bundling } from './bundling';
import { BundlingOptions } from './types';
import { callsites } from './util';
/**
* Properties for a RustFunction
*/
export interface RustFunctionProps extends FunctionOptions {
/**
* Path to the entry Cargo.toml file.
*
* @default - Derived from the name of the defining file and the construct's id.
* If the `RustsFunction` is defined in `stack.ts` with `my-binary` as id
* (`new RustFunction(this, 'my-binary')`), the construct will look at `stack.my-binary/Cargo.toml`
*/
readonly entry?: string;
/**
* The name of the binary to build, in case that it's different that the package's name.
*
* @default Build all binaries
*/
readonly binaryName?: string;
/**
* The runtime environment. Only OS-only runtimes are supported.
*
* @default `Runtime.PROVIDED_AL2023`
*/
readonly runtime?: Runtime;
/**
* Bundling options
*
* @default - use default bundling options: all
* binaries and packages are bundled.
*/
readonly bundling?: BundlingOptions;
/**
* The path to the directory containing project Manifest file
*
* @default - the directory containing the `depsLockFilePath`
*/
readonly projectRoot?: string;
}
/**
* A Rust Lambda function
*/
export class RustFunction extends Function {
constructor(scope: Construct, id: string, props: RustFunctionProps = {}) {
if (props.runtime && props.runtime.family !== RuntimeFamily.OTHER) {
throw new Error('Only `Custom` runtimes are supported.');
}
// Entry and defaults
const entry = path.resolve(findEntry(id, props.entry));
// Handler is not important for Rust function
const handler = 'rust.handler';
const architecture = props.architecture ?? Architecture.X86_64;
const runtime = props.runtime ?? Runtime.PROVIDED_AL2023;
super(scope, id, {
...props,
runtime,
code: Bundling.bundle({
...(props.bundling ?? {}),
entry,
runtime,
architecture,
binaryName: props.binaryName,
}),
handler: handler,
});
}
}
/**
* Searches for an entry file. Preference order is the following:
* 1. Given entry file
* 2. A Cargo.toml file named as the defining file with id as u (defining-file.id/Cargo.toml)
*/
function findEntry(id: string, entry?: string): string {
if (entry) {
if (!/\Cargo.toml$/.test(entry)) {
// Only Cargo.toml files are supported.
// If the user gave a path to a directory, we'll look for a Cargo.toml file in it.
// If the user gave a path to a file, we'll use it as-is.
entry = path.join(entry, 'Cargo.toml');
}
if (!fs.existsSync(entry)) {
throw new Error(`Cannot find manifest file at ${entry}`);
}
return entry;
}
const definingFile = findDefiningFile();
const extname = path.extname(definingFile);
const manifestFile = definingFile.replace(
new RegExp(`${extname}$`),
`.${id}/Cargo.toml`,
);
if (fs.existsSync(manifestFile)) {
return manifestFile;
}
throw new Error('Cannot find manifest file - Cargo.toml');
}
/**
* Finds the name of the file where the `RustFunction` is defined
*/
function findDefiningFile(): string {
let definingIndex;
const sites = callsites();
for (const [index, site] of sites.entries()) {
if (site.getFunctionName() === 'RustFunction') {
// The next site is the site where the RustFunction was created
definingIndex = index + 1;
break;
}
}
if (!definingIndex || !sites[definingIndex]) {
throw new Error('Cannot find defining file.');
}
return sites[definingIndex].getFileName();
}