@ngrok/ngrok 0.0.6 → 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 (51) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/CONTRIBUTING.md +4 -4
  3. package/Cargo.toml +5 -2
  4. package/README.md +11 -11
  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 +18 -8
  28. package/examples/ngrok-http-minimum.js +1 -3
  29. package/examples/ngrok-labeled.js +2 -4
  30. package/examples/ngrok-tcp.js +4 -6
  31. package/examples/ngrok-tls.js +6 -8
  32. package/examples/ngrok-typescript.ts +64 -0
  33. package/examples/ngrok-winston.js +24 -0
  34. package/index.d.ts +193 -41
  35. package/index.js +114 -27
  36. package/package.json +25 -17
  37. package/post-build.js +23 -16
  38. package/scripts/gc-node-drop.js +42 -0
  39. package/scripts/gc-rust-drop.js +47 -0
  40. package/src/http.rs +64 -42
  41. package/src/lib.rs +10 -0
  42. package/src/logging.rs +134 -0
  43. package/src/session.rs +239 -63
  44. package/src/tcp.rs +2 -1
  45. package/src/tls.rs +10 -18
  46. package/src/tunnel.rs +146 -22
  47. package/src/tunnel_builder.rs +46 -28
  48. package/trailer.d.ts +9 -0
  49. package/trailer.js +111 -25
  50. package/tsconfig.json +6 -0
  51. package/examples/ngrok-get-socket.js +0 -13
package/index.d.ts CHANGED
@@ -4,41 +4,113 @@
4
4
  /* auto-generated by NAPI-RS */
5
5
 
6
6
  /**
7
- * turn on tracing subscriber which obeys NGROK_LOG env variable, e.g.:
8
- * process.env.NGROK_LOG = 'ngrok=debug';
9
- * ngrok.tracingSubscriber();
7
+ * Register a callback function that will receive logging event information.
8
+ * An absent callback will unregister an existing callback function.
9
+ * The log level defaults to INFO, it can be set to one of ERROR, WARN, INFO, DEBUG, or TRACE.
10
10
  */
11
- export function tracingSubscriber(): void
11
+ export function loggingCallback(callback?: (level: string, target: string, message: string) => void, level?: string): void
12
12
  /** The builder for an ngrok session. */
