Skip to content

Commit 8741ac6

Browse files
committed
add support for traffic_policy
1 parent 0f7c921 commit 8741ac6

9 files changed

+63
-38
lines changed

Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mio = { version = "=0.8.6" }
1717
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
1818
napi = { version = "2.12.1", default-features = false, features = ["napi4", "tokio_rt"] }
1919
napi-derive = "2.12.1"
20-
ngrok = { version = "=0.14.0-pre.13" }
20+
ngrok = { version = "=0.14.0-pre.14" }
2121
parking_lot = "0.12.1"
2222
regex = "1.9.5"
2323
rustls = "0.22.2"
@@ -32,3 +32,6 @@ napi-build = "2.0.1"
3232

3333
[profile.release]
3434
lto = true
35+
36+
[package.metadata.cargo-udeps.ignore]
37+
normal = ["mio"]

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ const listener = await ngrok.forward({
260260
oidc_allow_domains: ["<domain>"],
261261
oidc_allow_emails: ["<email>"],
262262
oidc_scopes: ["<scope>"],
263-
policy: "<policy_json>",
263+
traffic_policy: "<policy_json>",
264264
request_header_remove: ["X-Req-Nope"],
265265
response_header_remove: ["X-Res-Nope"],
266266
request_header_add: ["X-Req-Yup:true"],

__test__/connect.spec.mjs

+18
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,21 @@ test("policy", async (t) => {
338338
const response = await validateShutdown(t, httpServer, url);
339339
t.is("bar", response.headers["foo"]);
340340
});
341+
342+
test("traffic policy", async (t) => {
343+
const trafficPolicy = fs.readFileSync(path.resolve("__test__", "policy.json"), "utf8");
344+
345+
const httpServer = await makeHttp();
346+
const listener = await ngrok.forward({
347+
addr: httpServer.listenTo,
348+
authtoken: process.env["NGROK_AUTHTOKEN"],
349+
proto: "http",
350+
trafficPolicy: trafficPolicy,
351+
});
352+
const url = listener.url();
353+
354+
t.truthy(url);
355+
t.truthy(url.startsWith("https://"), url);
356+
const response = await validateShutdown(t, httpServer, url);
357+
t.is("bar", response.headers["foo"]);
358+
});

__test__/online.spec.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,12 @@ test("policy", async (t) => {
544544
const response = await forwardValidateShutdown(t, httpServer, listener, listener.url());
545545
t.is("bar", response.headers["foo"]);
546546
});
547+
548+
test("traffic policy", async (t) => {
549+
const trafficPolicy = fs.readFileSync(path.resolve("__test__", "policy.json"), "utf8");
550+
551+
const [httpServer, session] = await makeHttpAndSession();
552+
const listener = await session.httpEndpoint().trafficPolicy(trafficPolicy).listen();
553+
const response = await forwardValidateShutdown(t, httpServer, listener, listener.url());
554+
t.is("bar", response.headers["foo"]);
555+
});

flake.nix

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"rust-src"
3333
"rustc"
3434
"rustfmt"
35+
"rust-analyzer"
3536
];
3637
node-toolchain = with pkgs; [
3738
nodejs

index.d.ts

+15-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub struct Config {
209209
/// 'closed' - connection is lost, 'connected' - reconnected
210210
#[napi(ts_type = "(status: string) => void")]
211211
pub on_status_change: Option<bool>,
212-
/// The Traffic Policy to use for this endpoint.
212+
/// DEPRECATED: use TrafficPolicy instead.
213213
pub policy: Option<String>,
214214
/// The port for the listener to forward to.
215215
/// Only used if addr is not defined.
@@ -301,6 +301,8 @@ pub struct Config {
301301
/// Unused, will warn and be ignored
302302
#[napi(js_name = "terminate_at")]
303303
pub terminate_at: Option<String>,
304+
/// The Traffic Policy to use for this endpoint.
305+
pub traffic_policy: Option<String>,
304306
/// Whether to disable certificate verification for this listener
305307
#[napi(js_name = "verify_upstream_tls")]
306308
pub verify_upstream_tls: Option<bool>,

src/connect.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,9 @@ macro_rules! config_common {
102102
plumb!($builder, $config, proxy_proto);
103103
plumb!($builder, $config, forwards_to);
104104
plumb!($builder, $config, verify_upstream_tls);
105-
106-
// returns a Result, so we can't use the macro
107-
if let Some(ref v) = $config.policy {
108-
$builder.policy(v.clone())?;
109-
}
105+
plumb!($builder, $config, traffic_policy);
106+
// policy is in the process of being deprecated. for now, we just remap it to traffic_policy
107+
plumb!($builder, $config, traffic_policy, policy);
110108
};
111109
}
112110

src/listener_builder.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,16 @@ macro_rules! make_listener_builder {
165165
self
166166
}
167167
#[napi]
168-
pub fn policy(&mut self, policy: String) -> Result<&Self> {
168+
pub fn policy(&mut self, policy: String) -> &Self {
169169
let mut builder = self.listener_builder.lock();
170-
match builder.policy(policy.as_str()) {
171-
Ok(_) => Ok(self),
172-
Err(e) => Err(napi_err(format!("Error parsing policy, {e}"))),
173-
}
170+
builder.traffic_policy(policy);
171+
self
172+
}
173+
#[napi]
174+
pub fn traffic_policy(&mut self, traffic_policy: String) -> &Self {
175+
let mut builder = self.listener_builder.lock();
176+
builder.traffic_policy(traffic_policy);
177+
self
174178
}
175179
}
176180
};

0 commit comments

Comments
 (0)