@edryslabs/genericprovider 1.0.1
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 +24 -0
- package/README.md +660 -0
- package/dist/index.d.ts +323 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1001 -0
- package/dist/index.js.map +1 -0
- package/dist/lib.d.ts +36 -0
- package/dist/lib.d.ts.map +1 -0
- package/dist/lib.js +37 -0
- package/dist/lib.js.map +1 -0
- package/dist/providers/dummy/index.d.ts +226 -0
- package/dist/providers/dummy/index.d.ts.map +1 -0
- package/dist/providers/dummy/index.js +326 -0
- package/dist/providers/dummy/index.js.map +1 -0
- package/dist/providers/gun/index.d.ts +269 -0
- package/dist/providers/gun/index.d.ts.map +1 -0
- package/dist/providers/gun/index.js +683 -0
- package/dist/providers/gun/index.js.map +1 -0
- package/dist/providers/indexeddb/index.d.ts +161 -0
- package/dist/providers/indexeddb/index.d.ts.map +1 -0
- package/dist/providers/indexeddb/index.js +369 -0
- package/dist/providers/indexeddb/index.js.map +1 -0
- package/dist/providers/matrix/index.d.ts +109 -0
- package/dist/providers/matrix/index.d.ts.map +1 -0
- package/dist/providers/matrix/index.js +329 -0
- package/dist/providers/matrix/index.js.map +1 -0
- package/dist/providers/nostr/index.d.ts +172 -0
- package/dist/providers/nostr/index.d.ts.map +1 -0
- package/dist/providers/nostr/index.js +280 -0
- package/dist/providers/nostr/index.js.map +1 -0
- package/dist/providers/peerjs/index.d.ts +231 -0
- package/dist/providers/peerjs/index.d.ts.map +1 -0
- package/dist/providers/peerjs/index.js +1038 -0
- package/dist/providers/peerjs/index.js.map +1 -0
- package/dist/providers/pubnub/index.d.ts +106 -0
- package/dist/providers/pubnub/index.d.ts.map +1 -0
- package/dist/providers/pubnub/index.js +357 -0
- package/dist/providers/pubnub/index.js.map +1 -0
- package/dist/providers/simple-peer/index.d.ts +253 -0
- package/dist/providers/simple-peer/index.d.ts.map +1 -0
- package/dist/providers/simple-peer/index.js +783 -0
- package/dist/providers/simple-peer/index.js.map +1 -0
- package/dist/providers/supabase/index.d.ts +80 -0
- package/dist/providers/supabase/index.d.ts.map +1 -0
- package/dist/providers/supabase/index.js +202 -0
- package/dist/providers/supabase/index.js.map +1 -0
- package/dist/providers/trystero/index.d.ts +181 -0
- package/dist/providers/trystero/index.d.ts.map +1 -0
- package/dist/providers/trystero/index.js +187 -0
- package/dist/providers/trystero/index.js.map +1 -0
- package/dist/providers/websocket/index.d.ts +92 -0
- package/dist/providers/websocket/index.d.ts.map +1 -0
- package/dist/providers/websocket/index.js +272 -0
- package/dist/providers/websocket/index.js.map +1 -0
- package/dist/sync-monitor.d.ts +90 -0
- package/dist/sync-monitor.d.ts.map +1 -0
- package/dist/sync-monitor.js +149 -0
- package/dist/sync-monitor.js.map +1 -0
- package/dist/transport.d.ts +116 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +2 -0
- package/dist/transport.js.map +1 -0
- package/package.json +137 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Transport, ConnectionConfig } from '../../transport';
|
|
2
|
+
/**
|
|
3
|
+
* Matrix transport configuration
|
|
4
|
+
*/
|
|
5
|
+
export interface MatrixConfig extends ConnectionConfig {
|
|
6
|
+
/** Matrix homeserver URL (required) e.g., 'https://matrix.org' */
|
|
7
|
+
homeserverUrl: string;
|
|
8
|
+
/** Access token (optional - if not provided, will register as guest) */
|
|
9
|
+
accessToken?: string;
|
|
10
|
+
/** User ID (optional - required if accessToken is provided) */
|
|
11
|
+
userId?: string;
|
|
12
|
+
/** Device ID (optional) */
|
|
13
|
+
deviceId?: string;
|
|
14
|
+
/** Enable debug logging */
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Matrix Transport for Yjs
|
|
19
|
+
*
|
|
20
|
+
* Provides real-time synchronization using Matrix protocol.
|
|
21
|
+
*
|
|
22
|
+
* Features:
|
|
23
|
+
* - Decentralized federation
|
|
24
|
+
* - Guest access (no login required)
|
|
25
|
+
* - End-to-end encryption support
|
|
26
|
+
* - Persistent message history
|
|
27
|
+
* - Room-based collaboration
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import * as Y from 'yjs'
|
|
32
|
+
* import { GenericProvider } from 'y-generic'
|
|
33
|
+
* import { MatrixTransport } from 'y-generic/providers/matrix'
|
|
34
|
+
*
|
|
35
|
+
* const doc = new Y.Doc()
|
|
36
|
+
* const transport = new MatrixTransport()
|
|
37
|
+
*
|
|
38
|
+
* const provider = new GenericProvider(doc, transport)
|
|
39
|
+
* await provider.connect({
|
|
40
|
+
* homeserverUrl: 'https://matrix.org',
|
|
41
|
+
* room: '#my-room:matrix.org'
|
|
42
|
+
* })
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare class MatrixTransport implements Transport {
|
|
46
|
+
private config;
|
|
47
|
+
private messageCallback?;
|
|
48
|
+
private _isConnected;
|
|
49
|
+
private debug;
|
|
50
|
+
private accessToken;
|
|
51
|
+
private userId;
|
|
52
|
+
private roomId;
|
|
53
|
+
private syncToken;
|
|
54
|
+
private syncRunning;
|
|
55
|
+
private intentionalDisconnect;
|
|
56
|
+
private txnId;
|
|
57
|
+
private messageQueue;
|
|
58
|
+
private receivedBuffer;
|
|
59
|
+
get isConnected(): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Connect to Matrix homeserver and join room
|
|
62
|
+
*/
|
|
63
|
+
connect(config: MatrixConfig): Promise<void>;
|
|
64
|
+
/**
|
|
65
|
+
* Register as guest on the homeserver
|
|
66
|
+
*/
|
|
67
|
+
private registerAsGuest;
|
|
68
|
+
/**
|
|
69
|
+
* Join a Matrix room
|
|
70
|
+
*/
|
|
71
|
+
private joinRoom;
|
|
72
|
+
/**
|
|
73
|
+
* Start Matrix sync loop
|
|
74
|
+
*/
|
|
75
|
+
private startSync;
|
|
76
|
+
/**
|
|
77
|
+
* Perform one sync request
|
|
78
|
+
*/
|
|
79
|
+
private syncOnce;
|
|
80
|
+
/**
|
|
81
|
+
* Disconnect from Matrix
|
|
82
|
+
*/
|
|
83
|
+
disconnect(): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Send Yjs update to Matrix room
|
|
86
|
+
*/
|
|
87
|
+
send(data: Uint8Array): void;
|
|
88
|
+
/**
|
|
89
|
+
* Register message callback
|
|
90
|
+
*/
|
|
91
|
+
onMessage(callback: (data: Uint8Array) => void): () => void;
|
|
92
|
+
/**
|
|
93
|
+
* Flush queued messages
|
|
94
|
+
*/
|
|
95
|
+
private flushMessageQueue;
|
|
96
|
+
/**
|
|
97
|
+
* Convert Uint8Array to base64 string
|
|
98
|
+
*/
|
|
99
|
+
private uint8ToBase64;
|
|
100
|
+
/**
|
|
101
|
+
* Convert base64 string to Uint8Array
|
|
102
|
+
*/
|
|
103
|
+
private base64ToUint8;
|
|
104
|
+
/**
|
|
105
|
+
* Log debug messages
|
|
106
|
+
*/
|
|
107
|
+
private log;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/matrix/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAElE;;GAEG;AACH,MAAM,WAAW,YAAa,SAAQ,gBAAgB;IACpD,kEAAkE;IAClE,aAAa,EAAE,MAAM,CAAA;IACrB,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,+DAA+D;IAC/D,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,2BAA2B;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,eAAgB,YAAW,SAAS;IAC/C,OAAO,CAAC,MAAM,CAA4B;IAC1C,OAAO,CAAC,eAAe,CAAC,CAA4B;IACpD,OAAO,CAAC,YAAY,CAAiB;IACrC,OAAO,CAAC,KAAK,CAAiB;IAC9B,OAAO,CAAC,WAAW,CAAsB;IACzC,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,MAAM,CAAsB;IACpC,OAAO,CAAC,SAAS,CAAsB;IACvC,OAAO,CAAC,WAAW,CAAiB;IACpC,OAAO,CAAC,qBAAqB,CAAiB;IAC9C,OAAO,CAAC,KAAK,CAAY;IAGzB,OAAO,CAAC,YAAY,CAAmB;IACvC,OAAO,CAAC,cAAc,CAAmB;IAEzC,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC;IA0ClD;;OAEG;YACW,eAAe;IA6C7B;;OAEG;YACW,QAAQ;IA+BtB;;OAEG;YACW,SAAS;IAiBvB;;OAEG;YACW,QAAQ;IAsDtB;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAWjC;;OAEG;IACH,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,IAAI;IAuC5B;;OAEG;IACH,SAAS,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,GAAG,MAAM,IAAI;IAiB3D;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAYzB;;OAEG;IACH,OAAO,CAAC,aAAa;IASrB;;OAEG;IACH,OAAO,CAAC,aAAa;IASrB;;OAEG;IACH,OAAO,CAAC,GAAG;CAKZ"}
|
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Matrix Transport for Yjs
|
|
3
|
+
*
|
|
4
|
+
* Provides real-time synchronization using Matrix protocol.
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Decentralized federation
|
|
8
|
+
* - Guest access (no login required)
|
|
9
|
+
* - End-to-end encryption support
|
|
10
|
+
* - Persistent message history
|
|
11
|
+
* - Room-based collaboration
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import * as Y from 'yjs'
|
|
16
|
+
* import { GenericProvider } from 'y-generic'
|
|
17
|
+
* import { MatrixTransport } from 'y-generic/providers/matrix'
|
|
18
|
+
*
|
|
19
|
+
* const doc = new Y.Doc()
|
|
20
|
+
* const transport = new MatrixTransport()
|
|
21
|
+
*
|
|
22
|
+
* const provider = new GenericProvider(doc, transport)
|
|
23
|
+
* await provider.connect({
|
|
24
|
+
* homeserverUrl: 'https://matrix.org',
|
|
25
|
+
* room: '#my-room:matrix.org'
|
|
26
|
+
* })
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export class MatrixTransport {
|
|
30
|
+
constructor() {
|
|
31
|
+
this.config = null;
|
|
32
|
+
this._isConnected = false;
|
|
33
|
+
this.debug = false;
|
|
34
|
+
this.accessToken = null;
|
|
35
|
+
this.userId = null;
|
|
36
|
+
this.roomId = null;
|
|
37
|
+
this.syncToken = null;
|
|
38
|
+
this.syncRunning = false;
|
|
39
|
+
this.intentionalDisconnect = false;
|
|
40
|
+
this.txnId = 0;
|
|
41
|
+
// Message buffering
|
|
42
|
+
this.messageQueue = [];
|
|
43
|
+
this.receivedBuffer = [];
|
|
44
|
+
}
|
|
45
|
+
get isConnected() {
|
|
46
|
+
return this._isConnected;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Connect to Matrix homeserver and join room
|
|
50
|
+
*/
|
|
51
|
+
async connect(config) {
|
|
52
|
+
this.config = config;
|
|
53
|
+
this.debug = config.debug ?? false;
|
|
54
|
+
this.intentionalDisconnect = false;
|
|
55
|
+
if (!config.homeserverUrl) {
|
|
56
|
+
throw new Error('Matrix homeserverUrl is required');
|
|
57
|
+
}
|
|
58
|
+
if (!config.room) {
|
|
59
|
+
throw new Error('Room identifier is required');
|
|
60
|
+
}
|
|
61
|
+
this.log(`🔌 Connecting to Matrix homeserver: ${config.homeserverUrl}`);
|
|
62
|
+
try {
|
|
63
|
+
// Get access token (guest registration or provided token)
|
|
64
|
+
if (config.accessToken && config.userId) {
|
|
65
|
+
this.accessToken = config.accessToken;
|
|
66
|
+
this.userId = config.userId;
|
|
67
|
+
this.log(`✅ Using provided credentials: ${this.userId}`);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
await this.registerAsGuest();
|
|
71
|
+
}
|
|
72
|
+
// Join or create room
|
|
73
|
+
await this.joinRoom(config.room);
|
|
74
|
+
// Start sync loop
|
|
75
|
+
this._isConnected = true;
|
|
76
|
+
this.startSync();
|
|
77
|
+
// Flush any queued messages
|
|
78
|
+
this.flushMessageQueue();
|
|
79
|
+
this.log(`✅ Connected to Matrix room: ${config.room}`);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
this.log(`❌ Connection failed:`, error);
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Register as guest on the homeserver
|
|
88
|
+
*/
|
|
89
|
+
async registerAsGuest() {
|
|
90
|
+
if (!this.config)
|
|
91
|
+
throw new Error('Config not set');
|
|
92
|
+
this.log('👤 Registering as guest...');
|
|
93
|
+
const response = await fetch(`${this.config.homeserverUrl}/_matrix/client/v3/register?kind=guest`, {
|
|
94
|
+
method: 'POST',
|
|
95
|
+
headers: { 'Content-Type': 'application/json' },
|
|
96
|
+
body: JSON.stringify({}),
|
|
97
|
+
});
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
const errorText = await response.text();
|
|
100
|
+
let errorData;
|
|
101
|
+
try {
|
|
102
|
+
errorData = JSON.parse(errorText);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
errorData = { error: errorText };
|
|
106
|
+
}
|
|
107
|
+
// Provide helpful error messages
|
|
108
|
+
if (errorData.errcode === 'M_FORBIDDEN' || response.status === 403) {
|
|
109
|
+
throw new Error(`Guest registration is disabled on this homeserver.\n\n` +
|
|
110
|
+
`Solutions:\n` +
|
|
111
|
+
`1. Use a Matrix account: Provide accessToken and userId\n` +
|
|
112
|
+
`2. Try a different homeserver that supports guest access\n` +
|
|
113
|
+
`3. Run your own Matrix homeserver with guest access enabled\n\n` +
|
|
114
|
+
`Many public homeservers have disabled guest access due to spam.`);
|
|
115
|
+
}
|
|
116
|
+
throw new Error(`Guest registration failed: ${errorText}`);
|
|
117
|
+
}
|
|
118
|
+
const data = await response.json();
|
|
119
|
+
this.accessToken = data.access_token;
|
|
120
|
+
this.userId = data.user_id;
|
|
121
|
+
this.log(`✅ Registered as guest: ${this.userId}`);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Join a Matrix room
|
|
125
|
+
*/
|
|
126
|
+
async joinRoom(roomIdentifier) {
|
|
127
|
+
if (!this.accessToken)
|
|
128
|
+
throw new Error('Not authenticated');
|
|
129
|
+
this.log(`🚪 Joining room: ${roomIdentifier}`);
|
|
130
|
+
// Encode room identifier for URL
|
|
131
|
+
const encodedRoom = encodeURIComponent(roomIdentifier);
|
|
132
|
+
const response = await fetch(`${this.config.homeserverUrl}/_matrix/client/v3/join/${encodedRoom}`, {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: {
|
|
135
|
+
'Content-Type': 'application/json',
|
|
136
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
137
|
+
},
|
|
138
|
+
body: JSON.stringify({}),
|
|
139
|
+
});
|
|
140
|
+
if (!response.ok) {
|
|
141
|
+
const error = await response.text();
|
|
142
|
+
throw new Error(`Failed to join room: ${error}`);
|
|
143
|
+
}
|
|
144
|
+
const data = await response.json();
|
|
145
|
+
this.roomId = data.room_id;
|
|
146
|
+
this.log(`✅ Joined room: ${this.roomId}`);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Start Matrix sync loop
|
|
150
|
+
*/
|
|
151
|
+
async startSync() {
|
|
152
|
+
if (this.syncRunning)
|
|
153
|
+
return;
|
|
154
|
+
this.syncRunning = true;
|
|
155
|
+
this.log('🔄 Starting sync loop...');
|
|
156
|
+
while (this.syncRunning && !this.intentionalDisconnect) {
|
|
157
|
+
try {
|
|
158
|
+
await this.syncOnce();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
this.log('❌ Sync error:', error);
|
|
162
|
+
// Wait before retrying
|
|
163
|
+
await new Promise((resolve) => setTimeout(resolve, 5000));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Perform one sync request
|
|
169
|
+
*/
|
|
170
|
+
async syncOnce() {
|
|
171
|
+
if (!this.accessToken)
|
|
172
|
+
return;
|
|
173
|
+
// Build sync URL with optional since token for long-polling
|
|
174
|
+
let url = `${this.config.homeserverUrl}/_matrix/client/v3/sync?timeout=30000`;
|
|
175
|
+
if (this.syncToken) {
|
|
176
|
+
url += `&since=${this.syncToken}`;
|
|
177
|
+
}
|
|
178
|
+
const response = await fetch(url, {
|
|
179
|
+
headers: {
|
|
180
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
if (!response.ok) {
|
|
184
|
+
throw new Error(`Sync failed: ${response.statusText}`);
|
|
185
|
+
}
|
|
186
|
+
const data = await response.json();
|
|
187
|
+
this.syncToken = data.next_batch;
|
|
188
|
+
// Process room events
|
|
189
|
+
if (data.rooms?.join?.[this.roomId]?.timeline?.events) {
|
|
190
|
+
const events = data.rooms.join[this.roomId].timeline.events;
|
|
191
|
+
for (const event of events) {
|
|
192
|
+
if (event.type === 'm.room.message' &&
|
|
193
|
+
event.content?.msgtype === 'y.update') {
|
|
194
|
+
// Ignore our own messages
|
|
195
|
+
if (event.sender === this.userId)
|
|
196
|
+
continue;
|
|
197
|
+
// Decode the Yjs update from base64
|
|
198
|
+
try {
|
|
199
|
+
const updateBase64 = event.content.body;
|
|
200
|
+
const update = this.base64ToUint8(updateBase64);
|
|
201
|
+
this.log(`📨 Received ${update.length} bytes from ${event.sender}`);
|
|
202
|
+
if (this.messageCallback) {
|
|
203
|
+
this.messageCallback(update);
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
this.receivedBuffer.push(update);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
this.log('❌ Error decoding message:', error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Disconnect from Matrix
|
|
218
|
+
*/
|
|
219
|
+
async disconnect() {
|
|
220
|
+
this.log('🔌 Disconnecting from Matrix...');
|
|
221
|
+
this.intentionalDisconnect = true;
|
|
222
|
+
this.syncRunning = false;
|
|
223
|
+
this._isConnected = false;
|
|
224
|
+
this.accessToken = null;
|
|
225
|
+
this.userId = null;
|
|
226
|
+
this.roomId = null;
|
|
227
|
+
this.syncToken = null;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Send Yjs update to Matrix room
|
|
231
|
+
*/
|
|
232
|
+
send(data) {
|
|
233
|
+
if (!this._isConnected || !this.roomId || !this.accessToken) {
|
|
234
|
+
// Queue message if not connected
|
|
235
|
+
this.messageQueue.push(data);
|
|
236
|
+
this.log(`⏳ Queued ${data.length} bytes (not connected)`);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
try {
|
|
240
|
+
// Encode update as base64 for JSON transport
|
|
241
|
+
const base64Data = this.uint8ToBase64(data);
|
|
242
|
+
// Send as custom message type
|
|
243
|
+
const txnId = `y${this.txnId++}_${Date.now()}`;
|
|
244
|
+
const url = `${this.config.homeserverUrl}/_matrix/client/v3/rooms/${encodeURIComponent(this.roomId)}/send/m.room.message/${txnId}`;
|
|
245
|
+
// Send asynchronously (don't await)
|
|
246
|
+
fetch(url, {
|
|
247
|
+
method: 'PUT',
|
|
248
|
+
headers: {
|
|
249
|
+
'Content-Type': 'application/json',
|
|
250
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
251
|
+
},
|
|
252
|
+
body: JSON.stringify({
|
|
253
|
+
msgtype: 'y.update',
|
|
254
|
+
body: base64Data,
|
|
255
|
+
}),
|
|
256
|
+
}).then((response) => {
|
|
257
|
+
if (response.ok) {
|
|
258
|
+
this.log(`📤 Sent ${data.length} bytes`);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
this.log(`❌ Send failed: ${response.statusText}`);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
this.log('❌ Send error:', error);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Register message callback
|
|
271
|
+
*/
|
|
272
|
+
onMessage(callback) {
|
|
273
|
+
this.messageCallback = callback;
|
|
274
|
+
// Flush buffered messages
|
|
275
|
+
if (this.receivedBuffer.length > 0) {
|
|
276
|
+
this.log(`📦 Flushing ${this.receivedBuffer.length} buffered messages`);
|
|
277
|
+
for (const data of this.receivedBuffer) {
|
|
278
|
+
callback(data);
|
|
279
|
+
}
|
|
280
|
+
this.receivedBuffer = [];
|
|
281
|
+
}
|
|
282
|
+
return () => {
|
|
283
|
+
this.messageCallback = undefined;
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Flush queued messages
|
|
288
|
+
*/
|
|
289
|
+
flushMessageQueue() {
|
|
290
|
+
if (this.messageQueue.length === 0)
|
|
291
|
+
return;
|
|
292
|
+
this.log(`📦 Flushing ${this.messageQueue.length} queued messages`);
|
|
293
|
+
for (const data of this.messageQueue) {
|
|
294
|
+
this.send(data);
|
|
295
|
+
}
|
|
296
|
+
this.messageQueue = [];
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Convert Uint8Array to base64 string
|
|
300
|
+
*/
|
|
301
|
+
uint8ToBase64(bytes) {
|
|
302
|
+
let binary = '';
|
|
303
|
+
const len = bytes.length;
|
|
304
|
+
for (let i = 0; i < len; i++) {
|
|
305
|
+
binary += String.fromCharCode(bytes[i]);
|
|
306
|
+
}
|
|
307
|
+
return btoa(binary);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Convert base64 string to Uint8Array
|
|
311
|
+
*/
|
|
312
|
+
base64ToUint8(base64) {
|
|
313
|
+
const binary = atob(base64);
|
|
314
|
+
const bytes = new Uint8Array(binary.length);
|
|
315
|
+
for (let i = 0; i < binary.length; i++) {
|
|
316
|
+
bytes[i] = binary.charCodeAt(i);
|
|
317
|
+
}
|
|
318
|
+
return bytes;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Log debug messages
|
|
322
|
+
*/
|
|
323
|
+
log(...args) {
|
|
324
|
+
if (this.debug) {
|
|
325
|
+
console.log('[MatrixTransport]', ...args);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/providers/matrix/index.ts"],"names":[],"mappings":"AAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAM,OAAO,eAAe;IAA5B;QACU,WAAM,GAAwB,IAAI,CAAA;QAElC,iBAAY,GAAY,KAAK,CAAA;QAC7B,UAAK,GAAY,KAAK,CAAA;QACtB,gBAAW,GAAkB,IAAI,CAAA;QACjC,WAAM,GAAkB,IAAI,CAAA;QAC5B,WAAM,GAAkB,IAAI,CAAA;QAC5B,cAAS,GAAkB,IAAI,CAAA;QAC/B,gBAAW,GAAY,KAAK,CAAA;QAC5B,0BAAqB,GAAY,KAAK,CAAA;QACtC,UAAK,GAAW,CAAC,CAAA;QAEzB,oBAAoB;QACZ,iBAAY,GAAiB,EAAE,CAAA;QAC/B,mBAAc,GAAiB,EAAE,CAAA;IA6U3C,CAAC;IA3UC,IAA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CAAC;IAED;;OAEG;IACK,aAAa,CAAC,KAAiB;QACrC,IAAI,MAAM,GAAG,EAAE,CAAA;QACf,MAAM,GAAG,GAAG,KAAK,CAAC,MAAM,CAAA;QACxB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;YAC7B,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;QACzC,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAA;IACrB,CAAC;IAED;;OAEG;IACK,aAAa,CAAC,MAAc;QAClC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAA;QAC3B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;QAC3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACvC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAA;QACjC,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;OAEG;IACK,GAAG,CAAC,GAAG,IAAW;QACxB,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAAE,GAAG,IAAI,CAAC,CAAA;QAC3C,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nostr Transport Provider
|
|
3
|
+
*
|
|
4
|
+
* Serverless, decentralised synchronisation using the Nostr protocol.
|
|
5
|
+
* Binary Yjs updates are base64-encoded and published as signed Nostr events
|
|
6
|
+
* to one or more relays. Every connected client subscribes to the same room tag,
|
|
7
|
+
* so updates fan out through all configured relays automatically.
|
|
8
|
+
*
|
|
9
|
+
* Features:
|
|
10
|
+
* - No server setup required (uses public relays or your own)
|
|
11
|
+
* - Multi-relay fan-out for redundancy
|
|
12
|
+
* - Ephemeral or persistent identity (bring-your-own key pair)
|
|
13
|
+
* - Optional password to obfuscate the room tag (SHA-256)
|
|
14
|
+
* - Configurable history window to catch up on missed updates
|
|
15
|
+
* - Automatic deduplication (events from self are ignored)
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* import * as Y from 'yjs'
|
|
20
|
+
* import { GenericProvider } from 'y-generic'
|
|
21
|
+
* import { NostrTransport } from 'y-generic/providers/nostr'
|
|
22
|
+
* import { finalizeEvent, getPublicKey, SimplePool } from 'nostr-tools'
|
|
23
|
+
*
|
|
24
|
+
* const doc = new Y.Doc()
|
|
25
|
+
* const transport = new NostrTransport({ finalizeEvent, getPublicKey, SimplePool })
|
|
26
|
+
*
|
|
27
|
+
* const provider = new GenericProvider(doc, transport)
|
|
28
|
+
* await provider.connect({
|
|
29
|
+
* room: 'my-doc',
|
|
30
|
+
* relays: ['wss://relay.damus.io', 'wss://nos.lol'],
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* // With a persistent identity and password-protected room
|
|
37
|
+
* import { generateSecretKey } from 'nostr-tools/pure'
|
|
38
|
+
*
|
|
39
|
+
* const secretKey = generateSecretKey() // persist this to keep identity
|
|
40
|
+
* const transport = new NostrTransport({ finalizeEvent, getPublicKey, SimplePool, secretKey })
|
|
41
|
+
*
|
|
42
|
+
* await provider.connect({
|
|
43
|
+
* room: 'my-doc',
|
|
44
|
+
* password: 'secret',
|
|
45
|
+
* relays: ['wss://relay.damus.io'],
|
|
46
|
+
* historyWindowSecs: 3600, // fetch last hour of updates on connect
|
|
47
|
+
* })
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
import type { Transport, ConnectionConfig } from '../../transport';
|
|
51
|
+
/** Shape of a signed Nostr event returned by finalizeEvent. */
|
|
52
|
+
interface NostrEvent {
|
|
53
|
+
id: string;
|
|
54
|
+
pubkey: string;
|
|
55
|
+
created_at: number;
|
|
56
|
+
kind: number;
|
|
57
|
+
tags: string[][];
|
|
58
|
+
content: string;
|
|
59
|
+
sig: string;
|
|
60
|
+
}
|
|
61
|
+
/** Shape of an unsigned event template passed to finalizeEvent. */
|
|
62
|
+
interface EventTemplate {
|
|
63
|
+
kind: number;
|
|
64
|
+
created_at: number;
|
|
65
|
+
tags: string[][];
|
|
66
|
+
content: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Constructor options for NostrTransport.
|
|
70
|
+
*
|
|
71
|
+
* The three nostr-tools functions are injected so the transport doesn't bundle
|
|
72
|
+
* them as a hard dependency. Import them from `nostr-tools` or `nostr-tools/pure`.
|
|
73
|
+
*/
|
|
74
|
+
export interface NostrTransportOptions {
|
|
75
|
+
/**
|
|
76
|
+
* `finalizeEvent` from nostr-tools.
|
|
77
|
+
* Signs an event template with the given secret key.
|
|
78
|
+
* @example import { finalizeEvent } from 'nostr-tools/pure'
|
|
79
|
+
*/
|
|
80
|
+
finalizeEvent: (template: EventTemplate, secretKey: Uint8Array) => NostrEvent;
|
|
81
|
+
/**
|
|
82
|
+
* `getPublicKey` from nostr-tools.
|
|
83
|
+
* Derives the hex public key from a secret key.
|
|
84
|
+
* @example import { getPublicKey } from 'nostr-tools/pure'
|
|
85
|
+
*/
|
|
86
|
+
getPublicKey: (secretKey: Uint8Array) => string;
|
|
87
|
+
/**
|
|
88
|
+
* `SimplePool` class from nostr-tools.
|
|
89
|
+
* Manages WebSocket connections to multiple Nostr relays.
|
|
90
|
+
* @example import { SimplePool } from 'nostr-tools'
|
|
91
|
+
*/
|
|
92
|
+
SimplePool: new () => {
|
|
93
|
+
subscribeMany(relays: string[], filters: object[], handlers: {
|
|
94
|
+
onevent?: (event: NostrEvent) => void;
|
|
95
|
+
oneose?: () => void;
|
|
96
|
+
}): {
|
|
97
|
+
close(): void;
|
|
98
|
+
};
|
|
99
|
+
publish(relays: string[], event: NostrEvent): Promise<string>[];
|
|
100
|
+
close(relays: string[]): void;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* Secret key for signing events (32-byte Uint8Array).
|
|
104
|
+
* If omitted a random ephemeral key is generated on first connect — the
|
|
105
|
+
* identity changes on page reload, which is fine for anonymous editing.
|
|
106
|
+
* Persist this value (e.g. in localStorage) to maintain a stable identity.
|
|
107
|
+
* @example import { generateSecretKey } from 'nostr-tools/pure'
|
|
108
|
+
*/
|
|
109
|
+
secretKey?: Uint8Array;
|
|
110
|
+
/**
|
|
111
|
+
* Custom Nostr event kind to use for Yjs update events.
|
|
112
|
+
* @default 27370
|
|
113
|
+
*/
|
|
114
|
+
eventKind?: number;
|
|
115
|
+
/** Enable debug logging. @default false */
|
|
116
|
+
debug?: boolean;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Connection configuration for NostrTransport.
|
|
120
|
+
*/
|
|
121
|
+
export interface NostrConfig extends ConnectionConfig {
|
|
122
|
+
/** Room/document identifier. Mapped to the `r` tag on events. */
|
|
123
|
+
room: string;
|
|
124
|
+
/**
|
|
125
|
+
* Nostr relay WebSocket URLs to connect to.
|
|
126
|
+
* @default ['wss://relay.damus.io', 'wss://nos.lol', 'wss://relay.nostr.band']
|
|
127
|
+
*/
|
|
128
|
+
relays?: string[];
|
|
129
|
+
/**
|
|
130
|
+
* Optional password. When provided, a SHA-256 hash of the password is
|
|
131
|
+
* appended to the room tag so the channel is only discoverable by peers
|
|
132
|
+
* that know the password. This is NOT encryption — use NIP-44 for that.
|
|
133
|
+
*/
|
|
134
|
+
password?: string;
|
|
135
|
+
/**
|
|
136
|
+
* How many seconds of stored relay events to fetch on connect.
|
|
137
|
+
* Set to 0 to receive only real-time events (no catch-up).
|
|
138
|
+
* Increase for longer-lived documents that should survive peer restarts.
|
|
139
|
+
* @default 86400 (24 hours)
|
|
140
|
+
*/
|
|
141
|
+
historyWindowSecs?: number;
|
|
142
|
+
/** Enable debug logging (overrides constructor option). */
|
|
143
|
+
debug?: boolean;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Nostr transport for y-generic.
|
|
147
|
+
*
|
|
148
|
+
* Publishes Yjs binary updates as base64-encoded Nostr events and subscribes
|
|
149
|
+
* to matching events from peers. Works in both browser and Node.js environments.
|
|
150
|
+
*/
|
|
151
|
+
export declare class NostrTransport implements Transport {
|
|
152
|
+
private readonly opts;
|
|
153
|
+
private _connected;
|
|
154
|
+
private _callback?;
|
|
155
|
+
private _buffer;
|
|
156
|
+
private pool;
|
|
157
|
+
private sub;
|
|
158
|
+
private relays;
|
|
159
|
+
private secretKey;
|
|
160
|
+
private pubkey;
|
|
161
|
+
private roomTag;
|
|
162
|
+
private readonly eventKind;
|
|
163
|
+
constructor(opts: NostrTransportOptions);
|
|
164
|
+
get isConnected(): boolean;
|
|
165
|
+
connect(config: NostrConfig): Promise<void>;
|
|
166
|
+
disconnect(): void;
|
|
167
|
+
send(data: Uint8Array): Promise<void>;
|
|
168
|
+
onMessage(callback: (data: Uint8Array) => void): () => void;
|
|
169
|
+
private _deliver;
|
|
170
|
+
}
|
|
171
|
+
export {};
|
|
172
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/providers/nostr/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAA;AAmGlE,+DAA+D;AAC/D,UAAU,UAAU;IAClB,EAAE,EAAE,MAAM,CAAA;IACV,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,EAAE,EAAE,CAAA;IAChB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