agglomerative-clustering 3.0.0-beta.4 → 3.0.0-beta.6

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/dist/shared.mjs CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  // The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
6
6
  // is built from, src/version.hpp) in step with it. Do not edit it by hand.
7
- export const version = '3.0.0-beta.4';
7
+ export const version = '3.0.0-beta.6';
8
8
  export const colorSpaces = {
9
9
  'rgb': 0,
10
10
  'oklab': 1
@@ -17,6 +17,13 @@ export const colorSpaceName = code => code === 1 ? 'oklab' : 'rgb';
17
17
  // 'worker-terminated', 'worker-failed' and 'invalid-handle'. When the error is about one argument, `argument` names it
18
18
  // for programs (the message is for people): 'image', 'k', 'clustering', 'palette', 'options', or 'options.<key>' for
19
19
  // one option.
20
+ // Every code `fail` gives, for programs that branch on them or forward them (a wrapper package, an IPC bridge).
21
+ export const errorCodes = Object.freeze(['invalid-argument', 'invalid-clustering', 'unsupported-version', 'out-of-memory', 'wasm-load-failed', 'worker-terminated', 'worker-failed', 'invalid-handle']);
22
+
23
+ // Whether `value` is one of this library's errors: an error (or, after crossing Electron's IPC or a postMessage, a plain
24
+ // object) with a string message and one of `errorCodes`. Checking `code` alone isn't enough: Node's own errors have a
25
+ // `code` too ('ENOENT'), and an abort rejects with the signal's reason, which has none.
26
+ export const isCodedError = value => value !== null && typeof value === 'object' && typeof value.message === 'string' && typeof value.code === 'string' && errorCodes.includes(value.code);
20
27
  export const fail = (code, message, argument) => Object.assign(new Error(message), argument === undefined ? {
21
28
  code
22
29
  } : {
@@ -45,6 +52,21 @@ export const isDetached = view => {
45
52
  }
46
53
  };
47
54
 
55
+ // An ImageData: checked by tag as well as `instanceof`, so one from another realm (an iframe's canvas) is recognised, and by
56
+ // its class's name with the shape of one, for implementations that have neither the tag nor the global (node-canvas under
57
+ // jsdom, skia-canvas, whose tag is plain [object Object]).
58
+ export const isImageData = value => typeof value === 'object' && value !== null && (typeof ImageData !== 'undefined' && value instanceof ImageData || tag(value) === '[object ImageData]' || typeof value.constructor === 'function' && value.constructor.name === 'ImageData' && isUint8ClampedArray(value.data) && Number.isInteger(value.width) && Number.isInteger(value.height));
59
+
60
+ // ImageData can hold other pixel formats than 8-bit RGBA (Chromium's pixelFormat: 'rgba-float16', with a Float16Array of
61
+ // pixels). The library reads 8-bit RGBA only, so those are rejected, rather than read as the bytes of their floats.
62
+ export const checkImageDataPixels = image => {
63
+ if (isUint8ClampedArray(image.data)) {
64
+ return;
65
+ }
66
+ const format = typeof image.pixelFormat === 'string' ? ` (pixelFormat: '${image.pixelFormat}')` : '';
67
+ throw fail('invalid-argument', `Invalid image: this ImageData doesn't hold 8-bit RGBA pixels${format}, and the library reads only those. Ask for 8-bit pixels with getImageData(x, y, width, height, { pixelFormat: 'rgba-unorm8' }), or draw it to a canvas with the default format and take getImageData() from that.`, 'image');
68
+ };
69
+
48
70
  // An ImageData like `like`, with these pixels: made with `like`'s own class where it has one, so an ImageData from another
49
71
  // implementation (node-canvas, in Node or under jsdom) comes back as one that implementation accepts, and where there is
50
72
  // no global ImageData at all. The colour space is kept ('display-p3' canvases), since the pixels are still in it.
@@ -311,7 +333,10 @@ export const readHeader = clustering => {
311
333
  if (version === 3 && length >= 13) {
312
334
  const headerLength = clustering[2],
313
335
  recordLength = clustering[3];
314
- if (headerLength >= 13 && recordLength >= 21 && headerLength <= length) {
336
+ // Header lengths are 22 (every release) or 13 (the fixture from before 3.0.0-beta.1, kept readable by the format
337
+ // rules), or longer from a later release that adds fields. 14 to 20 was never written: it is a damaged length, which
338
+ // would also put the checksum field out of reach, so the checksum couldn't catch it.
339
+ if ((headerLength === 13 || headerLength >= checksumAt + 4) && recordLength >= 21 && headerLength <= length) {
315
340
  // A header field with a fixed set of meanings (colour space, weight unit) that holds a value this version
316
341
  // doesn't know is how a later release would add one, so it asks for an upgrade rather than misreading it.
317
342
  if (clustering[4] > 1 || clustering[6] > 1) {
@@ -320,8 +345,8 @@ export const readHeader = clustering => {
320
345
  const merges = length < headerLength + 3 ? 0 : Math.floor((length - headerLength - 3) / recordLength);
321
346
  const padding = length < headerLength + 3 ? length - headerLength : length - headerLength - 3 - merges * recordLength;
322
347
  if (padding < 9) {
323
- // Headers shorter than 21 bytes (from before 3.0.0-beta.1) have no checksum field. A damaged header length that
324
- // makes one look that short skips this check, but then the records don't line up, which the replay reports.
348
+ // A 13-byte header (from before 3.0.0-beta.1) has no checksum field. A damaged header length of 13 skips this
349
+ // check, but then the records don't line up, which the replay reports.
325
350
  if (!checked.has(clustering) && headerLength >= checksumAt + 4) {
326
351
  const stored = (clustering[checksumAt] | clustering[checksumAt + 1] << 8 | clustering[checksumAt + 2] << 16 | clustering[checksumAt + 3] << 24) >>> 0;
327
352
  if (stored !== 0 && stored !== crc32(clustering)) {