Files
shakespeare/src/lib/configUtils.ts
T
Alex Gleason fc26a8b27b Deploy to the gateway that actually serves shakespeare.wtf now
Shakespeare Deploy uploaded a zip to a host that kept the files itself. That
host has been replaced: those domains are served out of nsite manifests, so a
deploy through the old API has been succeeding and changing nothing anyone can
see. This replaces it.

NpanelAdapter is NsiteAdapter plus a name. The site is an ordinary nsite under
the user's own key — same event, readable by any nsite client, gateway or no
gateway — and the second half of the deploy asks the gateway to point a hostname
at it. Its predecessor claimed names implicitly, by watching relays and
reserving whichever d tag it saw first; this one keeps an explicit row, which is
what makes a name something its owner holds rather than something they won a
race for. The hostname is asked about before it is claimed, because "nobody
holds this", "I already hold this" and "somebody else holds this" are three
different acts and creating-then-reading-the-409 cannot tell the last two apart.

The name field checks availability as you type, against an endpoint that takes
no credentials: somebody choosing a name has not decided to make an account yet.
A name this project already deployed to is not reported as taken — it is theirs.
An unreachable gateway reports available, because being wrong that way costs a
clear error at deploy time, and being wrong the other way tells someone a free
name is taken and they settle for worse.

Also fixes readDeploySettings, which swallowed a missing file and an unreadable
one alike and answered "no providers" to both. The default provider it was meant
to hand new users had therefore never reached anybody: it was written, saved
back as an empty list on the next render, and gone. The two cases are now
distinct, because inventing defaults for a file that exists but will not parse
would silently replace whatever the user had.

Providers and per-project deploy records saved under the old type are migrated
on load rather than dropped. The project file holds one record per provider a
project ever deployed to, so rejecting one entry would take a Netlify site id
and a Vercel project id with it.

Verified end to end against the live gateway: a fresh key self-enrolled,
uploaded a blob, published a manifest, claimed a name, and the site served
publicly with the SPA fallback working on a deep path.

The package-lock version bump is npm reconciling it with package.json, which
had drifted before this work.
2026-09-13 12:54:30 -05:00

374 lines
11 KiB
TypeScript

