3 ms·
I agree in principle, but for some reason, the underlying implementation for fs.promises is completely separate. I think this is where fs.promises.readFile rea
by Jarred 6y ago
I agree in principle, but for some reason, the underlying implementation for fs.promises is completely separate.
I think this is where fs.promises.readFile reads the actual file https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L319-L339 https://github.com/nodejs/node/blob/master/lib/internal/fs/p..., with many layers of indirection before that
fs.readFile is this one function: https://github.com/nodejs/node/blob/master/lib/fs.js#L327-L360 https://github.com/nodejs/node/blob/master/lib/fs.js#L327-L3...
- wcarss 6y agoI'm brand new to the code, but I don't think it is as simple as you state. The 'one function' you linked to just passes a ReadFileContext into binding.open[1]. The ReadFileContext appears to have its own read that uses its own allocated Buffers[2] under some circumstances, but under others they are mysteriously already there[3]. Then binding.open refers to an internalBinding of fs[4], which I'm unsure of, is that js or C++ code? This[5] suggests C++ is involved, but there is also an open in lib/fs.js[6] with that signature, though it also just calls binding.open at the end with the same signature, so I think it's off into C++ by then. At that point, I lose the trail. Maybe C++ is even doing the allocating. This is not easy code to grok in its full extent. 1 - https://github.com/nodejs/node/blob/master/lib/fs.js#L356 https://github.com/nodejs/node/blob/master/lib/fs.js#L356 2 - https://github.com/nodejs/node/blob/master/lib/internal/fs/read_file_context.js#L103 https://github.com/nodejs/node/blob/master/lib/internal/fs/r... 3 - https://github.com/nodejs/node/blob/master/lib/internal/fs/read_file_context.js#L108 https://github.com/nodejs/node/blob/master/lib/internal/fs/r... 4 - https://github.com/nodejs/node/blob/master/lib/fs.js#L74 https://github.com/nodejs/node/blob/master/lib/fs.js#L74 5 - https://github.com/nodejs/node/blob/3b2863db12818778671713b5c30363f788912aae/lib/internal/bootstrap/loaders.js#L21 https://github.com/nodejs/node/blob/3b2863db12818778671713b5... 6 - https://github.com/nodejs/node/blob/master/lib/fs.js#L472 https://github.com/nodejs/node/blob/master/lib/fs.js#L472 edit: I was curious so I kept reading, It seems like open is probably C++ here[7], which makes an AsyncCall or SyncCall to "open", which are just wrappers around syscalls[8]. Still not sure about other places the buffer comes from, though. 7 - https://github.com/nodejs/node/blob/master/src/node_file.cc#L1709 https://github.com/nodejs/node/blob/master/src/node_file.cc#... 8 - https://github.com/nodejs/node/blob/606df7c4e79324b9725bfcfe019a8b75bfa04c3f/src/node_file-inl.h#L267 https://github.com/nodejs/node/blob/606df7c4e79324b9725bfcfe...