@meshinspector/meshlib-mt 3.1.3-337 → 3.1.4-297

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
@@ -20,14 +20,21 @@ npm install @meshinspector/meshlib-mt
20
20
 
21
21
  ## Use from CDN
22
22
 
23
- In the browser you can skip npm entirely and import the module directly:
23
+ In the browser you can load the module from the CDN instead of npm, but the browser refuses to run a worker
24
+ script from another origin, so pass the fetched module to the factory as a `Blob` and its worker pool starts
25
+ from a same-origin `blob:` URL:
24
26
 
25
27
  ```js
26
- // latest version
27
- import createMeshLib from 'https://js.meshlib.io/meshlib-mt/meshlib-mt.mjs';
28
+ // latest release
29
+ const url = 'https://js.meshlib.io/meshlib-mt/meshlib-mt.mjs';
28
30
 
29
- // or pin a specific version
30
- import createMeshLib from 'https://js.meshlib.io/meshlib-mt@1.2.3/meshlib-mt.mjs';
31
+ // or pin a release
32
+ // const url = 'https://js.meshlib.io/meshlib-mt@v1.2.3.456/meshlib-mt.mjs';
33
+
34
+ const { default: createMeshLib } = await import( url );
35
+ const blob = new Blob( [ await ( await fetch( url ) ).text() ], { type: 'text/javascript' } );
36
+
37
+ const ml = await createMeshLib( { mainScriptUrlOrBlob: blob } );
31
38
  ```
32
39
 
33
40
  ## Browser requirements: cross-origin isolation
@@ -65,6 +72,9 @@ export default {
65
72
  The default export is an async factory. Await it once to get the module instance, then call
66
73
  MeshLib functions on it:
67
74
 
75
+ > `using` requires Node.js 24+ or a current browser. On older runtimes, call `.delete()`
76
+ > instead — see [Memory management](#memory-management).
77
+
68
78
  ```js
69
79
  import createMeshLib from '@meshinspector/meshlib-mt';
70
80
 
@@ -80,51 +90,89 @@ const indices = new Uint32Array([
80
90
  3, 6, 2, 3, 7, 6, 0, 4, 7, 0, 7, 3, 1, 2, 6, 1, 6, 5,
81
91
  ]);
82
92
 
83
- const coords = ml.VertCoords.fromArray(positions);
84
- const tris = ml.Triangulation.fromArray(indices);
85
- const mesh = ml.Mesh.fromTriangles(coords, tris);
93
+ using coords = ml.VertCoords.fromArray(positions);
94
+ using tris = ml.Triangulation.fromArray(indices);
95
+ using mesh = ml.Mesh.fromTriangles(coords, tris);
86
96
 
87
97
  console.log('volume =', mesh.volume()); // ~8
88
-
89
- // Objects are backed by WebAssembly memory — free them explicitly.
90
- coords.delete();
91
- tris.delete();
92
- mesh.delete();
98
+ // `using` frees these WebAssembly-backed objects automatically at the end of scope
93
99
  ```
94
100
 
95
101
  ## Using with bundlers
96
102
 
97
- Bundlers (Vite, webpack, Rollup) hash and relocate the sidecar `meshlib-mt.wasm`, so the module
98
- can't locate it on its own. Import the wasm as an asset URL and hand it to the loader via
99
- `locateFile`:
103
+ Vite 8 and webpack 5 resolve `meshlib-mt.wasm` from the module and emit it as an asset, so a plain `import`
104
+ needs no configuration. For other bundlers, such as esbuild or Rollup, import the wasm as an asset URL
105
+ and hand it to the loader via `locateFile`:
100
106
 
101
107
  ```js
102
108
  import createMeshLib from '@meshinspector/meshlib-mt';
103
- import wasmUrl from '@meshinspector/meshlib-mt/meshlib-mt.wasm?url';
109
+ import wasmUrl from '@meshinspector/meshlib-mt/meshlib-mt.wasm';
104
110
 
105
111
  const ml = await createMeshLib( { locateFile: () => wasmUrl } );
106
112
  ```
107
113
 
114
+ The bundler must treat `.wasm` files as static assets, so that the import resolves to the URL of the
115
+ emitted file; the option is usually called an asset or file loader. For example:
116
+
117
+ - **esbuild**: pass `--loader:.wasm=file`
118
+ - **Rollup**: add `@rollup/plugin-url` with `include: /\.wasm$/`
119
+ - **Parcel**: use the `url:` scheme on the import specifier: `import wasmUrl from 'url:@meshinspector/meshlib-mt/meshlib-mt.wasm';`
120
+
108
121
  The page must also be [cross-origin isolated](#browser-requirements-cross-origin-isolation).
109
122
 
110
123
  ## TypeScript
111
124
 
112
125
  The package ships type definitions, so `createMeshLib` and the whole module API are typed with
113
- no extra setup:
126
+ minimal setup:
114
127
 
115
128
  ```ts
116
129
  import createMeshLib, { type Mesh } from '@meshinspector/meshlib-mt';
117
130
 
118
131
  const ml = await createMeshLib();
119
- const mesh: Mesh = ml.Mesh.fromTriangles(coords, tris);
132
+ const mesh: Mesh = ml.Mesh.fromTriangles(coords, tris)!;
120
133
  const { valid, distSq } = ml.findProjection(point, mesh);
134
+ mesh.delete();
121
135
  ```
122
136
 
137
+ Also make sure to add `"type": "module"` to your package.json.
138
+
123
139
  ## Memory management
124
140
 
125
141
  Values returned from the API (meshes, bit sets, settings, result objects, …) hold
126
- WebAssembly memory that the JavaScript garbage collector does not reclaim. Call
127
- `.delete()` on them when you are done to avoid leaks.
142
+ WebAssembly memory that the JavaScript garbage collector does not reclaim, so each one
143
+ must be freed explicitly.
144
+
145
+ The preferred way is JavaScript's explicit resource management: declare a handle with
146
+ `using` and it is freed automatically when its scope ends — even if an exception is thrown.
147
+
148
+ ```js
149
+ using mesh = ml.Mesh.fromTriangles(coords, tris);
150
+ // ... use mesh; it is freed at the end of this scope
151
+ ```
152
+
153
+ When the number of handles is dynamic (for example built in a loop), collect them in a
154
+ `DisposableStack`, which frees everything it holds, in reverse order, at the end of the scope:
155
+
156
+ ```js
157
+ using stack = new DisposableStack();
158
+ for (const path of inputPaths) {
159
+ const cloud = stack.use(ml.PointsLoad.fromAnySupportedFormat(path));
160
+ // ... use cloud
161
+ }
162
+ // every handle passed to stack.use(...) is freed here
163
+ ```
164
+
165
+ `using` and `DisposableStack` are part of JavaScript's Explicit Resource Management,
166
+ available in Node.js 24+ and current browsers. On older runtimes and browsers, call
167
+ `.delete()` on each object when you are done instead:
168
+
169
+ ```js
170
+ const mesh = ml.Mesh.fromTriangles(coords, tris);
171
+ // ... use mesh
172
+ mesh.delete();
173
+ ```
174
+
175
+ 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).
128
176
 
129
177
  ## License
130
178