@nora-link/client 1.0.2 → 1.0.4

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.
package/README.md CHANGED
@@ -30,6 +30,6 @@ You can acess the tunnel by navigating to [https://noralink.eu/home](https://nor
30
30
 
31
31
  ## Available subdomains
32
32
 
33
- Currently only a subset of subdomains are available to use with your own tunnels, from the following: *admin, api, app, control, dashboard, home, my, nodered*.
33
+ Currently only a subset of subdomains are available to use with your own tunnels, from the following: *admin, api, app, control, dashboard, home, my, nodered, smarthome, test*.
34
34
 
35
35
  If you want a new subdomain, add a request here: [Request a new subdomain thread](https://github.com/andrei-tatar/nora-link-client/issues/1).
package/dist/cli.js CHANGED
@@ -11,12 +11,18 @@ commander_1.program
11
11
  .usage('[OPTIONS]...')
12
12
  .option('-d, --debug', 'output extra debugging', false)
13
13
  .option('-n, --non-secure', 'don\'t use secure connection', false)
14
- .option('-l, --log', `min log level to display (${logger_1.LOG_LEVEL.join(',')})`, new RegExp(`^${logger_1.LOG_LEVEL.join('|')}$`), 'info')
15
- .requiredOption('-f, --forward <path|label|url...>', 'Forward path to local url')
14
+ .option('-l, --log', `min log level to display. Options: ${logger_1.LOG_LEVEL.join(',')}`, (value, previous) => {
15
+ const v = value.toLowerCase();
16
+ if (logger_1.LOG_LEVEL.includes(v)) {
17
+ return v;
18
+ }
19
+ return previous;
20
+ }, 'info')
21
+ .requiredOption('-f, --forward <subdomain|label|url>', 'Tunnel subdomain to local url. Label is optional')
16
22
  .requiredOption('-k, --key <api-key>', 'Api Key')
17
23
  .requiredOption('-h, --host <hostname>', 'Server host name', 'noralink.eu')
18
- .addHelpText('afterAll', 'To forward a local port on the public path /test, you can use `-f test|:3000`')
19
- .addHelpText('afterAll', 'To forward an ip on the public path /test, you can use `-f test|192.168.1.130`')
24
+ .addHelpText('afterAll', 'To tunnel a local port to the subdomain `test`, you can use `-f test|:3000`')
25
+ .addHelpText('afterAll', 'To tunnel an ip to the subdomain `test`, you can use `-f test|192.168.1.130`')
20
26
  .parse();
21
27
  const options = commander_1.program.opts();
22
28
  let tunnels = [];
@@ -43,6 +49,7 @@ if (Array.isArray(options.forward)) {
43
49
  });
44
50
  }
