agglomerative-clustering 3.0.0-beta.3 → 3.0.0-beta.5
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/README.md +221 -24
- package/dist/clustering.cjs +3 -0
- package/dist/clustering.js +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +101 -43
- package/dist/index.d.cts +12 -1
- package/dist/index.d.ts +12 -1
- package/dist/index.mjs +76 -38
- package/dist/shared.cjs +57 -5
- package/dist/shared.mjs +51 -3
- package/dist/worker-thread.mjs +37 -7
- package/dist/worker.cjs +968 -0
- package/dist/worker.d.cts +142 -0
- package/dist/worker.d.ts +27 -2
- package/dist/worker.mjs +187 -58
- package/package.json +17 -6
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.
|
|
7
|
+
export const version = '3.0.0-beta.5';
|
|
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
|
} : {
|
|
@@ -31,6 +38,44 @@ export const isUint8Array = value => ArrayBuffer.isView(value) && tag(value) ===
|
|
|
31
38
|
export const isUint8ClampedArray = value => ArrayBuffer.isView(value) && tag(value) === '[object Uint8ClampedArray]';
|
|
32
39
|
export const isArrayBufferLike = value => tag(value) === '[object ArrayBuffer]' || tag(value) === '[object SharedArrayBuffer]';
|
|
33
40
|
|
|
41
|
+
// A typed array whose buffer was transferred away (to a worker, say) is empty and can't be read or viewed.
|
|
42
|
+
export const isDetached = view => {
|
|
43
|
+
const buffer = view.buffer;
|
|
44
|
+
if (typeof buffer.detached === 'boolean') {
|
|
45
|
+
return buffer.detached;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
new Uint8Array(buffer, 0, 0);
|
|
49
|
+
return false;
|
|
50
|
+
} catch (error) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
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
|
+
// An ImageData like `like`, with these pixels: made with `like`'s own class where it has one, so an ImageData from another
|
|
61
|
+
// implementation (node-canvas, in Node or under jsdom) comes back as one that implementation accepts, and where there is
|
|
62
|
+
// no global ImageData at all. The colour space is kept ('display-p3' canvases), since the pixels are still in it.
|
|
63
|
+
export const imageDataLike = (like, pixels) => {
|
|
64
|
+
const {
|
|
65
|
+
width,
|
|
66
|
+
height,
|
|
67
|
+
colorSpace
|
|
68
|
+
} = like;
|
|
69
|
+
const own = like.constructor;
|
|
70
|
+
const make = typeof own === 'function' && own.name === 'ImageData' ? own : typeof ImageData === 'function' ? ImageData : null;
|
|
71
|
+
if (!make) {
|
|
72
|
+
throw fail('invalid-argument', "Invalid image: an ImageData whose class can't be used to make the result. Pass { data, format: 'rgba', width } instead.", 'image');
|
|
73
|
+
}
|
|
74
|
+
return colorSpace ? new make(pixels, width, height, {
|
|
75
|
+
colorSpace
|
|
76
|
+
}) : new make(pixels, width, height);
|
|
77
|
+
};
|
|
78
|
+
|
|
34
79
|
// What to say about something that isn't an image, when it is a common thing to pass by mistake: each has a one-line fix.
|
|
35
80
|
export const imageHint = image => {
|
|
36
81
|
const decode = " Decode it to pixels first, then pass them.";
|
|
@@ -282,7 +327,7 @@ export const readHeader = clustering => {
|
|
|
282
327
|
// A header field with a fixed set of meanings (colour space, weight unit) that holds a value this version
|
|
283
328
|
// doesn't know is how a later release would add one, so it asks for an upgrade rather than misreading it.
|
|
284
329
|
if (clustering[4] > 1 || clustering[6] > 1) {
|
|
285
|
-
throw fail('unsupported-version', "
|
|
330
|
+
throw fail('unsupported-version', "Unsupported clustering: it uses a colour space or weight unit that this version of agglomerative-clustering does not know. If it came from a newer version, upgrade the package; otherwise it is damaged.", 'clustering');
|
|
286
331
|
}
|
|
287
332
|
const merges = length < headerLength + 3 ? 0 : Math.floor((length - headerLength - 3) / recordLength);
|
|
288
333
|
const padding = length < headerLength + 3 ? length - headerLength : length - headerLength - 3 - merges * recordLength;
|
|
@@ -309,7 +354,7 @@ export const readHeader = clustering => {
|
|
|
309
354
|
}
|
|
310
355
|
}
|
|
311
356
|
if (version > latestVersion) {
|
|
312
|
-
throw fail('unsupported-version', `
|
|
357
|
+
throw fail('unsupported-version', `Unsupported clustering: format version ${version} is newer than this version of agglomerative-clustering reads. Upgrade the package.`, 'clustering');
|
|
313
358
|
}
|
|
314
359
|
}
|
|
315
360
|
throw notAClustering(clustering);
|
|
@@ -324,6 +369,9 @@ export const checkPalette = palette => {
|
|
|
324
369
|
if (!isUint8Array(palette) && !isUint8ClampedArray(palette) && !Array.isArray(palette)) {
|
|
325
370
|
throw fail('invalid-argument', "Invalid palette: must be a Uint8Array of RGB bytes.", 'palette');
|
|
326
371
|
}
|
|
372
|
+
if (Array.isArray(palette) && palette.some(value => Array.isArray(value) || ArrayBuffer.isView(value))) {
|
|
373
|
+
throw fail('invalid-argument', "Invalid palette: a plain array must be flat RGB bytes, integers from 0 to 255, not an array of [r, g, b] colours. Flatten it first: colours.flat().", 'palette');
|
|
374
|
+
}
|
|
327
375
|
if (palette.length % 3 !== 0) {
|
|
328
376
|
throw fail('invalid-argument', "Invalid palette: length must be a multiple of 3 (flat RGB bytes).", 'palette');
|
|
329
377
|
}
|
package/dist/worker-thread.mjs
CHANGED
|
@@ -179,17 +179,47 @@ const onMessage = (data, post) => {
|
|
|
179
179
|
};
|
|
180
180
|
|
|
181
181
|
// No top-level await: bundlers build workers as classic scripts by default (Vite's 'iife'), which can't have it.
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
//
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
182
|
+
// Where Node's APIs exist (Node, Deno, Bun) the thread was usually started through worker_threads and talks through
|
|
183
|
+
// parentPort. Bun also defines the browser's `self.postMessage` inside such a thread, so `self` is not checked first. But a
|
|
184
|
+
// Web Worker can have Node's globals too (Electron with nodeIntegrationInWorker) and no worker_threads, or no parentPort:
|
|
185
|
+
// then it talks through `self`. Messages that arrive while that is decided are kept, since a Web Worker drops messages
|
|
186
|
+
// that find no listener. (`globalThis.process`, so bundlers don't take this for a use of Node's `process` to polyfill.)
|
|
187
|
+
const runtime = globalThis.process;
|
|
188
|
+
const isNode = !!(runtime && runtime.versions && runtime.versions.node);
|
|
189
|
+
const hasSelf = typeof self !== 'undefined' && typeof self.postMessage === 'function' && typeof self.addEventListener === 'function';
|
|
190
|
+
const viaSelf = (early = []) => {
|
|
191
|
+
const post = (message, transfer) => self.postMessage(message, transfer);
|
|
192
|
+
self.addEventListener('message', event => onMessage(event.data, post));
|
|
193
|
+
for (const data of early) {
|
|
194
|
+
onMessage(data, post);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
if (!isNode && hasSelf) {
|
|
198
|
+
viaSelf();
|
|
188
199
|
} else {
|
|
200
|
+
const early = [];
|
|
201
|
+
const keep = event => early.push(event.data);
|
|
202
|
+
if (hasSelf) {
|
|
203
|
+
self.addEventListener('message', keep);
|
|
204
|
+
}
|
|
205
|
+
const fallback = () => {
|
|
206
|
+
if (!hasSelf) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
self.removeEventListener('message', keep);
|
|
210
|
+
viaSelf(early);
|
|
211
|
+
};
|
|
189
212
|
// Built at run time so no bundler tries to resolve it (see worker.js).
|
|
190
213
|
import(/* webpackIgnore: true */ /* @vite-ignore */['node', 'worker_threads'].join(':')).then(({
|
|
191
214
|
parentPort
|
|
192
215
|
}) => {
|
|
216
|
+
if (!parentPort) {
|
|
217
|
+
fallback();
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (hasSelf) {
|
|
221
|
+
self.removeEventListener('message', keep);
|
|
222
|
+
}
|
|
193
223
|
parentPort.on('message', data => onMessage(data, (message, transfer) => parentPort.postMessage(message, transfer)));
|
|
194
|
-
});
|
|
224
|
+
}, fallback);
|
|
195
225
|
}
|