Skip to content

Commit fe897a1

Browse files
wesbillmanBrain
andauthored
Add composer attachments and compatible media preparation (#183)
Signed-off-by: Brain <1a02c72794dcd0f07058a353bc3a81f4028b8c77c92c87fce6d5c8b85970a20b@buzz.block.builderlab.xyz> Co-authored-by: Brain <1a02c72794dcd0f07058a353bc3a81f4028b8c77c92c87fce6d5c8b85970a20b@buzz.block.builderlab.xyz>
1 parent 2226057 commit fe897a1

50 files changed

Lines changed: 3680 additions & 176 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,35 @@ do not delete or replace its Keychain entry. Environment variables override
106106
an existing supported credential, live development is unavailable; shell and
107107
fixture tests still work.
108108

109+
Media attachments in live development need `ffmpeg` on the server’s PATH for
110+
video, HEIC/HEIF, and the existing `voice-note-*.wav` exception (macOS:
111+
`brew install ffmpeg`; Linux: your distribution’s ffmpeg package). The broker
112+
prepares canonical H.264/AAC MP4 or single-frame JPEG before upload hashes/signs
113+
those exact bytes. Missing tools and unsupported codecs fail visibly. No generic
114+
audio conversion or recording UI is added.
115+
116+
JPEG/PNG/WebP cleanup preserves orientation and alpha; GIF/APNG/WebP structural
117+
cleanup preserves animation. Animated images requiring ICC or EXIF orientation
118+
transforms reject rather than silently change appearance. Snapshot PNG manifests
119+
survive cleanup. A lazy, cancellable lossless WebP encoder covers still-image pixel
120+
cleanup on WebKit, which lacks a canvas WebP encoder.
121+
122+
Final-file defaults match old Buzz: 50 MiB images, 10 MiB GIFs, 100 MiB generic
123+
files, 500 MiB videos. The relay remains authoritative and can enforce lower
124+
limits, 25-million-pixel images, and video codec/duration/resolution constraints.
125+
The client separately bounds source files at 500 MiB (voice notes: 128 MiB), ten
126+
files per draft, and 1,000 MiB retained sources per session. These source/batch
127+
safety budgets are not old-relay final-byte limits. Preparation can grow or shrink
128+
files. Browser Blobs still retain complete prepared payloads; only the broker's
129+
transfer buffers are streaming. Private spool files and conversion children are
130+
request-owned, cancelled on disconnect and cleaned before admission is released.
131+
132+
Picker/paste/drop share the same tab-local draft. Files must finish uploading
133+
before Send; navigation pauses unfinished uploads for explicit Retry. Reload loses
134+
unsent files. Background Send, attachment-first new sessions and UX polish are
135+
separate work. Live uploads currently use the development broker, not a packaged
136+
native upload implementation.
137+
109138
The broker supports reads, live traffic and basic message sending **as your real
110139
account**. Profile changes and invite admission can also write to real communities.
111140
Use **Switch community → Add a community** and type the community's `wss://` or

‎dev/attachment-file.mjs‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { createHash } from "node:crypto";
2+
import { createWriteStream } from "node:fs";
3+
import { mkdtemp, open, rm } from "node:fs/promises";
4+
import { tmpdir } from "node:os";
5+
import { join } from "node:path";
6+
import { Transform } from "node:stream";
7+
import { pipeline } from "node:stream/promises";
8+
9+
/** One request owns a private spool; cleanup includes receive/write failure. */
10+
export async function receiveAttachment(req, limit, signal, sizeError) {
11+
const directory = await mkdtemp(join(tmpdir(), "buzz-attachment-"));
12+
const path = join(directory, "source");
13+
const cleanup = () => rm(directory, { recursive: true, force: true });
14+
let size = 0;
15+
const hash = createHash("sha256");
16+
try {
17+
signal.throwIfAborted();
18+
if (Number(req.headers["content-length"]) > limit) throw sizeError();
19+
const meter = new Transform({
20+
transform(chunk, _encoding, callback) {
21+
size += chunk.length;
22+
if (size > limit) return callback(sizeError());
23+
hash.update(chunk);
24+
callback(null, chunk);
25+
},
26+
});
27+
// Do not pipeline the HTTP request itself: size rejection must still send a 413.
28+
async function* source() {
29+
const iterator = req.iterator({ destroyOnReturn: false });
30+
for await (const chunk of iterator) yield chunk;
31+
}
32+
const abort = () => req.destroy();
33+
signal.addEventListener("abort", abort, { once: true });
34+
try {
35+
await pipeline(
36+
source(),
37+
meter,
38+
createWriteStream(path, { mode: 0o600, flags: "wx" }),
39+
{ signal },
40+
);
41+
} finally {
42+
signal.removeEventListener("abort", abort);
43+
}
44+
if (!size) throw sizeError();
45+
const file = await open(path, "r");
46+
let header;
47+
try {
48+
const buffer = Buffer.alloc(Math.min(size, 4096));
49+
const { bytesRead } = await file.read(buffer, 0, buffer.length, 0);
50+
header = buffer.subarray(0, bytesRead);
51+
} finally {
52+
await file.close();
53+
}
54+
return {
55+
path,
56+
directory,
57+
size,
58+
header,
59+
sha256: hash.digest("hex"),
60+
cleanup,
61+
};
62+
} catch (error) {
63+
if (signal.aborted) req.destroy();
64+
await cleanup();
65+
throw error;
66+
}
67+
}

‎dev/attachment-upload.mjs‎

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import { createHash } from "node:crypto";
1+
import { createReadStream } from "node:fs";
2+
import { finished } from "node:stream/promises";
23
import { finalizeEvent } from "nostr-tools";
3-
4-
export const UPLOAD_MAX_BYTES = 20 * 1024 * 1024;
5-
export const UPLOAD_TIMEOUT_MS = 120_000;
4+
import { receiveAttachment } from "./attachment-file.mjs";
5+
import {
6+
imageType,
7+
mediaByteLimit,
8+
UPLOAD_MAX_BYTES,
9+
UPLOAD_TIMEOUT_MS,
10+
} from "../src/features/relay/attachment-limits.ts";
11+
import { videoDemuxer } from "../src/features/relay/video-preparation.ts";
12+
export { UPLOAD_MAX_BYTES, UPLOAD_TIMEOUT_MS };
613
export class UploadError extends Error {
714
constructor(code, status = 400) {
815
super(`Attachment upload: ${code}`);
@@ -23,22 +30,20 @@ export async function uploadAttachment(req, relay, key, fetchUpstream, signal) {
2330
signal,
2431
AbortSignal.timeout(UPLOAD_TIMEOUT_MS),
2532
]);
26-
const abort = () => req.destroy();
27-
bounded.addEventListener("abort", abort, { once: true });
33+
const spool = await receiveAttachment(
34+
req,
35+
UPLOAD_MAX_BYTES,
36+
bounded,
37+
() => new UploadError("size", 413),
38+
);
39+
let body;
2840
try {
2941
bounded.throwIfAborted();
30-
const chunks = [];
31-
const hash = createHash("sha256");
32-
let size = 0;
33-
for await (const chunk of req) {
34-
size += chunk.length;
35-
if (size > UPLOAD_MAX_BYTES) throw new UploadError("size", 413);
36-
hash.update(chunk);
37-
chunks.push(chunk);
38-
}
39-
if (!size) throw new UploadError("size", 413);
40-
bounded.throwIfAborted();
41-
const sha256 = hash.digest("hex");
42+
const { size, sha256 } = spool;
43+
const detected =
44+
imageType(spool.header) ??
45+
(videoDemuxer(spool.header) ? "video/mp4" : "application/octet-stream");
46+
if (size > mediaByteLimit(detected)) throw new UploadError("size", 413);
4247
const now = Math.floor(Date.now() / 1000);
4348
const event = finalizeEvent(
4449
{
@@ -49,22 +54,36 @@ export async function uploadAttachment(req, relay, key, fetchUpstream, signal) {
4954
["t", "upload"],
5055
["x", sha256],
5156
["server", new URL(relay).host],
52-
["expiration", String(now + 300)],
57+
[
58+
"expiration",
59+
String(now + Math.ceil(UPLOAD_TIMEOUT_MS / 1000) + 60),
60+
],
5361
],
5462
},
5563
key,
5664
);
57-
const response = await fetchUpstream(`${relay}/upload`, {
58-
method: "PUT",
59-
redirect: "error",
60-
signal: bounded,
61-
headers: {
62-
"Content-Type": type,
63-
"X-SHA-256": sha256,
64-
Authorization: `Nostr ${Buffer.from(JSON.stringify(event)).toString("base64url")}`,
65-
},
66-
body: Buffer.concat(chunks, size),
67-
});
65+
body = createReadStream(spool.path, { signal: bounded });
66+
// Attach before fetch: cancellation can race stream opening.
67+
const closed = finished(body).catch(() => {});
68+
let response;
69+
try {
70+
response = await fetchUpstream(`${relay}/upload`, {
71+
method: "PUT",
72+
redirect: "error",
73+
signal: bounded,
74+
headers: {
75+
"Content-Type": type,
76+
"Content-Length": String(size),
77+
"X-SHA-256": sha256,
78+
Authorization: `Nostr ${Buffer.from(JSON.stringify(event)).toString("base64url")}`,
79+
},
80+
body,
81+
duplex: "half",
82+
});
83+
} finally {
84+
body.destroy();
85+
await closed;
86+
}
6887
// These statuses do not need error text; cancel rather than read their bodies.
6988
const status = response.status;
7089
if (!response.ok && ![400, 415, 422].includes(status)) {
@@ -121,6 +140,6 @@ export async function uploadAttachment(req, relay, key, fetchUpstream, signal) {
121140
throw new UploadError("invalid", 502);
122141
return { url: url.href, type: v.type, size, sha256 };
123142
} finally {
124-
bounded.removeEventListener("abort", abort);
143+
await spool.cleanup();
125144
}
126145
}

0 commit comments

Comments
 (0)