-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Policies] Remove broken refs (#119)
* works single threaded * add concurrency * use await
- Loading branch information
Showing
5 changed files
with
72 additions
and
7 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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
{ | ||
"version": 0.1, | ||
"output_folder": "output", | ||
"output_filename": "combined.json", | ||
"output_filename": "combined.yaml", | ||
"benchmarks_folder": "input", | ||
"uuid_seed": "5d8d0dd5-acd2-4c46-b565-aa1fb03617af" | ||
"uuid_seed": "5d8d0dd5-acd2-4c46-b565-aa1fb03617af", | ||
"check_for_broken_references": true | ||
} |
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,59 @@ | ||
import config from "config"; | ||
import axios, {AxiosError} from "axios"; | ||
|
||
const check: string = config.get("check_for_broken_references"); | ||
|
||
const cache: HttpCache = {}; | ||
|
||
function setCache(key: string, code: number) { | ||
cache[key] = code; | ||
} | ||
|
||
function getCache(key: string): number { | ||
return cache[key]; | ||
} | ||
|
||
function logResponse(link: string, code: number, isFromCache: boolean) { | ||
const term = isFromCache ? "via cache" : ""; | ||
console.log("Got", code, term, "for", link); | ||
} | ||
|
||
async function checkReference(link: string): Promise<boolean> { | ||
const code = getCache(link); | ||
if (!isNaN(code)) { | ||
logResponse(link, code, true); | ||
return code == 200; | ||
} | ||
try { | ||
const res = await axios.head(link) | ||
logResponse(link, res.status, false); | ||
setCache(link, res.status); | ||
return res.status == 200; | ||
} catch (err) { | ||
if (err instanceof AxiosError && err.response) { | ||
setCache(link, err.response.status); | ||
logResponse(link, err.response.status, false); | ||
} else { | ||
// If we got here, it means that we failed to reach the server because of things that are IN OUR CONTROL | ||
// (e.g. timeout, socket reset) | ||
console.log(err); | ||
process.abort(); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
async function removeIfBroken(references: string[]) { | ||
for (let i = references.length - 1; i >= 0; i--) | ||
if (!(await checkReference(references[i]))) | ||
references.splice(i, 1) | ||
} | ||
|
||
export async function FixBrokenReferences(parsed_benchmarks: BenchmarkSchema[]): Promise<BenchmarkSchema[]> { | ||
if (!check) | ||
return parsed_benchmarks; | ||
await Promise.all(parsed_benchmarks.map( | ||
async bench => await Promise.all(bench.rules.map( | ||
async rule => await removeIfBroken(rule.references))))) | ||
return parsed_benchmarks | ||
} |
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