This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto.rs
146 lines (124 loc) · 4.49 KB
/
proto.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
use crate::config::BunPluginConfig;
use extism_pdk::*;
use proto_pdk::*;
use std::collections::HashMap;
#[host_fn]
extern "ExtismHost" {
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
}
static NAME: &str = "Bun";
static BIN: &str = "bun";
#[plugin_fn]
pub fn register_tool(Json(_): Json<ToolMetadataInput>) -> FnResult<Json<ToolMetadataOutput>> {
Ok(Json(ToolMetadataOutput {
name: NAME.into(),
type_of: PluginType::Language,
plugin_version: Some(env!("CARGO_PKG_VERSION").into()),
self_upgrade_commands: vec!["upgrade".into()],
..ToolMetadataOutput::default()
}))
}
#[plugin_fn]
pub fn load_versions(Json(_): Json<LoadVersionsInput>) -> FnResult<Json<LoadVersionsOutput>> {
let tags = load_git_tags("https://github.com/oven-sh/bun")?;
let tags = tags
.iter()
.filter_map(|tag| tag.strip_prefix("bun-v").map(|tag| tag.to_owned()))
.collect::<Vec<_>>();
Ok(Json(LoadVersionsOutput::from(tags)?))
}
#[plugin_fn]
pub fn download_prebuilt(
Json(input): Json<DownloadPrebuiltInput>,
) -> FnResult<Json<DownloadPrebuiltOutput>> {
let env = get_host_environment()?;
let has_windows_support = match &input.context.version {
VersionSpec::Canary => true,
VersionSpec::Alias(alias) => alias == "latest",
VersionSpec::Semantic(version) => version.major >= 1 && version.minor >= 1,
_ => false,
};
check_supported_os_and_arch(
NAME,
&env,
if has_windows_support {
permutations! [
HostOS::Linux => [HostArch::X64, HostArch::Arm64],
HostOS::MacOS => [HostArch::X64, HostArch::Arm64],
HostOS::Windows => [HostArch::X64],
]
} else {
permutations! [
HostOS::Linux => [HostArch::X64, HostArch::Arm64],
HostOS::MacOS => [HostArch::X64, HostArch::Arm64],
]
},
)?;
let version = &input.context.version;
let arch = match env.arch {
HostArch::Arm64 => "aarch64",
HostArch::X64 => "x64",
_ => unreachable!(),
};
let mut avx2_suffix = "";
if env.arch == HostArch::X64 && env.os == HostOS::Linux && command_exists(&env, "grep") {
let output = exec_command!("grep", ["avx2", "/proc/cpuinfo"]);
if output.exit_code != 0 {
avx2_suffix = "-baseline";
}
};
let prefix = match env.os {
HostOS::Linux => format!("bun-linux-{arch}{avx2_suffix}"),
HostOS::MacOS => format!("bun-darwin-{arch}{avx2_suffix}"),
HostOS::Windows => format!("bun-windows-{arch}"),
_ => unreachable!(),
};
let filename = format!("{prefix}.zip");
let mut host = get_tool_config::<BunPluginConfig>()?.dist_url;
// canary - bun-v1.2.3
if version.is_canary() {
host = host.replace("bun-v{version}", "{version}");
};
Ok(Json(DownloadPrebuiltOutput {
archive_prefix: Some(prefix),
download_url: host
.replace("{version}", &version.to_string())
.replace("{file}", &filename),
download_name: Some(filename),
// Checksums are not consistently updated
checksum_url: if version.is_canary() {
None
} else {
Some(
host.replace("{version}", &version.to_string())
.replace("{file}", "SHASUMS256.txt"),
)
},
..DownloadPrebuiltOutput::default()
}))
}
#[plugin_fn]
pub fn locate_executables(
Json(_): Json<LocateExecutablesInput>,
) -> FnResult<Json<LocateExecutablesOutput>> {
let env = get_host_environment()?;
let bunx = ExecutableConfig {
// `bunx` isn't a real binary provided by Bun so we can't symlink it.
// Instead, it's simply the `bun` binary named `bunx` and Bun toggles
// functionality based on `args[0]`.
exe_link_path: Some(env.os.get_exe_name(BIN).into()),
// The approach doesn't work for shims since we use child processes,
// so execute `bun x` instead (notice the space).
shim_before_args: Some(StringOrVec::String("x".into())),
..ExecutableConfig::default()
};
Ok(Json(LocateExecutablesOutput {
globals_lookup_dirs: vec!["$HOME/.bun/bin".into()],
primary: Some(ExecutableConfig::new(env.os.get_exe_name(BIN))),
secondary: HashMap::from_iter([
// bunx
("bunx".into(), bunx),
]),
..LocateExecutablesOutput::default()
}))
}