All files / if-run/lib initialize.ts

100% Statements 49/49
100% Branches 6/6
100% Functions 8/8
100% Lines 47/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 1361x   1x     1x   1x 1x 1x   1x                 1x   1x                 1x         1x 6x 1x     5x           1x 6x   5x               1x 6x   6x 1x   5x 1x 1x     5x 5x       6x           1x                 8x   8x 1x     7x 1x     6x   5x                   1x     9x 9x 9x   9x 8x   8x 5x   5x 2x         5x     6x    
import * as path from 'node:path';
 
import {ERRORS} from '@grnsft/if-core/utils';
import {PluginInterface} from '@grnsft/if-core/types';
 
import {storeAggregationMetrics} from './aggregate';
 
import {logger} from '../../common/util/logger';
import {memoizedLog} from '../util/log-memoize';
import {pluginStorage} from '../util/plugin-storage';
 
import {CONFIG, STRINGS} from '../config';
 
import {Context, PluginOptions} from '../../common/types/manifest';
import {PluginStorageInterface} from '../types/plugin-storage';
 
const {
  PluginInitializationError,
  MissingPluginMethodError,
  MissingPluginPathError,
} = ERRORS;
 
const {GITHUB_PATH, NATIVE_PLUGIN} = CONFIG;
const {
  MISSING_METHOD,
  MISSING_PATH,
  NOT_NATIVE_PLUGIN,
  INVALID_MODULE_PATH,
  LOADING_PLUGIN_FROM_PATH,
  INITIALIZING_PLUGIN,
  INITIALIZING_PLUGINS,
} = STRINGS;
 
/**
 * Imports module by given `path`.
 */
const importModuleFrom = async (path: string) => {
  const module = await import(path).catch(error => {
    throw new PluginInitializationError(INVALID_MODULE_PATH(path, error));
  });
 
  return module;
};
 
/**
 * Imports `module` from given `path` and returns requested `method`.
 */
const importAndVerifyModule = async (method: string, path: string) => {
  const pluginModule = await importModuleFrom(path);
 
  return pluginModule[method];
};
 
/**
 * Checks if plugin is missing then rejects with error.
 * Then checks if `path` is starting with github, then grabs the repository name.
 * Imports module, then checks if it's a valid plugin.
 */
const handModule = (method: string, pluginPath: string) => {
  console.debug(LOADING_PLUGIN_FROM_PATH(method, pluginPath), '\n');
 
  if (pluginPath === 'builtin') {
    pluginPath = path.normalize(`${__dirname}/../builtins`);
  } else {
    if (pluginPath?.startsWith(GITHUB_PATH)) {
      const parts = pluginPath.split('/');
      pluginPath = parts[parts.length - 1];
    }
 
    if (!pluginPath.includes(NATIVE_PLUGIN)) {
      memoizedLog(logger.warn, NOT_NATIVE_PLUGIN(pluginPath));
    }
  }
 
  return importAndVerifyModule(method, pluginPath);
};
 
/**
 * Initializes plugin with config.
 */
const initPlugin = async (
  initPluginParams: PluginOptions
): Promise<PluginInterface> => {
  const {
    method,
    path,
    mapping,
    config,
    'parameter-metadata': parameterMetadata,
  } = initPluginParams!;
 
  if (!method) {
    throw new MissingPluginMethodError(MISSING_METHOD);
  }
 
  if (!path) {
    throw new MissingPluginPathError(MISSING_PATH);
  }
 
  const plugin = await handModule(method, path);
 
  return plugin(config, parameterMetadata, mapping);
};
 
/**
 * Registers all plugins from `manifest`.`initialize` property.
 * 1. Initalizes plugin storage.
 * 2. Iterates over plugin names array.
 * 3. While iteration, initalizes current plugin, gathers it's parameters (input/output).
 *    Then stores the aggregation metrics for each parameter to override stub values.
 */
export const initialize = async (
  context: Context
): Promise<PluginStorageInterface> => {
  console.debug(INITIALIZING_PLUGINS, '\n');
  const {plugins} = context.initialize;
  const storage = pluginStorage();
 
  for await (const pluginName of Object.keys(plugins)) {
    console.debug(INITIALIZING_PLUGIN(pluginName));
 
    const plugin = await initPlugin(plugins[pluginName]);
    const parameters = {...plugin.metadata.inputs, ...plugin.metadata.outputs};
 
    Object.keys(parameters).forEach(current => {
      storeAggregationMetrics({
        [current]: parameters[current]['aggregation-method'],
      });
    });
 
    storage.set(pluginName, plugin);
  }
 
  return storage;
};