~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

pds xrpc writes an empty blob as nothing, {} only for an empty answer

Open chadtmiller.com wants to merge into main from chadtmiller.com/monolith:fix/xrpc-empty-blob
.changeset/xrpc-empty-blob.mdnew+7−0
@@ -0,0 +1,7 @@
---
default: patch
---
# `pds xrpc` writes an empty blob as nothing
`pds xrpc` prints `{}` only for an answer with neither a body nor a `Content-Type`, as a method that answers nothing. An empty body under any `Content-Type`, as `getBlob` serves a blob of no bytes, even one stored as `application/json`, is written as it is: nothing.
crates/pds-cli/src/xrpc.rs+3−2
@@ -244,8 +244,9 @@ fn send(call: Call) -> Result<u8, Stop> {
.ok()
.filter(|t| serde_json::from_str::<&RawValue>(t).is_ok());
match (json, text) {
// Nothing, as a method that answers nothing: `{}`.
(None | Some(true), _) if bytes.is_empty() => {
// Nothing, and no `Content-Type`, as a method that answers nothing:
// `{}`. An empty blob, under its own type (JSON's too), is nothing.
(None, _) if bytes.is_empty() => {
let _ = writeln!(out, "{{}}");
}
(Some(true), Some(text)) => {
harness/checks/commands.mjs+25−1
@@ -218,7 +218,9 @@ const bodyOf = (b) => {
/// answering as their routes do over XRPC; a method the server forwards
/// (spec/PROXY.md, "Which requests are forwarded") answering, and
/// forwarded, as over XRPC; a body as `-` with a `Content-Type:` header,
/// importRepo's, answered as over XRPC; and each usage error exiting 2,
/// importRepo's, answered as over XRPC; an answer with no body printed as
/// `{}` only when it has no `Content-Type`, and otherwise as nothing; and
/// each usage error exiting 2,
/// with what was wrong on standard error and no request reaching a host of
/// the harness's own that `PDS_URL` names, where calls well formed do
/// reach it, as they say.
@@ -265,6 +267,28 @@ async function checkXrpc(own) {
const got = await viaXrpc(IMPORT, { bytes: car, contentType: 'application/vnd.ipld.car', token: own })
if (!isDeepStrictEqual({ status: got.status, error: got.status === 200 ? null : (got.body?.error ?? null) }, want)) await differ(`pds xrpc procedure ${IMPORT} Content-Type:application/vnd.ipld.car -`, { stdin: 'not a CAR file' }, want, { status: got.status, error: got.body?.error ?? null }, spec)
// Answers with no body: `{}` only where there is no Content-Type either,
// as a method that answers nothing; under one, as an empty blob is
// served, even JSON's, nothing.
const empties = [
[null, '{}\n'],
['application/json', ''],
['text/plain', ''],
['application/octet-stream', ''],
]
const blank = await ownHost((req, res) => {
const type = new URL(req.url, 'http://x').searchParams.get('type')
res.writeHead(200, type ? { 'content-type': type } : {})
res.end()
})
for (const [type, want] of empties) {
const args = ['query', 'com.example.harness.empty', ...(type ? [`type==${type}`] : [])]
const r = await runCli(['xrpc', ...args], { url: `http://127.0.0.1:${blank.port}` })
const out = r.stdout.toString('utf8')
if (r.code !== 0 || out !== want) await differ(`pds xrpc ${args.join(' ')}`, { answer: { status: 200, 'content-type': type, body: '' } }, { code: 0, stdout: want }, { code: r.code, stdout: out.slice(0, 300), stderr: r.stderr.slice(0, 300) }, spec)
}
await blank.close()
// Usage errors, against a host of the harness's own that counts every
// request: none may reach it. Calls well formed, beside them, do.
const host = await ownHost()
spec/COMMANDS.md+6−3
@@ -331,9 +331,12 @@ wrong on standard error, and no call made. `--help` among the arguments
prints how it is called, exit 0, and makes no call.
It answers as a command does ("Answers"): a JSON answer printed on one
line, and an answer with no body `{}`, exit 0; an answer of another
`Content-Type` written as its bytes; an error printed as `{status, error,
message}`, exit 1; and a server that cannot be reached, exit 3.
line; an answer with no body and no `Content-Type`, as a method that
answers nothing, `{}`; any other answer written as its bytes, so a blob
of no bytes, which `getBlob` serves under its own `Content-Type` (even
`application/json`), writes nothing; all exit 0. An error is printed as
`{status, error, message}`, exit 1; and a server that cannot be reached,
exit 3.
```sh
pds xrpc query com.atproto.repo.listRecords repo==alice.test collection==app.bsky.feed.post limit==5
Cargo.lock+4−4
@@ -545,9 +545,9 @@ dependencies = [
[[package]]
name = "form_urlencoded"
version = "1.2.2"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
checksum = "afa489199eaffb1211795df1c6236ce1ad0d41a6fa45250add73e3b02420f06e"
dependencies = [
"percent-encoding",
]
@@ -2421,9 +2421,9 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "url"
version = "2.5.8"
version = "2.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
checksum = "f00cd2407bb42c0ef17be88d4cf020f5cceffd121665bac9b8b9493a369b3b1a"
dependencies = [
"form_urlencoded",
"idna",