1
1
/*!
2
2
3
- JSZip v3.1.3 - A Javascript class for generating and reading zip files
3
+ JSZip v3.1.5 - A JavaScript class for generating and reading zip files
4
4
<http://stuartk.com/jszip>
5
5
6
6
(c) 2009-2016 Stuart Knightley <stuart [at] stuartk.com>
@@ -1057,7 +1057,7 @@ JSZip.defaults = require('./defaults');
1057
1057
1058
1058
// TODO find a better way to handle this version,
1059
1059
// a require('package.json').version doesn't work with webpack, see #327
1060
- JSZip . version = "3.1.3 " ;
1060
+ JSZip . version = "3.1.5 " ;
1061
1061
1062
1062
JSZip . loadAsync = function ( content , options ) {
1063
1063
return new JSZip ( ) . loadAsync ( content , options ) ;
@@ -1281,14 +1281,32 @@ module.exports = {
1281
1281
*/
1282
1282
isNode : typeof Buffer !== "undefined" ,
1283
1283
/**
1284
- * Create a new nodejs Buffer.
1284
+ * Create a new nodejs Buffer from an existing content .
1285
1285
* @param {Object } data the data to pass to the constructor.
1286
1286
* @param {String } encoding the encoding to use.
1287
1287
* @return {Buffer } a new Buffer.
1288
1288
*/
1289
- newBuffer : function ( data , encoding ) {
1289
+ newBufferFrom : function ( data , encoding ) {
1290
+ // XXX We can't use `Buffer.from` which comes from `Uint8Array.from`
1291
+ // in nodejs v4 (< v.4.5). It's not the expected implementation (and
1292
+ // has a different signature).
1293
+ // see https://github.com/nodejs/node/issues/8053
1294
+ // A condition on nodejs' version won't solve the issue as we don't
1295
+ // control the Buffer polyfills that may or may not be used.
1290
1296
return new Buffer ( data , encoding ) ;
1291
1297
} ,
1298
+ /**
1299
+ * Create a new nodejs Buffer with the specified size.
1300
+ * @param {Integer } size the size of the buffer.
1301
+ * @return {Buffer } a new Buffer.
1302
+ */
1303
+ allocBuffer : function ( size ) {
1304
+ if ( Buffer . alloc ) {
1305
+ return Buffer . alloc ( size ) ;
1306
+ } else {
1307
+ return new Buffer ( size ) ;
1308
+ }
1309
+ } ,
1292
1310
/**
1293
1311
* Find out if an object is a Buffer.
1294
1312
* @param {Object } b the object to test.
@@ -1787,7 +1805,7 @@ DataReader.prototype = {
1787
1805
this . checkIndex ( this . index + offset ) ;
1788
1806
} ,
1789
1807
/**
1790
- * Check that the specifed index will not be too far.
1808
+ * Check that the specified index will not be too far.
1791
1809
* @param {string } newIndex the index to check.
1792
1810
* @throws {Error } an Error if the index is out of bounds.
1793
1811
*/
@@ -2498,24 +2516,19 @@ if (support.nodestream) {
2498
2516
* Apply the final transformation of the data. If the user wants a Blob for
2499
2517
* example, it's easier to work with an U8intArray and finally do the
2500
2518
* ArrayBuffer/Blob conversion.
2501
- * @param {String } resultType the name of the final type
2502
- * @param {String } chunkType the type of the data in the given array.
2503
- * @param {Array } dataArray the array containing the data chunks to concatenate
2519
+ * @param {String } type the name of the final type
2504
2520
* @param {String|Uint8Array|Buffer } content the content to transform
2505
2521
* @param {String } mimeType the mime type of the content, if applicable.
2506
2522
* @return {String|Uint8Array|ArrayBuffer|Buffer|Blob } the content in the right format.
2507
2523
*/
2508
- function transformZipOutput ( resultType , chunkType , dataArray , mimeType ) {
2509
- var content = null ;
2510
- switch ( resultType ) {
2524
+ function transformZipOutput ( type , content , mimeType ) {
2525
+ switch ( type ) {
2511
2526
case "blob" :
2512
- return utils . newBlob ( dataArray , mimeType ) ;
2527
+ return utils . newBlob ( utils . transformTo ( "arraybuffer" , content ) , mimeType ) ;
2513
2528
case "base64" :
2514
- content = concat ( chunkType , dataArray ) ;
2515
2529
return base64 . encode ( content ) ;
2516
2530
default :
2517
- content = concat ( chunkType , dataArray ) ;
2518
- return utils . transformTo ( resultType , content ) ;
2531
+ return utils . transformTo ( type , content ) ;
2519
2532
}
2520
2533
}
2521
2534
@@ -2578,7 +2591,7 @@ function accumulate(helper, updateCallback) {
2578
2591
} )
2579
2592
. on ( 'end' , function ( ) {
2580
2593
try {
2581
- var result = transformZipOutput ( resultType , chunkType , dataArray , mimeType ) ;
2594
+ var result = transformZipOutput ( resultType , concat ( chunkType , dataArray ) , mimeType ) ;
2582
2595
resolve ( result ) ;
2583
2596
} catch ( e ) {
2584
2597
reject ( e ) ;
@@ -2600,8 +2613,6 @@ function StreamHelper(worker, outputType, mimeType) {
2600
2613
var internalType = outputType ;
2601
2614
switch ( outputType ) {
2602
2615
case "blob" :
2603
- internalType = "arraybuffer" ;
2604
- break ;
2605
2616
case "arraybuffer" :
2606
2617
internalType = "uint8array" ;
2607
2618
break ;
@@ -2721,7 +2732,7 @@ else {
2721
2732
}
2722
2733
catch ( e ) {
2723
2734
try {
2724
- var Builder = window . BlobBuilder || window . WebKitBlobBuilder || window . MozBlobBuilder || window . MSBlobBuilder ;
2735
+ var Builder = self . BlobBuilder || self . WebKitBlobBuilder || self . MozBlobBuilder || self . MSBlobBuilder ;
2725
2736
var builder = new Builder ( ) ;
2726
2737
builder . append ( buffer ) ;
2727
2738
exports . blob = builder . getBlob ( 'application/zip' ) . size === 0 ;
@@ -2909,7 +2920,7 @@ var buf2string = function (buf) {
2909
2920
*/
2910
2921
exports . utf8encode = function utf8encode ( str ) {
2911
2922
if ( support . nodebuffer ) {
2912
- return nodejsUtils . newBuffer ( str , "utf-8" ) ;
2923
+ return nodejsUtils . newBufferFrom ( str , "utf-8" ) ;
2913
2924
}
2914
2925
2915
2926
return string2buf ( str ) ;
@@ -3044,30 +3055,33 @@ function string2binary(str) {
3044
3055
3045
3056
/**
3046
3057
* Create a new blob with the given content and the given type.
3047
- * @param {Array[ String|ArrayBuffer] } parts the content to put in the blob. DO NOT use
3058
+ * @param {String|ArrayBuffer } part the content to put in the blob. DO NOT use
3048
3059
* an Uint8Array because the stock browser of android 4 won't accept it (it
3049
3060
* will be silently converted to a string, "[object Uint8Array]").
3061
+ *
3062
+ * Use only ONE part to build the blob to avoid a memory leak in IE11 / Edge:
3063
+ * when a large amount of Array is used to create the Blob, the amount of
3064
+ * memory consumed is nearly 100 times the original data amount.
3065
+ *
3050
3066
* @param {String } type the mime type of the blob.
3051
3067
* @return {Blob } the created blob.
3052
3068
*/
3053
- exports . newBlob = function ( parts , type ) {
3069
+ exports . newBlob = function ( part , type ) {
3054
3070
exports . checkSupport ( "blob" ) ;
3055
3071
3056
3072
try {
3057
3073
// Blob constructor
3058
- return new Blob ( parts , {
3074
+ return new Blob ( [ part ] , {
3059
3075
type : type
3060
3076
} ) ;
3061
3077
}
3062
3078
catch ( e ) {
3063
3079
3064
3080
try {
3065
3081
// deprecated, browser only, old way
3066
- var Builder = window . BlobBuilder || window . WebKitBlobBuilder || window . MozBlobBuilder || window . MSBlobBuilder ;
3082
+ var Builder = self . BlobBuilder || self . WebKitBlobBuilder || self . MozBlobBuilder || self . MSBlobBuilder ;
3067
3083
var builder = new Builder ( ) ;
3068
- for ( var i = 0 ; i < parts . length ; i ++ ) {
3069
- builder . append ( parts [ i ] ) ;
3070
- }
3084
+ builder . append ( part ) ;
3071
3085
return builder . getBlob ( type ) ;
3072
3086
}
3073
3087
catch ( e ) {
@@ -3163,7 +3177,7 @@ var arrayToStringHelper = {
3163
3177
*/
3164
3178
nodebuffer : ( function ( ) {
3165
3179
try {
3166
- return support . nodebuffer && String . fromCharCode . apply ( null , nodejsUtils . newBuffer ( 1 ) ) . length === 1 ;
3180
+ return support . nodebuffer && String . fromCharCode . apply ( null , nodejsUtils . allocBuffer ( 1 ) ) . length === 1 ;
3167
3181
} catch ( e ) {
3168
3182
return false ;
3169
3183
}
@@ -3243,7 +3257,7 @@ transform["string"] = {
3243
3257
return stringToArrayLike ( input , new Uint8Array ( input . length ) ) ;
3244
3258
} ,
3245
3259
"nodebuffer" : function ( input ) {
3246
- return stringToArrayLike ( input , nodejsUtils . newBuffer ( input . length ) ) ;
3260
+ return stringToArrayLike ( input , nodejsUtils . allocBuffer ( input . length ) ) ;
3247
3261
}
3248
3262
} ;
3249
3263
@@ -3258,7 +3272,7 @@ transform["array"] = {
3258
3272
return new Uint8Array ( input ) ;
3259
3273
} ,
3260
3274
"nodebuffer" : function ( input ) {
3261
- return nodejsUtils . newBuffer ( input ) ;
3275
+ return nodejsUtils . newBufferFrom ( input ) ;
3262
3276
}
3263
3277
} ;
3264
3278
@@ -3275,7 +3289,7 @@ transform["arraybuffer"] = {
3275
3289
return new Uint8Array ( input ) ;
3276
3290
} ,
3277
3291
"nodebuffer" : function ( input ) {
3278
- return nodejsUtils . newBuffer ( new Uint8Array ( input ) ) ;
3292
+ return nodejsUtils . newBufferFrom ( new Uint8Array ( input ) ) ;
3279
3293
}
3280
3294
} ;
3281
3295
@@ -3286,17 +3300,11 @@ transform["uint8array"] = {
3286
3300
return arrayLikeToArrayLike ( input , new Array ( input . length ) ) ;
3287
3301
} ,
3288
3302
"arraybuffer" : function ( input ) {
3289
- // copy the uint8array: DO NOT propagate the original ArrayBuffer, it
3290
- // can be way larger (the whole zip file for example).
3291
- var copy = new Uint8Array ( input . length ) ;
3292
- if ( input . length ) {
3293
- copy . set ( input , 0 ) ;
3294
- }
3295
- return copy . buffer ;
3303
+ return input . buffer ;
3296
3304
} ,
3297
3305
"uint8array" : identity ,
3298
3306
"nodebuffer" : function ( input ) {
3299
- return nodejsUtils . newBuffer ( input ) ;
3307
+ return nodejsUtils . newBufferFrom ( input ) ;
3300
3308
}
3301
3309
} ;
3302
3310
@@ -3472,7 +3480,8 @@ exports.prepareContent = function(name, inputData, isBinary, isOptimizedBinarySt
3472
3480
3473
3481
if ( ! dataType ) {
3474
3482
return external . Promise . reject (
3475
- new Error ( "The data of '" + name + "' is in an unsupported format !" )
3483
+ new Error ( "Can't read the data of '" + name + "'. Is it " +
3484
+ "in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?" )
3476
3485
) ;
3477
3486
}
3478
3487
// special case : it's way easier to work with Uint8Array than with ArrayBuffer
@@ -3515,7 +3524,7 @@ function ZipEntries(loadOptions) {
3515
3524
}
3516
3525
ZipEntries . prototype = {
3517
3526
/**
3518
- * Check that the reader is on the speficied signature.
3527
+ * Check that the reader is on the specified signature.
3519
3528
* @param {string } expectedSignature the expected signature.
3520
3529
* @throws {Error } if it is an other signature.
3521
3530
*/
@@ -3691,7 +3700,7 @@ ZipEntries.prototype = {
3691
3700
3692
3701
/*
3693
3702
Warning : the zip64 extension is supported, but ONLY if the 64bits integer read from
3694
- the zip file can fit into a 32bits integer. This cannot be solved : Javascript represents
3703
+ the zip file can fit into a 32bits integer. This cannot be solved : JavaScript represents
3695
3704
all numbers as 64-bit double precision IEEE 754 floating point numbers.
3696
3705
So, we have 53bits for integers and bitwise operations treat everything as 32bits.
3697
3706
see https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators
@@ -4093,20 +4102,29 @@ ZipObject.prototype = {
4093
4102
* @return StreamHelper the stream.
4094
4103
*/
4095
4104
internalStream : function ( type ) {
4096
- var outputType = type . toLowerCase ( ) ;
4097
- var askUnicodeString = outputType === "string" || outputType === "text" ;
4098
- if ( outputType === "binarystring" || outputType === "text" ) {
4099
- outputType = "string" ;
4100
- }
4101
- var result = this . _decompressWorker ( ) ;
4105
+ var result = null , outputType = "string" ;
4106
+ try {
4107
+ if ( ! type ) {
4108
+ throw new Error ( "No output type specified." ) ;
4109
+ }
4110
+ outputType = type . toLowerCase ( ) ;
4111
+ var askUnicodeString = outputType === "string" || outputType === "text" ;
4112
+ if ( outputType === "binarystring" || outputType === "text" ) {
4113
+ outputType = "string" ;
4114
+ }
4115
+ result = this . _decompressWorker ( ) ;
4102
4116
4103
- var isUnicodeString = ! this . _dataBinary ;
4117
+ var isUnicodeString = ! this . _dataBinary ;
4104
4118
4105
- if ( isUnicodeString && ! askUnicodeString ) {
4106
- result = result . pipe ( new utf8 . Utf8EncodeWorker ( ) ) ;
4107
- }
4108
- if ( ! isUnicodeString && askUnicodeString ) {
4109
- result = result . pipe ( new utf8 . Utf8DecodeWorker ( ) ) ;
4119
+ if ( isUnicodeString && ! askUnicodeString ) {
4120
+ result = result . pipe ( new utf8 . Utf8EncodeWorker ( ) ) ;
4121
+ }
4122
+ if ( ! isUnicodeString && askUnicodeString ) {
4123
+ result = result . pipe ( new utf8 . Utf8DecodeWorker ( ) ) ;
4124
+ }
4125
+ } catch ( e ) {
4126
+ result = new GenericWorker ( "error" ) ;
4127
+ result . error ( e ) ;
4110
4128
}
4111
4129
4112
4130
return new StreamHelper ( result , outputType , "" ) ;
0 commit comments