@tensor-cad/engine 0.1.0 → 0.1.2

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/index.js CHANGED
@@ -176,15 +176,34 @@ function unwrap(text) {
176
176
  function point(options) {
177
177
  return options ? JSON.stringify(options) : "";
178
178
  }
179
+ var WASM_MAGIC = [0, 97, 115, 109];
180
+ function looksLikeWasm(bytes) {
181
+ if (bytes.byteLength < 4)
182
+ return false;
183
+ const head = new Uint8Array(bytes, 0, 4);
184
+ return WASM_MAGIC.every((b, i) => head[i] === b);
185
+ }
179
186
  async function bytesOf(wasm) {
180
187
  if (wasm && typeof wasm !== "string" && !(wasm instanceof URL))
181
188
  return wasm;
182
- const url = wasm ?? new URL("../wasm/tensorcad.wasm", import.meta.url);
183
- const response = await fetch(url);
184
- if (!response.ok) {
185
- throw new EngineError(`Could not load the engine from ${String(url)}: ${response.status}`);
189
+ const urls = wasm ? [wasm] : [new URL("./tensorcad.wasm", import.meta.url)];
190
+ const tried = [];
191
+ for (const url of urls) {
192
+ try {
193
+ const response = await fetch(url);
194
+ if (!response.ok) {
195
+ tried.push(`${String(url)} (${response.status})`);
196
+ continue;
197
+ }
198
+ const bytes = await response.arrayBuffer();
199
+ if (looksLikeWasm(bytes))
200
+ return bytes;
201
+ tried.push(`${String(url)} (not WebAssembly — ${bytes.byteLength} bytes)`);
202
+ } catch (error) {
203
+ tried.push(`${String(url)} (${error.message})`);
204
+ }
186
205
  }
187
- return await response.arrayBuffer();
206
+ throw new EngineError(`Could not load the engine. Tried ${tried.join(", ")}. ` + `Pass \`wasm\` to createEngine() if it lives somewhere else.`);
188
207
  }
189
208
  async function createEngine(options = {}) {
190
209
  if (typeof globalThis.Go !== "function") {
package/node.js CHANGED
@@ -757,15 +757,34 @@ function unwrap(text) {
757
757
  function point(options) {
758
758
  return options ? JSON.stringify(options) : "";
759
759
  }
760
+ var WASM_MAGIC = [0, 97, 115, 109];
761
+ function looksLikeWasm(bytes) {
762
+ if (bytes.byteLength < 4)
763
+ return false;
764
+ const head = new Uint8Array(bytes, 0, 4);
765
+ return WASM_MAGIC.every((b, i) => head[i] === b);
766
+ }
760
767
  async function bytesOf(wasm) {
761
768
  if (wasm && typeof wasm !== "string" && !(wasm instanceof URL))
762
769
  return wasm;
763
- const url = wasm ?? new URL("../wasm/tensorcad.wasm", import.meta.url);
764
- const response = await fetch(url);
765
- if (!response.ok) {
766
- throw new EngineError(`Could not load the engine from ${String(url)}: ${response.status}`);
770
+ const urls = wasm ? [wasm] : [new URL("../wasm/tensorcad.wasm", import.meta.url)];
771
+ const tried = [];
772
+ for (const url of urls) {
773
+ try {
774
+ const response = await fetch(url);
775
+ if (!response.ok) {
776
+ tried.push(`${String(url)} (${response.status})`);
777
+ continue;
778
+ }
779
+ const bytes = await response.arrayBuffer();
780
+ if (looksLikeWasm(bytes))
781
+ return bytes;
782
+ tried.push(`${String(url)} (not WebAssembly — ${bytes.byteLength} bytes)`);
783
+ } catch (error) {
784
+ tried.push(`${String(url)} (${error.message})`);
785
+ }
767
786
  }
768
- return await response.arrayBuffer();
787
+ throw new EngineError(`Could not load the engine. Tried ${tried.join(", ")}. ` + `Pass \`wasm\` to createEngine() if it lives somewhere else.`);
769
788
  }
770
789
  async function createEngine(options = {}) {
771
790
  if (typeof globalThis.Go !== "function") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tensor-cad/engine",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "The TensorCAD analysis engine, compiled from Go to WebAssembly, with its TypeScript client",
5
5
  "type": "module",
6
6
  "main": "./index.js",
package/tensorcad.wasm CHANGED
Binary file