@vibiz/cli 0.2.1 → 0.2.3

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.
@@ -126,6 +126,30 @@ var Vibiz = class {
126
126
  envLinks: (id) => this.request(
127
127
  `/projects/${id}/env?names=1`
128
128
  ),
129
+ /**
130
+ * The app's own variables (a third-party key, a setting): write-only.
131
+ * `list` gives names and when they were set, never values; the values
132
+ * reach the app in its env (loadEnv, env pull) and its connected hosts.
133
+ */
134
+ vars: {
135
+ list: (id) => this.request(`/projects/${id}/env/vars`).then((r) => r.data),
136
+ /** Production needs a full-access key; a Limited key gets `forbidden` with the console link. */
137
+ set: (id, name, value, environment = "production") => this.request(`/projects/${id}/env/${encodeURIComponent(name)}`, {
138
+ method: "PUT",
139
+ body: { value, environment }
140
+ }),
141
+ remove: (id, name, environment = "production") => this.request(
142
+ `/projects/${id}/env/${encodeURIComponent(name)}?environment=${environment}`,
143
+ { method: "DELETE" }
144
+ )
145
+ },
146
+ /**
147
+ * The console link where a person types one secret, write-only: hand it
148
+ * over instead of asking for the value in a chat. `kind`: env (the app's
149
+ * own variable, default), connector (a connector's key), ai (a lab key,
150
+ * name e.g. `google`) or host (a host token, name e.g. `vercel`).
151
+ */
152
+ secretLink: (id, input) => this.request(`/projects/${id}/secret-link`, { method: "POST", body: input }),
129
153
  /**
130
154
  * The development environment: its own database branch (schema, no
131
155
  * production rows), its own sign-in and AI key. What agents and laptops use.
@@ -689,6 +713,14 @@ var Vibiz = class {
689
713
  (r) => r.data
690
714
  ),
691
715
  get: (id) => this.request(`/deployments/${id}`),
716
+ /** The deployment's code as deployed: every file's path, content hash and size. */
717
+ files: (id) => this.request(`/deployments/${encodeURIComponent(id)}/files`),
718
+ /** One file's bytes, by its path in the deployment. */
719
+ readFile: async (id, path) => {
720
+ const encoded = path.replace(/^\/+/, "").split("/").map(encodeURIComponent).join("/");
721
+ const res = await this.stream(`/deployments/${encodeURIComponent(id)}/files/${encoded}`);
722
+ return new Uint8Array(await res.arrayBuffer());
723
+ },
692
724
  promote: (id) => this.request(
693
725
  `/deployments/${id}/promote`,
694
726
  { method: "POST" }
@@ -1096,6 +1128,27 @@ var Vibiz = class {
1096
1128
  body: input
1097
1129
  }),
1098
1130
  authCode: (domain) => this.request(`/domains/${encodeURIComponent(domain)}/auth-code`, { method: "POST", body: {} }),
1131
+ /**
1132
+ * Use a bought domain (or a name under it) on any of the account's
1133
+ * projects: no DNS to set, trusted for sign-in at once. Any key. A name
1134
+ * another project uses answers 409 `in_use` with the move.
1135
+ */
1136
+ use: (domain, projectId, input = {}) => this.request(`/domains/${encodeURIComponent(domain)}/use`, {
1137
+ method: "POST",
1138
+ body: { project_id: projectId, ...input }
1139
+ }),
1140
+ /** Full-access keys: move a name from the project using it to `toProjectId`. */
1141
+ move: (domain, toProjectId, input = {}) => this.request(`/domains/${encodeURIComponent(domain)}/move`, {
1142
+ method: "POST",
1143
+ body: { to_project_id: toProjectId, ...input }
1144
+ }),
1145
+ /** A bought name used by an app elsewhere: point it at that host (`null` clears it). */
1146
+ point: (domain, input) => this.request(`/domains/${encodeURIComponent(domain)}/point`, {
1147
+ method: "POST",
1148
+ body: input
1149
+ }),
1150
+ /** Buy for the account: on `project_id` once registered, or none to use later. */
1151
+ buy: (input) => this.request("/domains/buy", { method: "POST", body: input }),
1099
1152
  owner: () => this.request("/domains/owner"),
1100
1153
  setOwner: (contact) => this.request("/domains/owner", { method: "PUT", body: { contact } }),
1101
1154
  /**
@@ -1377,6 +1430,167 @@ async function deployDirectory(vibiz, projectId, root, opts = {}) {
1377
1430
  });
1378
1431
  return { deployment, uploaded: uploadedShas.size, total: files.length };
1379
1432
  }
1433
+ var INLINE_LIMITS = {
1434
+ files: 300,
1435
+ fileBytes: 3 * 1024 * 1024,
1436
+ totalBytes: 4 * 1024 * 1024,
1437
+ pathChars: 512
1438
+ };
1439
+ var BIG_APP = "For a bigger app, deploy its folder with the CLI: npx @vibiz/cli deploy";
1440
+ function invalid(message, details = {}) {
1441
+ throw new VibizApiError(400, "invalid_request", message, details);
1442
+ }
1443
+ function deployPathProblem(path) {
1444
+ if (!path) return "is empty";
1445
+ if (path.length > INLINE_LIMITS.pathChars)
1446
+ return `is longer than ${INLINE_LIMITS.pathChars} characters`;
1447
+ if (path.includes("\\")) return "uses a backslash: write paths with forward slashes (src/app.js)";
1448
+ if (path.startsWith("/") || /^[a-z]:/i.test(path))
1449
+ return "is absolute: write it relative to the app root (index.html, src/app.js)";
1450
+ if (/[\u0000-\u001f]/.test(path)) return "has a control character";
1451
+ for (const part of path.split("/")) {
1452
+ if (part === "" || part === ".") return 'has an empty or "." segment';
1453
+ if (part === "..") return 'climbs out of the app with ".."';
1454
+ if (isEnvFile(part))
1455
+ return "is an env file, which is never deployed: set secrets with a console link (vibiz_request_secret) instead";
1456
+ if (isSecretFile(part))
1457
+ return "looks like a credential (a private key or a token file), which is never deployed";
1458
+ if (PRIVATE_DIRS.includes(part)) return `is inside ${part}, a home or config folder`;
1459
+ if (IGNORED.has(part))
1460
+ return part === "vibiz.json" ? "is vibiz.json, the local link to the project, which is never deployed" : `is inside ${part}, which is never deployed (dependencies, VCS data or build output: Vibiz installs and builds the app itself)`;
1461
+ }
1462
+ return null;
1463
+ }
1464
+ function normalizeDeployPath(path, field = "path") {
1465
+ const clean = path.replace(/^(\.\/)+/, "");
1466
+ const problem = deployPathProblem(clean);
1467
+ if (problem)
1468
+ invalid(`The ${field} ${JSON.stringify(path.slice(0, 200))} ${problem}.`, {
1469
+ issues: [{ path: field, message: problem }]
1470
+ });
1471
+ return clean;
1472
+ }
1473
+ function decode(f, i) {
1474
+ const where = `files[${i}] (${f.path})`;
1475
+ if (typeof f.content !== "string") invalid(`${where} has no content: pass content as a string.`);
1476
+ if (f.encoding === "base64") {
1477
+ const b64 = f.content.replace(/\s+/g, "");
1478
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(b64) || b64.length % 4 === 1)
1479
+ invalid(
1480
+ `${where} is not valid base64: send it as base64 without a data: prefix, or as utf8 text.`
1481
+ );
1482
+ return new Uint8Array(Buffer.from(b64, "base64"));
1483
+ }
1484
+ return new Uint8Array(Buffer.from(f.content, "utf8"));
1485
+ }
1486
+ async function deployFiles(vibiz, projectId, files, opts = {}) {
1487
+ const deletedIn = opts.deleted ?? [];
1488
+ if (!files.length && !deletedIn.length)
1489
+ invalid("Nothing to deploy: pass files [{ path, content }] (and deleted, to remove some).");
1490
+ if (files.length > INLINE_LIMITS.files)
1491
+ invalid(
1492
+ `${files.length} files is more than the ${INLINE_LIMITS.files} one call can carry. Send only the files that changed (the live deployment is the base), or split them over several calls with base set to the previous deployment's id. ${BIG_APP}.`,
1493
+ { limit: INLINE_LIMITS.files }
1494
+ );
1495
+ const incoming = /* @__PURE__ */ new Map();
1496
+ let totalBytes = 0;
1497
+ files.forEach((f, i) => {
1498
+ const path = normalizeDeployPath(f.path, `files[${i}].path`);
1499
+ if (incoming.has(path)) invalid(`${path} is in files twice: send each path once.`);
1500
+ const bytes = decode(f, i);
1501
+ if (bytes.byteLength > INLINE_LIMITS.fileBytes)
1502
+ invalid(
1503
+ `${path} is ${(bytes.byteLength / 1048576).toFixed(1)} MiB, over the ${INLINE_LIMITS.fileBytes / 1048576} MiB a file can be in one call. Host big media in the project's Files (vibiz_files import) and link to it. ${BIG_APP}.`,
1504
+ { path, limit_bytes: INLINE_LIMITS.fileBytes }
1505
+ );
1506
+ totalBytes += bytes.byteLength;
1507
+ incoming.set(path, { bytes, sha: createHash("sha1").update(bytes).digest("hex") });
1508
+ });
1509
+ if (totalBytes > INLINE_LIMITS.totalBytes)
1510
+ invalid(
1511
+ `These files add up to ${(totalBytes / 1048576).toFixed(1)} MiB, over the ${INLINE_LIMITS.totalBytes / 1048576} MiB one call can carry. Split them over several calls (each with base set to the previous deployment's id), or move big media to the project's Files. ${BIG_APP}.`,
1512
+ { limit_bytes: INLINE_LIMITS.totalBytes }
1513
+ );
1514
+ const toDelete = /* @__PURE__ */ new Set();
1515
+ for (const [i, p] of deletedIn.entries()) {
1516
+ const path = normalizeDeployPath(p, `deleted[${i}]`);
1517
+ if (incoming.has(path)) invalid(`${path} is both in files and in deleted: pick one.`);
1518
+ toDelete.add(path);
1519
+ }
1520
+ let baseId = null;
1521
+ const choice = opts.base ?? "live";
1522
+ if (choice === "live") {
1523
+ const project = await vibiz.projects.get(projectId);
1524
+ baseId = project.production_deployment_id;
1525
+ if (!baseId && opts.base === "live")
1526
+ invalid(
1527
+ `${projectId} has nothing live yet, so there is no base to edit: send every file of the app with base "none".`
1528
+ );
1529
+ } else if (choice !== "none") {
1530
+ baseId = choice;
1531
+ }
1532
+ let baseFiles = [];
1533
+ if (baseId) {
1534
+ const listed = await vibiz.deployments.files(baseId);
1535
+ if (listed.project_id !== projectId)
1536
+ invalid(
1537
+ `The base ${baseId} belongs to another project (${listed.project_id}): pass a deployment of ${projectId}.`
1538
+ );
1539
+ baseFiles = listed.data;
1540
+ }
1541
+ const manifest = new Map(baseFiles.map((f) => [f.path, f]));
1542
+ const missingDeletes = [...toDelete].filter((p) => !manifest.has(p));
1543
+ if (missingDeletes.length)
1544
+ invalid(
1545
+ `deleted names ${missingDeletes.join(", ")}, which ${missingDeletes.length > 1 ? "are" : "is"} not in ${baseId ? `the base deployment ${baseId}` : "the deployment (there is no base)"}. List its files with vibiz_code list.`,
1546
+ { missing: missingDeletes }
1547
+ );
1548
+ const added = [];
1549
+ const updated = [];
1550
+ for (const p of toDelete) manifest.delete(p);
1551
+ for (const [path, f] of incoming) {
1552
+ const before = manifest.get(path);
1553
+ if (!before) added.push(path);
1554
+ else if (before.sha !== f.sha) updated.push(path);
1555
+ manifest.set(path, { path, sha: f.sha, size: f.bytes.byteLength });
1556
+ }
1557
+ const all = [...manifest.values()].sort((a, b) => a.path.localeCompare(b.path));
1558
+ if (all.length === 0)
1559
+ invalid("That would deploy no files at all: keep at least one (an index.html for a site).");
1560
+ if (baseId && !added.length && !updated.length && !toDelete.size) {
1561
+ return {
1562
+ deployment: await vibiz.deployments.get(baseId),
1563
+ base: baseId,
1564
+ unchanged: true,
1565
+ uploaded: 0,
1566
+ total: all.length,
1567
+ added,
1568
+ updated,
1569
+ deleted: []
1570
+ };
1571
+ }
1572
+ const missing = new Set(await vibiz.files.missing([...incoming.values()].map((f) => f.sha)));
1573
+ const uploaded = /* @__PURE__ */ new Set();
1574
+ for (const f of incoming.values()) {
1575
+ if (!missing.has(f.sha) || uploaded.has(f.sha)) continue;
1576
+ await vibiz.files.upload(f.sha, f.bytes);
1577
+ uploaded.add(f.sha);
1578
+ }
1579
+ const { deployment } = await vibiz.deployments.create(projectId, {
1580
+ files: all,
1581
+ target: opts.target ?? "production"
1582
+ });
1583
+ return {
1584
+ deployment,
1585
+ base: baseId,
1586
+ unchanged: false,
1587
+ uploaded: uploaded.size,
1588
+ total: all.length,
1589
+ added: added.sort(),
1590
+ updated: updated.sort(),
1591
+ deleted: [...toDelete].sort()
1592
+ };
1593
+ }
1380
1594
 
