API / Native module access

Native module access

Call any loaded native module from the main process, and introspect what is loaded. This is the counterpart of the renderer's ow.invoke.

import { invokeNative, listNativeModules, nativeModuleInfo } from '@owear/core'

invokeNative

invokeNative<T = unknown>(module: string, method: string, ...args: unknown[]): Promise<T>

Equivalent to ow.invoke in the renderer, but with no window involved. This is the piece that lets you port Electron main-process APIs.

const text = await invokeNative<string>('fs', 'readText', '/etc/hostname')
const displays = await invokeNative('screen', 'getAllDisplays')

Builtins that operate on a window expect the window id as the first argument:

await invokeNative('ow-window', 'setTitle', win.id, 'Hello')

listNativeModules

listNativeModules(): Promise<NativeModuleInfo[]>

nativeModuleInfo

nativeModuleInfo(name: string): Promise<NativeModuleInfo>
// rejects if the module does not exist
interface NativeModuleInfo {
  name: string
  version: string
  origin: string        // path to the .owm, or "builtin:<name>"
  builtin: boolean
  functions: number     // count (compat)
  functionNames: string[]
}

Example

const mods = await listNativeModules()
for (const m of mods) {
  console.log(m.name, m.version, m.builtin ? '(builtin)' : m.origin, m.functionNames)
}

These map to the control-protocol commands module.invoke, module.list, and module.info (see the control protocol).

Edit this page on GitHub ↗