Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/determine-filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* - given "https://compat.spec.whatwg.org/", the function would determine that
* the filename is "index.html".
*/
import { fetch } from "./fetch.js";

export default async function (url) {
// Extract filename directly from the URL when possible
Expand Down
1 change: 1 addition & 0 deletions src/fetch-iso-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* previous data instead. The expectation is that this option will be set for
* most automated builds.
*/
import { fetch } from "./fetch.js";

async function fetchInfoFromISO(specs, options) {
if (!specs || specs.find(spec => !spec.url)) {
Expand Down
1 change: 1 addition & 0 deletions src/fetch-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ThrottledQueue from "./throttled-queue.js";
import { fetch } from "./fetch.js";

// Make sure we remain "friendly" with servers
// In particular, we're going to have to fetch a number of w3c.json files from
Expand Down
33 changes: 33 additions & 0 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Wrapper around the fetch module to identify the crawler with a specific
* User-Agent, both not to hide that we're sending fetch requests as part of an
* automated workflow, and because some servers may maintain dedicated rules to
* allow our little crawling requests.
*
* Note: To keep things simple for people maintaining these servers, we're
* going to use the same User-Agent string as in Reffy, the "real" crawler.
*/

import packageConfig from '../package.json' with { type: 'json' };
const reffyVersion = packageConfig.devDependencies.reffy.replace('^', '');
const userAgent = `Reffy/${reffyVersion}`;

/**
* Fetch function that adds a User-Agent string to identify requests as coming
* from Reffy.
*
* @function
* @param {String} url URL to fetch
* @param {Object} options Fetch options
* @return {Promise(Response)} Promise to get an HTTP response
*/
async function crawlFetch(url, options) {
options = Object.assign({headers: {}}, options);
options.headers['User-Agent'] = userAgent;
return fetch(url, options);
}

export {
crawlFetch as fetch,
userAgent
}
2 changes: 2 additions & 0 deletions src/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Send a GraphQL request to the GitHub GraphQL endpoint, authenticating using
* the provided token.
*/
import { fetch } from "./fetch.js";

export default async function (query, variables, graphqlToken) {
if (typeof variables === 'string') {
graphqlToken = variables;
Expand Down
4 changes: 4 additions & 0 deletions src/load-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import process from "node:process";
import { Buffer } from "node:buffer";
import { userAgent } from "./fetch.js";

class HttpStatusError extends Error {
constructor(url, status, statusText) {
Expand Down Expand Up @@ -72,6 +73,9 @@ export default async function (url, page) {
}
}

// Set custom user-agent string
await page.setUserAgent(userAgent);

// Intercept network requests to avoid downloading images and streams
const cdp = await page.target().createCDPSession();

Expand Down
1 change: 1 addition & 0 deletions src/monitor-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import sendGraphQLQuery from "./graphql.js";
import splitIssueBodyIntoSections from "./split-issue-body.js";
import loadJSON from "./load-json.js";
import { fetch } from "./fetch.js";

const config = await loadJSON("config.json");
const githubToken = config?.GITHUB_TOKEN ?? process.env.GITHUB_TOKEN;
Expand Down