@openlv/session 0.0.1-beta.10

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 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
@@ -0,0 +1,3 @@
1
+ # @openlv/session
2
+
3
+ [Documentation](https://openlv.sh/api/session)
package/dist/base.d.ts ADDED
@@ -0,0 +1,48 @@
1
+ import { type SessionHandshakeParameters, type SessionLinkParameters } from "@openlv/core";
2
+ import { type CreateSignalLayerFn, type SignalingLayer, type SignalState } from "@openlv/signaling";
3
+ import { type TLayer, type TransportLayer } from "@openlv/transport";
4
+ import { EventEmitter } from "eventemitter3";
5
+ import type { SessionEvents } from "./events.js";
6
+ export declare const SESSION_STATE: {
7
+ readonly CREATED: "created";
8
+ readonly SIGNALING: "signaling";
9
+ readonly READY: "ready";
10
+ readonly LINKING: "linking";
11
+ readonly CONNECTED: "connected";
12
+ readonly DISCONNECTED: "disconnected";
13
+ };
14
+ export type SessionState = (typeof SESSION_STATE)[keyof typeof SESSION_STATE];
15
+ export type SessionStateObject = {
16
+ status: SessionState;
17
+ signaling?: {
18
+ state: SignalState;
19
+ };
20
+ };
21
+ /**
22
+ * an OpenLV Session
23
+ *
24
+ * https://openlv.sh/api/session
25
+ */
26
+ export type Session = {
27
+ getState(): SessionStateObject;
28
+ getHandshakeParameters(): SessionHandshakeParameters;
29
+ connect(): Promise<void>;
30
+ waitForLink(): Promise<void>;
31
+ close(): Promise<void>;
32
+ send(message: object, timeout?: number): Promise<unknown>;
33
+ emitter: EventEmitter<SessionEvents>;
34
+ _internal: {
35
+ signal: SignalingLayer;
36
+ transport: TransportLayer;
37
+ };
38
+ };
39
+ /**
40
+ * OpenLV Session
41
+ *
42
+ * https://openlv.sh/api/session
43
+ */
44
+ export declare const createSession: (initParameters: SessionLinkParameters, signalLayer: CreateSignalLayerFn, transportLayer: TLayer, onMessage: (message: object) => Promise<object | string>) => Promise<Session>;
45
+ /**
46
+ * Connect to a session from its openlv:// URL
47
+ */
48
+ export declare const connectSession: (connectionUrl: string, onMessage: (message: object) => Promise<object | string>, transport: TLayer) => Promise<Session>;
package/dist/base.js ADDED
@@ -0,0 +1,233 @@
1
+ import { decodeConnectionURL, OPENLV_PROTOCOL_VERSION, } from "@openlv/core";
2
+ import { deriveSymmetricKey, generateHandshakeKey, generateSessionId, initEncryptionKeys, initHash, parseEncryptionKey, } from "@openlv/core/encryption";
3
+ import { SIGNAL_STATE, } from "@openlv/signaling";
4
+ import { dynamicSignalingLayer } from "@openlv/signaling/dynamic";
5
+ import { TRANSPORT_STATE, } from "@openlv/transport";
6
+ import { EventEmitter } from "eventemitter3";
7
+ import { log } from "./utils/log.js";
8
+ export const SESSION_STATE = {
9
+ CREATED: "created",
10
+ SIGNALING: "signaling",
11
+ READY: "ready",
12
+ LINKING: "linking",
13
+ CONNECTED: "connected",
14
+ DISCONNECTED: "disconnected",
15
+ };
16
+ /**
17
+ * OpenLV Session
18
+ *
19
+ * https://openlv.sh/api/session
20
+ */
21
+ export const createSession = async (initParameters, signalLayer, transportLayer, onMessage) => {
22
+ const emitter = new EventEmitter();
23
+ const messages = new EventEmitter();
24
+ const sessionId = "sessionId" in initParameters
25
+ ? initParameters.sessionId
26
+ : generateSessionId();
27
+ const { encryptionKey, decryptionKey: { decrypt }, } = await initEncryptionKeys(initParameters);
28
+ let relyingPublicKey;
29
+ const handshakeKey = "k" in initParameters
30
+ ? await deriveSymmetricKey(initParameters.k)
31
+ : await generateHandshakeKey();
32
+ const { hash, isHost } = await initHash("h" in initParameters ? initParameters.h : undefined, encryptionKey);
33
+ let status = SESSION_STATE.CREATED;
34
+ const protocol = initParameters.p;
35
+ const server = initParameters.s;
36
+ const updateStatus = (newStatus) => {
37
+ log("updateStatus", newStatus);
38
+ status = newStatus;
39
+ emitter.emit("state_change", { status, signaling: signal.getState() });
40
+ };
41
+ const signaling = await signalLayer({
42
+ topic: sessionId,
43
+ url: server,
44
+ });
45
+ const signal = await signaling({
46
+ h: hash,
47
+ canEncrypt() {
48
+ return relyingPublicKey !== undefined;
49
+ },
50
+ async encrypt(message) {
51
+ if (!relyingPublicKey) {
52
+ throw new Error("Relying party public key not found");
53
+ }
54
+ log(`encrypting to ${relyingPublicKey.toString()}`, message);
55
+ return await relyingPublicKey.encrypt(message);
56
+ },
57
+ decrypt,
58
+ publicKey: encryptionKey,
59
+ k: handshakeKey,
60
+ async rpDiscovered(rpKey) {
61
+ const role = isHost ? "host" : "client";
62
+ log(`rpKey discovered by ${role}`, rpKey);
63
+ relyingPublicKey = await parseEncryptionKey(rpKey);
64
+ },
65
+ isHost,
66
+ });
67
+ const transport = transportLayer({
68
+ encrypt(message) {
69
+ if (!relyingPublicKey) {
70
+ throw new Error("Relying party public key not found");
71
+ }
72
+ return relyingPublicKey?.encrypt(message);
73
+ },
74
+ decrypt,
75
+ isHost,
76
+ onmessage: async (message) => {
77
+ console.log("Session: received message from transport", message);
78
+ // @ts-ignore
79
+ if (message["type"] === "request") {
80
+ // @ts-ignore
81
+ const data = await onMessage(message["payload"]);
82
+ const response = {
83
+ type: "response",
84
+ // @ts-ignore
85
+ messageId: message["messageId"],
86
+ payload: data,
87
+ };
88
+ await transport.send(response);
89
+ }
90
+ // @ts-ignore
91
+ if (message["type"] === "response") {
92
+ messages.emit("message", message);
93
+ }
94
+ },
95
+ async subsend(message) {
96
+ console.log("Session: sending trans msg to signal", message);
97
+ const sessionMessage = {
98
+ type: "request",
99
+ messageId: crypto.randomUUID(),
100
+ payload: message,
101
+ };
102
+ await signal.send(sessionMessage);
103
+ },
104
+ });
105
+ transport.emitter.on("state_change", (state) => {
106
+ log("transport state change", state);
107
+ if (state === TRANSPORT_STATE.CONNECTED) {
108
+ updateStatus(SESSION_STATE.CONNECTED);
109
+ }
110
+ });
111
+ const startTransport = async () => {
112
+ await transport.setup();
113
+ };
114
+ // let transport: TransportLayer | undefined;
115
+ const onSignalMessage = async (message) => {
116
+ log("Session: received message from signaling", message);
117
+ const sessionMsg = message;
118
+ if (sessionMsg.type === "response") {
119
+ log("Session: received data message", sessionMsg.payload);
120
+ messages.emit("message", sessionMsg);
121
+ }
122
+ if (sessionMsg.type === "request") {
123
+ log("Session: received request message", sessionMsg.payload);
124
+ transport.handle(sessionMsg.payload);
125
+ }
126
+ };
127
+ return {
128
+ connect: async () => {
129
+ updateStatus(SESSION_STATE.SIGNALING);
130
+ log("connecting to session, isHost:", isHost);
131
+ // TODO: implement
132
+ log("connecting to session");
133
+ signal.on("message", onSignalMessage);
134
+ signal.on("state_change", (state) => {
135
+ log("signal state change", state);
136
+ if (state === SIGNAL_STATE.READY) {
137
+ updateStatus(SESSION_STATE.READY);
138
+ }
139
+ if ([
140
+ SIGNAL_STATE.HANDSHAKE,
141
+ SIGNAL_STATE.HANDSHAKE_PARTIAL,
142
+ ].includes(state)) {
143
+ updateStatus(SESSION_STATE.LINKING);
144
+ }
145
+ if (state === SIGNAL_STATE.ENCRYPTED) {
146
+ // for now cuz we just use signaling call this a full connection
147
+ // updateStatus("connected");
148
+ startTransport();
149
+ }
150
+ });
151
+ // TODO: handle errors in setup nicely in modal UI
152
+ await signal.setup();
153
+ },
154
+ async close() {
155
+ log("session teardown");
156
+ await signal?.teardown();
157
+ updateStatus(SESSION_STATE.DISCONNECTED);
158
+ },
159
+ getState() {
160
+ return {
161
+ status,
162
+ signaling: signal.getState(),
163
+ // transport: transport.getState(),
164
+ };
165
+ },
166
+ getHandshakeParameters() {
167
+ return {
168
+ version: OPENLV_PROTOCOL_VERSION,
169
+ sessionId,
170
+ h: hash,
171
+ k: handshakeKey.toString(),
172
+ p: protocol,
173
+ s: server,
174
+ };
175
+ },
176
+ waitForLink: async () => {
177
+ return new Promise((resolve) => {
178
+ emitter.on("state_change", (state) => {
179
+ if (state?.status === SESSION_STATE.CONNECTED) {
180
+ resolve();
181
+ }
182
+ });
183
+ });
184
+ },
185
+ async send(message, timeout = 5000) {
186
+ const ready = signal.getState().state === SIGNAL_STATE.ENCRYPTED;
187
+ // const transportReady = transport.getState() === TRANSPORT_STATE.CONNECTED;
188
+ if (!ready) {
189
+ throw new Error("Session not ready");
190
+ }
191
+ const randomID = crypto.randomUUID();
192
+ const sessionMessage = {
193
+ type: "request",
194
+ messageId: randomID,
195
+ payload: message,
196
+ };
197
+ // for now use signaling
198
+ // await signal.send(sessionMessage);
199
+ await transport.send(sessionMessage);
200
+ return Promise.race([
201
+ new Promise((resolve) => {
202
+ messages.on("message", (message) => {
203
+ if (message.messageId === randomID && message.type === "response") {
204
+ resolve(message.payload);
205
+ }
206
+ });
207
+ }),
208
+ new Promise((resolve) => {
209
+ setTimeout(() => {
210
+ resolve(new Error("Timeout"));
211
+ }, timeout);
212
+ }),
213
+ ]);
214
+ },
215
+ _internal: {
216
+ signal,
217
+ transport,
218
+ },
219
+ emitter,
220
+ };
221
+ };
222
+ /**
223
+ * Connect to a session from its openlv:// URL
224
+ */
225
+ export const connectSession = async (connectionUrl, onMessage, transport) => {
226
+ const initParameters = decodeConnectionURL(connectionUrl);
227
+ const signaling = await dynamicSignalingLayer(initParameters.p);
228
+ if (!signaling) {
229
+ throw new Error(`Invalid signaling protocol: ${initParameters.p}`);
230
+ }
231
+ return createSession(initParameters, signaling, transport, onMessage);
232
+ };
233
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,uBAAuB,GAGxB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,kBAAkB,EAElB,oBAAoB,EACpB,iBAAiB,EACjB,kBAAkB,EAClB,QAAQ,EACR,kBAAkB,GACnB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,YAAY,GAGb,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAEL,eAAe,GAGhB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAI7C,OAAO,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAErC,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,OAAO,EAAE,SAAS;IAClB,SAAS,EAAE,WAAW;IACtB,KAAK,EAAE,OA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,CAAC,CAAC,CAAC,CAAC;IAEhE,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,+BAA+B,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC;IACrE,CAAC;IAED,OAAO,aAAa,CAAC,cAAc,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC;AACxE,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { SessionStateObject } from "./base.js";
2
+ export type SessionEvents = {
3
+ state_change: (state?: SessionStateObject) => void;
4
+ };
package/dist/events.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":""}
@@ -0,0 +1,8 @@
1
+ /** biome-ignore-all lint/performance/noBarrelFile: package entrypoint */
2
+ /**
3
+ * Open Lavatory Protocol - Decentralized Wallet Connection Library
4
+ *
5
+ * A privacy-first, peer-to-peer protocol for connecting dApps with wallets
6
+ * without relying on centralized infrastructure.
7
+ */
8
+ export * from "./base.js";
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ /** biome-ignore-all lint/performance/noBarrelFile: package entrypoint */
2
+ /**
3
+ * Open Lavatory Protocol - Decentralized Wallet Connection Library
4
+ *
5
+ * A privacy-first, peer-to-peer protocol for connecting dApps with wallets
6
+ * without relying on centralized infrastructure.
7
+ */
8
+ // biome-ignore lint/performance/noReExportAll: package entrypoint
9
+ export * from "./base.js";
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,yEAAyE;AAEzE;;;;;GAKG;AAEH,kEAAkE;AAClE,cAAc,WAAW,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { decodeConnectionURL, encodeConnectionURL } from "@openlv/core";
2
+ import { ntfy } from "@openlv/signaling/ntfy";
3
+ import { webrtc } from "@openlv/transport/webrtc";
4
+ import { describe, expect, test } from "vitest";
5
+ import { connectSession, createSession } from "./base.js";
6
+ describe("Session", () => {
7
+ test("Should be able to create a session", async () => {
8
+ const sessionA = await createSession({
9
+ sessionId: "mytestsession111",
10
+ p: "ntfy",
11
+ s: "https://ntfy.sh/",
12
+ }, ntfy, webrtc(), async (message) => {
13
+ console.log("sessionA received message", message);
14
+ return { result: "success" };
15
+ });
16
+ expect(sessionA).toBeDefined();
17
+ console.log(sessionA.getState());
18
+ await sessionA.connect();
19
+ const handshakeParametersA = sessionA.getHandshakeParameters();
20
+ const encodedUrl = encodeConnectionURL(handshakeParametersA);
21
+ console.log(encodedUrl);
22
+ const decodedUrl = decodeConnectionURL(encodedUrl);
23
+ expect(decodedUrl).toEqual(handshakeParametersA);
24
+ //
25
+ await new Promise((resolve) => setTimeout(resolve, 1000));
26
+ console.log("Connecting to session B");
27
+ const sessionB = await connectSession(encodedUrl, async (message) => {
28
+ console.log("sessionB received message", message);
29
+ return { result: "success" };
30
+ }, webrtc());
31
+ console.log(sessionB.getState());
32
+ await sessionB.connect();
33
+ await Promise.all([sessionA.waitForLink(), sessionB.waitForLink()]);
34
+ //
35
+ console.log("A", sessionA.getState());
36
+ console.log("B", sessionB.getState());
37
+ const response = await sessionA.send({ data: "test" });
38
+ console.log("response", response);
39
+ expect(response).toEqual({ result: "success" });
40
+ });
41
+ });
42
+ //# sourceMappingURL=index.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.test.js","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EA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,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE,OAAO,CAAC,CAAC;YAElD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QAC/B,CAAC,EACD,MAAM,EAAE,CACT,CAAC;QAEF,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;QACjC,MAAM,QAAQ,CAAC,OAAO,EAAE,CAAC;QAEzB,MAAM,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;QAEpE,EAAE;QACF,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;QACtC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;QAEtC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;QAEvD,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QAElC,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;IAClD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ export type SessionMessage = SessionMessageRequest | SessionMessageResponse;
2
+ export type SessionMessageRequest = {
3
+ type: "request";
4
+ messageId: string;
5
+ payload: object | string;
6
+ };
7
+ export type SessionMessageResponse = {
8
+ type: "response";
9
+ messageId: string;
10
+ payload: object | string;
11
+ };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/messages/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ export declare const log: (...x: Parameters<typeof console.log>) => void;
@@ -0,0 +1,5 @@
1
+ export const log = (...x) => console.log("[session]",
2
+ // "color: orange; font-weight: bold",
3
+ // "color: inherit; font-weight: normal",
4
+ ...x);
5
+ //# sourceMappingURL=log.js.map
@@ -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,WAAW;AACX,sCAAsC;AACtC,yCAAyC;AACzC,GAAG,CAAC,CACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@openlv/session",
3
+ "version": "0.0.1-beta.10",
4
+ "keywords": [
5
+ "open-lavatory"
6
+ ],
7
+ "homepage": "https://github.com/v3xlabs/open-lavatory",
8
+ "bugs": {
9
+ "url": "https://github.com/v3xlabs/open-lavatory/issues"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/v3xlabs/open-lavatory.git",
14
+ "directory": "packages/session"
15
+ },
16
+ "license": "LGPL-3.0-only",
17
+ "author": {
18
+ "name": "Open-Lavatory",
19
+ "url": "https://github.com/v3xlabs/open-lavatory"
20
+ },
21
+ "sideEffects": false,
22
+ "type": "module",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "import": "./dist/index.js",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "./provider": {
30
+ "types": "./dist/provider.d.ts",
31
+ "import": "./dist/provider.js",
32
+ "default": "./dist/provider.js"
33
+ }
34
+ },
35
+ "main": "./dist/index.js",
36
+ "types": "./dist/index.d.ts",
37
+ "files": [
38
+ "dist",
39
+ "README.md"
40
+ ],
41
+ "dependencies": {
42
+ "eventemitter3": "^5.0.1",
43
+ "globals": "^16.2.0",
44
+ "ts-pattern": "^5",
45
+ "@openlv/core": "0.0.1-beta.8",
46
+ "@openlv/signaling": "0.0.1-beta.7",
47
+ "@openlv/transport": "0.0.1-beta.7"
48
+ },
49
+ "devDependencies": {
50
+ "@vitest/browser-playwright": "^4.0.8",
51
+ "playwright": "1.58.1",
52
+ "typescript": "^5",
53
+ "vitest": "^4"
54
+ },
55
+ "peerDependencies": {
56
+ "@wagmi/core": "2",
57
+ "viem": "2",
58
+ "wagmi": "2"
59
+ },
60
+ "publishConfig": {
61
+ "access": "public",
62
+ "tag": "beta"
63
+ },
64
+ "scripts": {
65
+ "build": "tsc",
66
+ "test": "vitest"
67
+ }
68
+ }