@nora-link/client 1.0.5 → 1.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.
package/dist/cli.js CHANGED
@@ -18,40 +18,47 @@ commander_1.program
18
18
  }
19
19
  return previous;
20
20
  }, 'info')
21
- .requiredOption('-f, --forward <subdomain|label|url>', 'Tunnel subdomain to local url. Label is optional')
21
+ .requiredOption('-f, --forward <subdomain|label|url...>', 'Tunnel subdomain to local url. Label is optional')
22
22
  .requiredOption('-k, --key <api-key>', 'Api Key')
23
23
  .requiredOption('-h, --host <hostname>', 'Server host name', 'noralink.eu')
24
24
  .addHelpText('afterAll', 'To tunnel a local port to the subdomain `test`, you can use `-f test|:3000`')
25
25
  .addHelpText('afterAll', 'To tunnel an ip to the subdomain `test`, you can use `-f test|192.168.1.130`')
26
26
  .parse();
27
27
  const options = commander_1.program.opts();
28
- const forward = Array.isArray(options.forward) ? options.forward : [options.forward];
29
- const tunnels = forward
30
- .filter((v) => typeof v === 'string')
31
- .map(v => {
32
- const [segment, labelOrUrl, urlPart] = v.split('|');
33
- let toUrl;
34
- const label = urlPart ? labelOrUrl : segment;
35
- const url = urlPart ?? labelOrUrl;
36
- if (/^https?:\/\//.test(url)) {
37
- toUrl = url;
38
- }
39
- else if (url.startsWith(':')) {
40
- //only port specified
41
- const port = +url.split(':')[1];
42
- toUrl = `http://127.0.0.1:${port}`;
43
- }
44
- else {
45
- toUrl = `http://${url}`;
46
- }
47
- return { remotePath: segment, localUrl: toUrl, label };
48
- });
49
- new client_1.Client({
50
- agent: `${name}@${version}`,
51
- hostname: options.host,
52
- secure: !options.nonSecure,
53
- tunnels,
54
- apiKey: options.key,
55
- logger: (0, logger_1.createConsoleLogger)(options.log),
56
- }).handle$
57
- .subscribe();
28
+ let tunnels = [];
29
+ if (Array.isArray(options.forward)) {
30
+ tunnels = options.forward
31
+ .filter((v) => typeof v === 'string')
32
+ .map(v => {
33
+ const [segment, labelOrUrl, urlPart] = v.split('|');
34
+ let tunnelUrl;
35
+ const label = urlPart ? labelOrUrl : segment;
36
+ const url = urlPart ?? labelOrUrl;
37
+ if (/^https?:\/\//.test(url)) {
38
+ tunnelUrl = url;
39
+ }
40
+ else if (url.startsWith(':')) {
41
+ //only port and/or path specified
42
+ const portAndPath = url.split(':')[1];
43
+ tunnelUrl = `http://localhost:${portAndPath}`;
44
+ }
45
+ else {
46
+ tunnelUrl = `http://${url}`;
47
+ }
48
+ return { remotePath: segment, url: tunnelUrl, label };
49
+ });
50
+ }
51
+ if (!tunnels.length) {
52
+ console.info('no valid tunnels specified');
53
+ process.exit(1);
54
+ }
55
+ else {
56
+ new client_1.Client({
57
+ agent: `${name}@${version}`,
58
+ hostname: options.host,
59
+ secure: !options.nonSecure,
60
+ tunnels,
61
+ apiKey: options.key,
62
+ logger: (0, logger_1.createConsoleLogger)(options.log),
63
+ }).handle$.subscribe();
64
+ }
package/dist/client.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import { Observable } from 'rxjs';
2
+ import WebSocket from 'ws';
2
3
  import { Logger } from './logger';
3
4
  export interface TunnelOptions {
4
5
  remotePath: string;
5
- localUrl: string;
6
+ url: string;
6
7
  label: string;
7
8
  removeHostHeader?: boolean;
8
9
  }
