@konemono/nostr-login 1.11.5 → 1.11.6
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/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/modules/Nip46.d.ts +6 -1
- package/dist/unpkg.js +1 -1
- package/package.json +1 -1
- package/src/modules/Nip46.ts +80 -25
package/package.json
CHANGED
package/src/modules/Nip46.ts
CHANGED
|
@@ -15,7 +15,8 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
15
15
|
protected requests: Set<string> = new Set();
|
|
16
16
|
private sub?: NDKSubscription;
|
|
17
17
|
protected _useNip44: boolean = false;
|
|
18
|
-
|
|
18
|
+
protected eventEmitter: EventEmitter = new EventEmitter();
|
|
19
|
+
|
|
19
20
|
|
|
20
21
|
public constructor(ndk: NDK, signer: PrivateKeySigner) {
|
|
21
22
|
super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
|
|
@@ -233,6 +234,14 @@ class NostrRpc extends NDKNostrRpc {
|
|
|
233
234
|
return this;
|
|
234
235
|
}
|
|
235
236
|
|
|
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
|
+
|
|
236
245
|
public override emit = <EventKey extends string | symbol = string>(
|
|
237
246
|
event: EventKey,
|
|
238
247
|
...args: any[]
|
|
@@ -373,6 +382,7 @@ export class ReadyListener {
|
|
|
373
382
|
}
|
|
374
383
|
}
|
|
375
384
|
|
|
385
|
+
|
|
376
386
|
export class Nip46Signer extends NDKNip46Signer {
|
|
377
387
|
private _userPubkey: string = '';
|
|
378
388
|
private _rpc: IframeNostrRpc;
|
|
@@ -408,36 +418,64 @@ export class Nip46Signer extends NDKNip46Signer {
|
|
|
408
418
|
}
|
|
409
419
|
|
|
410
420
|
// 接続確認(必要時のみping)
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
if (!this._remotePubkey) return;
|
|
414
|
-
|
|
415
|
-
const now = Date.now();
|
|
421
|
+
private async ensureConnection(retries: number = 2): Promise<void> {
|
|
422
|
+
if (!this._remotePubkey) return;
|
|
416
423
|
|
|
417
|
-
|
|
418
|
-
return;
|
|
419
|
-
}
|
|
424
|
+
const now = Date.now();
|
|
420
425
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
await this._rpc.pingWithTimeout(this._remotePubkey, 10000);
|
|
424
|
-
this.lastPingTime = now;
|
|
425
|
-
console.log('Connection check OK');
|
|
426
|
+
// 最近ping成功していればスキップ
|
|
427
|
+
if (now - this.lastPingTime < this.pingCacheDuration) {
|
|
426
428
|
return;
|
|
427
|
-
}
|
|
428
|
-
if (i === retries) {
|
|
429
|
-
console.error('Ping failed, triggering reconnection');
|
|
430
|
-
// 再接続処理をトリガー
|
|
431
|
-
this.emit('connectionLost');
|
|
432
|
-
throw new Error('NIP-46 connection lost');
|
|
433
|
-
}
|
|
429
|
+
}
|
|
434
430
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
431
|
+
for (let i = 0; i <= retries; i++) {
|
|
432
|
+
try {
|
|
433
|
+
await this._rpc.pingWithTimeout(this._remotePubkey, 10000);
|
|
434
|
+
this.lastPingTime = now;
|
|
435
|
+
console.log('Connection check OK');
|
|
436
|
+
return;
|
|
437
|
+
} catch (error) {
|
|
438
|
+
if (i === retries) {
|
|
439
|
+
console.error('Ping failed, attempting reconnection');
|
|
440
|
+
|
|
441
|
+
// 再接続を試みる
|
|
442
|
+
try {
|
|
443
|
+
this.emit('connectionLost');
|
|
444
|
+
|
|
445
|
+
// 再接続完了を待つ(最大10秒)
|
|
446
|
+
await new Promise<void>((resolve, reject) => {
|
|
447
|
+
const timeout = setTimeout(() => reject(new Error('Reconnection timeout')), 10000);
|
|
448
|
+
|
|
449
|
+
const checkReconnection = async () => {
|
|
450
|
+
try {
|
|
451
|
+
await this._rpc.pingWithTimeout(this._remotePubkey!, 10000);
|
|
452
|
+
clearTimeout(timeout);
|
|
453
|
+
this.lastPingTime = Date.now();
|
|
454
|
+
console.log('Reconnection successful');
|
|
455
|
+
resolve();
|
|
456
|
+
} catch (e) {
|
|
457
|
+
// まだ再接続中、1秒後に再確認
|
|
458
|
+
setTimeout(checkReconnection, 1000);
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
// 少し待ってから確認開始
|
|
463
|
+
setTimeout(checkReconnection, 2000);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
return;
|
|
467
|
+
} catch (reconnectError) {
|
|
468
|
+
console.error('Reconnection failed:', reconnectError);
|
|
469
|
+
throw new Error('NIP-46 connection lost and reconnection failed');
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
const delay = Math.min(1000 * Math.pow(2, i), 5000);
|
|
474
|
+
console.log(`Ping failed, retrying in ${delay}ms...`);
|
|
475
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
476
|
+
}
|
|
438
477
|
}
|
|
439
478
|
}
|
|
440
|
-
}
|
|
441
479
|
|
|
442
480
|
private async setSignerPubkey(signerPubkey: string, sameAsUser: boolean = false) {
|
|
443
481
|
console.log('setSignerPubkey', signerPubkey);
|
|
@@ -542,6 +580,23 @@ private async ensureConnection(retries: number = 2): Promise<void> {
|
|
|
542
580
|
}
|
|
543
581
|
|
|
544
582
|
// EventEmitter互換メソッド
|
|
583
|
+
// ★ ここに once を追加 ★
|
|
584
|
+
public override on = <EventKey extends string | symbol = string>(
|
|
585
|
+
event: EventKey,
|
|
586
|
+
listener: (...args: any[]) => void
|
|
587
|
+
): this => {
|
|
588
|
+
this._rpc.on(event as string, listener);
|
|
589
|
+
return this;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
public override once = <EventKey extends string | symbol = string>(
|
|
593
|
+
event: EventKey,
|
|
594
|
+
listener: (...args: any[]) => void
|
|
595
|
+
): this => {
|
|
596
|
+
this._rpc.once(event as string, listener);
|
|
597
|
+
return this;
|
|
598
|
+
}
|
|
599
|
+
|
|
545
600
|
public override emit = <EventKey extends string | symbol = string>(
|
|
546
601
|
event: EventKey,
|
|
547
602
|
...args: any[]
|