-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit addresses the timeout issue. The current API is synchronous : if JSZip takes too much time to finish its task, the page crashes (it freezes during the task anyway). This commit does a the following : - rewrite the code into workers which can be asynchronous - add the needed public methods - add nodejs stream support - break the compatibility with existing code Workers ------- A worker is like a nodejs stream but with some differences. On the good side : - it works on IE 6-9 without any issue / polyfill - it weights less than the full dependencies bundled with browserify - it forwards errors (no need to declare an error handler EVERYWHERE) On the bad side : To get sync AND async methods on the public API without duplicating a lot of code, this class has `isSync` attribute and some if/then to choose between doing stuff now, or using an async callback. It is dangerously close to releasing Zalgo (see http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony for more). A chunk is an object with 2 attributes : `meta` and `data`. The former is an object containing anything (`percent` for example), see each worker for more details. The latter is the real data (String, Uint8Array, etc). Public API ---------- Each method generating data (generate, asText, etc) gain a stream sibling : generateStream, asTextStream, etc. This will need a solid discussion because I'm not really satified with this. Nodejs stream support --------------------- With this commit, `file(name, data)` accepts a nodejs stream as data. It also adds a `asNodejsStream()` on the StreamHelper. Breaking changes ---------------- The undocumented JSZip.compressions object changed : the object now returns workers to do the job, the previous methods are not used anymore. Not broken yet, but the the `checkCRC32` (when loading a zip file, it synchronously check the crc32 of every files) will need to be replaced.
- Loading branch information
1 parent
d8ab178
commit 0ceb14c
Showing
55 changed files
with
2,490 additions
and
774 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
"undef": true, | ||
"strict": true, | ||
"sub": true, | ||
"es3": true, | ||
|
||
"globals": { | ||
"TextEncoder": false, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: "generateStream(options)" | ||
layout: default | ||
section: api | ||
--- | ||
|
||
__Description__ : Generates the complete zip file asynchronously. | ||
|
||
__Arguments__ | ||
|
||
name | type | default | description | ||
--------------------|----------|---------|------------ | ||
options | object | | the options to generate the zip file, see [the options of `generate()`]({{site.baseurl}}/documentation/api_jszip/generate.html) | ||
|
||
__Metadata__ : this stream generates the following metadata : | ||
|
||
name | type | description | ||
------------|--------|------------ | ||
currentFile | string | the name of the file currently added to the zip file, `null` if none | ||
percent | number | the percent of completion (a double between 0 and 100) | ||
|
||
__Returns__ : a [StreamHelper]({{site.baseurl}}/documentation/api_streamhelper.html). | ||
|
||
__Throws__ : Nothing. | ||
|
||
__Example__ | ||
|
||
```js | ||
zip.generateStream({type:"blob"}).accumulate(function callback(err, content) { | ||
if (err) { | ||
// handle error | ||
} | ||
// see FileSaver.js | ||
saveAs(content, "hello.zip"); | ||
}, function updateCallback(metadata) { | ||
// print progression with metadata.percent and metadata.currentFile | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: "StreamHelper API" | ||
layout: default | ||
section: api | ||
--- | ||
|
||
A `StreamHelper` can be viewed as a pausable stream with some helper methods. | ||
It is not a full featured stream like in nodejs (and can't directly used as one) | ||
but the exposed methods should be enough to write the glue code with other async | ||
libraries : `on('data', function)`, `on('end', function)` and `on('error', function)`. | ||
|
||
It starts paused, be sure to `resume()` it when ready. | ||
|
||
If you are looking for an asynchronous helper without writing glue code, take a | ||
look at `accumulate(function)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
title: "accumulate(callback [,updateCallback])" | ||
layout: default | ||
section: api | ||
--- | ||
|
||
__Description__ : Read the whole stream and call a callback with the complete content. | ||
|
||
__Arguments__ | ||
|
||
name | type | description | ||
----------------|----------|------------ | ||
callback | function | the function called once when the final content is ready. | ||
updateCallback | function | the function called every time the stream updates. This function is optional. | ||
|
||
|
||
The callback function takes 2 parameters : | ||
- the error if any | ||
- the complete content | ||
|
||
The update callback function takes 1 parameter : the metadata (see the [`on` method]({{site.baseurl}}/documentation/api_streamhelper/on.html)). | ||
|
||
__Returns__ : Nothing. | ||
|
||
__Throws__ : Nothing. | ||
|
||
__Example__ | ||
|
||
```js | ||
zip | ||
.generateStream({type:"uint8array"}) | ||
.accumulate(function callback(err, data) { | ||
// err contains the error if something went wrong, null otherwise. | ||
// data contains here the complete zip file as a uint8array (the type asked in generateStream) | ||
}, function updateCallback(metadata) { | ||
// metadata contains for example currentFile and percent, see the generateStream doc. | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: "on(event, callback)" | ||
layout: default | ||
section: api | ||
--- | ||
|
||
__Description__ : Register a listener on an event. | ||
|
||
__Arguments__ | ||
|
||
name | type | description | ||
----------|----------|------------ | ||
event | string | the name of the event. Only 3 events are supported : `data`, `end` and `error`. | ||
callback | function | the function called when the event occurs. See below for the arguments. | ||
|
||
|
||
A `data` callback takes 2 parameters : | ||
- the current chunk of data (in a format specified by the method which | ||
generated this StreamHelper) | ||
- the metadata (see each method to know what's inside) | ||
|
||
A `end` callback does not take any parameter. | ||
|
||
A `error` callback takes an `Error` as parameter. | ||
|
||
The callbacks are executed in with the current `StreamHelper` as `this`. | ||
|
||
__Returns__ : The current StreamHelper object, for chaining. | ||
|
||
__Throws__ : An exception if the event is unkown. | ||
|
||
__Example__ | ||
|
||
```js | ||
zip | ||
.generateStream({type:"uint8array"}) | ||
.on('data', function (data, metadata) { | ||
// data is a Uint8Array because that's the type asked in generateStream | ||
// metadata contains for example currentFile and percent, see the generateStream doc. | ||
}) | ||
.on('error', function (e) { | ||
// e is the error | ||
}) | ||
.on('end', function () { | ||
// no parameter | ||
}) | ||
.resume(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: "pause()" | ||
layout: default | ||
section: api | ||
--- | ||
|
||
__Description__ : Pause the stream if the stream is running. Once paused, the | ||
stream stops sending `data` events. | ||
|
||
__Arguments__ : None. | ||
|
||
__Returns__ : The current StreamHelper object, for chaining. | ||
|
||
__Throws__ : Nothing. | ||
|
||
__Example__ | ||
|
||
```js | ||
zip | ||
.generateStream({type:"uint8array"}) | ||
.on('data', function(chunk) { | ||
|
||
// if we push the chunk to an other service which is overloaded, we can | ||
// pause the stream as backpressure. | ||
this.pause(); | ||
|
||
}).resume(); // start the stream the first time | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: "resume()" | ||
layout: default | ||
section: api | ||
--- | ||
|
||
__Description__ : Resume the stream if the stream is paused. Once resumed, the | ||
stream starts sending `data` events again. | ||
|
||
__Arguments__ : None. | ||
|
||
__Returns__ : The current StreamHelper object, for chaining. | ||
|
||
__Throws__ : Nothing. | ||
|
||
__Example__ | ||
|
||
```js | ||
zip | ||
.generateStream({type:"uint8array"}) | ||
.on('data', function() {...}) | ||
.resume(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.