@@ -19,7 +20,7 @@ export declare class Client {
19
20
  constructor(options: ClientConnectionOptions);
20
21
  private server$;
21
22
  private data$;
22
- readonly handle$: Observable<never>;
23
+ readonly handle$: Observable<void | WebSocket>;
23
24
  send(id: number, type: string, msg?: Buffer): Observable<void>;
24
25
  private getStreamData;
25
26
  private httpRequest;
package/dist/client.js CHANGED
@@ -53,22 +53,16 @@ class Client {
53
53
  msg: v.subarray(5 + typeLength),
54
54
  };
55
55
  }
56
- else {
57
- console.log(v);
58
- }
59
56
  return null;
60
57
  }), (0, rxjs_1.filter)(v => !!v), (0, rxjs_1.share)());
61
- this.handle$ = this.data$.pipe((0, rxjs_1.mergeMap)(({ id, msg, type }) => {
62
- if (type === 'http' || type === 'ws') {
63
- return this.httpRequest({
64
- type,
65
- options: JSON.parse(msg.toString()),
66
- data$: this.getStreamData(id),
67
- send: (type, msg) => this.send(id, type, msg),
68
- });
69
- }
70
- return rxjs_1.EMPTY;
71
- }), (0, rxjs_1.retry)({
58
+ this.handle$ = (0, rxjs_1.merge)(this.server$, this.data$.pipe((0, rxjs_1.mergeMap)(({ id, msg, type }) => type === 'http' || type === 'ws'
59
+ ? this.httpRequest({
60
+ type,
61
+ options: JSON.parse(msg.toString()),
62
+ data$: this.getStreamData(id),
63
+ send: (type, msg) => this.send(id, type, msg),
64
+ })
65
+ : rxjs_1.EMPTY))).pipe((0, rxjs_1.retry)({
72
66
  resetOnSuccess: true,
73
67
  delay: (err, retryCount) => {
74
68
  const delaySeconds = Math.round(Math.min(300, Math.pow(1.8, retryCount + 3)));
@@ -106,13 +100,12 @@ class Client {
106
100
  this.options.logger?.trace(`[nora-link] START ${method} ${url}`);
107
101
  const tunnel = this.options.tunnels.find(v => v.remotePath === subdomain);
108
102
  if (!tunnel) {
109
- //TODO: return an error
110
- throw new Error("Subdomain not registered");
103
+ throw new Error(`subdomain ${subdomain} not registered`);
111
104
  }
112
105
  if (tunnel.removeHostHeader ?? true) {
113
106
  delete headers["host"];
114
107
  }
115
- const { hostname, port, pathname } = new URL(tunnel.localUrl);
108
+ const { hostname, port, pathname } = new URL(tunnel.url);
116
109
  const req = (0, node_http_1.request)({
117
110
  host: hostname,
118
111
  port: port,
@@ -211,7 +204,11 @@ class Client {
211
204
  return (0, rxjs_1.merge)(handleResponse$, rx$)
212
205
  .subscribe(reqObserver);
213
206
  }
214
- }).pipe((0, rxjs_1.finalize)(() => {
207
+ return () => req.end();
208
+ }).pipe((0, rxjs_1.catchError)(err => {
209
+ this.options.logger?.warn(`[nora-link] error handling request`, err);
210
+ return send(const_1.MessageTypes.BADGATEWAY);
211
+ }), (0, rxjs_1.finalize)(() => {
215
212
  this.options.logger?.trace(`[nora-link] DONE ${method} ${url}`);
216
213
  }));
217
214
  }
package/dist/const.d.ts CHANGED
@@ -3,4 +3,5 @@ export declare const MessageTypes: {
3
3
  readonly DATA: "data";
4
4
  readonly END: "end";
5
5
  readonly CLOSE: "close";
6
+ readonly BADGATEWAY: "err-502";
6
7
  };
package/dist/const.js CHANGED
@@ -6,4 +6,5 @@ exports.MessageTypes = {
6
6
  DATA: 'data',
7
7
  END: 'end',
8
8
  CLOSE: 'close',
9
+ BADGATEWAY: 'err-502',
9
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nora-link/client",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "noralink.eu client",
5
5
  "bin": {
6
6
  "noralink": "dist/cli.js"
package/src/cli.ts CHANGED
@@ -19,7 +19,7 @@ program
19
19
  }
20
20
  return previous;
21
21
  }, 'info')
22
- .requiredOption('-f, --forward <subdomain|label|url>', 'Tunnel subdomain to local url. Label is optional')
22
+ .requiredOption('-f, --forward <subdomain|label|url...>', 'Tunnel subdomain to local url. Label is optional')
23
23
  .requiredOption('-k, --key <api-key>', 'Api Key')
24
24
  .requiredOption('-h, --host <hostname>', 'Server host name', 'noralink.eu')
25
25
  .addHelpText('afterAll', 'To tunnel a local port to the subdomain `test`, you can use `-f test|:3000`')
@@ -28,36 +28,42 @@ program
28
28
 
29
29
  const options = program.opts();
30
30
 
31
- const forward = Array.isArray(options.forward) ? options.forward : [options.forward];
32
- const tunnels: ClientConnectionOptions['tunnels'] = forward
33
- .filter((v): v is string => typeof v === 'string')
34
- .map(v => {
35
- const [segment, labelOrUrl, urlPart] = v.split('|');
36
- let toUrl: string;
37
-
38
- const label = urlPart ? labelOrUrl : segment;
39
- const url = urlPart ?? labelOrUrl;
40
-
41
- if (/^https?:\/\//.test(url)) {
42
- toUrl = url;
43
- } else if (url.startsWith(':')) {
44
- //only port specified
45
- const port = +url.split(':')[1];
46
- toUrl = `http://127.0.0.1:${port}`;
47
- } else {
48
- toUrl = `http://${url}`;
49
- }
31
+ let tunnels: ClientConnectionOptions['tunnels'] = [];
32
+ if (Array.isArray(options.forward)) {
33
+ tunnels = options.forward
34
+ .filter((v): v is string => typeof v === 'string')
35
+ .map(v => {
36
+ const [segment, labelOrUrl, urlPart] = v.split('|');
37
+ let tunnelUrl: string;
38
+
39
+ const label = urlPart ? labelOrUrl : segment;
40
+ const url = urlPart ?? labelOrUrl;
50
41
 
51
- return { remotePath: segment, localUrl: toUrl, label };
52
- });
42
+ if (/^https?:\/\//.test(url)) {
43
+ tunnelUrl = url;
44
+ } else if (url.startsWith(':')) {
45
+ //only port and/or path specified
46
+ const portAndPath = url.split(':')[1];
47
+ tunnelUrl = `http://localhost:${portAndPath}`;
48
+ } else {
49
+ tunnelUrl = `http://${url}`;
50
+ }
53
51
 
54
- new Client({
55
- agent: `${name}@${version}`,
56
- hostname: options.host,
57
- secure: !options.nonSecure,
58
- tunnels,
59
- apiKey: options.key,
60
- logger: createConsoleLogger(options.log),
61
- }).handle$
62
- .subscribe();
52
+ return { remotePath: segment, url: tunnelUrl, label };
53
+ });
54
+ }
63
55
 
56
+ if (!tunnels.length) {
57
+ console.info('no valid tunnels specified');
58
+ process.exit(1);
59
+ }
60
+ else {
61
+ new Client({
62
+ agent: `${name}@${version}`,
63
+ hostname: options.host,
64
+ secure: !options.nonSecure,
65
+ tunnels,
66
+ apiKey: options.key,
67
+ logger: createConsoleLogger(options.log),
68
+ }).handle$.subscribe();
69
+ }
package/src/client.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import {
2
- EMPTY, Observable, ReplaySubject, concatMap, filter, finalize,
2
+ EMPTY, Observable, ReplaySubject, catchError, concatMap, filter, finalize,
3
3
  first, ignoreElements, map, merge, mergeMap, retry, share, switchMap, takeWhile, timer
4
4
  } from 'rxjs';
5
5
  import WebSocket from 'ws';
@@ -11,7 +11,7 @@ import { Logger } from './logger';
11
11
 
12
12
  export interface TunnelOptions {
13
13
  remotePath: string;
14
- localUrl: string;
14
+ url: string;
15
15
  label: string;
16
16
  removeHostHeader?: boolean;
17
17
  }
@@ -79,8 +79,6 @@ export class Client {
79
79
  type,
80
80
  msg: v.subarray(5 + typeLength),
81
81
  };
82
- } else {
83
- console.log(v);
84
82
  }
85
83
  return null;
86
84
  }),
@@ -88,29 +86,33 @@ export class Client {
88
86
  share(),
89
87
  );
90
88
 
91
- readonly handle$ = this.data$.pipe(
92
- mergeMap(({ id, msg, type }) => {
93
- if (type === 'http' || type === 'ws') {
94
- return this.httpRequest({
95
- type,
96
- options: JSON.parse(msg.toString()),
97
- data$: this.getStreamData(id),
98
- send: (type, msg) => this.send(id, type, msg),
99
- });
100
- }
101
- return EMPTY;
102
- }),
103
- retry({
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);
110
- },
111
- }),
112
- share(),
113
- );
89
+ readonly handle$ =
90
+ merge(
91
+ this.server$,
92
+ this.data$.pipe(
93
+ mergeMap(({ id, msg, type }) =>
94
+ type === 'http' || type === 'ws'
95
+ ? this.httpRequest({
96
+ type,
97
+ options: JSON.parse(msg.toString()),
98
+ data$: this.getStreamData(id),
99
+ send: (type, msg) => this.send(id, type, msg),
100
+ })
101
+ : EMPTY
102
+ ),
103
+ ),
104
+ ).pipe(
105
+ retry({
106
+ resetOnSuccess: true,
107
+ delay: (err, retryCount) => {
108
+ const delaySeconds = Math.round(Math.min(300, Math.pow(1.8, retryCount + 3)));
109
+ this.options.logger?.trace(`[nora-link] connection error`, err);
110
+ this.options.logger?.error(`[nora-link] retrying in ${delaySeconds} sec`);
111
+ return timer(delaySeconds * 1000);
112
+ },
113
+ }),
114
+ share(),
115
+ );
114
116
 
115
117
  send(id: number, type: string, msg?: Buffer) {
116
118
  return this.server$.pipe(
@@ -150,15 +152,14 @@ export class Client {
150
152
 
151
153
  const tunnel = this.options.tunnels.find(v => v.remotePath === subdomain);
152
154
  if (!tunnel) {
153
- //TODO: return an error
154
- throw new Error("Subdomain not registered");
155
+ throw new Error(`subdomain ${subdomain} not registered`);
155
156
  }
156
157
 
157
158
  if (tunnel.removeHostHeader ?? true) {
158
159
  delete headers["host"];
159
160
  }
160
161
 
161
- const { hostname, port, pathname } = new URL(tunnel.localUrl);
162
+ const { hostname, port, pathname } = new URL(tunnel.url);
162
163
  const req = request({
163
164
  host: hostname,
164
165
  port: port,
@@ -295,7 +296,13 @@ export class Client {
295
296
  return merge(handleResponse$, rx$)
296
297
  .subscribe(reqObserver);
297
298
  }
299
+
300
+ return () => req.end();
298
301
  }).pipe(
302
+ catchError(err => {
303
+ this.options.logger?.warn(`[nora-link] error handling request`, err);
304
+ return send(MessageTypes.BADGATEWAY);
305
+ }),
299
306
  finalize(() => {
300
307
  this.options.logger?.trace(`[nora-link] DONE ${method} ${url}`);
301
308
  }),
package/src/const.ts CHANGED
@@ -3,4 +3,5 @@ export const MessageTypes = {
3
3
  DATA: 'data',
4
4
  END: 'end',
5
5
  CLOSE: 'close',
6
+ BADGATEWAY: 'err-502',
6
7
  } as const;