stock-bot/apps/stock/engine/index.mjs

58 lines
1.7 KiB
JavaScript

// ESM wrapper for the native module
import { createRequire } from 'module';
import { createHash } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Loaded executable identity is captured before native loading, not inferred later from disk.
// Build artifacts must remain immutable while the process runs; updates require a restart.
const nativePath = join(__dirname, 'dist', 'engine.node');
const fileHash = path => createHash('sha256').update(readFileSync(path)).digest('hex');
export const loadedNativeIdentity = Object.freeze({
engineWrapperSha256: fileHash(__filename),
engineBinarySha256: fileHash(nativePath),
});
const nativeBinding = require(nativePath);
if (
loadedNativeIdentity.engineWrapperSha256 !== fileHash(__filename) ||
loadedNativeIdentity.engineBinarySha256 !== fileHash(nativePath)
) {
throw new Error('Native artifacts changed while loading; restart with immutable build artifacts');
}
export const {
TradingEngine,
BacktestEngine,
FinancialReplaySession,
stageFinancialTransaction,
TechnicalIndicators,
IncrementalSma,
IncrementalEma,
IncrementalRsi,
RiskAnalyzer,
OrderbookAnalyzer,
MarketData,
MarketUpdate,
Order,
Fill,
Position,
RiskLimits,
RiskMetrics,
ExecutionResult,
OrderBookLevel,
OrderBookSnapshot,
MarketMicrostructure,
PositionUpdate,
RiskCheckResult,
} = nativeBinding;
export const IncrementalSMA = IncrementalSma;
export const IncrementalEMA = IncrementalEma;
export const IncrementalRSI = IncrementalRsi;
export default nativeBinding;