sheetjs icon indicating copy to clipboard operation
sheetjs copied to clipboard

assinging lengh on a buffer?

Open apolishch opened this issue 11 years ago • 7 comments

I am experiencing the following error:

Cannot set property length of [object Object] which has only a getter < at sbcs_d (/Users/abraham/env/dev/import/node_modules/xlsx/dist/cpexcel.js:909:20) < at Object.decode (/Users/abraham/env/dev/import/node_modules/xlsx/dist/cpexcel.js:1198:40) < at _gc2 (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:28:24) < at Buffer.ReadShift as read_shift < at parse_ShortXLUnicodeString (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:3088:21) < at Object.parse_Font as f < at slurp (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:9235:11) < at parse_workbook (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:9356:15) < at parse_xlscfb (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:9833:27) < at readSync (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:11397:11) < at Object.readFileSync (/Users/abraham/env/dev/import/node_modules/xlsx/xlsx.js:11409:9) < at /Users/abraham/env/dev/import/routers/import.js:103:21 < at tryCatcher (/Users/abraham/env/dev/import/node_modules/bluebird/js/main/util.js:26:23) < at Promise.module.exports.Promise._settlePromiseFromHandler (/Users/abraham/env/dev/import/node_modules/bluebird/js/main/promise.js:505:31) < at Promise.module.exports.Promise._settlePromiseAt (/Users/abraham/env/dev/import/node_modules/bluebird/js/main/promise.js:581:18) < at Promise.module.exports.Promise._settlePromises (/Users/abraham/env/dev/import/node_modules/bluebird/js/main/promise.js:697:14)

Looking at the line in question: mdb.length = 2 * len;

Which looks like its assigning a length on a Buffer?

Any insight into how this could happen would be appreciated...

apolishch avatar Oct 23 '15 19:10 apolishch

I'm seeing this as well. Node v.5.0.0.

There seems to be a fair amount of instances where the buffer length is attempted to be set. The docs warn against treating the length property on Buffer as it can create problems. The docs still say it's mutable, but apparently that may no longer the case. buf.length

xlsx-error

var sbcs_decode = function make_sbcs_decode(cp) {
      var D = cpt[cp].dec;
      var DD = new Buffer(131072), d=0, c;
      for(d=0;d<D.length;++d) {
        if(!(c=D[d])) continue;
        var w = c.charCodeAt(0);
        DD[2*d] = w&255; DD[2*d+1] = w>>8;
      }
      return function sbcs_d(data) {
        var len = data.length, i=0, j;
        if(2 * len > mdl) { mdl = 2 * len; mdb = new Buffer(mdl); }
        if(Buffer.isBuffer(data)) {
          for(i = 0; i < len; i++) {
            j = 2*data[i];
            mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
          }
        } else if(typeof data === "string") {
          for(i = 0; i < len; i++) {
            j = 2*data.charCodeAt(i);
            mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
          }
        } else {
          for(i = 0; i < len; i++) {
            j = 2*data[i];
            mdb[2*i] = DD[j]; mdb[2*i+1] = DD[j+1];
          }
        }
        mdb.length = 2 * len;
        return mdb.toString('ucs2');
      };
    };

This is a dependency and it was fixed almost a year ago. I will bump the package and PR.

bryannaegele avatar Dec 03 '15 21:12 bryannaegele

Been running into this problem as well. Why wasn't #331 merged?

psawaya avatar Jan 26 '16 01:01 psawaya

@psawaya The Travis build was broken. There was a chain of issues in js-codepage that had to be resolved first which only @SheetJSDev could correct which has been done in https://github.com/SheetJS/js-codepage/commit/8bc8da47bafccc2bcb05876af355615fca385b28.

I just tried bumping it again but the tests are still broken on newer versions of node. There's more work to be done to get this repo updated. Unfortunately, I don't have time to investigate further at this time. Hopefully @SheetJSDev can provide some insight for you.

bryannaegele avatar Jan 26 '16 05:01 bryannaegele

+1

robgraeber avatar Jun 27 '16 07:06 robgraeber

Complete newb here but I ran into this problem on one of my projects. There's bound to be a better way of fixing this for everyone else but here's what I did:

  1. Updated packages.json to pull in codepage v1.4.0
  2. Ran npm install to download the new files
  3. Copied the cpexcel.full.js and cputils.js files from codepage into the ~/dist folder of xlsx
  4. Renamed cpexcel.js to cpexcel.js.bak and cpexcel.full.js to cpcexcel.js

After that roundabout patching process, I no longer see this error. There is obviously a better way of doing this, but hopefully this helps.

maxjanisse avatar Jul 22 '16 00:07 maxjanisse

+1 I'm struck here. How to get rid of this ?

PManager1 avatar Sep 30 '16 04:09 PManager1

This issue should be resolved in newer versions. We changed the js-codepage dependency version and rebuilt the cpexcel source.

reviewher avatar Mar 12 '17 20:03 reviewher