brainy/scripts/patch-textencoder.js
David Snelling 04a33b9ae8 **chore(release): bump version to 0.10.0 and refactor project structure**
- Updated version to `0.10.0` in `README.md`, `cli-package/package.json`, and `src/utils/version.ts`.
- Removed `CHANGELOG.md` from the root and `cli-package`, as GitHub auto-generates release notes.
- Refactored `brainy-wrapper.js` to simplify utility methods (`isFloat32Array` and `isTypedArray`) by moving them to instance-level definitions.
- Standardized Node.js version requirement to `>= 24.3.0` across documentation and configuration files.
- Improved `textEncoding.ts` by introducing a unified `Platform` class for cross-environment compatibility (Node.js and browser).
- Restructured scripts:
  - Replaced outdated commands (e.g., `test-cli` -> `test:cli` and `test-all` -> `test:all`) for consistency.
  - Removed changelog updates from `scripts/create-github-release.js` as part of simplification.
- Enhanced `patch-textencoder.js` to also patch `worker.js` for both `TextEncoder` and `TextDecoder`.

This release improves compatibility with Node.js 24, simplifies the deployment/changelog process, and refines text encoding and worker functionalities.
2025-07-07 10:19:04 -07:00

141 lines
4.5 KiB
JavaScript
Executable file

#!/usr/bin/env node
/**
* Simplified TextEncoder Patch
*
* This script patches the compiled unified.js file to fix the TextEncoder issue in Node.js
* by replacing references to this.util.TextEncoder with direct TextEncoder usage.
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// Path to the compiled unified.js file
const unifiedJsPath = path.join(__dirname, '..', 'dist', 'unified.js')
// Read the file
console.log(`Reading ${unifiedJsPath}...`)
let content = fs.readFileSync(unifiedJsPath, 'utf8')
// Simple replacement: replace all instances of new this.util.TextEncoder() with new TextEncoder()
const pattern = /new\s+this\.util\.TextEncoder\(\)/g
const replacement = 'new TextEncoder()'
// Apply the patch
const patchedContent = content.replace(pattern, replacement)
// Check if the patch was applied
if (patchedContent === content) {
console.warn(
'No instances of "new this.util.TextEncoder()" found in the file.'
)
} else {
// Write the patched file
console.log('Writing patched file...')
fs.writeFileSync(unifiedJsPath, patchedContent, 'utf8')
console.log('Patch applied successfully!')
}
// Also patch the minified version if it exists
const minifiedJsPath = path.join(__dirname, '..', 'dist', 'unified.min.js')
if (fs.existsSync(minifiedJsPath)) {
console.log(`Reading ${minifiedJsPath}...`)
const minContent = fs.readFileSync(minifiedJsPath, 'utf8')
// Apply the same replacement to the minified file
const patchedMinContent = minContent.replace(pattern, replacement)
// Check if the patch was applied
if (patchedMinContent === minContent) {
console.warn(
'No instances of "new this.util.TextEncoder()" found in the minified file.'
)
} else {
// Write the patched file
console.log('Writing patched minified file...')
fs.writeFileSync(minifiedJsPath, patchedMinContent, 'utf8')
console.log('Minified file patched successfully!')
}
}
// Also patch the worker.js file if it exists
const workerJsPath = path.join(__dirname, '..', 'dist', 'worker.js')
if (fs.existsSync(workerJsPath)) {
console.log(`Reading ${workerJsPath}...`)
const workerContent = fs.readFileSync(workerJsPath, 'utf8')
// Apply the same replacement to the worker file
const patchedWorkerContent = workerContent.replace(pattern, replacement)
// Check if the patch was applied
if (patchedWorkerContent === workerContent) {
console.warn(
'No instances of "new this.util.TextEncoder()" found in the worker file.'
)
} else {
// Write the patched file
console.log('Writing patched worker file...')
fs.writeFileSync(workerJsPath, patchedWorkerContent, 'utf8')
console.log('Worker file patched successfully!')
}
}
// Also patch TextDecoder
console.log('Patching TextDecoder references...')
content = fs.readFileSync(unifiedJsPath, 'utf8')
const decoderPattern = /new\s+this\.util\.TextDecoder\(\)/g
const decoderReplacement = 'new TextDecoder()'
const patchedDecoderContent = content.replace(
decoderPattern,
decoderReplacement
)
if (patchedDecoderContent === content) {
console.warn(
'No instances of "new this.util.TextDecoder()" found in the file.'
)
} else {
fs.writeFileSync(unifiedJsPath, patchedDecoderContent, 'utf8')
console.log('TextDecoder patch applied successfully!')
}
// Patch the minified file for TextDecoder as well
if (fs.existsSync(minifiedJsPath)) {
const minContent = fs.readFileSync(minifiedJsPath, 'utf8')
const patchedMinDecoderContent = minContent.replace(
decoderPattern,
decoderReplacement
)
if (patchedMinDecoderContent === minContent) {
console.warn(
'No instances of "new this.util.TextDecoder()" found in the minified file.'
)
} else {
fs.writeFileSync(minifiedJsPath, patchedMinDecoderContent, 'utf8')
console.log('TextDecoder patch applied to minified file successfully!')
}
}
// Patch the worker.js file for TextDecoder as well
if (fs.existsSync(workerJsPath)) {
const workerContent = fs.readFileSync(workerJsPath, 'utf8')
const patchedWorkerDecoderContent = workerContent.replace(
decoderPattern,
decoderReplacement
)
if (patchedWorkerDecoderContent === workerContent) {
console.warn(
'No instances of "new this.util.TextDecoder()" found in the worker file.'
)
} else {
fs.writeFileSync(workerJsPath, patchedWorkerDecoderContent, 'utf8')
console.log('TextDecoder patch applied to worker file successfully!')
}
}