Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `process.binding('fs')` in `fs.js`? [duplicate]

I see in top of the fs.js there is a process.binding('fs').

https://github.com/nodejs/node/blob/master/lib/fs.js#L10:

const binding = process.binding('fs');

And then, it's used as:

binding.open(pathModule._makeLong(path),
           stringToFlags(flag),
           0o666,
           req);

(In https://github.com/nodejs/node/blob/master/lib/fs.js#L303-L306)

My question is:

  • What does process.binding('fs') mean?
  • What's fs here (We already in fs.js)?
  • Where can I find the source code of binding.open? Is it Javascript code or c/c++ code?
like image 459
Freewind Avatar asked Jun 20 '16 07:06

Freewind


People also ask

What does fs do in Javascript?

js fs module provides two different functions for writing files: writeFile and writeFileSync . Both functions take a file path and data as arguments, and write the data to the specified file. However, there is a key difference between the two functions: writeFile is asynchronous, while writeFileSync is synchronous.

How do I copy a file in fs?

The fs. copyFile() method is used to asynchronously copy a file from the source path to destination path. By default, Node. js will overwrite the file if it already exists at the given destination.

What is the use of fs module in node JS?

Commonly used features of the fs module include fs. readFile to read data from a file, fs. writeFile to write data to a file and replace the file if it already exists, fs. watchFile to get notified of changes, and fs.

What is fs unlink?

To delete a file in Node. js, Node FS unlink(path, callback) can be used for asynchronous file operation and unlinkSync(path) can be used for synchronous file operation.


1 Answers

  1. process.binding() is an internal API used by node to get a reference to the various core C++ bindings.
  2. 'fs' in process.binding('fs') is a reference to the C++ binding (src/node_file.cc in the node source tree) for the fs module.
  3. As mentioned, process.binding() references C++ bindings, so in this case binding.open() is exported here and defined here.
like image 190
mscdex Avatar answered Sep 24 '22 17:09

mscdex