Skip to content

fs.promises API is not working as in node #255

@fxdave

Description

@fxdave

Introduction

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

The bug

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:

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:

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

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions