@sequenceholdings/studio-cli 0.9.24 → 0.9.26
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/README.md +14 -2
- package/dist/app-preview/commands.d.ts +7 -0
- package/dist/app-preview/commands.js +264 -0
- package/dist/app-preview/fingerprint.d.ts +20 -0
- package/dist/app-preview/fingerprint.js +32 -0
- package/dist/app-preview/install.d.ts +31 -0
- package/dist/app-preview/install.js +53 -0
- package/dist/app-preview/namespace-probe.d.ts +13 -0
- package/dist/app-preview/namespace-probe.js +50 -0
- package/dist/app-preview/plan.d.ts +28 -0
- package/dist/app-preview/plan.js +41 -0
- package/dist/app-preview/run.d.ts +32 -0
- package/dist/app-preview/run.js +48 -0
- package/dist/app-preview/runners.d.ts +73 -0
- package/dist/app-preview/runners.js +226 -0
- package/dist/app-preview/secrets.d.ts +48 -0
- package/dist/app-preview/secrets.js +104 -0
- package/dist/app-preview/target.d.ts +28 -0
- package/dist/app-preview/target.js +62 -0
- package/dist/artifact/delegate.d.ts +0 -11
- package/dist/artifact/delegate.js +6 -127
- package/dist/artifact/sandbox.js +1 -1
- package/dist/auth.js +1 -3
- package/dist/command-help.d.ts +3 -3
- package/dist/command-help.js +7 -3
- package/dist/config.d.ts +0 -7
- package/dist/config.js +0 -12
- package/dist/edge-auth-headers.js +1 -18
- package/dist/functions/commands.d.ts +15 -0
- package/dist/functions/commands.js +67 -11
- package/dist/main.js +1 -1
- package/dist/registry/lifecycle-commands.js +7 -0
- package/dist/repos/commands.d.ts +1 -4
- package/dist/repos/commands.js +2 -19
- package/dist/templates/catalog.d.ts +1 -1
- package/dist/templates/catalog.js +1 -1
- package/package.json +10 -10
- package/release-summary.json +3 -1
- package/dist/preview.d.ts +0 -90
- package/dist/preview.js +0 -292
package/README.md
CHANGED
|
@@ -212,8 +212,7 @@ packages, so its `package.json` must have no entries in `dependencies` or
|
|
|
212
212
|
`optionalDependencies`; one that does is refused rather than installed. Atlas
|
|
213
213
|
needs a committed `pnpm-lock.yaml` for a Next.js build, which a sandbox cannot
|
|
214
214
|
generate, so start new sandbox apps from the default static template. `build`, `plan` and `deploy` require `--repo`;
|
|
215
|
-
`dev`, `login`, `logout`, `--git-url
|
|
216
|
-
are unavailable, and `--token` is refused as everywhere else. List, show,
|
|
215
|
+
`dev`, `login`, `logout`, `--git-url` and `--source-env` are unavailable, and `--token` is refused as everywhere else. List, show,
|
|
217
216
|
status, whoami, pull, link, promote and rollback work as usual.
|
|
218
217
|
|
|
219
218
|
## Environments
|
|
@@ -1526,6 +1525,19 @@ seq-studio function logs --dir ./my-function -e local
|
|
|
1526
1525
|
seq-studio function delete --dir ./my-function -e local --yes
|
|
1527
1526
|
```
|
|
1528
1527
|
|
|
1528
|
+
A local Atlas that runs on a copy of another environment's database lists that environment's
|
|
1529
|
+
functions, but their runtime is elsewhere, so deploying or invoking one fails with "belongs to
|
|
1530
|
+
another runtime". On a personal development database branch, take the function over:
|
|
1531
|
+
|
|
1532
|
+
```bash
|
|
1533
|
+
seq-studio function deploy --dir ./my-function -e local --adopt
|
|
1534
|
+
```
|
|
1535
|
+
|
|
1536
|
+
`--adopt` rebinds the copied row to this local Atlas and then deploys. The function keeps its
|
|
1537
|
+
slug, so its callers need no change. It has no active version until that deploy finishes, and
|
|
1538
|
+
its secrets need local values. The runtime it was copied from is not contacted or changed.
|
|
1539
|
+
A local Atlas on any shared database branch refuses it.
|
|
1540
|
+
|
|
1529
1541
|
For a worktree, replace `local` with its environment from `seq-studio envs list`
|
|
1530
1542
|
(for example `local:my-worktree`). Local Atlas builds the uploaded source and
|
|
1531
1543
|
runs Google's Functions Framework in a supervised local Node process. It needs
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ParsedArgs } from '../argv.js';
|
|
2
|
+
/** Set where the local Atlas is reached through a tunnel, so the printed link opens for the viewer. */
|
|
3
|
+
export declare const PREVIEW_ORIGIN_ENV = "SEQ_STUDIO_PREVIEW_ORIGIN";
|
|
4
|
+
/** A directory of reviewed `<app id>.env` secret files, for machines provisioned to preview apps. */
|
|
5
|
+
export declare const PREVIEW_SECRETS_DIR_ENV = "SEQ_STUDIO_PREVIEW_SECRETS_DIR";
|
|
6
|
+
export declare const PREVIEW_USAGE = "usage: seq-studio preview [dir] -e local [--only <id>[,<id>...]] [--secrets <file>] [--apply-orm] [--watch] [--force] [--yes] [--json]\n\nStand up this app on a local Atlas and print where to open it. Reads sequence.app.yml and, in\ndependency order: checks each ORM namespace answers reads, deploys each function to the local\nruntime, and publishes each artifact to its preview channel. By default it previews the app's\nartifacts and what they depend on; --only names other primitives (a process, a function) to start.\n\nRuns only against a local Atlas on a personal or devbox database branch. It changes that database\ncopy and nothing else. Only the database is a copy: a function deployed here runs with the secret\nvalues you give it and reaches the real systems those credentials open. Declared egress is not\nenforced by the local runtime. Each function's egress and secret sources are shown before it deploys.\n\n --only <ids> preview these primitives and their dependencies instead of the artifacts\n --secrets <file> reviewed dotenv file of function secret values (default: $SEQ_STUDIO_PREVIEW_SECRETS_DIR/<app id>.env).\n A value may use ${preview.atlas_origin}, ${preview.database.host},\n ${preview.database.endpoint}, ${preview.database.name}, ${preview.database.branch}.\n ${env.NAME} takes the value from that variable of this command's environment.\n A secret with no value there or in this local Atlas stops the preview.\n --apply-orm apply each namespace's local definition to this database copy before checking it\n --watch keep rebuilding a static artifact on change (a Next.js app always stays attached)\n --force redeploy a function even when nothing it is built from changed. Needed after a\n credential held in an environment variable is rotated.\n --yes do not ask before deploying functions\n --json print the result as JSON\n\nSet SEQ_STUDIO_PREVIEW_ORIGIN to the URL you open the local Atlas at when that is not its own address.";
|
|
7
|
+
export declare function runPreviewCommand(args: ParsedArgs): Promise<number>;
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { getJsonOr404 } from '../atlas-client.js';
|
|
5
|
+
import { loadAppManifest } from '../app/manifest.js';
|
|
6
|
+
import { ExitCode, UsageError } from '../exit-codes.js';
|
|
7
|
+
import { buildContext, clientOptions, flagBool, parseDotenv, requestedEnvironment } from '../functions/commands.js';
|
|
8
|
+
import { resolveConfiguredEnv } from '../config.js';
|
|
9
|
+
import { REQUIRE_EXPLICIT_ENV_MESSAGE } from '../env-flags.js';
|
|
10
|
+
import { isLoopbackUrl } from '../host-token.js';
|
|
11
|
+
import { confirmYes, LOG } from '../prompt.js';
|
|
12
|
+
import { planPreview } from './plan.js';
|
|
13
|
+
import { previewSucceeded, runPreviewPlan } from './run.js';
|
|
14
|
+
import { applyProcess, checkNamespace, deployFunction, inspectArtifact, DEFAULT_PREVIEW_KEY, ensureArtifactDependencies, inspectFunction, publishStaticArtifact, } from './runners.js';
|
|
15
|
+
import { describeSecretSource } from './secrets.js';
|
|
16
|
+
import { decidePreviewTarget } from './target.js';
|
|
17
|
+
/** Set where the local Atlas is reached through a tunnel, so the printed link opens for the viewer. */
|
|
18
|
+
export const PREVIEW_ORIGIN_ENV = 'SEQ_STUDIO_PREVIEW_ORIGIN';
|
|
19
|
+
/** A directory of reviewed `<app id>.env` secret files, for machines provisioned to preview apps. */
|
|
20
|
+
export const PREVIEW_SECRETS_DIR_ENV = 'SEQ_STUDIO_PREVIEW_SECRETS_DIR';
|
|
21
|
+
export const PREVIEW_USAGE = `usage: seq-studio preview [dir] -e local [--only <id>[,<id>...]] [--secrets <file>] [--apply-orm] [--watch] [--force] [--yes] [--json]
|
|
22
|
+
|
|
23
|
+
Stand up this app on a local Atlas and print where to open it. Reads sequence.app.yml and, in
|
|
24
|
+
dependency order: checks each ORM namespace answers reads, deploys each function to the local
|
|
25
|
+
runtime, and publishes each artifact to its preview channel. By default it previews the app's
|
|
26
|
+
artifacts and what they depend on; --only names other primitives (a process, a function) to start.
|
|
27
|
+
|
|
28
|
+
Runs only against a local Atlas on a personal or devbox database branch. It changes that database
|
|
29
|
+
copy and nothing else. Only the database is a copy: a function deployed here runs with the secret
|
|
30
|
+
values you give it and reaches the real systems those credentials open. Declared egress is not
|
|
31
|
+
enforced by the local runtime. Each function's egress and secret sources are shown before it deploys.
|
|
32
|
+
|
|
33
|
+
--only <ids> preview these primitives and their dependencies instead of the artifacts
|
|
34
|
+
--secrets <file> reviewed dotenv file of function secret values (default: $${PREVIEW_SECRETS_DIR_ENV}/<app id>.env).
|
|
35
|
+
A value may use \${preview.atlas_origin}, \${preview.database.host},
|
|
36
|
+
\${preview.database.endpoint}, \${preview.database.name}, \${preview.database.branch}.
|
|
37
|
+
\${env.NAME} takes the value from that variable of this command's environment.
|
|
38
|
+
A secret with no value there or in this local Atlas stops the preview.
|
|
39
|
+
--apply-orm apply each namespace's local definition to this database copy before checking it
|
|
40
|
+
--watch keep rebuilding a static artifact on change (a Next.js app always stays attached)
|
|
41
|
+
--force redeploy a function even when nothing it is built from changed. Needed after a
|
|
42
|
+
credential held in an environment variable is rotated.
|
|
43
|
+
--yes do not ask before deploying functions
|
|
44
|
+
--json print the result as JSON
|
|
45
|
+
|
|
46
|
+
Set ${PREVIEW_ORIGIN_ENV} to the URL you open the local Atlas at when that is not its own address.`;
|
|
47
|
+
function csv(value) {
|
|
48
|
+
if (typeof value !== 'string')
|
|
49
|
+
return undefined;
|
|
50
|
+
const parts = value.split(',').map((part) => part.trim()).filter(Boolean);
|
|
51
|
+
if (parts.length === 0)
|
|
52
|
+
throw new UsageError('--only requires at least one primitive id');
|
|
53
|
+
return parts;
|
|
54
|
+
}
|
|
55
|
+
async function readSecretsFile({ flag, appId }) {
|
|
56
|
+
if (flag !== undefined && typeof flag !== 'string')
|
|
57
|
+
throw new UsageError('--secrets requires a file path');
|
|
58
|
+
const fromEnv = process.env[PREVIEW_SECRETS_DIR_ENV];
|
|
59
|
+
const file = flag ? resolve(flag) : fromEnv ? join(resolve(fromEnv), `${appId}.env`) : null;
|
|
60
|
+
if (!file)
|
|
61
|
+
return { file: null, values: {} };
|
|
62
|
+
if (!existsSync(file)) {
|
|
63
|
+
if (flag)
|
|
64
|
+
throw new UsageError(`--secrets: file not found: ${file}`);
|
|
65
|
+
return { file: null, values: {} };
|
|
66
|
+
}
|
|
67
|
+
return { file, values: parseDotenv(await readFile(file, 'utf8')) };
|
|
68
|
+
}
|
|
69
|
+
function publicOrigin(atlasOrigin) {
|
|
70
|
+
const configured = process.env[PREVIEW_ORIGIN_ENV]?.trim();
|
|
71
|
+
if (!configured)
|
|
72
|
+
return atlasOrigin;
|
|
73
|
+
let url;
|
|
74
|
+
try {
|
|
75
|
+
url = new URL(configured);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw new UsageError(`${PREVIEW_ORIGIN_ENV} is not a URL: ${configured}`);
|
|
79
|
+
}
|
|
80
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:')
|
|
81
|
+
throw new UsageError(`${PREVIEW_ORIGIN_ENV} must be an http(s) URL`);
|
|
82
|
+
return url.origin;
|
|
83
|
+
}
|
|
84
|
+
function functionReport(inspection) {
|
|
85
|
+
const lines = [`${LOG} function ${inspection.slug} (${inspection.entry.id})`];
|
|
86
|
+
lines.push(inspection.egress.length
|
|
87
|
+
? `${LOG} declares egress to: ${inspection.egress.join(', ')}`
|
|
88
|
+
: `${LOG} declares no egress`);
|
|
89
|
+
lines.push(`${LOG} egress is NOT enforced here: the local runtime can reach any host its credentials open`);
|
|
90
|
+
if (inspection.secrets.sources.length === 0 && inspection.unavailableSets.length === 0) {
|
|
91
|
+
lines.push(`${LOG} secrets: none declared`);
|
|
92
|
+
}
|
|
93
|
+
for (const source of inspection.secrets.sources) {
|
|
94
|
+
lines.push(`${LOG} secret ${source.name}: ${describeSecretSource(source)}`);
|
|
95
|
+
}
|
|
96
|
+
for (const set of inspection.unavailableSets) {
|
|
97
|
+
lines.push(`${LOG} key/value set ${set}: NO VALUE in this local Atlas`);
|
|
98
|
+
}
|
|
99
|
+
return lines;
|
|
100
|
+
}
|
|
101
|
+
function missingSecretsMessage({ inspections, secretsFile, appId }) {
|
|
102
|
+
const lines = [];
|
|
103
|
+
for (const inspection of inspections) {
|
|
104
|
+
const names = [...inspection.secrets.missing, ...inspection.unavailableSets];
|
|
105
|
+
if (names.length)
|
|
106
|
+
lines.push(`${LOG} ${inspection.slug} has no value for: ${names.join(', ')}`);
|
|
107
|
+
}
|
|
108
|
+
if (lines.length === 0)
|
|
109
|
+
return lines;
|
|
110
|
+
lines.push(`${LOG} A database copy does not carry secret values, and preview never substitutes another credential.`, secretsFile
|
|
111
|
+
? `${LOG} Add them to ${secretsFile}, or store each in this local Atlas with: seq-studio secret set <NAME> -e local`
|
|
112
|
+
: `${LOG} Pass a reviewed dotenv file with --secrets <file> (or put ${appId}.env under $${PREVIEW_SECRETS_DIR_ENV}), or store each in this local Atlas with: seq-studio secret set <NAME> -e local`);
|
|
113
|
+
return lines;
|
|
114
|
+
}
|
|
115
|
+
function printSummary({ results, plan, target }) {
|
|
116
|
+
console.log(`${LOG} preview on database branch ${target.database.branch}:`);
|
|
117
|
+
const width = Math.max(...results.map((result) => result.id.length));
|
|
118
|
+
for (const result of results) {
|
|
119
|
+
console.log(`${LOG} ${result.status.padEnd(11)} ${result.id.padEnd(width)} ${result.detail}`);
|
|
120
|
+
}
|
|
121
|
+
if (plan.omitted.length) {
|
|
122
|
+
console.log(`${LOG} not part of this preview: ${plan.omitted.map((entry) => entry.id).join(', ')} (start one with --only)`);
|
|
123
|
+
}
|
|
124
|
+
for (const result of results) {
|
|
125
|
+
if (result.url)
|
|
126
|
+
console.log(`${LOG} open: ${result.url}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** Decide the target, refusing a hosted environment before signing in to it or sending it anything. */
|
|
130
|
+
async function resolveTarget({ args, envName, rootDir }) {
|
|
131
|
+
const requested = await resolveConfiguredEnv({ requested: envName });
|
|
132
|
+
const hosted = decidePreviewTarget({ envName: requested.name, url: requested.url, report: null });
|
|
133
|
+
if (!isLoopbackUrl(requested.url) && !hosted.ok) {
|
|
134
|
+
console.error(`${LOG} ${hosted.reason}`);
|
|
135
|
+
return ExitCode.Blocked;
|
|
136
|
+
}
|
|
137
|
+
const command = await buildContext(args);
|
|
138
|
+
const client = clientOptions(command);
|
|
139
|
+
const report = await getJsonOr404({ ...client, path: '/api/platform/local-runtime' });
|
|
140
|
+
const decision = decidePreviewTarget({ envName: command.env.name, url: command.env.url, report });
|
|
141
|
+
if (!decision.ok) {
|
|
142
|
+
console.error(`${LOG} ${decision.reason}`);
|
|
143
|
+
return ExitCode.Blocked;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
rootDir,
|
|
147
|
+
env: command.env.name,
|
|
148
|
+
client,
|
|
149
|
+
target: decision.target,
|
|
150
|
+
publicOrigin: publicOrigin(decision.target.origin),
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
export async function runPreviewCommand(args) {
|
|
154
|
+
if (args.flags.help === true || args.flags.h === true) {
|
|
155
|
+
console.log(PREVIEW_USAGE);
|
|
156
|
+
return ExitCode.Ok;
|
|
157
|
+
}
|
|
158
|
+
const envName = requestedEnvironment(args);
|
|
159
|
+
if (!envName) {
|
|
160
|
+
console.error(`${LOG} ${REQUIRE_EXPLICIT_ENV_MESSAGE}`);
|
|
161
|
+
return ExitCode.Usage;
|
|
162
|
+
}
|
|
163
|
+
const json = flagBool(args.flags, 'json');
|
|
164
|
+
const rootDir = resolve(args.positional[0] ?? (typeof args.flags.dir === 'string' ? args.flags.dir : '.'));
|
|
165
|
+
const manifest = await loadAppManifest(rootDir);
|
|
166
|
+
const plan = planPreview({ manifest, only: csv(args.flags.only) });
|
|
167
|
+
const resolved = await resolveTarget({ args, envName, rootDir });
|
|
168
|
+
if (typeof resolved === 'number')
|
|
169
|
+
return resolved;
|
|
170
|
+
const ctx = resolved;
|
|
171
|
+
const secrets = await readSecretsFile({ flag: args.flags.secrets, appId: manifest.app.id });
|
|
172
|
+
const functions = new Map();
|
|
173
|
+
const artifacts = new Map();
|
|
174
|
+
for (const step of plan.steps) {
|
|
175
|
+
if (step.action === 'deploy-function') {
|
|
176
|
+
functions.set(step.entry.id, await inspectFunction({ ctx, entry: step.entry, provided: secrets.values, providedFile: secrets.file }));
|
|
177
|
+
}
|
|
178
|
+
if (step.action === 'publish-artifact')
|
|
179
|
+
artifacts.set(step.entry.id, await inspectArtifact({ ctx, entry: step.entry }));
|
|
180
|
+
}
|
|
181
|
+
const nextApps = [...artifacts.values()].filter((artifact) => artifact.nextjs);
|
|
182
|
+
if (nextApps.length > 1) {
|
|
183
|
+
throw new UsageError(`A Next.js preview stays attached to this terminal, so one command starts one: pass --only with one of ${nextApps.map((app) => app.entry.id).join(', ')}.`);
|
|
184
|
+
}
|
|
185
|
+
const overview = [
|
|
186
|
+
`${LOG} previewing ${manifest.app.id} on the local Atlas at ${ctx.target.origin} (database branch ${ctx.target.database.branch})`,
|
|
187
|
+
`${LOG} steps: ${plan.steps.map((step) => step.entry.id).join(' -> ')}`,
|
|
188
|
+
...[...functions.values()].flatMap(functionReport),
|
|
189
|
+
];
|
|
190
|
+
const missing = missingSecretsMessage({ inspections: [...functions.values()], secretsFile: secrets.file, appId: manifest.app.id });
|
|
191
|
+
if (missing.length) {
|
|
192
|
+
for (const line of overview)
|
|
193
|
+
console.error(line);
|
|
194
|
+
for (const line of missing)
|
|
195
|
+
console.error(line);
|
|
196
|
+
return ExitCode.Blocked;
|
|
197
|
+
}
|
|
198
|
+
const startsCode = plan.steps.some((step) => step.action === 'deploy-function' || step.action === 'apply-process');
|
|
199
|
+
// Under --json, stdout carries the result document and nothing else, whoever calls this.
|
|
200
|
+
const say = json ? console.error : console.log;
|
|
201
|
+
for (const line of overview)
|
|
202
|
+
say(line);
|
|
203
|
+
if (startsCode && !(await confirmYes({ preview: [], confirmed: flagBool(args.flags, 'yes') })))
|
|
204
|
+
return ExitCode.Failed;
|
|
205
|
+
const applyOrm = flagBool(args.flags, 'apply-orm');
|
|
206
|
+
const results = await runPreviewPlan({
|
|
207
|
+
plan,
|
|
208
|
+
run: async (step) => {
|
|
209
|
+
switch (step.action) {
|
|
210
|
+
case 'check-namespace':
|
|
211
|
+
return checkNamespace({ ctx, entry: step.entry, apply: applyOrm });
|
|
212
|
+
case 'deploy-function':
|
|
213
|
+
return deployFunction({ ctx, inspection: functions.get(step.entry.id), force: flagBool(args.flags, 'force') });
|
|
214
|
+
case 'apply-process':
|
|
215
|
+
return applyProcess({ ctx, entry: step.entry });
|
|
216
|
+
case 'publish-artifact': {
|
|
217
|
+
const inspection = artifacts.get(step.entry.id);
|
|
218
|
+
return inspection.nextjs
|
|
219
|
+
? { status: 'ready', detail: `Next.js app ${inspection.projectId}: its owner-only session starts below and prints the link` }
|
|
220
|
+
: publishStaticArtifact({ ctx, inspection });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
const ok = previewSucceeded(results);
|
|
226
|
+
if (json) {
|
|
227
|
+
console.log(JSON.stringify({
|
|
228
|
+
ok,
|
|
229
|
+
app: manifest.app.id,
|
|
230
|
+
atlas: ctx.target.origin,
|
|
231
|
+
database: { branch: ctx.target.database.branch },
|
|
232
|
+
steps: results,
|
|
233
|
+
omitted: plan.omitted.map((entry) => entry.id),
|
|
234
|
+
urls: results.flatMap((result) => (result.url ? [result.url] : [])),
|
|
235
|
+
}));
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
printSummary({ results, plan, target: ctx.target });
|
|
239
|
+
}
|
|
240
|
+
if (!ok)
|
|
241
|
+
return ExitCode.Failed;
|
|
242
|
+
return attachSession({ ctx, next: nextApps[0], watched: flagBool(args.flags, 'watch') ? [...artifacts.values()][0] : undefined });
|
|
243
|
+
}
|
|
244
|
+
/** A Next.js app, or a watched static one, keeps this command attached until it is stopped. */
|
|
245
|
+
async function attachSession({ ctx, next, watched }) {
|
|
246
|
+
if (!next && !watched)
|
|
247
|
+
return ExitCode.Ok;
|
|
248
|
+
const { runArtifactCommand } = await import('../artifact/delegate.js');
|
|
249
|
+
const failed = await ensureArtifactDependencies({ ctx, inspection: (next ?? watched) });
|
|
250
|
+
if (failed) {
|
|
251
|
+
console.error(`${LOG} ${failed.detail}`);
|
|
252
|
+
return ExitCode.Failed;
|
|
253
|
+
}
|
|
254
|
+
if (next) {
|
|
255
|
+
console.error(`${LOG} starting the Next.js session for ${next.projectId}; it runs until you stop it (Ctrl+C).`);
|
|
256
|
+
console.error(`${LOG} its link is served by the local app gateway, which a remote viewer must also be able to reach.`);
|
|
257
|
+
if (ctx.publicOrigin !== ctx.target.origin) {
|
|
258
|
+
console.error(`${LOG} the link below is printed on ${ctx.target.origin}; open the same path on ${ctx.publicOrigin}`);
|
|
259
|
+
}
|
|
260
|
+
return runArtifactCommand('dev', [next.dir, '--env', ctx.env]);
|
|
261
|
+
}
|
|
262
|
+
console.error(`${LOG} watching ${watched.projectId} for changes (Ctrl+C to stop)`);
|
|
263
|
+
return runArtifactCommand('dev', [watched.dir, '--env', ctx.env, '--key', DEFAULT_PREVIEW_KEY]);
|
|
264
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SecretSource } from './secrets.js';
|
|
2
|
+
/**
|
|
3
|
+
* What a function deploy was made from, recorded in the version's deploy message so the next
|
|
4
|
+
* preview can tell there is nothing new to deploy without keeping any state on disk.
|
|
5
|
+
*
|
|
6
|
+
* It covers the bundle, the secret sources and every value that is not a credential. A value read
|
|
7
|
+
* from an environment variable is represented by the variable's name only, so no derivative of a
|
|
8
|
+
* credential is ever written into deployment metadata. The cost: rotating such a credential does
|
|
9
|
+
* not change the fingerprint, and needs `--force`.
|
|
10
|
+
*/
|
|
11
|
+
export declare function functionFingerprint({ files, sources, values }: {
|
|
12
|
+
files: ReadonlyArray<{
|
|
13
|
+
path: string;
|
|
14
|
+
content: string;
|
|
15
|
+
}>;
|
|
16
|
+
sources: readonly SecretSource[];
|
|
17
|
+
values: Readonly<Record<string, string>>;
|
|
18
|
+
}): string;
|
|
19
|
+
export declare function fingerprintMessage(fingerprint: string): string;
|
|
20
|
+
export declare function fingerprintOf(deployMessage: string | null | undefined): string | undefined;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
const MARKER = /\bpreview:([a-f0-9]{16})\b/;
|
|
3
|
+
/**
|
|
4
|
+
* What a function deploy was made from, recorded in the version's deploy message so the next
|
|
5
|
+
* preview can tell there is nothing new to deploy without keeping any state on disk.
|
|
6
|
+
*
|
|
7
|
+
* It covers the bundle, the secret sources and every value that is not a credential. A value read
|
|
8
|
+
* from an environment variable is represented by the variable's name only, so no derivative of a
|
|
9
|
+
* credential is ever written into deployment metadata. The cost: rotating such a credential does
|
|
10
|
+
* not change the fingerprint, and needs `--force`.
|
|
11
|
+
*/
|
|
12
|
+
export function functionFingerprint({ files, sources, values }) {
|
|
13
|
+
const hash = createHash('sha256');
|
|
14
|
+
for (const file of [...files].sort((a, b) => a.path.localeCompare(b.path))) {
|
|
15
|
+
hash.update(`file\0${file.path}\0${file.content}\0`);
|
|
16
|
+
}
|
|
17
|
+
for (const source of [...sources].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
18
|
+
const described = source.source === 'environment'
|
|
19
|
+
? `environment:${source.variables.join(',')}`
|
|
20
|
+
: source.source === 'derived' || source.source === 'provided'
|
|
21
|
+
? `${source.source}:${values[source.name] ?? ''}`
|
|
22
|
+
: source.source;
|
|
23
|
+
hash.update(`secret\0${source.name}\0${described}\0`);
|
|
24
|
+
}
|
|
25
|
+
return hash.digest('hex').slice(0, 16);
|
|
26
|
+
}
|
|
27
|
+
export function fingerprintMessage(fingerprint) {
|
|
28
|
+
return `seq-studio preview:${fingerprint}`;
|
|
29
|
+
}
|
|
30
|
+
export function fingerprintOf(deployMessage) {
|
|
31
|
+
return deployMessage ? MARKER.exec(deployMessage)?.[1] : undefined;
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A frozen install fails when pnpm's peer setting differs from the one the lockfile was written
|
|
3
|
+
* with, and a fresh clone has no local workspace file to say which that was. The lockfile records
|
|
4
|
+
* it, so install with the lockfile's own value.
|
|
5
|
+
*/
|
|
6
|
+
export declare function lockfilePeerSetting(lockfile: string): boolean | undefined;
|
|
7
|
+
export type InstallDecision = {
|
|
8
|
+
install: false;
|
|
9
|
+
reason: 'installed' | 'no-package';
|
|
10
|
+
} | {
|
|
11
|
+
install: true;
|
|
12
|
+
root: string;
|
|
13
|
+
args: string[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A fresh clone has no node_modules, and an artifact cannot build without them. Install where the
|
|
17
|
+
* artifact resolves its dependencies from: its own directory when it keeps its own workspace or
|
|
18
|
+
* lockfile, else the app root. A committed lockfile is installed exactly; one is never rewritten
|
|
19
|
+
* by a preview.
|
|
20
|
+
*/
|
|
21
|
+
export declare function decideInstall({ artifactDir, appRoot, exists, read }: {
|
|
22
|
+
artifactDir: string;
|
|
23
|
+
appRoot: string;
|
|
24
|
+
exists?: (path: string) => boolean;
|
|
25
|
+
read?: (path: string) => string | undefined;
|
|
26
|
+
}): InstallDecision;
|
|
27
|
+
/** Progress goes to stderr so `--json` output stays a single document. */
|
|
28
|
+
export declare function installDependencies({ root, args }: {
|
|
29
|
+
root: string;
|
|
30
|
+
args: string[];
|
|
31
|
+
}): Promise<number>;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
/**
|
|
5
|
+
* A frozen install fails when pnpm's peer setting differs from the one the lockfile was written
|
|
6
|
+
* with, and a fresh clone has no local workspace file to say which that was. The lockfile records
|
|
7
|
+
* it, so install with the lockfile's own value.
|
|
8
|
+
*/
|
|
9
|
+
export function lockfilePeerSetting(lockfile) {
|
|
10
|
+
const settings = /^settings:\n((?:[ \t]+.*\n?)+)/m.exec(lockfile)?.[1];
|
|
11
|
+
const value = settings ? /^[ \t]+autoInstallPeers:[ \t]*(true|false)[ \t]*$/m.exec(settings)?.[1] : undefined;
|
|
12
|
+
return value === undefined ? undefined : value === 'true';
|
|
13
|
+
}
|
|
14
|
+
const readText = (path) => {
|
|
15
|
+
try {
|
|
16
|
+
return readFileSync(path, 'utf8');
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* A fresh clone has no node_modules, and an artifact cannot build without them. Install where the
|
|
24
|
+
* artifact resolves its dependencies from: its own directory when it keeps its own workspace or
|
|
25
|
+
* lockfile, else the app root. A committed lockfile is installed exactly; one is never rewritten
|
|
26
|
+
* by a preview.
|
|
27
|
+
*/
|
|
28
|
+
export function decideInstall({ artifactDir, appRoot, exists = existsSync, read = readText }) {
|
|
29
|
+
const standalone = exists(join(artifactDir, 'pnpm-workspace.yaml')) || exists(join(artifactDir, 'pnpm-lock.yaml'));
|
|
30
|
+
const root = standalone ? artifactDir : appRoot;
|
|
31
|
+
if (!exists(join(root, 'package.json')))
|
|
32
|
+
return { install: false, reason: 'no-package' };
|
|
33
|
+
if (exists(join(root, 'node_modules')))
|
|
34
|
+
return { install: false, reason: 'installed' };
|
|
35
|
+
const lockfile = exists(join(root, 'pnpm-lock.yaml')) ? read(join(root, 'pnpm-lock.yaml')) : undefined;
|
|
36
|
+
if (lockfile === undefined)
|
|
37
|
+
return { install: true, root, args: ['install', '--ignore-scripts'] };
|
|
38
|
+
const peers = lockfilePeerSetting(lockfile);
|
|
39
|
+
return {
|
|
40
|
+
install: true,
|
|
41
|
+
root,
|
|
42
|
+
// Dependency build scripts are not needed to bundle a browser app and are not run for a preview.
|
|
43
|
+
args: ['install', '--frozen-lockfile', '--ignore-scripts', ...(peers === undefined ? [] : [`--config.auto-install-peers=${peers}`])],
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Progress goes to stderr so `--json` output stays a single document. */
|
|
47
|
+
export async function installDependencies({ root, args }) {
|
|
48
|
+
return new Promise((resolvePromise, reject) => {
|
|
49
|
+
const child = spawn('pnpm', args, { cwd: root, stdio: ['ignore', 2, 2], shell: false });
|
|
50
|
+
child.once('error', reject);
|
|
51
|
+
child.once('exit', (code) => resolvePromise(code ?? 1));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { StepOutcome } from './run.js';
|
|
2
|
+
/**
|
|
3
|
+
* Read one row's worth of a namespace through the data plane and say whether this Atlas serves it.
|
|
4
|
+
* Only a 2xx proves that. A 403 is the data plane answering this caller, so the namespace is
|
|
5
|
+
* served but this command cannot vouch for the app's own access. Anything else is a failure
|
|
6
|
+
* reported with what Atlas said: on a fork the usual cause is a database role the local login
|
|
7
|
+
* cannot assume, and a local Atlas names it in the message.
|
|
8
|
+
*/
|
|
9
|
+
export declare function interpretNamespaceProbe({ namespace, status, body }: {
|
|
10
|
+
namespace: string;
|
|
11
|
+
status: number;
|
|
12
|
+
body: unknown;
|
|
13
|
+
}): StepOutcome;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
function firstError(body) {
|
|
2
|
+
if (typeof body !== 'object' || body === null)
|
|
3
|
+
return {};
|
|
4
|
+
const errors = Reflect.get(body, 'errors');
|
|
5
|
+
const first = Array.isArray(errors) ? errors[0] : undefined;
|
|
6
|
+
if (typeof first !== 'object' || first === null)
|
|
7
|
+
return {};
|
|
8
|
+
const message = Reflect.get(first, 'message');
|
|
9
|
+
const extensions = Reflect.get(first, 'extensions');
|
|
10
|
+
const read = (key) => {
|
|
11
|
+
if (typeof extensions !== 'object' || extensions === null)
|
|
12
|
+
return undefined;
|
|
13
|
+
const value = Reflect.get(extensions, key);
|
|
14
|
+
return typeof value === 'string' ? value : undefined;
|
|
15
|
+
};
|
|
16
|
+
return {
|
|
17
|
+
...(typeof message === 'string' ? { message } : {}),
|
|
18
|
+
...(read('code') ? { code: read('code') } : {}),
|
|
19
|
+
...(read('incidentId') ? { incidentId: read('incidentId') } : {}),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Read one row's worth of a namespace through the data plane and say whether this Atlas serves it.
|
|
24
|
+
* Only a 2xx proves that. A 403 is the data plane answering this caller, so the namespace is
|
|
25
|
+
* served but this command cannot vouch for the app's own access. Anything else is a failure
|
|
26
|
+
* reported with what Atlas said: on a fork the usual cause is a database role the local login
|
|
27
|
+
* cannot assume, and a local Atlas names it in the message.
|
|
28
|
+
*/
|
|
29
|
+
export function interpretNamespaceProbe({ namespace, status, body }) {
|
|
30
|
+
if (status >= 200 && status < 300)
|
|
31
|
+
return { status: 'ready', detail: `namespace ${namespace} answers reads on this Atlas` };
|
|
32
|
+
const error = firstError(body);
|
|
33
|
+
if (status === 403) {
|
|
34
|
+
return {
|
|
35
|
+
status: 'ready',
|
|
36
|
+
detail: `namespace ${namespace} is served; your own read was refused (403), so the app's access is not verified here`,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (status === 404) {
|
|
40
|
+
return {
|
|
41
|
+
status: 'failed',
|
|
42
|
+
detail: `namespace ${namespace} is not on this Atlas, or you cannot see it. A fork has only what its parent had; apply it with seq-studio orm apply <dir> -e local, or rerun with --apply-orm.`,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const opaque = !error.message || error.message === 'internal error';
|
|
46
|
+
const said = opaque
|
|
47
|
+
? `Atlas gave no cause${error.incidentId ? ` (incident ${error.incidentId}; see the Atlas log)` : '; see the Atlas log'}`
|
|
48
|
+
: error.message;
|
|
49
|
+
return { status: 'failed', detail: `namespace ${namespace} answered HTTP ${status}: ${said}` };
|
|
50
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type AppManifest, type PrimitiveEntry } from '../app/manifest.js';
|
|
2
|
+
/** What a preview does with one primitive on a local Atlas. */
|
|
3
|
+
export type PreviewAction = 'check-namespace' | 'deploy-function' | 'publish-artifact' | 'apply-process';
|
|
4
|
+
export type PreviewStep = {
|
|
5
|
+
entry: PrimitiveEntry;
|
|
6
|
+
action: PreviewAction;
|
|
7
|
+
} | {
|
|
8
|
+
entry: PrimitiveEntry;
|
|
9
|
+
action: 'unsupported';
|
|
10
|
+
reason: string;
|
|
11
|
+
};
|
|
12
|
+
export interface PreviewPlan {
|
|
13
|
+
/** In dependency order: a primitive always follows everything it depends on. */
|
|
14
|
+
steps: PreviewStep[];
|
|
15
|
+
/** Declared in the manifest but not needed by what is being previewed. */
|
|
16
|
+
omitted: PrimitiveEntry[];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Choose what to stand up and in which order.
|
|
20
|
+
*
|
|
21
|
+
* With no selection, a preview is the app's artifacts plus everything they depend on. That is
|
|
22
|
+
* deliberately narrower than a deploy: a scheduled process or a function nothing on screen calls
|
|
23
|
+
* would run for real against whatever its credentials reach, so it is started only when named.
|
|
24
|
+
*/
|
|
25
|
+
export declare function planPreview({ manifest, only }: {
|
|
26
|
+
manifest: AppManifest;
|
|
27
|
+
only?: readonly string[];
|
|
28
|
+
}): PreviewPlan;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { dependencyClosure, dependencyOrder } from '../app/graph.js';
|
|
2
|
+
import { defaultDeployOrder } from '../app/manifest.js';
|
|
3
|
+
const ACTION_BY_KIND = {
|
|
4
|
+
orm: 'check-namespace',
|
|
5
|
+
function: 'deploy-function',
|
|
6
|
+
artifact: 'publish-artifact',
|
|
7
|
+
process: 'apply-process',
|
|
8
|
+
};
|
|
9
|
+
const UNSUPPORTED_REASON = {
|
|
10
|
+
workspace: 'a local Atlas has no workspace storage to preview against',
|
|
11
|
+
agent: 'agents are not part of an app preview; deploy them with seq-studio agent apply',
|
|
12
|
+
pipeline: 'pipelines run on the data platform, not on a local Atlas',
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Choose what to stand up and in which order.
|
|
16
|
+
*
|
|
17
|
+
* With no selection, a preview is the app's artifacts plus everything they depend on. That is
|
|
18
|
+
* deliberately narrower than a deploy: a scheduled process or a function nothing on screen calls
|
|
19
|
+
* would run for real against whatever its credentials reach, so it is started only when named.
|
|
20
|
+
*/
|
|
21
|
+
export function planPreview({ manifest, only }) {
|
|
22
|
+
const primitives = manifest.primitives;
|
|
23
|
+
const selected = only?.length
|
|
24
|
+
? [...only]
|
|
25
|
+
: primitives.filter((entry) => entry.kind === 'artifact').map((entry) => entry.id);
|
|
26
|
+
if (selected.length === 0) {
|
|
27
|
+
throw new Error('This app declares no artifact to preview. Name the primitives to stand up with --only <id>[,<id>...].');
|
|
28
|
+
}
|
|
29
|
+
const needed = dependencyClosure({ entries: primitives, selected });
|
|
30
|
+
const entries = primitives.filter((entry) => needed.has(entry.id));
|
|
31
|
+
const order = dependencyOrder({ entries, priority: defaultDeployOrder(primitives) });
|
|
32
|
+
const byId = new Map(primitives.map((entry) => [entry.id, entry]));
|
|
33
|
+
const steps = order.map((id) => {
|
|
34
|
+
const entry = byId.get(id);
|
|
35
|
+
const action = ACTION_BY_KIND[entry.kind];
|
|
36
|
+
return action
|
|
37
|
+
? { entry, action }
|
|
38
|
+
: { entry, action: 'unsupported', reason: UNSUPPORTED_REASON[entry.kind] ?? `${entry.kind} primitives cannot be previewed` };
|
|
39
|
+
});
|
|
40
|
+
return { steps, omitted: primitives.filter((entry) => !needed.has(entry.id)) };
|
|
41
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { PreviewPlan, PreviewStep } from './plan.js';
|
|
2
|
+
export type StepStatus = 'ready' | 'failed' | 'skipped' | 'unsupported';
|
|
3
|
+
export interface StepOutcome {
|
|
4
|
+
status: 'ready' | 'failed';
|
|
5
|
+
/** One line for the summary: what is now true, or why it is not. */
|
|
6
|
+
detail: string;
|
|
7
|
+
/** Where to open the result, for an artifact. */
|
|
8
|
+
url?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface StepResult {
|
|
11
|
+
id: string;
|
|
12
|
+
kind: string;
|
|
13
|
+
status: StepStatus;
|
|
14
|
+
detail: string;
|
|
15
|
+
url?: string;
|
|
16
|
+
}
|
|
17
|
+
export type StepRunner = (step: Extract<PreviewStep, {
|
|
18
|
+
action: Exclude<PreviewStep['action'], 'unsupported'>;
|
|
19
|
+
}>) => Promise<StepOutcome>;
|
|
20
|
+
/**
|
|
21
|
+
* Run the plan in order. A primitive whose dependency failed is skipped, not attempted: a UI
|
|
22
|
+
* published over a function that did not deploy would look like a working preview and is not one.
|
|
23
|
+
* A dependency this command cannot preview does not block its dependents, but stays in the result
|
|
24
|
+
* so the summary says what the preview is missing.
|
|
25
|
+
*/
|
|
26
|
+
export declare function runPreviewPlan({ plan, run }: {
|
|
27
|
+
plan: PreviewPlan;
|
|
28
|
+
run: StepRunner;
|
|
29
|
+
}): Promise<StepResult[]>;
|
|
30
|
+
/** One line of a possibly multi-line error: the first that reads as the error itself, not a runtime warning or a stack frame. */
|
|
31
|
+
export declare function headline(message: string): string;
|
|
32
|
+
export declare function previewSucceeded(results: readonly StepResult[]): boolean;
|