@atelier201/cli 0.0.0-stage → 0.4.1
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/AGENTS.md +379 -0
- package/LICENSE +21 -0
- package/README.md +146 -2
- package/dist/api-types.generated.js +8 -0
- package/dist/api-types.generated.js.map +1 -0
- package/dist/commands/api.js +111 -0
- package/dist/commands/api.js.map +1 -0
- package/dist/commands/auth.js +284 -0
- package/dist/commands/auth.js.map +1 -0
- package/dist/commands/builds.js +169 -0
- package/dist/commands/builds.js.map +1 -0
- package/dist/commands/changesets.js +91 -0
- package/dist/commands/changesets.js.map +1 -0
- package/dist/commands/common.js +89 -0
- package/dist/commands/common.js.map +1 -0
- package/dist/commands/completion.js +120 -0
- package/dist/commands/completion.js.map +1 -0
- package/dist/commands/content.js +281 -0
- package/dist/commands/content.js.map +1 -0
- package/dist/commands/doctor.js +141 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/domains.js +109 -0
- package/dist/commands/domains.js.map +1 -0
- package/dist/commands/feedback.js +39 -0
- package/dist/commands/feedback.js.map +1 -0
- package/dist/commands/files.js +286 -0
- package/dist/commands/files.js.map +1 -0
- package/dist/commands/help-json.js +128 -0
- package/dist/commands/help-json.js.map +1 -0
- package/dist/commands/history.js +83 -0
- package/dist/commands/history.js.map +1 -0
- package/dist/commands/mcp.js +129 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/media.js +273 -0
- package/dist/commands/media.js.map +1 -0
- package/dist/commands/pages.js +33 -0
- package/dist/commands/pages.js.map +1 -0
- package/dist/commands/previews.js +73 -0
- package/dist/commands/previews.js.map +1 -0
- package/dist/commands/publish.js +228 -0
- package/dist/commands/publish.js.map +1 -0
- package/dist/commands/read.js +85 -0
- package/dist/commands/read.js.map +1 -0
- package/dist/commands/site-guest.js +159 -0
- package/dist/commands/site-guest.js.map +1 -0
- package/dist/commands/site.js +204 -0
- package/dist/commands/site.js.map +1 -0
- package/dist/commands/sites.js +168 -0
- package/dist/commands/sites.js.map +1 -0
- package/dist/commands/skills.js +45 -0
- package/dist/commands/skills.js.map +1 -0
- package/dist/commands/workspace.js +89 -0
- package/dist/commands/workspace.js.map +1 -0
- package/dist/credentials.js +151 -0
- package/dist/credentials.js.map +1 -0
- package/dist/errors.js +166 -0
- package/dist/errors.js.map +1 -0
- package/dist/exit-codes.js +88 -0
- package/dist/exit-codes.js.map +1 -0
- package/dist/http.js +275 -0
- package/dist/http.js.map +1 -0
- package/dist/index.js +184 -0
- package/dist/index.js.map +1 -0
- package/dist/keychain.js +185 -0
- package/dist/keychain.js.map +1 -0
- package/dist/oauth/browser.js +33 -0
- package/dist/oauth/browser.js.map +1 -0
- package/dist/oauth/pkce.js +118 -0
- package/dist/oauth/pkce.js.map +1 -0
- package/dist/oauth/server.js +276 -0
- package/dist/oauth/server.js.map +1 -0
- package/dist/output.js +163 -0
- package/dist/output.js.map +1 -0
- package/dist/project.js +178 -0
- package/dist/project.js.map +1 -0
- package/dist/refresh-lock.js +84 -0
- package/dist/refresh-lock.js.map +1 -0
- package/dist/run.js +197 -0
- package/dist/run.js.map +1 -0
- package/dist/session.js +147 -0
- package/dist/session.js.map +1 -0
- package/dist/site-link.js +145 -0
- package/dist/site-link.js.map +1 -0
- package/dist/site-sync.js +191 -0
- package/dist/site-sync.js.map +1 -0
- package/dist/tar.js +228 -0
- package/dist/tar.js.map +1 -0
- package/dist/version.js +34 -0
- package/dist/version.js.map +1 -0
- package/package.json +47 -3
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The link between a folder and a site: `.atelier201/site.json`.
|
|
3
|
+
*
|
|
4
|
+
* It holds what `site push` and `site pull` need to be safe and nothing else:
|
|
5
|
+
* which site this folder is, the revision it last synced with, and the hash of
|
|
6
|
+
* every file as it was then (so a pull can tell "you edited this" from "nobody
|
|
7
|
+
* touched this"). Never a credential, with one exception: a guest site's
|
|
8
|
+
* short-lived write token (see `GuestState`), in a file written with mode 0600.
|
|
9
|
+
* `site link` adds `.atelier201/` to `.gitignore` so none of it is committed.
|
|
10
|
+
*
|
|
11
|
+
* A command finds the link by walking up from the current folder, so it works
|
|
12
|
+
* from `src/pages` as well as from the project root, and it acts on the root.
|
|
13
|
+
*/
|
|
14
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
15
|
+
import { appendFile, chmod, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
import { CliError } from "./errors.js";
|
|
18
|
+
import { EXIT } from "./exit-codes.js";
|
|
19
|
+
export const LINK_FOLDER = ".atelier201";
|
|
20
|
+
export const LINK_FILE = "site.json";
|
|
21
|
+
export function linkPath(dir) {
|
|
22
|
+
return path.join(dir, LINK_FOLDER, LINK_FILE);
|
|
23
|
+
}
|
|
24
|
+
function invalidLink(file, why) {
|
|
25
|
+
return new CliError(`${file} is not a valid site link (${why}).`, {
|
|
26
|
+
exitCode: EXIT.USAGE,
|
|
27
|
+
code: "invalid_request",
|
|
28
|
+
hint: "Delete the .atelier201 folder and run 'atelier201 site link --site <id>' again.",
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function parseLink(file, text) {
|
|
32
|
+
let value;
|
|
33
|
+
try {
|
|
34
|
+
value = JSON.parse(text);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
throw invalidLink(file, "it is not JSON");
|
|
38
|
+
}
|
|
39
|
+
const record = (value ?? {});
|
|
40
|
+
if (typeof record.site_id !== "string" || !record.site_id)
|
|
41
|
+
throw invalidLink(file, "site_id is missing");
|
|
42
|
+
const files = {};
|
|
43
|
+
if (record.files && typeof record.files === "object") {
|
|
44
|
+
for (const [name, hash] of Object.entries(record.files)) {
|
|
45
|
+
if (typeof hash === "string")
|
|
46
|
+
files[name] = hash;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const guest = record.guest;
|
|
50
|
+
const validGuest = guest &&
|
|
51
|
+
typeof guest.write_token === "string" &&
|
|
52
|
+
typeof guest.write_expires_at === "string" &&
|
|
53
|
+
typeof guest.claim_url === "string" &&
|
|
54
|
+
typeof guest.claim_expires_at === "string"
|
|
55
|
+
? {
|
|
56
|
+
write_token: guest.write_token,
|
|
57
|
+
write_expires_at: guest.write_expires_at,
|
|
58
|
+
claim_url: guest.claim_url,
|
|
59
|
+
claim_expires_at: guest.claim_expires_at,
|
|
60
|
+
}
|
|
61
|
+
: undefined;
|
|
62
|
+
return {
|
|
63
|
+
version: 1,
|
|
64
|
+
...(validGuest ? { guest: validGuest } : {}),
|
|
65
|
+
site_id: record.site_id,
|
|
66
|
+
slug: typeof record.slug === "string" ? record.slug : "",
|
|
67
|
+
revision: typeof record.revision === "string" ? record.revision : null,
|
|
68
|
+
synced_at: typeof record.synced_at === "string" ? record.synced_at : null,
|
|
69
|
+
files,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
/** The link of this folder or of the nearest parent folder, or null. */
|
|
73
|
+
export function findLink(start = process.cwd()) {
|
|
74
|
+
let dir = path.resolve(start);
|
|
75
|
+
for (;;) {
|
|
76
|
+
const file = linkPath(dir);
|
|
77
|
+
if (existsSync(file))
|
|
78
|
+
return { dir, link: parseLink(file, readFileSync(file, "utf8")) };
|
|
79
|
+
const parent = path.dirname(dir);
|
|
80
|
+
if (parent === dir)
|
|
81
|
+
return null;
|
|
82
|
+
dir = parent;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export async function writeLink(dir, link) {
|
|
86
|
+
// A link that holds a write token is readable by its owner only.
|
|
87
|
+
const secret = link.guest !== undefined;
|
|
88
|
+
await mkdir(path.join(dir, LINK_FOLDER), { recursive: true, mode: secret ? 0o700 : 0o777 });
|
|
89
|
+
await writeFile(linkPath(dir), `${JSON.stringify(link, null, 2)}\n`, { mode: secret ? 0o600 : 0o666 });
|
|
90
|
+
// `mode` only applies when the file is created: an older, wider file is tightened here.
|
|
91
|
+
if (secret)
|
|
92
|
+
await chmod(linkPath(dir), 0o600);
|
|
93
|
+
}
|
|
94
|
+
export async function readLinkIn(dir) {
|
|
95
|
+
try {
|
|
96
|
+
return parseLink(linkPath(dir), await readFile(linkPath(dir), "utf8"));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (error.code === "ENOENT")
|
|
100
|
+
return null;
|
|
101
|
+
throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Adds `.atelier201/` to the folder's `.gitignore`, only when one exists (a folder
|
|
106
|
+
* that is not a Git project does not need a new file). Returns whether it wrote.
|
|
107
|
+
*/
|
|
108
|
+
export async function ignoreLinkFolder(dir) {
|
|
109
|
+
const file = path.join(dir, ".gitignore");
|
|
110
|
+
let text;
|
|
111
|
+
try {
|
|
112
|
+
text = await readFile(file, "utf8");
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const already = text.split(/\r?\n/).some((line) => /^\/?\.atelier201\/?$/.test(line.trim()));
|
|
118
|
+
if (already)
|
|
119
|
+
return false;
|
|
120
|
+
await appendFile(file, `${text.length === 0 || text.endsWith("\n") ? "" : "\n"}.atelier201/\n`);
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
export const NO_SITE_MESSAGE = "No site selected.";
|
|
124
|
+
export const NO_SITE_HINT = "Pass --site <id> (list them with 'atelier201 sites list'), or run 'atelier201 site link --site <id>' in your project folder.";
|
|
125
|
+
/**
|
|
126
|
+
* `--site <id>` wins; otherwise the linked site of the current folder. With
|
|
127
|
+
* both, the link is returned only when it names the same site, so a command
|
|
128
|
+
* never records the state of one site in another's folder.
|
|
129
|
+
*/
|
|
130
|
+
export function selectSite(options, cwd = process.cwd()) {
|
|
131
|
+
const explicit = options.site?.trim();
|
|
132
|
+
const found = findLink(cwd);
|
|
133
|
+
if (explicit) {
|
|
134
|
+
return { siteId: explicit, linked: found && found.link.site_id === explicit ? found : null };
|
|
135
|
+
}
|
|
136
|
+
if (found)
|
|
137
|
+
return { siteId: found.link.site_id, linked: found };
|
|
138
|
+
throw new CliError(NO_SITE_MESSAGE, {
|
|
139
|
+
exitCode: EXIT.USAGE,
|
|
140
|
+
code: "invalid_parameter",
|
|
141
|
+
param: "site",
|
|
142
|
+
hint: NO_SITE_HINT,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
//# sourceMappingURL=site-link.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"site-link.js","sourceRoot":"","sources":["../src/site-link.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACjF,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAEvC,MAAM,CAAC,MAAM,WAAW,GAAG,aAAa,CAAC;AACzC,MAAM,CAAC,MAAM,SAAS,GAAG,WAAW,CAAC;AA6BrC,MAAM,UAAU,QAAQ,CAAC,GAAW;IAClC,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,WAAW,CAAC,IAAY,EAAE,GAAW;IAC5C,OAAO,IAAI,QAAQ,CAAC,GAAG,IAAI,8BAA8B,GAAG,IAAI,EAAE;QAChE,QAAQ,EAAE,IAAI,CAAC,KAAK;QACpB,IAAI,EAAE,iBAAiB;QACvB,IAAI,EAAE,iFAAiF;KACxF,CAAC,CAAC;AACL,CAAC;AAED,SAAS,SAAS,CAAC,IAAY,EAAE,IAAY;IAC3C,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACH,KAAK,GAAG,I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201 site pull`, kept apart from
|
|
3
|
+
* the commands so they can be tested against a temporary folder.
|
|
4
|
+
*
|
|
5
|
+
* Push turns a folder into an archive. Pull turns an archive into a plan (what
|
|
6
|
+
* to write, what is already right, what would overwrite local work) and only
|
|
7
|
+
* then, if the plan is clean or `--force` was given, touches the disk. A refused
|
|
8
|
+
* pull writes nothing at all.
|
|
9
|
+
*/
|
|
10
|
+
import { createHash } from "node:crypto";
|
|
11
|
+
import { lstat, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
12
|
+
import path from "node:path";
|
|
13
|
+
import { CliError } from "./errors.js";
|
|
14
|
+
import { EXIT } from "./exit-codes.js";
|
|
15
|
+
import { collectProject } from "./project.js";
|
|
16
|
+
import { safeRelativePath, tarArchive, tarSize } from "./tar.js";
|
|
17
|
+
/** What `POST /sites/{id}/import` accepts (LIMITS.importArchiveBytes). */
|
|
18
|
+
export const MAX_ARCHIVE_BYTES = 50 * 1024 * 1024;
|
|
19
|
+
export const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
20
|
+
const megabytes = (bytes) => `${(bytes / 1024 / 1024).toFixed(1)} MB`;
|
|
21
|
+
export async function buildPushArchive(root, limit = { bytes: MAX_ARCHIVE_BYTES, label: "the limit" }) {
|
|
22
|
+
const project = await collectProject(root);
|
|
23
|
+
if (project.files.length === 0) {
|
|
24
|
+
throw new CliError("There is no file to send in this folder.", {
|
|
25
|
+
exitCode: EXIT.USAGE,
|
|
26
|
+
code: "invalid_request",
|
|
27
|
+
hint: "Run it in the project folder, or pull a site first with 'atelier201 site pull --site <id>'.",
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const size = tarSize(project.files);
|
|
31
|
+
if (size > limit.bytes) {
|
|
32
|
+
const biggest = [...project.files]
|
|
33
|
+
.sort((a, b) => b.size - a.size)
|
|
34
|
+
.slice(0, 3)
|
|
35
|
+
.map((file) => `${file.path} (${megabytes(file.size)})`)
|
|
36
|
+
.join(", ");
|
|
37
|
+
throw new CliError(`The project is ${megabytes(size)} once archived; ${limit.label} is ${megabytes(limit.bytes)}.`, {
|
|
38
|
+
exitCode: EXIT.USAGE,
|
|
39
|
+
code: "request_too_large",
|
|
40
|
+
hint: `Largest files: ${biggest}. Upload images and videos with 'atelier201 media upload', or add them to .gitignore.`,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
const entries = [];
|
|
44
|
+
const hashes = {};
|
|
45
|
+
for (const file of project.files) {
|
|
46
|
+
const bytes = new Uint8Array(await readFile(file.absolute));
|
|
47
|
+
entries.push({ path: file.path, bytes, mode: file.mode });
|
|
48
|
+
hashes[file.path] = sha256(bytes);
|
|
49
|
+
}
|
|
50
|
+
return { archive: tarArchive(entries), hashes, skipped: project.skipped, files: entries.length };
|
|
51
|
+
}
|
|
52
|
+
function unsafe(path, reason) {
|
|
53
|
+
return new CliError(`The archive holds a path this CLI will not write: "${path.slice(0, 200)}" (${reason}).`, {
|
|
54
|
+
exitCode: EXIT.API_ERROR,
|
|
55
|
+
code: "internal_error",
|
|
56
|
+
type: "api_error",
|
|
57
|
+
hint: "Nothing was written to your folder.",
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
/** Refuses a destination that resolves outside the folder, or through a symbolic link. */
|
|
61
|
+
async function assertWritableInside(root, relative) {
|
|
62
|
+
const target = path.resolve(root, relative);
|
|
63
|
+
const inside = path.relative(root, target);
|
|
64
|
+
if (inside.startsWith("..") || path.isAbsolute(inside))
|
|
65
|
+
throw unsafe(relative, "it leaves the project folder");
|
|
66
|
+
// A folder of the project that is really a link to somewhere else would carry
|
|
67
|
+
// the write out of the folder, so every existing parent is checked.
|
|
68
|
+
let current = root;
|
|
69
|
+
for (const segment of relative.split("/").slice(0, -1)) {
|
|
70
|
+
current = path.join(current, segment);
|
|
71
|
+
try {
|
|
72
|
+
if ((await lstat(current)).isSymbolicLink())
|
|
73
|
+
throw unsafe(relative, `${segment} is a symbolic link`);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
if (error.code === "ENOENT")
|
|
77
|
+
return;
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function inspectLocal(absolute) {
|
|
83
|
+
try {
|
|
84
|
+
const info = await lstat(absolute);
|
|
85
|
+
if (!info.isFile())
|
|
86
|
+
return { kind: "other" };
|
|
87
|
+
return { kind: "file", hash: sha256(new Uint8Array(await readFile(absolute))) };
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (error.code === "ENOENT")
|
|
91
|
+
return { kind: "missing" };
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* `manifest` is the hash of each file as of the last sync (empty before the
|
|
97
|
+
* first). A local file is "untouched" when it still has that hash.
|
|
98
|
+
*/
|
|
99
|
+
export async function planPull(root, entries, manifest) {
|
|
100
|
+
const plan = { write: [], unchanged: [], remove: [], conflicts: [], hashes: {} };
|
|
101
|
+
const seen = new Set();
|
|
102
|
+
for (const entry of entries) {
|
|
103
|
+
const checked = safeRelativePath(entry.path);
|
|
104
|
+
if (!checked.ok)
|
|
105
|
+
throw unsafe(entry.path, checked.reason);
|
|
106
|
+
if (seen.has(checked.path))
|
|
107
|
+
throw unsafe(entry.path, "it appears twice");
|
|
108
|
+
seen.add(checked.path);
|
|
109
|
+
await assertWritableInside(root, checked.path);
|
|
110
|
+
const incoming = sha256(entry.bytes);
|
|
111
|
+
plan.hashes[checked.path] = incoming;
|
|
112
|
+
const local = await inspectLocal(path.join(root, checked.path));
|
|
113
|
+
if (local.kind === "missing") {
|
|
114
|
+
plan.write.push({ ...entry, path: checked.path });
|
|
115
|
+
}
|
|
116
|
+
else if (local.kind === "other") {
|
|
117
|
+
plan.conflicts.push({ path: checked.path, reason: "a folder or a link is in the way" });
|
|
118
|
+
}
|
|
119
|
+
else if (local.hash === incoming) {
|
|
120
|
+
plan.unchanged.push(checked.path);
|
|
121
|
+
}
|
|
122
|
+
else if (manifest[checked.path] === local.hash) {
|
|
123
|
+
plan.write.push({ ...entry, path: checked.path });
|
|
124
|
+
}
|
|
125
|
+
else if (manifest[checked.path] === undefined) {
|
|
126
|
+
plan.conflicts.push({ path: checked.path, reason: "exists here and differs, and was never pulled" });
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
plan.conflicts.push({ path: checked.path, reason: "changed here since the last pull or push" });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const [known, hash] of Object.entries(manifest)) {
|
|
133
|
+
if (seen.has(known))
|
|
134
|
+
continue;
|
|
135
|
+
const checked = safeRelativePath(known);
|
|
136
|
+
if (!checked.ok)
|
|
137
|
+
continue;
|
|
138
|
+
await assertWritableInside(root, checked.path);
|
|
139
|
+
const local = await inspectLocal(path.join(root, checked.path));
|
|
140
|
+
if (local.kind === "missing")
|
|
141
|
+
continue;
|
|
142
|
+
if (local.kind === "file" && local.hash === hash)
|
|
143
|
+
plan.remove.push(checked.path);
|
|
144
|
+
else
|
|
145
|
+
plan.conflicts.push({ path: checked.path, reason: "deleted upstream but changed here" });
|
|
146
|
+
}
|
|
147
|
+
return plan;
|
|
148
|
+
}
|
|
149
|
+
export function refuseConflicts(plan) {
|
|
150
|
+
if (plan.conflicts.length === 0)
|
|
151
|
+
return;
|
|
152
|
+
const shown = plan.conflicts
|
|
153
|
+
.slice(0, 10)
|
|
154
|
+
.map((conflict) => `${conflict.path} (${conflict.reason})`)
|
|
155
|
+
.join("; ");
|
|
156
|
+
const more = plan.conflicts.length > 10 ? `; and ${plan.conflicts.length - 10} more` : "";
|
|
157
|
+
throw new CliError(`Pulling would overwrite ${plan.conflicts.length} local ${plan.conflicts.length === 1 ? "file" : "files"}: ${shown}${more}.`, {
|
|
158
|
+
exitCode: EXIT.CONFLICT,
|
|
159
|
+
code: "conflict",
|
|
160
|
+
type: "conflict_error",
|
|
161
|
+
hint: "Nothing was written. Push your changes first, copy them elsewhere, or pass --force to overwrite them.",
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/** Applies a plan. With `force`, conflicting files are overwritten or removed too. */
|
|
165
|
+
export async function applyPull(root, entries, plan, force) {
|
|
166
|
+
// Every path was validated by planPull, which returns it unchanged.
|
|
167
|
+
const byPath = new Map(entries.map((entry) => [entry.path, entry]));
|
|
168
|
+
const toWrite = [...plan.write];
|
|
169
|
+
const toRemove = [...plan.remove];
|
|
170
|
+
if (force) {
|
|
171
|
+
for (const conflict of plan.conflicts) {
|
|
172
|
+
const entry = byPath.get(conflict.path);
|
|
173
|
+
if (entry) {
|
|
174
|
+
// A folder or link in the way is cleared before the file takes its place.
|
|
175
|
+
await rm(path.join(root, conflict.path), { recursive: true, force: true });
|
|
176
|
+
toWrite.push({ ...entry, path: conflict.path });
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
toRemove.push(conflict.path);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
for (const file of toWrite) {
|
|
184
|
+
const absolute = path.join(root, file.path);
|
|
185
|
+
await mkdir(path.dirname(absolute), { recursive: true });
|
|
186
|
+
await writeFile(absolute, file.bytes, { mode: (file.mode ?? 0o644) & 0o777 });
|
|
187
|
+
}
|
|
188
|
+
for (const file of toRemove)
|
|
189
|
+
await rm(path.join(root, file), { force: true });
|
|
190
|
+
}
|
|
191
|
+
//# sourceMappingURL=site-sync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"site-sync.js","sourceRoot":"","sources":["../src/site-sync.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;A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,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC3E,OAAO,CAAC,IAAI,CAAC,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;YAClD,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YAC/B,CAAC;QACH,CAAC;IACH,CAAC;IAED,KAAK,MAAM,IAAI,IAAI,OAAO,EAAE,CAAC;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5C,MAAM,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACzD,MAAM,SAAS,CAAC,QAAQ,EAAE,IAAI,CAAC,KAAK,EAAE,EAAE,IAAI,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,KAAK,CAAC,GAAG,KAAK,EAAE,CAAC,CAAC;IAChF,CAAC;IACD,KAAK,MAAM,IAAI,IAAI,QAAQ;QAAE,MAAM,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAChF,CAAC"}
|
package/dist/tar.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TAR for `atelier201 site push` and `atelier201 site pull`.
|
|
3
|
+
*
|
|
4
|
+
* The writer produces what the Engine's reader accepts (POSIX ustar, with a pax
|
|
5
|
+
* `path` header for a name longer than 100 bytes): regular files only. The
|
|
6
|
+
* reader is the CLI's own trust boundary. An archive comes from the network and
|
|
7
|
+
* is extracted onto a person's disk, so it accepts regular files and
|
|
8
|
+
* directories and nothing else: a symbolic link, a hard link or a device is a
|
|
9
|
+
* refusal, never a skipped entry.
|
|
10
|
+
*
|
|
11
|
+
* Kept in this package, with no dependency, because the CLI is published on its
|
|
12
|
+
* own and cannot import the Engine's `@atelier201/site-format`. `test/tar.test.ts`
|
|
13
|
+
* round-trips both directions through that package's reader and writer, so the
|
|
14
|
+
* two cannot drift apart unnoticed.
|
|
15
|
+
*/
|
|
16
|
+
import { CliError } from "./errors.js";
|
|
17
|
+
import { EXIT } from "./exit-codes.js";
|
|
18
|
+
const BLOCK = 512;
|
|
19
|
+
const encoder = new TextEncoder();
|
|
20
|
+
const decoder = new TextDecoder();
|
|
21
|
+
// ── Writer ──────────────────────────────────────────────────────────────────
|
|
22
|
+
function octal(value, width) {
|
|
23
|
+
return `${value.toString(8).padStart(width - 1, "0")}\0`;
|
|
24
|
+
}
|
|
25
|
+
function header(name, size, type, mode) {
|
|
26
|
+
const block = new Uint8Array(BLOCK);
|
|
27
|
+
const put = (text, offset, width) => block.set(encoder.encode(text).subarray(0, width), offset);
|
|
28
|
+
put(name, 0, 100);
|
|
29
|
+
put(octal(mode, 8), 100, 8);
|
|
30
|
+
put(octal(0, 8), 108, 8);
|
|
31
|
+
put(octal(0, 8), 116, 8);
|
|
32
|
+
put(octal(size, 12), 124, 12);
|
|
33
|
+
put(octal(0, 12), 136, 12);
|
|
34
|
+
put(" ", 148, 8);
|
|
35
|
+
put(type, 156, 1);
|
|
36
|
+
put("ustar\u0000", 257, 6);
|
|
37
|
+
put("00", 263, 2);
|
|
38
|
+
let sum = 0;
|
|
39
|
+
for (const byte of block)
|
|
40
|
+
sum += byte;
|
|
41
|
+
put(`${sum.toString(8).padStart(6, "0")}\0 `, 148, 8);
|
|
42
|
+
return block;
|
|
43
|
+
}
|
|
44
|
+
function padding(size) {
|
|
45
|
+
const rest = size % BLOCK;
|
|
46
|
+
return new Uint8Array(rest === 0 ? 0 : BLOCK - rest);
|
|
47
|
+
}
|
|
48
|
+
/** `<length> <key>=<value>\n`, where the length counts its own digits. */
|
|
49
|
+
function paxRecord(key, value) {
|
|
50
|
+
const body = ` ${key}=${value}\n`;
|
|
51
|
+
const bodyBytes = encoder.encode(body).byteLength;
|
|
52
|
+
let length = bodyBytes + 1;
|
|
53
|
+
while (String(length).length + bodyBytes !== length) {
|
|
54
|
+
length = String(length).length + bodyBytes;
|
|
55
|
+
}
|
|
56
|
+
return `${length}${body}`;
|
|
57
|
+
}
|
|
58
|
+
/** The size of the archive `tarArchive` would write, without building it. */
|
|
59
|
+
export function tarSize(files) {
|
|
60
|
+
let total = BLOCK * 2;
|
|
61
|
+
for (const file of files) {
|
|
62
|
+
const nameBytes = encoder.encode(file.path).byteLength;
|
|
63
|
+
if (nameBytes > 100) {
|
|
64
|
+
const pax = encoder.encode(paxRecord("path", file.path)).byteLength;
|
|
65
|
+
total += BLOCK + pax + padding(pax).byteLength;
|
|
66
|
+
}
|
|
67
|
+
total += BLOCK + file.size + padding(file.size).byteLength;
|
|
68
|
+
}
|
|
69
|
+
return total;
|
|
70
|
+
}
|
|
71
|
+
export function tarArchive(files) {
|
|
72
|
+
const chunks = [];
|
|
73
|
+
for (const file of files) {
|
|
74
|
+
if (encoder.encode(file.path).byteLength > 100) {
|
|
75
|
+
const pax = encoder.encode(paxRecord("path", file.path));
|
|
76
|
+
chunks.push(header(`PaxHeader/${file.path.slice(-80)}`, pax.byteLength, "x", 0o644), pax, padding(pax.byteLength));
|
|
77
|
+
}
|
|
78
|
+
chunks.push(header(file.path, file.bytes.byteLength, "0", file.mode ?? 0o644), file.bytes, padding(file.bytes.byteLength));
|
|
79
|
+
}
|
|
80
|
+
chunks.push(new Uint8Array(BLOCK * 2));
|
|
81
|
+
const out = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.byteLength, 0));
|
|
82
|
+
let offset = 0;
|
|
83
|
+
for (const chunk of chunks) {
|
|
84
|
+
out.set(chunk, offset);
|
|
85
|
+
offset += chunk.byteLength;
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
export const DEFAULT_READ_LIMITS = {
|
|
90
|
+
maxEntries: 20_000,
|
|
91
|
+
maxTotalBytes: 500 * 1024 * 1024,
|
|
92
|
+
};
|
|
93
|
+
function malformed(message) {
|
|
94
|
+
return new CliError(`The archive is not valid: ${message}`, {
|
|
95
|
+
exitCode: EXIT.API_ERROR,
|
|
96
|
+
code: "internal_error",
|
|
97
|
+
type: "api_error",
|
|
98
|
+
hint: "Nothing was written to your folder.",
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function readString(block, offset, width) {
|
|
102
|
+
const slice = block.subarray(offset, offset + width);
|
|
103
|
+
const end = slice.indexOf(0);
|
|
104
|
+
return decoder.decode(end === -1 ? slice : slice.subarray(0, end));
|
|
105
|
+
}
|
|
106
|
+
function readOctal(block, offset, width) {
|
|
107
|
+
const text = readString(block, offset, width).trim();
|
|
108
|
+
if (!/^[0-7]*$/.test(text))
|
|
109
|
+
throw malformed("a header holds an invalid number.");
|
|
110
|
+
return text ? parseInt(text, 8) : 0;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* `<length> <key>=<value>\n` records, where `length` counts BYTES. Parsed on
|
|
114
|
+
* bytes, not on a decoded string: a path with an accented letter is longer in
|
|
115
|
+
* bytes than in characters, and counting characters would swallow the newline
|
|
116
|
+
* into the path.
|
|
117
|
+
*/
|
|
118
|
+
function parsePax(bytes) {
|
|
119
|
+
const out = {};
|
|
120
|
+
let offset = 0;
|
|
121
|
+
while (offset < bytes.byteLength) {
|
|
122
|
+
const space = bytes.indexOf(0x20, offset);
|
|
123
|
+
if (space === -1)
|
|
124
|
+
break;
|
|
125
|
+
const length = Number(decoder.decode(bytes.subarray(offset, space)));
|
|
126
|
+
if (!Number.isInteger(length) || length <= 0 || offset + length > bytes.byteLength) {
|
|
127
|
+
throw malformed("an extended header is invalid.");
|
|
128
|
+
}
|
|
129
|
+
const record = decoder.decode(bytes.subarray(space + 1, offset + length - 1));
|
|
130
|
+
const equals = record.indexOf("=");
|
|
131
|
+
if (equals > 0)
|
|
132
|
+
out[record.slice(0, equals)] = record.slice(equals + 1);
|
|
133
|
+
offset += length;
|
|
134
|
+
}
|
|
135
|
+
return out;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The regular files of an archive held in memory. Directories are skipped (the
|
|
139
|
+
* paths imply them); anything else, links and devices included, throws. Paths
|
|
140
|
+
* come back as written, minus a leading `./`: the caller validates them with
|
|
141
|
+
* `safeRelativePath` before it touches the disk.
|
|
142
|
+
*/
|
|
143
|
+
export function readTar(bytes, limits = DEFAULT_READ_LIMITS) {
|
|
144
|
+
const files = [];
|
|
145
|
+
let pax = {};
|
|
146
|
+
let offset = 0;
|
|
147
|
+
let total = 0;
|
|
148
|
+
for (;;) {
|
|
149
|
+
if (offset + BLOCK > bytes.byteLength)
|
|
150
|
+
return files;
|
|
151
|
+
const block = bytes.subarray(offset, offset + BLOCK);
|
|
152
|
+
offset += BLOCK;
|
|
153
|
+
if (block.every((byte) => byte === 0))
|
|
154
|
+
return files;
|
|
155
|
+
const stored = readOctal(block, 148, 8);
|
|
156
|
+
let sum = 0;
|
|
157
|
+
for (let i = 0; i < BLOCK; i++)
|
|
158
|
+
sum += i >= 148 && i < 156 ? 32 : block[i];
|
|
159
|
+
if (sum !== stored)
|
|
160
|
+
throw malformed("a header checksum does not match.");
|
|
161
|
+
const type = String.fromCharCode(block[156]);
|
|
162
|
+
const size = readOctal(block, 124, 12);
|
|
163
|
+
const padded = Math.ceil(size / BLOCK) * BLOCK;
|
|
164
|
+
if (offset + padded > bytes.byteLength)
|
|
165
|
+
throw malformed("it ends inside an entry.");
|
|
166
|
+
const body = bytes.subarray(offset, offset + size);
|
|
167
|
+
offset += padded;
|
|
168
|
+
if (type === "x" || type === "g") {
|
|
169
|
+
if (size > 64 * 1024)
|
|
170
|
+
throw malformed("an extended header is too large.");
|
|
171
|
+
if (type === "x")
|
|
172
|
+
pax = parsePax(body);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
const prefix = readString(block, 345, 155);
|
|
176
|
+
const name = readString(block, 0, 100);
|
|
177
|
+
const path = (pax.path ?? (prefix ? `${prefix}/${name}` : name)).replace(/^\.\//, "");
|
|
178
|
+
pax = {};
|
|
179
|
+
if (type === "5")
|
|
180
|
+
continue;
|
|
181
|
+
if (type !== "0" && type !== "\0") {
|
|
182
|
+
throw new CliError(`The archive holds "${path.slice(0, 200)}", which is not a regular file (links and devices are refused).`, {
|
|
183
|
+
exitCode: EXIT.API_ERROR,
|
|
184
|
+
code: "internal_error",
|
|
185
|
+
type: "api_error",
|
|
186
|
+
hint: "Nothing was written to your folder.",
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
if (files.length + 1 > limits.maxEntries)
|
|
190
|
+
throw malformed(`it holds more than ${limits.maxEntries} files.`);
|
|
191
|
+
total += size;
|
|
192
|
+
if (total > limits.maxTotalBytes)
|
|
193
|
+
throw malformed("it is larger than this CLI will unpack.");
|
|
194
|
+
files.push({ path, bytes: body, mode: readOctal(block, 100, 8) });
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// ── Paths ───────────────────────────────────────────────────────────────────
|
|
198
|
+
/** Folders of a project folder that belong to the tools, never to the site. */
|
|
199
|
+
const TOOL_FOLDERS = new Set([".atelier201", ".git"]);
|
|
200
|
+
/**
|
|
201
|
+
* A path from an archive, made safe to join onto a folder. Refuses an absolute
|
|
202
|
+
* path, a drive letter, `..`, a backslash, a NUL and an empty segment, and the
|
|
203
|
+
* folders this CLI and Git own (an archive must never overwrite
|
|
204
|
+
* `.atelier201/site.json` or `.git/config`). Returns the normalized path, or the
|
|
205
|
+
* reason it was refused.
|
|
206
|
+
*/
|
|
207
|
+
export function safeRelativePath(input) {
|
|
208
|
+
if (!input)
|
|
209
|
+
return { ok: false, reason: "the path is empty" };
|
|
210
|
+
if (input.includes("\0"))
|
|
211
|
+
return { ok: false, reason: "the path contains a NUL byte" };
|
|
212
|
+
if (input.includes("\\"))
|
|
213
|
+
return { ok: false, reason: "the path contains a backslash" };
|
|
214
|
+
if (input.startsWith("/"))
|
|
215
|
+
return { ok: false, reason: "the path is absolute" };
|
|
216
|
+
if (/^[A-Za-z]:/.test(input))
|
|
217
|
+
return { ok: false, reason: "the path starts with a drive letter" };
|
|
218
|
+
const segments = input.split("/");
|
|
219
|
+
if (segments.some((segment) => segment === ".."))
|
|
220
|
+
return { ok: false, reason: "the path goes up with .." };
|
|
221
|
+
if (segments.some((segment) => segment === "" || segment === ".")) {
|
|
222
|
+
return { ok: false, reason: "the path has an empty segment" };
|
|
223
|
+
}
|
|
224
|
+
if (TOOL_FOLDERS.has(segments[0]))
|
|
225
|
+
return { ok: false, reason: `${segments[0]} is not part of a site` };
|
|
226
|
+
return { ok: true, path: segments.join("/") };
|
|
227
|
+
}
|
|
228
|
+
//# sourceMappingURL=tar.js.map
|
package/dist/tar.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tar.js","sourceRoot":"","sources":["../src/tar.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAEvC,MAAM,KAAK,GAAG,GAAG,CAAC;AAClB,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAIlC,+EAA+E;AAE/E,SAAS,KAAK,CAAC,KAAa,EAAE,KAAa;IACzC,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC;AAC3D,CAAC;AAED,SAAS,MAAM,CAAC,IAAY,EAAE,IAAY,EAAE,IAAe,EAAE,IAAY;IACvE,MAAM,K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,EAAE,EAAE,CAAC,OAAO,KAAK,IAAI,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,0BAA0B,EAAE,CAAC;IAC3G,IAAI,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,KAAK,EAAE,IAAI,OAAO,KAAK,GAAG,CAAC,EAAE,CAAC;QAClE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,+BAA+B,EAAE,CAAC;IAChE,CAAC;IACD,IAAI,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,wBAAwB,EAAE,CAAC;IACxG,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;AAChD,CAAC"}
|
package/dist/version.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
const require = createRequire(import.meta.url);
|
|
3
|
+
const pkg = require("../package.json");
|
|
4
|
+
export const PACKAGE_NAME = pkg.name;
|
|
5
|
+
export const VERSION = pkg.version;
|
|
6
|
+
/** Sent on every request, so the API can distinguish CLI traffic and flag old versions. */
|
|
7
|
+
export const USER_AGENT = `atelier201/${VERSION}`;
|
|
8
|
+
export const DEFAULT_API_URL = "https://api.atelier201.com/v1";
|
|
9
|
+
export const DEFAULT_MCP_URL = "https://mcp.atelier201.com";
|
|
10
|
+
export function apiBaseUrl(env = process.env) {
|
|
11
|
+
return (env.ATELIER201_API_URL || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
12
|
+
}
|
|
13
|
+
export function mcpBaseUrl(env = process.env) {
|
|
14
|
+
return (env.ATELIER201_MCP_URL || DEFAULT_MCP_URL).replace(/\/+$/, "");
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The authorization server sits one level above the versioned API base:
|
|
18
|
+
* `https://api.atelier201.com/v1` → `https://api.atelier201.com`. `ATELIER201_AUTH_URL`
|
|
19
|
+
* overrides it for anyone running the two on different hosts.
|
|
20
|
+
*/
|
|
21
|
+
export function authBaseUrl(env = process.env) {
|
|
22
|
+
if (env.ATELIER201_AUTH_URL)
|
|
23
|
+
return env.ATELIER201_AUTH_URL.replace(/\/+$/, "");
|
|
24
|
+
const api = new URL(apiBaseUrl(env));
|
|
25
|
+
const segments = api.pathname.split("/").filter(Boolean);
|
|
26
|
+
segments.pop();
|
|
27
|
+
api.pathname = segments.length ? `/${segments.join("/")}` : "/";
|
|
28
|
+
return api.toString().replace(/\/+$/, "");
|
|
29
|
+
}
|
|
30
|
+
/** The origin of the API, where the unauthenticated meta routes live. */
|
|
31
|
+
export function apiOrigin(env = process.env) {
|
|
32
|
+
return new URL(apiBaseUrl(env)).origin;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=version.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAE5C,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC/C,MAAM,GAAG,GAAG,OAAO,CAAC,iBAAiB,CAAsC,CAAC;AAE5E,MAAM,CAAC,MAAM,YAAY,GAAG,GAAG,CAAC,IAAI,CAAC;AACrC,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC;AAEnC,2FAA2F;AAC3F,MAAM,CAAC,MAAM,UAAU,GAAG,cAAc,OAAO,EAAE,CAAC;AAElD,MAAM,CAAC,MAAM,eAAe,GAAG,+BAA+B,CAAC;AAC/D,MAAM,CAAC,MAAM,eAAe,GAAG,4BAA4B,CAAC;AAE5D,M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