ST-cmovs7ouz: lib/push-client.ts client-side push helpers + stub actions/push.ts
Client-side helpers: isPushSupported, isIOSSafari, isStandalonePWA, urlBase64ToUint8Array, subscribeToPush, unsubscribeFromPush. Stub actions/push.ts zodat imports resolven (implementatie volgt in volgende taak). Unit tests voor urlBase64ToUint8Array. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
880a3097af
commit
ba298a0ba6
3 changed files with 96 additions and 0 deletions
35
__tests__/lib/push-client.test.ts
Normal file
35
__tests__/lib/push-client.test.ts
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
import { describe, it, expect, vi } from 'vitest'
|
||||
|
||||
vi.mock('@/actions/push', () => ({
|
||||
subscribeToPushAction: vi.fn(),
|
||||
unsubscribeFromPushAction: vi.fn(),
|
||||
}))
|
||||
|
||||
import { urlBase64ToUint8Array } from '@/lib/push-client'
|
||||
|
||||
describe('urlBase64ToUint8Array', () => {
|
||||
it('converts a base64url-encoded VAPID public key to Uint8Array', () => {
|
||||
// 65-byte uncompressed EC public key encoded as base64url (no padding)
|
||||
const base64url = 'BNMxB-LJm6XvGGiJSsYLdumcYiM7q9s_1aM9i5lI8lVzZ7GYJw1QkQFmrknwFsI4dI-e1iyvUhYHjNpHJKJD3oc'
|
||||
const result = urlBase64ToUint8Array(base64url)
|
||||
expect(result).toBeInstanceOf(Uint8Array)
|
||||
expect(result.length).toBe(65)
|
||||
expect(result[0]).toBe(0x04) // uncompressed EC point prefix
|
||||
})
|
||||
|
||||
it('handles base64url with padding', () => {
|
||||
// simple known vector: "hello" = aGVsbG8= in base64
|
||||
const result = urlBase64ToUint8Array('aGVsbG8')
|
||||
expect(result).toBeInstanceOf(Uint8Array)
|
||||
expect(Array.from(result)).toEqual([104, 101, 108, 108, 111]) // "hello"
|
||||
})
|
||||
|
||||
it('converts - and _ characters correctly', () => {
|
||||
// base64url uses - and _ instead of + and /
|
||||
const base64standard = 'AB+/AA=='
|
||||
const base64url = 'AB-_AA'
|
||||
const fromStd = urlBase64ToUint8Array(base64standard)
|
||||
const fromUrl = urlBase64ToUint8Array(base64url)
|
||||
expect(Array.from(fromStd)).toEqual(Array.from(fromUrl))
|
||||
})
|
||||
})
|
||||
11
actions/push.ts
Normal file
11
actions/push.ts
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
'use server'
|
||||
|
||||
// Stub — full implementation added by ST-cmovs7t590009 (subscribeToPushAction + unsubscribeFromPushAction)
|
||||
|
||||
export async function subscribeToPushAction(_sub: unknown): Promise<void> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
|
||||
export async function unsubscribeFromPushAction(_args: { endpoint: string }): Promise<void> {
|
||||
throw new Error('Not implemented')
|
||||
}
|
||||
50
lib/push-client.ts
Normal file
50
lib/push-client.ts
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import { subscribeToPushAction, unsubscribeFromPushAction } from '@/actions/push'
|
||||
|
||||
export function isPushSupported(): boolean {
|
||||
return typeof window !== 'undefined' &&
|
||||
'serviceWorker' in navigator &&
|
||||
'PushManager' in window
|
||||
}
|
||||
|
||||
export function isIOSSafari(): boolean {
|
||||
if (typeof window === 'undefined') return false
|
||||
const ua = navigator.userAgent
|
||||
return /iPhone|iPad/.test(ua) && !/CriOS|FxiOS/.test(ua)
|
||||
}
|
||||
|
||||
export function isStandalonePWA(): boolean {
|
||||
if (typeof window === 'undefined') return false
|
||||
return (
|
||||
window.matchMedia('(display-mode: standalone)').matches ||
|
||||
!!(navigator as Navigator & { standalone?: boolean }).standalone
|
||||
)
|
||||
}
|
||||
|
||||
export function urlBase64ToUint8Array(base64: string): Uint8Array<ArrayBuffer> {
|
||||
const padding = '='.repeat((4 - (base64.length % 4)) % 4)
|
||||
const base64Std = (base64 + padding).replace(/-/g, '+').replace(/_/g, '/')
|
||||
const rawData = atob(base64Std)
|
||||
const buf = new Uint8Array(rawData.length)
|
||||
for (let i = 0; i < rawData.length; i++) buf[i] = rawData.charCodeAt(i)
|
||||
return buf
|
||||
}
|
||||
|
||||
export async function subscribeToPush(publicKey: string): Promise<PushSubscription> {
|
||||
const reg = await navigator.serviceWorker.register('/sw.js', { updateViaCache: 'none' })
|
||||
await navigator.serviceWorker.ready
|
||||
const sub = await reg.pushManager.subscribe({
|
||||
userVisibleOnly: true,
|
||||
applicationServerKey: urlBase64ToUint8Array(publicKey),
|
||||
})
|
||||
await subscribeToPushAction(sub.toJSON() as Parameters<typeof subscribeToPushAction>[0])
|
||||
return sub
|
||||
}
|
||||
|
||||
export async function unsubscribeFromPush(): Promise<void> {
|
||||
const reg = await navigator.serviceWorker.getRegistration()
|
||||
const sub = await reg?.pushManager.getSubscription()
|
||||
if (sub) {
|
||||
await sub.unsubscribe()
|
||||
await unsubscribeFromPushAction({ endpoint: sub.endpoint })
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue