From b4abe01b2005fe21279355473b49bcfe4a2362a9 Mon Sep 17 00:00:00 2001 From: Micooz Date: Sat, 2 Jun 2018 09:55:11 +0800 Subject: [PATCH] presets: refactor index.js --- bin/cli.js | 8 ++----- src/presets/index.js | 55 +++++++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/bin/cli.js b/bin/cli.js index bdad62b..cd3e7bd 100644 --- a/bin/cli.js +++ b/bin/cli.js @@ -101,12 +101,8 @@ function main() { } if (hasOption('--list-presets')) { - const { presets, legacyPresets } = modules; - console.log(chalk.bold.underline('[Built-In]')); - console.log(presets.join(os.EOL)); - console.log(''); - console.log(chalk.bold.underline('[Deprecated]')); - console.log(legacyPresets ? legacyPresets.map((name) => `${chalk.gray(name)}`).join(os.EOL) : '-'); + const { builtInPresetMap } = modules; + console.log(Object.keys(builtInPresetMap).join(os.EOL)); return; } diff --git a/src/presets/index.js b/src/presets/index.js index 42308bb..8e04724 100644 --- a/src/presets/index.js +++ b/src/presets/index.js @@ -26,7 +26,29 @@ import ObfsTls12TicketPreset from './obfs-tls1.2-ticket'; // others import AeadRandomCipherPreset from './aead-random-cipher'; -const presetMap = { +/** + * check if a class is a valid preset class + * @param clazz + * @returns {boolean} + */ +function checkPresetClass(clazz) { + if (typeof clazz !== 'function') { + return false; + } + // check require hooks + const requiredMethods = [ + 'onDestroy', 'onInit', + 'beforeOut', 'beforeIn', 'clientOut', 'serverIn', 'serverOut', 'clientIn', + 'beforeOutUdp', 'beforeInUdp', 'clientOutUdp', 'serverInUdp', 'serverOutUdp', 'clientInUdp' + ]; + if (requiredMethods.some((method) => typeof clazz.prototype[method] !== 'function')) { + return false; + } + const requiredStaticMethods = ['onCheckParams', 'onCache']; + return !requiredStaticMethods.some((method) => typeof clazz[method] !== 'function'); +} + +export const builtInPresetMap = { // functional 'mux': MuxPreset, @@ -56,35 +78,12 @@ const presetMap = { 'aead-random-cipher': AeadRandomCipherPreset }; -/** - * check if a class is a valid preset class - * @param clazz - * @returns {boolean} - */ -function checkPresetClass(clazz) { - if (typeof clazz !== 'function') { - return false; - } - // check require hooks - const requiredMethods = [ - 'onDestroy', 'onInit', - 'beforeOut', 'beforeIn', 'clientOut', 'serverIn', 'serverOut', 'clientIn', - 'beforeOutUdp', 'beforeInUdp', 'clientOutUdp', 'serverInUdp', 'serverOutUdp', 'clientInUdp' - ]; - if (requiredMethods.some((method) => typeof clazz.prototype[method] !== 'function')) { - return false; - } - const requiredStaticMethods = ['onCheckParams', 'onCache']; - if (requiredStaticMethods.some((method) => typeof clazz[method] !== 'function')) { - return false; - } - return true; -} - export function getPresetClassByName(name, allowPrivate = false) { - let clazz = presetMap[name]; + // load from built-in + let clazz = builtInPresetMap[name]; if (clazz === undefined) { try { + // load from external clazz = require(name); } catch (err) { throw Error(`cannot load preset "${name}" from built-in modules or external`); @@ -98,5 +97,3 @@ export function getPresetClassByName(name, allowPrivate = false) { } return clazz; } - -export const presets = Object.keys(presetMap);