@ngrok/ngrok 0.0.6 → 0.0.7

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.
@@ -11,14 +11,18 @@ use ngrok::{
11
11
  TlsTunnelBuilder,
12
12
  },
13
13
  prelude::*,
14
+ Session,
14
15
  };
15
16
  use tracing::debug;
16
17
 
17
- use crate::tunnel::{
18
- NgrokHttpTunnel,
19
- NgrokLabeledTunnel,
20
- NgrokTcpTunnel,
21
- NgrokTlsTunnel,
18
+ use crate::{
19
+ napi_err,
20
+ tunnel::{
21
+ NgrokHttpTunnel,
22
+ NgrokLabeledTunnel,
23
+ NgrokTcpTunnel,
24
+ NgrokTlsTunnel,
25
+ },
22
26
  };
23
27
 
24
28
  macro_rules! make_tunnel_builder {
@@ -27,14 +31,16 @@ macro_rules! make_tunnel_builder {
27
31
  #[napi(custom_finalize)]
28
32
  #[allow(dead_code)]
29
33
  pub(crate) struct $wrapper {
34
+ session: Session,
30
35
  pub(crate) tunnel_builder: $builder,
31
36
  }
32
37
 
33
38
  #[napi]
34
39
  #[allow(dead_code)]
35
40
  impl $wrapper {
36
- pub(crate) fn new(raw_tunnel_builder: $builder) -> Self {
41
+ pub(crate) fn new(session: Session, raw_tunnel_builder: $builder) -> Self {
37
42
  $wrapper {
43
+ session,
38
44
  tunnel_builder: raw_tunnel_builder,
39
45
  }
40
46
  }
@@ -49,22 +55,22 @@ macro_rules! make_tunnel_builder {
49
55
  /// Begin listening for new connections on this tunnel.
50
56
  #[napi]
51
57
  pub async fn listen(&self) -> Result<$tunnel> {
52
- self.tunnel_builder
58
+ let result = self.tunnel_builder
53
59
  .listen()
54
60
  .await
55
- .map($tunnel::new)
56
- .map_err(|e| {
57
- Error::new(
58
- Status::GenericFailure,
59
- format!("failed to start tunnel: {e:?}")
60
- )
61
- })
61
+ .map_err(|e| napi_err(format!("failed to start tunnel: {e:?}")));
62
+
63
+ // create the wrapping tunnel object via its async new()
64
+ match result {
65
+ Ok(raw_tun) => Ok($tunnel::new(self.session.clone(), raw_tun).await),
66
+ Err(val) => Err(val),
67
+ }
62
68
  }
63
69
  }
64
70
 
65
71
  impl ObjectFinalize for $wrapper {
66
72
  fn finalize(self, mut _env: Env) -> Result<()> {
67
- debug!("$wrapper finalize");
73
+ debug!("{} finalize", stringify!($wrapper));
68
74
  Ok(())
69
75
  }
70
76
  }
@@ -79,15 +85,15 @@ macro_rules! make_tunnel_builder {
79
85
  /// Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
80
86
  /// Call multiple times to add additional CIDR ranges.
81
87
  #[napi]
82
- pub fn allow_cidr_string(&mut self, cidr: String) -> &Self {
83
- self.tunnel_builder = self.tunnel_builder.clone().allow_cidr_string(cidr);
88
+ pub fn allow_cidr(&mut self, cidr: String) -> &Self {
89
+ self.tunnel_builder = self.tunnel_builder.clone().allow_cidr(cidr);
84
90
  self
85
91
  }
86
92
  /// Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
87
93
  /// Call multiple times to add additional CIDR ranges.
88
94
  #[napi]
89
- pub fn deny_cidr_string(&mut self, cidr: String) -> &Self {
90
- self.tunnel_builder = self.tunnel_builder.clone().deny_cidr_string(cidr);
95
+ pub fn deny_cidr(&mut self, cidr: String) -> &Self {
96
+ self.tunnel_builder = self.tunnel_builder.clone().deny_cidr(cidr);
91
97
  self
92
98
  }
93
99
  /// The version of PROXY protocol to use with this tunnel, None if not using.
package/trailer.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export function getSocket(tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): net.Server
2
+ export function listen(server: net.Server, tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
package/trailer.js CHANGED
@@ -23,7 +23,7 @@ async function getSocket(tunnel) {
23
23
  }
24
24
  // use tcp socket with random local port
25
25
  const server = new net.Server();
26
- server.listen(0);
26
+ await server.listen(0);
27
27
  // forward to this socket
28
28
  tunnel.forwardTcp('localhost:' + server.address().port);
29
29
  // surface to caller
@@ -40,20 +40,21 @@ async function ngrokListen(server, tunnel) {
40
40
  // todo: named pipe on windows: https://nodejs.org/api/net.html#ipc-support
41
41
 
42
42
  // attempt unix socket
43
- ngrokLinkUnix(tunnel, server)
44
- .then(() => {})
45
- .catch(function(err) {
46
- console.debug("Using TCP socket. " + err);
47
- // fallback to tcp socket
48
- ngrokLinkTcp(tunnel, server);
49
- });
43
+ try {
44
+ await ngrokLinkUnix(tunnel, server);
45
+ } catch (err) {
46
+ console.debug("Using TCP socket. " + err);
47
+ // fallback to tcp socket
48
+ await ngrokLinkTcp(tunnel, server);
49
+ }
50
50
 
51
51
  server.tunnel = tunnel; // surface to caller
52
+ return tunnel;
52
53
  }
53
54
 
54
55
  async function ngrokLinkTcp(tunnel, server) {
55
56
  // random local port
56
- server.listen(0);
57
+ await server.listen(0);
57
58
  // forward to socket
58
59
  tunnel.forwardTcp('localhost:' + server.address().port);
59
60
  }
@@ -76,7 +77,7 @@ async function ngrokLinkUnix(tunnel, server) {
76
77
  }
77
78
 
78
79
  // begin listening
79
- server.listen({path: filename});
80
+ await server.listen({path: filename});
80
81
  // tighten permissions
81
82
  try {
82
83
  fs.chmodSync(filename, fs.constants.S_IRWXU);
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es5",
4
+ "module": "commonjs"
5
+ }
6
+ }