@aqarios/luna-model-wasm32-wasi 0.6.4

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 ADDED
@@ -0,0 +1,3 @@
1
+ # `@aqarios/luna-model-wasm32-wasi`
2
+
3
+ This is the **wasm32-wasip1-threads** binary for `@aqarios/luna-model`
package/index.d.ts ADDED
@@ -0,0 +1,158 @@
1
+ /* auto-generated by NAPI-RS */
2
+ /* eslint-disable */
3
+ /**
4
+ * Provides access to solution information including the sample,
5
+ * objective value, feasibility, and constraint satisfaction, counts,
6
+ * raw energy, and comparison capabilities.
7
+ */
8
+ export declare class ResultView {
9
+ /** Get the number of times this result was observed. */
10
+ get counts(): number
11
+ /** Get the objective function value. */
12
+ get objValue(): number | null
13
+ /** Get the raw energy from the solver. */
14
+ get rawEnergy(): number | null
15
+ /** Get constraint satisfaction status. */
16
+ get constraints(): Record<string, boolean> | null
17
+ /** Get variable bound satisfaction status. */
18
+ get variableBounds(): Record<string, boolean> | null
19
+ /** Get feasibility status. */
20
+ get feasible(): boolean | null
21
+ }
22
+ export type JsResultView = ResultView
23
+
24
+ /**
25
+ * Column-oriented solution data for model evaluation or solver results.
26
+ *
27
+ * A solution is independent of the original model and stores all variable data
28
+ * by variable name. JavaScript solutions are created from LunaModel's binary
29
+ * serializer with `Solution.deserialize()`.
30
+ */
31
+ export declare class Solution {
32
+ /**
33
+ * Decode a LunaModel solution from serialized binary bytes.
34
+ *
35
+ * This is the JavaScript alias for Python's `Solution.decode()` /
36
+ * `Solution.deserialize()` path. `data` must contain bytes produced by the
37
+ * existing LunaModel `Solution` serializer.
38
+ */
39
+ static deserialize(data: Uint8Array): Solution
40
+ /**
41
+ * Number of occurrences for each stored sample row.
42
+ *
43
+ * This matches the Python `counts` property.
44
+ */
45
+ get counts(): Array<number>
46
+ /**
47
+ * Objective values as computed by the solver.
48
+ *
49
+ * Returns `null` if the solver did not provide raw energies. This matches
50
+ * the Python `raw_energies` property.
51
+ */
52
+ get rawEnergies(): Array<number> | null
53
+ /**
54
+ * Objective values as computed by the corresponding model.
55
+ *
56
+ * Returns `null` for solutions that have not yet been evaluated. This
57
+ * matches the Python `obj_values` property.
58
+ */
59
+ get objValues(): Array<number> | null
60
+ /**
61
+ * Feasibility flag for each stored sample row.
62
+ *
63
+ * A value is `true` when all constraints and variable bounds are satisfied
64
+ * for that sample. Returns `null` for solutions without feasibility data.
65
+ */
66
+ get feasible(): Array<boolean> | null
67
+ /**
68
+ * Per-constraint feasibility flags keyed by constraint name.
69
+ *
70
+ * Each vector is aligned with the stored sample rows.
71
+ */
72
+ get constraints(): Record<string, Array<boolean>>
73
+ /**
74
+ * Per-variable bound feasibility flags keyed by variable name.
75
+ *
76
+ * Each vector is aligned with the stored sample rows.
77
+ */
78
+ get variableBounds(): Record<string, Array<boolean>>
79
+ /**
80
+ * Runtime metrics carried by this solution.
81
+ *
82
+ * Returns `null` if no timing metadata is available. This corresponds to
83
+ * Python's `runtime` property.
84
+ */
85
+ get timing(): JsTiming | null
86
+ /**
87
+ * Sense carried by this solution.
88
+ *
89
+ * This corresponds to Python's `sense` property.
90
+ */
91
+ get sense(): Sense
92
+ /**
93
+ * Fraction of total sample mass marked as feasible.
94
+ *
95
+ * Computes the count-weighted ratio of feasible samples to all samples.
96
+ * Throws if feasibility data is not available.
97
+ */
98
+ feasibilityRatio(): number
99
+ /**
100
+ * Return a new solution containing only feasible sample rows.
101
+ *
102
+ * Throws if feasibility data is not available. Filtering feasible samples
103
+ * is not possible on a non-evaluated solution.
104
+ */
105
+ filterFeasible(): Solution
106
+ /**
107
+ * Get the best results according to the optimization sense.
108
+ *
109
+ * Returns `ResultView[]` or `null`.
110
+ * List of best results (lowest for MIN, highest for MAX).
111
+ */
112
+ best(): Array<ResultView> | null
113
+ }
114
+ export type JsSolution = Solution
115
+
116
+ /**
117
+ * Runtime timing metadata attached to a solution.
118
+ *
119
+ * JavaScript exposes wall-clock timestamps as milliseconds since the Unix
120
+ * epoch (UTC). Wrap with `new Date(timing.start)` for a `Date` object.
121
+ */
122
+ export declare class Timing {
123
+ /**
124
+ * Wall-clock start time, in milliseconds since the Unix epoch (UTC).
125
+ *
126
+ * This matches Python's `start` property; wrap with `new Date(...)` on
127
+ * the JS side if you want a `Date` object.
128
+ */
129
+ get start(): number
130
+ /**
131
+ * Wall-clock end time, in milliseconds since the Unix epoch (UTC).
132
+ *
133
+ * This matches Python's `end` property; wrap with `new Date(...)` on
134
+ * the JS side if you want a `Date` object.
135
+ */
136
+ get end(): number
137
+ /**
138
+ * Total runtime in seconds.
139
+ *
140
+ * This is computed as the difference between `end` and `start`. Throws if
141
+ * the timing record is inconsistent and the total duration cannot be
142
+ * computed. This matches Python's `total_seconds` property.
143
+ */
144
+ get totalSeconds(): number
145
+ /**
146
+ * QPU usage time reported by the backend.
147
+ *
148
+ * Returns `null` when no QPU timing was provided. This matches Python's
149
+ * `qpu` property.
150
+ */
151
+ get qpu(): number | null
152
+ }
153
+ export type JsTiming = Timing
154
+
155
+ export declare const enum Sense {
156
+ Max = 0,
157
+ Min = 1
158
+ }
@@ -0,0 +1,67 @@
1
+ import {
2
+ createOnMessage as __wasmCreateOnMessageForFsProxy,
3
+ getDefaultContext as __emnapiGetDefaultContext,
4
+ instantiateNapiModuleSync as __emnapiInstantiateNapiModuleSync,
5
+ WASI as __WASI,
6
+ } from '@napi-rs/wasm-runtime'
7
+
8
+
9
+
10
+ const __wasi = new __WASI({
11
+ version: 'preview1',
12
+ })
13
+
14
+ const __wasmUrl = new URL('./js_lunamodel.wasm32-wasi.wasm', import.meta.url).href
15
+ const __emnapiContext = __emnapiGetDefaultContext()
16
+
17
+
18
+ const __sharedMemory = new WebAssembly.Memory({
19
+ initial: 4000,
20
+ maximum: 65536,
21
+ shared: true,
22
+ })
23
+
24
+ const __wasmFile = await fetch(__wasmUrl).then((res) => res.arrayBuffer())
25
+
26
+ const {
27
+ instance: __napiInstance,
28
+ module: __wasiModule,
29
+ napiModule: __napiModule,
30
+ } = __emnapiInstantiateNapiModuleSync(__wasmFile, {
31
+ context: __emnapiContext,
32
+ asyncWorkPoolSize: 4,
33
+ wasi: __wasi,
34
+ onCreateWorker() {
35
+ const worker = new Worker(new URL('@aqarios/luna-model-wasm32-wasi/wasi-worker-browser.mjs', import.meta.url), {
36
+ type: 'module',
37
+ })
38
+
39
+
40
+ return worker
41
+ },
42
+ overwriteImports(importObject) {
43
+ importObject.env = {
44
+ ...importObject.env,
45
+ ...importObject.napi,
46
+ ...importObject.emnapi,
47
+ memory: __sharedMemory,
48
+ }
49
+ return importObject
50
+ },
51
+ beforeInit({ instance }) {
52
+ for (const name of Object.keys(instance.exports)) {
53
+ if (name.startsWith('__napi_register__')) {
54
+ instance.exports[name]()
55
+ }
56
+ }
57
+ },
58
+ })
59
+ export default __napiModule.exports
60
+ export const ResultView = __napiModule.exports.ResultView
61
+ export const JsResultView = __napiModule.exports.JsResultView
62
+ export const Solution = __napiModule.exports.Solution
63
+ export const JsSolution = __napiModule.exports.JsSolution
64
+ export const Timing = __napiModule.exports.Timing
65
+ export const JsTiming = __napiModule.exports.JsTiming
66
+ export const Sense = __napiModule.exports.Sense
67
+ export const JsSense = __napiModule.exports.JsSense
@@ -0,0 +1,118 @@
1
+ /* eslint-disable */
2
+ /* prettier-ignore */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ const __nodeFs = require('node:fs')
7
+ const __nodePath = require('node:path')
8
+ const { WASI: __nodeWASI } = require('node:wasi')
9
+ const { Worker } = require('node:worker_threads')
10
+
11
+ const {
12
+ createOnMessage: __wasmCreateOnMessageForFsProxy,
13
+ getDefaultContext: __emnapiGetDefaultContext,
14
+ instantiateNapiModuleSync: __emnapiInstantiateNapiModuleSync,
15
+ } = require('@napi-rs/wasm-runtime')
16
+
17
+ const __rootDir = __nodePath.parse(process.cwd()).root
18
+
19
+ const __wasi = new __nodeWASI({
20
+ version: 'preview1',
21
+ env: process.env,
22
+ preopens: {
23
+ [__rootDir]: __rootDir,
24
+ }
25
+ })
26
+
27
+ const __emnapiContext = __emnapiGetDefaultContext()
28
+
29
+ const __sharedMemory = new WebAssembly.Memory({
30
+ initial: 4000,
31
+ maximum: 65536,
32
+ shared: true,
33
+ })
34
+
35
+ let __wasmFilePath = __nodePath.join(__dirname, 'js_lunamodel.wasm32-wasi.wasm')
36
+ const __wasmDebugFilePath = __nodePath.join(__dirname, 'js_lunamodel.wasm32-wasi.debug.wasm')
37
+
38
+ if (__nodeFs.existsSync(__wasmDebugFilePath)) {
39
+ __wasmFilePath = __wasmDebugFilePath
40
+ } else if (!__nodeFs.existsSync(__wasmFilePath)) {
41
+ try {
42
+ __wasmFilePath = require.resolve('@aqarios/luna-model-wasm32-wasi/js_lunamodel.wasm32-wasi.wasm')
43
+ } catch {
44
+ throw new Error('Cannot find js_lunamodel.wasm32-wasi.wasm file, and @aqarios/luna-model-wasm32-wasi package is not installed.')
45
+ }
46
+ }
47
+
48
+ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
49
+ context: __emnapiContext,
50
+ asyncWorkPoolSize: (function() {
51
+ const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
52
+ // NaN > 0 is false
53
+ if (threadsSizeFromEnv > 0) {
54
+ return threadsSizeFromEnv
55
+ } else {
56
+ return 4
57
+ }
58
+ })(),
59
+ reuseWorker: true,
60
+ wasi: __wasi,
61
+ onCreateWorker() {
62
+ const worker = new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
63
+ env: process.env,
64
+ })
65
+ worker.onmessage = ({ data }) => {
66
+ __wasmCreateOnMessageForFsProxy(__nodeFs)(data)
67
+ }
68
+
69
+ // The main thread of Node.js waits for all the active handles before exiting.
70
+ // But Rust threads are never waited without `thread::join`.
71
+ // So here we hack the code of Node.js to prevent the workers from being referenced (active).
72
+ // According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
73
+ // a worker is consist of two handles: kPublicPort and kHandle.
74
+ {
75
+ const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
76
+ s.toString().includes("kPublicPort")
77
+ );
78
+ if (kPublicPort) {
79
+ worker[kPublicPort].ref = () => {};
80
+ }
81
+
82
+ const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
83
+ s.toString().includes("kHandle")
84
+ );
85
+ if (kHandle) {
86
+ worker[kHandle].ref = () => {};
87
+ }
88
+
89
+ worker.unref();
90
+ }
91
+ return worker
92
+ },
93
+ overwriteImports(importObject) {
94
+ importObject.env = {
95
+ ...importObject.env,
96
+ ...importObject.napi,
97
+ ...importObject.emnapi,
98
+ memory: __sharedMemory,
99
+ }
100
+ return importObject
101
+ },
102
+ beforeInit({ instance }) {
103
+ for (const name of Object.keys(instance.exports)) {
104
+ if (name.startsWith('__napi_register__')) {
105
+ instance.exports[name]()
106
+ }
107
+ }
108
+ },
109
+ })
110
+ module.exports = __napiModule.exports
111
+ module.exports.ResultView = __napiModule.exports.ResultView
112
+ module.exports.JsResultView = __napiModule.exports.JsResultView
113
+ module.exports.Solution = __napiModule.exports.Solution
114
+ module.exports.JsSolution = __napiModule.exports.JsSolution
115
+ module.exports.Timing = __napiModule.exports.Timing
116
+ module.exports.JsTiming = __napiModule.exports.JsTiming
117
+ module.exports.Sense = __napiModule.exports.Sense
118
+ module.exports.JsSense = __napiModule.exports.JsSense
Binary file
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@aqarios/luna-model-wasm32-wasi",
3
+ "version": "0.6.4",
4
+ "main": "js_lunamodel.wasi.cjs",
5
+ "files": [
6
+ "js_lunamodel.wasm32-wasi.wasm",
7
+ "js_lunamodel.wasi.cjs",
8
+ "js_lunamodel.wasi-browser.js",
9
+ "wasi-worker.mjs",
10
+ "wasi-worker-browser.mjs",
11
+ "index.d.ts"
12
+ ],
13
+ "description": "JavaScript and TypeScript bindings for LunaModel",
14
+ "author": "Aqarios GmbH <pypi@aqarios.com>",
15
+ "license": "Apache-2.0",
16
+ "engines": {
17
+ "node": ">=14.0.0"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/aqarios/luna-model.git",
22
+ "directory": "js-lunamodel"
23
+ },
24
+ "publishConfig": {
25
+ "registry": "https://registry.npmjs.org",
26
+ "access": "public"
27
+ },
28
+ "browser": "js_lunamodel.wasi-browser.js",
29
+ "dependencies": {
30
+ "@napi-rs/wasm-runtime": "^1.1.5",
31
+ "@emnapi/core": "1.11.1",
32
+ "@emnapi/runtime": "1.11.1"
33
+ },
34
+ "types": "index.d.ts",
35
+ "module": "js_lunamodel.wasi-browser.js",
36
+ "exports": {
37
+ ".": {
38
+ "types": "./index.d.ts",
39
+ "browser": "./js_lunamodel.wasi-browser.js",
40
+ "import": "./js_lunamodel.wasi.cjs",
41
+ "require": "./js_lunamodel.wasi.cjs"
42
+ },
43
+ "./js_lunamodel.wasm32-wasi.wasm": "./js_lunamodel.wasm32-wasi.wasm",
44
+ "./wasi-worker-browser.mjs": "./wasi-worker-browser.mjs",
45
+ "./wasi-worker.mjs": "./wasi-worker.mjs"
46
+ }
47
+ }
@@ -0,0 +1,34 @@
1
+ import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
2
+
3
+ const handler = new MessageHandler({
4
+ onLoad({ wasmModule, wasmMemory }) {
5
+ const wasi = new WASI({
6
+ print: function () {
7
+ // eslint-disable-next-line no-console
8
+ console.log.apply(console, arguments)
9
+ },
10
+ printErr: function() {
11
+ // eslint-disable-next-line no-console
12
+ console.error.apply(console, arguments)
13
+
14
+ },
15
+ })
16
+ return instantiateNapiModuleSync(wasmModule, {
17
+ childThread: true,
18
+ wasi,
19
+ overwriteImports(importObject) {
20
+ importObject.env = {
21
+ ...importObject.env,
22
+ ...importObject.napi,
23
+ ...importObject.emnapi,
24
+ memory: wasmMemory,
25
+ }
26
+ },
27
+ })
28
+ },
29
+
30
+ })
31
+
32
+ globalThis.onmessage = function (e) {
33
+ handler.handle(e)
34
+ }
@@ -0,0 +1,63 @@
1
+ import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parse } from "node:path";
4
+ import { WASI } from "node:wasi";
5
+ import { parentPort, Worker } from "node:worker_threads";
6
+
7
+ const require = createRequire(import.meta.url);
8
+
9
+ const { instantiateNapiModuleSync, MessageHandler, getDefaultContext } = require("@napi-rs/wasm-runtime");
10
+
11
+ if (parentPort) {
12
+ parentPort.on("message", (data) => {
13
+ globalThis.onmessage({ data });
14
+ });
15
+ }
16
+
17
+ Object.assign(globalThis, {
18
+ self: globalThis,
19
+ require,
20
+ Worker,
21
+ importScripts: function (f) {
22
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
23
+ },
24
+ postMessage: function (msg) {
25
+ if (parentPort) {
26
+ parentPort.postMessage(msg);
27
+ }
28
+ },
29
+ });
30
+
31
+ const emnapiContext = getDefaultContext();
32
+
33
+ const __rootDir = parse(process.cwd()).root;
34
+
35
+ const handler = new MessageHandler({
36
+ onLoad({ wasmModule, wasmMemory }) {
37
+ const wasi = new WASI({
38
+ version: 'preview1',
39
+ env: process.env,
40
+ preopens: {
41
+ [__rootDir]: __rootDir,
42
+ },
43
+ });
44
+
45
+ return instantiateNapiModuleSync(wasmModule, {
46
+ childThread: true,
47
+ wasi,
48
+ context: emnapiContext,
49
+ overwriteImports(importObject) {
50
+ importObject.env = {
51
+ ...importObject.env,
52
+ ...importObject.napi,
53
+ ...importObject.emnapi,
54
+ memory: wasmMemory
55
+ };
56
+ },
57
+ });
58
+ },
59
+ });
60
+
61
+ globalThis.onmessage = function (e) {
62
+ handler.handle(e);
63
+ };