Skip to content

Commit

Permalink
refactor: process allowlist/blocklist before even caching
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburnell committed Dec 12, 2023
1 parent 4cf7284 commit 988ebe1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 56 deletions.
99 changes: 44 additions & 55 deletions eleventy-cache-webmentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,50 @@ const fetchWebmentions = async (options) => {
if (!asset.isCacheValid(options.duration)) {
const since = webmentions.length ? getPublished(webmentions[0]) : false
const url = `${options.feed}${since ? `${options.feed.includes("?") ? "&" : "?"}since=${since}` : ""}`
await fetch(url)
.then(async (response) => {
if (response.ok) {
const feed = await response.json()
if (feed[options.key].length) {
webmentions = uniqBy([...feed[options.key], ...webmentions], (entry) => {
return getSource(entry)
await fetch(url).then(async (response) => {
if (response.ok) {
const feed = await response.json()
if (feed[options.key].length) {
// Process the blocklist, if it has any entries
if (options.blocklist.length) {
webmentions = webmentions.filter((webmention) => {
let sourceUrl = getSource(webmention)
for (let url of options.blocklist) {
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
return false
}
}
return true
})
console.log(`[${hostname(options.domain)}] ${feed[options.key].length} new Webmentions fetched into cache.`)
}
await asset.save(webmentions, "json")
return webmentions
// Process the allowlist, if it has any entries
if (options.allowlist.length) {
webmentions = webmentions.filter((webmention) => {
let sourceUrl = getSource(webmention)
for (let url of options.allowlist) {
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
return true
}
}
return false
})
}
// Remove duplicates by source URL
webmentions = uniqBy([...feed[options.key], ...webmentions], (entry) => {
return getSource(entry)
})
if (webmentions.length) {
console.log(`[${hostname(options.domain)}] ${webmentions.length} new Webmentions fetched into cache.`)
}
}
return Promise.reject(response)
})
.catch((error) => {
console.log(`[${hostname(options.domain)}] Something went wrong with your request to ${hostname(options.feed)}!`, error)
})
await asset.save(webmentions, "json")
return webmentions
}
return Promise.reject(response)
})
.catch((error) => {
console.log(`[${hostname(options.domain)}] Something went wrong with your request to ${hostname(options.feed)}!`, error)
})
}

return webmentions
Expand All @@ -132,36 +158,6 @@ const filteredWebmentions = async (options) => {

let rawWebmentions = await fetchWebmentions(options)

// Process the blocklist, if it has any entries
if (options.blocklist.length) {
rawWebmentions = rawWebmentions.filter((webmention) => {
let sourceUrl = getSource(webmention)

for (let url of options.blocklist) {
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
return false
}
}

return true
})
}

// Process the allowlist, if it has any entries
if (options.allowlist.length) {
rawWebmentions = rawWebmentions.filter((webmention) => {
let sourceUrl = getSource(webmention)

for (let url of options.allowlist) {
if (sourceUrl.includes(url.replace(/\/?$/, "/"))) {
return true
}
}

return false
})
}

// Fix local URLs based on urlReplacements and sort Webmentions into groups
// by target base URL
rawWebmentions.forEach((webmention) => {
Expand All @@ -174,13 +170,6 @@ const filteredWebmentions = async (options) => {
filtered[url].push(webmention)
})

// Remove duplicates by source URL
for (let url in filtered) {
filtered[url] = uniqBy(filtered[url], (entry) => {
return getSource(entry)
})
}

const filteredCount = Object.values(filtered).reduce((count, webmentions) => count + webmentions.length, 0)
console.log(`[${hostname(options.domain)}] ${filteredCount} filtered Webmentions pulled from cache.`)

Expand All @@ -197,11 +186,11 @@ const getWebmentions = async (options, url, allowedTypes = {}) => {

return (
webmentions[url]
// filter webmentions by allowed response post types
// Filter webmentions by allowed response post types
.filter((entry) => {
return typeof allowedTypes === "object" && Object.keys(allowedTypes).length ? allowedTypes.includes(getType(entry)) : true
})
// sanitize content of webmentions against HTML limit
// Sanitize content of webmentions against HTML limit
.map((entry) => {
const html = getContent(entry)

Expand All @@ -214,7 +203,7 @@ const getWebmentions = async (options, url, allowedTypes = {}) => {

return entry
})
// sort by published
// Sort by published
.sort((a, b) => {
return epoch(getPublished(a)) - epoch(getPublished(b))
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chrisburnell/eleventy-cache-webmentions",
"version": "1.2.5",
"version": "1.2.6",
"description": "Fetch and cache webmentions using eleventy-fetch.",
"author": "Chris Burnell <me@chrisburnell.com>",
"license": "MIT",
Expand Down

0 comments on commit 988ebe1

Please sign in to comment.