@konemono/nostr-login 1.7.24 → 1.7.25

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/modules/Nip46.ts +105 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konemono/nostr-login",
3
- "version": "1.7.24",
3
+ "version": "1.7.25",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -38,7 +38,7 @@ class NostrRpc extends NDKNostrRpc {
38
38
  this.requestTimeouts.clear();
39
39
  }
40
40
 
41
- private clearTimeout(id: string) {
41
+ protected clearTimeout(id: string) {
42
42
  const timeout = this.requestTimeouts.get(id);
43
43
  if (timeout) {
44
44
  clearTimeout(timeout);
@@ -145,43 +145,106 @@ class NostrRpc extends NDKNostrRpc {
145
145
  }
146
146
 
147
147
  protected async ensureConnected(): Promise<void> {
148
- const connectedRelays = Array.from(this._ndk.pool.relays.values()).filter(r => r.status === 1);
148
+ const relays = Array.from(this._ndk.pool.relays.values());
149
+ console.log(
150
+ 'Checking relay connections:',
151
+ relays.map(r => ({ url: r.url, status: r.status })),
152
+ );
153
+
154
+ const connectedRelays = relays.filter(r => r.status === 1);
149
155
 
150
156
  if (connectedRelays.length === 0) {
151
- console.log('No connected relays, reconnecting...');
157
+ console.log('No connected relays, forcing reconnection...');
158
+
159
+ // 既存の接続を全てクリーンアップ
160
+ for (const relay of relays) {
161
+ try {
162
+ await relay.disconnect();
163
+ } catch (e) {
164
+ console.log('Error disconnecting relay:', relay.url, e);
165
+ }
166
+ }
167
+
168
+ // 再接続
152
169
  await this._ndk.connect();
153
170
 
171
+ // 接続確立を待つ
154
172
  await new Promise<void>((resolve, reject) => {
155
173
  const timeout = setTimeout(() => {
174
+ const status = Array.from(this._ndk.pool.relays.values()).map(r => ({ url: r.url, status: r.status }));
175
+ console.error('Failed to reconnect to relays within 10s. Status:', status);
156
176
  reject(new Error('Failed to reconnect to relays'));
157
- }, 5000);
177
+ }, 10000);
158
178
 
159
179
  const checkConnection = () => {
160
180
  const connected = Array.from(this._ndk.pool.relays.values()).filter(r => r.status === 1);
161
181
  if (connected.length > 0) {
162
182
  clearTimeout(timeout);
163
- console.log('Reconnected to', connected.length, 'relays');
183
+ console.log(
184
+ 'Successfully reconnected to',
185
+ connected.length,
186
+ 'relays:',
187
+ connected.map(r => r.url),
188
+ );
164
189
  resolve();
165
190
  } else {
166
- setTimeout(checkConnection, 100);
191
+ setTimeout(checkConnection, 200);
167
192
  }
168
193
  };
169
194
  checkConnection();
170
195
  });
196
+ } else {
197
+ console.log(
198
+ 'Already connected to',
199
+ connectedRelays.length,
200
+ 'relays:',
201
+ connectedRelays.map(r => r.url),
202
+ );
171
203
  }
172
204
  }
173
205
 
174
206
  public async sendRequest(remotePubkey: string, method: string, params: string[] = [], kind = 24133, cb?: (res: NDKRpcResponse) => void): Promise<NDKRpcResponse> {
175
- await this.ensureConnected();
207
+ console.log('sendRequest called:', method, 'to', remotePubkey);
208
+
209
+ try {
210
+ await this.ensureConnected();
211
+ } catch (e) {
212
+ console.error('Failed to ensure connection:', e);
213
+ if (cb) {
214
+ cb({
215
+ id: '',
216
+ result: '',
217
+ error: 'Failed to connect to relays: ' + (e as Error).message,
218
+ event: undefined as any,
219
+ });
220
+ }
221
+ throw e;
222
+ }
176
223
 
177
224
  const id = this.getId();
178
225
 
179
226
  this.setResponseHandler(id, cb);
180
227
 
181
228
  const event = await this.createRequestEvent(id, remotePubkey, method, params, kind);
182
- console.log('sendRequest', { event, method, remotePubkey, params });
229
+ console.log('sendRequest event created', { event, method, remotePubkey, params });
183
230
 
184
- await event.publish();
231
+ try {
232
+ await event.publish();
233
+ console.log('sendRequest event published successfully');
234
+ } catch (e) {
235
+ console.error('Failed to publish event:', e);
236
+ this.clearTimeout(id);
237
+ this.requests.delete(id);
238
+ if (cb) {
239
+ cb({
240
+ id,
241
+ result: '',
242
+ error: 'Failed to publish event: ' + (e as Error).message,
243
+ event: undefined as any,
244
+ });
245
+ }
246
+ throw e;
247
+ }
185
248
 
186
249
  // @ts-ignore
187
250
  return undefined as NDKRpcResponse;
@@ -313,8 +376,23 @@ export class IframeNostrRpc extends NostrRpc {
313
376
  }
314
377
 
315
378
  public async sendRequest(remotePubkey: string, method: string, params: string[] = [], kind = 24133, cb?: (res: NDKRpcResponse) => void): Promise<NDKRpcResponse> {
379
+ console.log('IframeNostrRpc.sendRequest called:', method, 'iframePort:', !!this.iframePort);
380
+
316
381
  if (!this.iframePort) {
317
- await this.ensureConnected();
382
+ try {
383
+ await this.ensureConnected();
384
+ } catch (e) {
385
+ console.error('Failed to ensure connection:', e);
386
+ if (cb) {
387
+ cb({
388
+ id: '',
389
+ result: '',
390
+ error: 'Failed to connect to relays: ' + (e as Error).message,
391
+ event: undefined as any,
392
+ });
393
+ }
394
+ throw e;
395
+ }
318
396
  }
319
397
 
320
398
  const id = this.getId();
@@ -329,7 +407,23 @@ export class IframeNostrRpc extends NostrRpc {
329
407
  console.log('iframe-nip46 sending request to', this.peerOrigin, event.rawEvent());
330
408
  this.iframePort.postMessage(event.rawEvent());
331
409
  } else {
332
- await event.publish();
410
+ try {
411
+ await event.publish();
412
+ console.log('Request published to relays successfully');
413
+ } catch (e) {
414
+ console.error('Failed to publish event:', e);
415
+ this.clearTimeout(id);
416
+ this.requests.delete(id);
417
+ if (cb) {
418
+ cb({
419
+ id,
420
+ result: '',
421
+ error: 'Failed to publish event: ' + (e as Error).message,
422
+ event: undefined as any,
423
+ });
424
+ }
425
+ throw e;
426
+ }
333
427
  }
334
428
 
335
429
  // @ts-ignore