@sparkvault/sdk-js 8.9.2 → 8.10.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.
- package/dist/index.d.ts +77 -1
- package/dist/notify/index.d.ts +38 -2
- package/dist/notify/types.d.ts +40 -0
- package/dist/sparkvault.cjs.js +126 -0
- package/dist/sparkvault.cjs.js.map +1 -1
- package/dist/sparkvault.esm.js +126 -0
- package/dist/sparkvault.esm.js.map +1 -1
- package/dist/sparkvault.js +1 -1
- package/dist/sparkvault.js.map +1 -1
- package/package.json +1 -1
package/dist/sparkvault.esm.js
CHANGED
|
@@ -13530,6 +13530,34 @@ class EntropyModule {
|
|
|
13530
13530
|
* device push registration is intentionally omitted (Expo-only on mobile).
|
|
13531
13531
|
*/
|
|
13532
13532
|
const MARK_STATES = ['seen', 'read', 'archived'];
|
|
13533
|
+
/**
|
|
13534
|
+
* Decode a base64url VAPID key into the Uint8Array `pushManager.subscribe`
|
|
13535
|
+
* requires for `applicationServerKey`. Standalone (no atob dependency on a
|
|
13536
|
+
* data: URL) so it works in every browser context.
|
|
13537
|
+
*/
|
|
13538
|
+
function urlBase64ToUint8Array(base64url) {
|
|
13539
|
+
const padding = '='.repeat((4 - (base64url.length % 4)) % 4);
|
|
13540
|
+
const base64 = (base64url + padding).replace(/-/g, '+').replace(/_/g, '/');
|
|
13541
|
+
const raw = atob(base64);
|
|
13542
|
+
const output = new Uint8Array(new ArrayBuffer(raw.length));
|
|
13543
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
13544
|
+
output[i] = raw.charCodeAt(i);
|
|
13545
|
+
}
|
|
13546
|
+
return output;
|
|
13547
|
+
}
|
|
13548
|
+
/** Normalize a PushSubscription (or its JSON) to the server's wire shape. */
|
|
13549
|
+
function toSubscriptionInput(subscription) {
|
|
13550
|
+
const json = typeof subscription.toJSON === 'function'
|
|
13551
|
+
? subscription.toJSON()
|
|
13552
|
+
: subscription;
|
|
13553
|
+
const endpoint = json.endpoint;
|
|
13554
|
+
const p256dh = json.keys?.p256dh;
|
|
13555
|
+
const auth = json.keys?.auth;
|
|
13556
|
+
if (!endpoint || !p256dh || !auth) {
|
|
13557
|
+
throw new ValidationError('A web push subscription requires endpoint and keys.p256dh/auth');
|
|
13558
|
+
}
|
|
13559
|
+
return { endpoint, keys: { p256dh, auth } };
|
|
13560
|
+
}
|
|
13533
13561
|
class NotifyModule {
|
|
13534
13562
|
constructor(http) {
|
|
13535
13563
|
this.http = http;
|
|
@@ -13591,6 +13619,104 @@ class NotifyModule {
|
|
|
13591
13619
|
const response = await this.http.post(`/v1/me/notifications/${encodeURIComponent(notificationId)}/state`, { created_at: options.createdAt, state: options.state });
|
|
13592
13620
|
return response.data;
|
|
13593
13621
|
}
|
|
13622
|
+
// ── Browser Web Push ──────────────────────────────────────────────────────
|
|
13623
|
+
/**
|
|
13624
|
+
* Whether this environment can do Web Push (service worker + Push API +
|
|
13625
|
+
* Notifications). Feature-detect before showing an enable-notifications UI.
|
|
13626
|
+
*/
|
|
13627
|
+
isWebPushSupported() {
|
|
13628
|
+
return (typeof window !== 'undefined' &&
|
|
13629
|
+
'serviceWorker' in navigator &&
|
|
13630
|
+
'PushManager' in window &&
|
|
13631
|
+
'Notification' in window);
|
|
13632
|
+
}
|
|
13633
|
+
/**
|
|
13634
|
+
* The platform VAPID public key for `pushManager.subscribe()`. `configured`
|
|
13635
|
+
* is false until the server-side VAPID keypair is provisioned — hide the
|
|
13636
|
+
* enable-notifications affordance while false.
|
|
13637
|
+
*/
|
|
13638
|
+
async getWebPushKey() {
|
|
13639
|
+
const response = await this.http.get('/v1/me/notifications/web-push/key');
|
|
13640
|
+
return response.data;
|
|
13641
|
+
}
|
|
13642
|
+
/**
|
|
13643
|
+
* Register (or refresh) a browser push subscription for the signed-in user.
|
|
13644
|
+
* Accepts a live PushSubscription or its JSON shape.
|
|
13645
|
+
*/
|
|
13646
|
+
async registerWebPushSubscription(subscription) {
|
|
13647
|
+
const input = toSubscriptionInput(subscription);
|
|
13648
|
+
const response = await this.http.post('/v1/me/notifications/web-push', input);
|
|
13649
|
+
return response.data;
|
|
13650
|
+
}
|
|
13651
|
+
/** Remove a browser push subscription for the signed-in user, by endpoint. */
|
|
13652
|
+
async unsubscribeWebPushEndpoint(endpoint) {
|
|
13653
|
+
if (!endpoint || typeof endpoint !== 'string') {
|
|
13654
|
+
throw new ValidationError('endpoint is required');
|
|
13655
|
+
}
|
|
13656
|
+
const response = await this.http.post('/v1/me/notifications/web-push/unsubscribe', { endpoint });
|
|
13657
|
+
return response.data;
|
|
13658
|
+
}
|
|
13659
|
+
/**
|
|
13660
|
+
* End-to-end enable ceremony: feature-detect, fetch the VAPID key, ensure
|
|
13661
|
+
* Notification permission, subscribe via the caller's service worker, and
|
|
13662
|
+
* register the subscription with the server. Returns a typed status instead
|
|
13663
|
+
* of throwing on the expected declines (unsupported / not configured /
|
|
13664
|
+
* permission denied) so a host can branch on the outcome. Call it from a user
|
|
13665
|
+
* gesture (permission prompts require one).
|
|
13666
|
+
*
|
|
13667
|
+
* @example
|
|
13668
|
+
* const reg = await navigator.serviceWorker.register('/sw.js');
|
|
13669
|
+
* const { status } = await sv.notify.subscribeWebPush({ serviceWorkerRegistration: reg });
|
|
13670
|
+
*/
|
|
13671
|
+
async subscribeWebPush(options) {
|
|
13672
|
+
if (!this.isWebPushSupported()) {
|
|
13673
|
+
return { status: 'not_supported' };
|
|
13674
|
+
}
|
|
13675
|
+
const registration = options?.serviceWorkerRegistration;
|
|
13676
|
+
if (!registration || typeof registration.pushManager?.subscribe !== 'function') {
|
|
13677
|
+
throw new ValidationError('serviceWorkerRegistration with a pushManager is required');
|
|
13678
|
+
}
|
|
13679
|
+
const key = await this.getWebPushKey();
|
|
13680
|
+
if (!key.configured || !key.vapid_public_key) {
|
|
13681
|
+
return { status: 'not_configured' };
|
|
13682
|
+
}
|
|
13683
|
+
if (Notification.permission === 'denied') {
|
|
13684
|
+
return { status: 'permission_denied' };
|
|
13685
|
+
}
|
|
13686
|
+
if (Notification.permission === 'default') {
|
|
13687
|
+
const permission = await Notification.requestPermission();
|
|
13688
|
+
if (permission !== 'granted') {
|
|
13689
|
+
return { status: 'permission_denied' };
|
|
13690
|
+
}
|
|
13691
|
+
}
|
|
13692
|
+
// Reuse an existing subscription if present; browsers return the same one,
|
|
13693
|
+
// but registering again refreshes the server TTL.
|
|
13694
|
+
const existing = await registration.pushManager.getSubscription();
|
|
13695
|
+
const subscription = existing ??
|
|
13696
|
+
(await registration.pushManager.subscribe({
|
|
13697
|
+
userVisibleOnly: true,
|
|
13698
|
+
applicationServerKey: urlBase64ToUint8Array(key.vapid_public_key),
|
|
13699
|
+
}));
|
|
13700
|
+
await this.registerWebPushSubscription(subscription);
|
|
13701
|
+
return { status: existing ? 'already_subscribed' : 'subscribed', subscription };
|
|
13702
|
+
}
|
|
13703
|
+
/**
|
|
13704
|
+
* Disable Web Push on this device: unsubscribe the browser subscription and
|
|
13705
|
+
* remove it server-side. Safe to call when not subscribed (returns false).
|
|
13706
|
+
*/
|
|
13707
|
+
async unsubscribeWebPush(options) {
|
|
13708
|
+
const registration = options?.serviceWorkerRegistration;
|
|
13709
|
+
if (!registration || typeof registration.pushManager?.getSubscription !== 'function') {
|
|
13710
|
+
return false;
|
|
13711
|
+
}
|
|
13712
|
+
const subscription = await registration.pushManager.getSubscription();
|
|
13713
|
+
if (!subscription) {
|
|
13714
|
+
return false;
|
|
13715
|
+
}
|
|
13716
|
+
// Remove server-side first (still authenticated), then drop it locally.
|
|
13717
|
+
await this.unsubscribeWebPushEndpoint(subscription.endpoint).catch(() => undefined);
|
|
13718
|
+
return subscription.unsubscribe();
|
|
13719
|
+
}
|
|
13594
13720
|
}
|
|
13595
13721
|
|
|
13596
13722
|
/**
|