-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40434 from rushatgabhane/validate-redirect
[HelpDot] detect infinite redirect cycles in redirects.csv
- Loading branch information
Showing
7 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import {parse} from 'csv-parse'; | ||
import fs from 'fs'; | ||
|
||
const parser = parse(); | ||
|
||
const adjacencyList: Record<string, string[]> = {}; | ||
const visited: Map<string, boolean> = new Map<string, boolean>(); | ||
const backEdges: Map<string, boolean> = new Map<string, boolean>(); | ||
|
||
function addEdge(source: string, target: string) { | ||
if (!adjacencyList[source]) { | ||
adjacencyList[source] = []; | ||
} | ||
adjacencyList[source].push(target); | ||
} | ||
|
||
function isCyclic(currentNode: string): boolean { | ||
visited.set(currentNode, true); | ||
backEdges.set(currentNode, true); | ||
|
||
// Do a depth first search for all the neighbours. If a node is found in backedge, a cycle is detected. | ||
const neighbours = adjacencyList[currentNode]; | ||
if (neighbours) { | ||
for (const node of neighbours) { | ||
if (!visited.has(node)) { | ||
if (isCyclic(node)) { | ||
return true; | ||
} | ||
} else if (backEdges.has(node)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
backEdges.delete(currentNode); | ||
|
||
return false; | ||
} | ||
|
||
function detectCycle(): boolean { | ||
for (const [node] of Object.entries(adjacencyList)) { | ||
if (!visited.has(node)) { | ||
if (isCyclic(node)) { | ||
const cycle = Array.from(backEdges.keys()); | ||
console.log(`Infinite redirect found in the cycle: ${cycle.join(' -> ')} -> ${node}`); | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
fs.createReadStream(`${process.cwd()}/docs/redirects.csv`) | ||
.pipe(parser) | ||
.on('data', (row) => { | ||
// Create a directed graph of sourceURL -> targetURL | ||
addEdge(row[0], row[1]); | ||
}) | ||
.on('end', () => { | ||
if (detectCycle()) { | ||
process.exit(1); | ||
} | ||
process.exit(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters