@kodexa-ai/document-wasm-ts 8.0.0-20484695702

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 (39) hide show
  1. package/README.md +563 -0
  2. package/dist/ContentNode.d.ts +207 -0
  3. package/dist/ContentNode.d.ts.map +1 -0
  4. package/dist/ContentNode.js +633 -0
  5. package/dist/ContentNode.js.map +1 -0
  6. package/dist/ExtractionEngine.d.ts +65 -0
  7. package/dist/ExtractionEngine.d.ts.map +1 -0
  8. package/dist/ExtractionEngine.js +115 -0
  9. package/dist/ExtractionEngine.js.map +1 -0
  10. package/dist/KddbDocument.d.ts +193 -0
  11. package/dist/KddbDocument.d.ts.map +1 -0
  12. package/dist/KddbDocument.js +575 -0
  13. package/dist/KddbDocument.js.map +1 -0
  14. package/dist/index.d.ts +54 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +96 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/sqljs-bridge.bundle.js +330 -0
  19. package/dist/wasm/browser-bridge.d.ts +44 -0
  20. package/dist/wasm/browser-bridge.d.ts.map +1 -0
  21. package/dist/wasm/browser-bridge.js +104 -0
  22. package/dist/wasm/browser-bridge.js.map +1 -0
  23. package/dist/wasm/loader.d.ts +57 -0
  24. package/dist/wasm/loader.d.ts.map +1 -0
  25. package/dist/wasm/loader.js +193 -0
  26. package/dist/wasm/loader.js.map +1 -0
  27. package/dist/wasm/sqljs-bridge.d.ts +40 -0
  28. package/dist/wasm/sqljs-bridge.d.ts.map +1 -0
  29. package/dist/wasm/sqljs-bridge.js +98 -0
  30. package/dist/wasm/sqljs-bridge.js.map +1 -0
  31. package/dist/wasm/sqljs-core.d.ts +92 -0
  32. package/dist/wasm/sqljs-core.d.ts.map +1 -0
  33. package/dist/wasm/sqljs-core.js +372 -0
  34. package/dist/wasm/sqljs-core.js.map +1 -0
  35. package/dist/wasm/types.d.ts +179 -0
  36. package/dist/wasm/types.d.ts.map +1 -0
  37. package/dist/wasm/types.js +9 -0
  38. package/dist/wasm/types.js.map +1 -0
  39. package/package.json +62 -0
