chadtmiller.com/monolith
An atproto PDS
0
0
pds-server: a space's listRecords cursor is a record's URI, as the reference's is
.changeset/space-record-cursors.mdnew+7−0
| @@ -0,0 +1,7 @@ | ||
--- | ||
default: patch | ||
--- | ||
| ||
# A space's `listRecords` cursor is a record's URI, as the reference's is | ||
| ||
`com.atproto.space.listRecords` answers its cursor as the URI of the page's last record, `{space}/{repo}/{collection}/{rkey}`, as the reference does, where it answered `{collection}/{rkey}`. It takes a cursor of that form as a position, whether or not that record exists, so a client can start a page below a key of its choosing, as knotless does to read a private repository's refs; before, such a cursor listed nothing. A cursor that is not a record URI of the space and repo asked is `400 InvalidCursor`. | ||
crates/pds-server/src/space_repo.rs+35−4
| @@ -1881,6 +1881,18 @@ fn list_records( | ||
Some(c) => (Included(format!("{c}/")), Excluded(format!("{c}0"))), | ||
None => (Unbounded, Unbounded), | ||
}; | ||
// A cursor is a record URI of this space and repo, as getRecord answers | ||
// one, the position of its `{collection}/{rkey}` whether or not that | ||
// record exists: a client builds one to start below a key of its choosing. | ||
let under = format!("{space}/{}/", a.did); | ||
let cursor = match cursor { | ||
Some(c) => Some( | ||
c.strip_prefix(under.as_str()) | ||
.filter(|c| c.contains('/')) | ||
.map_or_else(bad_cursor, Ok)?, | ||
), | ||
None => None, | ||
}; | ||
if let Some(c) = cursor | ||
&& !matches!(&hi, Excluded(h) if c > h.as_str()) | ||
{ | ||
| @@ -1918,7 +1930,7 @@ fn list_records( | ||
if records.len() == limit | ||
&& let Some((path, _)) = records.last() | ||
{ | ||
out["cursor"] = json!(path); | ||
out["cursor"] = json!(format!("{under}{path}")); | ||
} | ||
Ok(Resp::Json(out)) | ||
} | ||
| @@ -2924,7 +2936,7 @@ pub(crate) mod tests { | ||
paths( | ||
&list(vec![ | ||
("collection", "a.b.c".into()), | ||
("cursor", "a.b.c.d/1".into()) | ||
("cursor", format!("{SPACE}/{alice}/a.b.c.d/1")) | ||
]) | ||
.await | ||
), | ||
| @@ -2934,12 +2946,30 @@ pub(crate) mod tests { | ||
paths( | ||
&list(vec![ | ||
("collection", "a.b.c.d".into()), | ||
("cursor", "a.b.c/zzz".into()) | ||
("cursor", format!("{SPACE}/{alice}/a.b.c/zzz")) | ||
]) | ||
.await | ||
), | ||
["a.b.c.d/1"] | ||
); | ||
// A cursor is a record URI of this space and repo: a bare path, or | ||
// another repo's URI, is refused. | ||
for cursor in [ | ||
"a.b.c/zzz".to_string(), | ||
format!("{OTHER}/{alice}/a.b.c/zzz"), | ||
] { | ||
assert_eq!( | ||
r( | ||
&s, | ||
"listRecords", | ||
&t, | ||
&[("space", SPACE), ("repo", &alice), ("cursor", &cursor)] | ||
) | ||
.await, | ||
Err("InvalidCursor".into()), | ||
"{cursor}" | ||
); | ||
} | ||
assert_eq!( | ||
r(&s, "listRecords", &t, &[("space", OTHER), ("repo", &alice)]).await, | ||
Ok(json!({ "records": [] })) | ||
| @@ -5688,7 +5718,8 @@ pub(crate) mod tests { | ||
.await; | ||
assert_eq!(got["value"]["n"], 2000); | ||
reads(1); | ||
let extra = [("limit", "10"), ("cursor", "a.b.c/k01000")]; | ||
let from = format!("{SPACE}/{did}/a.b.c/k01000"); | ||
let extra = [("limit", "10"), ("cursor", from.as_str())]; | ||
let page = ask(&s, "listRecords", &t, &did, SPACE, &extra).await; | ||
assert_eq!(page["records"][0]["rkey"], "k00999"); | ||
assert_eq!(page["records"].as_array().unwrap().len(), 10); | ||
harness/spaces/repo.mjs+21−2
| @@ -573,17 +573,36 @@ export function spaceRepo(h) { | ||
const excludeValues = chance(0.3) ? chance(0.7) : undefined | ||
const rk = chance(0.05) ? 'other' : 'self' | ||
const mk = (side, cursor) => ({ token: tokenOf(acct, side), params: { space: spaceUri(sp, acct, side), repo: repoFor(acct, rk)(side), collection: coll, limit, reverse, excludeValues, cursor } }) | ||
const uriOf = (side, r) => `${spaceUri(sp, acct, side)}/${repoFor(acct, rk)(side)}/${r.collection}/${r.rkey}` | ||
const onPage = async (side, res, its, request) => { | ||
if (Boolean(res.body.cursor) !== (its.length === (limit ?? 50))) await fail(side, 'listRecords cursor', request, its.length === (limit ?? 50) ? 'a cursor, as the page is full' : 'no cursor, as the page is not full', res.body.cursor ?? null) | ||
// A cursor is the URI of the page's last record, as getRecord answers it. | ||
if (res.body.cursor && its.length && res.body.cursor !== uriOf(side, its.at(-1))) await fail(side, 'listRecords cursor, the URI of the page\'s last record (spec/SPACES-REPO.md, "Each read")', request, uriOf(side, its.at(-1)), res.body.cursor) | ||
} | ||
const got = await pages(`listRecords${reverse === undefined ? '' : ' (reverse sent)'}`, acct, `${SP}listRecords`, mk, (b) => b.records, limit, onPage) | ||
if (!got) return | ||
const shape = ([p, r]) => ({ collection: p.slice(0, p.indexOf('/')), rkey: p.slice(p.indexOf('/') + 1), cid: r.cid, ...(excludeValues ? {} : { value: r.value }) }) | ||
for (const side of sides) { | ||
const m = repoOf(acct, side, sp) | ||
const want = [...m.records].filter(([p]) => !coll || p.startsWith(`${coll}/`)).sort(([x], [y]) => byBytes(y, x)) | ||
.map(([p, r]) => ({ collection: p.slice(0, p.indexOf('/')), rkey: p.slice(p.indexOf('/') + 1), cid: r.cid, ...(excludeValues ? {} : { value: r.value }) })) | ||
const want = [...m.records].filter(([p]) => !coll || p.startsWith(`${coll}/`)).sort(([x], [y]) => byBytes(y, x)).map(shape) | ||
if (!isDeepStrictEqual(got[side.name], want)) await fail(side, 'listRecords, every page, against the records the harness wrote', shown('listRecords', mk(side)), want, got[side.name]) | ||
} | ||
// A cursor a client builds for a key, a record URI whose record need not | ||
// exist, starts the page below it (spec/SPACES-REPO.md, "Each read"): the | ||
// path of a record written, with `~` after it, which sorts above it and | ||
// below the next. | ||
const own = !sp.malformed && rk === 'self' && repoOf(acct, ref, sp).records.size | ||
if (!own) return | ||
const paths = [...repoOf(acct, ref, sp).records.keys()].filter((p) => !coll || p.startsWith(`${coll}/`)) | ||
if (!paths.length) return | ||
const at = pick(paths) | ||
const built = (side) => `${spaceUri(sp, acct, side)}/${repoFor(acct, rk)(side)}/${at}~` | ||
const [a, b] = await both('listRecords from a built cursor', acct, `${SP}listRecords`, (side) => mk(side, built(side))) | ||
for (const [side, res] of [[ref, a], [cand, b]]) { | ||
const m = repoOf(acct, side, sp) | ||
const want = [...m.records].filter(([p]) => (!coll || p.startsWith(`${coll}/`)) && byBytes(p, `${at}~`) < 0).sort(([x], [y]) => byBytes(y, x)).slice(0, limit ?? 50).map(shape) | ||
if (res.status !== 200 || !isDeepStrictEqual(res.body.records, want)) await fail(side, 'listRecords from a cursor built for a key, the page below it (spec/SPACES-REPO.md, "Each read")', shown('listRecords', mk(side, built(side))), want, res.status === 200 ? res.body.records : res.body) | ||
} | ||
} | ||
| ||
/// listRepoOps, every page with the repo's commit and its MAC, and a | ||
spec/SPACES-REPO.md+21−12
| @@ -185,9 +185,14 @@ that decides whose writes count (`SPACES.md`, the proposal). | ||
`com.atproto.repo.listRecords` takes, is ignored, whatever its value, as | ||
is every parameter the lexicon does not name: the order is always | ||
descending. `limit` is 1 to 1000, 50 by default. A `cursor` is answered | ||
when the page is full (`limit` records), and the next page is the | ||
records after the last of this one, in the same order. No repo answers | ||
`{records: []}`. | ||
when the page is full (`limit` records): the URI of its last record, as | ||
`getRecord` answers it, `{space}/{repo}/{collection}/{rkey}`. A cursor of | ||
that form, of the `space` and `repo` asked, is a position, whether or | ||
not such a record is or was ever there: the next page is the records | ||
whose `{collection}/{rkey}` is below the cursor's, in the same order. So | ||
a client may start a page below a key of its choosing, as | ||
`{space}/{repo}/{collection}/{prefix}.~` starts below every key that | ||
begins with `{prefix}.`. No repo answers `{records: []}`. | ||
- **`getLatestCommit`** `?space&repo` answers `{commit}`, the repo's commit, | ||
`data` as `{"$bytes": <base64>}`. No repo is `400 RepoNotFound`. | ||
- **`listRepoOps`** `?space&repo&since?&limit?&cursor?&excludeValues?` | ||
| @@ -225,15 +230,19 @@ that decides whose writes count (`SPACES.md`, the proposal). | ||
| ||
### Cursors | ||
| ||
The server may choose what a `cursor` is: the harness passes each side only | ||
the cursors it answered. But every cursor `listRecords`, `listRepoOps` and | ||
`listSpaces` answer has a `/` in it (the reference's are the last record's | ||
URI, `{rev}/{index}` of the last operation, and the last space's URI), and a | ||
`cursor` with no `/` is `400 InvalidCursor`. It is checked after the | ||
`repo` and the account's state (above) and before the repo is read, so a | ||
space with no repo answers it too: for `listRepoOps` after its | ||
`Accept-Signature` (below), and for `listSpaces` after the token. A cursor | ||
with a `/` that the server did not answer may be taken or refused | ||
The server may choose what a `listRepoOps` or `listSpaces` cursor is: the | ||
harness passes each side only the cursors it answered. A `listRecords` | ||
cursor is a record's URI, as the reference's is (above), since clients | ||
build one to start below a key. Every cursor `listRecords`, `listRepoOps` | ||
and `listSpaces` answer has a `/` in it (the reference's are the last | ||
record's URI, `{rev}/{index}` of the last operation, and the last space's | ||
URI), and a `cursor` with no `/` is `400 InvalidCursor`. It is checked | ||
after the `repo` and the account's state (above) and before the repo is | ||
read, so a space with no repo answers it too: for `listRepoOps` after its | ||
`Accept-Signature` (below), and for `listSpaces` after the token. A | ||
`listRecords` cursor with a `/` that is not a record URI of the `space` | ||
and `repo` asked is `400 InvalidCursor`; another method's cursor with a | ||
`/` that the server did not answer may be taken or refused | ||
`InvalidCursor`. `listBlobs` (`SPACES-EXPORT.md`) takes any cursor. | ||
| ||
### MACs on request | ||