Is it possible to make a new method on a standard nodeJS object such as "fs" using C++/napi etc?

55 Views Asked by At

I'm interested in making my first native C/C++ addon for nodeJS.

Conceptually it's a filesystem method that acts on a path or a file descriptor and is closely related to fs.stat() and friends.

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.

Does anybody know if this can be done or if I have to just make it standalone?

1

There are 1 best solutions below

0
On

I think the only way to do this is to "wrap" the object you want to extend.

Basically make a new module "fs2" that passes through all the standard calls to "fs" that is required by your new module, but also add your new calls, or replace the calls you wish to augment, with your N-API calls.

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. I'm sure there are other SO questions covering all that.