@postfetch/core 0.13.6 → 0.13.7

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.
package/dist/index.js CHANGED
@@ -1706,7 +1706,12 @@ async function mediaItems3(input, post, id) {
1706
1706
  return own;
1707
1707
  }
1708
1708
  const parents = Array.isArray(post.crosspost_parent_list) ? post.crosspost_parent_list.filter(object) : [];
1709
- return parents[0] ? extract(input, parents[0], id) : [];
1709
+ const parent = parents[0];
1710
+ const inherited = parent ? await extract(input, parent, id) : [];
1711
+ if (inherited.length > 0) {
1712
+ return inherited;
1713
+ }
1714
+ return linkPreviewItems(post, id).concat(parent ? linkPreviewItems(parent, id) : []).slice(0, 1);
1710
1715
  }
1711
1716
  async function extract(input, post, id) {
1712
1717
  const gallery = galleryItems(post, id);
@@ -1830,6 +1835,50 @@ function imageItems2(post, id) {
1830
1835
  const isImage = string(post.post_hint) === "image" || asUrl(url).hostname === "i.redd.it" || /\.(jpe?g|png|webp|gif)$/i.test(path);
1831
1836
  return isImage ? [mediaItem(id, null, "image", mimeOf(null, url), url)] : [];
1832
1837
  }
1838
+ function linkPreviewItems(post, id) {
1839
+ const destination = string(post.url_overridden_by_dest) ?? string(post.url);
1840
+ if (post.is_self === true || !destination)
1841
+ return [];
1842
+ try {
1843
+ const url = new URL(destination);
1844
+ if (!/^https?:$/.test(url.protocol) || /(^|\.)(reddit\.com|redd\.it)$/.test(url.hostname))
1845
+ return [];
1846
+ } catch {
1847
+ return [];
1848
+ }
1849
+ const preview = object(post.preview) ? post.preview : null;
1850
+ const images = preview && Array.isArray(preview.images) ? preview.images.filter(object) : [];
1851
+ const candidates = images.flatMap((image) => {
1852
+ const source2 = object(image.source) ? [image.source] : [];
1853
+ const resolutions = Array.isArray(image.resolutions) ? image.resolutions.filter(object) : [];
1854
+ return [...source2, ...resolutions].flatMap((rendition, index) => {
1855
+ const rawUrl = string(rendition.url);
1856
+ if (!rawUrl)
1857
+ return [];
1858
+ try {
1859
+ const url = new URL(rawUrl.replace(/&/g, "&"));
1860
+ if (!/^https?:$/.test(url.protocol))
1861
+ return [];
1862
+ return [{
1863
+ url: url.href,
1864
+ original: source2.length > 0 && index === 0 ? 1 : 0,
1865
+ area: Math.max(0, number(rendition.width) ?? 0) * Math.max(0, number(rendition.height) ?? 0)
1866
+ }];
1867
+ } catch {
1868
+ return [];
1869
+ }
1870
+ });
1871
+ }).sort((a, b) => b.original - a.original || b.area - a.area);
1872
+ const best = candidates[0];
1873
+ if (!best)
1874
+ return [];
1875
+ const mime = mimeOf(null, best.url);
1876
+ return [{
1877
+ ...mediaItem(id, null, "image", mime, best.url),
1878
+ filename: filename(`reddit_${id}_preview.${extensionOf(mime)}`),
1879
+ role: "linkPreview"
1880
+ }];
1881
+ }
1833
1882
  function mediaItem(id, index, kind, mime, url) {
1834
1883
  const suffix = index === null ? "" : `_${index}`;
1835
1884
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postfetch/core",
3
- "version": "0.13.6",
3
+ "version": "0.13.7",
4
4
  "description": "Zero-dependency typed core that turns Facebook, Instagram, LinkedIn, Pinterest, Reddit, SoundCloud, Spotify, TikTok, X and YouTube post URLs into media files.",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/reddit.ts CHANGED
@@ -128,7 +128,12 @@ async function mediaItems(input: ResolveContext, post: Json, id: string): Promis
128
128
  return own;
129
129
  }
130
130
  const parents = Array.isArray(post.crosspost_parent_list) ? post.crosspost_parent_list.filter(object) : [];
131
- return parents[0] ? extract(input, parents[0], id) : [];
131
+ const parent = parents[0];
132
+ const inherited = parent ? await extract(input, parent, id) : [];
133
+ if (inherited.length > 0) {
134
+ return inherited;
135
+ }
136
+ return linkPreviewItems(post, id).concat(parent ? linkPreviewItems(parent, id) : []).slice(0, 1);
132
137
  }
133
138
 
134
139
  async function extract(input: ResolveContext, post: Json, id: string): Promise<MediaItem[]> {
@@ -277,6 +282,48 @@ function imageItems(post: Json, id: string): MediaItem[] {
277
282
  return isImage ? [mediaItem(id, null, "image", mimeOf(null, url), url)] : [];
278
283
  }
279
284
 
285
+ function linkPreviewItems(post: Json, id: string): MediaItem[] {
286
+ // Reddit also supplies preview images for photos/videos and self posts. Only
287
+ // external link cards belong here; native attachments take precedence above.
288
+ const destination = string(post.url_overridden_by_dest) ?? string(post.url);
289
+ if (post.is_self === true || !destination) return [];
290
+ try {
291
+ const url = new URL(destination);
292
+ if (!/^https?:$/.test(url.protocol) || /(^|\.)(reddit\.com|redd\.it)$/.test(url.hostname)) return [];
293
+ } catch {
294
+ return [];
295
+ }
296
+ const preview = object(post.preview) ? post.preview : null;
297
+ const images = preview && Array.isArray(preview.images) ? preview.images.filter(object) : [];
298
+ const candidates = images.flatMap((image) => {
299
+ const source = object(image.source) ? [image.source] : [];
300
+ const resolutions = Array.isArray(image.resolutions) ? image.resolutions.filter(object) : [];
301
+ return [...source, ...resolutions].flatMap((rendition, index) => {
302
+ const rawUrl = string(rendition.url);
303
+ if (!rawUrl) return [];
304
+ try {
305
+ const url = new URL(rawUrl.replace(/&amp;/g, "&"));
306
+ if (!/^https?:$/.test(url.protocol)) return [];
307
+ return [{
308
+ url: url.href,
309
+ original: source.length > 0 && index === 0 ? 1 : 0,
310
+ area: Math.max(0, number(rendition.width) ?? 0) * Math.max(0, number(rendition.height) ?? 0),
311
+ }];
312
+ } catch {
313
+ return [];
314
+ }
315
+ });
316
+ }).sort((a, b) => b.original - a.original || b.area - a.area);
317
+ const best = candidates[0];
318
+ if (!best) return [];
319
+ const mime = mimeOf(null, best.url);
320
+ return [{
321
+ ...mediaItem(id, null, "image", mime, best.url),
322
+ filename: filename(`reddit_${id}_preview.${extensionOf(mime)}`),
323
+ role: "linkPreview",
324
+ }];
325
+ }
326
+
280
327
  function mediaItem(id: string, index: number | null, kind: MediaItem["kind"], mime: string, url: string): MediaItem {
281
328
  const suffix = index === null ? "" : `_${index}`;
282
329
  return {