-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a17772
commit 379a3fa
Showing
3 changed files
with
141 additions
and
121 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,20 +1,27 @@ | ||
import { addError, list } from "./core"; | ||
import { addError, getList, onExit } from "./core"; | ||
|
||
// for each redirect | ||
await Promise.all( | ||
list.map(async ({ to }) => { | ||
try { | ||
// do simple request to target url | ||
const response = await fetch(to); | ||
if ( | ||
// only fail on certain status codes that might indicate link is "broken" | ||
[ | ||
400, 404, 405, 406, 408, 409, 410, 421, 500, 501, 502, 503, 504, | ||
].includes(response.status) | ||
) | ||
throw Error(response.status); | ||
} catch (error) { | ||
addError(`"to: ${to}" may be a broken link\n(${error})`); | ||
} | ||
}) | ||
); | ||
onExit(); | ||
|
||
// check list of redirects for broken links | ||
async function checkList(list) { | ||
return await Promise.all( | ||
// for each redirect | ||
list.map(async ({ to }) => { | ||
try { | ||
// do simple request to target url | ||
const response = await fetch(to); | ||
if ( | ||
// only fail on certain status codes that might indicate link is "broken" | ||
[ | ||
400, 404, 405, 406, 408, 409, 410, 421, 500, 501, 502, 503, 504, | ||
].includes(response.status) | ||
) | ||
throw Error(response.status); | ||
} catch (error) { | ||
addError(`"to: ${to}" may be a broken link\n(${error})`); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
await checkList(getList()); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,47 @@ | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import { addError, list, verbose } from "./core"; | ||
import { addError, getList, onExit, verbose } from "./core"; | ||
|
||
// encode redirect list to base64 to obfuscate | ||
const encoded = Buffer.from(JSON.stringify(list)).toString("base64"); | ||
onExit(); | ||
|
||
if (verbose) log("Encoded redirects list", encoded); | ||
// encode list of redirects into redirect script | ||
function encodeList(list) { | ||
// encode redirect list to base64 to obfuscate | ||
const encoded = Buffer.from(JSON.stringify(list)).toString("base64"); | ||
|
||
// redirect script from website repo | ||
const script = "./website-repo/redirect.js"; | ||
if (verbose) log("Encoded redirects list", encoded); | ||
|
||
// load contents of script | ||
let contents = ""; | ||
try { | ||
contents = readFileSync(script, "utf8").toString(); | ||
} catch (error) { | ||
addError(`Couldn't find script file at ${script}`); | ||
} | ||
// redirect script from website repo | ||
const script = "./website-repo/redirect.js"; | ||
|
||
// load contents of script | ||
let contents = ""; | ||
try { | ||
contents = readFileSync(script, "utf8").toString(); | ||
} catch (error) { | ||
addError(`Couldn't find script file at ${script}`); | ||
} | ||
|
||
// pattern to extract encoded redirect list from script string | ||
const regex = /(list\s*=\s*")([A-Za-z0-9+\/=]*)(")/; | ||
// pattern to extract encoded redirect list from script string | ||
const regex = /(list\s*=\s*")([A-Za-z0-9+\/=]*)(")/; | ||
|
||
// get encoded redirect list currently in script | ||
const oldEncoded = contents.match(regex)?.[2]; | ||
// get encoded redirect list currently in script | ||
const oldEncoded = contents.match(regex)?.[2]; | ||
|
||
if (verbose) log("Old encoded redirects list", oldEncoded); | ||
if (verbose) log("Old encoded redirects list", oldEncoded); | ||
|
||
// check that we could find it (and thus can replace it) | ||
if (typeof oldEncoded !== "string") | ||
addError("Couldn't find encoded redirects list in redirect script"); | ||
// check that we could find it (and thus can replace it) | ||
if (typeof oldEncoded !== "string") | ||
addError("Couldn't find encoded redirects list in redirect script"); | ||
|
||
// update encoded string in script | ||
const newContents = contents.replace(regex, "$1" + encoded + "$3"); | ||
// update encoded string in script | ||
const newContents = contents.replace(regex, "$1" + encoded + "$3"); | ||
|
||
// write updated redirect script to website repo | ||
try { | ||
writeFileSync(script, newContents, "utf-8"); | ||
} catch (error) { | ||
addError(`Couldn't write script file to ${script}`); | ||
// write updated redirect script to website repo | ||
try { | ||
writeFileSync(script, newContents, "utf-8"); | ||
} catch (error) { | ||
addError(`Couldn't write script file to ${script}`); | ||
} | ||
} | ||
|
||
encodeList(getList()); |