Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124/** * Bundle Request Orchestration * * Unifies parsing, cache lookup, and bundle execution for endpoints. * * Flow: * +---------+ +---------+ +---------+ +-----------+ * | Input |-->| Parse |-->| Cache |-->| Execute | * +---------+ +---------+ +---------+ +-----------+ */
import type { BundleRequestInput } from './parse.ts'import type { BundleResult, BundleConfig, ModuleSpec, CacheMode } from './types.ts'
import { parseInputToConfig } from './parse.ts'import { executeBundle, executeBundleWithOutput } from './service.ts'import { generateCacheKey, generateEntryPointHash } from './key.ts'import { getCachedResult, setCachedResult } from '../cache/operations.ts'
export type BundleResolution = { url: URL cacheKey: string cached: boolean durationMs: number result: BundleResult outputText?: string config: BundleConfig inputCode: string modules: ModuleSpec[] versions: string[]}
export interface ResolveBundleOptions { input: BundleRequestInput baseUrl: string cacheMode: CacheMode includeOutputText?: boolean | 'auto'}
export async function resolveBundleRequest( options: ResolveBundleOptions): Promise<BundleResolution> { const { input, baseUrl, cacheMode, includeOutputText = false } = options const startTime = performance.now()
const { inputCode, config, modules, versions, url } = await parseInputToConfig(input, baseUrl) const wantsOutputText = includeOutputText === 'auto' ? shouldIncludeOutputText(url) : includeOutputText
const cacheKey = await generateCacheKey(config, modules, versions, inputCode) const entryPointHash = await generateEntryPointHash(cacheKey)
const canReadCache = cacheMode === 'use' && !wantsOutputText
if (canReadCache) { const cached = await getCachedResult(cacheKey) if (cached) { return { url, cacheKey, cached: true, durationMs: performance.now() - startTime, result: cached, config, inputCode, modules, versions, } } }
const query = (url.searchParams.get('q') || url.searchParams.get('query')) ?? 'spring-easing'
let result: BundleResult let outputText: string | undefined
if (wantsOutputText) { const [bundleResult, rawOutput] = await executeBundleWithOutput({ config, inputCode, entryPointHash, url, versions, modules, query, })
result = bundleResult outputText = rawOutput } else { result = await executeBundle({ config, inputCode, entryPointHash, url, versions, modules, query, }) }
if (cacheMode !== 'bypass') { await setCachedResult(cacheKey, result) }
return { url, cacheKey, cached: false, durationMs: performance.now() - startTime, result, outputText, config, inputCode, modules, versions, }}
function shouldIncludeOutputText(url: URL): boolean { return url.searchParams.has('file') || url.pathname === '/file'}