exifreader 4.40.2 → 4.40.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.40.2",
3
+ "version": "4.40.3",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -78,6 +78,8 @@ function legacyRange(options) {
78
78
  * @param {{start?: number, end?: number}} [range] `end` is exclusive. Omit (or pass `Infinity`) to read to EOF.
79
79
  * @returns {Promise<{buffer: ArrayBuffer, totalSize: number|undefined, status: number|undefined}>}
80
80
  * `totalSize` is taken from the `Content-Range` or `Content-Length` response header when present.
81
+ * Rejects with `Could not fetch file: <status>` on non-2xx responses, except 416 which the
82
+ * `length: 'auto'` loop consumes as a fall-back signal. Mirrors `nodeGetRange`.
81
83
  */
82
84
  export function fetchRange(url, {start = 0, end} = {}) {
83
85
  const options = {method: 'GET'};
@@ -85,8 +87,12 @@ export function fetchRange(url, {start = 0, end} = {}) {
85
87
  options.headers = {range: buildRangeHeader(start, end)};
86
88
  }
87
89
  return fetch(url, options).then((response) => {
88
- const totalSize = totalSizeFromFetchResponse(response);
89
90
  const status = response && typeof response.status === 'number' ? response.status : undefined;
91
+ if (status !== undefined && !isAcceptableFetchStatus(status)) {
92
+ const statusText = response.statusText || '';
93
+ return Promise.reject(new Error(`Could not fetch file: ${status} ${statusText}`.trim()));
94
+ }
95
+ const totalSize = totalSizeFromFetchResponse(response);
90
96
  return Promise.resolve(response.arrayBuffer()).then((buffer) => ({buffer, totalSize, status}));
91
97
  });
92
98
  }
@@ -119,6 +125,15 @@ function totalSizeFromFetchResponse(response) {
119
125
  return undefined;
120
126
  }
121
127
 
128
+ function isAcceptableFetchStatus(status) {
129
+ if ((status >= HTTP_STATUS_OK) && (status <= HTTP_STATUS_SUCCESS_MAX)) {
130
+ return true;
131
+ }
132
+ // 416 is consumed by the `length: 'auto'` adaptive loop (it falls back
133
+ // to a full read), so it must not be surfaced as a fetch error.
134
+ return status === HTTP_STATUS_RANGE_NOT_SATISFIABLE;
135
+ }
136
+
122
137
  /**
123
138
  * Range-aware Node `http(s).get`. Same contract as `fetchRange`. Rejects
124
139
  * on non-2xx responses with the status line in the error message.
@@ -152,7 +167,7 @@ export function nodeGetRange(url, {start = 0, end} = {}) {
152
167
  response.resume();
153
168
  resolve({buffer: Buffer.alloc(0), totalSize: totalSizeFromNodeResponse(response), status: response.statusCode});
154
169
  } else {
155
- reject(`Could not fetch file: ${response.statusCode} ${response.statusMessage}`);
170
+ reject(new Error(`Could not fetch file: ${response.statusCode} ${response.statusMessage}`));
156
171
  response.resume();
157
172
  }
158
173
  }).on('error', (error) => reject(error));