kriskowal / zip

An implementation of unzip in JavaScript for Node
MIT License
85 stars 28 forks source link

Port to use bops for binary operations #13

Closed deathcap closed 10 years ago

deathcap commented 10 years ago

Changed to use the bops module, adding native support for both Buffer (in NodeJS) and Uint8Array ArrayBuffer (in the browser).

This is an improvement over https://github.com/kriskowal/zip/pull/12 - which added browserify 3 compatibility, but there is a problem on Firefox: native-buffer-browserify is backed by Object, which is much slower than Typed Arrays:

(Note: currently even Firefox isn't using the Typed Array implementation because of this bug.)

For my purposes, I wanted to pass the unzipped data from zip to a browser API only accepting typed arrays (the W3C File API, if you're curious). Worked fine on Chrome where Buffer is backed by Uint8Array, but on Firefox I get back an Object instead, which has to be manually converted to a typed array (iterating over each element, extremely slow).

bops solves this problem, providing an abstracted API for using typed arrays in the browser and Buffers in NodeJS.

deathcap commented 10 years ago

This PR failed on Travis CI, but for an unrelated reason: NodeJS v0.6 is no longer supported, and fails due to: https://github.com/npm/npm/issues/4450.

Removed 0.6 from .travis.yml to fix this error (0.10 and 0.8 are actively supported); tests now pass.

deathcap commented 10 years ago

Rebased on top of master, but Firefox since fixed their Uint8Array bug, so native-buffer-browserify should now use https://github.com/feross/buffer/issues/20 it which may obviate the need for bops in this case (except for support on older browsers - I'm on the Firefox beta update channel and get 29.0, but https://bugzilla.mozilla.org/show_bug.cgi?id=695438 says Target Milestone: mozilla30 so bops may be worth keeping, need to confirm).

deathcap commented 10 years ago

Latest Firefox beta 29.0 does not support assigning Uint8Array properties:

> a=new Uint8Array(10)
Uint8Array [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
> a.foo=1
1
> a.foo
undefined

(vs Chrome 33, where the a.foo keeps its value), so think switching to bops is still worth doing for the time being.