@kehto/paja 0.1.0 → 0.2.0

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.
@@ -3432,6 +3432,184 @@ function createNotificationService(options) {
3432
3432
  };
3433
3433
  }
3434
3434
  var IDENTITY_SERVICE_VERSION = "1.0.0";
3435
+ function sendProviderError(send, result, fallback, err) {
3436
+ send({
3437
+ ...result,
3438
+ error: err?.message ?? fallback
3439
+ });
3440
+ }
3441
+ async function getCurrentPubkey(options) {
3442
+ const currentSigner = options.getSigner();
3443
+ if (!currentSigner?.getPublicKey) return "";
3444
+ try {
3445
+ return await currentSigner.getPublicKey() ?? "";
3446
+ } catch {
3447
+ return "";
3448
+ }
3449
+ }
3450
+ function sendOptionalProviderResult(options, send, fallbackResult, errorFallback, buildResult) {
3451
+ if (!buildResult) {
3452
+ send(fallbackResult);
3453
+ return;
3454
+ }
3455
+ Promise.resolve(getCurrentPubkey(options)).then((pubkey) => buildResult(pubkey)).then((result) => send(result)).catch((err) => sendProviderError(send, fallbackResult, errorFallback, err));
3456
+ }
3457
+ function sendIdentityError(send, id, typeBase, error) {
3458
+ send({ type: `${typeBase}.error`, id, error });
3459
+ }
3460
+ function sendSignerError(send, id, typeBase, fallback, err) {
3461
+ sendIdentityError(send, id, typeBase, err?.message ?? fallback);
3462
+ }
3463
+ function handleGetPublicKey(options, id, send) {
3464
+ const signer = options.getSigner();
3465
+ if (!signer) {
3466
+ const result = {
3467
+ type: "identity.getPublicKey.result",
3468
+ id,
3469
+ pubkey: ""
3470
+ };
3471
+ send(result);
3472
+ return;
3473
+ }
3474
+ Promise.resolve(signer.getPublicKey?.()).then((pubkey) => {
3475
+ const result = {
3476
+ type: "identity.getPublicKey.result",
3477
+ id,
3478
+ pubkey: pubkey ?? ""
3479
+ };
3480
+ send(result);
3481
+ }).catch((err) => sendSignerError(send, id, "identity.getPublicKey", "getPublicKey failed", err));
3482
+ }
3483
+ function handleGetRelays(options, id, send) {
3484
+ const signer = options.getSigner();
3485
+ if (!signer) {
3486
+ sendIdentityError(send, id, "identity.getRelays", "no signer configured");
3487
+ return;
3488
+ }
3489
+ Promise.resolve(signer.getRelays?.() ?? {}).then((relays) => {
3490
+ const result = {
3491
+ type: "identity.getRelays.result",
3492
+ id,
3493
+ relays
3494
+ };
3495
+ send(result);
3496
+ }).catch((err) => sendSignerError(send, id, "identity.getRelays", "getRelays failed", err));
3497
+ }
3498
+ function handleReadProvider(options, id, message, send) {
3499
+ switch (message.type) {
3500
+ case "identity.getProfile":
3501
+ sendOptionalProviderResult(
3502
+ options,
3503
+ send,
3504
+ { type: "identity.getProfile.result", id, profile: null },
3505
+ "getProfile failed",
3506
+ options.getProfile ? async (pubkey) => ({
3507
+ type: "identity.getProfile.result",
3508
+ id,
3509
+ profile: await options.getProfile(pubkey, message)
3510
+ }) : void 0
3511
+ );
3512
+ return true;
3513
+ case "identity.getFollows":
3514
+ sendOptionalProviderResult(
3515
+ options,
3516
+ send,
3517
+ { type: "identity.getFollows.result", id, pubkeys: [] },
3518
+ "getFollows failed",
3519
+ options.getFollows ? async (pubkey) => ({
3520
+ type: "identity.getFollows.result",
3521
+ id,
3522
+ pubkeys: await options.getFollows(pubkey, message)
3523
+ }) : void 0
3524
+ );
3525
+ return true;
3526
+ case "identity.getList":
3527
+ handleGetList(options, id, message, send);
3528
+ return true;
3529
+ case "identity.getZaps":
3530
+ sendOptionalProviderResult(
3531
+ options,
3532
+ send,
3533
+ { type: "identity.getZaps.result", id, zaps: [] },
3534
+ "getZaps failed",
3535
+ options.getZaps ? async (pubkey) => ({
3536
+ type: "identity.getZaps.result",
3537
+ id,
3538
+ zaps: await options.getZaps(pubkey, message)
3539
+ }) : void 0
3540
+ );
3541
+ return true;
3542
+ case "identity.getMutes":
3543
+ sendOptionalProviderResult(
3544
+ options,
3545
+ send,
3546
+ { type: "identity.getMutes.result", id, pubkeys: [] },
3547
+ "getMutes failed",
3548
+ options.getMutes ? async (pubkey) => ({
3549
+ type: "identity.getMutes.result",
3550
+ id,
3551
+ pubkeys: await options.getMutes(pubkey, message)
3552
+ }) : void 0
3553
+ );
3554
+ return true;
3555
+ case "identity.getBlocked":
3556
+ sendOptionalProviderResult(
3557
+ options,
3558
+ send,
3559
+ { type: "identity.getBlocked.result", id, pubkeys: [] },
3560
+ "getBlocked failed",
3561
+ options.getBlocked ? async (pubkey) => ({
3562
+ type: "identity.getBlocked.result",
3563
+ id,
3564
+ pubkeys: await options.getBlocked(pubkey, message)
3565
+ }) : void 0
3566
+ );
3567
+ return true;
3568
+ case "identity.getBadges":
3569
+ sendOptionalProviderResult(
3570
+ options,
3571
+ send,
3572
+ { type: "identity.getBadges.result", id, badges: [] },
3573
+ "getBadges failed",
3574
+ options.getBadges ? async (pubkey) => ({
3575
+ type: "identity.getBadges.result",
3576
+ id,
3577
+ badges: await options.getBadges(pubkey, message)
3578
+ }) : void 0
3579
+ );
3580
+ return true;
3581
+ default:
3582
+ return false;
3583
+ }
3584
+ }
3585
+ function handleGetList(options, id, message, send) {
3586
+ sendOptionalProviderResult(
3587
+ options,
3588
+ send,
3589
+ { type: "identity.getList.result", id, entries: [] },
3590
+ "getList failed",
3591
+ options.getList ? async (pubkey) => ({
3592
+ type: "identity.getList.result",
3593
+ id,
3594
+ entries: await options.getList(message.listType, pubkey, message)
3595
+ }) : void 0
3596
+ );
3597
+ }
3598
+ function handleIdentityServiceMessage(options, message, send) {
3599
+ const id = message.id ?? "";
3600
+ switch (message.type) {
3601
+ case "identity.getPublicKey":
3602
+ handleGetPublicKey(options, id, send);
3603
+ return;
3604
+ case "identity.getRelays":
3605
+ handleGetRelays(options, id, send);
3606
+ return;
3607
+ default:
3608
+ if (!handleReadProvider(options, id, message, send)) {
3609
+ sendIdentityError(send, id, message.type, `Unknown identity method: ${message.type}`);
3610
+ }
3611
+ }
3612
+ }
3435
3613
  function createIdentityService(options) {
3436
3614
  return {
3437
3615
  descriptor: {
@@ -3440,116 +3618,7 @@ function createIdentityService(options) {
3440
3618
  description: "NIP-5D identity NAP reference handler (9 read-only identity queries)"
3441
3619
  },
3442
3620
  handleMessage(_windowId, message, send) {
3443
- const id = message.id ?? "";
3444
- function sendError(typeBase, error) {
3445
- send({ type: `${typeBase}.error`, id, error });
3446
- }
3447
- function sendSignerError(typeBase, fallback, err) {
3448
- sendError(typeBase, err?.message ?? fallback);
3449
- }
3450
- const signer = options.getSigner();
3451
- switch (message.type) {
3452
- case "identity.getPublicKey": {
3453
- if (!signer) {
3454
- const result = {
3455
- type: "identity.getPublicKey.result",
3456
- id,
3457
- pubkey: ""
3458
- };
3459
- send(result);
3460
- return;
3461
- }
3462
- Promise.resolve(signer.getPublicKey?.()).then((pubkey) => {
3463
- const result = {
3464
- type: "identity.getPublicKey.result",
3465
- id,
3466
- pubkey: pubkey ?? ""
3467
- };
3468
- send(result);
3469
- }).catch((err) => sendSignerError("identity.getPublicKey", "getPublicKey failed", err));
3470
- return;
3471
- }
3472
- case "identity.getRelays": {
3473
- if (!signer) {
3474
- sendError("identity.getRelays", "no signer configured");
3475
- return;
3476
- }
3477
- Promise.resolve(signer.getRelays?.() ?? {}).then((relays) => {
3478
- const result = {
3479
- type: "identity.getRelays.result",
3480
- id,
3481
- relays
3482
- };
3483
- send(result);
3484
- }).catch((err) => sendSignerError("identity.getRelays", "getRelays failed", err));
3485
- return;
3486
- }
3487
- case "identity.getProfile": {
3488
- const result = {
3489
- type: "identity.getProfile.result",
3490
- id,
3491
- profile: null
3492
- };
3493
- send(result);
3494
- return;
3495
- }
3496
- case "identity.getFollows": {
3497
- const result = {
3498
- type: "identity.getFollows.result",
3499
- id,
3500
- pubkeys: []
3501
- };
3502
- send(result);
3503
- return;
3504
- }
3505
- case "identity.getList": {
3506
- const result = {
3507
- type: "identity.getList.result",
3508
- id,
3509
- entries: []
3510
- };
3511
- send(result);
3512
- return;
3513
- }
3514
- case "identity.getZaps": {
3515
- const result = {
3516
- type: "identity.getZaps.result",
3517
- id,
3518
- zaps: []
3519
- };
3520
- send(result);
3521
- return;
3522
- }
3523
- case "identity.getMutes": {
3524
- const result = {
3525
- type: "identity.getMutes.result",
3526
- id,
3527
- pubkeys: []
3528
- };
3529
- send(result);
3530
- return;
3531
- }
3532
- case "identity.getBlocked": {
3533
- const result = {
3534
- type: "identity.getBlocked.result",
3535
- id,
3536
- pubkeys: []
3537
- };
3538
- send(result);
3539
- return;
3540
- }
3541
- case "identity.getBadges": {
3542
- const result = {
3543
- type: "identity.getBadges.result",
3544
- id,
3545
- badges: []
3546
- };
3547
- send(result);
3548
- return;
3549
- }
3550
- default:
3551
- sendError(message.type, `Unknown identity method: ${message.type}`);
3552
- }
3621
+ handleIdentityServiceMessage(options, message, send);
3553
3622
  },
3554
3623
  // Identity service has no per-window state to clean up.
3555
3624
  onWindowDestroyed(_windowId) {