2022-09-16 11:42:21 +03:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const DomainEvents = require('@tryghost/domain-events');
|
|
|
|
const RedirectEvent = require('./RedirectEvent');
|
2022-09-14 01:18:19 +03:00
|
|
|
const LinkRedirect = require('./LinkRedirect');
|
|
|
|
|
2022-09-16 11:42:21 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} ILinkRedirectRepository
|
2022-09-19 18:12:54 +03:00
|
|
|
* @prop {(url: URL) => Promise<LinkRedirect|undefined>} getByURL
|
2022-09-22 14:39:52 +03:00
|
|
|
* @prop {({filter: string}) => Promise<LinkRedirect[]>} getAll
|
2022-10-18 13:03:27 +03:00
|
|
|
* @prop {({filter: string}) => Promise<String[]>} getFilteredIds
|
2022-09-16 11:42:21 +03:00
|
|
|
* @prop {(linkRedirect: LinkRedirect) => Promise<void>} save
|
|
|
|
*/
|
|
|
|
|
2022-09-14 01:18:19 +03:00
|
|
|
class LinkRedirectsService {
|
2022-09-16 11:42:21 +03:00
|
|
|
/** @type ILinkRedirectRepository */
|
|
|
|
#linkRedirectRepository;
|
|
|
|
/** @type URL */
|
|
|
|
#baseURL;
|
|
|
|
|
2023-02-02 10:01:20 +03:00
|
|
|
/** @type String */
|
|
|
|
#redirectURLPrefix = 'r/';
|
|
|
|
|
2022-09-16 11:42:21 +03:00
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {ILinkRedirectRepository} deps.linkRedirectRepository
|
|
|
|
* @param {object} deps.config
|
|
|
|
* @param {URL} deps.config.baseURL
|
|
|
|
*/
|
|
|
|
constructor(deps) {
|
|
|
|
this.#linkRedirectRepository = deps.linkRedirectRepository;
|
|
|
|
if (!deps.config.baseURL.pathname.endsWith('/')) {
|
|
|
|
this.#baseURL = new URL(deps.config.baseURL);
|
|
|
|
this.#baseURL.pathname += '/';
|
|
|
|
} else {
|
|
|
|
this.#baseURL = deps.config.baseURL;
|
|
|
|
}
|
|
|
|
this.handleRequest = this.handleRequest.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-23 14:32:44 +03:00
|
|
|
* Get a unique URL with slug for creating unique redirects
|
2022-09-16 11:42:21 +03:00
|
|
|
*
|
2022-09-23 14:32:44 +03:00
|
|
|
* @returns {Promise<URL>}
|
2022-09-16 11:42:21 +03:00
|
|
|
*/
|
2022-09-23 14:32:44 +03:00
|
|
|
async getSlugUrl() {
|
|
|
|
let url;
|
|
|
|
while (!url || await this.#linkRedirectRepository.getByURL(url)) {
|
|
|
|
const slug = crypto.randomBytes(4).toString('hex');
|
2023-02-02 10:01:20 +03:00
|
|
|
url = new URL(`${this.#redirectURLPrefix}${slug}`, this.#baseURL);
|
2022-09-23 14:32:44 +03:00
|
|
|
}
|
|
|
|
return url;
|
2022-09-16 11:42:21 +03:00
|
|
|
}
|
|
|
|
|
2022-10-18 13:03:27 +03:00
|
|
|
/**
|
|
|
|
* @param {Object} options
|
|
|
|
*
|
|
|
|
* @returns {Promise<String[]>}
|
|
|
|
*/
|
|
|
|
async getFilteredIds(options) {
|
|
|
|
return await this.#linkRedirectRepository.getFilteredIds(options);
|
|
|
|
}
|
|
|
|
|
2022-09-14 01:18:19 +03:00
|
|
|
/**
|
2022-09-23 14:32:44 +03:00
|
|
|
* @param {URL} from
|
2022-09-14 01:18:19 +03:00
|
|
|
* @param {URL} to
|
|
|
|
*
|
|
|
|
* @returns {Promise<LinkRedirect>}
|
|
|
|
*/
|
2022-09-23 14:32:44 +03:00
|
|
|
async addRedirect(from, to) {
|
2022-09-14 01:18:19 +03:00
|
|
|
const link = new LinkRedirect({
|
2022-09-23 14:32:44 +03:00
|
|
|
from,
|
|
|
|
to
|
2022-09-14 01:18:19 +03:00
|
|
|
});
|
|
|
|
|
2022-09-16 11:42:21 +03:00
|
|
|
await this.#linkRedirectRepository.save(link);
|
|
|
|
|
2022-09-14 01:18:19 +03:00
|
|
|
return link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').Response} res
|
|
|
|
* @param {import('express').NextFunction} next
|
|
|
|
*
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async handleRequest(req, res, next) {
|
2023-02-08 13:49:15 +03:00
|
|
|
try {
|
|
|
|
// skip handling if original url doesn't match the prefix
|
|
|
|
const fullURLWithRedirectPrefix = `${this.#baseURL.pathname}${this.#redirectURLPrefix}`;
|
|
|
|
// @NOTE: below is equivalent to doing:
|
|
|
|
// router.get('/r/'), (req, res) ...
|
|
|
|
// To make it cleaner we should rework it to:
|
|
|
|
// linkRedirects.service.handleRequest(router);
|
|
|
|
// and mount routes on top like for example sitemapHandler does
|
|
|
|
// Cleanup issue: https://github.com/TryGhost/Toolbox/issues/516
|
|
|
|
if (!req.originalUrl.startsWith(fullURLWithRedirectPrefix)) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = new URL(req.originalUrl, this.#baseURL);
|
|
|
|
const link = await this.#linkRedirectRepository.getByURL(url);
|
|
|
|
|
|
|
|
if (!link) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = RedirectEvent.create({
|
|
|
|
url,
|
|
|
|
link
|
|
|
|
});
|
|
|
|
|
|
|
|
DomainEvents.dispatch(event);
|
|
|
|
|
|
|
|
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
|
|
|
|
return res.redirect(link.to.href);
|
|
|
|
} catch (e) {
|
|
|
|
return next(e);
|
2023-02-02 10:01:20 +03:00
|
|
|
}
|
2022-09-14 01:18:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LinkRedirectsService;
|