13
13
  export class NgrokSessionBuilder {
14
14
  /** Create a new session builder */
15
15
  constructor()
16
- /** Authenticate the ngrok session with the given authtoken. */
16
+ /**
17
+ * Configures the session to authenticate with the provided authtoken. You
18
+ * can [find your existing authtoken] or [create a new one] in the ngrok
19
+ * dashboard.
20
+ *
21
+ * See the [authtoken parameter in the ngrok docs] for additional details.
22
+ *
23
+ * [find your existing authtoken]: https://dashboard.ngrok.com/get-started/your-authtoken
24
+ * [create a new one]: https://dashboard.ngrok.com/tunnels/authtokens
25
+ * [authtoken parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#authtoken
26
+ */
17
27
  authtoken(authtoken: string): this
18
28
  /**
19
- * Authenticate using the authtoken in the `NGROK_AUTHTOKEN` environment
20
- * variable.
29
+ * Shortcut for calling [SessionBuilder::authtoken] with the value of the
30
+ * NGROK_AUTHTOKEN environment variable.
21
31
  */
22
32
  authtokenFromEnv(): this
23
33
  /**
24
- * Set the heartbeat interval for the session.
25
- * This value determines how often we send application level
26
- * heartbeats to the server go check connection liveness.
34
+ * Configures how often the session will send heartbeat messages to the ngrok
35
+ * service to check session liveness.
36
+ *
37
+ * See the [heartbeat_interval parameter in the ngrok docs] for additional
38
+ * details.
39
+ *
40
+ * [heartbeat_interval parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#heartbeat_interval
27
41
  */
28
42
  heartbeatInterval(heartbeatInterval: number): this
29
43
  /**
30
- * Set the heartbeat tolerance for the session.
31
- * If the session's heartbeats are outside of their interval by this duration,
32
- * the server will assume the session is dead and close it.
44
+ * Configures the duration to wait for a response to a heartbeat before
45
+ * assuming the session connection is dead and attempting to reconnect.
46
+ *
47
+ * See the [heartbeat_tolerance parameter in the ngrok docs] for additional
48
+ * details.
49
+ *
50
+ * [heartbeat_tolerance parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#heartbeat_tolerance
33
51
  */
34
52
  heartbeatTolerance(heartbeatTolerance: number): this
35
53
  /**
36
- * Use the provided opaque metadata string for this session.
37
- * Viewable from the ngrok dashboard or API.
54
+ * Configures the opaque, machine-readable metadata string for this session.
55
+ * Metadata is made available to you in the ngrok dashboard and the Agents API
56
+ * resource. It is a useful way to allow you to uniquely identify sessions. We
57
+ * suggest encoding the value in a structured format like JSON.
58
+ *
59
+ * See the [metdata parameter in the ngrok docs] for additional details.
60
+ *
61
+ * [metdata parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#metadata
38
62
  */
39
63
  metadata(metadata: string): this
40
- /** Connect to the provided ngrok server address. */
64
+ /**
65
+ * Configures the network address to dial to connect to the ngrok service.
66
+ * Use this option only if you are connecting to a custom agent ingress.
67
+ *
68
+ * See the [server_addr parameter in the ngrok docs] for additional details.
69
+ *
70
+ * [server_addr parameter in the ngrok docs]: https://ngrok.com/docs/ngrok-agent/config#server_addr
71
+ */
41
72
  serverAddr(addr: string): this
73
+ /**
74
+ * Configures a function which is called when the ngrok service requests that
75
+ * this [Session] stops. Your application may choose to interpret this callback
76
+ * as a request to terminate the [Session] or the entire process.
77
+ *
78
+ * Errors returned by this function will be visible to the ngrok dashboard or
79
+ * API as the response to the Stop operation.
80
+ *
81
+ * Do not block inside this callback. It will cause the Dashboard or API
82
+ * stop operation to time out. Do not call [std::process::exit] inside this
83
+ * callback, it will also cause the operation to time out.
84
+ */
85
+ handleStopCommand(handler: () => void): this
86
+ /**
87
+ * Configures a function which is called when the ngrok service requests
88
+ * that this [Session] updates. Your application may choose to interpret
89
+ * this callback as a request to restart the [Session] or the entire
90
+ * process.
91
+ *
92
+ * Errors returned by this function will be visible to the ngrok dashboard or
93
+ * API as the response to the Restart operation.
94
+ *
95
+ * Do not block inside this callback. It will cause the Dashboard or API
96
+ * stop operation to time out. Do not call [std::process::exit] inside this
97
+ * callback, it will also cause the operation to time out.
98
+ */
99
+ handleRestartCommand(handler: () => void): this
100
+ /**
101
+ * Configures a function which is called when the ngrok service requests
102
+ * that this [Session] updates. Your application may choose to interpret
103
+ * this callback as a request to update its configuration, itself, or to
104
+ * invoke some other application-specific behavior.
105
+ *
106
+ * Errors returned by this function will be visible to the ngrok dashboard or
107
+ * API as the response to the Restart operation.
108
+ *
109
+ * Do not block inside this callback. It will cause the Dashboard or API
110
+ * stop operation to time out. Do not call [std::process::exit] inside this
111
+ * callback, it will also cause the operation to time out.
112
+ */
113
+ handleUpdateCommand(handler: (update: UpdateRequest) => void): this
42
114
  /** Attempt to establish an ngrok session using the current configuration. */
43
115
  connect(): Promise<NgrokSession>
44
116
  }
@@ -52,6 +124,14 @@ export class NgrokSession {
52
124
  tlsEndpoint(): NgrokTlsTunnelBuilder
53
125
  /** Start building a labeled tunnel. */
54
126
  labeledTunnel(): NgrokLabeledTunnelBuilder
127
+ /** Close a tunnel with the given ID. */
128
+ closeTunnel(id: string): Promise<void>
129
+ }
130
+ export class UpdateRequest {
131
+ /** The version that the agent is requested to update to. */
132
+ version: string
133
+ /** Whether or not updating to the same major version is sufficient. */
134
+ permitMajorVersion: boolean
55
135
  }
