myde-remote-connect 0.0.1 → 0.0.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.
@@ -1,140 +1,117 @@
1
1
  import Peer from "peerjs";
2
- class PeerjsAdapter {
3
- constructor(options, supportNativeEncryption = true) {
4
- this.options = options;
5
- this.supportNativeEncryption = supportNativeEncryption;
6
- this.peer = null;
7
- this.connection = null;
8
- this.messageHandler = null;
9
- this.closeHandler = null;
10
- this.errorHandler = null;
11
- }
12
- get trustIdentity() {
13
- return false;
14
- }
15
- async init(myId) {
16
- return new Promise((resolve, reject) => {
17
- this.peer = new Peer(myId, {
18
- debug: this.options?.debug ?? 0
19
- });
20
- this.peer.on("open", (id) => {
21
- console.log(`PeerJS initialized with ID: ${id}`);
22
- resolve();
23
- });
24
- this.peer.on("error", (err) => {
25
- console.error("PeerJS error:", err);
26
- if (this.errorHandler) {
27
- this.errorHandler(new Error(err.message));
28
- }
29
- reject(err);
30
- });
31
- this.peer.on("disconnected", () => {
32
- console.log("PeerJS disconnected");
33
- if (this.closeHandler) {
34
- this.closeHandler();
35
- }
36
- });
37
- this.peer.on("connection", (conn) => {
38
- if (this.connection) {
39
- console.warn("Already have an active connection, rejecting new connection from", conn.peer);
40
- conn.close();
41
- return;
42
- }
43
- this.setupConnection(conn);
44
- });
45
- });
46
- }
47
- async connect(id) {
48
- if (!this.peer) {
49
- throw new Error("PeerJS not initialized. Call init() first.");
50
- }
51
- if (this.connection && this.connection.peer === id) {
52
- return;
53
- }
54
- return new Promise((resolve, reject) => {
55
- const peer = this.peer;
56
- if (!peer) {
57
- reject(new Error("PeerJS not initialized"));
58
- return;
59
- }
60
- const conn = peer.connect(id, {
61
- reliable: true,
62
- serialization: "binary"
63
- });
64
- conn.on("open", () => {
65
- this.setupConnection(conn);
66
- resolve();
67
- });
68
- conn.on("error", (err) => {
69
- console.error("Connection error:", err);
70
- if (this.errorHandler) {
71
- this.errorHandler(new Error(String(err)));
72
- }
73
- reject(err);
74
- });
75
- });
76
- }
77
- async send(data) {
78
- const connection = this.connection;
79
- if (!connection) {
80
- throw new Error("No active connection. Call connect() first.");
81
- }
82
- return new Promise((resolve, reject) => {
83
- try {
84
- connection.send(data);
85
- resolve();
86
- } catch (err) {
87
- if (this.errorHandler) {
88
- this.errorHandler(err instanceof Error ? err : new Error(String(err)));
89
- }
90
- reject(err);
91
- }
92
- });
93
- }
94
- close() {
95
- if (this.connection) {
96
- this.connection.close();
97
- this.connection = null;
98
- }
99
- }
100
- onMessage(handler) {
101
- this.messageHandler = handler;
102
- }
103
- onClose(handler) {
104
- this.closeHandler = handler;
105
- }
106
- onError(handler) {
107
- this.errorHandler = handler;
108
- }
109
- setupConnection(conn) {
110
- this.connection = conn;
111
- conn.on("data", (data) => {
112
- if (this.messageHandler) {
113
- if (data instanceof Uint8Array) {
114
- this.messageHandler(data);
115
- } else if (data instanceof ArrayBuffer) {
116
- this.messageHandler(new Uint8Array(data));
117
- } else if (typeof data === "string") {
118
- this.messageHandler(new TextEncoder().encode(data));
119
- }
120
- }
121
- });
122
- conn.on("close", () => {
123
- console.log("Connection closed");
124
- this.connection = null;
125
- if (this.closeHandler) {
126
- this.closeHandler();
127
- }
128
- });
129
- conn.on("error", (err) => {
130
- console.error("Connection error:", err);
131
- if (this.errorHandler) {
132
- this.errorHandler(new Error(String(err)));
133
- }
134
- });
135
- }
136
- }
137
- export {
138
- PeerjsAdapter
2
+ //#region peerjs_adapter.ts
3
+ var PeerjsAdapter = class {
4
+ constructor(options, supportNativeEncryption = true) {
5
+ this.options = options;
6
+ this.supportNativeEncryption = supportNativeEncryption;
7
+ this.peer = null;
8
+ this.connection = null;
9
+ this.messageHandler = null;
10
+ this.closeHandler = null;
11
+ this.errorHandler = null;
12
+ }
13
+ get trustIdentity() {
14
+ return false;
15
+ }
16
+ async init(myId) {
17
+ return new Promise((resolve, reject) => {
18
+ this.peer = new Peer(myId, { debug: this.options?.debug ?? 0 });
19
+ this.peer.on("open", (id) => {
20
+ console.log(`PeerJS initialized with ID: ${id}`);
21
+ resolve();
22
+ });
23
+ this.peer.on("error", (err) => {
24
+ console.error("PeerJS error:", err);
25
+ if (this.errorHandler) this.errorHandler(new Error(err.message));
26
+ reject(err);
27
+ });
28
+ this.peer.on("disconnected", () => {
29
+ console.log("PeerJS disconnected");
30
+ if (this.closeHandler) this.closeHandler();
31
+ });
32
+ this.peer.on("connection", (conn) => {
33
+ if (this.connection) {
34
+ console.warn("Already have an active connection, rejecting new connection from", conn.peer);
35
+ conn.close();
36
+ return;
37
+ }
38
+ this.setupConnection(conn);
39
+ });
40
+ });
41
+ }
42
+ async connect(id) {
43
+ if (!this.peer) throw new Error("PeerJS not initialized. Call init() first.");
44
+ if (this.connection && this.connection.peer === id) return;
45
+ return new Promise((resolve, reject) => {
46
+ const peer = this.peer;
47
+ if (!peer) {
48
+ reject(/* @__PURE__ */ new Error("PeerJS not initialized"));
49
+ return;
50
+ }
51
+ const conn = peer.connect(id, {
52
+ reliable: true,
53
+ serialization: "binary"
54
+ });
55
+ conn.on("open", () => {
56
+ this.setupConnection(conn);
57
+ resolve();
58
+ });
59
+ conn.on("error", (err) => {
60
+ console.error("Connection error:", err);
61
+ if (this.errorHandler) this.errorHandler(new Error(String(err)));
62
+ reject(err);
63
+ });
64
+ });
65
+ }
66
+ async send(data) {
67
+ const connection = this.connection;
68
+ if (!connection) throw new Error("No active connection. Call connect() first.");
69
+ return new Promise((resolve, reject) => {
70
+ try {
71
+ connection.send(data);
72
+ resolve();
73
+ } catch (err) {
74
+ if (this.errorHandler) this.errorHandler(err instanceof Error ? err : new Error(String(err)));
75
+ reject(err);
76
+ }
77
+ });
78
+ }
79
+ close() {
80
+ if (this.connection) {
81
+ this.connection.close();
82
+ this.connection = null;
83
+ }
84
+ }
85
+ onMessage(handler) {
86
+ this.messageHandler = handler;
87
+ }
88
+ onClose(handler) {
89
+ this.closeHandler = handler;
90
+ }
91
+ onError(handler) {
92
+ this.errorHandler = handler;
93
+ }
94
+ setupConnection(conn) {
95
+ this.connection = conn;
96
+ conn.on("data", (data) => {
97
+ if (this.messageHandler) {
98
+ if (data instanceof Uint8Array) this.messageHandler(data);
99
+ else if (data instanceof ArrayBuffer) this.messageHandler(new Uint8Array(data));
100
+ else if (typeof data === "string") this.messageHandler(new TextEncoder().encode(data));
101
+ }
102
+ });
103
+ conn.on("close", () => {
104
+ console.log("Connection closed");
105
+ this.connection = null;
106
+ if (this.closeHandler) this.closeHandler();
107
+ });
108
+ conn.on("error", (err) => {
109
+ console.error("Connection error:", err);
110
+ if (this.errorHandler) this.errorHandler(new Error(String(err)));
111
+ });
112
+ }
139
113
  };
140
- //# sourceMappingURL=peerjs_adapter.js.map
114
+ //#endregion
115
+ export { PeerjsAdapter };
116
+
117
+ //# sourceMappingURL=peerjs_adapter.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"peerjs_adapter.js","sources":["../peerjs_adapter.ts"],"sourcesContent":["import type { UntrustedSignalingAdapter } from \"./sconnect_type\";\nimport Peer, { type DataConnection } from \"peerjs\";\n\nexport class PeerjsAdapter implements UntrustedSignalingAdapter {\n private peer: Peer | null = null;\n private connection: DataConnection | null = null;\n private messageHandler: ((data: Uint8Array) => void) | null = null;\n private closeHandler: (() => void) | null = null;\n private errorHandler: ((err: Error) => void) | null = null;\n\n constructor(\n private options?: { debug?: number },\n public supportNativeEncryption = true,\n ) {}\n\n get trustIdentity(): false {\n return false;\n }\n\n async init(myId: string): Promise<void> {\n return new Promise((resolve, reject) => {\n this.peer = new Peer(myId, {\n debug: this.options?.debug ?? 0,\n });\n\n this.peer.on(\"open\", (id) => {\n console.log(`PeerJS initialized with ID: ${id}`);\n resolve();\n });\n\n this.peer.on(\"error\", (err) => {\n console.error(\"PeerJS error:\", err);\n if (this.errorHandler) {\n this.errorHandler(new Error(err.message));\n }\n reject(err);\n });\n\n this.peer.on(\"disconnected\", () => {\n console.log(\"PeerJS disconnected\");\n if (this.closeHandler) {\n this.closeHandler();\n }\n });\n\n this.peer.on(\"connection\", (conn) => {\n if (this.connection) {\n console.warn(\"Already have an active connection, rejecting new connection from\", conn.peer);\n conn.close();\n return;\n }\n this.setupConnection(conn);\n });\n });\n }\n\n async connect(id: string): Promise<void> {\n if (!this.peer) {\n throw new Error(\"PeerJS not initialized. Call init() first.\");\n }\n\n if (this.connection && this.connection.peer === id) {\n return;\n }\n\n return new Promise((resolve, reject) => {\n const peer = this.peer;\n if (!peer) {\n reject(new Error(\"PeerJS not initialized\"));\n return;\n }\n\n const conn = peer.connect(id, {\n reliable: true,\n serialization: \"binary\",\n });\n\n conn.on(\"open\", () => {\n this.setupConnection(conn);\n resolve();\n });\n\n conn.on(\"error\", (err) => {\n console.error(\"Connection error:\", err);\n if (this.errorHandler) {\n this.errorHandler(new Error(String(err)));\n }\n reject(err);\n });\n });\n }\n\n async send(data: Uint8Array): Promise<void> {\n const connection = this.connection;\n if (!connection) {\n throw new Error(\"No active connection. Call connect() first.\");\n }\n\n return new Promise((resolve, reject) => {\n try {\n connection.send(data);\n resolve();\n } catch (err) {\n if (this.errorHandler) {\n this.errorHandler(err instanceof Error ? err : new Error(String(err)));\n }\n reject(err);\n }\n });\n }\n\n close(): void {\n if (this.connection) {\n this.connection.close();\n this.connection = null;\n }\n }\n\n onMessage(handler: (data: Uint8Array) => void): void {\n this.messageHandler = handler;\n }\n\n onClose(handler: () => void): void {\n this.closeHandler = handler;\n }\n\n onError(handler: (err: Error) => void): void {\n this.errorHandler = handler;\n }\n\n private setupConnection(conn: DataConnection): void {\n this.connection = conn;\n\n conn.on(\"data\", (data) => {\n if (this.messageHandler) {\n if (data instanceof Uint8Array) {\n this.messageHandler(data);\n } else if (data instanceof ArrayBuffer) {\n this.messageHandler(new Uint8Array(data));\n } else if (typeof data === \"string\") {\n this.messageHandler(new TextEncoder().encode(data));\n }\n }\n });\n\n conn.on(\"close\", () => {\n console.log(\"Connection closed\");\n this.connection = null;\n if (this.closeHandler) {\n this.closeHandler();\n }\n });\n\n conn.on(\"error\", (err) => {\n console.error(\"Connection error:\", err);\n if (this.errorHandler) {\n this.errorHandler(new Error(String(err)));\n }\n });\n }\n}\n"],"names":[],"mappings":";AAGO,MAAM,cAAmD;AAAA,EAO5D,YACY,SACD,0BAA0B,MACnC;AAFU,SAAA,UAAA;AACD,SAAA,0BAAA;AARX,SAAQ,OAAoB;AAC5B,SAAQ,aAAoC;AAC5C,SAAQ,iBAAsD;AAC9D,SAAQ,eAAoC;AAC5C,SAAQ,eAA8C;AAAA,EAKnD;AAAA,EAEH,IAAI,gBAAuB;AACvB,WAAO;AAAA,EACX;AAAA,EAEA,MAAM,KAAK,MAA6B;AACpC,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACpC,WAAK,OAAO,IAAI,KAAK,MAAM;AAAA,QACvB,OAAO,KAAK,SAAS,SAAS;AAAA,MAAA,CACjC;AAED,WAAK,KAAK,GAAG,QAAQ,CAAC,OAAO;AACzB,gBAAQ,IAAI,+BAA+B,EAAE,EAAE;AAC/C,gBAAA;AAAA,MACJ,CAAC;AAED,WAAK,KAAK,GAAG,SAAS,CAAC,QAAQ;AAC3B,gBAAQ,MAAM,iBAAiB,GAAG;AAClC,YAAI,KAAK,cAAc;AACnB,eAAK,aAAa,IAAI,MAAM,IAAI,OAAO,CAAC;AAAA,QAC5C;AACA,eAAO,GAAG;AAAA,MACd,CAAC;AAED,WAAK,KAAK,GAAG,gBAAgB,MAAM;AAC/B,gBAAQ,IAAI,qBAAqB;AACjC,YAAI,KAAK,cAAc;AACnB,eAAK,aAAA;AAAA,QACT;AAAA,MACJ,CAAC;AAED,WAAK,KAAK,GAAG,cAAc,CAAC,SAAS;AACjC,YAAI,KAAK,YAAY;AACjB,kBAAQ,KAAK,oEAAoE,KAAK,IAAI;AAC1F,eAAK,MAAA;AACL;AAAA,QACJ;AACA,aAAK,gBAAgB,IAAI;AAAA,MAC7B,CAAC;AAAA,IACL,CAAC;AAAA,EACL;AAAA,EAEA,MAAM,QAAQ,IAA2B;AACrC,QA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iBAAiB;AAAA,EAC1B;AAAA,EAEA,QAAQ,SAA2B;AAC/B,SAAK,eAAe;AAAA,EACxB;AAAA,EAEA,QAAQ,SAAqC;AACzC,SAAK,eAAe;AAAA,EACxB;AAAA,EAEQ,gBAAgB,MAA4B;AAChD,SAAK,aAAa;AAElB,SAAK,GAAG,QAAQ,CAAC,SAAS;AACtB,UAAI,KAAK,gBAAgB;AACrB,YAAI,gBAAgB,YAAY;AAC5B,eAAK,eAAe,IAAI;AAAA,QAC5B,WAAW,gBAAgB,aAAa;AACpC,eAAK,eAAe,IAAI,WAAW,IAAI,CAAC;AAAA,QAC5C,WAAW,OAAO,SAAS,UAAU;AACjC,eAAK,eAAe,IAAI,YAAA,EAAc,OAAO,IAAI,CAAC;AAAA,QACtD;AAAA,MACJ;AAAA,IACJ,CAAC;AAED,SAAK,GAAG,SAAS,MAAM;AACnB,cAAQ,IAAI,mBAAmB;AAC/B,WAAK,aAAa;AAClB,UAAI,KAAK,cAAc;AACnB,aAAK,aAAA;AAAA,MACT;AAAA,IACJ,CAAC;AAED,SAAK,GAAG,SAAS,CAAC,QAAQ;AACtB,cAAQ,MAAM,qBAAqB,GAAG;AACtC,UAAI,KAAK,cAAc;AACnB,aAAK,aAAa,IAAI,MAAM,OAAO,GAAG,CAAC,CAAC;AAAA,MAC5C;AAAA,IACJ,CAAC;AAAA,EACL;AACJ;"}
1
+ {"version":3,"file":"peerjs_adapter.js","names":[],"sources":["../peerjs_adapter.ts"],"sourcesContent":["import type { UntrustedSignalingAdapter } from \"./sconnect_type\";\nimport Peer, { type DataConnection } from \"peerjs\";\n\nexport class PeerjsAdapter implements UntrustedSignalingAdapter {\n\tprivate peer: Peer | null = null;\n\tprivate connection: DataConnection | null = null;\n\tprivate messageHandler: ((data: Uint8Array) => void) | null = null;\n\tprivate closeHandler: (() => void) | null = null;\n\tprivate errorHandler: ((err: Error) => void) | null = null;\n\n\tconstructor(\n\t\tprivate options?: { debug?: number },\n\t\tpublic supportNativeEncryption = true,\n\t) {}\n\n\tget trustIdentity(): false {\n\t\treturn false;\n\t}\n\n\tasync init(myId: string): Promise<void> {\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tthis.peer = new Peer(myId, {\n\t\t\t\tdebug: this.options?.debug ?? 0,\n\t\t\t});\n\n\t\t\tthis.peer.on(\"open\", (id) => {\n\t\t\t\tconsole.log(`PeerJS initialized with ID: ${id}`);\n\t\t\t\tresolve();\n\t\t\t});\n\n\t\t\tthis.peer.on(\"error\", (err) => {\n\t\t\t\tconsole.error(\"PeerJS error:\", err);\n\t\t\t\tif (this.errorHandler) {\n\t\t\t\t\tthis.errorHandler(new Error(err.message));\n\t\t\t\t}\n\t\t\t\treject(err);\n\t\t\t});\n\n\t\t\tthis.peer.on(\"disconnected\", () => {\n\t\t\t\tconsole.log(\"PeerJS disconnected\");\n\t\t\t\tif (this.closeHandler) {\n\t\t\t\t\tthis.closeHandler();\n\t\t\t\t}\n\t\t\t});\n\n\t\t\tthis.peer.on(\"connection\", (conn) => {\n\t\t\t\tif (this.connection) {\n\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\"Already have an active connection, rejecting new connection from\",\n\t\t\t\t\t\tconn.peer,\n\t\t\t\t\t);\n\t\t\t\t\tconn.close();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tthis.setupConnection(conn);\n\t\t\t});\n\t\t});\n\t}\n\n\tasync connect(id: string): Promise<void> {\n\t\tif (!this.peer) {\n\t\t\tthrow new Error(\"PeerJS not initialized. Call init() first.\");\n\t\t}\n\n\t\tif (this.connection && this.connection.peer === id) {\n\t\t\treturn;\n\t\t}\n\n\t\treturn new Promise((resolve, reject) => {\n\t\t\tconst peer = this.peer;\n\t\t\tif (!peer) {\n\t\t\t\treject(new Error(\"PeerJS not initialized\"));\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tconst conn = peer.connect(id, {\n\t\t\t\treliable: true,\n\t\t\t\tserialization: \"binary\",\n\t\t\t});\n\n\t\t\tconn.on(\"open\", () => {\n\t\t\t\tthis.setupConnection(conn);\n\t\t\t\tresolve();\n\t\t\t});\n\n\t\t\tconn.on(\"error\", (err) => {\n\t\t\t\tconsole.error(\"Connection error:\", err);\n\t\t\t\tif (this.errorHandler) {\n\t\t\t\t\tthis.errorHandler(new Error(String(err)));\n\t\t\t\t}\n\t\t\t\treject(err);\n\t\t\t});\n\t\t});\n\t}\n\n\tasync send(data: Uint8Array): Promise<void> {\n\t\tconst connection = this.connection;\n\t\tif (!connection) {\n\t\t\tthrow new Error(\"No active connection. Call connect() first.\");\n\t\t}\n\n\t\treturn new Promise((resolve, reject) => {\n\t\t\ttry {\n\t\t\t\tconnection.send(data);\n\t\t\t\tresolve();\n\t\t\t} catch (err) {\n\t\t\t\tif (this.errorHandler) {\n\t\t\t\t\tthis.errorHandler(\n\t\t\t\t\t\terr instanceof Error ? err : new Error(String(err)),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treject(err);\n\t\t\t}\n\t\t});\n\t}\n\n\tclose(): void {\n\t\tif (this.connection) {\n\t\t\tthis.connection.close();\n\t\t\tthis.connection = null;\n\t\t}\n\t}\n\n\tonMessage(handler: (data: Uint8Array) => void): void {\n\t\tthis.messageHandler = handler;\n\t}\n\n\tonClose(handler: () => void): void {\n\t\tthis.closeHandler = handler;\n\t}\n\n\tonError(handler: (err: Error) => void): void {\n\t\tthis.errorHandler = handler;\n\t}\n\n\tprivate setupConnection(conn: DataConnection): void {\n\t\tthis.connection = conn;\n\n\t\tconn.on(\"data\", (data) => {\n\t\t\tif (this.messageHandler) {\n\t\t\t\tif (data instanceof Uint8Array) {\n\t\t\t\t\tthis.messageHandler(data);\n\t\t\t\t} else if (data instanceof ArrayBuffer) {\n\t\t\t\t\tthis.messageHandler(new Uint8Array(data));\n\t\t\t\t} else if (typeof data === \"string\") {\n\t\t\t\t\tthis.messageHandler(new TextEncoder().encode(data));\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tconn.on(\"close\", () => {\n\t\t\tconsole.log(\"Connection closed\");\n\t\t\tthis.connection = null;\n\t\t\tif (this.closeHandler) {\n\t\t\t\tthis.closeHandler();\n\t\t\t}\n\t\t});\n\n\t\tconn.on(\"error\", (err) => {\n\t\t\tconsole.error(\"Connection error:\", err);\n\t\t\tif (this.errorHandler) {\n\t\t\t\tthis.errorHandler(new Error(String(err)));\n\t\t\t}\n\t\t});\n\t}\n}\n"],"mappings":";;AAGA,IAAa,gBAAb,MAAgE;CAO/D,YACC,SACA,0BAAiC,MAChC;EAFO,KAAA,UAAA;EACD,KAAA,0BAAA;cARoB;oBACgB;wBACkB;sBAClB;sBACU;CAKnD;CAEH,IAAI,gBAAuB;EAC1B,OAAO;CACR;CAEA,MAAM,KAAK,MAA6B;EACvC,OAAO,IAAI,SAAS,SAAS,WAAW;GACvC,KAAK,OAAO,IAAI,KAAK,MAAM,EAC1B,OAAO,KAAK,SAAS,SAAS,EAC/B,CAAC;GAED,KAAK,KAAK,GAAG,SAAS,OAAO;IAC5B,QAAQ,IAAI,+BAA+B,IAAI;IAC/C,QAAQ;GACT,CAAC;GAED,KAAK,KAAK,GAAG,UAAU,QAAQ;IAC9B,QAAQ,MAAM,iBAAiB,GAAG;IAClC,IAAI,KAAK,cACR,KAAK,aAAa,IAAI,MAAM,IAAI,OAAO,CAAC;IAEzC,OAAO,GAAG;GACX,CAAC;GAED,KAAK,KAAK,GAAG,sBAAsB;IAClC,QAAQ,IAAI,qBAAqB;IACjC,IAAI,KAAK,cACR,KAAK,aAAa;GAEpB,CAAC;GAED,KAAK,KAAK,GAAG,eAAe,SAAS;IACpC,IAAI,KAAK,YAAY;KACpB,QAAQ,KACP,oEACA,KAAK,IACN;KACA,KAAK,MAAM;KACX;IACD;IACA,KAAK,gBAAgB,IAAI;GAC1B,CAAC;EACF,CAAC;CACF;CAEA,MAAM,QAAQ,IAA2B;EACxC,IAAI,CAAC,KAAK,MACT,MAAM,IAAI,MAAM,4CAA4C;EAG7D,IAAI,KAAK,cAAc,KA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C,IAAI,OAAO,SAAS,UAC1B,KAAK,eAAe,IAAI,YAAY,CAAC,CAAC,OAAO,IAAI,CAAC;GAAA;EAGrD,CAAC;EAED,KAAK,GAAG,eAAe;GACtB,QAAQ,IAAI,mBAAmB;GAC/B,KAAK,aAAa;GAClB,IAAI,KAAK,cACR,KAAK,aAAa;EAEpB,CAAC;EAED,KAAK,GAAG,UAAU,QAAQ;GACzB,QAAQ,MAAM,qBAAqB,GAAG;GACtC,IAAI,KAAK,cACR,KAAK,aAAa,IAAI,MAAM,OAAO,GAAG,CAAC,CAAC;EAE1C,CAAC;CACF;AACD"}
@@ -1,19 +1,20 @@
1
1
  import type { ChannelOptions, ConnectResult, Credential, CredentialPrivateInfo, CredentialPublicInfo, SecureChannel, SecureChannelEvents, SignalingAdapter } from "./sconnect_type";
2
+ interface KeyPair {
3
+ publicKey: Uint8Array;
4
+ privateKey: Uint8Array;
5
+ }
2
6
  export declare class SConnect implements SecureChannel {
3
7
  private signalAdapter;
4
8
  private options;
5
9
  private remoteId;
6
- private noise;
7
- private sendCipher;
8
- private receiveCipher;
10
+ private cipher;
9
11
  private PIN;
10
12
  private eventHandlers;
11
13
  private state;
12
14
  private handshakeType;
13
- private handshakeMessageHandler;
14
- private handshakeMessageQueue;
15
+ private typeMessageQueue;
16
+ private typeMessageResolvers;
15
17
  private myDeviceId;
16
- private myKeyPair;
17
18
  private activeTimers;
18
19
  private pairLimiter;
19
20
  private connectLimiter;
@@ -21,12 +22,16 @@ export declare class SConnect implements SecureChannel {
21
22
  constructor(signalAdapter: SignalingAdapter, options?: ChannelOptions);
22
23
  init(myDeviceId: string, remoteId?: string): Promise<void>;
23
24
  tryConnect(credential?: CredentialPrivateInfo): Promise<ConnectResult>;
25
+ private connectWithCredential;
26
+ private handleConnectRequest;
24
27
  updatePIN(): string;
25
28
  pairInit(credential: CredentialPublicInfo): Promise<{
26
29
  pin: string;
27
30
  inputOtherPin: (pin: string) => void;
28
31
  waitForPairing: () => Promise<Credential>;
29
32
  }>;
33
+ private genWaitForPairing;
34
+ private handlePairRequest;
30
35
  disconnect(): void;
31
36
  destroy(): void;
32
37
  send(payload: string): Promise<void>;
@@ -34,39 +39,36 @@ export declare class SConnect implements SecureChannel {
34
39
  private sendData;
35
40
  on<K extends keyof SecureChannelEvents>(event: K, callback: SecureChannelEvents[K]): void;
36
41
  off(event: string, callback: (...args: unknown[]) => void): void;
37
- rotateCredential(): Promise<void>;
38
42
  private setState;
39
- private createTimer;
40
43
  private clearAllTimers;
41
44
  private handleRawMessage;
42
- private isHandshakeMessage;
43
- private handleHandshakeMessage;
44
- private handleProtocolMessage;
45
- private handleUseYourPin;
46
- private handlePairReject;
47
45
  private handleAppData;
48
- private sendErrorToPeer;
49
46
  private handleErrorFromPeer;
50
- private onUseYourPin;
51
- private onPairReject;
52
- private handlePairRequest;
53
- private sendPairRequest;
54
- private handleConnectRequest;
55
- private sendConnectRequest;
56
- private setupPAKEResponder;
57
- private performPAKEClient;
58
- private performIKInitiator;
59
- private performIKResponder;
60
- private waitForNoiseResponse;
61
- private sendHandshakeMessage;
62
- private setHandshakeMessageHandler;
63
- private restoreMessageHandler;
64
- private initializeEncryption;
47
+ private sendTypeMessage;
48
+ private getTypeMessage;
65
49
  private buildCredential;
66
50
  private handleDisconnect;
67
51
  private emit;
68
52
  private generatePin;
69
53
  private validatePin;
70
54
  private wrapError;
71
- private generateKeyPair;
72
55
  }
56
+ export declare function generateKeyPair(): Promise<KeyPair>;
57
+ export declare function generateSigningKeyPair(): Promise<KeyPair>;
58
+ export declare class cipher {
59
+ private sendKey;
60
+ private receiveKey;
61
+ private sendCryptoKey;
62
+ private receiveCryptoKey;
63
+ constructor(sendKey: Uint8Array, receiveKey: Uint8Array);
64
+ private getSendKey;
65
+ private getReceiveKey;
66
+ encrypt(data: Uint8Array): Promise<Uint8Array>;
67
+ decrypt(data: Uint8Array): Promise<Uint8Array>;
68
+ }
69
+ export declare function blind(pin: string, publicKey: Uint8Array): Uint8Array;
70
+ export declare function deBlind(blindedKey: Uint8Array, pin: string): Uint8Array;
71
+ export declare function dh(privateKey: Uint8Array, publicKey: Uint8Array): Promise<Uint8Array>;
72
+ export declare function sigh(privateKey: Uint8Array, data: Uint8Array): Promise<Uint8Array>;
73
+ export declare function verifySignature(publicKey: Uint8Array, data: Uint8Array, signature: Uint8Array): Promise<boolean>;
74
+ export {};