@ngrok/ngrok 0.5.2 → 0.7.0

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 (58) hide show
  1. package/CHANGELOG.md +41 -28
  2. package/CODE_OF_CONDUCT.md +45 -0
  3. package/Cargo.toml +8 -2
  4. package/README.md +161 -21
  5. package/docs/assets/highlight.css +13 -6
  6. package/docs/assets/search.js +1 -1
  7. package/docs/classes/NgrokHttpTunnelBuilder.html +59 -22
  8. package/docs/classes/NgrokLabeledTunnelBuilder.html +42 -5
  9. package/docs/classes/NgrokSession.html +24 -9
  10. package/docs/classes/NgrokSessionBuilder.html +20 -17
  11. package/docs/classes/NgrokTcpTunnelBuilder.html +46 -9
  12. package/docs/classes/NgrokTlsTunnelBuilder.html +48 -11
  13. package/docs/classes/NgrokTunnel.html +33 -35
  14. package/docs/classes/UpdateRequest.html +7 -4
  15. package/docs/functions/authtoken.html +5 -2
  16. package/docs/functions/connect.html +5 -2
  17. package/docs/functions/consoleLog.html +5 -2
  18. package/docs/functions/disconnect.html +5 -2
  19. package/docs/functions/getTunnel.html +88 -0
  20. package/docs/functions/getTunnelByUrl.html +88 -0
  21. package/docs/functions/kill.html +5 -2
  22. package/docs/functions/listen.html +5 -2
  23. package/docs/functions/listenable.html +5 -2
  24. package/docs/functions/loggingCallback.html +5 -2
  25. package/docs/functions/tunnels.html +83 -0
  26. package/docs/index.html +74 -13
  27. package/docs/interfaces/Config.html +57 -54
  28. package/docs/modules.html +7 -1
  29. package/examples/fastify/ngrok-fastify.js +1 -1
  30. package/examples/hapi/ngrok-hapi.js +1 -1
  31. package/examples/nestjs/src/main.ts +1 -1
  32. package/examples/nextjs/ngrok.config.js +1 -1
  33. package/examples/ngrok-connect-full.js +22 -23
  34. package/examples/ngrok-connect-minimal.js +3 -2
  35. package/examples/ngrok-http-full.js +1 -1
  36. package/examples/ngrok-http-minimum.js +1 -1
  37. package/examples/ngrok-labeled.js +1 -1
  38. package/examples/ngrok-tcp.js +1 -1
  39. package/examples/ngrok-tls.js +1 -1
  40. package/examples/ngrok-typescript.ts +1 -1
  41. package/examples/ngrok-windows-pipe.js +1 -1
  42. package/examples/remix/ngrok.config.js +1 -1
  43. package/examples/svelte/ngrok.config.cjs +1 -1
  44. package/examples/vue/ngrok.config.ts +1 -1
  45. package/index.d.ts +30 -6
  46. package/index.js +100 -22
  47. package/package.json +14 -14
  48. package/scripts/gc-node-drop.js +1 -1
  49. package/scripts/gc-rust-drop.js +1 -1
  50. package/src/connect.rs +17 -9
  51. package/src/http.rs +20 -22
  52. package/src/lib.rs +21 -0
  53. package/src/session.rs +61 -50
  54. package/src/tcp.rs +1 -1
  55. package/src/tls.rs +3 -5
  56. package/src/tunnel.rs +232 -110
  57. package/src/tunnel_builder.rs +34 -7
  58. package/trailer.js +96 -21
package/trailer.js CHANGED
@@ -5,6 +5,11 @@
5
5
  const net = require("net");
6
6
  const fs = require("fs");
7
7
  const os = require("os");
8
+ const path = require("path");
9
+
10
+ // wrap connect with the code for extending exception with error code
11
+ NgrokSessionBuilder.prototype._connect = NgrokSessionBuilder.prototype.connect;
12
+ NgrokSessionBuilder.prototype.connect = ngrokSessionConnect;
8
13
 
