or-tools-wasm 0.5.0 → 0.7.0

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.
Files changed (52) hide show
  1. package/README.md +173 -55
  2. package/build/javascript/browser/cp_sat_module_loader.js +53 -0
  3. package/build/javascript/browser/cpsat_worker.js +135 -0
  4. package/build/javascript/browser/cpsat_worker_types.js +0 -0
  5. package/build/javascript/browser/index.js +5910 -0
  6. package/build/javascript/lib/assets/cp_sat_runtime-BcQ-oG9G.js +2568 -0
  7. package/build/javascript/lib/assets/cp_sat_runtime-BcQ-oG9G.js.map +1 -0
  8. package/build/javascript/lib/assets/{cp_sat_runtime-BUuF8d7w.wasm → cp_sat_runtime.wasm} +0 -0
  9. package/build/javascript/lib/assets/cp_sat_runtime.wasm.map +1 -0
  10. package/build/javascript/lib/assets/cp_sat_runtime_asyncify-CebveWgj.js +2642 -0
  11. package/build/javascript/lib/assets/cp_sat_runtime_asyncify-CebveWgj.js.map +1 -0
  12. package/build/javascript/lib/assets/{cp_sat_runtime_asyncify-CjMVqM1A.wasm → cp_sat_runtime_asyncify.wasm} +0 -0
  13. package/build/javascript/lib/assets/cp_sat_runtime_asyncify.wasm.map +1 -0
  14. package/build/javascript/lib/assets/cpsat_worker-CxyMtsph.js +145 -0
  15. package/build/javascript/lib/assets/cpsat_worker-CxyMtsph.js.map +1 -0
  16. package/build/javascript/lib/chunks/cp_sat_runtime-D-Bi0Puh.js +2568 -0
  17. package/build/javascript/lib/chunks/cp_sat_runtime-D-Bi0Puh.js.map +1 -0
  18. package/build/javascript/lib/chunks/cp_sat_runtime_asyncify-pyd1qgFK.js +2642 -0
  19. package/build/javascript/lib/chunks/cp_sat_runtime_asyncify-pyd1qgFK.js.map +1 -0
  20. package/build/javascript/lib/cp_sat_api.d.ts +277 -98
  21. package/build/javascript/lib/cp_sat_api.js +3364 -219
  22. package/build/javascript/lib/cp_sat_api.js.map +1 -1
  23. package/build/javascript/wasm/cp_sat_runtime.d.ts +51 -0
  24. package/build/javascript/wasm/cp_sat_runtime.js +10 -0
  25. package/build/javascript/wasm/cp_sat_runtime.wasm +0 -0
  26. package/build/javascript/wasm/cp_sat_runtime.wasm.map +1 -0
  27. package/build/javascript/wasm/cp_sat_runtime_asyncify.js +10 -0
  28. package/build/javascript/wasm/cp_sat_runtime_asyncify.wasm +0 -0
  29. package/build/javascript/wasm/cp_sat_runtime_asyncify.wasm.map +1 -0
  30. package/javascript/lib/cp_sat_api.ts +475 -0
  31. package/javascript/lib/cp_sat_module_loader.bundled.ts +58 -0
  32. package/javascript/lib/cp_sat_module_loader.ts +57 -0
  33. package/javascript/lib/cpsat_worker.ts +160 -0
  34. package/javascript/lib/cpsat_worker_types.ts +32 -0
  35. package/javascript/lib/generated/sat_parameters.ts +2760 -0
  36. package/javascript/lib/index.ts +8 -0
  37. package/javascript/lib/internal_wasm_asyncify.d.ts +15 -0
  38. package/javascript/lib/vite-shims.d.ts +25 -0
  39. package/javascript/lib/webassembly_promising.d.ts +7 -0
  40. package/package.json +46 -7
  41. package/build/javascript/lib/assets/cp_sat_runtime-N6mpxHFm.js +0 -2565
  42. package/build/javascript/lib/assets/cp_sat_runtime-N6mpxHFm.js.map +0 -1
  43. package/build/javascript/lib/assets/cp_sat_runtime_asyncify-DAFhCUmM.js +0 -2639
  44. package/build/javascript/lib/assets/cp_sat_runtime_asyncify-DAFhCUmM.js.map +0 -1
  45. package/build/javascript/lib/assets/cpsat_worker-Bc6EW-gj.js +0 -139
  46. package/build/javascript/lib/assets/cpsat_worker-Bc6EW-gj.js.map +0 -1
  47. package/build/javascript/lib/chunks/cp_sat_runtime-B9zhYjnh.js +0 -2565
  48. package/build/javascript/lib/chunks/cp_sat_runtime-B9zhYjnh.js.map +0 -1
  49. package/build/javascript/lib/chunks/cp_sat_runtime_asyncify-BaxdGqjW.js +0 -2639
  50. package/build/javascript/lib/chunks/cp_sat_runtime_asyncify-BaxdGqjW.js.map +0 -1
  51. package/build/javascript/lib/chunks/index-Cq3lJx2c.js +0 -3815
  52. package/build/javascript/lib/chunks/index-Cq3lJx2c.js.map +0 -1
