@phnx-labs/artifacts-cli 0.4.7 → 0.6.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/CHANGELOG.md +135 -1
- package/README.md +161 -10
- package/dist/cli.d.ts +1 -6
- package/dist/cli.js +100 -88
- package/dist/cli.js.map +1 -1
- package/dist/commands/auth.d.ts +5 -0
- package/dist/commands/auth.js +112 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/favorite.d.ts +9 -0
- package/dist/commands/favorite.js +59 -0
- package/dist/commands/favorite.js.map +1 -0
- package/dist/commands/share.d.ts +297 -14
- package/dist/commands/share.js +1351 -71
- package/dist/commands/share.js.map +1 -1
- package/dist/lib/browser-detect.d.ts +28 -0
- package/dist/lib/browser-detect.js +126 -0
- package/dist/lib/browser-detect.js.map +1 -0
- package/dist/lib/components.d.ts +23 -0
- package/dist/lib/components.js +44 -0
- package/dist/lib/components.js.map +1 -0
- package/dist/lib/config.d.ts +17 -0
- package/dist/lib/config.js +8 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/embedded-html.d.ts +1 -1
- package/dist/lib/embedded-html.js +14 -0
- package/dist/lib/embedded-html.js.map +1 -1
- package/dist/lib/excerpt.d.ts +48 -0
- package/dist/lib/excerpt.js +167 -0
- package/dist/lib/excerpt.js.map +1 -0
- package/dist/lib/explorer.d.ts +37 -0
- package/dist/lib/explorer.js +166 -0
- package/dist/lib/explorer.js.map +1 -0
- package/dist/lib/favorites.d.ts +73 -0
- package/dist/lib/favorites.js +180 -0
- package/dist/lib/favorites.js.map +1 -0
- package/dist/lib/format.d.ts +2 -0
- package/dist/lib/format.js +14 -0
- package/dist/lib/format.js.map +1 -0
- package/dist/lib/github.d.ts +14 -0
- package/dist/lib/github.js +36 -0
- package/dist/lib/github.js.map +1 -0
- package/dist/lib/header-actions.d.ts +44 -0
- package/dist/lib/header-actions.js +181 -0
- package/dist/lib/header-actions.js.map +1 -0
- package/dist/lib/identity/client.d.ts +49 -0
- package/dist/lib/identity/client.js +97 -0
- package/dist/lib/identity/client.js.map +1 -0
- package/dist/lib/identity/index.d.ts +60 -0
- package/dist/lib/identity/index.js +69 -0
- package/dist/lib/identity/index.js.map +1 -0
- package/dist/lib/link-previews.d.ts +67 -0
- package/dist/lib/link-previews.js +427 -0
- package/dist/lib/link-previews.js.map +1 -0
- package/dist/lib/links.d.ts +2 -0
- package/dist/lib/links.js +21 -2
- package/dist/lib/links.js.map +1 -1
- package/dist/lib/math.d.ts +31 -0
- package/dist/lib/math.js +122 -0
- package/dist/lib/math.js.map +1 -0
- package/dist/lib/metadata-menu.d.ts +15 -0
- package/dist/lib/metadata-menu.js +23 -0
- package/dist/lib/metadata-menu.js.map +1 -1
- package/dist/lib/open.d.ts +6 -0
- package/dist/lib/open.js +54 -0
- package/dist/lib/open.js.map +1 -0
- package/dist/lib/render.d.ts +5 -0
- package/dist/lib/render.js +166 -35
- package/dist/lib/render.js.map +1 -1
- package/dist/lib/revisions.d.ts +17 -0
- package/dist/lib/revisions.js +6 -1
- package/dist/lib/revisions.js.map +1 -1
- package/dist/lib/share/analytics.d.ts +13 -0
- package/dist/lib/share/analytics.js +46 -0
- package/dist/lib/share/analytics.js.map +1 -0
- package/dist/lib/share/backend.d.ts +105 -0
- package/dist/lib/share/backend.js +161 -0
- package/dist/lib/share/backend.js.map +1 -0
- package/dist/lib/share/capture.d.ts +31 -0
- package/dist/lib/share/capture.js +175 -0
- package/dist/lib/share/capture.js.map +1 -0
- package/dist/lib/share/config.d.ts +82 -0
- package/dist/lib/share/config.js +181 -0
- package/dist/lib/share/config.js.map +1 -0
- package/dist/lib/share/delete.d.ts +122 -0
- package/dist/lib/share/delete.js +174 -0
- package/dist/lib/share/delete.js.map +1 -0
- package/dist/lib/share/html.d.ts +22 -0
- package/dist/lib/share/html.js +89 -0
- package/dist/lib/share/html.js.map +1 -0
- package/dist/lib/share/http-error.d.ts +52 -0
- package/dist/lib/share/http-error.js +66 -0
- package/dist/lib/share/http-error.js.map +1 -0
- package/dist/lib/share/og.d.ts +25 -0
- package/dist/lib/share/og.js +85 -0
- package/dist/lib/share/og.js.map +1 -0
- package/dist/lib/share/provision.d.ts +99 -0
- package/dist/lib/share/provision.js +271 -0
- package/dist/lib/share/provision.js.map +1 -0
- package/dist/lib/share/publish.d.ts +393 -0
- package/dist/lib/share/publish.js +829 -0
- package/dist/lib/share/publish.js.map +1 -0
- package/dist/lib/share/qr.d.ts +36 -0
- package/dist/lib/share/qr.js +102 -0
- package/dist/lib/share/qr.js.map +1 -0
- package/dist/lib/share/storage-policy.d.ts +9 -0
- package/dist/lib/share/storage-policy.js +43 -0
- package/dist/lib/share/storage-policy.js.map +1 -0
- package/dist/lib/share/visibility.d.ts +92 -0
- package/dist/lib/share/visibility.js +108 -0
- package/dist/lib/share/visibility.js.map +1 -0
- package/dist/lib/share/worker-template.d.ts +29 -0
- package/dist/lib/share/worker-template.js +2154 -0
- package/dist/lib/share/worker-template.js.map +1 -0
- package/dist/lib/status-list.d.ts +49 -0
- package/dist/lib/status-list.js +153 -0
- package/dist/lib/status-list.js.map +1 -0
- package/dist/lib/validate.js +128 -9
- package/dist/lib/validate.js.map +1 -1
- package/package.json +12 -3
- package/dist/lib/share-provision.d.ts +0 -31
- package/dist/lib/share-provision.js +0 -83
- package/dist/lib/share-provision.js.map +0 -1
- package/dist/lib/share-worker.d.ts +0 -1
- package/dist/lib/share-worker.js +0 -66
- package/dist/lib/share-worker.js.map +0 -1
- package/dist/lib/share.d.ts +0 -39
- package/dist/lib/share.js +0 -159
- package/dist/lib/share.js.map +0 -1
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ShareBackend — the token-source seam for `artifacts share`.
|
|
3
|
+
*
|
|
4
|
+
* Two legitimate principals, picked once at publish time:
|
|
5
|
+
* - **managed**: the caller is signed in to Phoenix (`readSession()`), no
|
|
6
|
+
* explicit BYO override. Token is the Phoenix access_token; endpoint is
|
|
7
|
+
* the already-live `share.getrush.ai`. Zero Cloudflare setup.
|
|
8
|
+
* - **byo**: the existing static WRITE_TOKEN path (`readShareConfig()` +
|
|
9
|
+
* `readWriteToken()`). Unchanged for power users / custom domains / fleet
|
|
10
|
+
* injection of `SHARE_WRITE_TOKEN`.
|
|
11
|
+
*
|
|
12
|
+
* Dispatch is not a fallback: signed-in AND no BYO override → managed;
|
|
13
|
+
* otherwise BYO. Fail loud when neither principal can authenticate.
|
|
14
|
+
*
|
|
15
|
+
* Explicit BYO override (any one is enough):
|
|
16
|
+
* - `opts.byo === true`
|
|
17
|
+
* - `opts.writeToken` (a caller-supplied static token, including tests)
|
|
18
|
+
* - `ARTIFACTS_SHARE_BACKEND=byo`
|
|
19
|
+
*/
|
|
20
|
+
import { PHOENIX_ID_BASE, readSession } from '../identity/client.js';
|
|
21
|
+
import { DEFAULT_SHARE_DOMAIN, LEGACY_MANAGED_SHARE_DOMAINS, readShareConfig, readWriteToken, readWriteTokenEnv, } from './config.js';
|
|
22
|
+
/** Env var that forces the BYO path. Value must be exactly `byo`. */
|
|
23
|
+
export const SHARE_BACKEND_ENV = 'ARTIFACTS_SHARE_BACKEND';
|
|
24
|
+
/** URL-safe namespace: lowercase `[a-z0-9-]+`. Matches the Worker's sanitize.
|
|
25
|
+
*
|
|
26
|
+
* This is URL-safety, not collision-resistance: two values that differ only
|
|
27
|
+
* in case or punctuation share a prefix. */
|
|
28
|
+
export function sanitizeShareNamespace(s) {
|
|
29
|
+
return s.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Public managed-share handle from a verified email.
|
|
33
|
+
*
|
|
34
|
+
* `muqsitnawaz@gmail.com` → `muqsitnawaz`. Plus-tags are dropped
|
|
35
|
+
* (`muqsitnawaz+dev@gmail.com` → `muqsitnawaz`) so the URL stays the
|
|
36
|
+
* account's default name. Empty / missing email → `''` (caller falls
|
|
37
|
+
* back to userId). Must match the Worker `handleFromEmail`.
|
|
38
|
+
*/
|
|
39
|
+
export function handleFromEmail(email) {
|
|
40
|
+
if (!email)
|
|
41
|
+
return '';
|
|
42
|
+
const local = email.split('@')[0] ?? '';
|
|
43
|
+
const beforePlus = local.split('+')[0] ?? local;
|
|
44
|
+
return sanitizeShareNamespace(beforePlus);
|
|
45
|
+
}
|
|
46
|
+
/** Managed URL namespace: email handle, else sanitized userId (never empty if either is set). */
|
|
47
|
+
export function managedShareHandle(session) {
|
|
48
|
+
return handleFromEmail(session.email) || sanitizeShareNamespace(session.userId ?? '');
|
|
49
|
+
}
|
|
50
|
+
export function managedShareBaseUrl() {
|
|
51
|
+
return `https://${DEFAULT_SHARE_DOMAIN}`;
|
|
52
|
+
}
|
|
53
|
+
function shareEndpointHost(value) {
|
|
54
|
+
return value.replace(/^https?:\/\//i, '').replace(/\/+$/, '').toLowerCase();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* True when this config is the platform managed endpoint (`share.getrush.ai`, or a
|
|
58
|
+
* legacy alias — `share.agents-cli.sh` / `artifacts.prix.dev` — still bound to the
|
|
59
|
+
* same Worker). The
|
|
60
|
+
* deploy path uses this — not "the caller is signed in" — to decide whether to
|
|
61
|
+
* bind `PHOENIX_ID_BASE` on the Worker.
|
|
62
|
+
*/
|
|
63
|
+
export function isManagedShareEndpoint(cfg) {
|
|
64
|
+
const managedHosts = new Set([DEFAULT_SHARE_DOMAIN, ...LEGACY_MANAGED_SHARE_DOMAINS].map((h) => h.toLowerCase()));
|
|
65
|
+
if (cfg.domain && managedHosts.has(shareEndpointHost(cfg.domain)))
|
|
66
|
+
return true;
|
|
67
|
+
if (cfg.baseUrl && managedHosts.has(shareEndpointHost(cfg.baseUrl)))
|
|
68
|
+
return true;
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Phoenix ID URL to bind on a managed Worker deploy. `undefined` for a
|
|
73
|
+
* pure-BYO Worker (WRITE_TOKEN only). Fail loud if this is a managed deploy
|
|
74
|
+
* but the base is empty — never skip-set a blank secret.
|
|
75
|
+
*/
|
|
76
|
+
export function phoenixIdBaseForDeploy(opts = {}, cfg) {
|
|
77
|
+
const managed = opts.managed === true || (cfg != null && isManagedShareEndpoint(cfg));
|
|
78
|
+
if (!managed)
|
|
79
|
+
return undefined;
|
|
80
|
+
const base = PHOENIX_ID_BASE.replace(/\/+$/, '').trim();
|
|
81
|
+
if (!base) {
|
|
82
|
+
throw new Error('Managed share deploy requires PHOENIX_ID_BASE so the Worker can verify Phoenix bearers. Set the PHOENIX_ID_BASE env var.');
|
|
83
|
+
}
|
|
84
|
+
return base;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The BYO-override signals: an explicit `--byo`, a caller-supplied static write
|
|
88
|
+
* token, or `ARTIFACTS_SHARE_BACKEND=byo`. A persisted BYO endpoint config is
|
|
89
|
+
* deliberately NOT an override — a signed-in user with a stale BYO config still
|
|
90
|
+
* publishes to managed unless they opt out explicitly (the managed-first
|
|
91
|
+
* contract).
|
|
92
|
+
*/
|
|
93
|
+
function shareByoOverride(opts) {
|
|
94
|
+
if (opts.byo === true)
|
|
95
|
+
return true;
|
|
96
|
+
if (opts.writeToken)
|
|
97
|
+
return true;
|
|
98
|
+
return (process.env[SHARE_BACKEND_ENV] ?? '').trim().toLowerCase() === 'byo';
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Pick the principal. Signed in (`readSession() != null`) AND no explicit BYO
|
|
102
|
+
* override → managed; otherwise BYO.
|
|
103
|
+
*
|
|
104
|
+
* This is not a fallback chain — it is a single decision. A caller that can
|
|
105
|
+
* authenticate NEITHER principal (signed out AND no BYO config) resolves `byo`
|
|
106
|
+
* here and fails loud in {@link resolveByoBackend}, where the actionable
|
|
107
|
+
* "log in or set up your bucket" message belongs.
|
|
108
|
+
*/
|
|
109
|
+
export function shouldUseManaged(opts = {}) {
|
|
110
|
+
if (shareByoOverride(opts))
|
|
111
|
+
return false;
|
|
112
|
+
const session = opts.session === undefined ? readSession() : opts.session;
|
|
113
|
+
return session != null;
|
|
114
|
+
}
|
|
115
|
+
/** The resolved principal, as a kind. Thin sugar over {@link shouldUseManaged}. */
|
|
116
|
+
export function shareBackendKind(opts = {}) {
|
|
117
|
+
return shouldUseManaged(opts) ? 'managed' : 'byo';
|
|
118
|
+
}
|
|
119
|
+
export function resolveShareBackend(opts = {}) {
|
|
120
|
+
if (shouldUseManaged(opts)) {
|
|
121
|
+
const session = opts.session === undefined ? readSession() : opts.session;
|
|
122
|
+
if (!session) {
|
|
123
|
+
throw new Error("Not signed in. Run 'artifacts auth login'.");
|
|
124
|
+
}
|
|
125
|
+
return resolveManagedBackend(session);
|
|
126
|
+
}
|
|
127
|
+
return resolveByoBackend(opts);
|
|
128
|
+
}
|
|
129
|
+
function resolveManagedBackend(session) {
|
|
130
|
+
if (!session.access_token) {
|
|
131
|
+
throw new Error("Not signed in. Run 'artifacts auth login'.");
|
|
132
|
+
}
|
|
133
|
+
const namespace = managedShareHandle(session);
|
|
134
|
+
if (!namespace) {
|
|
135
|
+
throw new Error("Signed in but the session has no email or user id. Run 'artifacts auth login' again.");
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
kind: 'managed',
|
|
139
|
+
baseUrl: managedShareBaseUrl(),
|
|
140
|
+
token: session.access_token,
|
|
141
|
+
namespace,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function resolveByoBackend(opts) {
|
|
145
|
+
const cfg = opts.config ?? readShareConfig();
|
|
146
|
+
if (!cfg) {
|
|
147
|
+
throw new Error("Not set up yet. Run 'artifacts auth login' to publish on the managed endpoint (zero Cloudflare setup), or 'artifacts share setup' / 'artifacts share join' for your own Cloudflare.");
|
|
148
|
+
}
|
|
149
|
+
const token = opts.writeToken ??
|
|
150
|
+
(opts.requireToken === false ? (readWriteTokenEnv() ?? '') : readWriteToken());
|
|
151
|
+
// Namespace is the GitHub username. An explicit --github-user is sanitized
|
|
152
|
+
// here; otherwise publish fills it via resolveShareUsername (gh/git config).
|
|
153
|
+
const namespace = opts.githubUser ? sanitizeShareNamespace(opts.githubUser) : '';
|
|
154
|
+
return {
|
|
155
|
+
kind: 'byo',
|
|
156
|
+
baseUrl: cfg.baseUrl.replace(/\/+$/, ''),
|
|
157
|
+
token,
|
|
158
|
+
namespace,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=backend.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../../src/lib/share/backend.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,eAAe,EAAE,WAAW,EAAuB,MAAM,uBAAuB,CAAC;AAE1F,OAAO,EACL,oBAAoB,EACpB,4BAA4B,EAC5B,eAAe,EACf,cAAc,EACd,iBAAiB,GAElB,MAAM,aAAa,CAAC;AAkCrB,qEAAqE;AACrE,MAAM,CAAC,MAAM,iBAAiB,GAAG,yBAAyB,CAAC;AAE3D;;;4CAG4C;AAC5C,MAAM,UAAU,sBAAsB,CAAC,CAAS;IAC9C,OAAO,CAAC,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;AAC7E,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,eAAe,CAAC,KAAyB;IACvD,IAAI,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IACtB,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IACxC,MAAM,UAAU,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC;IAChD,OAAO,sBAAsB,CAAC,UAAU,CAAC,CAAC;AAC5C,CAAC;AAED,iGAAiG;AACjG,MAAM,UAAU,kBAA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QACL,SAAS;KACV,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render an HTML file to a 1200×630 PNG — the Open Graph cover for a shared plan.
|
|
3
|
+
*
|
|
4
|
+
* When `artifacts share plan.html` runs, we screenshot the plan's own hero and use it
|
|
5
|
+
* as the `og:image`, so the link unfurls into a card in Slack / iMessage / Twitter /
|
|
6
|
+
* Discord. No AI, no central render service: it's a headless screenshot on the
|
|
7
|
+
* publisher's machine, so it works identically for us and for any user, and costs
|
|
8
|
+
* nothing.
|
|
9
|
+
*
|
|
10
|
+
* Browser resolution uses this repo's detector (`findFirstInstalledBrowser`), then
|
|
11
|
+
* falls back to a managed
|
|
12
|
+
* Chromium in the Playwright/Puppeteer caches (present on machines that have done
|
|
13
|
+
* browser automation), then `PUPPETEER_EXECUTABLE_PATH`. If nothing headless-capable
|
|
14
|
+
* is found, `captureCover` returns null and publishing proceeds without a cover —
|
|
15
|
+
* the link still works, it just won't have a preview image.
|
|
16
|
+
*/
|
|
17
|
+
/** OG standard card size; captured at OG_SCALE× for retina crispness. */
|
|
18
|
+
export declare const OG_WIDTH = 1200;
|
|
19
|
+
export declare const OG_HEIGHT = 630;
|
|
20
|
+
/** Device scale factor for the capture — the served PNG is OG_WIDTH*OG_SCALE × OG_HEIGHT*OG_SCALE. */
|
|
21
|
+
export declare const OG_SCALE = 2;
|
|
22
|
+
/** Ordered list of candidate Chromium-family binaries to try for headless capture. */
|
|
23
|
+
export declare function candidateBrowsers(): string[];
|
|
24
|
+
/** Newest-first Chromium binaries under the Playwright / Puppeteer download caches. */
|
|
25
|
+
export declare function scanCaches(home?: string, platform?: NodeJS.Platform): string[];
|
|
26
|
+
/**
|
|
27
|
+
* Screenshot `htmlPath`'s top 1200×630 (its hero) to a PNG buffer, or null if no
|
|
28
|
+
* headless-capable browser is available or every candidate fails. Never throws —
|
|
29
|
+
* a cover is a nice-to-have, never a reason to fail a publish.
|
|
30
|
+
*/
|
|
31
|
+
export declare function captureCover(htmlPath: string, timeoutMs?: number): Promise<Buffer | null>;
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Render an HTML file to a 1200×630 PNG — the Open Graph cover for a shared plan.
|
|
3
|
+
*
|
|
4
|
+
* When `artifacts share plan.html` runs, we screenshot the plan's own hero and use it
|
|
5
|
+
* as the `og:image`, so the link unfurls into a card in Slack / iMessage / Twitter /
|
|
6
|
+
* Discord. No AI, no central render service: it's a headless screenshot on the
|
|
7
|
+
* publisher's machine, so it works identically for us and for any user, and costs
|
|
8
|
+
* nothing.
|
|
9
|
+
*
|
|
10
|
+
* Browser resolution uses this repo's detector (`findFirstInstalledBrowser`), then
|
|
11
|
+
* falls back to a managed
|
|
12
|
+
* Chromium in the Playwright/Puppeteer caches (present on machines that have done
|
|
13
|
+
* browser automation), then `PUPPETEER_EXECUTABLE_PATH`. If nothing headless-capable
|
|
14
|
+
* is found, `captureCover` returns null and publishing proceeds without a cover —
|
|
15
|
+
* the link still works, it just won't have a preview image.
|
|
16
|
+
*/
|
|
17
|
+
import { execFile } from 'node:child_process';
|
|
18
|
+
import fs from 'node:fs';
|
|
19
|
+
import os from 'node:os';
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
import { findFirstInstalledBrowser } from '../browser-detect.js';
|
|
22
|
+
/** OG standard card size; captured at OG_SCALE× for retina crispness. */
|
|
23
|
+
export const OG_WIDTH = 1200;
|
|
24
|
+
export const OG_HEIGHT = 630;
|
|
25
|
+
/** Device scale factor for the capture — the served PNG is OG_WIDTH*OG_SCALE × OG_HEIGHT*OG_SCALE. */
|
|
26
|
+
export const OG_SCALE = 2;
|
|
27
|
+
// Installed browsers that are poor `--headless` hosts (they hang instead of
|
|
28
|
+
// capturing). We still let a managed Chromium or an explicit override handle those
|
|
29
|
+
// machines; we just don't burn a timeout probing these. (Only members of the
|
|
30
|
+
// browser detector's `BrowserType` are meaningful here; Comet is the real case.)
|
|
31
|
+
const BAD_HEADLESS_TYPES = new Set(['comet']);
|
|
32
|
+
/** Ordered list of candidate Chromium-family binaries to try for headless capture. */
|
|
33
|
+
export function candidateBrowsers() {
|
|
34
|
+
const out = [];
|
|
35
|
+
const push = (p) => {
|
|
36
|
+
if (p && fs.existsSync(p) && !out.includes(p))
|
|
37
|
+
out.push(p);
|
|
38
|
+
};
|
|
39
|
+
// 1) Explicit overrides are contracts, not hints. A typo must fail at the
|
|
40
|
+
// boundary instead of silently falling through to an unrelated browser.
|
|
41
|
+
for (const name of ['PUPPETEER_EXECUTABLE_PATH', 'ARTIFACTS_SHARE_BROWSER', 'AGENTS_SHARE_BROWSER']) {
|
|
42
|
+
const value = process.env[name]?.trim();
|
|
43
|
+
if (!value)
|
|
44
|
+
continue;
|
|
45
|
+
if (!fs.existsSync(value)) {
|
|
46
|
+
throw new Error(`${name} points to a browser that does not exist: ${value}`);
|
|
47
|
+
}
|
|
48
|
+
push(value);
|
|
49
|
+
}
|
|
50
|
+
// 2) Managed Chromium in the Playwright / Puppeteer caches — purpose-built for
|
|
51
|
+
// headless, so it's the most reliable capture host when present.
|
|
52
|
+
for (const bin of scanCaches())
|
|
53
|
+
push(bin);
|
|
54
|
+
// 3) The repo's own auto-pick (Chrome → Brave → Edge → Chromium by platform),
|
|
55
|
+
// skipping browsers known to be poor headless hosts so we don't eat a timeout.
|
|
56
|
+
try {
|
|
57
|
+
const pick = findFirstInstalledBrowser();
|
|
58
|
+
if (pick && !BAD_HEADLESS_TYPES.has(pick.browserType))
|
|
59
|
+
push(pick.binary);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// detector is best-effort
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/** Newest-first Chromium binaries under the Playwright / Puppeteer download caches. */
|
|
67
|
+
export function scanCaches(home = os.homedir(), platform = os.platform()) {
|
|
68
|
+
const roots = platform === 'darwin'
|
|
69
|
+
? [
|
|
70
|
+
path.join(home, 'Library/Caches/ms-playwright'),
|
|
71
|
+
path.join(home, '.cache/ms-playwright'),
|
|
72
|
+
path.join(home, '.cache/puppeteer/chrome'),
|
|
73
|
+
]
|
|
74
|
+
: [path.join(home, '.cache/ms-playwright'), path.join(home, '.cache/puppeteer/chrome')];
|
|
75
|
+
const rel = platform === 'darwin'
|
|
76
|
+
? [
|
|
77
|
+
'chrome-mac/Chromium.app/Contents/MacOS/Chromium',
|
|
78
|
+
'chrome-mac-arm64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing',
|
|
79
|
+
'chrome-mac-x64/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing',
|
|
80
|
+
// Playwright's newer headless-shell packages (chromium_headless_shell-*) —
|
|
81
|
+
// a raw binary, not an .app bundle.
|
|
82
|
+
'chrome-headless-shell-mac-arm64/chrome-headless-shell',
|
|
83
|
+
'chrome-headless-shell-mac-x64/chrome-headless-shell',
|
|
84
|
+
]
|
|
85
|
+
: [
|
|
86
|
+
'chrome-linux/chrome',
|
|
87
|
+
'chrome-linux64/chrome',
|
|
88
|
+
'chrome-headless-shell-linux64/chrome-headless-shell',
|
|
89
|
+
];
|
|
90
|
+
const found = [];
|
|
91
|
+
for (const root of roots) {
|
|
92
|
+
let entries;
|
|
93
|
+
try {
|
|
94
|
+
entries = fs.readdirSync(root).sort().reverse(); // newest version dir first
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
for (const e of entries) {
|
|
100
|
+
for (const r of rel) {
|
|
101
|
+
const p = path.join(root, e, r);
|
|
102
|
+
if (fs.existsSync(p))
|
|
103
|
+
found.push(p);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return found;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Screenshot `htmlPath`'s top 1200×630 (its hero) to a PNG buffer, or null if no
|
|
111
|
+
* headless-capable browser is available or every candidate fails. Never throws —
|
|
112
|
+
* a cover is a nice-to-have, never a reason to fail a publish.
|
|
113
|
+
*/
|
|
114
|
+
export async function captureCover(htmlPath, timeoutMs = 15_000) {
|
|
115
|
+
const abs = path.resolve(htmlPath);
|
|
116
|
+
if (!fs.existsSync(abs))
|
|
117
|
+
return null;
|
|
118
|
+
const fileUrl = `file://${abs.split('/').map(encodeURIComponent).join('/')}`;
|
|
119
|
+
const candidates = candidateBrowsers();
|
|
120
|
+
if (candidates.length === 0) {
|
|
121
|
+
// A silently-dropped cover reads as "the CLI decided this page needs none",
|
|
122
|
+
// when in fact no headless browser was found. Say so and point at the escape
|
|
123
|
+
// hatch instead of leaving the publish coverless with no explanation.
|
|
124
|
+
process.stderr.write('[artifacts share] no headless browser found for the OG cover — install Chrome/Chromium ' +
|
|
125
|
+
'or set ARTIFACTS_SHARE_BROWSER=/path/to/chrome. Publishing without a preview image.\n');
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
let lastFailure = '';
|
|
129
|
+
for (const bin of candidates) {
|
|
130
|
+
const outPng = path.join(os.tmpdir(), `artifacts-share-cover-${process.pid}-${Date.now()}.png`);
|
|
131
|
+
const userDir = fs.mkdtempSync(path.join(os.tmpdir(), 'artifacts-share-chrome-'));
|
|
132
|
+
try {
|
|
133
|
+
await new Promise((resolve, reject) => {
|
|
134
|
+
execFile(bin, [
|
|
135
|
+
'--headless=new',
|
|
136
|
+
'--disable-gpu',
|
|
137
|
+
'--hide-scrollbars',
|
|
138
|
+
'--no-first-run',
|
|
139
|
+
'--no-default-browser-check',
|
|
140
|
+
`--force-device-scale-factor=${OG_SCALE}`,
|
|
141
|
+
// Bound page JS (count-up animations etc.) so the shot fires promptly
|
|
142
|
+
// instead of waiting on long-running timers.
|
|
143
|
+
'--virtual-time-budget=8000',
|
|
144
|
+
`--window-size=${OG_WIDTH},${OG_HEIGHT}`,
|
|
145
|
+
`--user-data-dir=${userDir}`,
|
|
146
|
+
`--screenshot=${outPng}`,
|
|
147
|
+
fileUrl,
|
|
148
|
+
], { timeout: timeoutMs }, (err) => (err ? reject(err) : resolve()));
|
|
149
|
+
});
|
|
150
|
+
if (fs.existsSync(outPng)) {
|
|
151
|
+
const buf = fs.readFileSync(outPng);
|
|
152
|
+
// A valid PNG starts with the 8-byte signature; guard against 0-byte writes.
|
|
153
|
+
if (buf.length > 8 && buf[0] === 0x89 && buf[1] === 0x50)
|
|
154
|
+
return buf;
|
|
155
|
+
lastFailure = `${path.basename(bin)}: produced no valid PNG`;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
lastFailure = `${path.basename(bin)}: no screenshot written`;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
lastFailure = `${path.basename(bin)}: ${err.message}`;
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
fs.rmSync(outPng, { force: true });
|
|
166
|
+
fs.rmSync(userDir, { recursive: true, force: true });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// Every candidate ran but none yielded a cover — surface the last reason so a
|
|
170
|
+
// missing preview card is diagnosable (timeout, crash, bad binary) rather than silent.
|
|
171
|
+
process.stderr.write(`[artifacts share] OG cover capture failed (${lastFailure || 'unknown error'}) — ` +
|
|
172
|
+
'publishing without a preview image. Set ARTIFACTS_SHARE_BROWSER to override.\n');
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=capture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"capture.js","sourceRoot":"","sources":["../../../src/lib/share/capture.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAEjE,yEAAyE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AC,OAAO,GAAG,EAAE,CAAC;YACb,WAAW,GAAG,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAM,GAAa,CAAC,OAAO,EAAE,CAAC;QACnE,CAAC;gBAAS,CAAC;YACT,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;YACnC,EAAE,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,CAAC;IACH,CAAC;IACD,8EAA8E;IAC9E,uFAAuF;IACvF,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,8CAA8C,WAAW,IAAI,eAAe,MAAM;QAChF,gFAAgF,CACnF,CAAC;IACF,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { UnlimitedStorage } from './storage-policy.js';
|
|
2
|
+
export interface ShareConfig {
|
|
3
|
+
/** Public base, e.g. `https://share.getrush.ai` or `https://artifacts-share.<acct>.workers.dev`. */
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
accountId: string;
|
|
6
|
+
workerName: string;
|
|
7
|
+
bucketName: string;
|
|
8
|
+
/** Custom domain when mapped (e.g. `share.getrush.ai`). */
|
|
9
|
+
domain?: string;
|
|
10
|
+
/** Cloudflare Web Analytics token injected into published HTML pages. */
|
|
11
|
+
analyticsToken?: string;
|
|
12
|
+
unlimitedStorage?: UnlimitedStorage;
|
|
13
|
+
/** sha256 of the Worker script deployed at the last provision/update. Absent
|
|
14
|
+
* on any config written before this field existed — treat that as "unknown",
|
|
15
|
+
* never as "outdated" (there is nothing to compare against). */
|
|
16
|
+
templateHash?: string;
|
|
17
|
+
}
|
|
18
|
+
/** Env vars a fleet runner may use to inject the BYO write token ephemerally.
|
|
19
|
+
* `ARTIFACTS_SHARE_TOKEN` is this CLI's own name; `SHARE_WRITE_TOKEN` is the
|
|
20
|
+
* name the wider fleet already injects, and is accepted so an existing runner
|
|
21
|
+
* keeps working after the relocation. */
|
|
22
|
+
export declare const SHARE_TOKEN_ENV_KEYS: readonly ["ARTIFACTS_SHARE_TOKEN", "SHARE_WRITE_TOKEN"];
|
|
23
|
+
export declare const DEFAULT_CF_BUNDLE = "cloudflare";
|
|
24
|
+
export declare const DEFAULT_WORKER_NAME = "agents-share";
|
|
25
|
+
export declare const DEFAULT_BUCKET_NAME = "agents-share";
|
|
26
|
+
export declare const DEFAULT_SHARE_DOMAIN = "share.getrush.ai";
|
|
27
|
+
export declare const LEGACY_MANAGED_SHARE_DOMAINS: readonly ["share.agents-cli.sh", "artifacts.prix.dev"];
|
|
28
|
+
/** The write token may be injected ephemerally into fleet/cloud agents. */
|
|
29
|
+
export declare function readWriteTokenEnv(env?: NodeJS.ProcessEnv): string | null;
|
|
30
|
+
/**
|
|
31
|
+
* Read the persisted endpoint config, or null if there is no `share.baseUrl`.
|
|
32
|
+
*
|
|
33
|
+
* Publish talks only to `baseUrl` with the write token. `accountId` / worker /
|
|
34
|
+
* bucket are provisioning metadata for `setup`/`update`. Requiring all four
|
|
35
|
+
* (and treating `accountId: ""` as missing) would make `status` and publish both
|
|
36
|
+
* report "not set up" on an endpoint that still works.
|
|
37
|
+
*/
|
|
38
|
+
export declare function readShareConfig(): ShareConfig | null;
|
|
39
|
+
/** Persist the endpoint config. Empty strings never overwrite a previously
|
|
40
|
+
* stored value and are not written back as `accountId: ""`. The stored write
|
|
41
|
+
* token is left untouched — {@link storeWriteToken} owns it. */
|
|
42
|
+
export declare function writeShareConfig(cfg: ShareConfig): void;
|
|
43
|
+
/** A fresh 32-byte hex write token. */
|
|
44
|
+
export declare function generateWriteToken(): string;
|
|
45
|
+
/** Persist the BYO write token next to the endpoint config. */
|
|
46
|
+
export declare function storeWriteToken(token: string): void;
|
|
47
|
+
/** Read the BYO write token from the persisted config. */
|
|
48
|
+
export declare function readWriteTokenFromConfig(): string;
|
|
49
|
+
/** Read the write token from injected env first, then the persisted config.
|
|
50
|
+
* Throws with an actionable message if absent. */
|
|
51
|
+
export declare function readWriteToken(): string;
|
|
52
|
+
/**
|
|
53
|
+
* Cloudflare API credentials for provisioning and `share update`.
|
|
54
|
+
*
|
|
55
|
+
* Order, and why: an explicit `--token`/`--account` is a contract and wins
|
|
56
|
+
* outright; otherwise the environment, which is exactly what a secrets manager
|
|
57
|
+
* injects (`agents secrets exec cloudflare -- artifacts share update`). When
|
|
58
|
+
* neither carries a token and a `--bundle` name was given, one bounded read
|
|
59
|
+
* through that manager is attempted so `artifacts share update --bundle
|
|
60
|
+
* cloudflare` works unwrapped too. If none of the three produce a token this
|
|
61
|
+
* fails loud naming all three — it never falls through to an unauthenticated
|
|
62
|
+
* Cloudflare call.
|
|
63
|
+
*/
|
|
64
|
+
export declare function readCloudflareCreds(bundle?: string, override?: {
|
|
65
|
+
apiToken?: string;
|
|
66
|
+
accountId?: string;
|
|
67
|
+
env?: NodeJS.ProcessEnv;
|
|
68
|
+
resolver?: BundleResolver;
|
|
69
|
+
}): {
|
|
70
|
+
apiToken: string;
|
|
71
|
+
accountId: string;
|
|
72
|
+
};
|
|
73
|
+
/** Resolve one variable out of a named secrets bundle. */
|
|
74
|
+
export type BundleResolver = (bundle: string, key: string) => string;
|
|
75
|
+
/**
|
|
76
|
+
* Read a single value from a named `agents secrets` bundle by running the
|
|
77
|
+
* documented injected-read (`agents secrets exec <bundle> -- printenv <KEY>`).
|
|
78
|
+
* Returns '' when the CLI is absent, the bundle has no such key, or the read
|
|
79
|
+
* takes longer than the timeout — the caller then fails loud with the full
|
|
80
|
+
* three-way message rather than a spawn error nobody can act on.
|
|
81
|
+
*/
|
|
82
|
+
export declare function readBundleValue(bundle: string, key: string): string;
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// Config + credential glue for `artifacts share`.
|
|
2
|
+
//
|
|
3
|
+
// - The endpoint config (base URL, account, worker/bucket names, last-deployed
|
|
4
|
+
// template hash) lives in `~/.artifacts/config.json` under `share:`, alongside
|
|
5
|
+
// everything else this CLI persists.
|
|
6
|
+
// - The BYO write token lives beside it, or in `ARTIFACTS_SHARE_TOKEN` /
|
|
7
|
+
// `SHARE_WRITE_TOKEN` when a fleet runner injects it ephemerally.
|
|
8
|
+
// - The Cloudflare API token (for provisioning and `share update`) is read from
|
|
9
|
+
// the environment, which is how a secrets manager hands it over — see
|
|
10
|
+
// {@link readCloudflareCreds}.
|
|
11
|
+
//
|
|
12
|
+
// The MANAGED path needs none of this: a Phoenix session (`artifacts auth login`)
|
|
13
|
+
// publishes to share.getrush.ai with no Cloudflare account at all.
|
|
14
|
+
import { randomBytes } from 'node:crypto';
|
|
15
|
+
import { execFileSync } from 'node:child_process';
|
|
16
|
+
import { readConfig, updateConfig } from '../config.js';
|
|
17
|
+
/** Env vars a fleet runner may use to inject the BYO write token ephemerally.
|
|
18
|
+
* `ARTIFACTS_SHARE_TOKEN` is this CLI's own name; `SHARE_WRITE_TOKEN` is the
|
|
19
|
+
* name the wider fleet already injects, and is accepted so an existing runner
|
|
20
|
+
* keeps working after the relocation. */
|
|
21
|
+
export const SHARE_TOKEN_ENV_KEYS = ['ARTIFACTS_SHARE_TOKEN', 'SHARE_WRITE_TOKEN'];
|
|
22
|
+
export const DEFAULT_CF_BUNDLE = 'cloudflare';
|
|
23
|
+
export const DEFAULT_WORKER_NAME = 'agents-share';
|
|
24
|
+
export const DEFAULT_BUCKET_NAME = 'agents-share';
|
|
25
|
+
// The product share endpoint: share.getrush.ai, bound to the `agents-share`
|
|
26
|
+
// Worker. Publishing prints this host.
|
|
27
|
+
export const DEFAULT_SHARE_DOMAIN = 'share.getrush.ai';
|
|
28
|
+
// Managed hosts still recognized as "the platform endpoint" for backward
|
|
29
|
+
// compatibility. Both stay attached to the same Worker, so every link already
|
|
30
|
+
// shared on them keeps resolving, and a persisted config pointing at one is
|
|
31
|
+
// still treated as managed (e.g. for deploy-time PHOENIX_ID_BASE binding).
|
|
32
|
+
// Compared lowercased.
|
|
33
|
+
export const LEGACY_MANAGED_SHARE_DOMAINS = ['share.agents-cli.sh', 'artifacts.prix.dev'];
|
|
34
|
+
/** The write token may be injected ephemerally into fleet/cloud agents. */
|
|
35
|
+
export function readWriteTokenEnv(env = process.env) {
|
|
36
|
+
for (const key of SHARE_TOKEN_ENV_KEYS) {
|
|
37
|
+
const token = env[key]?.trim();
|
|
38
|
+
if (token)
|
|
39
|
+
return token;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
/** Trim; empty / whitespace-only strings are absent. */
|
|
44
|
+
function nonempty(v) {
|
|
45
|
+
const t = v?.trim();
|
|
46
|
+
return t ? t : undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Read the persisted endpoint config, or null if there is no `share.baseUrl`.
|
|
50
|
+
*
|
|
51
|
+
* Publish talks only to `baseUrl` with the write token. `accountId` / worker /
|
|
52
|
+
* bucket are provisioning metadata for `setup`/`update`. Requiring all four
|
|
53
|
+
* (and treating `accountId: ""` as missing) would make `status` and publish both
|
|
54
|
+
* report "not set up" on an endpoint that still works.
|
|
55
|
+
*/
|
|
56
|
+
export function readShareConfig() {
|
|
57
|
+
const s = readConfig().share;
|
|
58
|
+
const baseUrl = nonempty(s?.baseUrl)?.replace(/\/+$/, '');
|
|
59
|
+
if (!baseUrl)
|
|
60
|
+
return null;
|
|
61
|
+
return {
|
|
62
|
+
baseUrl,
|
|
63
|
+
accountId: nonempty(s?.accountId) ?? '',
|
|
64
|
+
workerName: nonempty(s?.workerName) ?? DEFAULT_WORKER_NAME,
|
|
65
|
+
bucketName: nonempty(s?.bucketName) ?? DEFAULT_BUCKET_NAME,
|
|
66
|
+
domain: nonempty(s?.domain),
|
|
67
|
+
analyticsToken: nonempty(s?.analyticsToken),
|
|
68
|
+
unlimitedStorage: s?.unlimitedStorage,
|
|
69
|
+
templateHash: nonempty(s?.templateHash),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** Persist the endpoint config. Empty strings never overwrite a previously
|
|
73
|
+
* stored value and are not written back as `accountId: ""`. The stored write
|
|
74
|
+
* token is left untouched — {@link storeWriteToken} owns it. */
|
|
75
|
+
export function writeShareConfig(cfg) {
|
|
76
|
+
updateConfig((current) => {
|
|
77
|
+
const prev = current.share ?? {};
|
|
78
|
+
const next = { ...prev };
|
|
79
|
+
if (cfg.unlimitedStorage !== undefined)
|
|
80
|
+
next.unlimitedStorage = cfg.unlimitedStorage;
|
|
81
|
+
else if (cfg.baseUrl !== prev.baseUrl || cfg.accountId !== prev.accountId || cfg.workerName !== prev.workerName) {
|
|
82
|
+
delete next.unlimitedStorage;
|
|
83
|
+
}
|
|
84
|
+
const assign = (key, incoming) => {
|
|
85
|
+
const v = nonempty(incoming);
|
|
86
|
+
if (v)
|
|
87
|
+
next[key] = v;
|
|
88
|
+
};
|
|
89
|
+
assign('baseUrl', cfg.baseUrl);
|
|
90
|
+
assign('accountId', cfg.accountId);
|
|
91
|
+
assign('workerName', cfg.workerName);
|
|
92
|
+
assign('bucketName', cfg.bucketName);
|
|
93
|
+
assign('domain', cfg.domain);
|
|
94
|
+
assign('analyticsToken', cfg.analyticsToken);
|
|
95
|
+
assign('templateHash', cfg.templateHash);
|
|
96
|
+
for (const key of Object.keys(next)) {
|
|
97
|
+
if (typeof next[key] === 'string' && !nonempty(next[key]))
|
|
98
|
+
delete next[key];
|
|
99
|
+
}
|
|
100
|
+
return { ...current, share: next };
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/** A fresh 32-byte hex write token. */
|
|
104
|
+
export function generateWriteToken() {
|
|
105
|
+
return randomBytes(32).toString('hex');
|
|
106
|
+
}
|
|
107
|
+
/** Persist the BYO write token next to the endpoint config. */
|
|
108
|
+
export function storeWriteToken(token) {
|
|
109
|
+
updateConfig((current) => ({
|
|
110
|
+
...current,
|
|
111
|
+
share: { ...(current.share ?? {}), writeToken: token },
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
/** Read the BYO write token from the persisted config. */
|
|
115
|
+
export function readWriteTokenFromConfig() {
|
|
116
|
+
const token = nonempty(readConfig().share?.writeToken);
|
|
117
|
+
if (!token) {
|
|
118
|
+
throw new Error("No share write token stored. Run 'artifacts auth login' to publish on the managed endpoint " +
|
|
119
|
+
"(no Cloudflare setup), or 'artifacts share setup' / 'artifacts share join' for your own Cloudflare.");
|
|
120
|
+
}
|
|
121
|
+
return token;
|
|
122
|
+
}
|
|
123
|
+
/** Read the write token from injected env first, then the persisted config.
|
|
124
|
+
* Throws with an actionable message if absent. */
|
|
125
|
+
export function readWriteToken() {
|
|
126
|
+
return readWriteTokenEnv() ?? readWriteTokenFromConfig();
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Cloudflare API credentials for provisioning and `share update`.
|
|
130
|
+
*
|
|
131
|
+
* Order, and why: an explicit `--token`/`--account` is a contract and wins
|
|
132
|
+
* outright; otherwise the environment, which is exactly what a secrets manager
|
|
133
|
+
* injects (`agents secrets exec cloudflare -- artifacts share update`). When
|
|
134
|
+
* neither carries a token and a `--bundle` name was given, one bounded read
|
|
135
|
+
* through that manager is attempted so `artifacts share update --bundle
|
|
136
|
+
* cloudflare` works unwrapped too. If none of the three produce a token this
|
|
137
|
+
* fails loud naming all three — it never falls through to an unauthenticated
|
|
138
|
+
* Cloudflare call.
|
|
139
|
+
*/
|
|
140
|
+
export function readCloudflareCreds(bundle = DEFAULT_CF_BUNDLE, override = {}) {
|
|
141
|
+
if (override.apiToken) {
|
|
142
|
+
return { apiToken: override.apiToken, accountId: override.accountId ?? '' };
|
|
143
|
+
}
|
|
144
|
+
const env = override.env ?? process.env;
|
|
145
|
+
const fromEnv = (key) => env[key]?.trim() ?? '';
|
|
146
|
+
let apiToken = fromEnv('CLOUDFLARE_API_TOKEN') || fromEnv('CF_API_TOKEN');
|
|
147
|
+
let accountId = override.accountId?.trim() || fromEnv('CLOUDFLARE_ACCOUNT_ID') || fromEnv('CF_ACCOUNT_ID');
|
|
148
|
+
if (!apiToken && bundle) {
|
|
149
|
+
const resolver = override.resolver ?? readBundleValue;
|
|
150
|
+
apiToken = resolver(bundle, 'CLOUDFLARE_API_TOKEN') || resolver(bundle, 'CF_API_TOKEN');
|
|
151
|
+
if (!accountId) {
|
|
152
|
+
accountId = resolver(bundle, 'CLOUDFLARE_ACCOUNT_ID') || resolver(bundle, 'CF_ACCOUNT_ID');
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!apiToken) {
|
|
156
|
+
throw new Error(`No Cloudflare API token. Pass it directly with --token <t> [--account <id>], ` +
|
|
157
|
+
`set CLOUDFLARE_API_TOKEN in the environment, or run the command under your secrets manager: ` +
|
|
158
|
+
`agents secrets exec ${bundle} -- artifacts share update`);
|
|
159
|
+
}
|
|
160
|
+
return { apiToken, accountId };
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Read a single value from a named `agents secrets` bundle by running the
|
|
164
|
+
* documented injected-read (`agents secrets exec <bundle> -- printenv <KEY>`).
|
|
165
|
+
* Returns '' when the CLI is absent, the bundle has no such key, or the read
|
|
166
|
+
* takes longer than the timeout — the caller then fails loud with the full
|
|
167
|
+
* three-way message rather than a spawn error nobody can act on.
|
|
168
|
+
*/
|
|
169
|
+
export function readBundleValue(bundle, key) {
|
|
170
|
+
try {
|
|
171
|
+
return execFileSync('agents', ['secrets', 'exec', bundle, '--', 'printenv', key], {
|
|
172
|
+
encoding: 'utf8',
|
|
173
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
174
|
+
timeout: 20_000,
|
|
175
|
+
}).trim();
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
return '';
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
//# sourceMappingURL=config.js.map
|