简体   繁体   English

是否可以在标准 nodeJS object 上创建新方法,例如使用 C++/napi 等的“fs”?

[英]Is it possible to make a new method on a standard nodeJS object such as “fs” using C++/napi etc?

I'm interested in making my first native C/C++ addon for nodeJS.我有兴趣为 nodeJS 制作我的第一个本机 C/C++ 插件。

Conceptually it's a filesystem method that acts on a path or a file descriptor and is closely related to fs.stat() and friends.从概念上讲,它是一种文件系统方法,作用于路径或文件描述符,与fs.stat()和朋友密切相关。

From reading the documentation and looking at N-API examples I can't see whether it's possible or definitely impossible to create a new method.通过阅读文档和查看 N-API 示例,我看不出创建新方法是否可能或绝对不可能。

Does anybody know if this can be done or if I have to just make it standalone?有谁知道这是否可以完成,或者我是否必须让它独立?

I think the only way to do this is to "wrap" the object you want to extend.我认为这样做的唯一方法是“包装”您要扩展的 object。

Basically make a new module "fs2" that passes through all the standard calls to "fs" that is require d by your new module, but also add your new calls, or replace the calls you wish to augment, with your N-API calls.基本上制作一个新模块“fs2”,它通过新模块require的“fs”的所有标准调用,但也添加你的新调用,或者用你的N-API调用替换你想要增加的调用.

Code using your module would then only need to use使用您的模块的代码只需要使用

let fs = require("fs2");

instead of代替

let fs = require("fs");

I'm not sure if there are several ways to wrap a nodeJS module to achieve this, or if there is one clearly best way.我不确定是否有几种方法可以包装 nodeJS 模块来实现这一点,或者是否有一种明显最好的方法。 I'm sure there are other SO questions covering all that.我敢肯定还有其他 SO 问题涵盖了所有这些。

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

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