27 lines
1.2 KiB
JavaScript
27 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
// Reviewed first-party modules only. No filesystem scanning or runtime installs.
|
|
const { manifests } = require('./manifest');
|
|
const { validate } = require('./validation');
|
|
const plugins = Object.freeze({
|
|
weebcentral: require('./weebcentral'),
|
|
mangadex: require('./mangadex'),
|
|
mangapill: require('./mangapill'),
|
|
});
|
|
|
|
function parse(plugin, operation, input) {
|
|
const manifest = manifests[plugin];
|
|
const handler = manifest?.operations.includes(operation) ? plugins[plugin]?.[operation] : null;
|
|
if (typeof handler !== 'function') throw new Error('Unknown source plugin or operation');
|
|
if (!input || typeof input !== 'object' || typeof input.url !== 'string'
|
|
|| (typeof input.html !== 'string' && !(plugin === 'mangadex' && input.data && typeof input.data === 'object'))) {
|
|
throw new Error('Invalid parser input');
|
|
}
|
|
const url = new URL(input.url);
|
|
if (!['https:', 'http:'].includes(url.protocol)) throw new Error('Invalid source URL');
|
|
if (!manifest.sourceHosts.includes(url.hostname)) throw new Error('Source URL does not belong to plugin');
|
|
return validate(plugin, operation, handler({ html: input.html, data: input.data, url: url.href }));
|
|
}
|
|
|
|
module.exports = { parse, manifests };
|