@@ -0,0 +1,160 @@
1
+ /// <reference lib="webworker" />
2
+
3
+ import type { MainModule } from '#internal-wasm/cp_sat_runtime.js';
4
+ import { loadCpSat } from './cp_sat_module_loader.js';
5
+ import type { WorkerRequest, WorkerResponse } from './cpsat_worker_types.js';
6
+
7
+ const workerScope = self as DedicatedWorkerGlobalScope;
8
+
9
+
10
+ let moduleInstance: MainModule | null = null;
11
+
12
+ // The loader handles locateFile and mainScriptUrlOrBlob automatically now.
13
+ const moduleReady = loadCpSat()
14
+ .then((module: MainModule) => {
15
+ moduleInstance = module;
16
+ workerScope.postMessage({ type: 'ready' } satisfies WorkerResponse);
17
+ return module;
18
+ })
19
+ .catch((error: unknown) => {
20
+ console.error('[cpsat_worker] cpSatModule init failed:', error);
21
+ workerScope.postMessage({
22
+ type: 'error',
23
+ id: 0,
24
+ error: String(error),
25
+ } satisfies WorkerResponse);
26
+ throw error;
27
+ });
28
+
29
+ const readUint32LE = (buffer: ArrayBuffer, ptr: number) =>
30
+ new DataView(buffer, ptr, 4).getUint32(0, true);
31
+
32
+ const copyBytesToHeap = (bytes: Uint8Array | null) => {
33
+ if (!moduleInstance || !bytes?.length) {
34
+ return 0;
35
+ }
36
+ const ptr = moduleInstance._malloc(bytes.length);
37
+ moduleInstance.HEAPU8.set(bytes, ptr);
38
+ return ptr;
39
+ };
40
+
41
+ async function solveModel(modelBytes: Uint8Array, paramsBytes?: Uint8Array) {
42
+ if (!moduleInstance) {
43
+ throw new Error('Module not initialized.');
44
+ }
45
+ const lenPtr = moduleInstance._malloc(4);
46
+ const modelPtr = copyBytesToHeap(modelBytes);
47
+ const paramsPtr = copyBytesToHeap(paramsBytes ?? null);
48
+ let responsePtr = 0;
49
+
50
+ try {
51
+ responsePtr = (await moduleInstance.ccall(
52
+ 'solve_model',
53
+ 'number',
54
+ ['number', 'number', 'number', 'number', 'number'],
55
+ [
56
+ modelPtr,
57
+ modelBytes.length,
58
+ paramsPtr,
59
+ paramsBytes ? paramsBytes.length : 0,
60
+ lenPtr,
61
+ ],
62
+ { async: true },
63
+ )) as number;
64
+ } finally {
65
+ if (modelPtr) moduleInstance._free(modelPtr);
66
+ if (paramsPtr) moduleInstance._free(paramsPtr);
67
+ }
68
+
69
+ const len = readUint32LE(moduleInstance.HEAPU8.buffer, lenPtr);
70
+ moduleInstance._free(lenPtr);
71
+
72
+ if (!responsePtr || len === 0) {
73
+ if (responsePtr) moduleInstance._free_buffer(responsePtr);
74
+ return new Uint8Array();
75
+ }
76
+
77
+ const bytes = moduleInstance.HEAPU8.slice(responsePtr, responsePtr + len);
78
+ moduleInstance._free_buffer(responsePtr);
79
+ return bytes;
80
+ }
81
+
82
+ async function validateModel(modelBytes: Uint8Array) {
83
+ if (!moduleInstance) {
84
+ throw new Error('Module not initialized.');
85
+ }
86
+ const lenPtr = moduleInstance._malloc(4);
87
+ const modelPtr = copyBytesToHeap(modelBytes);
88
+ let msgPtr = 0;
89
+
90
+ try {
91
+ msgPtr = moduleInstance.ccall(
92
+ 'validate_model',
93
+ 'number',
94
+ ['number', 'number', 'number'],
95
+ [modelPtr, modelBytes.length, lenPtr],
96
+ ) as number;
97
+ } finally {
98
+ if (modelPtr) moduleInstance._free(modelPtr);
99
+ }
100
+
101
+ const len = readUint32LE(moduleInstance.HEAPU8.buffer, lenPtr);
102
+ moduleInstance._free(lenPtr);
103
+
104
+ if (!msgPtr || len === 0) {
105
+ if (msgPtr) moduleInstance._free_buffer(msgPtr);
106
+ return { ok: true, message: '' };
107
+ }
108
+
109
+ const messageBytes = moduleInstance.HEAPU8.slice(msgPtr, msgPtr + len);
110
+ moduleInstance._free_buffer(msgPtr);
111
+ const message = new TextDecoder().decode(messageBytes);
112
+ return { ok: false, message };
113
+ }
114
+
115
+ workerScope.onmessage = async (event: MessageEvent<WorkerRequest>) => {
116
+ const message = event.data as WorkerRequest;
117
+ try {
118
+ await moduleReady;
119
+ if (!moduleInstance) {
120
+ throw new Error('Module not initialized.');
121
+ }
122
+
123
+ if (message.type === 'validate') {
124
+ const validation = await validateModel(message.modelBytes);
125
+ workerScope.postMessage({
126
+ type: 'validateResult',
127
+ id: message.id,
128
+ ok: validation.ok,
129
+ message: validation.message,
130
+ } satisfies WorkerResponse);
131
+ return;
132
+ } else if (message.type === 'solve') {
133
+ const bytes = await solveModel(message.modelBytes, message.paramsBytes);
134
+ workerScope.postMessage({
135
+ type: 'solveResult',
136
+ id: message.id,
137
+ bytes,
138
+ } satisfies WorkerResponse);
139
+ return;
140
+ } else if (message.type === "getSchemas") {
141
+ const schemas = {
142
+ cp_model: moduleInstance.ccall('get_cp_model_schema', 'string', [], []),
143
+ sat_parameters: moduleInstance.ccall('get_sat_parameters_schema', 'string', [], [])
144
+ };
145
+ self.postMessage({ type: 'schemaResult', id: message.id, schemas });
146
+ return
147
+ } else if (message.type === "cancel_solve") {
148
+ moduleInstance.ccall('interrupt_solve', 'void', [], []);
149
+ self.postMessage({ type: 'solved_cancelled', id: message.id });
150
+ return
151
+ }
152
+ } catch (error) {
153
+ console.error('[cpsat_worker] request failed', message?.type, error);
154
+ workerScope.postMessage({
155
+ type: 'error',
156
+ id: message?.id ?? 0,
157
+ error: String(error),
158
+ } satisfies WorkerResponse);
159
+ }
160
+ };
@@ -0,0 +1,32 @@
1
+ export type SolveRequest = {
2
+ type: 'solve';
3
+ id: number;
4
+ modelBytes: Uint8Array;
5
+ paramsBytes?: Uint8Array;
6
+ };
7
+
8
+ export type ValidateRequest = {
9
+ type: 'validate';
10
+ id: number;
11
+ modelBytes: Uint8Array;
12
+ };
13
+
14
+ export type SchemaRequest = {
15
+ type: 'getSchemas';
16
+ id: number;
17
+ };
18
+
19
+ export type CancelSolve = {
20
+ type: 'cancel_solve',
21
+ id: number
22
+ }
23
+
24
+ export type WorkerRequest = SolveRequest | ValidateRequest | SchemaRequest | CancelSolve;
25
+
26
+ export type WorkerResponse =
27
+ | { type: 'ready' }
28
+ | { type: 'solveResult'; id: number; bytes: Uint8Array }
29
+ | { type: 'validateResult'; id: number; ok: boolean; message: string }
30
+ | { type: 'schemaResult'; id: number; schemas: { cp_model: string; sat_parameters: string } }
31
+ | { type: 'solved_cancelled', id: number }
32
+ | { type: 'error'; id: number; error: string };