@nora-link/client 1.0.16 → 1.1.0
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 +0 -1
- package/dist/client.d.ts +10 -2
- package/dist/client.js +63 -27
- package/dist/firebase.d.ts +11 -0
- package/dist/firebase.js +36 -0
- package/package.json +2 -1
package/dist/cli.js
CHANGED
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Observable } from 'rxjs';
|
|
1
|
+
import { BehaviorSubject, Observable } from 'rxjs';
|
|
2
2
|
import { Logger } from './logger';
|
|
3
3
|
export interface TunnelOptions {
|
|
4
4
|
subdomain: string;
|
|
@@ -17,10 +17,18 @@ export interface ClientConnectionOptions {
|
|
|
17
17
|
export declare class Client {
|
|
18
18
|
private options;
|
|
19
19
|
private retryCount;
|
|
20
|
+
private clientId;
|
|
20
21
|
constructor(options: ClientConnectionOptions);
|
|
21
22
|
private server$;
|
|
22
23
|
private data$;
|
|
23
|
-
readonly
|
|
24
|
+
private readonly connectToServer$;
|
|
25
|
+
readonly idle$: BehaviorSubject<{
|
|
26
|
+
db: string;
|
|
27
|
+
dbKey: string;
|
|
28
|
+
apiKey: string;
|
|
29
|
+
token: string;
|
|
30
|
+
} | null>;
|
|
31
|
+
readonly handle$: Observable<'connected' | 'connecting' | 'disconnected' | 'idle'>;
|
|
24
32
|
private send;
|
|
25
33
|
private getStreamData;
|
|
26
34
|
private httpRequest;
|
package/dist/client.js
CHANGED
|
@@ -9,13 +9,15 @@ const ws_1 = __importDefault(require("ws"));
|
|
|
9
9
|
const node_http_1 = require("node:http");
|
|
10
10
|
const node_querystring_1 = require("node:querystring");
|
|
11
11
|
const const_1 = require("./const");
|
|
12
|
+
const firebase_1 = require("./firebase");
|
|
13
|
+
const node_crypto_1 = require("node:crypto");
|
|
12
14
|
class Client {
|
|
13
15
|
constructor(options) {
|
|
14
16
|
this.options = options;
|
|
15
17
|
this.retryCount = 1;
|
|
16
18
|
this.server$ = new rxjs_1.Observable(observer => {
|
|
17
19
|
const subdomains = this.options.tunnels.map(v => `${v.subdomain}|${v.label}`);
|
|
18
|
-
const qs = (0, node_querystring_1.stringify)({ s: subdomains });
|
|
20
|
+
const qs = (0, node_querystring_1.stringify)({ s: subdomains, c: this.clientId });
|
|
19
21
|
const protocol = (this.options.secure ?? true) ? 'wss' : 'ws';
|
|
20
22
|
const headers = {
|
|
21
23
|
authorization: `Bearer ${this.options.apiKey}`,
|
|
@@ -55,28 +57,47 @@ class Client {
|
|
|
55
57
|
ws.close();
|
|
56
58
|
};
|
|
57
59
|
})), (0, rxjs_1.map)(v => {
|
|
58
|
-
if (v.length
|
|
59
|
-
|
|
60
|
-
const typeLength = v.readUint8(4);
|
|
61
|
-
const type = v.subarray(5, 5 + typeLength).toString();
|
|
62
|
-
return {
|
|
63
|
-
id,
|
|
64
|
-
type,
|
|
65
|
-
msg: v.subarray(5 + typeLength),
|
|
66
|
-
};
|
|
60
|
+
if (v.length < 18) {
|
|
61
|
+
return null;
|
|
67
62
|
}
|
|
68
|
-
|
|
63
|
+
let offset = 0;
|
|
64
|
+
const version = v.readUInt8(offset++);
|
|
65
|
+
if (version !== 1) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const id = v.subarray(offset, offset + 16);
|
|
69
|
+
offset += 16;
|
|
70
|
+
const typeLength = v.readUint8(offset++);
|
|
71
|
+
const type = v.subarray(offset, offset + typeLength).toString();
|
|
72
|
+
offset += typeLength;
|
|
73
|
+
const msg = v.subarray(offset);
|
|
74
|
+
return {
|
|
75
|
+
id,
|
|
76
|
+
type,
|
|
77
|
+
msg,
|
|
78
|
+
};
|
|
69
79
|
}), (0, rxjs_1.filter)(v => !!v), (0, rxjs_1.share)());
|
|
70
|
-
this.
|
|
80
|
+
this.connectToServer$ = (0, rxjs_1.merge)(this.server$.pipe(
|
|
81
|
+
//wait a bit before deeming `connected`. Server might close the connection for various reasons.
|
|
82
|
+
(0, rxjs_1.delay)(500), (0, rxjs_1.tap)(() => {
|
|
71
83
|
this.retryCount = 1;
|
|
72
|
-
}), (0, rxjs_1.map)(_ => 'connected')), this.data$.pipe((0, rxjs_1.mergeMap)(({ id, msg, type }) =>
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
84
|
+
}), (0, rxjs_1.map)(_ => 'connected')), this.data$.pipe((0, rxjs_1.mergeMap)(({ id, msg, type }) => {
|
|
85
|
+
switch (type) {
|
|
86
|
+
case 'http':
|
|
87
|
+
case 'ws':
|
|
88
|
+
return this.httpRequest({
|
|
89
|
+
type,
|
|
90
|
+
options: JSON.parse(msg.toString()),
|
|
91
|
+
data$: this.getStreamData(id),
|
|
92
|
+
send: (type, msg) => this.send(id, type, msg),
|
|
93
|
+
});
|
|
94
|
+
case 'go-idle':
|
|
95
|
+
const { db, dbKey, apiKey, token } = JSON.parse(msg.toString());
|
|
96
|
+
this.idle$.next({ db, dbKey, apiKey, token });
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
return rxjs_1.EMPTY;
|
|
100
|
+
}))).pipe((0, rxjs_1.startWith)('connecting'), (0, rxjs_1.catchError)(err => (0, rxjs_1.concat)((0, rxjs_1.of)('disconnected'), (0, rxjs_1.throwError)(() => err))), (0, rxjs_1.retry)({
|
|
80
101
|
delay: (err) => {
|
|
81
102
|
const delaySeconds = Math.round(Math.min(600, Math.pow(1.8, this.retryCount - 1)));
|
|
82
103
|
this.options.logger?.error(`[nora-link] connection error ${this.retryCount}: ${err}`);
|
|
@@ -85,16 +106,31 @@ class Client {
|
|
|
85
106
|
return (0, rxjs_1.timer)(delaySeconds * 1000);
|
|
86
107
|
},
|
|
87
108
|
}), (0, rxjs_1.share)());
|
|
109
|
+
this.idle$ = new rxjs_1.BehaviorSubject(null);
|
|
110
|
+
this.handle$ = this.idle$.pipe((0, rxjs_1.switchMap)(idle => idle
|
|
111
|
+
? (0, firebase_1.goIdleAndWaitForSignal)({
|
|
112
|
+
...idle,
|
|
113
|
+
subdomains: this.options.tunnels.map(t => t.subdomain),
|
|
114
|
+
goOutOfIdle: () => this.idle$.next(null),
|
|
115
|
+
logger: this.options.logger,
|
|
116
|
+
})
|
|
117
|
+
: this.connectToServer$));
|
|
118
|
+
if (!options.agent) {
|
|
119
|
+
const { name, version } = require('../package.json');
|
|
120
|
+
options.agent = `${name}@${version}`;
|
|
121
|
+
}
|
|
122
|
+
this.clientId = (0, node_crypto_1.randomBytes)(16).toString('base64url');
|
|
88
123
|
}
|
|
89
124
|
send(id, type, msg) {
|
|
90
125
|
return this.server$.pipe((0, rxjs_1.first)(), (0, rxjs_1.switchMap)(ws => {
|
|
91
|
-
const header = Buffer.alloc(5);
|
|
92
126
|
const typeBuffer = Buffer.from(type);
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
127
|
+
let offset = 0;
|
|
128
|
+
const all = Buffer.alloc(18 + typeBuffer.length + (msg?.length ?? 0));
|
|
129
|
+
offset = all.writeUint8(1, offset);
|
|
130
|
+
offset += id.copy(all, offset, 0, 16);
|
|
131
|
+
offset = all.writeUint8(typeBuffer.length, offset);
|
|
132
|
+
offset += typeBuffer.copy(all, offset);
|
|
133
|
+
msg?.copy(all, offset);
|
|
98
134
|
return new rxjs_1.Observable(observer => {
|
|
99
135
|
ws.send(all, (err) => {
|
|
100
136
|
if (err)
|
|
@@ -106,7 +142,7 @@ class Client {
|
|
|
106
142
|
}));
|
|
107
143
|
}
|
|
108
144
|
getStreamData(id) {
|
|
109
|
-
return this.data$.pipe((0, rxjs_1.filter)((m) => m.id
|
|
145
|
+
return this.data$.pipe((0, rxjs_1.filter)((m) => m.id.equals(id)), (0, rxjs_1.map)(({ id, ...rest }) => rest));
|
|
110
146
|
}
|
|
111
147
|
httpRequest({ type, options, data$, send }) {
|
|
112
148
|
const { url, subdomain, method, headers } = options;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
import { Logger } from "./logger";
|
|
3
|
+
export declare function goIdleAndWaitForSignal(opts: {
|
|
4
|
+
db: string;
|
|
5
|
+
dbKey: string;
|
|
6
|
+
apiKey: string;
|
|
7
|
+
token: string;
|
|
8
|
+
subdomains: string[];
|
|
9
|
+
goOutOfIdle: () => void;
|
|
10
|
+
logger?: Logger;
|
|
11
|
+
}): Observable<'idle'>;
|
package/dist/firebase.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.goIdleAndWaitForSignal = goIdleAndWaitForSignal;
|
|
4
|
+
const rxjs_1 = require("rxjs");
|
|
5
|
+
const app_1 = require("firebase/app");
|
|
6
|
+
const auth_1 = require("firebase/auth");
|
|
7
|
+
const database_1 = require("firebase/database");
|
|
8
|
+
function goIdleAndWaitForSignal(opts) {
|
|
9
|
+
return (0, rxjs_1.defer)(async () => {
|
|
10
|
+
const app = (0, app_1.initializeApp)({
|
|
11
|
+
apiKey: opts.apiKey,
|
|
12
|
+
});
|
|
13
|
+
const auth = (0, auth_1.getAuth)(app);
|
|
14
|
+
await (0, auth_1.signInWithCustomToken)(auth, opts.token);
|
|
15
|
+
const db = (0, database_1.getDatabase)(app, opts.db);
|
|
16
|
+
return (0, database_1.ref)(db, opts.dbKey);
|
|
17
|
+
}).pipe((0, rxjs_1.switchMap)((watchRef) => new rxjs_1.Observable(observer => (0, database_1.onValue)(watchRef, snap => {
|
|
18
|
+
const value = snap.val() ?? {};
|
|
19
|
+
observer.next(opts.subdomains.map(s => value[s] ?? null));
|
|
20
|
+
}, (err) => {
|
|
21
|
+
observer.error(err);
|
|
22
|
+
}))), (0, rxjs_1.distinctUntilChanged)((a, b) => {
|
|
23
|
+
for (let i = 0; i < a.length; i++) {
|
|
24
|
+
if (a[i] !== b[i]) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}), (0, rxjs_1.skip)(1), (0, rxjs_1.timeout)(3600 * 1000), (0, rxjs_1.tap)(() => opts.goOutOfIdle()), (0, rxjs_1.ignoreElements)(), (0, rxjs_1.startWith)('idle'), (0, rxjs_1.catchError)(err => {
|
|
30
|
+
if (!(err instanceof rxjs_1.TimeoutError)) {
|
|
31
|
+
opts.logger?.error(`[nora-link] idle error ${err}`);
|
|
32
|
+
}
|
|
33
|
+
opts.goOutOfIdle();
|
|
34
|
+
return rxjs_1.EMPTY;
|
|
35
|
+
}));
|
|
36
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nora-link/client",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "noralink.eu client",
|
|
5
5
|
"bin": {
|
|
6
6
|
"noralink": "dist/cli.js"
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"commander": "^12.0.0",
|
|
31
|
+
"firebase": "^10.13.1",
|
|
31
32
|
"rxjs": "^7.8.1",
|
|
32
33
|
"ws": "^8.16.0"
|
|
33
34
|
}
|