UNPKG

zephyr-xpack-internal

Version:

Xpack internals for Zephyr

118 lines (117 loc) • 5.12 kB
import "node:module"; const globalCacheKey = '__ZEPHYR_MANIFEST_CACHE__'; const _global = "u" > typeof window ? window : globalThis; function getGlobalManifestCache() { if (!_global[globalCacheKey]) _global[globalCacheKey] = new Map(); return _global[globalCacheKey]; } const manifestFileName = 'zephyr-manifest.json'; function toHttpUrl(value) { if (!value) return; try { const url = new URL(value); return 'http:' === url.protocol || 'https:' === url.protocol ? url : void 0; } catch { return; } } function getCurrentScriptSrc() { if ("u" < typeof document || !document.currentScript) return; return document.currentScript.src || void 0; } function getDocumentBaseUrl() { if ("u" < typeof document || 'function' != typeof document.querySelector) return; if (!document.querySelector('base[href]')) return; try { return toHttpUrl(new URL('.', document.baseURI).href); } catch { return; } } function resolveManifestUrl(scriptUrl) { const script = toHttpUrl(scriptUrl); const documentBase = getDocumentBaseUrl(); if (documentBase && (!script || script.origin === documentBase.origin)) return `${documentBase.href}${manifestFileName}`; if (script) return `${script.origin}/${manifestFileName}`; return `/${manifestFileName}`; } function createZephyrRuntimePlugin(options = {}) { const defaultManifestUrl = resolveManifestUrl(getCurrentScriptSrc()); const { manifestUrl = defaultManifestUrl } = options; let processedRemotes; async function fetchManifest(url) { try { const response = await fetch(url); if (!response.ok) return; const manifest = await response.json().catch(()=>void 0); if (!manifest) return void console.error('[Zephyr] Failed to parse manifest JSON'); return manifest; } catch (error) { console.error('[Zephyr] Unexpected error fetching manifest:', error); return; } } const manifestCache = getGlobalManifestCache(); if (!manifestCache.has(manifestUrl)) manifestCache.set(manifestUrl, fetchManifest(manifestUrl)); const zephyrManifestPromise = manifestCache.get(manifestUrl); const plugin = { name: 'zephyr-runtime-remote-resolver', async beforeRequest (args) { const zephyrManifest = await zephyrManifestPromise; if (!processedRemotes) processedRemotes = identifyRemotes(args, zephyrManifest); const remoteName = args.id.split('/')[0]; if (!processedRemotes[remoteName]) return args; const resolvedEntry = getResolvedRemoteEntry(processedRemotes[remoteName]); const targetRemote = args.options.remotes.find((remote)=>hasEntry(remote) && (remote.name === remoteName || remote.alias === remoteName)); if (!targetRemote) return args; targetRemote.entry = resolvedEntry.url; if (resolvedEntry.isManifest) delete targetRemote.type; else if (resolvedEntry.libraryType) targetRemote.type = resolvedEntry.libraryType; return args; } }; return plugin; } function identifyRemotes(args, zephyrManifest) { const identifiedRemotes = {}; if (!zephyrManifest) return identifiedRemotes; if (!args.options.remotes.length) return identifiedRemotes; const { dependencies } = zephyrManifest; const remotes = args.options.remotes; remotes.forEach((remote)=>{ const resolvedRemote = dependencies[remote.name] ?? dependencies[remote.alias ?? '']; if (resolvedRemote) { identifiedRemotes[remote.name] = resolvedRemote; if (remote.alias && remote.alias !== remote.name) identifiedRemotes[remote.alias] = resolvedRemote; } }); return identifiedRemotes; } function hasEntry(remote) { return null != remote && 'object' == typeof remote && 'entry' in remote && 'string' == typeof remote.entry; } function getResolvedRemoteEntry(resolvedRemote) { const _window = "u" > typeof window ? window : globalThis; const sessionEdgeURL = _window.sessionStorage?.getItem(resolvedRemote.application_uid); let edgeUrl = sessionEdgeURL ?? resolvedRemote.manifest_url ?? resolvedRemote.remote_entry_url; edgeUrl = stripRemoteNamePrefix(edgeUrl); const pathname = edgeUrl.split(/[?#]/, 1)[0]; const isManifest = pathname.endsWith('.json'); return { url: edgeUrl, isManifest, libraryType: isManifest ? void 0 : resolvedRemote.library_type }; } function stripRemoteNamePrefix(entry) { if (/^(?:https?:)?\/\//.test(entry)) return entry; const absoluteUrlIndex = entry.search(/https?:\/\//); if (absoluteUrlIndex > 0) return entry.slice(absoluteUrlIndex); const separatorIndex = entry.lastIndexOf('@'); if (-1 !== separatorIndex) return entry.slice(separatorIndex + 1); return entry; } const runtime_plugin = createZephyrRuntimePlugin; export default runtime_plugin; export { createZephyrRuntimePlugin, resolveManifestUrl }; //# sourceMappingURL=runtime-plugin.mjs.map