compressorjs 1.2.1 → 1.4.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,99 +1,84 @@
1
1
  /*!
2
- * Compressor.js v1.2.1
2
+ * Compressor.js v1.4.0
3
3
  * https://fengyuanchen.github.io/compressorjs
4
4
  *
5
5
  * Copyright 2018-present Chen Fengyuan
6
6
  * Released under the MIT license
7
7
  *
8
- * Date: 2023-02-28T14:09:41.732Z
8
+ * Date: 2026-10-01T04:45:36.902Z
9
9
  */
10
10
 
11
11
  'use strict';
12
12
 
13
- function ownKeys(object, enumerableOnly) {
14
- var keys = Object.keys(object);
15
- if (Object.getOwnPropertySymbols) {
16
- var symbols = Object.getOwnPropertySymbols(object);
17
- enumerableOnly && (symbols = symbols.filter(function (sym) {
18
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
19
- })), keys.push.apply(keys, symbols);
20
- }
21
- return keys;
13
+ function _classCallCheck(a, n) {
14
+ if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function");
22
15
  }
23
- function _objectSpread2(target) {
24
- for (var i = 1; i < arguments.length; i++) {
25
- var source = null != arguments[i] ? arguments[i] : {};
26
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
27
- _defineProperty(target, key, source[key]);
28
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
29
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
30
- });
16
+ function _defineProperties(e, r) {
17
+ for (var t = 0; t < r.length; t++) {
18
+ var o = r[t];
19
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
31
20
  }
32
- return target;
33
21
  }
34
- function _classCallCheck(instance, Constructor) {
35
- if (!(instance instanceof Constructor)) {
36
- throw new TypeError("Cannot call a class as a function");
37
- }
22
+ function _createClass(e, r, t) {
23
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
24
+ writable: !1
25
+ }), e;
38
26
  }
39
- function _defineProperties(target, props) {
40
- for (var i = 0; i < props.length; i++) {
41
- var descriptor = props[i];
42
- descriptor.enumerable = descriptor.enumerable || false;
43
- descriptor.configurable = true;
44
- if ("value" in descriptor) descriptor.writable = true;
45
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
46
- }
27
+ function _defineProperty(e, r, t) {
28
+ return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
29
+ value: t,
30
+ enumerable: !0,
31
+ configurable: !0,
32
+ writable: !0
33
+ }) : e[r] = t, e;
47
34
  }
48
- function _createClass(Constructor, protoProps, staticProps) {
49
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
50
- if (staticProps) _defineProperties(Constructor, staticProps);
51
- Object.defineProperty(Constructor, "prototype", {
52
- writable: false
53
- });
54
- return Constructor;
35
+ function _extends() {
36
+ return _extends = Object.assign ? Object.assign.bind() : function (n) {
37
+ for (var e = 1; e < arguments.length; e++) {
38
+ var t = arguments[e];
39
+ for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
40
+ }
41
+ return n;
42
+ }, _extends.apply(null, arguments);
55
43
  }
56
- function _defineProperty(obj, key, value) {
57
- key = _toPropertyKey(key);
58
- if (key in obj) {
59
- Object.defineProperty(obj, key, {
60
- value: value,
61
- enumerable: true,
62
- configurable: true,
63
- writable: true
64
- });
65
- } else {
66
- obj[key] = value;
44
+ function ownKeys(e, r) {
45
+ var t = Object.keys(e);
46
+ if (Object.getOwnPropertySymbols) {
47
+ var o = Object.getOwnPropertySymbols(e);
48
+ r && (o = o.filter(function (r) {
49
+ return Object.getOwnPropertyDescriptor(e, r).enumerable;
50
+ })), t.push.apply(t, o);
67
51
  }
68
- return obj;
52
+ return t;
69
53
  }
70
- function _extends() {
71
- _extends = Object.assign ? Object.assign.bind() : function (target) {
72
- for (var i = 1; i < arguments.length; i++) {
73
- var source = arguments[i];
74
- for (var key in source) {
75
- if (Object.prototype.hasOwnProperty.call(source, key)) {
76
- target[key] = source[key];
77
- }
78
- }
79
- }
80
- return target;
81
- };
82
- return _extends.apply(this, arguments);
54
+ function _objectSpread2(e) {
55
+ for (var r = 1; r < arguments.length; r++) {
56
+ var t = null != arguments[r] ? arguments[r] : {};
57
+ r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
58
+ _defineProperty(e, r, t[r]);
59
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
60
+ Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
61
+ });
62
+ }
63
+ return e;
83
64
  }
84
- function _toPrimitive(input, hint) {
85
- if (typeof input !== "object" || input === null) return input;
86
- var prim = input[Symbol.toPrimitive];
87
- if (prim !== undefined) {
88
- var res = prim.call(input, hint || "default");
89
- if (typeof res !== "object") return res;
65
+ function _toPrimitive(t, r) {
66
+ if ("object" != typeof t || !t) return t;
67
+ var e = t[Symbol.toPrimitive];
68
+ if (void 0 !== e) {
69
+ var i = e.call(t, r || "default");
70
+ if ("object" != typeof i) return i;
90
71
  throw new TypeError("@@toPrimitive must return a primitive value.");
91
72
  }
92
- return (hint === "string" ? String : Number)(input);
73
+ return ("string" === r ? String : Number)(t);
93
74
  }
94
- function _toPropertyKey(arg) {
95
- var key = _toPrimitive(arg, "string");
96
- return typeof key === "symbol" ? key : String(key);
75
+ function _toPropertyKey(t) {
76
+ var i = _toPrimitive(t, "string");
77
+ return "symbol" == typeof i ? i : i + "";
78
+ }
79
+
80
+ function getDefaultExportFromCjs (x) {
81
+ return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
97
82
  }
98
83
 
99
84
  var canvasToBlob = {exports: {}};
@@ -209,7 +194,8 @@ var canvasToBlob = {exports: {}};
209
194
  }
210
195
  })(window);
211
196
  })(canvasToBlob);
