@kreuzberg/wasm 4.0.0-rc.21 → 4.0.0-rc.24
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 +520 -837
- package/dist/adapters/wasm-adapter.d.ts +7 -10
- package/dist/adapters/wasm-adapter.d.ts.map +1 -0
- package/dist/adapters/wasm-adapter.js +41 -19
- package/dist/adapters/wasm-adapter.js.map +1 -1
- package/dist/index.d.ts +23 -24
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +240 -67
- package/dist/index.js.map +1 -1
- package/dist/ocr/registry.d.ts +7 -10
- package/dist/ocr/registry.d.ts.map +1 -0
- package/dist/ocr/registry.js.map +1 -1
- package/dist/ocr/tesseract-wasm-backend.d.ts +3 -6
- package/dist/ocr/tesseract-wasm-backend.d.ts.map +1 -0
- package/dist/ocr/tesseract-wasm-backend.js +0 -46
- package/dist/ocr/tesseract-wasm-backend.js.map +1 -1
- package/dist/pdfium.js +0 -5
- package/dist/plugin-registry.d.ts +246 -0
- package/dist/plugin-registry.d.ts.map +1 -0
- package/dist/runtime.d.ts +21 -22
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +0 -1
- package/dist/runtime.js.map +1 -1
- package/dist/{types-CKjcIYcX.d.ts → types.d.ts} +91 -22
- package/dist/types.d.ts.map +1 -0
- package/package.json +119 -162
- package/dist/adapters/wasm-adapter.cjs +0 -245
- package/dist/adapters/wasm-adapter.cjs.map +0 -1
- package/dist/adapters/wasm-adapter.d.cts +0 -121
- package/dist/index.cjs +0 -1245
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -423
- package/dist/ocr/registry.cjs +0 -92
- package/dist/ocr/registry.cjs.map +0 -1
- package/dist/ocr/registry.d.cts +0 -102
- package/dist/ocr/tesseract-wasm-backend.cjs +0 -456
- package/dist/ocr/tesseract-wasm-backend.cjs.map +0 -1
- package/dist/ocr/tesseract-wasm-backend.d.cts +0 -257
- package/dist/runtime.cjs +0 -174
- package/dist/runtime.cjs.map +0 -1
- package/dist/runtime.d.cts +0 -256
- package/dist/types-CKjcIYcX.d.cts +0 -294
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { O as OcrBackendProtocol } from '../types-CKjcIYcX.cjs';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Tesseract WASM OCR Backend
|
|
5
|
-
*
|
|
6
|
-
* Provides OCR capabilities using tesseract-wasm library for browser environments.
|
|
7
|
-
* Loads training data on-demand from jsDelivr CDN and implements the OcrBackendProtocol.
|
|
8
|
-
*
|
|
9
|
-
* ## Browser-Only Requirement
|
|
10
|
-
*
|
|
11
|
-
* This backend requires browser APIs like createImageBitmap and Web Workers.
|
|
12
|
-
* It will NOT work in Node.js environments without additional canvas polyfills.
|
|
13
|
-
*
|
|
14
|
-
* ## Supported Languages
|
|
15
|
-
*
|
|
16
|
-
* Common ISO 639-1 and ISO 639-2 codes:
|
|
17
|
-
* - English: "eng"
|
|
18
|
-
* - German: "deu"
|
|
19
|
-
* - French: "fra"
|
|
20
|
-
* - Spanish: "spa"
|
|
21
|
-
* - Italian: "ita"
|
|
22
|
-
* - Portuguese: "por"
|
|
23
|
-
* - Dutch: "nld"
|
|
24
|
-
* - Russian: "rus"
|
|
25
|
-
* - Chinese (Simplified): "chi_sim"
|
|
26
|
-
* - Chinese (Traditional): "chi_tra"
|
|
27
|
-
* - Japanese: "jpn"
|
|
28
|
-
* - Korean: "kor"
|
|
29
|
-
* - Arabic: "ara"
|
|
30
|
-
* - Hindi: "hin"
|
|
31
|
-
*
|
|
32
|
-
* For complete language list, see: https://github.com/naptha/tesseract.js
|
|
33
|
-
*
|
|
34
|
-
* @example Basic Usage
|
|
35
|
-
* ```typescript
|
|
36
|
-
* import { TesseractWasmBackend } from '@kreuzberg/wasm/ocr/tesseract-wasm-backend';
|
|
37
|
-
* import { registerOcrBackend, extractBytes, initWasm } from '@kreuzberg/wasm';
|
|
38
|
-
*
|
|
39
|
-
* // Initialize
|
|
40
|
-
* await initWasm();
|
|
41
|
-
* const backend = new TesseractWasmBackend();
|
|
42
|
-
* await backend.initialize();
|
|
43
|
-
* registerOcrBackend(backend);
|
|
44
|
-
*
|
|
45
|
-
* // Use in extraction
|
|
46
|
-
* const imageBytes = new Uint8Array(buffer);
|
|
47
|
-
* const result = await extractBytes(imageBytes, 'image/png', {
|
|
48
|
-
* ocr: { backend: 'tesseract-wasm', language: 'eng' }
|
|
49
|
-
* });
|
|
50
|
-
* console.log(result.content); // Extracted text
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* @example With Language Auto-Detection
|
|
54
|
-
* ```typescript
|
|
55
|
-
* const backend = new TesseractWasmBackend();
|
|
56
|
-
* await backend.initialize();
|
|
57
|
-
* registerOcrBackend(backend);
|
|
58
|
-
*
|
|
59
|
-
* // Extract without specifying language - backend will auto-detect
|
|
60
|
-
* const result = await extractBytes(imageBytes, 'image/png', {
|
|
61
|
-
* ocr: { backend: 'tesseract-wasm' } // language will auto-detect
|
|
62
|
-
* });
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* TesseractWasmBackend - OCR backend using tesseract-wasm library
|
|
68
|
-
*
|
|
69
|
-
* Implements the OcrBackendProtocol for Kreuzberg document extraction pipeline.
|
|
70
|
-
* Provides comprehensive OCR support with model caching, error handling, and progress reporting.
|
|
71
|
-
*/
|
|
72
|
-
declare class TesseractWasmBackend implements OcrBackendProtocol {
|
|
73
|
-
/** Tesseract WASM client instance */
|
|
74
|
-
private client;
|
|
75
|
-
/** Track which models are currently loaded to avoid redundant loads */
|
|
76
|
-
private loadedLanguages;
|
|
77
|
-
/** Cache for language availability validation */
|
|
78
|
-
private supportedLangsCache;
|
|
79
|
-
/** Progress callback for UI updates */
|
|
80
|
-
private progressCallback;
|
|
81
|
-
/** Base URL for training data CDN */
|
|
82
|
-
private readonly CDN_BASE_URL;
|
|
83
|
-
/**
|
|
84
|
-
* Return the unique name of this OCR backend
|
|
85
|
-
*
|
|
86
|
-
* @returns Backend identifier "tesseract-wasm"
|
|
87
|
-
*/
|
|
88
|
-
name(): string;
|
|
89
|
-
/**
|
|
90
|
-
* Return list of supported language codes
|
|
91
|
-
*
|
|
92
|
-
* Returns a curated list of commonly available Tesseract language models.
|
|
93
|
-
* Tesseract supports many more languages through custom models.
|
|
94
|
-
*
|
|
95
|
-
* @returns Array of ISO 639-1/2/3 language codes
|
|
96
|
-
*/
|
|
97
|
-
supportedLanguages(): string[];
|
|
98
|
-
/**
|
|
99
|
-
* Initialize the OCR backend
|
|
100
|
-
*
|
|
101
|
-
* Creates the Tesseract WASM client instance. This is called once when
|
|
102
|
-
* the backend is registered with the extraction pipeline.
|
|
103
|
-
*
|
|
104
|
-
* The actual model loading happens in processImage() on-demand to avoid
|
|
105
|
-
* loading all models upfront.
|
|
106
|
-
*
|
|
107
|
-
* @throws {Error} If tesseract-wasm is not available or initialization fails
|
|
108
|
-
*
|
|
109
|
-
* @example
|
|
110
|
-
* ```typescript
|
|
111
|
-
* const backend = new TesseractWasmBackend();
|
|
112
|
-
* try {
|
|
113
|
-
* await backend.initialize();
|
|
114
|
-
* } catch (error) {
|
|
115
|
-
* console.error('Failed to initialize OCR:', error);
|
|
116
|
-
* }
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
initialize(): Promise<void>;
|
|
120
|
-
/**
|
|
121
|
-
* Process image bytes and extract text via OCR
|
|
122
|
-
*
|
|
123
|
-
* Handles image loading, model loading, OCR processing, and result formatting.
|
|
124
|
-
* Automatically loads the language model on first use and caches it for subsequent calls.
|
|
125
|
-
*
|
|
126
|
-
* @param imageBytes - Raw image data (Uint8Array) or Base64-encoded string
|
|
127
|
-
* @param language - ISO 639-2/3 language code (e.g., "eng", "deu")
|
|
128
|
-
* @returns Promise resolving to OCR result with content and metadata
|
|
129
|
-
* @throws {Error} If image processing fails, model loading fails, or language is unsupported
|
|
130
|
-
*
|
|
131
|
-
* @example
|
|
132
|
-
* ```typescript
|
|
133
|
-
* const backend = new TesseractWasmBackend();
|
|
134
|
-
* await backend.initialize();
|
|
135
|
-
*
|
|
136
|
-
* const imageBuffer = fs.readFileSync('scanned.png');
|
|
137
|
-
* const result = await backend.processImage(
|
|
138
|
-
* new Uint8Array(imageBuffer),
|
|
139
|
-
* 'eng'
|
|
140
|
-
* );
|
|
141
|
-
*
|
|
142
|
-
* console.log(result.content); // Extracted text
|
|
143
|
-
* console.log(result.metadata.confidence); // OCR confidence score
|
|
144
|
-
* ```
|
|
145
|
-
*/
|
|
146
|
-
processImage(imageBytes: Uint8Array | string, language: string): Promise<{
|
|
147
|
-
content: string;
|
|
148
|
-
mime_type: string;
|
|
149
|
-
metadata: Record<string, unknown>;
|
|
150
|
-
tables: unknown[];
|
|
151
|
-
}>;
|
|
152
|
-
/**
|
|
153
|
-
* Shutdown the OCR backend and release resources
|
|
154
|
-
*
|
|
155
|
-
* Properly cleans up the Tesseract WASM client, freeing memory and Web Workers.
|
|
156
|
-
* Called when the backend is unregistered or the application shuts down.
|
|
157
|
-
*
|
|
158
|
-
* @throws {Error} If cleanup fails (errors are logged but not critical)
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* const backend = new TesseractWasmBackend();
|
|
163
|
-
* await backend.initialize();
|
|
164
|
-
* // ... use backend ...
|
|
165
|
-
* await backend.shutdown(); // Clean up resources
|
|
166
|
-
* ```
|
|
167
|
-
*/
|
|
168
|
-
shutdown(): Promise<void>;
|
|
169
|
-
/**
|
|
170
|
-
* Set a progress callback for UI updates
|
|
171
|
-
*
|
|
172
|
-
* Allows the UI to display progress during OCR processing.
|
|
173
|
-
* The callback will be called with values from 0 to 100.
|
|
174
|
-
*
|
|
175
|
-
* @param callback - Function to call with progress percentage
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```typescript
|
|
179
|
-
* const backend = new TesseractWasmBackend();
|
|
180
|
-
* backend.setProgressCallback((progress) => {
|
|
181
|
-
* console.log(`OCR Progress: ${progress}%`);
|
|
182
|
-
* document.getElementById('progress-bar').style.width = `${progress}%`;
|
|
183
|
-
* });
|
|
184
|
-
* ```
|
|
185
|
-
*/
|
|
186
|
-
setProgressCallback(callback: (progress: number) => void): void;
|
|
187
|
-
/**
|
|
188
|
-
* Load language model from CDN
|
|
189
|
-
*
|
|
190
|
-
* Fetches the training data for a specific language from jsDelivr CDN.
|
|
191
|
-
* This is an MVP approach - models are cached by the browser.
|
|
192
|
-
*
|
|
193
|
-
* @param language - ISO 639-2/3 language code
|
|
194
|
-
* @throws {Error} If model download fails or language is not available
|
|
195
|
-
*
|
|
196
|
-
* @internal
|
|
197
|
-
*/
|
|
198
|
-
private loadLanguageModel;
|
|
199
|
-
/**
|
|
200
|
-
* Convert image bytes or Base64 string to ImageBitmap
|
|
201
|
-
*
|
|
202
|
-
* Handles both Uint8Array and Base64-encoded image data, converting to
|
|
203
|
-
* ImageBitmap format required by Tesseract WASM.
|
|
204
|
-
*
|
|
205
|
-
* @param imageBytes - Image data as Uint8Array or Base64 string
|
|
206
|
-
* @returns Promise resolving to ImageBitmap
|
|
207
|
-
* @throws {Error} If conversion fails (browser API not available or invalid image data)
|
|
208
|
-
*
|
|
209
|
-
* @internal
|
|
210
|
-
*/
|
|
211
|
-
private convertToImageBitmap;
|
|
212
|
-
/**
|
|
213
|
-
* Get confidence score from OCR result
|
|
214
|
-
*
|
|
215
|
-
* Attempts to retrieve confidence score from Tesseract.
|
|
216
|
-
* Returns a safe default if unavailable.
|
|
217
|
-
*
|
|
218
|
-
* @returns Confidence score between 0 and 1
|
|
219
|
-
*
|
|
220
|
-
* @internal
|
|
221
|
-
*/
|
|
222
|
-
private getConfidenceScore;
|
|
223
|
-
/**
|
|
224
|
-
* Get page metadata from OCR result
|
|
225
|
-
*
|
|
226
|
-
* Retrieves additional metadata like image dimensions and processing info.
|
|
227
|
-
*
|
|
228
|
-
* @returns Metadata object (may be empty if unavailable)
|
|
229
|
-
*
|
|
230
|
-
* @internal
|
|
231
|
-
*/
|
|
232
|
-
private getPageMetadata;
|
|
233
|
-
/**
|
|
234
|
-
* Dynamically load tesseract-wasm module
|
|
235
|
-
*
|
|
236
|
-
* Uses dynamic import to load tesseract-wasm only when needed,
|
|
237
|
-
* avoiding hard dependency in browser environments where it may not be bundled.
|
|
238
|
-
*
|
|
239
|
-
* @returns tesseract-wasm module object
|
|
240
|
-
* @throws {Error} If module cannot be imported
|
|
241
|
-
*
|
|
242
|
-
* @internal
|
|
243
|
-
*/
|
|
244
|
-
private loadTesseractWasm;
|
|
245
|
-
/**
|
|
246
|
-
* Report progress to progress callback
|
|
247
|
-
*
|
|
248
|
-
* Internal helper for notifying progress updates during OCR processing.
|
|
249
|
-
*
|
|
250
|
-
* @param progress - Progress percentage (0-100)
|
|
251
|
-
*
|
|
252
|
-
* @internal
|
|
253
|
-
*/
|
|
254
|
-
private reportProgress;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export { TesseractWasmBackend };
|
package/dist/runtime.cjs
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// typescript/runtime.ts
|
|
21
|
-
var runtime_exports = {};
|
|
22
|
-
__export(runtime_exports, {
|
|
23
|
-
detectRuntime: () => detectRuntime,
|
|
24
|
-
getRuntimeInfo: () => getRuntimeInfo,
|
|
25
|
-
getRuntimeVersion: () => getRuntimeVersion,
|
|
26
|
-
getWasmCapabilities: () => getWasmCapabilities,
|
|
27
|
-
hasBigInt: () => hasBigInt,
|
|
28
|
-
hasBlob: () => hasBlob,
|
|
29
|
-
hasFileApi: () => hasFileApi,
|
|
30
|
-
hasModuleWorkers: () => hasModuleWorkers,
|
|
31
|
-
hasSharedArrayBuffer: () => hasSharedArrayBuffer,
|
|
32
|
-
hasWasm: () => hasWasm,
|
|
33
|
-
hasWasmStreaming: () => hasWasmStreaming,
|
|
34
|
-
hasWorkers: () => hasWorkers,
|
|
35
|
-
isBrowser: () => isBrowser,
|
|
36
|
-
isBun: () => isBun,
|
|
37
|
-
isDeno: () => isDeno,
|
|
38
|
-
isNode: () => isNode,
|
|
39
|
-
isServerEnvironment: () => isServerEnvironment,
|
|
40
|
-
isWebEnvironment: () => isWebEnvironment
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(runtime_exports);
|
|
43
|
-
function detectRuntime() {
|
|
44
|
-
if (typeof globalThis.Deno !== "undefined") {
|
|
45
|
-
return "deno";
|
|
46
|
-
}
|
|
47
|
-
if (typeof globalThis.Bun !== "undefined") {
|
|
48
|
-
return "bun";
|
|
49
|
-
}
|
|
50
|
-
if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
51
|
-
return "node";
|
|
52
|
-
}
|
|
53
|
-
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
54
|
-
return "browser";
|
|
55
|
-
}
|
|
56
|
-
return "unknown";
|
|
57
|
-
}
|
|
58
|
-
function isBrowser() {
|
|
59
|
-
return detectRuntime() === "browser";
|
|
60
|
-
}
|
|
61
|
-
function isNode() {
|
|
62
|
-
return detectRuntime() === "node";
|
|
63
|
-
}
|
|
64
|
-
function isDeno() {
|
|
65
|
-
return detectRuntime() === "deno";
|
|
66
|
-
}
|
|
67
|
-
function isBun() {
|
|
68
|
-
return detectRuntime() === "bun";
|
|
69
|
-
}
|
|
70
|
-
function isWebEnvironment() {
|
|
71
|
-
const runtime = detectRuntime();
|
|
72
|
-
return runtime === "browser";
|
|
73
|
-
}
|
|
74
|
-
function isServerEnvironment() {
|
|
75
|
-
const runtime = detectRuntime();
|
|
76
|
-
return runtime === "node" || runtime === "deno" || runtime === "bun";
|
|
77
|
-
}
|
|
78
|
-
function hasFileApi() {
|
|
79
|
-
return typeof window !== "undefined" && typeof File !== "undefined" && typeof Blob !== "undefined";
|
|
80
|
-
}
|
|
81
|
-
function hasBlob() {
|
|
82
|
-
return typeof Blob !== "undefined";
|
|
83
|
-
}
|
|
84
|
-
function hasWorkers() {
|
|
85
|
-
return typeof Worker !== "undefined";
|
|
86
|
-
}
|
|
87
|
-
function hasSharedArrayBuffer() {
|
|
88
|
-
return typeof SharedArrayBuffer !== "undefined";
|
|
89
|
-
}
|
|
90
|
-
function hasModuleWorkers() {
|
|
91
|
-
if (!hasWorkers()) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
try {
|
|
95
|
-
const blob = new Blob(['console.log("test")'], {
|
|
96
|
-
type: "application/javascript"
|
|
97
|
-
});
|
|
98
|
-
const workerUrl = URL.createObjectURL(blob);
|
|
99
|
-
try {
|
|
100
|
-
return true;
|
|
101
|
-
} finally {
|
|
102
|
-
URL.revokeObjectURL(workerUrl);
|
|
103
|
-
}
|
|
104
|
-
} catch {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
function hasWasm() {
|
|
109
|
-
return typeof WebAssembly !== "undefined" && WebAssembly.instantiate !== void 0;
|
|
110
|
-
}
|
|
111
|
-
function hasWasmStreaming() {
|
|
112
|
-
return typeof WebAssembly !== "undefined" && WebAssembly.instantiateStreaming !== void 0;
|
|
113
|
-
}
|
|
114
|
-
function hasBigInt() {
|
|
115
|
-
try {
|
|
116
|
-
const test = BigInt("1");
|
|
117
|
-
return typeof test === "bigint";
|
|
118
|
-
} catch {
|
|
119
|
-
return false;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
function getRuntimeVersion() {
|
|
123
|
-
const runtime = detectRuntime();
|
|
124
|
-
switch (runtime) {
|
|
125
|
-
case "node":
|
|
126
|
-
return process.version?.substring(1);
|
|
127
|
-
// Remove 'v' prefix
|
|
128
|
-
case "deno": {
|
|
129
|
-
const deno = globalThis.Deno;
|
|
130
|
-
const version = deno?.version;
|
|
131
|
-
return version?.deno;
|
|
132
|
-
}
|
|
133
|
-
case "bun": {
|
|
134
|
-
const bun = globalThis.Bun;
|
|
135
|
-
return bun?.version;
|
|
136
|
-
}
|
|
137
|
-
default:
|
|
138
|
-
return void 0;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
function getWasmCapabilities() {
|
|
142
|
-
const runtime = detectRuntime();
|
|
143
|
-
const version = getRuntimeVersion();
|
|
144
|
-
const capabilities = {
|
|
145
|
-
runtime,
|
|
146
|
-
hasWasm: hasWasm(),
|
|
147
|
-
hasWasmStreaming: hasWasmStreaming(),
|
|
148
|
-
hasFileApi: hasFileApi(),
|
|
149
|
-
hasBlob: hasBlob(),
|
|
150
|
-
hasWorkers: hasWorkers(),
|
|
151
|
-
hasSharedArrayBuffer: hasSharedArrayBuffer(),
|
|
152
|
-
hasModuleWorkers: hasModuleWorkers(),
|
|
153
|
-
hasBigInt: hasBigInt(),
|
|
154
|
-
...version !== void 0 ? { runtimeVersion: version } : {}
|
|
155
|
-
};
|
|
156
|
-
return capabilities;
|
|
157
|
-
}
|
|
158
|
-
function getRuntimeInfo() {
|
|
159
|
-
const runtime = detectRuntime();
|
|
160
|
-
const capabilities = getWasmCapabilities();
|
|
161
|
-
return {
|
|
162
|
-
runtime,
|
|
163
|
-
isBrowser: isBrowser(),
|
|
164
|
-
isNode: isNode(),
|
|
165
|
-
isDeno: isDeno(),
|
|
166
|
-
isBun: isBun(),
|
|
167
|
-
isWeb: isWebEnvironment(),
|
|
168
|
-
isServer: isServerEnvironment(),
|
|
169
|
-
runtimeVersion: getRuntimeVersion(),
|
|
170
|
-
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : "N/A",
|
|
171
|
-
capabilities
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
//# sourceMappingURL=runtime.cjs.map
|
package/dist/runtime.cjs.map
DELETED
|
@@ -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\trunt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