56
136
  /**r" An ngrok tunnel backing an HTTP endpoint. */
57
137
  export class NgrokHttpTunnel {
@@ -59,12 +139,29 @@ export class NgrokHttpTunnel {
59
139
  url(): string
60
140
  /** The protocol of the endpoint that this tunnel backs. */
61
141
  proto(): string
62
- /** The ID of this tunnel, assigned by the remote server. */
142
+ /** Returns a tunnel's unique ID. */
63
143
  id(): string
144
+ /**
145
+ * Returns a human-readable string presented in the ngrok dashboard
146
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
147
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
148
+ * explicitly.
149
+ */
150
+ forwardsTo(): string
151
+ /** Returns the arbitrary metadata string for this tunnel. */
152
+ metadata(): string
64
153
  /** Forward incoming tunnel connections to the provided TCP address. */
65
154
  forwardTcp(addr: string): Promise<void>
66
155
  /** Forward incoming tunnel connections to the provided Unix socket path. */
67
156
  forwardUnix(addr: string): Promise<void>
157
+ /**
158
+ * Close the tunnel.
159
+ *
160
+ * This is an RPC call that must be `.await`ed.
161
+ * It is equivalent to calling `Session::close_tunnel` with this
162
+ * tunnel's ID.
163
+ */
164
+ close(): Promise<void>
68
165
  }
69
166
  /**r" An ngrok tunnel backing a TCP endpoint. */
70
167
  export class NgrokTcpTunnel {
@@ -72,12 +169,29 @@ export class NgrokTcpTunnel {
72
169
  url(): string
73
170
  /** The protocol of the endpoint that this tunnel backs. */
74
171
  proto(): string
75
- /** The ID of this tunnel, assigned by the remote server. */
172
+ /** Returns a tunnel's unique ID. */
76
173
  id(): string
174
+ /**
175
+ * Returns a human-readable string presented in the ngrok dashboard
176
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
177
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
178
+ * explicitly.
179
+ */
180
+ forwardsTo(): string
181
+ /** Returns the arbitrary metadata string for this tunnel. */
182
+ metadata(): string
77
183
  /** Forward incoming tunnel connections to the provided TCP address. */
78
184
  forwardTcp(addr: string): Promise<void>
79
185
  /** Forward incoming tunnel connections to the provided Unix socket path. */
80
186
  forwardUnix(addr: string): Promise<void>
187
+ /**
188
+ * Close the tunnel.
189
+ *
190
+ * This is an RPC call that must be `.await`ed.
191
+ * It is equivalent to calling `Session::close_tunnel` with this
192
+ * tunnel's ID.
193
+ */
194
+ close(): Promise<void>
81
195
  }
82
196
  /**r" An ngrok tunnel bcking a TLS endpoint. */
83
197
  export class NgrokTlsTunnel {
@@ -85,23 +199,57 @@ export class NgrokTlsTunnel {
85
199
  url(): string
86
200
  /** The protocol of the endpoint that this tunnel backs. */
87
201
  proto(): string
88
- /** The ID of this tunnel, assigned by the remote server. */
202
+ /** Returns a tunnel's unique ID. */
89
203
  id(): string
204
+ /**
205
+ * Returns a human-readable string presented in the ngrok dashboard
206
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
207
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
208
+ * explicitly.
209
+ */
210
+ forwardsTo(): string
211
+ /** Returns the arbitrary metadata string for this tunnel. */
212
+ metadata(): string
90
213
  /** Forward incoming tunnel connections to the provided TCP address. */
91
214
  forwardTcp(addr: string): Promise<void>
92
215
  /** Forward incoming tunnel connections to the provided Unix socket path. */
93
216
  forwardUnix(addr: string): Promise<void>
217
+ /**
218
+ * Close the tunnel.
219
+ *
220
+ * This is an RPC call that must be `.await`ed.
221
+ * It is equivalent to calling `Session::close_tunnel` with this
222
+ * tunnel's ID.
223
+ */
224
+ close(): Promise<void>
94
225
  }
95
226
  /**r" A labeled ngrok tunnel. */
96
227
  export class NgrokLabeledTunnel {
97
228
  /** The labels this tunnel was started with. */
98
229
  labels(): Record<string, string>
99
- /** The ID of this tunnel, assigned by the remote server. */
230
+ /** Returns a tunnel's unique ID. */
100
231
  id(): string
232
+ /**
233
+ * Returns a human-readable string presented in the ngrok dashboard
234
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
235
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
236
+ * explicitly.
237
+ */
238
+ forwardsTo(): string
239
+ /** Returns the arbitrary metadata string for this tunnel. */
240
+ metadata(): string
101
241
  /** Forward incoming tunnel connections to the provided TCP address. */
102
242
  forwardTcp(addr: string): Promise<void>
103
243
  /** Forward incoming tunnel connections to the provided Unix socket path. */
104
244
  forwardUnix(addr: string): Promise<void>
245
+ /**
246
+ * Close the tunnel.
247
+ *
248
+ * This is an RPC call that must be `.await`ed.
249
+ * It is equivalent to calling `Session::close_tunnel` with this
250
+ * tunnel's ID.
251
+ */
252
+ close(): Promise<void>
105
253
  }
106
254
  /**r" An ngrok tunnel backing an HTTP endpoint. */
107
255
  export class NgrokHttpTunnelBuilder {
@@ -140,12 +288,12 @@ export class NgrokHttpTunnelBuilder {
140
288
  * OAuth configuration.
141
289
  * If not called, OAuth is disabled.
142
290
  */
143
- oauth(provider: string, allowEmails: Array<string>, allowDomains: Array<string>, scopes: Array<string>): this
291
+ oauth(provider: string, allowEmails?: Array<string> | undefined | null, allowDomains?: Array<string> | undefined | null, scopes?: Array<string> | undefined | null): this
144
292
  /**
145
293
  * OIDC configuration.
146
294
  * If not called, OIDC is disabled.
147
295
  */
148
- oidc(issuerUrl: string, clientId: string, clientSecret: string, allowEmails: Array<string>, allowDomains: Array<string>, scopes: Array<string>): this
296
+ oidc(issuerUrl: string, clientId: string, clientSecret: string, allowEmails?: Array<string> | undefined | null, allowDomains?: Array<string> | undefined | null, scopes?: Array<string> | undefined | null): this
149
297
  /**
150
298
  * WebhookVerification configuration.
151
299
  * If not called, WebhookVerification is disabled.
@@ -154,18 +302,18 @@ export class NgrokHttpTunnelBuilder {
154
302
  /** Tunnel-specific opaque metadata. Viewable via the API. */
155
303
  metadata(metadata: string): this
156
304
  /** Begin listening for new connections on this tunnel. */
157
- listen(): Promise<NgrokHttpTunnel>
305
+ listen(bind?: boolean | undefined | null): Promise<NgrokHttpTunnel>
158
306
  /**
159
307
  * Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
160
308
  * Call multiple times to add additional CIDR ranges.
161
309
  */
162
- allowCidrString(cidr: string): this
310
+ allowCidr(cidr: string): this
163
311
  /**
164
312
  * Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
165
313
  * Call multiple times to add additional CIDR ranges.
166
314
  */
167
- denyCidrString(cidr: string): this
168
- /** The version of PROXY protocol to use with this tunnel, None if not using. */
315
+ denyCidr(cidr: string): this
316
+ /** The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using. */
169
317
  proxyProto(proxyProto: string): this
170
318
  /**
171
319
  * Tunnel backend metadata. Viewable via the dashboard and API, but has no
@@ -180,18 +328,18 @@ export class NgrokTcpTunnelBuilder {
180
328
  /** Tunnel-specific opaque metadata. Viewable via the API. */
181
329
  metadata(metadata: string): this
182
330
  /** Begin listening for new connections on this tunnel. */
183
- listen(): Promise<NgrokTcpTunnel>
331
+ listen(bind?: boolean | undefined | null): Promise<NgrokTcpTunnel>
184
332
  /**
185
333
  * Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
186
334
  * Call multiple times to add additional CIDR ranges.
187
335
  */
188
- allowCidrString(cidr: string): this
336
+ allowCidr(cidr: string): this
189
337
  /**
190
338
  * Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
191
339
  * Call multiple times to add additional CIDR ranges.
192
340
  */
193
- denyCidrString(cidr: string): this
194
- /** The version of PROXY protocol to use with this tunnel, None if not using. */
341
+ denyCidr(cidr: string): this
342
+ /** The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using. */
195
343
  proxyProto(proxyProto: string): this
196
344
  /**
197
345
  * Tunnel backend metadata. Viewable via the dashboard and API, but has no
@@ -206,27 +354,22 @@ export class NgrokTlsTunnelBuilder {
206
354
  /** Certificates to use for client authentication at the ngrok edge. */
207
355
  mutualTlsca(mutualTlsca: Uint8Array): this
208
356
  /** The key to use for TLS termination at the ngrok edge in PEM format. */
209
- keyPem(keyPem: Uint8Array): this
210
- /**
211
- * The certificate to use for TLS termination at the ngrok edge in PEM
212
- * format.
213
- */
214
- certPem(certPem: Uint8Array): this
357
+ termination(certPem: Uint8Array, keyPem: Uint8Array): this
215
358
  /** Tunnel-specific opaque metadata. Viewable via the API. */
216
359
  metadata(metadata: string): this
217
360
  /** Begin listening for new connections on this tunnel. */
218
- listen(): Promise<NgrokTlsTunnel>
361
+ listen(bind?: boolean | undefined | null): Promise<NgrokTlsTunnel>
219
362
  /**
220
363
  * Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
221
364
  * Call multiple times to add additional CIDR ranges.
222
365
  */
223
- allowCidrString(cidr: string): this
366
+ allowCidr(cidr: string): this
224
367
  /**
225
368
  * Restriction placed on the origin of incoming connections to the edge to deny these CIDR ranges.
226
369
  * Call multiple times to add additional CIDR ranges.
227
370
  */
228
- denyCidrString(cidr: string): this
229
- /** The version of PROXY protocol to use with this tunnel, None if not using. */
371
+ denyCidr(cidr: string): this
372
+ /** The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using. */
230
373
  proxyProto(proxyProto: string): this
231
374
  /**
232
375
  * Tunnel backend metadata. Viewable via the dashboard and API, but has no
@@ -239,7 +382,16 @@ export class NgrokLabeledTunnelBuilder {
239
382
  /** Tunnel-specific opaque metadata. Viewable via the API. */
240
383
  metadata(metadata: string): this
241
384
  /** Begin listening for new connections on this tunnel. */
242
- listen(): Promise<NgrokLabeledTunnel>
385
+ listen(bind?: boolean | undefined | null): Promise<NgrokLabeledTunnel>
243
386
  /** Add a label, value pair for this tunnel. */
244
387
  label(label: string, value: string): this
245
388
  }
389
+ /** Generate, or convert a given tunnel, into one that can be passed into net.Server.listen(). */
390
+ export function listenable(tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
391
+ /** Start the given net.Server listening to a generated, or passed in, tunnel. */
392
+ export function listen(server: import("net").Server, tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
393
+ /**
394
+ * Register a console.log callback for ngrok INFO logging.
395
+ * Optionally set the logging level to one of ERROR, WARN, INFO, DEBUG, or TRACE.
396
+ */
397
+ export function consoleLog(level?: String)
package/index.js CHANGED
@@ -246,11 +246,12 @@ if (!nativeBinding) {
246
246
  throw new Error(`Failed to load native binding`)
247
247
  }
248
248
 
249
- const { NgrokSessionBuilder, tracingSubscriber, NgrokSession, NgrokHttpTunnel, NgrokTcpTunnel, NgrokTlsTunnel, NgrokLabeledTunnel, NgrokHttpTunnelBuilder, NgrokTcpTunnelBuilder, NgrokTlsTunnelBuilder, NgrokLabeledTunnelBuilder } = nativeBinding
249
+ const { loggingCallback, NgrokSessionBuilder, NgrokSession, UpdateRequest, NgrokHttpTunnel, NgrokTcpTunnel, NgrokTlsTunnel, NgrokLabeledTunnel, NgrokHttpTunnelBuilder, NgrokTcpTunnelBuilder, NgrokTlsTunnelBuilder, NgrokLabeledTunnelBuilder } = nativeBinding
250
250
 
251
+ module.exports.loggingCallback = loggingCallback
251
252
  module.exports.NgrokSessionBuilder = NgrokSessionBuilder
252
- module.exports.tracingSubscriber = tracingSubscriber
253
253
  module.exports.NgrokSession = NgrokSession
254
+ module.exports.UpdateRequest = UpdateRequest
254
255
  module.exports.NgrokHttpTunnel = NgrokHttpTunnel
255
256
  module.exports.NgrokTcpTunnel = NgrokTcpTunnel
256
257
  module.exports.NgrokTlsTunnel = NgrokTlsTunnel
@@ -267,6 +268,63 @@ const net = require('net');
267
268
  const fs = require('fs');
268
269
  const os = require('os');
269
270
 
271
+ // wrap listen with the bind code for passing to net.Server.listen()
272
+ NgrokHttpTunnelBuilder.prototype._listen = NgrokHttpTunnelBuilder.prototype.listen;
273
+ NgrokTcpTunnelBuilder.prototype._listen = NgrokTcpTunnelBuilder.prototype.listen;
274
+ NgrokTlsTunnelBuilder.prototype._listen = NgrokTlsTunnelBuilder.prototype.listen;
275
+ NgrokLabeledTunnelBuilder.prototype._listen = NgrokLabeledTunnelBuilder.prototype.listen;
276
+
277
+ NgrokHttpTunnelBuilder.prototype.listen = ngrokBind;
278
+ NgrokTcpTunnelBuilder.prototype.listen = ngrokBind;
279
+ NgrokTlsTunnelBuilder.prototype.listen = ngrokBind;
280
+ NgrokLabeledTunnelBuilder.prototype.listen = ngrokBind;
281
+
282
+ // Begin listening for new connections on this tunnel,
283
+ // and bind to a local socket so this tunnel can be
284
+ // passed into net.Server.listen().
285
+ async function ngrokBind(bind) {
286
+ const tunnel = await this._listen();
287
+ if (bind !== false) {
288
+ const socket = await randomTcpSocket();
289
+ defineTunnelHandle(tunnel, socket);
290
+ }
291
+ return tunnel;
292
+ }
293
+
294
+ // add a 'handle' getter to the tunnel so it can be
295
+ // passed into net.Server.listen().
296
+ function defineTunnelHandle(tunnel, socket) {
297
+ // NodeJS net.Server asks passed-in object for 'handle',
298
+ // Return the native TCP object so the pre-existing socket is used.
299
+ Object.defineProperty( tunnel, 'handle', {
300
+ get: function() {
301
+ // turn on forwarding now that it has been requested
302
+ tunnel.forwardTcp('localhost:' + socket.address().port);
303
+ return socket._handle;
304
+ }
305
+ });
306
+ }
307
+
308
+ // generate a net.Server listening to a random port
309
+ async function randomTcpSocket() {
310
+ return await asyncListen(new net.Server(), {host:'localhost', port:0});
311
+ }
312
+
313
+ // NodeJS has not promisified 'net': https://github.com/nodejs/node/issues/21482
314
+ function asyncListen(server, options) {
315
+ return new Promise((resolve, reject) => {
316
+ const socket = server.listen(options);
317
+ socket.once('listening', () => {
318
+ resolve(socket);
319
+ })
320
+ .once('error', (err) => {
321
+ reject(err);
322
+ });
323
+ });
324
+ }
325
+
326
+ // Make a session using NGROK_AUTHTOKEN from the environment,
327
+ // and then return a listening HTTP tunnel.
270
328
  async function defaultTunnel() {
271
329
  // set up a default session and tunnel
272
330
  var builder = new NgrokSessionBuilder();
@@ -277,22 +335,12 @@ async function defaultTunnel() {
277
335
  return tunnel;
278
336
  }
279
337
 
280
- // get a ngrok-connect socket, optionally passing in a pre-existing tunnel
281
- async function getSocket(tunnel) {
282
- if (!tunnel) {
283
- tunnel = await defaultTunnel();
284
- }
285
- // use tcp socket with random local port
286
- const server = new net.Server();
287
- server.listen(0);
288
- // forward to this socket
289
- tunnel.forwardTcp('localhost:' + server.address().port);
290
- // surface to caller
291
- server.tunnel = tunnel;
292
- return server;
338
+ // Get a listenable ngrok tunnel, suitable for passing to net.Server.listen().
339
+ async function listenable() {
340
+ return await defaultTunnel();
293
341
  }
294
342
 
295
- // bind a server to a ngrok tunnel, optionally passing in a pre-existing tunnel
343
+ // Bind a server to a ngrok tunnel, optionally passing in a pre-existing tunnel
296
344
  async function ngrokListen(server, tunnel) {
297
345
  if (!tunnel) {
298
346
  tunnel = await defaultTunnel();
@@ -301,22 +349,28 @@ async function ngrokListen(server, tunnel) {
301
349
  // todo: named pipe on windows: https://nodejs.org/api/net.html#ipc-support
302
350
 
303
351
  // attempt unix socket
304
- ngrokLinkUnix(tunnel, server)
305
- .then(() => {})
306
- .catch(function(err) {
307
- console.debug("Using TCP socket. " + err);
308
- // fallback to tcp socket
309
- ngrokLinkTcp(tunnel, server);
310
- });
352
+ var socket;
353
+ try {
354
+ socket = await ngrokLinkUnix(tunnel, server);
355
+ } catch (err) {
356
+ console.debug("Using TCP socket. " + err);
357
+ // fallback to tcp socket
358
+ socket = await ngrokLinkTcp(tunnel, server);
359
+ }
360
+ registerCleanup(tunnel, socket);
311
361
 
312
362
  server.tunnel = tunnel; // surface to caller
363
+ socket.tunnel = tunnel; // surface to caller
364
+ // return the newly created net.Server, which will be different in the express case
365
+ return socket;
313
366
  }
314
367
 
315
368
  async function ngrokLinkTcp(tunnel, server) {
316
369
  // random local port
317
- server.listen(0);
370
+ const socket = await asyncListen(server, {host:'localhost', port:0});
318
371
  // forward to socket
319
- tunnel.forwardTcp('localhost:' + server.address().port);
372
+ tunnel.forwardTcp('localhost:' + socket.address().port);
373
+ return socket;
320
374
  }
321
375
 
322
376
  async function ngrokLinkUnix(tunnel, server) {
@@ -337,7 +391,7 @@ async function ngrokLinkUnix(tunnel, server) {
337
391
  }
338
392
 
339
393
  // begin listening
340
- server.listen({path: filename});
394
+ const socket = await asyncListen(server, {path: filename});
341
395
  // tighten permissions
342
396
  try {
343
397
  fs.chmodSync(filename, fs.constants.S_IRWXU);
@@ -346,7 +400,40 @@ async function ngrokLinkUnix(tunnel, server) {
346
400
  }
347
401
  // forward tunnel
348
402
  tunnel.forwardUnix(filename);
403
+ socket.path = filename; // surface to caller
404
+
405
+ return socket;
406
+ }
407
+
408
+ function registerCleanup(tunnel, socket) {
409
+ process.on('SIGINT', function() {
410
+ if (process.listenerCount('SIGINT') > 1) {
411
+ // user has registered a handler, abort this one
412
+ return;
413
+ }
414
+ // close tunnel
415
+ if (tunnel) {
416
+ tunnel.close().then(()=>{
417
+ console.debug('ngrok closed tunnel: ' + tunnel.id());
418
+ });
419
+ }
420
+ // close webserver's socket
421
+ if (socket) {
422
+ socket.close(function () {
423
+ console.debug('ngrok closed socket');
424
+ });
425
+ }
426
+ // unregister any logging callback
427
+ loggingCallback();
428
+ });
429
+ }
430
+
431
+ function consoleLog(level) {
432
+ loggingCallback((level, target, message) => {
433
+ console.log(`${level} ${target} - ${message}`);
434
+ }, level);
349
435
  }
350
436
 
351
- module.exports.getSocket = getSocket;
437
+ module.exports.listenable = listenable;
352
438
  module.exports.listen = ngrokListen;
439
+ module.exports.consoleLog = consoleLog;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngrok/ngrok",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "napi": {
@@ -24,8 +24,15 @@
24
24
  "license": "MIT",
25
25
  "devDependencies": {
26
26
  "@napi-rs/cli": "^2.14.1",
27
+ "@types/node": "^18.13.0",
27
28
  "ava": "^4.3.3",
28
- "express": "^4.18.2"
29
+ "axios": "^1.3.3",
30
+ "express": "^4.18.2",
31
+ "segfault-handler": "^1.3.0",
32
+ "ts-node": "^10.9.1",
33
+ "typedoc": "^0.23.26",
34
+ "typescript": "^4.9.5",
35
+ "winston": "^3.8.2"
29
36
  },
30
37
  "ava": {
31
38
  "timeout": "3m"
@@ -35,8 +42,9 @@
35
42
  },
36
43
  "scripts": {
37
44
  "artifacts": "napi artifacts",
38
- "build": "napi build --platform --release --js gen.js --pipe 'node post-build.js trailer index'",
45
+ "build": "napi build --platform --release --js gen.js --dts gen.d.ts --pipe 'node post-build.js trailer index'",
39
46
  "build:debug": "napi build --platform",
47
+ "docs": "mv trailer.d.ts trailer.d.hidden; npx typedoc index.d.ts; mv trailer.d.hidden trailer.d.ts",
40
48
  "prepublishOnly": "napi prepublish -t npm",
41
49
  "test": "ava",
42
50
  "universal": "napi universal",
@@ -46,19 +54,19 @@
46
54
  "description": "The ngrok agent in library form, suitable for integrating directly into your NodeJS application.",
47
55
  "repository": "https://github.com/ngrok/ngrok-js",
48
56
  "optionalDependencies": {
49
- "@ngrok/ngrok-win32-x64-msvc": "0.0.6",
50
- "@ngrok/ngrok-darwin-x64": "0.0.6",
51
- "@ngrok/ngrok-linux-x64-gnu": "0.0.6",
52
- "@ngrok/ngrok-darwin-arm64": "0.0.6",
53
- "@ngrok/ngrok-android-arm64": "0.0.6",
54
- "@ngrok/ngrok-linux-arm64-gnu": "0.0.6",
55
- "@ngrok/ngrok-linux-arm64-musl": "0.0.6",
56
- "@ngrok/ngrok-win32-arm64-msvc": "0.0.6",
57
- "@ngrok/ngrok-linux-arm-gnueabihf": "0.0.6",
58
- "@ngrok/ngrok-linux-x64-musl": "0.0.6",
59
- "@ngrok/ngrok-freebsd-x64": "0.0.6",
60
- "@ngrok/ngrok-win32-ia32-msvc": "0.0.6",
61
- "@ngrok/ngrok-android-arm-eabi": "0.0.6",
62
- "@ngrok/ngrok-darwin-universal": "0.0.6"
57
+ "@ngrok/ngrok-win32-x64-msvc": "0.0.8",
58
+ "@ngrok/ngrok-darwin-x64": "0.0.8",
59
+ "@ngrok/ngrok-linux-x64-gnu": "0.0.8",
60
+ "@ngrok/ngrok-darwin-arm64": "0.0.8",
61
+ "@ngrok/ngrok-android-arm64": "0.0.8",
62
+ "@ngrok/ngrok-linux-arm64-gnu": "0.0.8",
63
+ "@ngrok/ngrok-linux-arm64-musl": "0.0.8",
64
+ "@ngrok/ngrok-win32-arm64-msvc": "0.0.8",
65
+ "@ngrok/ngrok-linux-arm-gnueabihf": "0.0.8",
66
+ "@ngrok/ngrok-linux-x64-musl": "0.0.8",
67
+ "@ngrok/ngrok-freebsd-x64": "0.0.8",
68
+ "@ngrok/ngrok-win32-ia32-msvc": "0.0.8",
69
+ "@ngrok/ngrok-android-arm-eabi": "0.0.8",
70
+ "@ngrok/ngrok-darwin-universal": "0.0.8"
63
71
  }
64
72
  }