question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Library not working with Ember.js because of extended array prototype

See original GitHub issue

Configuration:

  • Web browser and its version: chrome Version 78.0.3904.108
  • Operating system and its version: MacOs
  • PDF.js version: 2.0.943
  • Is a browser extension:

This is not an issue with the lib itself, but hopefully I can ask for feedback and help.

I am using this library in an ember.js project. Usually, everything works well, but some pdf do not render the text correctly, and I get Error during font loading: this._map[t].charCodeAt is not a function as an error. Searching around, I found #7064 and #7318 that explains that the fact that Ember extend the Array prototype and make them enumerable seems to be causing a problem.

A fix that works is to disable globally Ember array extension, but our app is quite large and manually going into the code to make sure every array is an Ember Array would be extremely cumbersome.

So, my questions:

  1. Is there any possible reason why some file would display correctly why other won’t? Could it be a font or encodage issue? I tried loading a file that does not display correctly with the simple viewer demo provided and it works, so I am thinking this is possibly not the issue, but it’s still weird that some file work correctly while some don’t.

The way it works is that an external user add text to the pdf with an editing pdf program, and then upload the pdf to our web site that they then use a PDF.js based viewer to look at, and this is where sometime the text does not get displayed with the error.

  1. Is there any modification I could make to the library code itself that would make this work, without having to disable globally my ember array extension?

I found this commit and I tried to make the same modification to my node_modules/pdfjs-dist/lib/core/fonts.js file but it had no effect. That kind of solution would be perfect for me though, even if I lose some performance.

Thank you very much!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
charlesfriescommented, Apr 29, 2021

I’m sure the code below has some potentially dire implications, but simply setting enumerable to false on the _super array prototype fixes the issue for me, without breaking the rest of my app [Ember v3.25.1 (Octane)]

Object.defineProperty(Array.prototype, '_super', {
  enumerable: false,
});
2reactions
timvandermeijcommented, Dec 18, 2019

PDF.js requires that array are implemented in a standard way, so either a native implementation or one that implements everything from the specification. If not, you’ll need to polyfill the implementation yourself. There are existing polyfills for arrays, for example in core-js. However, this is outside of the scope of PDF.js and we would recommend to disable any Ember-specific overrides of native JavaScript primitives because you’re bound to run into issues sooner or later.

Is there any possible reason why some file would display correctly why other won’t?

Depending on the PDF file itself (based on encoding, font types, et cetera), other code paths will be taken, so not all may require charCodeAt to be present. It’s not possible to say in general without code inspection.

Is there any modification I could make to the library code itself that would make this work, without having to disable globally my ember array extension?

The extensions are in fact breaking normal JavaScript behavior, so it’s very much not recommended, but if you really need to you can use existing polyfills at outlined above. They might conflict with Ember’s modifications though, but for that you’ll need to ask Ember developers for support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better fix than globally disabling prototype extension to use ...
It seems that the problem is caused by Ember, since it's adding a number of methods on Arrays, but for some reason they...
Read more >
Deprecate array prototype extensions - Ember RFCs
This RFC proposes to deprecate array prototype extensions. Motivation. Ember historically extended the prototypes of native Javascript arrays to ...
Read more >
Why Ember.A() is used in component? - Design
It might be interesting to note that Ember can extend the Array.prototype so that you don't ever even have to use Ember.A() at...
Read more >
Disabling Prototype Extensions - Configuring Ember.js
By default, Ember.js will extend the prototypes of native JavaScript objects in the following ways: - Array is extended to implement the Ember....
Read more >
Deprecations Added in Ember 1.x
Ember.js libraries and plugins. If the code triggering this deprecation is being fired from a library, that library may need to update its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found