exifreader 4.10.0 → 4.11.1

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
@@ -53,6 +53,25 @@ interface PngFileTags {
53
53
  }
54
54
  }
55
55
 
56
+ interface PngPhysTags {
57
+ 'Pixels Per Unit X'?: NumberFileTag,
58
+ 'Pixels Per Unit Y'?: NumberFileTag,
59
+ 'Pixel Units'?: {
60
+ value: number,
61
+ description: 'meters' | 'Unknown'
62
+ },
63
+ 'Modify Date'?: NumberArrayFileTag
64
+ }
65
+
66
+ interface PngTag {
67
+ description: string,
68
+ value: string | number
69
+ }
70
+
71
+ type PngTags = PngFileTags & PngPhysTags & {
72
+ [name: string]: PngTag
73
+ }
74
+
56
75
  interface PngTextTag {
57
76
  description: string,
58
77
  value: string
@@ -139,6 +158,7 @@ interface ExpandedTags {
139
158
  jfif?: JfifTags,
140
159
  pngFile?: PngFileTags,
141
160
  pngText?: PngTextTags,
161
+ png?: PngTags,
142
162
  exif?: Tags,
143
163
  iptc?: Tags,
144
164
  xmp?: { _raw: string } & XmpTags,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.10.0",
3
+ "version": "4.11.1",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -44,6 +44,7 @@
44
44
  "rimraf": "^3.0.2",
45
45
  "string-replace-loader": "^3.0.3",
46
46
  "terser-webpack-plugin": "^5.2.4",
47
+ "typescript": "^5.0.2",
47
48
  "webpack": "^5.74.0",
48
49
  "webpack-cli": "^4.10.0",
49
50
  "webpack-dev-server": "^4.11.1"
@@ -60,21 +61,22 @@
60
61
  "test:build:custom": "mocha --bail test/build/test-custom.js",
61
62
  "test:build:update": "node test/build/update.js",
62
63
  "test:e2e": "cypress run",
63
- "test:all": "npm-run-all lint coverage test:e2e test:build test:build:custom",
64
+ "test:types": "tsc --noEmit --project test/types",
65
+ "test:all": "npm-run-all lint coverage test:e2e test:build test:build:custom test:types",
64
66
  "postinstall": "node bin/build.js --only-with-config"
65
67
  },
66
68
  "nyc": {
67
69
  "check-coverage": true,
68
70
  "statements": 95,
69
- "branches": 93,
71
+ "branches": 92,
70
72
  "functions": 98,
71
- "lines": 96,
73
+ "lines": 95,
72
74
  "reporter": [
73
75
  "lcov",
74
76
  "text"
75
77
  ],
76
78
  "exclude": [
77
- "test/**/*.js",
79
+ "test/*",
78
80
  "**/node_modules/*"
79
81
  ]
80
82
  },
@@ -8,7 +8,7 @@
8
8
  */
9
9
  /* global Buffer, __non_webpack_require__ */
10
10
 
11
- import {objectAssign} from './utils.js';
11
+ import {objectAssign, dataUriToBuffer} from './utils.js';
12
12
  import DataViewWrapper from './dataview.js';
13
13
  import Constants from './constants.js';
14
14
  import {getStringValueFromArray} from './utils.js';
@@ -22,6 +22,7 @@ import XmpTags from './xmp-tags.js';
22
22
  import IccTags from './icc-tags.js';
23
23
  import PngFileTags from './png-file-tags.js';
24
24
  import PngTextTags from './png-text-tags.js';
25
+ import PngTags from './png-tags.js';
25
26
  import Thumbnail from './thumbnail.js';
26
27
  import exifErrors from './errors.js';
27
28
 
@@ -56,6 +57,10 @@ function loadFile(filename, options) {
56
57
  return nodeFetchRemoteFile(filename, options);
57
58
  }
58
59
 
60
+ if (isDataUri(filename)) {
61
+ return Promise.resolve(dataUriToBuffer(filename));
62
+ }
63
+
59
64
  return loadLocalFile(filename, options);
60
65
  }
61
66
 
@@ -100,6 +105,10 @@ function requireNodeGet(url) {
100
105
  return __non_webpack_require__('http').get;
101
106
  }
