-
Notifications
You must be signed in to change notification settings - Fork 45
Add Link header support #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,7 +30,7 @@ function buildFullURL( | |
| ) | ||
| } | ||
|
|
||
| export function findLinksByType(text: TextResponse, type: string): string[] { | ||
| export function findDocumentLinks(text: TextResponse, type: string): string[] { | ||
| let document = text.parseXml() | ||
| if (!document) return [] | ||
| return [...document.querySelectorAll('link')] | ||
|
|
@@ -61,6 +61,34 @@ export function findAnchorHrefs( | |
| .map(a => buildFullURL(a, text.url)) | ||
| } | ||
|
|
||
| /** | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good comment. But we don’t use it in another functions. Can we keep the old comment-less format in PR? But I am open to discuss adding more comments (just prefer to have some policy).
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's consider adding more comments. It's hard to understand what functions do otherwise, even looking at tests. Especially for newcomers. And especially now without ui. |
||
| * Returns an array of links found in the Link http header of a website, | ||
| * if the header is present. | ||
| * An example of a Link header with multiple urls: | ||
| * <http://blog.com/?feed=rss2>; rel="alternate"; type="application/rss+xml" | ||
| * Urls can also be multiple, comma-separated. And possibly relative. | ||
| */ | ||
| export function findHeaderLinks( | ||
| response: TextResponse, | ||
| type: string | ||
| ): string[] { | ||
| let linkHeader = response.headers.get('Link') | ||
| if (!linkHeader) { | ||
| return [] | ||
| } | ||
| return linkHeader.split(/,\s?/).reduce<string[]>((urls, link) => { | ||
| let [, url] = link.match(/<(.*)>/) || [] | ||
| let attributes = link.split(/;\s?/) | ||
| let matchesType = attributes.includes(`type="${type}"`) | ||
| let isAlternate = attributes.includes('rel="alternate"') | ||
| if (url && matchesType && isAlternate) { | ||
| let fullUrl = /^https?/.test(url) ? url : new URL(url, response.url).href | ||
| urls.push(fullUrl) | ||
| } | ||
| return urls | ||
| }, []) | ||
| } | ||
|
|
||
| export function toTime(date: null | string | undefined): number | undefined { | ||
| if (!date) return undefined | ||
| let time = new Date(date).getTime() / 1000 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { deepStrictEqual } from 'node:assert' | ||
| import { test } from 'node:test' | ||
|
|
||
| import { createTextResponse } from '../../download.js' | ||
| import { findHeaderLinks } from '../../loader/utils.js' | ||
|
|
||
| test('returns urls from link http header', () => { | ||
| deepStrictEqual( | ||
| findHeaderLinks( | ||
| createTextResponse(`<!DOCTYPE html><html><head></head></html>`, { | ||
| headers: new Headers({ | ||
| Link: '<https://one.example.com>; rel="alternate"; type="application/rss+xml"' | ||
| + ', <https://two.example.com>; rel="alternate"; type="application/rss+xml"' | ||
| }), | ||
| url: 'https://example.com' | ||
| }), | ||
| 'application/rss+xml' | ||
| ), | ||
| ['https://one.example.com', 'https://two.example.com'] | ||
| ) | ||
| }) | ||
|
|
||
| test('handles root-relative urls in http header', () => { | ||
| deepStrictEqual( | ||
| findHeaderLinks( | ||
| createTextResponse(`<!DOCTYPE html><html><head></head></html>`, { | ||
| headers: new Headers({ | ||
| Link: '</rss>; rel="alternate"; type="application/atom+xml"' | ||
| }), | ||
| url: 'https://example.com/blog' | ||
| }), | ||
| 'application/atom+xml' | ||
| ), | ||
| ['https://example.com/rss'] | ||
| ) | ||
| }) | ||
|
|
||
| test('handles relative urls in http header', () => { | ||
| deepStrictEqual( | ||
| findHeaderLinks( | ||
| createTextResponse(`<!DOCTYPE html><html><head></head></html>`, { | ||
| headers: new Headers({ | ||
| Link: '<./rss>; rel="alternate"; type="application/atom+xml"' | ||
| }), | ||
| url: 'https://example.com/blog/' | ||
| }), | ||
| 'application/atom+xml' | ||
| ), | ||
| ['https://example.com/blog/rss'] | ||
| ) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.