-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for 410 Gone URL (#431)
- Loading branch information
1 parent
46560cb
commit 8820a42
Showing
3 changed files
with
45 additions
and
41 deletions.
There are no files selected for viewing
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
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,73 +1,77 @@ | ||
const url = require("url"); | ||
const logError = require("./logger").error; | ||
const { match, compile } = require("path-to-regexp"); | ||
const url = require('url') | ||
const logError = require('./logger').error | ||
const { match, compile } = require('path-to-regexp') | ||
|
||
function isUrl(url) { | ||
function isUrl (url) { | ||
try { | ||
return new URL(url); | ||
return new URL(url) | ||
} catch (err) { | ||
return false; | ||
return false | ||
} | ||
} | ||
|
||
function processRedirects(req, res, next, sourceUrlArray, urls) { | ||
const query = url.parse(req.url, true) || {}; | ||
const search = query.search || ""; | ||
function processRedirects (req, res, next, sourceUrlArray, urls) { | ||
const query = url.parse(req.url, true) || {} | ||
const search = query.search || '' | ||
|
||
sourceUrlArray.some((sourceUrl) => { | ||
sourceUrlArray.some(sourceUrl => { | ||
try { | ||
const statusCode = parseInt(urls[sourceUrl].statusCode, 10) | ||
if (statusCode === 410) { | ||
res.sendStatus(410) | ||
return true | ||
} | ||
if (urls[sourceUrl]) { | ||
const destinationPath = urls[sourceUrl].destinationUrl; | ||
const destinationPath = urls[sourceUrl].destinationUrl | ||
const extractedSourceUrl = match(sourceUrl, { | ||
decode: decodeURIComponent, | ||
}); | ||
const destinationUrl = isUrl(destinationPath); | ||
decode: decodeURIComponent | ||
}) | ||
const destinationUrl = isUrl(destinationPath) | ||
if (extractedSourceUrl) { | ||
let extractedDestinationUrl; | ||
let extractedDestinationUrl | ||
if (destinationUrl) { | ||
extractedDestinationUrl = compile(destinationUrl.pathname, { | ||
encode: encodeURIComponent, | ||
}); | ||
encode: encodeURIComponent | ||
}) | ||
} else { | ||
extractedDestinationUrl = compile(destinationPath, { | ||
encode: encodeURIComponent, | ||
}); | ||
encode: encodeURIComponent | ||
}) | ||
} | ||
const dynamicKeys = extractedSourceUrl(req.path); | ||
const compiledPath = dynamicKeys && extractedDestinationUrl(dynamicKeys.params); | ||
const dynamicKeys = extractedSourceUrl(req.path) | ||
const compiledPath = dynamicKeys && extractedDestinationUrl(dynamicKeys.params) | ||
if (compiledPath) { | ||
const validStatusCodes = { 301: "max-age=604800", 302: "max-age=86400" }; | ||
const statusCode = parseInt(urls[sourceUrl].statusCode, 10); | ||
const cacheValue = validStatusCodes[statusCode]; | ||
const validStatusCodes = { 301: 'max-age=604800', 302: 'max-age=86400' } | ||
const cacheValue = validStatusCodes[statusCode] | ||
if (cacheValue) { | ||
res.set("cache-control", `public,${cacheValue}`); | ||
res.set('cache-control', `public,${cacheValue}`) | ||
} | ||
res.redirect( | ||
statusCode, | ||
destinationUrl | ||
? `${destinationUrl.protocol}//${destinationUrl.hostname}${compiledPath}${search}` | ||
: `${compiledPath}${search}` | ||
); | ||
return true; | ||
) | ||
return true | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
console.log(`Redirection error on ${req.host}-----`, err); | ||
console.log(`Redirection error on ${req.host}-----`, err) | ||
} | ||
}); | ||
}) | ||
} | ||
|
||
exports.getRedirectUrl = async function getRedirectUrl(req, res, next, { redirectUrls, config }) { | ||
let sourceUrls; | ||
if (typeof redirectUrls === "function") { | ||
const redirectUrlsList = await redirectUrls(config); | ||
sourceUrls = Object.keys(redirectUrlsList); | ||
exports.getRedirectUrl = async function getRedirectUrl (req, res, next, { redirectUrls, config }) { | ||
let sourceUrls | ||
if (typeof redirectUrls === 'function') { | ||
const redirectUrlsList = await redirectUrls(config) | ||
sourceUrls = Object.keys(redirectUrlsList) | ||
if (sourceUrls.length > 0) { | ||
processRedirects(req, res, next, sourceUrls, redirectUrlsList); | ||
processRedirects(req, res, next, sourceUrls, redirectUrlsList) | ||
} | ||
} else if (redirectUrls) { | ||
sourceUrls = Object.keys(redirectUrls); | ||
sourceUrls.length > 0 && processRedirects(req, res, next, sourceUrls, redirectUrls); | ||
sourceUrls = Object.keys(redirectUrls) | ||
sourceUrls.length > 0 && processRedirects(req, res, next, sourceUrls, redirectUrls) | ||
} | ||
}; | ||
} |