@protobase/cli 0.55.8 → 0.55.9

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.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.55.8";
2
+ var version = "0.55.9";
3
3
  //#endregion
4
4
  export { version };
@@ -1,4 +1,5 @@
1
1
  import { loadNearestEnvFile } from "../dev/env.js";
2
+ import { projectUrl } from "./storage-url.js";
2
3
  import path from "node:path";
3
4
  //#region src/supabase/register.ts
4
5
  const out = (text) => process.stdout.write(text);
@@ -7,14 +8,9 @@ const variable = (name) => {
7
8
  if (!value) throw new Error(`Environment variable ${name} is not set`);
8
9
  return value;
9
10
  };
10
- const projectUrl = (text) => {
11
- const url = URL.canParse(text) ? new URL(text) : void 0;
12
- if (!url || url.protocol !== "https:" && url.protocol !== "http:") throw new Error("--storage-url is not a URL; write it like https://<ref>.supabase.co");
13
- if (url.username || url.password) throw new Error("--storage-url must not hold credentials; put the secret key in the variable --storage-key-env names");
14
- return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
15
- };
16
11
  const registerImport = (program) => {
17
12
  program.command("import").description("Turn another platform's project into this Protobase project").command("supabase").description("Import a Supabase database into the project in the current directory. The first run dumps it and writes its schemas as the project's next migration; once the project's migrations are applied, the second run loads the data, scaffolds the config, writes the migrations that sign its users in with Better Auth and writes supabase-import/report.md. With --storage-url, the second run, or any later one, copies Storage's files into the app's file provider").argument("[source]", "the Supabase database's connection string, for the first run").option("--source-env <variable>", "read the Supabase connection string from this environment variable").option("--env <variable>", "environment variable holding the project's database URL", "DATABASE_URL").option("--dir <dir>", "migrations folder", "db/migrations").option("--config <dir>", "config folder", "config").option("--storage-url <url>", "the Supabase project's URL, such as https://<ref>.supabase.co, to copy Storage's files from").option("--storage-key-env <variable>", "environment variable holding the Supabase project's secret key, for --storage-url", "SUPABASE_SERVICE_ROLE_KEY").option("--yes", "copy Storage's files even when they exceed the file provider's max_bytes", false).action(async (source, opts) => {
13
+ const storageUrl = opts.storageUrl ? projectUrl(opts.storageUrl) : void 0;
18
14
  if (source && opts.sourceEnv) throw new Error("Pass either a connection string or --source-env, not both");
19
15
  const projectDir = process.cwd();
20
16
  loadNearestEnvFile(projectDir);
@@ -25,8 +21,8 @@ const registerImport = (program) => {
25
21
  projectDir,
26
22
  migrationsDir: path.resolve(opts.dir),
27
23
  configDir: path.resolve(opts.config),
28
- storage: opts.storageUrl ? {
29
- url: projectUrl(opts.storageUrl),
24
+ storage: storageUrl ? {
25
+ url: storageUrl,
30
26
  key: variable(opts.storageKeyEnv)
31
27
  } : void 0,
32
28
  yes: opts.yes
@@ -1,10 +1,16 @@
1
1
  //#region src/supabase/storage-download.ts
2
2
  /** Where Supabase's Storage API serves an object's bytes to the secret key: each segment of the name encoded. */
3
3
  const objectUrl = (projectUrl, bucket, name) => `${projectUrl.replace(/\/+$/, "")}/storage/v1/object/authenticated/${encodeURIComponent(bucket)}/${name.split("/").map(encodeURIComponent).join("/")}`;
4
- /** Downloads from the Supabase project `source` with its secret key, which goes in the request's headers only. */
5
- const supabaseDownload = (source) => (bucket, name) => fetch(objectUrl(source.url, bucket, name), { headers: {
6
- apikey: source.key,
7
- authorization: `Bearer ${source.key}`
8
- } });
4
+ /**
5
+ * Downloads from the Supabase project `source` with its secret key, which goes in the request's headers only. A redirect
6
+ * is not followed, since fetch would send the `apikey` header on to another host too: the 3xx answer fails that object.
7
+ */
8
+ const supabaseDownload = (source) => (bucket, name) => fetch(objectUrl(source.url, bucket, name), {
9
+ headers: {
10
+ apikey: source.key,
11
+ authorization: `Bearer ${source.key}`
12
+ },
13
+ redirect: "manual"
14
+ });
9
15
  //#endregion
10
16
  export { objectUrl, supabaseDownload };
@@ -0,0 +1,19 @@
1
+ //#region src/supabase/storage-url.ts
2
+ const loopback = /* @__PURE__ */ new Set([
3
+ "localhost",
4
+ "127.0.0.1",
5
+ "[::1]"
6
+ ]);
7
+ /**
8
+ * The Supabase project's URL, such as https://<ref>.supabase.co, which the storage report names, so never with
9
+ * credentials. The secret key goes to it, so plain http is refused but on loopback. No message repeats the URL.
10
+ */
11
+ const projectUrl = (text) => {
12
+ const url = URL.canParse(text) ? new URL(text) : void 0;
13
+ if (!url || url.protocol !== "https:" && url.protocol !== "http:") throw new Error("--storage-url is not a URL; write it like https://<ref>.supabase.co");
14
+ if (url.username || url.password) throw new Error("--storage-url must not hold credentials; put the secret key in the variable --storage-key-env names");
15
+ if (url.protocol === "http:" && !loopback.has(url.hostname)) throw new Error("--storage-url must use https, as it sends the secret key; plain http is only for a local Supabase on localhost, 127.0.0.1 or [::1]");
16
+ return `${url.origin}${url.pathname.replace(/\/+$/, "")}`;
17
+ };
18
+ //#endregion
19
+ export { projectUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.55.8",
3
+ "version": "0.55.9",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -42,12 +42,12 @@
42
42
  "dependencies": {
43
43
  "@better-auth/core": "^1.7.12",
44
44
  "@hono/node-server": "^2.1.3",
45
- "@protobase/config": "0.55.8",
46
- "@protobase/layout": "0.55.8",
47
- "@protobase/query": "0.55.8",
48
- "@protobase/schema": "0.55.8",
49
- "@protobase/server": "0.55.8",
50
- "@protobase/ui": "0.55.8",
45
+ "@protobase/config": "0.55.9",
46
+ "@protobase/layout": "0.55.9",
47
+ "@protobase/query": "0.55.9",
48
+ "@protobase/schema": "0.55.9",
49
+ "@protobase/server": "0.55.9",
50
+ "@protobase/ui": "0.55.9",
51
51
  "@react-email/render": "^2.1.0",
52
52
  "@scalar/hono-api-reference": "^0.12.10",
53
53
  "@tailwindcss/vite": "^4.3.3",
@@ -543,7 +543,7 @@ Turns a Supabase project's database into the project in the current directory, i
543
543
  3. **The second run**, without a connection string, loads the data in one transaction, with the app's triggers off so a sign-up trigger on `auth.users` does not run again for users who already have their rows, and fails the transaction unless every table holds the source's number of rows. In the same transaction it makes Vault's secrets and pgmq's queues again through `vault.create_secret`, `pgmq.create` and `pgmq.send`, each message with its headers and the time it becomes visible. Where the project's database has pg_cron, as Supabase's Postgres image does when it serves that database, it schedules pg_cron's jobs again there with `cron.schedule`, run by the importing role, an inactive one inactive. Elsewhere it writes each job of plain SQL as a [recurring job](jobs.md#recurring-jobs) of its own in `jobs/`, of the same name, schedule and `cron.timezone`, that runs its SQL as the app's database role, tried once each time as pg_cron did, and declares it in the `jobs` of `protobase.config.ts` (or writes one); they run once [`migration`](#migration) has added the jobs schema. Recurring jobs wake an app that sleeps, so they run where a host stops an idle app too. A job that calls what the database lacks, such as pg_net's `net.http_*`, `cron` or Vault, an inactive one and a schedule recurring jobs do not take, such as `30 seconds` or `$`, stay in the report, as does a job of another database. It loads only into empty tables, deletes the data dump and the replay, runs [`scaffold`](#scaffold) on the imported tables of the app's schemas (not `auth` or `storage`), giving the tables of the `supabase_realtime` publication [`.live()`](live.md) so [`/api/sb/realtime/v1`](supabase-realtime.md) keeps sending their `postgres_changes` once [`migration`](#migration) has written their capture triggers, writes Better Auth's other tables next to `auth.users` as [`auth migration`](#auth-migration) does (`NNN_auth.sql`), then `NNN_supabase_accounts.sql`, and writes `supabase-import/report.md`: the rows loaded, the API keys `/api/sb` needs and the `createClient` change to the app's `/api/sb` and its publishable key, the imported tables' size against the Starter plan's 500 MB, each sign-in provider's identities, what it made again, the tables it made live, and what needs a person: the pg_cron jobs it could not carry over, each with why, Realtime tables it made no resource of, Vault secrets by name and pgmq queues where the database has no such extension (and partitioned queues), the columns pgsodium encrypts, still holding Supabase's ciphertext, whose values to export decrypted in Supabase before declaring them [`.encrypted()`](data-config.md#encrypted-fields) and running [`encryption apply`](#encryption-apply), the extensions it cannot create, hypertables and plv8, plls or plcoffee functions to convert, triggers that call out over HTTP without pg_net, the converted Edge Functions with the packages to install and the code the conversion left (`deno.land/x` imports, Deno's file, KV and WebSocket APIs, `Supabase.ai`), and the indexes [`doctor`](#doctor) finds missing. `NNN_supabase_accounts.sql` fills the user columns from what Supabase Auth recorded and leaves them empty where it recorded nothing: the name from `raw_user_meta_data`'s `full_name` or `name`, the picture from its `avatar_url` or `picture`, `email_verified` from `email_confirmed_at`, and a ban from `banned_until`, which Better Auth lifts once it has passed. It gives each password of a confirmed address, as Supabase hashed it, a `credential` account (a user who never confirmed theirs signs in by code or resets the password, since GoTrue refused their password sign-in too), and each identity of a social provider an account under Better Auth's name for it (`azure` is `microsoft`, keyed on the `oid` claim; `linkedin_oidc` is `linkedin`, `slack_oidc` is `slack`, `x` is `twitter`), unless its user has an address they never confirmed, since GoTrue refused that sign-in too. Email, phone, anonymous and single sign-on identities and sessions do not carry over. <gap>The import cannot carry over MFA factors because that is not implemented.</gap> <gap>The import cannot carry over passkeys because that is not implemented.</gap>
544
544
  4. **Apply the migrations** again. Supabase's users then sign in with their own passwords, and with a social provider once it is in the `socialProviders` of the config's `auth`; new users get a UUID in `auth.users`, so the app's triggers there run for them.
545
545
 
546
- 5. **Storage's files**: with `--storage-url https://<ref>.supabase.co` and the project's secret key in the variable `--storage-key-env` names, `SUPABASE_SERVICE_ROLE_KEY` by default, the second run, or any run after it, copies the bytes of every object in `storage.objects` from the Supabase project's Storage API into the app's [Storage file provider](supabase-storage.md#where-things-are-kept), where [`/api/sb/storage/v1`](supabase-storage.md) serves them, with the type and cache control the rows recorded. It streams each object, checks its byte count against the size Supabase recorded and its MD5 against the ETag where that is one, and deletes a copy that does not match. It first prints each bucket's size and stops when what is left to copy and what the provider holds exceed the provider's `max_bytes`, unless `--yes`. An object that fails does not stop the rest; the run then fails, listing them in `supabase-import/storage.md`, and a rerun copies only what the provider does not hold yet. The key goes only in the requests' headers, never in a file.
546
+ 5. **Storage's files**: with `--storage-url https://<ref>.supabase.co` and the project's secret key in the variable `--storage-key-env` names, `SUPABASE_SERVICE_ROLE_KEY` by default, the second run, or any run after it, copies the bytes of every object in `storage.objects` from the Supabase project's Storage API into the app's [Storage file provider](supabase-storage.md#where-things-are-kept), where [`/api/sb/storage/v1`](supabase-storage.md) serves them, with the type and cache control the rows recorded. It streams each object, checks its byte count against the size Supabase recorded and its MD5 against the ETag where that is one, and deletes a copy that does not match. It first prints each bucket's size and stops when what is left to copy and what the provider holds exceed the provider's `max_bytes`, unless `--yes`. An object that fails does not stop the rest; the run then fails, listing them in `supabase-import/storage.md`, and a rerun copies only what the provider does not hold yet. The key goes only in the requests' headers, never in a file. Plain `http://` is accepted only for a local Supabase on `localhost`, `127.0.0.1` or `[::1]`, such as `supabase start`'s `http://127.0.0.1:54321`, since the key bypasses every Storage policy; a download Supabase answers with a redirect is not followed, so the key goes to no other host, and that object fails.
547
547
 
548
548
  An agent with the `supabase-move` skill of [`skill install`](#skill) finishes what the report leaves: it rewrites each item, checking the project's database for the extensions an item needs, and runs `doctor` and the typecheck until both pass, leaving the values of Vault's secrets for you to enter.
549
549