@jant/core 1.1.0 → 1.3.0

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 (196) hide show
  1. package/bin/commands/site/import.js +2 -1
  2. package/bin/lib/site-snapshot.js +10 -0
  3. package/dist/{app-CKQ9PqAr.js → app-C8dETvgL.js} +9267 -5697
  4. package/dist/client/.vite/manifest.json +82 -93
  5. package/dist/client/_assets/chunks/{confirm-CMe01lne.js → confirm-BQebcuNB.js} +1 -1
  6. package/dist/client/_assets/chunks/{create-editor-BQkrw8Ax.js → create-editor-B8KuszlS.js} +38 -38
  7. package/dist/client/_assets/chunks/footnote-rail-DUrqz0Py.js +2 -0
  8. package/dist/client/_assets/chunks/{jant-locale-picker-2L4rkDaz.js → jant-locale-picker-CLq0iKDH.js} +1 -1
  9. package/dist/client/_assets/chunks/lit-BN4mrI3j.js +2 -0
  10. package/dist/client/_assets/chunks/{settings-paths-B0bNUaih.js → settings-paths-_fry4yWW.js} +1 -1
  11. package/dist/client/_assets/chunks/{slug-format-CYmQE5ln.js → slug-format-bcJ2ozr4.js} +1 -1
  12. package/dist/client/_assets/chunks/sortable-list-Bz2Na2E9.js +2 -0
  13. package/dist/client/_assets/chunks/types-DrYFqJH2.js +1 -0
  14. package/dist/client/_assets/chunks/{unsafe-html-CWos3v6N.js → unsafe-html-OJ9m154w.js} +1 -1
  15. package/dist/client/_assets/chunks/unsafe-svg-DVJ4NPcb.js +1 -0
  16. package/dist/client/_assets/client-CoNHvipV.css +2 -0
  17. package/dist/client/_assets/{client-auth-CG55rWb5.js → client-auth-CbnZkLJ6.js} +99 -129
  18. package/dist/client/_assets/{client-author-CGVvrNZ8.css → client-author-2WYBkgni.css} +1 -1
  19. package/dist/client/_assets/client-compose-DYtDlYm-.js +2008 -0
  20. package/dist/client/_assets/{client-manage-DnYdQAhf.js → client-manage-BODyYP5Z.js} +157 -157
  21. package/dist/client/_assets/client-pq6sVS7y.js +354 -0
  22. package/dist/client/_assets/{client-settings-CHgsIqXJ.js → client-settings-DDpe_tXJ.js} +166 -166
  23. package/dist/client/_assets/client-share-B9OsXx9p.js +149 -0
  24. package/dist/{constants-DEgyqWDl.js → constants-C2CdWAqt.js} +55 -5
  25. package/dist/index.js +1 -1
  26. package/dist/node.js +3 -2
  27. package/package.json +2 -1
  28. package/src/app.tsx +9 -0
  29. package/src/client/components/compose-types.ts +6 -1
  30. package/src/client/components/jant-compose-dialog.ts +26 -4
  31. package/src/client/components/jant-compose-editor.ts +30 -0
  32. package/src/client/components/jant-compose-fullscreen.ts +1 -0
  33. package/src/client/components/jant-media-lightbox.ts +18 -3
  34. package/src/client/components/jant-nav-manager.ts +2 -0
  35. package/src/client/components/jant-post-menu.ts +42 -89
  36. package/src/client/components/nav-manager-types.ts +2 -0
  37. package/src/client/components/settings-types.ts +1 -1
  38. package/src/client/compose-bridge.ts +61 -1
  39. package/src/client/feed-video-player.ts +6 -0
  40. package/src/client/list-views.ts +382 -0
  41. package/src/client/media-lightbox-events.ts +12 -0
  42. package/src/client/runtime-paths.ts +12 -0
  43. package/src/client/share-capture.ts +63 -0
  44. package/src/client/share-launch.ts +36 -9
  45. package/src/client/tiptap/bubble-menu.ts +25 -2
  46. package/src/client/tiptap/create-editor.ts +6 -0
  47. package/src/client/tiptap/embed-node.ts +3 -1
  48. package/src/client/tiptap/extensions.ts +4 -0
  49. package/src/client/tiptap/html-block-node.ts +3 -1
  50. package/src/client/tiptap/image-node.ts +3 -1
  51. package/src/client/video-processor.ts +106 -52
  52. package/src/client/view-confirm.ts +136 -0
  53. package/src/client.ts +2 -0
  54. package/src/db/migrations/0035_brainy_next_avengers.sql +9 -0
  55. package/src/db/migrations/0036_gorgeous_smasher.sql +7 -0
  56. package/src/db/migrations/0037_round_post.sql +16 -0
  57. package/src/db/migrations/0038_flashy_changeling.sql +15 -0
  58. package/src/db/migrations/0039_mixed_sister_grimm.sql +1 -0
  59. package/src/db/migrations/meta/0035_snapshot.json +2877 -0
  60. package/src/db/migrations/meta/0036_snapshot.json +2924 -0
  61. package/src/db/migrations/meta/0037_snapshot.json +2932 -0
  62. package/src/db/migrations/meta/0038_snapshot.json +2924 -0
  63. package/src/db/migrations/meta/0039_snapshot.json +2931 -0
  64. package/src/db/migrations/meta/_journal.json +35 -0
  65. package/src/db/migrations/pg/0033_cute_magdalene.sql +10 -0
  66. package/src/db/migrations/pg/0034_material_agent_brand.sql +8 -0
  67. package/src/db/migrations/pg/0035_flimsy_outlaw_kid.sql +3 -0
  68. package/src/db/migrations/pg/0036_milky_wolverine.sql +5 -0
  69. package/src/db/migrations/pg/0037_fuzzy_hammerhead.sql +1 -0
  70. package/src/db/migrations/pg/meta/0033_snapshot.json +3719 -0
  71. package/src/db/migrations/pg/meta/0034_snapshot.json +3766 -0
  72. package/src/db/migrations/pg/meta/0035_snapshot.json +3773 -0
  73. package/src/db/migrations/pg/meta/0036_snapshot.json +3766 -0
  74. package/src/db/migrations/pg/meta/0037_snapshot.json +3772 -0
  75. package/src/db/migrations/pg/meta/_journal.json +35 -0
  76. package/src/db/pg/schema.ts +41 -0
  77. package/src/db/post-visibility.ts +37 -6
  78. package/src/db/schema.ts +64 -0
  79. package/src/i18n/locales/glossary.zh-Hans.yml +4 -0
  80. package/src/i18n/locales/glossary.zh-Hant.yml +4 -0
  81. package/src/i18n/locales/public/en.po +26 -7
  82. package/src/i18n/locales/public/en.ts +1 -1
  83. package/src/i18n/locales/public/zh-Hans.po +25 -6
  84. package/src/i18n/locales/public/zh-Hans.ts +1 -1
  85. package/src/i18n/locales/public/zh-Hant.po +25 -6
  86. package/src/i18n/locales/public/zh-Hant.ts +1 -1
  87. package/src/i18n/locales/settings/en.po +159 -0
  88. package/src/i18n/locales/settings/en.ts +1 -1
  89. package/src/i18n/locales/settings/zh-Hans.po +159 -0
  90. package/src/i18n/locales/settings/zh-Hans.ts +1 -1
  91. package/src/i18n/locales/settings/zh-Hant.po +159 -0
  92. package/src/i18n/locales/settings/zh-Hant.ts +1 -1
  93. package/src/lib/api-public-posts.ts +10 -0
  94. package/src/lib/env.ts +20 -0
  95. package/src/lib/feed-policy.ts +2 -2
  96. package/src/lib/filter-dimensions.ts +11 -4
  97. package/src/lib/github-app-state.ts +1 -1
  98. package/src/lib/github-app.ts +183 -1
  99. package/src/lib/hugo-markdown.ts +7 -0
  100. package/src/lib/icons.ts +2 -0
  101. package/src/lib/link-preview.ts +499 -0
  102. package/src/lib/list-views.ts +117 -0
  103. package/src/lib/markdown-manager.ts +169 -2
  104. package/src/lib/navigation.ts +1 -1
  105. package/src/lib/post-meta.ts +13 -3
  106. package/src/lib/referrer-aliases.ts +311 -0
  107. package/src/lib/render.tsx +10 -0
  108. package/src/lib/resolve-config.ts +14 -0
  109. package/src/lib/share-card.ts +4 -3
  110. package/src/lib/summary.ts +152 -54
  111. package/src/lib/thread-query.ts +6 -2
  112. package/src/lib/timeline.ts +12 -11
  113. package/src/lib/tiptap-render.ts +1 -0
  114. package/src/lib/upload.ts +21 -1
  115. package/src/lib/url-fetch.ts +291 -74
  116. package/src/lib/video-playback.ts +55 -0
  117. package/src/lib/view-buffer.ts +201 -0
  118. package/src/lib/view-counting.ts +540 -0
  119. package/src/lib/view-token.ts +199 -0
  120. package/src/lib/view.ts +30 -1
  121. package/src/middleware/view-counter.ts +145 -0
  122. package/src/node/runtime.ts +4 -0
  123. package/src/preset.css +36 -17
  124. package/src/routes/api/media.ts +21 -3
  125. package/src/routes/api/search.ts +1 -1
  126. package/src/routes/dash/settings.tsx +434 -169
  127. package/src/routes/feed/sitemap.ts +1 -1
  128. package/src/routes/pages/archive.tsx +26 -19
  129. package/src/routes/pages/collection.tsx +9 -3
  130. package/src/routes/pages/collections.tsx +1 -0
  131. package/src/routes/pages/featured.tsx +1 -0
  132. package/src/routes/pages/home.tsx +1 -0
  133. package/src/routes/pages/page.tsx +18 -3
  134. package/src/routes/pages/search.tsx +3 -3
  135. package/src/routes/pages/smart-collection.tsx +2 -0
  136. package/src/routes/pages/subscribe.tsx +1 -0
  137. package/src/routes/view-confirm.ts +71 -0
  138. package/src/services/collection.ts +1 -1
  139. package/src/services/export-theme/assets/client-site.js +19 -19
  140. package/src/services/export-theme/layouts/archive/list.html +4 -2
  141. package/src/services/export-theme/layouts/partials/collection-members.html +4 -3
  142. package/src/services/export-theme/layouts/partials/featured-members.html +3 -3
  143. package/src/services/export-theme/layouts/partials/head.html +2 -0
  144. package/src/services/export-theme/layouts/partials/latest-members.html +4 -4
  145. package/src/services/export-theme/layouts/partials/post-card.html +1 -9
  146. package/src/services/export-theme/layouts/partials/reply.html +1 -9
  147. package/src/services/export-theme/layouts/partials/smart-collection-members.html +4 -3
  148. package/src/services/export-theme/layouts/post/list.html +1 -9
  149. package/src/services/export-theme/styles/main.css +41 -47
  150. package/src/services/export.ts +12 -3
  151. package/src/services/index.ts +17 -0
  152. package/src/services/mcp.ts +36 -6
  153. package/src/services/media.ts +63 -0
  154. package/src/services/navigation.ts +9 -4
  155. package/src/services/post.ts +102 -112
  156. package/src/services/search.ts +22 -7
  157. package/src/services/smart-collection.ts +3 -3
  158. package/src/services/stats.ts +1137 -0
  159. package/src/services/thread.ts +6 -2
  160. package/src/styles/components-author.css +242 -3
  161. package/src/styles/components.css +11 -3
  162. package/src/styles/tokens.css +89 -55
  163. package/src/styles/ui-author.css +140 -353
  164. package/src/styles/ui.css +90 -45
  165. package/src/types/app-context.ts +9 -0
  166. package/src/types/bindings.ts +5 -0
  167. package/src/types/config.ts +54 -0
  168. package/src/types/constants.ts +68 -4
  169. package/src/types/entities.ts +1 -0
  170. package/src/types/views.ts +8 -0
  171. package/src/ui/compose/ComposeDialog.tsx +26 -1
  172. package/src/ui/dash/StatusBadge.tsx +11 -0
  173. package/src/ui/dash/appearance/FontThemeContent.tsx +1 -1
  174. package/src/ui/dash/appearance/NavigationContent.tsx +8 -0
  175. package/src/ui/dash/settings/SettingsRootContent.tsx +24 -6
  176. package/src/ui/dash/settings/StatsContent.tsx +855 -0
  177. package/src/ui/feed/LinkCard.tsx +19 -2
  178. package/src/ui/feed/PostStatusBadges.tsx +19 -3
  179. package/src/ui/feed/TimelineFeed.tsx +3 -0
  180. package/src/ui/font-themes.ts +56 -6
  181. package/src/ui/layouts/BaseLayout.tsx +4 -2
  182. package/src/ui/layouts/SiteLayout.tsx +8 -3
  183. package/src/ui/pages/ArchivePage.tsx +1 -0
  184. package/src/ui/shared/MediaGallery.tsx +19 -3
  185. package/src/ui/shared/PostFooter.tsx +1 -22
  186. package/src/ui/shared/custom-icons.ts +5 -5
  187. package/dist/client/_assets/chunks/footnote-rail-B08scyhW.js +0 -2
  188. package/dist/client/_assets/chunks/lit-Bw_voPoW.js +0 -2
  189. package/dist/client/_assets/chunks/runtime-paths-y0_gHowM.js +0 -1
  190. package/dist/client/_assets/chunks/sortable-list-t11Ma1Ek.js +0 -2
  191. package/dist/client/_assets/chunks/types-D5iF3H1a.js +0 -1
  192. package/dist/client/_assets/chunks/unsafe-svg-DZl-yjas.js +0 -1
  193. package/dist/client/_assets/client-B0-s9sqx.css +0 -2
  194. package/dist/client/_assets/client-Dp46QSht.js +0 -354
  195. package/dist/client/_assets/client-compose-DkUTuavR.js +0 -2004
  196. package/dist/client/_assets/client-share-C59mOLJ1.js +0 -149
