**feat(cli, workers): introduce text encoding patches and worker improvements**
- Added `cli-package/brainy-wrapper.js` to patch global `TextEncoder` and `TextDecoder` for Node.js environments, ensuring compatibility with TensorFlow.js. - Implemented unified text encoding utilities in `cli-package/src/utils/textEncoding.ts` for cross-environment consistency. - Enhanced `src/worker.js` and introduced `src/worker.ts` to improve worker execution with better function serialization and error handling. - Updated `package.json`: - Modified the `build` script to include a patch for `TextEncoder`. - Added `test-all` script for multi-environment testing. - Introduced the Puppeteer dependency for browser testing. - Created a favicon generation script `scripts/create-favicon.js` using a base64-encoded icon. - Added `scripts/test-all-environments.js` to run automated tests across Node.js, browser, and CLI environments. - Enhanced `src/utils/workerUtils.ts` to support improved fallback mechanisms and robust worker pooling. This update improves the project's compatibility across environments, refines worker functionalities, and adds comprehensive testing support for reliability.
This commit is contained in:
parent
0b42290096
commit
7e6718f59b
23 changed files with 2319 additions and 229 deletions
75
src/worker.ts
Normal file
75
src/worker.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// Brainy Worker Script
|
||||
// This script is used by the workerUtils.js file to execute functions in a separate thread
|
||||
|
||||
// Import text encoding utilities
|
||||
import { applyTensorFlowPatch } from './utils/textEncoding.js'
|
||||
|
||||
// Apply the TensorFlow.js platform patch if needed
|
||||
applyTensorFlowPatch()
|
||||
|
||||
// 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
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue