@konemono/nostr-login 1.11.7 → 1.11.9

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.11.7",
3
+ "version": "1.11.9",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -12,7 +12,6 @@
12
12
  "author": "a-fralou",
13
13
  "dependencies": {
14
14
  "@nostr-dev-kit/ndk": "^2.3.1",
15
- "events": "^3.3.0",
16
15
  "nostr-tools": "^1.17.0",
17
16
  "tseep": "^1.2.1"
18
17
  },
@@ -610,8 +610,7 @@ class AuthNostrService extends EventEmitter implements Signer {
610
610
  const localSigner = new PrivateKeySigner(info.sk!);
611
611
  this.signer = new Nip46Signer(this.ndk, localSigner, info.signerPubkey!, info.iframeUrl ? new URL(info.iframeUrl!).origin : undefined);
612
612
 
613
-
614
- // ★ once を使う - 1回だけ実行される ★
613
+ // ★ once を使う - 1回だけ実行される ★
615
614
  this.signer.once('connectionLost', async () => {
616
615
  console.log('Connection lost, attempting to reconnect...');
617
616
  this.signer = null;
@@ -621,7 +620,6 @@ class AuthNostrService extends EventEmitter implements Signer {
621
620
  }
622
621
  });
623
622
 
624
-
625
623
  // we should notify the banner the same way as
626
624
  // the onAuthUrl does
627
625
  this.signer.on(`iframeRestart`, async () => {
@@ -17,7 +17,6 @@ class NostrRpc extends NDKNostrRpc {
17
17
  protected _useNip44: boolean = false;
18
18
  protected eventEmitter: EventEmitter = new EventEmitter();
19
19
 
20
-
21
20
  public constructor(ndk: NDK, signer: PrivateKeySigner) {
22
21
  super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
23
22
  this._ndk = ndk;
@@ -150,7 +149,7 @@ class NostrRpc extends NDKNostrRpc {
150
149
  }
151
150
 
152
151
  // タイムアウト対応のping
153
- public async pingWithTimeout(remotePubkey: string, timeoutMs: number = 10000): Promise<void> {
152
+ public async pingWithTimeout(remotePubkey: string, timeoutMs: number = 3000): Promise<void> {
154
153
  return withTimeout(this.ping(remotePubkey), timeoutMs, `Ping timeout after ${timeoutMs}ms`);
155
154
  }
156
155
 
@@ -158,6 +157,14 @@ class NostrRpc extends NDKNostrRpc {
158
157
  return Math.random().toString(36).substring(7);
159
158
  }
160
159
 
160
+ public override once = <EventKey extends string | symbol = string>(
161
+ event: EventKey,
162
+ listener: (...args: any[]) => void
163
+ ): this => {
164
+ this.eventEmitter.once(event as string, listener);
165
+ return this;
166
+ }
167
+
161
168
  public async sendRequest(remotePubkey: string, method: string, params: string[] = [], kind = 24133, cb?: (res: NDKRpcResponse) => void): Promise<NDKRpcResponse> {
162
169
  const id = this.getId();
163
170
 
@@ -234,14 +241,6 @@ class NostrRpc extends NDKNostrRpc {
234
241
  return this;
235
242
  }
236
243
 
237
- public override once = <EventKey extends string | symbol = string>(
238
- event: EventKey,
239
- listener: (...args: any[]) => void
240
- ): this => {
241
- this.eventEmitter.once(event as string, listener);
242
- return this;
243
- }
244
-
245
244
  public override emit = <EventKey extends string | symbol = string>(
246
245
  event: EventKey,
247
246
  ...args: any[]
@@ -382,7 +381,6 @@ export class ReadyListener {
382
381
  }
383
382
  }
384
383
 
385
-
386
384
  export class Nip46Signer extends NDKNip46Signer {
387
385
  private _userPubkey: string = '';
388
386
  private _rpc: IframeNostrRpc;
@@ -401,7 +399,6 @@ export class Nip46Signer extends NDKNip46Signer {
401
399
  });
402
400
 
403
401
  this.rpc = this._rpc;
404
-
405
402
  }
406
403
 
407
404
  get userPubkey() {
@@ -410,7 +407,10 @@ export class Nip46Signer extends NDKNip46Signer {
410
407
 
411
408
 
412
409
 
413
- // 接続確認(必要時のみping)
410
+ // Nip46.tsのNip46Signerクラス内
411
+ // 接続確認(必要時のみping リトライ付き 最大10秒)
412
+ // リトライ回数: 2回(計3回試行)
413
+ //合計最大時間: 2秒(ping) × 3回 + 2秒(待機) × 2回 = 10秒
414
414
  private async ensureConnection(retries: number = 2): Promise<void> {
415
415
  if (!this.remotePubkey) return;
416
416
 
@@ -423,48 +423,18 @@ export class Nip46Signer extends NDKNip46Signer {
423
423
 
424
424
  for (let i = 0; i <= retries; i++) {
425
425
  try {
426
- await this._rpc.pingWithTimeout(this.remotePubkey, 10000);
426
+ await this._rpc.pingWithTimeout(this.remotePubkey, 2000); // 2秒タイムアウト
427
427
  this.lastPingTime = now;
428
428
  console.log('Connection check OK');
429
429
  return;
430
430
  } catch (error) {
431
431
  if (i === retries) {
432
- console.error('Ping failed, attempting reconnection');
433
-
434
- // 再接続を試みる
435
- try {
436
- this.emit('connectionLost');
437
-
438
- // 再接続完了を待つ(最大10秒)
439
- await new Promise<void>((resolve, reject) => {
440
- const timeout = setTimeout(() => reject(new Error('Reconnection timeout')), 10000);
441
-
442
- const checkReconnection = async () => {
443
- try {
444
- await this._rpc.pingWithTimeout(this.remotePubkey!, 10000);
445
- clearTimeout(timeout);
446
- this.lastPingTime = Date.now();
447
- console.log('Reconnection successful');
448
- resolve();
449
- } catch (e) {
450
- // まだ再接続中、1秒後に再確認
451
- setTimeout(checkReconnection, 1000);
452
- }
453
- };
454
-
455
- // 少し待ってから確認開始
456
- setTimeout(checkReconnection, 2000);
457
- });
458
-
459
- return;
460
- } catch (reconnectError) {
461
- console.error('Reconnection failed:', reconnectError);
462
- throw new Error('NIP-46 connection lost and reconnection failed');
463
- }
432
+ console.error('Connection check failed after retries', error);
433
+ throw new Error('NIP-46 connection lost');
464
434
  }
465
435
 
466
- const delay = Math.min(1000 * Math.pow(2, i), 5000);
467
- console.log(`Ping failed, retrying in ${delay}ms...`);
436
+ const delay = 2000; // 2秒間隔で再送
437
+ console.log(`Ping failed (${i + 1}/${retries + 1}), retrying in ${delay}ms...`);
468
438
  await new Promise(resolve => setTimeout(resolve, delay));
469
439
  }
470
440
  }
@@ -473,7 +443,6 @@ export class Nip46Signer extends NDKNip46Signer {
473
443
  private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
474
444
  console.log('setSignerPubkey', signerPubkey);
475
445
 
476
-
477
446
  // ensure it's set
478
447
  this.remotePubkey = signerPubkey;
479
448
 
@@ -574,7 +543,7 @@ export class Nip46Signer extends NDKNip46Signer {
574
543
  }
575
544
 
576
545
  // EventEmitter互換メソッド
577
- // ★ ここに once を追加 ★
546
+ // ★ ここに once を追加 ★
578
547
  public override on = <EventKey extends string | symbol = string>(
579
548
  event: EventKey,
580
549
  listener: (...args: any[]) => void