@topolab/tda 0.0.0-stage → 0.3.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.
Files changed (90) hide show
  1. package/CHANGELOG.md +82 -0
  2. package/CITATION.cff +72 -0
  3. package/LICENSE +21 -0
  4. package/README.md +100 -2
  5. package/SECURITY.md +34 -0
  6. package/THIRD_PARTY_LICENSES.md +696 -0
  7. package/dist/build_info.json +12 -0
  8. package/dist/check.d.ts +118 -0
  9. package/dist/check.d.ts.map +1 -0
  10. package/dist/check.js +298 -0
  11. package/dist/check.js.map +1 -0
  12. package/dist/complex.d.ts +543 -0
  13. package/dist/complex.d.ts.map +1 -0
  14. package/dist/complex.js +712 -0
  15. package/dist/complex.js.map +1 -0
  16. package/dist/diagram.d.ts +116 -0
  17. package/dist/diagram.d.ts.map +1 -0
  18. package/dist/diagram.js +238 -0
  19. package/dist/diagram.js.map +1 -0
  20. package/dist/diagram_of.d.ts +17 -0
  21. package/dist/diagram_of.d.ts.map +1 -0
  22. package/dist/diagram_of.js +40 -0
  23. package/dist/diagram_of.js.map +1 -0
  24. package/dist/experimental.d.ts +14 -0
  25. package/dist/experimental.d.ts.map +1 -0
  26. package/dist/experimental.js +13 -0
  27. package/dist/experimental.js.map +1 -0
  28. package/dist/flat.d.ts +64 -0
  29. package/dist/flat.d.ts.map +1 -0
  30. package/dist/flat.js +158 -0
  31. package/dist/flat.js.map +1 -0
  32. package/dist/index.d.ts +38 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +29 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/ladder/tda_ladder.d.ts +223 -0
  37. package/dist/ladder/tda_ladder.js +1360 -0
  38. package/dist/ladder/tda_ladder_bg.wasm +0 -0
  39. package/dist/ladder/tda_ladder_bg.wasm.d.ts +43 -0
  40. package/dist/ladder.d.ts +306 -0
  41. package/dist/ladder.d.ts.map +1 -0
  42. package/dist/ladder.js +607 -0
  43. package/dist/ladder.js.map +1 -0
  44. package/dist/load.d.ts +53 -0
  45. package/dist/load.d.ts.map +1 -0
  46. package/dist/load.js +173 -0
  47. package/dist/load.js.map +1 -0
  48. package/dist/memory.d.ts +58 -0
  49. package/dist/memory.d.ts.map +1 -0
  50. package/dist/memory.js +2 -0
  51. package/dist/memory.js.map +1 -0
  52. package/dist/persistence.d.ts +349 -0
  53. package/dist/persistence.d.ts.map +1 -0
  54. package/dist/persistence.js +478 -0
  55. package/dist/persistence.js.map +1 -0
  56. package/dist/pkg/tda_wasm.d.ts +13 -0
  57. package/dist/pkg/tda_wasm.js +1900 -0
  58. package/dist/prepared.d.ts +62 -0
  59. package/dist/prepared.d.ts.map +1 -0
  60. package/dist/prepared.js +81 -0
  61. package/dist/prepared.js.map +1 -0
  62. package/dist/source.d.ts +43 -0
  63. package/dist/source.d.ts.map +1 -0
  64. package/dist/source.js +99 -0
  65. package/dist/source.js.map +1 -0
  66. package/dist/tda.wasm +0 -0
  67. package/dist/types.d.ts +146 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/types.js +2 -0
  70. package/dist/types.js.map +1 -0
  71. package/dist/vineyard.d.ts +278 -0
  72. package/dist/vineyard.d.ts.map +1 -0
  73. package/dist/vineyard.js +236 -0
  74. package/dist/vineyard.js.map +1 -0
  75. package/package.json +77 -4
  76. package/src/check.ts +338 -0
  77. package/src/complex.ts +1179 -0
  78. package/src/diagram.ts +299 -0
  79. package/src/diagram_of.ts +40 -0
  80. package/src/experimental.ts +13 -0
  81. package/src/flat.ts +204 -0
  82. package/src/index.ts +155 -0
  83. package/src/ladder.ts +903 -0
  84. package/src/load.ts +201 -0
  85. package/src/memory.ts +58 -0
  86. package/src/persistence.ts +752 -0
  87. package/src/prepared.ts +111 -0
  88. package/src/source.ts +132 -0
  89. package/src/types.ts +163 -0
  90. package/src/vineyard.ts +463 -0