@@ -1,19 +1,19 @@
1
1
  /**
2
- * Safe remote URL fetching for server-side image sideloading.
2
+ * Safe remote URL fetching on the server.
3
3
  *
4
- * When an author pastes an article from another site, its `<img>` tags point at
5
- * remote URLs. To rehost those images into the site's own storage the server
6
- * must fetch the bytes itself (a browser `fetch` of a third-party image is
7
- * blocked by CORS for most hosts). Because the URL comes from pasted HTML it is
8
- * attacker-influenced, so every fetch passes through an SSRF guard and a bounded
9
- * reader that caps size and time.
4
+ * Two callers fetch URLs an author supplied. Image sideloading rehosts the
5
+ * `<img>` sources of an article pasted from another site (a browser `fetch` of
6
+ * a third-party image is blocked by CORS for most hosts). Link previews read
7
+ * the `<head>` of the page a Link post points at, and the images it names.
8
+ * Either URL is attacker-influenced, so every fetch passes through an SSRF
9
+ * guard and a bounded reader that caps size and time.
10
10
  *
11
11
  * Two layers guard the address. {@link assertPublicHttpUrl} refuses a URL whose
12
12
  * host is a private name or IP literal, in any of the forms an IPv6 address can
13
- * carry an IPv4 one. On Node, {@link fetchImageBytes} also resolves each host
14
- * and refuses it when any address it resolves to is private, which catches
15
- * names like `127.0.0.1.nip.io`. A Worker's fetch can't reach a private network
16
- * at all, so it skips the lookup. A host that answers a public address to the
13
+ * carry an IPv4 one. On Node, every fetch here also resolves each host and
14
+ * refuses it when any address it resolves to is private, which catches names
15
+ * like `127.0.0.1.nip.io`. A Worker's fetch can't reach a private network at
16
+ * all, so it skips the lookup. A host that answers a public address to the
17
17
  * check and a private one to the fetch a moment later (DNS rebinding) is out of
18
18
  * reach of both; that needs network-level egress control.
19
19
  */
