@ngrok/ngrok 0.0.7 → 0.0.9

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 (49) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/CONTRIBUTING.md +4 -4
  3. package/Cargo.toml +4 -2
  4. package/README.md +26 -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 +3 -2
  28. package/examples/ngrok-labeled.js +1 -1
  29. package/examples/ngrok-tcp.js +1 -1
  30. package/examples/ngrok-tls.js +1 -1
  31. package/examples/ngrok-typescript.ts +29 -8
  32. package/examples/ngrok-windows-pipe.js +17 -0
  33. package/examples/ngrok-winston.js +24 -0
  34. package/index.d.ts +115 -31
  35. package/index.js +121 -33
  36. package/package.json +20 -16
  37. package/scripts/gc-node-drop.js +6 -2
  38. package/scripts/gc-rust-drop.js +8 -4
  39. package/src/http.rs +64 -42
  40. package/src/lib.rs +1 -0
  41. package/src/logging.rs +134 -0
  42. package/src/session.rs +75 -85
  43. package/src/tcp.rs +2 -1
  44. package/src/tls.rs +6 -4
  45. package/src/tunnel.rs +99 -51
  46. package/src/tunnel_builder.rs +27 -15
  47. package/trailer.d.ts +9 -2
  48. package/trailer.js +119 -31
  49. package/examples/ngrok-get-socket.js +0 -12
package/index.d.ts CHANGED
@@ -4,11 +4,11 @@
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 */
@@ -124,6 +124,7 @@ export class NgrokSession {
124
124
  tlsEndpoint(): NgrokTlsTunnelBuilder
125
125
  /** Start building a labeled tunnel. */
126
126
  labeledTunnel(): NgrokLabeledTunnelBuilder
127
+ /** Close a tunnel with the given ID. */
127
128
  closeTunnel(id: string): Promise<void>
128
129
  }
