-
Notifications
You must be signed in to change notification settings - Fork 41
fix(nitro): scope runtime config probes to active major version #340
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
Open
HugoRCD
wants to merge
4
commits into
main
Choose a base branch
from
fix/nitro-bridge-active-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
add960c
fix(nitro): scope runtime config probes to active major version
HugoRCD e11a13d
Merge branch 'main' into fix/nitro-bridge-active-runtime
HugoRCD 518783c
Merge branch 'main' into fix/nitro-bridge-active-runtime
HugoRCD 984193d
fix(nitro): inline evlog config via nitro.options.replace
HugoRCD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| 'evlog': patch | ||
| --- | ||
|
|
||
| Fix a runtime crash on Vercel + Bun + Nitro v3 where every request failed with `bun is unable to write files: ReadOnlyFileSystem`. The Nitro plugin probed `nitro/runtime-config` at runtime to read evlog's config; that module transitively imports the build-only `#nitro/virtual/runtime-config`, which doesn't exist in deployed bundles. On Vercel + Bun the missing virtual triggered Bun's package auto-installer, which tried to write `node_modules/.cache` and crashed on the read-only function filesystem. | ||
|
|
||
| The Nitro modules now bake the evlog config into the bundle as a literal via `nitro.options.replace.__EVLOG_CONFIG__`. The shared config bridge reads that build-time literal first and skips all runtime probing β no `import('nitro/runtime-config')`, no env propagation guesswork. The bridge also exposes the inlined value as a synthetic `{ evlog: <inlined> }` record, so drain adapters resolving `runtimeConfig.evlog.<adapter>` never trigger the probe either. | ||
|
|
||
| For defense-in-depth, the bridge additionally scopes its dynamic-import fallback to the major version declared by the plugin (new internal `setActiveNitroRuntime` helper) β `nitro/runtime-config` for v3, `nitropack/...` for v2 β so standalone use outside a plugin (e.g. adapters called from non-Nitro code) doesn't probe both versions. | ||
|
|
||
| No public-API change. | ||
|
|
||
| Closes [#312](https://github.com/HugoRCD/evlog/issues/312). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| declare global { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| var __EVLOG_CONFIG__: unknown | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetModules() | ||
| vi.unstubAllEnvs() | ||
| delete process.env.__EVLOG_CONFIG | ||
| delete (globalThis as { __EVLOG_CONFIG__?: unknown }).__EVLOG_CONFIG__ | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| vi.resetModules() | ||
| vi.doUnmock(['nitro', 'runtime-config'].join('/')) | ||
| vi.doUnmock(['nitropack', 'runtime', 'internal', 'config'].join('/')) | ||
| vi.doUnmock(['nitropack', 'runtime'].join('/')) | ||
| delete (globalThis as { __EVLOG_CONFIG__?: unknown }).__EVLOG_CONFIG__ | ||
| }) | ||
|
|
||
| async function loadBridgeWithMocks() { | ||
| const importSpy = vi.fn<(specifier: string) => void>() | ||
| vi.doMock(['nitro', 'runtime-config'].join('/'), () => { | ||
| importSpy('nitro/runtime-config') | ||
| return { useRuntimeConfig: () => ({ evlog: { env: { service: 'svc-v3' } }, posthog: { apiKey: 'phc-v3' } }) } | ||
| }) | ||
| vi.doMock(['nitropack', 'runtime', 'internal', 'config'].join('/'), () => { | ||
| importSpy('nitropack/runtime/internal/config') | ||
| return { useRuntimeConfig: () => ({ evlog: { env: { service: 'svc-v2' } }, posthog: { apiKey: 'phc-v2' } }) } | ||
| }) | ||
| vi.doMock(['nitropack', 'runtime'].join('/'), () => { | ||
| importSpy('nitropack/runtime') | ||
| return { useRuntimeConfig: () => ({ evlog: { env: { service: 'svc-v2-barrel' } }, posthog: { apiKey: 'phc-v2-barrel' } }) } | ||
| }) | ||
| const bridge = await import('../../src/shared/nitroConfigBridge') | ||
| return { bridge, importSpy } | ||
| } | ||
|
|
||
| describe('nitroConfigBridge β active runtime', () => { | ||
| it('only probes Nitro v3 modules when v3 is the active runtime', async () => { | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.setActiveNitroRuntime('v3') | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
| const record = await bridge.getNitroRuntimeConfigRecord() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-v3' } }) | ||
| expect(record).toEqual({ evlog: { env: { service: 'svc-v3' } }, posthog: { apiKey: 'phc-v3' } }) | ||
|
|
||
| const probed = importSpy.mock.calls.map(call => call[0]) | ||
| expect(probed).toContain('nitro/runtime-config') | ||
| expect(probed).not.toContain('nitropack/runtime') | ||
| expect(probed).not.toContain('nitropack/runtime/internal/config') | ||
| }) | ||
|
|
||
| it('only probes nitropack v2 modules when v2 is the active runtime', async () => { | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.setActiveNitroRuntime('v2') | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
| const record = await bridge.getNitroRuntimeConfigRecord() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-v2' } }) | ||
| expect(record).toEqual({ evlog: { env: { service: 'svc-v2-barrel' } }, posthog: { apiKey: 'phc-v2-barrel' } }) | ||
|
|
||
| const probed = importSpy.mock.calls.map(call => call[0]) | ||
| expect(probed).not.toContain('nitro/runtime-config') | ||
| }) | ||
|
|
||
| it('reads __EVLOG_CONFIG from env without probing any Nitro module', async () => { | ||
| process.env.__EVLOG_CONFIG = JSON.stringify({ env: { service: 'svc-env' } }) | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.setActiveNitroRuntime('v3') | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-env' } }) | ||
| expect(importSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('falls back to historical probe order when no runtime is declared', async () => { | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.resetActiveNitroRuntime() | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-v3' } }) | ||
| const probed = importSpy.mock.calls.map(call => call[0]) | ||
| expect(probed).toContain('nitro/runtime-config') | ||
| }) | ||
|
|
||
| it('returns the build-time inlined __EVLOG_CONFIG__ without probing', async () => { | ||
| globalThis.__EVLOG_CONFIG__ = { env: { service: 'svc-inline' } } | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.setActiveNitroRuntime('v3') | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
| const record = await bridge.getNitroRuntimeConfigRecord() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-inline' } }) | ||
| expect(record).toEqual({ evlog: { env: { service: 'svc-inline' } } }) | ||
| expect(importSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('prefers __EVLOG_CONFIG__ over process.env.__EVLOG_CONFIG', async () => { | ||
| globalThis.__EVLOG_CONFIG__ = { env: { service: 'svc-inline' } } | ||
| process.env.__EVLOG_CONFIG = JSON.stringify({ env: { service: 'svc-env' } }) | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.setActiveNitroRuntime('v3') | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-inline' } }) | ||
| expect(importSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('ignores __EVLOG_CONFIG__ when it is not an object literal', async () => { | ||
| globalThis.__EVLOG_CONFIG__ = 'not-an-object' | ||
| const { bridge, importSpy } = await loadBridgeWithMocks() | ||
| bridge.setActiveNitroRuntime('v3') | ||
|
|
||
| const config = await bridge.resolveEvlogConfigForNitroPlugin() | ||
|
|
||
| expect(config).toEqual({ env: { service: 'svc-v3' } }) | ||
| expect(importSpy.mock.calls.map(c => c[0])).toContain('nitro/runtime-config') | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.