Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mvillalba opened this issue Dec 16, 2022 · 0 comments · May be fixed by #37
Open

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

mvillalba opened this issue Dec 16, 2022 · 0 comments · May be fixed by #37

Comments

@mvillalba
Copy link

mvillalba commented Dec 16, 2022

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 added a commit to mvillalba/node-bplist-parser that referenced this issue Dec 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant