79 lines
3 KiB
TypeScript
79 lines
3 KiB
TypeScript
/** Read-only build identity. This module never imports the native addon or strategy code. */
|
|
import { createHash } from 'node:crypto';
|
|
import { realpathSync } from 'node:fs';
|
|
import { readFile } from 'node:fs/promises';
|
|
import { dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
import {
|
|
canonicalBuildIdentitySchema,
|
|
type CanonicalBuildIdentity,
|
|
} from '@stock-bot/stock-config/canonical-research';
|
|
import { canonicalStrategyJson } from '@stock-bot/stock-config/strategy';
|
|
|
|
const engineWrapper = realpathSync(fileURLToPath(import.meta.resolve('@stock-bot/engine')));
|
|
const repositoryRoot = resolve(dirname(engineWrapper), '../../..');
|
|
const ownPath = relative(repositoryRoot, realpathSync(fileURLToPath(import.meta.url)));
|
|
if (isAbsolute(ownPath) || ownPath === '..' || ownPath.startsWith('../')) {
|
|
throw new Error('Canonical engine resolves outside this checkout');
|
|
}
|
|
if (engineWrapper !== resolve(repositoryRoot, 'apps/stock/engine/index.mjs')) {
|
|
throw new Error('Unsupported canonical engine wrapper location');
|
|
}
|
|
|
|
export const canonicalWorkerUrl = pathToFileURL(
|
|
resolve(repositoryRoot, 'apps/stock/orchestrator/dist/native-worker.js')
|
|
);
|
|
|
|
async function hashLocalFile(path: string): Promise<string> {
|
|
if (realpathSync(path) !== path) {
|
|
throw new Error('Canonical build artifacts must belong to this checkout');
|
|
}
|
|
return createHash('sha256')
|
|
.update(await readFile(path))
|
|
.digest('hex');
|
|
}
|
|
|
|
/** Capture all executable worker/native bytes and the runtime that interprets them. */
|
|
export async function readCanonicalBuildIdentity(): Promise<CanonicalBuildIdentity> {
|
|
if (typeof Bun === 'undefined') {
|
|
throw new Error('Canonical research requires the Bun runtime');
|
|
}
|
|
const [workerSha256, engineWrapperSha256, engineBinarySha256, dependencyLockSha256] =
|
|
await Promise.all([
|
|
hashLocalFile(fileURLToPath(canonicalWorkerUrl)),
|
|
hashLocalFile(engineWrapper),
|
|
hashLocalFile(resolve(repositoryRoot, 'apps/stock/engine/dist/engine.node')),
|
|
hashLocalFile(resolve(repositoryRoot, 'bun.lock')),
|
|
]);
|
|
return canonicalBuildIdentitySchema.parse({
|
|
version: 1,
|
|
workerSha256,
|
|
engineWrapperSha256,
|
|
engineBinarySha256,
|
|
dependencyLockSha256,
|
|
runtime: 'bun',
|
|
runtimeVersion: Bun.version,
|
|
platform: process.platform,
|
|
arch: process.arch,
|
|
});
|
|
}
|
|
|
|
/** The running child supplies its own module URL, never a URL from the request. */
|
|
export async function verifyCanonicalWorkerBuild(
|
|
expected: CanonicalBuildIdentity,
|
|
actualWorkerUrl: string
|
|
): Promise<CanonicalBuildIdentity> {
|
|
if (
|
|
realpathSync(fileURLToPath(actualWorkerUrl)) !== realpathSync(fileURLToPath(canonicalWorkerUrl))
|
|
) {
|
|
throw new Error('Canonical research requires the bundled worker from this checkout');
|
|
}
|
|
const actual = await readCanonicalBuildIdentity();
|
|
if (
|
|
canonicalStrategyJson(actual) !==
|
|
canonicalStrategyJson(canonicalBuildIdentitySchema.parse(expected))
|
|
) {
|
|
throw new Error('Canonical worker/native/runtime build identity mismatch');
|
|
}
|
|
return actual;
|
|
}
|