Open History to see the last commit
1import type { ComponentType } from "react";
2import type { NativeSyntheticEvent, ViewProps } from "react-native";
3import { requireNativeView, requireOptionalNativeModule } from "expo";
4
5import { NativeViewResolutionError } from "../../native/nativeViewResolutionError";
6
7const NATIVE_TERMINAL_MODULE_NAME = "V12TerminalSurface";
8
9interface ExpoGlobalWithViewConfig {
10 readonly expo?: {
11 getViewConfig?: (moduleName: string, viewName?: string) => unknown;
12 };
13}
14
15interface TerminalInputEvent {
16 readonly data: string;
17}
18
19interface TerminalResizeEvent {
20 readonly cols: number;
21 readonly rows: number;
22}
23
24export interface NativeTerminalSurfaceProps extends ViewProps {
25 readonly appearanceScheme?: "light" | "dark";
26 readonly focusRequest?: number;
27 readonly themeConfig?: string;
28 readonly backgroundColor?: string;
29 readonly foregroundColor?: string;
30 readonly mutedForegroundColor?: string;
31 readonly terminalKey: string;
32 readonly initialBuffer: string;
33 readonly fontSize: number;
34 readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
35 readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
36}
37
38let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
39let nativeTerminalSurfaceViewResolutionFailed = false;
40
41function getExpoViewConfig(moduleName: string) {
42 return (globalThis as typeof globalThis & ExpoGlobalWithViewConfig).expo?.getViewConfig?.(
43 moduleName,
44 );
45}
46
47export function resolveNativeTerminalSurfaceView(): ComponentType<NativeTerminalSurfaceProps> | null {
48 if (cachedNativeTerminalSurfaceView) {
49 return cachedNativeTerminalSurfaceView;
50 }
51
52 if (nativeTerminalSurfaceViewResolutionFailed) {
53 return null;
54 }
55
56 if (getExpoViewConfig(NATIVE_TERMINAL_MODULE_NAME) == null) {
57 return null;
58 }
59
60 try {
61 cachedNativeTerminalSurfaceView = requireNativeView<NativeTerminalSurfaceProps>(
62 NATIVE_TERMINAL_MODULE_NAME,
63 );
64 } catch (cause) {
65 nativeTerminalSurfaceViewResolutionFailed = true;
66 console.error(
67 new NativeViewResolutionError({
68 nativeModuleName: NATIVE_TERMINAL_MODULE_NAME,
69 cause,
70 }),
71 );
72 return null;
73 }
74
75 return cachedNativeTerminalSurfaceView ?? null;
76}
77
78/**
79 * Revision of the native hardware-keyboard handling compiled into the installed binary,
80 * or `null` when the binary predates the revision constant (or the module is missing).
81 * Used in terminal debug logs to detect stale native builds.
82 */
83export function getNativeTerminalHardwareKeyRevision(): number | null {
84 try {
85 if (typeof requireOptionalNativeModule !== "function") {
86 return null;
87 }
88 const module = requireOptionalNativeModule<{ readonly hardwareKeyRevision?: number }>(
89 NATIVE_TERMINAL_MODULE_NAME,
90 );
91 return module?.hardwareKeyRevision ?? null;
92 } catch {
93 return null;
94 }
95}
96
97export function hasNativeTerminalSurface() {
98 return resolveNativeTerminalSurfaceView() !== null;
99}