@autochitect/engine 1.1.5 → 1.1.6
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.browser.mjs +17 -13
- package/index.d.ts +1 -1
- package/index.mjs +32 -28
- package/package.json +1 -1
package/index.browser.mjs
CHANGED
|
@@ -82,21 +82,25 @@ export async function createEngine(wasmSource) {
|
|
|
82
82
|
const memoryRef = { current: null };
|
|
83
83
|
const imports = { wasi_snapshot_preview1: buildWasiImports(memoryRef) };
|
|
84
84
|
|
|
85
|
-
let
|
|
86
|
-
if (wasmSource instanceof
|
|
87
|
-
|
|
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();
|
|
85
|
+
let instance;
|
|
86
|
+
if (wasmSource instanceof WebAssembly.Module) {
|
|
87
|
+
instance = await WebAssembly.instantiate(wasmSource, imports);
|
|
95
88
|
} else {
|
|
96
|
-
|
|
89
|
+
let bytes;
|
|
90
|
+
if (wasmSource instanceof ArrayBuffer || ArrayBuffer.isView(wasmSource)) {
|
|
91
|
+
bytes = wasmSource;
|
|
92
|
+
} else if (wasmSource instanceof Response || (typeof wasmSource === 'object' && typeof wasmSource.then === 'function')) {
|
|
93
|
+
const response = await wasmSource;
|
|
94
|
+
bytes = await response.arrayBuffer();
|
|
95
|
+
} else if (wasmSource instanceof URL) {
|
|
96
|
+
bytes = await (await fetch(wasmSource)).arrayBuffer();
|
|
97
|
+
} else if (typeof wasmSource === 'string') {
|
|
98
|
+
bytes = await (await fetch(wasmSource)).arrayBuffer();
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error('wasmSource must be a WebAssembly.Module, URL, URL string, Response, or ArrayBuffer');
|
|
101
|
+
}
|
|
102
|
+
({ instance } = await WebAssembly.instantiate(bytes, imports));
|
|
97
103
|
}
|
|
98
|
-
|
|
99
|
-
const { instance } = await WebAssembly.instantiate(bytes, imports);
|
|
100
104
|
memoryRef.current = instance.exports.memory;
|
|
101
105
|
|
|
102
106
|
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
|
|
93
|
-
if (wasmSource instanceof
|
|
94
|
-
|
|
95
|
-
} else
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (wasmSource.
|
|
100
|
-
const
|
|
101
|
-
bytes =
|
|
102
|
-
} else {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
3
|
+
"version": "1.1.6",
|
|
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",
|