212
- var toBlob = canvasToBlob.exports;
197
+ var canvasToBlobExports = canvasToBlob.exports;
198
+ var toBlob = /*@__PURE__*/getDefaultExportFromCjs(canvasToBlobExports);
213
199
 
214
200
  var isBlob = function isBlob(value) {
215
201
  if (typeof Blob === 'undefined') {
@@ -217,22 +203,23 @@ var isBlob = function isBlob(value) {
217
203
  }
218
204
  return value instanceof Blob || Object.prototype.toString.call(value) === '[object Blob]';
219
205
  };
206
+ var isBlob$1 = /*@__PURE__*/getDefaultExportFromCjs(isBlob);
220
207
 
221
208
  var DEFAULTS = {
222
209
  /**
223
- * Indicates if output the original image instead of the compressed one
210
+ * Indicates whether to output the original image instead of the compressed one
224
211
  * when the size of the compressed image is greater than the original one's
225
212
  * @type {boolean}
226
213
  */
227
214
  strict: true,
228
215
  /**
229
- * Indicates if read the image's Exif Orientation information,
216
+ * Indicates whether to read the image's Exif Orientation information,
230
217
  * and then rotate or flip the image automatically.
231
218
  * @type {boolean}
232
219
  */
233
220
  checkOrientation: true,
234
221
  /**
235
- * Indicates if retain the image's Exif information after compressed.
222
+ * Indicates whether to retain the image's Exif information after compression.
236
223
  * @type {boolean}
237
224
  */
238
225
  retainExif: false,
@@ -291,7 +278,7 @@ var DEFAULTS = {
291
278
  /**
292
279
  * Files whose file type is included in this list,
293
280
  * and whose file size exceeds the `convertSize` value will be converted to JPEGs.
294
- * @type {string|Array}
281
+ * @type {string|string[]}
295
282
  */
296
283
  convertTypes: ['image/png'],
297
284
  /**
@@ -301,9 +288,9 @@ var DEFAULTS = {
301
288
  */
302
289
  convertSize: 5000000,
303
290
  /**
304
- * The hook function to execute before draw the image into the canvas for compression.
291
+ * The hook function to execute before drawing the image into the canvas for compression.
305
292
  * @type {Function}
306
- * @param {CanvasRenderingContext2D} context - The 2d rendering context of the canvas.
293
+ * @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
307
294
  * @param {HTMLCanvasElement} canvas - The canvas for compression.
308
295
  * @example
309
296
  * function (context, canvas) {
@@ -312,9 +299,9 @@ var DEFAULTS = {
312
299
  */
313
300
  beforeDraw: null,
314
301
  /**
315
- * The hook function to execute after drew the image into the canvas for compression.
302
+ * The hook function to execute after drawing the image into the canvas for compression.
316
303
  * @type {Function}
317
- * @param {CanvasRenderingContext2D} context - The 2d rendering context of the canvas.
304
+ * @param {CanvasRenderingContext2D} context - The 2D rendering context of the canvas.
318
305
  * @param {HTMLCanvasElement} canvas - The canvas for compression.
319
306
  * @example
320
307
  * function (context, canvas) {
@@ -323,7 +310,7 @@ var DEFAULTS = {
323
310
  */
324
311
  drew: null,
325
312
  /**
326
- * The hook function to execute when success to compress the image.
313
+ * The hook function to execute when the image is compressed successfully.
327
314
  * @type {Function}
328
315
  * @param {File} file - The compressed image File object.
329
316
  * @example
@@ -368,9 +355,9 @@ function toArray(value) {
368
355
  var REGEXP_IMAGE_TYPE = /^image\/.+$/;
369
356
 
370
357
  /**
371
- * Check if the given value is a mime type of image.
358
+ * Check if the given value is an image MIME type.
372
359
  * @param {*} value - The value to check.
373
- * @returns {boolean} Returns `true` if the given is a mime type of image, else `false`.
360
+ * @returns {boolean} Returns `true` if the given value is an image MIME type, else `false`.
374
361
  */
375
362
  function isImageType(value) {
376
363
  return REGEXP_IMAGE_TYPE.test(value);
@@ -379,7 +366,7 @@ function isImageType(value) {
379
366
  /**
380
367
  * Convert image type to extension.
381
368
  * @param {string} value - The image type to convert.
382
- * @returns {boolean} Returns the image extension.
369
+ * @returns {string} Returns the image extension.
383
370
  */
384
371
  function imageTypeToExtension(value) {
385
372
  var extension = isImageType(value) ? value.substr(6) : '';
@@ -406,12 +393,55 @@ function getStringFromCharCode(dataView, start, length) {
406
393
  }
407
394
  return str;
408
395
  }
396
+
397
+ /**
398
+ * Check if `canvas.getContext('2d').getImageData` is available,
399
+ * Firefox randomizes the output of that function in `privacy.resistFingerprinting` mode (#137)
400
+ * @link https://github.com/nodeca/pica/blob/master/lib/utils.js
401
+ * @returns {boolean} Returns `true` if it is available, else `false`.
402
+ */
403
+ function isCanvasAvailable() {
404
+ try {
405
+ var canvas = document.createElement('canvas');
406
+ canvas.width = 2;
407
+ canvas.height = 1;
408
+ var context = canvas.getContext('2d');
409
+ if (!context) return false;
410
+
411
+ // Create 2x1 image data containing RGBA values for two pixels
412
+ var imageData = context.createImageData(2, 1);
413
+
414
+ // First pixel: R=12, G=23, B=34, A=255
415
+ imageData.data[0] = 12;
416
+ imageData.data[1] = 23;
417
+ imageData.data[2] = 34;
418
+ imageData.data[3] = 255;
419
+
420
+ // Second pixel: R=45, G=56, B=67, A=255
421
+ imageData.data[4] = 45;
422
+ imageData.data[5] = 56;
423
+ imageData.data[6] = 67;
424
+ imageData.data[7] = 255;
425
+ context.putImageData(imageData, 0, 0);
426
+ var readBack = context.getImageData(0, 0, 2, 1);
427
+
428
+ // Expected pixel data (matching the written values)
429
+ var expected = [12, 23, 34, 255, 45, 56, 67, 255];
430
+
431
+ // Compare element by element to ensure write and read consistency
432
+ return readBack.data.every(function (value, index) {
433
+ return value === expected[index];
434
+ });
435
+ } catch (error) {
436
+ return false;
437
+ }
438
+ }
409
439
  var btoa = WINDOW.btoa;
410
440
 
411
441
  /**
412
442
  * Transform array buffer to Data URL.
413
443
  * @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
414
- * @param {string} mimeType - The mime type of the Data URL.
444
+ * @param {string} mimeType - The MIME type of the Data URL.
415
445
  * @returns {string} The result Data URL.
416
446
  */
417
447
  function arrayBufferToDataURL(arrayBuffer, mimeType) {
@@ -659,11 +689,12 @@ var AnotherCompressor = WINDOW.Compressor;
659
689
  */
660
690
  var Compressor = /*#__PURE__*/function () {
661
691
  /**
662
- * The constructor of Compressor.
663
- * @param {File|Blob} file - The target image file for compressing.
664
- * @param {Object} [options] - The options for compressing.
692
+ * Create a new Compressor.
693
+ * @param {File|Blob} file - The target image file to compress.
694
+ * @param {Object} [options={}] - The configuration options.
665
695
  */
666
- function Compressor(file, options) {
696
+ function Compressor(file) {
697
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
667
698
  _classCallCheck(this, Compressor);
668
699
  this.file = file;
669
700
  this.exif = [];
@@ -673,13 +704,13 @@ var Compressor = /*#__PURE__*/function () {
673
704
  this.result = null;
674
705
  this.init();
675
706
  }
676
- _createClass(Compressor, [{
707
+ return _createClass(Compressor, [{
677
708
  key: "init",
678
709
  value: function init() {
679
710
  var _this = this;
680
711
  var file = this.file,
681
712
  options = this.options;
682
- if (!isBlob(file)) {
713
+ if (!isBlob$1(file)) {
683
714
  this.fail(new Error('The first argument must be a File or Blob object.'));
684
715
  return;
685
716
  }
@@ -759,10 +790,18 @@ var Compressor = /*#__PURE__*/function () {
759
790
  var file = this.file,
760
791
  image = this.image;
761
792
  image.onload = function () {
762
- _this2.draw(_objectSpread2(_objectSpread2({}, data), {}, {
763
- naturalWidth: image.naturalWidth,
764
- naturalHeight: image.naturalHeight
765
- }));
793
+ if (isCanvasAvailable()) {
794
+ _this2.draw(_objectSpread2(_objectSpread2({}, data), {}, {
795
+ naturalWidth: image.naturalWidth,
796
+ naturalHeight: image.naturalHeight
797
+ }));
798
+ } else {
799
+ _this2.done({
800
+ naturalWidth: image.naturalWidth,
801
+ naturalHeight: image.naturalHeight,
802
+ result: null
803
+ });
804
+ }
766
805
  };
767
806
  image.onabort = function () {
768
807
  _this2.fail(new Error('Aborted to load the image.'));
@@ -985,14 +1024,25 @@ var Compressor = /*#__PURE__*/function () {
985
1024
  if (options.strict && !options.retainExif && result.size > file.size && options.mimeType === file.type && !(options.width > naturalWidth || options.height > naturalHeight || options.minWidth > naturalWidth || options.minHeight > naturalHeight || options.maxWidth < naturalWidth || options.maxHeight < naturalHeight)) {
986
1025
  result = file;
987
1026
  } else {
988
- var date = new Date();
989
- result.lastModified = date.getTime();
990
- result.lastModifiedDate = date;
991
- result.name = file.name;
1027
+ var fileName = file.name;
992
1028
 
993
1029
  // Convert the extension to match its type
994
- if (result.name && result.type !== file.type) {
995
- result.name = result.name.replace(REGEXP_EXTENSION, imageTypeToExtension(result.type));
1030
+ if (fileName && result.type !== file.type) {
1031
+ fileName = fileName.replace(REGEXP_EXTENSION, imageTypeToExtension(result.type));
1032
+ }
1033
+ try {
1034
+ // Convert the resulting Blob object into a File object for modern browsers.
1035
+ result = new File([result], fileName, {
1036
+ type: result.type
1037
+ });
1038
+ } catch (error) {
1039
+ // Fallback to Blob if the File constructor is not supported.
1040
+ var date = new Date();
1041
+ result.name = fileName;
1042
+
1043
+ // The last modified date is not accurate, but it's better than nothing.
1044
+ result.lastModified = date.getTime();
1045
+ result.lastModifiedDate = date;
996
1046
  }
997
1047
  }
998
1048
  } else {
@@ -1031,14 +1081,15 @@ var Compressor = /*#__PURE__*/function () {
1031
1081
  }
1032
1082
 
1033
1083
  /**
1034
- * Get the no conflict compressor class.
1035
- * @returns {Compressor} The compressor class.
1084
+ * Creates a new Compressor instance.
1085
+ * @param {File|Blob} file - The target image file to compress.
1086
+ * @param {Object} [options={}] - The configuration options.
1087
+ * @returns {Compressor} A new Compressor instance.
1036
1088
  */
1037
1089
  }], [{
1038
- key: "noConflict",
1039
- value: function noConflict() {
1040
- window.Compressor = AnotherCompressor;
1041
- return Compressor;
1090
+ key: "create",
1091
+ value: function create(file, options) {
1092
+ return new Compressor(file, options);
1042
1093
  }
1043
1094
 
1044
1095
  /**
@@ -1050,8 +1101,18 @@ var Compressor = /*#__PURE__*/function () {
1050
1101
  value: function setDefaults(options) {
1051
1102
  _extends(DEFAULTS, options);
1052
1103
  }
1104
+
1105
+ /**
1106
+ * Get the no conflict compressor class.
1107
+ * @returns {Compressor} The compressor class.
1108
+ */
1109
+ }, {
1110
+ key: "noConflict",
1111
+ value: function noConflict() {
1112
+ window.Compressor = AnotherCompressor;
1113
+ return Compressor;
1114
+ }
1053
1115
  }]);
1054
- return Compressor;
1055
1116
  }();
1056
1117
 
1057
1118
  module.exports = Compressor;