Skip to content
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

Fix some outstanding bugs in the docs linter. #2906

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
37 changes: 35 additions & 2 deletions scripts/_lint-docs/lint-http-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function checkKnownCorrectRequestFail(href, headers, status, body) {

/**
* Returns `undefined` if no error, a string if an error does occur.
* @param {(message?: string)} callback
* @param {(message?: string) => void} callback
*/
function checkHttp(href, callback) {
function checkHttpInner(href, callback) {
// Prefer https: > http: where possible, but allow http: when https: is inaccessible.

const url = new URL(href)
Expand Down Expand Up @@ -67,6 +67,39 @@ function checkHttp(href, callback) {
})
}

// Kill the remaining duplication by using a global cache.
const urlCache = new Map()

/**
* Returns `undefined` if no error, a string if an error does occur.
* @param {(message?: string) => void} callback
*/
function checkHttp(href, callback) {
if (href.includes("#")) {
process.exitCode = 1
callback(`Expected href to be sanitized of hashes, but found ${href}`)
}

if (urlCache.has(href)) {
const message = urlCache.get(href)
if (Array.isArray(message)) {
message.push(callback)
} else {
process.nextTick(callback, message)
}
} else {
const queue = []
urlCache.set(href, queue)
checkHttpInner(href, (message) => {
urlCache.set(href, message)
process.nextTick(callback, message)
for (const callback of queue) {
process.nextTick(callback, message)
}
})
}
}

module.exports = {
checkHttp,
}
10 changes: 9 additions & 1 deletion scripts/_lint-docs/task-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let running = 0
function runTask(task, callback) {
process.nextTick(task, (...args) => {
process.nextTick(callback, ...args)
if (running === maxConcurrency) {
if (running === maxConcurrency && queue.length !== 0) {
const [nextTask, nextCallback] = queue.splice(0, 2)
runTask(nextTask, nextCallback)
}
Expand All @@ -22,6 +22,14 @@ function runTask(task, callback) {
* @param {(...args: A) => void} callback
*/
function submitTask(task, callback) {
if (typeof task !== "function") {
throw new TypeError("`task` must be a function")
}

if (typeof callback !== "function") {
throw new TypeError("`callback` must be a function")
}

if (running < maxConcurrency) {
running++
runTask(task, callback)
Expand Down