@ngrok/ngrok 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/CONTRIBUTING.md +4 -4
  3. package/Cargo.toml +4 -2
  4. package/README.md +5 -1
  5. package/docs/.nojekyll +1 -0
  6. package/docs/assets/highlight.css +71 -0
  7. package/docs/assets/main.js +58 -0
  8. package/docs/assets/search.js +1 -0
  9. package/docs/assets/style.css +1280 -0
  10. package/docs/classes/NgrokHttpTunnel.html +189 -0
  11. package/docs/classes/NgrokHttpTunnelBuilder.html +440 -0
  12. package/docs/classes/NgrokLabeledTunnel.html +177 -0
  13. package/docs/classes/NgrokLabeledTunnelBuilder.html +130 -0
  14. package/docs/classes/NgrokSession.html +142 -0
  15. package/docs/classes/NgrokSessionBuilder.html +303 -0
  16. package/docs/classes/NgrokTcpTunnel.html +189 -0
  17. package/docs/classes/NgrokTcpTunnelBuilder.html +199 -0
  18. package/docs/classes/NgrokTlsTunnel.html +189 -0
  19. package/docs/classes/NgrokTlsTunnelBuilder.html +235 -0
  20. package/docs/classes/UpdateRequest.html +92 -0
  21. package/docs/functions/consoleLog.html +73 -0
  22. package/docs/functions/listen.html +74 -0
  23. package/docs/functions/listenable.html +72 -0
  24. package/docs/functions/loggingCallback.html +91 -0
  25. package/docs/index.html +129 -0
  26. package/docs/modules.html +79 -0
  27. package/examples/ngrok-http-full.js +2 -1
  28. package/examples/ngrok-typescript.ts +29 -8
  29. package/examples/ngrok-winston.js +24 -0
  30. package/index.d.ts +91 -23
  31. package/index.js +108 -23
  32. package/package.json +20 -16
  33. package/scripts/gc-node-drop.js +6 -2
  34. package/scripts/gc-rust-drop.js +8 -4
  35. package/src/http.rs +64 -42
  36. package/src/lib.rs +1 -0
  37. package/src/logging.rs +134 -0
  38. package/src/session.rs +75 -85
  39. package/src/tcp.rs +2 -1
  40. package/src/tls.rs +6 -4
  41. package/src/tunnel.rs +89 -34
  42. package/src/tunnel_builder.rs +27 -15
  43. package/trailer.d.ts +9 -2
  44. package/trailer.js +106 -21
  45. package/examples/ngrok-get-socket.js +0 -12
package/src/session.rs CHANGED
@@ -20,11 +20,11 @@ use ngrok::{
20
20
  },
21
21
  Session,
22
22
  };
23
+ use parking_lot::Mutex as SyncMutex;
23
24
  use tokio::sync::Mutex;
