@tradalab/mediakit 2.9.1 → 2.10.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 +29 -0
- package/dist/detect-worker.js +1138 -12
- package/dist/detect-worker.js.map +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/index.js +186 -133
- package/dist/index.js.map +1 -1
- package/dist/worker.js +1193 -60
- package/dist/worker.js.map +1 -1
- package/package.json +4 -1
package/dist/worker.js
CHANGED
|
@@ -4677,21 +4677,1144 @@
|
|
|
4677
4677
|
|
|
4678
4678
|
// src/assets.ts
|
|
4679
4679
|
var ASSET_BASE = "/mediakit/";
|
|
4680
|
+
var ASSETS = [
|
|
4681
|
+
{ path: "mediakit.wasm", part: "core", source: { from: "dist", file: "mediakit.wasm" } },
|
|
4682
|
+
{ path: "wasm_exec.js", part: "core", source: { from: "dist", file: "wasm_exec.js" } },
|
|
4683
|
+
{ path: "worker.js", part: "core", source: { from: "dist", file: "worker.js" } },
|
|
4684
|
+
{ path: "worker.js.map", part: "core", source: { from: "dist", file: "worker.js.map" }, debug: true },
|
|
4685
|
+
{ path: "detect-worker.js", part: "core", source: { from: "dist", file: "detect-worker.js" } },
|
|
4686
|
+
{ path: "detect-worker.js.map", part: "core", source: { from: "dist", file: "detect-worker.js.map" }, debug: true },
|
|
4687
|
+
{ path: "whiten_pink.png", part: "core", source: { from: "dist", file: "whiten_pink.png" } },
|
|
4688
|
+
{ path: "NOTICE", part: "core", source: { from: "dist", file: "NOTICE" } },
|
|
4689
|
+
{ path: "mp/", part: "core", source: { from: "peer-dir", name: "@mediapipe/tasks-vision", dir: "wasm", skip: "_module_" } },
|
|
4690
|
+
{
|
|
4691
|
+
path: "selfie_segmenter.tflite",
|
|
4692
|
+
part: "person",
|
|
4693
|
+
source: { from: "model", url: "https://storage.googleapis.com/mediapipe-models/image_segmenter/selfie_segmenter/float16/latest/selfie_segmenter.tflite" }
|
|
4694
|
+
},
|
|
4695
|
+
{
|
|
4696
|
+
path: "selfie_multiclass_256x256.tflite",
|
|
4697
|
+
part: "classes",
|
|
4698
|
+
source: {
|
|
4699
|
+
from: "model",
|
|
4700
|
+
url: "https://storage.googleapis.com/mediapipe-models/image_segmenter/selfie_multiclass_256x256/float32/latest/selfie_multiclass_256x256.tflite"
|
|
4701
|
+
}
|
|
4702
|
+
},
|
|
4703
|
+
{
|
|
4704
|
+
path: "face_landmarker.task",
|
|
4705
|
+
part: "face",
|
|
4706
|
+
source: { from: "model", url: "https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/latest/face_landmarker.task" }
|
|
4707
|
+
},
|
|
4708
|
+
{ path: "face_cover.onnx", part: "cover", source: { from: "dist", file: "face_cover.onnx" } },
|
|
4709
|
+
{ path: "ort/ort-wasm-simd-threaded.wasm", part: "cover", source: { from: "peer", name: "onnxruntime-web", file: "dist/ort-wasm-simd-threaded.wasm" } },
|
|
4710
|
+
{ path: "ort/ort-wasm-simd-threaded.mjs", part: "cover", source: { from: "peer", name: "onnxruntime-web", file: "dist/ort-wasm-simd-threaded.mjs" } }
|
|
4711
|
+
];
|
|
4680
4712
|
function assetUrl(path) {
|
|
4681
4713
|
return ASSET_BASE + path;
|
|
4682
4714
|
}
|
|
4683
4715
|
|
|
4716
|
+
// node_modules/.pnpm/@tradalab+tskit@0.3.0/node_modules/@tradalab/tskit/dist/chunk-PFK4SOUW.js
|
|
4717
|
+
var FETCH_SHARE = 0.9;
|
|
4718
|
+
var DEFAULT_INTERVAL_MS = 100;
|
|
4719
|
+
var EMPTY_PROGRESS = Object.freeze({ loaded: 0, total: 0, done: 0, failed: 0, building: 0, items: 0, ratio: 1 });
|
|
4720
|
+
var ProgressTracker = class {
|
|
4721
|
+
entries = /* @__PURE__ */ new Map();
|
|
4722
|
+
listeners = /* @__PURE__ */ new Set();
|
|
4723
|
+
intervalMs;
|
|
4724
|
+
now;
|
|
4725
|
+
told = Number.NEGATIVE_INFINITY;
|
|
4726
|
+
constructor(opts = {}) {
|
|
4727
|
+
this.intervalMs = opts.intervalMs ?? DEFAULT_INTERVAL_MS;
|
|
4728
|
+
this.now = opts.now ?? (() => performance.now());
|
|
4729
|
+
}
|
|
4730
|
+
item(id, size) {
|
|
4731
|
+
const e2 = { phase: "waiting", loaded: 0, size, error: "" };
|
|
4732
|
+
this.entries.set(id, e2);
|
|
4733
|
+
this.tell(true);
|
|
4734
|
+
const settled = () => e2.phase === "ready" || e2.phase === "failed";
|
|
4735
|
+
return {
|
|
4736
|
+
size: (bytes) => {
|
|
4737
|
+
if (settled()) return;
|
|
4738
|
+
e2.size = bytes !== null && bytes >= 0 ? bytes : null;
|
|
4739
|
+
this.tell(false);
|
|
4740
|
+
},
|
|
4741
|
+
bytes: (n2) => {
|
|
4742
|
+
if (settled()) return;
|
|
4743
|
+
e2.phase = "fetching";
|
|
4744
|
+
e2.loaded += n2;
|
|
4745
|
+
this.tell(false);
|
|
4746
|
+
},
|
|
4747
|
+
rollback: () => {
|
|
4748
|
+
if (settled()) return;
|
|
4749
|
+
e2.loaded = 0;
|
|
4750
|
+
this.tell(true);
|
|
4751
|
+
},
|
|
4752
|
+
building: () => {
|
|
4753
|
+
if (settled()) return;
|
|
4754
|
+
e2.phase = "building";
|
|
4755
|
+
this.tell(true);
|
|
4756
|
+
},
|
|
4757
|
+
ready: () => {
|
|
4758
|
+
if (settled()) return;
|
|
4759
|
+
e2.phase = "ready";
|
|
4760
|
+
this.tell(true);
|
|
4761
|
+
},
|
|
4762
|
+
fail: (reason) => {
|
|
4763
|
+
if (settled()) return;
|
|
4764
|
+
e2.phase = "failed";
|
|
4765
|
+
e2.error = reason;
|
|
4766
|
+
e2.loaded = 0;
|
|
4767
|
+
e2.size = 0;
|
|
4768
|
+
this.tell(true);
|
|
4769
|
+
}
|
|
4770
|
+
};
|
|
4771
|
+
}
|
|
4772
|
+
get(id) {
|
|
4773
|
+
return this.entries.get(id);
|
|
4774
|
+
}
|
|
4775
|
+
snapshot() {
|
|
4776
|
+
const all = [...this.entries.values()];
|
|
4777
|
+
if (all.length === 0) return { ...EMPTY_PROGRESS };
|
|
4778
|
+
const known = all.every((e2) => typeof e2.size === "number");
|
|
4779
|
+
const total = known ? all.reduce((a2, e2) => a2 + e2.size, 0) : 0;
|
|
4780
|
+
const raw = all.reduce((a2, e2) => a2 + e2.loaded, 0);
|
|
4781
|
+
let share = 0;
|
|
4782
|
+
for (const e2 of all) share += itemRatio(e2);
|
|
4783
|
+
return {
|
|
4784
|
+
loaded: total ? Math.min(raw, total) : raw,
|
|
4785
|
+
total,
|
|
4786
|
+
done: all.filter((e2) => e2.phase === "ready" || e2.phase === "failed").length,
|
|
4787
|
+
failed: all.filter((e2) => e2.phase === "failed").length,
|
|
4788
|
+
building: all.filter((e2) => e2.phase === "building").length,
|
|
4789
|
+
items: all.length,
|
|
4790
|
+
ratio: share / all.length
|
|
4791
|
+
};
|
|
4792
|
+
}
|
|
4793
|
+
subscribe(listener) {
|
|
4794
|
+
this.listeners.add(listener);
|
|
4795
|
+
return () => this.listeners.delete(listener);
|
|
4796
|
+
}
|
|
4797
|
+
/** Tells every listener the current progress now, whatever the interval. */
|
|
4798
|
+
flush() {
|
|
4799
|
+
this.tell(true);
|
|
4800
|
+
}
|
|
4801
|
+
tell(force) {
|
|
4802
|
+
if (this.listeners.size === 0) return;
|
|
4803
|
+
const t2 = this.now();
|
|
4804
|
+
if (!force && t2 - this.told < this.intervalMs) return;
|
|
4805
|
+
this.told = t2;
|
|
4806
|
+
const p2 = this.snapshot();
|
|
4807
|
+
for (const l2 of [...this.listeners]) {
|
|
4808
|
+
try {
|
|
4809
|
+
l2(p2);
|
|
4810
|
+
} catch (err) {
|
|
4811
|
+
this.listeners.delete(l2);
|
|
4812
|
+
console.error("tskit: a progress listener threw and was removed", err);
|
|
4813
|
+
}
|
|
4814
|
+
}
|
|
4815
|
+
}
|
|
4816
|
+
};
|
|
4817
|
+
function itemRatio(e2) {
|
|
4818
|
+
if (e2.phase === "ready" || e2.phase === "failed") return 1;
|
|
4819
|
+
if (e2.phase === "building") return FETCH_SHARE;
|
|
4820
|
+
if (typeof e2.size === "number" && e2.size > 0) return FETCH_SHARE * Math.min(1, e2.loaded / e2.size);
|
|
4821
|
+
return 0;
|
|
4822
|
+
}
|
|
4823
|
+
|
|
4824
|
+
// node_modules/.pnpm/@tradalab+tskit@0.3.0/node_modules/@tradalab/tskit/dist/chunk-QNVFDCFJ.js
|
|
4825
|
+
var Retainer = class {
|
|
4826
|
+
constructor(opts) {
|
|
4827
|
+
this.opts = opts;
|
|
4828
|
+
this.keepMs = opts.keepMs ?? 0;
|
|
4829
|
+
this.setTimer = opts.setTimer ?? unrefTimer;
|
|
4830
|
+
this.clearTimer = opts.clearTimer ?? ((id) => clearTimeout(id));
|
|
4831
|
+
}
|
|
4832
|
+
opts;
|
|
4833
|
+
entries = /* @__PURE__ */ new Map();
|
|
4834
|
+
keepMs;
|
|
4835
|
+
setTimer;
|
|
4836
|
+
clearTimer;
|
|
4837
|
+
async acquire(key, signal) {
|
|
4838
|
+
signal?.throwIfAborted();
|
|
4839
|
+
let e2 = this.entries.get(key);
|
|
4840
|
+
if (!e2) {
|
|
4841
|
+
const abort2 = new AbortController();
|
|
4842
|
+
const entry2 = { refs: 0, abort: abort2, settled: false, hasValue: false, promise: void 0 };
|
|
4843
|
+
entry2.promise = this.opts.create(key, abort2.signal).then(
|
|
4844
|
+
(v2) => {
|
|
4845
|
+
entry2.settled = true;
|
|
4846
|
+
entry2.value = v2;
|
|
4847
|
+
entry2.hasValue = true;
|
|
4848
|
+
if (this.entries.get(key) !== entry2) void this.dispose(key, entry2);
|
|
4849
|
+
return v2;
|
|
4850
|
+
},
|
|
4851
|
+
(err) => {
|
|
4852
|
+
entry2.settled = true;
|
|
4853
|
+
if (this.entries.get(key) === entry2) this.entries.delete(key);
|
|
4854
|
+
throw err;
|
|
4855
|
+
}
|
|
4856
|
+
);
|
|
4857
|
+
entry2.promise.catch(() => {
|
|
4858
|
+
});
|
|
4859
|
+
this.entries.set(key, entry2);
|
|
4860
|
+
e2 = entry2;
|
|
4861
|
+
}
|
|
4862
|
+
if (e2.timer !== void 0) {
|
|
4863
|
+
this.clearTimer(e2.timer);
|
|
4864
|
+
e2.timer = void 0;
|
|
4865
|
+
}
|
|
4866
|
+
e2.refs++;
|
|
4867
|
+
const entry = e2;
|
|
4868
|
+
let value;
|
|
4869
|
+
try {
|
|
4870
|
+
value = await waitFor(entry.promise, signal, entry.abort.signal);
|
|
4871
|
+
} catch (err) {
|
|
4872
|
+
this.release(key, entry);
|
|
4873
|
+
throw err;
|
|
4874
|
+
}
|
|
4875
|
+
if (this.entries.get(key) !== entry) {
|
|
4876
|
+
throw new DOMException(`${key} was cleared while it was being created`, "AbortError");
|
|
4877
|
+
}
|
|
4878
|
+
let released = false;
|
|
4879
|
+
return {
|
|
4880
|
+
value,
|
|
4881
|
+
release: () => {
|
|
4882
|
+
if (released) return;
|
|
4883
|
+
released = true;
|
|
4884
|
+
this.release(key, entry);
|
|
4885
|
+
}
|
|
4886
|
+
};
|
|
4887
|
+
}
|
|
4888
|
+
state(key) {
|
|
4889
|
+
const e2 = this.entries.get(key);
|
|
4890
|
+
if (!e2) return void 0;
|
|
4891
|
+
if (!e2.settled) return "creating";
|
|
4892
|
+
return e2.refs > 0 ? "live" : "kept";
|
|
4893
|
+
}
|
|
4894
|
+
refs(key) {
|
|
4895
|
+
return this.entries.get(key)?.refs ?? 0;
|
|
4896
|
+
}
|
|
4897
|
+
keys() {
|
|
4898
|
+
return [...this.entries.keys()];
|
|
4899
|
+
}
|
|
4900
|
+
async clear() {
|
|
4901
|
+
const all = [...this.entries.entries()];
|
|
4902
|
+
this.entries.clear();
|
|
4903
|
+
await Promise.all(all.map(([key, e2]) => this.drop(key, e2)));
|
|
4904
|
+
}
|
|
4905
|
+
release(key, e2) {
|
|
4906
|
+
e2.refs--;
|
|
4907
|
+
if (e2.refs > 0 || this.entries.get(key) !== e2) return;
|
|
4908
|
+
if (!e2.settled) {
|
|
4909
|
+
this.entries.delete(key);
|
|
4910
|
+
e2.abort.abort(new DOMException(`every caller of ${key} gave up before it was ready`, "AbortError"));
|
|
4911
|
+
return;
|
|
4912
|
+
}
|
|
4913
|
+
if (this.keepMs === Number.POSITIVE_INFINITY) return;
|
|
4914
|
+
if (this.keepMs <= 0) {
|
|
4915
|
+
this.entries.delete(key);
|
|
4916
|
+
void this.drop(key, e2);
|
|
4917
|
+
return;
|
|
4918
|
+
}
|
|
4919
|
+
e2.timer = this.setTimer(() => {
|
|
4920
|
+
e2.timer = void 0;
|
|
4921
|
+
if (e2.refs > 0 || this.entries.get(key) !== e2) return;
|
|
4922
|
+
this.entries.delete(key);
|
|
4923
|
+
void this.drop(key, e2);
|
|
4924
|
+
}, this.keepMs);
|
|
4925
|
+
}
|
|
4926
|
+
async drop(key, e2) {
|
|
4927
|
+
if (e2.timer !== void 0) {
|
|
4928
|
+
this.clearTimer(e2.timer);
|
|
4929
|
+
e2.timer = void 0;
|
|
4930
|
+
}
|
|
4931
|
+
if (!e2.settled) {
|
|
4932
|
+
e2.abort.abort(new DOMException(`${key} was cleared while it was being created`, "AbortError"));
|
|
4933
|
+
return;
|
|
4934
|
+
}
|
|
4935
|
+
await this.dispose(key, e2);
|
|
4936
|
+
}
|
|
4937
|
+
async dispose(key, e2) {
|
|
4938
|
+
if (!e2.hasValue) return;
|
|
4939
|
+
e2.hasValue = false;
|
|
4940
|
+
const value = e2.value;
|
|
4941
|
+
e2.value = void 0;
|
|
4942
|
+
try {
|
|
4943
|
+
await this.opts.dispose(value, key);
|
|
4944
|
+
} catch (err) {
|
|
4945
|
+
console.error(`tskit: disposing ${key} threw`, err);
|
|
4946
|
+
}
|
|
4947
|
+
}
|
|
4948
|
+
};
|
|
4949
|
+
function waitFor(p2, ...signals) {
|
|
4950
|
+
const live = signals.filter((s2) => s2 !== void 0);
|
|
4951
|
+
return new Promise((resolve, reject) => {
|
|
4952
|
+
const stop2 = () => {
|
|
4953
|
+
for (const s2 of live) s2.removeEventListener("abort", onAbort);
|
|
4954
|
+
};
|
|
4955
|
+
const onAbort = (ev) => {
|
|
4956
|
+
stop2();
|
|
4957
|
+
const s2 = ev.target;
|
|
4958
|
+
reject(s2.reason ?? new DOMException("aborted", "AbortError"));
|
|
4959
|
+
};
|
|
4960
|
+
for (const s2 of live) s2.addEventListener("abort", onAbort, { once: true });
|
|
4961
|
+
p2.then(
|
|
4962
|
+
(v2) => {
|
|
4963
|
+
stop2();
|
|
4964
|
+
resolve(v2);
|
|
4965
|
+
},
|
|
4966
|
+
(err) => {
|
|
4967
|
+
stop2();
|
|
4968
|
+
reject(err);
|
|
4969
|
+
}
|
|
4970
|
+
);
|
|
4971
|
+
});
|
|
4972
|
+
}
|
|
4973
|
+
function unrefTimer(fn2, ms2) {
|
|
4974
|
+
const t2 = setTimeout(fn2, ms2);
|
|
4975
|
+
t2.unref?.();
|
|
4976
|
+
return t2;
|
|
4977
|
+
}
|
|
4978
|
+
|
|
4979
|
+
// node_modules/.pnpm/@tradalab+tskit@0.3.0/node_modules/@tradalab/tskit/dist/chunk-T3PNHVKJ.js
|
|
4980
|
+
var FetchError = class extends Error {
|
|
4981
|
+
constructor(url, failure, retryable, message, status, options) {
|
|
4982
|
+
super(`tskit: ${url}: ${message}`, options);
|
|
4983
|
+
this.url = url;
|
|
4984
|
+
this.failure = failure;
|
|
4985
|
+
this.retryable = retryable;
|
|
4986
|
+
this.status = status;
|
|
4987
|
+
this.name = "FetchError";
|
|
4988
|
+
}
|
|
4989
|
+
url;
|
|
4990
|
+
failure;
|
|
4991
|
+
retryable;
|
|
4992
|
+
status;
|
|
4993
|
+
};
|
|
4994
|
+
var DEFAULT_STALL_MS = 15e3;
|
|
4995
|
+
var DEFAULT_RETRIES = 2;
|
|
4996
|
+
var DEFAULT_BACKOFF_MS = 500;
|
|
4997
|
+
async function fetchBytes(url, opts) {
|
|
4998
|
+
const retries = opts.retries ?? DEFAULT_RETRIES;
|
|
4999
|
+
const backoff = opts.backoffMs ?? DEFAULT_BACKOFF_MS;
|
|
5000
|
+
const sleep = opts.sleep ?? abortableSleep;
|
|
5001
|
+
for (let attempt = 0; ; attempt++) {
|
|
5002
|
+
try {
|
|
5003
|
+
const bytes = await attemptOnce(url, opts);
|
|
5004
|
+
if (opts.sha256) await verify(url, bytes, opts.sha256);
|
|
5005
|
+
return bytes;
|
|
5006
|
+
} catch (err) {
|
|
5007
|
+
const e2 = asFetchError(url, err, opts.signal);
|
|
5008
|
+
if (!e2.retryable || attempt >= retries) throw e2;
|
|
5009
|
+
opts.onRetry?.(attempt + 1, e2);
|
|
5010
|
+
await sleep(backoff * 2 ** attempt, opts.signal).catch((reason) => {
|
|
5011
|
+
throw new FetchError(url, "aborted", false, "aborted", void 0, { cause: reason });
|
|
5012
|
+
});
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
}
|
|
5016
|
+
function usableSize(res) {
|
|
5017
|
+
const encoding = res.headers.get("content-encoding");
|
|
5018
|
+
if (encoding && encoding !== "identity") return null;
|
|
5019
|
+
const header = res.headers.get("content-length");
|
|
5020
|
+
if (header === null || header.trim() === "") return null;
|
|
5021
|
+
const n2 = Number(header);
|
|
5022
|
+
return Number.isInteger(n2) && n2 >= 0 ? n2 : null;
|
|
5023
|
+
}
|
|
5024
|
+
async function attemptOnce(url, opts) {
|
|
5025
|
+
const stallMs = opts.stallMs ?? DEFAULT_STALL_MS;
|
|
5026
|
+
const local = new AbortController();
|
|
5027
|
+
const outer = opts.signal;
|
|
5028
|
+
const onOuter = () => local.abort(outer?.reason);
|
|
5029
|
+
if (outer?.aborted) throw new FetchError(url, "aborted", false, "aborted", void 0, { cause: outer.reason });
|
|
5030
|
+
outer?.addEventListener("abort", onOuter, { once: true });
|
|
5031
|
+
let stalled = false;
|
|
5032
|
+
let timer;
|
|
5033
|
+
const arm = () => {
|
|
5034
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
5035
|
+
timer = setTimeout(() => {
|
|
5036
|
+
stalled = true;
|
|
5037
|
+
local.abort(new DOMException("stalled", "TimeoutError"));
|
|
5038
|
+
}, stallMs);
|
|
5039
|
+
};
|
|
5040
|
+
let got = 0;
|
|
5041
|
+
try {
|
|
5042
|
+
arm();
|
|
5043
|
+
const init = { signal: local.signal };
|
|
5044
|
+
if (opts.priority) init.priority = opts.priority;
|
|
5045
|
+
const res = await opts.fetch(url, init);
|
|
5046
|
+
if (!res.ok) {
|
|
5047
|
+
await res.body?.cancel().catch(() => {
|
|
5048
|
+
});
|
|
5049
|
+
const retryable = res.status >= 500 || res.status === 408 || res.status === 429;
|
|
5050
|
+
throw new FetchError(url, "status", retryable, `HTTP ${res.status}`, res.status);
|
|
5051
|
+
}
|
|
5052
|
+
const size = usableSize(res);
|
|
5053
|
+
opts.onSize?.(size);
|
|
5054
|
+
const reader = res.body?.getReader();
|
|
5055
|
+
if (reader) {
|
|
5056
|
+
const cancel = () => void reader.cancel(local.signal.reason).catch(() => {
|
|
5057
|
+
});
|
|
5058
|
+
if (local.signal.aborted) cancel();
|
|
5059
|
+
else local.signal.addEventListener("abort", cancel, { once: true });
|
|
5060
|
+
}
|
|
5061
|
+
if (!reader) {
|
|
5062
|
+
const all = new Uint8Array(await res.arrayBuffer());
|
|
5063
|
+
opts.onBytes?.(all.byteLength);
|
|
5064
|
+
return all;
|
|
5065
|
+
}
|
|
5066
|
+
const chunks = [];
|
|
5067
|
+
for (; ; ) {
|
|
5068
|
+
arm();
|
|
5069
|
+
const { done, value } = await reader.read();
|
|
5070
|
+
if (local.signal.aborted) throw local.signal.reason;
|
|
5071
|
+
if (done) break;
|
|
5072
|
+
chunks.push(value);
|
|
5073
|
+
got += value.byteLength;
|
|
5074
|
+
opts.onBytes?.(value.byteLength);
|
|
5075
|
+
}
|
|
5076
|
+
return join(chunks, got);
|
|
5077
|
+
} catch (err) {
|
|
5078
|
+
if (got > 0) opts.onBytes?.(-got);
|
|
5079
|
+
if (stalled) throw new FetchError(url, "stall", true, `no bytes for ${stallMs} ms`, void 0, { cause: err });
|
|
5080
|
+
throw err;
|
|
5081
|
+
} finally {
|
|
5082
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
5083
|
+
outer?.removeEventListener("abort", onOuter);
|
|
5084
|
+
}
|
|
5085
|
+
}
|
|
5086
|
+
function asFetchError(url, err, signal) {
|
|
5087
|
+
if (err instanceof FetchError) return err;
|
|
5088
|
+
if (signal?.aborted) return new FetchError(url, "aborted", false, "aborted", void 0, { cause: err });
|
|
5089
|
+
return new FetchError(url, "network", true, err instanceof Error ? err.message : String(err), void 0, { cause: err });
|
|
5090
|
+
}
|
|
5091
|
+
async function verify(url, bytes, expected) {
|
|
5092
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", bytes));
|
|
5093
|
+
const hex = [...digest].map((b2) => b2.toString(16).padStart(2, "0")).join("");
|
|
5094
|
+
if (hex !== expected.toLowerCase()) {
|
|
5095
|
+
throw new FetchError(url, "integrity", false, `sha256 ${hex} is not the expected ${expected}`);
|
|
5096
|
+
}
|
|
5097
|
+
}
|
|
5098
|
+
function join(chunks, n2) {
|
|
5099
|
+
if (chunks.length === 1) return chunks[0];
|
|
5100
|
+
const out = new Uint8Array(n2);
|
|
5101
|
+
let at2 = 0;
|
|
5102
|
+
for (const c2 of chunks) {
|
|
5103
|
+
out.set(c2, at2);
|
|
5104
|
+
at2 += c2.byteLength;
|
|
5105
|
+
}
|
|
5106
|
+
return out;
|
|
5107
|
+
}
|
|
5108
|
+
function abortableSleep(ms2, signal) {
|
|
5109
|
+
return new Promise((resolve, reject) => {
|
|
5110
|
+
if (signal?.aborted) return reject(signal.reason);
|
|
5111
|
+
const t2 = setTimeout(() => {
|
|
5112
|
+
signal?.removeEventListener("abort", onAbort);
|
|
5113
|
+
resolve();
|
|
5114
|
+
}, ms2);
|
|
5115
|
+
const onAbort = () => {
|
|
5116
|
+
clearTimeout(t2);
|
|
5117
|
+
reject(signal.reason);
|
|
5118
|
+
};
|
|
5119
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
5120
|
+
});
|
|
5121
|
+
}
|
|
5122
|
+
|
|
5123
|
+
// node_modules/.pnpm/@tradalab+tskit@0.3.0/node_modules/@tradalab/tskit/dist/chunk-7P5XCNQW.js
|
|
5124
|
+
var KEY_BASE = "https://tskit.invalid/";
|
|
5125
|
+
function cacheName(name, version) {
|
|
5126
|
+
return `${name}-v${version}`;
|
|
5127
|
+
}
|
|
5128
|
+
async function openByteStore(opts) {
|
|
5129
|
+
const full = cacheName(opts.name, opts.version);
|
|
5130
|
+
const storage2 = opts.caches === void 0 ? globalCaches() : opts.caches;
|
|
5131
|
+
if (storage2) {
|
|
5132
|
+
try {
|
|
5133
|
+
const cache = await storage2.open(full);
|
|
5134
|
+
return cacheStore(storage2, cache, opts.name, full);
|
|
5135
|
+
} catch (err) {
|
|
5136
|
+
console.warn(`tskit: Cache Storage refused ${full}, keeping bytes in memory for this page`, err);
|
|
5137
|
+
}
|
|
5138
|
+
}
|
|
5139
|
+
return memoryStore(full);
|
|
5140
|
+
}
|
|
5141
|
+
async function storageUsage() {
|
|
5142
|
+
const s2 = globalThis.navigator?.storage;
|
|
5143
|
+
if (!s2) return { usage: null, quota: null, persisted: null };
|
|
5144
|
+
const [est, persisted] = await Promise.all([
|
|
5145
|
+
s2.estimate?.().catch(() => void 0),
|
|
5146
|
+
s2.persisted?.().catch(() => null) ?? Promise.resolve(null)
|
|
5147
|
+
]);
|
|
5148
|
+
return { usage: est?.usage ?? null, quota: est?.quota ?? null, persisted: persisted ?? null };
|
|
5149
|
+
}
|
|
5150
|
+
function globalCaches() {
|
|
5151
|
+
return globalThis.caches ?? null;
|
|
5152
|
+
}
|
|
5153
|
+
function toRequest(key) {
|
|
5154
|
+
return KEY_BASE + encodeURIComponent(key);
|
|
5155
|
+
}
|
|
5156
|
+
function fromRequest(url) {
|
|
5157
|
+
return url.startsWith(KEY_BASE) ? decodeURIComponent(url.slice(KEY_BASE.length)) : null;
|
|
5158
|
+
}
|
|
5159
|
+
function cacheStore(storage2, cache, prefix, full) {
|
|
5160
|
+
return {
|
|
5161
|
+
kind: "cache",
|
|
5162
|
+
name: full,
|
|
5163
|
+
async get(key) {
|
|
5164
|
+
const r2 = await cache.match(toRequest(key));
|
|
5165
|
+
return r2 ? new Uint8Array(await r2.arrayBuffer()) : null;
|
|
5166
|
+
},
|
|
5167
|
+
async put(key, bytes, type = "application/octet-stream") {
|
|
5168
|
+
await cache.put(
|
|
5169
|
+
toRequest(key),
|
|
5170
|
+
new Response(bytes, { headers: { "content-type": type, "content-length": String(bytes.byteLength) } })
|
|
5171
|
+
);
|
|
5172
|
+
},
|
|
5173
|
+
delete: (key) => cache.delete(toRequest(key)),
|
|
5174
|
+
async keys() {
|
|
5175
|
+
const reqs = await cache.keys();
|
|
5176
|
+
return reqs.map((r2) => fromRequest(r2.url)).filter((k2) => k2 !== null);
|
|
5177
|
+
},
|
|
5178
|
+
async clear() {
|
|
5179
|
+
for (const r2 of await cache.keys()) await cache.delete(r2);
|
|
5180
|
+
},
|
|
5181
|
+
async purgeOld() {
|
|
5182
|
+
const stale = (await storage2.keys()).filter((n2) => n2 !== full && n2.startsWith(`${prefix}-v`));
|
|
5183
|
+
await Promise.all(stale.map((n2) => storage2.delete(n2)));
|
|
5184
|
+
return stale;
|
|
5185
|
+
}
|
|
5186
|
+
};
|
|
5187
|
+
}
|
|
5188
|
+
function memoryStore(full) {
|
|
5189
|
+
const m2 = /* @__PURE__ */ new Map();
|
|
5190
|
+
return {
|
|
5191
|
+
kind: "memory",
|
|
5192
|
+
name: full,
|
|
5193
|
+
get: async (key) => m2.get(key)?.slice() ?? null,
|
|
5194
|
+
put: async (key, bytes) => {
|
|
5195
|
+
m2.set(key, bytes.slice());
|
|
5196
|
+
},
|
|
5197
|
+
delete: async (key) => m2.delete(key),
|
|
5198
|
+
keys: async () => [...m2.keys()],
|
|
5199
|
+
clear: async () => m2.clear(),
|
|
5200
|
+
purgeOld: async () => []
|
|
5201
|
+
};
|
|
5202
|
+
}
|
|
5203
|
+
|
|
5204
|
+
// node_modules/.pnpm/@tradalab+tskit@0.3.0/node_modules/@tradalab/tskit/dist/assets.js
|
|
5205
|
+
var TAG = "tskit/assets";
|
|
5206
|
+
function isTagged(data) {
|
|
5207
|
+
return typeof data === "object" && data !== null && data.tag === TAG;
|
|
5208
|
+
}
|
|
5209
|
+
function isAssetsMessage(data) {
|
|
5210
|
+
return isTagged(data);
|
|
5211
|
+
}
|
|
5212
|
+
function newRealm() {
|
|
5213
|
+
const c2 = globalThis.crypto;
|
|
5214
|
+
return c2?.randomUUID?.() ?? `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
|
5215
|
+
}
|
|
5216
|
+
function applyPhase(phases, m2) {
|
|
5217
|
+
let realm = phases.get(m2.realm);
|
|
5218
|
+
if (!realm) phases.set(m2.realm, realm = /* @__PURE__ */ new Map());
|
|
5219
|
+
if (m2.phase === "disposed") realm.delete(m2.id);
|
|
5220
|
+
else realm.set(m2.id, { phase: m2.phase, error: m2.error ?? "" });
|
|
5221
|
+
if (realm.size === 0) phases.delete(m2.realm);
|
|
5222
|
+
}
|
|
5223
|
+
var LOW_MEMORY_GB = 4;
|
|
5224
|
+
var KEEP_MS = 3e4;
|
|
5225
|
+
function defaultKeepMs(deviceMemory) {
|
|
5226
|
+
const gb = deviceMemory ?? globalThis.navigator?.deviceMemory;
|
|
5227
|
+
return typeof gb === "number" && gb <= LOW_MEMORY_GB ? 0 : KEEP_MS;
|
|
5228
|
+
}
|
|
5229
|
+
var Assets = class {
|
|
5230
|
+
constructor(opts) {
|
|
5231
|
+
this.opts = opts;
|
|
5232
|
+
for (const s2 of opts.manifest) {
|
|
5233
|
+
if (this.specs.has(s2.id)) throw new Error(`tskit: asset ${s2.id} is declared twice`);
|
|
5234
|
+
this.specs.set(s2.id, s2);
|
|
5235
|
+
this.states.set(s2.id, { phase: "idle", loaded: 0, size: s2.size, cached: false, error: "" });
|
|
5236
|
+
this.stamps.set(s2.id, 0);
|
|
5237
|
+
}
|
|
5238
|
+
this.store = opts.store ? Promise.resolve(opts.store) : openByteStore({ name: opts.name, version: opts.version, caches: opts.caches });
|
|
5239
|
+
this.retainer = new Retainer({
|
|
5240
|
+
keepMs: opts.keepMs ?? defaultKeepMs(),
|
|
5241
|
+
create: (id, signal) => this.create(id, signal),
|
|
5242
|
+
dispose: async (value, id) => {
|
|
5243
|
+
const rt2 = this.runtimes.get(id);
|
|
5244
|
+
this.runtimes.delete(id);
|
|
5245
|
+
this.set(id, { phase: "idle", loaded: 0 });
|
|
5246
|
+
await rt2?.dispose(value);
|
|
5247
|
+
}
|
|
5248
|
+
});
|
|
5249
|
+
this.downloads = new Retainer({
|
|
5250
|
+
create: (id, signal) => this.download(this.spec(id), signal),
|
|
5251
|
+
dispose: () => {
|
|
5252
|
+
}
|
|
5253
|
+
});
|
|
5254
|
+
}
|
|
5255
|
+
opts;
|
|
5256
|
+
specs = /* @__PURE__ */ new Map();
|
|
5257
|
+
states = /* @__PURE__ */ new Map();
|
|
5258
|
+
listeners = /* @__PURE__ */ new Set();
|
|
5259
|
+
runtimes = /* @__PURE__ */ new Map();
|
|
5260
|
+
sinks = /* @__PURE__ */ new Map();
|
|
5261
|
+
stamps = /* @__PURE__ */ new Map();
|
|
5262
|
+
views = /* @__PURE__ */ new Map();
|
|
5263
|
+
given = /* @__PURE__ */ new WeakSet();
|
|
5264
|
+
retainer;
|
|
5265
|
+
downloads;
|
|
5266
|
+
store;
|
|
5267
|
+
rev = 0;
|
|
5268
|
+
remote = /* @__PURE__ */ new Map();
|
|
5269
|
+
served = /* @__PURE__ */ new Map();
|
|
5270
|
+
snap = null;
|
|
5271
|
+
spec(id) {
|
|
5272
|
+
const s2 = this.specs.get(id);
|
|
5273
|
+
if (!s2) throw new Error(`tskit: asset ${id} is not in the manifest`);
|
|
5274
|
+
return s2;
|
|
5275
|
+
}
|
|
5276
|
+
state(id) {
|
|
5277
|
+
this.spec(id);
|
|
5278
|
+
const s2 = this.states.get(id);
|
|
5279
|
+
const local = this.retainer.state(id);
|
|
5280
|
+
const kept = s2.phase === "ready" && local === "kept";
|
|
5281
|
+
const far = local === void 0 ? this.remotePhase(id) : null;
|
|
5282
|
+
const key = `${this.stamps.get(id)}:${kept}:${far?.phase ?? ""}:${far?.error ?? ""}`;
|
|
5283
|
+
const cached = this.views.get(id);
|
|
5284
|
+
if (cached && cached.key === key) return cached.view;
|
|
5285
|
+
const phase = kept ? "kept" : far ? far.phase : s2.phase;
|
|
5286
|
+
const view = Object.freeze({ ...s2, phase, error: far?.error || s2.error });
|
|
5287
|
+
this.views.set(id, { key, view });
|
|
5288
|
+
return view;
|
|
5289
|
+
}
|
|
5290
|
+
snapshot() {
|
|
5291
|
+
if (this.snap && this.snap.rev === this.rev) return this.snap.view;
|
|
5292
|
+
const out = {};
|
|
5293
|
+
for (const id of this.specs.keys()) out[id] = this.state(id);
|
|
5294
|
+
this.snap = { rev: this.rev, view: Object.freeze(out) };
|
|
5295
|
+
return this.snap.view;
|
|
5296
|
+
}
|
|
5297
|
+
get revision() {
|
|
5298
|
+
return this.rev;
|
|
5299
|
+
}
|
|
5300
|
+
subscribe(listener) {
|
|
5301
|
+
this.listeners.add(listener);
|
|
5302
|
+
return () => this.listeners.delete(listener);
|
|
5303
|
+
}
|
|
5304
|
+
async bytes(id, signal, item) {
|
|
5305
|
+
const spec = this.spec(id);
|
|
5306
|
+
if (spec.cacheable !== false) {
|
|
5307
|
+
const hit = await (await this.store).get(id);
|
|
5308
|
+
if (hit) {
|
|
5309
|
+
this.set(id, { cached: true, size: hit.byteLength, loaded: hit.byteLength });
|
|
5310
|
+
item?.size(hit.byteLength);
|
|
5311
|
+
item?.bytes(hit.byteLength);
|
|
5312
|
+
return hit;
|
|
5313
|
+
}
|
|
5314
|
+
}
|
|
5315
|
+
let sinks = this.sinks.get(id);
|
|
5316
|
+
if (!sinks) this.sinks.set(id, sinks = /* @__PURE__ */ new Set());
|
|
5317
|
+
if (item) {
|
|
5318
|
+
const now = this.states.get(id);
|
|
5319
|
+
if (this.downloads.state(id) === "creating") {
|
|
5320
|
+
if (typeof now.size === "number" || now.size === null) item.size(now.size);
|
|
5321
|
+
if (now.loaded > 0) item.bytes(now.loaded);
|
|
5322
|
+
}
|
|
5323
|
+
sinks.add(item);
|
|
5324
|
+
}
|
|
5325
|
+
try {
|
|
5326
|
+
const h2 = await this.downloads.acquire(id, signal);
|
|
5327
|
+
const bytes = h2.value;
|
|
5328
|
+
h2.release();
|
|
5329
|
+
if (this.given.has(bytes)) return bytes.slice();
|
|
5330
|
+
this.given.add(bytes);
|
|
5331
|
+
return bytes;
|
|
5332
|
+
} finally {
|
|
5333
|
+
if (item) sinks.delete(item);
|
|
5334
|
+
}
|
|
5335
|
+
}
|
|
5336
|
+
async acquire(id, runtime, signal) {
|
|
5337
|
+
this.spec(id);
|
|
5338
|
+
if (!this.runtimes.has(id)) this.runtimes.set(id, runtime);
|
|
5339
|
+
const h2 = await this.retainer.acquire(id, signal);
|
|
5340
|
+
this.emit();
|
|
5341
|
+
let released = false;
|
|
5342
|
+
return {
|
|
5343
|
+
value: h2.value,
|
|
5344
|
+
release: () => {
|
|
5345
|
+
if (released) return;
|
|
5346
|
+
released = true;
|
|
5347
|
+
h2.release();
|
|
5348
|
+
this.emit();
|
|
5349
|
+
}
|
|
5350
|
+
};
|
|
5351
|
+
}
|
|
5352
|
+
serve(port) {
|
|
5353
|
+
const existing = this.served.get(port);
|
|
5354
|
+
if (existing) return existing;
|
|
5355
|
+
const phases = /* @__PURE__ */ new Map();
|
|
5356
|
+
this.remote.set(port, phases);
|
|
5357
|
+
const onMessage = (ev) => {
|
|
5358
|
+
const m2 = ev.data;
|
|
5359
|
+
if (!isTagged(m2)) return;
|
|
5360
|
+
if (m2.t === "need") void this.answer(port, m2);
|
|
5361
|
+
else if (m2.t === "phase") {
|
|
5362
|
+
if (!this.specs.has(m2.id)) return;
|
|
5363
|
+
applyPhase(phases, m2);
|
|
5364
|
+
this.emit();
|
|
5365
|
+
} else if (m2.t === "bye") {
|
|
5366
|
+
if (phases.delete(m2.realm)) this.emit();
|
|
5367
|
+
}
|
|
5368
|
+
};
|
|
5369
|
+
const close = () => {
|
|
5370
|
+
if (this.served.get(port) !== close) return;
|
|
5371
|
+
this.served.delete(port);
|
|
5372
|
+
port.removeEventListener("message", onMessage);
|
|
5373
|
+
if (this.remote.delete(port)) this.emit();
|
|
5374
|
+
};
|
|
5375
|
+
this.served.set(port, close);
|
|
5376
|
+
port.addEventListener("message", onMessage);
|
|
5377
|
+
port.start?.();
|
|
5378
|
+
return close;
|
|
5379
|
+
}
|
|
5380
|
+
async bytesFor(ids, signal) {
|
|
5381
|
+
const out = {};
|
|
5382
|
+
await Promise.all(ids.map(async (id) => out[id] = (await this.bytes(id, signal)).slice()));
|
|
5383
|
+
return out;
|
|
5384
|
+
}
|
|
5385
|
+
async preload(ids, onProgress, signal) {
|
|
5386
|
+
const tracker = new ProgressTracker();
|
|
5387
|
+
const items = ids.map((id) => tracker.item(id, this.spec(id).size));
|
|
5388
|
+
const stop2 = onProgress ? tracker.subscribe(onProgress) : () => {
|
|
5389
|
+
};
|
|
5390
|
+
tracker.flush();
|
|
5391
|
+
try {
|
|
5392
|
+
await Promise.all(
|
|
5393
|
+
ids.map(async (id, i2) => {
|
|
5394
|
+
const item = items[i2];
|
|
5395
|
+
try {
|
|
5396
|
+
await this.bytes(id, signal, item);
|
|
5397
|
+
item.ready();
|
|
5398
|
+
} catch (err) {
|
|
5399
|
+
item.fail(err instanceof Error ? err.message : String(err));
|
|
5400
|
+
}
|
|
5401
|
+
})
|
|
5402
|
+
);
|
|
5403
|
+
return tracker.snapshot();
|
|
5404
|
+
} finally {
|
|
5405
|
+
stop2();
|
|
5406
|
+
}
|
|
5407
|
+
}
|
|
5408
|
+
progress(ids = [...this.specs.keys()]) {
|
|
5409
|
+
const tracker = new ProgressTracker();
|
|
5410
|
+
for (const id of ids) {
|
|
5411
|
+
const s2 = this.states.get(id);
|
|
5412
|
+
if (!s2) continue;
|
|
5413
|
+
const item = tracker.item(id, s2.size);
|
|
5414
|
+
if (s2.loaded) item.bytes(s2.loaded);
|
|
5415
|
+
if (s2.phase === "building") item.building();
|
|
5416
|
+
if (s2.phase === "ready" || s2.phase === "idle" && s2.cached) item.ready();
|
|
5417
|
+
if (s2.phase === "failed") item.fail(s2.error);
|
|
5418
|
+
}
|
|
5419
|
+
return tracker.snapshot();
|
|
5420
|
+
}
|
|
5421
|
+
async usage() {
|
|
5422
|
+
const store = await this.store;
|
|
5423
|
+
const [u2, cached] = await Promise.all([storageUsage(), store.keys()]);
|
|
5424
|
+
return { ...u2, store: store.kind, cached };
|
|
5425
|
+
}
|
|
5426
|
+
async purgeOld() {
|
|
5427
|
+
return (await this.store).purgeOld();
|
|
5428
|
+
}
|
|
5429
|
+
async clear() {
|
|
5430
|
+
await this.downloads.clear();
|
|
5431
|
+
await this.retainer.clear();
|
|
5432
|
+
await (await this.store).clear();
|
|
5433
|
+
for (const id of this.specs.keys()) this.set(id, { phase: "idle", loaded: 0, cached: false, error: "" });
|
|
5434
|
+
}
|
|
5435
|
+
remotePhase(id) {
|
|
5436
|
+
let building = false;
|
|
5437
|
+
let ready = false;
|
|
5438
|
+
let failed = "";
|
|
5439
|
+
for (const phases of this.remote.values()) for (const realm of phases.values()) {
|
|
5440
|
+
const p2 = realm.get(id);
|
|
5441
|
+
if (!p2) continue;
|
|
5442
|
+
if (p2.phase === "building") building = true;
|
|
5443
|
+
else if (p2.phase === "ready") ready = true;
|
|
5444
|
+
else if (p2.phase === "failed") failed ||= p2.error || "failed";
|
|
5445
|
+
}
|
|
5446
|
+
if (building) return { phase: "building", error: "" };
|
|
5447
|
+
if (ready) return { phase: "ready", error: "" };
|
|
5448
|
+
if (failed) return { phase: "failed", error: failed };
|
|
5449
|
+
return null;
|
|
5450
|
+
}
|
|
5451
|
+
async answer(port, m2) {
|
|
5452
|
+
const reply = (d2, transfer = []) => port.postMessage({ tag: TAG, t: "have", req: m2.req, id: m2.id, ...d2 }, transfer);
|
|
5453
|
+
try {
|
|
5454
|
+
const bytes = await this.bytes(m2.id);
|
|
5455
|
+
const store = await this.store;
|
|
5456
|
+
if (m2.caches && store.kind === "cache" && this.spec(m2.id).cacheable !== false) return reply({ via: "cache" });
|
|
5457
|
+
const copy = bytes.slice();
|
|
5458
|
+
reply({ via: "bytes", bytes: copy }, [copy.buffer]);
|
|
5459
|
+
} catch (err) {
|
|
5460
|
+
reply({ via: "bytes", error: describe(err) });
|
|
5461
|
+
}
|
|
5462
|
+
}
|
|
5463
|
+
async download(spec, signal) {
|
|
5464
|
+
const id = spec.id;
|
|
5465
|
+
const sinks = () => this.sinks.get(id) ?? /* @__PURE__ */ new Set();
|
|
5466
|
+
this.set(id, { phase: "fetching", loaded: 0, error: "" });
|
|
5467
|
+
let bytes;
|
|
5468
|
+
try {
|
|
5469
|
+
bytes = await fetchBytes(spec.url, {
|
|
5470
|
+
fetch: this.opts.fetch,
|
|
5471
|
+
signal,
|
|
5472
|
+
sha256: spec.sha256,
|
|
5473
|
+
stallMs: this.opts.stallMs,
|
|
5474
|
+
retries: this.opts.retries,
|
|
5475
|
+
onSize: (n2) => {
|
|
5476
|
+
const size = n2 ?? (spec.size === void 0 ? null : spec.size);
|
|
5477
|
+
for (const it2 of sinks()) it2.size(size);
|
|
5478
|
+
this.set(id, { size });
|
|
5479
|
+
},
|
|
5480
|
+
onBytes: (n2) => {
|
|
5481
|
+
for (const it2 of sinks()) {
|
|
5482
|
+
if (n2 < 0) it2.rollback();
|
|
5483
|
+
else it2.bytes(n2);
|
|
5484
|
+
}
|
|
5485
|
+
this.set(id, { loaded: Math.max(0, this.states.get(id).loaded + n2) });
|
|
5486
|
+
}
|
|
5487
|
+
});
|
|
5488
|
+
} catch (err) {
|
|
5489
|
+
if (isAbort(err, signal)) this.set(id, { phase: "idle", loaded: 0, error: "" });
|
|
5490
|
+
else this.set(id, { phase: "failed", loaded: 0, error: describe(err) });
|
|
5491
|
+
throw err;
|
|
5492
|
+
}
|
|
5493
|
+
const next = this.retainer.state(id) === "creating" ? "building" : "idle";
|
|
5494
|
+
this.set(id, { phase: next, size: bytes.byteLength, loaded: bytes.byteLength });
|
|
5495
|
+
if (spec.cacheable !== false) {
|
|
5496
|
+
try {
|
|
5497
|
+
await (await this.store).put(id, bytes, spec.type);
|
|
5498
|
+
this.set(id, { cached: true });
|
|
5499
|
+
} catch (err) {
|
|
5500
|
+
console.warn(`tskit: could not cache ${id}`, err);
|
|
5501
|
+
}
|
|
5502
|
+
}
|
|
5503
|
+
return bytes;
|
|
5504
|
+
}
|
|
5505
|
+
async create(id, signal) {
|
|
5506
|
+
const rt2 = this.runtimes.get(id);
|
|
5507
|
+
if (!rt2) throw new Error(`tskit: no runtime for ${id}`);
|
|
5508
|
+
try {
|
|
5509
|
+
this.set(id, { phase: "fetching", error: "" });
|
|
5510
|
+
const bytes = await this.bytes(id, signal);
|
|
5511
|
+
this.set(id, { phase: "building" });
|
|
5512
|
+
const value = await rt2.build(bytes, { id, signal });
|
|
5513
|
+
this.set(id, { phase: "ready" });
|
|
5514
|
+
return value;
|
|
5515
|
+
} catch (err) {
|
|
5516
|
+
this.runtimes.delete(id);
|
|
5517
|
+
if (isAbort(err, signal)) this.set(id, { phase: "idle", error: "" });
|
|
5518
|
+
else this.set(id, { phase: "failed", error: describe(err) });
|
|
5519
|
+
throw err;
|
|
5520
|
+
}
|
|
5521
|
+
}
|
|
5522
|
+
set(id, patch) {
|
|
5523
|
+
const s2 = this.states.get(id);
|
|
5524
|
+
if (!s2) return;
|
|
5525
|
+
Object.assign(s2, patch);
|
|
5526
|
+
this.stamps.set(id, (this.stamps.get(id) ?? 0) + 1);
|
|
5527
|
+
this.emit();
|
|
5528
|
+
}
|
|
5529
|
+
emit() {
|
|
5530
|
+
this.rev++;
|
|
5531
|
+
for (const l2 of [...this.listeners]) {
|
|
5532
|
+
try {
|
|
5533
|
+
l2();
|
|
5534
|
+
} catch (err) {
|
|
5535
|
+
this.listeners.delete(l2);
|
|
5536
|
+
console.error("tskit: an assets listener threw and was removed", err);
|
|
5537
|
+
}
|
|
5538
|
+
}
|
|
5539
|
+
}
|
|
5540
|
+
};
|
|
5541
|
+
function createAssets(opts) {
|
|
5542
|
+
return new Assets(opts);
|
|
5543
|
+
}
|
|
5544
|
+
var AssetsClient = class {
|
|
5545
|
+
constructor(port, opts) {
|
|
5546
|
+
this.port = port;
|
|
5547
|
+
this.store = opts.store ? Promise.resolve(opts.store) : openByteStore({ name: opts.name, version: opts.version, caches: opts.caches });
|
|
5548
|
+
this.retainer = new Retainer({
|
|
5549
|
+
keepMs: opts.keepMs ?? defaultKeepMs(),
|
|
5550
|
+
create: (id, signal) => this.create(id, signal),
|
|
5551
|
+
dispose: async (value, id) => {
|
|
5552
|
+
const rt2 = this.runtimes.get(id);
|
|
5553
|
+
this.runtimes.delete(id);
|
|
5554
|
+
this.report(id, "disposed");
|
|
5555
|
+
await rt2?.dispose(value);
|
|
5556
|
+
}
|
|
5557
|
+
});
|
|
5558
|
+
port.addEventListener("message", this.onMessage);
|
|
5559
|
+
port.start?.();
|
|
5560
|
+
}
|
|
5561
|
+
port;
|
|
5562
|
+
waiting = /* @__PURE__ */ new Map();
|
|
5563
|
+
retainer;
|
|
5564
|
+
runtimes = /* @__PURE__ */ new Map();
|
|
5565
|
+
store;
|
|
5566
|
+
req = 0;
|
|
5567
|
+
closed = false;
|
|
5568
|
+
closing = false;
|
|
5569
|
+
realm = newRealm();
|
|
5570
|
+
children = /* @__PURE__ */ new Map();
|
|
5571
|
+
viaCache = /* @__PURE__ */ new Set();
|
|
5572
|
+
inflight = /* @__PURE__ */ new Map();
|
|
5573
|
+
onMessage = (ev) => {
|
|
5574
|
+
const m2 = ev.data;
|
|
5575
|
+
if (!isTagged(m2) || m2.t !== "have") return;
|
|
5576
|
+
const w2 = this.waiting.get(m2.req);
|
|
5577
|
+
if (!w2) return;
|
|
5578
|
+
this.waiting.delete(m2.req);
|
|
5579
|
+
w2.resolve(m2);
|
|
5580
|
+
};
|
|
5581
|
+
async bytes(id, signal) {
|
|
5582
|
+
signal?.throwIfAborted();
|
|
5583
|
+
let entry = this.inflight.get(id);
|
|
5584
|
+
if (!entry) {
|
|
5585
|
+
const e2 = { waiters: 0, done: void 0 };
|
|
5586
|
+
e2.done = this.load(id).then((bytes) => ({ bytes, shared: e2.waiters > 1 }));
|
|
5587
|
+
e2.done.catch(() => {
|
|
5588
|
+
}).finally(() => {
|
|
5589
|
+
if (this.inflight.get(id) === e2) this.inflight.delete(id);
|
|
5590
|
+
});
|
|
5591
|
+
this.inflight.set(id, e2);
|
|
5592
|
+
entry = e2;
|
|
5593
|
+
}
|
|
5594
|
+
entry.waiters++;
|
|
5595
|
+
let left = false;
|
|
5596
|
+
const leave = () => {
|
|
5597
|
+
if (left) return;
|
|
5598
|
+
left = true;
|
|
5599
|
+
entry.waiters--;
|
|
5600
|
+
};
|
|
5601
|
+
try {
|
|
5602
|
+
const { bytes, shared } = await raceAbort(entry.done, signal, leave);
|
|
5603
|
+
return shared ? bytes.slice() : bytes;
|
|
5604
|
+
} finally {
|
|
5605
|
+
leave();
|
|
5606
|
+
}
|
|
5607
|
+
}
|
|
5608
|
+
async load(id) {
|
|
5609
|
+
const store = await this.store;
|
|
5610
|
+
const d2 = await this.ask(id, store.kind === "cache");
|
|
5611
|
+
if (d2.via === "cache") {
|
|
5612
|
+
const hit = await store.get(id);
|
|
5613
|
+
if (hit) {
|
|
5614
|
+
this.viaCache.add(id);
|
|
5615
|
+
return hit;
|
|
5616
|
+
}
|
|
5617
|
+
const again = await this.ask(id, false);
|
|
5618
|
+
this.viaCache.delete(id);
|
|
5619
|
+
return this.unwrap(id, again);
|
|
5620
|
+
}
|
|
5621
|
+
this.viaCache.delete(id);
|
|
5622
|
+
return this.unwrap(id, d2);
|
|
5623
|
+
}
|
|
5624
|
+
async acquire(id, runtime, signal) {
|
|
5625
|
+
if (this.closing) throw new DOMException("assets client closed", "AbortError");
|
|
5626
|
+
if (!this.runtimes.has(id)) this.runtimes.set(id, runtime);
|
|
5627
|
+
const h2 = await this.retainer.acquire(id, signal);
|
|
5628
|
+
return { value: h2.value, release: () => h2.release() };
|
|
5629
|
+
}
|
|
5630
|
+
report(id, phase, error) {
|
|
5631
|
+
if (this.closed) return;
|
|
5632
|
+
this.port.postMessage({ tag: TAG, t: "phase", realm: this.realm, id, phase, error });
|
|
5633
|
+
}
|
|
5634
|
+
/** Serves a worker started from this realm through this client: it asks the page, and the page sees its builds. */
|
|
5635
|
+
serve(port) {
|
|
5636
|
+
const existing = this.children.get(port);
|
|
5637
|
+
if (existing) return existing.close;
|
|
5638
|
+
const realms = /* @__PURE__ */ new Set();
|
|
5639
|
+
const onMessage = (ev) => {
|
|
5640
|
+
const m2 = ev.data;
|
|
5641
|
+
if (!isTagged(m2)) return;
|
|
5642
|
+
if (m2.t === "need") void this.relay(port, m2);
|
|
5643
|
+
else if (m2.t === "phase" || m2.t === "bye") {
|
|
5644
|
+
if (m2.t === "phase") realms.add(m2.realm);
|
|
5645
|
+
else realms.delete(m2.realm);
|
|
5646
|
+
if (!this.closed) this.port.postMessage(m2);
|
|
5647
|
+
}
|
|
5648
|
+
};
|
|
5649
|
+
const close = () => {
|
|
5650
|
+
if (this.children.get(port)?.close !== close) return;
|
|
5651
|
+
this.children.delete(port);
|
|
5652
|
+
port.removeEventListener("message", onMessage);
|
|
5653
|
+
if (!this.closed) for (const r2 of realms) this.port.postMessage({ tag: TAG, t: "bye", realm: r2 });
|
|
5654
|
+
};
|
|
5655
|
+
this.children.set(port, { close, realms });
|
|
5656
|
+
port.addEventListener("message", onMessage);
|
|
5657
|
+
port.start?.();
|
|
5658
|
+
return close;
|
|
5659
|
+
}
|
|
5660
|
+
async close() {
|
|
5661
|
+
if (this.closing) return;
|
|
5662
|
+
this.closing = true;
|
|
5663
|
+
await this.retainer.clear();
|
|
5664
|
+
for (const child of [...this.children.values()]) child.close();
|
|
5665
|
+
this.closed = true;
|
|
5666
|
+
this.port.postMessage({ tag: TAG, t: "bye", realm: this.realm });
|
|
5667
|
+
this.port.removeEventListener("message", this.onMessage);
|
|
5668
|
+
for (const w2 of this.waiting.values()) w2.reject(new DOMException("assets client closed", "AbortError"));
|
|
5669
|
+
this.waiting.clear();
|
|
5670
|
+
}
|
|
5671
|
+
ask(id, caches) {
|
|
5672
|
+
if (this.closing) return Promise.reject(new DOMException("assets client closed", "AbortError"));
|
|
5673
|
+
const req = ++this.req;
|
|
5674
|
+
return new Promise((resolve, reject) => {
|
|
5675
|
+
this.waiting.set(req, { resolve, reject });
|
|
5676
|
+
this.port.postMessage({ tag: TAG, t: "need", req, id, caches });
|
|
5677
|
+
});
|
|
5678
|
+
}
|
|
5679
|
+
async relay(port, m2) {
|
|
5680
|
+
const reply = (d2, transfer = []) => port.postMessage({ tag: TAG, t: "have", req: m2.req, id: m2.id, ...d2 }, transfer);
|
|
5681
|
+
try {
|
|
5682
|
+
const bytes = await this.bytes(m2.id);
|
|
5683
|
+
if (m2.caches && this.viaCache.has(m2.id)) return reply({ via: "cache" });
|
|
5684
|
+
const copy = bytes.slice();
|
|
5685
|
+
reply({ via: "bytes", bytes: copy }, [copy.buffer]);
|
|
5686
|
+
} catch (err) {
|
|
5687
|
+
reply({ via: "bytes", error: describe(err) });
|
|
5688
|
+
}
|
|
5689
|
+
}
|
|
5690
|
+
unwrap(id, d2) {
|
|
5691
|
+
if (d2.error) throw new Error(d2.error);
|
|
5692
|
+
if (!d2.bytes) throw new Error(`tskit: ${id} came back without bytes`);
|
|
5693
|
+
return d2.bytes;
|
|
5694
|
+
}
|
|
5695
|
+
async create(id, signal) {
|
|
5696
|
+
const rt2 = this.runtimes.get(id);
|
|
5697
|
+
if (!rt2) throw new Error(`tskit: no runtime for ${id}`);
|
|
5698
|
+
try {
|
|
5699
|
+
const bytes = await this.bytes(id, signal);
|
|
5700
|
+
this.report(id, "building");
|
|
5701
|
+
const value = await rt2.build(bytes, { id, signal });
|
|
5702
|
+
this.report(id, "ready");
|
|
5703
|
+
return value;
|
|
5704
|
+
} catch (err) {
|
|
5705
|
+
this.runtimes.delete(id);
|
|
5706
|
+
if (isAbort(err, signal)) this.report(id, "disposed");
|
|
5707
|
+
else this.report(id, "failed", describe(err));
|
|
5708
|
+
throw err;
|
|
5709
|
+
}
|
|
5710
|
+
}
|
|
5711
|
+
};
|
|
5712
|
+
function connectAssets(port, opts) {
|
|
5713
|
+
return new AssetsClient(port, opts);
|
|
5714
|
+
}
|
|
5715
|
+
function raceAbort(p2, signal, onAbort) {
|
|
5716
|
+
if (!signal) return p2;
|
|
5717
|
+
return new Promise((resolve, reject) => {
|
|
5718
|
+
const abort2 = () => {
|
|
5719
|
+
onAbort();
|
|
5720
|
+
reject(signal.reason ?? new DOMException("aborted", "AbortError"));
|
|
5721
|
+
};
|
|
5722
|
+
signal.addEventListener("abort", abort2, { once: true });
|
|
5723
|
+
p2.then(
|
|
5724
|
+
(v2) => {
|
|
5725
|
+
signal.removeEventListener("abort", abort2);
|
|
5726
|
+
resolve(v2);
|
|
5727
|
+
},
|
|
5728
|
+
(err) => {
|
|
5729
|
+
signal.removeEventListener("abort", abort2);
|
|
5730
|
+
reject(err);
|
|
5731
|
+
}
|
|
5732
|
+
);
|
|
5733
|
+
});
|
|
5734
|
+
}
|
|
5735
|
+
function isAbort(err, signal) {
|
|
5736
|
+
if (signal.aborted) return true;
|
|
5737
|
+
if (err instanceof FetchError) return err.failure === "aborted";
|
|
5738
|
+
return err instanceof DOMException && err.name === "AbortError";
|
|
5739
|
+
}
|
|
5740
|
+
function describe(err) {
|
|
5741
|
+
if (err instanceof Error) return err.message;
|
|
5742
|
+
return String(err);
|
|
5743
|
+
}
|
|
5744
|
+
|
|
5745
|
+
// src/res.ts
|
|
5746
|
+
var RES_NAME = "mediakit";
|
|
5747
|
+
var RES_VERSION = true ? "2.10.0" : "dev";
|
|
5748
|
+
var MP_RUNTIME = ["vision_wasm_internal.js", "vision_wasm_internal.wasm", "vision_wasm_nosimd_internal.js", "vision_wasm_nosimd_internal.wasm"];
|
|
5749
|
+
var NOT_FETCHED = /* @__PURE__ */ new Set(["worker.js", "detect-worker.js", "NOTICE"]);
|
|
5750
|
+
var TYPES = {
|
|
5751
|
+
js: "text/javascript",
|
|
5752
|
+
mjs: "text/javascript",
|
|
5753
|
+
wasm: "application/wasm",
|
|
5754
|
+
png: "image/png"
|
|
5755
|
+
};
|
|
5756
|
+
function typeOf(path) {
|
|
5757
|
+
return TYPES[path.slice(path.lastIndexOf(".") + 1)] ?? "application/octet-stream";
|
|
5758
|
+
}
|
|
5759
|
+
function resourceManifest() {
|
|
5760
|
+
const specs = [];
|
|
5761
|
+
for (const a2 of ASSETS) {
|
|
5762
|
+
if (a2.debug || a2.path.endsWith("/") || NOT_FETCHED.has(a2.path)) continue;
|
|
5763
|
+
specs.push({ id: a2.path, url: assetUrl(a2.path), type: typeOf(a2.path), group: a2.part });
|
|
5764
|
+
}
|
|
5765
|
+
for (const f2 of MP_RUNTIME) specs.push({ id: `mp/${f2}`, url: assetUrl(`mp/${f2}`), type: typeOf(f2), group: "mp" });
|
|
5766
|
+
return specs;
|
|
5767
|
+
}
|
|
5768
|
+
var source = null;
|
|
5769
|
+
var urls = /* @__PURE__ */ new Map();
|
|
5770
|
+
function resources() {
|
|
5771
|
+
if (!source) {
|
|
5772
|
+
const owner = createAssets({ manifest: resourceManifest(), name: RES_NAME, version: RES_VERSION, fetch: (u2, i2) => fetch(u2, i2) });
|
|
5773
|
+
owner.purgeOld().catch((err) => console.warn("mediakit: could not delete the files of older versions", err));
|
|
5774
|
+
source = owner;
|
|
5775
|
+
}
|
|
5776
|
+
return source;
|
|
5777
|
+
}
|
|
5778
|
+
function connectResources(port) {
|
|
5779
|
+
source = connectAssets(port, { name: RES_NAME, version: RES_VERSION });
|
|
5780
|
+
}
|
|
5781
|
+
function serveResources(port) {
|
|
5782
|
+
return resources().serve(port);
|
|
5783
|
+
}
|
|
5784
|
+
function resourceId(pathOrUrl) {
|
|
5785
|
+
return pathOrUrl.startsWith(ASSET_BASE) ? pathOrUrl.slice(ASSET_BASE.length) : pathOrUrl;
|
|
5786
|
+
}
|
|
5787
|
+
function resourceBytes(id, signal) {
|
|
5788
|
+
return resources().bytes(id, signal);
|
|
5789
|
+
}
|
|
5790
|
+
function resourceUrl(id) {
|
|
5791
|
+
let p2 = urls.get(id);
|
|
5792
|
+
if (!p2) {
|
|
5793
|
+
p2 = resourceBytes(id).then((b2) => URL.createObjectURL(new Blob([b2], { type: typeOf(id) })));
|
|
5794
|
+
p2.catch(() => urls.delete(id));
|
|
5795
|
+
urls.set(id, p2);
|
|
5796
|
+
}
|
|
5797
|
+
return p2;
|
|
5798
|
+
}
|
|
5799
|
+
|
|
4684
5800
|
// src/mp.ts
|
|
4685
5801
|
var MP_DIR = assetUrl("mp");
|
|
4686
|
-
var MODEL_URL = {
|
|
4687
|
-
person: assetUrl("selfie_segmenter.tflite"),
|
|
4688
|
-
classes: assetUrl("selfie_multiclass_256x256.tflite"),
|
|
4689
|
-
face: assetUrl("face_landmarker.task")
|
|
4690
|
-
};
|
|
4691
5802
|
var LOAD_MS = 6e4;
|
|
4692
5803
|
var fileset = null;
|
|
5804
|
+
var MODEL_ID = {
|
|
5805
|
+
person: "selfie_segmenter.tflite",
|
|
5806
|
+
classes: "selfie_multiclass_256x256.tflite",
|
|
5807
|
+
face: "face_landmarker.task"
|
|
5808
|
+
};
|
|
5809
|
+
function modelBytes(model) {
|
|
5810
|
+
return resourceBytes(MODEL_ID[model]);
|
|
5811
|
+
}
|
|
4693
5812
|
function visionFileset() {
|
|
4694
|
-
fileset ??=
|
|
5813
|
+
fileset ??= (async () => {
|
|
5814
|
+
const base = await uh.forVisionTasks(MP_DIR);
|
|
5815
|
+
const [loader, binary] = await Promise.all([resourceUrl(resourceId(base.wasmLoaderPath)), resourceUrl(resourceId(base.wasmBinaryPath))]);
|
|
5816
|
+
return { ...base, wasmLoaderPath: loader, wasmBinaryPath: binary };
|
|
5817
|
+
})().catch((e2) => {
|
|
4695
5818
|
fileset = null;
|
|
4696
5819
|
throw e2;
|
|
4697
5820
|
});
|
|
@@ -4750,7 +5873,7 @@
|
|
|
4750
5873
|
const fileset2 = await visionFileset();
|
|
4751
5874
|
const segmenter = await $c.createFromOptions(fileset2, {
|
|
4752
5875
|
baseOptions: {
|
|
4753
|
-
|
|
5876
|
+
modelAssetBuffer: await modelBytes(model),
|
|
4754
5877
|
// MediaPipe's GPU delegate runs on WebGL2, which Chrome, Safari and Firefox all
|
|
4755
5878
|
// have. That is why mediakit picked MediaPipe over ONNX Runtime Web at P1: ORT's
|
|
4756
5879
|
// fast path is WebGPU, which Firefox only has on Windows.
|
|
@@ -4808,7 +5931,7 @@
|
|
|
4808
5931
|
async function createFaceDetector(canvas, maxFaces, delegate = "GPU", mode = "VIDEO") {
|
|
4809
5932
|
const fileset2 = await visionFileset();
|
|
4810
5933
|
const landmarker = await yc.createFromOptions(fileset2, {
|
|
4811
|
-
baseOptions: {
|
|
5934
|
+
baseOptions: { modelAssetBuffer: await modelBytes("face"), delegate },
|
|
4812
5935
|
canvas,
|
|
4813
5936
|
runningMode: mode,
|
|
4814
5937
|
// As many as the core tracks: one landmark pass returns them all, so the cost of a second
|
|
@@ -4847,11 +5970,15 @@
|
|
|
4847
5970
|
constructor(on2) {
|
|
4848
5971
|
this.on = on2;
|
|
4849
5972
|
this.worker = new Worker(DETECT_WORKER_URL);
|
|
5973
|
+
this.unserve = serveResources(this.worker);
|
|
4850
5974
|
this.worker.onerror = (e2) => this.failAll(e2.message || "detect worker failed to load");
|
|
4851
|
-
this.worker.onmessage = (e2) =>
|
|
5975
|
+
this.worker.onmessage = (e2) => {
|
|
5976
|
+
if (!isAssetsMessage(e2.data)) this.receive(e2.data);
|
|
5977
|
+
};
|
|
4852
5978
|
}
|
|
4853
5979
|
on;
|
|
4854
5980
|
worker;
|
|
5981
|
+
unserve;
|
|
4855
5982
|
ready = /* @__PURE__ */ new Map();
|
|
4856
5983
|
up = /* @__PURE__ */ new Set();
|
|
4857
5984
|
busy = /* @__PURE__ */ new Set();
|
|
@@ -4880,6 +6007,7 @@
|
|
|
4880
6007
|
}
|
|
4881
6008
|
close() {
|
|
4882
6009
|
this.send({ t: "close" });
|
|
6010
|
+
this.unserve();
|
|
4883
6011
|
this.worker = null;
|
|
4884
6012
|
for (const kind of [...this.ready.keys()]) this.settle(kind, new Error("closed"));
|
|
4885
6013
|
this.up.clear();
|
|
@@ -4934,6 +6062,7 @@
|
|
|
4934
6062
|
}
|
|
4935
6063
|
failAll(reason) {
|
|
4936
6064
|
this.worker?.terminate();
|
|
6065
|
+
this.unserve();
|
|
4937
6066
|
this.worker = null;
|
|
4938
6067
|
for (const kind of [...this.ready.keys()]) this.settle(kind, new Error(reason));
|
|
4939
6068
|
for (const kind of [...this.up]) this.fail(kind, reason);
|
|
@@ -4952,18 +6081,21 @@
|
|
|
4952
6081
|
if (execLoaded) return execLoaded;
|
|
4953
6082
|
execLoaded = (async () => {
|
|
4954
6083
|
if (globalThis.Go) return;
|
|
6084
|
+
const execUrl = await resourceUrl("wasm_exec.js").catch((err) => {
|
|
6085
|
+
throw new Error(`mediakit: cannot load ${EXEC_URL} (${err instanceof Error ? err.message : String(err)})`);
|
|
6086
|
+
});
|
|
4955
6087
|
const workerLoad = globalThis.importScripts;
|
|
4956
6088
|
if (typeof workerLoad === "function") {
|
|
4957
|
-
workerLoad.call(globalThis,
|
|
6089
|
+
workerLoad.call(globalThis, execUrl);
|
|
4958
6090
|
} else if (typeof document === "undefined") {
|
|
4959
6091
|
await import(
|
|
4960
6092
|
/* webpackIgnore: true */
|
|
4961
|
-
|
|
6093
|
+
execUrl
|
|
4962
6094
|
);
|
|
4963
6095
|
} else {
|
|
4964
6096
|
await new Promise((resolve, reject) => {
|
|
4965
6097
|
const el = document.createElement("script");
|
|
4966
|
-
el.src =
|
|
6098
|
+
el.src = execUrl;
|
|
4967
6099
|
el.async = true;
|
|
4968
6100
|
el.onload = () => resolve();
|
|
4969
6101
|
el.onerror = () => reject(new Error(`mediakit: cannot load ${EXEC_URL}`));
|
|
@@ -4977,13 +6109,12 @@
|
|
|
4977
6109
|
function compileModule() {
|
|
4978
6110
|
if (compiled) return compiled;
|
|
4979
6111
|
compiled = (async () => {
|
|
4980
|
-
const
|
|
4981
|
-
if (!res.ok) {
|
|
6112
|
+
const bytes = await resourceBytes("mediakit.wasm").catch((err) => {
|
|
4982
6113
|
throw new Error(
|
|
4983
|
-
`mediakit: cannot load ${WASM_URL} (
|
|
6114
|
+
`mediakit: cannot load ${WASM_URL} (${err instanceof Error ? err.message : String(err)}) - the host has to serve this package's dist/ under /mediakit/`
|
|
4984
6115
|
);
|
|
4985
|
-
}
|
|
4986
|
-
return WebAssembly.
|
|
6116
|
+
});
|
|
6117
|
+
return WebAssembly.compile(bytes);
|
|
4987
6118
|
})();
|
|
4988
6119
|
return compiled;
|
|
4989
6120
|
}
|
|
@@ -5361,10 +6492,10 @@
|
|
|
5361
6492
|
const name = String(gl.getParameter(info ? info.UNMASKED_RENDERER_WEBGL : gl.RENDERER) ?? "unknown");
|
|
5362
6493
|
return gl.isContextLost() ? `${name}, context lost` : name;
|
|
5363
6494
|
}
|
|
5364
|
-
function sourceSize(
|
|
5365
|
-
if (
|
|
5366
|
-
if (typeof HTMLVideoElement !== "undefined" &&
|
|
5367
|
-
const s2 =
|
|
6495
|
+
function sourceSize(source2) {
|
|
6496
|
+
if (source2 instanceof VideoFrame) return [source2.displayWidth, source2.displayHeight];
|
|
6497
|
+
if (typeof HTMLVideoElement !== "undefined" && source2 instanceof HTMLVideoElement) return [source2.videoWidth, source2.videoHeight];
|
|
6498
|
+
const s2 = source2;
|
|
5368
6499
|
return [s2.width, s2.height];
|
|
5369
6500
|
}
|
|
5370
6501
|
function tileGrid(sw, sh2) {
|
|
@@ -5652,7 +6783,7 @@
|
|
|
5652
6783
|
}
|
|
5653
6784
|
if (effects.whitenStyle === "pink" && this.skinLutState === "none") {
|
|
5654
6785
|
this.skinLutState = "loading";
|
|
5655
|
-
|
|
6786
|
+
resourceBytes("whiten_pink.png").then((b2) => createImageBitmap(new Blob([b2], { type: "image/png" }), { colorSpaceConversion: "none", premultiplyAlpha: "none" })).then((bmp) => {
|
|
5656
6787
|
if (this.closed) {
|
|
5657
6788
|
bmp.close();
|
|
5658
6789
|
return;
|
|
@@ -5880,7 +7011,7 @@
|
|
|
5880
7011
|
* Sends the detect worker a downscaled copy, never a clone: a camera frame held through a slow
|
|
5881
7012
|
* detection drains Chrome's few capture buffers and throttles the camera itself.
|
|
5882
7013
|
*/
|
|
5883
|
-
requestRemote(want,
|
|
7014
|
+
requestRemote(want, source2, timestampUs) {
|
|
5884
7015
|
const lockstep = want === "face" && this.syncing();
|
|
5885
7016
|
const kind = lockstep ? this.freeFaceWorker() : want;
|
|
5886
7017
|
if (!kind) return;
|
|
@@ -5891,16 +7022,16 @@
|
|
|
5891
7022
|
let followed = null;
|
|
5892
7023
|
let scanned = null;
|
|
5893
7024
|
try {
|
|
5894
|
-
const copyable =
|
|
7025
|
+
const copyable = source2 instanceof VideoFrame || source2 instanceof OffscreenCanvas;
|
|
5895
7026
|
if (copyable) {
|
|
5896
|
-
this.srcW =
|
|
5897
|
-
this.srcH =
|
|
7027
|
+
this.srcW = source2 instanceof VideoFrame ? source2.displayWidth : source2.width;
|
|
7028
|
+
this.srcH = source2 instanceof VideoFrame ? source2.displayHeight : source2.height;
|
|
5898
7029
|
}
|
|
5899
7030
|
if (copyable && (kind === "face" || kind === "faceExtra")) {
|
|
5900
|
-
followed = this.followCopy(
|
|
5901
|
-
if (!followed) scanned = this.scanCopy(
|
|
7031
|
+
followed = this.followCopy(source2, timestampUs);
|
|
7032
|
+
if (!followed) scanned = this.scanCopy(source2, timestampUs);
|
|
5902
7033
|
}
|
|
5903
|
-
frame = new VideoFrame(followed ?? scanned ?? (copyable ? this.detectCopy(
|
|
7034
|
+
frame = new VideoFrame(followed ?? scanned ?? (copyable ? this.detectCopy(source2, timestampUs) : source2), {
|
|
5904
7035
|
timestamp: timestampUs
|
|
5905
7036
|
});
|
|
5906
7037
|
} catch {
|
|
@@ -5913,11 +7044,11 @@
|
|
|
5913
7044
|
if (kind !== "face" && kind !== "faceExtra" && this.remoteMaskKind() === "merged") this.noteThumb(timestampUs);
|
|
5914
7045
|
} else if (followed || scanned) this.faceCrops.delete(timestampUs);
|
|
5915
7046
|
}
|
|
5916
|
-
followCopy(
|
|
7047
|
+
followCopy(source2, timestampUs) {
|
|
5917
7048
|
const box = this.followBox;
|
|
5918
7049
|
if (!box || timestampUs - this.followTs > FOLLOW_STALE_US) return null;
|
|
5919
|
-
const sw =
|
|
5920
|
-
const sh2 =
|
|
7050
|
+
const sw = source2 instanceof VideoFrame ? source2.displayWidth : source2.width;
|
|
7051
|
+
const sh2 = source2 instanceof VideoFrame ? source2.displayHeight : source2.height;
|
|
5921
7052
|
const want = Math.max(FOLLOW_MIN_SIDE, FOLLOW_MARGIN * (1 + this.followMiss) * Math.max(box[2] * sw, box[3] * sh2));
|
|
5922
7053
|
if (want > Math.min(sw, sh2) && this.followFloats > FACE_FLOATS) return null;
|
|
5923
7054
|
if (!this.followAll && this.followTurn++ % FOLLOW_REFRESH === FOLLOW_REFRESH - 1) {
|
|
@@ -5933,24 +7064,24 @@
|
|
|
5933
7064
|
[x2, y2, side] = kept;
|
|
5934
7065
|
} else this.followCrop = [x2, y2, side];
|
|
5935
7066
|
this.followCanvas ??= new OffscreenCanvas(FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5936
|
-
this.followCanvas.getContext("2d").drawImage(
|
|
7067
|
+
this.followCanvas.getContext("2d").drawImage(source2, x2, y2, side, side, 0, 0, FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5937
7068
|
const tight = Math.min(side, Math.max(FOLLOW_MIN_SIDE, FOLLOW_RETRY * Math.max(box[2] * sw, box[3] * sh2)));
|
|
5938
7069
|
this.followRetry = [((box[0] + box[2] / 2) * sw - x2) / side, ((box[1] + box[3] / 2) * sh2 - y2) / side, tight / side];
|
|
5939
7070
|
this.faceCrops.set(timestampUs, [x2 / sw, y2 / sh2, side / sw, side / sh2]);
|
|
5940
7071
|
return this.followCanvas;
|
|
5941
7072
|
}
|
|
5942
|
-
scanCopy(
|
|
7073
|
+
scanCopy(source2, timestampUs) {
|
|
5943
7074
|
if (this.faceSeen) return null;
|
|
5944
7075
|
const turn = this.scanTurn++;
|
|
5945
7076
|
if (turn % 2 === 0) return null;
|
|
5946
|
-
const sw =
|
|
5947
|
-
const sh2 =
|
|
7077
|
+
const sw = source2 instanceof VideoFrame ? source2.displayWidth : source2.width;
|
|
7078
|
+
const sh2 = source2 instanceof VideoFrame ? source2.displayHeight : source2.height;
|
|
5948
7079
|
const { side, step, nx, ny } = tileGrid(sw, sh2);
|
|
5949
7080
|
const at2 = (this.scanFrom + (turn >> 1)) % (nx * ny);
|
|
5950
7081
|
const x2 = Math.max(0, Math.min(sw - side, Math.round(at2 % nx * step)));
|
|
5951
7082
|
const y2 = Math.max(0, Math.min(sh2 - side, Math.round(Math.floor(at2 / nx) * step)));
|
|
5952
7083
|
this.followCanvas ??= new OffscreenCanvas(FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5953
|
-
this.followCanvas.getContext("2d").drawImage(
|
|
7084
|
+
this.followCanvas.getContext("2d").drawImage(source2, x2, y2, side, side, 0, 0, FOLLOW_SIDE, FOLLOW_SIDE);
|
|
5954
7085
|
this.faceCrops.set(timestampUs, [x2 / sw, y2 / sh2, side / sw, side / sh2]);
|
|
5955
7086
|
this.probes.add(timestampUs);
|
|
5956
7087
|
return this.followCanvas;
|
|
@@ -6062,9 +7193,9 @@
|
|
|
6062
7193
|
});
|
|
6063
7194
|
}
|
|
6064
7195
|
/** Face and mask asked on the same frame share one draw. */
|
|
6065
|
-
detectCopy(
|
|
6066
|
-
const sw =
|
|
6067
|
-
const sh2 =
|
|
7196
|
+
detectCopy(source2, timestampUs) {
|
|
7197
|
+
const sw = source2 instanceof VideoFrame ? source2.displayWidth : source2.width;
|
|
7198
|
+
const sh2 = source2 instanceof VideoFrame ? source2.displayHeight : source2.height;
|
|
6068
7199
|
const s2 = Math.min(1, 512 / Math.max(sw, sh2));
|
|
6069
7200
|
const w2 = Math.max(1, Math.round(sw * s2));
|
|
6070
7201
|
const h2 = Math.max(1, Math.round(sh2 * s2));
|
|
@@ -6073,7 +7204,7 @@
|
|
|
6073
7204
|
this.detectCanvasTs = -1;
|
|
6074
7205
|
}
|
|
6075
7206
|
if (this.detectCanvasTs !== timestampUs) {
|
|
6076
|
-
this.detectCanvas.getContext("2d").drawImage(
|
|
7207
|
+
this.detectCanvas.getContext("2d").drawImage(source2, 0, 0, w2, h2);
|
|
6077
7208
|
this.detectCanvasTs = timestampUs;
|
|
6078
7209
|
}
|
|
6079
7210
|
return this.detectCanvas;
|
|
@@ -6134,7 +7265,7 @@
|
|
|
6134
7265
|
* LiveKit's reference implementation requires a `VideoFrame` even on its fallback path, so
|
|
6135
7266
|
* it constructs one only to destroy it again.
|
|
6136
7267
|
*/
|
|
6137
|
-
render(
|
|
7268
|
+
render(source2, timestampUs) {
|
|
6138
7269
|
if (this.closed || this.lostDuring) return false;
|
|
6139
7270
|
if (this.lastHeld) this.spare.push(this.lastHeld);
|
|
6140
7271
|
this.lastHeld = null;
|
|
@@ -6164,19 +7295,19 @@
|
|
|
6164
7295
|
this.thumbs.clear();
|
|
6165
7296
|
}
|
|
6166
7297
|
this.prevTs = timestampUs;
|
|
6167
|
-
if (!this.syncing() || typeof ImageData !== "undefined" &&
|
|
7298
|
+
if (!this.syncing() || typeof ImageData !== "undefined" && source2 instanceof ImageData) {
|
|
6168
7299
|
this.releaseHeld();
|
|
6169
7300
|
this.renderedTs = timestampUs;
|
|
6170
|
-
return this.draw(
|
|
7301
|
+
return this.draw(source2, timestampUs, false);
|
|
6171
7302
|
}
|
|
6172
7303
|
this.ensureFaceExtra();
|
|
6173
|
-
this.requestRemote("face",
|
|
7304
|
+
this.requestRemote("face", source2, timestampUs);
|
|
6174
7305
|
const remoteMask = this.remoteMaskKind();
|
|
6175
7306
|
if (remoteMask === "merged") {
|
|
6176
|
-
this.requestRemote("person",
|
|
6177
|
-
this.requestRemote("mask",
|
|
6178
|
-
} else if (remoteMask) this.requestRemote(remoteMask,
|
|
6179
|
-
this.queue.push({ c: this.hold(
|
|
7307
|
+
this.requestRemote("person", source2, timestampUs);
|
|
7308
|
+
this.requestRemote("mask", source2, timestampUs);
|
|
7309
|
+
} else if (remoteMask) this.requestRemote(remoteMask, source2, timestampUs);
|
|
7310
|
+
this.queue.push({ c: this.hold(source2), ts: timestampUs });
|
|
6180
7311
|
while (this.queue.length > MAX_HOLD + 1) this.faceAsked.delete(this.spareFront());
|
|
6181
7312
|
if (this.queue.length <= MAX_HOLD && !this.faceReady(this.queue[0].ts)) {
|
|
6182
7313
|
this.swallowed = true;
|
|
@@ -6343,15 +7474,15 @@
|
|
|
6343
7474
|
syncing() {
|
|
6344
7475
|
return this.syncFaces && this.faceRemote && needsFace(this.effects);
|
|
6345
7476
|
}
|
|
6346
|
-
hold(
|
|
6347
|
-
const [w2, h2] = sourceSize(
|
|
7477
|
+
hold(source2) {
|
|
7478
|
+
const [w2, h2] = sourceSize(source2);
|
|
6348
7479
|
let c2 = this.spare.pop();
|
|
6349
7480
|
while (c2 && (c2.width !== w2 || c2.height !== h2)) c2 = this.spare.pop();
|
|
6350
7481
|
c2 ??= new OffscreenCanvas(w2, h2);
|
|
6351
|
-
c2.getContext("2d").drawImage(
|
|
7482
|
+
c2.getContext("2d").drawImage(source2, 0, 0, w2, h2);
|
|
6352
7483
|
return c2;
|
|
6353
7484
|
}
|
|
6354
|
-
draw(
|
|
7485
|
+
draw(source2, timestampUs, faceDone) {
|
|
6355
7486
|
const t0 = performance.now();
|
|
6356
7487
|
const [lo2, hi2] = splitMicros(timestampUs);
|
|
6357
7488
|
const needSeg = this.core.decide(lo2, hi2, this.prevTotalMs, this.prevSegMs) === 1;
|
|
@@ -6365,8 +7496,8 @@
|
|
|
6365
7496
|
const remoteMask = this.remoteMaskKind();
|
|
6366
7497
|
if (remoteMask === "merged") {
|
|
6367
7498
|
if (!faceDone) {
|
|
6368
|
-
this.requestRemote("person",
|
|
6369
|
-
this.requestRemote("mask",
|
|
7499
|
+
this.requestRemote("person", source2, timestampUs);
|
|
7500
|
+
this.requestRemote("mask", source2, timestampUs);
|
|
6370
7501
|
}
|
|
6371
7502
|
const tex = this.mergedTex ?? this.personMaskTex;
|
|
6372
7503
|
if (tex) {
|
|
@@ -6380,7 +7511,7 @@
|
|
|
6380
7511
|
segMs = this.personWorkerMs;
|
|
6381
7512
|
}
|
|
6382
7513
|
}
|
|
6383
|
-
} else if (remoteMask && !faceDone) this.requestRemote(remoteMask,
|
|
7514
|
+
} else if (remoteMask && !faceDone) this.requestRemote(remoteMask, source2, timestampUs);
|
|
6384
7515
|
if (remoteMask === "person") {
|
|
6385
7516
|
if (this.personMaskTex) {
|
|
6386
7517
|
served = true;
|
|
@@ -6411,7 +7542,7 @@
|
|
|
6411
7542
|
if (!this.remoteMaskComing(this.filter, this.effects)) void this.ensureSegmenter();
|
|
6412
7543
|
} else {
|
|
6413
7544
|
const s0 = performance.now();
|
|
6414
|
-
mask = seg.segment(
|
|
7545
|
+
mask = seg.segment(source2, timestampUs / 1e3);
|
|
6415
7546
|
segMs = performance.now() - s0;
|
|
6416
7547
|
this.used = seg;
|
|
6417
7548
|
maskKind = classes ? 1 : 0;
|
|
@@ -6420,7 +7551,7 @@
|
|
|
6420
7551
|
if (faceDone) {
|
|
6421
7552
|
faceMs = this.faceWorkerMs;
|
|
6422
7553
|
} else if (this.faceRemote && needsFace(this.effects)) {
|
|
6423
|
-
this.requestRemote("face",
|
|
7554
|
+
this.requestRemote("face", source2, timestampUs);
|
|
6424
7555
|
faceMs = this.faceWorkerMs;
|
|
6425
7556
|
} else if (this.core.decideFace() === 1) {
|
|
6426
7557
|
if (this.faceRemote) {
|
|
@@ -6429,7 +7560,7 @@
|
|
|
6429
7560
|
void this.ensureFace();
|
|
6430
7561
|
} else if (this.lmBuf) {
|
|
6431
7562
|
const f0 = performance.now();
|
|
6432
|
-
const n2 = this.face.detect(
|
|
7563
|
+
const n2 = this.face.detect(source2, timestampUs / 1e3, this.lmBuf);
|
|
6433
7564
|
faceMs = performance.now() - f0;
|
|
6434
7565
|
this.core.setFace(lo2, hi2, n2);
|
|
6435
7566
|
this.faceTs = timestampUs;
|
|
@@ -6437,7 +7568,7 @@
|
|
|
6437
7568
|
}
|
|
6438
7569
|
if (mask) this.maskUsedTs = maskTs;
|
|
6439
7570
|
const [maskLo, maskHi] = splitMicros(maskTs);
|
|
6440
|
-
const glCalls = this.core.process(
|
|
7571
|
+
const glCalls = this.core.process(source2, mask, this.background, maskKind, maskLo, maskHi);
|
|
6441
7572
|
if (mask) this.used?.release();
|
|
6442
7573
|
this.used = null;
|
|
6443
7574
|
this.noteLinking();
|
|
@@ -6606,6 +7737,7 @@
|
|
|
6606
7737
|
}
|
|
6607
7738
|
|
|
6608
7739
|
// src/worker/entry.ts
|
|
7740
|
+
connectResources(self);
|
|
6609
7741
|
var post = (m2) => self.postMessage(m2);
|
|
6610
7742
|
var engine = null;
|
|
6611
7743
|
var abort = null;
|
|
@@ -6732,6 +7864,7 @@
|
|
|
6732
7864
|
engine = null;
|
|
6733
7865
|
}
|
|
6734
7866
|
self.onmessage = (e2) => {
|
|
7867
|
+
if (isAssetsMessage(e2.data)) return;
|
|
6735
7868
|
const m2 = e2.data;
|
|
6736
7869
|
try {
|
|
6737
7870
|
if (m2.t === "start" || m2.t === "startTrack") {
|