129
130
  export class UpdateRequest {
@@ -138,13 +139,32 @@ export class NgrokHttpTunnel {
138
139
  url(): string
139
140
  /** The protocol of the endpoint that this tunnel backs. */
140
141
  proto(): string
141
- /** The ID of this tunnel, assigned by the remote server. */
142
+ /** Returns a tunnel's unique ID. */
142
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
143
153
  /** Forward incoming tunnel connections to the provided TCP address. */
144
154
  forwardTcp(addr: string): Promise<void>
145
- /** Forward incoming tunnel connections to the provided Unix socket path. */
146
- forwardUnix(addr: string): Promise<void>
147
- /** Close the tunnel. */
155
+ /**
156
+ * Forward incoming tunnel connections to the provided file socket path.
157
+ * On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
158
+ * On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
159
+ */
160
+ forwardPipe(addr: string): Promise<void>
161
+ /**
162
+ * Close the tunnel.
163
+ *
164
+ * This is an RPC call that must be `.await`ed.
165
+ * It is equivalent to calling `Session::close_tunnel` with this
166
+ * tunnel's ID.
167
+ */
148
168
  close(): Promise<void>
149
169
  }
150
170
  /**r" An ngrok tunnel backing a TCP endpoint. */
@@ -153,13 +173,32 @@ export class NgrokTcpTunnel {
153
173
  url(): string
154
174
  /** The protocol of the endpoint that this tunnel backs. */
155
175
  proto(): string
156
- /** The ID of this tunnel, assigned by the remote server. */
176
+ /** Returns a tunnel's unique ID. */
157
177
  id(): string
178
+ /**
179
+ * Returns a human-readable string presented in the ngrok dashboard
180
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
181
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
182
+ * explicitly.
183
+ */
184
+ forwardsTo(): string
185
+ /** Returns the arbitrary metadata string for this tunnel. */
186
+ metadata(): string
158
187
  /** Forward incoming tunnel connections to the provided TCP address. */
159
188
  forwardTcp(addr: string): Promise<void>
160
- /** Forward incoming tunnel connections to the provided Unix socket path. */
161
- forwardUnix(addr: string): Promise<void>
162
- /** Close the tunnel. */
189
+ /**
190
+ * Forward incoming tunnel connections to the provided file socket path.
191
+ * On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
192
+ * On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
193
+ */
194
+ forwardPipe(addr: string): Promise<void>
195
+ /**
196
+ * Close the tunnel.
197
+ *
198
+ * This is an RPC call that must be `.await`ed.
199
+ * It is equivalent to calling `Session::close_tunnel` with this
200
+ * tunnel's ID.
201
+ */
163
202
  close(): Promise<void>
164
203
  }
165
204
  /**r" An ngrok tunnel bcking a TLS endpoint. */
@@ -168,26 +207,64 @@ export class NgrokTlsTunnel {
168
207
  url(): string
169
208
  /** The protocol of the endpoint that this tunnel backs. */
170
209
  proto(): string
171
- /** The ID of this tunnel, assigned by the remote server. */
210
+ /** Returns a tunnel's unique ID. */
172
211
  id(): string
212
+ /**
213
+ * Returns a human-readable string presented in the ngrok dashboard
214
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
215
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
216
+ * explicitly.
217
+ */
218
+ forwardsTo(): string
219
+ /** Returns the arbitrary metadata string for this tunnel. */
220
+ metadata(): string
173
221
  /** Forward incoming tunnel connections to the provided TCP address. */
174
222
  forwardTcp(addr: string): Promise<void>
175
- /** Forward incoming tunnel connections to the provided Unix socket path. */
176
- forwardUnix(addr: string): Promise<void>
177
- /** Close the tunnel. */
223
+ /**
224
+ * Forward incoming tunnel connections to the provided file socket path.
225
+ * On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
226
+ * On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
227
+ */
228
+ forwardPipe(addr: string): Promise<void>
229
+ /**
230
+ * Close the tunnel.
231
+ *
232
+ * This is an RPC call that must be `.await`ed.
233
+ * It is equivalent to calling `Session::close_tunnel` with this
234
+ * tunnel's ID.
235
+ */
178
236
  close(): Promise<void>
179
237
  }
180
238
  /**r" A labeled ngrok tunnel. */
181
239
  export class NgrokLabeledTunnel {
182
240
  /** The labels this tunnel was started with. */
183
241
  labels(): Record<string, string>
184
- /** The ID of this tunnel, assigned by the remote server. */
242
+ /** Returns a tunnel's unique ID. */
185
243
  id(): string
244
+ /**
245
+ * Returns a human-readable string presented in the ngrok dashboard
246
+ * and the Tunnels API. Use the [HttpTunnelBuilder::forwards_to],
247
+ * [TcpTunnelBuilder::forwards_to], etc. to set this value
248
+ * explicitly.
249
+ */
250
+ forwardsTo(): string
251
+ /** Returns the arbitrary metadata string for this tunnel. */
252
+ metadata(): string
186
253
  /** Forward incoming tunnel connections to the provided TCP address. */
187
254
  forwardTcp(addr: string): Promise<void>
188
- /** Forward incoming tunnel connections to the provided Unix socket path. */
189
- forwardUnix(addr: string): Promise<void>
190
- /** Close the tunnel. */
255
+ /**
256
+ * Forward incoming tunnel connections to the provided file socket path.
257
+ * On Linux/Darwin addr can be a unix domain socket path, e.g. "/tmp/ngrok.sock"
258
+ * On Windows addr can be a named pipe, e.e. "\\.\pipe\an_ngrok_pipe"
259
+ */
260
+ forwardPipe(addr: string): Promise<void>
261
+ /**
262
+ * Close the tunnel.
263
+ *
264
+ * This is an RPC call that must be `.await`ed.
265
+ * It is equivalent to calling `Session::close_tunnel` with this
266
+ * tunnel's ID.
267
+ */
191
268
  close(): Promise<void>
192
269
  }
193
270
  /**r" An ngrok tunnel backing an HTTP endpoint. */
@@ -227,12 +304,12 @@ export class NgrokHttpTunnelBuilder {
227
304
  * OAuth configuration.
228
305
  * If not called, OAuth is disabled.
229
306
  */
230
- oauth(provider: string, allowEmails: Array<string>, allowDomains: Array<string>, scopes: Array<string>): this
307
+ oauth(provider: string, allowEmails?: Array<string> | undefined | null, allowDomains?: Array<string> | undefined | null, scopes?: Array<string> | undefined | null): this
231
308
  /**
232
309
  * OIDC configuration.
233
310
  * If not called, OIDC is disabled.
234
311
  */
235
- oidc(issuerUrl: string, clientId: string, clientSecret: string, allowEmails: Array<string>, allowDomains: Array<string>, scopes: Array<string>): this
312
+ oidc(issuerUrl: string, clientId: string, clientSecret: string, allowEmails?: Array<string> | undefined | null, allowDomains?: Array<string> | undefined | null, scopes?: Array<string> | undefined | null): this
236
313
  /**
237
314
  * WebhookVerification configuration.
238
315
  * If not called, WebhookVerification is disabled.
@@ -241,7 +318,7 @@ export class NgrokHttpTunnelBuilder {
241
318
  /** Tunnel-specific opaque metadata. Viewable via the API. */
242
319
  metadata(metadata: string): this
243
320
  /** Begin listening for new connections on this tunnel. */
244
- listen(): Promise<NgrokHttpTunnel>
321
+ listen(bind?: boolean | undefined | null): Promise<NgrokHttpTunnel>
245
322
  /**
246
323
  * Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
247
324
  * Call multiple times to add additional CIDR ranges.
@@ -252,7 +329,7 @@ export class NgrokHttpTunnelBuilder {
252
329
  * Call multiple times to add additional CIDR ranges.
253
330
  */
254
331
  denyCidr(cidr: string): this
255
- /** The version of PROXY protocol to use with this tunnel, None if not using. */
332
+ /** The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using. */
256
333
  proxyProto(proxyProto: string): this
257
334
  /**
258
335
  * Tunnel backend metadata. Viewable via the dashboard and API, but has no
@@ -267,7 +344,7 @@ export class NgrokTcpTunnelBuilder {
267
344
  /** Tunnel-specific opaque metadata. Viewable via the API. */
268
345
  metadata(metadata: string): this
269
346
  /** Begin listening for new connections on this tunnel. */
270
- listen(): Promise<NgrokTcpTunnel>
347
+ listen(bind?: boolean | undefined | null): Promise<NgrokTcpTunnel>
271
348
  /**
272
349
  * Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
273
350
  * Call multiple times to add additional CIDR ranges.
@@ -278,7 +355,7 @@ export class NgrokTcpTunnelBuilder {
278
355
  * Call multiple times to add additional CIDR ranges.
279
356
  */
280
357
  denyCidr(cidr: string): this
281
- /** The version of PROXY protocol to use with this tunnel, None if not using. */
358
+ /** The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using. */
282
359
  proxyProto(proxyProto: string): this
283
360
  /**
284
361
  * Tunnel backend metadata. Viewable via the dashboard and API, but has no
@@ -297,7 +374,7 @@ export class NgrokTlsTunnelBuilder {
297
374
  /** Tunnel-specific opaque metadata. Viewable via the API. */
298
375
  metadata(metadata: string): this
299
376
  /** Begin listening for new connections on this tunnel. */
300
- listen(): Promise<NgrokTlsTunnel>
377
+ listen(bind?: boolean | undefined | null): Promise<NgrokTlsTunnel>
301
378
  /**
302
379
  * Restriction placed on the origin of incoming connections to the edge to only allow these CIDR ranges.
303
380
  * Call multiple times to add additional CIDR ranges.
@@ -308,7 +385,7 @@ export class NgrokTlsTunnelBuilder {
308
385
  * Call multiple times to add additional CIDR ranges.
309
386
  */
310
387
  denyCidr(cidr: string): this
311
- /** The version of PROXY protocol to use with this tunnel, None if not using. */
388
+ /** The version of PROXY protocol to use with this tunnel "1", "2", or "" if not using. */
312
389
  proxyProto(proxyProto: string): this
313
390
  /**
314
391
  * Tunnel backend metadata. Viewable via the dashboard and API, but has no
@@ -321,9 +398,16 @@ export class NgrokLabeledTunnelBuilder {
321
398
  /** Tunnel-specific opaque metadata. Viewable via the API. */
322
399
  metadata(metadata: string): this
323
400
  /** Begin listening for new connections on this tunnel. */
324
- listen(): Promise<NgrokLabeledTunnel>
401
+ listen(bind?: boolean | undefined | null): Promise<NgrokLabeledTunnel>
325
402
  /** Add a label, value pair for this tunnel. */
326
403
  label(label: string, value: string): this
327
404
  }
328
- export function getSocket(tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): net.Server
329
- export function listen(server: net.Server, tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
405
+ /** Generate, or convert a given tunnel, into one that can be passed into net.Server.listen(). */
406
+ export function listenable(tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
407
+ /** Start the given net.Server listening to a generated, or passed in, tunnel. */
408
+ export function listen(server: import("net").Server, tunnel?: (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)): (NgrokHttpTunnel|NgrokTcpTunnel|NgrokTlsTunnel|NgrokLabeledTunnel)
409
+ /**
410
+ * Register a console.log callback for ngrok INFO logging.
411
+ * Optionally set the logging level to one of ERROR, WARN, INFO, DEBUG, or TRACE.
412
+ */
413
+ export function consoleLog(level?: String)
package/index.js CHANGED
@@ -246,10 +246,10 @@ if (!nativeBinding) {
246
246
  throw new Error(`Failed to load native binding`)
247
247
  }
248
248
 
249
- const { NgrokSessionBuilder, tracingSubscriber, NgrokSession, UpdateRequest, 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
254
  module.exports.UpdateRequest = UpdateRequest
255
255
  module.exports.NgrokHttpTunnel = NgrokHttpTunnel
@@ -268,61 +268,114 @@ const net = require('net');
268
268
  const fs = require('fs');
269
269
  const os = require('os');
270
270
 
271
- async function defaultTunnel() {
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.
328
+ async function defaultTunnel(bind) {
272
329
  // set up a default session and tunnel
273
330
  var builder = new NgrokSessionBuilder();
274
331
  builder.authtokenFromEnv();
275
332
  var session = await builder.connect();
276
- var tunnel = await session.httpEndpoint().listen();
333
+ var tunnel = await session.httpEndpoint().listen(bind);
277
334
  tunnel.session = session; // surface to caller
278
335
  return tunnel;
279
336
  }
280
337
 
281
- // get a ngrok-connect socket, optionally passing in a pre-existing tunnel
282
- async function getSocket(tunnel) {
283
- if (!tunnel) {
284
- tunnel = await defaultTunnel();
285
- }
286
- // use tcp socket with random local port
287
- const server = new net.Server();
288
- await server.listen(0);
289
- // forward to this socket
290
- tunnel.forwardTcp('localhost:' + server.address().port);
291
- // surface to caller
292
- server.tunnel = tunnel;
293
- return server;
338
+ // Get a listenable ngrok tunnel, suitable for passing to net.Server.listen().
339
+ async function listenable() {
340
+ return await defaultTunnel();
294
341
  }
295
342
 
296
- // 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
297
344
  async function ngrokListen(server, tunnel) {
298
345
  if (!tunnel) {
299
- tunnel = await defaultTunnel();
346
+ // turn off automatic bind
347
+ tunnel = await defaultTunnel(false);
300
348
  }
301
- // todo: abstract socket on linux: https://stackoverflow.com/a/60014174
302
- // todo: named pipe on windows: https://nodejs.org/api/net.html#ipc-support
303
349
 
304
- // attempt unix socket
350
+ // attempt pipe socket
305
351
  try {
306
- await ngrokLinkUnix(tunnel, server);
352
+ socket = await ngrokLinkPipe(tunnel, server);
307
353
  } catch (err) {
308
354
  console.debug("Using TCP socket. " + err);
309
355
  // fallback to tcp socket
310
- await ngrokLinkTcp(tunnel, server);
356
+ socket = await ngrokLinkTcp(tunnel, server);
311
357
  }
358
+ registerCleanup(tunnel, socket);
312
359
 
313
360
  server.tunnel = tunnel; // surface to caller
314
- return tunnel;
361
+ socket.tunnel = tunnel; // surface to caller
362
+ // return the newly created net.Server, which will be different in the express case
363
+ return socket;
315
364
  }
316
365
 
317
366
  async function ngrokLinkTcp(tunnel, server) {
318
367
  // random local port
319
- await server.listen(0);
368
+ const socket = await asyncListen(server, {host:'localhost', port:0});
320
369
  // forward to socket
321
- tunnel.forwardTcp('localhost:' + server.address().port);
370
+ tunnel.forwardTcp('localhost:' + socket.address().port);
371
+ return socket;
322
372
  }
323
373
 
324
- async function ngrokLinkUnix(tunnel, server) {
325
- const proposed = "tun-" + tunnel.id() + ".sock";
374
+ async function ngrokLinkPipe(tunnel, server) {
375
+ var proposed = "tun-" + tunnel.id() + ".sock";
376
+ if (platform == 'win32') {
377
+ proposed = '\\\\.\\pipe\\' + proposed;
378
+ }
326
379
  var filename;
327
380
  try {
328
381
  fs.accessSync(process.cwd(), fs.constants.W_OK);
@@ -339,16 +392,51 @@ async function ngrokLinkUnix(tunnel, server) {
339
392
  }
340
393
 
341
394
  // begin listening
342
- await server.listen({path: filename});
395
+ const socket = await asyncListen(server, {path: filename});
343
396
  // tighten permissions
344
397
  try {
345
- fs.chmodSync(filename, fs.constants.S_IRWXU);
398
+ if (platform != 'win32') {
399
+ fs.chmodSync(filename, fs.constants.S_IRWXU);
400
+ }
346
401
  } catch (err) {
347
402
  console.debug("Cannot change permissions of file: " + filename);
348
403
  }
349
404
  // forward tunnel
350
- tunnel.forwardUnix(filename);
405
+ tunnel.forwardPipe(filename);
406
+ socket.path = filename; // surface to caller
407
+
408
+ return socket;
409
+ }
410
+
411
+ function registerCleanup(tunnel, socket) {
412
+ process.on('SIGINT', function() {
413
+ if (process.listenerCount('SIGINT') > 1) {
414
+ // user has registered a handler, abort this one
415
+ return;
416
+ }
417
+ // close tunnel
418
+ if (tunnel) {
419
+ tunnel.close().then(()=>{
420
+ console.debug('ngrok closed tunnel: ' + tunnel.id());
421
+ });
422
+ }
423
+ // close webserver's socket
424
+ if (socket) {
425
+ socket.close(function () {
426
+ console.debug('ngrok closed socket');
427
+ });
428
+ }
429
+ // unregister any logging callback
430
+ loggingCallback();
431
+ });
432
+ }
433
+
434
+ function consoleLog(level) {
435
+ loggingCallback((level, target, message) => {
436
+ console.log(`${level} ${target} - ${message}`);
437
+ }, level);
351
438
  }
352
439
 
353
- module.exports.getSocket = getSocket;
440
+ module.exports.listenable = listenable;
354
441
  module.exports.listen = ngrokListen;
442
+ module.exports.consoleLog = consoleLog;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ngrok/ngrok",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "napi": {
@@ -26,10 +26,13 @@
26
26
  "@napi-rs/cli": "^2.14.1",
27
27
  "@types/node": "^18.13.0",
28
28
  "ava": "^4.3.3",
29
+ "axios": "^1.3.3",
29
30
  "express": "^4.18.2",
30
31
  "segfault-handler": "^1.3.0",
31
32
  "ts-node": "^10.9.1",
32
- "typescript": "^4.9.5"
33
+ "typedoc": "^0.23.26",
34
+ "typescript": "^4.9.5",
35
+ "winston": "^3.8.2"
33
36
  },
34
37
  "ava": {
35
38
  "timeout": "3m"
@@ -41,6 +44,7 @@
41
44
  "artifacts": "napi artifacts",
42
45
  "build": "napi build --platform --release --js gen.js --dts gen.d.ts --pipe 'node post-build.js trailer index'",
43
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",
44
48
  "prepublishOnly": "napi prepublish -t npm",
45
49
  "test": "ava",
46
50
  "universal": "napi universal",
@@ -50,19 +54,19 @@
50
54
  "description": "The ngrok agent in library form, suitable for integrating directly into your NodeJS application.",
51
55
  "repository": "https://github.com/ngrok/ngrok-js",
52
56
  "optionalDependencies": {
53
- "@ngrok/ngrok-win32-x64-msvc": "0.0.7",
54
- "@ngrok/ngrok-darwin-x64": "0.0.7",
55
- "@ngrok/ngrok-linux-x64-gnu": "0.0.7",
56
- "@ngrok/ngrok-darwin-arm64": "0.0.7",
57
- "@ngrok/ngrok-android-arm64": "0.0.7",
58
- "@ngrok/ngrok-linux-arm64-gnu": "0.0.7",
59
- "@ngrok/ngrok-linux-arm64-musl": "0.0.7",
60
- "@ngrok/ngrok-win32-arm64-msvc": "0.0.7",
61
- "@ngrok/ngrok-linux-arm-gnueabihf": "0.0.7",
62
- "@ngrok/ngrok-linux-x64-musl": "0.0.7",
63
- "@ngrok/ngrok-freebsd-x64": "0.0.7",
64
- "@ngrok/ngrok-win32-ia32-msvc": "0.0.7",
65
- "@ngrok/ngrok-android-arm-eabi": "0.0.7",
66
- "@ngrok/ngrok-darwin-universal": "0.0.7"
57
+ "@ngrok/ngrok-win32-x64-msvc": "0.0.9",
58
+ "@ngrok/ngrok-darwin-x64": "0.0.9",
59
+ "@ngrok/ngrok-linux-x64-gnu": "0.0.9",
60
+ "@ngrok/ngrok-darwin-arm64": "0.0.9",
61
+ "@ngrok/ngrok-android-arm64": "0.0.9",
62
+ "@ngrok/ngrok-linux-arm64-gnu": "0.0.9",
63
+ "@ngrok/ngrok-linux-arm64-musl": "0.0.9",
64
+ "@ngrok/ngrok-win32-arm64-msvc": "0.0.9",
65
+ "@ngrok/ngrok-linux-arm-gnueabihf": "0.0.9",
66
+ "@ngrok/ngrok-linux-x64-musl": "0.0.9",
67
+ "@ngrok/ngrok-freebsd-x64": "0.0.9",
68
+ "@ngrok/ngrok-win32-ia32-msvc": "0.0.9",
69
+ "@ngrok/ngrok-android-arm-eabi": "0.0.9",
70
+ "@ngrok/ngrok-darwin-universal": "0.0.9"
67
71
  }
68
72
  }
@@ -1,4 +1,9 @@
1
1
  // run with: node --trace-gc --expose-gc
2
+ if (typeof global.gc !== 'function') {
3
+ console.log("must run with '--expose-gc'");
4
+ process.exit();
5
+ };
6
+
2
7
  const SegfaultHandler = require('../node_modules/segfault-handler');
3
8
  SegfaultHandler.registerHandler('crash.log');
4
9
 
@@ -11,8 +16,7 @@ http.createServer(
11
16
 
12
17
  var ngrok = require('..');
13
18
  // turn on debug
14
- process.env.NGROK_LOG = 'ngrok_js=debug';
15
- ngrok.tracingSubscriber();
19
+ ngrok.consoleLog();
16
20
 
17
21
  new ngrok.NgrokSessionBuilder().authtokenFromEnv().connect().then((session) => {
18
22
  session.httpEndpoint().listen().then((tunnel) => {
@@ -1,5 +1,10 @@
1
1
  // run with: node --trace-gc --expose-gc
2
- const SegfaultHandler = require('segfault-handler');
2
+ if (typeof global.gc !== 'function') {
3
+ console.log("must run with '--expose-gc'");
4
+ process.exit();
5
+ };
6
+
7
+ const SegfaultHandler = require('../node_modules/segfault-handler');
3
8
  SegfaultHandler.registerHandler('crash.log');
4
9
 
5
10
  const http = require('http');
@@ -9,10 +14,9 @@ http.createServer(
9
14
  res.end();
10
15
  } ).listen(8081);
11
16
 
12
- var ngrok = require('/Users/bob/projects/ngrok-js/index.js');
17
+ var ngrok = require('..');
13
18
  // turn on debug
14
- process.env.NGROK_LOG = 'ngrok_js=debug';
15
- ngrok.tracingSubscriber();
19
+ ngrok.consoleLog();
16
20
 
17
21
  new ngrok.NgrokSessionBuilder().authtokenFromEnv().connect().then((session) => {
18
22
  session.httpEndpoint().listen().then((tunnel) => {