@kreuzberg/wasm 4.0.0-rc.6 → 4.0.1

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 (56) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +317 -801
  3. package/dist/adapters/wasm-adapter.d.ts +7 -10
  4. package/dist/adapters/wasm-adapter.d.ts.map +1 -0
  5. package/dist/adapters/wasm-adapter.js +53 -54
  6. package/dist/adapters/wasm-adapter.js.map +1 -1
  7. package/dist/index.d.ts +23 -67
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +1102 -104
  10. package/dist/index.js.map +1 -1
  11. package/dist/ocr/registry.d.ts +7 -10
  12. package/dist/ocr/registry.d.ts.map +1 -0
  13. package/dist/ocr/registry.js +9 -28
  14. package/dist/ocr/registry.js.map +1 -1
  15. package/dist/ocr/tesseract-wasm-backend.d.ts +3 -6
  16. package/dist/ocr/tesseract-wasm-backend.d.ts.map +1 -0
  17. package/dist/ocr/tesseract-wasm-backend.js +8 -83
  18. package/dist/ocr/tesseract-wasm-backend.js.map +1 -1
  19. package/dist/pdfium.js +77 -0
  20. package/dist/pkg/LICENSE +7 -0
  21. package/dist/pkg/README.md +498 -0
  22. package/dist/{kreuzberg_wasm.d.ts → pkg/kreuzberg_wasm.d.ts} +24 -12
  23. package/dist/{kreuzberg_wasm.js → pkg/kreuzberg_wasm.js} +224 -233
  24. package/dist/pkg/kreuzberg_wasm_bg.js +1871 -0
  25. package/dist/{kreuzberg_wasm_bg.wasm → pkg/kreuzberg_wasm_bg.wasm} +0 -0
  26. package/dist/{kreuzberg_wasm_bg.wasm.d.ts → pkg/kreuzberg_wasm_bg.wasm.d.ts} +10 -13
  27. package/dist/pkg/package.json +27 -0
  28. package/dist/plugin-registry.d.ts +246 -0
  29. package/dist/plugin-registry.d.ts.map +1 -0
  30. package/dist/runtime.d.ts +21 -22
  31. package/dist/runtime.d.ts.map +1 -0
  32. package/dist/runtime.js +21 -41
  33. package/dist/runtime.js.map +1 -1
  34. package/dist/types.d.ts +363 -0
  35. package/dist/types.d.ts.map +1 -0
  36. package/package.json +34 -51
  37. package/dist/adapters/wasm-adapter.d.mts +0 -121
  38. package/dist/adapters/wasm-adapter.mjs +0 -221
  39. package/dist/adapters/wasm-adapter.mjs.map +0 -1
  40. package/dist/index.d.mts +0 -466
  41. package/dist/index.mjs +0 -384
  42. package/dist/index.mjs.map +0 -1
  43. package/dist/kreuzberg_wasm.d.mts +0 -758
  44. package/dist/kreuzberg_wasm.mjs +0 -48
  45. package/dist/ocr/registry.d.mts +0 -102
  46. package/dist/ocr/registry.mjs +0 -70
  47. package/dist/ocr/registry.mjs.map +0 -1
  48. package/dist/ocr/tesseract-wasm-backend.d.mts +0 -257
  49. package/dist/ocr/tesseract-wasm-backend.mjs +0 -424
  50. package/dist/ocr/tesseract-wasm-backend.mjs.map +0 -1
  51. package/dist/runtime.d.mts +0 -256
  52. package/dist/runtime.mjs +0 -152
  53. package/dist/runtime.mjs.map +0 -1
  54. package/dist/snippets/wasm-bindgen-rayon-38edf6e439f6d70d/src/workerHelpers.js +0 -107
  55. package/dist/types-GJVIvbPy.d.mts +0 -221
  56. package/dist/types-GJVIvbPy.d.ts +0 -221
