@lde/iiif-validator 0.1.1 → 0.1.2

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.
@@ -3,7 +3,7 @@
3
3
  * *what kind* of failure occurred, not a detailed diagnosis — only enough to
4
4
  * tell apart an unreachable host from a malformed document.
5
5
  */
6
- export type ManifestValidationReason = 'valid-manifest' | 'timeout' | 'network-error' | 'http-error' | 'invalid-json' | 'not-a-manifest';
6
+ export type ManifestValidationReason = 'valid-manifest' | 'timeout' | 'network-error' | 'http-error' | 'invalid-json' | 'binary-content' | 'not-a-manifest';
7
7
  /**
8
8
  * Verdict returned by {@link validateManifest}.
9
9
  */
@@ -1 +1 @@
1
- {"version":3,"file":"validateManifest.d.ts","sourceRoot":"","sources":["../src/validateManifest.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,GAChC,gBAAgB,GAChB,SAAS,GACT,eAAe,GACf,YAAY,GACZ,cAAc,GACd,gBAAgB,CAAC;AAErB;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,0EAA0E;IAC1E,KAAK,EAAE,OAAO,CAAC;IACf,4CAA4C;IAC5C,MAAM,EAAE,wBAAwB,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC,+DAA+D;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAKD;;;;GAIG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,uBAAuB,GAChC,OAAO,CAAC,kBAAkB,CAAC,CA6B7B"}
1
+ {"version":3,"file":"validateManifest.d.ts","sourceRoot":"","sources":["../src/validateManifest.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,wBAAwB,GAChC,gBAAgB,GAChB,SAAS,GACT,eAAe,GACf,YAAY,GACZ,cAAc,GACd,gBAAgB,GAChB,gBAAgB,CAAC;AAErB;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,0EAA0E;IAC1E,KAAK,EAAE,OAAO,CAAC;IACf,4CAA4C;IAC5C,MAAM,EAAE,wBAAwB,CAAC;CAClC;AAED;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,KAAK,CAAC,EAAE,OAAO,UAAU,CAAC,KAAK,CAAC;IAChC,+DAA+D;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAKD;;;;GAIG;AACH,wBAAsB,gBAAgB,CACpC,GAAG,EAAE,MAAM,EACX,OAAO,CAAC,EAAE,uBAAuB,GAChC,OAAO,CAAC,kBAAkB,CAAC,CAwC7B"}
@@ -21,6 +21,16 @@ export async function validateManifest(url, options) {
21
21
  if (!response.ok) {
22
22
  return { valid: false, reason: 'http-error' };
23
23
  }
24
+ // A manifest is JSON. When the server announces a binary media type, skip
25
+ // reading the body: a sampled `schema:contentUrl` can dereference to the
26
+ // full-resolution image, audio or video asset, and `response.json()` would
27
+ // buffer the whole thing before failing to parse it — wasting bandwidth and
28
+ // time in the pipeline that calls this for every sampled manifest. Cancel the
29
+ // stream so the connection is freed without the download.
30
+ if (isBinaryMedia(response.headers.get('content-type'))) {
31
+ await response.body?.cancel();
32
+ return { valid: false, reason: 'binary-content' };
33
+ }
24
34
  let body;
25
35
  try {
26
36
  body = await response.json();
@@ -33,6 +43,20 @@ export async function validateManifest(url, options) {
33
43
  }
34
44
  return { valid: false, reason: 'not-a-manifest' };
35
45
  }
46
+ /**
47
+ * Whether a `Content-Type` header announces a binary media asset (image, audio
48
+ * or video) rather than a JSON document. Used to skip downloading non-manifest
49
+ * media. A missing or ambiguous type (e.g. `text/plain`, `application/octet-stream`)
50
+ * returns `false` so a manifest served with an odd type is still parsed.
51
+ */
52
+ function isBinaryMedia(contentType) {
53
+ if (contentType === null)
54
+ return false;
55
+ const mediaType = contentType.toLowerCase();
56
+ return (mediaType.startsWith('image/') ||
57
+ mediaType.startsWith('audio/') ||
58
+ mediaType.startsWith('video/'));
59
+ }
36
60
  /**
37
61
  * Classify a thrown `fetch` error. An aborted request (our own
38
62
  * `AbortSignal.timeout` firing, surfaced as `AbortError`/`TimeoutError`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lde/iiif-validator",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "repository": {
5
5
  "url": "git+https://github.com/ldelements/lde.git",
6
6
  "directory": "packages/iiif-validator"