@nora-link/client 1.0.6 → 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
@@ -31,21 +31,21 @@ if (Array.isArray(options.forward)) {
31
31
  .filter((v) => typeof v === 'string')
32
32
  .map(v => {
33
33
  const [segment, labelOrUrl, urlPart] = v.split('|');
34
- let toUrl;
34
+ let tunnelUrl;
35
35
  const label = urlPart ? labelOrUrl : segment;
36
36
  const url = urlPart ?? labelOrUrl;
37
37
  if (/^https?:\/\//.test(url)) {
38
- toUrl = url;
38
+ tunnelUrl = url;
39
39
  }
40
40
  else if (url.startsWith(':')) {
41
- //only port specified
42
- const port = +url.split(':')[1];
43
- toUrl = `http://127.0.0.1:${port}`;
41
+ //only port and/or path specified
42
+ const portAndPath = url.split(':')[1];
43
+ tunnelUrl = `http://localhost:${portAndPath}`;
44
44
  }
45
45
  else {
46
- toUrl = `http://${url}`;
46
+ tunnelUrl = `http://${url}`;
47
47
  }
48
- return { remotePath: segment, localUrl: toUrl, label };
48
+ return { remotePath: segment, url: tunnelUrl, label };
49
49
  });
50
50
  }
51
51
  if (!tunnels.length) {
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.6",
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
@@ -34,22 +34,22 @@ if (Array.isArray(options.forward)) {
34
34
  .filter((v): v is string => typeof v === 'string')
35
35
  .map(v => {
36
36
  const [segment, labelOrUrl, urlPart] = v.split('|');
37
- let toUrl: string;
37
+ let tunnelUrl: string;
38
38
 
39
39
  const label = urlPart ? labelOrUrl : segment;
40
40
  const url = urlPart ?? labelOrUrl;
41
41
 
42
42
  if (/^https?:\/\//.test(url)) {
43
- toUrl = url;
43
+ tunnelUrl = url;
44
44
  } else if (url.startsWith(':')) {
45
- //only port specified
46
- const port = +url.split(':')[1];
47
- toUrl = `http://127.0.0.1:${port}`;
45
+ //only port and/or path specified
46
+ const portAndPath = url.split(':')[1];
47
+ tunnelUrl = `http://localhost:${portAndPath}`;
48
48
  } else {
49
- toUrl = `http://${url}`;
49
+ tunnelUrl = `http://${url}`;
50
50
  }
51
51
 
52
- return { remotePath: segment, localUrl: toUrl, label };
52
+ return { remotePath: segment, url: tunnelUrl, label };
53
53
  });
54
54
  }
55
55
 
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;