/* * Pure Web Push helper functions shared by the notifications UI script. * Kept dependency-free so the same file runs in the browser (window.PushUtils) * and under Node (module.exports) for unit tests. */ (function (root, factory) { const api = factory(); if (typeof module !== 'undefined' && module.exports) { module.exports = api; } if (root) { root.PushUtils = api; } }(typeof self !== 'undefined' ? self : this, function () { 'use strict'; /** * Convert a base64url VAPID public key into the Uint8Array the * PushManager.subscribe({ applicationServerKey }) API expects. */ function urlBase64ToUint8Array(base64String) { const padding = '='.repeat((4 - (base64String.length % 4)) % 4); const base64 = (base64String + padding) .replace(/-/g, '+') .replace(/_/g, '/'); const rawData = atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } return outputArray; } /** * Only allow notification click targets that resolve to the site's own * origin (never open arbitrary external URLs from push payloads). */ function isSameOriginUrl(url, siteOrigin) { try { const parsed = new URL(url, siteOrigin); return parsed.origin === siteOrigin; } catch (e) { return false; } } return { urlBase64ToUint8Array: urlBase64ToUint8Array, isSameOriginUrl: isSameOriginUrl, }; }));