-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(bun): add support for bunfig.toml registry config #39830
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
sebdanielsson
wants to merge
8
commits into
renovatebot:main
Choose a base branch
from
sebdanielsson:feat/add-bunfig-support
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.
+508
−0
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1cea0ba
feat(bunfig): add support for bunfig.toml registry config
sebdanielsson 8bdcfed
Improve test coverage for empty bunfig and invalid bunfig schema
sebdanielsson 53bae77
Use transform in schema, use for ... of loop
sebdanielsson 4fe77cd
Merge branch 'main' into feat/add-bunfig-support
sebdanielsson dc778f8
Update bunfig.ts
sebdanielsson 8ee4824
Import isString
sebdanielsson 589f0ec
Simplify BunfigRegistrySchema by removing unused fields
sebdanielsson 9172382
Merge branch 'main' into feat/add-bunfig-support
sebdanielsson 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,194 @@ | ||
| import { parseBunfigToml, resolveRegistryUrl } from './bunfig'; | ||
|
|
||
| describe('modules/manager/bun/bunfig', () => { | ||
| describe('parseBunfigToml', () => { | ||
| it('returns null for invalid TOML', () => { | ||
| expect(parseBunfigToml('invalid toml {')).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns empty config for empty TOML', () => { | ||
| expect(parseBunfigToml('')).toEqual({}); | ||
| }); | ||
|
|
||
| it('returns null for valid TOML with invalid schema', () => { | ||
| // Valid TOML but registry is not a string or valid object | ||
| const toml = ` | ||
| [install] | ||
| registry = 123 | ||
| `; | ||
| expect(parseBunfigToml(toml)).toBeNull(); | ||
| }); | ||
|
|
||
| it('parses simple string registry', () => { | ||
| const toml = ` | ||
| [install] | ||
| registry = "https://registry.example.com" | ||
| `; | ||
| expect(parseBunfigToml(toml)).toEqual({ | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('parses registry object with url and extracts url', () => { | ||
| const toml = ` | ||
| [install] | ||
| registry = { url = "https://registry.example.com", token = "abc123" } | ||
| `; | ||
| expect(parseBunfigToml(toml)).toEqual({ | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('parses scoped registries', () => { | ||
| const toml = ` | ||
| [install.scopes] | ||
| myorg = "https://registry.myorg.com" | ||
| `; | ||
| expect(parseBunfigToml(toml)).toEqual({ | ||
| install: { | ||
| scopes: { | ||
| myorg: 'https://registry.myorg.com', | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('parses mixed default and scoped registries', () => { | ||
| const toml = ` | ||
| [install] | ||
| registry = "https://registry.example.com" | ||
|
|
||
| [install.scopes] | ||
| myorg = "https://registry.myorg.com" | ||
| otherorg = { url = "https://registry.other.com", token = "secret" } | ||
| `; | ||
| expect(parseBunfigToml(toml)).toEqual({ | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| scopes: { | ||
| myorg: 'https://registry.myorg.com', | ||
| otherorg: 'https://registry.other.com', | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('ignores unrelated TOML sections', () => { | ||
| const toml = ` | ||
| [run] | ||
| shell = "zsh" | ||
|
|
||
| [install] | ||
| registry = "https://registry.example.com" | ||
| `; | ||
| expect(parseBunfigToml(toml)).toEqual({ | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('resolveRegistryUrl', () => { | ||
| it('returns null when no install config', () => { | ||
| expect(resolveRegistryUrl('dep', {})).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns null when no registry configured', () => { | ||
| expect(resolveRegistryUrl('dep', { install: {} })).toBeNull(); | ||
| }); | ||
|
|
||
| it('returns default registry for unscoped package', () => { | ||
| const config = { | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('lodash', config)).toBe( | ||
| 'https://registry.example.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns default registry for unscoped package with object config', () => { | ||
| // After schema transform, registry is always a string URL | ||
| const config = { | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('lodash', config)).toBe( | ||
| 'https://registry.example.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns scoped registry for scoped package', () => { | ||
| const config = { | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| scopes: { | ||
| myorg: 'https://registry.myorg.com', | ||
| }, | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('@myorg/utils', config)).toBe( | ||
| 'https://registry.myorg.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns scoped registry for scoped package with object config', () => { | ||
| // After schema transform, scoped registries are always string URLs | ||
| const config = { | ||
| install: { | ||
| scopes: { | ||
| myorg: 'https://registry.myorg.com', | ||
| }, | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('@myorg/utils', config)).toBe( | ||
| 'https://registry.myorg.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('falls back to default registry for unmatched scope', () => { | ||
| const config = { | ||
| install: { | ||
| registry: 'https://registry.example.com', | ||
| scopes: { | ||
| myorg: 'https://registry.myorg.com', | ||
| }, | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('@other/pkg', config)).toBe( | ||
| 'https://registry.example.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns null for unmatched scope with no default', () => { | ||
| const config = { | ||
| install: { | ||
| scopes: { | ||
| myorg: 'https://registry.myorg.com', | ||
| }, | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('@other/pkg', config)).toBeNull(); | ||
| }); | ||
|
|
||
| it('handles scope with @ prefix in config', () => { | ||
| const config = { | ||
| install: { | ||
| scopes: { | ||
| '@myorg': 'https://registry.myorg.com', | ||
| }, | ||
| }, | ||
| }; | ||
| expect(resolveRegistryUrl('@myorg/utils', config)).toBe( | ||
| 'https://registry.myorg.com', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
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,107 @@ | ||
| import { z } from 'zod'; | ||
| import { logger } from '../../../logger'; | ||
| import { findLocalSiblingOrParent, readLocalFile } from '../../../util/fs'; | ||
| import { Result } from '../../../util/result'; | ||
| import { parse as parseToml } from '../../../util/toml'; | ||
|
|
||
| /** | ||
| * Schema for bunfig.toml registry configuration. | ||
| * Supports both string URLs and object with url/token/username/password. | ||
| * Transforms to extract just the URL string. | ||
| * See: https://bun.sh/docs/runtime/bunfig#install-registry | ||
| */ | ||
| const BunfigRegistrySchema = z | ||
| .union([ | ||
| z.string(), | ||
| z.object({ | ||
| url: z.string(), | ||
| token: z.string().optional(), | ||
| username: z.string().optional(), | ||
| password: z.string().optional(), | ||
| }), | ||
| ]) | ||
| .transform((val) => (typeof val === 'string' ? val : val.url)); | ||
|
|
||
| const BunfigInstallSchema = z.object({ | ||
| registry: BunfigRegistrySchema.optional(), | ||
| scopes: z.record(z.string(), BunfigRegistrySchema).optional(), | ||
| }); | ||
|
|
||
| const BunfigSchema = z.object({ | ||
| install: BunfigInstallSchema.optional(), | ||
| }); | ||
|
|
||
| export type BunfigConfig = z.infer<typeof BunfigSchema>; | ||
|
|
||
| /** | ||
| * Resolves the registry URL for a given package name based on bunfig.toml config. | ||
| * Scoped packages (@org/pkg) are matched against scoped registries first. | ||
| */ | ||
| export function resolveRegistryUrl( | ||
| packageName: string, | ||
| bunfigConfig: BunfigConfig, | ||
| ): string | null { | ||
| const install = bunfigConfig.install; | ||
| if (!install) { | ||
| return null; | ||
| } | ||
|
|
||
| // Check scoped registries first | ||
| if (install.scopes) { | ||
| for (const [scope, registryUrl] of Object.entries(install.scopes)) { | ||
| // Bun scopes in bunfig.toml don't include the @ prefix | ||
| const scopePrefix = scope.startsWith('@') ? scope : `@${scope}`; | ||
| if (packageName.startsWith(`${scopePrefix}/`)) { | ||
| return registryUrl; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Fall back to default registry | ||
| if (install.registry) { | ||
| return install.registry; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Loads and parses bunfig.toml from the filesystem. | ||
| * Returns null if file not found or parsing fails. | ||
| */ | ||
| export async function loadBunfigToml( | ||
| packageFile: string, | ||
| ): Promise<BunfigConfig | null> { | ||
| const bunfigFileName = await findLocalSiblingOrParent( | ||
| packageFile, | ||
| 'bunfig.toml', | ||
| ); | ||
|
|
||
| if (!bunfigFileName) { | ||
| return null; | ||
| } | ||
|
|
||
| const content = await readLocalFile(bunfigFileName, 'utf8'); | ||
| if (!content) { | ||
| return null; | ||
| } | ||
|
|
||
| return parseBunfigToml(content); | ||
| } | ||
|
|
||
| /** | ||
| * Parses bunfig.toml content string into a typed config object. | ||
| */ | ||
| export function parseBunfigToml(content: string): BunfigConfig | null { | ||
| try { | ||
| const parsed = parseToml(content); | ||
| return Result.parse(parsed, BunfigSchema) | ||
| .onError((err) => { | ||
| logger.debug({ err }, 'Failed to parse bunfig.toml'); | ||
| }) | ||
| .unwrapOrNull(); | ||
| } catch (err) { | ||
| logger.debug({ err }, 'Failed to parse bunfig.toml TOML syntax'); | ||
| return null; | ||
| } | ||
| } | ||
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.