102
107
 
108
+ function isDataUri(filename) {
109
+ return /^data:[^;,]*(;base64)?,/.test(filename);
110
+ }
111
+
103
112
  function loadLocalFile(filename, {length} = {}) {
104
113
  return new Promise((resolve, reject) => {
105
114
  const fs = requireNodeFs();
@@ -195,7 +204,8 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
195
204
  iccChunks,
196
205
  mpfDataOffset,
197
206
  pngHeaderOffset,
198
- pngTextChunks
207
+ pngTextChunks,
208
+ pngChunkOffsets
199
209
  } = ImageHeader.parseAppMarkers(dataView);
200
210
 
201
211
  if (Constants.USE_JPEG && Constants.USE_FILE && hasFileData(fileDataOffset)) {
@@ -315,6 +325,7 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
315
325
  foundMetaData = true;
316
326
  const readTags = PngFileTags.read(dataView, pngHeaderOffset);
317
327
  if (expanded) {
328
+ tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
318
329
  tags.pngFile = readTags;
319
330
  } else {
320
331
  tags = objectAssign({}, tags, readTags);
@@ -325,12 +336,23 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
325
336
  foundMetaData = true;
326
337
  const readTags = PngTextTags.read(dataView, pngTextChunks);
327
338
  if (expanded) {
339
+ tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
328
340
  tags.pngText = readTags;
329
341
  } else {
330
342
  tags = objectAssign({}, tags, readTags);
331
343
  }
332
344
  }
333
345
 
346
+ if (Constants.USE_PNG && hasPngData(pngChunkOffsets)) {
347
+ foundMetaData = true;
348
+ const readTags = PngTags.read(dataView, pngChunkOffsets);
349
+ if (expanded) {
350
+ tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
351
+ } else {
352
+ tags = objectAssign({}, tags, readTags);
353
+ }
354
+ }
355
+
334
356
  const thumbnail = (Constants.USE_JPEG || Constants.USE_WEBP)
335
357
  && Constants.USE_EXIF
336
358
  && Constants.USE_THUMBNAIL
@@ -424,3 +446,7 @@ function hasPngFileData(pngFileDataOffset) {
424
446
  function hasPngTextData(pngTextChunks) {
425
447
  return pngTextChunks !== undefined;
426
448
  }
449
+
450
+ function hasPngData(pngChunkOffsets) {
451
+ return pngChunkOffsets !== undefined;
452
+ }
@@ -2,6 +2,8 @@
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
+ // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
+
5
7
  import {getStringFromDataView} from './utils.js';
6
8
  import Constants from './constants.js';
7
9
 
@@ -12,11 +14,16 @@ export default {
12
14
 
13
15
  const PNG_ID = '\x89\x50\x4e\x47\x0d\x0a\x1a\x0a';
14
16
  const PNG_CHUNK_LENGTH_SIZE = 4;
15
- const PNG_CHUNK_TYPE_SIZE = 4;
16
- const PNG_CHUNK_LENGTH_OFFSET = 0;
17
- const PNG_CHUNK_TYPE_OFFSET = PNG_CHUNK_LENGTH_SIZE;
18
- const PNG_CHUNK_DATA_OFFSET = PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE;
17
+ export const PNG_CHUNK_TYPE_SIZE = 4;
18
+ export const PNG_CHUNK_LENGTH_OFFSET = 0;
19
+ export const PNG_CHUNK_TYPE_OFFSET = PNG_CHUNK_LENGTH_SIZE;
20
+ export const PNG_CHUNK_DATA_OFFSET = PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE;
19
21
  const PNG_XMP_PREFIX = 'XML:com.adobe.xmp\x00';
22
+ export const TYPE_TEXT = 'tEXt';
23
+ export const TYPE_ITXT = 'iTXt';
24
+ export const TYPE_PHYS = 'pHYs';
25
+ export const TYPE_TIME = 'tIME';
26
+ export const TYPE_EXIF = 'eXIf';
20
27
 
21
28
  function isPngFile(dataView) {
22
29
  return !!dataView && getStringFromDataView(dataView, 0, PNG_ID.length) === PNG_ID;
@@ -46,10 +53,24 @@ function findPngOffsets(dataView) {
46
53
  }
47
54
  } else if (isPngTextChunk(dataView, offset)) {
48
55
  offsets.hasAppMarkers = true;
56
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
49
57
  if (!offsets.pngTextChunks) {
50
58
  offsets.pngTextChunks = [];
51
59
  }
52
- offsets.pngTextChunks.push({length: dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET), offset: offset + PNG_CHUNK_DATA_OFFSET});
60
+ offsets.pngTextChunks.push({
61
+ length: dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET),
62
+ type: chunkType,
63
+ offset: offset + PNG_CHUNK_DATA_OFFSET
64
+ });
65
+ } else if (isPngExifChunk(dataView, offset)) {
66
+ offsets.hasAppMarkers = true;
67
+ offsets.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
68
+ } else if (isPngChunk(dataView, offset)) {
69
+ offsets.hasAppMarkers = true;
70
+ if (!offsets.pngChunkOffsets) {
71
+ offsets.pngChunkOffsets = [];
72
+ }
73
+ offsets.pngChunkOffsets.push(offset + PNG_CHUNK_LENGTH_OFFSET);
53
74
  }
54
75
 
55
76
  offset += dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET)
@@ -73,8 +94,20 @@ function isPngXmpChunk(dataView, offset) {
73
94
  }
74
95
 
75
96
  function isPngTextChunk(dataView, offset) {
76
- const PNG_CHUNK_TYPE_TEXTUAL = 'tEXt';
77
- return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === PNG_CHUNK_TYPE_TEXTUAL;
97
+ const PNG_CHUNK_TYPE_TEXT = 'tEXt';
98
+ const PNG_CHUNK_TYPE_ITXT = 'iTXt';
99
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
100
+ return chunkType === PNG_CHUNK_TYPE_TEXT || chunkType === PNG_CHUNK_TYPE_ITXT;
101
+ }
102
+
103
+ function isPngExifChunk(dataView, offset) {
104
+ return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_EXIF;
105
+ }
106
+
107
+ function isPngChunk(dataView, offset) {
108
+ const SUPPORTED_PNG_CHUNK_TYPES = [TYPE_PHYS, TYPE_TIME];
109
+ const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
110
+ return SUPPORTED_PNG_CHUNK_TYPES.includes(chunkType);
78
111
  }
79
112
 
80
113
  function getPngXmpDataOffset(dataView, offset) {
@@ -0,0 +1,106 @@
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
+ import Types from './types.js';
6
+ import {PNG_CHUNK_LENGTH_OFFSET, PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_DATA_OFFSET, PNG_CHUNK_TYPE_SIZE, TYPE_PHYS, TYPE_TIME} from './image-header-png.js';
7
+ import {getStringFromDataView} from './utils.js';
8
+
9
+ export default {
10
+ read
11
+ };
12
+
13
+ function read(dataView, chunkOffsets) {
14
+ const tags = {};
15
+
16
+ for (let i = 0; i < chunkOffsets.length; i++) {
17
+ const chunkLength = Types.getLongAt(dataView, chunkOffsets[i] + PNG_CHUNK_LENGTH_OFFSET);
18
+ const chunkType = getStringFromDataView(dataView, chunkOffsets[i] + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
19
+
20
+ if (chunkType === TYPE_PHYS) {
21
+ tags['Pixels Per Unit X'] = getPixelsPerUnitX(dataView, chunkOffsets[i], chunkLength);
22
+ tags['Pixels Per Unit Y'] = getPixelsPerUnitY(dataView, chunkOffsets[i], chunkLength);
23
+ tags['Pixel Units'] = getPixelUnits(dataView, chunkOffsets[i], chunkLength);
24
+ } else if (chunkType === TYPE_TIME) {
25
+ tags['Modify Date'] = getModifyDate(dataView, chunkOffsets[i], chunkLength);
26
+ }
27
+ }
28
+
29
+ return tags;
30
+ }
31
+
32
+ function getPixelsPerUnitX(dataView, chunkOffset, chunkLength) {
33
+ const TAG_OFFSET = 0;
34
+ const TAG_SIZE = 4;
35
+
36
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, TAG_OFFSET, TAG_SIZE)) {
37
+ return undefined;
38
+ }
39
+
40
+ const value = Types.getLongAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + TAG_OFFSET);
41
+
42
+ return {
43
+ value,
44
+ description: '' + value
45
+ };
46
+ }
47
+
48
+ function getPixelsPerUnitY(dataView, chunkOffset, chunkLength) {
49
+ const TAG_OFFSET = 4;
50
+ const TAG_SIZE = 4;
51
+
52
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, TAG_OFFSET, TAG_SIZE)) {
53
+ return undefined;
54
+ }
55
+
56
+ const value = Types.getLongAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + TAG_OFFSET);
57
+
58
+ return {
59
+ value,
60
+ description: '' + value
61
+ };
62
+ }
63
+
64
+ function getPixelUnits(dataView, chunkOffset, chunkLength) {
65
+ const TAG_OFFSET = 8;
66
+ const TAG_SIZE = 1;
67
+
68
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, TAG_OFFSET, TAG_SIZE)) {
69
+ return undefined;
70
+ }
71
+
72
+ const value = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + TAG_OFFSET);
73
+
74
+ return {
75
+ value,
76
+ description: value === 1 ? 'meters' : 'Unknown'
77
+ };
78
+ }
79
+
80
+ function getModifyDate(dataView, chunkOffset, chunkLength) {
81
+ const TIME_TAG_SIZE = 7;
82
+
83
+ if (!tagFitsInBuffer(dataView, chunkOffset, chunkLength, 0, TIME_TAG_SIZE)) {
84
+ return undefined;
85
+ }
86
+
87
+ const year = Types.getShortAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET);
88
+ const month = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 2);
89
+ const day = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 3);
90
+ const hours = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 4);
91
+ const minutes = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 5);
92
+ const seconds = Types.getByteAt(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET + 6);
93
+
94
+ return {
95
+ value: [year, month, day, hours, minutes, seconds],
96
+ description: `${pad(year, 4)}-${pad(month, 2)}-${pad(day, 2)} ${pad(hours, 2)}:${pad(minutes, 2)}:${pad(seconds, 2)}`
97
+ };
98
+ }
99
+
100
+ function tagFitsInBuffer(dataView, chunkOffset, chunkLength, tagOffset, tagSize) {
101
+ return tagOffset + tagSize <= chunkLength && chunkOffset + PNG_CHUNK_DATA_OFFSET + tagOffset + tagSize <= dataView.byteLength;
102
+ }
103
+
104
+ function pad(number, size) {
105
+ return `${'0'.repeat(size - ('' + number).length)}${number}`;
106
+ }
@@ -3,41 +3,90 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import {getStringValueFromArray} from './utils.js';
6
+ import TagDecoder from './tag-decoder.js';
7
+ import {TYPE_TEXT, TYPE_ITXT} from './image-header-png.js';
6
8
 
