Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: npm
- run: npm ci
- run: npx wrangler types
Expand Down
49 changes: 37 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ async function handleCreate(req, env, url) {
const createdAtMs = Date.now();
const putOpts = ttlSec > 0 ? { expirationTtl: ttlSec } : {};
const meta = JSON.stringify({ v: 1, h: tokenHash, t: ttlKey, ct: createdAtMs });
await env.NOTES.put(key, content, putOpts);
await env.NOTES.put("m:" + name, meta, putOpts);
await Promise.all([

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Parallel content+meta writes can commit metadata on failed edits. Preserve write order so metadata changes only after content persistence succeeds.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/index.ts, line 96:

<comment>Parallel content+meta writes can commit metadata on failed edits. Preserve write order so metadata changes only after content persistence succeeds.</comment>

<file context>
@@ -93,8 +93,10 @@ async function handleCreate(req, env, url) {
   const meta = JSON.stringify({ v: 1, h: tokenHash, t: ttlKey, ct: createdAtMs });
-  await env.NOTES.put(key, content, putOpts);
-  await env.NOTES.put("m:" + name, meta, putOpts);
+  await Promise.all([
+    env.NOTES.put(key, content, putOpts),
+    env.NOTES.put("m:" + name, meta, putOpts),
</file context>

env.NOTES.put(key, content, putOpts),
env.NOTES.put("m:" + name, meta, putOpts),
]);

const kind = urlMode ? "url" : "text";
const target = urlMode ? content.trim() : null;
Expand Down Expand Up @@ -174,8 +176,10 @@ async function handleEdit(req, env, sub, url) {

const ttlSec = TTL_OPTIONS[ttlKey];
const putOpts = ttlSec > 0 ? { expirationTtl: ttlSec } : {};
await env.NOTES.put("n:" + sub, content, putOpts);
await env.NOTES.put("m:" + sub, metaRaw, putOpts);
await Promise.all([
env.NOTES.put("n:" + sub, content, putOpts),
env.NOTES.put("m:" + sub, metaRaw, putOpts),
]);

const kind = urlMode ? "url" : "text";
const target = urlMode ? content.trim() : null;
Expand All @@ -202,8 +206,10 @@ async function handleAbuseReport(req, env, sub, url) {
let disabled = false;
if (!already) {
count += 1;
await env.NOTES.put(counterKey, String(count), { expirationTtl: 30 * 86400 });
await env.NOTES.put(dedupeKey, "1", { expirationTtl: 86400 });
await Promise.all([
env.NOTES.put(counterKey, String(count), { expirationTtl: 30 * 86400 }),
env.NOTES.put(dedupeKey, "1", { expirationTtl: 86400 }),
]);
if (count >= ABUSE_AUTO_DISABLE) {
await env.NOTES.put("d:" + sub, JSON.stringify({ reason: "community_reports", count, at: Date.now() }), { expirationTtl: 365 * 86400 });
disabled = true;
Expand Down Expand Up @@ -244,8 +250,10 @@ async function handleSubdomain(req, env, host, url) {
if (url.searchParams.has("edit") || req.method === "POST" || req.method === "PUT") return handleEdit(req, env, sub, url);

if (pathname === "/edit") {
const metaRaw = await env.NOTES.get("m:" + sub);
const existing = await env.NOTES.get("n:" + sub);
const [metaRaw, existing] = await Promise.all([
env.NOTES.get("m:" + sub),
env.NOTES.get("n:" + sub),
]);
if (existing === null) {
if (wantsJson(req, url)) return jsonError("not_found", "Not found", 404, { name: sub });
return notFoundPage(sub);
Expand All @@ -256,13 +264,15 @@ async function handleSubdomain(req, env, host, url) {
return editNotePage(sub, ttlKey);
}

const content = await env.NOTES.get("n:" + sub);
const [content, metaRaw] = await Promise.all([
env.NOTES.get("n:" + sub),
env.NOTES.get("m:" + sub),
]);
if (content === null) {
if (wantsJson(req, url)) return jsonError("not_found", "Not found", 404, { name: sub });
return notFoundPage(sub);
}

const metaRaw = await env.NOTES.get("m:" + sub);
let meta: any = {};
try { meta = metaRaw ? JSON.parse(metaRaw) : {}; } catch {}
const ttlKey = TTL_OPTIONS[meta.t] !== undefined ? meta.t : DEFAULT_TTL;
Expand Down