aadsm/jschardet

TypeError: Cannot assign to read only property '1' of string ''

maiermic opened this issue · 14 comments

The error

ERROR TypeError: Cannot assign to read only property '1' of string '��'
    at SJISProber.feed (sjisprober.js:73)
    at MBCSGroupProber.CharSetGroupProber.feed (charsetgroupprober.js:69)
    at UniversalDetector.feed (universaldetector.js:156)
    at runUniversalDetector (index.js:52)
    at Object.push.rVdK.exports.detect (index.js:34)
    at FileReader.fileReader.onload (app.component.ts:21)
    at ZoneDelegate.invoke (zone-evergreen.js:364)
    at Object.onInvoke (core.js:28494)
    at ZoneDelegate.invoke (zone-evergreen.js:363)
    at Zone.runGuarded (zone-evergreen.js:133)

is thrown in

this._mLastChar[1] = aBuf[0];

in my Angular 11 project, when I select cp1252.txt in the input of my AppComponent

app.component.ts

import { Component } from '@angular/core';
import * as jschardet from 'jschardet';

@Component({
  selector: 'app-root',
  template: `
      <input type="file" id="file" (change)="decode($event)">
  `
})
export class AppComponent {
  decode(e: any) {
    const file = e.target.files[0];
    console.log(typeof file);
    const fileReader = new FileReader();
    fileReader.onload = function() {
      const array = new Uint8Array(fileReader.result as ArrayBuffer);
      let string = "";
      for (let i = 0; i < array.length; ++i) {
        string += String.fromCharCode(array[i]);
      }
      console.log(jschardet.detect(string));
    };
    fileReader.readAsArrayBuffer(file);
  }
}

I only have this issue in a larger Angular project. I tried to reproduce it in a new Angular project as minimal example, but the TypeError is not thrown in this project. I suspect that some (build) configuration is different between the two projects, but I can't think of what that might be. Do you have any idea?

Besides, what should

this._mLastChar[1] = aBuf[0];

actually do? this._mLastChar is a string. They are immutable in JavaScript, aren't they?


https://stackoverflow.com/q/68568242/1065654

The error only occurs in strict mode, but I use the same tsconfig.json and tsconfig.app.json in both projects. Even angular.json, package.json and package-lock.json are the same. Why is my large project running jschardet in strict mode, but my other Angular app not? 😕

I discovered a workaround. It is working if I use a dynamic import

const jschardet = await import('jschardet');
console.log(jschardet.detect(string));

These are immutable. These lines do nothing. I have no idea what the author intended to do there - it may be some kind of translation from C or Java. In which case, string slice and concatenation should occur.

aadsm commented

These are immutable. These lines do nothing. I have no idea what the author intended to do there - it may be some kind of translation from C or Java. In which case, string slice and concatenation should occur.

Yap, it's a translation from python, and at the time I had 0 knowledge on python 😅.

@aadsm @danielgindi Thank you very much ❤️

When will a version with this fix be published?

@aadsm Has this fix been published in the meantime? The workaround doesn't seem to work anymore.

@aadsm Has this fix been published in the meantime? The workaround doesn't seem to work anymore.

It was merged but a new version was not released

@danielgindi Thanks, that's what I was afraid of. Are you able to build the project (see #81)? It would be possible to install the library from a Github repository (e.g. fork), but the official repository still contains the old build files (last release 3.0.0):

npm install --save github:aadsm/jschardet#master

It was merged but a new version was not released

@danielgindi any updates regarding when a new version will be released ?

I also have this issue and a new release would be great

+1

I have the same issue I see the fix is merged but not released. Are you planning to release a new version soon? if not is there any work arounds?

aadsm commented

I have the same issue I see the fix is merged but not released. Are you planning to release a new version soon? if not is there any work arounds?

I'm working on this right now. I'm automating the publishing so it's automatic for new commits.