**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:
David Snelling 2025-07-04 14:42:33 -07:00
parent 9293328e72
commit da760a34ed
23 changed files with 2319 additions and 229 deletions

53
cli-package/brainy-wrapper.js Executable file
View file

@ -0,0 +1,53 @@
#!/usr/bin/env node
/**
* Brainy CLI Wrapper
* This script patches the global object to fix TextEncoder issues before loading the CLI
*/
console.log('Brainy running in Node.js environment')
// Define a custom PlatformNode class that doesn't rely on this.util.TextEncoder
if (
typeof global !== 'undefined' &&
typeof process !== 'undefined' &&
process.versions &&
process.versions.node
) {
try {
// Define a PlatformNode class that uses the global TextEncoder/TextDecoder directly
class PlatformNode {
constructor() {
// Create a util object with only the necessary methods
this.util = {
isFloat32Array: (arr) =>
!!(
arr instanceof Float32Array ||
(arr &&
Object.prototype.toString.call(arr) === '[object Float32Array]')
),
isTypedArray: (arr) =>
!!(ArrayBuffer.isView(arr) && !(arr instanceof DataView))
}
// Initialize TextEncoder/TextDecoder instances directly from global
this.textEncoder = new TextEncoder()
this.textDecoder = new TextDecoder()
}
}
// Assign the PlatformNode class to the global object
global.PlatformNode = PlatformNode
// Also create an instance and assign it to global.platformNode (lowercase p)
global.platformNode = new PlatformNode()
} catch (error) {
console.warn('Failed to define global PlatformNode class:', error)
}
}
// Now load and run the actual CLI
import('./dist/cli.js').catch((err) => {
console.error('Error loading CLI:', err)
process.exit(1)
})