diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e13856f6..65ab62328 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -91,6 +91,26 @@ jobs: pnpm vitest run --project e2e "$test_file" done + windows-cache-driver: + name: Windows cache driver + runs-on: windows-latest + steps: + - uses: actions/checkout@v6 + - uses: pnpm/action-setup@v6 + - uses: actions/setup-node@v6 + with: + node-version: lts/* + cache: pnpm + - run: pnpm i + - run: pnpm --filter @nuxt/scripts dev:prepare + - run: pnpm exec nuxt prepare + - run: pnpm --filter @nuxt/scripts build + - run: pnpm exec vitest run --project unit test/unit/cached-upstream-lifecycle.test.ts + - name: Check dev startup + run: pnpm exec vitest run --project e2e test/e2e/cache-driver-dev.test.ts + - name: Check production build + run: pnpm exec vitest run --project e2e test/e2e/proxy-alias.test.ts -t "runs Nitro runtime helpers locally" + nitro-3: runs-on: ubuntu-24.04-arm name: Nitro 3 compatibility diff --git a/packages/script/src/runtime/server/utils/cache-config.ts b/packages/script/src/runtime/server/utils/cache-config.ts index f546762ff..e6e01b960 100644 --- a/packages/script/src/runtime/server/utils/cache-config.ts +++ b/packages/script/src/runtime/server/utils/cache-config.ts @@ -1,3 +1,5 @@ +import { pathToFileURL } from 'node:url' + /** Dedicated Nitro storage mount used by Nuxt Scripts' upstream caches. */ export const NUXT_SCRIPTS_CACHE_BASE = 'nuxt-scripts-cache' @@ -11,12 +13,21 @@ export const NUXT_SCRIPTS_CACHE_MAX_ENTRY_SIZE = 8 * 1024 * 1024 interface NitroOptionsWithStorage { storage?: Record + alias?: Record } export function ensureNuxtScriptsCacheStorage(nitroOptions: NitroOptionsWithStorage, driver: string): void { - nitroOptions.storage ||= {} - nitroOptions.storage[NUXT_SCRIPTS_CACHE_BASE] ||= { - driver, + const mounts = (nitroOptions.storage ||= {}) + if (mounts[NUXT_SCRIPTS_CACHE_BASE]) + return + + // Nitro imports custom drivers with the native ESM loader during setup. + const driverURL = pathToFileURL(driver).href + // Rollup needs a local path to bundle the driver into the server output. + nitroOptions.alias ||= {} + nitroOptions.alias[driverURL] = driver + mounts[NUXT_SCRIPTS_CACHE_BASE] = { + driver: driverURL, max: NUXT_SCRIPTS_CACHE_MAX_ENTRIES, maxSize: NUXT_SCRIPTS_CACHE_MAX_SIZE, maxEntrySize: NUXT_SCRIPTS_CACHE_MAX_ENTRY_SIZE, diff --git a/test/e2e/cache-driver-dev.test.ts b/test/e2e/cache-driver-dev.test.ts new file mode 100644 index 000000000..c240f7227 --- /dev/null +++ b/test/e2e/cache-driver-dev.test.ts @@ -0,0 +1,14 @@ +import { createResolver } from '@nuxt/kit' +import { $fetch, setup } from '@nuxt/test-utils/e2e' +import { expect, it } from 'vitest' + +const { resolve } = createResolver(import.meta.url) + +await setup({ + rootDir: resolve('../fixtures/proxy-alias'), + dev: true, +}) + +it('serves requests after loading the default cache driver in development', async () => { + expect(await $fetch('/api/nitro-runtime')).toEqual({ app: true, cached: 'ok', config: true }) +}) diff --git a/test/unit/cached-upstream-lifecycle.test.ts b/test/unit/cached-upstream-lifecycle.test.ts index 5cbfcea1e..60738c387 100644 --- a/test/unit/cached-upstream-lifecycle.test.ts +++ b/test/unit/cached-upstream-lifecycle.test.ts @@ -1,3 +1,9 @@ +import { execFileSync } from 'node:child_process' +import { mkdtemp, rm, symlink } from 'node:fs/promises' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +import { fileURLToPath, pathToFileURL } from 'node:url' +import { createResolver, resolveAlias } from '@nuxt/kit' import { beforeEach, describe, expect, it, vi } from 'vitest' import { ensureNuxtScriptsCacheStorage, @@ -19,15 +25,57 @@ describe('upstream cache storage ownership', () => { it('installs a bounded cache mount without registry server endpoints', () => { const nitroOptions: { storage?: Record } = {} + const driver = fileURLToPath(new URL('../../packages/script/src/runtime/server/utils/lru-cache-driver.ts', import.meta.url)) - ensureNuxtScriptsCacheStorage(nitroOptions, '/nuxt-scripts/lru-cache-driver') + ensureNuxtScriptsCacheStorage(nitroOptions, driver) expect(nitroOptions.storage?.[NUXT_SCRIPTS_CACHE_BASE]).toEqual(expect.objectContaining({ - driver: '/nuxt-scripts/lru-cache-driver', max: NUXT_SCRIPTS_CACHE_MAX_ENTRIES, })) }) + it('loads the cache driver through native ESM from a path with special characters', async () => { + const root = await mkdtemp(join(tmpdir(), 'nuxt-scripts-cache-')) + try { + const driverDir = join(root, 'cache # % ΓΌ') + const sourceDir = fileURLToPath(new URL('../../packages/script/src/runtime/server/utils/', import.meta.url)) + await symlink(sourceDir, driverDir, 'junction') + const nitroOptions: { storage?: Record } = {} + ensureNuxtScriptsCacheStorage(nitroOptions, join(driverDir, 'lru-cache-driver.ts')) + const driver = nitroOptions.storage![NUXT_SCRIPTS_CACHE_BASE]!.driver + + const value = execFileSync(process.execPath, ['--input-type=module', '-e', ` + const { default: createDriver } = await import(process.argv[1]) + const cache = createDriver({ max: 2 }) + cache.setItem('key', 'value') + process.stdout.write(cache.getItem('key')) + `, driver], { encoding: 'utf8' }) + + expect(value).toBe('value') + } + finally { + await rm(root, { recursive: true, force: true }) + } + }) + + it('resolves the driver URL to a local file for server bundling', () => { + const driver = createResolver(import.meta.url).resolve('../../packages/script/src/runtime/server/utils/lru-cache-driver.ts') + const nitroOptions: { alias?: Record } = {} + + ensureNuxtScriptsCacheStorage(nitroOptions, driver) + + expect(resolveAlias(pathToFileURL(driver).href, nitroOptions.alias)).toBe(driver) + }) + + it('preserves an application cache mount', () => { + const mount = { driver: 'redis', url: 'redis://localhost:6379' } + const nitroOptions = { storage: { [NUXT_SCRIPTS_CACHE_BASE]: mount } } + + ensureNuxtScriptsCacheStorage(nitroOptions, '/nuxt-scripts/lru-cache-driver') + + expect(nitroOptions.storage[NUXT_SCRIPTS_CACHE_BASE]).toBe(mount) + }) + it('puts binary proxy payloads in the bounded Nuxt Scripts cache mount', () => { createCachedBinaryFetch('binary-test', 60)