// ESM native loader for the usecomputer Zig addon using createRequire. import os from 'node:os' import { createRequire } from 'node:module' import type { MouseButton, NativeCommandResult, NativeDataResult, Point, Region, } from './types.js' type NativeScreenshotOutput = { path: string desktopIndex: number captureX: number captureY: number captureWidth: number captureHeight: number imageWidth: number imageHeight: number } const require = createRequire(import.meta.url) export interface NativeModule { screenshot(input: { path: string | null display: number | null window: number | null region: Region | null annotate: boolean | null }): NativeDataResult click(input: { point: Point; button: MouseButton | null; count: number | null; modifiers?: string[] | null }): NativeCommandResult typeText(input: { text: string; delayMs: number | null }): NativeCommandResult press(input: { key: string; count: number | null; delayMs: number | null }): NativeCommandResult scroll(input: { direction: string; amount: number; at: Point | null }): NativeCommandResult drag(input: { from: Point; to: Point; cp: Point | null; durationMs: number | null; button: MouseButton | null }): NativeCommandResult hover(input: Point): NativeCommandResult mouseMove(input: Point): NativeCommandResult mouseDown(input: { button: MouseButton | null }): NativeCommandResult mouseUp(input: { button: MouseButton | null }): NativeCommandResult mousePosition(): NativeDataResult displayList(): NativeDataResult windowList(): NativeDataResult } function loadCandidate(path: string): NativeModule | null { try { return require(path) as NativeModule } catch { return null } } function loadNativeModule(): NativeModule | null { const dev = loadCandidate('../zig-out/lib/usecomputer.node') if (dev) { return dev } const platform = os.platform() const arch = os.arch() const target = `${platform}-${arch}` const packaged = loadCandidate(`../dist/${target}/usecomputer.node`) if (packaged) { return packaged } return null } export const native = loadNativeModule()