@konemono/nostr-login 1.7.23 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konemono/nostr-login",
3
- "version": "1.7.23",
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);
@@ -144,15 +144,107 @@ class NostrRpc extends NDKNostrRpc {
144
144
  return Math.random().toString(36).substring(7);
145
145
  }
146
146
 
147
+ protected async ensureConnected(): Promise<void> {
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);
155
+
156
+ if (connectedRelays.length === 0) {
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
+ // 再接続
169
+ await this._ndk.connect();
170
+
171
+ // 接続確立を待つ
172
+ await new Promise<void>((resolve, reject) => {
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);
176
+ reject(new Error('Failed to reconnect to relays'));
177
+ }, 10000);
178
+
179
+ const checkConnection = () => {
180
+ const connected = Array.from(this._ndk.pool.relays.values()).filter(r => r.status === 1);
181
+ if (connected.length > 0) {
182
+ clearTimeout(timeout);
183
+ console.log(
184
+ 'Successfully reconnected to',
185
+ connected.length,
186
+ 'relays:',
187
+ connected.map(r => r.url),
188
+ );
189
+ resolve();
190
+ } else {
191
+ setTimeout(checkConnection, 200);
192
+ }
193
+ };
194
+ checkConnection();
195
+ });
196
+ } else {
197
+ console.log(
198
+ 'Already connected to',
199
+ connectedRelays.length,
200
+ 'relays:',
201
+ connectedRelays.map(r => r.url),
202
+ );
203
+ }
204
+ }
205
+
147
206
  public async sendRequest(remotePubkey: string, method: string, params: string[] = [], kind = 24133, cb?: (res: NDKRpcResponse) => void): Promise<NDKRpcResponse> {
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
+ }
223
+
148
224
  const id = this.getId();
149
225
 
150
226
  this.setResponseHandler(id, cb);
151
227
 
152
228
  const event = await this.createRequestEvent(id, remotePubkey, method, params, kind);
153
- console.log('sendRequest', { event, method, remotePubkey, params });
229
+ console.log('sendRequest event created', { event, method, remotePubkey, params });
154
230
 
155
- 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
+ }
156
248
 
157
249
  // @ts-ignore
158
250
  return undefined as NDKRpcResponse;
@@ -166,6 +258,7 @@ class NostrRpc extends NDKNostrRpc {
166
258
  if (this.requests.has(id)) {
167
259
  this.requests.delete(id);
168
260
  this.requestTimeouts.delete(id);
261
+ console.log('NIP46 request timeout for', id);
169
262
  if (cb) {
170
263
  cb({
171
264
  id,
@@ -283,6 +376,25 @@ export class IframeNostrRpc extends NostrRpc {
283
376
  }
284
377
 
285
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
+
381
+ if (!this.iframePort) {
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
+ }
396
+ }
397
+
286
398
  const id = this.getId();
287
399
 
288
400
  const event = await this.createRequestEvent(id, remotePubkey, method, params, kind);
@@ -295,7 +407,23 @@ export class IframeNostrRpc extends NostrRpc {
295
407
  console.log('iframe-nip46 sending request to', this.peerOrigin, event.rawEvent());
296
408
  this.iframePort.postMessage(event.rawEvent());
297
409
  } else {
298
- 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
+ }
299
427
  }
300
428
 
301
429
  // @ts-ignore