@@ -0,0 +1,193 @@
1
+ /**
2
+ * WebAssembly loader for Kodexa Go library
3
+ *
4
+ * NOTE: Go WASM with syscall/js uses a different model than typical WASM.
5
+ * Functions are registered on globalThis and accept/return JS values directly.
6
+ * No pointer-based memory management is needed for strings.
7
+ *
8
+ * IMPORTANT: sql.js is initialized BEFORE Go WASM to provide SQLite support.
9
+ */
10
+ import { initializeSqlJs, exposeSqljsBridge, cleanupSqlJs, isSqlJsInitialized } from './sqljs-bridge';
11
+ // Global WASM instance
12
+ let wasmInstance = null;
13
+ let wasmLoaded = false;
14
+ let wasmLoadPromise = null;
15
+ /**
16
+ * WASM helper for Go-based WASM modules
17
+ *
18
+ * With Go's syscall/js, strings are passed directly between JS and Go.
19
+ * This helper provides utilities for JSON serialization and result parsing.
20
+ */
21
+ export class WasmHelper {
22
+ constructor(_instance) {
23
+ // Instance stored for potential future use
24
+ }
25
+ /**
26
+ * Parse JSON string returned from Go
27
+ */
28
+ parseJsonResult(jsonStr) {
29
+ if (!jsonStr)
30
+ return null;
31
+ try {
32
+ return JSON.parse(jsonStr);
33
+ }
34
+ catch (e) {
35
+ console.error('Failed to parse JSON result:', jsonStr, e);
36
+ return null;
37
+ }
38
+ }
39
+ /**
40
+ * Create JSON string for Go functions
41
+ */
42
+ toJson(obj) {
43
+ return JSON.stringify(obj);
44
+ }
45
+ }
46
+ /**
47
+ * Load the WebAssembly module
48
+ */
49
+ async function loadWasm() {
50
+ if (wasmInstance) {
51
+ return wasmInstance;
52
+ }
53
+ try {
54
+ // CRITICAL: Initialize sql.js FIRST, before Go WASM
55
+ // This ensures the sql.js bridge functions are available on globalThis
56
+ // when Go WASM starts and tries to use them
57
+ if (!isSqlJsInitialized()) {
58
+ console.log('[loader] Initializing sql.js...');
59
+ await initializeSqlJs();
60
+ exposeSqljsBridge();
61
+ console.log('[loader] sql.js ready');
62
+ }
63
+ let wasmBytes;
64
+ // Check if we're in Node.js or browser
65
+ if (typeof window === 'undefined') {
66
+ // Node.js environment
67
+ const fs = require('fs');
68
+ const path = require('path');
69
+ // Add crypto polyfill for Node.js
70
+ if (!globalThis.crypto) {
71
+ const crypto = require('crypto');
72
+ globalThis.crypto = {
73
+ getRandomValues: (arr) => crypto.randomFillSync(arr)
74
+ };
75
+ }
76
+ // Load the Go WASM runtime
77
+ const wasmExecPath = path.join(__dirname, '../../dist/wasm_exec.js');
78
+ require(wasmExecPath);
79
+ const wasmPath = path.join(__dirname, '../../dist/kodexa.wasm');
80
+ wasmBytes = fs.readFileSync(wasmPath);
81
+ }
82
+ else {
83
+ // Browser environment
84
+ // Load wasm_exec.js first (defines globalThis.Go)
85
+ if (!globalThis.Go) {
86
+ // Use absolute path from root
87
+ const wasmExecUrl = '/f/dist/wasm_exec.js';
88
+ await new Promise((resolve, reject) => {
89
+ const script = document.createElement('script');
90
+ script.src = wasmExecUrl;
91
+ script.onload = () => resolve();
92
+ script.onerror = () => reject(new Error('Failed to load wasm_exec.js'));
93
+ document.head.appendChild(script);
94
+ });
95
+ }
96
+ const wasmUrl = '/f/dist/kodexa.wasm';
97
+ const response = await fetch(wasmUrl);
98
+ if (!response.ok) {
99
+ throw new Error(`Failed to fetch WASM: ${response.statusText}`);
100
+ }
101
+ wasmBytes = await response.arrayBuffer();
102
+ }
103
+ // Create Go runtime imports
104
+ const go = new globalThis.Go(); // Go WASM runtime
105
+ const result = await WebAssembly.instantiate(wasmBytes, go.importObject);
106
+ // Start the Go program
107
+ go.run(result.instance);
108
+ // Configure log level: check for override, default to "warn"
109
+ // Users can set window.KODEXA_LOG_LEVEL before loading, or call kodexa_setLogLevel() after
110
+ const logLevel = globalThis.KODEXA_LOG_LEVEL || 'warn';
111
+ if (globalThis.kodexa_setLogLevel) {
112
+ globalThis.kodexa_setLogLevel(logLevel);
113
+ }
114
+ wasmInstance = result.instance;
115
+ wasmLoaded = true;
116
+ return wasmInstance;
117
+ }
118
+ catch (error) {
119
+ console.error('Failed to load WASM module:', error);
120
+ throw new Error(`WASM loading failed: ${error instanceof Error ? error.message : String(error)}`);
121
+ }
122
+ }
123
+ /**
124
+ * Get the loaded WASM instance
125
+ */
126
+ export async function getWasmInstance() {
127
+ if (wasmInstance) {
128
+ return wasmInstance;
129
+ }
130
+ if (wasmLoadPromise) {
131
+ return wasmLoadPromise;
132
+ }
133
+ wasmLoadPromise = loadWasm();
134
+ return wasmLoadPromise;
135
+ }
136
+ /**
137
+ * Get WASM helper for the current instance
138
+ */
139
+ export async function getWasmHelper() {
140
+ const instance = await getWasmInstance();
141
+ return new WasmHelper(instance);
142
+ }
143
+ /**
144
+ * Check if WASM is loaded
145
+ */
146
+ export function isWasmLoaded() {
147
+ return wasmLoaded;
148
+ }
149
+ /**
150
+ * Initialize WASM (call this before using any Go functions)
151
+ */
152
+ export async function initWasm() {
153
+ if (wasmLoaded) {
154
+ return;
155
+ }
156
+ await getWasmInstance();
157
+ console.log('✅ Kodexa WASM module loaded successfully');
158
+ }
159
+ /**
160
+ * Cleanup WASM resources
161
+ */
162
+ export function cleanupWasm() {
163
+ if (wasmInstance) {
164
+ try {
165
+ globalThis.cleanup();
166
+ }
167
+ catch (e) {
168
+ console.warn('Error during WASM cleanup:', e);
169
+ }
170
+ wasmInstance = null;
171
+ wasmLoaded = false;
172
+ wasmLoadPromise = null;
173
+ }
174
+ // Also cleanup sql.js databases
175
+ cleanupSqlJs();
176
+ }
177
+ /**
178
+ * Set the log level for Go WASM logging.
179
+ * Valid levels: "debug", "info", "warn", "error"
180
+ * Default is "warn" (set during initialization).
181
+ *
182
+ * Can be called anytime after WASM is loaded.
183
+ * To set before loading, use: window.KODEXA_LOG_LEVEL = "debug"
184
+ */
185
+ export function setLogLevel(level) {
186
+ if (globalThis.kodexa_setLogLevel) {
187
+ const result = globalThis.kodexa_setLogLevel(level);
188
+ return result === 'ok';
189
+ }
190
+ console.warn('WASM not loaded yet. Set window.KODEXA_LOG_LEVEL before loading instead.');
191
+ return false;
192
+ }
193
+ //# sourceMappingURL=loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"loader.js","sourceRoot":"","sources":["../../src/wasm/loader.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEtG,uBAAuB;AACvB,IAAI,YAAY,GAA0B,IAAI,CAAC;AAC/C,IAAI,UAAU,GAAG,KAAK,CAAC;AACvB,IAAI,eAAe,GAAmC,IAAI,CAAC;AAE3D;;;;;GAKG;AACH,MAAM,OAAO,UAAU;IACrB,YAAY,SAAyB;QACnC,2CAA2C;IAC7C,CAAC;IAED;;OAEG;IACH,eAAe,CAAC,OAAe;QAC7B,IAAI,CAAC,OAAO;YAAE,OAAO,IAAI,CAAC;QAC1B,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,CAAC,KAAK,CAAC,8BAA8B,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;YAC1D,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAQ;QACb,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;CACF;AAED;;GAEG;AACH,KAAK,UAAU,QAAQ;IACrB,IAAI,YAAY,EAAE,CAAC;QACjB,OAAO,YAAY,CAAC;IACtB,CAAC;IAED,IAAI,CAAC;QACH,oDAAoD;QACpD,uEAAuE;QACvE,4CAA4C;QAC5C,IAAI,CAAC,kBAAkB,EAAE,EAAE,CAAC;YAC1B,OAAO,CAAC,GAAG,CAAC,iCAAiC,CAAC,CAAC;YAC/C,MAAM,eAAe,EAAE,CAAC;YACxB,iBAAiB,EAAE,CAAC;YACpB,OAAO,CAAC,GAAG,CA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kBAAkB,EAAE,CAAC;QAC3C,MAAM,MAAM,GAAI,UAAkB,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC;QAC7D,OAAO,MAAM,KAAK,IAAI,CAAC;IACzB,CAAC;IACD,OAAO,CAAC,IAAI,CAAC,0EAA0E,CAAC,CAAC;IACzF,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * sql.js Bridge for Go WASM (npm package version)
3
+ *
4
+ * This module initializes sql.js (JavaScript SQLite) via npm import
5
+ * and exposes bridge functions to globalThis for Go WASM access.
6
+ *
7
+ * For browser CDN usage, see browser-bridge.ts instead.
8
+ *
9
+ * CRITICAL: This must be initialized BEFORE Go WASM is loaded.
10
+ */
11
+ /**
12
+ * Initialize sql.js WASM
13
+ * Must be called before any database operations
14
+ */
15
+ export declare function initializeSqlJs(wasmUrl?: string): Promise<void>;
16
+ /**
17
+ * Check if sql.js is initialized
18
+ */
19
+ export declare function isSqlJsInitialized(): boolean;
20
+ /**
21
+ * Expose sql.js bridge functions to globalThis for Go WASM access
22
+ * Must be called after initializeSqlJs() and before loading Go WASM
23
+ */
24
+ export declare function exposeSqljsBridge(): void;
25
+ /**
26
+ * Cleanup all databases and reset state
27
+ */
28
+ export declare function cleanupSqlJs(): void;
29
+ /**
30
+ * Load document: loads bytes directly into sql.js and passes handle to Go.
31
+ * Go handles migration if needed. This avoids the bytes round-trip for ALL documents.
32
+ * @param bytes Raw KDDB file bytes (Uint8Array)
33
+ * @returns Document reference or 0 on error
34
+ */
35
+ export declare function loadDocument(bytes: Uint8Array): number;
36
+ /**
37
+ * Get a database by handle (for debugging from console)
38
+ */
39
+ export declare function getDatabase(handle: number): any;
40
+ //# sourceMappingURL=sqljs-bridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqljs-bridge.d.ts","sourceRoot":"","sources":["../../src/wasm/sqljs-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAkBH;;;GAGG;AACH,wBAAsB,eAAe,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAgCrE;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,OAAO,CAE5C;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,IAAI,IAAI,CAQxC;AAED;;GAEG;AACH,wBAAgB,YAAY,IAAI,IAAI,CAEnC;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAYtD;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAE/C"}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * sql.js Bridge for Go WASM (npm package version)
3
+ *
4
+ * This module initializes sql.js (JavaScript SQLite) via npm import
5
+ * and exposes bridge functions to globalThis for Go WASM access.
6
+ *
7
+ * For browser CDN usage, see browser-bridge.ts instead.
8
+ *
9
+ * CRITICAL: This must be initialized BEFORE Go WASM is loaded.
10
+ */
11
+ import initSqlJs from 'sql.js';
12
+ import { setSqlInstance, exposeBridgeFunctions, cleanupOp, getDatabase as getDatabaseCore, loadDirectOp, } from './sqljs-core';
13
+ // sql.js static instance
14
+ let SQL = null;
15
+ // Track initialization state
16
+ let initialized = false;
17
+ let initPromise = null;
18
+ /**
19
+ * Initialize sql.js WASM
20
+ * Must be called before any database operations
21
+ */
22
+ export async function initializeSqlJs(wasmUrl) {
23
+ if (initialized) {
24
+ return;
25
+ }
26
+ if (initPromise) {
27
+ return initPromise;
28
+ }
29
+ initPromise = (async () => {
30
+ try {
31
+ // Configure sql.js WASM location
32
+ const config = {};
33
+ if (wasmUrl) {
34
+ config.locateFile = () => wasmUrl;
35
+ }
36
+ else if (typeof window !== 'undefined') {
37
+ // Browser: use CDN
38
+ config.locateFile = (file) => `https://sql.js.org/dist/${file}`;
39
+ }
40
+ // Node.js: sql.js will find the WASM file automatically
41
+ SQL = await initSqlJs(config);
42
+ setSqlInstance(SQL);
43
+ initialized = true;
44
+ }
45
+ catch (error) {
46
+ console.error('[sql.js] Initialization failed:', error);
47
+ throw error;
48
+ }
49
+ })();
50
+ return initPromise;
51
+ }
52
+ /**
53
+ * Check if sql.js is initialized
54
+ */
55
+ export function isSqlJsInitialized() {
56
+ return initialized;
57
+ }
58
+ /**
59
+ * Expose sql.js bridge functions to globalThis for Go WASM access
60
+ * Must be called after initializeSqlJs() and before loading Go WASM
61
+ */
62
+ export function exposeSqljsBridge() {
63
+ exposeBridgeFunctions();
64
+ // Expose additional npm-specific functions
65
+ const g = globalThis;
66
+ g.loadDocument = loadDocument;
67
+ console.log('[sql.js] Bridge functions exposed to globalThis');
68
+ }
69
+ /**
70
+ * Cleanup all databases and reset state
71
+ */
72
+ export function cleanupSqlJs() {
73
+ cleanupOp();
74
+ }
75
+ /**
76
+ * Load document: loads bytes directly into sql.js and passes handle to Go.
77
+ * Go handles migration if needed. This avoids the bytes round-trip for ALL documents.
78
+ * @param bytes Raw KDDB file bytes (Uint8Array)
79
+ * @returns Document reference or 0 on error
80
+ */
81
+ export function loadDocument(bytes) {
82
+ const g = globalThis;
83
+ // Load directly into sql.js (fast - no bytes round-trip through Go)
84
+ const handle = loadDirectOp(bytes);
85
+ if (handle === 0) {
86
+ console.error('[sql.js] Failed to load database');
87
+ return 0;
88
+ }
89
+ // Pass handle to Go - it will detect and migrate if needed
90
+ return g.createDocumentFromHandle(handle);
91
+ }
92
+ /**
93
+ * Get a database by handle (for debugging from console)
94
+ */
95
+ export function getDatabase(handle) {
96
+ return getDatabaseCore(handle);
97
+ }
98
+ //# sourceMappingURL=sqljs-bridge.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqljs-bridge.js","sourceRoot":"","sources":["../../src/wasm/sqljs-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,SAA0B,MAAM,QAAQ,CAAC;AAChD,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,SAAS,EACT,WAAW,I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ExB,2CAA2C;IAC3C,MAAM,CAAC,GAAG,UAAiB,CAAC;IAC5B,CAAC,CAAC,YAAY,GAAG,YAAY,CAAC;IAE9B,OAAO,CAAC,GAAG,CAAC,iDAAiD,CAAC,CAAC;AACjE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY;IAC1B,SAAS,EAAE,CAAC;AACd,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,KAAiB;IAC5C,MAAM,CAAC,GAAG,UAAiB,CAAC;IAE5B,oEAAoE;IACpE,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;IACnC,IAAI,MAAM,KAAK,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAClD,OAAO,CAAC,CAAC;IACX,CAAC;IAED,2DAA2D;IAC3D,OAAO,CAAC,CAAC,wBAAwB,CAAC,MAAM,CAAC,CAAC;AAC5C,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,MAAc;IACxC,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;AACjC,CAAC"}
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Core sql.js bridge operations - shared between environments
3
+ *
4
+ * This module contains all the database operations that are common between
5
+ * the npm package (sqljs-bridge.ts) and browser CDN (browser-bridge.ts) implementations.
6
+ *
7
+ * The SQL instance is passed in from the wrapper modules, allowing them to
8
+ * load sql.js in different ways (npm import vs CDN global).
9
+ */
10
+ /**
11
+ * Create a new in-memory database
12
+ * @returns Database handle (positive integer) or 0 on error
13
+ */
14
+ export declare function createDatabaseOp(): number;
15
+ /**
16
+ * Execute SQL statement without returning results
17
+ * @param handle Database handle
18
+ * @param sql SQL statement
19
+ * @returns 1 on success, 0 on error
20
+ */
21
+ export declare function execOp(handle: number, sql: string): number;
22
+ /**
23
+ * Execute SQL query with parameters and return results as JSON
24
+ * @param handle Database handle
25
+ * @param sql SQL query with ? placeholders
26
+ * @param paramsJson JSON array of parameters
27
+ * @returns JSON string of results (array of objects) or empty string on error
28
+ */
29
+ export declare function queryOp(handle: number, sql: string, paramsJson: string): string;
30
+ /**
31
+ * Execute INSERT statement and return the last inserted row ID
32
+ * @param handle Database handle
33
+ * @param sql INSERT statement with ? placeholders
34
+ * @param paramsJson JSON array of parameters
35
+ * @returns Last insert row ID or 0 on error
36
+ */
37
+ export declare function insertOp(handle: number, sql: string, paramsJson: string): number;
38
+ /**
39
+ * Execute UPDATE/DELETE statement and return rows affected
40
+ * @param handle Database handle
41
+ * @param sql UPDATE or DELETE statement with ? placeholders
42
+ * @param paramsJson JSON array of parameters
43
+ * @returns Number of rows affected or -1 on error
44
+ */
45
+ export declare function execParamsOp(handle: number, sql: string, paramsJson: string): number;
46
+ /**
47
+ * Export database to binary format
48
+ * @param handle Database handle
49
+ * @returns Base64-encoded database bytes or empty string on error
50
+ */
51
+ export declare function exportOp(handle: number): string;
52
+ /**
53
+ * Import database from binary format
54
+ * @param base64Data Base64-encoded database bytes
55
+ * @returns Database handle or 0 on error
56
+ */
57
+ export declare function importOp(base64Data: string): number;
58
+ /**
59
+ * Load database directly from bytes
60
+ * @param bytes Raw database bytes
61
+ * @returns Database handle or 0 on error
62
+ */
63
+ export declare function loadDirectOp(bytes: Uint8Array): number;
64
+ /**
65
+ * Close a database
66
+ * @param handle Database handle
67
+ */
68
+ export declare function closeOp(handle: number): void;
69
+ /**
70
+ * Get last error message (for debugging)
71
+ * @param _handle Database handle
72
+ * @returns Error message or empty string
73
+ */
74
+ export declare function getErrorOp(_handle: number): string;
75
+ /**
76
+ * Cleanup all databases and reset state
77
+ */
78
+ export declare function cleanupOp(): void;
79
+ /**
80
+ * Set the SQL instance for database operations
81
+ * Must be called before any database operations
82
+ */
83
+ export declare function setSqlInstance(SQL: any): void;
84
+ /**
85
+ * Expose sql.js bridge functions to globalThis for Go WASM access
86
+ */
87
+ export declare function exposeBridgeFunctions(): void;
88
+ /**
89
+ * Get a database by handle (for debugging from console)
90
+ */
91
+ export declare function getDatabase(handle: number): any;
92
+ //# sourceMappingURL=sqljs-core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqljs-core.d.ts","sourceRoot":"","sources":["../../src/wasm/sqljs-core.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAiEH;;;GAGG;AACH,wBAAgB,gBAAgB,IAAI,MAAM,CAgBzC;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAc1D;AAED;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CAgC/E;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CA+BhF;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CA+BpF;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAe/C;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAuBnD;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAetD;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAW5C;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAIlD;AAED;;GAEG;AACH,wBAAgB,SAAS,IAAI,IAAI,CAWhC;AAMD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,GAAG,GAA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