1381
1595
  // packages/sdk/src/env.ts
1382
1596
  import { chmod, readFile as readFile2, writeFile } from "node:fs/promises";
@@ -1448,12 +1662,30 @@ function formatUnits(units) {
1448
1662
  return parts.join(" \xB7 ") || "\u2013";
1449
1663
  }
1450
1664
 
1665
+ // packages/sdk/src/one-key.ts
1666
+ var APP_SDK_VERSION = "0.2.3";
1667
+ var ONE_KEY_GIVES = "database, sign-in, email and inbox, AI models, files, payments, analytics with session replays, web data, social and connectors";
1668
+ function oneKeyMessage(url, host) {
1669
+ const where = host ?? "your host";
1670
+ return `Add one key to ${where} and your app can use everything on Vibiz (${ONE_KEY_GIVES}): copy VIBIZ_APP_KEY here: ${url} , paste it into ${host ? `${host}'s` : "your host's"} production environment variables, and redeploy.`;
1671
+ }
1672
+ function connectHostMessage(kind, host) {
1673
+ return `Or connect ${host ?? "the host"} once (vibiz hosts add ${kind ?? "<host>"} <app>) and Vibiz keeps every variable in sync.`;
1674
+ }
1675
+
1451
1676
  export {
1452
1677
  VibizApiError,
1453
1678
  Vibiz,
1454
1679
  deployDirectory,
1680
+ INLINE_LIMITS,
1681
+ deployPathProblem,
1682
+ deployFiles,
1455
1683
  mergeEnvFile,
1456
1684
  writeEnvFile,
1457
1685
  formatUsd,
1458
- formatUnits
1686
+ formatUnits,
1687
+ APP_SDK_VERSION,
1688
+ ONE_KEY_GIVES,
1689
+ oneKeyMessage,
1690
+ connectHostMessage
1459
1691
  };