@meshinspector/meshlib-mt 3.1.3-322 → 3.1.3-429

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,6 +45,21 @@ Without them `SharedArrayBuffer` is unavailable and the module will fail to init
45
45
  use the single-threaded [`@meshinspector/meshlib`](https://www.npmjs.com/package/@meshinspector/meshlib)
46
46
  package in that case.
47
47
 
48
+ For a Vite dev server, set them in `vite.config.js` (and make sure whatever hosts the production
49
+ build sends them too; `vite-plugin-cross-origin-isolation` can stamp them for `vite preview`):
50
+
51
+ ```js
52
+ // vite.config.js
53
+ export default {
54
+ server: {
55
+ headers: {
56
+ 'Cross-Origin-Opener-Policy': 'same-origin',
57
+ 'Cross-Origin-Embedder-Policy': 'require-corp',
58
+ },
59
+ },
60
+ };
61
+ ```
62
+
48
63
  ## Usage
49
64
 
50
65
  The default export is an async factory. Await it once to get the module instance, then call
@@ -65,18 +80,32 @@ const indices = new Uint32Array([
65
80
  3, 6, 2, 3, 7, 6, 0, 4, 7, 0, 7, 3, 1, 2, 6, 1, 6, 5,
66
81
  ]);
67
82
 
68
- const coords = ml.VertCoords.fromArray(positions);
69
- const tris = ml.Triangulation.fromArray(indices);
70
- const mesh = ml.Mesh.fromTriangles(coords, tris);
83
+ using coords = ml.VertCoords.fromArray(positions);
84
+ using tris = ml.Triangulation.fromArray(indices);
85
+ using mesh = ml.Mesh.fromTriangles(coords, tris);
71
86
 
72
87
  console.log('volume =', mesh.volume()); // ~8
88
+ // `using` frees these WebAssembly-backed objects automatically at the end of scope
89
+ ```
73
90
 
74
- // Objects are backed by WebAssembly memory free them explicitly.
75
- coords.delete();
76
- tris.delete();
77
- mesh.delete();
91
+ > `using` requires Node.js 24+ or a current browser. On older runtimes, call `.delete()`
92
+ > instead — see [Memory management](#memory-management).
93
+
94
+ ## Using with bundlers
95
+
96
+ Bundlers (Vite, webpack, Rollup) hash and relocate the sidecar `meshlib-mt.wasm`, so the module
97
+ can't locate it on its own. Import the wasm as an asset URL and hand it to the loader via
98
+ `locateFile`:
99
+
100
+ ```js
101
+ import createMeshLib from '@meshinspector/meshlib-mt';
102
+ import wasmUrl from '@meshinspector/meshlib-mt/meshlib-mt.wasm?url';
103
+
104
+ const ml = await createMeshLib( { locateFile: () => wasmUrl } );
78
105
  ```
79
106
 
107
+ The page must also be [cross-origin isolated](#browser-requirements-cross-origin-isolation).
108
+
80
109
  ## TypeScript
81
110
 
82
111
  The package ships type definitions, so `createMeshLib` and the whole module API are typed with
@@ -86,15 +115,47 @@ no extra setup:
86
115
  import createMeshLib, { type Mesh } from '@meshinspector/meshlib-mt';
87
116
 
88
117
  const ml = await createMeshLib();
89
- const mesh: Mesh = ml.Mesh.fromTriangles(coords, tris);
118
+ using mesh: Mesh = ml.Mesh.fromTriangles(coords, tris);
90
119
  const { valid, distSq } = ml.findProjection(point, mesh);
91
120
  ```
92
121
 
93
122
  ## Memory management
94
123
 
95
124
  Values returned from the API (meshes, bit sets, settings, result objects, …) hold
96
- WebAssembly memory that the JavaScript garbage collector does not reclaim. Call
97
- `.delete()` on them when you are done to avoid leaks.
125
+ WebAssembly memory that the JavaScript garbage collector does not reclaim, so each one
126
+ must be freed explicitly.
127
+
128
+ The preferred way is JavaScript's explicit resource management: declare a handle with
129
+ `using` and it is freed automatically when its scope ends — even if an exception is thrown.
130
+
131
+ ```js
132
+ using mesh = ml.Mesh.fromTriangles(coords, tris);
133
+ // ... use mesh; it is freed at the end of this scope
134
+ ```
135
+
136
+ When the number of handles is dynamic (for example built in a loop), collect them in a
137
+ `DisposableStack`, which frees everything it holds, in reverse order, at the end of the scope:
138
+
139
+ ```js
140
+ using stack = new DisposableStack();
141
+ for (const path of inputPaths) {
142
+ const cloud = stack.use(ml.PointsLoad.fromAnySupportedFormat(path));
143
+ // ... use cloud
144
+ }
145
+ // every handle passed to stack.use(...) is freed here
146
+ ```
147
+
148
+ `using` and `DisposableStack` are part of JavaScript's Explicit Resource Management,
149
+ available in Node.js 24+ and current browsers. On older runtimes and browsers, call
150
+ `.delete()` on each object when you are done instead:
151
+
152
+ ```js
153
+ const mesh = ml.Mesh.fromTriangles(coords, tris);
154
+ // ... use mesh
155
+ mesh.delete();
156
+ ```
157
+
158
+ See also, on MDN: [`using`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using) and [`DisposableStack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DisposableStack).
98
159
 
99
160
  ## License
100
161