Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

javascript - 将yaml字符串转换为函数引用(不调用函数)(Convert yaml string to function reference (not call the function))

For clarity.

(为了清楚。)

I am working on a plugin for hapijs (17.0.0+) that will read in a yaml file of api routes, parse it into an array of hapijs valid route objects, and pass that array into the server.route() method in the plugin.

(我正在为hapijs (17.0.0+)开发一个插件,该插件将读取api路由的yaml文件,将其解析为hapijs有效路由对象的数组,并将该数组传递到api中的server.route()方法中。插入。)

This is all working correctly and as I had hoped except for one small caveat, I cannot get the handler function names converted to actual function references in the objects I create during the parse phase.

(这一切都正常工作,除了一个小的警告,正如我希望的那样,我无法在解析阶段将创建的对象中的处理函数名称转换为实际函数引用。)

Because this is a plugin the normal global[<function name>] does not have access to the correct scope to resolve the functions in question (they live in another module, or in the main server script but would never live in this plugin) and I am desperately trying to avoid a solution that involves either eval() or new Function() due to the security concerns.

(因为这是一个插件,所以普通的global[<function name>]无权访问正确的范围来解析有问题的功能(它们存在于另一个模块或主服务器脚本中,但永远不会存在于此插件中)并且由于安全性考虑,我极力尝试避免涉及eval()new Function()的解决方案。)

This plugin only gets called once at the beginning of server startup to load the route table so performance concerns do not really come into it, but eval() is a serious code smell for me and if I cannot find another solution I will end up abandoning yaml and choose another means to accomplish this task.

(此插件在服务器启动开始时仅被调用一次以加载路由表,因此实际上并没有涉及到性能问题,但是eval()对我来说是一种严重的代码味道,如果我找不到其他解决方案,我将最终放弃yaml,然后选择另一种方式来完成此任务。)

My two current train's of thought are to either find a way (trick) to make yaml parse a value as a literal (function name) rather than a string, or to find a way to look for the function name on the calling module's global[] rather than that of my own module.

(我目前的两种思路是找到一种方法(技巧),使yaml将值解析为文字(函数名)而不是字符串,或者找到一种方法来在调用模块的global[]上查找函数名。 global[]而不是我自己的模块。)

So far I cannot find that either of these is possible.

(到目前为止,我找不到这两种可能性中的任何一种。)

yaml route example:

(yaml路由示例:)

- method: GET
  path: /
  description: "Test base route"
  handler: baseRoute
  tags:
    - test
    - base
- method: GET
  path: /users
  description: "Get users"
  handler: getUsers
  tags:
    - users
    - list

Current module implementation (with some debugging oddities still included):

(当前的模块实现(仍然包括一些调试问题):)

'use strict';

const fs = require('fs');
const Path = require('path');
const YAML = require('js-yaml');

module.exports = {
  pkg: require('./package.json'),
  register: async function (server, options) {

    let routeInput = null;
    let routes = [];
    fs.readFile(Path.join(__dirname, 'routes.yml'), (err, data) => {
      if (err) {
        console.error(err);
      } else {
        routeInput = YAML.safeLoad(data.toString(), { onWarning: (err) => { console.error(err) }, json: true });
        routeInput.forEach((r) => {
          console.log(getFunction(r.handler, server));
          routes.push({
            method: r.method,
            path: r.path,
            handler: r.handler,
            options: {
              description: r.description,
              tags: r.tags
            }
          })
        });
        server.route(routes);
      }
    });
  }
};

let getFunction = function getFunction(name, server) {

  let fn = name.toString().trim();

  console.log(fn);

  if (fn in server) {
    console.log('in global')
  }
  console.log(typeof(server));
  console.log(typeof(server[fn]));
}

Any suggestions or tricks to make this possible would be appreciated.

(任何建议或使之成为可能的技巧将不胜感激。)

  ask by Blanning translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...