运行 npm 按需打包为微服务而不在本地安装的最佳方法是什么?

What is the best way to run npm packages on demand as mircoservices without installing it locally?

假设我将这些 npm 包发布到 npm: service1@v1.0 service1@v2.0 service2@v1.0 每个包都有一个功能:

function run(extraStr) {
  return 'package_name_and_version' + extraStr; // i.e. service1 v1.0 extraStr
}

而且我想编写使用这些包的 nodejs 代码而不在本地安装它

var server = require("my-server-sdk");
  // get(package_name, version, function_in_package, arguments, callback)
  server.get('service1', '2.0', 'run', ['app1'], (err, result) => {
  console.log(result); // this should print service1 v2.0 app1
});

其中 my-server-sdk 是一个与我的服务器 api 接口的 sdk,它 安装所需的包并缓存它以备后用 。 最好的方法是什么?有哪些安全问题以及如何预防?

这是我想要的简单图表

注意:service1@v1.0 service1@v2.0 service2@v1.0 只是 npm 中任何包的示例,即 lodash


缓存示例:

假设 TTL 等于 60 分钟

client1lodash 请求了一个函数,在 01:00 从 underscore 请求了另一个函数。 现在在服务器中 lodashunderscore 安装了时间戳 01:00.

client201:30underscore 请求了一个函数,它立即被使用,因为 underscore 是之前安装但时间戳已更新为 1:30.

02:01 lodash 被删除,因为它没有在过去的 TTL currenttime - lodash_timestamp > TTL 上使用,但 underscore 保留,因为currenttime - underscore_timestamp < TTL

因此,当 client302:30 请求 lodash 时,它再次获得 intsalled 02:30 作为 timestamp.

npmi 包,它为 npm install 提供 API。

我会使用的逻辑是:

  1. 从 npm 获取特定的包和版本(如果尚未安装则安装)

  2. 需要nodejs里面的包

  3. 运行指定参数的指定方法
  4. Return结果给客户端

var npmi = require('npmi');
var path = require('path');

function runModule(moduleName, moduleVersion, moduleMethod, moduleMethodParams) {

  return new Promise((resolve, reject) => {

    var options = {
      name: moduleName, // your module name 
      version: moduleVersion, // expected version [default: 'latest'] 
      forceInstall: false, // force install if set to true (even if already installed, it will do a reinstall) [default: false] 
      npmLoad: { // npm.load(options, callback): this is the "options" given to npm.load() 
        loglevel: 'silent' // [default: {loglevel: 'silent'}] 
      }
    };
    options.path = './' + options.name + "@" + options.version,
      npmi(options, function(err, result) {
        if (err) {
          if (err.code === npmi.LOAD_ERR) console.log('npm load error');
          else if (err.code === npmi.INSTALL_ERR) console.log('npm install error');
          console.log(err.message);
          return reject(err)
        }

        // installed 
        console.log(options.name + '@' + options.version + ' installed successfully in ' + path.resolve(options.path));

        var my_module = require(path.resolve(options.path, "node_modules", options.name))

        console.log("Running :", options.name + '@' + options.version)
        console.log("Method :", moduleMethod);
        console.log("With params :", ...moduleMethodParams)
        resolve(my_module[moduleMethod](...moduleMethodParams))
      });

  })

}

runModule('lodash', '4.10.0', 'fill', [Array(3), 2])
  .then(result => console.log("Result :", result))

runModule('lodash', '3.10.0', 'fill', [Array(3), 2])
  .then(result => console.log("Result :", result))

您现在可以看到有 2 个已创建的文件夹 (lodash@3.10.0 , lodash@4.10.0) 指示软件包名称和版本。

我假设 npm 在路径中并且服务器有权在当前目录中安装包,而且 "MODULE_NAME@MODULE_VERSION" 是一个有效的文件夹当前名称OS.