@sanity-labs/client-fake-for-test 0.17.1 → 0.17.2
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 +5 -1
- package/dist/index.cjs +53 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +53 -40
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -178,9 +178,13 @@ The Content Lake parses a token's path grants on every request it serves and ref
|
|
|
178
178
|
|
|
179
179
|
Filter grants come from the ACL, not the token, and are not checked. `/users/me`, the project API (`/projects/…`), the Access API and the ACL are not the Content Lake's and do not read a token's grants, so they keep answering.
|
|
180
180
|
|
|
181
|
+
#### Which permission a write needs
|
|
182
|
+
|
|
183
|
+
The fake checks every written document the way the Content Lake does (gradient `pkg/security/document_permission_checker.go`): a create needs `create` on the new document; an update, replace or patch needs `update` on the document before the write **and** on the document after it; a delete needs `update` on the document before. So a patch or `createOrReplace` that moves a document out of the identity's `update` grant (a filter it stops matching) is refused with `WriteAccessDeniedError`, and nothing is written. The same holds for `version.create` replacing a version and for the documents `document.publish` / `document.unpublish` write and delete. A transaction, `mutate` or action batch is checked per document on its net change once every mutation is staged, so a document patched out of the grant and back again in one transaction passes, and one created and deleted in it is not checked. Reads inside a write are not checked.
|
|
184
|
+
|
|
181
185
|
#### System-document grants
|
|
182
186
|
|
|
183
|
-
User-issued writes to system document IDs beginning with `_.` require the `manage` permission
|
|
187
|
+
User-issued writes to system document IDs beginning with `_.` require the `manage` permission **in addition to** the `create` or `update` the write needs, each checked on the same documents (before and after the write), as the Content Lake's permission checker does. `create` and `update` alone do not authorize these writes, and neither does `manage` alone; grant `manage` alongside `create` / `update` to identities whose tests intentionally create, replace, patch, delete, transact, mutate, publish, or unpublish system documents. The fake's internal release/version bookkeeping remains outside access-control interception.
|
|
184
188
|
|
|
185
189
|
#### Dual-namespace identities: global vs project user ids
|
|
186
190
|
|
package/dist/index.cjs
CHANGED
|
@@ -1279,7 +1279,7 @@ function requireActive(release) {
|
|
|
1279
1279
|
`Release "${release.name}" is "${release.state}", but only "active" releases accept version writes (create / replace / discard / unpublish). Unschedule or unarchive it back to "active" first, or target a different release.`
|
|
1280
1280
|
);
|
|
1281
1281
|
}
|
|
1282
|
-
async function applyAction(store, action
|
|
1282
|
+
async function applyAction(store, action) {
|
|
1283
1283
|
switch (action.actionType) {
|
|
1284
1284
|
case "sanity.action.release.create": {
|
|
1285
1285
|
if (maybeRelease(store, action.releaseId) !== void 0)
|
|
@@ -1383,7 +1383,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1383
1383
|
return store.remove(release._id), { transactionId: cryptoRandomKey() };
|
|
1384
1384
|
}
|
|
1385
1385
|
case "sanity.action.document.version.create": {
|
|
1386
|
-
if ("baseId" in action) return createVersionFromBase(store, action
|
|
1386
|
+
if ("baseId" in action) return createVersionFromBase(store, action);
|
|
1387
1387
|
const releaseId = action.releaseId ?? (action.document._id !== void 0 ? parseVersionId(action.document._id).releaseId : void 0);
|
|
1388
1388
|
if (releaseId === void 0)
|
|
1389
1389
|
throw fakeError(
|
|
@@ -1395,7 +1395,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1395
1395
|
...action.document,
|
|
1396
1396
|
_id: vid
|
|
1397
1397
|
}, existing = store.get(vid), promoted = promote(doc);
|
|
1398
|
-
return existing !== void 0 && (promoted._createdAt = existing._createdAt),
|
|
1398
|
+
return existing !== void 0 && (promoted._createdAt = existing._createdAt), store.put(promoted), { transactionId: cryptoRandomKey() };
|
|
1399
1399
|
}
|
|
1400
1400
|
case "sanity.action.document.version.replace": {
|
|
1401
1401
|
const id = action.document._id;
|
|
@@ -1432,7 +1432,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1432
1432
|
const published = store.get(action.publishedId);
|
|
1433
1433
|
requireRevisionMatch(action.draftId, draft._rev, action.ifDraftRevisionId), requireRevisionMatch(action.publishedId, published?._rev, action.ifPublishedRevisionId);
|
|
1434
1434
|
const promoted = promote({ ...draft, _id: action.publishedId });
|
|
1435
|
-
return published !== void 0 && (promoted._createdAt = published._createdAt),
|
|
1435
|
+
return published !== void 0 && (promoted._createdAt = published._createdAt), store.put(promoted), store.remove(action.draftId), { transactionId: cryptoRandomKey() };
|
|
1436
1436
|
}
|
|
1437
1437
|
case "sanity.action.document.unpublish": {
|
|
1438
1438
|
requireDraftOf(action);
|
|
@@ -1441,7 +1441,7 @@ async function applyAction(store, action, hooks) {
|
|
|
1441
1441
|
throw fakeError(
|
|
1442
1442
|
`Cannot unpublish "${action.publishedId}": no published document with that _id exists. document.unpublish retracts a live published document into "${action.draftId}".`
|
|
1443
1443
|
);
|
|
1444
|
-
if (
|
|
1444
|
+
if (store.get(action.draftId) === void 0) {
|
|
1445
1445
|
const draft = promote({ ...published, _id: action.draftId });
|
|
1446
1446
|
draft._createdAt = published._createdAt, store.put(draft);
|
|
1447
1447
|
}
|
|
@@ -1455,7 +1455,7 @@ function requireDraftOf(action) {
|
|
|
1455
1455
|
`${action.actionType}: draftId "${action.draftId}" is not the draft of publishedId "${action.publishedId}" \u2014 expected draftId "drafts.${action.publishedId}".`
|
|
1456
1456
|
);
|
|
1457
1457
|
}
|
|
1458
|
-
async function createVersionFromBase(store, action
|
|
1458
|
+
async function createVersionFromBase(store, action) {
|
|
1459
1459
|
const { releaseId, publishedId } = parseVersionId(action.versionId);
|
|
1460
1460
|
if (publishedId !== action.publishedId)
|
|
1461
1461
|
throw fakeError(
|
|
@@ -1474,7 +1474,7 @@ async function createVersionFromBase(store, action, hooks) {
|
|
|
1474
1474
|
_createdAt: existing?._createdAt ?? nowIso(),
|
|
1475
1475
|
_system: { base: { id: base._id, rev: base._rev } }
|
|
1476
1476
|
}, promoted = promote(copy);
|
|
1477
|
-
return
|
|
1477
|
+
return store.put(promoted), { transactionId: cryptoRandomKey() };
|
|
1478
1478
|
}
|
|
1479
1479
|
function requireRevisionMatch(documentId, actualRev, expectedRev) {
|
|
1480
1480
|
if (!(expectedRev === void 0 || actualRev === expectedRev))
|
|
@@ -1486,7 +1486,7 @@ function requireRevisionMatch(documentId, actualRev, expectedRev) {
|
|
|
1486
1486
|
);
|
|
1487
1487
|
}
|
|
1488
1488
|
async function applyActions(store, actions, hooks) {
|
|
1489
|
-
const list = Array.isArray(actions) ? actions : [actions], snapshot = /* @__PURE__ */ new Map(), guardedIds = /* @__PURE__ */ new Set(), remember = (id) => {
|
|
1489
|
+
const list = Array.isArray(actions) ? actions : [actions], snapshot = /* @__PURE__ */ new Map(), guardedIds = /* @__PURE__ */ new Set(), gatedIds = /* @__PURE__ */ new Set(), remember = (id) => {
|
|
1490
1490
|
snapshot.has(id) || snapshot.set(id, store.get(id));
|
|
1491
1491
|
};
|
|
1492
1492
|
try {
|
|
@@ -1499,9 +1499,15 @@ async function applyActions(store, actions, hooks) {
|
|
|
1499
1499
|
case "sanity.action.release.unschedule":
|
|
1500
1500
|
case "sanity.action.release.archive":
|
|
1501
1501
|
case "sanity.action.release.unarchive":
|
|
1502
|
-
case "sanity.action.release.delete":
|
|
1503
1502
|
remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
|
|
1504
1503
|
break;
|
|
1504
|
+
case "sanity.action.release.delete": {
|
|
1505
|
+
remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
|
|
1506
|
+
const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;
|
|
1507
|
+
for (const d of store.all())
|
|
1508
|
+
d._id.startsWith(prefix) && remember(d._id);
|
|
1509
|
+
break;
|
|
1510
|
+
}
|
|
1505
1511
|
case "sanity.action.release.publish": {
|
|
1506
1512
|
remember(`${RELEASE_DOC_PREFIX}${a.releaseId}`);
|
|
1507
1513
|
const prefix = `${VERSIONS_PREFIX}${a.releaseId}.`;
|
|
@@ -1511,11 +1517,11 @@ async function applyActions(store, actions, hooks) {
|
|
|
1511
1517
|
}
|
|
1512
1518
|
case "sanity.action.document.version.create": {
|
|
1513
1519
|
if ("baseId" in a) {
|
|
1514
|
-
remember(a.versionId);
|
|
1520
|
+
remember(a.versionId), gatedIds.add(a.versionId);
|
|
1515
1521
|
break;
|
|
1516
1522
|
}
|
|
1517
1523
|
const releaseId = a.releaseId ?? (typeof a.document._id == "string" ? parseVersionId(a.document._id).releaseId : void 0);
|
|
1518
|
-
releaseId !== void 0 && remember(versionDocId(releaseId, a.publishedId));
|
|
1524
|
+
releaseId !== void 0 && (remember(versionDocId(releaseId, a.publishedId)), gatedIds.add(versionDocId(releaseId, a.publishedId)));
|
|
1519
1525
|
break;
|
|
1520
1526
|
}
|
|
1521
1527
|
case "sanity.action.document.version.replace":
|
|
@@ -1526,13 +1532,17 @@ async function applyActions(store, actions, hooks) {
|
|
|
1526
1532
|
remember(a.versionId);
|
|
1527
1533
|
break;
|
|
1528
1534
|
case "sanity.action.document.publish":
|
|
1529
|
-
remember(a.draftId), remember(a.publishedId), guardedIds.add(a.publishedId), guardedIds.add(a.draftId);
|
|
1535
|
+
remember(a.draftId), remember(a.publishedId), guardedIds.add(a.publishedId), guardedIds.add(a.draftId), gatedIds.add(a.publishedId), gatedIds.add(a.draftId);
|
|
1530
1536
|
break;
|
|
1531
1537
|
case "sanity.action.document.unpublish":
|
|
1532
|
-
remember(a.draftId), remember(a.publishedId), guardedIds.add(a.draftId), guardedIds.add(a.publishedId);
|
|
1538
|
+
remember(a.draftId), remember(a.publishedId), guardedIds.add(a.draftId), guardedIds.add(a.publishedId), gatedIds.add(a.publishedId), gatedIds.add(a.draftId);
|
|
1533
1539
|
break;
|
|
1534
1540
|
}
|
|
1535
|
-
last = await applyAction(store, a
|
|
1541
|
+
last = await applyAction(store, a);
|
|
1542
|
+
}
|
|
1543
|
+
for (const id of gatedIds) {
|
|
1544
|
+
const before = snapshot.get(id), after = store.get(id);
|
|
1545
|
+
before !== after && await hooks?.gate?.(before, after);
|
|
1536
1546
|
}
|
|
1537
1547
|
const contexts = mutationContexts(
|
|
1538
1548
|
[...guardedIds].map((id) => ({ before: snapshot.get(id), after: store.get(id) }))
|
|
@@ -2010,14 +2020,21 @@ function makeHandle(handleArgs) {
|
|
|
2010
2020
|
throw fakeError(
|
|
2011
2021
|
`ifRevisionId check failed: document "${id}" is at _rev="${existing._rev}" but the mutation required _rev="${ifRevisionId}". The document changed since you read it \u2014 re-read it and retry. (This mirrors Sanity's optimistic-locking 409.)`
|
|
2012
2022
|
);
|
|
2013
|
-
}, gate = async (
|
|
2023
|
+
}, gate = async (prev, next) => {
|
|
2014
2024
|
const access = activeAccess();
|
|
2015
|
-
access
|
|
2016
|
-
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2025
|
+
if (access === void 0) return;
|
|
2026
|
+
const id = (prev ?? next)?._id;
|
|
2027
|
+
if (id === void 0) return;
|
|
2028
|
+
const required = isSystemId(id) ? ["manage"] : [];
|
|
2029
|
+
required.push(prev === void 0 ? "create" : "update");
|
|
2030
|
+
for (const permission of required)
|
|
2031
|
+
for (const document of [prev, next])
|
|
2032
|
+
document !== void 0 && await enforceWriteAccess({
|
|
2033
|
+
document,
|
|
2034
|
+
grants: access.grants,
|
|
2035
|
+
permission,
|
|
2036
|
+
userId: actorId()
|
|
2037
|
+
});
|
|
2021
2038
|
}, guardGate = async (contexts) => {
|
|
2022
2039
|
const identity = actorId();
|
|
2023
2040
|
await enforceMutationGuards(
|
|
@@ -2040,13 +2057,13 @@ function makeHandle(handleArgs) {
|
|
|
2040
2057
|
throw fakeError(
|
|
2041
2058
|
`Cannot patch document "${documentId}": not found \u2014 no document with that _id exists in ${targetLabel}. patch() requires the target to already exist; use createOrReplace() / createIfNotExists() to upsert, or seed the document first.`
|
|
2042
2059
|
);
|
|
2043
|
-
checkRevision(documentId, ifRev)
|
|
2060
|
+
checkRevision(documentId, ifRev);
|
|
2044
2061
|
const patched = applyPatchOps(existing, ops), now = (/* @__PURE__ */ new Date()).toISOString(), next = {
|
|
2045
2062
|
...patched,
|
|
2046
2063
|
_rev: cryptoRandomKey(),
|
|
2047
2064
|
_updatedAt: now
|
|
2048
2065
|
};
|
|
2049
|
-
return options.
|
|
2066
|
+
return options.inTransaction !== !0 && (await gate(existing, next), await guardOne("update", existing, next)), store.put(next), next;
|
|
2050
2067
|
}, applyTransaction = async (mutations, txnOptions) => {
|
|
2051
2068
|
const transactionId = txnOptions?.transactionId ?? cryptoRandomKey(), snapshot = /* @__PURE__ */ new Map(), touchedIds = [], operations = [], remember = (id) => {
|
|
2052
2069
|
snapshot.has(id) || snapshot.set(id, store.get(id));
|
|
@@ -2061,39 +2078,35 @@ function makeHandle(handleArgs) {
|
|
|
2061
2078
|
throw fakeError(
|
|
2062
2079
|
`create() failed: a document with _id "${promoted._id}" already exists in ${targetLabel}. Use createOrReplace() to overwrite it, or createIfNotExists() to leave the existing one untouched.`
|
|
2063
2080
|
);
|
|
2064
|
-
|
|
2081
|
+
store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
|
|
2065
2082
|
break;
|
|
2066
2083
|
}
|
|
2067
2084
|
case "createOrReplace": {
|
|
2068
2085
|
remember(mut.doc._id);
|
|
2069
|
-
const promoted = promote2(mut.doc),
|
|
2070
|
-
await gate(existing ?? promoted, existing !== void 0 ? "update" : "create");
|
|
2071
|
-
const wasPresent = existing !== void 0;
|
|
2086
|
+
const promoted = promote2(mut.doc), wasPresent = store.get(promoted._id) !== void 0;
|
|
2072
2087
|
store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: wasPresent ? "update" : "create" });
|
|
2073
2088
|
break;
|
|
2074
2089
|
}
|
|
2075
2090
|
case "createIfNotExists": {
|
|
2076
2091
|
if (remember(mut.doc._id), store.get(mut.doc._id) === void 0) {
|
|
2077
2092
|
const promoted = promote2(mut.doc);
|
|
2078
|
-
|
|
2093
|
+
store.put(promoted), touchedIds.push(promoted._id), operations.push({ id: promoted._id, operation: "create" });
|
|
2079
2094
|
}
|
|
2080
2095
|
break;
|
|
2081
2096
|
}
|
|
2082
2097
|
case "delete": {
|
|
2083
|
-
remember(mut.id);
|
|
2084
|
-
const existing = store.get(mut.id);
|
|
2085
|
-
existing !== void 0 && await gate(existing, "update"), store.remove(mut.id) && (touchedIds.push(mut.id), operations.push({ id: mut.id, operation: "delete" }));
|
|
2098
|
+
remember(mut.id), store.remove(mut.id) && (touchedIds.push(mut.id), operations.push({ id: mut.id, operation: "delete" }));
|
|
2086
2099
|
break;
|
|
2087
2100
|
}
|
|
2088
2101
|
case "patch": {
|
|
2089
|
-
remember(mut.documentId), await applyPatch(mut.documentId, mut.ops, mut.ifRev, {
|
|
2102
|
+
remember(mut.documentId), await applyPatch(mut.documentId, mut.ops, mut.ifRev, { inTransaction: !0 }), touchedIds.push(mut.documentId), operations.push({ id: mut.documentId, operation: "update" });
|
|
2090
2103
|
break;
|
|
2091
2104
|
}
|
|
2092
2105
|
}
|
|
2093
|
-
const
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
await guardGate(
|
|
2106
|
+
const images = [...snapshot].map(([id, before]) => ({ before, after: store.get(id) }));
|
|
2107
|
+
for (const { before, after } of images)
|
|
2108
|
+
before !== after && await gate(before, after);
|
|
2109
|
+
await guardGate(mutationContexts(images));
|
|
2097
2110
|
} catch (error) {
|
|
2098
2111
|
for (const [id, doc] of snapshot)
|
|
2099
2112
|
doc === void 0 ? store.remove(id) : store.put(doc);
|
|
@@ -2176,7 +2189,7 @@ function makeHandle(handleArgs) {
|
|
|
2176
2189
|
`create() failed: a document with _id "${doc._id}" already exists in ${targetLabel}. Use createOrReplace() to overwrite it, or createIfNotExists() to leave the existing one untouched.`
|
|
2177
2190
|
);
|
|
2178
2191
|
const promoted = promote2(doc);
|
|
2179
|
-
await gate(
|
|
2192
|
+
await gate(void 0, promoted), await guardOne("create", null, promoted);
|
|
2180
2193
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
2181
2194
|
return opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "create", doc }], opts?.tag)), writeResult(
|
|
2182
2195
|
[promoted],
|
|
@@ -2188,7 +2201,7 @@ function makeHandle(handleArgs) {
|
|
|
2188
2201
|
createOrReplace: (doc, opts) => write(async () => {
|
|
2189
2202
|
checkRevision(doc._id, opts?.ifRevisionId);
|
|
2190
2203
|
const promoted = promote2(doc), existing = store.get(doc._id);
|
|
2191
|
-
await gate(existing
|
|
2204
|
+
await gate(existing, promoted), await guardOne(existing !== void 0 ? "update" : "create", existing ?? null, promoted);
|
|
2192
2205
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
2193
2206
|
opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createOrReplace", doc }], opts?.tag));
|
|
2194
2207
|
const operation = existing !== void 0 ? "update" : "create";
|
|
@@ -2210,7 +2223,7 @@ function makeHandle(handleArgs) {
|
|
|
2210
2223
|
opts
|
|
2211
2224
|
);
|
|
2212
2225
|
const promoted = promote2(doc);
|
|
2213
|
-
return await gate(
|
|
2226
|
+
return await gate(void 0, promoted), await guardOne("create", null, promoted), opts?.dryRun !== !0 && (store.put(promoted), record(transactionId, [{ kind: "createIfNotExists", doc }], opts?.tag)), writeResult(
|
|
2214
2227
|
[promoted],
|
|
2215
2228
|
[{ id: promoted._id, operation: "create" }],
|
|
2216
2229
|
transactionId,
|
|
@@ -2220,7 +2233,7 @@ function makeHandle(handleArgs) {
|
|
|
2220
2233
|
delete: (id, opts) => write(async () => {
|
|
2221
2234
|
checkRevision(id, opts?.ifRevisionId);
|
|
2222
2235
|
const existing = store.get(id);
|
|
2223
|
-
existing !== void 0 && await gate(existing,
|
|
2236
|
+
existing !== void 0 && await gate(existing, void 0), existing !== void 0 && await guardOne("delete", existing, null);
|
|
2224
2237
|
const transactionId = opts?.transactionId ?? cryptoRandomKey();
|
|
2225
2238
|
let removed = existing;
|
|
2226
2239
|
return opts?.dryRun !== !0 && (removed = store.remove(id), removed !== void 0 && record(transactionId, [{ kind: "delete", id }], opts?.tag)), opts?.returnDocuments === void 0 && opts?.returnFirst === void 0 ? removed !== void 0 ? { _id: id } : null : formatMutationResult(
|