electron-userland / electron-userland/electron-webpack

fs.promises API is not working as in node

Open
#255 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
902
Forks
168
PR merge metrics
No merged PRs in 30d

Description

## Introduction
The fs library of node provides an experimental feature: the fs/promises which has the same functionality but with promises.

## The bug

```javascript
import {promises as fsp} from 'fs'
fsp.readFile(SOME_VALID_URL, { encoding: 'utf8' })
```
Considering the code above, the readFile has 2 parameters: the `url` and the `options`.
In theory, the parameter `options` is default to `{encoding:null, flag:'r'}`.
The problem is that the flag won't be default if I run this with electron-webpack. But working if I run the code with node (and with babel of course). So with electron-webpack I get an exception:

```
NodeError: The value "undefined" is invalid for option "flags"
at stringToFlags (internal/fs/utils.js:253:9)
at open (internal/fs/promises.js:198:34)
at Object.readFile (internal/fs/promises.js:466:20)
...
```

The stack trace doesn't tell so much but there's a function in `promises.js`:
```javascript
async function readFile(path, options) {
options = getOptions(options, { flag: 'r' });

if (path instanceof FileHandle)
return readFileHandle(path, options);

const fd = await open(path, options.flag, 0o666);
return readFileHandle(fd, options).finally(fd.close.bind(fd));
}
```
I tested and the getOptions(options, { flag: 'r' }) is giving back the options without modifications. Which is right because of the definition of getOptions:
```javascript
function getOptions(options, defaultOptions) {
if (options === null || options === undefined ||
typeof options === 'function') {
return defaultOptions;
}

if (typeof options === 'string') {
defaultOptions = util._extend({}, defaultOptions);
defaultOptions.encoding = options;
options = defaultOptions;
} else if (typeof options !== 'object') {
throw new ERR_INVALID_ARG_TYPE('options', ['string', 'Object'], options);
}

if (options.encoding !== 'buffer')
assertEncoding(options.encoding);
return options;
}
```
By then, I don't really understand why is it working without electron-webpack. But the node documentation says it is correct to give object into the `options`.
https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options

## Possible workaround

```javascript
import {promises as fsp} from 'fs'
fsp.readFile(SOME_VALID_URL, { encoding: 'utf8' , flag: 'r'})
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.