memfs icon indicating copy to clipboard operation
memfs copied to clipboard

Bind methods to instance

Open privatenumber opened this issue 5 years ago • 2 comments

Description

Native fs methods work without binding, but memfs methods don't

Native fs

const fs = require('fs');
const { readFile } = fs;
readFile('package.json', console.log);

Memfs

const { Volume } = require('memfs');
const fs = Volume.fromJSON({ '/package.json': 'test' });
const { readFile } = fs;
readFile('package.json', console.log);

Get the following error due to a lack of binding:

TypeError: Cannot read property 'wrapAsync' of undefined at Volume.readFile (memfs/lib/volume.js:957:14)

Motivation

A lot of libraries still use promisify instead of calling the native fs promise API:

const mkdir = promisify(options.fs.mkdir);

Source: https://github.com/sindresorhus/make-dir/blob/master/index.js#L50

In this use-case, using memfs breaks due to the error above

Expected behavior

For all memfs methods to be bound to the Volume instance.

privatenumber avatar May 27 '20 07:05 privatenumber

Maybe we should do that in the future.

But for now we have createFsFromVolume method to achieve this behavior.

streamich avatar May 28 '20 20:05 streamich

Ah nice, thank you.

I think it will be more intuitive if it's on by default since I use memfs as a drop-in replacement to fs.

privatenumber avatar May 29 '20 07:05 privatenumber