agglomerative-clustering 3.7.0 → 3.7.1

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.
Binary file
package/dist/index.cjs CHANGED
@@ -190,7 +190,13 @@ const isBrowserLike = () => typeof document !== 'undefined' || typeof WorkerGlob
190
190
  const deployAdvice = "In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the bundler or file tracer may have left the package's dist/ files out: include node_modules/agglomerative-clustering/dist in the function's files, or leave the package unbundled (external).";
191
191
  // Emscripten appends advice about its own debug build, which doesn't apply to this package's users.
192
192
  const assertionsAdvice = /\s*\(?Build with -sASSERTIONS for more info\.?\)?/;
193
- const wasmLoadFailed = (cause, given) => {
193
+ // Whether init({ wasm }) was given bytes that start with gzip's magic (1f 8b): a gzipped module passed without decompressing.
194
+ const isGzipped = wasm => {
195
+ const tag = Object.prototype.toString.call(wasm);
196
+ const bytes = tag === '[object ArrayBuffer]' ? new Uint8Array(wasm) : ArrayBuffer.isView(wasm) ? new Uint8Array(wasm.buffer, wasm.byteOffset, wasm.byteLength) : null;
197
+ return bytes !== null && bytes.length >= 2 && bytes[0] === 0x1f && bytes[1] === 0x8b;
198
+ };
199
+ const wasmLoadFailed = (cause, given, gzipped = false) => {
194
200
  const text = String(cause && cause.message !== undefined ? cause.message : cause).split('\n')[0];
195
201
  // Emscripten wraps a failed compile in Aborted(...), which is how it stops itself, not part of the reason.
196
202
  let reason = text.replace(assertionsAdvice, '').trim().replace(/\.+$/, '');
@@ -221,6 +227,10 @@ const wasmLoadFailed = (cause, given) => {
221
227
  advice = "A Content-Security-Policy blocks compiling WebAssembly: add 'wasm-unsafe-eval' to the script-src of the policy that applies. That is the page's for a page and for the worker entry's worker (also when started from a content script, whose worker follows the page's policy); for a browser extension's own pages, offscreen document and service worker, it is content_security_policy.extension_pages in the manifest.";
222
228
  } else if (!hasSimd()) {
223
229
  advice = "This engine has no WebAssembly SIMD, which the module needs (Chrome and Edge 91, Firefox 89, Safari 16.4, Node 16.4 or newer).";
230
+ } else if (gzipped) {
231
+ // Engines word this differently (V8 and Firefox name the magic number, Safari doesn't), so it goes by the bytes.
232
+ reason = `the bytes given to init({ wasm }) are gzipped (they start with 1f 8b), not a WebAssembly module (${reason})`;
233
+ advice = "Decompress them first, for example with new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'))).arrayBuffer(), and pass the result (see Plugins and single-file hosts in https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/environments.md).";
224
234
  } else if (/magic (word|number)|BufferSource argument is empty|wasm validation error: at offset [0-4]:/i.test(text)) {
225
235
  // What a 404 page, or a server's fallback HTML page, looks like once it has been compiled as WebAssembly.
226
236
  if (given === true) {
@@ -398,13 +408,16 @@ const acquire = async options => {
398
408
  // way Node resolves packages, from the working directory; if that fails too, the first error is reported.
399
409
  const fallback = wasm === undefined && isNodeLike ? await packagedWasm().catch(() => null) : null;
400
410
  if (!fallback) {
401
- throw wasmLoadFailed(error, typeof wasm === 'string' ? wasm : isURL(wasm) ? wasm.href : wasm !== undefined);
411
+ throw wasmLoadFailed(error, typeof wasm === 'string' ? wasm : isURL(wasm) ? wasm.href : wasm !== undefined, isGzipped(wasm));
402
412
  }
403
413
  instance = await instantiate(fallback).catch(() => {
404
414
  throw wasmLoadFailed(error, false);
405
415
  });
406
416
  }
407
417
  checkWasmVersion(instance);
418
+ if (instance.HEAPU8) {
419
+ startSizes.set(instance, instance.HEAPU8.length);
420
+ }
408
421
  Module = instance;
409
422
  return instance;
410
423
  })().catch(error => {
@@ -436,10 +449,14 @@ const discard = instance => {
436
449
  // WebAssembly memory only grows: an instance that once held a huge image keeps all that memory for the life of the process.
437
450
  // So when a call leaves the heap larger than this, the instance is retired like an aborted one: calls already running keep
438
451
  // it, the next call loads a fresh one (a few milliseconds, next to the seconds such an image took), and the old memory is
439
- // freed when nothing uses it any more.
440
- const retireAbove = 256 * 1024 * 1024;
452
+ // freed when nothing uses it any more. The limit counts growth from the size the instance started at (32 MB in the
453
+ // release build), so a build that starts larger, such as the sanitized one scripts/asan-test.sh uses (512 MB, for its
454
+ // shadow memory), isn't retired after every call.
455
+ const releaseStart = 32 * 1024 * 1024; // INITIAL_MEMORY in build:wasm
456
+ const retireAfter = 256 * 1024 * 1024 - releaseStart; // so the release build retires above 256 MB, as before
457
+ const startSizes = new WeakMap();
441
458
  const retireIfLarge = instance => {
442
- if (instance.HEAPU8 && instance.HEAPU8.length > retireAbove) {
459
+ if (instance.HEAPU8 && instance.HEAPU8.length > (startSizes.get(instance) ?? releaseStart) + retireAfter) {
443
460
  discard(instance);
444
461
  }
445
462
  };
package/dist/index.mjs CHANGED
@@ -115,7 +115,13 @@ const isBrowserLike = () => typeof document !== 'undefined' || typeof WorkerGlob
115
115
  const deployAdvice = "In a serverless deployment (AWS Lambda, Vercel or Netlify functions), the bundler or file tracer may have left the package's dist/ files out: include node_modules/agglomerative-clustering/dist in the function's files, or leave the package unbundled (external).";
116
116
  // Emscripten appends advice about its own debug build, which doesn't apply to this package's users.
117
117
  const assertionsAdvice = /\s*\(?Build with -sASSERTIONS for more info\.?\)?/;
118
- const wasmLoadFailed = (cause, given) => {
118
+ // Whether init({ wasm }) was given bytes that start with gzip's magic (1f 8b): a gzipped module passed without decompressing.
119
+ const isGzipped = wasm => {
120
+ const tag = Object.prototype.toString.call(wasm);
121
+ const bytes = tag === '[object ArrayBuffer]' ? new Uint8Array(wasm) : ArrayBuffer.isView(wasm) ? new Uint8Array(wasm.buffer, wasm.byteOffset, wasm.byteLength) : null;
122
+ return bytes !== null && bytes.length >= 2 && bytes[0] === 0x1f && bytes[1] === 0x8b;
123
+ };
124
+ const wasmLoadFailed = (cause, given, gzipped = false) => {
119
125
  const text = String(cause && cause.message !== undefined ? cause.message : cause).split('\n')[0];
120
126
  // Emscripten wraps a failed compile in Aborted(...), which is how it stops itself, not part of the reason.
121
127
  let reason = text.replace(assertionsAdvice, '').trim().replace(/\.+$/, '');
@@ -146,6 +152,10 @@ const wasmLoadFailed = (cause, given) => {
146
152
  advice = "A Content-Security-Policy blocks compiling WebAssembly: add 'wasm-unsafe-eval' to the script-src of the policy that applies. That is the page's for a page and for the worker entry's worker (also when started from a content script, whose worker follows the page's policy); for a browser extension's own pages, offscreen document and service worker, it is content_security_policy.extension_pages in the manifest.";
147
153
  } else if (!hasSimd()) {
148
154
  advice = "This engine has no WebAssembly SIMD, which the module needs (Chrome and Edge 91, Firefox 89, Safari 16.4, Node 16.4 or newer).";
155
+ } else if (gzipped) {
156
+ // Engines word this differently (V8 and Firefox name the magic number, Safari doesn't), so it goes by the bytes.
157
+ reason = `the bytes given to init({ wasm }) are gzipped (they start with 1f 8b), not a WebAssembly module (${reason})`;
158
+ advice = "Decompress them first, for example with new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream('gzip'))).arrayBuffer(), and pass the result (see Plugins and single-file hosts in https://github.com/RohanFredriksson/agglomerative-clustering/blob/master/docs/environments.md).";
149
159
  } else if (/magic (word|number)|BufferSource argument is empty|wasm validation error: at offset [0-4]:/i.test(text)) {
150
160
  // What a 404 page, or a server's fallback HTML page, looks like once it has been compiled as WebAssembly.
151
161
  if (given === true) {
@@ -323,13 +333,16 @@ const acquire = async options => {
323
333
  // way Node resolves packages, from the working directory; if that fails too, the first error is reported.
324
334
  const fallback = wasm === undefined && isNodeLike ? await packagedWasm().catch(() => null) : null;
325
335
  if (!fallback) {
326
- throw wasmLoadFailed(error, typeof wasm === 'string' ? wasm : isURL(wasm) ? wasm.href : wasm !== undefined);
336
+ throw wasmLoadFailed(error, typeof wasm === 'string' ? wasm : isURL(wasm) ? wasm.href : wasm !== undefined, isGzipped(wasm));
327
337
  }
328
338
  instance = await instantiate(fallback).catch(() => {
329
339
  throw wasmLoadFailed(error, false);
330
340
  });
331
341
  }
332
342
  checkWasmVersion(instance);
343
+ if (instance.HEAPU8) {
344
+ startSizes.set(instance, instance.HEAPU8.length);
345
+ }
333
346
  Module = instance;
334
347
  return instance;
335
348
  })().catch(error => {
@@ -360,10 +373,14 @@ const discard = instance => {
360
373
  // WebAssembly memory only grows: an instance that once held a huge image keeps all that memory for the life of the process.
361
374
  // So when a call leaves the heap larger than this, the instance is retired like an aborted one: calls already running keep
362
375
  // it, the next call loads a fresh one (a few milliseconds, next to the seconds such an image took), and the old memory is
363
- // freed when nothing uses it any more.
364
- const retireAbove = 256 * 1024 * 1024;
376
+ // freed when nothing uses it any more. The limit counts growth from the size the instance started at (32 MB in the
377
+ // release build), so a build that starts larger, such as the sanitized one scripts/asan-test.sh uses (512 MB, for its
378
+ // shadow memory), isn't retired after every call.
379
+ const releaseStart = 32 * 1024 * 1024; // INITIAL_MEMORY in build:wasm
380
+ const retireAfter = 256 * 1024 * 1024 - releaseStart; // so the release build retires above 256 MB, as before
381
+ const startSizes = new WeakMap();
365
382
  const retireIfLarge = instance => {
366
- if (instance.HEAPU8 && instance.HEAPU8.length > retireAbove) {
383
+ if (instance.HEAPU8 && instance.HEAPU8.length > (startSizes.get(instance) ?? releaseStart) + retireAfter) {
367
384
  discard(instance);
368
385
  }
369
386
  };
package/dist/shared.cjs CHANGED
@@ -13,7 +13,7 @@ exports.warnOnce = exports.version = exports.tooLarge = exports.sniffImageFormat
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 = '3.7.0';
16
+ const version = exports.version = '3.7.1';
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 = '3.7.0';
7
+ export const version = '3.7.1';
8
8
  export const colorSpaceCodes = {
9
9
  'rgb': 0,
10
10
  'oklab': 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agglomerative-clustering",
3
- "version": "3.7.0",
3
+ "version": "3.7.1",
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",