Skip to content
Merged
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
16 changes: 15 additions & 1 deletion services/openwork-share/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ It is designed to be deployed on Vercel and backed by Vercel Blob.
- Returns `{ "url": "https://share.openwork.software/b/<id>" }`.

- `GET /b/:id`
- Proxies the stored object back to the caller.
- Returns an HTML share page by default for browser requests.
- Returns raw JSON for API/programmatic requests:
- send `Accept: application/json`, or
- append `?format=json`.
- Supports `?format=json&download=1` to download the bundle as a file.

## Required Environment Variables

Expand Down Expand Up @@ -40,6 +44,16 @@ pnpm install
vercel dev
```

## Quick checks

```bash
# Human-friendly page
curl -i "http://localhost:3000/b/<id>" -H "Accept: text/html"

# Machine-readable payload (OpenWork parser path)
curl -i "http://localhost:3000/b/<id>?format=json"
```

## Notes

- Links are public and unguessable (no auth, no encryption).
Expand Down
21 changes: 18 additions & 3 deletions services/openwork-share/api/b/[id].js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { head } from "@vercel/blob";
import { renderBundlePage, wantsDownload, wantsJsonResponse } from "./render-bundle-page.js";

function setCors(res) {
res.setHeader("Access-Control-Allow-Origin", "*");
Expand Down Expand Up @@ -40,9 +41,23 @@ export default async function handler(req, res) {
return;
}

res.setHeader("Content-Type", blob.contentType || response.headers.get("content-type") || "application/json");
const rawBuffer = Buffer.from(await response.arrayBuffer());
const rawJson = rawBuffer.toString("utf8");
const serveJson = wantsJsonResponse(req);

res.setHeader("Vary", "Accept");
res.setHeader("Cache-Control", "public, max-age=3600");

const buffer = Buffer.from(await response.arrayBuffer());
res.status(200).send(buffer);
if (serveJson) {
res.setHeader("Content-Type", blob.contentType || response.headers.get("content-type") || "application/json");
if (wantsDownload(req)) {
res.setHeader("Content-Disposition", `attachment; filename="openwork-bundle-${id}.json"`);
}
res.status(200).send(rawBuffer);
return;
}

const html = renderBundlePage({ id, rawJson, req });
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.status(200).send(html);
}
Loading
Loading