7
9
  export default {
8
10
  read
9
11
  };
10
12
 
13
+ const STATE_KEYWORD = 'STATE_KEYWORD';
14
+ const STATE_COMPRESSION = 'STATE_COMPRESSION';
15
+ const STATE_LANG = 'STATE_LANG';
16
+ const STATE_TRANSLATED_KEYWORD = 'STATE_TRANSLATED_KEYWORD';
17
+ const STATE_TEXT = 'STATE_TEXT';
18
+ const COMPRESSION_SECTION_LENGTH = 2;
19
+
11
20
  function read(dataView, pngTextChunks) {
12
- return pngTextChunks.reduce((tags, {length, offset}) => {
13
- const {name, value} = getNameAndValue(dataView, offset, length);
14
- tags[name] = {
15
- value,
16
- description: value
17
- };
21
+ return pngTextChunks.reduce((tags, {offset, length, type}) => {
22
+ const {name, value, description} = getNameAndValue(dataView, offset, length, type);
23
+ if (name) {
24
+ tags[name] = {
25
+ value,
26
+ description
27
+ };
28
+ }
18
29
  return tags;
19
30
  }, {});
20
31
  }
21
32
 
22
- function getNameAndValue(dataView, offset, length) {
23
- const nameChars = [];
33
+ function getNameAndValue(dataView, offset, length, type) {
34
+ const keywordChars = [];
35
+ const langChars = [];
36
+ const translatedKeywordChars = [];
24
37
  const valueChars = [];
25
- let foundNullDivider = false;
38
+ let parsingState = STATE_KEYWORD;
39
+
26
40
  for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
41
+ if (parsingState === STATE_COMPRESSION) {
42
+ const compressionFlag = dataView.getUint8(offset + i);
43
+ if (compressionFlag !== 0) {
44
+ return {}; // We don't support compression.
45
+ }
46
+ i += COMPRESSION_SECTION_LENGTH;
47
+ parsingState = moveToNextState(type, parsingState);
48
+ }
27
49
  const byte = dataView.getUint8(offset + i);
28
50
  if (byte === 0) {
29
- foundNullDivider = true;
51
+ parsingState = moveToNextState(type, parsingState);
52
+ } else if (parsingState === STATE_KEYWORD) {
53
+ keywordChars.push(byte);
54
+ } else if (parsingState === STATE_LANG) {
55
+ langChars.push(byte);
56
+ } else if (parsingState === STATE_TRANSLATED_KEYWORD) {
57
+ translatedKeywordChars.push(byte);
30
58
  } else {
31
- if (!foundNullDivider) {
32
- nameChars.push(byte);
33
- } else {
34
- valueChars.push(byte);
35
- }
59
+ valueChars.push(byte);
36
60
  }
37
61
  }
38
62
 
63
+ const textValue = getStringValueFromArray(valueChars);
64
+
39
65
  return {
40
- name: getStringValueFromArray(nameChars),
41
- value: getStringValueFromArray(valueChars)
66
+ name: getName(type, langChars, keywordChars),
67
+ value: textValue,
68
+ description: type === TYPE_ITXT ? TagDecoder.decode('UTF-8', valueChars) : textValue
42
69
  };
43
70
  }
71
+
72
+ function moveToNextState(type, parsingState) {
73
+ if (parsingState === STATE_KEYWORD && type === TYPE_ITXT) {
74
+ return STATE_COMPRESSION;
75
+ }
76
+ if (parsingState === STATE_COMPRESSION) {
77
+ return STATE_LANG;
78
+ }
79
+ if (parsingState === STATE_LANG) {
80
+ return STATE_TRANSLATED_KEYWORD;
81
+ }
82
+ return STATE_TEXT;
83
+ }
84
+
85
+ function getName(type, langChars, keywordChars) {
86
+ const name = getStringValueFromArray(keywordChars);
87
+ if (type === TYPE_TEXT || langChars.length === 0) {
88
+ return name;
89
+ }
90
+ const lang = getStringValueFromArray(langChars);
91
+ return `${name} (${lang})`;
92
+ }
package/src/utils.js CHANGED
@@ -73,3 +73,29 @@ export function getBase64Image(image) {
73
73
  }
74
74
  return (new Buffer(image)).toString('base64'); // eslint-disable-line no-undef
75
75
  }
76
+
77
+ export function dataUriToBuffer(dataUri) {
78
+ const data = dataUri.substring(dataUri.indexOf(',') + 1);
79
+
80
+ if (dataUri.indexOf(';base64') !== -1) {
81
+ if (typeof atob !== 'undefined') {
82
+ return Uint8Array.from(atob(data), (char) => char.charCodeAt(0)).buffer;
83
+ }
84
+ if (typeof Buffer === 'undefined') {
85
+ return undefined;
86
+ }
87
+ if (typeof Buffer.from !== undefined) { // eslint-disable-line no-undef
88
+ return Buffer.from(data, 'base64'); // eslint-disable-line no-undef
89
+ }
90
+ return new Buffer(data, 'base64'); // eslint-disable-line no-undef
91
+ }
92
+
93
+ const decodedData = decodeURIComponent(data);
94
+ if (typeof Buffer !== 'undefined') {
95
+ if (typeof Buffer.from !== undefined) { // eslint-disable-line no-undef
96
+ return Buffer.from(decodedData); // eslint-disable-line no-undef
97
+ }
98
+ return new Buffer(decodedData); // eslint-disable-line no-undef
99
+ }
100
+ return Uint8Array.from(decodedData, (char) => char.charCodeAt(0)).buffer;
101
+ }