24
- use tracing::debug;
25
- use tracing_subscriber::{
26
- self,
27
- fmt::format::FmtSpan,
25
+ use tracing::{
26
+ debug,
27
+ info,
28
28
  };
29
29
 
30
30
  use crate::{
@@ -42,19 +42,7 @@ use crate::{
42
42
  #[napi]
43
43
  #[allow(dead_code)]
44
44
  struct NgrokSessionBuilder {
45
- raw_builder: SessionBuilder,
46
- }
47
-
48
- /// turn on tracing subscriber which obeys NGROK_LOG env variable, e.g.:
49
- /// process.env.NGROK_LOG = 'ngrok=debug';
50
- /// ngrok.tracingSubscriber();
51
- #[napi()]
52
- pub fn tracing_subscriber() {
53
- tracing_subscriber::fmt()
54
- .pretty()
55
- .with_span_events(FmtSpan::ENTER)
56
- .with_env_filter(std::env::var("NGROK_LOG").unwrap_or_default())
57
- .init();
45
+ raw_builder: Arc<SyncMutex<SessionBuilder>>,
58
46
  }
59
47
 
60
48
  #[napi]
@@ -65,7 +53,7 @@ impl NgrokSessionBuilder {
65
53
  #[napi(constructor)]
66
54
  pub fn new() -> Self {
67
55
  NgrokSessionBuilder {
68
- raw_builder: Session::builder(),
56
+ raw_builder: Arc::new(SyncMutex::new(Session::builder())),
69
57
  }
70
58
  }
71
59
 
@@ -80,11 +68,8 @@ impl NgrokSessionBuilder {
80
68
  /// [authtoken parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#authtoken
81
69
  #[napi]
82
70
  pub fn authtoken(&mut self, authtoken: String) -> &Self {
83
- // can't put lifetimes or generics on napi structs, which limits our options.
84
- // there is a Reference which can force static lifetime, but haven't figured
85
- // out a way to make this actually helpful. so send in the clones.
86
- // https://napi.rs/docs/concepts/reference
87
- self.raw_builder = self.raw_builder.clone().authtoken(authtoken);
71
+ let mut builder = self.raw_builder.lock();
72
+ *builder = builder.clone().authtoken(authtoken);
88
73
  self
89
74
  }
90
75
 
@@ -92,7 +77,8 @@ impl NgrokSessionBuilder {
92
77
  /// NGROK_AUTHTOKEN environment variable.
93
78
  #[napi]
94
79
  pub fn authtoken_from_env(&mut self) -> &Self {
95
- self.raw_builder = self.raw_builder.clone().authtoken_from_env();
80
+ let mut builder = self.raw_builder.lock();
81
+ *builder = builder.clone().authtoken_from_env();
96
82
  self
97
83
  }
98
84
 
@@ -105,8 +91,8 @@ impl NgrokSessionBuilder {
105
91
  /// [heartbeat_interval parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#heartbeat_interval
106
92
  #[napi]
107
93
  pub fn heartbeat_interval(&mut self, heartbeat_interval: u32) -> &Self {
108
- self.raw_builder = self
109
- .raw_builder
94
+ let mut builder = self.raw_builder.lock();
95
+ *builder = builder
110
96
  .clone()
111
97
  .heartbeat_interval(Duration::new(heartbeat_interval.into(), 0));
112
98
  self
@@ -121,8 +107,8 @@ impl NgrokSessionBuilder {
121
107
  /// [heartbeat_tolerance parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#heartbeat_tolerance
122
108
  #[napi]
123
109
  pub fn heartbeat_tolerance(&mut self, heartbeat_tolerance: u32) -> &Self {
124
- self.raw_builder = self
125
- .raw_builder
110
+ let mut builder = self.raw_builder.lock();
111
+ *builder = builder
126
112
  .clone()
127
113
  .heartbeat_tolerance(Duration::new(heartbeat_tolerance.into(), 0));
128
114
  self
@@ -138,7 +124,8 @@ impl NgrokSessionBuilder {
138
124
  /// [metdata parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#metadata
139
125
  #[napi]
140
126
  pub fn metadata(&mut self, metadata: String) -> &Self {
141
- self.raw_builder = self.raw_builder.clone().metadata(metadata);
127
+ let mut builder = self.raw_builder.lock();
128
+ *builder = builder.clone().metadata(metadata);
142
129
  self
143
130
  }
144
131
 
@@ -150,7 +137,8 @@ impl NgrokSessionBuilder {
150
137
  /// [server_addr parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#server_addr
151
138
  #[napi]
152
139
  pub fn server_addr(&mut self, addr: String) -> &Self {
153
- self.raw_builder = self.raw_builder.clone().server_addr(addr).clone();
140
+ let mut builder = self.raw_builder.lock();
141
+ *builder = builder.clone().server_addr(addr);
154
142
  self
155
143
  }
156
144
 
@@ -169,20 +157,17 @@ impl NgrokSessionBuilder {
169
157
  // create threadsafe function
170
158
  let tsfn = create_no_io_tsfn(handler);
171
159
  // register stop handler
172
- self.raw_builder = self
173
- .raw_builder
174
- .clone()
175
- .handle_stop_command(move |_req| {
176
- let tsfn = tsfn.clone();
177
- async move {
178
- tsfn.clone()
179
- .lock()
180
- .await
181
- .call((), ThreadsafeFunctionCallMode::NonBlocking);
182
- Ok(())
183
- }
184
- })
185
- .clone();
160
+ let mut builder = self.raw_builder.lock();
161
+ *builder = builder.clone().handle_stop_command(move |_req| {
162
+ let tsfn = tsfn.clone();
163
+ async move {
164
+ tsfn.clone()
165
+ .lock()
166
+ .await
167
+ .call((), ThreadsafeFunctionCallMode::NonBlocking);
168
+ Ok(())
169
+ }
170
+ });
186
171
  self
187
172
  }
188
173
 
@@ -202,20 +187,17 @@ impl NgrokSessionBuilder {
202
187
  // create threadsafe function
203
188
  let tsfn = create_no_io_tsfn(handler);
204
189
  // register restart handler
205
- self.raw_builder = self
206
- .raw_builder
207
- .clone()
208
- .handle_restart_command(move |_req| {
209
- let tsfn = tsfn.clone();
210
- async move {
211
- tsfn.clone()
212
- .lock()
213
- .await
214
- .call((), ThreadsafeFunctionCallMode::NonBlocking);
215
- Ok(())
216
- }
217
- })
218
- .clone();
190
+ let mut builder = self.raw_builder.lock();
191
+ *builder = builder.clone().handle_restart_command(move |_req| {
192
+ let tsfn = tsfn.clone();
193
+ async move {
194
+ tsfn.clone()
195
+ .lock()
196
+ .await
197
+ .call((), ThreadsafeFunctionCallMode::NonBlocking);
198
+ Ok(())
199
+ }
200
+ });
219
201
  self
220
202
  }
221
203
 
@@ -242,25 +224,22 @@ impl NgrokSessionBuilder {
242
224
  .expect("Failed to create update callback function"),
243
225
  ));
244
226
  // register update handler
245
- self.raw_builder = self
246
- .raw_builder
247
- .clone()
248
- .handle_update_command(move |req: Update| {
249
- let tsfn = tsfn.clone();
250
- let update = UpdateRequest {
251
- version: req.version,
252
- permit_major_version: req.permit_major_version,
253
- };
227
+ let mut builder = self.raw_builder.lock();
228
+ *builder = builder.clone().handle_update_command(move |req: Update| {
229
+ let tsfn = tsfn.clone();
230
+ let update = UpdateRequest {
231
+ version: req.version,
232
+ permit_major_version: req.permit_major_version,
233
+ };
254
234
 
255
- async move {
256
- tsfn.clone()
257
- .lock()
258
- .await
259
- .call(update, ThreadsafeFunctionCallMode::NonBlocking);
260
- Ok(())
261
- }
262
- })
263
- .clone();
235
+ async move {
236
+ tsfn.clone()
237
+ .lock()
238
+ .await
239
+ .call(update, ThreadsafeFunctionCallMode::NonBlocking);
240
+ Ok(())
241
+ }
242
+ });
264
243
  self
265
244
  }
266
245
 
@@ -271,10 +250,16 @@ impl NgrokSessionBuilder {
271
250
  /// Attempt to establish an ngrok session using the current configuration.
272
251
  #[napi]
273
252
  pub async fn connect(&self) -> Result<NgrokSession> {
274
- self.raw_builder
253
+ let builder = self.raw_builder.lock().clone();
254
+ builder
275
255
  .connect()
276
256
  .await
277
- .map(|s| NgrokSession { raw_session: s })
257
+ .map(|s| {
258
+ info!("Session created");
259
+ NgrokSession {
260
+ raw_session: Arc::new(SyncMutex::new(s)),
261
+ }
262
+ })
278
263
  .map_err(|e| napi_err(format!("failed to connect session, {e:?}")))
279
264
  }
280
265
  }
@@ -283,7 +268,7 @@ impl NgrokSessionBuilder {
283
268
  #[napi(custom_finalize)]
284
269
  struct NgrokSession {
285
270
  #[allow(dead_code)]
286
- raw_session: Session,
271
+ raw_session: Arc<SyncMutex<Session>>,
287
272
  }
288
273
 
289
274
  #[napi]
@@ -292,32 +277,37 @@ impl NgrokSession {
292
277
  /// Start building a tunnel backing an HTTP endpoint.
293
278
  #[napi]
294
279
  pub fn http_endpoint(&self) -> NgrokHttpTunnelBuilder {
295
- NgrokHttpTunnelBuilder::new(self.raw_session.clone(), self.raw_session.http_endpoint())
280
+ let session = self.raw_session.lock().clone();
281
+ NgrokHttpTunnelBuilder::new(session.clone(), session.http_endpoint())
296
282
  }
297
283
 
298
284
  /// Start building a tunnel backing a TCP endpoint.
299
285
  #[napi]
300
286
  pub fn tcp_endpoint(&self) -> NgrokTcpTunnelBuilder {
301
- NgrokTcpTunnelBuilder::new(self.raw_session.clone(), self.raw_session.tcp_endpoint())
287
+ let session = self.raw_session.lock().clone();
288
+ NgrokTcpTunnelBuilder::new(session.clone(), session.tcp_endpoint())
302
289
  }
303
290
 
304
291
  /// Start building a tunnel backing a TLS endpoint.
305
292
  #[napi]
306
293
  pub fn tls_endpoint(&self) -> NgrokTlsTunnelBuilder {
307
- NgrokTlsTunnelBuilder::new(self.raw_session.clone(), self.raw_session.tls_endpoint())
294
+ let session = self.raw_session.lock().clone();
295
+ NgrokTlsTunnelBuilder::new(session.clone(), session.tls_endpoint())
308
296
  }
309
297
 
310
298
  /// Start building a labeled tunnel.
311
299
  #[napi]
312
300
  pub fn labeled_tunnel(&self) -> NgrokLabeledTunnelBuilder {
313
- NgrokLabeledTunnelBuilder::new(self.raw_session.clone(), self.raw_session.labeled_tunnel())
301
+ let session = self.raw_session.lock().clone();
302
+ NgrokLabeledTunnelBuilder::new(session.clone(), session.labeled_tunnel())
314
303
  }
315
304
 
305
+ /// Close a tunnel with the given ID.
316
306
  #[napi]
317
307
  pub async fn close_tunnel(&self, id: String) -> Result<()> {
308
+ let session = self.raw_session.lock().clone();
318
309
  // close tunnel
319
- let res = self
320
- .raw_session
310
+ let res = session
321
311
  .close_tunnel(id.clone())
322
312
  .await
323
313
  .map_err(|e| napi_err(format!("failed to close tunnel, {e:?}")));
package/src/tcp.rs CHANGED
@@ -8,7 +8,8 @@ impl NgrokTcpTunnelBuilder {
8
8
  /// The TCP address to request for this edge.
9
9
  #[napi]
10
10
  pub fn remote_addr(&mut self, remote_addr: String) -> &Self {
11
- self.tunnel_builder = self.tunnel_builder.clone().remote_addr(remote_addr);
11
+ let mut builder = self.tunnel_builder.lock();
12
+ *builder = builder.clone().remote_addr(remote_addr);
12
13
  self
13
14
  }
14
15
  }
package/src/tls.rs CHANGED
@@ -10,14 +10,15 @@ impl NgrokTlsTunnelBuilder {
10
10
  /// The domain to request for this edge.
11
11
  #[napi]
12
12
  pub fn domain(&mut self, domain: String) -> &Self {
13
- self.tunnel_builder = self.tunnel_builder.clone().domain(domain);
13
+ let mut builder = self.tunnel_builder.lock();
14
+ *builder = builder.clone().domain(domain);
14
15
  self
15
16
  }
16
17
  /// Certificates to use for client authentication at the ngrok edge.
17
18
  #[napi]
18
19
  pub fn mutual_tlsca(&mut self, mutual_tlsca: Uint8Array) -> &Self {
19
- self.tunnel_builder = self
20
- .tunnel_builder
20
+ let mut builder = self.tunnel_builder.lock();
21
+ *builder = builder
21
22
  .clone()
22
23
  .mutual_tlsca(Bytes::from(mutual_tlsca.to_vec()));
23
24
  self
@@ -25,7 +26,8 @@ impl NgrokTlsTunnelBuilder {
25
26
  /// The key to use for TLS termination at the ngrok edge in PEM format.
26
27
  #[napi]
27
28
  pub fn termination(&mut self, cert_pem: Uint8Array, key_pem: Uint8Array) -> &Self {
28
- self.tunnel_builder = self.tunnel_builder.clone().termination(
29
+ let mut builder = self.tunnel_builder.lock();
30
+ *builder = builder.clone().termination(
29
31
  Bytes::from(cert_pem.to_vec()),
30
32
  Bytes::from(key_pem.to_vec()),
31
33
  );
package/src/tunnel.rs CHANGED
@@ -1,8 +1,10 @@
1
1
  use std::{
2
2
  collections::HashMap,
3
+ io,
3
4
  sync::Arc,
4
5
  };
5
6
 
7
+ use async_trait::async_trait;
6
8
  use lazy_static::lazy_static;
7
9
  use napi::bindgen_prelude::*;
8
10
  use napi_derive::napi;
@@ -19,7 +21,10 @@ use ngrok::{
19
21
  Session,
20
22
  };
21
23
  use tokio::sync::Mutex;
22
- use tracing::debug;
24
+ use tracing::{
25
+ debug,
26
+ info,
27
+ };
23
28
 
24
29
  use crate::napi_err;
25
30
 
@@ -27,7 +32,16 @@ lazy_static! {
27
32
  // tunnel references to be kept until explicit close to prevent nodejs gc from dropping them.
28
33
  // the tunnel wrapper object, and the underlying tunnel, both have references to the Session
29
34
  // so the Session is safe from premature dropping.
30
- static ref GLOBAL_TUNNELS: Mutex<HashMap<String,Arc<Mutex<dyn Tunnel>>>> = Mutex::new(HashMap::new());
35
+ static ref GLOBAL_TUNNELS: Mutex<HashMap<String,Arc<Mutex<dyn ExtendedTunnel>>>> = Mutex::new(HashMap::new());
36
+ }
37
+
38
+ /// The TunnelExt cannot be turned into an object since it contains generics, so implementing
39
+ /// a proxy trait without generics which can be the dyn type stored in the global map.
40
+ #[async_trait]
41
+ pub trait ExtendedTunnel: Tunnel {
42
+ async fn fwd_tcp(&mut self, addr: String) -> core::result::Result<(), io::Error>;
43
+ #[cfg(not(target_os = "windows"))]
44
+ async fn fwd_unix(&mut self, addr: String) -> core::result::Result<(), io::Error>;
31
45
  }
32
46
 
33
47
  macro_rules! make_tunnel_type {
@@ -39,10 +53,11 @@ macro_rules! make_tunnel_type {
39
53
  #[allow(dead_code)]
40
54
  pub(crate) struct $wrapper {
41
55
  id: String,
56
+ forwards_to: String,
57
+ metadata: String,
42
58
  url: String,
43
59
  proto: String,
44
60
  session: Session,
45
- inner: Option<Arc<Mutex<$tunnel>>>,
46
61
  }
47
62
 
48
63
  #[napi]
@@ -50,17 +65,20 @@ macro_rules! make_tunnel_type {
50
65
  impl $wrapper {
51
66
  pub(crate) async fn new(session: Session, raw_tunnel: $tunnel) -> Self {
52
67
  let id = raw_tunnel.id().to_string();
68
+ let forwards_to = raw_tunnel.forwards_to().to_string();
69
+ let metadata = raw_tunnel.metadata().to_string();
53
70
  let url = raw_tunnel.url().to_string();
54
71
  let proto = raw_tunnel.proto().to_string();
55
- let arc = Arc::new(Mutex::new(raw_tunnel));
72
+ info!("Created tunnel {id:?} with url {url:?}");
56
73
  // keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
57
- GLOBAL_TUNNELS.lock().await.insert(id.clone(), arc.clone());
74
+ GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Mutex::new(raw_tunnel)));
58
75
  $wrapper {
59
76
  id,
77
+ forwards_to,
78
+ metadata,
60
79
  url,
61
80
  proto,
62
81
  session,
63
- inner: Some(arc),
64
82
  }
65
83
  }
66
84
 
@@ -87,9 +105,10 @@ macro_rules! make_tunnel_type {
87
105
  #[allow(dead_code)]
88
106
  pub(crate) struct $wrapper {
89
107
  id: String,
108
+ forwards_to: String,
109
+ metadata: String,
90
110
  labels: HashMap<String,String>,
91
111
  session: Session,
92
- inner: Option<Arc<Mutex<$tunnel>>>,
93
112
  }
94
113
 
95
114
  #[napi]
@@ -97,15 +116,18 @@ macro_rules! make_tunnel_type {
97
116
  impl $wrapper {
98
117
  pub(crate) async fn new(session: Session, raw_tunnel: $tunnel) -> Self {
99
118
  let id = raw_tunnel.id().to_string();
119
+ let forwards_to = raw_tunnel.forwards_to().to_string();
120
+ let metadata = raw_tunnel.metadata().to_string();
100
121
  let labels = raw_tunnel.labels().clone();
101
- let arc = Arc::new(Mutex::new(raw_tunnel));
122
+ info!("Created tunnel {id:?} with labels {labels:?}");
102
123
  // keep a tunnel reference until an explicit call to close to prevent nodejs gc dropping it
103
- GLOBAL_TUNNELS.lock().await.insert(id.clone(), arc.clone());
124
+ GLOBAL_TUNNELS.lock().await.insert(id.clone(), Arc::new(Mutex::new(raw_tunnel)));
104
125
  $wrapper {
105
126
  id,
127
+ forwards_to,
128
+ metadata,
106
129
  labels,
107
130
  session,
108
- inner: Some(arc),
109
131
  }
110
132
  }
111
133
 
@@ -121,26 +143,57 @@ macro_rules! make_tunnel_type {
121
143
 
122
144
  // all tunnels get these
123
145
  ($wrapper:ident, $tunnel:tt) => {
146
+ #[async_trait]
147
+ impl ExtendedTunnel for $tunnel {
148
+ async fn fwd_tcp(&mut self, addr: String) -> core::result::Result<(), io::Error> {
149
+ self.forward_tcp(addr).await
150
+ }
151
+ #[cfg(not(target_os = "windows"))]
152
+ async fn fwd_unix(&mut self, addr: String) -> core::result::Result<(), io::Error> {
153
+ self.forward_unix(addr).await
154
+ }
155
+ }
156
+
124
157
  #[napi]
125
158
  #[allow(dead_code)]
126
159
  impl $wrapper {
127
- /// The ID of this tunnel, assigned by the remote server.
160
+ /// Returns a tunnel's unique ID.
128
161
  #[napi]
129
162
  pub fn id(&self) -> String {
130
163
  self.id.clone()
131
164
  }
132
165
 
166
+ /// Returns a human-readable string presented in the ngrok dashboard
167
+ /// and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
168
+ /// [TcpTunnelBuilder::forwards_to], etc. to set this value
169
+ /// explicitly.
170
+ #[napi]
171
+ pub fn forwards_to(&self) -> String {
172
+ self.forwards_to.clone()
173
+ }
174
+
175
+ /// Returns the arbitrary metadata string for this tunnel.
176
+ #[napi]
177
+ pub fn metadata(&self) -> String {
178
+ self.metadata.clone()
179
+ }
180
+
133
181
  /// Forward incoming tunnel connections to the provided TCP address.
134
182
  #[napi]
135
183
  pub async fn forward_tcp(&self, addr: String) -> Result<()> {
184
+ info!("Tunnel {:?} TCP forwarding to {addr:?}", &self.id);
136
185
  // we must clone the Arc before locking so we have a local reference
137
186
  // to the mutex to unlock if this struct is dropped.
138
- let res = self.inner.as_ref()
187
+ let arc = GLOBAL_TUNNELS.lock().await
188
+ .get(&self.id)
139
189
  .ok_or(napi_err("Tunnel is no longer running"))?
140
- .clone() // required clone
190
+ .clone(); // required clone
191
+
192
+ // doing this as a seperate statement so the GLOBAL_TUNNELS lock is dropped
193
+ let res = arc
141
194
  .lock()
142
195
  .await
143
- .forward_tcp(addr)
196
+ .fwd_tcp(addr)
144
197
  .await
145
198
  .map_err(|e| napi_err(format!("cannot forward tcp: {e:?}")));
146
199
  debug!("forward_tcp returning");
@@ -150,16 +203,21 @@ macro_rules! make_tunnel_type {
150
203
  /// Forward incoming tunnel connections to the provided Unix socket path.
151
204
  #[napi]
152
205
  pub async fn forward_unix(&self, addr: String) -> Result<()> {
206
+ info!("Tunnel {:?} pipe forwarding to {addr:?}", &self.id);
153
207
  #[cfg(not(target_os = "windows"))]
154
208
  {
155
209
  // we must clone the Arc before locking so we have a local reference
156
210
  // to the mutex to unlock if this struct is dropped.
157
- let res = self.inner.as_ref()
211
+ let arc = GLOBAL_TUNNELS.lock().await
212
+ .get(&self.id)
158
213
  .ok_or(napi_err("Tunnel is no longer running"))?
159
- .clone() // required clone
214
+ .clone(); // required clone
215
+
216
+ // doing this as a seperate statement so the GLOBAL_TUNNELS lock is dropped
217
+ let res = arc
160
218
  .lock()
161
219
  .await
162
- .forward_unix(addr)
220
+ .fwd_unix(addr)
163
221
  .await
164
222
  .map_err(|e| napi_err(format!("cannot forward unix: {e:?}")));
165
223
  debug!("forward_unix returning");
@@ -171,35 +229,32 @@ macro_rules! make_tunnel_type {
171
229
  }
172
230
 
173
231
  /// Close the tunnel.
232
+ ///
233
+ /// This is an RPC call that must be `.await`ed.
234
+ /// It is equivalent to calling `Session::close_tunnel` with this
235
+ /// tunnel's ID.
174
236
  #[napi]
175
237
  pub async fn close(&self) -> Result<()> {
176
238
  debug!("{} closing, id: {}", stringify!($wrapper), self.id);
177
- // drop our internal reference to the tunnel, giving drop control of the tunnel back to the nodejs.
178
- GLOBAL_TUNNELS.lock().await.remove(&self.id);
179
239
 
180
240
  // we may not be able to lock our reference to the tunnel due to the forward_* calls which
181
241
  // continuously accept-loop while the tunnel is active, so calling close on the Session.
182
- self.session.close_tunnel(self.id.clone())
242
+ let res = self.session.close_tunnel(self.id.clone())
183
243
  .await
184
- .map_err(|e| napi_err(format!("error closing tunnel: {e:?}")))
244
+ .map_err(|e| napi_err(format!("error closing tunnel: {e:?}")));
245
+
246
+ // drop our internal reference to the tunnel after awaiting close
247
+ GLOBAL_TUNNELS.lock().await.remove(&self.id);
248
+
249
+ res
185
250
  }
186
251
  }
187
252
 
253
+ #[allow(unused_mut)]
188
254
  impl ObjectFinalize for $wrapper {
189
- fn finalize(mut self, env: Env) -> Result<()> {
255
+ fn finalize(mut self, _env: Env) -> Result<()> {
190
256
  debug!("{} finalize, id: {}", stringify!($wrapper), self.id);
191
- // take control of inner Tunnel, removing the reference from this evaporating object
192
- let inner = self.inner.take();
193
-
194
- // send inner Tunnel into a tokio spawn to be dropped, as dropping spawns a tokio task but
195
- // there is no tokio context here.
196
- env.spawn_future(
197
- async move {
198
- debug!("{} dropping tunnel reference, id: {}", stringify!($wrapper), self.id);
199
- drop(inner);
200
- Ok(())
201
- })
202
- .map(|_| ())
257
+ Ok(())
203
258
  }
204
259
  }
205
260
  };