- Refactored `applyTensorFlowPatch` in `textEncoding.ts` to introduce a unified `Platform` class supporting both Node.js and browser environments with native `TextEncoder`/`TextDecoder`. - Modified global object handling to target `PlatformNode` in Node.js and `PlatformBrowser` in browser environments. - Added `getTextEncoder` and `getTextDecoder` utility functions to simplify text encoding/decoding across platforms. - Introduced `setup.ts` to apply environment patches before other modules load. - Updated CLI entry points (`cli.ts`, `rollup.config.js`) to ensure patching precedes TensorFlow.js usage. - Enhanced test coverage for TextEncoder compatibility with `test-fix.js`. - Adjusted imports in `test-tensorflow-textencoder.ts` to align with the updated `./utils/textEncoding.js`. - Streamlined constructor definitions in `Platform` for improved maintainability. This update ensures robust compatibility for TensorFlow.js by patching and standardizing text encoding/decoding functionality across environments.
47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
// Test script to verify the TextEncoder fix
|
|
import { applyTensorFlowPatch } from './dist/utils/textEncoding.js'
|
|
|
|
console.log('Testing TextEncoder fix...')
|
|
|
|
// Apply the TensorFlow.js platform patch
|
|
applyTensorFlowPatch()
|
|
|
|
// Check if PlatformNode is defined in the global object
|
|
if (typeof global.PlatformNode === 'function') {
|
|
console.log('PlatformNode is defined in the global object')
|
|
|
|
// Create an instance of PlatformNode
|
|
try {
|
|
const platform = new global.PlatformNode()
|
|
console.log('Successfully created PlatformNode instance')
|
|
|
|
// Check if textEncoder is defined
|
|
if (platform.textEncoder) {
|
|
console.log('textEncoder is defined')
|
|
|
|
// Test encoding a string
|
|
const testString = 'Hello, world! 👋'
|
|
const encoded = platform.textEncoder.encode(testString)
|
|
console.log(`Successfully encoded string: ${testString}`)
|
|
console.log(`Encoded: [${encoded}]`)
|
|
|
|
// Test decoding
|
|
const decoded = platform.textDecoder.decode(encoded)
|
|
console.log(`Successfully decoded back to: ${decoded}`)
|
|
|
|
if (testString === decoded) {
|
|
console.log('✅ TextEncoder/TextDecoder test passed!')
|
|
} else {
|
|
console.error('❌ TextEncoder/TextDecoder test failed!')
|
|
}
|
|
} else {
|
|
console.error('textEncoder is not defined in the platform instance')
|
|
}
|
|
} catch (error) {
|
|
console.error('Error creating PlatformNode instance:', error)
|
|
}
|
|
} else {
|
|
console.error('PlatformNode is not defined in the global object')
|
|
}
|
|
|
|
console.log('Test completed')
|