@hazem3500/palette 0.9.131 → 0.9.133

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/README.md +10 -6
  2. package/dist/adapters/css-variables.js +1 -1
  3. package/dist/adapters/tailwind.d.ts +1 -1
  4. package/dist/adapters/tailwind.js +1 -1
  5. package/dist/anchors.d.ts +2 -1
  6. package/dist/chunk-3SNXBRAS.js +1 -0
  7. package/dist/chunk-JWGDIIKJ.js +1 -0
  8. package/dist/{chunk-UUV356RI.js → chunk-KMRWUNT7.js} +1 -1
  9. package/dist/{chunk-RAAJF54I.js → chunk-OAGWNW4W.js} +1 -1
  10. package/dist/{color-editor-I5ELNYPO.js → color-editor-GCCLM3TV.js} +1 -1
  11. package/dist/design-comment-LHFSWNU7.js +1 -0
  12. package/dist/{design-export-55QSPEWM.js → design-export-I766FRDX.js} +1 -1
  13. package/dist/{design-geometry-OXGWR5J2.js → design-geometry-W7PSW5J2.js} +1 -1
  14. package/dist/design-mode.js +1 -1
  15. package/dist/design-system.d.ts +1 -1
  16. package/dist/frame-size-E6OGNHNX.js +1 -0
  17. package/dist/frame-size.d.ts +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/{inline-text-editor-S4LVCKZL.js → inline-text-editor-5JSAHEHE.js} +1 -1
  20. package/dist/message-links.d.ts +2 -0
  21. package/dist/node.mjs +100 -21
  22. package/dist/open-state.d.ts +7 -0
  23. package/dist/postgres/011_comment_api_key_name.sql +1 -0
  24. package/dist/postgres/012_comment_images.sql +25 -0
  25. package/dist/react-props-panel-P47YFFSU.js +1 -0
  26. package/dist/react-props-panel.d.ts +1 -1
  27. package/dist/react.js +1 -1
  28. package/dist/review-layer.d.ts +1 -0
  29. package/dist/server.js +97 -20
  30. package/dist/setup.js +1 -1
  31. package/dist/snapshot-CFKV3VWX.js +1 -0
  32. package/dist/snapshot-annotate-HW4ZWT7T.js +1 -0
  33. package/dist/snapshot-annotate.d.ts +16 -0
  34. package/dist/snapshot.d.ts +15 -4
  35. package/dist/thread-snapshot-2V4AV3UN.js +1 -0
  36. package/dist/thread-snapshot.d.ts +4 -6
  37. package/dist/token-adapter.d.ts +1 -0
  38. package/dist/{token-custom-7MJFPGVB.js → token-custom-CPPHGYMJ.js} +1 -1
  39. package/dist/{token-page-YQ52JS6G.js → token-page-63C3T4DY.js} +1 -1
  40. package/dist/{token-picker-HHT4OC3V.js → token-picker-ARVCFUCY.js} +1 -1
  41. package/dist/types.d.ts +4 -0
  42. package/package.json +1 -1
  43. package/server/bot-tokens.ts +7 -3
  44. package/server/index.ts +80 -17
  45. package/server/migrations/0025_comment_api_key_name.sql +1 -0
  46. package/server/migrations/0026_comment_images.sql +22 -0
  47. package/server/setup-client.txt +115 -79
  48. package/server/snapshots.ts +2 -1
  49. package/server/tokens.ts +2 -2
  50. package/src/types.ts +4 -0
  51. package/dist/chunk-EVRPBA4F.js +0 -1
  52. package/dist/design-comment-VXRH45UI.js +0 -1
  53. package/dist/frame-size-EAVSR4SA.js +0 -1
  54. package/dist/react-props-panel-VON2NO7N.js +0 -1
  55. package/dist/snapshot-BFHCCGOL.js +0 -1
  56. package/dist/thread-snapshot-TJHRZZOR.js +0 -1
  57. /package/dist/{design-panel-DBAUVCIJ.js → design-panel-GU2SWV33.js} +0 -0
package/server/index.ts CHANGED
@@ -95,6 +95,7 @@ type CommentRow = {
95
95
  verified: number;
96
96
  created_at: number;
97
97
  edited_at: number | null;
98
+ api_key_name?: string | null;
98
99
  };
