@rebasepro/cli 0.31.0-canary.g902a0ce → 0.32.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.
- package/dist/{agent-setup-DqAcoDcn.js → agent-setup-Cg_zBWoW.js} +3 -3
- package/dist/{agent-setup-DqAcoDcn.js.map → agent-setup-Cg_zBWoW.js.map} +1 -1
- package/dist/api-TbmW33D7.js +275 -0
- package/dist/api-TbmW33D7.js.map +1 -0
- package/dist/{api-keys-eIUjMqWn.js → api-keys-fPQ55DCm.js} +151 -158
- package/dist/api-keys-fPQ55DCm.js.map +1 -0
- package/dist/{apps-DExO4PEl.js → apps-DBTRoOeP.js} +18 -13
- package/dist/apps-DBTRoOeP.js.map +1 -0
- package/dist/{args-CaCcrYE1.js → args-DHyrshtI.js} +3 -132
- package/dist/args-DHyrshtI.js.map +1 -0
- package/dist/{atlas-dev-database-DOCKk6ht.js → atlas-dev-database-D6h65f2x.js} +3 -3
- package/dist/{atlas-dev-database-DOCKk6ht.js.map → atlas-dev-database-D6h65f2x.js.map} +1 -1
- package/dist/{auth-CyADghFQ.js → auth-BpyYLEFY.js} +93 -12
- package/dist/auth-BpyYLEFY.js.map +1 -0
- package/dist/auth-session-B6aT0Dyj.js +371 -0
- package/dist/auth-session-B6aT0Dyj.js.map +1 -0
- package/dist/{backend-guard-C0mVkXBw.js → backend-guard-Cf8eMK_f.js} +2 -2
- package/dist/{backend-guard-C0mVkXBw.js.map → backend-guard-Cf8eMK_f.js.map} +1 -1
- package/dist/{branch-pointer-DMPyvZoK.js → branch-pointer-BJOF2Ubv.js} +1 -1
- package/dist/{build-CA5R07tw.js → build-BfdT-AUR.js} +12 -10
- package/dist/{build-CA5R07tw.js.map → build-BfdT-AUR.js.map} +1 -1
- package/dist/{bundle-DgILeA5b.js → bundle-CIiKGrte.js} +78 -12
- package/dist/bundle-CIiKGrte.js.map +1 -0
- package/dist/bundle.d.ts +31 -1
- package/dist/{cloud-CYMk92iG.js → cloud-MqnyPnmX.js} +1540 -1531
- package/dist/cloud-MqnyPnmX.js.map +1 -0
- package/dist/{cms-directories-BK3vcswg.js → cms-directories-BaV-Is9a.js} +2 -2
- package/dist/{cms-directories-BK3vcswg.js.map → cms-directories-BaV-Is9a.js.map} +1 -1
- package/dist/codemod/collections-to-tables/backend.d.ts +0 -2
- package/dist/codemod/collections-to-tables/customization.d.ts +2 -1
- package/dist/codemod/collections-to-tables/modules.d.ts +32 -0
- package/dist/codemod/collections-to-tables/plan-context.d.ts +7 -1
- package/dist/{command-D5lhW_-B.js → command-D9g46gg9.js} +68 -42
- package/dist/command-D9g46gg9.js.map +1 -0
- package/dist/commands/api-keys.d.ts +89 -52
- package/dist/commands/api.d.ts +114 -0
- package/dist/commands/auth-session.d.ts +94 -0
- package/dist/commands/auth.d.ts +1 -1
- package/dist/commands/cloud/alerts.d.ts +29 -0
- package/dist/commands/cloud/context.d.ts +8 -0
- package/dist/commands/cloud/debug.d.ts +16 -1
- package/dist/commands/cloud/deploy-hooks.d.ts +19 -0
- package/dist/commands/cloud/deploy.d.ts +25 -0
- package/dist/commands/cloud/env.d.ts +1 -0
- package/dist/commands/cloud/index.d.ts +5 -0
- package/dist/commands/cloud/maintenance.d.ts +26 -1
- package/dist/commands/cloud/not-served.d.ts +27 -0
- package/dist/commands/cloud/orgs.d.ts +5 -0
- package/dist/commands/cloud/resources.d.ts +82 -2
- package/dist/commands/cloud/runtime-logs.d.ts +57 -0
- package/dist/commands/cloud/settings.d.ts +3 -0
- package/dist/commands/cron.d.ts +68 -0
- package/dist/commands/data.d.ts +197 -0
- package/dist/commands/functions.d.ts +38 -0
- package/dist/commands/logs.d.ts +22 -0
- package/dist/commands/rls.d.ts +31 -0
- package/dist/commands/schema-reconcile.d.ts +2 -1
- package/dist/commands/schema-show.d.ts +38 -0
- package/dist/commands/storage-objects.d.ts +88 -0
- package/dist/commands/storage.d.ts +18 -2
- package/dist/commands/users.d.ts +147 -0
- package/dist/{consent-BXLIrMl8.js → consent-UV6ObCME.js} +2 -2
- package/dist/{consent-BXLIrMl8.js.map → consent-UV6ObCME.js.map} +1 -1
- package/dist/{console-CMjhgMUE.js → console-BVoYOLAO.js} +6 -6
- package/dist/{console-CMjhgMUE.js.map → console-BVoYOLAO.js.map} +1 -1
- package/dist/{context--JwaHaZp.js → context-BOMEoKxn.js} +80 -9
- package/dist/{context--JwaHaZp.js.map → context-BOMEoKxn.js.map} +1 -1
- package/dist/cron-CpUsQ739.js +298 -0
- package/dist/cron-CpUsQ739.js.map +1 -0
- package/dist/cron-ZRXsla0v.js +259 -0
- package/dist/cron-ZRXsla0v.js.map +1 -0
- package/dist/{daemon-w30PsdwV.js → daemon-D5gdG7gx.js} +3 -3
- package/dist/{daemon-w30PsdwV.js.map → daemon-D5gdG7gx.js.map} +1 -1
- package/dist/{daemon-entry-zOC1revO.js → daemon-entry--ummUJnk.js} +2 -2
- package/dist/{daemon-entry-zOC1revO.js.map → daemon-entry--ummUJnk.js.map} +1 -1
- package/dist/data-BJ9AZFUC.js +947 -0
- package/dist/data-BJ9AZFUC.js.map +1 -0
- package/dist/{db-A7XDCIIO.js → db-D_iF4E-v.js} +60 -36
- package/dist/db-D_iF4E-v.js.map +1 -0
- package/dist/debug-nNbbxAeo.js +1114 -0
- package/dist/debug-nNbbxAeo.js.map +1 -0
- package/dist/{derive-C3ezmY-p.js → derive-Mky_u59_.js} +3 -3
- package/dist/{derive-C3ezmY-p.js.map → derive-Mky_u59_.js.map} +1 -1
- package/dist/{dev-tWwPyWkQ.js → dev-bj6dEeSQ.js} +13 -13
- package/dist/{dev-tWwPyWkQ.js.map → dev-bj6dEeSQ.js.map} +1 -1
- package/dist/dev-db/__fixtures__/unanswering-postgres.d.ts +11 -0
- package/dist/dev-db/attver.d.ts +38 -6
- package/dist/dev-db/local-timeouts.d.ts +25 -0
- package/dist/dev-db/pull.d.ts +49 -9
- package/dist/{dev-preflight-DjvcV6SK.js → dev-preflight-BMOf2N6r.js} +2 -2
- package/dist/{dev-preflight-DjvcV6SK.js.map → dev-preflight-BMOf2N6r.js.map} +1 -1
- package/dist/{dev-server-BLxCbQEP.js → dev-server-2XHSHM_q.js} +2 -2
- package/dist/{dev-server-BLxCbQEP.js.map → dev-server-2XHSHM_q.js.map} +1 -1
- package/dist/{doctor-BBM0dWvZ.js → doctor-2Jlau0Rm.js} +9 -8
- package/dist/{doctor-BBM0dWvZ.js.map → doctor-2Jlau0Rm.js.map} +1 -1
- package/dist/{eject-Bhzd6hgb.js → eject-DK_M9kor.js} +4 -4
- package/dist/{eject-Bhzd6hgb.js.map → eject-DK_M9kor.js.map} +1 -1
- package/dist/{fold-static-CKewnAPX.js → fold-static-2t3UUX4z.js} +10 -6
- package/dist/fold-static-2t3UUX4z.js.map +1 -0
- package/dist/fold-static.d.ts +7 -0
- package/dist/functions-B4JR6EZL.js +120 -0
- package/dist/functions-B4JR6EZL.js.map +1 -0
- package/dist/{generate-view-CTOluXvH.js → generate-view-BMaLQm8H.js} +5 -5
- package/dist/{generate-view-CTOluXvH.js.map → generate-view-BMaLQm8H.js.map} +1 -1
- package/dist/{generate_sdk-De-aLC1L.js → generate_sdk-ByD-gTsK.js} +7 -7
- package/dist/{generate_sdk-De-aLC1L.js.map → generate_sdk-ByD-gTsK.js.map} +1 -1
- package/dist/index.es.js +123 -47
- package/dist/index.es.js.map +1 -1
- package/dist/{init-Dtl-IcXf.js → init-DHCe5mJ5.js} +59 -11
- package/dist/init-DHCe5mJ5.js.map +1 -0
- package/dist/{invoked-command-CG_rIa_o.js → invoked-command-FBeIBS8o.js} +2 -2
- package/dist/{invoked-command-CG_rIa_o.js.map → invoked-command-FBeIBS8o.js.map} +1 -1
- package/dist/last-dev-database-CBVkEUEl.js +2 -0
- package/dist/local-timeouts-B1Kwligw.js +30 -0
- package/dist/local-timeouts-B1Kwligw.js.map +1 -0
- package/dist/logs-CIgfLBsu.js +192 -0
- package/dist/logs-CIgfLBsu.js.map +1 -0
- package/dist/{manifest-C3uLDrrJ.js → manifest-RjqxfCpb.js} +114 -5
- package/dist/manifest-RjqxfCpb.js.map +1 -0
- package/dist/{migrate-VrKoi3nB.js → migrate-Cx8-Y4TG.js} +5 -4
- package/dist/{migrate-VrKoi3nB.js.map → migrate-Cx8-Y4TG.js.map} +1 -1
- package/dist/{normalize-imports-DwxqEMYq.js → normalize-imports-P86NVfpo.js} +3 -3
- package/dist/{normalize-imports-DwxqEMYq.js.map → normalize-imports-P86NVfpo.js.map} +1 -1
- package/dist/{path-flags-7O5uPBcQ.js → path-flags-ekLdkbra.js} +3 -3
- package/dist/{path-flags-7O5uPBcQ.js.map → path-flags-ekLdkbra.js.map} +1 -1
- package/dist/{prepare-Ccxspoj-.js → prepare-BaJe0Zik.js} +4 -4
- package/dist/{prepare-Ccxspoj-.js.map → prepare-BaJe0Zik.js.map} +1 -1
- package/dist/{project-CLcF5i8c.js → project-DALmhb5R.js} +2 -2
- package/dist/{project-CLcF5i8c.js.map → project-DALmhb5R.js.map} +1 -1
- package/dist/project-command-Cl9EZzjr.js +662 -0
- package/dist/project-command-Cl9EZzjr.js.map +1 -0
- package/dist/{project-schema-CsSOG2gE.js → project-schema-Dm6Jk0Y_.js} +4 -2
- package/dist/{project-schema-CsSOG2gE.js.map → project-schema-Dm6Jk0Y_.js.map} +1 -1
- package/dist/{pull-D9WvpDYY.js → pull-CTIsST6T.js} +336 -55
- package/dist/pull-CTIsST6T.js.map +1 -0
- package/dist/resolve-C36px9tR.js +2 -0
- package/dist/{resources-Dpfs2y8P.js → resources-Db3USuPO.js} +5 -5
- package/dist/{resources-Dpfs2y8P.js.map → resources-Db3USuPO.js.map} +1 -1
- package/dist/rls-BxrQ0P50.js +133 -0
- package/dist/rls-BxrQ0P50.js.map +1 -0
- package/dist/{schema-DwFDI1iU.js → schema-_FzZEoB8.js} +54 -40
- package/dist/schema-_FzZEoB8.js.map +1 -0
- package/dist/{schema-dir-Cd1kvYbf.js → schema-dir-ChcYfWjP.js} +2 -2
- package/dist/{schema-dir-Cd1kvYbf.js.map → schema-dir-ChcYfWjP.js.map} +1 -1
- package/dist/schema-reconcile-DV14XY4o.js +2 -0
- package/dist/schema-show-h72UJ9zH.js +127 -0
- package/dist/schema-show-h72UJ9zH.js.map +1 -0
- package/dist/{skills-CXjA9TWw.js → skills-DQ2bZGkj.js} +6 -5
- package/dist/{skills-CXjA9TWw.js.map → skills-DQ2bZGkj.js.map} +1 -1
- package/dist/star-nudge.d.ts +28 -0
- package/dist/{start-Bwbtt2W5.js → start-Cjx4W0bJ.js} +7 -7
- package/dist/{start-Bwbtt2W5.js.map → start-Cjx4W0bJ.js.map} +1 -1
- package/dist/{status-DQ_PPz4C.js → status-CDBk25VO.js} +9 -9
- package/dist/{status-DQ_PPz4C.js.map → status-CDBk25VO.js.map} +1 -1
- package/dist/{storage-iTqj5-Av.js → storage-Clc8dLG8.js} +79 -36
- package/dist/storage-Clc8dLG8.js.map +1 -0
- package/dist/storage-objects-CfRKlTDe.js +279 -0
- package/dist/storage-objects-CfRKlTDe.js.map +1 -0
- package/dist/{telemetry-BP_1Cl9r.js → telemetry-DZU2-NCm.js} +5 -4
- package/dist/{telemetry-BP_1Cl9r.js.map → telemetry-DZU2-NCm.js.map} +1 -1
- package/dist/{telemetry-M3HjlyhZ.js → telemetry-EVtASbKb.js} +2 -2
- package/dist/{telemetry-M3HjlyhZ.js.map → telemetry-EVtASbKb.js.map} +1 -1
- package/dist/unknown-command-DnZ2uhGs.js +139 -0
- package/dist/unknown-command-DnZ2uhGs.js.map +1 -0
- package/dist/{upgrade-oZzJ13jM.js → upgrade-DhpUFb6p.js} +5 -5
- package/dist/{upgrade-oZzJ13jM.js.map → upgrade-DhpUFb6p.js.map} +1 -1
- package/dist/users-5PkqBY6R.js +561 -0
- package/dist/users-5PkqBY6R.js.map +1 -0
- package/dist/utils/project-command.d.ts +38 -0
- package/dist/utils/project-target.d.ts +193 -0
- package/dist/{verify-DkJqhYPI.js → verify-BdeqNb-V.js} +138 -12
- package/dist/verify-BdeqNb-V.js.map +1 -0
- package/dist/{version-Buyyo1ut.js → version-ONIeo7jl.js} +7 -2
- package/dist/{version-Buyyo1ut.js.map → version-ONIeo7jl.js.map} +1 -1
- package/package.json +10 -10
- package/dist/api-keys-eIUjMqWn.js.map +0 -1
- package/dist/apps-DExO4PEl.js.map +0 -1
- package/dist/args-CaCcrYE1.js.map +0 -1
- package/dist/auth-CyADghFQ.js.map +0 -1
- package/dist/bundle-DgILeA5b.js.map +0 -1
- package/dist/cloud-CYMk92iG.js.map +0 -1
- package/dist/command-D5lhW_-B.js.map +0 -1
- package/dist/db-A7XDCIIO.js.map +0 -1
- package/dist/fold-static-CKewnAPX.js.map +0 -1
- package/dist/init-Dtl-IcXf.js.map +0 -1
- package/dist/last-dev-database-Dc8cP01o.js +0 -2
- package/dist/manifest-C3uLDrrJ.js.map +0 -1
- package/dist/pull-D9WvpDYY.js.map +0 -1
- package/dist/resolve-BY7bSFRT.js +0 -2
- package/dist/schema-DwFDI1iU.js.map +0 -1
- package/dist/schema-reconcile-Tto54Qbv.js +0 -2
- package/dist/storage-iTqj5-Av.js.map +0 -1
- package/dist/verify-DkJqhYPI.js.map +0 -1
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
import { a as wantsHelp } from "./args-DHyrshtI.js";
|
|
2
|
+
import { c as findProjectRoot, d as readEnvFile } from "./project-DALmhb5R.js";
|
|
3
|
+
import { t as cliUserAgent } from "./version-ONIeo7jl.js";
|
|
4
|
+
import { a as serverUnreachableHint } from "./dev-server-2XHSHM_q.js";
|
|
5
|
+
import { E as initOutputMode, I as printGroupHelp, v as fail } from "./context-BOMEoKxn.js";
|
|
6
|
+
import chalk from "chalk";
|
|
7
|
+
import fs from "fs";
|
|
8
|
+
import os from "os";
|
|
9
|
+
import path from "path";
|
|
10
|
+
//#region src/utils/project-target.ts
|
|
11
|
+
/**
|
|
12
|
+
* Which server a project command talks to, and as whom.
|
|
13
|
+
*
|
|
14
|
+
* Every command that acts on a running Rebase app — `rebase api`, `data`,
|
|
15
|
+
* `users`, `cron`, `logs`, `functions`, `storage`, `api-keys`, `schema
|
|
16
|
+
* reconcile` — resolves its target here, so they all accept the same flags,
|
|
17
|
+
* refuse the same way, and never disagree about where the service key may go.
|
|
18
|
+
*
|
|
19
|
+
* ## Targets
|
|
20
|
+
*
|
|
21
|
+
* 1. `--project <slug>` (or `--cloud`, the project this directory is linked
|
|
22
|
+
* to) — a Rebase Cloud project. The default transport is the console's
|
|
23
|
+
* Studio proxy (`/api/functions/studio/<id>/proxy/*` on the control
|
|
24
|
+
* plane): the CLI presents its `rebase cloud login` session there and the
|
|
25
|
+
* control plane attaches the app's service key on its own side, so no
|
|
26
|
+
* static secret ever reaches this machine. It answers an organization
|
|
27
|
+
* owner or admin, on a project whose Studio switch is on — the gate the
|
|
28
|
+
* console's Studio tab has. A command that only needs cron asks for a
|
|
29
|
+
* platform token instead (see {@link ProjectTargetOptions.platformScopes}),
|
|
30
|
+
* which any member with log access holds.
|
|
31
|
+
* 2. `--url <server>` — a server the developer names.
|
|
32
|
+
* 3. `REBASE_BASE_URL`, in the shell or in `.env`.
|
|
33
|
+
* 4. The `rebase dev` this checkout is running (`.rebase/state.json`, while
|
|
34
|
+
* the pid it names is alive).
|
|
35
|
+
*
|
|
36
|
+
* Nothing else: an address the CLI only guessed is one it would hand the
|
|
37
|
+
* service key to without knowing whose server answers.
|
|
38
|
+
*
|
|
39
|
+
* ## Credentials, on a server target
|
|
40
|
+
*
|
|
41
|
+
* The service key from `.env` (`REBASE_SERVICE_KEY`, or the older
|
|
42
|
+
* `SERVICE_KEY`; the shell's `REBASE_SERVICE_KEY` wins) unless the line says
|
|
43
|
+
* otherwise: `--token <bearer>` sends that bearer (an `rk_` key, a user's
|
|
44
|
+
* access token), `--session` sends the person session `rebase auth login`
|
|
45
|
+
* stored for that server, and `--anon` sends nothing. On a cloud target the
|
|
46
|
+
* same three flags skip the proxy and go to the app's public address directly.
|
|
47
|
+
*/
|
|
48
|
+
/**
|
|
49
|
+
* The flags every project command accepts, merged into its own spec.
|
|
50
|
+
*
|
|
51
|
+
* `--json` and `--yes` are here because the output mode and the confirmation
|
|
52
|
+
* prompt are shared too (`commands/cloud/context.ts`): one JSON value on
|
|
53
|
+
* stdout when piped or asked for, and a refusal rather than a prompt when
|
|
54
|
+
* there is nobody to answer one.
|
|
55
|
+
*/
|
|
56
|
+
var PROJECT_TARGET_FLAGS = {
|
|
57
|
+
"--project": String,
|
|
58
|
+
"-p": "--project",
|
|
59
|
+
"--cloud": Boolean,
|
|
60
|
+
"--url": String,
|
|
61
|
+
"--token": String,
|
|
62
|
+
"--session": Boolean,
|
|
63
|
+
"--anon": Boolean,
|
|
64
|
+
"--json": Boolean,
|
|
65
|
+
"--yes": Boolean,
|
|
66
|
+
"-y": "--yes"
|
|
67
|
+
};
|
|
68
|
+
/** The same flags, as every project command's help page lists them. */
|
|
69
|
+
var PROJECT_TARGET_HELP = [
|
|
70
|
+
["--project, -p <slug>", "Act on a Rebase Cloud project, through the console's Studio proxy"],
|
|
71
|
+
["--cloud", "Act on the Rebase Cloud project this directory is linked to"],
|
|
72
|
+
["--url <server>", "Act on the server at this address (else REBASE_BASE_URL, then `rebase dev`)"],
|
|
73
|
+
["--token <bearer>", "Send this bearer instead of the service key (an rk_ key, a user's token)"],
|
|
74
|
+
["--session", "Send the session `rebase auth login` stored for this server"],
|
|
75
|
+
["--anon", "Send no credential at all"],
|
|
76
|
+
["--json", "Machine-readable output (also when piped, or REBASE_JSON=1)"],
|
|
77
|
+
["--yes, -y", "Skip confirmation prompts"]
|
|
78
|
+
];
|
|
79
|
+
/** Whether a process with this pid is alive and ours to signal. */
|
|
80
|
+
function isAlive(pid) {
|
|
81
|
+
try {
|
|
82
|
+
process.kill(pid, 0);
|
|
83
|
+
return true;
|
|
84
|
+
} catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function isRecord$1(value) {
|
|
89
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The address the service key may be sent to, from the project alone.
|
|
93
|
+
*
|
|
94
|
+
* In order: `REBASE_BASE_URL` in the shell or in `.env`, then the
|
|
95
|
+
* `.rebase/state.json` this project's backend writes when it binds — and only
|
|
96
|
+
* while the pid it names is alive, because a backend that was killed leaves
|
|
97
|
+
* the file behind. `.env`'s `PORT` was once the fallback, and every scaffold
|
|
98
|
+
* writes `PORT=3001`: with `rebase dev` stopped, the key went to whatever
|
|
99
|
+
* listened on 3001.
|
|
100
|
+
*/
|
|
101
|
+
function resolveBackendAddress(env, projectRoot) {
|
|
102
|
+
const fromShell = process.env.REBASE_BASE_URL?.trim();
|
|
103
|
+
const configured = fromShell || env.REBASE_BASE_URL?.trim();
|
|
104
|
+
if (configured) {
|
|
105
|
+
const cloudProject = fromShell ? void 0 : linkedCloudProjectAt(projectRoot, configured);
|
|
106
|
+
return cloudProject ? {
|
|
107
|
+
url: configured,
|
|
108
|
+
source: "configured",
|
|
109
|
+
cloudProject
|
|
110
|
+
} : {
|
|
111
|
+
url: configured,
|
|
112
|
+
source: "configured"
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
const state = readBackendState(projectRoot);
|
|
116
|
+
if (state && isAlive(state.pid)) return {
|
|
117
|
+
url: state.url,
|
|
118
|
+
source: "running"
|
|
119
|
+
};
|
|
120
|
+
return {
|
|
121
|
+
url: null,
|
|
122
|
+
reason: `This project's server is not running, so the service key was not sent anywhere.${state ? ` The last one recorded here, at ${state.url} (pid ${state.pid}), has stopped.` : ""} Start it with \`rebase dev\`. If it runs some other way — \`rebase start\`, a container — set REBASE_BASE_URL in .env to its address, or pass --url. For a Rebase Cloud project, pass --project <slug>.`
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** The slug of the cloud project `.rebase/cloud.json` links, when `url` is its address. */
|
|
126
|
+
function linkedCloudProjectAt(projectRoot, url) {
|
|
127
|
+
const link = readJsonFile(path.join(projectRoot, ".rebase", "cloud.json"));
|
|
128
|
+
if (!isRecord$1(link) || link.mode === "direct" || typeof link.apiUrl !== "string") return void 0;
|
|
129
|
+
const bare = (u) => u.trim().replace(/\/+$/, "");
|
|
130
|
+
if (bare(link.apiUrl) !== bare(url)) return void 0;
|
|
131
|
+
if (typeof link.slug === "string" && link.slug) return link.slug;
|
|
132
|
+
return typeof link.projectId === "string" && link.projectId ? link.projectId : void 0;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* What to say before sending a request to the address of a cloud project
|
|
136
|
+
* `.env` names: it goes there, and that project does not take this
|
|
137
|
+
* project's local service key.
|
|
138
|
+
*/
|
|
139
|
+
function cloudTargetNote(address) {
|
|
140
|
+
if (address.url === null || !address.cloudProject) return void 0;
|
|
141
|
+
return `Sending this to ${address.url}: REBASE_BASE_URL in .env is the address of cloud project ${address.cloudProject} (\`rebase cloud init --claim\` writes it there for the app, the SDK and the MCP server), and that project does not take this project's local service key. To reach it with your Cloud login, pass --project ${address.cloudProject}; to reach the local backend, run the command with REBASE_BASE_URL set to it in the shell, e.g. REBASE_BASE_URL=http://localhost:3001.`;
|
|
142
|
+
}
|
|
143
|
+
function readJsonFile(file) {
|
|
144
|
+
try {
|
|
145
|
+
return JSON.parse(fs.readFileSync(file, "utf-8"));
|
|
146
|
+
} catch {
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/** What `.rebase/state.json` says about the backend that wrote it, or null. */
|
|
151
|
+
function readBackendState(projectRoot) {
|
|
152
|
+
const parsed = readJsonFile(path.join(projectRoot, ".rebase", "state.json"));
|
|
153
|
+
if (!isRecord$1(parsed) || typeof parsed.pid !== "number" || !Number.isInteger(parsed.pid) || parsed.pid <= 0) return null;
|
|
154
|
+
const url = typeof parsed.baseUrl === "string" && parsed.baseUrl.trim() ? parsed.baseUrl.trim() : typeof parsed.port === "number" ? `http://localhost:${parsed.port}` : null;
|
|
155
|
+
return url ? {
|
|
156
|
+
url,
|
|
157
|
+
pid: parsed.pid
|
|
158
|
+
} : null;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* `~/.rebase/sessions.json`: the person sessions `rebase auth login` keeps,
|
|
162
|
+
* keyed by server origin. Never the project directory — a token there is one
|
|
163
|
+
* `git add .` from a repository — and never the cloud credentials file, which
|
|
164
|
+
* holds control-plane sessions a different command family manages.
|
|
165
|
+
* `REBASE_SESSIONS_FILE` moves it, for tests.
|
|
166
|
+
*/
|
|
167
|
+
function sessionsPath() {
|
|
168
|
+
return process.env.REBASE_SESSIONS_FILE || path.join(os.homedir(), ".rebase", "sessions.json");
|
|
169
|
+
}
|
|
170
|
+
function readSessions() {
|
|
171
|
+
const parsed = readJsonFile(sessionsPath());
|
|
172
|
+
if (!isRecord$1(parsed) || !isRecord$1(parsed.sessions)) return { sessions: {} };
|
|
173
|
+
const sessions = {};
|
|
174
|
+
for (const [origin, value] of Object.entries(parsed.sessions)) if (isRecord$1(value) && typeof value.accessToken === "string" && typeof value.expiresAt === "number") sessions[origin] = {
|
|
175
|
+
accessToken: value.accessToken,
|
|
176
|
+
expiresAt: value.expiresAt,
|
|
177
|
+
...typeof value.refreshToken === "string" ? { refreshToken: value.refreshToken } : {},
|
|
178
|
+
...typeof value.email === "string" ? { email: value.email } : {},
|
|
179
|
+
...typeof value.uid === "string" ? { uid: value.uid } : {}
|
|
180
|
+
};
|
|
181
|
+
return { sessions };
|
|
182
|
+
}
|
|
183
|
+
/** Write the file beside itself, then rename it over: a concurrent reader sees the old file or the new one. */
|
|
184
|
+
function writeSessions(data) {
|
|
185
|
+
const file = sessionsPath();
|
|
186
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
187
|
+
const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
188
|
+
try {
|
|
189
|
+
fs.writeFileSync(temporary, JSON.stringify(data, null, 2), { mode: 384 });
|
|
190
|
+
try {
|
|
191
|
+
fs.chmodSync(temporary, 384);
|
|
192
|
+
} catch {}
|
|
193
|
+
fs.renameSync(temporary, file);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
fs.rmSync(temporary, { force: true });
|
|
196
|
+
throw error;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** The origin a session is filed under: scheme, host and port, no path. */
|
|
200
|
+
function sessionKey(baseUrl) {
|
|
201
|
+
try {
|
|
202
|
+
return new URL(baseUrl).origin;
|
|
203
|
+
} catch {
|
|
204
|
+
return baseUrl.replace(/\/+$/, "");
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
function storedSession(baseUrl) {
|
|
208
|
+
return readSessions().sessions[sessionKey(baseUrl)];
|
|
209
|
+
}
|
|
210
|
+
function saveSession(baseUrl, session) {
|
|
211
|
+
const data = readSessions();
|
|
212
|
+
data.sessions[sessionKey(baseUrl)] = session;
|
|
213
|
+
writeSessions(data);
|
|
214
|
+
}
|
|
215
|
+
/** Forget a server's session. Returns whether there was one. */
|
|
216
|
+
function forgetSession(baseUrl) {
|
|
217
|
+
const data = readSessions();
|
|
218
|
+
const key = sessionKey(baseUrl);
|
|
219
|
+
if (!data.sessions[key]) return false;
|
|
220
|
+
delete data.sessions[key];
|
|
221
|
+
writeSessions(data);
|
|
222
|
+
return true;
|
|
223
|
+
}
|
|
224
|
+
/** Two minutes of head-room before an access token is treated as expired. */
|
|
225
|
+
var SESSION_EXPIRY_BUFFER_MS = 12e4;
|
|
226
|
+
/**
|
|
227
|
+
* The session's access token, refreshed first when it is close to expiry —
|
|
228
|
+
* through the server's own `POST /api/auth/refresh`, which rotates the refresh
|
|
229
|
+
* token, so the new pair is written back.
|
|
230
|
+
*/
|
|
231
|
+
async function freshSessionToken(baseUrl) {
|
|
232
|
+
const session = storedSession(baseUrl);
|
|
233
|
+
if (!session) throw new TargetError(`No session is stored for ${sessionKey(baseUrl)}.`, "Sign in first with `rebase auth login` (it takes the same --url / --project flags).", "not_logged_in");
|
|
234
|
+
if (session.expiresAt > Date.now() + SESSION_EXPIRY_BUFFER_MS) return session.accessToken;
|
|
235
|
+
if (!session.refreshToken) throw new TargetError(`Your session for ${sessionKey(baseUrl)} has expired.`, "Run `rebase auth login` again.", "session_expired");
|
|
236
|
+
let res;
|
|
237
|
+
try {
|
|
238
|
+
res = await fetch(`${baseUrl.replace(/\/+$/, "")}/api/auth/refresh`, {
|
|
239
|
+
method: "POST",
|
|
240
|
+
headers: {
|
|
241
|
+
"Content-Type": "application/json",
|
|
242
|
+
"User-Agent": cliUserAgent()
|
|
243
|
+
},
|
|
244
|
+
body: JSON.stringify({ refreshToken: session.refreshToken })
|
|
245
|
+
});
|
|
246
|
+
} catch (e) {
|
|
247
|
+
throw new TargetError(`Could not reach ${sessionKey(baseUrl)} to refresh your session: ${errorText(e)}`, void 0, "network_error");
|
|
248
|
+
}
|
|
249
|
+
const tokens = sessionTokensOf(await res.json().catch(() => null));
|
|
250
|
+
if (!res.ok || !tokens) {
|
|
251
|
+
forgetSession(baseUrl);
|
|
252
|
+
throw new TargetError(`Your session for ${sessionKey(baseUrl)} has expired.`, "Run `rebase auth login` again.", "session_expired");
|
|
253
|
+
}
|
|
254
|
+
saveSession(baseUrl, {
|
|
255
|
+
...session,
|
|
256
|
+
...tokens
|
|
257
|
+
});
|
|
258
|
+
return tokens.accessToken;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* The token pair out of an auth response — `{ tokens: { accessToken,
|
|
262
|
+
* refreshToken, accessTokenExpiresAt } }`, the shape login, refresh and the
|
|
263
|
+
* device grant all answer.
|
|
264
|
+
*/
|
|
265
|
+
function sessionTokensOf(body) {
|
|
266
|
+
if (!isRecord$1(body)) return void 0;
|
|
267
|
+
const tokens = isRecord$1(body.tokens) ? body.tokens : body;
|
|
268
|
+
if (typeof tokens.accessToken !== "string" || !tokens.accessToken) return void 0;
|
|
269
|
+
const expiresRaw = tokens.accessTokenExpiresAt ?? tokens.expiresAt;
|
|
270
|
+
const expiresAt = typeof expiresRaw === "number" ? expiresRaw < 0xe8d4a51000 ? expiresRaw * 1e3 : expiresRaw : typeof expiresRaw === "string" ? Date.parse(expiresRaw) : Date.now() + 15 * 6e4;
|
|
271
|
+
return {
|
|
272
|
+
accessToken: tokens.accessToken,
|
|
273
|
+
...typeof tokens.refreshToken === "string" ? { refreshToken: tokens.refreshToken } : {},
|
|
274
|
+
expiresAt: Number.isFinite(expiresAt) ? expiresAt : Date.now() + 15 * 6e4
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* A refusal while resolving or reaching a target: what happened, what to do
|
|
279
|
+
* about it, and a stable code. Commands report it through the shared `fail`,
|
|
280
|
+
* so it is one JSON value when piped.
|
|
281
|
+
*/
|
|
282
|
+
var TargetError = class extends Error {
|
|
283
|
+
hint;
|
|
284
|
+
code;
|
|
285
|
+
constructor(message, hint, code = "error") {
|
|
286
|
+
super(message);
|
|
287
|
+
this.hint = hint;
|
|
288
|
+
this.code = code;
|
|
289
|
+
this.name = "TargetError";
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
/** One line of query string out of {@link RequestOptions.query}. */
|
|
293
|
+
function queryString(query) {
|
|
294
|
+
if (!query) return "";
|
|
295
|
+
const params = new URLSearchParams();
|
|
296
|
+
for (const [key, value] of Object.entries(query)) {
|
|
297
|
+
if (value === void 0) continue;
|
|
298
|
+
if (Array.isArray(value)) for (const item of value) params.append(key, String(item));
|
|
299
|
+
else params.append(key, String(value));
|
|
300
|
+
}
|
|
301
|
+
const qs = params.toString();
|
|
302
|
+
return qs ? `?${qs}` : "";
|
|
303
|
+
}
|
|
304
|
+
function errorText(e) {
|
|
305
|
+
return e instanceof Error ? e.message : String(e);
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* A `fetch` that sends one request with a bearer, refusing redirects: a 3xx
|
|
309
|
+
* that crosses origins drops `Authorization` in some runtimes and keeps it in
|
|
310
|
+
* others, and neither is what the caller meant.
|
|
311
|
+
*/
|
|
312
|
+
async function send(url, bearer, options, label) {
|
|
313
|
+
const headers = {
|
|
314
|
+
"User-Agent": cliUserAgent(),
|
|
315
|
+
Accept: "application/json",
|
|
316
|
+
...options.headers ?? {}
|
|
317
|
+
};
|
|
318
|
+
if (bearer) headers.Authorization = `Bearer ${bearer}`;
|
|
319
|
+
let body = options.body;
|
|
320
|
+
if (options.json !== void 0) {
|
|
321
|
+
body = JSON.stringify(options.json);
|
|
322
|
+
headers["Content-Type"] = "application/json";
|
|
323
|
+
}
|
|
324
|
+
const controller = options.timeoutMs ? new AbortController() : void 0;
|
|
325
|
+
const timer = controller ? setTimeout(() => controller.abort(), options.timeoutMs) : void 0;
|
|
326
|
+
const init = {
|
|
327
|
+
method: options.method ?? "GET",
|
|
328
|
+
headers,
|
|
329
|
+
body,
|
|
330
|
+
redirect: "manual",
|
|
331
|
+
signal: controller?.signal
|
|
332
|
+
};
|
|
333
|
+
if (body instanceof ReadableStream) init.duplex = "half";
|
|
334
|
+
try {
|
|
335
|
+
return await fetch(url, init);
|
|
336
|
+
} catch (e) {
|
|
337
|
+
if (controller?.signal.aborted) throw new TargetError(`${label} did not answer within ${Math.round((options.timeoutMs ?? 0) / 1e3)}s.`, void 0, "timeout");
|
|
338
|
+
throw new TargetError(`Could not reach ${label}: ${errorText(e)}`, void 0, "network_error");
|
|
339
|
+
} finally {
|
|
340
|
+
if (timer) clearTimeout(timer);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* The service key this project's `.env` holds, and the variable it came from.
|
|
345
|
+
* The shell's `REBASE_SERVICE_KEY` wins over the file.
|
|
346
|
+
*/
|
|
347
|
+
function projectServiceKey(projectRoot) {
|
|
348
|
+
const shell = process.env.REBASE_SERVICE_KEY?.trim();
|
|
349
|
+
if (shell) return {
|
|
350
|
+
key: shell,
|
|
351
|
+
variable: "REBASE_SERVICE_KEY"
|
|
352
|
+
};
|
|
353
|
+
if (!projectRoot) return void 0;
|
|
354
|
+
const env = readEnvFile(projectRoot);
|
|
355
|
+
if (env.SERVICE_KEY) return {
|
|
356
|
+
key: env.SERVICE_KEY,
|
|
357
|
+
variable: "SERVICE_KEY"
|
|
358
|
+
};
|
|
359
|
+
if (env.REBASE_SERVICE_KEY) return {
|
|
360
|
+
key: env.REBASE_SERVICE_KEY,
|
|
361
|
+
variable: "REBASE_SERVICE_KEY"
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
/** At most one of these may be on a line. */
|
|
365
|
+
function chosenCredential(flags) {
|
|
366
|
+
const picked = [
|
|
367
|
+
flags["--token"] !== void 0 ? "token" : void 0,
|
|
368
|
+
flags["--session"] ? "session" : void 0,
|
|
369
|
+
flags["--anon"] ? "anon" : void 0
|
|
370
|
+
].filter((c) => c !== void 0);
|
|
371
|
+
if (picked.length > 1) throw new TargetError("--token, --session and --anon each pick the credential; give one of them.", void 0, "usage");
|
|
372
|
+
return picked[0];
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Resolve a project command's target from its parsed flags.
|
|
376
|
+
*
|
|
377
|
+
* @throws TargetError with a hint and a code; the caller reports it.
|
|
378
|
+
*/
|
|
379
|
+
async function resolveProjectTarget(flags, options) {
|
|
380
|
+
const cloudRef = flags["--project"]?.trim() || (flags["--cloud"] ? "" : void 0);
|
|
381
|
+
if (cloudRef !== void 0 && flags["--url"]) throw new TargetError("--url names a server and --project/--cloud a Rebase Cloud project; give one of them.", void 0, "usage");
|
|
382
|
+
const credential = chosenCredential(flags);
|
|
383
|
+
if (cloudRef !== void 0) return resolveCloudTarget(cloudRef, credential, flags, options);
|
|
384
|
+
return resolveServerTarget(credential, flags, options);
|
|
385
|
+
}
|
|
386
|
+
async function resolveServerTarget(credential, flags, options) {
|
|
387
|
+
const projectRoot = findProjectRoot();
|
|
388
|
+
let baseUrl;
|
|
389
|
+
let label;
|
|
390
|
+
if (flags["--url"]) {
|
|
391
|
+
baseUrl = normalizeOrigin(flags["--url"]);
|
|
392
|
+
label = baseUrl;
|
|
393
|
+
} else {
|
|
394
|
+
if (!projectRoot) throw new TargetError("No server to send the request to: this is not a Rebase project, and the line names none.", "Pass --url <server> or --project <slug>, or run this inside a project.", "no_target");
|
|
395
|
+
const address = resolveBackendAddress(readEnvFile(projectRoot), projectRoot);
|
|
396
|
+
if (address.url === null) throw new TargetError("No server to send the request to.", address.reason, "no_target");
|
|
397
|
+
const note = cloudTargetNote(address);
|
|
398
|
+
if (note) process.stderr.write(chalk.yellow(` ⚠ ${note}\n`));
|
|
399
|
+
baseUrl = normalizeOrigin(address.url);
|
|
400
|
+
label = address.source === "running" ? `${baseUrl} (rebase dev)` : baseUrl;
|
|
401
|
+
}
|
|
402
|
+
const wantsPerson = options.defaultCredential === "person";
|
|
403
|
+
let bearer;
|
|
404
|
+
let used;
|
|
405
|
+
if (credential === "anon") {
|
|
406
|
+
bearer = async () => void 0;
|
|
407
|
+
used = "none";
|
|
408
|
+
} else if (credential === "token") {
|
|
409
|
+
const token = flags["--token"] ?? "";
|
|
410
|
+
if (!token.trim()) throw new TargetError("--token was given an empty value.", void 0, "usage");
|
|
411
|
+
bearer = async () => token;
|
|
412
|
+
used = "token";
|
|
413
|
+
} else if (credential === "session" || wantsPerson) {
|
|
414
|
+
bearer = () => freshSessionToken(baseUrl);
|
|
415
|
+
used = "session";
|
|
416
|
+
} else {
|
|
417
|
+
const found = projectServiceKey(projectRoot);
|
|
418
|
+
if (!found) throw new TargetError(`No credential for ${label}: REBASE_SERVICE_KEY is not in .env or the shell.`, "`rebase " + options.command + "` sends the project's service key by default. Pass --token <bearer>, --session (after `rebase auth login`) or --anon instead.", "no_credential");
|
|
419
|
+
bearer = async () => found.key;
|
|
420
|
+
used = "service-key";
|
|
421
|
+
}
|
|
422
|
+
return {
|
|
423
|
+
kind: "server",
|
|
424
|
+
label,
|
|
425
|
+
credential: used,
|
|
426
|
+
baseUrl,
|
|
427
|
+
bearer,
|
|
428
|
+
async request(requestPath, requestOptions = {}) {
|
|
429
|
+
return send(`${baseUrl}${requestPath}${queryString(requestOptions.query)}`, await bearer(), requestOptions, label);
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
/** `https://x.example/` and `x.example` both become `https://x.example`; a path is kept. */
|
|
434
|
+
function normalizeOrigin(raw) {
|
|
435
|
+
const trimmed = raw.trim().replace(/\/+$/, "");
|
|
436
|
+
if (/^https?:\/\//i.test(trimmed)) return trimmed;
|
|
437
|
+
return `${/^(localhost|127\.|\[::1\]|0\.0\.0\.0)/i.test(trimmed) ? "http" : "https"}://${trimmed}`;
|
|
438
|
+
}
|
|
439
|
+
async function resolveCloudTarget(ref, credential, flags, options) {
|
|
440
|
+
const cloud = await import("./context-BOMEoKxn.js").then((n) => n.u);
|
|
441
|
+
const link = cloud.readLink();
|
|
442
|
+
const projectRef = ref || link?.projectId;
|
|
443
|
+
if (!projectRef) throw new TargetError("--cloud acts on the project this directory is linked to, and it is not linked.", "Pass --project <slug>, or run `rebase cloud link`.", "no_project");
|
|
444
|
+
const cloudArgs = [
|
|
445
|
+
"node",
|
|
446
|
+
"rebase",
|
|
447
|
+
"--project",
|
|
448
|
+
projectRef
|
|
449
|
+
];
|
|
450
|
+
const { client, url } = await cloud.requireClient(cloudArgs);
|
|
451
|
+
let projectId;
|
|
452
|
+
try {
|
|
453
|
+
projectId = await cloud.resolveProjectRef(projectRef, client);
|
|
454
|
+
} catch (e) {
|
|
455
|
+
throw new TargetError(`Rebase Cloud project ${projectRef} not found: ${errorText(e)}`, "`rebase cloud projects list` shows the projects you can reach.", "not_found");
|
|
456
|
+
}
|
|
457
|
+
const label = `cloud project ${projectRef}`;
|
|
458
|
+
if (credential !== void 0 || options.platformScopes !== void 0 && options.platformScopes.length > 0) {
|
|
459
|
+
const { resolveOrigin } = await import("./debug-nNbbxAeo.js").then((n) => n.r);
|
|
460
|
+
const origin = await resolveOrigin(cloudArgs, client, url, projectId);
|
|
461
|
+
let bearer;
|
|
462
|
+
let used;
|
|
463
|
+
if (credential === "anon") {
|
|
464
|
+
bearer = async () => void 0;
|
|
465
|
+
used = "none";
|
|
466
|
+
} else if (credential === "token") {
|
|
467
|
+
const token = flags["--token"] ?? "";
|
|
468
|
+
bearer = async () => token;
|
|
469
|
+
used = "token";
|
|
470
|
+
} else if (credential === "session") {
|
|
471
|
+
bearer = () => freshSessionToken(origin);
|
|
472
|
+
used = "session";
|
|
473
|
+
} else {
|
|
474
|
+
const { mintRuntimeToken } = await import("./cron-ZRXsla0v.js");
|
|
475
|
+
const minted = await mintRuntimeToken(client, projectId, [...options.platformScopes ?? []]);
|
|
476
|
+
bearer = async () => minted.token;
|
|
477
|
+
used = "platform-token";
|
|
478
|
+
}
|
|
479
|
+
return {
|
|
480
|
+
kind: "cloud",
|
|
481
|
+
label: `${label} (${origin})`,
|
|
482
|
+
credential: used,
|
|
483
|
+
baseUrl: origin,
|
|
484
|
+
projectId,
|
|
485
|
+
bearer,
|
|
486
|
+
async request(requestPath, requestOptions = {}) {
|
|
487
|
+
return send(`${origin}${requestPath}${queryString(requestOptions.query)}`, await bearer(), requestOptions, label);
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const proxyBase = `${url.replace(/\/+$/, "")}/api/functions/studio/${encodeURIComponent(projectId)}/proxy`;
|
|
492
|
+
return {
|
|
493
|
+
kind: "cloud",
|
|
494
|
+
label,
|
|
495
|
+
credential: "studio-proxy",
|
|
496
|
+
baseUrl: proxyBase,
|
|
497
|
+
projectId,
|
|
498
|
+
bearer: () => cloud.freshAccessToken(client),
|
|
499
|
+
async request(requestPath, requestOptions = {}) {
|
|
500
|
+
const bearer = await cloud.freshAccessToken(client);
|
|
501
|
+
const res = await send(`${proxyBase}${requestPath}${queryString(requestOptions.query)}`, bearer, requestOptions, label);
|
|
502
|
+
await explainProxyRefusal(res, projectRef);
|
|
503
|
+
return res;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* The Studio proxy's own refusals, in the words of what to do about them.
|
|
509
|
+
*
|
|
510
|
+
* The proxy answers 401/403 itself — before the app sees anything — when the
|
|
511
|
+
* caller is not an owner or admin, when the token holds no `project:studio`,
|
|
512
|
+
* or when the project's Studio switch is off; the app's own refusals come back
|
|
513
|
+
* in its error envelope and are left to the caller. Read from a clone so the
|
|
514
|
+
* caller still has the body.
|
|
515
|
+
*/
|
|
516
|
+
async function explainProxyRefusal(res, projectRef) {
|
|
517
|
+
if (res.status !== 401 && res.status !== 403) return;
|
|
518
|
+
const body = await res.clone().json().catch(() => null);
|
|
519
|
+
const error = isRecord$1(body) && isRecord$1(body.error) ? body.error : void 0;
|
|
520
|
+
const message = typeof error?.message === "string" ? error.message : "";
|
|
521
|
+
if (/Studio access is switched off/i.test(message)) throw new TargetError(`Studio access is switched off for ${projectRef}, and this command reaches the app through the console's Studio proxy.`, `An organization owner or admin can turn it on: \`rebase cloud settings set --studio on --project ${projectRef}\`.`, "studio_disabled");
|
|
522
|
+
if (error && typeof error.code === "string" && /^(forbidden|unauthorized|scope_missing)$/i.test(error.code)) throw new TargetError(`The control plane refused the Studio proxy for ${projectRef}: ${message || error.code}`, "It answers organization owners and admins signed in with `rebase cloud login`; a token needs the project:studio scope. Pass --token <bearer> to call the app's public address with a credential of its own instead.", "proxy_refused");
|
|
523
|
+
}
|
|
524
|
+
function apiErrorOf(body) {
|
|
525
|
+
if (!isRecord$1(body) || !isRecord$1(body.error)) return void 0;
|
|
526
|
+
const e = body.error;
|
|
527
|
+
return {
|
|
528
|
+
...typeof e.code === "string" ? { code: e.code } : {},
|
|
529
|
+
...typeof e.message === "string" ? { message: e.message } : {},
|
|
530
|
+
...e.details !== void 0 ? { details: e.details } : {}
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Read a JSON answer, or throw a {@link TargetError} carrying the app's own
|
|
535
|
+
* code and message — and, on a 401, which credential was refused, since that
|
|
536
|
+
* is the question the next command will be about.
|
|
537
|
+
*/
|
|
538
|
+
async function readJson(res, target, action) {
|
|
539
|
+
const text = await res.text();
|
|
540
|
+
let body = null;
|
|
541
|
+
if (text) try {
|
|
542
|
+
body = JSON.parse(text);
|
|
543
|
+
} catch {
|
|
544
|
+
body = text;
|
|
545
|
+
}
|
|
546
|
+
if (res.ok) return body;
|
|
547
|
+
throw failureOf(res.status, body, target, action);
|
|
548
|
+
}
|
|
549
|
+
/** The refusal a non-2xx answer becomes. Exported for commands that read streams. */
|
|
550
|
+
function failureOf(status, body, target, action) {
|
|
551
|
+
const error = apiErrorOf(body);
|
|
552
|
+
const what = error?.message ?? (typeof body === "string" && body.trim() ? body.trim().slice(0, 300) : `HTTP ${status}`);
|
|
553
|
+
const code = error?.code ?? (status === 404 ? "not_found" : status === 401 ? "unauthenticated" : status === 403 ? "forbidden" : "error");
|
|
554
|
+
let hint;
|
|
555
|
+
if (status === 401) hint = target.credential === "service-key" ? `${target.label} did not accept REBASE_SERVICE_KEY — is it this project's server?` : target.credential === "session" ? "The stored session was refused; run `rebase auth login` again." : target.credential === "none" ? "This route needs a credential; drop --anon." : void 0;
|
|
556
|
+
else if (status === 501 && error?.code) hint = "The server does not offer this here (see the message); a newer @rebasepro/server may.";
|
|
557
|
+
return new TargetError(`${action} — ${code} (${status}): ${what}`, hint, code);
|
|
558
|
+
}
|
|
559
|
+
/** Lines that say why a server could not be reached — `rebase dev` not running, and where it would be. */
|
|
560
|
+
function unreachableHint(target) {
|
|
561
|
+
if (target.kind !== "server") return void 0;
|
|
562
|
+
return serverUnreachableHint(target.baseUrl).map((line) => line.trim()).join(" ");
|
|
563
|
+
}
|
|
564
|
+
//#endregion
|
|
565
|
+
//#region src/utils/project-command.ts
|
|
566
|
+
/**
|
|
567
|
+
* What every project command shares besides its target: the output mode, the
|
|
568
|
+
* help page, the parse refusal and the way a failed request is reported.
|
|
569
|
+
*
|
|
570
|
+
* The output contract is the cloud family's (`commands/cloud/context.ts`),
|
|
571
|
+
* reused rather than copied: one JSON value on stdout when `--json` is given,
|
|
572
|
+
* `REBASE_JSON=1` is set or stdout is not a terminal; everything else on
|
|
573
|
+
* stderr; a refusal is `{ "error": { message, code, hint } }` and exit 1; a
|
|
574
|
+
* destructive action refuses rather than prompts when nobody can answer.
|
|
575
|
+
*
|
|
576
|
+
* Help is the one difference: a project command's `--help` is the page a
|
|
577
|
+
* person reads even when piped (`rebase data --help | less`), and JSON only
|
|
578
|
+
* when `--json` asks for it.
|
|
579
|
+
*/
|
|
580
|
+
/** Latch the output mode for this run. Call first, before anything prints. */
|
|
581
|
+
function startProjectCommand(rawArgs) {
|
|
582
|
+
initOutputMode(rawArgs, { whenPiped: wantsHelp(rawArgs) ? "human" : "json" });
|
|
583
|
+
}
|
|
584
|
+
/** A project group's help page, with the target flags as its global options. */
|
|
585
|
+
function printProjectHelp(page) {
|
|
586
|
+
printGroupHelp({
|
|
587
|
+
...page,
|
|
588
|
+
globalFlags: PROJECT_TARGET_HELP
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Run a parse, and report its refusal through {@link fail} — one JSON value
|
|
593
|
+
* when piped — rather than as a throw, which `bin/rebase.js` prints as text.
|
|
594
|
+
*/
|
|
595
|
+
function parsedLine(parse) {
|
|
596
|
+
try {
|
|
597
|
+
return parse();
|
|
598
|
+
} catch (e) {
|
|
599
|
+
fail(e instanceof Error ? e.message : String(e), void 0, "usage");
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
/** Report a {@link TargetError} through {@link fail}; rethrow anything else. */
|
|
603
|
+
function reportTargetError(e, target) {
|
|
604
|
+
if (e instanceof TargetError) {
|
|
605
|
+
const hint = e.code === "network_error" && target ? unreachableHint(target) ?? e.hint : e.hint;
|
|
606
|
+
fail(e.message, hint, e.code);
|
|
607
|
+
}
|
|
608
|
+
throw e;
|
|
609
|
+
}
|
|
610
|
+
/** Resolve the target, or end the command with why not. */
|
|
611
|
+
async function projectTarget(flags, options) {
|
|
612
|
+
try {
|
|
613
|
+
return await resolveProjectTarget(targetFlagsOf(flags), options);
|
|
614
|
+
} catch (e) {
|
|
615
|
+
reportTargetError(e);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
/** The target flags out of a command's parsed flags, whatever else its spec declares. */
|
|
619
|
+
function targetFlagsOf(flags) {
|
|
620
|
+
const str = (key) => typeof flags[key] === "string" ? String(flags[key]) : void 0;
|
|
621
|
+
const bool = (key) => flags[key] === true ? true : void 0;
|
|
622
|
+
return {
|
|
623
|
+
"--project": str("--project"),
|
|
624
|
+
"--cloud": bool("--cloud"),
|
|
625
|
+
"--url": str("--url"),
|
|
626
|
+
"--token": str("--token"),
|
|
627
|
+
"--session": bool("--session"),
|
|
628
|
+
"--anon": bool("--anon")
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
/** Send one request and read its JSON answer, or end the command with the app's own refusal. */
|
|
632
|
+
async function requestJson(target, path, action, options = {}) {
|
|
633
|
+
try {
|
|
634
|
+
return await readJson(await target.request(path, {
|
|
635
|
+
timeoutMs: 6e4,
|
|
636
|
+
...options
|
|
637
|
+
}), target, action);
|
|
638
|
+
} catch (e) {
|
|
639
|
+
reportTargetError(e, target);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
/** Narrowing helpers for the answers commands read. */
|
|
643
|
+
function isRecord(value) {
|
|
644
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
645
|
+
}
|
|
646
|
+
function recordsOf(value) {
|
|
647
|
+
return Array.isArray(value) ? value.filter(isRecord) : [];
|
|
648
|
+
}
|
|
649
|
+
function stringOf(value) {
|
|
650
|
+
return typeof value === "string" ? value : typeof value === "number" ? String(value) : void 0;
|
|
651
|
+
}
|
|
652
|
+
/** A cell's text for a table: strings as they are, everything else as compact JSON. */
|
|
653
|
+
function cellText(value) {
|
|
654
|
+
if (value === null || value === void 0) return "";
|
|
655
|
+
if (typeof value === "string") return value;
|
|
656
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
657
|
+
return JSON.stringify(value);
|
|
658
|
+
}
|
|
659
|
+
//#endregion
|
|
660
|
+
export { resolveBackendAddress as _, projectTarget as a, storedSession as b, requestJson as c, PROJECT_TARGET_FLAGS as d, cloudTargetNote as f, readJson as g, projectServiceKey as h, printProjectHelp as i, startProjectCommand as l, forgetSession as m, isRecord as n, recordsOf as o, failureOf as p, parsedLine as r, reportTargetError as s, cellText as t, stringOf as u, saveSession as v, sessionKey as y };
|
|
661
|
+
|
|
662
|
+
//# sourceMappingURL=project-command-Cl9EZzjr.js.map
|