Problem
The formatEnclosure function in atom1.ts ignores the explicitly provided type parameter in Enclosure objects and always tries to auto-detect MIME types from URLs, leading to incorrect types like image// for proxy URLs.
Current Code
const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => {
if (typeof enclosure === "string") {
const type = new URL(enclosure).pathname.split(".").slice(-1)[0];
return { _attributes: { rel: "enclosure", href: enclosure, type: `${mimeCategory}/${type}` } };
}
const type = new URL(enclosure.url).pathname.split(".").slice(-1)[0]; // BUG: ignores enclosure.type!
return {
_attributes: {
rel: "enclosure",
href: enclosure.url,
title: enclosure.title,
type: `${mimeCategory}/${type}`, // Should use enclosure.type if provided
length: enclosure.length,
},
};
};
Issue
- When an
Enclosure object provides an explicit type, it's completely ignored
- Auto-detection fails for proxy URLs like
https://wsrv.nl/?url=... resulting in image//
- Users have no way to override incorrect auto-detection
Proposed Fix
const formatEnclosure = (enclosure: string | Enclosure, mimeCategory = "image") => {
if (typeof enclosure === "string") {
const detectedType = new URL(enclosure).pathname.split(".").slice(-1)[0];
return { _attributes: { rel: "enclosure", href: enclosure, type: `${mimeCategory}/${detectedType}` } };
}
// Use explicitly provided type, otherwise auto-detect
let type = enclosure.type;
if (!type) {
const detectedType = new URL(enclosure.url).pathname.split(".").slice(-1)[0];
type = `${mimeCategory}/${detectedType}`;
}
return {
_attributes: {
rel: "enclosure",
href: enclosure.url,
title: enclosure.title,
type: type,
length: enclosure.length,
},
};
};
Benefits
- Respects explicit types: When users provide
type, it's used as-is
- Backward compatible: Auto-detection still works for string URLs and Enclosure objects without type
- Fixes proxy URL issues: Users can specify correct MIME types for any URL
- Simple and focused: No complex URL parsing logic needed
Example Usage
// Before: Always resulted in image// for proxy URLs
feed.addItem({
image: "https://wsrv.nl/?url=https%3A//example.com/image.jpg&w=1000"
});
// After: Users can specify the correct type
feed.addItem({
image: {
url: "https://wsrv.nl/?url=https%3A//example.com/image.jpg&w=1000",
type: "image/jpeg"
}
});
Problem
The
formatEnclosurefunction inatom1.tsignores the explicitly providedtypeparameter inEnclosureobjects and always tries to auto-detect MIME types from URLs, leading to incorrect types likeimage//for proxy URLs.Current Code
Issue
Enclosureobject provides an explicittype, it's completely ignoredhttps://wsrv.nl/?url=...resulting inimage//Proposed Fix
Benefits
type, it's used as-isExample Usage