@openlv/signaling 0.0.1-beta.2
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/LICENSE +165 -0
- package/README.md +3 -0
- package/dist/base.d.ts +49 -0
- package/dist/base.js +169 -0
- package/dist/base.js.map +1 -0
- package/dist/dynamic.d.ts +6 -0
- package/dist/dynamic.js +16 -0
- package/dist/dynamic.js.map +1 -0
- package/dist/events.d.ts +4 -0
- package/dist/events.js +2 -0
- package/dist/events.js.map +1 -0
- package/dist/gundb/index.d.ts +6 -0
- package/dist/gundb/index.js +49 -0
- package/dist/gundb/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +72 -0
- package/dist/index.test.js.map +1 -0
- package/dist/messages/index.d.ts +21 -0
- package/dist/messages/index.js +2 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/mqtt/index.d.ts +7 -0
- package/dist/mqtt/index.js +51 -0
- package/dist/mqtt/index.js.map +1 -0
- package/dist/ntfy/index.d.ts +26 -0
- package/dist/ntfy/index.js +87 -0
- package/dist/ntfy/index.js.map +1 -0
- package/dist/ntfy/url.d.ts +13 -0
- package/dist/ntfy/url.js +41 -0
- package/dist/ntfy/url.js.map +1 -0
- package/dist/ntfy/url.test.d.ts +1 -0
- package/dist/ntfy/url.test.js +54 -0
- package/dist/ntfy/url.test.js.map +1 -0
- package/dist/utils/log.d.ts +1 -0
- package/dist/utils/log.js +2 -0
- package/dist/utils/log.js.map +1 -0
- package/package.json +81 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
Version 3, 29 June 2007
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
6
|
+
of this license document, but changing it is not allowed.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
package/README.md
ADDED
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { EncryptionKey, SymmetricKey } from "@openlv/core/encryption";
|
|
2
|
+
import { EventEmitter } from "eventemitter3";
|
|
3
|
+
import type { MaybePromise } from "viem";
|
|
4
|
+
import type { SignalingEvents } from "./events.js";
|
|
5
|
+
export type SignalBaseProperties = {
|
|
6
|
+
topic: string;
|
|
7
|
+
url: string;
|
|
8
|
+
};
|
|
9
|
+
export type SignalingBaseLayer = {
|
|
10
|
+
type: string;
|
|
11
|
+
setup: () => MaybePromise<void>;
|
|
12
|
+
teardown: () => MaybePromise<void>;
|
|
13
|
+
publish: (payload: string) => MaybePromise<void>;
|
|
14
|
+
subscribe: (handler: (payload: string) => void) => MaybePromise<void>;
|
|
15
|
+
} & {
|
|
16
|
+
[Symbol.asyncDispose]?: () => MaybePromise<void>;
|
|
17
|
+
};
|
|
18
|
+
export type CreateSignalLayerFn = (properties: SignalBaseProperties) => MaybePromise<SignalingLayer>;
|
|
19
|
+
export type SignalingProperties = {
|
|
20
|
+
isHost: boolean;
|
|
21
|
+
h: string;
|
|
22
|
+
k?: SymmetricKey;
|
|
23
|
+
rpDiscovered: (rpKey: string) => MaybePromise<void>;
|
|
24
|
+
decrypt: (message: string) => MaybePromise<string>;
|
|
25
|
+
encrypt: (message: string) => MaybePromise<string>;
|
|
26
|
+
publicKey: EncryptionKey;
|
|
27
|
+
canEncrypt: () => boolean;
|
|
28
|
+
};
|
|
29
|
+
export type SignalingLayer = (properties: SignalingProperties) => Promise<{
|
|
30
|
+
type: string;
|
|
31
|
+
send: (message: object) => MaybePromise<void>;
|
|
32
|
+
subscribe: (handler: (message: object) => void) => MaybePromise<void>;
|
|
33
|
+
setup: () => MaybePromise<void>;
|
|
34
|
+
teardown: () => MaybePromise<void>;
|
|
35
|
+
getState: () => {
|
|
36
|
+
state: SignalingMode;
|
|
37
|
+
};
|
|
38
|
+
emitter: EventEmitter<SignalingEvents>;
|
|
39
|
+
[Symbol.asyncDispose]?: () => MaybePromise<void>;
|
|
40
|
+
}>;
|
|
41
|
+
export declare const XR_PREFIX = "x";
|
|
42
|
+
export declare const XR_H_PREFIX = "h";
|
|
43
|
+
export type SignalingMode = "handshake-open" | "handshaking" | "handshake-closed" | "xr-encrypted" | "connecting" | "disconnected";
|
|
44
|
+
/**
|
|
45
|
+
* Base Signaling Layer implementation
|
|
46
|
+
*
|
|
47
|
+
* https://openlv.sh/api/signaling
|
|
48
|
+
*/
|
|
49
|
+
export declare const createSignalingLayer: (init: SignalingBaseLayer) => SignalingLayer;
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { match } from "ts-pattern";
|
|
3
|
+
import { log } from "./utils/log.js";
|
|
4
|
+
export const XR_PREFIX = "x";
|
|
5
|
+
export const XR_H_PREFIX = "h";
|
|
6
|
+
/**
|
|
7
|
+
* Base Signaling Layer implementation
|
|
8
|
+
*
|
|
9
|
+
* https://openlv.sh/api/signaling
|
|
10
|
+
*/
|
|
11
|
+
export const createSignalingLayer = (init) => {
|
|
12
|
+
const emitter = new EventEmitter();
|
|
13
|
+
return async ({ canEncrypt, encrypt, decrypt, rpDiscovered, k, publicKey, isHost, }) => {
|
|
14
|
+
// shared signaling layer logic goes here
|
|
15
|
+
const handshakeKey = k ? k : undefined;
|
|
16
|
+
let mode = "disconnected";
|
|
17
|
+
const subscribeHandler = new EventEmitter();
|
|
18
|
+
const setMode = (_mode) => {
|
|
19
|
+
mode = _mode;
|
|
20
|
+
emitter.emit("state_change", _mode);
|
|
21
|
+
};
|
|
22
|
+
const send = async (method, recipient, payload) => {
|
|
23
|
+
const prefix = match(method)
|
|
24
|
+
.with("handshake", () => XR_H_PREFIX)
|
|
25
|
+
.with("encrypted", () => XR_PREFIX)
|
|
26
|
+
.exhaustive();
|
|
27
|
+
const message = await match(method)
|
|
28
|
+
.with("handshake", () => {
|
|
29
|
+
if (!handshakeKey)
|
|
30
|
+
return;
|
|
31
|
+
return handshakeKey.encrypt(JSON.stringify(payload));
|
|
32
|
+
})
|
|
33
|
+
.with("encrypted", () => {
|
|
34
|
+
if (!canEncrypt())
|
|
35
|
+
return;
|
|
36
|
+
return encrypt(JSON.stringify(payload));
|
|
37
|
+
})
|
|
38
|
+
.exhaustive();
|
|
39
|
+
await init.publish(prefix + recipient + message);
|
|
40
|
+
};
|
|
41
|
+
const handleReceive = async (payload) => {
|
|
42
|
+
const prefix = payload.slice(0, XR_H_PREFIX.length);
|
|
43
|
+
const recipient = payload.slice(XR_H_PREFIX.length, XR_H_PREFIX.length + 1);
|
|
44
|
+
const body = payload.slice(XR_PREFIX.length + 1);
|
|
45
|
+
const isRecipient = (isHost ? "h" : "c") === recipient;
|
|
46
|
+
log("isRecipient", isRecipient);
|
|
47
|
+
if (!isRecipient)
|
|
48
|
+
return;
|
|
49
|
+
await match({ prefix, body })
|
|
50
|
+
.with({ prefix: XR_H_PREFIX }, async () => {
|
|
51
|
+
if (!handshakeKey)
|
|
52
|
+
return;
|
|
53
|
+
const message = await handshakeKey.decrypt(body);
|
|
54
|
+
const msg = JSON.parse(message);
|
|
55
|
+
await match(msg)
|
|
56
|
+
.with({ type: "flash" }, async () => {
|
|
57
|
+
if (!isHost)
|
|
58
|
+
return;
|
|
59
|
+
setMode("handshaking");
|
|
60
|
+
await send("handshake", "c", {
|
|
61
|
+
type: "pubkey",
|
|
62
|
+
payload: {
|
|
63
|
+
publicKey: publicKey.toString(),
|
|
64
|
+
},
|
|
65
|
+
timestamp: Date.now(),
|
|
66
|
+
});
|
|
67
|
+
})
|
|
68
|
+
.with({ type: "pubkey" }, async ({ payload }) => {
|
|
69
|
+
if (isHost)
|
|
70
|
+
return;
|
|
71
|
+
await rpDiscovered(payload.publicKey);
|
|
72
|
+
setMode("handshake-closed");
|
|
73
|
+
return await send("encrypted", "h", {
|
|
74
|
+
type: "pubkey",
|
|
75
|
+
payload: {
|
|
76
|
+
publicKey: publicKey.toString(),
|
|
77
|
+
},
|
|
78
|
+
timestamp: Date.now(),
|
|
79
|
+
});
|
|
80
|
+
})
|
|
81
|
+
.otherwise(() => {
|
|
82
|
+
log("Received invalid message H", msg);
|
|
83
|
+
});
|
|
84
|
+
})
|
|
85
|
+
.with({ prefix: XR_PREFIX }, async () => {
|
|
86
|
+
const message = await decrypt(body);
|
|
87
|
+
const msg = JSON.parse(message);
|
|
88
|
+
await match(msg)
|
|
89
|
+
.with({ type: "pubkey" }, async ({ payload }) => {
|
|
90
|
+
if (!isHost)
|
|
91
|
+
return;
|
|
92
|
+
await rpDiscovered(payload.publicKey);
|
|
93
|
+
setMode("xr-encrypted");
|
|
94
|
+
return await send("encrypted", "c", {
|
|
95
|
+
type: "ack",
|
|
96
|
+
payload: undefined,
|
|
97
|
+
timestamp: Date.now(),
|
|
98
|
+
});
|
|
99
|
+
})
|
|
100
|
+
.with({ type: "ack" }, async () => {
|
|
101
|
+
if (isHost)
|
|
102
|
+
return;
|
|
103
|
+
setMode("xr-encrypted");
|
|
104
|
+
})
|
|
105
|
+
.with({ type: "data" }, async () => subscribeHandler.emit("message", msg.payload))
|
|
106
|
+
.otherwise(() => {
|
|
107
|
+
log("Received invalid message X", msg);
|
|
108
|
+
});
|
|
109
|
+
})
|
|
110
|
+
.otherwise(() => {
|
|
111
|
+
log("Received invalid message", payload);
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
return {
|
|
115
|
+
type: init.type,
|
|
116
|
+
async setup() {
|
|
117
|
+
setMode("connecting");
|
|
118
|
+
await init.setup();
|
|
119
|
+
if (!canEncrypt()) {
|
|
120
|
+
if (!isHost) {
|
|
121
|
+
setMode("handshaking");
|
|
122
|
+
await send("handshake", "h", {
|
|
123
|
+
type: "flash",
|
|
124
|
+
payload: {},
|
|
125
|
+
timestamp: Date.now(),
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (isHost) {
|
|
129
|
+
setMode("handshake-open");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
await init.subscribe(handleReceive);
|
|
133
|
+
},
|
|
134
|
+
async teardown() {
|
|
135
|
+
log("teardown");
|
|
136
|
+
await init.teardown?.();
|
|
137
|
+
return await init[Symbol.asyncDispose]?.();
|
|
138
|
+
},
|
|
139
|
+
async [Symbol.asyncDispose]() {
|
|
140
|
+
await init.teardown?.();
|
|
141
|
+
return await init[Symbol.asyncDispose]?.();
|
|
142
|
+
},
|
|
143
|
+
send(message) {
|
|
144
|
+
if (!canEncrypt()) {
|
|
145
|
+
return Promise.reject(new Error("Cannot encrypt message before keys are exchanged"));
|
|
146
|
+
}
|
|
147
|
+
const them = isHost ? "c" : "h";
|
|
148
|
+
return send("encrypted", them, {
|
|
149
|
+
type: "data",
|
|
150
|
+
payload: message,
|
|
151
|
+
timestamp: Date.now(),
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
/**
|
|
155
|
+
* Base Signaling Layer Subscriber Handler
|
|
156
|
+
*
|
|
157
|
+
* This handler is responsible for decryption of Handshake & XR Encrypted Messages
|
|
158
|
+
*/
|
|
159
|
+
subscribe(handler) {
|
|
160
|
+
subscribeHandler.on("message", handler);
|
|
161
|
+
},
|
|
162
|
+
getState() {
|
|
163
|
+
return { state: mode };
|
|
164
|
+
},
|
|
165
|
+
emitter,
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
//# sourceMappingURL=base.js.map
|
package/dist/base.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAKnC,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAmDrC,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,CAAC;AAC7B,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAgB/B;;;;GAIG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAClC,IAAwB,EACR,EAAE;IAClB,MAAM,OAAO,GAAG,IAAI,YAAY,EAAmB,CAAC;IAEpD,OAAO,KAAK,EAAE,EACZ,UAAU,EACV,OAAO,EACP,OAAO,EACP,YAAY,EACZ,CAAC,EACD,SAAS,EACT,MAAM,GACc,EAAE,EAAE;QACxB,yCAAyC;QACzC,MAAM,YAAY,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACvC,IAAI,IAAI,GAAkB,cAAc,CAAC;QACzC,MAAM,gBAA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AC;gBACzB,MAAM,IAAI,CAAC,QAAQ,EAAE,EAAE,CAAC;gBAExB,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,EAAE,EAAE,CAAC;YAC7C,CAAC;YACD,IAAI,CAAC,OAAO;gBACV,IAAI,CAAC,UAAU,EAAE,EAAE,CAAC;oBAClB,OAAO,OAAO,CAAC,MAAM,CACnB,IAAI,KAAK,CAAC,kDAAkD,CAAC,CAC9D,CAAC;gBACJ,CAAC;gBAED,MAAM,IAAI,GAAG,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC;gBAEhC,OAAO,IAAI,CAAC,WAAW,EAAE,IAAI,EAAE;oBAC7B,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE,OAAO;oBAChB,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE;iBACtB,CAAC,CAAC;YACL,CAAC;YACD;;;;eAIG;YACH,SAAS,CAAC,OAAO;gBACf,gBAAgB,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAC1C,CAAC;YACD,QAAQ;gBACN,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;YACzB,CAAC;YACD,OAAO;SACR,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
package/dist/dynamic.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { match } from "ts-pattern";
|
|
2
|
+
/**
|
|
3
|
+
* Imports the signaling layer based on the protocol at runtime
|
|
4
|
+
*
|
|
5
|
+
* TODO: Make this a built-in feature of session/provider
|
|
6
|
+
*/
|
|
7
|
+
export const dynamicSignalingLayer = async (protocol) => {
|
|
8
|
+
return match(protocol)
|
|
9
|
+
.with("mqtt", async () => (await import("./mqtt/index.js"))["mqtt"])
|
|
10
|
+
.with("ntfy", async () => (await import("./ntfy/index.js"))["ntfy"])
|
|
11
|
+
.with("gun", async () => (await import("./gundb/index.js"))["gundb"])
|
|
12
|
+
.otherwise(() => {
|
|
13
|
+
throw new Error(`Unknown signaling protocol: ${protocol}`);
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=dynamic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dynamic.js","sourceRoot":"","sources":["../src/dynamic.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EAAE,QAAgB,EAAE,EAAE;IAC9D,OAAO,KAAK,CAAC,QAAQ,CAAC;SACnB,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;SACnE,IAAI,CAAC,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,MAAM,CAAC,iBAAiB,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;SACnE,IAAI,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,MAAM,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC;SACpE,SAAS,CAAC,GAAG,EAAE;QACd,MAAM,IAAI,KAAK,CAAC,+BAA+B,QAAQ,EAAE,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;AACP,CAAC,CAAC"}
|
package/dist/events.d.ts
ADDED
package/dist/events.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { SignalNoConnectionError } from "@openlv/core/errors";
|
|
2
|
+
import Gun from "gun";
|
|
3
|
+
import { createSignalingLayer } from "../base.js";
|
|
4
|
+
import { log } from "../utils/log.js";
|
|
5
|
+
/**
|
|
6
|
+
* GunDB Signaling Layer
|
|
7
|
+
* https://openlv.sh/api/signaling/gun
|
|
8
|
+
*/
|
|
9
|
+
export const gundb = ({ topic, url = "wss://try.axe.eco/gun", }) => {
|
|
10
|
+
let connection;
|
|
11
|
+
return createSignalingLayer({
|
|
12
|
+
type: "gundb",
|
|
13
|
+
async setup() {
|
|
14
|
+
log("GUNDB: Setting up");
|
|
15
|
+
const x = new Gun({
|
|
16
|
+
peers: [url],
|
|
17
|
+
file: undefined,
|
|
18
|
+
localStorage: undefined,
|
|
19
|
+
});
|
|
20
|
+
connection = x;
|
|
21
|
+
await connection?.get(topic);
|
|
22
|
+
},
|
|
23
|
+
teardown() {
|
|
24
|
+
// connection?.();
|
|
25
|
+
connection = undefined;
|
|
26
|
+
},
|
|
27
|
+
async publish(payload) {
|
|
28
|
+
if (!connection)
|
|
29
|
+
throw new SignalNoConnectionError();
|
|
30
|
+
log("GUNDB: Publishing message", topic, payload);
|
|
31
|
+
connection.get(topic).put({ data: payload });
|
|
32
|
+
},
|
|
33
|
+
async subscribe(handler) {
|
|
34
|
+
if (!connection)
|
|
35
|
+
throw new SignalNoConnectionError();
|
|
36
|
+
connection.get(topic).on((data) => {
|
|
37
|
+
log("GUNDB: Received message", data);
|
|
38
|
+
const message = data.data?.toString();
|
|
39
|
+
if (!message)
|
|
40
|
+
return;
|
|
41
|
+
handler(message);
|
|
42
|
+
});
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
Object.assign(gundb, {
|
|
47
|
+
__name: "gundb",
|
|
48
|
+
});
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gundb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,GAAqC,MAAM,KAAK,CAAC;AAExD,OAAO,EAAE,oBAAoB,EAA4B,MAAM,YAAY,CAAC;AAC5E,OA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,OAAO,CAAC,OAAO,CAAC,CAAC;YACnB,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE;IACnB,MAAM,EAAE,OAAO;CAChB,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,yEAAyE;AAEzE,cAAc,WAAW,CAAC;AAC1B,cAAc,qBAAqB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { generateKeyPair } from "@openlv/core/encryption";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { gundb } from "./gundb/index.js";
|
|
4
|
+
import { mqtt } from "./mqtt/index.js";
|
|
5
|
+
import { ntfy } from "./ntfy/index.js";
|
|
6
|
+
import { log } from "./utils/log.js";
|
|
7
|
+
const hKey = "test";
|
|
8
|
+
const layers = [
|
|
9
|
+
[
|
|
10
|
+
mqtt,
|
|
11
|
+
{ topic: "mytesttopic1111", url: "wss://test.mosquitto.org:8081/mqtt" },
|
|
12
|
+
],
|
|
13
|
+
[
|
|
14
|
+
mqtt,
|
|
15
|
+
{ topic: "mytesttopic1111", url: "wss://mqtt-dashboard.com:8884/mqtt" },
|
|
16
|
+
],
|
|
17
|
+
[mqtt, { topic: "mytesttopic1111", url: "ws://broker.emqx.io:8083/mqtt" }],
|
|
18
|
+
[ntfy, { topic: "mytesttopic1111", url: "https://ntfy.sh/" }],
|
|
19
|
+
[gundb, { topic: "mytesttopic1111", url: "wss://try.axe.eco/gun" }],
|
|
20
|
+
];
|
|
21
|
+
describe("Signaling layers", () => {
|
|
22
|
+
layers.forEach(([layer, props]) => {
|
|
23
|
+
const layername = layer.__name;
|
|
24
|
+
it(`should create a signaling layer for ${layername} - ${props.url}`, async () => {
|
|
25
|
+
const { encryptionKey: publicKey, decryptionKey } = await generateKeyPair();
|
|
26
|
+
const h = hKey;
|
|
27
|
+
const signalingLayer = await layer(props);
|
|
28
|
+
const signalA = await signalingLayer({
|
|
29
|
+
h,
|
|
30
|
+
rpDiscovered: (v) => log("rpKey", v),
|
|
31
|
+
canEncrypt: () => true,
|
|
32
|
+
encrypt: publicKey.encrypt,
|
|
33
|
+
decrypt: decryptionKey.decrypt,
|
|
34
|
+
publicKey,
|
|
35
|
+
isHost: true,
|
|
36
|
+
});
|
|
37
|
+
const signalB = await signalingLayer({
|
|
38
|
+
h,
|
|
39
|
+
rpDiscovered: (v) => log("rpKey", v),
|
|
40
|
+
canEncrypt: () => true,
|
|
41
|
+
encrypt: publicKey.encrypt,
|
|
42
|
+
decrypt: decryptionKey.decrypt,
|
|
43
|
+
publicKey,
|
|
44
|
+
isHost: false,
|
|
45
|
+
});
|
|
46
|
+
expect(signalA).toBeDefined();
|
|
47
|
+
expect(signalB).toBeDefined();
|
|
48
|
+
await Promise.all([signalA.setup(), signalB.setup()]);
|
|
49
|
+
const messageReceivedA = new Promise((resolve) => {
|
|
50
|
+
signalA.subscribe((message) => {
|
|
51
|
+
log("messageReceivedA", message);
|
|
52
|
+
expect(message).toEqual({ data: "test2" });
|
|
53
|
+
resolve();
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
const messageReceivedB = new Promise((resolve) => {
|
|
57
|
+
signalB.subscribe((message) => {
|
|
58
|
+
log("messageReceivedB", message);
|
|
59
|
+
expect(message).toEqual({ data: "test1" });
|
|
60
|
+
resolve();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
await signalA.send({ data: "test1" });
|
|
64
|
+
await messageReceivedB;
|
|
65
|
+
await signalB.send({ data: "test2" });
|
|
66
|
+
await messageReceivedA;
|
|
67
|
+
await signalA.teardown();
|
|
68
|
+
await signalB.teardown();
|
|
69
|
+
});
|
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,OAAO,CAAC,CAAC;oBACjC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;oBAC3C,OAAO,EAAE,CAAC;gBACZ,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;YACH,MAAM,gBAAgB,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;gBACrD,OAAO,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,EAAE;oBAC5B,GAAG,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;oBACjC,MAAM,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;oBAC3C,OAAO,EAAE,CAAC;gBACZ,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;YAEH,MAAM,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;YAEtC,MAAM,gBAAgB,CAAC;YAEvB,MAAM,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;YAEtC,MAAM,gBAAgB,CAAC;YAEvB,MAAM,OAAO,CAAC,QAAQ,EAAE,CAAC;YACzB,MAAM,OAAO,CAAC,QAAQ,EAAE,CAAC;QAC3B,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type SignalMessageBase<T extends string, P> = {
|
|
2
|
+
type: T;
|
|
3
|
+
payload: P;
|
|
4
|
+
timestamp: number;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Flash message
|
|
8
|
+
* Sent to initiate handshake by non-host
|
|
9
|
+
*/
|
|
10
|
+
export type SignalMessageFlash = SignalMessageBase<"flash", object>;
|
|
11
|
+
export type SignalMessagePubkey = SignalMessageBase<"pubkey", {
|
|
12
|
+
publicKey: string;
|
|
13
|
+
dAppInfo?: {
|
|
14
|
+
name: string;
|
|
15
|
+
url: string;
|
|
16
|
+
icon: string;
|
|
17
|
+
};
|
|
18
|
+
}>;
|
|
19
|
+
export type SignalMessageAck = SignalMessageBase<"ack", undefined>;
|
|
20
|
+
export type SignalMessageData = SignalMessageBase<"data", object>;
|
|
21
|
+
export type SignalMessage = SignalMessageFlash | SignalMessagePubkey | SignalMessageAck | SignalMessageData;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/messages/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { SignalNoConnectionError } from "@openlv/core/errors";
|
|
2
|
+
import MQTT from "mqtt";
|
|
3
|
+
import { createSignalingLayer } from "../index.js";
|
|
4
|
+
import { log } from "../utils/log.js";
|
|
5
|
+
/**
|
|
6
|
+
* MQTT Signaling Layer
|
|
7
|
+
*
|
|
8
|
+
* https://openlv.sh/api/signaling/mqtt
|
|
9
|
+
*/
|
|
10
|
+
export const mqtt = ({ url = "wss://test.mosquitto.org:8081/mqtt", topic, }) => {
|
|
11
|
+
let connection;
|
|
12
|
+
return createSignalingLayer({
|
|
13
|
+
type: "mqtt",
|
|
14
|
+
async setup() {
|
|
15
|
+
connection = await MQTT.connectAsync(url);
|
|
16
|
+
},
|
|
17
|
+
teardown() {
|
|
18
|
+
connection?.end();
|
|
19
|
+
connection = undefined;
|
|
20
|
+
},
|
|
21
|
+
async publish(payload) {
|
|
22
|
+
if (!connection) {
|
|
23
|
+
throw new Error("MQTT: No connection to publish to");
|
|
24
|
+
}
|
|
25
|
+
connection?.publish(topic, payload, { retain: false });
|
|
26
|
+
},
|
|
27
|
+
async subscribe(handler) {
|
|
28
|
+
if (!connection)
|
|
29
|
+
throw new SignalNoConnectionError();
|
|
30
|
+
log("MQTT: Subscribing to topic", topic);
|
|
31
|
+
connection.on("message", (receivedTopic, message) => {
|
|
32
|
+
log("MQTT: Received message on topic", topic);
|
|
33
|
+
if (receivedTopic !== topic)
|
|
34
|
+
return;
|
|
35
|
+
const decoded = new TextDecoder().decode(message);
|
|
36
|
+
handler(decoded);
|
|
37
|
+
});
|
|
38
|
+
return new Promise((resolve, reject) => {
|
|
39
|
+
connection?.subscribe(topic, (err) => {
|
|
40
|
+
if (err) {
|
|
41
|
+
log("MQTT: Error subscribing to topic", err);
|
|
42
|
+
reject(err);
|
|
43
|
+
}
|
|
44
|
+
resolve();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
Object.defineProperty(mqtt, "__name", { value: "mqtt" });
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/mqtt/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,OAAO,IAAI,MAAM,MAAM,CAAC;AAGxB,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAEtC;;;;GAIG;AACH,MAAM,CAAC,MAAM,IAAI,GAAwB,CAAC,EACxC,GAAG,GAAG,oCAAoC,EAC1C,KAAK,GACgB,EAAE,EAAE;IACzB,IAAI,UAAkC,CAAC;IAEvC,OAAO,oBAAoB,CAAC;QAC1B,IAAI,EAAE,MAAM;QACZ,KAAK,CAAC,KAAK;YACT,UAAU,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;QAC5C,CAAC;QACD,QAAQ;YACN,UAAU,EAAE,GAAG,EAAE,CAAC;YAClB,UAAU,GAAG,SAAS,CAAC;QACzB,CAAC;QACD,KAAK,CAAC,OAAO,CAAC,OAAO;YACnB,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,MAAM,IAAI,KAAK,C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
+
expires: number;
|
|
6
|
+
event: string;
|
|
7
|
+
topic: string;
|
|
8
|
+
message: string;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Ntfy Signaling Layer
|
|
12
|
+
*
|
|
13
|
+
* Uses HTTP Post for publishing
|
|
14
|
+
* Uses WebSocket for subscribing
|
|
15
|
+
*
|
|
16
|
+
* URL format supports the ?auth= parameter
|
|
17
|
+
* example: https://ntfy.sh/mytopic?auth=mytoken
|
|
18
|
+
*
|
|
19
|
+
* The same goes for apprise ntfy urls
|
|
20
|
+
* example: ntfy://{token}@{hostname}/{topic}
|
|
21
|
+
* example: ntfy://{user}:{password}@{hostname}/{topic}
|
|
22
|
+
* https variant: ntfys://{user}:{password}@{hostname}/{topic}
|
|
23
|
+
*
|
|
24
|
+
* https://openlv.sh/api/signaling/ntfy
|
|
25
|
+
*/
|
|
26
|
+
export declare const ntfy: CreateSignalLayerFn;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { EventEmitter } from "eventemitter3";
|
|
2
|
+
import { match } from "ts-pattern";
|
|
3
|
+
import { createSignalingLayer } from "../index.js";
|
|
4
|
+
import { log } from "../utils/log.js";
|
|
5
|
+
import { parseNtfyUrl } from "./url.js";
|
|
6
|
+
/**
|
|
7
|
+
* Ntfy Signaling Layer
|
|
8
|
+
*
|
|
9
|
+
* Uses HTTP Post for publishing
|
|
10
|
+
* Uses WebSocket for subscribing
|
|
11
|
+
*
|
|
12
|
+
* URL format supports the ?auth= parameter
|
|
13
|
+
* example: https://ntfy.sh/mytopic?auth=mytoken
|
|
14
|
+
*
|
|
15
|
+
* The same goes for apprise ntfy urls
|
|
16
|
+
* example: ntfy://{token}@{hostname}/{topic}
|
|
17
|
+
* example: ntfy://{user}:{password}@{hostname}/{topic}
|
|
18
|
+
* https variant: ntfys://{user}:{password}@{hostname}/{topic}
|
|
19
|
+
*
|
|
20
|
+
* https://openlv.sh/api/signaling/ntfy
|
|
21
|
+
*/
|
|
22
|
+
export const ntfy = ({ topic, url }) => {
|
|
23
|
+
let connection = null;
|
|
24
|
+
const connectionInfo = parseNtfyUrl(url);
|
|
25
|
+
const wsProtocol = match(connectionInfo.protocol)
|
|
26
|
+
.with("https", () => "wss")
|
|
27
|
+
.with("http", () => "ws")
|
|
28
|
+
.exhaustive();
|
|
29
|
+
const events = new EventEmitter();
|
|
30
|
+
return createSignalingLayer({
|
|
31
|
+
type: "ntfy",
|
|
32
|
+
async setup() {
|
|
33
|
+
log("NTFY: Setting up");
|
|
34
|
+
const wsUrl = wsProtocol +
|
|
35
|
+
"://" +
|
|
36
|
+
connectionInfo.host +
|
|
37
|
+
"/" +
|
|
38
|
+
topic +
|
|
39
|
+
"/ws" +
|
|
40
|
+
(connectionInfo.parameters || "");
|
|
41
|
+
log("NTFY: Connecting to WebSocket", wsUrl);
|
|
42
|
+
connection = new WebSocket(wsUrl);
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
44
|
+
connection.onerror = (_event) => { };
|
|
45
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
46
|
+
connection.onclose = (_event) => { };
|
|
47
|
+
const awaitOpenConfirm = new Promise((resolve) => {
|
|
48
|
+
connection.onmessage = (event) => {
|
|
49
|
+
log("NTFY: Received message:", event.data);
|
|
50
|
+
const data = JSON.parse(event.data);
|
|
51
|
+
if (data.event === "open") {
|
|
52
|
+
resolve();
|
|
53
|
+
}
|
|
54
|
+
else if (data.event === "message") {
|
|
55
|
+
events.emit("message", data.message);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
const awaitOpen = new Promise((resolve) => {
|
|
60
|
+
connection.onopen = () => {
|
|
61
|
+
log("NTFY: Connected to WebSocket");
|
|
62
|
+
resolve();
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
await awaitOpen;
|
|
66
|
+
await awaitOpenConfirm;
|
|
67
|
+
},
|
|
68
|
+
teardown() {
|
|
69
|
+
connection?.close();
|
|
70
|
+
},
|
|
71
|
+
async publish(body) {
|
|
72
|
+
const headers = { "Content-Type": "application/json" };
|
|
73
|
+
// TODO: Add response handling
|
|
74
|
+
// @ts-expect-error
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
76
|
+
const _response = await fetch(`${connectionInfo.protocol}://${connectionInfo.host}/${topic}`, { method: "POST", body, headers });
|
|
77
|
+
},
|
|
78
|
+
subscribe: (handler) => {
|
|
79
|
+
events.on("message", handler);
|
|
80
|
+
},
|
|
81
|
+
async [Symbol.asyncDispose]() {
|
|
82
|
+
connection?.close();
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
Object.defineProperty(ntfy, "__name", { value: "ntfy" });
|
|
87
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/ntfy/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAYxC;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,IAAI,GAAwB,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,EAAE;IAC1D,IAAI,UAAU,GAAqB,IAAI,CAAC;IACxC,MAAM,cAAc,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;IACzC,MAAM,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,QAAQ,CAAC;SAC9C,IAAI,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC;SAC1B,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,CAAC,IAAI,CAAC;S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EAAE,EAAE;gBAC9C,UAAW,CAAC,MAAM,GAAG,GAAG,EAAE;oBACxB,GAAG,CAAC,8BAA8B,CAAC,CAAC;oBACpC,OAAO,EAAE,CAAC;gBACZ,CAAC,CAAC;YACJ,CAAC,CAAC,CAAC;YAEH,MAAM,SAAS,CAAC;YAChB,MAAM,gBAAgB,CAAC;QACzB,CAAC;QACD,QAAQ;YACN,UAAU,EAAE,KAAK,EAAE,CAAC;QACtB,CAAC;QACD,KAAK,CAAC,OAAO,CAAC,IAAI;YAChB,MAAM,OAAO,GAAG,EAAE,cAAc,EAAE,kBAAkB,EAAiB,CAAC;YAEtE,8BAA8B;YAC9B,mBAAmB;YACnB,6DAA6D;YAC7D,MAAM,SAAS,GAAG,MAAM,KAAK,CAC3B,GAAG,cAAc,CAAC,QAAQ,MAAM,cAAc,CAAC,IAAI,IAAI,KAAK,EAAE,EAC9D,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,CAClC,CAAC;QACJ,CAAC;QACD,SAAS,EAAE,CAAC,OAAO,EAAE,EAAE;YACrB,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QAChC,CAAC;QACD,KAAK,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC;YACzB,UAAU,EAAE,KAAK,EAAE,CAAC;QACtB,CAAC;KACF,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type NtfyUrl = {
|
|
2
|
+
host: string;
|
|
3
|
+
protocol: "http" | "https";
|
|
4
|
+
credentials?: {
|
|
5
|
+
user: string;
|
|
6
|
+
password: string;
|
|
7
|
+
} | {
|
|
8
|
+
token: string;
|
|
9
|
+
};
|
|
10
|
+
parameters?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare const NTFY_URL_REGEX: RegExp;
|
|
13
|
+
export declare const parseNtfyUrl: (url: string) => NtfyUrl;
|
package/dist/ntfy/url.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export const NTFY_URL_REGEX = /^(?<protocol>https?|ntfys?):\/\/(?:(?:(?<user>[^:@]+):)?(?<password>[^@]+)?@)?(?<host>[^/?]+)\/?(?<parameters>.*)$/;
|
|
2
|
+
/*
|
|
3
|
+
* URL format supports the ?auth= parameter
|
|
4
|
+
* example: https://ntfy.sh/?auth=mytoken
|
|
5
|
+
* example: http://authtoken123@myserver.example.com/
|
|
6
|
+
*
|
|
7
|
+
* The same goes for apprise ntfy urls
|
|
8
|
+
* example: ntfy://{token}@{host}/
|
|
9
|
+
* example: ntfy://{user}:{password}@{host}/
|
|
10
|
+
* https variant: ntfys://{user}:{password}@{host}/
|
|
11
|
+
*/
|
|
12
|
+
export const parseNtfyUrl = (url) => {
|
|
13
|
+
const match = url.match(NTFY_URL_REGEX);
|
|
14
|
+
if (!match) {
|
|
15
|
+
throw new Error(`Invalid NTFY URL: ${url}`);
|
|
16
|
+
}
|
|
17
|
+
const { protocol: protocolType, user, password, host, parameters, } = match.groups || {};
|
|
18
|
+
// Determine protocol
|
|
19
|
+
let protocol;
|
|
20
|
+
if (["http", "ntfy"].includes(protocolType)) {
|
|
21
|
+
protocol = "http";
|
|
22
|
+
}
|
|
23
|
+
else if (["https", "ntfys"].includes(protocolType)) {
|
|
24
|
+
protocol = "https";
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error(`Invalid NTFY URL: ${url}`);
|
|
28
|
+
}
|
|
29
|
+
const credentials = user
|
|
30
|
+
? { user, password }
|
|
31
|
+
: password
|
|
32
|
+
? { token: password }
|
|
33
|
+
: undefined;
|
|
34
|
+
return {
|
|
35
|
+
host,
|
|
36
|
+
protocol,
|
|
37
|
+
credentials,
|
|
38
|
+
parameters: parameters ? parameters : undefined,
|
|
39
|
+
};
|
|
40
|
+
};
|
|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CAAC,CAAC,CAAC,SAAS;KAChD,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { parseNtfyUrl } from "./url.js";
|
|
3
|
+
const testCases = [
|
|
4
|
+
{
|
|
5
|
+
url: "https://ntfy.sh/?auth=mytoken1",
|
|
6
|
+
expected: {
|
|
7
|
+
host: "ntfy.sh",
|
|
8
|
+
protocol: "https",
|
|
9
|
+
parameters: "?auth=mytoken1",
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
url: "http://ntfy.sh/?auth=mytoken2",
|
|
14
|
+
expected: {
|
|
15
|
+
host: "ntfy.sh",
|
|
16
|
+
protocol: "http",
|
|
17
|
+
parameters: "?auth=mytoken2",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
url: "ntfy://mytoken@example.com",
|
|
22
|
+
expected: {
|
|
23
|
+
host: "example.com",
|
|
24
|
+
protocol: "http",
|
|
25
|
+
credentials: { token: "mytoken" },
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
url: "ntfys://myuser:mypassword@ntfy.sh/",
|
|
30
|
+
expected: {
|
|
31
|
+
host: "ntfy.sh",
|
|
32
|
+
protocol: "https",
|
|
33
|
+
credentials: { user: "myuser", password: "mypassword" },
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
url: "ntfy://mytoken@ntfy.sh/?auth=mytoken",
|
|
38
|
+
expected: {
|
|
39
|
+
host: "ntfy.sh",
|
|
40
|
+
protocol: "http",
|
|
41
|
+
credentials: { token: "mytoken" },
|
|
42
|
+
parameters: "?auth=mytoken",
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
];
|
|
46
|
+
describe("should parse NTFY URL", () => {
|
|
47
|
+
testCases.forEach((testCase) => {
|
|
48
|
+
it(`should parse NTFY URL ${testCase.url}`, () => {
|
|
49
|
+
const result = parseNtfyUrl(testCase.url);
|
|
50
|
+
expect(result).toEqual(testCase.expected);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
//# sourceMappingURL=url.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url.test.js","sourceRoot":"","sources":["../../src/ntfy/url.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAE9C,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,MAAM,SAAS,GAAG;IAChB;QACE,GAAG,EAAE,gCAAgC;QACrC,QAAQ,EAAE;YACR,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,OAAO;YACjB,UAAU,EAAE,gBAAgB;SAC7B;KACF;IACD;QACE,GAAG,EAAE,+BAA+B;QACpC,QAAQ,EAAE;YACR,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,MAAM;YAChB,UAAU,EAAE,gBAAgB;SAC7B;KACF;IACD;QACE,GAAG,EAAE,4BAA4B;QACjC,QAAQ,EAAE;YACR,IAAI,EAAE,aAAa;YACnB,QAAQ,EAAE,MAAM;YAChB,WAAW,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE;SAClC;KACF;IACD;QACE,GAAG,EAAE,oCAAoC;QACzC,QAAQ,EAAE;YACR,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,OAAO;YACjB,WAAW,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE;SACxD;KACF;IACD;QACE,GAAG,EAAE,sCAAsC;QAC3C,QAAQ,EAAE;YACR,IAAI,EAAE,SAAS;YACf,QAAQ,EAAE,MAAM;YAChB,WAAW,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE;YACjC,UAAU,EAAE,eAAe;SAC5B;KACF;CACF,CAAC;AAEF,QAAQ,CAAC,uBAAuB,EAAE,GAAG,EAAE;IACrC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;QAC7B,EAAE,CAAC,yBAAyB,QAAQ,CAAC,GAAG,EAAE,EAAE,GAAG,EAAE;YAC/C,MAAM,MAAM,GAAG,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;YAE1C,MAAM,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAC5C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const log: (...x: Parameters<typeof console.log>) => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"log.js","sourceRoot":"","sources":["../../src/utils/log.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,GAAG,CAAiC,EAAE,EAAE,CAC1D,OAAO,CAAC,GAAG,CACT,iBAAiB,EACjB,qCAAqC,EACrC,qCAAqC,EACrC,GAAG,CAAC,CACL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openlv/signaling",
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
|
+
"license": "LGPL-3.0-only",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"open-lavatory"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/v3xlabs/open-lavatory",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/v3xlabs/open-lavatory/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/v3xlabs/open-lavatory.git",
|
|
15
|
+
"directory": "packages/signaling"
|
|
16
|
+
},
|
|
17
|
+
"author": {
|
|
18
|
+
"name": "Open-Lavatory",
|
|
19
|
+
"url": "https://github.com/v3xlabs/open-lavatory"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./dist/index.js",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./ntfy": {
|
|
30
|
+
"types": "./dist/ntfy/index.d.ts",
|
|
31
|
+
"import": "./dist/ntfy/index.js",
|
|
32
|
+
"default": "./dist/ntfy/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./mqtt": {
|
|
35
|
+
"types": "./dist/mqtt/index.d.ts",
|
|
36
|
+
"import": "./dist/mqtt/index.js",
|
|
37
|
+
"default": "./dist/mqtt/index.js"
|
|
38
|
+
},
|
|
39
|
+
"./gundb": {
|
|
40
|
+
"types": "./dist/gundb/index.d.ts",
|
|
41
|
+
"import": "./dist/gundb/index.js",
|
|
42
|
+
"default": "./dist/gundb/index.js"
|
|
43
|
+
},
|
|
44
|
+
"./dynamic": {
|
|
45
|
+
"types": "./dist/dynamic.d.ts",
|
|
46
|
+
"import": "./dist/dynamic.js",
|
|
47
|
+
"default": "./dist/dynamic.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"types": "./dist/index.d.ts",
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"README.md"
|
|
54
|
+
],
|
|
55
|
+
"sideEffects": false,
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@openlv/core": "^0.0.1-beta.2",
|
|
58
|
+
"eventemitter3": "^5",
|
|
59
|
+
"globals": "^16",
|
|
60
|
+
"gun": "^0.2020.1241",
|
|
61
|
+
"mqtt": "^5",
|
|
62
|
+
"ts-pattern": "^5"
|
|
63
|
+
},
|
|
64
|
+
"devDependencies": {
|
|
65
|
+
"typescript": "^5",
|
|
66
|
+
"vitest": "^4"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"@wagmi/core": "2",
|
|
70
|
+
"viem": "2",
|
|
71
|
+
"wagmi": "2"
|
|
72
|
+
},
|
|
73
|
+
"publishConfig": {
|
|
74
|
+
"access": "public",
|
|
75
|
+
"tag": "beta"
|
|
76
|
+
},
|
|
77
|
+
"scripts": {
|
|
78
|
+
"build": "tsc",
|
|
79
|
+
"test": "vitest"
|
|
80
|
+
}
|
|
81
|
+
}
|