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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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');
|
|
|
|
url = new URL(`r/${slug}`, this.#baseURL);
|
|
|
|
}
|
|
|
|
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) {
|
2022-09-16 11:42:21 +03:00
|
|
|
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);
|
|
|
|
|
2022-10-27 06:07:36 +03:00
|
|
|
res.setHeader('X-Robots-Tag', 'noindex, nofollow');
|
2022-09-16 11:42:21 +03:00
|
|
|
return res.redirect(link.to.href);
|
2022-09-14 01:18:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = LinkRedirectsService;
|