简体   繁体   English

Node.js / npm-包中的动态服务发现

[英]Node.js/npm - dynamic service discovery in packages

I was wondering whether Node.js/npm include any kind of exension mechanism comparable to Python setuptools' "entry points" . 我想知道Node.js / npm是否包含任何类似于Python setuptools的“入口点”的扩展机制。

So, in short: 因此,简而言之:

  • is there any way I can do dynamic discovery of services provided by other packages using npm? 有什么方法可以使用npm动态发现其他软件包提供的服务?
  • if not, what would be the best way to implement something similar? 如果没有,实现类似的最佳方法是什么? Specifying the extension name in the main module's configuration file seems to be the logical solution, but I wonder whether something "automatic" can be done. 在主模块的配置文件中指定扩展名似乎是合乎逻辑的解决方案,但我想知道是否可以执行“自动”操作。

I'm not aware of any builtin mechanism to do this. 我不知道有任何内置的机制可以做到这一点。

One viable way of doing it yourself: 一种自己做的可行方法:
I made a small tool (Jumpstart) to quickly create project scaffolding from templates with placeholders, and I used a kind of plugin mechanism for that. 我制作了一个小工具(Jumpstart),可以使用占位符从模板快速创建项目支架,为此我使用了一种插件机制。 It basically comes down to that the Jumpstart script searches for modules named jumpstart-* "adjacent" to where the module itself is installed. 从根本上讲,Jumpstart脚本搜索名为“ jumpstart-* ”与模块本身安装位置相邻的模块。 So it would work for both local and global installations. 因此,它适用于本地和全局安装。 If installed locally, it would search the other local modules (on the same level) and if global, it searches the other global modules. 如果在本地安装,它将搜索其他本地模块(在同一级别上),如果是全局安装,它将搜索其他全局模块。

Note that here, "search" comes down to a simple fs.exists check to see if there's a Jumpstart template module with a particular name installed. 请注意,这里的“搜索”可以归结为简单的fs.exists检查是否安装了具有特定名称的Jumpstart模板模块。 However, nothing would stand in the way to actually get a full list of all installed packages matching the jumpstart-* pattern, and loading all at once. 但是,没有任何办法可以真正获得与jumpstart-*模式匹配的所有已安装软件包的完整列表,并一次全部加载。 I could also search up the entire directory tree for node_modules directories and do the same. 我还可以在整个目录树中搜索node_modules目录,然后执行相同的操作。 There's no point in doing this for this particular program, however. 但是,对于此特定程序没有必要这样做。

See https://npmjs.org/package/jumpstart for docs. 有关文档,请参见https://npmjs.org/package/jumpstart

The only limitation to this technique is that all modules must be named in a consistent fashion. 此技术的唯一限制是所有模块必须以一致的方式命名。 Start with some string, end with some string, something like that. 以某个字符串开头,以某个字符串结尾,类似这样。 Any rogue packages polluting the namespace could be detected by doing further checks on a package contents: What files does it contain? 通过对软件包内容进行进一步检查,可以检测到任何污染名称空间的恶意软件包:它包含哪些文件? What kind of object does its main module export? 它的主要模块输出哪种对象? etc. 等等

Brunch also uses a plugin mechanism. 早午餐还使用插件机制。 This one actually deals with file extensions, so is more relevant: https://github.com/brunch/brunch/wiki/Plugins . 这个实际上处理文件扩展名,因此更相关: https : //github.com/brunch/brunch/wiki/Plugins See for example source of the CoffeeScript plugin https://github.com/brunch/coffee-script-brunch/blob/master/src/index.coffee . 请参阅例如CoffeeScript插件的来源https://github.com/brunch/coffee-script-brunch/blob/master/src/index.coffee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM