@gadicc/loom 1.31.1 → 1.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,6 +54,7 @@ exports.usePublishedDevPackage = usePublishedDevPackage;
54
54
  exports.syncDbSchemas = syncDbSchemas;
55
55
  exports.configureFilesIdGeneration = configureFilesIdGeneration;
56
56
  exports.buildVercelFilesEnvProbeArgs = buildVercelFilesEnvProbeArgs;
57
+ exports.setupLocalFiles = setupLocalFiles;
57
58
  exports.setupCloudflareR2 = setupCloudflareR2;
58
59
  exports.buildVercelNeonSetupCommand = buildVercelNeonSetupCommand;
59
60
  exports.setupVercelNeon = setupVercelNeon;
@@ -76,6 +77,8 @@ const promises_1 = require("node:fs/promises");
76
77
  const node_module_1 = require("node:module");
77
78
  const node_path_1 = require("node:path");
78
79
  const node_process_1 = __importDefault(require("node:process"));
80
+ const promises_2 = require("node:readline/promises");
81
+ const node_os_1 = require("node:os");
79
82
  const node_util_1 = require("node:util");
80
83
  const filesR2Policy_js_1 = require("./filesR2Policy.js");
81
84
  const releaseConfig_js_1 = require("./releaseConfig.js");
@@ -246,7 +249,7 @@ const DB_DEFAULT_DEPENDENCIES = {
246
249
  "valibot": "^1.4.2",
247
250
  };
