@konemono/nostr-login 1.11.4 → 1.11.5

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.4",
3
+ "version": "1.11.5",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
@@ -610,6 +610,17 @@ 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
+ // ★ once を使う - 1回だけ実行される ★
614
+ this.signer.once('connectionLost', async () => {
615
+ console.log('Connection lost, attempting to reconnect...');
616
+ this.signer = null;
617
+
618
+ if (this.params.userInfo) {
619
+ await this.initSigner(this.params.userInfo);
620
+ }
621
+ });
622
+
623
+
613
624
  // we should notify the banner the same way as
614
625
  // the onAuthUrl does
615
626
  this.signer.on(`iframeRestart`, async () => {
@@ -408,34 +408,36 @@ export class Nip46Signer extends NDKNip46Signer {
408
408
  }
409
409
 
410
410
  // 接続確認(必要時のみping)
411
- private async ensureConnection(retries: number = 2): Promise<void> {
412
- if (!this._remotePubkey) return;
411
+ // Nip46.tsのNip46Signerクラス
412
+ private async ensureConnection(retries: number = 2): Promise<void> {
413
+ if (!this._remotePubkey) return;
413
414
 
414
- const now = Date.now();
415
-
416
- // 最近ping成功していればスキップ
417
- if (now - this.lastPingTime < this.pingCacheDuration) {
418
- return;
419
- }
415
+ const now = Date.now();
420
416
 
421
- for (let i = 0; i <= retries; i++) {
422
- try {
423
- await this._rpc.pingWithTimeout(this._remotePubkey, 10000);
424
- this.lastPingTime = now;
425
- console.log('Connection check OK');
426
- return;
427
- } catch (error) {
428
- if (i === retries) {
429
- console.error('Connection check failed after retries', error);
430
- throw new Error('NIP-46 connection lost');
431
- }
417
+ if (now - this.lastPingTime < this.pingCacheDuration) {
418
+ return;
419
+ }
432
420
 
433
- const delay = Math.min(1000 * Math.pow(2, i), 5000);
434
- console.log(`Ping failed, retrying in ${delay}ms...`);
435
- await new Promise(resolve => setTimeout(resolve, delay));
421
+ for (let i = 0; i <= retries; i++) {
422
+ try {
423
+ await this._rpc.pingWithTimeout(this._remotePubkey, 10000);
424
+ this.lastPingTime = now;
425
+ console.log('Connection check OK');
426
+ return;
427
+ } catch (error) {
428
+ if (i === retries) {
429
+ console.error('Ping failed, triggering reconnection');
430
+ // 再接続処理をトリガー
431
+ this.emit('connectionLost');
432
+ throw new Error('NIP-46 connection lost');
436
433
  }
434
+
435
+ const delay = Math.min(1000 * Math.pow(2, i), 5000);
436
+ console.log(`Ping failed, retrying in ${delay}ms...`);
437
+ await new Promise(resolve => setTimeout(resolve, delay));
437
438
  }
438
439
  }
440
+ }
439
441
 
440
442
  private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
441
443
  console.log('setSignerPubkey', signerPubkey);