@equinor/fusion-framework-vite-plugin-spa 1.0.0-next.5 → 1.0.0-next.7

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sw.js","sources":["../esm/html/sw.js"],"sourcesContent":["/// <reference lib=\"webworker\" />\n/**\n * A reference to the global scope of the service worker.\n *\n * The `self` variable is explicitly cast to `ServiceWorkerGlobalScope` to ensure\n * type safety and provide access to service worker-specific APIs.\n *\n * This is necessary because `globalThis` is a generic global object and does not\n * include service worker-specific properties and methods by default.\n */\nconst self = globalThis;\n/**\n * An array of settings used for token injection.\n * Each setting defines the configuration for injecting tokens\n * into the application, such as authentication or API tokens.\n */\nlet resourceConfigurations = [];\n/**\n * A cache for storing tokens, implemented as a `Map`.\n * This cache is used to temporarily hold tokens for quick retrieval.\n *\n * @type {TokenCache} - A `Map` instance where the keys and values are determined by the `TokenCache` type definition.\n */\nconst tokenCache = new Map();\n/**\n * Generates a unique key by sorting and concatenating an array of scope strings.\n *\n * @param scopes - An array of strings representing the scopes to be processed.\n * @returns A single string representing the sorted and concatenated scopes, separated by commas.\n */\nfunction getScopeKey(scopes) {\n return scopes.sort().join(',');\n}\n/**\n * Checks if a token associated with the specified scopes is valid.\n *\n * This function determines the validity of a token by checking if it exists\n * in the token cache and if its expiration time has not been reached.\n *\n * @param scopes - An array of strings representing the scopes for which the token is required.\n * @returns `true` if a valid token exists for the given scopes; otherwise, `false`.\n */\nfunction isTokenValid(scopes) {\n const scopeKey = getScopeKey(scopes);\n if (!tokenCache.has(scopeKey)) {\n return false;\n }\n const tokenData = tokenCache.get(scopeKey);\n return tokenData !== undefined && Date.now() < tokenData.expiresOn;\n}\n/**\n * Requests an access token from a client using the Service Worker's `clients` API.\n * Communicates with the client via a `MessageChannel` to retrieve the token.\n *\n * @param scopes - An array of strings representing the scopes for which the token is requested.\n * @returns A promise that resolves to the token object containing the `accessToken` and `expiresOn` properties.\n * @throws An error if no clients are available or if the client responds with an error.\n *\n * @example\n * ```typescript\n * const token = await requestTokenFromClient(['scope1', 'scope2']);\n * console.log(token.accessToken); // Access token string\n * console.log(token.expiresOn); // Expiration timestamp\n * ```\n */\nasync function requestTokenFromClient(scopes) {\n const clients = await self.clients.matchAll();\n // ensure there are clients available\n if (clients.length === 0) {\n throw new Error('No clients available');\n }\n // create a message channel to communicate with the client\n const messageChannel = new MessageChannel();\n const token = await new Promise((resolve, reject) => {\n messageChannel.port1.onmessage = (event) => {\n if (event.data.error) {\n reject(event.data.error);\n }\n resolve(event.data);\n };\n clients[0].postMessage({ type: 'GET_TOKEN', scopes }, [messageChannel.port2]);\n });\n if (!token) {\n throw new Error('No token received');\n }\n // store the token in the cache\n tokenCache.set(getScopeKey(scopes), token);\n return token;\n}\n/**\n * Retrieves an access token for the specified scopes. If no valid token is found,\n * it requests a new one from the client.\n *\n * @param scopes - An array of strings representing the required scopes for the token.\n * @returns A promise that resolves to the access token as a string.\n * @throws An error if no access token is found after attempting to retrieve or request one.\n */\nasync function getToken(scopes) {\n // if no valid token is found, request a new one\n if (!isTokenValid(scopes)) {\n await requestTokenFromClient(scopes);\n }\n const scopeKey = getScopeKey(scopes);\n const { accessToken } = tokenCache.get(scopeKey) || {};\n if (!accessToken) {\n throw new Error('No access token found');\n }\n return accessToken;\n}\n// Match request to proxy config\n/**\n * Retrieves the matching token injection configuration for a given URL.\n *\n * @param url - The URL to match against the token injection settings.\n * @returns The matching `TokenInjectionSetting` if found, otherwise `undefined`.\n *\n * The function compares the provided URL with the `url` property of each\n * `TokenInjectionSetting` in the `tokenInjectionSettings` array. If the\n * provided URL starts with the resolved `config.url`, it is considered a match.\n *\n * Note:\n * - If `config.url` starts with a `/`, it is resolved relative to the service\n * worker's origin (`self.location.origin`).\n * - The comparison is performed using fully resolved absolute URLs.\n */\nfunction getMatchingConfig(url) {\n return resourceConfigurations.find((config) => {\n const configUrl = new URL(config.url, config.url.startsWith('/') ? self.location.origin : undefined).href;\n const requestUrl = new URL(url, self.location.origin).href;\n return requestUrl.startsWith(configUrl);\n });\n}\n// Install event\nself.addEventListener('install', (event) => {\n event.waitUntil(self.skipWaiting());\n});\n// Activate event\nself.addEventListener('activate', (event) => {\n event.waitUntil(self.clients.claim());\n});\n// Handle configuration from main thread\nself.addEventListener('message', (event) => {\n const { type, config } = event.data;\n if (type === 'INIT_CONFIG') {\n resourceConfigurations = config;\n }\n});\n// Handle fetch events\nself.addEventListener('fetch', (event) => {\n const url = new URL(event.request.url);\n const matchedConfig = getMatchingConfig(url.toString());\n // only handle requests that match the config\n if (matchedConfig) {\n const requestHeaders = new Headers(event.request.headers);\n const handleRequest = async () => {\n // if the matched config has scopes, append the token to the request\n if (matchedConfig.scopes) {\n const token = await getToken(matchedConfig.scopes);\n requestHeaders.set('Authorization', `Bearer ${token}`);\n }\n // if the matched config has a rewrite, rewrite the url\n if (typeof matchedConfig.rewrite === 'string') {\n url.pathname = url.pathname.replace(matchedConfig?.url, matchedConfig.rewrite);\n }\n // fetch the request with the modified url and headers\n return fetch(url, { headers: requestHeaders });\n };\n event.respondWith(handleRequest());\n }\n});\nexport {};\n//# sourceMappingURL=sw.js.map"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,IAAI,GAAG,UAAU;AACvB;AACA;AACA;AACA;AACA;AACA,IAAI,sBAAsB,GAAG,EAAE;AAC/B;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,UAAU,GAAG,IAAI,GAAG,EAAE;AAC5B;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,WAAW,CAAC,MAAM,EAAE;AAC7B,IAAI,OAAO,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC;AAClC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,YAAY,CAAC,MAAM,EAAE;AAC9B,IAAI,MAAM,QAAQ,GAAG,WAAW,CAAC,MAAM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