9
14
  // wrap listen with the bind code for passing to net.Server.listen()
10
15
  NgrokHttpTunnelBuilder.prototype._listen = NgrokHttpTunnelBuilder.prototype.listen;
@@ -17,19 +22,56 @@ NgrokTcpTunnelBuilder.prototype.listen = ngrokBind;
17
22
  NgrokTlsTunnelBuilder.prototype.listen = ngrokBind;
18
23
  NgrokLabeledTunnelBuilder.prototype.listen = ngrokBind;
19
24
 
25
+ NgrokHttpTunnelBuilder.prototype.listenAndServe = listenAndServe;
26
+ NgrokTcpTunnelBuilder.prototype.listenAndServe = listenAndServe;
27
+ NgrokTlsTunnelBuilder.prototype.listenAndServe = listenAndServe;
28
+ NgrokLabeledTunnelBuilder.prototype.listenAndServe = listenAndServe;
29
+
30
+ // Wrap session connect to fill in exception's errorCode
31
+ async function ngrokSessionConnect() {
32
+ try {
33
+ return await this._connect();
34
+ } catch (err) {
35
+ populateErrorCode(err);
36
+ throw err;
37
+ }
38
+ }
39
+
20
40
  // Begin listening for new connections on this tunnel,
21
41
  // and bind to a local socket so this tunnel can be
22
42
  // passed into net.Server.listen().
