Skip to content

Commit a81afa9

Browse files
committed
feat: move types to ensure consistent usage
1 parent 051b970 commit a81afa9

3 files changed

Lines changed: 134 additions & 98 deletions

File tree

lib/get-bindings.d.ts

Lines changed: 26 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
export declare interface HypervisorOptions {
2-
uri: string;
3-
}
1+
import type {
2+
HypervisorOptions,
3+
DomainInfo,
4+
NodeInfo,
5+
DomainBlockInfo,
6+
DomainInterfaceInfo,
7+
DomainInterfaceAddress,
8+
DomainState,
9+
DomainRebootFlags,
10+
DomainInterfaceAddressesSource,
11+
ConnectListAllDomainsFlags,
12+
DomainGetXMLDescFlags
13+
} from './types.js';
414

515
export declare class Hypervisor {
616
constructor(options: HypervisorOptions);
@@ -31,32 +41,6 @@ export declare class Hypervisor {
3141
nodeGetInfo(): Promise<NodeInfo>;
3242
}
3343

34-
export declare const enum ConnectListAllDomainsFlags {
35-
ACTIVE = 1,
36-
INACTIVE = 2,
37-
PERSISTENT = 4,
38-
TRANSIENT = 8,
39-
RUNNING = 16,
40-
PAUSED = 32,
41-
SHUTOFF = 64,
42-
OTHER = 128,
43-
MANAGEDSAVE = 256,
44-
NO_MANAGEDSAVE = 512,
45-
AUTOSTART = 1024,
46-
NO_AUTOSTART = 2048,
47-
HAS_SNAPSHOT = 4096,
48-
NO_SNAPSHOT = 8192,
49-
HAS_CHECKPOINT = 16384,
50-
NO_CHECKPOINT = 32768,
51-
}
52-
53-
export declare const enum DomainGetXMLDescFlags {
54-
SECURE = 1,
55-
INACTIVE = 2,
56-
UPDATE_CPU = 4,
57-
MIGRATABLE = 8,
58-
}
59-
6044
export declare class Domain {
6145
isActive(): Promise<boolean>;
6246
isPersistent(): Promise<boolean>;
@@ -89,65 +73,16 @@ export declare class Domain {
8973
getInterfaceAddresses(source?: DomainInterfaceAddressesSource): Promise<DomainInterfaceInfo[]>;
9074
}
9175

92-
export declare enum DomainRebootFlags {
93-
NONE = 0,
94-
ACPI = 1,
95-
GUEST_AGENT = 2,
96-
INIT = 4,
97-
SIGNAL = 8,
98-
}
99-
100-
export declare enum DomainInterfaceAddressesSource {
101-
LEASE = 0,
102-
AGENT = 1,
103-
ARP = 2,
104-
}
105-
106-
export declare interface DomainBlockInfo {
107-
capacity: number;
108-
allocation: number;
109-
physical: number;
110-
}
111-
112-
export declare interface DomainInterfaceInfo {
113-
name: string;
114-
hwaddr: string;
115-
addrs: DomainInterfaceAddress[];
116-
}
117-
118-
export declare interface DomainInterfaceAddress {
119-
type: number;
120-
addr: string;
121-
prefix: number;
122-
}
123-
124-
// https://libvirt.org/manpages/virsh.html#list
125-
export declare const enum DomainState {
126-
NOSTATE = 0,
127-
RUNNING = 1,
128-
BLOCKED = 2,
129-
PAUSED = 3,
130-
SHUTDOWN = 4,
131-
SHUTOFF = 5,
132-
CRASHED = 6,
133-
PMSUSPENDED = 7,
134-
}
135-
136-
export declare interface DomainInfo {
137-
state: DomainState;
138-
maxMem: number;
139-
memory: number;
140-
nrVirtCpu: number;
141-
cpuTime: number;
142-
}
143-
144-
export declare interface NodeInfo {
145-
model: string;
146-
memory: number;
147-
cpus: number;
148-
mhz: number;
149-
nodes: number;
150-
sockets: number;
151-
cores: number;
152-
threads: number;
153-
}
76+
export type {
77+
HypervisorOptions,
78+
DomainInfo,
79+
NodeInfo,
80+
DomainBlockInfo,
81+
DomainInterfaceInfo,
82+
DomainInterfaceAddress,
83+
DomainState,
84+
DomainRebootFlags,
85+
DomainInterfaceAddressesSource,
86+
ConnectListAllDomainsFlags,
87+
DomainGetXMLDescFlags
88+
};

lib/get-bindings.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,23 @@
77
* @brief Contains actual libvirt bindings and related declarations.
88
*/
99
import { createRequire } from "node:module";
10+
import type { HypervisorOptions } from './types.js';
11+
1012
const require = createRequire(import.meta.url);
1113

12-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-require-imports
13-
export const { Hypervisor } = require('bindings')('libvirt.node');
14-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-var-requires,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-require-imports
15-
export const { Domain } = require('bindings')('libvirt.node');
14+
// Define the native module interface
15+
interface NativeLibvirt {
16+
Hypervisor: new (options: HypervisorOptions) => any;
17+
Domain: new () => any;
18+
}
19+
20+
// Import the native bindings
21+
const bindings = require('bindings')('libvirt.node') as NativeLibvirt;
22+
23+
// Export the native classes directly
24+
export const { Hypervisor, Domain } = bindings;
1625

26+
// Export the flags as const objects for better type inference
1727
export const ConnectListAllDomainsFlags = {
1828
ACTIVE: 1,
1929
INACTIVE: 2,
@@ -31,14 +41,14 @@ export const ConnectListAllDomainsFlags = {
3141
NO_SNAPSHOT: 8192,
3242
HAS_CHECKPOINT: 16384,
3343
NO_CHECKPOINT: 32768
34-
};
44+
} as const;
3545

3646
export const DomainGetXMLDescFlags = {
3747
SECURE: 1,
3848
INACTIVE: 2,
3949
UPDATE_CPU: 4,
4050
MIGRATABLE: 8
41-
};
51+
} as const;
4252

4353
// https://libvirt.org/manpages/virsh.html#list
4454
export const DomainState = {
@@ -52,4 +62,4 @@ export const DomainState = {
5262
SHUTOFF: 5,
5363
CRASHED: 6,
5464
PMSUSPENDED: 7
55-
};
65+
} as const;

lib/types.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
export interface HypervisorOptions {
2+
uri: string;
3+
}
4+
5+
export interface DomainInfo {
6+
state: DomainState;
7+
maxMem: number;
8+
memory: number;
9+
nrVirtCpu: number;
10+
cpuTime: number;
11+
}
12+
13+
export interface NodeInfo {
14+
model: string;
15+
memory: number;
16+
cpus: number;
17+
mhz: number;
18+
nodes: number;
19+
sockets: number;
20+
cores: number;
21+
threads: number;
22+
}
23+
24+
export interface DomainBlockInfo {
25+
capacity: number;
26+
allocation: number;
27+
physical: number;
28+
}
29+
30+
export interface DomainInterfaceInfo {
31+
name: string;
32+
hwaddr: string;
33+
addrs: DomainInterfaceAddress[];
34+
}
35+
36+
export interface DomainInterfaceAddress {
37+
type: number;
38+
addr: string;
39+
prefix: number;
40+
}
41+
42+
export enum DomainState {
43+
NOSTATE = 0,
44+
RUNNING = 1,
45+
BLOCKED = 2,
46+
PAUSED = 3,
47+
SHUTDOWN = 4,
48+
SHUTOFF = 5,
49+
CRASHED = 6,
50+
PMSUSPENDED = 7
51+
}
52+
53+
export enum DomainRebootFlags {
54+
NONE = 0,
55+
ACPI = 1,
56+
GUEST_AGENT = 2,
57+
INIT = 4,
58+
SIGNAL = 8
59+
}
60+
61+
export enum DomainInterfaceAddressesSource {
62+
LEASE = 0,
63+
AGENT = 1,
64+
ARP = 2
65+
}
66+
67+
export enum ConnectListAllDomainsFlags {
68+
ACTIVE = 1,
69+
INACTIVE = 2,
70+
PERSISTENT = 4,
71+
TRANSIENT = 8,
72+
RUNNING = 16,
73+
PAUSED = 32,
74+
SHUTOFF = 64,
75+
OTHER = 128,
76+
MANAGEDSAVE = 256,
77+
NO_MANAGEDSAVE = 512,
78+
AUTOSTART = 1024,
79+
NO_AUTOSTART = 2048,
80+
HAS_SNAPSHOT = 4096,
81+
NO_SNAPSHOT = 8192,
82+
HAS_CHECKPOINT = 16384,
83+
NO_CHECKPOINT = 32768
84+
}
85+
86+
export enum DomainGetXMLDescFlags {
87+
SECURE = 1,
88+
INACTIVE = 2,
89+
UPDATE_CPU = 4,
90+
MIGRATABLE = 8
91+
}

0 commit comments

Comments
 (0)