@autochitect/engine 1.1.5 → 1.1.7

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/engine.wasm CHANGED
Binary file
package/index.browser.mjs CHANGED
@@ -82,21 +82,29 @@ export async function createEngine(wasmSource) {
82
82
  const memoryRef = { current: null };
83
83
  const imports = { wasi_snapshot_preview1: buildWasiImports(memoryRef) };
84
84
 
85
- let bytes;
86
- if (wasmSource instanceof ArrayBuffer || ArrayBuffer.isView(wasmSource)) {
87
- bytes = wasmSource;
88
- } else if (wasmSource instanceof Response || (typeof wasmSource === 'object' && typeof wasmSource.then === 'function')) {
89
- const response = await wasmSource;
90
- bytes = await response.arrayBuffer();
91
- } else if (wasmSource instanceof URL) {
92
- bytes = await (await fetch(wasmSource)).arrayBuffer();
93
- } else if (typeof wasmSource === 'string') {
94
- bytes = await (await fetch(wasmSource)).arrayBuffer();
95
- } else {
96
- throw new Error('wasmSource must be a URL, URL string, Response, or ArrayBuffer');
85
+ if (wasmSource == null) {
86
+ wasmSource = new URL('./engine.wasm', import.meta.url);
97
87
  }
98
88
 
99
- const { instance } = await WebAssembly.instantiate(bytes, imports);
89
+ let instance;
90
+ if (wasmSource instanceof WebAssembly.Module) {
91
+ instance = await WebAssembly.instantiate(wasmSource, imports);
92
+ } else {
93
+ let bytes;
94
+ if (wasmSource instanceof ArrayBuffer || ArrayBuffer.isView(wasmSource)) {
95
+ bytes = wasmSource;
96
+ } else if (wasmSource instanceof Response || (typeof wasmSource === 'object' && typeof wasmSource.then === 'function')) {
97
+ const response = await wasmSource;
98
+ bytes = await response.arrayBuffer();
99
+ } else if (wasmSource instanceof URL) {
100
+ bytes = await (await fetch(wasmSource)).arrayBuffer();
101
+ } else if (typeof wasmSource === 'string') {
102
+ bytes = await (await fetch(wasmSource)).arrayBuffer();
103
+ } else {
104
+ throw new Error('wasmSource must be a WebAssembly.Module, URL, URL string, Response, or ArrayBuffer');
105
+ }
106
+ ({ instance } = await WebAssembly.instantiate(bytes, imports));
107
+ }
100
108
  memoryRef.current = instance.exports.memory;
101
109
 
102
110
  function writeString(str) {
package/index.d.ts CHANGED
@@ -24,4 +24,4 @@ export interface Engine {
24
24
  estimate(source: string, inputs?: Record<string, unknown> | object, options?: EstimateOptions): EstimateResult;
25
25
  }
26
26
 
27
- export function createEngine(wasmSource?: string | URL | Response | ArrayBuffer): Promise<Engine>;
27
+ export function createEngine(wasmSource?: string | URL | Response | ArrayBuffer | WebAssembly.Module): Promise<Engine>;
package/index.mjs CHANGED
@@ -89,40 +89,44 @@ export async function createEngine(wasmSource) {
89
89
  wasmSource = join(dirname(fileURLToPath(import.meta.url)), 'engine.wasm');
90
90
  }
91
91
 
92
- let bytes;
93
- if (wasmSource instanceof ArrayBuffer || ArrayBuffer.isView(wasmSource)) {
94
- bytes = wasmSource;
95
- } else if (wasmSource instanceof Response || (typeof wasmSource === 'object' && typeof wasmSource.then === 'function')) {
96
- const response = await wasmSource;
97
- bytes = await response.arrayBuffer();
98
- } else if (wasmSource instanceof URL) {
99
- if (wasmSource.protocol === 'file:') {
100
- const fs = await import('fs');
101
- bytes = fs.readFileSync(wasmSource);
102
- } else {
103
- bytes = await (await fetch(wasmSource)).arrayBuffer();
104
- }
105
- } else if (typeof wasmSource === 'string') {
106
- const isFilePath = wasmSource.startsWith('/') || wasmSource.startsWith('./') || wasmSource.startsWith('../') || wasmSource.startsWith('file://') || /^[a-zA-Z]:\\/.test(wasmSource);
107
- if (isFilePath && typeof globalThis.process !== 'undefined') {
108
- const fs = await import('fs');
109
- if (wasmSource.startsWith('file://')) {
110
- const { fileURLToPath } = await import('url');
111
- bytes = fs.readFileSync(fileURLToPath(wasmSource));
92
+ let instance;
93
+ if (wasmSource instanceof WebAssembly.Module) {
94
+ instance = await WebAssembly.instantiate(wasmSource, imports);
95
+ } else {
96
+ let bytes;
97
+ if (wasmSource instanceof ArrayBuffer || ArrayBuffer.isView(wasmSource)) {
98
+ bytes = wasmSource;
99
+ } else if (wasmSource instanceof Response || (typeof wasmSource === 'object' && typeof wasmSource.then === 'function')) {
100
+ const response = await wasmSource;
101
+ bytes = await response.arrayBuffer();
102
+ } else if (wasmSource instanceof URL) {
103
+ if (wasmSource.protocol === 'file:') {
104
+ const fs = await import('fs');
105
+ bytes = fs.readFileSync(wasmSource);
112
106
  } else {
107
+ bytes = await (await fetch(wasmSource)).arrayBuffer();
108
+ }
109
+ } else if (typeof wasmSource === 'string') {
110
+ const isFilePath = wasmSource.startsWith('/') || wasmSource.startsWith('./') || wasmSource.startsWith('../') || wasmSource.startsWith('file://') || /^[a-zA-Z]:\\/.test(wasmSource);
111
+ if (isFilePath && typeof globalThis.process !== 'undefined') {
112
+ const fs = await import('fs');
113
+ if (wasmSource.startsWith('file://')) {
114
+ const { fileURLToPath } = await import('url');
115
+ bytes = fs.readFileSync(fileURLToPath(wasmSource));
116
+ } else {
117
+ bytes = fs.readFileSync(wasmSource);
118
+ }
119
+ } else if (typeof fetch !== 'undefined') {
120
+ bytes = await (await fetch(wasmSource)).arrayBuffer();
121
+ } else {
122
+ const fs = await import('fs');
113
123
  bytes = fs.readFileSync(wasmSource);
114
124
  }
115
- } else if (typeof fetch !== 'undefined') {
116
- bytes = await (await fetch(wasmSource)).arrayBuffer();
117
125
  } else {
118
- const fs = await import('fs');
119
- bytes = fs.readFileSync(wasmSource);
126
+ throw new Error('wasmSource must be a WebAssembly.Module, URL, URL string, file path, Response, or ArrayBuffer');
120
127
  }
121
- } else {
122
- throw new Error('wasmSource must be a URL, URL string, file path, Response, or ArrayBuffer');
128
+ ({ instance } = await WebAssembly.instantiate(bytes, imports));
123
129
  }
124
-
125
- const { instance } = await WebAssembly.instantiate(bytes, imports);
126
130
  memoryRef.current = instance.exports.memory;
127
131
 
128
132
  function writeString(str) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autochitect/engine",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "description": "A 245KB WebAssembly financial modeling engine. Define cost models in a purpose-built DSL, get instant estimates with full dependency tracing. No server required.",
5
5
  "license": "MIT",
6
6
  "type": "module",