248
251
  const DB_DEFAULT_DEV_DEPENDENCIES = {
249
- "@electric-sql/pglite": "^0.3.15",
252
+ "@electric-sql/pglite": "^0.5.8",
250
253
  "drizzle-kit": "^0.31.10",
251
254
  "drizzle-valibot": "^0.4.2",
252
255
  "tsx": "^4.23.1",
@@ -280,6 +283,7 @@ const FILES_S3_NON_SECRET_ENV_VALUES = {
280
283
  };
281
284
  const FILES_R2_ENVIRONMENTS = ["production", "preview"];
282
285
  const DEFAULT_MINIO_ENDPOINT = "http://127.0.0.1:9000";
286
+ const FILES_LOCAL_ENV_NAMES = FILES_S3_REQUIRED_ENV_NAMES;
283
287
  const API_KEY_AUDIT_SCHEMA_BLUEPRINT_VERSION = 3;
284
288
  const API_KEY_AUDIT_SCHEMA_DEFAULT_EXPORT = "loomApiKeyAuditEventsTable";
285
289
  const API_KEY_AUDIT_SCHEMA_DEFAULT_TABLE = "loom_api_key_audit_events";
@@ -5843,17 +5847,31 @@ async function configureFilesIdGeneration(cwd, idGeneration) {
5843
5847
  }
5844
5848
  function parseEnvValue(rawValue) {
5845
5849
  const trimmed = rawValue.trim();
5846
- if (trimmed.length >= 2 &&
5847
- ((trimmed.startsWith('"') && trimmed.endsWith('"')) ||
5848
- (trimmed.startsWith("'") && trimmed.endsWith("'")))) {
5849
- return trimmed.slice(1, -1);
5850
+ const quote = trimmed[0];
5851
+ if (quote === '"' || quote === "'" || quote === "`") {
5852
+ for (let index = 1; index < trimmed.length; index++) {
5853
+ if (trimmed[index] === "\\") {
5854
+ index++;
5855
+ continue;
5856
+ }
5857
+ if (trimmed[index] !== quote)
5858
+ continue;
5859
+ const trailing = trimmed.slice(index + 1).trim();
5860
+ if (!trailing || trailing.startsWith("#")) {
5861
+ const value = trimmed.slice(1, index);
5862
+ return quote === '"'
5863
+ ? value.replaceAll("\\n", "\n").replaceAll("\\r", "\r")
5864
+ : value;
5865
+ }
5866
+ break;
5867
+ }
5850
5868
  }
5851
- return trimmed;
5869
+ return trimmed.split("#", 1)[0].trim();
5852
5870
  }
5853
5871
  function readEnvAssignments(text) {
5854
5872
  const assignments = new Map();
5855
5873
  for (const [lineIndex, line] of normalizeText(text).split("\n").entries()) {
5856
- const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
5874
+ const match = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
5857
5875
  if (!match)
5858
5876
  continue;
5859
5877
  assignments.set(match[1], {
@@ -6114,6 +6132,420 @@ async function ensureMinioBucket(cwd, commandRunner, env, endpoint, bucket, acce
6114
6132
  ok: false,
6115
6133
  };
6116
6134
  }
6135
+ async function promptForLocalFilesValue(question, secret = false) {
6136
+ if (!node_process_1.default.stdin.isTTY) {
6137
+ throw new Error("MinIO admin credentials are required. Run interactively or set MINIO_ROOT_USER and MINIO_ROOT_PASSWORD in the command environment.");
6138
+ }
6139
+ if (!secret) {
6140
+ const input = (0, promises_2.createInterface)({
6141
+ input: node_process_1.default.stdin,
6142
+ output: node_process_1.default.stderr,
6143
+ });
6144
+ try {
6145
+ return (await input.question(question)).trim();
6146
+ }
6147
+ finally {
6148
+ input.close();
6149
+ }
6150
+ }
6151
+ node_process_1.default.stderr.write(question);
6152
+ const stdin = node_process_1.default.stdin;
6153
+ const wasRaw = stdin.isRaw;
6154
+ stdin.setRawMode(true);
6155
+ stdin.resume();
6156
+ try {
6157
+ return await new Promise((resolvePromise, rejectPromise) => {
6158
+ let value = "";
6159
+ const onData = (chunk) => {
6160
+ for (const character of new TextDecoder().decode(chunk)) {
6161
+ if (character === "\r" || character === "\n") {
6162
+ stdin.off("data", onData);
6163
+ node_process_1.default.stderr.write("\n");
6164
+ resolvePromise(value);
6165
+ return;
6166
+ }
6167
+ if (character === "\u0003") {
6168
+ stdin.off("data", onData);
6169
+ rejectPromise(new Error("MinIO setup was cancelled."));
6170
+ return;
6171
+ }
6172
+ if (character === "\u007f" || character === "\b") {
6173
+ value = value.slice(0, -1);
6174
+ }
6175
+ else {
6176
+ value += character;
6177
+ }
6178
+ }
6179
+ };
6180
+ stdin.on("data", onData);
6181
+ });
6182
+ }
6183
+ finally {
6184
+ stdin.setRawMode(wasRaw);
6185
+ stdin.pause();
6186
+ }
6187
+ }
6188
+ function minioHostEnv(env, endpoint, accessKey, secretKey) {
6189
+ const url = new URL(endpoint);
6190
+ url.username = accessKey;
6191
+ url.password = secretKey;
6192
+ return { ...env, MC_HOST_loomlocal: url.toString() };
6193
+ }
6194
+ async function checkMinioAdmin(cwd, runner, command, env, endpoint, user, password) {
6195
+ const result = await runner(command, ["admin", "info", "loomlocal"], {
6196
+ cwd,
6197
+ env: minioHostEnv(env, endpoint, user, password),
6198
+ stdio: "pipe",
6199
+ });
6200
+ return result.code === 0;
6201
+ }
6202
+ function filesBucketPolicy(bucket) {
6203
+ return {
6204
+ Version: "2012-10-17",
6205
+ Statement: [
6206
+ {
6207
+ Effect: "Allow",
6208
+ Action: ["s3:GetBucketLocation", "s3:ListBucket"],
6209
+ Resource: [`arn:aws:s3:::${bucket}`],
6210
+ },
6211
+ {
6212
+ Effect: "Allow",
6213
+ Action: ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
6214
+ Resource: [`arn:aws:s3:::${bucket}/*`],
6215
+ },
6216
+ ],
6217
+ };
6218
+ }
6219
+ async function verifyMinioReadWrite(cwd, runner, command, env, endpoint, bucket, accessKey, secretKey) {
6220
+ const target = `loomlocal/${bucket}/.loom-setup-${(0, node_crypto_1.randomUUID)()}`;
6221
+ const keyEnv = minioHostEnv(env, endpoint, accessKey, secretKey);
6222
+ const payload = "loom-files-local-check\n";
6223
+ const put = await runner(command, ["pipe", target], {
6224
+ cwd,
6225
+ env: keyEnv,
6226
+ input: payload,
6227
+ stdio: "pipe",
6228
+ });
6229
+ if (put.code !== 0) {
6230
+ throw new Error("The MinIO access key cannot write to its bucket.");
6231
+ }
6232
+ let readOk = false;
6233
+ let removeOk = false;
6234
+ try {
6235
+ const get = await runner(command, ["cat", target], {
6236
+ cwd,
6237
+ env: keyEnv,
6238
+ stdio: "pipe",
6239
+ });
6240
+ readOk = get.code === 0 && get.stdout === payload;
6241
+ }
6242
+ finally {
6243
+ const remove = await runner(command, ["rm", target], {
6244
+ cwd,
6245
+ env: keyEnv,
6246
+ stdio: "pipe",
6247
+ });
6248
+ removeOk = remove.code === 0;
6249
+ }
6250
+ if (!removeOk) {
6251
+ throw new Error("The MinIO access key could not remove its setup verification object.");
6252
+ }
6253
+ if (!readOk) {
6254
+ throw new Error("The MinIO access key cannot read from its bucket.");
6255
+ }
6256
+ }
6257
+ /**
6258
+ * Configures Files against local MinIO, preserving any existing app key and
6259
+ * verifying a newly created bucket-scoped key before writing it to .env.local.
6260
+ */
6261
+ async function setupLocalFiles(cwd, options = {}) {
6262
+ const runner = options.commandRunner ?? defaultCommandRunner;
6263
+ const env = options.env ?? node_process_1.default.env;
6264
+ const prompt = options.prompt ?? promptForLocalFilesValue;
6265
+ const manifestPath = (0, node_path_1.join)(cwd, "loom.json");
6266
+ const manifestText = await readTextIfExists(manifestPath);
6267
+ if (!manifestText) {
6268
+ throw new Error("Missing loom.json. Run `loom add files` first.");
6269
+ }
6270
+ const manifest = parseJson(manifestText, "loom.json");
6271
+ const feature = manifest.features.files;
6272
+ if (feature?.status !== "active") {
6273
+ throw new Error("The files feature is not active. Run `loom add files` first.");
6274
+ }
6275
+ const { packageJson } = await readProjectPackageJson(cwd);
6276
+ const framework = await detectFramework(cwd, packageJson);
6277
+ const dbFeature = manifest.features.db;
6278
+ if (dbFeature?.status !== "active") {
6279
+ throw new Error("The files feature requires the active DB feature.");
6280
+ }
6281
+ const config = getFilesConfig(framework, mergeDbConfig(framework, dbFeature.config), feature.config);
6282
+ if (config.storage.provider?.localDevelopment.kind === "none") {
6283
+ throw new Error("Files local storage is disabled in loom.json. Change the provider policy before running local setup.");
6284
+ }
6285
+ if (config.storage.provider?.localDevelopment.kind === "r2") {
6286
+ throw new Error("Files local storage is set to R2. Change the local provider policy to MinIO before running local setup.");
6287
+ }
6288
+ const storagePath = (0, node_path_1.join)(cwd, config.modules.storageModule);
6289
+ const storageText = await readTextIfExists(storagePath);
6290
+ if (storageText && !isGeneratedFilesStorageStub(storageText) &&
6291
+ config.storage.adapter !== "s3") {
6292
+ throw new Error(`Files uses a custom storage adapter at ${config.modules.storageModule}; configure it locally without replacing it.`);
6293
+ }
6294
+ const envPath = (0, node_path_1.join)(cwd, ".env.local");
6295
+ const envText = await readTextIfExists(envPath);
6296
+ const tracked = await runner("git", [
6297
+ "ls-files",
6298
+ "--error-unmatch",
6299
+ "--",
6300
+ ".env.local",
6301
+ ], { cwd, env, stdio: "pipe" });
6302
+ if (tracked.code === 0) {
6303
+ throw new Error(".env.local is tracked by Git. Remove it from the index before Loom writes Files credentials.");
6304
+ }
6305
+ try {
6306
+ if ((await (0, promises_1.lstat)(envPath)).isSymbolicLink()) {
6307
+ throw new Error(".env.local is a symlink; Loom will not write credentials through it.");
6308
+ }
6309
+ }
6310
+ catch (error) {
6311
+ if (!isNodeErrorCode(error, "ENOENT"))
6312
+ throw error;
6313
+ }
6314
+ const assignments = readEnvAssignments(envText ?? "");
6315
+ const bucket = normalizeBucketName(options.localBucket ??
6316
+ assignments.get("FILES_S3_BUCKET")?.value ??
6317
+ config.storage.provider?.localDevelopment.bucket ??
6318
+ `${getDefaultFilesBucketBase(cwd, packageJson, manifest)}-dev`, "--local-bucket");
6319
+ let endpoint = options.localEndpoint ??
6320
+ assignments.get("FILES_S3_ENDPOINT")?.value ??
6321
+ config.storage.provider?.localDevelopment.endpoint ??
6322
+ DEFAULT_MINIO_ENDPOINT;
6323
+ function validateEndpoint(value) {
6324
+ let url;
6325
+ try {
6326
+ url = new URL(value);
6327
+ }
6328
+ catch {
6329
+ throw new Error("--local-endpoint must be an HTTP(S) origin without credentials or a path.");
6330
+ }
6331
+ if (!["http:", "https:"].includes(url.protocol) || url.username ||
6332
+ url.password || url.pathname !== "/" || url.search || url.hash) {
6333
+ throw new Error("--local-endpoint must be an HTTP(S) origin without credentials or a path.");
6334
+ }
6335
+ return url.origin;
6336
+ }
6337
+ endpoint = validateEndpoint(endpoint);
6338
+ const client = await detectMinioClient(cwd, runner, env);
6339
+ if (!client) {
6340
+ throw new Error("MinIO Client is unavailable. Install `mcli` (Arch: `sudo pacman -S minio-client`) or `mc`, then rerun `loom files local setup`.");
6341
+ }
6342
+ const existingAccess = assignments.get("FILES_S3_ACCESS_KEY_ID")?.value;
6343
+ const existingSecret = assignments.get("FILES_S3_SECRET_ACCESS_KEY")?.value;
6344
+ if ((assignments.has("FILES_S3_ACCESS_KEY_ID") && !existingAccess) ||
6345
+ (assignments.has("FILES_S3_SECRET_ACCESS_KEY") && !existingSecret)) {
6346
+ throw new Error(".env.local contains an empty Files credential. Remove the empty assignment before local setup.");
6347
+ }
6348
+ if (Boolean(existingAccess) !== Boolean(existingSecret)) {
6349
+ throw new Error(".env.local has only one Files access credential. Restore the matching key and secret before local setup; Loom will not replace either one.");
6350
+ }
6351
+ let bucketStatus = existingAccess && existingSecret
6352
+ ? await ensureMinioBucket(cwd, runner, env, endpoint, bucket, existingAccess, existingSecret, { create: false })
6353
+ : null;
6354
+ if (bucketStatus?.ok && existingAccess && existingSecret) {
6355
+ await verifyMinioReadWrite(cwd, runner, client, env, endpoint, bucket, existingAccess, existingSecret);
6356
+ }
6357
+ let createdKey = null;
6358
+ if (!bucketStatus?.ok) {
6359
+ if (Boolean(env.MINIO_ROOT_USER) !== Boolean(env.MINIO_ROOT_PASSWORD)) {
6360
+ throw new Error("Set both MINIO_ROOT_USER and MINIO_ROOT_PASSWORD, or leave both unset for interactive MinIO setup.");
6361
+ }
6362
+ let adminUser = env.MINIO_ROOT_USER || "minioadmin";
6363
+ let adminPassword = env.MINIO_ROOT_PASSWORD || "minioadmin";
6364
+ let authenticated = await checkMinioAdmin(cwd, runner, client, env, endpoint, adminUser, adminPassword);
6365
+ if (!authenticated) {
6366
+ const promptedEndpoint = (await prompt(`MinIO endpoint [${endpoint}]: `)).trim();
6367
+ if (promptedEndpoint)
6368
+ endpoint = validateEndpoint(promptedEndpoint);
6369
+ adminUser = (await prompt("MinIO admin user: ")).trim();
6370
+ adminPassword = await prompt("MinIO admin password: ", true);
6371
+ authenticated = await checkMinioAdmin(cwd, runner, client, env, endpoint, adminUser, adminPassword);
6372
+ }
6373
+ if (!authenticated) {
6374
+ throw new Error("Could not authenticate to MinIO. Check the server endpoint and admin credentials, then rerun local setup.");
6375
+ }
6376
+ const adminEnv = minioHostEnv(env, endpoint, adminUser, adminPassword);
6377
+ bucketStatus = await ensureMinioBucket(cwd, runner, env, endpoint, bucket, adminUser, adminPassword);
6378
+ if (!bucketStatus.ok) {
6379
+ throw new Error(`Could not create or reuse MinIO bucket \`${bucket}\`. Check admin permissions and the endpoint.`);
6380
+ }
6381
+ if (existingAccess && existingSecret) {
6382
+ const existingCheck = await ensureMinioBucket(cwd, runner, env, endpoint, bucket, existingAccess, existingSecret, { create: false });
6383
+ if (!existingCheck.ok) {
6384
+ throw new Error("Existing FILES_S3 credentials cannot access the bucket. Loom preserved them; repair or remove them manually before rerunning setup.");
6385
+ }
6386
+ await verifyMinioReadWrite(cwd, runner, client, env, endpoint, bucket, existingAccess, existingSecret);
6387
+ }
6388
+ else {
6389
+ const policyDir = await (0, promises_1.mkdtemp)((0, node_path_1.join)((0, node_os_1.tmpdir)(), "loom-files-policy-"));
6390
+ const policyPath = (0, node_path_1.join)(policyDir, "bucket.json");
6391
+ try {
6392
+ await (0, promises_1.writeFile)(policyPath, `${JSON.stringify(filesBucketPolicy(bucket))}\n`, { mode: 0o600 });
6393
+ const result = await runner(client, [
6394
+ "--json",
6395
+ "admin",
6396
+ "accesskey",
6397
+ "create",
6398
+ "loomlocal/",
6399
+ "--policy",
6400
+ policyPath,
6401
+ ], { cwd, env: adminEnv, stdio: "pipe" });
6402
+ if (result.code !== 0) {
6403
+ throw new Error("MinIO could not create a bucket-scoped access key. Check that the client supports `admin accesskey create` and the admin account has permission.");
6404
+ }
6405
+ let parsed = {};
6406
+ try {
6407
+ parsed = JSON.parse(result.stdout.trim());
6408
+ }
6409
+ catch {
6410
+ throw new Error("MinIO returned malformed JSON while creating a local access key.");
6411
+ }
6412
+ if (typeof parsed.accessKey !== "string" ||
6413
+ typeof parsed.secretKey !== "string") {
6414
+ throw new Error("MinIO did not return an access key and secret key in JSON output.");
6415
+ }
6416
+ createdKey = {
6417
+ accessKey: parsed.accessKey,
6418
+ secretKey: parsed.secretKey,
6419
+ };
6420
+ }
6421
+ finally {
6422
+ await (0, promises_1.rm)(policyDir, { recursive: true, force: true });
6423
+ }
6424
+ try {
6425
+ const keyCheck = await ensureMinioBucket(cwd, runner, env, endpoint, bucket, createdKey.accessKey, createdKey.secretKey, { create: false });
6426
+ if (!keyCheck.ok) {
6427
+ throw new Error("The new MinIO access key could not read its bucket. Check the MinIO policy before rerunning setup.");
6428
+ }
6429
+ await verifyMinioReadWrite(cwd, runner, client, env, endpoint, bucket, createdKey.accessKey, createdKey.secretKey);
6430
+ }
6431
+ catch (error) {
6432
+ const cleanup = await runner(client, ["admin", "accesskey", "remove", "loomlocal/", createdKey.accessKey], { cwd, env: adminEnv, stdio: "pipe" });
6433
+ if (cleanup.code !== 0) {
6434
+ throw new Error("The new MinIO key failed verification and could not be removed automatically. Remove the newly created key in MinIO before retrying.");
6435
+ }
6436
+ throw error;
6437
+ }
6438
+ }
6439
+ }
6440
+ const desired = {
6441
+ FILES_S3_BUCKET: bucket,
6442
+ FILES_S3_ENDPOINT: endpoint,
6443
+ FILES_S3_FORCE_PATH_STYLE: "true",
6444
+ FILES_S3_REGION: "auto",
6445
+ FILES_STORAGE_PROVIDER: "minio",
6446
+ FILES_S3_ACCESS_KEY_ID: existingAccess ?? createdKey?.accessKey ?? "",
6447
+ FILES_S3_SECRET_ACCESS_KEY: existingSecret ?? createdKey?.secretKey ?? "",
6448
+ };
6449
+ const merged = mergeEnvValues(envText, desired, {
6450
+ neverReplace: new Set([
6451
+ "FILES_S3_ACCESS_KEY_ID",
6452
+ "FILES_S3_SECRET_ACCESS_KEY",
6453
+ ]),
6454
+ repair: true,
6455
+ });
6456
+ if (merged.conflicts.length) {
6457
+ throw new Error(`Could not preserve existing Files credentials: ${merged.conflicts.join(", ")}.`);
6458
+ }
6459
+ const changedFiles = [];
6460
+ const notes = [];
6461
+ const gitignorePath = (0, node_path_1.join)(cwd, ".gitignore");
6462
+ const gitignore = await readTextIfExists(gitignorePath) ?? "";
6463
+ const lastIgnoreRule = normalizeText(gitignore).split("\n")
6464
+ .map((line) => line.trim()).filter(Boolean).at(-1);
6465
+ if (lastIgnoreRule !== ".env.local" && lastIgnoreRule !== "/.env.local") {
6466
+ if (await writeTextIfChanged(gitignorePath, `${gitignore && !gitignore.endsWith("\n") ? `${gitignore}\n` : gitignore}.env.local\n`))
6467
+ changedFiles.push(".gitignore");
6468
+ }
6469
+ if (envText !== merged.text) {
6470
+ await (0, promises_1.writeFile)(envPath, merged.text, { mode: 0o600 });
6471
+ await (0, promises_1.chmod)(envPath, 0o600);
6472
+ changedFiles.push(".env.local");
6473
+ }
6474
+ const tooling = await getProjectTooling(cwd, manifest);
6475
+ const dependencyUpdate = await (0, projectTooling_js_1.mutateProjectDependencies)(cwd, tooling.dependencies, { dependencies: FILES_S3_DEPENDENCIES });
6476
+ if (dependencyUpdate.changed) {
6477
+ changedFiles.push(dependencyUpdate.config);
6478
+ notes.push(`Run ${tooling.dependencies.manager} install to update the dependency lockfile.`);
6479
+ }
6480
+ if (!storageText || isGeneratedFilesStorageStub(storageText)) {
6481
+ if (await writeTextIfChanged(storagePath, filesS3StorageTemplate())) {
6482
+ changedFiles.push(config.modules.storageModule);
6483
+ }
6484
+ }
6485
+ config.storage.adapter = "s3";
6486
+ if (config.storage.provider) {
6487
+ config.storage.provider.localDevelopment = {
6488
+ kind: "minio",
6489
+ bucket,
6490
+ endpoint,
6491
+ };
6492
+ }
6493
+ feature.config = config;
6494
+ if (await writeTextIfChanged(manifestPath, stringifyJson(manifest))) {
6495
+ changedFiles.push("loom.json");
6496
+ }
6497
+ return {
6498
+ changedFiles,
6499
+ notes: [
6500
+ bucketStatus?.detail ?? `MinIO bucket \`${bucket}\` is ready.`,
6501
+ createdKey
6502
+ ? "Created a bucket-scoped MinIO access key and saved it to .env.local."
6503
+ : "Reused existing Files access credentials.",
6504
+ ...notes,
6505
+ ],
6506
+ };
6507
+ }
6508
+ async function checkLocalFilesEnvironment(cwd, options = {}) {
6509
+ const issues = [];
6510
+ const suggestions = [];
6511
+ const warnings = [];
6512
+ const text = await readTextIfExists((0, node_path_1.join)(cwd, ".env.local"));
6513
+ const assignments = readEnvAssignments(text ?? "");
6514
+ const missing = FILES_LOCAL_ENV_NAMES.filter((name) => !assignments.get(name)?.value);
6515
+ if (!text || missing.length) {
6516
+ issues.push(!text
6517
+ ? "Files local storage is missing .env.local."
6518
+ : `Files .env.local is missing ${missing.join(", ")}.`);
6519
+ suggestions.push("Run `loom files local setup` to configure local MinIO.");
6520
+ suggestions.push("Install and start a MinIO server with `mcli` or `mc`, or configure another S3-compatible service with the canonical FILES_S3_* variables.");
6521
+ return { issues, suggestions, warnings };
6522
+ }
6523
+ const endpoint = assignments.get("FILES_S3_ENDPOINT")?.value ?? "";
6524
+ try {
6525
+ const url = new URL(endpoint);
6526
+ if (!["http:", "https:"].includes(url.protocol) || url.username ||
6527
+ url.password)
6528
+ throw new Error("invalid");
6529
+ }
6530
+ catch {
6531
+ issues.push("FILES_S3_ENDPOINT in .env.local must be an HTTP(S) URL without credentials.");
6532
+ }
6533
+ if (assignments.get("FILES_S3_FORCE_PATH_STYLE")?.value !== "true" &&
6534
+ assignments.get("FILES_STORAGE_PROVIDER")?.value === "minio") {
6535
+ issues.push("Local MinIO requires FILES_S3_FORCE_PATH_STYLE=true.");
6536
+ }
6537
+ if (options.verifyBucket && !issues.length) {
6538
+ const verified = await ensureMinioBucket(cwd, options.commandRunner ?? defaultCommandRunner, options.env ?? node_process_1.default.env, endpoint, assignments.get("FILES_S3_BUCKET")?.value ?? "", assignments.get("FILES_S3_ACCESS_KEY_ID")?.value ?? "", assignments.get("FILES_S3_SECRET_ACCESS_KEY")?.value ?? "", { create: false });
6539
+ if (!verified.ok) {
6540
+ issues.push("Files local bucket or credentials could not be verified.");
6541
+ suggestions.push("Start the configured storage server and check its endpoint, bucket, and credentials; use `loom files local setup` for MinIO.");
6542
+ if (verified.detail.includes("not available")) {
6543
+ suggestions.push("Install MinIO Client as `mcli` (Arch: `sudo pacman -S minio-client`) or `mc`.");
6544
+ }
6545
+ }
6546
+ }
6547
+ return { issues, suggestions, warnings };
6548
+ }
6117
6549
  function filesR2ProviderMatches(existing, desired) {
6118
6550
  if (!existing)
6119
6551
  return true;
@@ -6278,7 +6710,7 @@ async function setupCloudflareR2(cwd, options = {}) {
6278
6710
  const envLocalText = await readTextIfExists(envLocalPath);
6279
6711
  const localAssignments = readEnvAssignments(envLocalText ?? "");
6280
6712
  const localBucket = normalizeBucketName(options.localBucket ?? existingProvider?.localDevelopment.bucket ??
6281
- localAssignments.get("FILES_S3_BUCKET")?.value ?? `${baseName}-files`, "--local-bucket");
6713
+ localAssignments.get("FILES_S3_BUCKET")?.value ?? `${baseName}-dev`, "--local-bucket");
6282
6714
  const localEndpoint = options.localEndpoint ??
6283
6715
  existingProvider?.localDevelopment.endpoint ??
6284
6716
  localAssignments.get("FILES_S3_ENDPOINT")?.value ??
@@ -6360,13 +6792,6 @@ async function setupCloudflareR2(cwd, options = {}) {
6360
6792
  ? "minio"
6361
6793
  : "cloudflare-r2",
6362
6794
  };
6363
- if (!localAssignments.has("FILES_S3_ACCESS_KEY_ID") && env.MINIO_ROOT_USER) {
6364
- desiredLocalValues.FILES_S3_ACCESS_KEY_ID = env.MINIO_ROOT_USER;
6365
- }
6366
- if (!localAssignments.has("FILES_S3_SECRET_ACCESS_KEY") &&
6367
- env.MINIO_ROOT_PASSWORD) {
6368
- desiredLocalValues.FILES_S3_SECRET_ACCESS_KEY = env.MINIO_ROOT_PASSWORD;
6369
- }
6370
6795
  const localMerge = mergeEnvValues(envLocalText, desiredLocalValues, {
6371
6796
  neverReplace: new Set([
6372
6797
  "FILES_S3_ACCESS_KEY_ID",
@@ -6390,7 +6815,7 @@ async function setupCloudflareR2(cwd, options = {}) {
6390
6815
  notes.push(minio.detail);
6391
6816
  }
6392
6817
  else if (!localAccessKey || !localSecretKey) {
6393
- notes.push("Local storage credentials were not found. Add FILES_S3_ACCESS_KEY_ID and FILES_S3_SECRET_ACCESS_KEY to gitignored .env.local; Loom did not synthesize credentials.");
6818
+ notes.push("Local storage credentials were not found. Run `loom files local setup` to create a bucket-scoped MinIO key, or add credentials for another S3-compatible provider to gitignored .env.local.");
6394
6819
  }
6395
6820
  }
6396
6821
  const envListings = new Map();
@@ -7486,7 +7911,7 @@ function checkFilesNetworkFeature(context) {
7486
7911
  if (!text) {
7487
7912
  return networkCheckResult("issue", "Gitignored .env.local is missing.", {
7488
7913
  issues: ["Local MinIO Files configuration is missing."],
7489
- suggestions: ["Run `loom files r2 setup`."],
7914
+ suggestions: ["Run `loom files local setup`."],
7490
7915
  });
7491
7916
  }
7492
7917
  const assignments = readEnvAssignments(text);
@@ -7497,7 +7922,7 @@ function checkFilesNetworkFeature(context) {
7497
7922
  `Local MinIO Files variables are incomplete: ${missing.join(", ")}.`,
7498
7923
  ],
7499
7924
  suggestions: [
7500
- "Add the existing MinIO credentials to .env.local; Loom never synthesizes or replaces them.",
7925
+ "Run `loom files local setup` to create a scoped MinIO key, or add an existing credential pair to .env.local.",
7501
7926
  ],
7502
7927
  });
7503
7928
  }
@@ -7511,7 +7936,7 @@ function checkFilesNetworkFeature(context) {
7511
7936
  "Local MinIO is configured but the bucket could not be verified.",
7512
7937
  ],
7513
7938
  suggestions: [
7514
- "Start MinIO or correct FILES_S3_ENDPOINT, then rerun `loom files r2 check`.",
7939
+ "Start MinIO or correct FILES_S3_ENDPOINT, then rerun `loom files local check`.",
7515
7940
  ],
7516
7941
  });
7517
7942
  },
@@ -9113,6 +9538,17 @@ async function checkFilesFeature(cwd, framework, manifest, options) {
9113
9538
  : "Rerun `loom files r2 setup`; existing buckets and working credentials will be reused.");
9114
9539
  }
9115
9540
  }
9541
+ if (!options.production && provider?.localDevelopment.kind !== "none" &&
9542
+ (config.storage.adapter === "s3" ||
9543
+ (storageText !== null && isGeneratedFilesStorageStub(storageText)))) {
9544
+ const local = await checkLocalFilesEnvironment(cwd);
9545
+ if (config.storage.adapter === "s3")
9546
+ issues.push(...local.issues);
9547
+ else
9548
+ warnings.push(...local.issues);
9549
+ for (const suggestion of local.suggestions)
9550
+ suggestions.add(suggestion);
9551
+ }
9116
9552
  const routeText = await readTextIfExists((0, node_path_1.join)(cwd, config.entrypoints.routeHandler));
9117
9553
  if (!routeText) {
9118
9554
  issues.push(`Files route handler is missing: ${config.entrypoints.routeHandler}`);
@@ -10845,8 +11281,9 @@ function printHelp({ out }) {
10845
11281
  out(" init Set up package/Deno config, loom.json, managed AGENTS.md guidance, first-party links, and package skills.");
10846
11282
  out(" add <feature> Activate a shared feature. Run `loom add` to list available features.");
10847
11283
  out(" files id-generation uuid-v4|uuid-v7 Select the managed Files schema ID default.");
10848
- out(" files r2 setup Create/reuse private Cloudflare R2 buckets, configure Vercel Production/Preview, and configure local MinIO by default.");
10849
- out(" files r2 check Verify R2 buckets/account/privacy, Vercel env scoping, local MinIO, and Files adapter wiring.");
11284
+ out(" files local setup|check Provision or verify local MinIO and .env.local without R2/Vercel.");
11285
+ out(" files r2 setup Create/reuse private Cloudflare R2 buckets and configure Vercel Production/Preview.");
11286
+ out(" files r2 check Verify R2 buckets/account/privacy, Vercel env scoping, local storage, and Files adapter wiring.");
10850
11287
  out(" api-keys integrate jobs Record an explicit API-key jobs integration after the route has session/admin authorization.");
10851
11288
  out(" api-keys retention enable|disable Record API-key audit cleanup policy; scheduling remains app-owned.");
10852
11289
  out(" status Show the Loom package, manifest, active features, local-dev state, and project skill state.");
@@ -10960,8 +11397,53 @@ async function runFiles(cwd, command, target, options, io) {
10960
11397
  io.out(`Note: ${note}`);
10961
11398
  return 0;
10962
11399
  }
11400
+ if (command === "local") {
11401
+ if (target === "setup") {
11402
+ const result = await setupLocalFiles(cwd, {
11403
+ localBucket: options.localBucket,
11404
+ localEndpoint: options.localEndpoint,
11405
+ });
11406
+ io.out(result.changedFiles.length
11407
+ ? `Updated ${result.changedFiles.join(", ")}.`
11408
+ : "Files local storage already matches the configuration.");
11409
+ for (const note of result.notes)
11410
+ io.out(`Note: ${note}`);
11411
+ return 0;
11412
+ }
11413
+ if (target === "check") {
11414
+ const manifestText = await readTextIfExists((0, node_path_1.join)(cwd, "loom.json"));
11415
+ const manifest = manifestText
11416
+ ? parseJson(manifestText, "loom.json")
11417
+ : null;
11418
+ if (manifest?.features.files?.status !== "active") {
11419
+ io.err("The files feature is not active. Run `loom add files` first.");
11420
+ return 1;
11421
+ }
11422
+ const filesConfig = manifest.features.files.config;
11423
+ if (filesConfig?.storage?.provider?.localDevelopment.kind === "none") {
11424
+ io.out("Files local storage is disabled in loom.json.");
11425
+ return 0;
11426
+ }
11427
+ const result = await checkLocalFilesEnvironment(cwd, {
11428
+ verifyBucket: true,
11429
+ });
11430
+ if (result.issues.length) {
11431
+ io.err("loom files local check found setup issues:");
11432
+ for (const issue of result.issues)
11433
+ io.err(`- ${issue}`);
11434
+ for (const suggestion of result.suggestions) {
11435
+ io.err(`Suggestion: ${suggestion}`);
11436
+ }
11437
+ return 1;
11438
+ }
11439
+ io.out("loom files local check: setup looks good.");
11440
+ return 0;
11441
+ }
11442
+ io.err("Usage: loom files local setup|check [--local-endpoint <url>] [--local-bucket <name>]");
11443
+ return 1;
11444
+ }
10963
11445
  if (command !== "r2" || !target) {
10964
- io.err("Usage: loom files id-generation uuid-v4|uuid-v7\n or: loom files r2 setup|check [R2 options]");
11446
+ io.err("Usage: loom files id-generation uuid-v4|uuid-v7\n or: loom files local setup|check\n or: loom files r2 setup|check [R2 options]");
10965
11447
  return 1;
10966
11448
  }
10967
11449
  const jurisdiction = options.jurisdiction;
@@ -20,6 +20,15 @@ It does not own:
20
20
  - domain-specific Valibot refinements
21
21
  - fixture table selection policy
22
22
 
23
+ For a local Neon HTTP proxy on a different port, set
24
+ `LOOM_DB_LOCAL_HTTP_ENDPOINT=http://db.localtest.me:4445/sql` in the consumer's
25
+ local environment. The override applies only when `DATABASE_URL` uses
26
+ `db.localtest.me`. Without it, Loom uses `http://db.localtest.me:4444/sql`;
27
+ remote database hosts still use HTTPS port 443. The override accepts only an
28
+ HTTP(S) `/sql` URL at `db.localtest.me`, `localhost`, `127.0.0.1`, or `[::1]`,
29
+ without credentials, query parameters, or a fragment. An invalid value fails
30
+ when the local HTTP endpoint is used.
31
+
23
32
  Schema-bearing Loom features may own versioned blueprints that are materialized
24
33
  into committed app-local schema files. `loom db schemas sync` refreshes those
25
34
  files only when the current file still matches the hash recorded in `loom.json`;