exifreader 4.50.0 → 4.52.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/src/thumbnail.js CHANGED
@@ -18,7 +18,7 @@ function get(dataView, thumbnailTags, tiffHeaderOffset) {
18
18
 
19
19
  if (fitsInDataView(dataView, offset, length)) {
20
20
  thumbnailTags.type = 'image/jpeg';
21
- const byteOffset = dataView.byteOffset || 0;
21
+ const byteOffset = dataView.byteOffset;
22
22
  thumbnailTags.image = dataView.buffer.slice(byteOffset + offset, byteOffset + offset + length);
23
23
  deferInit(thumbnailTags, 'base64', function () {
24
24
  return getBase64Image(this.image);
package/src/utils.js CHANGED
@@ -2,16 +2,38 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
- import DataViewWrapper from './dataview.js';
6
-
7
5
  export function getDataView(data, byteOffset, byteLength) {
6
+ return new DataView(data, byteOffset, byteLength);
7
+ }
8
+
9
+ /**
10
+ * Views the bytes of an ArrayBuffer, a SharedArrayBuffer, any typed array (a
11
+ * Node Buffer included) or a DataView, without copying them.
12
+ * @param {ArrayBuffer|SharedArrayBuffer|ArrayBufferView} data
13
+ * @returns {DataView} A view over the same bytes as `data`.
14
+ * @throws {TypeError} If `data` is none of the accepted types.
15
+ */
16
+ export function getDataViewFromBytes(data) {
17
+ if (isArrayBufferViewLike(data)) {
18
+ return new DataView(data.buffer, data.byteOffset, data.byteLength);
19
+ }
8
20
  try {
9
- return new DataView(data, byteOffset, byteLength);
21
+ return new DataView(data);
10
22
  } catch (error) {
11
- return new DataViewWrapper(data, byteOffset, byteLength);
23
+ throw new TypeError('Passed data type is unsupported. Pass an ArrayBuffer, SharedArrayBuffer, DataView, a typed array such as a Uint8Array or Node Buffer, or a File.');
12
24
  }
13
25
  }
14
26
 
27
+ /**
28
+ * Tells a typed array or DataView by its shape: older runtimes lack the native check.
29
+ */
30
+ export function isArrayBufferViewLike(data) {
31
+ return !!data
32
+ && !!data.buffer
33
+ && (typeof data.byteOffset === 'number')
34
+ && (typeof data.byteLength === 'number');
35
+ }
36
+
15
37
  export function getStringFromDataView(dataView, offset, length) {
16
38
  const chars = [];
17
39
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
@@ -478,20 +500,22 @@ export const COMPRESSION_METHOD_NONE = undefined;
478
500
  export const COMPRESSION_METHOD_DEFLATE = 0;
479
501
  export const COMPRESSION_METHOD_BROTLI = 'brotli';
480
502
  export const DEFAULT_MAX_DECOMPRESSED_SIZE = 128 * 1024 * 1024;
503
+ // buffer.constants.MAX_LENGTH of 32-bit Node, the lowest maxOutputLength bound of any Node zlib.
504
+ const MAX_CUSTOM_OUTPUT_LENGTH = (1024 * 1024 * 1024) - 1;
481
505
 
482
506
  /**
483
507
  * Returns a copy of the decompress config whose compressed blocks share one
484
508
  * total budget of maxDecompressedSize bytes. The input is never mutated.
485
509
  *
486
510
  * @param {Object|undefined} decompressConfig - The caller's decompress option.
487
- * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned}}
511
+ * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned, failed, pending}}
488
512
  */
489
513
  export function withDecompressBudget(decompressConfig) {
490
514
  return {
491
515
  brotli: decompressConfig ? decompressConfig.brotli : undefined,
492
516
  deflate: decompressConfig ? decompressConfig.deflate : undefined,
493
517
  maxDecompressedSize: decompressConfig ? decompressConfig.maxDecompressedSize : undefined,
494
- budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false}
518
+ budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false, failed: false, pending: undefined}
495
519
  };
496
520
  }
497
521
 
@@ -507,26 +531,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
507
531
  if (decompressConfig && decompressType) {
508
532
  const customFn = decompressConfig[decompressType];
509
533
  if (typeof customFn === 'function') {
510
- // Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
511
- return new Promise((resolve) => resolve(customFn(getUint8View(dataView)))).then((result) => {
512
- budget.remaining -= getResultByteLength(result);
513
- if (budget.remaining < 0) {
514
- return rejectExceedsMax(budget, maxDecompressedSize);
515
- }
516
- if (returnType === 'dataview') {
517
- if (result instanceof ArrayBuffer) {
518
- return new DataView(result);
519
- }
520
- // A DataView or a pooled Buffer can be a window into a
521
- // larger buffer, and parsers slice dataView.buffer, so
522
- // copy a window out but keep a whole buffer as it is.
523
- if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
524
- return new DataView(result.buffer);
525
- }
526
- return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
527
- }
528
- return new TextDecoder(encoding).decode(result);
529
- });
534
+ return decompressWithCustomFunctionInTurn(customFn, dataView, returnType, encoding, budget, maxDecompressedSize);
530
535
  }
531
536
  }
532
537
 
@@ -586,7 +591,110 @@ function getDecompressBudget(decompressConfig, maxDecompressedSize) {
586
591
  if (decompressConfig && decompressConfig.budget) {
587
592
  return decompressConfig.budget;
588
593
  }
589
- return {remaining: maxDecompressedSize, warned: false};
594
+ return {remaining: maxDecompressedSize, warned: false, failed: false, pending: undefined};
595
+ }
596
+
597
+ function decompressWithCustomFunctionInTurn(customFn, dataView, returnType, encoding, budget, maxDecompressedSize) {
598
+ // The executor runs now, so a synchronous result is charged before this returns and a throw becomes a rejection.
599
+ return new Promise((resolve) => {
600
+ if (budget.pending) {
601
+ resolve(queueCustomFunctionCall(customFn, dataView, returnType, encoding, budget, maxDecompressedSize));
602
+ } else {
603
+ resolve(decompressWithCustomFunction(customFn, dataView, returnType, encoding, budget, maxDecompressedSize));
604
+ }
605
+ });
606
+ }
607
+
608
+ function queueCustomFunctionCall(customFn, dataView, returnType, encoding, budget, maxDecompressedSize) {
609
+ const previousCall = budget.pending;
610
+ // Copy now: the input can view the caller's buffer, which may be reused once load() returns.
611
+ const inputCopy = new DataView(dataView.buffer.slice(dataView.byteOffset, dataView.byteOffset + dataView.byteLength));
612
+ return holdTurn(budget, previousCall.then(() => {
613
+ if (budget.remaining < 0) {
614
+ return rejectExceedsMax(budget, maxDecompressedSize);
615
+ }
616
+ return decompressWithCustomFunction(customFn, inputCopy, returnType, encoding, budget, maxDecompressedSize);
617
+ }));
618
+ }
619
+
620
+ function holdTurn(budget, call) {
621
+ const settled = call.then(() => undefined, () => undefined);
622
+ budget.pending = settled;
623
+ settled.then(() => {
624
+ if (budget.pending === settled) {
625
+ budget.pending = undefined;
626
+ }
627
+ });
628
+ return call;
629
+ }
630
+
631
+ function decompressWithCustomFunction(customFn, dataView, returnType, encoding, budget, maxDecompressedSize) {
632
+ // Allow one byte more than is left: a function that cuts its output off at this limit
633
+ // then returns more than fits, and is rejected instead of being taken as complete.
634
+ const maxOutputLength = Math.min(budget.remaining + 1, MAX_CUSTOM_OUTPUT_LENGTH);
635
+ let result;
636
+ try {
637
+ // Only a call made at once gets the caller's input uncopied: that buffer may be reused once load() returns.
638
+ result = customFn(getUint8View(dataView), {maxOutputLength});
639
+ } catch (error) {
640
+ return rejectCustomFunctionError(error, budget, maxDecompressedSize, maxOutputLength);
641
+ }
642
+ if (isThenable(result)) {
643
+ const charged = Promise.resolve(result).then(
644
+ (resolvedResult) => chargeCustomResult(resolvedResult, returnType, encoding, budget, maxDecompressedSize),
645
+ (error) => rejectCustomFunctionError(error, budget, maxDecompressedSize, maxOutputLength)
646
+ );
647
+ // A queued call already holds its turn.
648
+ return budget.pending ? charged : holdTurn(budget, charged);
649
+ }
650
+ return chargeCustomResult(result, returnType, encoding, budget, maxDecompressedSize);
651
+ }
652
+
653
+ function rejectCustomFunctionError(error, budget, maxDecompressedSize, maxOutputLength) {
654
+ // Node's zlib throws this when the output would pass maxOutputLength.
655
+ if (error && error.code === 'ERR_BUFFER_TOO_LARGE') {
656
+ budget.remaining = -1;
657
+ return rejectExceedsMax(budget, maxDecompressedSize);
658
+ }
659
+ chargeFailedCall(budget, maxOutputLength);
660
+ return Promise.reject(error);
661
+ }
662
+
663
+ function chargeFailedCall(budget, maxOutputLength) {
664
+ // One corrupt block in a legitimate file costs nothing; each later failure may have done maxOutputLength of work.
665
+ if (budget.failed) {
666
+ budget.remaining -= maxOutputLength;
667
+ } else {
668
+ budget.failed = true;
669
+ }
670
+ }
671
+
672
+ export function isThenable(value) {
673
+ return !!value && typeof value.then === 'function';
674
+ }
675
+
676
+ function chargeCustomResult(result, returnType, encoding, budget, maxDecompressedSize) {
677
+ budget.remaining -= getResultByteLength(result);
678
+ if (budget.remaining < 0) {
679
+ return rejectExceedsMax(budget, maxDecompressedSize);
680
+ }
681
+ return convertCustomResult(result, returnType, encoding);
682
+ }
683
+
684
+ function convertCustomResult(result, returnType, encoding) {
685
+ if (returnType === 'dataview') {
686
+ if (result instanceof ArrayBuffer) {
687
+ return new DataView(result);
688
+ }
689
+ // A DataView or a pooled Buffer can be a window into a
690
+ // larger buffer, and parsers slice dataView.buffer, so
691
+ // copy a window out but keep a whole buffer as it is.
692
+ if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
693
+ return new DataView(result.buffer);
694
+ }
695
+ return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
696
+ }
697
+ return new TextDecoder(encoding).decode(result);
590
698
  }
591
699
 
592
700
  function getUint8View(dataView) {
package/src/xmp-tags.js CHANGED
@@ -109,15 +109,13 @@ function combineChunks(dataView, chunks) {
109
109
  // A chunk length comes verbatim from the image and is not trusted. Bound it to
110
110
  // the bytes actually present so a crafted length cannot force a huge allocation.
111
111
  function getBoundedChunkBytes(dataView, chunk) {
112
- // The bound is the view's own extent, not the buffer's, since the slice
113
- // starts at the view's byteOffset.
114
- // dataView can be a DataViewWrapper whose buffer is a Buffer, which a
115
- // Uint8Array view constructor would copy whole, so the bytes come from slice.
112
+ // The bound is the view's own extent, not the buffer's, since the bytes
113
+ // start at the view's byteOffset.
116
114
  const bufferLength = dataView.byteLength;
117
- const byteOffset = dataView.byteOffset || 0;
115
+ const byteOffset = dataView.byteOffset;
118
116
  const start = Math.min(Math.max(chunk.dataOffset, 0), bufferLength);
119
117
  const end = Math.min(start + Math.max(chunk.length, 0), bufferLength);
120
- return new Uint8Array(dataView.buffer.slice(byteOffset + start, byteOffset + end));
118
+ return new Uint8Array(dataView.buffer, byteOffset + start, end - start);
121
119
  }
122
120
 
123
121
  function readTags(tags, chunkDataView, domParser) {
package/webpack.config.js CHANGED
@@ -83,7 +83,7 @@ module.exports = {
83
83
  }
84
84
  },
85
85
  {
86
- test: /[/\\](exif-reader|image-header-?(tiff|jpeg|png|heic|avif|jxl|iso-bmff|webp|gif)?|tags|tag-names|tags-helpers|png-text-tags|loadview-pipeline)\.js$/,
86
+ test: /[/\\](exif-reader|image-header|loadview-pipeline)\.js$/,
87
87
  loader: 'string-replace-loader',
88
88
  options: {
89
89
  multiple: getConstantReplacements(includedModules)
package/src/dataview.js DELETED
@@ -1,51 +0,0 @@
1
- /* This Source Code Form is subject to the terms of the Mozilla Public
2
- * License, v. 2.0. If a copy of the MPL was not distributed with this
3
- * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
-
5
- export default class DataView {
6
- constructor(buffer) {
7
- if (bufferTypeIsUnsupported(buffer)) {
8
- throw new Error('DataView: Passed buffer type is unsupported.');
9
- }
10
-
11
- this.buffer = buffer;
12
- this.byteLength = this.buffer.length;
13
- }
14
-
15
- getUint8(offset) {
16
- return this.buffer.readUInt8(offset);
17
- }
18
-
19
- getUint16(offset, littleEndian) {
20
- if (littleEndian) {
21
- return this.buffer.readUInt16LE(offset);
22
- }
23
- return this.buffer.readUInt16BE(offset);
24
- }
25
-
26
- getUint32(offset, littleEndian) {
27
- if (littleEndian) {
28
- return this.buffer.readUInt32LE(offset);
29
- }
30
- return this.buffer.readUInt32BE(offset);
31
- }
32
-
33
- getInt32(offset, littleEndian) {
34
- if (littleEndian) {
35
- return this.buffer.readInt32LE(offset);
36
- }
37
- return this.buffer.readInt32BE(offset);
38
- }
39
- }
40
-
41
- function bufferTypeIsUnsupported(buffer) {
42
- return typeof buffer !== 'object'
43
- || buffer.length === undefined
44
- || buffer.readUInt8 === undefined
45
- || buffer.readUInt16LE === undefined
46
- || buffer.readUInt16BE === undefined
47
- || buffer.readUInt32LE === undefined
48
- || buffer.readUInt32BE === undefined
49
- || buffer.readInt32LE === undefined
50
- || buffer.readInt32BE === undefined;
51
- }