**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 0b42290096
commit 7e6718f59b
23 changed files with 2319 additions and 229 deletions

View file

@ -10,52 +10,12 @@ import type {
PlatformNodeObject
} from '../types/tensorflowTypes.js'
// Define a global PlatformNode class for TensorFlow.js compatibility
// This is needed because TensorFlow.js creates its own PlatformNode instance
// and we need to ensure it uses the correct TextEncoder/TextDecoder
if (
typeof global !== 'undefined' &&
typeof process !== 'undefined' &&
process.versions &&
process.versions.node
) {
try {
// In Node.js v24.3.0+, TextEncoder and TextDecoder are globally available
// Define the PlatformNode class that TensorFlow.js will use
class PlatformNode {
util: any
textEncoder: any
textDecoder: any
// Import the unified text encoding utilities
import { applyTensorFlowPatch } from './textEncoding.js'
constructor() {
// Create a util object with the necessary methods
this.util = {
isFloat32Array,
isTypedArray,
TextEncoder,
TextDecoder
}
// Initialize TextEncoder/TextDecoder instances
this.textEncoder = new TextEncoder()
this.textDecoder = new TextDecoder()
}
}
// Assign the PlatformNode class to the global object
;(global as any).PlatformNode = PlatformNode
// Also create an instance and assign it to global.platformNode (lowercase p)
// Some TensorFlow.js code might look for this
;(global as any).platformNode = new PlatformNode()
console.log(
'Defined global PlatformNode class for TensorFlow.js compatibility'
)
} catch (error) {
console.warn('Failed to define global PlatformNode class:', error)
}
}
// Apply the TensorFlow.js platform patch if needed
// This will define a global PlatformNode class that uses our text encoding utilities
applyTensorFlowPatch()
/**
* Check if an array is a Float32Array