notificare-web
Version:
The official Notificare JS library.
1,187 lines (1,147 loc) • 42.6 kB
JavaScript
;
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
LogLevel[LogLevel["INFO"] = 1] = "INFO";
LogLevel[LogLevel["WARNING"] = 2] = "WARNING";
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
})(LogLevel || (LogLevel = {}));
class Logger {
constructor(name, options) {
this.logLevel = LogLevel.INFO;
this.name = name;
this.options = Object.assign(Object.assign({}, defaultLoggingOptions), options);
}
getLogLevel() {
return this.logLevel;
}
setLogLevel(logLevel) {
if (typeof logLevel === 'string') {
this.logLevel = logLevelStringConverter[logLevel];
return;
}
this.logLevel = logLevel;
}
debug(...args) {
this.log(LogLevel.DEBUG, ...args);
}
info(...args) {
this.log(LogLevel.INFO, ...args);
}
warning(...args) {
this.log(LogLevel.WARNING, ...args);
}
error(...args) {
this.log(LogLevel.ERROR, ...args);
}
log(logLevel, ...args) {
if (logLevel < this.logLevel)
return;
const method = consoleMethodConverter[logLevel];
const groupBadge = this.getGroupBadge(logLevel);
const messagePrefix = this.getMessagePrefix(logLevel);
// eslint-disable-next-line no-console
console[method](...groupBadge, messagePrefix, ...args);
}
getGroupBadge(logLevel) {
const styles = [
`background: ${consoleMethodColor[logLevel]}`,
'border-radius: 0.5em',
'color: white',
'font-weight: bold',
'padding: 2px 0.5em',
];
const { group } = this.options;
if (!group)
return [];
return [`%c${group}`, styles.join(';')];
}
getMessagePrefix(logLevel) {
const parts = [];
if (this.options.includeTimestamp) {
const now = new Date().toISOString();
parts.push(`[${now}]`);
}
if (this.options.includeName) {
parts.push(`[${this.name}]`);
}
if (this.options.includeLogLevel ||
(this.options.includeLogLevel === undefined && logLevel < LogLevel.INFO)) {
parts.push(`[${logLevelToStringConverter[logLevel]}]`);
}
return parts.join(' ');
}
}
const defaultLoggingOptions = {
includeName: true,
includeTimestamp: true,
};
const logLevelStringConverter = {
debug: LogLevel.DEBUG,
info: LogLevel.INFO,
warning: LogLevel.WARNING,
error: LogLevel.ERROR,
};
const logLevelToStringConverter = {
[LogLevel.DEBUG]: 'debug',
[LogLevel.INFO]: 'info',
[LogLevel.WARNING]: 'warning',
[LogLevel.ERROR]: 'error',
};
const consoleMethodConverter = {
[LogLevel.DEBUG]: 'log',
[LogLevel.INFO]: 'info',
[LogLevel.WARNING]: 'warn',
[LogLevel.ERROR]: 'error',
};
const consoleMethodColor = {
[LogLevel.DEBUG]: '#7F8C8D',
[LogLevel.INFO]: '#3498DB',
[LogLevel.WARNING]: '#F39C12',
[LogLevel.ERROR]: '#C0392B',
};
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise, SuppressedError, Symbol */
function __rest(s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
}
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
var e = new Error(message);
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
};
const logger$1 = new Logger('notificare/network');
function sleep$1(milliseconds) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}
function base64Encode(data) {
// eslint-disable-next-line no-restricted-globals
return self.btoa(data);
}
class NotificareNetworkRequestError extends Error {
constructor(response) {
super(`Failed to fetch a resource with response code '${response.status}'.`);
this.response = response;
Object.setPrototypeOf(this, NotificareNetworkRequestError.prototype);
}
}
async function request(params) {
var _a;
const { url, method = 'GET' } = params;
logger$1.debug(`${method} ${url}`);
const retries = (_a = params.retries) !== null && _a !== void 0 ? _a : defaults.retries;
for (let attempt = 0; attempt <= retries; attempt += 1) {
let response;
try {
// eslint-disable-next-line no-await-in-loop
response = await fetch(url, {
method,
body: getRequestBody(params),
headers: getRequestHeaders(params),
});
// The request completed successfully and the response is OK.
// Otherwise, don't retry and throw immediately.
if (response.ok)
return response;
}
catch (e) {
logger$1.warning(`Request attempt #${attempt + 1} failed.`, e);
if (attempt < retries) {
const delay = calculateRetryDelayInMilliseconds(attempt, params);
logger$1.debug(`Retrying in ${delay} milliseconds...`);
// eslint-disable-next-line no-await-in-loop
await sleep$1(delay);
}
}
// Having a response after the try catch means the request completed successfully
// but there's something wrong with it (ie bad request).
if (response)
throw new NotificareNetworkRequestError(response);
}
logger$1.error('Request exceeded maximum retries.');
throw new Error('Request exceeded maximum retries.');
}
function getRequestHeaders(params) {
const headers = new Headers();
headers.set('Accept', 'application/json');
if (params.body)
headers.set('Content-Type', 'application/json');
// if (params.formData) headers.set('Content-Type', 'multipart/form-data');
const authorizationHeader = getAuthorizationHeader(params);
if (authorizationHeader)
headers.set('Authorization', authorizationHeader);
return headers;
}
function getAuthorizationHeader({ authorization }) {
if (!authorization)
return undefined;
const { basic } = authorization;
if (basic) {
const encoded = base64Encode(`${basic.username}:${basic.password}`);
return `Basic ${encoded}`;
}
const { bearer } = authorization;
if (bearer) {
return `Bearer ${bearer}`;
}
return undefined;
}
function getRequestBody(params) {
if (params.body)
return JSON.stringify(params.body);
if (params.formData)
return params.formData;
return undefined;
}
function calculateRetryDelayInMilliseconds(attempt, params) {
var _a;
const retryDelay = (_a = params.retryDelay) !== null && _a !== void 0 ? _a : defaults.retryDelay;
if (typeof retryDelay === 'function') {
return retryDelay(attempt);
}
return retryDelay;
}
const defaults = {
retries: 3,
retryDelay: (attempt) => 2 ** attempt * 1000, // 1000, 2000, 4000
};
async function cloudRequest(params) {
const { environment, path, searchParams } = params, rest = __rest(params, ["environment", "path", "searchParams"]);
const url = getCloudUrl(environment);
url.pathname = path;
searchParams === null || searchParams === void 0 ? void 0 : searchParams.forEach((value, key) => {
url.searchParams.set(key, value);
});
return request(Object.assign(Object.assign({}, rest), { url, authorization: getCloudRequestAuthorization(environment) }));
}
function getCloudUrl({ cloudHost }) {
return new URL(`https://${cloudHost}`);
}
function getCloudRequestAuthorization({ applicationKey, applicationSecret, }) {
return {
basic: {
username: applicationKey,
password: applicationSecret,
},
};
}
async function fetchCloudApplication(params) {
const { language } = params, rest = __rest(params, ["language"]);
const searchParams = new URLSearchParams();
if (language)
searchParams.set('language', language);
const response = await cloudRequest(Object.assign(Object.assign({}, rest), { path: '/api/application/info', searchParams }));
return response.json();
}
async function fetchCloudDeviceInbox(params) {
var _a, _b;
const { deviceId, skip, limit, since } = params, rest = __rest(params, ["deviceId", "skip", "limit", "since"]);
const searchParams = new URLSearchParams({
skip: (_a = skip === null || skip === void 0 ? void 0 : skip.toString()) !== null && _a !== void 0 ? _a : '0',
limit: (_b = limit === null || limit === void 0 ? void 0 : limit.toString()) !== null && _b !== void 0 ? _b : '100',
});
if (since)
searchParams.set('since', since);
const response = await cloudRequest(Object.assign(Object.assign({}, rest), { path: `/api/notification/inbox/fordevice/${encodeURIComponent(deviceId)}`, searchParams }));
return response.json();
}
async function fetchCloudDynamicLink(params) {
const { deviceId, url } = params, rest = __rest(params, ["deviceId", "url"]);
const searchParams = new URLSearchParams({ platform: 'Web' });
if (deviceId)
searchParams.set('deviceID', deviceId);
const response = await cloudRequest(Object.assign(Object.assign({}, rest), { path: `/api/link/dynamic/${encodeURIComponent(url)}`, searchParams }));
return response.json();
}
async function createCloudEvent(params) {
const { payload } = params, rest = __rest(params, ["payload"]);
await cloudRequest(Object.assign(Object.assign({}, rest), { method: 'POST', path: `/api/event`, body: payload }));
}
async function createCloudNotificationReply(params) {
const { payload } = params, rest = __rest(params, ["payload"]);
await cloudRequest(Object.assign(Object.assign({}, rest), { method: 'POST', path: '/api/reply', body: payload }));
}
async function fetchCloudNotification(params) {
const { id } = params, rest = __rest(params, ["id"]);
const response = await cloudRequest(Object.assign(Object.assign({}, rest), { path: `/api/notification/${encodeURIComponent(id)}` }));
return response.json();
}
async function fetchCloudPass(params) {
const { serial } = params, rest = __rest(params, ["serial"]);
const response = await cloudRequest(Object.assign(Object.assign({}, rest), { path: `/api/pass/forserial/${encodeURIComponent(serial)}` }));
return response.json();
}
async function fetchCloudPassSaveLinks(params) {
const { serial } = params, rest = __rest(params, ["serial"]);
const response = await cloudRequest(Object.assign(Object.assign({}, rest), { path: `/api/pass/savelinks/${encodeURIComponent(serial)}` }));
return response.json();
}
const logger = new Logger('notificare/push/sw');
let clientState = 'unready';
function getClientState() {
return clientState;
}
function setClientState(state) {
clientState = state;
}
// Let TS know this is scoped to a service worker.
// declare const self: ServiceWorkerGlobalScope;
function onMessage(event) {
if (!event.data)
return;
try {
switch (event.data.action) {
case 're.notifica.ready':
setClientState('ready');
break;
default:
logger.warning('Unknown service worker message event: ', event);
}
}
catch (e) {
logger.error('Failed to process a service worker message event: ', e);
}
}
function convertCloudNotificationToPublic(notification) {
var _a, _b, _c, _d, _e, _f, _g;
return {
// eslint-disable-next-line no-underscore-dangle
id: notification._id,
partial: (_a = notification.partial) !== null && _a !== void 0 ? _a : false,
type: notification.type,
time: notification.time,
title: notification.title,
subtitle: notification.subtitle,
message: notification.message,
content: (_c = (_b = notification.content) === null || _b === void 0 ? void 0 : _b.map(convertNotificationContentToPublic)) !== null && _c !== void 0 ? _c : [],
actions: ((_d = notification.actions) !== null && _d !== void 0 ? _d : []).reduce((acc, currentValue) => {
const action = convertNotificationActionToPublic(currentValue);
if (action)
acc.push(action);
return acc;
}, []),
attachments: (_f = (_e = notification.attachments) === null || _e === void 0 ? void 0 : _e.map(convertNotificationAttachmentToPublic)) !== null && _f !== void 0 ? _f : [],
extra: (_g = notification.extra) !== null && _g !== void 0 ? _g : {},
};
}
function convertNotificationContentToPublic(content) {
return {
type: content.type,
data: content.data,
};
}
function convertNotificationActionToPublic(action) {
var _a, _b;
if (!action.label)
return undefined;
return {
// eslint-disable-next-line no-underscore-dangle
id: action._id,
type: action.type,
label: action.label,
target: action.target,
camera: (_a = action.camera) !== null && _a !== void 0 ? _a : false,
keyboard: (_b = action.keyboard) !== null && _b !== void 0 ? _b : false,
};
}
function convertNotificationAttachmentToPublic(attachment) {
return {
mimeType: attachment.mimeType,
uri: attachment.uri,
};
}
class InvalidWorkerConfigurationError extends Error {
constructor() {
super('The service worker was not registered with the expected configuration.');
}
}
function sleep(milliseconds) {
return new Promise((resolve) => {
setTimeout(resolve, milliseconds);
});
}
function isAppleDevice() {
const expression = /Mac|iPhone|iPod|iPad/i;
return expression.test(navigator.userAgent);
}
function isChromeBrowser() {
const expression = /Chrome/i;
return expression.test(navigator.userAgent);
}
function isSafariBrowser() {
const expression = /Safari/i;
return expression.test(navigator.userAgent) && !isChromeBrowser();
}
function getEmailUrl(email) {
return prefixed(email, 'mailto:');
}
function getSmsUrl(phoneNumber) {
return prefixed(phoneNumber, 'sms:');
}
function getTelephoneUrl(phoneNumber) {
return prefixed(phoneNumber, 'tel:');
}
function prefixed(value, prefix) {
if (value.startsWith(prefix))
return value;
return `${prefix}${value}`;
}
function getServiceWorkerLocation() {
return self.location;
}
function base64Decode(data) {
return self.atob(data);
}
function parseWorkerConfiguration() {
const location = getServiceWorkerLocation();
const searchParams = new URLSearchParams(location.search);
const encodedConfig = searchParams.get('notificareConfig');
if (!encodedConfig) {
logger.warning('Cannot parse the worker configuration: missing config.');
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let config;
try {
const decoded = base64Decode(encodedConfig);
config = JSON.parse(decoded);
}
catch (e) {
logger.warning('Cannot parse the worker configuration: unable to decode the config.');
return undefined;
}
const { cloudHost } = config;
if (!cloudHost) {
logger.warning('Cannot parse the worker configuration: missing cloud host.');
return undefined;
}
const { applicationId } = config;
if (!applicationId) {
// The service worker was updated on the background and the user hasn't opened
// the website yet to update the worker registration.
logger.warning('Parsing older worker configuration: missing application id.');
}
const { applicationKey } = config;
if (!applicationKey) {
logger.warning('Cannot parse the worker configuration: missing application key.');
return undefined;
}
const { applicationSecret } = config;
if (!applicationSecret) {
logger.warning('Cannot parse the worker configuration: missing application secret.');
return undefined;
}
const { deviceId } = config;
if (!deviceId) {
logger.warning('Cannot parse the worker configuration: missing device id.');
return undefined;
}
return {
cloudHost,
applicationId,
applicationKey,
applicationSecret,
deviceId,
standalone: config.standalone,
};
}
function getCurrentDeviceId() {
const configuration = parseWorkerConfiguration();
if (!configuration)
throw new InvalidWorkerConfigurationError();
return configuration.deviceId;
}
async function getCloudApiEnvironment() {
const configuration = parseWorkerConfiguration();
if (!configuration)
throw new InvalidWorkerConfigurationError();
return {
cloudHost: configuration.cloudHost,
applicationKey: configuration.applicationKey,
applicationSecret: configuration.applicationSecret,
};
}
async function logNotificationReceived(id) {
await createCloudEvent({
environment: await getCloudApiEnvironment(),
payload: {
type: 're.notifica.event.notification.Receive',
notification: id,
deviceID: getCurrentDeviceId(),
timestamp: Date.now(),
},
});
}
async function logNotificationOpen(id) {
await createCloudEvent({
environment: await getCloudApiEnvironment(),
payload: {
type: 're.notifica.event.notification.Open',
notification: id,
deviceID: getCurrentDeviceId(),
timestamp: Date.now(),
},
});
}
async function logNotificationInfluenced(id) {
await createCloudEvent({
environment: await getCloudApiEnvironment(),
payload: {
type: 're.notifica.event.notification.Influenced',
notification: id,
deviceID: getCurrentDeviceId(),
timestamp: Date.now(),
},
});
}
async function presentWindowClient(notification, action) {
const client = await ensureOpenWindowClient();
logger.debug('Sending notification clicked event to window client.');
client.postMessage({
cmd: 're.notifica.push.sw.notification_clicked',
content: {
notification,
action,
},
});
try {
await client.focus();
}
catch (e) {
logger.error('Failed to focus client: ', client, e);
}
}
async function ensureOpenWindowClient() {
logger.debug('Searching for a ready window client.');
const clients = await self.clients.matchAll({ type: 'window' });
if (clients.length) {
logger.debug(`Found ${clients.length} open clients. Client state is '${getClientState()}'.`);
if (getClientState() === 'ready') {
return clients[0];
}
return waitForOpenWindowClient();
}
// Reset the readiness state in case the service worker instance is reused across
// application restarts, when it used to be ready.
logger.debug('Resetting the client state.');
setClientState('unready');
logger.debug('Opening a new window client.');
self.clients.openWindow('/').catch((e) => logger.error('Unable to open a window client.', e));
return waitForOpenWindowClient();
}
async function waitForOpenWindowClient() {
logger.debug('Waiting for an open window client.');
const clients = await self.clients.matchAll({ type: 'window' });
if (clients.length && getClientState() === 'ready')
return clients[0];
await sleep(1000);
return waitForOpenWindowClient();
}
async function presentNotificationAction(notification, action) {
const config = parseWorkerConfiguration();
if (!config)
throw new InvalidWorkerConfigurationError();
if (config.standalone) {
logger.debug('Presenting a notification action in standalone mode.');
await presentWindowClient(notification, action);
return;
}
switch (action.type) {
case 're.notifica.action.App':
await presentAppNotificationAction(action);
break;
case 're.notifica.action.Browser':
await presentBrowserNotificationAction(action);
break;
case 're.notifica.action.InAppBrowser':
await presentInAppBrowserNotificationAction(action);
break;
case 're.notifica.action.Mail':
await presentMailNotificationAction(action);
break;
case 're.notifica.action.SMS':
await presentSmsNotificationAction(action);
break;
case 're.notifica.action.Telephone':
await presentTelephoneNotificationAction(action);
break;
default:
await presentWindowClient(notification, action);
return;
}
await createNotificationReply(notification, action);
}
async function presentAppNotificationAction(action) {
var _a;
try {
const urlStr = ((_a = action.target) === null || _a === void 0 ? void 0 : _a.trim()) ? action.target.trim() : '/';
await self.clients.openWindow(urlStr);
}
catch (_b) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
}
async function presentBrowserNotificationAction(action) {
var _a;
const urlStr = ((_a = action.target) === null || _a === void 0 ? void 0 : _a.trim()) ? action.target.trim() : '/';
await self.clients.openWindow(urlStr);
}
async function presentInAppBrowserNotificationAction(action) {
var _a;
const urlStr = ((_a = action.target) === null || _a === void 0 ? void 0 : _a.trim()) ? action.target.trim() : '/';
await self.clients.openWindow(urlStr);
}
async function presentMailNotificationAction(action) {
if (!action.target)
throw new Error('Invalid action target.');
try {
await self.clients.openWindow(getEmailUrl(action.target));
}
catch (_a) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
}
async function presentSmsNotificationAction(action) {
if (!action.target)
throw new Error('Invalid action target.');
try {
await self.clients.openWindow(getSmsUrl(action.target));
}
catch (_a) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
}
async function presentTelephoneNotificationAction(action) {
if (!action.target)
throw new Error('Invalid action target.');
try {
await self.clients.openWindow(getTelephoneUrl(action.target));
}
catch (_a) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
}
async function createNotificationReply(notification, action) {
await createCloudNotificationReply({
environment: await getCloudApiEnvironment(),
payload: {
notification: notification.id,
label: action.label,
deviceID: getCurrentDeviceId(),
data: {
target: action.target,
},
},
});
}
function resolveUrl(notification) {
const content = notification.content.find(({ type }) => type === 're.notifica.content.URL');
if (!content || !content.data)
return UrlResolverResult.NONE;
const isStringContent = typeof content.data === 'string' || content.data instanceof String;
if (!isStringContent)
return UrlResolverResult.NONE;
const urlStr = content.data.toString().trim();
if (!urlStr || !urlStr.trim())
return UrlResolverResult.NONE;
if (urlStr.startsWith('/'))
return UrlResolverResult.IN_APP_BROWSER;
let url;
try {
url = new URL(urlStr);
}
catch (e) {
return UrlResolverResult.NONE;
}
const isHttpUrl = url.protocol === 'http:' || url.protocol === 'https:';
const isDynamicLink = url.host.endsWith('ntc.re');
if (!isHttpUrl || isDynamicLink)
return UrlResolverResult.URL_SCHEME;
const webViewQueryParameter = url.searchParams.get('notificareWebView');
const isWebViewMode = webViewQueryParameter === '1' || (webViewQueryParameter === null || webViewQueryParameter === void 0 ? void 0 : webViewQueryParameter.toLowerCase()) === 'true';
return isWebViewMode ? UrlResolverResult.WEB_VIEW : UrlResolverResult.IN_APP_BROWSER;
}
var UrlResolverResult;
(function (UrlResolverResult) {
UrlResolverResult[UrlResolverResult["NONE"] = 0] = "NONE";
UrlResolverResult[UrlResolverResult["URL_SCHEME"] = 1] = "URL_SCHEME";
UrlResolverResult[UrlResolverResult["IN_APP_BROWSER"] = 2] = "IN_APP_BROWSER";
UrlResolverResult[UrlResolverResult["WEB_VIEW"] = 3] = "WEB_VIEW";
})(UrlResolverResult || (UrlResolverResult = {}));
async function presentNotification(notification) {
const config = parseWorkerConfiguration();
if (!config)
throw new InvalidWorkerConfigurationError();
if (config.standalone) {
logger.debug('Presenting a notification in standalone mode.');
await presentWindowClient(notification);
return;
}
switch (notification.type) {
case 're.notifica.notification.InAppBrowser':
await presentInAppBrowserNotification(notification);
break;
case 're.notifica.notification.Passbook':
await presentPassbookNotification(notification);
break;
case 're.notifica.notification.URLResolver':
await presentUrlResolverNotification(notification);
break;
case 're.notifica.notification.URLScheme':
await presentUrlSchemeNotification(notification);
break;
default:
await presentWindowClient(notification);
}
}
async function presentInAppBrowserNotification(notification) {
const content = notification.content.find(({ type }) => type === 're.notifica.content.URL');
if (!content)
throw new Error('Invalid notification content.');
const url = sanitizeContentUrl(content);
await self.clients.openWindow(url);
}
function sanitizeContentUrl(content) {
var _a;
const url = (_a = content.data) === null || _a === void 0 ? void 0 : _a.trim();
if (!url)
return '/';
try {
const parsedUrl = new URL(url);
// The URLResolver type may include an auxiliary notificareWebView parameter.
// Remove it from the destination URL.
parsedUrl.searchParams.delete('notificareWebView');
return parsedUrl.toString();
}
catch (_b) {
return url;
}
}
async function presentPassbookNotification(notification) {
const content = notification.content.find(({ type }) => type === 're.notifica.content.PKPass');
if (!content)
throw new Error('Invalid notification content.');
const passUrlStr = content.data;
const components = passUrlStr.split('/');
if (!components.length)
throw new Error('Invalid notification content.');
const id = components[components.length - 1];
const { pass } = await fetchCloudPass({
environment: await getCloudApiEnvironment(),
serial: id,
});
if (pass.version === 2) {
const { saveLinks } = await fetchCloudPassSaveLinks({
environment: await getCloudApiEnvironment(),
serial: id,
});
if (isAppleDevice() && isSafariBrowser() && (saveLinks === null || saveLinks === void 0 ? void 0 : saveLinks.appleWallet)) {
await self.clients.openWindow(saveLinks.appleWallet);
return;
}
if (saveLinks === null || saveLinks === void 0 ? void 0 : saveLinks.googlePay) {
await self.clients.openWindow(saveLinks.googlePay);
return;
}
}
const config = parseWorkerConfiguration();
if (!config)
throw new InvalidWorkerConfigurationError();
if (isAppleDevice() && isSafariBrowser()) {
await self.clients.openWindow(`https://${config.cloudHost}/pass/pkpass/${id}`);
return;
}
await self.clients.openWindow(`https://${config.cloudHost}/pass/web/${id}?showWebVersion=1`);
}
async function presentUrlResolverNotification(notification) {
const result = resolveUrl(notification);
switch (result) {
case UrlResolverResult.NONE:
logger.debug("Resolving as 'none' notification.");
await presentWindowClient(notification);
break;
case UrlResolverResult.URL_SCHEME:
logger.debug("Resolving as 'url scheme' notification.");
await presentUrlSchemeNotification(notification);
break;
case UrlResolverResult.IN_APP_BROWSER:
logger.debug("Resolving as 'in-app browser' notification.");
await presentInAppBrowserNotification(notification);
break;
case UrlResolverResult.WEB_VIEW:
logger.debug("Resolving as 'web view' notification.");
await presentWindowClient(notification);
break;
default:
throw new Error(`Unknown URL resolver result '${result}'.`);
}
}
async function presentUrlSchemeNotification(notification) {
const content = notification.content.find(({ type }) => type === 're.notifica.content.URL');
if (!content)
throw new Error('Invalid notification content.');
if (!content.data || !content.data.trim()) {
await self.clients.openWindow('/');
return;
}
const urlStr = content.data.trim();
let url;
try {
url = new URL(urlStr);
}
catch (e) {
logger.warning(`Unable to parse URL string '${urlStr}'.`, e);
try {
await self.clients.openWindow(urlStr);
}
catch (_a) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
return;
}
if (!url.host.endsWith('ntc.re')) {
try {
await self.clients.openWindow(urlStr);
}
catch (_b) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
return;
}
const { link } = await fetchCloudDynamicLink({
environment: await getCloudApiEnvironment(),
deviceId: getCurrentDeviceId(),
url: urlStr,
});
try {
await self.clients.openWindow(link.target);
}
catch (_c) {
// The promise fails when opening a deep link
// even when it succeeds in processing it.
}
}
async function onNotificationClick(event) {
event.notification.close();
const workerConfiguration = parseWorkerConfiguration();
if (workerConfiguration) {
await handleStandardClick(event);
}
else {
await handleLegacyClick(event);
}
}
async function handleLegacyClick(event) {
const client = await ensureOpenWindowClient();
client.postMessage({
cmd: event.action
? 're.notifica.push.sw.notification_reply'
: 're.notifica.push.sw.notification_clicked',
notification: event.notification.data,
action: event.action,
});
try {
await client.focus();
}
catch (e) {
logger.error('Failed to focus client: ', client, e);
}
}
async function handleStandardClick(event) {
await logNotificationOpen(event.notification.data.notificationId);
await logNotificationInfluenced(event.notification.data.notificationId);
await broadcastInboxUpdate();
const response = await fetchCloudNotification({
environment: await getCloudApiEnvironment(),
id: event.notification.data.id,
});
const notification = convertCloudNotificationToPublic(response.notification);
if (!event.action) {
await presentNotification(notification);
await refreshApplicationBadge();
return;
}
const action = notification.actions.find((element) => element.id === event.action);
if (!action)
throw new Error('Cannot find the action clicked to process the event.');
const isQuickResponse = action.type === 're.notifica.action.Callback' && !action.camera && !action.keyboard;
if (isQuickResponse) {
await createNotificationReply(notification, action);
await refreshApplicationBadge();
return;
}
await presentNotificationAction(notification, action);
await refreshApplicationBadge();
}
async function refreshApplicationBadge() {
var _a;
logger.debug('Updating application badge.');
if (!navigator.setAppBadge && !navigator.setClientBadge) {
logger.debug('There is no badge support. Skipping badge update.');
return;
}
try {
const { application } = await fetchCloudApplication({
environment: await getCloudApiEnvironment(),
});
if (!((_a = application.inboxConfig) === null || _a === void 0 ? void 0 : _a.autoBadge)) {
logger.debug('Auto badge functionality disabled. Skipping badge update.');
return;
}
const { unread } = await fetchCloudDeviceInbox({
environment: await getCloudApiEnvironment(),
deviceId: getCurrentDeviceId(),
skip: 0,
limit: 0,
});
if (navigator.setAppBadge)
await navigator.setAppBadge(unread);
if (navigator.setClientBadge)
navigator.setClientBadge(unread);
}
catch (e) {
logger.warning('Failed to update the application badge.', e);
}
}
async function broadcastInboxUpdate() {
const clients = await self.clients.matchAll();
clients.forEach((client) => {
client.postMessage({
cmd: 're.notifica.push.sw.update_inbox',
});
});
}
function createPartialNotification(message) {
var _a;
const ignoreKeys = [
'system',
'push',
'requireInteraction',
'renotify',
'urlFormatString',
'badge',
'id',
'inboxItemId',
'inboxItemVisible',
'inboxItemExpires',
'notificationId',
'notificationType',
'application',
'alertTitle',
'alertSubtitle',
'alert',
'icon',
'sound',
'attachment',
'actions',
];
const extras = Object.keys(message)
.filter((key) => !ignoreKeys.includes(key) && !key.startsWith('x-'))
.reduce((acc, key) => {
acc[key] = message[key];
return acc;
}, {});
return {
id: message.notificationId,
partial: true,
type: message.notificationType,
time: new Date().toUTCString(),
title: message.alertTitle,
subtitle: message.alertSubtitle,
message: (_a = message.alert) !== null && _a !== void 0 ? _a : '',
content: [],
actions: [],
attachments: message.attachment ? [message.attachment] : [],
extra: extras,
};
}
async function onPush(event) {
logger.debug('Received a remote notification.');
if (!event.data) {
logger.warning('The push event contained no data. Skipping...');
return;
}
let workerNotification;
try {
workerNotification = event.data.json();
}
catch (e) {
logger.error('Unable to parse the push event data.');
return;
}
if (workerNotification['x-sender'] !== 'notificare') {
await handleUnknownNotification(workerNotification);
return;
}
const workerConfiguration = parseWorkerConfiguration();
if (workerConfiguration &&
workerConfiguration.applicationId &&
workerConfiguration.applicationId !== workerNotification['x-application']) {
logger.warning('Incoming notification originated from another application.');
logger.debug(workerConfiguration);
logger.debug(workerNotification);
return;
}
const notification = workerNotification;
if (notification.system) {
await handleSystemNotification(notification);
return;
}
await handleNotification(notification);
}
async function handleUnknownNotification(workerNotification) {
logger.debug('Processing an unknown notification.');
const clients = await self.clients.matchAll();
clients.forEach((client) => {
client.postMessage({
cmd: 're.notifica.push.sw.unknown_notification_received',
message: workerNotification,
});
});
}
async function handleSystemNotification(workerNotification) {
logger.debug('Processing a system notification.', workerNotification);
const clients = await self.clients.matchAll();
clients.forEach((client) => {
client.postMessage({
cmd: 're.notifica.push.sw.system_notification_received',
message: workerNotification,
});
});
}
async function handleNotification(workerNotification) {
const workerConfiguration = parseWorkerConfiguration();
if (!workerConfiguration) {
logger.debug('Service worker has no configuration. Falling back to legacy behaviour.');
await updateApplicationBadge(workerNotification);
await showNotificationPreview(workerNotification);
const clients = await self.clients.matchAll();
clients.forEach((client) => {
client.postMessage({
cmd: 're.notifica.push.sw.notification_received',
message: workerNotification,
});
});
return;
}
await logNotificationReceived(workerNotification.notificationId);
await updateApplicationBadge(workerNotification);
await showNotificationPreview(workerNotification);
const clients = await self.clients.matchAll();
if (!clients.length)
return;
let notification;
try {
const response = await fetchCloudNotification({
environment: await getCloudApiEnvironment(),
id: workerNotification.id,
});
notification = convertCloudNotificationToPublic(response.notification);
}
catch (e) {
logger.error('Failed to fetch notification.', e);
notification = createPartialNotification(workerNotification);
}
clients.forEach((client) => {
client.postMessage({
cmd: 're.notifica.push.sw.notification_received',
content: {
message: workerNotification,
notification,
},
});
});
}
async function updateApplicationBadge(notification) {
try {
const { badge } = notification;
if (!badge)
return;
if (navigator.setAppBadge)
await navigator.setAppBadge(badge);
if (navigator.setClientBadge)
navigator.setClientBadge(badge);
}
catch (e) {
logger.warning('Unable to update the application badge.', e);
}
}
async function showNotificationPreview(notification) {
var _a, _b, _c, _d, _e;
if (!notification.push || !notification.alert) {
// Silent notification.
return;
}
const title = (_b = (_a = notification.alertTitle) !== null && _a !== void 0 ? _a : notification.application) !== null && _b !== void 0 ? _b : '';
const icon = 'image' in Notification.prototype ? notification.icon : (_c = notification.attachment) === null || _c === void 0 ? void 0 : _c.uri;
const options = {
tag: notification.id,
body: notification.alert,
icon,
image: (_d = notification.attachment) === null || _d === void 0 ? void 0 : _d.uri,
requireInteraction: notification.requireInteraction,
renotify: notification.renotify,
actions: (_e = notification.actions) === null || _e === void 0 ? void 0 : _e.map((action) => ({
// eslint-disable-next-line no-underscore-dangle
action: action._id,
title: action.label,
icon: action.icon,
})),
data: notification,
};
await self.registration.showNotification(title, options);
}
self.addEventListener('install', (event) => {
logger.debug('Service worker installed.');
event.waitUntil(self.skipWaiting()); // Activate worker immediately.
// NOTE: without this install + activate setup, the service worker controller
// will not be available on the website to send post messages to the worker.
});
self.addEventListener('activate', (event) => {
logger.debug('Service worker activated.');
event.waitUntil(self.clients.claim()); // Become available to all pages.
// NOTE: without this install + activate setup, the service worker controller
// will not be available on the website to send post messages to the worker.
});
self.addEventListener('message', (event) => {
logger.debug('Handling a service worker message event.');
onMessage(event);
});
self.addEventListener('push', (event) => {
logger.debug('Handling a push notification event.');
event.waitUntil(onPush(event));
});
self.addEventListener('pushsubscriptionchange', () => {
logger.info('Handling a push subscription change event.');
});
self.addEventListener('notificationclick', (event) => {
logger.debug('Handling a notification click event.');
event.waitUntil(onNotificationClick(event));
});
//# sourceMappingURL=index.cjs.js.map