@@ -1,256 +0,0 @@
1
- /**
2
- * Runtime detection and environment-specific utilities
3
- *
4
- * This module provides utilities for detecting the JavaScript runtime environment,
5
- * checking for feature availability, and enabling environment-specific WASM loading strategies.
6
- *
7
- * @example Basic Runtime Detection
8
- * ```typescript
9
- * import { detectRuntime, isBrowser, isNode } from '@kreuzberg/wasm/runtime';
10
- *
11
- * if (isBrowser()) {
12
- * console.log('Running in browser');
13
- * } else if (isNode()) {
14
- * console.log('Running in Node.js');
15
- * }
16
- * ```
17
- *
18
- * @example Feature Detection
19
- * ```typescript
20
- * import { hasFileApi, hasWorkers } from '@kreuzberg/wasm/runtime';
21
- *
22
- * if (hasFileApi()) {
23
- * // Can use File API for browser file uploads
24
- * }
25
- *
26
- * if (hasWorkers()) {
27
- * // Can use Web Workers for parallel processing
28
- * }
29
- * ```
30
- */
31
- type RuntimeType = "browser" | "node" | "deno" | "bun" | "unknown";
32
- /**
33
- * WebAssembly capabilities available in the runtime
34
- */
35
- interface WasmCapabilities {
36
- /** Runtime environment type */
37
- runtime: RuntimeType;
38
- /** WebAssembly support available */
39
- hasWasm: boolean;
40
- /** Streaming WebAssembly instantiation available */
41
- hasWasmStreaming: boolean;
42
- /** File API available (browser) */
43
- hasFileApi: boolean;
44
- /** Blob API available */
45
- hasBlob: boolean;
46
- /** Worker support available */
47
- hasWorkers: boolean;
48
- /** SharedArrayBuffer available (may be restricted) */
49
- hasSharedArrayBuffer: boolean;
50
- /** Module Workers available */
51
- hasModuleWorkers: boolean;
52
- /** BigInt support */
53
- hasBigInt: boolean;
54
- /** Specific runtime version if available */
55
- runtimeVersion?: string;
56
- }
57
- /**
58
- * Detect the current JavaScript runtime
59
- *
60
- * Checks for various global objects and properties to determine
61
- * which JavaScript runtime environment is currently executing.
62
- *
63
- * @returns The detected runtime type
64
- *
65
- * @example
66
- * ```typescript
67
- * import { detectRuntime } from '@kreuzberg/wasm/runtime';
68
- *
69
- * const runtime = detectRuntime();
70
- * switch (runtime) {
71
- * case 'browser':
72
- * console.log('Running in browser');
73
- * break;
74
- * case 'node':
75
- * console.log('Running in Node.js');
76
- * break;
77
- * case 'deno':
78
- * console.log('Running in Deno');
79
- * break;
80
- * case 'bun':
81
- * console.log('Running in Bun');
82
- * break;
83
- * }
84
- * ```
85
- */
86
- declare function detectRuntime(): RuntimeType;
87
- /**
88
- * Check if running in a browser environment
89
- *
90
- * @returns True if running in a browser, false otherwise
91
- */
92
- declare function isBrowser(): boolean;
93
- /**
94
- * Check if running in Node.js
95
- *
96
- * @returns True if running in Node.js, false otherwise
97
- */
98
- declare function isNode(): boolean;
99
- /**
100
- * Check if running in Deno
101
- *
102
- * @returns True if running in Deno, false otherwise
103
- */
104
- declare function isDeno(): boolean;
105
- /**
106
- * Check if running in Bun
107
- *
108
- * @returns True if running in Bun, false otherwise
109
- */
110
- declare function isBun(): boolean;
111
- /**
112
- * Check if running in a web environment (browser or similar)
113
- *
114
- * @returns True if running in a web browser, false otherwise
115
- */
116
- declare function isWebEnvironment(): boolean;
117
- /**
118
- * Check if running in a server-like environment (Node.js, Deno, Bun)
119
- *
120
- * @returns True if running on a server runtime, false otherwise
121
- */
122
- declare function isServerEnvironment(): boolean;
123
- /**
124
- * Check if File API is available
125
- *
126
- * The File API is required for handling browser file uploads.
127
- *
128
- * @returns True if File API is available, false otherwise
129
- *
130
- * @example
131
- * ```typescript
132
- * if (hasFileApi()) {
133
- * const fileInput = document.getElementById('file');
134
- * fileInput.addEventListener('change', (e) => {
135
- * const file = e.target.files?.[0];
136
- * // Handle file
137
- * });
138
- * }
139
- * ```
140
- */
141
- declare function hasFileApi(): boolean;
142
- /**
143
- * Check if Blob API is available
144
- *
145
- * @returns True if Blob API is available, false otherwise
146
- */
147
- declare function hasBlob(): boolean;
148
- /**
149
- * Check if Web Workers are available
150
- *
151
- * @returns True if Web Workers can be created, false otherwise
152
- */
153
- declare function hasWorkers(): boolean;
154
- /**
155
- * Check if SharedArrayBuffer is available
156
- *
157
- * Note: SharedArrayBuffer is restricted in some browser contexts
158
- * due to security considerations (Spectre/Meltdown mitigations).
159
- *
160
- * @returns True if SharedArrayBuffer is available, false otherwise
161
- */
162
- declare function hasSharedArrayBuffer(): boolean;
163
- /**
164
- * Check if module workers are available
165
- *
166
- * Module workers allow importing ES modules in worker threads.
167
- *
168
- * @returns True if module workers are supported, false otherwise
169
- */
170
- declare function hasModuleWorkers(): boolean;
171
- /**
172
- * Check if WebAssembly is available
173
- *
174
- * @returns True if WebAssembly is supported, false otherwise
175
- */
176
- declare function hasWasm(): boolean;
177
- /**
178
- * Check if WebAssembly.instantiateStreaming is available
179
- *
180
- * Streaming instantiation is more efficient than buffering the entire WASM module.
181
- *
182
- * @returns True if streaming WebAssembly is supported, false otherwise
183
- */
184
- declare function hasWasmStreaming(): boolean;
185
- /**
186
- * Check if BigInt is available
187
- *
188
- * @returns True if BigInt type is supported, false otherwise
189
- */
190
- declare function hasBigInt(): boolean;
191
- /**
192
- * Get runtime version information
193
- *
194
- * @returns Version string if available, undefined otherwise
195
- *
196
- * @example
197
- * ```typescript
198
- * const version = getRuntimeVersion();
199
- * console.log(`Running on Node ${version}`); // "Running on Node 18.12.0"
200
- * ```
201
- */
202
- declare function getRuntimeVersion(): string | undefined;
203
- /**
204
- * Get comprehensive WebAssembly capabilities for current runtime
205
- *
206
- * Returns detailed information about WASM and related APIs available
207
- * in the current runtime environment.
208
- *
209
- * @returns Object describing available WASM capabilities
210
- *
211
- * @example
212
- * ```typescript
213
- * import { getWasmCapabilities } from '@kreuzberg/wasm/runtime';
214
- *
215
- * const caps = getWasmCapabilities();
216
- * console.log(`WASM available: ${caps.hasWasm}`);
217
- * console.log(`Streaming WASM: ${caps.hasWasmStreaming}`);
218
- * console.log(`Workers available: ${caps.hasWorkers}`);
219
- *
220
- * if (caps.hasWasm && caps.hasWorkers) {
221
- * // Can offload WASM processing to workers
222
- * }
223
- * ```
224
- */
225
- declare function getWasmCapabilities(): WasmCapabilities;
226
- /**
227
- * Get comprehensive runtime information
228
- *
229
- * Returns detailed information about the current runtime environment,
230
- * capabilities, and identifying information.
231
- *
232
- * @returns Object with runtime details and capabilities
233
- *
234
- * @example
235
- * ```typescript
236
- * const info = getRuntimeInfo();
237
- * console.log(info.runtime); // 'browser' | 'node' | 'deno' | 'bun'
238
- * console.log(info.isBrowser); // true/false
239
- * console.log(info.userAgent); // Browser user agent string
240
- * console.log(info.capabilities); // Detailed capability information
241
- * ```
242
- */
243
- declare function getRuntimeInfo(): {
244
- runtime: RuntimeType;
245
- isBrowser: boolean;
246
- isNode: boolean;
247
- isDeno: boolean;
248
- isBun: boolean;
249
- isWeb: boolean;
250
- isServer: boolean;
251
- runtimeVersion: string | undefined;
252
- userAgent: string;
253
- capabilities: WasmCapabilities;
254
- };
255
-
256
- export { type RuntimeType, type WasmCapabilities, detectRuntime, getRuntimeInfo, getRuntimeVersion, getWasmCapabilities, hasBigInt, hasBlob, hasFileApi, hasModuleWorkers, hasSharedArrayBuffer, hasWasm, hasWasmStreaming, hasWorkers, isBrowser, isBun, isDeno, isNode, isServerEnvironment, isWebEnvironment };
package/dist/runtime.mjs DELETED
@@ -1,152 +0,0 @@
1
- function detectRuntime() {
2
- if (typeof globalThis.Deno !== "undefined") {
3
- return "deno";
4
- }
5
- if (typeof globalThis.Bun !== "undefined") {
6
- return "bun";
7
- }
8
- if (typeof process !== "undefined" && process.versions && process.versions.node) {
9
- return "node";
10
- }
11
- if (typeof window !== "undefined" && typeof document !== "undefined") {
12
- return "browser";
13
- }
14
- return "unknown";
15
- }
16
- function isBrowser() {
17
- return detectRuntime() === "browser";
18
- }
19
- function isNode() {
20
- return detectRuntime() === "node";
21
- }
22
- function isDeno() {
23
- return detectRuntime() === "deno";
24
- }
25
- function isBun() {
26
- return detectRuntime() === "bun";
27
- }
28
- function isWebEnvironment() {
29
- const runtime = detectRuntime();
30
- return runtime === "browser";
31
- }
32
- function isServerEnvironment() {
33
- const runtime = detectRuntime();
34
- return runtime === "node" || runtime === "deno" || runtime === "bun";
35
- }
36
- function hasFileApi() {
37
- return typeof window !== "undefined" && typeof File !== "undefined" && typeof Blob !== "undefined";
38
- }
39
- function hasBlob() {
40
- return typeof Blob !== "undefined";
41
- }
42
- function hasWorkers() {
43
- return typeof Worker !== "undefined";
44
- }
45
- function hasSharedArrayBuffer() {
46
- return typeof SharedArrayBuffer !== "undefined";
47
- }
48
- function hasModuleWorkers() {
49
- if (!hasWorkers()) {
50
- return false;
51
- }
52
- try {
53
- const blob = new Blob(['console.log("test")'], {
54
- type: "application/javascript"
55
- });
56
- const workerUrl = URL.createObjectURL(blob);
57
- try {
58
- return true;
59
- } finally {
60
- URL.revokeObjectURL(workerUrl);
61
- }
62
- } catch {
63
- return false;
64
- }
65
- }
66
- function hasWasm() {
67
- return typeof WebAssembly !== "undefined" && WebAssembly.instantiate !== void 0;
68
- }
69
- function hasWasmStreaming() {
70
- return typeof WebAssembly !== "undefined" && WebAssembly.instantiateStreaming !== void 0;
71
- }
72
- function hasBigInt() {
73
- try {
74
- const test = BigInt("1");
75
- return typeof test === "bigint";
76
- } catch {
77
- return false;
78
- }
79
- }
80
- function getRuntimeVersion() {
81
- const runtime = detectRuntime();
82
- switch (runtime) {
83
- case "node":
84
- return process.version?.substring(1);
85
- // Remove 'v' prefix
86
- case "deno": {
87
- const deno = globalThis.Deno;
88
- const version = deno?.version;
89
- return version?.deno;
90
- }
91
- case "bun": {
92
- const bun = globalThis.Bun;
93
- return bun?.version;
94
- }
95
- default:
96
- return void 0;
97
- }
98
- }
99
- function getWasmCapabilities() {
100
- const runtime = detectRuntime();
101
- const version = getRuntimeVersion();
102
- const capabilities = {
103
- runtime,
104
- hasWasm: hasWasm(),
105
- hasWasmStreaming: hasWasmStreaming(),
106
- hasFileApi: hasFileApi(),
107
- hasBlob: hasBlob(),
108
- hasWorkers: hasWorkers(),
109
- hasSharedArrayBuffer: hasSharedArrayBuffer(),
110
- hasModuleWorkers: hasModuleWorkers(),
111
- hasBigInt: hasBigInt(),
112
- ...version !== void 0 ? { runtimeVersion: version } : {}
113
- };
114
- return capabilities;
115
- }
116
- function getRuntimeInfo() {
117
- const runtime = detectRuntime();
118
- const capabilities = getWasmCapabilities();
119
- return {
120
- runtime,
121
- isBrowser: isBrowser(),
122
- isNode: isNode(),
123
- isDeno: isDeno(),
124
- isBun: isBun(),
125
- isWeb: isWebEnvironment(),
126
- isServer: isServerEnvironment(),
127
- runtimeVersion: getRuntimeVersion(),
128
- userAgent: typeof navigator !== "undefined" ? navigator.userAgent : "N/A",
129
- capabilities
130
- };
131
- }
132
- export {
133
- detectRuntime,
134
- getRuntimeInfo,
135
- getRuntimeVersion,
136
- getWasmCapabilities,
137
- hasBigInt,
138
- hasBlob,
139
- hasFileApi,
140
- hasModuleWorkers,
141
- hasSharedArrayBuffer,
142
- hasWasm,
143
- hasWasmStreaming,
144
- hasWorkers,
145
- isBrowser,
146
- isBun,
147
- isDeno,
148
- isNode,
149
- isServerEnvironment,
150
- isWebEnvironment
151
- };
152
- //# sourceMappingURL=runtime.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../typescript/runtime.ts"],"sourcesContent":["/**\n * Runtime detection and environment-specific utilities\n *\n * This module provides utilities for detecting the JavaScript runtime environment,\n * checking for feature availability, and enabling environment-specific WASM loading strategies.\n *\n * @example Basic Runtime Detection\n * ```typescript\n * import { detectRuntime, isBrowser, isNode } from '@kreuzberg/wasm/runtime';\n *\n * if (isBrowser()) {\n * console.log('Running in browser');\n * } else if (isNode()) {\n * console.log('Running in Node.js');\n * }\n * ```\n *\n * @example Feature Detection\n * ```typescript\n * import { hasFileApi, hasWorkers } from '@kreuzberg/wasm/runtime';\n *\n * if (hasFileApi()) {\n * // Can use File API for browser file uploads\n * }\n *\n * if (hasWorkers()) {\n * // Can use Web Workers for parallel processing\n * }\n * ```\n */\n\nexport type RuntimeType = \"browser\" | \"node\" | \"deno\" | \"bun\" | \"unknown\";\n\n/**\n * WebAssembly capabilities available in the runtime\n */\nexport interface WasmCapabilities {\n\t/** Runtime environment type */\n\truntime: RuntimeType;\n\t/** WebAssembly support available */\n\thasWasm: boolean;\n\t/** Streaming WebAssembly instantiation available */\n\thasWasmStreaming: boolean;\n\t/** File API available (browser) */\n\thasFileApi: boolean;\n\t/** Blob API available */\n\thasBlob: boolean;\n\t/** Worker support available */\n\thasWorkers: boolean;\n\t/** SharedArrayBuffer available (may be restricted) */\n\thasSharedArrayBuffer: boolean;\n\t/** Module Workers available */\n\thasModuleWorkers: boolean;\n\t/** BigInt support */\n\thasBigInt: boolean;\n\t/** Specific runtime version if available */\n\truntimeVersion?: string;\n}\n\n/**\n * Detect the current JavaScript runtime\n *\n * Checks for various global objects and properties to determine\n * which JavaScript runtime environment is currently executing.\n *\n * @returns The detected runtime type\n *\n * @example\n * ```typescript\n * import { detectRuntime } from '@kreuzberg/wasm/runtime';\n *\n * const runtime = detectRuntime();\n * switch (runtime) {\n * case 'browser':\n * console.log('Running in browser');\n * break;\n * case 'node':\n * console.log('Running in Node.js');\n * break;\n * case 'deno':\n * console.log('Running in Deno');\n * break;\n * case 'bun':\n * console.log('Running in Bun');\n * break;\n * }\n * ```\n */\nexport function detectRuntime(): RuntimeType {\n\t// Check for Deno\n\tif (typeof (globalThis as unknown as Record<string, unknown>).Deno !== \"undefined\") {\n\t\treturn \"deno\";\n\t}\n\n\t// Check for Bun\n\tif (typeof (globalThis as unknown as Record<string, unknown>).Bun !== \"undefined\") {\n\t\treturn \"bun\";\n\t}\n\n\t// Check for Node.js\n\tif (typeof process !== \"undefined\" && process.versions && process.versions.node) {\n\t\treturn \"node\";\n\t}\n\n\t// Check for browser\n\tif (typeof window !== \"undefined\" && typeof document !== \"undefined\") {\n\t\treturn \"browser\";\n\t}\n\n\treturn \"unknown\";\n}\n\n/**\n * Check if running in a browser environment\n *\n * @returns True if running in a browser, false otherwise\n */\nexport function isBrowser(): boolean {\n\treturn detectRuntime() === \"browser\";\n}\n\n/**\n * Check if running in Node.js\n *\n * @returns True if running in Node.js, false otherwise\n */\nexport function isNode(): boolean {\n\treturn detectRuntime() === \"node\";\n}\n\n/**\n * Check if running in Deno\n *\n * @returns True if running in Deno, false otherwise\n */\nexport function isDeno(): boolean {\n\treturn detectRuntime() === \"deno\";\n}\n\n/**\n * Check if running in Bun\n *\n * @returns True if running in Bun, false otherwise\n */\nexport function isBun(): boolean {\n\treturn detectRuntime() === \"bun\";\n}\n\n/**\n * Check if running in a web environment (browser or similar)\n *\n * @returns True if running in a web browser, false otherwise\n */\nexport function isWebEnvironment(): boolean {\n\tconst runtime = detectRuntime();\n\treturn runtime === \"browser\";\n}\n\n/**\n * Check if running in a server-like environment (Node.js, Deno, Bun)\n *\n * @returns True if running on a server runtime, false otherwise\n */\nexport function isServerEnvironment(): boolean {\n\tconst runtime = detectRuntime();\n\treturn runtime === \"node\" || runtime === \"deno\" || runtime === \"bun\";\n}\n\n/**\n * Check if File API is available\n *\n * The File API is required for handling browser file uploads.\n *\n * @returns True if File API is available, false otherwise\n *\n * @example\n * ```typescript\n * if (hasFileApi()) {\n * const fileInput = document.getElementById('file');\n * fileInput.addEventListener('change', (e) => {\n * const file = e.target.files?.[0];\n * // Handle file\n * });\n * }\n * ```\n */\nexport function hasFileApi(): boolean {\n\treturn typeof window !== \"undefined\" && typeof File !== \"undefined\" && typeof Blob !== \"undefined\";\n}\n\n/**\n * Check if Blob API is available\n *\n * @returns True if Blob API is available, false otherwise\n */\nexport function hasBlob(): boolean {\n\treturn typeof Blob !== \"undefined\";\n}\n\n/**\n * Check if Web Workers are available\n *\n * @returns True if Web Workers can be created, false otherwise\n */\nexport function hasWorkers(): boolean {\n\treturn typeof Worker !== \"undefined\";\n}\n\n/**\n * Check if SharedArrayBuffer is available\n *\n * Note: SharedArrayBuffer is restricted in some browser contexts\n * due to security considerations (Spectre/Meltdown mitigations).\n *\n * @returns True if SharedArrayBuffer is available, false otherwise\n */\nexport function hasSharedArrayBuffer(): boolean {\n\treturn typeof SharedArrayBuffer !== \"undefined\";\n}\n\n/**\n * Check if module workers are available\n *\n * Module workers allow importing ES modules in worker threads.\n *\n * @returns True if module workers are supported, false otherwise\n */\nexport function hasModuleWorkers(): boolean {\n\tif (!hasWorkers()) {\n\t\treturn false;\n\t}\n\n\ttry {\n\t\t// Try to detect module worker support\n\t\tconst blob = new Blob(['console.log(\"test\")'], {\n\t\t\ttype: \"application/javascript\",\n\t\t});\n\t\tconst workerUrl = URL.createObjectURL(blob);\n\t\ttry {\n\t\t\t// Module workers require type: 'module' option\n\t\t\t// We can't actually instantiate without issues, so we check the API exists\n\t\t\treturn true;\n\t\t} finally {\n\t\t\tURL.revokeObjectURL(workerUrl);\n\t\t}\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Check if WebAssembly is available\n *\n * @returns True if WebAssembly is supported, false otherwise\n */\nexport function hasWasm(): boolean {\n\treturn typeof WebAssembly !== \"undefined\" && WebAssembly.instantiate !== undefined;\n}\n\n/**\n * Check if WebAssembly.instantiateStreaming is available\n *\n * Streaming instantiation is more efficient than buffering the entire WASM module.\n *\n * @returns True if streaming WebAssembly is supported, false otherwise\n */\nexport function hasWasmStreaming(): boolean {\n\treturn typeof WebAssembly !== \"undefined\" && WebAssembly.instantiateStreaming !== undefined;\n}\n\n/**\n * Check if BigInt is available\n *\n * @returns True if BigInt type is supported, false otherwise\n */\nexport function hasBigInt(): boolean {\n\ttry {\n\t\tconst test = BigInt(\"1\");\n\t\treturn typeof test === \"bigint\";\n\t} catch {\n\t\treturn false;\n\t}\n}\n\n/**\n * Get runtime version information\n *\n * @returns Version string if available, undefined otherwise\n *\n * @example\n * ```typescript\n * const version = getRuntimeVersion();\n * console.log(`Running on Node ${version}`); // \"Running on Node 18.12.0\"\n * ```\n */\nexport function getRuntimeVersion(): string | undefined {\n\tconst runtime = detectRuntime();\n\n\tswitch (runtime) {\n\t\tcase \"node\":\n\t\t\treturn process.version?.substring(1); // Remove 'v' prefix\n\t\tcase \"deno\": {\n\t\t\tconst deno = (globalThis as unknown as Record<string, unknown>).Deno as Record<string, unknown> | undefined;\n\t\t\tconst version = deno?.version as Record<string, unknown> | undefined;\n\t\t\treturn version?.deno as string | undefined;\n\t\t}\n\t\tcase \"bun\": {\n\t\t\tconst bun = (globalThis as unknown as Record<string, unknown>).Bun as Record<string, unknown> | undefined;\n\t\t\treturn bun?.version as string | undefined;\n\t\t}\n\t\tdefault:\n\t\t\treturn undefined;\n\t}\n}\n\n/**\n * Get comprehensive WebAssembly capabilities for current runtime\n *\n * Returns detailed information about WASM and related APIs available\n * in the current runtime environment.\n *\n * @returns Object describing available WASM capabilities\n *\n * @example\n * ```typescript\n * import { getWasmCapabilities } from '@kreuzberg/wasm/runtime';\n *\n * const caps = getWasmCapabilities();\n * console.log(`WASM available: ${caps.hasWasm}`);\n * console.log(`Streaming WASM: ${caps.hasWasmStreaming}`);\n * console.log(`Workers available: ${caps.hasWorkers}`);\n *\n * if (caps.hasWasm && caps.hasWorkers) {\n * // Can offload WASM processing to workers\n * }\n * ```\n */\nexport function getWasmCapabilities(): WasmCapabilities {\n\tconst runtime = detectRuntime();\n\tconst version = getRuntimeVersion();\n\tconst capabilities: WasmCapabilities = {\n\t\truntime,\n\t\thasWasm: hasWasm(),\n\t\thasWasmStreaming: hasWasmStreaming(),\n\t\thasFileApi: hasFileApi(),\n\t\thasBlob: hasBlob(),\n\t\thasWorkers: hasWorkers(),\n\t\thasSharedArrayBuffer: hasSharedArrayBuffer(),\n\t\thasModuleWorkers: hasModuleWorkers(),\n\t\thasBigInt: hasBigInt(),\n\t\t...(version !== undefined ? { runtimeVersion: version } : {}),\n\t};\n\treturn capabilities;\n}\n\n/**\n * Get comprehensive runtime information\n *\n * Returns detailed information about the current runtime environment,\n * capabilities, and identifying information.\n *\n * @returns Object with runtime details and capabilities\n *\n * @example\n * ```typescript\n * const info = getRuntimeInfo();\n * console.log(info.runtime); // 'browser' | 'node' | 'deno' | 'bun'\n * console.log(info.isBrowser); // true/false\n * console.log(info.userAgent); // Browser user agent string\n * console.log(info.capabilities); // Detailed capability information\n * ```\n */\nexport function getRuntimeInfo() {\n\tconst runtime = detectRuntime();\n\tconst capabilities = getWasmCapabilities();\n\n\treturn {\n\t\truntime,\n\t\tisBrowser: isBrowser(),\n\t\tisNode: isNode(),\n\t\tisDeno: isDeno(),\n\t\tisBun: isBun(),\n\t\tisWeb: isWebEnvironment(),\n\t\tisServer: isServerEnvironment(),\n\t\truntimeVersion: getRuntimeVersion(),\n\t\tuserAgent: typeof navigator !== \"undefined\" ? navigator.userAgent : \"N/A\",\n\t\tcapabilities,\n\t};\n}\n"],"mappings":"AAwFO,SAAS,gBAA6B;AAE5C,MAAI,OAAQ,WAAkD,SAAS,aAAa;AACnF,WAAO;AAAA,EACR;AAGA,MAAI,OAAQ,WAAkD,QAAQ,aAAa;AAClF,WAAO;AAA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UAAU,YAAY;AAAA,IACpE;AAAA,EACD;AACD;","names":[]}
@@ -1,107 +0,0 @@
1
- /*
2
- * Copyright 2022 Google Inc. All Rights Reserved.
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- * http://www.apache.org/licenses/LICENSE-2.0
7
- * Unless required by applicable law or agreed to in writing, software
8
- * distributed under the License is distributed on an "AS IS" BASIS,
9
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
- * See the License for the specific language governing permissions and
11
- * limitations under the License.
12
- */
13
-
14
- // Note: we use `wasm_bindgen_worker_`-prefixed message types to make sure
15
- // we can handle bundling into other files, which might happen to have their
16
- // own `postMessage`/`onmessage` communication channels.
17
- //
18
- // If we didn't take that into the account, we could send much simpler signals
19
- // like just `0` or whatever, but the code would be less resilient.
20
-
21
- function waitForMsgType(target, type) {
22
- return new Promise(resolve => {
23
- target.addEventListener('message', function onMsg({ data }) {
24
- if (data?.type !== type) return;
25
- target.removeEventListener('message', onMsg);
26
- resolve(data);
27
- });
28
- });
29
- }
30
-
31
- waitForMsgType(self, 'wasm_bindgen_worker_init').then(async ({ init, receiver }) => {
32
- // # Note 1
33
- // Our JS should have been generated in
34
- // `[out-dir]/snippets/wasm-bindgen-rayon-[hash]/workerHelpers.js`,
35
- // resolve the main module via `../../..`.
36
- //
37
- // This might need updating if the generated structure changes on wasm-bindgen
38
- // side ever in the future, but works well with bundlers today. The whole
39
- // point of this crate, after all, is to abstract away unstable features
40
- // and temporary bugs so that you don't need to deal with them in your code.
41
- //
42
- // # Note 2
43
- // This could be a regular import, but then some bundlers complain about
44
- // circular deps.
45
- //
46
- // Dynamic import could be cheap if this file was inlined into the parent,
47
- // which would require us just using `../../..` in `new Worker` below,
48
- // but that doesn't work because wasm-pack unconditionally adds
49
- // "sideEffects":false (see below).
50
- //
51
- // OTOH, even though it can't be inlined, it should be still reasonably
52
- // cheap since the requested file is already in cache (it was loaded by
53
- // the main thread).
54
- const pkg = await import('../../..');
55
- await pkg.default(init);
56
- postMessage({ type: 'wasm_bindgen_worker_ready' });
57
- pkg.wbg_rayon_start_worker(receiver);
58
- });
59
-
60
- // Note: this is never used, but necessary to prevent a bug in Firefox
61
- // (https://bugzilla.mozilla.org/show_bug.cgi?id=1702191) where it collects
62
- // Web Workers that have a shared WebAssembly memory with the main thread,
63
- // but are not explicitly rooted via a `Worker` instance.
64
- //
65
- // By storing them in a variable, we can keep `Worker` objects around and
66
- // prevent them from getting GC-d.
67
- let _workers;
68
-
69
- export async function startWorkers(module, memory, builder) {
70
- if (builder.numThreads() === 0) {
71
- throw new Error(`num_threads must be > 0.`);
72
- }
73
-
74
- const workerInit = {
75
- type: 'wasm_bindgen_worker_init',
76
- init: { module_or_path: module, memory },
77
- receiver: builder.receiver()
78
- };
79
-
80
- _workers = await Promise.all(
81
- Array.from({ length: builder.numThreads() }, async () => {
82
- // Self-spawn into a new Worker.
83
- //
84
- // TODO: while `new URL('...', import.meta.url) becomes a semi-standard
85
- // way to get asset URLs relative to the module across various bundlers
86
- // and browser, ideally we should switch to `import.meta.resolve`
87
- // once it becomes a standard.
88
- //
89
- // Note: we could use `../../..` as the URL here to inline workerHelpers.js
90
- // into the parent entry instead of creating another split point -
91
- // this would be preferable from optimization perspective -
92
- // however, Webpack then eliminates all message handler code
93
- // because wasm-pack produces "sideEffects":false in package.json
94
- // unconditionally.
95
- //
96
- // The only way to work around that is to have side effect code
97
- // in an entry point such as Worker file itself.
98
- const worker = new Worker(new URL('./workerHelpers.js', import.meta.url), {
99
- type: 'module'
100
- });
101
- worker.postMessage(workerInit);
102
- await waitForMsgType(worker, 'wasm_bindgen_worker_ready');
103
- return worker;
104
- })
105
- );
106
- builder.build();
107
- }