-
Notifications
You must be signed in to change notification settings - Fork 46
fix(nitro): stop the v3 path importing the optional h3 peer #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "evlog": patch | ||
| --- | ||
|
|
||
| Fix `evlog/nitro/v3` pulling in the optional `h3` peer. The v3 plugin shared a deferred-drain helper from the v2 module, which imports `getHeaders` from `h3`, so the v3 bundle referenced `h3` even though the v3 runtime never uses it. Consumers that don't install `h3` directly (e.g. Nitro v3 / TanStack Start on Vite) failed to build with `"getHeaders" is not exported by "__vite-optional-peer-dep:h3:evlog"`. The helper now lives in an h3-free module, so the v3 path no longer references `h3`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| /** @internal Extend drain lifetime on Cloudflare without blocking Nitro Node responses. */ | ||
| export function extendDeferredDrain( | ||
| drainPromise: Promise<unknown>, | ||
| waitUntil?: (promise: Promise<unknown>) => void, | ||
| ): void { | ||
| void drainPromise.catch((err) => { | ||
| console.error('[evlog] background drain failed:', err) | ||
| }) | ||
| if (typeof waitUntil === 'function') { | ||
| waitUntil(drainPromise) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { existsSync, readFileSync } from 'node:fs' | ||
| import { dirname, resolve } from 'node:path' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| const v3Plugin = resolve(__dirname, '../../dist/nitro/v3/plugin.mjs') | ||
| const distExists = existsSync(v3Plugin) | ||
|
|
||
| if (!distExists) { | ||
| console.warn('[evlog test] Skipping h3 peer isolation: dist/ not found. Run `pnpm --filter evlog run build` first.') | ||
| } | ||
|
|
||
| // h3 is legitimately imported by the v2 runtime chunks, so it can't be forbidden | ||
| // dist-wide. Collect the chunks the v3 plugin actually reaches (it pulls in | ||
| // shared root-level chunks via `../../*.mjs`) and forbid h3 only within that set. | ||
| function collectV3Chunks(entry: string): string[] { | ||
| const seen = new Set<string>() | ||
| const relativeRe = /(?:import|export)\s+(?:[^'"]*?\sfrom\s+)?['"](\.[^'"]+)['"]|import\s*\(\s*['"](\.[^'"]+)['"]\s*\)/g | ||
|
|
||
| const walk = (file: string): void => { | ||
| if (seen.has(file)) return | ||
| seen.add(file) | ||
| let source: string | ||
| try { | ||
| source = readFileSync(file, 'utf8') | ||
| } catch { | ||
| return | ||
| } | ||
| let match: RegExpExecArray | null | ||
| while ((match = relativeRe.exec(source))) { | ||
| walk(resolve(dirname(file), match[1] ?? match[2])) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Built chunks use the π Proposed fix: append `.mjs` if no extension present let match: RegExpExecArray | null
while ((match = relativeRe.exec(source))) {
- walk(resolve(dirname(file), match[1] ?? match[2]))
+ const relPath = match[1] ?? match[2]
+ const absPath = resolve(dirname(file), relPath)
+ walk(absPath.endsWith('.mjs') ? absPath : `${absPath}.mjs`)
}
}π€ Prompt for AI Agents |
||
| } | ||
| } | ||
|
jmcgoldrick marked this conversation as resolved.
|
||
|
|
||
| walk(entry) | ||
| return [...seen] | ||
| } | ||
|
|
||
| /** | ||
| * Regression: the v3 plugin reused `extendDeferredDrain` from `nitro/enrich-drain`, | ||
| * which imports `getHeaders` from the optional `h3` peer for the v2 runtime. v3 | ||
| * consumers without a direct `h3` dependency hit the stubbed optional peer and | ||
| * failed to build with `"getHeaders" is not exported by "__vite-optional-peer-dep:h3:evlog"`. | ||
| * No chunk in the v3 plugin's graph may import `h3`. | ||
| */ | ||
| describe.skipIf(!distExists)('evlog/nitro/v3 avoids the optional h3 peer', () => { | ||
| it('no chunk reachable from the v3 plugin imports h3', () => { | ||
| const chunks = collectV3Chunks(v3Plugin) | ||
| expect(chunks.length).toBeGreaterThan(0) | ||
|
|
||
| const forbidden = [ | ||
| 'from "h3"', | ||
| 'from \'h3\'', | ||
| 'import("h3")', | ||
| 'import(\'h3\')', | ||
| ] | ||
|
|
||
| for (const file of chunks) { | ||
| const src = readFileSync(file, 'utf8') | ||
| for (const needle of forbidden) { | ||
| expect(src, file).not.toContain(needle) | ||
| } | ||
| } | ||
|
jmcgoldrick marked this conversation as resolved.
|
||
| }) | ||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.