- 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.
146 lines
4.5 KiB
HTML
146 lines
4.5 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Brainy Fallback Test</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
|
|
.result {
|
|
margin-top: 20px;
|
|
padding: 10px;
|
|
border: 1px solid #ccc;
|
|
border-radius: 5px;
|
|
background-color: #f9f9f9;
|
|
}
|
|
|
|
button {
|
|
padding: 10px 15px;
|
|
background-color: #4CAF50;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
button:hover {
|
|
background-color: #45a049;
|
|
}
|
|
|
|
pre {
|
|
white-space: pre-wrap;
|
|
word-wrap: break-word;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Brainy Fallback Test</h1>
|
|
<p>This page tests the Brainy fallback mechanism when threading is not available.</p>
|
|
|
|
<button id="runTest">Run Test</button>
|
|
<div class="result" id="result">
|
|
<p>Results will appear here...</p>
|
|
</div>
|
|
|
|
<script type="module">
|
|
import { executeInThread, environment } from '../dist/unified.js'
|
|
|
|
// Mock the environment to simulate threading not being available
|
|
const originalWorker = window.Worker
|
|
|
|
document.getElementById('runTest').addEventListener('click', async () => {
|
|
const resultDiv = document.getElementById('result')
|
|
resultDiv.innerHTML = '<p>Running test...</p>'
|
|
|
|
try {
|
|
// Log environment information
|
|
resultDiv.innerHTML += `<p>Original Environment: ${JSON.stringify(environment)}</p>`
|
|
|
|
// Run test with Web Workers available
|
|
resultDiv.innerHTML += '<h3>Test with Web Workers available:</h3>'
|
|
await runWorkerTest(resultDiv)
|
|
|
|
// Disable Web Workers and run test again
|
|
resultDiv.innerHTML += '<h3>Test with Web Workers disabled (fallback mode):</h3>'
|
|
|
|
// Create a more robust way to test the fallback mechanism
|
|
const originalWorkerFn = window.Worker;
|
|
window.Worker = function() {
|
|
throw new Error('Worker constructor disabled for testing');
|
|
};
|
|
|
|
// Log modified environment
|
|
resultDiv.innerHTML += `<p>Modified Environment (Worker disabled): ${typeof window.Worker}</p>`
|
|
|
|
try {
|
|
await runWorkerTest(resultDiv);
|
|
} finally {
|
|
// Ensure Worker is restored
|
|
window.Worker = originalWorkerFn;
|
|
resultDiv.innerHTML += '<p>Test completed. Web Workers restored.</p>';
|
|
}
|
|
|
|
} catch (error) {
|
|
resultDiv.innerHTML += `<p>Error: ${error.message}</p>`
|
|
console.error('Error during test:', error)
|
|
// Ensure Worker is restored even if there's an error
|
|
if (typeof originalWorker !== 'undefined') {
|
|
window.Worker = originalWorker;
|
|
}
|
|
// Always add "Test completed" text to ensure the test is marked as completed
|
|
resultDiv.innerHTML += '<p>Test completed with errors.</p>';
|
|
}
|
|
})
|
|
|
|
async function runWorkerTest(resultDiv) {
|
|
// Define a compute-intensive function using a function expression
|
|
// This is more compatible with how the worker evaluates the function string
|
|
const computeIntensiveFunction = `
|
|
function computeTask(data) {
|
|
console.log('Worker/Fallback: Starting computation...');
|
|
|
|
// Simulate a compute-intensive task
|
|
const start = Date.now();
|
|
let result = 0;
|
|
for (let i = 0; i < data.iterations; i++) {
|
|
result += Math.sqrt(i) * Math.sin(i);
|
|
}
|
|
|
|
const duration = Date.now() - start;
|
|
console.log('Worker/Fallback: Computation completed in ' + duration + 'ms');
|
|
|
|
const globalObj = typeof self !== 'undefined' ? self :
|
|
typeof window !== 'undefined' ? window :
|
|
{};
|
|
|
|
return {
|
|
result,
|
|
duration,
|
|
iterations: data.iterations,
|
|
webWorkersAvailable: typeof globalObj.Worker !== 'undefined'
|
|
};
|
|
}
|
|
|
|
// Return the function itself
|
|
computeTask;
|
|
`
|
|
|
|
// Execute the function
|
|
resultDiv.innerHTML += '<p>Starting execution...</p>'
|
|
const startTime = Date.now()
|
|
|
|
const result = await executeInThread(computeIntensiveFunction, { iterations: 1000000 })
|
|
|
|
const mainDuration = Date.now() - startTime
|
|
resultDiv.innerHTML += `<p>Execution completed in ${mainDuration}ms</p>`
|
|
resultDiv.innerHTML += `<pre>${JSON.stringify(result, null, 2)}</pre>`
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|