import z from 'zod';
import { filteredArray } from '@/lib/schema';
import { AI_PROVIDER_PRESETS } from '@/lib/aiProviderPresets';
import { DEFAULT_DEPLOY_PROVIDERS, migrateDeployProvider } from '@/lib/deployProviderPresets';
import type { JSRuntimeFS } from '@/lib/JSRuntime';
import type { AISettings, MCPServer } from '@/contexts/AISettingsContext';
import type { GitSettings, GitCredential } from '@/contexts/GitSettingsContext';
import type { DeployProvider, DeploySettings } from '@/contexts/DeploySettingsContext';
/**
* Get AI config file path
* @param configPath - Custom config path (default: /config)
*/
function getAIConfigPath(configPath = '/config'): string {
return `${configPath}/ai.json`;
}
/**
* Get Git config file path
* @param configPath - Custom config path (default: /config)
*/
function getGitConfigPath(configPath = '/config'): string {
return `${configPath}/git.json`;
}
/**
* Get Deploy config file path
* @param configPath - Custom config path (default: /config)
*/
function getDeployConfigPath(configPath = '/config'): string {
return `${configPath}/deploy.json`;
}
const aiProviderSchema = z.object({
id: z.string(),
name: z.string().optional(),
baseURL: z.string().url(),
apiKey: z.string().optional(),
nostr: z.boolean().optional(),
proxy: z.boolean().optional(),
openSecret: z.string().url().optional(),
});
const providerModelSchema = z.string().regex(
/^[^/]+\/.+$/,
'Must be in format provider/model (e.g., "openai/gpt-4o")',
);
const mcpServerSchema: z.ZodType<MCPServer> = z.object({
type: z.literal('streamable-http'),
url: z.string().url(),
headers: z.record(z.string(), z.string()).optional(),
});
const aiSettingsSchema = z.object({
providers: filteredArray(aiProviderSchema),
recentlyUsedModels: filteredArray(providerModelSchema),
imageModel: providerModelSchema.optional(),
mcpServers: z.record(z.string(), mcpServerSchema).optional(),
});
// New format for git credentials
const gitCredentialSchema = z.object({
id: z.string(),
name: z.string(),
origin: z.string(),
username: z.string(),
password: z.string(),
});
// Old format (map of origins to credentials)
const gitCredentialOldSchema = z.object({
username: z.string(),
password: z.string(),
});
// Schema that accepts both old and new formats
const gitSettingsSchema = z.object({
credentials: z.union([
z.array(gitCredentialSchema), // New format: array
z.record(z.string(), gitCredentialOldSchema), // Old format: map
]),
name: z.string().optional(),
email: z.string().optional(),
coAuthorEnabled: z.boolean().optional(),
});
const baseDeployProviderSchema = z.object({
id: z.string(),
name: z.string(),
proxy: z.boolean().optional(),
});
const npanelProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('npanel'),
dashboardHost: z.string(),
domain: z.string(),
relayUrls: z.array(z.string()),
blossomServers: z.array(z.string()),
});
const netlifyProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('netlify'),
apiKey: z.string(),
baseURL: z.string().optional(),
});
const vercelProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('vercel'),
apiKey: z.string(),
baseURL: z.string().optional(),
});
const nsiteProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('nsite'),
gateway: z.string(),
relayUrls: z.array(z.string()),
blossomServers: z.array(z.string()),
});
const cloudflareProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('cloudflare'),
apiKey: z.string(),
accountId: z.string(),
baseURL: z.string().optional(),
baseDomain: z.string().optional(),
});
const denoDeployProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('deno'),
apiKey: z.string(),
organizationId: z.string(),
baseURL: z.string().optional(),
baseDomain: z.string().optional(),
});
const railwayProviderSchema = baseDeployProviderSchema.extend({
type: z.literal('railway'),
apiKey: z.string(),
baseURL: z.string().optional(),
});
const deployProviderSchema: z.ZodType<DeployProvider> = z.discriminatedUnion('type', [
npanelProviderSchema,
netlifyProviderSchema,
vercelProviderSchema,
nsiteProviderSchema,
cloudflareProviderSchema,
denoDeployProviderSchema,
railwayProviderSchema,
]);
const deploySettingsSchema = z.object({
providers: filteredArray(deployProviderSchema),
});
/**
* Ensures the config directory exists
* @param fs - Filesystem instance
* @param configPath - Custom config path (default: /config)
*/
async function ensureConfigDir(fs: JSRuntimeFS, configPath = '/config'): Promise<void> {
try {
await fs.stat(configPath);
} catch {
await fs.mkdir(configPath, { recursive: true });
}
}
/**
* Read AI settings from VFS
* @param fs - Filesystem instance
* @param configPath - Custom config path (default: /config)
*/
export async function readAISettings(fs: JSRuntimeFS, configPath = '/config'): Promise<AISettings> {
const defaultSettings: AISettings = {
providers: [],
recentlyUsedModels: [],
mcpServers: {},
};
try {
const aiConfigPath = getAIConfigPath(configPath);
const content = await fs.readFile(aiConfigPath, 'utf8');
const data = JSON.parse(content);
const parsed = aiSettingsSchema.parse(data);
return {
...parsed,
providers: parsed.providers.map((provider) => {
let name = provider.name;
// Fill in name from presets if missing
if (typeof name !== 'string') {
const presetProvider = AI_PROVIDER_PRESETS.find(preset => preset.id === provider.id);
if (presetProvider) {
name = presetProvider.name;
}
}
// Fallback to id if name is still missing
if (typeof name !== 'string') {
name = provider.id;
}
return {
...provider,
name,
}
}),
mcpServers: parsed.mcpServers || {},
};
} catch (error) {
if (error instanceof z.ZodError) {
console.error('AI settings parsing error:', error.issues);
}
return defaultSettings;
}
}
/**
* Write AI settings to VFS
* @param fs - Filesystem instance
* @param settings - AI settings to write
* @param configPath - Custom config path (default: /config)
*/
export async function writeAISettings(fs: JSRuntimeFS, settings: AISettings, configPath = '/config'): Promise<void> {
await ensureConfigDir(fs, configPath);
const aiConfigPath = getAIConfigPath(configPath);
await fs.writeFile(aiConfigPath, JSON.stringify(settings, null, 2), 'utf8');
}
/**
* Generate a display name for a credential based on its hostname
*/
function generateCredentialName(host: string): string {
const hostname = host.split(':')[0]; // Remove port if present
if (hostname === 'github.com') {
return 'GitHub';
}
if (hostname === 'gitlab.com') {
return 'GitLab';
}
// For other hosts, use the hostname as the name
return hostname;
}
/**
* Convert old format credentials (map) to new format (array)
* This is only used for migrating from the old map-based format
*/
function convertCredentialsToNewFormat(
credentials: Record<string, { username: string; password: string }>
): GitCredential[] {
return Object.entries(credentials).map(([origin, cred]) => {
const url = new URL(origin);
const host = url.host; // Includes port if non-standard
return {
id: crypto.randomUUID(),
name: generateCredentialName(host),
origin,
username: cred.username,
password: cred.password,
};
});
}
/**
* Read Git settings from VFS
* @param fs - Filesystem instance
* @param configPath - Custom config path (default: /config)
*/
export async function readGitSettings(fs: JSRuntimeFS, configPath = '/config'): Promise<GitSettings> {
const defaultSettings: GitSettings = {
credentials: [],
coAuthorEnabled: true,
};
// Try to read from VFS first
try {
const gitConfigPath = getGitConfigPath(configPath);
const content = await fs.readFile(gitConfigPath, 'utf8');
const data = JSON.parse(content);
const parsed = gitSettingsSchema.parse(data);
// Convert old format to new format if needed
let credentials: GitCredential[];
if (Array.isArray(parsed.credentials)) {
credentials = parsed.credentials;
} else {
credentials = convertCredentialsToNewFormat(parsed.credentials);
}
return {
...parsed,
credentials,
};
} catch {
return defaultSettings;
}
}
/**
* Write Git settings to VFS
* @param fs - Filesystem instance
* @param settings - Git settings to write
* @param configPath - Custom config path (default: /config)
*/
export async function writeGitSettings(fs: JSRuntimeFS, settings: GitSettings, configPath = '/config'): Promise<void> {
await ensureConfigDir(fs, configPath);
const gitConfigPath = getGitConfigPath(configPath);
await fs.writeFile(gitConfigPath, JSON.stringify(settings, null, 2), 'utf8');
}
/**
* Read Deploy settings from VFS
*
* A user with no settings file is not the same as a user who removed every
* provider, and the difference is what a new user sees the first time they open
* the deploy button: the default gateway, or an empty screen asking them to
* choose a host. Both used to answer "no providers", so the default had never
* actually reached anybody — it was written, saved back as an empty list on the
* next render, and gone.
*
* @param fs - Filesystem instance
* @param configPath - Custom config path (default: /config)
*/
export async function readDeploySettings(fs: JSRuntimeFS, configPath = '/config'): Promise<DeploySettings> {
const deployConfigPath = getDeployConfigPath(configPath);
let content: string;
try {
content = await fs.readFile(deployConfigPath, 'utf8');
} catch {
// A copy, so the caller editing its providers can't reach back into the
// module-level defaults every other caller will be handed.
return { providers: [...DEFAULT_DEPLOY_PROVIDERS] };
}
try {
const data = JSON.parse(content) as { providers?: unknown };
const migrated = {
...data,
providers: Array.isArray(data?.providers) ? data.providers.map(migrateDeployProvider) : data?.providers,
};
return deploySettingsSchema.parse(migrated);
} catch (error) {
// A file that exists and cannot be read is a different problem from one that
// isn't there: handing back the defaults would silently replace whatever the
// user had, so this keeps nothing rather than inventing something.
if (error instanceof z.ZodError) {
console.error('Deploy settings parsing error:', error.issues);
}
return { providers: [] };
}
}
/**
* Write Deploy settings to VFS
* @param fs - Filesystem instance
* @param settings - Deploy settings to write
* @param configPath - Custom config path (default: /config)
*/
export async function writeDeploySettings(fs: JSRuntimeFS, settings: DeploySettings, configPath = '/config'): Promise<void> {
await ensureConfigDir(fs, configPath);
const deployConfigPath = getDeployConfigPath(configPath);
await fs.writeFile(deployConfigPath, JSON.stringify(settings, null, 2), 'utf8');
}