2025-07-04 14:42:33 -07:00
|
|
|
/**
|
|
|
|
|
* Unified Text Encoding Utilities
|
|
|
|
|
*
|
|
|
|
|
* This module provides a consistent way to handle text encoding/decoding across all environments
|
2025-07-11 11:11:56 -07:00
|
|
|
* using the native TextEncoder/TextDecoder APIs.
|
2025-07-04 14:42:33 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a text encoder that works in the current environment
|
2025-07-11 11:11:56 -07:00
|
|
|
* @returns A TextEncoder instance
|
2025-07-04 14:42:33 -07:00
|
|
|
*/
|
2025-07-11 11:11:56 -07:00
|
|
|
export function getTextEncoder(): TextEncoder {
|
|
|
|
|
return new TextEncoder()
|
2025-07-04 14:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a text decoder that works in the current environment
|
2025-07-11 11:11:56 -07:00
|
|
|
* @returns A TextDecoder instance
|
2025-07-04 14:42:33 -07:00
|
|
|
*/
|
2025-07-11 11:11:56 -07:00
|
|
|
export function getTextDecoder(): TextDecoder {
|
|
|
|
|
return new TextDecoder()
|
2025-07-04 14:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Apply the TensorFlow.js platform patch if needed
|
|
|
|
|
* This function patches the global object to provide a PlatformNode class
|
2025-07-11 11:11:56 -07:00
|
|
|
* that uses native TextEncoder/TextDecoder
|
2025-07-04 14:42:33 -07:00
|
|
|
*/
|
|
|
|
|
export function applyTensorFlowPatch(): void {
|
2025-07-07 10:19:04 -07:00
|
|
|
try {
|
|
|
|
|
// Define a custom Platform class that works in both Node.js and browser environments
|
|
|
|
|
class Platform {
|
|
|
|
|
util: any
|
2025-07-11 11:11:56 -07:00
|
|
|
textEncoder: TextEncoder
|
|
|
|
|
textDecoder: TextDecoder
|
2025-07-07 10:19:04 -07:00
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
// Create a util object with necessary methods and constructors
|
|
|
|
|
this.util = {
|
2025-07-11 11:11:56 -07:00
|
|
|
// Use native TextEncoder and TextDecoder
|
|
|
|
|
TextEncoder: TextEncoder,
|
|
|
|
|
TextDecoder: TextDecoder
|
2025-07-04 14:42:33 -07:00
|
|
|
}
|
2025-07-07 10:19:04 -07:00
|
|
|
|
2025-07-11 11:11:56 -07:00
|
|
|
// Initialize using native constructors
|
|
|
|
|
this.textEncoder = new TextEncoder()
|
|
|
|
|
this.textDecoder = new TextDecoder()
|
2025-07-07 10:19:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Define isFloat32Array directly on the instance
|
|
|
|
|
isFloat32Array(arr: any) {
|
|
|
|
|
return !!(
|
|
|
|
|
arr instanceof Float32Array ||
|
|
|
|
|
(arr &&
|
2025-07-11 11:11:56 -07:00
|
|
|
Object.prototype.toString.call(arr) === '[object Float32Array]')
|
2025-07-07 10:19:04 -07:00
|
|
|
)
|
2025-07-04 14:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 10:19:04 -07:00
|
|
|
// Define isTypedArray directly on the instance
|
|
|
|
|
isTypedArray(arr: any) {
|
|
|
|
|
return !!(ArrayBuffer.isView(arr) && !(arr instanceof DataView))
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-04 14:42:33 -07:00
|
|
|
|
2025-07-07 10:19:04 -07:00
|
|
|
// Get the global object in a way that works in both Node.js and browser
|
2025-07-11 11:11:56 -07:00
|
|
|
const globalObj =
|
|
|
|
|
typeof global !== 'undefined'
|
|
|
|
|
? global
|
|
|
|
|
: typeof window !== 'undefined'
|
|
|
|
|
? window
|
|
|
|
|
: typeof self !== 'undefined'
|
|
|
|
|
? self
|
|
|
|
|
: {}
|
2025-07-07 10:19:04 -07:00
|
|
|
|
|
|
|
|
// Only apply in Node.js environment
|
|
|
|
|
if (
|
|
|
|
|
typeof process !== 'undefined' &&
|
|
|
|
|
process.versions &&
|
|
|
|
|
process.versions.node
|
|
|
|
|
) {
|
|
|
|
|
// Assign the Platform class to the global object as PlatformNode for Node.js
|
2025-07-11 11:11:56 -07:00
|
|
|
;(globalObj as any).PlatformNode = Platform
|
2025-07-04 14:42:33 -07:00
|
|
|
// Also create an instance and assign it to global.platformNode (lowercase p)
|
2025-07-11 11:11:56 -07:00
|
|
|
;(globalObj as any).platformNode = new Platform()
|
2025-07-07 10:19:04 -07:00
|
|
|
} else if (typeof window !== 'undefined' || typeof self !== 'undefined') {
|
|
|
|
|
// In browser environments, we might need to provide similar functionality
|
|
|
|
|
// but we'll use a different name to avoid conflicts
|
2025-07-11 11:11:56 -07:00
|
|
|
;(globalObj as any).PlatformBrowser = Platform
|
|
|
|
|
;(globalObj as any).platformBrowser = new Platform()
|
2025-07-04 14:42:33 -07:00
|
|
|
}
|
2025-07-07 10:19:04 -07:00
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Failed to apply TensorFlow.js platform patch:', error)
|
2025-07-04 14:42:33 -07:00
|
|
|
}
|
|
|
|
|
}
|