Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"array" type does not support Uint8Array #620

Closed
dullin opened this issue Apr 4, 2017 · 3 comments
Closed

"array" type does not support Uint8Array #620

dullin opened this issue Apr 4, 2017 · 3 comments

Comments

@dullin
Copy link

dullin commented Apr 4, 2017

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
Copy link
Contributor

@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
Copy link
Author

dullin commented Apr 4, 2017

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
Copy link
Contributor

@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)

@SheetJSDev SheetJSDev changed the title __hexlify doesn't work correctly when input is Uint8Array (Fails to read file when input is Uint8Array ) "array" type does not support Uint8Array Apr 4, 2017
saarCiklum pushed a commit to Folcon/js-xlsx that referenced this issue Aug 20, 2020
- phased out Array map invocations (fixes SheetJS#620 h/t @dullin)
- "array" type supports Uint8Array
- Headless browser examples
- blank cells are omitted or stubbed (fixes SheetJS#779 h/t @Domxa)
- removed transferrables from demo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants