Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/sanitize-wrangler-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'sv': patch
---

fix: sanitize wrangler project name to comply with Cloudflare naming requirements

When using the Cloudflare adapter, project names from `package.json` are now sanitized to be wrangler-compatible:
- Dots, underscores, and special characters are replaced with dashes
- Names are converted to lowercase
- Truncated to 63 characters (DNS subdomain limit)
- Empty results fallback to `undefined-project-name`
20 changes: 18 additions & 2 deletions packages/sv/lib/addons/_tests/sveltekit-adapter/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ test.concurrent.for(testCases)('adapter $kind.type $variant', async (testCase, {
'adapter-auto only supports some environments'
);
} else if (testCase.kind.type === 'cloudflare-workers') {
expect(await readFile(join(cwd, 'wrangler.jsonc'), 'utf8')).toMatch('ASSETS');
const wranglerContent = await readFile(join(cwd, 'wrangler.jsonc'), 'utf8');
expect(wranglerContent).toMatch('ASSETS');

const nameMatch = wranglerContent.match(/"name":\s*"([^"]+)"/);
expect(nameMatch).toBeTruthy();
if (nameMatch) {
expect(nameMatch[1]).toMatch(/^[a-z0-9-]+$/);
expect(nameMatch[1]).not.toContain('.');
}
} else if (testCase.kind.type === 'cloudflare-pages') {
expect(await readFile(join(cwd, 'wrangler.jsonc'), 'utf8')).toMatch('pages_build_output_dir');
const wranglerContent = await readFile(join(cwd, 'wrangler.jsonc'), 'utf8');
expect(wranglerContent).toMatch('pages_build_output_dir');

const nameMatch = wranglerContent.match(/"name":\s*"([^"]+)"/);
expect(nameMatch).toBeTruthy();
if (nameMatch) {
expect(nameMatch[1]).toMatch(/^[a-z0-9-]+$/);
expect(nameMatch[1]).not.toContain('.');
}
}
});
4 changes: 2 additions & 2 deletions packages/sv/lib/addons/sveltekit-adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineAddon, defineAddonOptions } from '../../core/index.ts';
import { exports, functions, imports, object, type AstTypes } from '../../core/tooling/js/index.ts';
import { parseJson, parseScript, parseToml } from '../../core/tooling/parsers.ts';
import { fileExists, readFile } from '../../cli/add/utils.ts';
import { fileExists, readFile, sanitizeWranglerName } from '../../cli/add/utils.ts';
import { resolveCommand } from 'package-manager-detector';
import * as js from '../../core/tooling/js/index.ts';

Expand Down Expand Up @@ -139,7 +139,7 @@ export default defineAddon({

if (!data.name) {
const pkg = parseJson(readFile(cwd, files.package));
data.name = pkg.data.name;
data.name = sanitizeWranglerName(pkg.data.name);
}

data.compatibility_date ??= new Date().toISOString().split('T')[0];
Expand Down
17 changes: 17 additions & 0 deletions packages/sv/lib/cli/add/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,20 @@ export function getHighlighter(): Highlighter {
optional: (str) => pc.gray(str)
};
}

/**
* Sanitizes a project name for Cloudflare Wrangler compatibility.
* Wrangler requires names to be alphanumeric, lowercase, dashes only, and max 63 chars.
* @example sanitizeWranglerName("sub.example.com") // "sub-example-com"
* @example sanitizeWranglerName("My_Project.Name") // "my-project-name"
*/
export function sanitizeWranglerName(name: string): string {
const sanitized = name
.toLowerCase()
.replace(/[^a-z0-9-]/g, '-')
.replace(/-+/g, '-')
.slice(0, 63)
.replace(/^-|-$/g, '');

return sanitized || 'undefined-project-name';
}
67 changes: 67 additions & 0 deletions packages/sv/lib/core/tests/wrangler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, it, expect } from 'vitest';
import { sanitizeWranglerName } from '../../cli/add/utils.ts';

describe('sanitizeWranglerName', () => {
const testCases = [
// Basic cases
{ input: 'my-project', expected: 'my-project' },
{ input: 'myproject', expected: 'myproject' },

// Dots
{ input: 'sub.example.com', expected: 'sub-example-com' },
{ input: 'my.cool.app', expected: 'my-cool-app' },

// Underscores
{ input: 'my_project_name', expected: 'my-project-name' },

// Mixed cases
{ input: 'My_Project.Name', expected: 'my-project-name' },
{ input: 'MyAwesomeApp', expected: 'myawesomeapp' },

// Special characters
{ input: '@scope/package', expected: 'scope-package' },
{ input: 'hello@world!test', expected: 'hello-world-test' },

// Multiple consecutive invalid chars
{ input: 'my..project__name', expected: 'my-project-name' },

// Leading/trailing invalid chars
{ input: '.my-project.', expected: 'my-project' },
{ input: '---test---', expected: 'test' },

// Numbers
{ input: 'project123', expected: 'project123' },
{ input: '123project', expected: '123project' },

// Empty/invalid fallback
{ input: '___', expected: 'undefined-project-name' },
{ input: '!@#$%', expected: 'undefined-project-name' },
{ input: '', expected: 'undefined-project-name' },

// Length limit (63 chars max)
{ input: 'a'.repeat(70), expected: 'a'.repeat(63) },
{
input: 'my-very-long-project-name-that-exceeds-the-limit-of-63-characters-allowed',
expected: 'my-very-long-project-name-that-exceeds-the-limit-of-63-characte'
},

// Truncation trap: slice leaves trailing dash
{ input: 'a'.repeat(62) + '-b', expected: 'a'.repeat(62) },

// Spaces
{ input: 'my cool project', expected: 'my-cool-project' },
{ input: ' spaced out ', expected: 'spaced-out' },

// Exact boundary (off-by-one check)
{ input: 'a'.repeat(63), expected: 'a'.repeat(63) },

// Unicode / accents / emojis (replaced with dashes)
{ input: 'piñata', expected: 'pi-ata' },
{ input: 'café', expected: 'caf' },
{ input: 'cool 🚀 app', expected: 'cool-app' }
];

it.each(testCases)('sanitizes "$input" to "$expected"', ({ input, expected }) => {
expect(sanitizeWranglerName(input)).toBe(expected);
});
});