"array" type does not support Uint8Array #620

Closed
opened 2017-04-04 21:45:13 +00:00 by dullin · 3 comments
dullin commented 2017-04-04 21:45:13 +00:00 (Migrated from github.com)

I've been having problems using a Uint8Array as data for XLSX.read (using type as 'array').

It's throwing an error when trying to read the signature header of the file. I've checked the array against the signature and everything seemed fined.

I've tracked down the issue to __hexlify which tries to convert the first few bytes into a string but fails to do it correctly.

I have an example of the problem here : https://jsfiddle.net/dullin/hchgjs4g/1/

I've currently circumvented the problem by formating my data as a node Buffer which uses it's version of toString instead of the problematic code.

I've been having problems using a Uint8Array as data for XLSX.read (using type as 'array'). It's throwing an error when trying to read the signature header of the file. I've checked the array against the signature and everything seemed fined. I've tracked down the issue to __hexlify which tries to convert the first few bytes into a string but fails to do it correctly. I have an example of the problem here : https://jsfiddle.net/dullin/hchgjs4g/1/ I've currently circumvented the problem by formating my data as a node Buffer which uses it's version of toString instead of the problematic code.
SheetJSDev commented 2017-04-04 22:04:19 +00:00 (Migrated from github.com)

@dullin So this is a funny issue that boils down to the semantics of typedarray: http://www.ecma-international.org/ecma-262/6.0/#sec-%typedarray%.prototype.map

The array mode assumes Array semantics whereby map returns an untyped array, but typed arrays return a typed array. This means you can't do things like map from an array of numbers to an array of strings:

> var x = [1,2,3], y = new Uint8Array(x)
> x.map(function(v) { return v + "!"; })
[ '1!', '2!', '3!' ]
> y.map(function(v) { return v + "!"; })
Uint8Array [ 0, 0, 0 ]

When working with typed arrays like those coming from readAsArrayBuffer, as explained in the wiki and shown in the drag-and-drop sample in the README the web demo pre-processes typed arrays.

We do that type of conversion (mapping from an array of one type to an array of another type) in multiple places :( In this case, we can either add a new input type typedarray or restrict array to only use features that are supported by both arrays and typed arrays.

ping @aymkdn any thoughts?

@dullin So this is a funny issue that boils down to the semantics of typedarray: http://www.ecma-international.org/ecma-262/6.0/#sec-%typedarray%.prototype.map The `array` mode assumes Array semantics whereby `map` returns an untyped array, but typed arrays return a typed array. This means you can't do things like map from an array of numbers to an array of strings: ```js > var x = [1,2,3], y = new Uint8Array(x) > x.map(function(v) { return v + "!"; }) [ '1!', '2!', '3!' ] > y.map(function(v) { return v + "!"; }) Uint8Array [ 0, 0, 0 ] ``` When working with typed arrays like those coming from `readAsArrayBuffer`, as explained [in the wiki](https://github.com/SheetJS/js-xlsx/wiki/Reading-XLSX-from-FileReader.readAsArrayBuffer()) and shown [in the drag-and-drop sample in the README](https://github.com/sheetjs/js-xlsx#parsing-workbooks) the web demo pre-processes typed arrays. We do that type of conversion (mapping from an array of one type to an array of another type) in multiple places :( In this case, we can either add a new input type `typedarray` or restrict `array` to only use features that are supported by both arrays and typed arrays. ping @aymkdn any thoughts?
dullin commented 2017-04-04 22:31:33 +00:00 (Migrated from github.com)

Wow!

I must admit I'm still new to javascript so didn't know the difference between typed and untyped arrays.

Thank you for the thorough and educational explication!

Wow! I must admit I'm still new to javascript so didn't know the difference between typed and untyped arrays. Thank you for the thorough and educational explication!
SheetJSDev commented 2017-04-04 22:38:38 +00:00 (Migrated from github.com)

@dullin typed arrays are relatively new (IE10+, specced in ES6 but not ES5). If you are in node you are better off sticking with Buffer (and the "buffer" type)

@dullin typed arrays are relatively new (IE10+, specced in ES6 but not ES5). If you are in node you are better off sticking with `Buffer` (and the `"buffer"` type)
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#620
No description provided.