i18ntk
Version:
🚀 The fastest i18n toolkit with 97% performance boost! Zero-dependency, enterprise-grade internationalization for React, Vue, Angular, Python, Java, PHP & more. Features PIN protection, auto framework detection, 7+ UI languages, and comprehensive transla
39 lines (33 loc) • 878 B
JavaScript
const path = require('path');
function loadOptionalModule(name, cwd = process.cwd()) {
// Sanitize the module name to prevent path traversal
const sanitizedName = name.replace(/[^a-zA-Z0-9@/_-]/g, '');
if (sanitizedName !== name) {
// If the name was changed, it was invalid
return null;
}
try {
const resolved = require.resolve(sanitizedName, { paths: [cwd] });
return require(resolved);
} catch {
return null;
}
}
class PluginLoader {
constructor() {
this.plugins = {};
}
registerPlugin(plugin) {
if (!plugin || !plugin.type) return;
const type = plugin.type;
if (!this.plugins[type]) {
this.plugins[type] = [];
}
this.plugins[type].push(plugin);
}
getPlugins(type) {
return this.plugins[type] || [];
}
}
module.exports = PluginLoader;
module.exports.loadOptionalModule = loadOptionalModule;