brainy/src/worker.ts
David Snelling f0db5b471f **feat(tests): add tests for TextEncoder, TensorFlow.js, and fallback mechanisms**
- Introduced `test-fallback-function.js` and `test-fallback-simple.js` to validate `executeInThread` fallback functionality with both named and anonymous compute-intensive functions.
- Added `test-tensorflow-textencoder.js` for TensorFlow.js and TextEncoder tests in a Node.js environment.
- Created `test-tensorflow-textencoder.html` for browser-based TensorFlow.js and TextEncoder tests.
- Implemented cross-environment test support in `cli-package/src/test-tensorflow-textencoder.ts` for CLI functionality.
- Enhanced `src/utils/embedding.ts`, `textEncoding.ts`, and `brainy-wrapper.js` to include updated global `TextEncoder` and `TextDecoder` utilities for compatibility and worker improvements.
- Standardized and expanded utility methods in `PlatformNode` for broader support, including `isFloat32Array` and `isTypedArray` checks.
- Updated Node.js requirement to `>= 24.4.0` across documentation and configuration files for compatibility improvements.

This update introduces comprehensive testing for fallback mechanisms, TensorFlow.js, and TextEncoder across multiple environments, ensuring robustness and compatibility.
2025-07-11 11:11:56 -07:00

72 lines
2.2 KiB
TypeScript

// Brainy Worker Script
// This script is used by the workerUtils.js file to execute functions in a separate thread
// Note: TensorFlow.js platform patch is applied in setup.ts
// Worker scripts should import setup.ts if they need TensorFlow.js functionality
// Log that the worker has started
console.log('Brainy Worker: Started')
// Define the message handler with proper TypeScript typing
self.onmessage = function (e: MessageEvent): void {
try {
console.log(
'Brainy Worker: Received message',
e.data ? 'with data' : 'without data'
)
if (!e.data || !e.data.fnString) {
throw new Error('Invalid message: missing function string')
}
console.log('Brainy Worker: Creating function from string')
// Use Function constructor to create a function from the string
let fn
try {
// First try with 'return' prefix
fn = new Function('return ' + e.data.fnString)()
} catch (functionError) {
console.warn(
'Brainy Worker: Error creating function with return syntax, trying alternative approaches',
functionError
)
try {
// Try wrapping in parentheses for function expressions
fn = new Function('return (' + e.data.fnString + ')')()
} catch (wrapError) {
console.warn(
'Brainy Worker: Error creating function with parentheses wrapping',
wrapError
)
try {
// Try direct approach for named functions
fn = new Function(e.data.fnString)()
} catch (directError) {
console.error(
'Brainy Worker: All approaches to create function failed',
directError
)
throw new Error(
'Failed to create function from string: ' +
(functionError as Error).message
)
}
}
}
console.log('Brainy Worker: Executing function with args')
const result = fn(e.data.args)
console.log('Brainy Worker: Function executed successfully, posting result')
self.postMessage({ result: result })
} catch (error: any) {
console.error('Brainy Worker: Error executing function', error)
self.postMessage({
error: error.message,
stack: error.stack
})
}
}