exifreader 4.51.0 → 4.53.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/exif-reader.d.ts CHANGED
@@ -237,6 +237,9 @@ export interface ExpandedTags {
237
237
  * The raw, unsanitized XMP packet as it appears in the image, decoded
238
238
  * as text. Treat it as untrusted: escape or sanitize it before
239
239
  * inserting it into a page as HTML to avoid cross-site scripting.
240
+ *
241
+ * Tag filtering does not remove the values of excluded XMP tags from
242
+ * `_raw`; exclude `_raw` too.
240
243
  */
241
244
  _raw: string
242
245
  } & XmpTags,
@@ -412,7 +415,14 @@ export type IncludeTagsOptions = {
412
415
 
413
416
  export type ExcludeTagsOptions = IncludeTagsOptions;
414
417
 
415
- type DecompressFunction = (data: Uint8Array) => Promise<Uint8Array | ArrayBuffer> | Uint8Array | ArrayBuffer;
418
+ type DecompressFunction = (data: Uint8Array, options: {
419
+ /**
420
+ * The most bytes the function may produce: one more than what is left of
421
+ * the `maxDecompressedSize` total of the current `load()` or `loadView()`
422
+ * call, at most 2^30 - 1.
423
+ */
424
+ maxOutputLength: number,
425
+ }) => Promise<Uint8Array | ArrayBuffer> | Uint8Array | ArrayBuffer;
416
426
 
417
427
  type DecompressOptions = {
418
428
  brotli?: DecompressFunction,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.51.0",
3
+ "version": "4.53.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -54,19 +54,19 @@
54
54
  "c8": "^12.0.0",
55
55
  "chai": "^6.2.2",
56
56
  "cross-env": "^10.0.0",
57
- "cypress": "^16.1.1",
57
+ "cypress": "^16.2.0",
58
58
  "eslint": "^9.39.4",
59
59
  "eslint-plugin-cypress": "^6.4.4",
60
60
  "globals": "^17.13.0",
61
61
  "husky": "^9.0.11",
62
- "linkedom": "^0.18.13",
62
+ "linkedom": "^0.18.14",
63
63
  "mocha": "^12.0.3",
64
- "npm-run-all2": "^9.0.3",
64
+ "npm-run-all2": "^9.0.4",
65
65
  "rimraf": "^5.0.5",
66
66
  "string-replace-loader": "^3.1.0",
67
67
  "terser-webpack-plugin": "^5.6.1",
68
68
  "typescript": "^7.0.2",
69
- "webpack": "5.111.1",
69
+ "webpack": "5.112.2",
70
70
  "webpack-cli": "^7.2.3",
71
71
  "webpack-dev-server": "^6.1.0"
72
72
  },
package/src/dom-parser.js CHANGED
@@ -4,22 +4,72 @@
4
4
 
5
5
  import NodeRequire from './node-require.js';
6
6
 
7
+ // The XMP depth and markup node limits are checked the way @xmldom/xmldom 0.9.12 reads markup.
8
+ const MIN_XMLDOM_VERSION = [0, 9, 12];
9
+ const NO_PARSER_WARNING = 'Warning: DOMParser is not available. It is needed to be able to parse XMP tags.';
10
+ const XMLDOM_REMEDY = 'ExifReader needs 0.9.12 or later, or a parser passed in the domParser option.';
11
+ const UNREADABLE_VERSION_WARNING = 'Warning: XMP tags are skipped because the version of the @xmldom/xmldom that was found could not be read. ' + XMLDOM_REMEDY;
12
+
7
13
  export default {
8
14
  get
9
15
  };
10
16
 
17
+ /**
18
+ * Finds the DOM parser to read XMP with: the one passed in, a global DOMParser,
19
+ * or an @xmldom/xmldom of version 0.9.12 or later.
20
+ *
21
+ * @param {Object} [domParser] A parser passed in by the caller.
22
+ * @returns {{parser: (Object|undefined), warning: (string|undefined)}} The
23
+ * parser, or no parser and the warning that says why.
24
+ */
11
25
  function get(domParser) {
12
26
  if (domParser) {
13
- return domParser;
27
+ return {parser: domParser};
14
28
  }
15
29
 
16
30
  if (typeof DOMParser !== 'undefined') {
17
- return new DOMParser();
31
+ return {parser: new DOMParser()};
18
32
  }
33
+
34
+ return getXmldomParser();
35
+ }
36
+
37
+ function getXmldomParser() {
19
38
  try {
20
39
  const {DOMParser, onErrorStopParsing} = NodeRequire.optionalPackage('@xmldom/xmldom');
21
- return new DOMParser({onError: onErrorStopParsing});
40
+ const warning = getXmldomVersionWarning(readXmldomVersion());
41
+ if (warning) {
42
+ return {parser: undefined, warning};
43
+ }
44
+ return {parser: new DOMParser({onError: onErrorStopParsing})};
22
45
  } catch (error) {
23
- return undefined;
46
+ return {parser: undefined, warning: NO_PARSER_WARNING};
47
+ }
48
+ }
49
+
50
+ function readXmldomVersion() {
51
+ const packageJson = NodeRequire.optionalPackage('@xmldom/xmldom/package.json');
52
+ return packageJson ? packageJson.version : undefined;
53
+ }
54
+
55
+ function getXmldomVersionWarning(version) {
56
+ const match = typeof version === 'string' ? /^(\d+)\.(\d+)\.(\d+)(-?)/.exec(version) : null;
57
+ if (!match) {
58
+ return UNREADABLE_VERSION_WARNING;
59
+ }
60
+ if (isOlderThanMinimum(match)) {
61
+ return `Warning: XMP tags are skipped because the @xmldom/xmldom that was found is version ${version}. ${XMLDOM_REMEDY}`;
62
+ }
63
+ return undefined;
64
+ }
65
+
66
+ // A prerelease of the minimum version (0.9.12-beta.1) comes before it.
67
+ function isOlderThanMinimum(match) {
68
+ for (let i = 0; i < MIN_XMLDOM_VERSION.length; i++) {
69
+ const part = Number(match[i + 1]);
70
+ if (part !== MIN_XMLDOM_VERSION[i]) {
71
+ return part < MIN_XMLDOM_VERSION[i];
72
+ }
24
73
  }
74
+ return match[4] === '-';
25
75
  }
@@ -7,7 +7,7 @@
7
7
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
8
  */
9
9
 
10
- import {objectAssign, decompress, withDecompressBudget, COMPRESSION_METHOD_BROTLI, getDataView, getDataViewFromBytes, getByteString, getStringValueFromArray, assertPromiseSupport, setProperty, MAX_METADATA_BLOCKS} from './utils.js';
10
+ import {objectAssign, decompress, withDecompressBudget, COMPRESSION_METHOD_BROTLI, getDataView, getDataViewFromBytes, getByteString, getStringValueFromArray, assertPromiseSupport, setProperty, isThenable, MAX_METADATA_BLOCKS} from './utils.js';
11
11
  import {isFilePathOrURL, isBrowserFileObject, loadFile, loadFileObject} from './file-loaders.js';
12
12
  import {makeLoadAuto, validateAutoOptions} from './load-auto.js';
13
13
  import Constants from './constants.js';
@@ -41,7 +41,7 @@ import GifFileTags from './gif-file-tags.js';
41
41
  import Thumbnail from './thumbnail.js';
42
42
  import Composite from './composite.js';
43
43
  import {createTagFilter} from './tag-filter.js';
44
- import {buildTagsFromMergeSteps, isThenable, EXIF_CARRIED_GROUP_ORDER} from './loadview-pipeline.js';
44
+ import {buildTagsFromMergeSteps, EXIF_CARRIED_GROUP_ORDER} from './loadview-pipeline.js';
45
45
  import exifErrors from './errors.js';
46
46
 
47
47
  // Brob XMP is bounded by the file size, as brob Exif is, plus room for one JPEG
@@ -926,8 +926,7 @@ function isParsedMetadataBlock(format, blockType) {
926
926
  case 'file':
927
927
  return format === 'png' ? Constants.USE_PNG_FILE : Constants.USE_FILE;
928
928
  case 'exif':
929
- // The PNG finder never gated eXIf, so it is listed in every build.
930
- return format === 'png' || Constants.USE_EXIF;
929
+ return Constants.USE_EXIF;
931
930
  case 'jfif':
932
931
  return Constants.USE_JFIF;
933
932
  case 'xmp':
package/src/load-auto.js CHANGED
@@ -290,9 +290,20 @@ function sliceInputBuffer(data, end) {
290
290
  const viewEnd = Math.max(0, Math.min(end, data.byteLength));
291
291
  return data.buffer.slice(data.byteOffset, data.byteOffset + viewEnd);
292
292
  }
293
+ if (isArrayBufferLike(data)) {
294
+ return data.slice(0, end);
295
+ }
293
296
  return data;
294
297
  }
295
298
 
299
+ // Tells an ArrayBuffer or SharedArrayBuffer from another realm, where instanceof fails.
300
+ function isArrayBufferLike(data) {
301
+ return !!data
302
+ && !data.buffer
303
+ && (typeof data.slice === 'function')
304
+ && (typeof data.byteLength === 'number');
305
+ }
306
+
296
307
  function bufferByteLength(buffer) {
297
308
  if (!buffer) {
298
309
  return 0;
@@ -607,10 +607,6 @@ function createInPlaceMerge(deps) {
607
607
  }
608
608
  }
609
609
 
610
- export function isThenable(value) {
611
- return !!value && typeof value.then === 'function';
612
- }
613
-
614
610
  function buildMetadataRange(metadataBlocks, metadataTruncated, dataView, parsedGroups) {
615
611
  const blocks = (metadataBlocks || []).slice();
616
612
  const finderTruncated = !!metadataTruncated;
@@ -49,7 +49,7 @@ function firstLoaded(name, loaders) {
49
49
 
50
50
  // Literal names make webpack emit require('fs') and so on, which bundlers and
51
51
  // file tracers follow. No typeof guard: webpack may not rewrite the identifier there.
52
- // A try in this function makes bundlers treat fs and @xmldom/xmldom as optional.
52
+ // A try in this function makes bundlers treat fs, @xmldom/xmldom and its package.json as optional.
53
53
  function webpackRequire(name) {
54
54
  switch (name) {
55
55
  case 'fs':
@@ -70,6 +70,13 @@ function webpackRequire(name) {
70
70
  } catch (error) {
71
71
  throw error;
72
72
  }
73
+ case '@xmldom/xmldom/package.json':
74
+ // eslint-disable-next-line no-useless-catch
75
+ try {
76
+ return __non_webpack_require__('@xmldom/xmldom/package.json');
77
+ } catch (error) {
78
+ throw error;
79
+ }
73
80
  default:
74
81
  throw new Error(`No webpack require for ${name}.`);
75
82
  }
@@ -4,7 +4,7 @@
4
4
 
5
5
  import TagNamesCommon from './tag-names-common.js';
6
6
  import {joinMapped} from './utils.js';
7
- import {getTableDescription} from './tag-names-utils.js';
7
+ import {getTableDescription, isStringValue, getStringsText} from './tag-names-utils.js';
8
8
 
9
9
  export default {
10
10
  0x000b: 'ProcessingSoftware',
@@ -385,6 +385,10 @@ function decodeXPValue(value) {
385
385
  // A one-element value arrives as a bare number, which Uint8Array would take as a length.
386
386
  return '';
387
387
  }
388
+ if (isStringValue(value)) {
389
+ // An ASCII-typed value holds strings, not UCS-2 bytes.
390
+ return getStringsText(value);
391
+ }
388
392
  // The XP tags are encoded as UCS-2 which uses two bytes per character but
389
393
  // it's close to UTF-16 so we can use that to decode them.
390
394
  // https://www.loc.gov/preservation/digital/formats/content/tiff_tags.shtml
@@ -7,6 +7,9 @@ import {getStringValueFromArray as getStringValue, getByteString} from './utils.
7
7
  export {getStringValue};
8
8
 
9
9
  export function getEncodedString(value) {
10
+ if (isStringValue(value)) {
11
+ return getStringsText(value) || 'Undefined';
12
+ }
10
13
  if (value.length >= 8) {
11
14
  const encoding = getStringValue(value.slice(0, 8));
12
15
 
@@ -38,14 +41,36 @@ export function getEncodedString(value) {
38
41
  */
39
42
  export function getVersionString(value) {
40
43
  if (Array.isArray(value)) {
41
- if (value.some(isString)) {
42
- return value.filter(isString).join(', ');
44
+ if (isStringValue(value)) {
45
+ return getStringsText(value);
43
46
  }
44
47
  return getByteString(value);
45
48
  }
46
49
  return value;
47
50
  }
48
51
 
52
+ /**
53
+ * Tells whether a value is an array without character codes. Character codes
54
+ * are numbers. An ASCII-typed value holds strings, with a hole where an empty
55
+ * piece was, index 0 included; a rational value holds number pairs. Neither
56
+ * has character codes to convert, and an empty array has none either.
57
+ * @param {*} value
58
+ * @returns {boolean} True for an array whose element 0 is not a number.
59
+ */
60
+ export function isStringValue(value) {
61
+ return Array.isArray(value) && typeof value[0] !== 'number';
62
+ }
63
+
64
+ /**
65
+ * Describes an ASCII-typed value as its strings.
66
+ * @param {Array} value
67
+ * @returns {string} The strings joined with ', ', holes and non-strings
68
+ * skipped, or '' when there are none.
69
+ */
70
+ export function getStringsText(value) {
71
+ return value.filter(isString).join(', ');
72
+ }
73
+
49
74
  function isString(item) {
50
75
  return typeof item === 'string';
51
76
  }
@@ -30,10 +30,10 @@ const MAX_DECOMPRESSED_VALUE_ALLOWANCE = 1024 * 1024;
30
30
  // visit at most about as many entries as the buffer holds.
31
31
  const MAX_IFD_ENTRY_READS_PER_BUFFER_ENTRY = 4;
32
32
 
33
- // Each string after the first of an out-of-slot ASCII value costs this much
34
- // budget. 4 MiB files whose tags use the budget up on short strings peaked at
35
- // up to 107 times their size in RSS without it, 88 with 1 and 76 with 2,
36
- // against 87 for BYTE values.
33
+ // Each string after the first of an out-of-slot ASCII or UTF-8 value costs
34
+ // this much budget. 4 MiB files whose tags use the budget up on short strings
35
+ // peaked at up to 107 times their size in RSS without it, 88 with 1 and 76
36
+ // with 2, against 87 for BYTE values.
37
37
  export const BUDGET_BYTES_PER_EXTRA_ASCII_STRING = 2;
38
38
 
39
39
  // A buffer gets one IPTC dataset unit per this many bytes, and a dataset draws
@@ -60,9 +60,13 @@ const getTagValueAt = {
60
60
  3: Types.getShortAt,
61
61
  4: Types.getLongAt,
62
62
  5: Types.getRationalAt,
63
+ 6: Types.getSbyteAt,
63
64
  7: Types.getUndefinedAt,
65
+ 8: Types.getSshortAt,
64
66
  9: Types.getSlongAt,
65
67
  10: Types.getSrationalAt,
68
+ 11: Types.getFloatAt,
69
+ 12: Types.getDoubleAt,
66
70
  13: Types.getIfdPointerAt
67
71
  };
68
72
 
@@ -341,7 +345,7 @@ function readTag(
341
345
  }
342
346
  }
343
347
 
344
- if (tagType === Types.tagTypes['ASCII']) {
348
+ if (isStringType(tagType)) {
345
349
  tagValue = getAsciiTagValue(tagValue, stringBudget);
346
350
  }
347
351
 
@@ -395,7 +399,7 @@ function getTagValue(dataView, offset, type, count, byteOrder, forceByteType = f
395
399
  count = count * Types.typeSizes[type];
396
400
  type = Types.tagTypes['BYTE'];
397
401
  }
398
- if (type === Types.tagTypes['ASCII']) {
402
+ if (isStringType(type)) {
399
403
  return getAsciiBytes(dataView, offset, count);
400
404
  }
401
405
  for (let valueIndex = 0; valueIndex < count; valueIndex++) {
@@ -436,6 +440,10 @@ function getBoundedTagCount(remainingBudget, tagType, tagCount) {
436
440
  return boundedCount;
437
441
  }
438
442
 
443
+ function isStringType(type) {
444
+ return type === Types.tagTypes['ASCII'] || type === Types.tagTypes['UTF8'];
445
+ }
446
+
439
447
  function getAsciiTagValue(tagValue, stringBudget) {
440
448
  if (tagValue instanceof Uint8Array) {
441
449
  return getNullSeparatedStrings(tagValue, stringBudget);
@@ -494,7 +502,7 @@ function getDescriptionFromTagValue(tagValue) {
494
502
  }
495
503
 
496
504
  function getComputedTagValue(tagType, value) {
497
- if (tagType === Types.tagTypes['ASCII']) {
505
+ if (isStringType(tagType)) {
498
506
  if (Array.isArray(value) && value.length === 1) {
499
507
  return value[0];
500
508
  }
package/src/types.js CHANGED
@@ -10,10 +10,15 @@ const typeSizes = {
10
10
  3: 2, // SHORT
11
11
  4: 4, // LONG
12
12
  5: 8, // RATIONAL
13
+ 6: 1, // SBYTE
13
14
  7: 1, // UNDEFINED
15
+ 8: 2, // SSHORT
14
16
  9: 4, // SLONG
15
17
  10: 8, // SRATIONAL
16
- 13: 4 // IFD
18
+ 11: 4, // FLOAT
19
+ 12: 8, // DOUBLE
20
+ 13: 4, // IFD
21
+ 129: 1 // UTF8
17
22
  };
18
23
 
19
24
  const tagTypes = {
@@ -22,10 +27,15 @@ const tagTypes = {
22
27
  'SHORT': 3,
23
28
  'LONG': 4,
24
29
  'RATIONAL': 5,
30
+ 'SBYTE': 6,
25
31
  'UNDEFINED': 7,
32
+ 'SSHORT': 8,
26
33
  'SLONG': 9,
27
34
  'SRATIONAL': 10,
28
- 'IFD': 13
35
+ 'FLOAT': 11,
36
+ 'DOUBLE': 12,
37
+ 'IFD': 13,
38
+ 'UTF8': 129
29
39
  };
30
40
 
31
41
  export default {
@@ -34,9 +44,13 @@ export default {
34
44
  getShortAt,
35
45
  getLongAt,
36
46
  getRationalAt,
47
+ getSbyteAt,
37
48
  getUndefinedAt,
49
+ getSshortAt,
38
50
  getSlongAt,
39
51
  getSrationalAt,
52
+ getFloatAt,
53
+ getDoubleAt,
40
54
  getIfdPointerAt,
41
55
  typeSizes,
42
56
  tagTypes,
@@ -63,10 +77,18 @@ function getRationalAt(dataView, offset, byteOrder) {
63
77
  return [getLongAt(dataView, offset, byteOrder), getLongAt(dataView, offset + 4, byteOrder)];
64
78
  }
65
79
 
80
+ function getSbyteAt(dataView, offset) {
81
+ return dataView.getInt8(offset);
82
+ }
83
+
66
84
  function getUndefinedAt(dataView, offset) {
67
85
  return getByteAt(dataView, offset);
68
86
  }
69
87
 
88
+ function getSshortAt(dataView, offset, byteOrder) {
89
+ return dataView.getInt16(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
90
+ }
91
+
70
92
  function getSlongAt(dataView, offset, byteOrder) {
71
93
  return dataView.getInt32(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
72
94
  }
@@ -75,6 +97,14 @@ function getSrationalAt(dataView, offset, byteOrder) {
75
97
  return [getSlongAt(dataView, offset, byteOrder), getSlongAt(dataView, offset + 4, byteOrder)];
76
98
  }
77
99
 
100
+ function getFloatAt(dataView, offset, byteOrder) {
101
+ return dataView.getFloat32(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
102
+ }
103
+
104
+ function getDoubleAt(dataView, offset, byteOrder) {
105
+ return dataView.getFloat64(offset, byteOrder === ByteOrder.LITTLE_ENDIAN);
106
+ }
107
+
78
108
  function getIfdPointerAt(dataView, offset, byteOrder) {
79
109
  return getLongAt(dataView, offset, byteOrder);
80
110
  }
package/src/utils.js CHANGED
@@ -500,20 +500,22 @@ export const COMPRESSION_METHOD_NONE = undefined;
500
500
  export const COMPRESSION_METHOD_DEFLATE = 0;
501
501
  export const COMPRESSION_METHOD_BROTLI = 'brotli';
502
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;
503
505
 
504
506
  /**
505
507
  * Returns a copy of the decompress config whose compressed blocks share one
506
508
  * total budget of maxDecompressedSize bytes. The input is never mutated.
507
509
  *
508
510
  * @param {Object|undefined} decompressConfig - The caller's decompress option.
509
- * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned}}
511
+ * @returns {Object} {brotli, deflate, maxDecompressedSize, budget: {remaining, warned, failed, pending}}
510
512
  */
511
513
  export function withDecompressBudget(decompressConfig) {
512
514
  return {
513
515
  brotli: decompressConfig ? decompressConfig.brotli : undefined,
514
516
  deflate: decompressConfig ? decompressConfig.deflate : undefined,
515
517
  maxDecompressedSize: decompressConfig ? decompressConfig.maxDecompressedSize : undefined,
516
- budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false}
518
+ budget: {remaining: getMaxDecompressedSize(decompressConfig), warned: false, failed: false, pending: undefined}
517
519
  };
518
520
  }
519
521
 
@@ -529,26 +531,7 @@ export function decompress(dataView, compressionMethod, encoding, returnType = '
529
531
  if (decompressConfig && decompressType) {
530
532
  const customFn = decompressConfig[decompressType];
531
533
  if (typeof customFn === 'function') {
532
- // Called now, not deferred: the input can view the caller's buffer, which may be reused once load() returns.
533
- return new Promise((resolve) => resolve(customFn(getUint8View(dataView)))).then((result) => {
534
- budget.remaining -= getResultByteLength(result);
535
- if (budget.remaining < 0) {
536
- return rejectExceedsMax(budget, maxDecompressedSize);
537
- }
538
- if (returnType === 'dataview') {
539
- if (result instanceof ArrayBuffer) {
540
- return new DataView(result);
541
- }
542
- // A DataView or a pooled Buffer can be a window into a
543
- // larger buffer, and parsers slice dataView.buffer, so
544
- // copy a window out but keep a whole buffer as it is.
545
- if ((result.byteOffset === 0) && (result.byteLength === result.buffer.byteLength)) {
546
- return new DataView(result.buffer);
547
- }
548
- return new DataView(result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength));
549
- }
550
- return new TextDecoder(encoding).decode(result);
551
- });
534
+ return decompressWithCustomFunctionInTurn(customFn, dataView, returnType, encoding, budget, maxDecompressedSize);
552
535
  }
553
536
  }
554
537
 
@@ -608,7 +591,110 @@ function getDecompressBudget(decompressConfig, maxDecompressedSize) {
608
591
  if (decompressConfig && decompressConfig.budget) {
609
592
  return decompressConfig.budget;
610
593
  }
611
- 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);
612
698
  }
613
699
 
614
700
  function getUint8View(dataView) {
@@ -10,16 +10,19 @@ const NAME_END_CHARACTERS = ' \t\r\n/><="\'';
10
10
  /**
11
11
  * Tells whether an XML string has elements nested deeper than a bound or more
12
12
  * markup nodes than a bound, in linear time. The nodes are elements, attributes
13
- * (namespace declarations included), comments, processing instructions, CDATA
14
- * sections and <! declarations together; text is not counted. Malformed markup
15
- * may be counted deeper and with more nodes than @xmldom/xmldom 0.9.12 builds,
16
- * never shallower or with fewer.
13
+ * (namespace declarations included), comments, processing instructions and
14
+ * CDATA sections together; text is not counted. Malformed markup may be counted
15
+ * deeper and with more nodes than @xmldom/xmldom 0.9.12 builds, never shallower
16
+ * or with fewer. A markup declaration such as a DOCTYPE also makes it true,
17
+ * unless it is in a comment, a CDATA section or an end tag, and so does one in a
18
+ * processing instruction: a parser may expand an entity declared there into
19
+ * markup the scan cannot count.
17
20
  *
18
21
  * @param {string} xmlString
19
22
  * @param {number} maxDepth The deepest allowed element, the root being 1.
20
23
  * @param {number} [maxNodes] The most markup nodes allowed.
21
- * @returns {boolean} True as soon as an element is nested deeper than maxDepth
22
- * or the markup nodes are more than maxNodes.
24
+ * @returns {boolean} True as soon as an element is nested deeper than maxDepth,
25
+ * the markup nodes are more than maxNodes, or a markup declaration is found.
23
26
  */
24
27
  export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
25
28
  let depth = 0;
@@ -33,11 +36,15 @@ export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
33
36
  nodeCount++;
34
37
  index = skipPast(xmlString, index + 9, ']]>');
35
38
  } else if (hasSubstringAt(xmlString, '<?', index)) {
39
+ const endIndex = skipPast(xmlString, index + 2, '?>');
40
+ // A parser that recovers from errors may end the instruction early.
41
+ if (xmlString.slice(index + 2, endIndex).indexOf('<!') !== -1) {
42
+ return true;
43
+ }
36
44
  nodeCount++;
37
- index = skipPast(xmlString, index + 2, '?>');
45
+ index = endIndex;
38
46
  } else if (hasSubstringAt(xmlString, '<!', index)) {
39
- nodeCount++;
40
- index = skipDeclaration(xmlString, index + 2);
47
+ return true;
41
48
  } else if (hasSubstringAt(xmlString, '</', index)) {
42
49
  depth = Math.max(depth - 1, 0);
43
50
  // xmldom ends an end tag at its first >, whatever comes between.
@@ -62,23 +69,6 @@ export function exceedsMarkupBounds(xmlString, maxDepth, maxNodes = Infinity) {
62
69
  return false;
63
70
  }
64
71
 
65
- // Stopping at [ lets the main loop read the declarations of a DOCTYPE internal
66
- // subset one by one.
67
- function skipDeclaration(xmlString, fromIndex) {
68
- for (let index = fromIndex; index < xmlString.length; index++) {
69
- const character = xmlString.charAt(index);
70
- if (character === '"' || character === '\'') {
71
- index = xmlString.indexOf(character, index + 1);
72
- if (index === -1) {
73
- return xmlString.length;
74
- }
75
- } else if (character === '>' || character === '[') {
76
- return index + 1;
77
- }
78
- }
79
- return xmlString.length;
80
- }
81
-
82
72
  // A start tag cannot hold a <, so every search in it stops at the next <, which
83
73
  // keeps the scan linear. Only a well-formed tag is read as self-closing: some
84
74
  // parsers keep a malformed one open, so reading it as open can only count deeper.