@@ -21,9 +21,53 @@
21
21
  import { ValidationError } from "./errors.js";
22
22
 
23
23
  /** A browser-like UA — many CDNs serving article images block unknown bots. */
24
- const FETCH_USER_AGENT =
24
+ const IMAGE_USER_AGENT =
25
25
  "Mozilla/5.0 (compatible; Jant image sideloader) AppleWebKit/537.36";
26
26
 
27
+ /** The UA a page is read with for a Link preview. */
28
+ const PAGE_USER_AGENT =
29
+ "Mozilla/5.0 (compatible; Jant link preview; +https://jant.me) AppleWebKit/537.36";
30
+
31
+ /** What a fetch is for, which names it in the errors it throws. */
32
+ export type FetchTarget = "image" | "page";
33
+
34
+ const FETCH_ERRORS: Record<
35
+ FetchTarget,
36
+ {
37
+ invalid: string;
38
+ protocol: string;
39
+ credentials: string;
40
+ privateAddress: string;
41
+ unresolved: string;
42
+ timeout: string;
43
+ failed: string;
44
+ redirects: string;
45
+ }
46
+ > = {
47
+ image: {
48
+ invalid: "That doesn't look like a valid image URL.",
49
+ protocol: "Only http and https image URLs can be fetched.",
50
+ credentials: "Image URLs can't include credentials.",
51
+ privateAddress: "That image URL points to a private address.",
52
+ unresolved: "Couldn't find the image's host.",
53
+ timeout: "Timed out fetching the image.",
54
+ failed: "Couldn't fetch the image",
55
+ redirects: "Too many redirects fetching the image.",
56
+ },
57
+ page: {
58
+ invalid: "That doesn't look like a valid URL.",
59
+ protocol: "Only http and https pages can be fetched.",
60
+ credentials: "Page URLs can't include credentials.",
61
+ privateAddress: "That URL points to a private address.",
62
+ unresolved: "Couldn't find the page's host.",
63
+ timeout: "Timed out fetching the page.",
64
+ failed: "Couldn't fetch the page",
65
+ redirects: "Too many redirects fetching the page.",
66
+ },
67
+ };
68
+
69
+ const IMAGE_TOO_LARGE = "That image is too large.";
70
+
27
71
  export interface FetchedImage {
28
72
  bytes: Uint8Array;
29
73
  /** Lowercased content-type with parameters stripped, or null if absent. */
@@ -39,6 +83,22 @@ export interface FetchImageBytesOptions {
39
83
  maxRedirects?: number;
40
84
  }
41
85
 
86
+ export interface FetchedHtmlHead {
87
+ /** The document up to its `</head>` or `<body>`, or up to `maxBytes`. */
88
+ html: string;
89
+ /** The URL the document came from, after redirects. */
90
+ url: URL;
91
+ }
92
+
93
+ export interface FetchHtmlHeadOptions {
94
+ /** Stop reading (without failing) once this many bytes have arrived. */
95
+ maxBytes: number;
96
+ /** Abort the whole request after this many milliseconds. */
97
+ timeoutMs: number;
98
+ /** Maximum redirect hops to follow (each re-validated). Default 5. */
99
+ maxRedirects?: number;
100
+ }
101
+
42
102
  /**
43
103
  * Validate that a string is a public http(s) URL safe to fetch server-side.
44
104
  *
@@ -47,25 +107,30 @@ export interface FetchImageBytesOptions {
47
107
  * literals (including the `169.254.169.254` cloud-metadata address).
48
108
  *
49
109
  * @param raw - The candidate URL string
110
+ * @param target - What the URL is fetched for, which names it in the error
50
111
  * @returns The parsed {@link URL}
51
112
  * @example
52
113
  * ```ts
53
114
  * const url = assertPublicHttpUrl("https://example.com/photo.jpg");
54
115
  * ```
55
116
  */
56
- export function assertPublicHttpUrl(raw: string): URL {
117
+ export function assertPublicHttpUrl(
118
+ raw: string,
119
+ target: FetchTarget = "image",
120
+ ): URL {
121
+ const errors = FETCH_ERRORS[target];
57
122
  let url: URL;
58
123
  try {
59
124
  url = new URL(raw);
60
125
  } catch {
61
- throw new ValidationError("That doesn't look like a valid image URL.");
126
+ throw new ValidationError(errors.invalid);
62
127
  }
63
128
 
64
129
  if (url.protocol !== "http:" && url.protocol !== "https:") {
65
- throw new ValidationError("Only http and https image URLs can be fetched.");
130
+ throw new ValidationError(errors.protocol);
66
131
  }
67
132
  if (url.username || url.password) {
68
- throw new ValidationError("Image URLs can't include credentials.");
133
+ throw new ValidationError(errors.credentials);
69
134
  }
70
135
 
71
136
  // A trailing dot names the same host (`localhost.` is `localhost`).
@@ -75,7 +140,7 @@ export function assertPublicHttpUrl(raw: string): URL {
75
140
  host.endsWith(".localhost") ||
76
141
  isPrivateAddress(host)
77
142
  ) {
78
- throw new ValidationError("That image URL points to a private address.");
143
+ throw new ValidationError(errors.privateAddress);
79
144
  }
80
145
 
81
146
  return url;
@@ -87,6 +152,7 @@ export function assertPublicHttpUrl(raw: string): URL {
87
152
  * Skipped on Cloudflare Workers, whose fetch can't reach a private network.
88
153
  *
89
154
  * @param url - A URL already accepted by {@link assertPublicHttpUrl}
155
+ * @param target - What the URL is fetched for, which names it in the error
90
156
  * @throws {ValidationError} When the host resolves to a private address or
91
157
  * doesn't resolve
92
158
  * @example
@@ -94,23 +160,40 @@ export function assertPublicHttpUrl(raw: string): URL {
94
160
  * await assertResolvesPublic(new URL("https://127.0.0.1.nip.io/a.png")); // throws
95
161
  * ```
96
162
  */
97
- export async function assertResolvesPublic(url: URL): Promise<void> {
163
+ export async function assertResolvesPublic(
164
+ url: URL,
165
+ target: FetchTarget = "image",
166
+ ): Promise<void> {
98
167
  if (isCloudflareWorker()) return;
99
168
  const host = url.hostname.replace(/^\[|\]$/g, "");
100
169
  if (isIpLiteral(host)) return; // Already checked by assertPublicHttpUrl.
101
170
 
102
- const { lookup } = await import("node:dns/promises");
171
+ const errors = FETCH_ERRORS[target];
172
+ const lookup = await loadDnsLookup();
103
173
  let addresses: { address: string }[];
104
174
  try {
105
175
  addresses = await lookup(host, { all: true, verbatim: true });
106
176
  } catch {
107
- throw new ValidationError("Couldn't find the image's host.");
177
+ throw new ValidationError(errors.unresolved);
108
178
  }
109
179
  if (addresses.some(({ address }) => isPrivateAddress(address))) {
110
- throw new ValidationError("That image URL points to a private address.");
180
+ throw new ValidationError(errors.privateAddress);
111
181
  }
112
182
  }
113
183
 
184
+ type DnsLookup = (typeof import("node:dns/promises"))["lookup"];
185
+ let dnsLookup: Promise<DnsLookup> | undefined;
186
+
187
+ /**
188
+ * `node:dns/promises`, imported once. A preview resolves its thumbnail and
189
+ * its icon side by side, so two lookups often start together; they share one
190
+ * import rather than each starting their own.
191
+ */
192
+ function loadDnsLookup(): Promise<DnsLookup> {
193
+ dnsLookup ??= import("node:dns/promises").then(({ lookup }) => lookup);
194
+ return dnsLookup;
195
+ }
196
+
114
197
  function isCloudflareWorker(): boolean {
115
198
  return (
116
199
  typeof navigator !== "undefined" &&
@@ -122,6 +205,67 @@ function isIpLiteral(host: string): boolean {
122
205
  return /^\d{1,3}(\.\d{1,3}){3}$/.test(host) || host.includes(":");
123
206
  }
124
207
 
208
+ /**
209
+ * GET a URL, following redirects by hand so that every hop passes the SSRF
210
+ * guard, and return the first response that isn't a redirect.
211
+ */
212
+ async function fetchFollowingPublicRedirects(
213
+ startUrl: URL,
214
+ input: {
215
+ target: FetchTarget;
216
+ headers: (url: URL) => Record<string, string>;
217
+ signal: AbortController["signal"];
218
+ maxRedirects: number;
219
+ },
220
+ ): Promise<{ response: Response; url: URL }> {
221
+ const errors = FETCH_ERRORS[input.target];
222
+ let url = startUrl;
223
+ let response: Response | null = null;
224
+
225
+ for (let hop = 0; hop <= input.maxRedirects; hop++) {
226
+ await assertResolvesPublic(url, input.target);
227
+ response = await fetch(url.toString(), {
228
+ method: "GET",
229
+ redirect: "manual",
230
+ signal: input.signal,
231
+ headers: input.headers(url),
232
+ }).catch((error) => {
233
+ if (input.signal.aborted) {
234
+ throw new ValidationError(errors.timeout);
235
+ }
236
+ throw new ValidationError(
237
+ error instanceof Error
238
+ ? `${errors.failed}: ${error.message}`
239
+ : `${errors.failed}.`,
240
+ );
241
+ });
242
+
243
+ if (response.status >= 300 && response.status < 400) {
244
+ const location = response.headers.get("location");
245
+ if (!location) break; // No target — fall through to the (failing) checks.
246
+ if (hop === input.maxRedirects) {
247
+ throw new ValidationError(errors.redirects);
248
+ }
249
+ // Re-validate every hop so a redirect can't escape the SSRF guard.
250
+ url = assertPublicHttpUrl(
251
+ new URL(location, url).toString(),
252
+ input.target,
253
+ );
254
+ continue;
255
+ }
256
+ break;
257
+ }
258
+
259
+ if (!response) {
260
+ throw new ValidationError(`${errors.failed}.`);
261
+ }
262
+ if (!response.ok) {
263
+ await response.body?.cancel().catch(() => {});
264
+ throw new ValidationError(`${errors.failed} (HTTP ${response.status}).`);
265
+ }
266
+ return { response, url };
267
+ }
268
+
125
269
  /**
126
270
  * Fetch a remote image with an SSRF-checked redirect chain, a size cap, and a
127
271
  * timeout. Reads the body in chunks and aborts the moment it exceeds `maxBytes`
@@ -142,64 +286,29 @@ export async function fetchImageBytes(
142
286
  startUrl: URL,
143
287
  options: FetchImageBytesOptions,
144
288
  ): Promise<FetchedImage> {
145
- const maxRedirects = options.maxRedirects ?? 3;
146
289
  const controller = new AbortController();
147
290
  const timer = setTimeout(() => controller.abort(), options.timeoutMs);
148
291
 
149
292
  try {
150
- let url = startUrl;
151
- let response: Response | null = null;
152
-
153
- for (let hop = 0; hop <= maxRedirects; hop++) {
154
- await assertResolvesPublic(url);
155
- response = await fetch(url.toString(), {
156
- method: "GET",
157
- redirect: "manual",
158
- signal: controller.signal,
159
- headers: {
160
- Accept: "image/*,*/*;q=0.8",
161
- "User-Agent": FETCH_USER_AGENT,
162
- // Many CDNs (Douban, WeChat, etc.) reject hotlinked image requests
163
- // that lack a Referer. Sending the image's own origin satisfies the
164
- // common "referer must be same-site" hotlink check.
165
- Referer: `${url.origin}/`,
166
- },
167
- }).catch((error) => {
168
- if (controller.signal.aborted) {
169
- throw new ValidationError("Timed out fetching the image.");
170
- }
171
- throw new ValidationError(
172
- error instanceof Error
173
- ? `Couldn't fetch the image: ${error.message}`
174
- : "Couldn't fetch the image.",
175
- );
176
- });
177
-
178
- if (response.status >= 300 && response.status < 400) {
179
- const location = response.headers.get("location");
180
- if (!location) break; // No target — fall through to the (failing) checks.
181
- if (hop === maxRedirects) {
182
- throw new ValidationError("Too many redirects fetching the image.");
183
- }
184
- // Re-validate every hop so a redirect can't escape the SSRF guard.
185
- url = assertPublicHttpUrl(new URL(location, url).toString());
186
- continue;
187
- }
188
- break;
189
- }
190
-
191
- if (!response) {
192
- throw new ValidationError("Couldn't fetch the image.");
193
- }
194
- if (!response.ok) {
195
- throw new ValidationError(
196
- `Couldn't fetch the image (HTTP ${response.status}).`,
197
- );
198
- }
293
+ const { response } = await fetchFollowingPublicRedirects(startUrl, {
294
+ target: "image",
295
+ signal: controller.signal,
296
+ maxRedirects: options.maxRedirects ?? 3,
297
+ headers: (url) => ({
298
+ Accept: "image/*,*/*;q=0.8",
299
+ "User-Agent": IMAGE_USER_AGENT,
300
+ // Many CDNs (Douban, WeChat, Bilibili, etc.) reject hotlinked image
301
+ // requests that lack a Referer or carry a foreign one. Sending the
302
+ // image's own origin satisfies the common "referer must be same-site"
303
+ // hotlink check.
304
+ Referer: `${url.origin}/`,
305
+ }),
306
+ });
199
307
 
200
308
  const declared = Number(response.headers.get("content-length"));
201
309
  if (Number.isFinite(declared) && declared > options.maxBytes) {
202
- throw new ValidationError("That image is too large.");
310
+ await response.body?.cancel().catch(() => {});
311
+ throw new ValidationError(IMAGE_TOO_LARGE);
203
312
  }
204
313
 
205
314
  const contentType = normalizeContentType(
@@ -212,6 +321,114 @@ export async function fetchImageBytes(
212
321
  }
213
322
  }
214
323
 
324
+ /**
325
+ * Fetch the `<head>` of an HTML page with an SSRF-checked redirect chain and a
326
+ * timeout. Reading stops at `</head>` or `<body>`, or once `maxBytes` have
327
+ * arrived — a truncated head is returned, not refused, because the metadata a
328
+ * preview needs sits near the top. Only HTML is read: a response that declares
329
+ * another content type is refused before its body is touched.
330
+ *
331
+ * The bytes are decoded as UTF-8 whatever the page's charset: callers read
332
+ * attribute values that are URLs and keywords, which are ASCII in any
333
+ * charset a page is likely to use.
334
+ *
335
+ * @param startUrl - A URL already validated by {@link assertPublicHttpUrl}
336
+ * @param options - Read cap, timeout, and redirect budget
337
+ * @returns The head's HTML and the URL it came from after redirects
338
+ * @example
339
+ * ```ts
340
+ * const { html, url } = await fetchHtmlHead(
341
+ * assertPublicHttpUrl("https://example.com/post", "page"),
342
+ * { maxBytes: 512 * 1024, timeoutMs: 3000 },
343
+ * );
344
+ * ```
345
+ */
346
+ export async function fetchHtmlHead(
347
+ startUrl: URL,
348
+ options: FetchHtmlHeadOptions,
349
+ ): Promise<FetchedHtmlHead> {
350
+ const controller = new AbortController();
351
+ const timer = setTimeout(() => controller.abort(), options.timeoutMs);
352
+
353
+ try {
354
+ const { response, url } = await fetchFollowingPublicRedirects(startUrl, {
355
+ target: "page",
356
+ signal: controller.signal,
357
+ maxRedirects: options.maxRedirects ?? 5,
358
+ headers: () => ({
359
+ Accept: "text/html,application/xhtml+xml;q=0.9,*/*;q=0.1",
360
+ "User-Agent": PAGE_USER_AGENT,
361
+ }),
362
+ });
363
+
364
+ const contentType = normalizeContentType(
365
+ response.headers.get("content-type"),
366
+ );
367
+ if (
368
+ contentType &&
369
+ contentType !== "text/html" &&
370
+ contentType !== "application/xhtml+xml"
371
+ ) {
372
+ await response.body?.cancel().catch(() => {});
373
+ throw new ValidationError(`${FETCH_ERRORS.page.failed}: not HTML.`);
374
+ }
375
+
376
+ const html = await readHead(response, options.maxBytes).catch((error) => {
377
+ if (controller.signal.aborted) {
378
+ throw new ValidationError(FETCH_ERRORS.page.timeout);
379
+ }
380
+ throw error;
381
+ });
382
+ return { html, url };
383
+ } finally {
384
+ clearTimeout(timer);
385
+ }
386
+ }
387
+
388
+ /** Where a document's head ends: its closing tag, or the body's opening one. */
389
+ const HEAD_END = /<\/head[\s>]|<body[\s>]/i;
390
+
391
+ /** Longest tail of one chunk that could start a {@link HEAD_END} match. */
392
+ const HEAD_END_OVERLAP = 6;
393
+
394
+ async function readHead(response: Response, maxBytes: number): Promise<string> {
395
+ const decoder = new TextDecoder("utf-8");
396
+ const body = response.body;
397
+ if (!body) {
398
+ const text = decoder.decode(
399
+ new Uint8Array(await response.arrayBuffer()).subarray(0, maxBytes),
400
+ );
401
+ return cutAtHeadEnd(text);
402
+ }
403
+
404
+ const reader = body.getReader();
405
+ let text = "";
406
+ let total = 0;
407
+ try {
408
+ for (;;) {
409
+ const { done, value } = await reader.read();
410
+ if (done) break;
411
+ if (!value) continue;
412
+ const room = maxBytes - total;
413
+ const chunk = value.byteLength > room ? value.subarray(0, room) : value;
414
+ total += chunk.byteLength;
415
+ const searchFrom = Math.max(0, text.length - HEAD_END_OVERLAP);
416
+ text += decoder.decode(chunk, { stream: true });
417
+ const end = text.slice(searchFrom).search(HEAD_END);
418
+ if (end !== -1) return text.slice(0, searchFrom + end);
419
+ if (total >= maxBytes) break;
420
+ }
421
+ } finally {
422
+ await reader.cancel().catch(() => {});
423
+ }
424
+ return text + decoder.decode();
425
+ }
426
+
427
+ function cutAtHeadEnd(text: string): string {
428
+ const end = text.search(HEAD_END);
429
+ return end === -1 ? text : text.slice(0, end);
430
+ }
431
+
215
432
  async function readBounded(
216
433
  response: Response,
217
434
  maxBytes: number,
@@ -220,7 +437,7 @@ async function readBounded(
220
437
  if (!body) {
221
438
  const buffer = new Uint8Array(await response.arrayBuffer());
222
439
  if (buffer.byteLength > maxBytes) {
223
- throw new ValidationError("That image is too large.");
440
+ throw new ValidationError(IMAGE_TOO_LARGE);
224
441
  }
225
442
  return buffer;
226
443
  }
@@ -235,7 +452,7 @@ async function readBounded(
235
452
  total += value.byteLength;
236
453
  if (total > maxBytes) {
237
454
  await reader.cancel().catch(() => {});
238
- throw new ValidationError("That image is too large.");
455
+ throw new ValidationError(IMAGE_TOO_LARGE);
239
456
  }
240
457
  chunks.push(value);
241
458
  }
@@ -71,3 +71,58 @@ export function shouldUseShortVideoExperience(media: {
71
71
 
72
72
  return true;
73
73
  }
74
+
75
+ /**
76
+ * Where in a clip its poster frame is taken: a tenth of the way in, so the
77
+ * still is not the fade-from-black most clips open on, and never later than
78
+ * the first second. With no duration to take a tenth of, the opening frame is
79
+ * the only one to name.
80
+ *
81
+ * @param durationSeconds - Clip length in seconds, when known
82
+ * @returns Offset into the clip, in seconds
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * getPosterFrameSeconds(4); // 0.4
87
+ * getPosterFrameSeconds(90); // 1
88
+ * getPosterFrameSeconds(undefined); // 0
89
+ * ```
90
+ */
91
+ export function getPosterFrameSeconds(
92
+ durationSeconds: number | null | undefined,
93
+ ): number {
94
+ if (
95
+ !Number.isFinite(durationSeconds) ||
96
+ !durationSeconds ||
97
+ durationSeconds <= 0
98
+ ) {
99
+ return 0;
100
+ }
101
+
102
+ return Math.min(durationSeconds * 0.1, 1);
103
+ }
104
+
105
+ /**
106
+ * The address a `<video>` loads to show the poster frame itself, for a clip
107
+ * stored without a poster. The media fragment is what makes iOS Safari draw a
108
+ * frame under `preload="metadata"` at all, and a start of 0 is no fragment, so
109
+ * a clip of unknown length starts a tenth of a second in.
110
+ *
111
+ * @param url - The video file's address
112
+ * @param durationSeconds - Clip length in seconds, when known
113
+ * @returns The address with a `#t=` fragment at the poster frame
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * getPosterFrameUrl("https://media.example/clip.mp4", 4);
118
+ * // "https://media.example/clip.mp4#t=0.4"
119
+ * ```
120
+ */
121
+ export function getPosterFrameUrl(
122
+ url: string,
123
+ durationSeconds: number | null | undefined,
124
+ ): string {
125
+ const seconds =
126
+ Math.round(getPosterFrameSeconds(durationSeconds) * 100) / 100;
127
+ return `${url}#t=${seconds > 0 ? seconds : 0.1}`;
128
+ }