-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathmain.rs
292 lines (233 loc) · 8.09 KB
/
main.rs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//! Arguments are forwarded directly to wasm-pack
use std::{
env::{self, VarError},
fs::{self, read_to_string, File},
io::{Read, Write},
path::{Path, PathBuf},
process::{Command, Stdio},
};
use anyhow::Result;
use clap::Parser;
use wasm_pack::command::build::{Build, BuildOptions};
const OUT_DIR: &str = "build";
const OUT_NAME: &str = "index";
const WORKER_SUBDIR: &str = "worker";
const SHIM_TEMPLATE: &str = include_str!("./js/shim.js");
mod install;
pub fn main() -> Result<()> {
// Our tests build the bundle ourselves.
if !cfg!(test) {
wasm_pack_build(env::args().skip(1))?;
}
let with_coredump = env::var("COREDUMP").is_ok();
if with_coredump {
println!("Adding wasm coredump");
wasm_coredump()?;
}
let esbuild_path = install::ensure_esbuild()?;
create_worker_dir()?;
copy_generated_code_to_worker_dir()?;
let shim_template = match env::var("CUSTOM_SHIM") {
Ok(path) => {
let path = Path::new(&path).to_owned();
println!("Using custom shim from {}", path.display());
// NOTE: we fail in case that file doesnt exist or something else happens
read_to_string(path)?
}
Err(_) => SHIM_TEMPLATE.to_owned(),
};
let shim = if env::var("RUN_TO_COMPLETION").is_ok() {
shim_template.replace("$WAIT_UNTIL_RESPONSE", "this.ctx.waitUntil(response);")
} else {
shim_template.replace("$WAIT_UNTIL_RESPONSE", "")
};
write_string_to_file(worker_path("shim.js"), shim)?;
bundle(&esbuild_path)?;
remove_unused_js()?;
Ok(())
}
const INSTALL_HELP: &str = "In case you are missing the binary, you can install it using: `cargo install wasm-coredump-rewriter`";
fn wasm_coredump() -> Result<()> {
let coredump_flags = env::var("COREDUMP_FLAGS");
let coredump_flags: Vec<&str> = if let Ok(flags) = &coredump_flags {
flags.split(' ').collect()
} else {
vec![]
};
let mut child = Command::new("wasm-coredump-rewriter")
.args(coredump_flags)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(|err| {
anyhow::anyhow!("failed to spawn wasm-coredump-rewriter: {err}\n\n{INSTALL_HELP}.")
})?;
let input_filename = output_path("index_bg.wasm");
let input_bytes = {
let mut input = File::open(input_filename.clone())
.map_err(|err| anyhow::anyhow!("failed to open input file: {err}"))?;
let mut input_bytes = Vec::new();
input
.read_to_end(&mut input_bytes)
.map_err(|err| anyhow::anyhow!("failed to open input file: {err}"))?;
input_bytes
};
{
let child_stdin = child.stdin.as_mut().unwrap();
child_stdin
.write_all(&input_bytes)
.map_err(|err| anyhow::anyhow!("failed to write input file to rewriter: {err}"))?;
// Close stdin to finish and avoid indefinite blocking
}
let output = child
.wait_with_output()
.map_err(|err| anyhow::anyhow!("failed to get rewriter's status: {err}"))?;
if output.status.success() {
// Open the input file again with truncate to write the output
let mut f = fs::OpenOptions::new()
.truncate(true)
.write(true)
.open(input_filename)
.map_err(|err| anyhow::anyhow!("failed to open output file: {err}"))?;
f.write_all(&output.stdout)
.map_err(|err| anyhow::anyhow!("failed to write output file: {err}"))?;
Ok(())
} else {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
Err(anyhow::anyhow!(format!(
"failed to run Wasm coredump rewriter: {stdout}\n{stderr}"
)))
}
}
#[derive(Parser)]
struct BuildArgs {
#[clap(flatten)]
pub build_options: BuildOptions,
}
fn parse_wasm_pack_opts<I>(args: I) -> Result<BuildOptions>
where
I: IntoIterator<Item = String>,
{
// This is done instead of explicitly constructing
// BuildOptions to preserve the behavior of appending
// arbitrary arguments in `args`.
let mut build_args = vec![
"--no-typescript".to_owned(),
"--target".to_owned(),
"bundler".to_owned(),
"--out-dir".to_owned(),
OUT_DIR.to_owned(),
"--out-name".to_owned(),
OUT_NAME.to_owned(),
];
build_args.extend(args);
let command = BuildArgs::try_parse_from(build_args)?;
Ok(command.build_options)
}
fn wasm_pack_build<I>(args: I) -> Result<()>
where
I: IntoIterator<Item = String>,
{
let opts = parse_wasm_pack_opts(args)?;
let mut build = Build::try_from_opts(opts)?;
build.run()
}
fn create_worker_dir() -> Result<()> {
// create a directory for our worker to live in
let worker_dir = PathBuf::from(OUT_DIR).join(WORKER_SUBDIR);
// remove anything that already exists
if worker_dir.is_dir() {
fs::remove_dir_all(&worker_dir)?
} else if worker_dir.is_file() {
fs::remove_file(&worker_dir)?
};
// create an output dir
fs::create_dir(worker_dir)?;
Ok(())
}
fn copy_generated_code_to_worker_dir() -> Result<()> {
let glue_src = output_path(format!("{OUT_NAME}_bg.js"));
let glue_dest = worker_path(format!("{OUT_NAME}_bg.js"));
let wasm_src = output_path(format!("{OUT_NAME}_bg.wasm"));
let wasm_dest = worker_path(format!("{OUT_NAME}.wasm"));
// wasm-bindgen supports adding arbitrary JavaScript for a library, so we need to move that as well.
// https://rustwasm.github.io/wasm-bindgen/reference/js-snippets.html
let snippets_src = output_path("snippets");
let snippets_dest = worker_path("snippets");
for (src, dest) in [
(glue_src, glue_dest),
(wasm_src, wasm_dest),
(snippets_src, snippets_dest),
] {
if !src.exists() {
continue;
}
fs::rename(src, dest)?;
}
Ok(())
}
// Bundles the snippets and worker-related code into a single file.
fn bundle(esbuild_path: &Path) -> Result<()> {
let no_minify = !matches!(env::var("NO_MINIFY"), Err(VarError::NotPresent));
let path = PathBuf::from(OUT_DIR).join(WORKER_SUBDIR).canonicalize()?;
let esbuild_path = esbuild_path.canonicalize()?;
let mut command = Command::new(esbuild_path);
command.args([
"--external:./index.wasm",
"--external:cloudflare:sockets",
"--external:cloudflare:workers",
"--format=esm",
"--bundle",
"./shim.js",
"--outfile=shim.mjs",
]);
if !no_minify {
command.arg("--minify");
}
let exit_status = command.current_dir(path).spawn()?.wait()?;
match exit_status.success() {
true => Ok(()),
false => anyhow::bail!("esbuild exited with status {}", exit_status),
}
}
// After bundling there's no reason why we'd want to upload our now un-used JavaScript so we'll
// delete it.
fn remove_unused_js() -> Result<()> {
let snippets_dir = worker_path("snippets");
if snippets_dir.exists() {
std::fs::remove_dir_all(&snippets_dir)?;
}
std::fs::remove_file(worker_path(format!("{OUT_NAME}_bg.js")))?;
std::fs::remove_file(worker_path("shim.js"))?;
Ok(())
}
fn write_string_to_file<P: AsRef<Path>>(path: P, contents: impl AsRef<str>) -> Result<()> {
let mut file = File::create(path)?;
file.write_all(contents.as_ref().as_bytes())?;
Ok(())
}
pub fn worker_path(name: impl AsRef<str>) -> PathBuf {
PathBuf::from(OUT_DIR)
.join(WORKER_SUBDIR)
.join(name.as_ref())
}
pub fn output_path(name: impl AsRef<str>) -> PathBuf {
PathBuf::from(OUT_DIR).join(name.as_ref())
}
#[cfg(test)]
mod test {
use super::parse_wasm_pack_opts;
#[test]
fn test_wasm_pack_args_build_arg() {
let args = vec!["--release".to_owned()];
let result = parse_wasm_pack_opts(args);
assert!(result.is_ok());
}
#[test]
fn test_wasm_pack_args_additional_arg() {
let args = vec!["--weak-refs".to_owned()];
let result = parse_wasm_pack_opts(args).unwrap();
assert!(result.weak_refs);
}
}