Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ at the reviewer’s chosen width. Use local asset paths and ordinary links to ot
An accepted publication writes a pending theme selection to `application.appearance.web_theme` in the working Plan.
An authored preset takes precedence over the Project preset. If the Plan changed during publication, the result
includes `published_web_theme` for reconciliation. Retrying a pending publication sends the exact saved request.
The CLI verifies the Project, revision and digest of the submitted Plan before saving its strong ETag.
API 0.10.3 supplies that validator in the receipt JSON even if a proxy weakens or removes the HTTP ETag.
With an older service, the CLI requires the original strong header.

Record the returned review ID with the owner's feedback. After separate Compile approval, run
`firstdraft plan compile --revision <approved-id>`, optionally with `--output <absent-directory>`. The CLI
Expand Down Expand Up @@ -1096,7 +1099,7 @@ publication sets the Head to its submitted Plan. The Service stores those bytes
keeps the Plan's meaning still makes a new Head, although the Project's `graph_version` stays the same.

A successful Plan push or screen publication returns a strong `ETag` of the form `"sha256:<hex>"`, quotes included. `<hex>` is the
64-character lowercase SHA-256 of the Head bytes. The CLI saves the complete header value in `.firstdraft/state.json`
64-character lowercase SHA-256 of the Head bytes. The CLI saves the complete service-issued validator in `.firstdraft/state.json`
and replays it in `If-Match`. `plan compile` also extracts `<hex>` to check that the local Plan still matches the
Head before it starts a Compilation. A saved ETag in any other form stops that check with
`invalid_configuration`.
Expand Down
8 changes: 7 additions & 1 deletion src/commands/sketches.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,16 @@ async function publishCurrentSketches({
);
}
const sha = request.manifest.plan_sha256;
const etag = response.headers.get("etag");
const headerEtag = response.headers.get("etag");
const { etag = headerEtag } = body?.foundation_plan ?? {};
if (
responseMediaType(response) !== "application/json" ||
typeof request.plan_source !== "string" ||
digest(request.plan_source) !== sha ||
etag !== '"sha256:' + sha + '"' ||
(headerEtag !== null &&
headerEtag !== etag &&
headerEtag !== "W/" + etag) ||
body?.project?.id !== state.project_id ||
body?.review?.id !== pending.id ||
body?.review?.plan_sha256 !== sha ||
Expand Down
79 changes: 79 additions & 0 deletions test/sketches.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,85 @@ function draw(cwd) {
);
}

for (const header of ["weak", "absent"]) {
test(`replays the saved Screens receipt when the HTTP validator is ${header}`, async (t) => {
const cwd = directory(t);
const opts = { cwd, apiUrl };
const { project } = await startProject({
...opts,
fetchFunction: async (input) => {
const id = String(input).split("/").at(-1);
return Response.json({ project: { id, url: "/projects/" + id } });
},
});
initializePlan({
cwd,
projectId: project.id,
name: "Reading",
applicationKey: "reading",
});
draw(cwd);
const dir = path.join(cwd, ".firstdraft");
const sha = digest(readFileSync(path.join(dir, "foundation-plan.json")));
const etag = '"sha256:' + sha + '"';
const pendingPath = path.join(dir, "sketch-pending.json");

/** @param {string | null} httpTag @param {string | undefined} planTag */
const publish = (httpTag, planTag) =>
publishSketches({
...opts,
fetchFunction: async (input, init) => {
const pending = JSON.parse(readFileSync(pendingPath, "utf8"));
assert.equal(init?.body, pending.body);
assert.equal(String(input).split("/").at(-1), pending.id);
return Response.json(
{
project: { id: project.id },
review: { id: pending.id, plan_sha256: sha },
foundation_plan: { source_sha256: sha, etag: planTag },
},
{ status: 200, headers: httpTag === null ? {} : { ETag: httpTag } },
);
},
});

const weak = "W/" + etag;
await assert.rejects(
publish(weak, undefined),
/Unexpected review identity/,
);
const pending = readFileSync(pendingPath, "utf8");
await assert.rejects(publish(etag, weak), /Unexpected review identity/);
await assert.rejects(
publish('"other"', etag),
/Unexpected review identity/,
);
assert.equal(readFileSync(pendingPath, "utf8"), pending);
assert.equal(
JSON.parse(readFileSync(path.join(dir, "state.json"), "utf8"))
.foundation_plan_etag,
undefined,
);
const changed = JSON.parse(pending);
const changedBody = JSON.parse(changed.body);
changedBody.plan_source += "\n";
changed.body = JSON.stringify(changedBody);
writeFileSync(pendingPath, JSON.stringify(changed));
await assert.rejects(publish(weak, etag), /Unexpected review identity/);
assert.equal(readFileSync(pendingPath, "utf8"), JSON.stringify(changed));
writeFileSync(pendingPath, pending);

const result = await publish(header === "weak" ? weak : null, etag);
assert.equal(result.review.id, JSON.parse(pending).id);
assert.equal(existsSync(pendingPath), false);
assert.equal(
JSON.parse(readFileSync(path.join(dir, "state.json"), "utf8"))
.foundation_plan_etag,
etag,
);
});
}

test("a reserved Project survives a lost start, Plan init and an ambiguous review publication", async (t) => {
const cwd = directory(t);
const opts = { cwd, apiUrl };
Expand Down
Loading