Skip to content
This repository was archived by the owner on Jul 17, 2024. It is now read-only.

Commit 2e1ebc0

Browse files
authored
new: Support dist-url setting. (#34)
1 parent 73a4281 commit 2e1ebc0

File tree

11 files changed

+64
-50
lines changed

11 files changed

+64
-50
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## 0.10.2
4+
5+
#### 🚀 Updates
6+
7+
- Added a `dist-url` config setting, allowing the download host to be customized.
8+
39
## 0.10.1
410

511
#### 🚀 Updates

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ All plugins can be configured with a `.prototools` file.
2424
### Node.js
2525

2626
- `bundled-npm` (bool) - When `node` is installed, also install `npm` with the version of npm that came bundled with Node.js. Defaults to `false`.
27+
- `dist-url` (string) - The distribution URL to download Node.js archives from. Supports `{version}` and `{file}` tokens.
2728

2829
```toml
2930
[tools.node]
3031
bundled-npm = true
32+
dist-url = "https://..."
3133
```
3234

3335
### Package managers

crates/common/src/config.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
1+
#[derive(Debug, serde::Deserialize, serde::Serialize)]
22
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
3-
pub struct PluginConfig {
3+
pub struct NodePluginConfig {
44
pub bundled_npm: bool,
5+
pub dist_url: String,
6+
}
7+
8+
impl Default for NodePluginConfig {
9+
fn default() -> Self {
10+
Self {
11+
bundled_npm: false,
12+
dist_url: "https://nodejs.org/download/release/v{version}/{file}".into(),
13+
}
14+
}
15+
}
16+
17+
#[derive(Debug, Default, serde::Deserialize, serde::Serialize)]
18+
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
19+
pub struct NodeDepmanPluginConfig {
520
pub shared_globals_dir: bool,
621
}

crates/node-depman/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "node_depman_plugin"
3-
version = "0.10.1"
3+
version = "0.10.2"
44
edition = "2021"
55
license = "MIT"
66
publish = false

crates/node-depman/src/proto.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::npm_registry::parse_registry_response;
22
use crate::package_manager::PackageManager;
33
use extism_pdk::*;
4-
use node_common::{NodeDistVersion, PluginConfig, VoltaField};
4+
use node_common::{NodeDepmanPluginConfig, NodeDistVersion, VoltaField};
55
use nodejs_package_json::PackageJson;
66
use proto_pdk::*;
77
use std::collections::HashMap;
@@ -354,7 +354,7 @@ pub fn locate_executables(
354354
}
355355
};
356356

357-
let config = get_tool_config::<PluginConfig>()?;
357+
let config = get_tool_config::<NodeDepmanPluginConfig>()?;
358358

359359
if config.shared_globals_dir {
360360
globals_lookup_dirs.clear();
@@ -378,7 +378,7 @@ pub fn pre_run(Json(input): Json<RunHook>) -> FnResult<Json<RunHookResult>> {
378378
};
379379

380380
let args = &input.passthrough_args;
381-
let config = get_tool_config::<PluginConfig>()?;
381+
let config = get_tool_config::<NodeDepmanPluginConfig>()?;
382382

383383
if args.len() < 3 || !config.shared_globals_dir {
384384
return Ok(Json(result));

crates/node-depman/tests/hooks_test.rs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use node_common::PluginConfig;
1+
use node_common::NodeDepmanPluginConfig;
22
use proto_pdk_api::RunHook;
33
use proto_pdk_test_utils::*;
44
use std::collections::HashMap;
@@ -33,9 +33,8 @@ mod pre_run {
3333
fn does_nothing_if_disabled() {
3434
let sandbox = create_empty_proto_sandbox();
3535
let plugin = sandbox.create_plugin_with_config("npm-test", |config| {
36-
config.tool_config(PluginConfig {
36+
config.tool_config(NodeDepmanPluginConfig {
3737
shared_globals_dir: false,
38-
..Default::default()
3938
});
4039
});
4140

@@ -49,9 +48,8 @@ mod pre_run {
4948
fn does_nothing_if_enabled_but_no_args() {
5049
let sandbox = create_empty_proto_sandbox();
5150
let plugin = sandbox.create_plugin_with_config("npm-test", |config| {
52-
config.tool_config(PluginConfig {
51+
config.tool_config(NodeDepmanPluginConfig {
5352
shared_globals_dir: true,
54-
..Default::default()
5553
});
5654
});
5755

@@ -68,9 +66,8 @@ mod pre_run {
6866
fn does_nothing_if_a_prefix_was_provided() {
6967
let sandbox = create_empty_proto_sandbox();
7068
let plugin = sandbox.create_plugin_with_config("npm-test", |config| {
71-
config.tool_config(PluginConfig {
69+
config.tool_config(NodeDepmanPluginConfig {
7270
shared_globals_dir: true,
73-
..Default::default()
7471
});
7572
});
7673

@@ -94,9 +91,8 @@ mod pre_run {
9491
fn adds_env_var() {
9592
let sandbox = create_empty_proto_sandbox();
9693
let plugin = sandbox.create_plugin_with_config("npm-test", |config| {
97-
config.tool_config(PluginConfig {
94+
config.tool_config(NodeDepmanPluginConfig {
9895
shared_globals_dir: true,
99-
..Default::default()
10096
});
10197
});
10298

@@ -124,9 +120,8 @@ mod pre_run {
124120
fn adds_env_var_with_aliases() {
125121
let sandbox = create_empty_proto_sandbox();
126122
let plugin = sandbox.create_plugin_with_config("npm-test", |config| {
127-
config.tool_config(PluginConfig {
123+
config.tool_config(NodeDepmanPluginConfig {
128124
shared_globals_dir: true,
129-
..Default::default()
130125
});
131126
});
132127

@@ -158,9 +153,8 @@ mod pre_run {
158153
fn does_nothing_if_disabled() {
159154
let sandbox = create_empty_proto_sandbox();
160155
let plugin = sandbox.create_plugin_with_config("pnpm-test", |config| {
161-
config.tool_config(PluginConfig {
156+
config.tool_config(NodeDepmanPluginConfig {
162157
shared_globals_dir: false,
163-
..Default::default()
164158
});
165159
});
166160

@@ -174,9 +168,8 @@ mod pre_run {
174168
fn does_nothing_if_enabled_but_no_args() {
175169
let sandbox = create_empty_proto_sandbox();
176170
let plugin = sandbox.create_plugin_with_config("pnpm-test", |config| {
177-
config.tool_config(PluginConfig {
171+
config.tool_config(NodeDepmanPluginConfig {
178172
shared_globals_dir: true,
179-
..Default::default()
180173
});
181174
});
182175

@@ -193,9 +186,8 @@ mod pre_run {
193186
fn does_nothing_if_a_dir_was_provided() {
194187
let sandbox = create_empty_proto_sandbox();
195188
let plugin = sandbox.create_plugin_with_config("pnpm-test", |config| {
196-
config.tool_config(PluginConfig {
189+
config.tool_config(NodeDepmanPluginConfig {
197190
shared_globals_dir: true,
198-
..Default::default()
199191
});
200192
});
201193

@@ -219,9 +211,8 @@ mod pre_run {
219211
fn adds_args() {
220212
let sandbox = create_empty_proto_sandbox();
221213
let plugin = sandbox.create_plugin_with_config("pnpm-test", |config| {
222-
config.tool_config(PluginConfig {
214+
config.tool_config(NodeDepmanPluginConfig {
223215
shared_globals_dir: true,
224-
..Default::default()
225216
});
226217
});
227218

@@ -247,9 +238,8 @@ mod pre_run {
247238
fn adds_args_with_aliases() {
248239
let sandbox = create_empty_proto_sandbox();
249240
let plugin = sandbox.create_plugin_with_config("pnpm-test", |config| {
250-
config.tool_config(PluginConfig {
241+
config.tool_config(NodeDepmanPluginConfig {
251242
shared_globals_dir: true,
252-
..Default::default()
253243
});
254244
});
255245

@@ -281,9 +271,8 @@ mod pre_run {
281271
fn does_nothing_if_disabled() {
282272
let sandbox = create_empty_proto_sandbox();
283273
let plugin = sandbox.create_plugin_with_config("yarn-test", |config| {
284-
config.tool_config(PluginConfig {
274+
config.tool_config(NodeDepmanPluginConfig {
285275
shared_globals_dir: false,
286-
..Default::default()
287276
});
288277
});
289278

@@ -297,9 +286,8 @@ mod pre_run {
297286
fn does_nothing_if_enabled_but_no_args() {
298287
let sandbox = create_empty_proto_sandbox();
299288
let plugin = sandbox.create_plugin_with_config("yarn-test", |config| {
300-
config.tool_config(PluginConfig {
289+
config.tool_config(NodeDepmanPluginConfig {
301290
shared_globals_dir: true,
302-
..Default::default()
303291
});
304292
});
305293

@@ -316,9 +304,8 @@ mod pre_run {
316304
fn does_nothing_if_a_prefix_was_provided() {
317305
let sandbox = create_empty_proto_sandbox();
318306
let plugin = sandbox.create_plugin_with_config("yarn-test", |config| {
319-
config.tool_config(PluginConfig {
307+
config.tool_config(NodeDepmanPluginConfig {
320308
shared_globals_dir: true,
321-
..Default::default()
322309
});
323310
});
324311

@@ -342,9 +329,8 @@ mod pre_run {
342329
fn adds_env_var() {
343330
let sandbox = create_empty_proto_sandbox();
344331
let plugin = sandbox.create_plugin_with_config("yarn-test", |config| {
345-
config.tool_config(PluginConfig {
332+
config.tool_config(NodeDepmanPluginConfig {
346333
shared_globals_dir: true,
347-
..Default::default()
348334
});
349335
});
350336

crates/node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "node_plugin"
3-
version = "0.10.1"
3+
version = "0.10.2"
44
edition = "2021"
55
license = "MIT"
66
publish = false

crates/node/src/proto.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use extism_pdk::*;
2-
use node_common::{NodeDistLTS, NodeDistVersion, PluginConfig, VoltaField};
2+
use node_common::{NodeDistLTS, NodeDistVersion, NodePluginConfig, VoltaField};
33
use nodejs_package_json::PackageJson;
44
use proto_pdk::*;
55

@@ -163,14 +163,14 @@ pub fn download_prebuilt(
163163

164164
let arch = map_arch(env.os, env.arch)?;
165165
let mut version = input.context.version;
166-
let mut host = "https://nodejs.org/download/release".to_owned();
166+
let mut host = get_tool_config::<NodePluginConfig>()?.dist_url;
167167

168168
// When canary, extract the latest version from the index
169169
if version.is_canary() {
170170
let response: Vec<NodeDistVersion> =
171171
fetch_url("https://nodejs.org/download/nightly/index.json")?;
172172

173-
host = "https://nodejs.org/download/nightly".into();
173+
host = host.replace("/release/", "/nightly/");
174174
version = VersionSpec::parse(&response[0].version)?;
175175
}
176176

@@ -202,9 +202,14 @@ pub fn download_prebuilt(
202202

203203
Ok(Json(DownloadPrebuiltOutput {
204204
archive_prefix: Some(prefix),
205-
download_url: format!("{host}/v{version}/{filename}"),
205+
download_url: host
206+
.replace("{version}", &version.to_string())
207+
.replace("{file}", &filename),
206208
download_name: Some(filename),
207-
checksum_url: Some(format!("{host}/v{version}/SHASUMS256.txt")),
209+
checksum_url: Some(
210+
host.replace("{version}", &version.to_string())
211+
.replace("{file}", "SHASUMS256.txt"),
212+
),
208213
..DownloadPrebuiltOutput::default()
209214
}))
210215
}
@@ -228,7 +233,7 @@ pub fn locate_executables(
228233

229234
#[plugin_fn]
230235
pub fn post_install(Json(input): Json<InstallHook>) -> FnResult<()> {
231-
let config = get_tool_config::<PluginConfig>()?;
236+
let config = get_tool_config::<NodePluginConfig>()?;
232237

233238
if !config.bundled_npm
234239
|| input

crates/node/tests/hooks_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Importing proto_pdk crashes Windows because it contains WASM code
22
#[cfg(not(windows))]
33
mod node_hooks {
4-
use node_common::PluginConfig;
4+
use node_common::NodePluginConfig;
55
use proto_pdk::InstallHook;
66
use proto_pdk_test_utils::*;
77
use serial_test::serial;
@@ -24,7 +24,7 @@ mod node_hooks {
2424
fn installs_bundled_npm() {
2525
let sandbox = create_empty_proto_sandbox();
2626
let plugin = sandbox.create_plugin_with_config("node-test", |config| {
27-
config.tool_config(PluginConfig {
27+
config.tool_config(NodePluginConfig {
2828
bundled_npm: true,
2929
..Default::default()
3030
});
@@ -61,7 +61,7 @@ mod node_hooks {
6161
fn can_pin_bundled_npm() {
6262
let sandbox = create_empty_proto_sandbox();
6363
let plugin = sandbox.create_plugin_with_config("node-test", |config| {
64-
config.tool_config(PluginConfig {
64+
config.tool_config(NodePluginConfig {
6565
bundled_npm: true,
6666
..Default::default()
6767
});
@@ -89,7 +89,7 @@ mod node_hooks {
8989
fn can_skip_bundled_npm() {
9090
let sandbox = create_empty_proto_sandbox();
9191
let plugin = sandbox.create_plugin_with_config("node-test", |config| {
92-
config.tool_config(PluginConfig {
92+
config.tool_config(NodePluginConfig {
9393
bundled_npm: true,
9494
..Default::default()
9595
});

0 commit comments

Comments
 (0)