zip-js 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,153 +0,0 @@
1
- /* jshint worker:true */
2
- (function main(global) {
3
- "use strict";
4
-
5
- if (global.zWorkerInitialized)
6
- throw new Error('z-worker.js should be run only once');
7
- global.zWorkerInitialized = true;
8
-
9
- addEventListener("message", function(event) {
10
- var message = event.data, type = message.type, sn = message.sn;
11
- var handler = handlers[type];
12
- if (handler) {
13
- try {
14
- handler(message);
15
- } catch (e) {
16
- onError(type, sn, e);
17
- }
18
- }
19
- //for debug
20
- //postMessage({type: 'echo', originalType: type, sn: sn});
21
- });
22
-
23
- var handlers = {
24
- importScripts: doImportScripts,
25
- newTask: newTask,
26
- append: processData,
27
- flush: processData,
28
- };
29
-
30
- // deflater/inflater tasks indexed by serial numbers
31
- var tasks = {};
32
-
33
- function doImportScripts(msg) {
34
- if (msg.scripts && msg.scripts.length > 0)
35
- importScripts.apply(undefined, msg.scripts);
36
- postMessage({type: 'importScripts'});
37
- }
38
-
39
- function newTask(msg) {
40
- var CodecClass = global[msg.codecClass];
41
- var sn = msg.sn;
42
- if (tasks[sn])
43
- throw Error('duplicated sn');
44
- tasks[sn] = {
45
- codec: new CodecClass(msg.options),
46
- crcInput: msg.crcType === 'input',
47
- crcOutput: msg.crcType === 'output',
48
- crc: new Crc32(),
49
- };
50
- postMessage({type: 'newTask', sn: sn});
51
- }
52
-
53
- // performance may not be supported
54
- var now = global.performance ? global.performance.now.bind(global.performance) : Date.now;
55
-
56
- function processData(msg) {
57
- var sn = msg.sn, type = msg.type, input = msg.data;
58
- var task = tasks[sn];
59
- // allow creating codec on first append
60
- if (!task && msg.codecClass) {
61
- newTask(msg);
62
- task = tasks[sn];
63
- }
64
- var isAppend = type === 'append';
65
- var start = now();
66
- var output;
67
- if (isAppend) {
68
- try {
69
- output = task.codec.append(input, function onprogress(loaded) {
70
- postMessage({type: 'progress', sn: sn, loaded: loaded});
71
- });
72
- } catch (e) {
73
- delete tasks[sn];
74
- throw e;
75
- }
76
- } else {
77
- delete tasks[sn];
78
- output = task.codec.flush();
79
- }
80
- var codecTime = now() - start;
81
-
82
- start = now();
83
- if (input && task.crcInput)
84
- task.crc.append(input);
85
- if (output && task.crcOutput)
86
- task.crc.append(output);
87
- var crcTime = now() - start;
88
-
89
- var rmsg = {type: type, sn: sn, codecTime: codecTime, crcTime: crcTime};
90
- var transferables = [];
91
- if (output) {
92
- rmsg.data = output;
93
- transferables.push(output.buffer);
94
- }
95
- if (!isAppend && (task.crcInput || task.crcOutput))
96
- rmsg.crc = task.crc.get();
97
-
98
- // posting a message with transferables will fail on IE10
99
- try {
100
- postMessage(rmsg, transferables);
101
- } catch(ex) {
102
- postMessage(rmsg); // retry without transferables
103
- }
104
- }
105
-
106
- function onError(type, sn, e) {
107
- var msg = {
108
- type: type,
109
- sn: sn,
110
- error: formatError(e)
111
- };
112
- postMessage(msg);
113
- }
114
-
115
- function formatError(e) {
116
- return { message: e.message, stack: e.stack };
117
- }
118
-
119
- // Crc32 code copied from file zip.js
120
- function Crc32() {
121
- this.crc = -1;
122
- }
123
- Crc32.prototype.append = function append(data) {
124
- var crc = this.crc | 0, table = this.table;
125
- for (var offset = 0, len = data.length | 0; offset < len; offset++)
126
- crc = (crc >>> 8) ^ table[(crc ^ data[offset]) & 0xFF];
127
- this.crc = crc;
128
- };
129
- Crc32.prototype.get = function get() {
130
- return ~this.crc;
131
- };
132
- Crc32.prototype.table = (function() {
133
- var i, j, t, table = []; // Uint32Array is actually slower than []
134
- for (i = 0; i < 256; i++) {
135
- t = i;
136
- for (j = 0; j < 8; j++)
137
- if (t & 1)
138
- t = (t >>> 1) ^ 0xEDB88320;
139
- else
140
- t = t >>> 1;
141
- table[i] = t;
142
- }
143
- return table;
144
- })();
145
-
146
- // "no-op" codec
147
- function NOOP() {}
148
- global.NOOP = NOOP;
149
- NOOP.prototype.append = function append(bytes, onprogress) {
150
- return bytes;
151
- };
152
- NOOP.prototype.flush = function flush() {};
153
- })(this);
@@ -1,242 +0,0 @@
1
- /*
2
- Copyright (c) 2013 Gildas Lormeau. All rights reserved.
3
-
4
- Redistribution and use in source and binary forms, with or without
5
- modification, are permitted provided that the following conditions are met:
6
-
7
- 1. Redistributions of source code must retain the above copyright notice,
8
- this list of conditions and the following disclaimer.
9
-
10
- 2. Redistributions in binary form must reproduce the above copyright
11
- notice, this list of conditions and the following disclaimer in
12
- the documentation and/or other materials provided with the distribution.
13
-
14
- 3. The names of the authors may not be used to endorse or promote products
15
- derived from this software without specific prior written permission.
16
-
17
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
18
- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
20
- INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
21
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
- EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
- */
28
-
29
- (function() {
30
- "use strict";
31
-
32
- var ERR_HTTP_RANGE = "HTTP Range not supported.";
33
-
34
- var Reader = zip.Reader;
35
- var Writer = zip.Writer;
36
-
37
- var ZipDirectoryEntry;
38
-
39
- var appendABViewSupported;
40
- try {
41
- appendABViewSupported = new Blob([ new DataView(new ArrayBuffer(0)) ]).size === 0;
42
- } catch (e) {
43
- }
44
-
45
- function HttpReader(url) {
46
- var that = this;
47
-
48
- function getData(callback, onerror) {
49
- var request;
50
- if (!that.data) {
51
- request = new XMLHttpRequest();
52
- request.addEventListener("load", function() {
53
- if (!that.size)
54
- that.size = Number(request.getResponseHeader("Content-Length"));
55
- that.data = new Uint8Array(request.response);
56
- callback();
57
- }, false);
58
- request.addEventListener("error", onerror, false);
59
- request.open("GET", url);
60
- request.responseType = "arraybuffer";
61
- request.send();
62
- } else
63
- callback();
64
- }
65
-
66
- function init(callback, onerror) {
67
- var request = new XMLHttpRequest();
68
- request.addEventListener("load", function() {
69
- that.size = Number(request.getResponseHeader("Content-Length"));
70
- callback();
71
- }, false);
72
- request.addEventListener("error", onerror, false);
73
- request.open("HEAD", url);
74
- request.send();
75
- }
76
-
77
- function readUint8Array(index, length, callback, onerror) {
78
- getData(function() {
79
- callback(new Uint8Array(that.data.subarray(index, index + length)));
80
- }, onerror);
81
- }
82
-
83
- that.size = 0;
84
- that.init = init;
85
- that.readUint8Array = readUint8Array;
86
- }
87
- HttpReader.prototype = new Reader();
88
- HttpReader.prototype.constructor = HttpReader;
89
-
90
- function HttpRangeReader(url) {
91
- var that = this;
92
-
93
- function init(callback, onerror) {
94
- var request = new XMLHttpRequest();
95
- request.addEventListener("load", function() {
96
- that.size = Number(request.getResponseHeader("Content-Length"));
97
- if (request.getResponseHeader("Accept-Ranges") == "bytes")
98
- callback();
99
- else
100
- onerror(ERR_HTTP_RANGE);
101
- }, false);
102
- request.addEventListener("error", onerror, false);
103
- request.open("HEAD", url);
104
- request.send();
105
- }
106
-
107
- function readArrayBuffer(index, length, callback, onerror) {
108
- var request = new XMLHttpRequest();
109
- request.open("GET", url);
110
- request.responseType = "arraybuffer";
111
- request.setRequestHeader("Range", "bytes=" + index + "-" + (index + length - 1));
112
- request.addEventListener("load", function() {
113
- callback(request.response);
114
- }, false);
115
- request.addEventListener("error", onerror, false);
116
- request.send();
117
- }
118
-
119
- function readUint8Array(index, length, callback, onerror) {
120
- readArrayBuffer(index, length, function(arraybuffer) {
121
- callback(new Uint8Array(arraybuffer));
122
- }, onerror);
123
- }
124
-
125
- that.size = 0;
126
- that.init = init;
127
- that.readUint8Array = readUint8Array;
128
- }
129
- HttpRangeReader.prototype = new Reader();
130
- HttpRangeReader.prototype.constructor = HttpRangeReader;
131
-
132
- function ArrayBufferReader(arrayBuffer) {
133
- var that = this;
134
-
135
- function init(callback, onerror) {
136
- that.size = arrayBuffer.byteLength;
137
- callback();
138
- }
139
-
140
- function readUint8Array(index, length, callback, onerror) {
141
- callback(new Uint8Array(arrayBuffer.slice(index, index + length)));
142
- }
143
-
144
- that.size = 0;
145
- that.init = init;
146
- that.readUint8Array = readUint8Array;
147
- }
148
- ArrayBufferReader.prototype = new Reader();
149
- ArrayBufferReader.prototype.constructor = ArrayBufferReader;
150
-
151
- function ArrayBufferWriter() {
152
- var array, that = this;
153
-
154
- function init(callback, onerror) {
155
- array = new Uint8Array();
156
- callback();
157
- }
158
-
159
- function writeUint8Array(arr, callback, onerror) {
160
- var tmpArray = new Uint8Array(array.length + arr.length);
161
- tmpArray.set(array);
162
- tmpArray.set(arr, array.length);
163
- array = tmpArray;
164
- callback();
165
- }
166
-
167
- function getData(callback) {
168
- callback(array.buffer);
169
- }
170
-
171
- that.init = init;
172
- that.writeUint8Array = writeUint8Array;
173
- that.getData = getData;
174
- }
175
- ArrayBufferWriter.prototype = new Writer();
176
- ArrayBufferWriter.prototype.constructor = ArrayBufferWriter;
177
-
178
- function FileWriter(fileEntry, contentType) {
179
- var writer, that = this;
180
-
181
- function init(callback, onerror) {
182
- fileEntry.createWriter(function(fileWriter) {
183
- writer = fileWriter;
184
- callback();
185
- }, onerror);
186
- }
187
-
188
- function writeUint8Array(array, callback, onerror) {
189
- var blob = new Blob([ appendABViewSupported ? array : array.buffer ], {
190
- type : contentType
191
- });
192
- writer.onwrite = function() {
193
- writer.onwrite = null;
194
- callback();
195
- };
196
- writer.onerror = onerror;
197
- writer.write(blob);
198
- }
199
-
200
- function getData(callback) {
201
- fileEntry.file(callback);
202
- }
203
-
204
- that.init = init;
205
- that.writeUint8Array = writeUint8Array;
206
- that.getData = getData;
207
- }
208
- FileWriter.prototype = new Writer();
209
- FileWriter.prototype.constructor = FileWriter;
210
-
211
- zip.FileWriter = FileWriter;
212
- zip.HttpReader = HttpReader;
213
- zip.HttpRangeReader = HttpRangeReader;
214
- zip.ArrayBufferReader = ArrayBufferReader;
215
- zip.ArrayBufferWriter = ArrayBufferWriter;
216
-
217
- if (zip.fs) {
218
- ZipDirectoryEntry = zip.fs.ZipDirectoryEntry;
219
- ZipDirectoryEntry.prototype.addHttpContent = function(name, URL, useRangeHeader) {
220
- function addChild(parent, name, params, directory) {
221
- if (parent.directory)
222
- return directory ? new ZipDirectoryEntry(parent.fs, name, params, parent) : new zip.fs.ZipFileEntry(parent.fs, name, params, parent);
223
- else
224
- throw "Parent entry is not a directory.";
225
- }
226
-
227
- return addChild(this, name, {
228
- data : URL,
229
- Reader : useRangeHeader ? HttpRangeReader : HttpReader
230
- });
231
- };
232
- ZipDirectoryEntry.prototype.importHttpContent = function(URL, useRangeHeader, onend, onerror) {
233
- this.importZip(useRangeHeader ? new HttpRangeReader(URL) : new HttpReader(URL), onend, onerror);
234
- };
235
- zip.fs.FS.prototype.importHttpContent = function(URL, useRangeHeader, onend, onerror) {
236
- this.entries = [];
237
- this.root = new ZipDirectoryEntry(this);
238
- this.root.importHttpContent(URL, useRangeHeader, onend, onerror);
239
- };
240
- }
241
-
242
- })();