package/src/load.ts ADDED
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Loading and unloading the WebAssembly module, and the bridge every
3
+ * function calls it through (internal).
4
+ */
5
+ // @ts-ignore - the module's glue (wasm-bindgen --target web), built with the module
6
+ import { __tda_create } from '../dist/pkg/tda_wasm.js';
7
+ import { compileSource } from './source.js';
8
+ import type { LoadSource } from './source.js';
9
+
10
+ export type { LoadSource };
11
+
12
+ /** The module's exports: called through {@link call}. */
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ type Engine = any;
15
+
16
+ /** The load in progress or done, `null` when none is. */
17
+ let ready: Promise<void> | null = null;
18
+ /** The `source` the load in {@link ready} was given (`undefined` for the default). */
19
+ let readySource: LoadSource | undefined;
20
+ /** Settles when the last load started has settled: a new load waits for it, so two never instantiate at once. */
21
+ let settled: Promise<unknown> = Promise.resolve();
22
+ /** The loaded module, `undefined` when none is. */
23
+ let engine: Engine | undefined;
24
+ /** The loaded module's `version()`, read once at load (a string costs a UTF-8 decode per call). */
25
+ let engineVersion = '';
26
+ /** Incremented by every `unload()`, so a load it overtook drops its instance instead of keeping it. */
27
+ let generation = 0;
28
+ /**
29
+ * Owners of experimental native handles; ordinary calls retain no state. A
30
+ * cleanup gets `trapped = true` when the instance trapped: it must then
31
+ * drop its handle without calling into the instance.
32
+ */
33
+ type Cleanup = (trapped: boolean) => void;
34
+ const unloadCleanups = new Set<Cleanup>();
35
+
36
+ /** @internal Register a native owner's cleanup, returning its unregister function. */
37
+ export function onUnload(cleanup: Cleanup): () => void {
38
+ unloadCleanups.add(cleanup);
39
+ return () => { unloadCleanups.delete(cleanup); };
40
+ }
41
+
42
+ /**
43
+ * Instantiates `module` with a glue instance of its own (`__tda_create`, a
44
+ * factory around wasm-bindgen's glue), so the glue
45
+ * reads no global (`fetch`, `Response`, `Request`) and every load has its own
46
+ * module object; a failure rejects the load.
47
+ */
48
+ async function instantiate(module: WebAssembly.Module): Promise<Engine> {
49
+ const glue = __tda_create();
50
+ const instance = await WebAssembly.instantiate(module, glue.imports());
51
+ glue.finalize(instance, module);
52
+ glue.exports.__oom_on_recover(() => {
53
+ const cleanups = [...unloadCleanups];
54
+ unloadCleanups.clear();
55
+ for (const cleanup of cleanups) {
56
+ try { cleanup(true); } catch { /* Recovery must invalidate every owner. */ }
57
+ }
58
+ });
59
+ return glue.exports;
60
+ }
61
+
62
+ /**
63
+ * Load the module. Without `source` it is read from `tda.wasm` next to the
64
+ * package's code (`@topolab/tda/tda.wasm`): from the file in Node, by
65
+ * `fetch` in a browser, where bundlers that understand
66
+ * `new URL(…, import.meta.url)` resolve it. Nothing is written to
67
+ * `globalThis`.
68
+ *
69
+ * Once the module is loaded, `load` returns the same resolved promise and
70
+ * ignores `source`. While a load is in progress, a call with the same
71
+ * `source` (the same value, or none both times) shares that load and its
72
+ * outcome. A call with another `source` waits for the load in progress: if
73
+ * it succeeds, the call resolves too, without using its own `source` (the
74
+ * module is loaded once); if it fails, the call does not share that failure
75
+ * but loads from its own `source`. Two loads never instantiate at once. A
76
+ * failed load is not kept, so a later call can retry with a corrected
77
+ * source; {@link unload} rejects every load in progress.
78
+ */
79
+ export function load(source?: LoadSource): Promise<void> {
80
+ if (ready && engine === undefined && !Object.is(source, readySource)) {
81
+ const mine = generation;
82
+ const overtaken = () => new Error('@topolab/tda: unload() was called while load() was in progress');
83
+ return ready.then(
84
+ () => {
85
+ if (mine !== generation) throw overtaken();
86
+ },
87
+ () => {
88
+ if (mine !== generation) throw overtaken();
89
+ return load(source);
90
+ }
91
+ );
92
+ }
93
+ if (!ready) {
94
+ readySource = source;
95
+ const mine = generation;
96
+ const p = settled
97
+ .then(() =>
98
+ compileSource(source, () => new URL('./tda.wasm', import.meta.url), {
99
+ exports: ['version', 'persistenceFlat', '__oom_record'],
100
+ file: 'tda.wasm',
101
+ pkg: '@topolab/tda'
102
+ })
103
+ )
104
+ .then(instantiate)
105
+ .then(
106
+ (m: Engine) => {
107
+ if (mine !== generation) {
108
+ // unload() ran while this load was in flight: it wins
109
+ throw new Error('@topolab/tda: unload() was called while load() was in progress');
110
+ }
111
+ engine = m;
112
+ engineVersion = m.version() as string;
113
+ },
114
+ (e: unknown) => {
115
+ if (mine === generation) ready = null;
116
+ throw e;
117
+ }
118
+ );
119
+ ready = p;
120
+ settled = p.catch(() => undefined);
121
+ }
122
+ return ready;
123
+ }
124
+
125
+ /**
126
+ * Drop the module, so that its memory (which wasm never shrinks) can be
127
+ * reclaimed once nothing else refers to it. Results already returned are
128
+ * copies and stay valid. Functions that need the module throw until
129
+ * {@link load} is called again; a load in progress rejects.
130
+ */
131
+ export function unload(): void {
132
+ drop(false);
133
+ }
134
+
135
+ /**
136
+ * Clears the loader's state first, so that nothing a cleanup does can leave
137
+ * the module loaded, then runs every cleanup; after a trap the cleanups are
138
+ * told not to call into the instance. Throws the first error a cleanup
139
+ * threw, once all have run, unless the instance trapped (then the trap is
140
+ * what the caller is told about).
141
+ */
142
+ function drop(trapped: boolean): void {
143
+ generation++;
144
+ ready = null;
145
+ engine = undefined;
146
+ const cleanups = [...unloadCleanups];
147
+ unloadCleanups.clear();
148
+ let failed = false;
149
+ let first: unknown;
150
+ for (const cleanup of cleanups) {
151
+ try {
152
+ cleanup(trapped);
153
+ } catch (error) {
154
+ if (!failed) first = error;
155
+ failed = true;
156
+ }
157
+ }
158
+ if (failed && !trapped) throw first;
159
+ }
160
+
161
+ /** Whether the module is loaded (a {@link load} has resolved and no {@link unload} followed). */
162
+ export function isLoaded(): boolean {
163
+ return engine !== undefined;
164
+ }
165
+
166
+ /** The package version the loaded module was built as (equal to the package's). */
167
+ export function version(): string {
168
+ module();
169
+ return engineVersion;
170
+ }
171
+
172
+ /** @internal The loaded module; throws when none is. */
173
+ export function module(): Engine {
174
+ if (engine === undefined) throw new Error('@topolab/tda is not loaded: await load() first');
175
+ return engine;
176
+ }
177
+
178
+ /** An engine message's leading routine name (`cech_complex: `), after an optional `invalid input: ` or `input too large: `. */
179
+ const ROUTINE = /^((?:invalid input|input too large): )?[a-z][a-z0-9]*(?:_[a-z0-9]+)+: /;
180
+
181
+ /**
182
+ * @internal Run `f` on the module for the API function `fn`. Every engine
183
+ * export handles traps and recovery in its generated glue; an engine
184
+ * refusal reaches the caller as the very error the module threw, its
185
+ * message naming `fn` where the engine names the Rust routine that refused.
186
+ */
187
+ export function call<T>(fn: string, f: (m: Engine) => T): T;
188
+ /** @internal Run `f` on the module, its errors as the module threw them. */
189
+ export function call<T>(f: (m: Engine) => T): T;
190
+ export function call<T>(a: string | ((m: Engine) => T), b?: (m: Engine) => T): T {
191
+ if (typeof a !== 'string') return a(module());
192
+ const m = module();
193
+ try {
194
+ return b!(m);
195
+ } catch (e) {
196
+ if (e instanceof Error && (e as { code?: unknown }).code === undefined && ROUTINE.test(e.message)) {
197
+ e.message = e.message.replace(ROUTINE, (_, head: string | undefined) => `${head ?? ''}${a}: `);
198
+ }
199
+ throw e;
200
+ }
201
+ }
package/src/memory.ts ADDED
@@ -0,0 +1,58 @@
1
+ /** A WebAssembly memory refusal; the next call needs no load(). */
2
+ export interface CapacityError extends RangeError {
3
+ readonly code: 'TOO_LARGE';
4
+ readonly details: CapacityDetails;
5
+ }
6
+
7
+ /** Numbers describe a failed buffer request and committed wasm memory. */
8
+ export interface CapacityDetails {
9
+ readonly function: string;
10
+ /** 'allocator' when the exact internal structure is unknown. */
11
+ readonly phase: string;
12
+ readonly requestedBytes: number;
13
+ /** Diagnostic, always `null`: committed pages include free allocator space, so the bytes in use are not known. */
14
+ readonly inUseBytes: null;
15
+ readonly memoryBytes: number;
16
+ readonly committedBytes: number;
17
+ /** Applicable ceiling: Rust Layout, reached wasm memory, module maximum, or documented JS conversion budget. */
18
+ readonly limitBytes: number;
19
+ /** Module maximum, even when the host refuses growth earlier. */
20
+ readonly declaredMaxBytes: number;
21
+ readonly allocationLimitBytes: number;
22
+ readonly input: Readonly<Record<string, number | readonly number[]>>;
23
+ readonly parameters: Readonly<Record<string, unknown>>;
24
+ /** True for a checked Rust Layout refusal before the allocator. */
25
+ readonly capacityOverflow?: boolean;
26
+ /** A conservative API budget for an extra JavaScript conversion buffer, not the host heap limit. */
27
+ readonly hostConversionBudgetBytes?: number;
28
+ readonly alignment?: number;
29
+ /** Diagnostic for bug reports (the module's internal function number); not part of the stable API and may change. */
30
+ readonly functionId?: number;
31
+ /** Diagnostic for bug reports (the module's internal phase number); not part of the stable API and may change. */
32
+ readonly phaseId?: number;
33
+ /** Diagnostic: how long the instance replacement took; not part of the stable API and may change. */
34
+ readonly recoveryMilliseconds?: number;
35
+ readonly elements?: number;
36
+ readonly elementBytes?: number;
37
+ /** Rounded down fresh-instance estimate, in estimateUnit; never an admission guarantee. */
38
+ readonly estimatedMaxInput?: number;
39
+ readonly estimatedInputShape?: readonly number[];
40
+ readonly estimateUnit?: string;
41
+ /** Growth law, failed-structure scaling, working-set model and limits, or omission reason. */
42
+ readonly estimateMethod?: string;
43
+ /** Measured capacity-benchmark success at the stated parameters, not a universal capacity. */
44
+ readonly capacityReference?: {
45
+ readonly size: number;
46
+ readonly limitBytes: number;
47
+ readonly unit: string;
48
+ readonly parameters: string;
49
+ };
50
+ readonly capacityEstimate?: {
51
+ /** A single-buffer ceiling, not a whole-input estimate. */
52
+ readonly maximumElements?: number;
53
+ /** Fresh-instance pairwise estimate at the original dimension. */
54
+ readonly maximumEqualSamples?: number;
55
+ readonly failingSamples?: number;
56
+ readonly method: string;
57
+ };
58
+ }