agglomerative-clustering 4.0.0-beta.3 → 4.0.0-beta.4
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 +1 -1
- package/dist/clustering.wasm +0 -0
- package/dist/index.cjs +26 -194
- package/dist/index.mjs +16 -184
- package/dist/shared.cjs +216 -8
- package/dist/shared.mjs +208 -6
- package/dist/worker-thread.mjs +1 -1
- package/dist/worker.cjs +129 -78
- package/dist/worker.mjs +130 -79
- package/package.json +1 -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 }).";
|
|
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.";
|
|
211
211
|
let worker,
|
|
212
212
|
source = null;
|
|
213
213
|
try {
|
|
@@ -264,11 +264,12 @@ 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
268
|
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)));
|
|
268
269
|
// Yarn Plug'n'Play keeps packages in zip archives, which Node reads ES modules from only through Yarn's loader.
|
|
269
270
|
const inZip = error => /\.zip[\\/]/.test(String(error && error.message));
|
|
270
271
|
worker.on('error', error => receive({
|
|
271
|
-
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 })
|
|
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
|
|
272
273
|
}));
|
|
273
274
|
worker.on('exit', code => receive({
|
|
274
275
|
failure: `The worker exited unexpectedly (code ${code}).`
|
|
@@ -327,6 +328,10 @@ const send = image => {
|
|
|
327
328
|
// An image object goes through the main entry's own checks here (its fields read once, then shape, data, detached buffer, format,
|
|
328
329
|
// sizes, unknown keys, premultiplied), so what can't be sent is reported as the main entry reports it, before anything is posted.
|
|
329
330
|
// A function, a Symbol or a thenable gets the shape error rather than the structured clone's.
|
|
331
|
+
// A bare array has to be what the main entry accepts as one (the checks it makes before its options).
|
|
332
|
+
if (ArrayBuffer.isView(image)) {
|
|
333
|
+
(0, _shared.checkPixelArray)(image);
|
|
334
|
+
}
|
|
330
335
|
if (!ArrayBuffer.isView(image) && !(0, _shared.isImageData)(image)) {
|
|
331
336
|
const copy = (0, _shared.imageCopy)(image);
|
|
332
337
|
if (copy === image) {
|
|
@@ -488,81 +493,68 @@ const initOptions = (options, allowScript) => {
|
|
|
488
493
|
return checked;
|
|
489
494
|
};
|
|
490
495
|
|
|
496
|
+
// What the options of a call are checked against: the image's data length, format and width (a bare array is RGBA).
|
|
497
|
+
const imageShape = sent => ArrayBuffer.isView(sent) ? {
|
|
498
|
+
data: sent,
|
|
499
|
+
format: 'rgba',
|
|
500
|
+
width: undefined
|
|
501
|
+
} : {
|
|
502
|
+
data: sent.data,
|
|
503
|
+
format: sent.format,
|
|
504
|
+
width: sent.width
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// Checks a call's options exactly as the main entry does (the same parseOptions, `allowed` being the keys the function takes,
|
|
508
|
+
// `image` the shape of the image it maps, if any), so a wrong option is reported here, in the main entry's order and words,
|
|
509
|
+
// before anything is posted or a buffer is transferred. What it returns is what is sent: the options as one plain copy, and
|
|
510
|
+
// `parsed`, what parseOptions made of them.
|
|
491
511
|
// A signal and a progress callback can't cross to the worker, so they stay on this side: the options are sent without
|
|
492
512
|
// them, a cancel message is sent when the signal aborts, and the worker's progress messages call the callback.
|
|
493
|
-
const takeControls = (options,
|
|
494
|
-
|
|
495
|
-
// once first: structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
496
|
-
if (!(0, _shared.isOptionsObject)(options)) {
|
|
513
|
+
const takeControls = (options, allowed, image) => {
|
|
514
|
+
if (options === undefined) {
|
|
497
515
|
return {
|
|
498
516
|
options,
|
|
499
517
|
signal: undefined,
|
|
500
|
-
onProgress: undefined
|
|
518
|
+
onProgress: undefined,
|
|
519
|
+
parsed: (0, _shared.parseOptions)(options, allowed, image)
|
|
501
520
|
};
|
|
502
521
|
}
|
|
522
|
+
// Anything but an ordinary object is rejected by parseOptions, as in the main entry. An ordinary one is copied once first:
|
|
523
|
+
// structured cloning drops inherited properties, and the copy is what gets validated and sent.
|
|
524
|
+
if (!(0, _shared.isOptionsObject)(options)) {
|
|
525
|
+
(0, _shared.parseOptions)(options, allowed, image);
|
|
526
|
+
}
|
|
503
527
|
options = (0, _shared.snapshot)(options);
|
|
504
528
|
// The dither setting is an object of its own, which structured cloning would copy without its inherited keys (or not
|
|
505
|
-
// at all, for a Proxy or a function): read it here the way the main entry does, and send
|
|
506
|
-
// A function that takes no dither option rejects the key whatever its value, as the main entry does: send a value that
|
|
507
|
-
// can cross, for the worker's validation to name the option.
|
|
508
|
-
if (!ditherAllowed && 'dither' in options) {
|
|
509
|
-
options.dither = null;
|
|
510
|
-
}
|
|
529
|
+
// at all, for a Proxy or a function): read it here the way the main entry does, once, and validate and send the copy.
|
|
511
530
|
if (options.dither !== null && (typeof options.dither === 'object' || typeof options.dither === 'function' || typeof options.dither === 'symbol')) {
|
|
512
|
-
|
|
513
|
-
const copy = (0, _shared.isOptionsObject)(options.dither) ? Object.assign({}, (0, _shared.snapshot)(options.dither)) : options.dither;
|
|
514
|
-
// A wrong setting is the worker's to report, after the image and k, as the main entry orders its checks. Only a
|
|
515
|
-
// copy that can't be sent is reported here.
|
|
516
|
-
try {
|
|
517
|
-
(0, _shared.parseDither)(copy);
|
|
518
|
-
} catch (error) {
|
|
519
|
-
if (!sendable(copy)) {
|
|
520
|
-
throw error;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
options.dither = copy;
|
|
531
|
+
options.dither = (0, _shared.isOptionsObject)(options.dither) ? Object.assign({}, (0, _shared.snapshot)(options.dither)) : options.dither;
|
|
524
532
|
}
|
|
525
|
-
|
|
526
|
-
//
|
|
527
|
-
//
|
|
528
|
-
// those messages not echoing the value: if they ever say what they got, send a stand-in that reads the same.)
|
|
533
|
+
const parsed = (0, _shared.parseOptions)(options, allowed, image);
|
|
534
|
+
// Every value left is a primitive, a copy of the dither object or the signal and callback taken out here, so what is
|
|
535
|
+
// posted can be cloned. (The `cannot be sent` error in `unsendable` stays as a safety net for anything that still isn't.)
|
|
529
536
|
const {
|
|
530
537
|
signal,
|
|
531
538
|
onProgress
|
|
532
539
|
} = options;
|
|
533
|
-
const badSignal = signal !== undefined && (signal === null || typeof signal !== 'object' || typeof signal.aborted !== 'boolean' || typeof signal.addEventListener !== 'function');
|
|
534
|
-
const badProgress = onProgress !== undefined && typeof onProgress !== 'function';
|
|
535
540
|
const {
|
|
536
541
|
signal: _signal,
|
|
537
542
|
onProgress: _onProgress,
|
|
538
543
|
...rest
|
|
539
544
|
} = options;
|
|
540
|
-
if (badSignal || badProgress) {
|
|
541
|
-
return {
|
|
542
|
-
options: {
|
|
543
|
-
...rest,
|
|
544
|
-
...(badSignal ? {
|
|
545
|
-
signal: null
|
|
546
|
-
} : {}),
|
|
547
|
-
...(badProgress ? {
|
|
548
|
-
onProgress: null
|
|
549
|
-
} : {})
|
|
550
|
-
},
|
|
551
|
-
signal: undefined,
|
|
552
|
-
onProgress: undefined
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
545
|
if (signal === undefined && onProgress === undefined) {
|
|
556
546
|
return {
|
|
557
547
|
options,
|
|
558
548
|
signal,
|
|
559
|
-
onProgress
|
|
549
|
+
onProgress,
|
|
550
|
+
parsed
|
|
560
551
|
};
|
|
561
552
|
}
|
|
562
553
|
return {
|
|
563
554
|
options: rest,
|
|
564
555
|
signal,
|
|
565
|
-
onProgress
|
|
556
|
+
onProgress,
|
|
557
|
+
parsed
|
|
566
558
|
};
|
|
567
559
|
};
|
|
568
560
|
|
|
@@ -620,15 +612,24 @@ const unsendable = (method, args, error) => {
|
|
|
620
612
|
const cause = {
|
|
621
613
|
cause: error
|
|
622
614
|
};
|
|
623
|
-
return index < 0 ? Object.assign((0, _shared.fail)('invalid-argument', `Invalid argument: it cannot be sent to the worker
|
|
615
|
+
return index < 0 ? Object.assign((0, _shared.fail)('invalid-argument', `Invalid argument: it cannot be sent to the worker.`), cause) : Object.assign((0, _shared.fail)('invalid-argument', `Invalid ${names[index]}: it cannot be sent to the worker.`, names[index]), cause);
|
|
624
616
|
};
|
|
625
617
|
|
|
626
618
|
// Like fetch: an aborted call rejects with the signal's reason, an AbortError unless the caller gave another.
|
|
627
619
|
const abortReason = signal => signal.reason !== undefined ? signal.reason : new DOMException('This operation was aborted', 'AbortError');
|
|
620
|
+
|
|
621
|
+
// The option keys each function takes (the main entry's lists).
|
|
622
|
+
const keysOf = {
|
|
623
|
+
buildClustering: _shared.clusterOptions,
|
|
624
|
+
buildPalette: _shared.clusterOptions,
|
|
625
|
+
buildSwatches: _shared.clusterOptions,
|
|
626
|
+
quantize: _shared.quantizeOptions,
|
|
627
|
+
quantizeToIndices: _shared.quantizeOptions,
|
|
628
|
+
quantizeWithPalette: _shared.mapOptions,
|
|
629
|
+
quantizeToIndicesWithPalette: _shared.mapIndexOptions
|
|
630
|
+
};
|
|
628
631
|
const imageFunctions = ['buildClustering', 'buildPalette', 'buildSwatches', 'quantize', 'quantizeWithPalette', 'quantizeToIndices', 'quantizeToIndicesWithPalette'];
|
|
629
632
|
const mappedImageFunctions = ['quantize', 'quantizeWithPalette'];
|
|
630
|
-
// The functions that only cluster, and so take no dither option.
|
|
631
|
-
const clusteringOnlyFunctions = ['buildClustering', 'buildPalette', 'buildSwatches'];
|
|
632
633
|
|
|
633
634
|
// Calls each worker's `abandon` once the worker made by createWorker() has been garbage-collected (see `abandoned`).
|
|
634
635
|
const abandonment = typeof FinalizationRegistry === 'function' ? new FinalizationRegistry(abandon => abandon()) : null;
|
|
@@ -977,11 +978,25 @@ const makeWorker = (options, allowScript) => {
|
|
|
977
978
|
// then the rest of the image's checks (its fields are read once here), before the options, as in the main entry
|
|
978
979
|
const checked = send(image);
|
|
979
980
|
const position = optionsPosition[name];
|
|
981
|
+
// then the argument before the options (k, or a palette), and the options, as the main entry orders them
|
|
982
|
+
const shape = imageShape(checked);
|
|
983
|
+
const byPalette = name.endsWith('WithPalette');
|
|
984
|
+
if (position === 1) {
|
|
985
|
+
if (byPalette) {
|
|
986
|
+
(0, _shared.checkPalette)(rest[0]);
|
|
987
|
+
} else {
|
|
988
|
+
(0, _shared.checkK)(rest[0]);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
980
991
|
const {
|
|
981
992
|
options,
|
|
982
993
|
signal,
|
|
983
|
-
onProgress
|
|
984
|
-
|
|
994
|
+
onProgress,
|
|
995
|
+
parsed
|
|
996
|
+
} = takeControls(rest[position], keysOf[name], shape);
|
|
997
|
+
if (byPalette) {
|
|
998
|
+
(0, _shared.checkNotEmpty)(rest[0], shape, name === 'quantizeToIndicesWithPalette' && parsed.transparent > 0 ? parsed.transparent : 1);
|
|
999
|
+
}
|
|
985
1000
|
let args = rest.slice();
|
|
986
1001
|
if (position < args.length) {
|
|
987
1002
|
args[position] = options;
|
|
@@ -1041,7 +1056,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1041
1056
|
const looksLikeImage = value => value !== null && typeof value === 'object' && ((0, _shared.isUint8Array)(value) || (0, _shared.isUint8ClampedArray)(value) || (0, _shared.isImageData)(value) || 'data' in value);
|
|
1042
1057
|
// Builds the handle object for what the worker keeps under `id`: `kind` is 'image' (from `cluster`: an image and its
|
|
1043
1058
|
// clustering) or 'clustering' (from `load`: a clustering alone). `output` turns a mapped result back into the type of the image given.
|
|
1044
|
-
const makeHandle = (id, kind, output) => {
|
|
1059
|
+
const makeHandle = (id, kind, output, shape) => {
|
|
1045
1060
|
let released = false;
|
|
1046
1061
|
// Every method goes through `use`, which refers to `handle`, so a method kept after the handle itself is dropped
|
|
1047
1062
|
// keeps it alive and it is not reclaimed while still in use.
|
|
@@ -1052,13 +1067,36 @@ const makeWorker = (options, allowScript) => {
|
|
|
1052
1067
|
return invoke(method, [id, ...args], [], new Error(), controls);
|
|
1053
1068
|
};
|
|
1054
1069
|
// Calls with mapping options, whose signal and progress callback stay on this side (see takeControls).
|
|
1055
|
-
|
|
1070
|
+
// `lead` is what comes first: k or a palette, checked before the options, as in the main entry. `keys` are the options the
|
|
1071
|
+
// method takes, and `shape` the image of this handle (see imageShape).
|
|
1072
|
+
const withControls = (method, leading, mapOptions, keys, lead) => {
|
|
1056
1073
|
let taken;
|
|
1057
1074
|
try {
|
|
1058
|
-
|
|
1075
|
+
if (lead === 'palette') {
|
|
1076
|
+
(0, _shared.checkPalette)(leading[0]);
|
|
1077
|
+
} else {
|
|
1078
|
+
(0, _shared.checkK)(leading[0]);
|
|
1079
|
+
}
|
|
1080
|
+
taken = takeControls(mapOptions, keys, shape);
|
|
1059
1081
|
} catch (error) {
|
|
1060
1082
|
return Promise.reject(error);
|
|
1061
1083
|
}
|
|
1084
|
+
// A palette with no colours is wrong only if the image has pixels to map to it, which the thread knows (this side keeps
|
|
1085
|
+
// the image's size only). So with a signal already aborted, the thread checks the call, as the functions do, and the abort
|
|
1086
|
+
// is reported when that finds nothing wrong.
|
|
1087
|
+
if (lead === 'palette' && leading[0].length === 0 && taken.signal && taken.signal.aborted) {
|
|
1088
|
+
const aborted = taken.signal;
|
|
1089
|
+
return use(method, [...leading, taken.options], {
|
|
1090
|
+
check: true
|
|
1091
|
+
}).then(() => {
|
|
1092
|
+
throw abortReason(aborted);
|
|
1093
|
+
}, error => {
|
|
1094
|
+
if ((0, _shared.isCodedError)(error) && error.argument !== undefined) {
|
|
1095
|
+
throw error;
|
|
1096
|
+
}
|
|
1097
|
+
throw abortReason(aborted);
|
|
1098
|
+
});
|
|
1099
|
+
}
|
|
1062
1100
|
return use(method, [...leading, taken.options], {
|
|
1063
1101
|
signal: taken.signal,
|
|
1064
1102
|
onProgress: taken.onProgress
|
|
@@ -1066,29 +1104,34 @@ const makeWorker = (options, allowScript) => {
|
|
|
1066
1104
|
};
|
|
1067
1105
|
// The cuts take no options (the handle's clustering already says how it was made): one passed anyway is an error,
|
|
1068
1106
|
// as in the main entry, rather than silently ignored.
|
|
1069
|
-
const plain = (method, name, args, extra) =>
|
|
1107
|
+
const plain = (method, name, args, extra) => {
|
|
1108
|
+
// k first, then the options that aren't taken, as the main entry's cuts check them
|
|
1109
|
+
try {
|
|
1110
|
+
if (args.length > 0) {
|
|
1111
|
+
(0, _shared.checkK)(args[0]);
|
|
1112
|
+
}
|
|
1113
|
+
} catch (error) {
|
|
1114
|
+
return Promise.reject(error);
|
|
1115
|
+
}
|
|
1116
|
+
return extra.some(value => value !== undefined) ? Promise.reject((0, _shared.fail)('invalid-argument', `Invalid arguments: handle.${name} takes no options. To map pixels with options, use handle.quantize.`, 'options')) : use(method, args);
|
|
1117
|
+
};
|
|
1070
1118
|
// An image where k belongs is a mix-up between the two kinds of handle: say so, rather than "k must be an integer".
|
|
1071
1119
|
const imageAsK = (name, k) => looksLikeImage(k) ? Promise.reject((0, _shared.fail)('invalid-argument', `Invalid k: this handle holds an image (from worker.cluster) and maps it with ${name}(k); pass k, not an image. To map other images with a stored clustering, use worker.load(clustering).`, 'k')) : null;
|
|
1072
1120
|
const handle = {
|
|
1073
1121
|
getPalette: (k, ...extra) => plain('handleGetPalette', 'getPalette', [k], extra),
|
|
1074
1122
|
getSwatches: (k, ...extra) => plain('handleGetSwatches', 'getSwatches', [k], extra),
|
|
1075
1123
|
getClusteringInfo: (...extra) => plain('handleGetClusteringInfo', 'getClusteringInfo', [], extra),
|
|
1076
|
-
// Takes the signal only: the main entry's chooseFrom takes no progress callback, so one given
|
|
1077
|
-
// the
|
|
1078
|
-
// That relies on the thread injecting a progress poster only for calls that have one (see `optionsIndex` in
|
|
1079
|
-
// worker-thread.js): the null is then the only onProgress it sees, so its own option check rejects it.
|
|
1124
|
+
// Takes the signal only: the main entry's chooseFrom takes no progress callback, so one given is rejected here, by
|
|
1125
|
+
// the same check.
|
|
1080
1126
|
chooseFrom: (palette, k, chooseOptions) => {
|
|
1081
1127
|
let taken;
|
|
1082
1128
|
try {
|
|
1083
|
-
|
|
1129
|
+
(0, _shared.checkChoice)(palette, k);
|
|
1130
|
+
taken = takeControls(chooseOptions, ['colorSpace', 'signal']);
|
|
1084
1131
|
} catch (error) {
|
|
1085
1132
|
return Promise.reject(error);
|
|
1086
1133
|
}
|
|
1087
|
-
|
|
1088
|
-
...taken.options,
|
|
1089
|
-
onProgress: null
|
|
1090
|
-
} : taken.options;
|
|
1091
|
-
return use('handleChooseFrom', [palette, k, sent], {
|
|
1134
|
+
return use('handleChooseFrom', [palette, k, taken.options], {
|
|
1092
1135
|
signal: taken.signal
|
|
1093
1136
|
});
|
|
1094
1137
|
},
|
|
@@ -1116,15 +1159,15 @@ const makeWorker = (options, allowScript) => {
|
|
|
1116
1159
|
if (kind === 'image') {
|
|
1117
1160
|
Object.assign(handle, {
|
|
1118
1161
|
getClustering: (...extra) => plain('handleGetClustering', 'getClustering', [], extra),
|
|
1119
|
-
quantize: (k, mapOptions) => imageAsK('quantize', k) || (async () => output(await withControls('handleQuantize', [k], mapOptions)))(),
|
|
1120
|
-
quantizeToIndices: (k, mapOptions) => imageAsK('quantizeToIndices', k) || withControls('handleQuantizeToIndices', [k], mapOptions),
|
|
1121
|
-
quantizeWithPalette: async (palette, mapOptions) => output(await withControls('handleQuantizeWithPalette', [palette], mapOptions)),
|
|
1122
|
-
quantizeToIndicesWithPalette: (palette, mapOptions) => withControls('handleQuantizeToIndicesWithPalette', [palette], mapOptions)
|
|
1162
|
+
quantize: (k, mapOptions) => imageAsK('quantize', k) || (async () => output(await withControls('handleQuantize', [k], mapOptions, _shared.mapOptions, 'k')))(),
|
|
1163
|
+
quantizeToIndices: (k, mapOptions) => imageAsK('quantizeToIndices', k) || withControls('handleQuantizeToIndices', [k], mapOptions, _shared.mapIndexOptions, 'k'),
|
|
1164
|
+
quantizeWithPalette: async (palette, mapOptions) => output(await withControls('handleQuantizeWithPalette', [palette], mapOptions, _shared.mapOptions, 'palette')),
|
|
1165
|
+
quantizeToIndicesWithPalette: (palette, mapOptions) => withControls('handleQuantizeToIndicesWithPalette', [palette], mapOptions, _shared.mapIndexOptions, 'palette')
|
|
1123
1166
|
});
|
|
1124
1167
|
}
|
|
1125
1168
|
if (kind === 'clustering') {
|
|
1126
1169
|
// The handle holds no image: each call maps the image it is given, and returns the type of that image.
|
|
1127
|
-
const mapping = (name, method, mapped) => (image, k, mapOptions) => {
|
|
1170
|
+
const mapping = (name, method, mapped, keys) => (image, k, mapOptions) => {
|
|
1128
1171
|
if (typeof image === 'number') {
|
|
1129
1172
|
return Promise.reject((0, _shared.fail)('invalid-argument', "Invalid image: this handle holds a clustering, not an image (from worker.load); pass the image, then k.", 'image'));
|
|
1130
1173
|
}
|
|
@@ -1138,11 +1181,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
1138
1181
|
throw bare;
|
|
1139
1182
|
}
|
|
1140
1183
|
const checked = send(image);
|
|
1184
|
+
(0, _shared.checkK)(k);
|
|
1141
1185
|
const {
|
|
1142
1186
|
options,
|
|
1143
1187
|
signal,
|
|
1144
1188
|
onProgress
|
|
1145
|
-
} = takeControls(mapOptions);
|
|
1189
|
+
} = takeControls(mapOptions, keys, imageShape(checked));
|
|
1146
1190
|
if (signal && signal.aborted) {
|
|
1147
1191
|
// As the functions on the worker do: a wrong argument is reported before the abort.
|
|
1148
1192
|
try {
|
|
@@ -1169,8 +1213,8 @@ const makeWorker = (options, allowScript) => {
|
|
|
1169
1213
|
})();
|
|
1170
1214
|
};
|
|
1171
1215
|
Object.assign(handle, {
|
|
1172
|
-
quantize: mapping('quantize', 'handleQuantizeImage', true),
|
|
1173
|
-
quantizeToIndices: mapping('quantizeToIndices', 'handleQuantizeToIndicesImage', false)
|
|
1216
|
+
quantize: mapping('quantize', 'handleQuantizeImage', true, _shared.mapOptions),
|
|
1217
|
+
quantizeToIndices: mapping('quantizeToIndices', 'handleQuantizeToIndicesImage', false, _shared.mapIndexOptions)
|
|
1174
1218
|
});
|
|
1175
1219
|
}
|
|
1176
1220
|
// `await using handle = await worker.cluster(image)` releases it at the end of the block.
|
|
@@ -1184,7 +1228,7 @@ const makeWorker = (options, allowScript) => {
|
|
|
1184
1228
|
};
|
|
1185
1229
|
|
|
1186
1230
|
// Clusters `image` and keeps it, with its clustering, in the worker.
|
|
1187
|
-
instance.cluster = (image,
|
|
1231
|
+
instance.cluster = (image, givenOptions) => {
|
|
1188
1232
|
const origin = new Error();
|
|
1189
1233
|
return (async () => {
|
|
1190
1234
|
// The image is checked before the options and the abort, as the main entry does.
|
|
@@ -1193,11 +1237,12 @@ const makeWorker = (options, allowScript) => {
|
|
|
1193
1237
|
throw bare;
|
|
1194
1238
|
}
|
|
1195
1239
|
const checked = send(image);
|
|
1240
|
+
const shape = imageShape(checked);
|
|
1196
1241
|
const {
|
|
1197
1242
|
options,
|
|
1198
1243
|
signal,
|
|
1199
1244
|
onProgress
|
|
1200
|
-
} = takeControls(clusterOptions,
|
|
1245
|
+
} = takeControls(givenOptions, _shared.clusterOptions, shape);
|
|
1201
1246
|
if (signal && signal.aborted) {
|
|
1202
1247
|
throw abortReason(signal);
|
|
1203
1248
|
}
|
|
@@ -1221,7 +1266,13 @@ const makeWorker = (options, allowScript) => {
|
|
|
1221
1266
|
}
|
|
1222
1267
|
throw error;
|
|
1223
1268
|
}
|
|
1224
|
-
|
|
1269
|
+
// The handle keeps the image's size, not its bytes (a transferred buffer is empty by now).
|
|
1270
|
+
return makeHandle(id, 'image', rebuild(image), {
|
|
1271
|
+
...shape,
|
|
1272
|
+
data: {
|
|
1273
|
+
length: shape.data.length
|
|
1274
|
+
}
|
|
1275
|
+
});
|
|
1225
1276
|
})();
|
|
1226
1277
|
};
|
|
1227
1278
|
|