@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/index.d.ts
CHANGED
|
@@ -1873,6 +1873,46 @@ interface MarkNotificationStateResponse {
|
|
|
1873
1873
|
state: NotificationMarkState;
|
|
1874
1874
|
updated_at: number;
|
|
1875
1875
|
}
|
|
1876
|
+
/** The platform VAPID public key needed for pushManager.subscribe(). */
|
|
1877
|
+
interface WebPushKeyResponse {
|
|
1878
|
+
/** False until the VAPID keypair is provisioned server-side — hide the UI. */
|
|
1879
|
+
configured: boolean;
|
|
1880
|
+
/** base64url VAPID application server key, or null when not configured. */
|
|
1881
|
+
vapid_public_key: string | null;
|
|
1882
|
+
/** The VAPID `sub` contact (mailto:/https), or null when not configured. */
|
|
1883
|
+
subject: string | null;
|
|
1884
|
+
}
|
|
1885
|
+
/** RFC 8030 subscription shape sent to the server (a PushSubscription's JSON). */
|
|
1886
|
+
interface WebPushSubscriptionInput {
|
|
1887
|
+
endpoint: string;
|
|
1888
|
+
keys: {
|
|
1889
|
+
p256dh: string;
|
|
1890
|
+
auth: string;
|
|
1891
|
+
};
|
|
1892
|
+
}
|
|
1893
|
+
interface RegisterWebPushResponse {
|
|
1894
|
+
success: boolean;
|
|
1895
|
+
created: boolean;
|
|
1896
|
+
refreshed: boolean;
|
|
1897
|
+
}
|
|
1898
|
+
interface UnsubscribeWebPushResponse {
|
|
1899
|
+
success: boolean;
|
|
1900
|
+
deleted: boolean;
|
|
1901
|
+
}
|
|
1902
|
+
/** Terminal outcomes of the end-to-end subscribeWebPush ceremony. */
|
|
1903
|
+
type SubscribeWebPushStatus = 'subscribed' | 'already_subscribed' | 'not_supported' | 'not_configured' | 'permission_denied';
|
|
1904
|
+
interface SubscribeWebPushOptions {
|
|
1905
|
+
/**
|
|
1906
|
+
* The caller's service-worker registration (the app owns the worker file and
|
|
1907
|
+
* its lifecycle). Web Push requires a service worker to receive push events.
|
|
1908
|
+
*/
|
|
1909
|
+
serviceWorkerRegistration: ServiceWorkerRegistration;
|
|
1910
|
+
}
|
|
1911
|
+
interface SubscribeWebPushResult {
|
|
1912
|
+
status: SubscribeWebPushStatus;
|
|
1913
|
+
/** The live PushSubscription on 'subscribed'/'already_subscribed'. */
|
|
1914
|
+
subscription?: PushSubscription;
|
|
1915
|
+
}
|
|
1876
1916
|
|
|
1877
1917
|
/**
|
|
1878
1918
|
* Notify Module
|
|
@@ -1912,6 +1952,42 @@ declare class NotifyModule {
|
|
|
1912
1952
|
* await sv.notify.markState('ntf_abc', { createdAt: row.created_at, state: 'read' });
|
|
1913
1953
|
*/
|
|
1914
1954
|
markState(notificationId: string, options: MarkNotificationStateOptions): Promise<MarkNotificationStateResponse>;
|
|
1955
|
+
/**
|
|
1956
|
+
* Whether this environment can do Web Push (service worker + Push API +
|
|
1957
|
+
* Notifications). Feature-detect before showing an enable-notifications UI.
|
|
1958
|
+
*/
|
|
1959
|
+
isWebPushSupported(): boolean;
|
|
1960
|
+
/**
|
|
1961
|
+
* The platform VAPID public key for `pushManager.subscribe()`. `configured`
|
|
1962
|
+
* is false until the server-side VAPID keypair is provisioned — hide the
|
|
1963
|
+
* enable-notifications affordance while false.
|
|
1964
|
+
*/
|
|
1965
|
+
getWebPushKey(): Promise<WebPushKeyResponse>;
|
|
1966
|
+
/**
|
|
1967
|
+
* Register (or refresh) a browser push subscription for the signed-in user.
|
|
1968
|
+
* Accepts a live PushSubscription or its JSON shape.
|
|
1969
|
+
*/
|
|
1970
|
+
registerWebPushSubscription(subscription: PushSubscription | WebPushSubscriptionInput): Promise<RegisterWebPushResponse>;
|
|
1971
|
+
/** Remove a browser push subscription for the signed-in user, by endpoint. */
|
|
1972
|
+
unsubscribeWebPushEndpoint(endpoint: string): Promise<UnsubscribeWebPushResponse>;
|
|
1973
|
+
/**
|
|
1974
|
+
* End-to-end enable ceremony: feature-detect, fetch the VAPID key, ensure
|
|
1975
|
+
* Notification permission, subscribe via the caller's service worker, and
|
|
1976
|
+
* register the subscription with the server. Returns a typed status instead
|
|
1977
|
+
* of throwing on the expected declines (unsupported / not configured /
|
|
1978
|
+
* permission denied) so a host can branch on the outcome. Call it from a user
|
|
1979
|
+
* gesture (permission prompts require one).
|
|
1980
|
+
*
|
|
1981
|
+
* @example
|
|
1982
|
+
* const reg = await navigator.serviceWorker.register('/sw.js');
|
|
1983
|
+
* const { status } = await sv.notify.subscribeWebPush({ serviceWorkerRegistration: reg });
|
|
1984
|
+
*/
|
|
1985
|
+
subscribeWebPush(options: SubscribeWebPushOptions): Promise<SubscribeWebPushResult>;
|
|
1986
|
+
/**
|
|
1987
|
+
* Disable Web Push on this device: unsubscribe the browser subscription and
|
|
1988
|
+
* remove it server-side. Safe to call when not subscribed (returns false).
|
|
1989
|
+
*/
|
|
1990
|
+
unsubscribeWebPush(options: SubscribeWebPushOptions): Promise<boolean>;
|
|
1915
1991
|
}
|
|
1916
1992
|
|
|
1917
1993
|
/**
|
|
@@ -2037,4 +2113,4 @@ declare global {
|
|
|
2037
2113
|
}
|
|
2038
2114
|
|
|
2039
2115
|
export { AuthenticationError, AuthorizationError, EntropyModule, FoldersModule, IdentityModule, IngotsModule, NetworkError, NotifyModule, PlanRequiredError, PopupBlockedError, ProductsModule, QuotaExceededError, SparkVault, SparkVaultError, SparksModule, TimeoutError, UserCancelledError, ValidationError, VaultsModule, SparkVault as default, logger, setDebugMode };
|
|
2040
|
-
export type { AccessLogRetention, AccessLogsOptions, ApiResponse, AuthMethod, BandwidthPool, CreateFolderOptions, CreateSparkOptions, CreateUploadOptions, CreateVaultOptions, CreatedIngot, CreatedSpark, EntropyFormat, EntropyResult, Folder, FolderBreadcrumbItem, GenerateEntropyOptions, HealthCheckOptions, HealthCheckResult, AttachOptions as IdentityAttachOptions, Ingot, IngotAccessLog, IngotAccessLogPage, IngotDownloadLink, IngotInvite, IngotPage, IngotSharingConfig, IngotUpload, ListContentsOptions, ListIngotsOptions, ListNotificationsOptions, ListNotificationsResponse, ListSparksOptions, MarkNotificationStateOptions, MarkNotificationStateResponse, NotificationInboxState, NotificationMarkState, NotificationRow, PoolState, PoolsMeta, QuotaResource, ReplaceIngotOptions, ResponseMeta, SearchIngotsOptions, ShareSparkOptions, Spark, SparkPage, SparkShare, SparkSummary, SparkVaultConfig, SparkVisibility, StoragePool, Theme, TokenClaims, UnreadCountResponse, UnsealedVault, UpdateFolderOptions, UpdateIngotSharingOptions, UpdateVaultUploadOptions, UpdatedVault, UploadAttachOptions, UploadBranding, UploadIngotOptions, UploadOptions, UploadProgress, UploadResult, Vault, VaultDetail, VaultSharingConfig, VaultSharingOptions, VaultSummary, VaultUploadConfig, VaultUploadSettings, VerifyOptions, VerifyResult };
|
|
2116
|
+
export type { AccessLogRetention, AccessLogsOptions, ApiResponse, AuthMethod, BandwidthPool, CreateFolderOptions, CreateSparkOptions, CreateUploadOptions, CreateVaultOptions, CreatedIngot, CreatedSpark, EntropyFormat, EntropyResult, Folder, FolderBreadcrumbItem, GenerateEntropyOptions, HealthCheckOptions, HealthCheckResult, AttachOptions as IdentityAttachOptions, Ingot, IngotAccessLog, IngotAccessLogPage, IngotDownloadLink, IngotInvite, IngotPage, IngotSharingConfig, IngotUpload, ListContentsOptions, ListIngotsOptions, ListNotificationsOptions, ListNotificationsResponse, ListSparksOptions, MarkNotificationStateOptions, MarkNotificationStateResponse, NotificationInboxState, NotificationMarkState, NotificationRow, PoolState, PoolsMeta, QuotaResource, RegisterWebPushResponse, ReplaceIngotOptions, ResponseMeta, SearchIngotsOptions, ShareSparkOptions, Spark, SparkPage, SparkShare, SparkSummary, SparkVaultConfig, SparkVisibility, StoragePool, SubscribeWebPushOptions, SubscribeWebPushResult, SubscribeWebPushStatus, Theme, TokenClaims, UnreadCountResponse, UnsealedVault, UnsubscribeWebPushResponse, UpdateFolderOptions, UpdateIngotSharingOptions, UpdateVaultUploadOptions, UpdatedVault, UploadAttachOptions, UploadBranding, UploadIngotOptions, UploadOptions, UploadProgress, UploadResult, Vault, VaultDetail, VaultSharingConfig, VaultSharingOptions, VaultSummary, VaultUploadConfig, VaultUploadSettings, VerifyOptions, VerifyResult, WebPushKeyResponse, WebPushSubscriptionInput };
|
package/dist/notify/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* device push registration is intentionally omitted (Expo-only on mobile).
|
|
9
9
|
*/
|
|
10
10
|
import { HttpClient } from '../http';
|
|
11
|
-
import type { ListNotificationsOptions, ListNotificationsResponse, UnreadCountResponse, MarkNotificationStateOptions, MarkNotificationStateResponse } from './types';
|
|
11
|
+
import type { ListNotificationsOptions, ListNotificationsResponse, UnreadCountResponse, MarkNotificationStateOptions, MarkNotificationStateResponse, WebPushKeyResponse, WebPushSubscriptionInput, RegisterWebPushResponse, UnsubscribeWebPushResponse, SubscribeWebPushOptions, SubscribeWebPushResult } from './types';
|
|
12
12
|
export declare class NotifyModule {
|
|
13
13
|
private readonly http;
|
|
14
14
|
constructor(http: HttpClient);
|
|
@@ -37,5 +37,41 @@ export declare class NotifyModule {
|
|
|
37
37
|
* await sv.notify.markState('ntf_abc', { createdAt: row.created_at, state: 'read' });
|
|
38
38
|
*/
|
|
39
39
|
markState(notificationId: string, options: MarkNotificationStateOptions): Promise<MarkNotificationStateResponse>;
|
|
40
|
+
/**
|
|
41
|
+
* Whether this environment can do Web Push (service worker + Push API +
|
|
42
|
+
* Notifications). Feature-detect before showing an enable-notifications UI.
|
|
43
|
+
*/
|
|
44
|
+
isWebPushSupported(): boolean;
|
|
45
|
+
/**
|
|
46
|
+
* The platform VAPID public key for `pushManager.subscribe()`. `configured`
|
|
47
|
+
* is false until the server-side VAPID keypair is provisioned — hide the
|
|
48
|
+
* enable-notifications affordance while false.
|
|
49
|
+
*/
|
|
50
|
+
getWebPushKey(): Promise<WebPushKeyResponse>;
|
|
51
|
+
/**
|
|
52
|
+
* Register (or refresh) a browser push subscription for the signed-in user.
|
|
53
|
+
* Accepts a live PushSubscription or its JSON shape.
|
|
54
|
+
*/
|
|
55
|
+
registerWebPushSubscription(subscription: PushSubscription | WebPushSubscriptionInput): Promise<RegisterWebPushResponse>;
|
|
56
|
+
/** Remove a browser push subscription for the signed-in user, by endpoint. */
|
|
57
|
+
unsubscribeWebPushEndpoint(endpoint: string): Promise<UnsubscribeWebPushResponse>;
|
|
58
|
+
/**
|
|
59
|
+
* End-to-end enable ceremony: feature-detect, fetch the VAPID key, ensure
|
|
60
|
+
* Notification permission, subscribe via the caller's service worker, and
|
|
61
|
+
* register the subscription with the server. Returns a typed status instead
|
|
62
|
+
* of throwing on the expected declines (unsupported / not configured /
|
|
63
|
+
* permission denied) so a host can branch on the outcome. Call it from a user
|
|
64
|
+
* gesture (permission prompts require one).
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* const reg = await navigator.serviceWorker.register('/sw.js');
|
|
68
|
+
* const { status } = await sv.notify.subscribeWebPush({ serviceWorkerRegistration: reg });
|
|
69
|
+
*/
|
|
70
|
+
subscribeWebPush(options: SubscribeWebPushOptions): Promise<SubscribeWebPushResult>;
|
|
71
|
+
/**
|
|
72
|
+
* Disable Web Push on this device: unsubscribe the browser subscription and
|
|
73
|
+
* remove it server-side. Safe to call when not subscribed (returns false).
|
|
74
|
+
*/
|
|
75
|
+
unsubscribeWebPush(options: SubscribeWebPushOptions): Promise<boolean>;
|
|
40
76
|
}
|
|
41
|
-
export type { NotificationInboxState, NotificationMarkState, NotificationRow, ListNotificationsOptions, ListNotificationsResponse, UnreadCountResponse, MarkNotificationStateOptions, MarkNotificationStateResponse, } from './types';
|
|
77
|
+
export type { NotificationInboxState, NotificationMarkState, NotificationRow, ListNotificationsOptions, ListNotificationsResponse, UnreadCountResponse, MarkNotificationStateOptions, MarkNotificationStateResponse, WebPushKeyResponse, WebPushSubscriptionInput, RegisterWebPushResponse, UnsubscribeWebPushResponse, SubscribeWebPushStatus, SubscribeWebPushOptions, SubscribeWebPushResult, } from './types';
|
package/dist/notify/types.d.ts
CHANGED
|
@@ -63,3 +63,43 @@ export interface MarkNotificationStateResponse {
|
|
|
63
63
|
state: NotificationMarkState;
|
|
64
64
|
updated_at: number;
|
|
65
65
|
}
|
|
66
|
+
/** The platform VAPID public key needed for pushManager.subscribe(). */
|
|
67
|
+
export interface WebPushKeyResponse {
|
|
68
|
+
/** False until the VAPID keypair is provisioned server-side — hide the UI. */
|
|
69
|
+
configured: boolean;
|
|
70
|
+
/** base64url VAPID application server key, or null when not configured. */
|
|
71
|
+
vapid_public_key: string | null;
|
|
72
|
+
/** The VAPID `sub` contact (mailto:/https), or null when not configured. */
|
|
73
|
+
subject: string | null;
|
|
74
|
+
}
|
|
75
|
+
/** RFC 8030 subscription shape sent to the server (a PushSubscription's JSON). */
|
|
76
|
+
export interface WebPushSubscriptionInput {
|
|
77
|
+
endpoint: string;
|
|
78
|
+
keys: {
|
|
79
|
+
p256dh: string;
|
|
80
|
+
auth: string;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export interface RegisterWebPushResponse {
|
|
84
|
+
success: boolean;
|
|
85
|
+
created: boolean;
|
|
86
|
+
refreshed: boolean;
|
|
87
|
+
}
|
|
88
|
+
export interface UnsubscribeWebPushResponse {
|
|
89
|
+
success: boolean;
|
|
90
|
+
deleted: boolean;
|
|
91
|
+
}
|
|
92
|
+
/** Terminal outcomes of the end-to-end subscribeWebPush ceremony. */
|
|
93
|
+
export type SubscribeWebPushStatus = 'subscribed' | 'already_subscribed' | 'not_supported' | 'not_configured' | 'permission_denied';
|
|
94
|
+
export interface SubscribeWebPushOptions {
|
|
95
|
+
/**
|
|
96
|
+
* The caller's service-worker registration (the app owns the worker file and
|
|
97
|
+
* its lifecycle). Web Push requires a service worker to receive push events.
|
|
98
|
+
*/
|
|
99
|
+
serviceWorkerRegistration: ServiceWorkerRegistration;
|
|
100
|
+
}
|
|
101
|
+
export interface SubscribeWebPushResult {
|
|
102
|
+
status: SubscribeWebPushStatus;
|
|
103
|
+
/** The live PushSubscription on 'subscribed'/'already_subscribed'. */
|
|
104
|
+
subscription?: PushSubscription;
|
|
105
|
+
}
|
package/dist/sparkvault.cjs.js
CHANGED
|
@@ -13534,6 +13534,34 @@ class EntropyModule {
|
|
|
13534
13534
|
* device push registration is intentionally omitted (Expo-only on mobile).
|
|
13535
13535
|
*/
|
|
13536
13536
|
const MARK_STATES = ['seen', 'read', 'archived'];
|
|
13537
|
+
/**
|
|
13538
|
+
* Decode a base64url VAPID key into the Uint8Array `pushManager.subscribe`
|
|
13539
|
+
* requires for `applicationServerKey`. Standalone (no atob dependency on a
|
|
13540
|
+
* data: URL) so it works in every browser context.
|
|
13541
|
+
*/
|
|
13542
|
+
function urlBase64ToUint8Array(base64url) {
|
|
13543
|
+
const padding = '='.repeat((4 - (base64url.length % 4)) % 4);
|
|
13544
|
+
const base64 = (base64url + padding).replace(/-/g, '+').replace(/_/g, '/');
|
|
13545
|
+
const raw = atob(base64);
|
|
13546
|
+
const output = new Uint8Array(new ArrayBuffer(raw.length));
|
|
13547
|
+
for (let i = 0; i < raw.length; i += 1) {
|
|
13548
|
+
output[i] = raw.charCodeAt(i);
|
|
13549
|
+
}
|
|
13550
|
+
return output;
|
|
13551
|
+
}
|
|
13552
|
+
/** Normalize a PushSubscription (or its JSON) to the server's wire shape. */
|
|
13553
|
+
function toSubscriptionInput(subscription) {
|
|
13554
|
+
const json = typeof subscription.toJSON === 'function'
|
|
13555
|
+
? subscription.toJSON()
|
|
13556
|
+
: subscription;
|
|
13557
|
+
const endpoint = json.endpoint;
|
|
13558
|
+
const p256dh = json.keys?.p256dh;
|
|
13559
|
+
const auth = json.keys?.auth;
|
|
13560
|
+
if (!endpoint || !p256dh || !auth) {
|
|
13561
|
+
throw new ValidationError('A web push subscription requires endpoint and keys.p256dh/auth');
|
|
13562
|
+
}
|
|
13563
|
+
return { endpoint, keys: { p256dh, auth } };
|
|
13564
|
+
}
|
|
13537
13565
|
class NotifyModule {
|
|
13538
13566
|
constructor(http) {
|
|
13539
13567
|
this.http = http;
|
|
@@ -13595,6 +13623,104 @@ class NotifyModule {
|
|
|
13595
13623
|
const response = await this.http.post(`/v1/me/notifications/${encodeURIComponent(notificationId)}/state`, { created_at: options.createdAt, state: options.state });
|
|
13596
13624
|
return response.data;
|
|
13597
13625
|
}
|
|
13626
|
+
// ── Browser Web Push ──────────────────────────────────────────────────────
|
|
13627
|
+
/**
|
|
13628
|
+
* Whether this environment can do Web Push (service worker + Push API +
|
|
13629
|
+
* Notifications). Feature-detect before showing an enable-notifications UI.
|
|
13630
|
+
*/
|
|
13631
|
+
isWebPushSupported() {
|
|
13632
|
+
return (typeof window !== 'undefined' &&
|
|
13633
|
+
'serviceWorker' in navigator &&
|
|
13634
|
+
'PushManager' in window &&
|
|
13635
|
+
'Notification' in window);
|
|
13636
|
+
}
|
|
13637
|
+
/**
|
|
13638
|
+
* The platform VAPID public key for `pushManager.subscribe()`. `configured`
|
|
13639
|
+
* is false until the server-side VAPID keypair is provisioned — hide the
|
|
13640
|
+
* enable-notifications affordance while false.
|
|
13641
|
+
*/
|
|
13642
|
+
async getWebPushKey() {
|
|
13643
|
+
const response = await this.http.get('/v1/me/notifications/web-push/key');
|
|
13644
|
+
return response.data;
|
|
13645
|
+
}
|
|
13646
|
+
/**
|
|
13647
|
+
* Register (or refresh) a browser push subscription for the signed-in user.
|
|
13648
|
+
* Accepts a live PushSubscription or its JSON shape.
|
|
13649
|
+
*/
|
|
13650
|
+
async registerWebPushSubscription(subscription) {
|
|
13651
|
+
const input = toSubscriptionInput(subscription);
|
|
13652
|
+
const response = await this.http.post('/v1/me/notifications/web-push', input);
|
|
13653
|
+
return response.data;
|
|
13654
|
+
}
|
|
13655
|
+
/** Remove a browser push subscription for the signed-in user, by endpoint. */
|
|
13656
|
+
async unsubscribeWebPushEndpoint(endpoint) {
|
|
13657
|
+
if (!endpoint || typeof endpoint !== 'string') {
|
|
13658
|
+
throw new ValidationError('endpoint is required');
|
|
13659
|
+
}
|
|
13660
|
+
const response = await this.http.post('/v1/me/notifications/web-push/unsubscribe', { endpoint });
|
|
13661
|
+
return response.data;
|
|
13662
|
+
}
|
|
13663
|
+
/**
|
|
13664
|
+
* End-to-end enable ceremony: feature-detect, fetch the VAPID key, ensure
|
|
13665
|
+
* Notification permission, subscribe via the caller's service worker, and
|
|
13666
|
+
* register the subscription with the server. Returns a typed status instead
|
|
13667
|
+
* of throwing on the expected declines (unsupported / not configured /
|
|
13668
|
+
* permission denied) so a host can branch on the outcome. Call it from a user
|
|
13669
|
+
* gesture (permission prompts require one).
|
|
13670
|
+
*
|
|
13671
|
+
* @example
|
|
13672
|
+
* const reg = await navigator.serviceWorker.register('/sw.js');
|
|
13673
|
+
* const { status } = await sv.notify.subscribeWebPush({ serviceWorkerRegistration: reg });
|
|
13674
|
+
*/
|
|
13675
|
+
async subscribeWebPush(options) {
|
|
13676
|
+
if (!this.isWebPushSupported()) {
|
|
13677
|
+
return { status: 'not_supported' };
|
|
13678
|
+
}
|
|
13679
|
+
const registration = options?.serviceWorkerRegistration;
|
|
13680
|
+
if (!registration || typeof registration.pushManager?.subscribe !== 'function') {
|
|
13681
|
+
throw new ValidationError('serviceWorkerRegistration with a pushManager is required');
|
|
13682
|
+
}
|
|
13683
|
+
const key = await this.getWebPushKey();
|
|
13684
|
+
if (!key.configured || !key.vapid_public_key) {
|
|
13685
|
+
return { status: 'not_configured' };
|
|
13686
|
+
}
|
|
13687
|
+
if (Notification.permission === 'denied') {
|
|
13688
|
+
return { status: 'permission_denied' };
|
|
13689
|
+
}
|
|
13690
|
+
if (Notification.permission === 'default') {
|
|
13691
|
+
const permission = await Notification.requestPermission();
|
|
13692
|
+
if (permission !== 'granted') {
|
|
13693
|
+
return { status: 'permission_denied' };
|
|
13694
|
+
}
|
|
13695
|
+
}
|
|
13696
|
+
// Reuse an existing subscription if present; browsers return the same one,
|
|
13697
|
+
// but registering again refreshes the server TTL.
|
|
13698
|
+
const existing = await registration.pushManager.getSubscription();
|
|
13699
|
+
const subscription = existing ??
|
|
13700
|
+
(await registration.pushManager.subscribe({
|
|
13701
|
+
userVisibleOnly: true,
|
|
13702
|
+
applicationServerKey: urlBase64ToUint8Array(key.vapid_public_key),
|
|
13703
|
+
}));
|
|
13704
|
+
await this.registerWebPushSubscription(subscription);
|
|
13705
|
+
return { status: existing ? 'already_subscribed' : 'subscribed', subscription };
|
|
13706
|
+
}
|
|
13707
|
+
/**
|
|
13708
|
+
* Disable Web Push on this device: unsubscribe the browser subscription and
|
|
13709
|
+
* remove it server-side. Safe to call when not subscribed (returns false).
|
|
13710
|
+
*/
|
|
13711
|
+
async unsubscribeWebPush(options) {
|
|
13712
|
+
const registration = options?.serviceWorkerRegistration;
|
|
13713
|
+
if (!registration || typeof registration.pushManager?.getSubscription !== 'function') {
|
|
13714
|
+
return false;
|
|
13715
|
+
}
|
|
13716
|
+
const subscription = await registration.pushManager.getSubscription();
|
|
13717
|
+
if (!subscription) {
|
|
13718
|
+
return false;
|
|
13719
|
+
}
|
|
13720
|
+
// Remove server-side first (still authenticated), then drop it locally.
|
|
13721
|
+
await this.unsubscribeWebPushEndpoint(subscription.endpoint).catch(() => undefined);
|
|
13722
|
+
return subscription.unsubscribe();
|
|
13723
|
+
}
|
|
13598
13724
|
}
|
|
13599
13725
|
|
|
13600
13726
|
/**
|