45
51
  new client_1.Client({
52
+ agent: `${name}@${version}`,
46
53
  hostname: options.host,
47
54
  secure: !options.nonSecure,
48
55
  tunnels,
package/dist/client.d.ts CHANGED
@@ -7,6 +7,7 @@ export interface TunnelOptions {
7
7
  removeHostHeader?: boolean;
8
8
  }
9
9
  export interface ClientConnectionOptions {
10
+ agent?: string;
10
11
  hostname: string;
11
12
  secure?: boolean;
12
13
  apiKey: string;
package/dist/client.js CHANGED
@@ -15,13 +15,15 @@ class Client {
15
15
  this.server$ = new rxjs_1.Observable(observer => {
16
16
  const segments = this.options.tunnels.map(v => `${v.remotePath}|${v.label}`);
17
17
  const qs = (0, node_querystring_1.stringify)({ s: segments });
18
- this.options.logger?.info(`[nora-link] connecting to ${this.options.hostname}`);
18
+ this.options.logger?.info(`[nora-link] connecting`);
19
19
  const protocol = this.options.secure ? 'wss' : 'ws';
20
- const ws = new ws_1.default(`${protocol}://${this.options.hostname}/api/tunnel?${qs}`, {
21
- headers: {
22
- authorization: `Bearer ${this.options.apiKey}`,
23
- },
24
- });
20
+ const headers = {
21
+ authorization: `Bearer ${this.options.apiKey}`,
22
+ ...(this.options.agent ? {
23
+ 'user-agent': this.options.agent,
24
+ } : {}),
25
+ };
26
+ const ws = new ws_1.default(`${protocol}://${this.options.hostname}/api/tunnel?${qs}`, { headers });
25
27
  ws.on('open', () => {
26
28
  this.options.logger?.info(`[nora-link] connected`);
27
29
  observer.next(ws);
@@ -67,9 +69,12 @@ class Client {
67
69
  }
68
70
  return rxjs_1.EMPTY;
69
71
  }), (0, rxjs_1.retry)({
70
- delay: (err) => {
71
- this.options.logger?.error(`[nora-link] retrying in 10 sec `, err);
72
- return (0, rxjs_1.timer)(10000);
72
+ resetOnSuccess: true,
73
+ delay: (err, retryCount) => {
74
+ const delaySeconds = Math.round(Math.min(300, Math.pow(1.8, retryCount + 3)));
75
+ this.options.logger?.trace(`[nora-link] connection error`, err);
76
+ this.options.logger?.error(`[nora-link] retrying in ${delaySeconds} sec`);
77
+ return (0, rxjs_1.timer)(delaySeconds * 1000);
73
78
  },
74
79
  }), (0, rxjs_1.share)());
75
80
  }
package/dist/logger.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export declare const LOG_LEVEL: readonly ["trace", "debug", "info", "warn", "error"];
2
- type LogLevel = typeof LOG_LEVEL[number];
2
+ export type LogLevel = typeof LOG_LEVEL[number];
3
3
  export type Logger = {
4
4
  [P in LogLevel]: (msg: string, err?: any) => void;
5
5
  };
6
6
  export declare function createConsoleLogger(minLevel: LogLevel): Logger;
7
- export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nora-link/client",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "noralink.eu client",
5
5
  "bin": {
6
6
  "noralink": "dist/cli.js"
package/src/cli.ts CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { program } from 'commander';
4
4
  import { Client, ClientConnectionOptions } from './client';
5
- import { createConsoleLogger, LOG_LEVEL } from './logger';
5
+ import { createConsoleLogger, LOG_LEVEL, LogLevel } from './logger';
6
6
 
7
7
  const { name, version } = require('../package.json');
8
8
 
@@ -12,12 +12,18 @@ program
12
12
  .usage('[OPTIONS]...')
13
13
  .option('-d, --debug', 'output extra debugging', false)
14
14
  .option('-n, --non-secure', 'don\'t use secure connection', false)
15
- .option('-l, --log', `min log level to display (${LOG_LEVEL.join(',')})`, new RegExp(`^${LOG_LEVEL.join('|')}$`), 'info')
16
- .requiredOption('-f, --forward <path|label|url...>', 'Forward path to local url')
15
+ .option<LogLevel>('-l, --log', `min log level to display. Options: ${LOG_LEVEL.join(',')}`, (value, previous) => {
16
+ const v = value.toLowerCase() as LogLevel;
17
+ if (LOG_LEVEL.includes(v)) {
18
+ return v;
19
+ }
20
+ return previous;
21
+ }, 'info')
22
+ .requiredOption('-f, --forward <subdomain|label|url>', 'Tunnel subdomain to local url. Label is optional')
17
23
  .requiredOption('-k, --key <api-key>', 'Api Key')
18
24
  .requiredOption('-h, --host <hostname>', 'Server host name', 'noralink.eu')
19
- .addHelpText('afterAll', 'To forward a local port on the public path /test, you can use `-f test|:3000`')
20
- .addHelpText('afterAll', 'To forward an ip on the public path /test, you can use `-f test|192.168.1.130`')
25
+ .addHelpText('afterAll', 'To tunnel a local port to the subdomain `test`, you can use `-f test|:3000`')
26
+ .addHelpText('afterAll', 'To tunnel an ip to the subdomain `test`, you can use `-f test|192.168.1.130`')
21
27
  .parse();
22
28
 
23
29
  const options = program.opts();
@@ -48,6 +54,7 @@ if (Array.isArray(options.forward)) {
48
54
  }
49
55
 
50
56
  new Client({
57
+ agent: `${name}@${version}`,
51
58
  hostname: options.host,
52
59
  secure: !options.nonSecure,
53
60
  tunnels,
package/src/client.ts CHANGED
@@ -17,6 +17,7 @@ export interface TunnelOptions {
17
17
  }
18
18
 
19
19
  export interface ClientConnectionOptions {
20
+ agent?: string;
20
21
  hostname: string;
21
22
  secure?: boolean;
22
23
  apiKey: string;
@@ -31,13 +32,17 @@ export class Client {
31
32
  private server$ = new Observable<WebSocket>(observer => {
32
33
  const segments = this.options.tunnels.map(v => `${v.remotePath}|${v.label}`);
33
34
  const qs = querystringStringify({ s: segments });
34
- this.options.logger?.info(`[nora-link] connecting to ${this.options.hostname}`);
35
+ this.options.logger?.info(`[nora-link] connecting`);
35
36
  const protocol = this.options.secure ? 'wss' : 'ws';
36
- const ws = new WebSocket(`${protocol}://${this.options.hostname}/api/tunnel?${qs}`, {
37
- headers: {
38
- authorization: `Bearer ${this.options.apiKey}`,
39
- },
40
- });
37
+
38
+ const headers = {
39
+ authorization: `Bearer ${this.options.apiKey}`,
40
+ ...(this.options.agent ? {
41
+ 'user-agent': this.options.agent,
42
+ } : {}),
43
+ };
44
+
45
+ const ws = new WebSocket(`${protocol}://${this.options.hostname}/api/tunnel?${qs}`, { headers });
41
46
 
42
47
  ws.on('open', () => {
43
48
  this.options.logger?.info(`[nora-link] connected`);
@@ -96,9 +101,12 @@ export class Client {
96
101
  return EMPTY;
97
102
  }),
98
103
  retry({
99
- delay: (err) => {
100
- this.options.logger?.error(`[nora-link] retrying in 10 sec `, err);
101
- return timer(10000);
104
+ resetOnSuccess: true,
105
+ delay: (err, retryCount) => {
106
+ const delaySeconds = Math.round(Math.min(300, Math.pow(1.8, retryCount + 3)));
107
+ this.options.logger?.trace(`[nora-link] connection error`, err);
108
+ this.options.logger?.error(`[nora-link] retrying in ${delaySeconds} sec`);
109
+ return timer(delaySeconds * 1000);
102
110
  },
103
111
  }),
104
112
  share(),
package/src/logger.ts CHANGED
@@ -6,7 +6,7 @@ export const LOG_LEVEL = [
6
6
  'error'
7
7
  ] as const;
8
8
 
9
- type LogLevel = typeof LOG_LEVEL[number];
9
+ export type LogLevel = typeof LOG_LEVEL[number];
10
10
 
11
11
  export type Logger = {
12
12
  [P in LogLevel]: (msg: string, err?: any) => void;