@node-projects/jszip 4.0.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.
package/lib/index.js ADDED
@@ -0,0 +1,63 @@
1
+ import object from './object.js'
2
+ import load from './load.js'
3
+ import support from './support.js'
4
+ import defaults from './defaults.js'
5
+
6
+ export class JSZip extends object {
7
+
8
+ /**
9
+ * Representation a of zip file in js
10
+ * @constructor
11
+ */
12
+ constructor() {
13
+ super();
14
+
15
+ // if this constructor is used without `new`, it adds `new` before itself:
16
+ if (!(this instanceof JSZip)) {
17
+ return new JSZip();
18
+ }
19
+
20
+ if (arguments.length) {
21
+ throw new Error("The constructor with parameters has been removed in JSZip 3.0, please check the upgrade guide.");
22
+ }
23
+
24
+ // object containing the files :
25
+ // {
26
+ // "folder/" : {...},
27
+ // "folder/data.txt" : {...}
28
+ // }
29
+ // NOTE: we use a null prototype because we do not
30
+ // want filenames like "toString" coming from a zip file
31
+ // to overwrite methods and attributes in a normal Object.
32
+ this.files = Object.create(null);
33
+
34
+ this.comment = null;
35
+
36
+ // Where we are in the hierarchy
37
+ this.root = "";
38
+ this.clone = function () {
39
+ var newObj = new JSZip();
40
+ for (var i in this) {
41
+ if (typeof this[i] !== "function") {
42
+ newObj[i] = this[i];
43
+ }
44
+ }
45
+ return newObj;
46
+ };
47
+ }
48
+
49
+ static support = support;
50
+ static defaults = defaults;
51
+
52
+ // TODO find a better way to handle this version,
53
+ // a require('package.json').version doesn't work with webpack, see #327
54
+ static version = "4.0.0";
55
+
56
+ loadAsync(content, options) {
57
+ return load(this, content, options);
58
+ };
59
+
60
+ static loadAsync(content, options) {
61
+ return new JSZip().loadAsync(content, options);
62
+ };
63
+ }
@@ -0,0 +1,11 @@
1
+ /*!
2
+
3
+ JSZip v__VERSION__ - A JavaScript class for generating and reading zip files
4
+ <http://stuartk.com/jszip>
5
+
6
+ (c) 2009-2016 Stuart Knightley <stuart [at] stuartk.com>
7
+ Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip/main/LICENSE.markdown.
8
+
9
+ JSZip uses the library pako released under the MIT license :
10
+ https://github.com/nodeca/pako/blob/main/LICENSE
11
+ */
package/lib/load.js ADDED
@@ -0,0 +1,85 @@
1
+ import utils from "./utils.js";
2
+ import utf8 from "./utf8.js";
3
+ import { ZipEntries } from "./zipEntries.js";
4
+ import { Crc32Probe } from "./stream/Crc32Probe.js";
5
+ import nodejsUtils from "./nodejsUtils.js";
6
+
7
+ /**
8
+ * Check the CRC32 of an entry.
9
+ * @param {ZipEntry} zipEntry the zip entry to check.
10
+ * @return {Promise} the result.
11
+ */
12
+ function checkEntryCRC32(zipEntry) {
13
+ return new Promise(function (resolve, reject) {
14
+ var worker = zipEntry.decompressed.getContentWorker().pipe(new Crc32Probe());
15
+ worker.on("error", function (e) {
16
+ reject(e);
17
+ })
18
+ .on("end", function () {
19
+ if (worker.streamInfo.crc32 !== zipEntry.decompressed.crc32) {
20
+ reject(new Error("Corrupted zip : CRC32 mismatch"));
21
+ } else {
22
+ resolve();
23
+ }
24
+ })
25
+ .resume();
26
+ });
27
+ }
28
+
29
+ export default function (zip, data, options) {
30
+ options = utils.extend(options || {}, {
31
+ base64: false,
32
+ checkCRC32: false,
33
+ optimizedBinaryString: false,
34
+ createFolders: false,
35
+ decodeFileName: utf8.utf8decode
36
+ });
37
+
38
+ if (nodejsUtils.isNode && nodejsUtils.isStream(data)) {
39
+ return Promise.reject(new Error("JSZip can't accept a stream when loading a zip file."));
40
+ }
41
+
42
+ return utils.prepareContent("the loaded zip file", data, true, options.optimizedBinaryString, options.base64)
43
+ .then(function (data) {
44
+ var zipEntries = new ZipEntries(options);
45
+ zipEntries.load(data);
46
+ return zipEntries;
47
+ }).then(function checkCRC32(zipEntries) {
48
+ var promises = [Promise.resolve(zipEntries)];
49
+ var files = zipEntries.files;
50
+ if (options.checkCRC32) {
51
+ for (var i = 0; i < files.length; i++) {
52
+ promises.push(checkEntryCRC32(files[i]));
53
+ }
54
+ }
55
+ return Promise.all(promises);
56
+ }).then(function addFiles(results) {
57
+ var zipEntries = results.shift();
58
+ var files = zipEntries.files;
59
+ for (var i = 0; i < files.length; i++) {
60
+ var input = files[i];
61
+
62
+ var unsafeName = input.fileNameStr;
63
+ var safeName = utils.resolve(input.fileNameStr);
64
+
65
+ zip.file(safeName, input.decompressed, {
66
+ binary: true,
67
+ optimizedBinaryString: true,
68
+ date: input.date,
69
+ dir: input.dir,
70
+ comment: input.fileCommentStr.length ? input.fileCommentStr : null,
71
+ unixPermissions: input.unixPermissions,
72
+ dosPermissions: input.dosPermissions,
73
+ createFolders: options.createFolders
74
+ });
75
+ if (!input.dir) {
76
+ zip.file(safeName).unsafeOriginalName = unsafeName;
77
+ }
78
+ }
79
+ if (zipEntries.zipComment.length) {
80
+ zip.comment = zipEntries.zipComment;
81
+ }
82
+
83
+ return zip;
84
+ });
85
+ };
@@ -0,0 +1,71 @@
1
+ import { GenericWorker } from "../stream/GenericWorker.js";
2
+
3
+ export class NodejsStreamInputAdapter extends GenericWorker {
4
+ /**
5
+ * A worker that use a nodejs stream as source.
6
+ * @constructor
7
+ * @param {String} filename the name of the file entry for this stream.
8
+ * @param {Readable} stream the nodejs stream.
9
+ */
10
+ constructor(filename, stream) {
11
+ super("Nodejs stream input adapter for " + filename);
12
+ this._upstreamEnded = false;
13
+ this._bindStream(stream);
14
+ }
15
+
16
+ /**
17
+ * Prepare the stream and bind the callbacks on it.
18
+ * Do this ASAP on node 0.10 ! A lazy binding doesn't always work.
19
+ * @param {Stream} stream the nodejs stream to use.
20
+ */
21
+ _bindStream(stream) {
22
+ var self = this;
23
+ this._stream = stream;
24
+ stream.pause();
25
+ stream
26
+ .on("data", function (chunk) {
27
+ self.push({
28
+ data: chunk,
29
+ meta: {
30
+ percent: 0
31
+ }
32
+ });
33
+ })
34
+ .on("error", function (e) {
35
+ if (self.isPaused) {
36
+ this.generatedError = e;
37
+ } else {
38
+ self.error(e);
39
+ }
40
+ })
41
+ .on("end", function () {
42
+ if (self.isPaused) {
43
+ self._upstreamEnded = true;
44
+ } else {
45
+ self.end();
46
+ }
47
+ });
48
+ };
49
+
50
+ pause() {
51
+ if (!GenericWorker.prototype.pause.call(this)) {
52
+ return false;
53
+ }
54
+ this._stream.pause();
55
+ return true;
56
+ };
57
+
58
+ resume() {
59
+ if (!GenericWorker.prototype.resume.call(this)) {
60
+ return false;
61
+ }
62
+
63
+ if (this._upstreamEnded) {
64
+ this.end();
65
+ } else {
66
+ this._stream.resume();
67
+ }
68
+
69
+ return true;
70
+ };
71
+ }
@@ -0,0 +1,42 @@
1
+
2
+ //import { Readable } from "readable-stream";
3
+ //var Readable = require("readable-stream").Readable;
4
+
5
+ //import utils from "../utils.js";
6
+ //utils.inherits(NodejsStreamOutputAdapter, Readable);
7
+
8
+ // export class NodejsStreamOutputAdapter extends Readable {
9
+ // /**
10
+ // * A nodejs stream using a worker as source.
11
+ // * @see the SourceWrapper in http://nodejs.org/api/stream.html
12
+ // * @constructor
13
+ // * @param {StreamHelper} helper the helper wrapping the worker
14
+ // * @param {Object} options the nodejs stream options
15
+ // * @param {Function} updateCb the update callback.
16
+ // */
17
+ // constructor(helper, options, updateCb) {
18
+ // Readable.call(this, options);
19
+ // this._helper = helper;
20
+
21
+ // var self = this;
22
+ // helper.on("data", function (data, meta) {
23
+ // if (!self.push(data)) {
24
+ // self._helper.pause();
25
+ // }
26
+ // if (updateCb) {
27
+ // updateCb(meta);
28
+ // }
29
+ // })
30
+ // .on("error", function (e) {
31
+ // self.emit("error", e);
32
+ // })
33
+ // .on("end", function () {
34
+ // self.push(null);
35
+ // });
36
+ // }
37
+
38
+
39
+ // _read() {
40
+ // this._helper.resume();
41
+ // };
42
+ // }
@@ -0,0 +1,55 @@
1
+ export default {
2
+ /**
3
+ * True if this is running in Nodejs, will be undefined in a browser.
4
+ * In a browser, browserify won't include this file and the whole module
5
+ * will be resolved an empty object.
6
+ */
7
+ isNode : typeof Buffer !== "undefined",
8
+ /**
9
+ * Create a new nodejs Buffer from an existing content.
10
+ * @param {Object} data the data to pass to the constructor.
11
+ * @param {String} encoding the encoding to use.
12
+ * @return {Buffer} a new Buffer.
13
+ */
14
+ newBufferFrom: function(data, encoding) {
15
+ if (Buffer.from && Buffer.from !== Uint8Array.from) {
16
+ return Buffer.from(data, encoding);
17
+ } else {
18
+ if (typeof data === "number") {
19
+ // Safeguard for old Node.js versions. On newer versions,
20
+ // Buffer.from(number) / Buffer(number, encoding) already throw.
21
+ throw new Error("The \"data\" argument must not be a number");
22
+ }
23
+ return new Buffer(data, encoding);
24
+ }
25
+ },
26
+ /**
27
+ * Create a new nodejs Buffer with the specified size.
28
+ * @param {Integer} size the size of the buffer.
29
+ * @return {Buffer} a new Buffer.
30
+ */
31
+ allocBuffer: function (size) {
32
+ if (Buffer.alloc) {
33
+ return Buffer.alloc(size);
34
+ } else {
35
+ var buf = new Buffer(size);
36
+ buf.fill(0);
37
+ return buf;
38
+ }
39
+ },
40
+ /**
41
+ * Find out if an object is a Buffer.
42
+ * @param {Object} b the object to test.
43
+ * @return {Boolean} true if the object is a Buffer, false otherwise.
44
+ */
45
+ isBuffer : function(b){
46
+ return Buffer.isBuffer(b);
47
+ },
48
+
49
+ isStream : function (obj) {
50
+ return obj &&
51
+ typeof obj.on === "function" &&
52
+ typeof obj.pause === "function" &&
53
+ typeof obj.resume === "function";
54
+ }
55
+ };