23
43
  async function ngrokBind(bind) {
24
- const tunnel = await this._listen();
25
- if (bind !== false) {
26
- const socket = await randomTcpSocket();
27
- tunnel.socket = socket;
28
- defineTunnelHandle(tunnel, socket);
44
+ try {
45
+ const tunnel = await this._listen();
46
+ if (bind !== false) {
47
+ const socket = await randomTcpSocket();
48
+ tunnel.socket = socket;
49
+ defineTunnelHandle(tunnel, socket);
50
+ }
51
+ return tunnel;
52
+ } catch (err) {
53
+ populateErrorCode(err);
54
+ throw err;
29
55
  }
56
+ }
57
+
58
+ /// Begin listening for new connections on this tunnel and forwarding them to the given server.
59
+ async function listenAndServe(server) {
60
+ const tunnel = await this._listen();
61
+ tunnel.socket = await ngrokListen(server, tunnel);
30
62
  return tunnel;
31
63
  }
32
64
 
65
+ function populateErrorCode(err) {
66
+ if (err.message) {
67
+ const regex = /error_code: (ERR_NGROK_\d+)$/;
68
+ const errorCode = err.message.match(regex);
69
+ if (errorCode && errorCode.length > 1) {
70
+ err.errorCode = errorCode[1];
71
+ }
72
+ }
73
+ }
74
+
33
75
  // add a 'handle' getter to the tunnel so it can be
34
76
  // passed into net.Server.listen().
35
77
  function defineTunnelHandle(tunnel, socket) {
@@ -38,7 +80,7 @@ function defineTunnelHandle(tunnel, socket) {
38
80
  Object.defineProperty(tunnel, "handle", {
39
81
  get: function () {
40
82
  // turn on forwarding now that it has been requested
41
- tunnel.forwardTcp("localhost:" + socket.address().port);
83
+ tunnel.forward("localhost:" + socket.address().port);
42
84
  return socket._handle;
43
85
  },
44
86
  });
@@ -113,30 +155,56 @@ async function ngrokLinkTcp(tunnel, server) {
113
155
  // random local port
114
156
  const socket = await asyncListen(server, { host: "localhost", port: 0 });
115
157
  // forward to socket
116
- tunnel.forwardTcp("localhost:" + socket.address().port);
158
+ tunnel.forward("localhost:" + socket.address().port);
117
159
  return socket;
118
160
  }
119
161
 
120
- async function ngrokLinkPipe(tunnel, server) {
162
+ function generatePipeFilename(tunnel, server) {
121
163
  var proposed = "tun-" + tunnel.id() + ".sock";
164
+
165
+ // windows leaves little choice
122
166
  if (platform == "win32") {
123
- proposed = "\\\\.\\pipe\\" + proposed;
167
+ return "\\\\.\\pipe\\" + proposed;
168
+ }
169
+
170
+ // try to make a directory in the current working directory
171
+ const dir = ".ngrok";
172
+ try {
173
+ fs.mkdirSync(dir);
174
+ } catch (err) {
175
+ // move on
124
176
  }
125
- var filename;
126
177
  try {
127
- fs.accessSync(process.cwd(), fs.constants.W_OK);
128
- filename = proposed;
178
+ fs.accessSync(dir, fs.constants.W_OK);
179
+ return dir + path.sep + proposed;
129
180
  } catch (err) {
130
- console.debug("Cannot write to: " + process.cwd());
131
- // try tmp. allow any exception to propagate
132
- fs.accessSync(os.tmpdir(), fs.constants.W_OK);
133
- filename = os.tmpdir() + proposed;
181
+ // move on
134
182
  }
135
183
 
136
- if (!filename) {
137
- throw new Error("no writeable directory found");
184
+ // try the OS temp directory, being careful not to exceed the maximum path length for unix sockets
185
+ // https://linux.die.net/man/7/unix
186
+ // https://unix.stackexchange.com/a/367012
187
+ if (os.tmpdir().length < 90) {
188
+ try {
189
+ fs.accessSync(os.tmpdir(), fs.constants.W_OK);
190
+ filepath = os.tmpdir() + path.sep + proposed;
191
+ if (filepath.length > 100) {
192
+ // truncate
193
+ filepath = filepath.substring(0, 100);
194
+ }
195
+ return filepath;
196
+ } catch (err) {
197
+ // move on
198
+ }
138
199
  }
139
200
 
201
+ // fallback to current working directory. allow any exception to propagate
202
+ fs.accessSync(process.cwd(), fs.constants.W_OK);
203
+ return proposed;
204
+ }
205
+
206
+ async function ngrokLinkPipe(tunnel, server) {
207
+ var filename = generatePipeFilename(tunnel);
140
208
  // begin listening
141
209
  const socket = await asyncListen(server, { path: filename });
142
210
  // tighten permissions
@@ -148,7 +216,8 @@ async function ngrokLinkPipe(tunnel, server) {
148
216
  console.debug("Cannot change permissions of file: " + filename);
149
217
  }
150
218
  // forward tunnel
151
- tunnel.forwardPipe(filename);
219
+ const proto = platform == "win32" ? "pipe:" : "unix:";
220
+ tunnel.forward(proto + filename);
152
221
  socket.path = filename; // surface to caller
153
222
 
154
223
  return socket;
@@ -188,9 +257,10 @@ function consoleLog(level) {
188
257
  // wrap connect with code to vectorize and split out functions
189
258
  const _connect = connect;
190
259
  async function ngrokConnect(config) {
260
+ if (config == undefined) config = 80;
191
261
  if (Number.isInteger(config) || typeof config === "string" || config instanceof String) {
192
262
  address = String(config);
193
- if (!address.includes(":")) {
263
+ if (Number.isInteger(config) && !address.includes(":")) {
194
264
  address = `localhost:${address}`;
195
265
  }
196
266
  config = { addr: address };
@@ -264,7 +334,12 @@ async function ngrokConnect(config) {
264
334
  config["onStatusChange"] = true;
265
335
  }
266
336
  // call into rust
267
- return await _connect(config, on_log_event, on_connection, on_disconnection);
337
+ try {
338
+ return await _connect(config, on_log_event, on_connection, on_disconnection);
339
+ } catch (err) {
340
+ populateErrorCode(err);
341
+ throw err;
342
+ }
268
343
  }
269
344
 
270
345
  function undot(config, dotKey) {