Skip to content

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

@mvillalba

Description

@mvillalba

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions