node-bplist-parser icon indicating copy to clipboard operation
node-bplist-parser copied to clipboard

parseFile() throws TypeError on fs error if no callback is provided

Open mvillalba opened this issue 3 years ago • 0 comments

Given the following sample code:

import * as bPlistParser from "bplist-parser"

const archive = await bPlistParser.parseFile('this-path-does-not-exist.bplist')
    .then((archive) => console.log(archive))
    .catch((err) => console.error(err))

If the path does not exist, instead of it being caught by the .catch() call, this happens:

$ node webarchive.js
/Users/.../node_modules/bplist-parser/bplistParser.js:46
        return callback(err);
               ^

TypeError: callback is not a function
    at ReadFileContext.callback (/Users/martin/.../node_modules/bplist-parser/bplistParser.js:46:16)
    at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:324:13)

Node.js v18.12.1

This is caused by an oversight in the function's code:

    fs.readFile(fileNameOrBuffer, function (err, data) {
      if (err) {
        reject(err);
        return callback(err);
      }
      tryParseBuffer(data);
    });

When the call to readFile() fails, the code rejects the promise and assumes the callback exists, when it might not.

Conversely, a few lines above, this is handled differently, where the buffers are read:

      } catch (ex) {
        err = ex;
        reject(err);
      } finally {
        if (callback) callback(err, result);
      }

A PR with a fix will follow this bug report.

mvillalba avatar Dec 16 '22 07:12 mvillalba