99
100
  const identity = (row: UserRow): Identity => ({
100
101
  id: row.id,
@@ -306,7 +307,7 @@ async function authenticate(
306
307
  env: Env,
307
308
  project: string,
308
309
  access?: PatAccess
309
- ): Promise<Identity> {
310
+ ): Promise<Identity & { apiKeyName?: string }> {
310
311
  const bearer = request.headers
311
312
  .get("Authorization")
312
313
  ?.match(/^Bearer (.{1,200})$/)?.[1];
@@ -319,9 +320,8 @@ async function authenticate(
319
320
  403,
320
321
  "Access tokens are off behind the sign-in proxy. Use /auth/proxy/session."
321
322
  );
322
- return identity(
323
- await authenticateToken(env, bearer, project, access, request)
324
- );
323
+ const actor = await authenticateToken(env, bearer, project, access, request);
324
+ return { ...identity(actor), apiKeyName: actor.tokenName };
325
325
  }
326
326
  const now = Date.now();
327
327
  let row: UserRow | null;
@@ -1017,7 +1017,7 @@ async function route(
1017
1017
  "Unknown management route."
1018
1018
  );
1019
1019
  check(
1020
- ["GET", "POST", "PATCH", "DELETE"].includes(request.method),
1020
+ ["GET", "POST", "PUT", "PATCH", "DELETE"].includes(request.method),
1021
1021
  405,
1022
1022
  "Method not allowed."
1023
1023
  );
@@ -1201,6 +1201,7 @@ async function route(
1201
1201
  // Element snapshots are opt-in on the client; guests need project consent.
1202
1202
  // Clients may send `share` on new threads and edit `/me/settings`.
1203
1203
  share: true,
1204
+ commentImages: true,
1204
1205
  snapshots: {
1205
1206
  readiness: true,
1206
1207
  guests: auth.allowGuests && config.allowGuestSnapshots === true,
@@ -1520,6 +1521,15 @@ async function route(
1520
1521
  `${url.origin}${env.PALETTE_PUBLIC_PATH ?? ""}/threads/${item.thread_id}/snapshot/${item.kind}?${scope}`;
1521
1522
  snapshotsByThread.set(item.thread_id, group);
1522
1523
  }
1524
+ const imagesByComment = new Map<string, number>();
1525
+ try {
1526
+ const images = await env.DB.prepare(
1527
+ `SELECT i.comment_id,i.created_at FROM comment_images i JOIN comments c ON c.id=i.comment_id WHERE c.thread_id IN (${placeholders})`
1528
+ ).bind(...ids).all<{ comment_id: string; created_at: number }>();
1529
+ for (const image of images.results) imagesByComment.set(image.comment_id, image.created_at);
1530
+ } catch (error) {
1531
+ if (!(error instanceof Error) || !/no such table: comment_images|relation "comment_images" does not exist/.test(error.message)) throw error;
1532
+ }
1523
1533
  const commentsByThread = new Map<string, CommentRow[]>();
1524
1534
  for (const comment of comments.results) {
1525
1535
  const group = commentsByThread.get(comment.thread_id) ?? [];
@@ -1562,6 +1572,10 @@ async function route(
1562
1572
  avatarUrl: c.avatar_url || undefined,
1563
1573
  accentColor: c.accent_color || undefined,
1564
1574
  },
1575
+ ...(c.api_key_name ? { apiKeyName: c.api_key_name } : {}),
1576
+ ...(imagesByComment.has(c.id) ? {
1577
+ image: `${url.origin}${env.PALETTE_PUBLIC_PATH ?? ""}/threads/${c.thread_id}/comments/${c.id}/image?${new URLSearchParams({ project, repo, branch, v: String(imagesByComment.get(c.id)) })}`,
1578
+ } : {}),
1565
1579
  createdAt: c.created_at,
1566
1580
  editedAt: c.edited_at,
1567
1581
  reactions: reactionsByComment.get(c.id) ?? {},
@@ -1592,7 +1606,7 @@ async function route(
1592
1606
  revision,
1593
1607
  });
1594
1608
  }
1595
- const user = await authenticate(request, env, project, access);
1609
+ const { apiKeyName, ...user } = await authenticate(request, env, project, access);
1596
1610
  await limit(env, `${project}:session:${user.id}`, 60);
1597
1611
  const now = Date.now();
1598
1612
  if (url.pathname === "/threads" && request.method === "POST") {
@@ -1621,9 +1635,13 @@ async function route(
1621
1635
  : env.DB.prepare(
1622
1636
  "INSERT INTO threads(id,project,repo,branch,page,anchor,tags,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?)"
1623
1637
  ).bind(id, project, repo, branch, page, JSON.stringify(anchor), JSON.stringify(tags), now, now),
1624
- env.DB.prepare(
1625
- "INSERT INTO comments(id,thread_id,user_id,body,created_at) VALUES(?,?,?,?,?)"
1626
- ).bind(firstCommentId, id, user.id, text, now),
1638
+ apiKeyName
1639
+ ? env.DB.prepare(
1640
+ "INSERT INTO comments(id,thread_id,user_id,body,created_at,api_key_name) VALUES(?,?,?,?,?,?)"
1641
+ ).bind(firstCommentId, id, user.id, text, now, apiKeyName)
1642
+ : env.DB.prepare(
1643
+ "INSERT INTO comments(id,thread_id,user_id,body,created_at) VALUES(?,?,?,?,?)"
1644
+ ).bind(firstCommentId, id, user.id, text, now),
1627
1645
  env.DB.prepare("UPDATE threads SET snapshot_status=? WHERE id=?").bind(snapshotStatus, id),
1628
1646
  enqueueWebhook(env.DB, project, { ...webhookEvent(
1629
1647
  "comment.created",
@@ -1764,10 +1782,10 @@ async function route(
1764
1782
  );
1765
1783
  }
1766
1784
  const match = url.pathname.match(
1767
- /^\/threads\/([\w:-]{1,100})(?:\/comments(?:\/([\w:-]{1,100})(?:\/(reactions))?)?)?$/
1785
+ /^\/threads\/([\w:-]{1,100})(?:\/comments(?:\/([\w:-]{1,100})(?:\/(reactions|image))?)?)?$/
1768
1786
  );
1769
1787
  check(match, 404, "Not found.");
1770
- const [, threadId, commentId, reaction] = match;
1788
+ const [, threadId, commentId, action] = match;
1771
1789
  const thread = await env.DB.prepare(
1772
1790
  "SELECT * FROM threads WHERE id=? AND project=? AND repo=? AND branch=?"
1773
1791
  )
@@ -1883,9 +1901,13 @@ async function route(
1883
1901
  const text = string(data.body, 4000, "reply");
1884
1902
  const replyId = crypto.randomUUID();
1885
1903
  const result = await env.DB.batch([
1886
- env.DB.prepare(
1887
- "INSERT INTO comments(id,thread_id,user_id,body,created_at) SELECT ?,?,?,?,? WHERE (SELECT COUNT(*) FROM comments WHERE thread_id=?)<200"
1888
- ).bind(replyId, threadId, user.id, text, now, threadId),
1904
+ apiKeyName
1905
+ ? env.DB.prepare(
1906
+ "INSERT INTO comments(id,thread_id,user_id,body,created_at,api_key_name) SELECT ?,?,?,?,?,? WHERE (SELECT COUNT(*) FROM comments WHERE thread_id=?)<200"
1907
+ ).bind(replyId, threadId, user.id, text, now, apiKeyName, threadId)
1908
+ : env.DB.prepare(
1909
+ "INSERT INTO comments(id,thread_id,user_id,body,created_at) SELECT ?,?,?,?,? WHERE (SELECT COUNT(*) FROM comments WHERE thread_id=?)<200"
1910
+ ).bind(replyId, threadId, user.id, text, now, threadId),
1889
1911
  env.DB.prepare("UPDATE threads SET updated_at=? WHERE id=?").bind(
1890
1912
  now,
1891
1913
  threadId
@@ -1924,7 +1946,47 @@ async function route(
1924
1946
  .bind(commentId, threadId)
1925
1947
  .first<CommentRow>();
1926
1948
  check(comment, 404, "Comment not found.");
1927
- if (reaction && request.method === "POST") {
1949
+ if (action === "image") {
1950
+ check(comment.body !== "[Comment deleted]", 404, "Comment not found.");
1951
+ if (request.method === "GET") {
1952
+ const image = await env.DB.prepare("SELECT mime,bytes FROM comment_images WHERE comment_id=?")
1953
+ .bind(commentId).first<{ mime: string; bytes: unknown }>();
1954
+ check(image, 404, "Image not found.");
1955
+ const bytes = blobBytes(image.bytes);
1956
+ return new Response(bytes as Uint8Array<ArrayBuffer>, {
1957
+ headers: {
1958
+ "Content-Type": image.mime,
1959
+ "X-Content-Type-Options": "nosniff",
1960
+ "Content-Security-Policy": "sandbox; default-src 'none'",
1961
+ "Cache-Control": "private, max-age=300",
1962
+ },
1963
+ });
1964
+ }
1965
+ check(request.method === "PUT" || request.method === "DELETE", 405, "Method not allowed.");
1966
+ check(comment.user_id === user.id, 403, "Only the author can change this image.");
1967
+ if (request.method === "DELETE") {
1968
+ await env.DB.batch([
1969
+ env.DB.prepare("DELETE FROM comment_images WHERE comment_id=?").bind(commentId),
1970
+ env.DB.prepare("UPDATE threads SET updated_at=? WHERE id=?").bind(now, threadId),
1971
+ ]);
1972
+ return json({ ok: true });
1973
+ }
1974
+ check(user.verified || (auth.allowGuests && config.allowGuestSnapshots === true),
1975
+ 403, "Guests cannot attach images in this project.");
1976
+ await limit(env, `${project}:snapshot:${user.id}`, 60, 600);
1977
+ const image = await readSnapshot(request, "image");
1978
+ const stored = await env.DB.prepare(
1979
+ `INSERT INTO comment_images(comment_id,project,mime,bytes,size,created_at)
1980
+ SELECT ?,?,?,?,?,? WHERE EXISTS (SELECT 1 FROM comments WHERE id=? AND body<>'[Comment deleted]')
1981
+ AND (SELECT COALESCE(SUM(size),0) FROM comment_images WHERE project=? AND comment_id<>?)
1982
+ + (SELECT COALESCE(SUM(s.size),0) FROM snapshots s JOIN threads t ON t.id=s.thread_id WHERE t.project=?)
1983
+ + ? <= ? ON CONFLICT(comment_id) DO UPDATE SET mime=excluded.mime,bytes=excluded.bytes,size=excluded.size,created_at=excluded.created_at`
1984
+ ).bind(commentId, project, image.mime, image.bytes, image.bytes.length, now, commentId,
1985
+ project, commentId, project, image.bytes.length, snapshotCap(config.maxSnapshotBytes)).run();
1986
+ check(stored.meta.changes, 409, "Could not attach the image. Check the comment and the project storage limit.");
1987
+ return json({ ok: true }, 201);
1988
+ }
1989
+ if (action === "reactions" && request.method === "POST") {
1928
1990
  const data = await body(request);
1929
1991
  check(
1930
1992
  isEmoji(data.emoji) && typeof data.active === "boolean",
@@ -1954,7 +2016,7 @@ async function route(
1954
2016
  return json({ ok: true });
1955
2017
  }
1956
2018
  check(
1957
- !reaction && comment.user_id === user.id,
2019
+ !action && comment.user_id === user.id,
1958
2020
  403,
1959
2021
  "Only the author can change this comment."
1960
2022
  );
@@ -1981,6 +2043,7 @@ async function route(
1981
2043
  now,
1982
2044
  commentId
1983
2045
  ),
2046
+ env.DB.prepare("DELETE FROM comment_images WHERE comment_id=?").bind(commentId),
1984
2047
  env.DB.prepare("DELETE FROM reactions WHERE comment_id=?").bind(
1985
2048
  commentId
1986
2049
  ),
@@ -2094,7 +2157,7 @@ export default {
2094
2157
  headers.set("Access-Control-Allow-Origin", origin);
2095
2158
  headers.set(
2096
2159
  "Access-Control-Allow-Methods",
2097
- "GET, POST, PATCH, DELETE, OPTIONS"
2160
+ "GET, POST, PUT, PATCH, DELETE, OPTIONS"
2098
2161
  );
2099
2162
  headers.set(
2100
2163
  "Access-Control-Allow-Headers",
@@ -0,0 +1 @@
1
+ ALTER TABLE comments ADD COLUMN api_key_name TEXT;
@@ -0,0 +1,22 @@
1
+ CREATE TABLE comment_images (
2
+ comment_id TEXT PRIMARY KEY REFERENCES comments(id) ON DELETE CASCADE,
3
+ project TEXT NOT NULL,
4
+ mime TEXT NOT NULL,
5
+ bytes BLOB NOT NULL,
6
+ size INTEGER NOT NULL,
7
+ created_at INTEGER NOT NULL
8
+ );
9
+ CREATE INDEX comment_images_by_project ON comment_images(project);
10
+ CREATE TRIGGER comment_image_quota_insert AFTER INSERT ON comment_images BEGIN
11
+ UPDATE project_quotas SET bytes=bytes+NEW.size+256 WHERE project=NEW.project;
12
+ SELECT RAISE(ABORT, 'komo_quota_exceeded') WHERE EXISTS(SELECT 1 FROM project_quotas WHERE project=NEW.project AND bytes>max_bytes);
13
+ UPDATE threads SET updated_at=MAX(updated_at,NEW.created_at) WHERE id=(SELECT thread_id FROM comments WHERE id=NEW.comment_id);
14
+ END;
15
+ CREATE TRIGGER comment_image_quota_update AFTER UPDATE OF size ON comment_images BEGIN
16
+ UPDATE project_quotas SET bytes=MAX(0,bytes+NEW.size-OLD.size) WHERE project=NEW.project;
17
+ SELECT RAISE(ABORT, 'komo_quota_exceeded') WHERE EXISTS(SELECT 1 FROM project_quotas WHERE project=NEW.project AND bytes>max_bytes);
18
+ UPDATE threads SET updated_at=MAX(updated_at,NEW.created_at) WHERE id=(SELECT thread_id FROM comments WHERE id=NEW.comment_id);
19
+ END;
20
+ CREATE TRIGGER comment_image_quota_delete AFTER DELETE ON comment_images BEGIN
21
+ UPDATE project_quotas SET bytes=MAX(0,bytes-OLD.size-256) WHERE project=OLD.project;
22
+ END;