agglomerative-clustering 4.0.0-beta.4 → 4.0.0

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 CHANGED
@@ -13,7 +13,7 @@ Most colour quantizers answer one question: *give me k colours*. Ask for a diffe
13
13
  - **Every palette size from one pass.** Clustering a photo takes about 70 ms. After that, a palette of any size takes about 0.4 ms.
14
14
  - **Palettes nest, in a stable order.** The 16-colour palette is the 32-colour one with pairs merged, so swatches don't jump around as the count changes.
15
15
  - **It knows each colour's share of the image,** so you can show swatches with percentages, or pick the smallest palette that meets an error target.
16
- - **Transparency-aware.** A logo's transparent background never ends up in its palette. GIF and palette PNG output are built in.
16
+ - **Transparency-aware.** A logo's transparent background never ends up in its palette. Indexed output (`quantizeToIndices`) gives a GIF or palette PNG writer a palette, one index per pixel and the transparent entry's position; the [guides](docs/guides.md#gifs-and-palette-pngs) show it with gifenc and fast-png.
17
17
  - **Off the main thread.** `agglomerative-clustering/worker` runs the same API in a Web Worker or Node worker thread.
18
18
  - **Accurate and fast.** The lowest average error of the six JavaScript quantizers we benchmarked, at about the speed of libimagequant ([benchmarks](#benchmarks)).
19
19
  - **Small and portable.** About 86 KB of WebAssembly (41 KB gzipped)<!-- bench/spikes/slicing-mapping/results-size.txt -->, Node and browsers, TypeScript types, no runtime dependencies, deterministic output.
Binary file
package/dist/shared.cjs CHANGED
@@ -13,7 +13,7 @@ exports.version = exports.tooLarge = exports.sniffImageFormat = exports.snapshot
13
13
 
14
14
  // The library's version: package.json is the source, and scripts/sync-version.mjs keeps this line (and the copy the .wasm
15
15
  // is built from, src/version.hpp) in step with it. Do not edit it by hand.
16
- const version = exports.version = '4.0.0-beta.4';
16
+ const version = exports.version = '4.0.0';
17
17
  const colorSpaceCodes = exports.colorSpaceCodes = {
18
18
  'rgb': 0,
19
19
  'oklab': 1
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 = '4.0.0-beta.4';
7
+ export const version = '4.0.0';
8
8
  export const colorSpaceCodes = {
9
9
  'rgb': 0,
10
10
  'oklab': 1
package/dist/worker.cjs CHANGED
@@ -207,7 +207,7 @@ const start = async (receive, chosen) => {
207
207
  // A page with an opaque origin (a sandboxed iframe without allow-same-origin, as plugin hosts such as Figma use) can't
208
208
  // start a worker from any http(s) script, nor, in Chromium, from a blob: URL; a data: URL module can, and imports it.
209
209
  const opaque = !crossOrigin && isOpaqueOrigin() && webOrigins.includes(script.protocol);
210
- const scriptHint = "Check that worker-thread.mjs is served (open its URL), with index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it, which it imports, or say where your copy of dist/ is with createWorker({ script }) or, for the shared worker, init({ script }). The worker's files can also be from different versions of agglomerative-clustering: serve all of dist/ from this version.";
210
+ const scriptHint = "Check that worker-thread.mjs is served (open its URL), with index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it, which it imports, or say where your copy of dist/ is with createWorker({ script }) or, for the shared worker, init({ script }). The worker's files can also be from different versions of agglomerative-clustering. Serve all of the package's dist/ files from this version.";
211
211
  let worker,
212
212
  source = null;
213
213
  try {
@@ -264,12 +264,17 @@ const start = async (receive, chosen) => {
264
264
  });
265
265
  worker.on('message', receive);
266
266
  // A worker script that can't be found, or that can't find what it imports: a copy of dist/ split up or bundled away.
267
- const versions = " The worker's files can also be from different versions of agglomerative-clustering: serve all of dist/ from this version.";
267
+ const versions = " The worker's files can also be from different versions of agglomerative-clustering. Serve all of the package's dist/ files from this version.";
268
+ // A link or load failure (a script of another version, a file the thread can't import): not a script that throws, nor a crash.
269
+ const linking = error => error && (/^ERR_(MODULE|REQUIRE|UNKNOWN_FILE|UNSUPPORTED|INVALID_MODULE|PACKAGE|IMPORT)/.test(String(error.code)) || /does not provide an export named|The requested module/.test(String(error.message)));
268
270
  const missing = error => error && (error.code === 'ERR_MODULE_NOT_FOUND' || /Cannot find module|import\.meta' outside a module|Qualified path resolution failed/.test(String(error.message)));
269
271
  // Yarn Plug'n'Play keeps packages in zip archives, which Node reads ES modules from only through Yarn's loader.
270
272
  const inZip = error => /\.zip[\\/]/.test(String(error && error.message));
271
273
  worker.on('error', error => receive({
272
- failure: missing(error) ? inZip(error) ? `${String(error.message).split('\n')[0]}. Under Yarn Plug'n'Play the package sits in a zip archive, which a worker thread can load its ES modules from only through Yarn's loader: run the app with yarn node (or node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), or unplug the package (yarn unplug agglomerative-clustering). Otherwise keep dist/ together and say where it is with createWorker({ script }).${versions}` : `${String(error.message).split('\n')[0]}. The worker script (worker-thread.mjs) needs index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it: keep dist/ together, and say where it is with createWorker({ script }) or, for the shared worker, init({ script }) (a path, or a file: URL). In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the file tracer may have left them out: include node_modules/agglomerative-clustering/dist in the function's files.${versions}` : error.message
274
+ failure: missing(error) ? inZip(error) ? `${String(error.message).split('\n')[0]}. Under Yarn Plug'n'Play the package sits in a zip archive, which a worker thread can load its ES modules from only through Yarn's loader: run the app with yarn node (or node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), or unplug the package (yarn unplug agglomerative-clustering). Otherwise keep dist/ together and say where it is with createWorker({ script }).${versions}` : `${String(error.message).split('\n')[0]}. The worker script (worker-thread.mjs) needs index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it: keep dist/ together, and say where it is with createWorker({ script }) or, for the shared worker, init({ script }) (a path, or a file: URL). In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the file tracer may have left them out: include node_modules/agglomerative-clustering/dist in the function's files.${versions}`
275
+ // A link or load error gets the same advice; anything else (a script that throws, a crash) keeps its own message.
276
+ : linking(error) ? `${String(error.message).split('\n')[0].replace(/\.$/, '')}.${versions}` : error && typeof error.message === 'string' ? error.message : `The worker thread threw a value that is not an Error.`,
277
+ cause: error
273
278
  }));
274
279
  worker.on('exit', code => receive({
275
280
  failure: `The worker exited unexpectedly (code ${code}).`
@@ -527,7 +532,8 @@ const takeControls = (options, allowed, image) => {
527
532
  options = (0, _shared.snapshot)(options);
528
533
  // The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
529
534
  // at all, for a Proxy or a function): read it here the way the main entry does, once, and validate and send the copy.
530
- if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
535
+ // A function that takes no dither is left to parseOptions, which names the unknown key before the object is read.
536
+ if (allowed.includes('dither') && options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
531
537
  options.dither = (0, _shared.isOptionsObject)(options.dither) ? Object.assign({}, (0, _shared.snapshot)(options.dither)) : options.dither;
532
538
  }
533
539
  const parsed = (0, _shared.parseOptions)(options, allowed, image);
@@ -655,7 +661,7 @@ const makeWorker = (options, allowScript) => {
655
661
  // The worker script to start instead of the package's own (see scriptOption); set by init({ script }) for the shared one.
656
662
  let script = options.script === undefined ? null : scriptOption(options.script);
657
663
  // Where the worker finds its .wasm: sent as the worker's first call, and repeated until it has succeeded.
658
- const initialWasm = options.wasm === undefined ? undefined : wasmOption(options.wasm);
664
+ let initialWasm = options.wasm === undefined ? undefined : wasmOption(options.wasm);
659
665
  let initial = initialWasm === undefined ? null : {
660
666
  wasm: initialWasm
661
667
  };
@@ -713,7 +719,7 @@ const makeWorker = (options, allowScript) => {
713
719
  // A worker that failed (could not start, crashed or exited) is dropped: the calls waiting on it get the failure, and
714
720
  // the next call starts a new one, which either works or fails the same clear way. Without this, later calls would be
715
721
  // posted to a thread that will never answer and hang forever. Handles kept in the dropped worker are gone with it.
716
- const lose = reason => {
722
+ const lose = (reason, cause) => {
717
723
  const lost = connection;
718
724
  generation++;
719
725
  connection = null;
@@ -730,7 +736,9 @@ const makeWorker = (options, allowScript) => {
730
736
  pending.clear();
731
737
  for (const call of calls) {
732
738
  call.cleanup();
733
- call.reject((0, _shared.fail)('worker-failed', reason));
739
+ call.reject(cause === undefined ? (0, _shared.fail)('worker-failed', reason) : Object.assign((0, _shared.fail)('worker-failed', reason), {
740
+ cause
741
+ }));
734
742
  }
735
743
  if (lost) {
736
744
  try {
@@ -741,7 +749,7 @@ const makeWorker = (options, allowScript) => {
741
749
  const receive = message => {
742
750
  if (message.failure !== undefined) {
743
751
  if (!terminated) {
744
- lose(`Worker failed: ${message.failure}`);
752
+ lose(`Worker failed: ${message.failure}`, message.cause);
745
753
  }
746
754
  return;
747
755
  }
@@ -847,7 +855,7 @@ const makeWorker = (options, allowScript) => {
847
855
  if (terminated) {
848
856
  throw (0, _shared.fail)('worker-terminated', "Worker terminated.");
849
857
  }
850
- const reason = `The worker script is from another version of agglomerative-clustering (${typeof theirs === 'string' ? theirs : 'an older one'}; this is ${_shared.version}). Use the worker-thread.mjs from this version.`;
858
+ const reason = `The worker script is from another version of agglomerative-clustering (${typeof theirs === 'string' ? theirs : 'an older one'}; this is ${_shared.version}). Serve all of the package's dist/ files from this version.`;
851
859
  if (mine === generation) {
852
860
  lose(reason);
853
861
  }
@@ -857,8 +865,11 @@ const makeWorker = (options, allowScript) => {
857
865
  await shaking;
858
866
  }
859
867
  if (initial && !(method === 'init' && args.length > 0 && args[0].wasm !== undefined)) {
868
+ const sending = initial;
860
869
  initialising = initialising || post('init', [initial], [], origin).then(() => {
861
- initial = null;
870
+ if (initial === sending) {
871
+ initial = null;
872
+ }
862
873
  }, error => {
863
874
  initialising = null;
864
875
  throw error;
@@ -1049,7 +1060,21 @@ const makeWorker = (options, allowScript) => {
1049
1060
  } catch (error) {
1050
1061
  return Promise.reject(error);
1051
1062
  }
1052
- return invoke('init', checked === undefined || Object.keys(checked).length === 0 ? [] : [checked], [], origin).then(() => undefined);
1063
+ if (terminated) {
1064
+ return Promise.reject((0, _shared.fail)('worker-terminated', "Worker terminated."));
1065
+ }
1066
+ // The options are only recorded: `wasm` is sent as the worker's first call (see `initial`), and a worker that can't
1067
+ // start or load fails the calls, which is where a caller handles it. So init() can't leave a rejection nobody
1068
+ // awaits (it is often written as a setup line). The warm-up below is started and its failure dropped for the same reason.
1069
+ if (checked !== undefined && checked.wasm !== undefined) {
1070
+ initialWasm = checked.wasm;
1071
+ initial = {
1072
+ wasm: initialWasm
1073
+ };
1074
+ initialising = null;
1075
+ }
1076
+ invoke('init', [], [], origin).catch(() => {});
1077
+ return Promise.resolve();
1053
1078
  };
1054
1079
 
1055
1080
  // Whether a value is an image where a palette size was expected (a pixel array, an ImageData or `{data, ...}`).
package/dist/worker.d.cts CHANGED
@@ -123,7 +123,10 @@ export interface LoadedClusteringHandle {
123
123
 
124
124
  /** The main entry's functions, run in a worker, plus handles and `terminate`. */
125
125
  export interface AgglomerativeWorker {
126
- /** Loads the module in the worker. `wasm` is the only option, and `createWorker({ wasm })` does the same. */
126
+ /**
127
+ * Records `wasm` (the only option; `createWorker({ wasm })` does the same) and warms the worker up. Resolves once the
128
+ * options are recorded, without waiting for the worker: a worker that can't start or load fails the calls that follow.
129
+ */
127
130
  init(options?: InitOptions): Promise<void>;
128
131
  /** The main entry's `buildClustering`, run in this worker. */
129
132
  buildClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
@@ -161,8 +164,9 @@ export function terminate(): Promise<void>;
161
164
 
162
165
  // The main entry's functions, run in one shared worker started on first use.
163
166
  /**
164
- * The main entry's `init`, run in the shared worker (see `createWorker` for a worker of your own). It also takes the
165
- * shared worker's `script`, before any other call.
167
+ * The main entry's `init` for the shared worker (see `createWorker` for a worker of your own). It also takes the
168
+ * shared worker's `script`, before any other call. It checks the options, records them and resolves, so it is safe
169
+ * without `await`: a worker that can't start or load fails the calls that follow, not `init`.
166
170
  */
167
171
  export function init(options?: SharedInitOptions): Promise<void>;
168
172
  /** The main entry's `buildClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
package/dist/worker.d.ts CHANGED
@@ -123,7 +123,10 @@ export interface LoadedClusteringHandle {
123
123
 
124
124
  /** The main entry's functions, run in a worker, plus handles and `terminate`. */
125
125
  export interface AgglomerativeWorker {
126
- /** Loads the module in the worker. `wasm` is the only option, and `createWorker({ wasm })` does the same. */
126
+ /**
127
+ * Records `wasm` (the only option; `createWorker({ wasm })` does the same) and warms the worker up. Resolves once the
128
+ * options are recorded, without waiting for the worker: a worker that can't start or load fails the calls that follow.
129
+ */
127
130
  init(options?: InitOptions): Promise<void>;
128
131
  /** The main entry's `buildClustering`, run in this worker. */
129
132
  buildClustering(image: ImageLike, options?: ClusteringOptions): Promise<Clustering>;
@@ -161,8 +164,9 @@ export function terminate(): Promise<void>;
161
164
 
162
165
  // The main entry's functions, run in one shared worker started on first use.
163
166
  /**
164
- * The main entry's `init`, run in the shared worker (see `createWorker` for a worker of your own). It also takes the
165
- * shared worker's `script`, before any other call.
167
+ * The main entry's `init` for the shared worker (see `createWorker` for a worker of your own). It also takes the
168
+ * shared worker's `script`, before any other call. It checks the options, records them and resolves, so it is safe
169
+ * without `await`: a worker that can't start or load fails the calls that follow, not `init`.
166
170
  */
167
171
  export function init(options?: SharedInitOptions): Promise<void>;
168
172
  /** The main entry's `buildClustering`, run in the shared worker (see `createWorker` for a worker of your own). */
package/dist/worker.mjs CHANGED
@@ -138,7 +138,7 @@ const start = async (receive, chosen) => {
138
138
  // A page with an opaque origin (a sandboxed iframe without allow-same-origin, as plugin hosts such as Figma use) can't
139
139
  // start a worker from any http(s) script, nor, in Chromium, from a blob: URL; a data: URL module can, and imports it.
140
140
  const opaque = !crossOrigin && isOpaqueOrigin() && webOrigins.includes(script.protocol);
141
- const scriptHint = "Check that worker-thread.mjs is served (open its URL), with index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it, which it imports, or say where your copy of dist/ is with createWorker({ script }) or, for the shared worker, init({ script }). The worker's files can also be from different versions of agglomerative-clustering: serve all of dist/ from this version.";
141
+ const scriptHint = "Check that worker-thread.mjs is served (open its URL), with index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it, which it imports, or say where your copy of dist/ is with createWorker({ script }) or, for the shared worker, init({ script }). The worker's files can also be from different versions of agglomerative-clustering. Serve all of the package's dist/ files from this version.";
142
142
  let worker,
143
143
  source = null;
144
144
  try {
@@ -195,12 +195,17 @@ const start = async (receive, chosen) => {
195
195
  });
196
196
  worker.on('message', receive);
197
197
  // A worker script that can't be found, or that can't find what it imports: a copy of dist/ split up or bundled away.
198
- const versions = " The worker's files can also be from different versions of agglomerative-clustering: serve all of dist/ from this version.";
198
+ const versions = " The worker's files can also be from different versions of agglomerative-clustering. Serve all of the package's dist/ files from this version.";
199
+ // A link or load failure (a script of another version, a file the thread can't import): not a script that throws, nor a crash.
200
+ const linking = error => error && (/^ERR_(MODULE|REQUIRE|UNKNOWN_FILE|UNSUPPORTED|INVALID_MODULE|PACKAGE|IMPORT)/.test(String(error.code)) || /does not provide an export named|The requested module/.test(String(error.message)));
199
201
  const missing = error => error && (error.code === 'ERR_MODULE_NOT_FOUND' || /Cannot find module|import\.meta' outside a module|Qualified path resolution failed/.test(String(error.message)));
200
202
  // Yarn Plug'n'Play keeps packages in zip archives, which Node reads ES modules from only through Yarn's loader.
201
203
  const inZip = error => /\.zip[\\/]/.test(String(error && error.message));
202
204
  worker.on('error', error => receive({
203
- failure: missing(error) ? inZip(error) ? `${String(error.message).split('\n')[0]}. Under Yarn Plug'n'Play the package sits in a zip archive, which a worker thread can load its ES modules from only through Yarn's loader: run the app with yarn node (or node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), or unplug the package (yarn unplug agglomerative-clustering). Otherwise keep dist/ together and say where it is with createWorker({ script }).${versions}` : `${String(error.message).split('\n')[0]}. The worker script (worker-thread.mjs) needs index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it: keep dist/ together, and say where it is with createWorker({ script }) or, for the shared worker, init({ script }) (a path, or a file: URL). In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the file tracer may have left them out: include node_modules/agglomerative-clustering/dist in the function's files.${versions}` : error.message
205
+ failure: missing(error) ? inZip(error) ? `${String(error.message).split('\n')[0]}. Under Yarn Plug'n'Play the package sits in a zip archive, which a worker thread can load its ES modules from only through Yarn's loader: run the app with yarn node (or node --require ./.pnp.cjs --loader ./.pnp.loader.mjs), or unplug the package (yarn unplug agglomerative-clustering). Otherwise keep dist/ together and say where it is with createWorker({ script }).${versions}` : `${String(error.message).split('\n')[0]}. The worker script (worker-thread.mjs) needs index.mjs, shared.mjs, choose.mjs, clustering.js and clustering.wasm next to it: keep dist/ together, and say where it is with createWorker({ script }) or, for the shared worker, init({ script }) (a path, or a file: URL). In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the file tracer may have left them out: include node_modules/agglomerative-clustering/dist in the function's files.${versions}`
206
+ // A link or load error gets the same advice; anything else (a script that throws, a crash) keeps its own message.
207
+ : linking(error) ? `${String(error.message).split('\n')[0].replace(/\.$/, '')}.${versions}` : error && typeof error.message === 'string' ? error.message : `The worker thread threw a value that is not an Error.`,
208
+ cause: error
204
209
  }));
205
210
  worker.on('exit', code => receive({
206
211
  failure: `The worker exited unexpectedly (code ${code}).`
@@ -458,7 +463,8 @@ const takeControls = (options, allowed, image) => {
458
463
  options = snapshot(options);
459
464
  // The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
460
465
  // at all, for a Proxy or a function): read it here the way the main entry does, once, and validate and send the copy.
461
- if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
466
+ // A function that takes no dither is left to parseOptions, which names the unknown key before the object is read.
467
+ if (allowed.includes('dither') && options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
462
468
  options.dither = isOptionsObject(options.dither) ? Object.assign({}, snapshot(options.dither)) : options.dither;
463
469
  }
464
470
  const parsed = parseOptions(options, allowed, image);
@@ -586,7 +592,7 @@ const makeWorker = (options, allowScript) => {
586
592
  // The worker script to start instead of the package's own (see scriptOption); set by init({ script }) for the shared one.
587
593
  let script = options.script === undefined ? null : scriptOption(options.script);
588
594
  // Where the worker finds its .wasm: sent as the worker's first call, and repeated until it has succeeded.
589
- const initialWasm = options.wasm === undefined ? undefined : wasmOption(options.wasm);
595
+ let initialWasm = options.wasm === undefined ? undefined : wasmOption(options.wasm);
590
596
  let initial = initialWasm === undefined ? null : {
591
597
  wasm: initialWasm
592
598
  };
@@ -644,7 +650,7 @@ const makeWorker = (options, allowScript) => {
644
650
  // A worker that failed (could not start, crashed or exited) is dropped: the calls waiting on it get the failure, and
645
651
  // the next call starts a new one, which either works or fails the same clear way. Without this, later calls would be
646
652
  // posted to a thread that will never answer and hang forever. Handles kept in the dropped worker are gone with it.
647
- const lose = reason => {
653
+ const lose = (reason, cause) => {
648
654
  const lost = connection;
649
655
  generation++;
650
656
  connection = null;
@@ -661,7 +667,9 @@ const makeWorker = (options, allowScript) => {
661
667
  pending.clear();
662
668
  for (const call of calls) {
663
669
  call.cleanup();
664
- call.reject(fail('worker-failed', reason));
670
+ call.reject(cause === undefined ? fail('worker-failed', reason) : Object.assign(fail('worker-failed', reason), {
671
+ cause
672
+ }));
665
673
  }
666
674
  if (lost) {
667
675
  try {
@@ -672,7 +680,7 @@ const makeWorker = (options, allowScript) => {
672
680
  const receive = message => {
673
681
  if (message.failure !== undefined) {
674
682
  if (!terminated) {
675
- lose(`Worker failed: ${message.failure}`);
683
+ lose(`Worker failed: ${message.failure}`, message.cause);
676
684
  }
677
685
  return;
678
686
  }
@@ -778,7 +786,7 @@ const makeWorker = (options, allowScript) => {
778
786
  if (terminated) {
779
787
  throw fail('worker-terminated', "Worker terminated.");
780
788
  }
781
- const reason = `The worker script is from another version of agglomerative-clustering (${typeof theirs === 'string' ? theirs : 'an older one'}; this is ${version}). Use the worker-thread.mjs from this version.`;
789
+ const reason = `The worker script is from another version of agglomerative-clustering (${typeof theirs === 'string' ? theirs : 'an older one'}; this is ${version}). Serve all of the package's dist/ files from this version.`;
782
790
  if (mine === generation) {
783
791
  lose(reason);
784
792
  }
@@ -788,8 +796,11 @@ const makeWorker = (options, allowScript) => {
788
796
  await shaking;
789
797
  }
790
798
  if (initial && !(method === 'init' && args.length > 0 && args[0].wasm !== undefined)) {
799
+ const sending = initial;
791
800
  initialising = initialising || post('init', [initial], [], origin).then(() => {
792
- initial = null;
801
+ if (initial === sending) {
802
+ initial = null;
803
+ }
793
804
  }, error => {
794
805
  initialising = null;
795
806
  throw error;
@@ -980,7 +991,21 @@ const makeWorker = (options, allowScript) => {
980
991
  } catch (error) {
981
992
  return Promise.reject(error);
982
993
  }
983
- return invoke('init', checked === undefined || Object.keys(checked).length === 0 ? [] : [checked], [], origin).then(() => undefined);
994
+ if (terminated) {
995
+ return Promise.reject(fail('worker-terminated', "Worker terminated."));
996
+ }
997
+ // The options are only recorded: `wasm` is sent as the worker's first call (see `initial`), and a worker that can't
998
+ // start or load fails the calls, which is where a caller handles it. So init() can't leave a rejection nobody
999
+ // awaits (it is often written as a setup line). The warm-up below is started and its failure dropped for the same reason.
1000
+ if (checked !== undefined && checked.wasm !== undefined) {
1001
+ initialWasm = checked.wasm;
1002
+ initial = {
1003
+ wasm: initialWasm
1004
+ };
1005
+ initialising = null;
1006
+ }
1007
+ invoke('init', [], [], origin).catch(() => {});
1008
+ return Promise.resolve();
984
1009
  };
985
1010
 
986
1011
  // Whether a value is an image where a palette size was expected (a pixel array, an ImageData or `{data, ...}`).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agglomerative-clustering",
3
- "version": "4.0.0-beta.4",
3
+ "version": "4.0.0",
4
4
  "description": "Colour palette extraction and image quantization with exact Ward clustering, in WebAssembly. Cluster an image once, then cut it to a palette of any size.",
5
5
  "license": "MIT",
6
6
  "author": "Rohan Fredriksson",