0

I'm using the pdf-lib package in a TypeScript project and wrote some wrapper code to use it in test automation. It worked fine. We decided to push this wrapper code to a shared project so others could use it too, but then the test breaks. After debugging, it seems that the transpiler may have caused the problem.

Pdf-lib's PDFDict class has a get method that returns a value based on the key parameter. Internally, it is calling this.dict.get(key) where this.dict is a javascript Map object. The key parameter can be of type any.

When running from our project, this.dict.get(key) returns FlateDecode.

When referencing the shared project, this.dict.get(key) returns undefined.

When running from our project, the keys in this.dict and the key object passed into get are all PDFName objects.

When running from the shared project, the keys in this.dict are PDFName2 objects and the key object passed in is a PDFName3 object.

If my assumption that get is returning undefined because the Map key objects and the key passed in are 2 different types (PDFName2 vs PDFName3), did the transpiler screw up here?

Kevin McDowell
  • 532
  • 6
  • 20
  • It's a bit hard to say from this somewhat abstract description. Perhaps someone with integral knowledge of that library might know the answer. In any case, recommend giving more information and/or creating a minimum reproducible example. – Webber Jul 07 '23 at 22:52
  • maybe pdf-lib has multiple versions running at once? – Dimava Jul 07 '23 at 23:49

2 Answers2

0

This sounds like the kind of bug you get when your code bundler is including multiple versions of a library; and that library isn't equipped to deal with that.

If you're using npm, check your lock file for multiple versions of one package and massage your package file so only one version is used. Resolving this could be very easy or impossible depending on what other dependencies you have.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • The shared project uses yarn. Its yarn.lock file has a single pdf-lib reference (v. 1.17.1). My project uses npm and currently has no references to the pdf-lib package. I actually tried completely deleting both repos locally, then re-adding everything from scratch, but still see the same issue. – Kevin McDowell Jul 10 '23 at 17:11
  • 1
    Does the problem persist if you add pdf-lib as a peer dependency instead of a regular dependency? This is probably not what you want, but it's worth trying for the sake of debugging. – Halcyon Jul 12 '23 at 14:31
0

Not sure how, but blowing away my project repo locally, re-cloning it, then re-adding my changes cleared up the issue.

Kevin McDowell
  • 532
  • 6
  • 20