@bun-win32/psapi 2.0.1 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -2
- package/index.ts +0 -2
- package/package.json +8 -5
- package/structs/Psapi.ts +6 -65
- package/types/Psapi.ts +1 -12
- package/runtime/extensions.ts +0 -210
package/README.md
CHANGED
|
@@ -4,7 +4,9 @@ Zero-dependency, zero-overhead Win32 PSAPI bindings for [Bun](https://bun.sh) on
|
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
`@bun-win32/psapi` exposes the `psapi.dll` exports using Bun's FFI. It provides a single class, `Psapi`, which lazily binds native symbols on first use. You can optionally preload a subset or all symbols up-front via `Preload()`.
|
|
7
|
+
`@bun-win32/psapi` exposes the `psapi.dll` exports using [Bun](https://bun.sh)'s FFI. It provides a single class, `Psapi`, which lazily binds native symbols on first use. You can optionally preload a subset or all symbols up-front via `Preload()`.
|
|
8
|
+
|
|
9
|
+
The bindings are strongly typed for a smooth DX in TypeScript.
|
|
8
10
|
|
|
9
11
|
## Features
|
|
10
12
|
|
|
@@ -46,7 +48,7 @@ console.log('Processes: %d', count);
|
|
|
46
48
|
|
|
47
49
|
## Examples
|
|
48
50
|
|
|
49
|
-
Run the included
|
|
51
|
+
Run the included examples:
|
|
50
52
|
|
|
51
53
|
```sh
|
|
52
54
|
bun run example # Process enumeration + performance info
|
package/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "Stev Peifer <stev@bell.net>",
|
|
3
3
|
"bugs": {
|
|
4
|
-
"url": "https://github.com/bun-win32/
|
|
4
|
+
"url": "https://github.com/ObscuritySRL/bun-win32/issues"
|
|
5
|
+
},
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@bun-win32/core": "workspace:*"
|
|
5
8
|
},
|
|
6
9
|
"description": "Zero-dependency, zero-overhead Win32 PSAPI bindings for Bun (FFI) on Windows.",
|
|
7
10
|
"devDependencies": {
|
|
@@ -17,13 +20,14 @@
|
|
|
17
20
|
"typescript": "^5"
|
|
18
21
|
},
|
|
19
22
|
"private": false,
|
|
20
|
-
"homepage": "https://github.com/bun-win32
|
|
23
|
+
"homepage": "https://github.com/ObscuritySRL/bun-win32#readme",
|
|
21
24
|
"repository": {
|
|
22
25
|
"type": "git",
|
|
23
|
-
"url": "git://github.com/bun-win32
|
|
26
|
+
"url": "git://github.com/ObscuritySRL/bun-win32.git",
|
|
27
|
+
"directory": "packages/psapi"
|
|
24
28
|
},
|
|
25
29
|
"type": "module",
|
|
26
|
-
"version": "2.0.
|
|
30
|
+
"version": "2.0.3",
|
|
27
31
|
"main": "./index.ts",
|
|
28
32
|
"keywords": [
|
|
29
33
|
"bun",
|
|
@@ -37,7 +41,6 @@
|
|
|
37
41
|
],
|
|
38
42
|
"files": [
|
|
39
43
|
"index.ts",
|
|
40
|
-
"runtime/*.ts",
|
|
41
44
|
"structs/*.ts",
|
|
42
45
|
"types/*.ts",
|
|
43
46
|
"README.md"
|
package/structs/Psapi.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type FFIFunction, FFIType
|
|
1
|
+
import { type FFIFunction, FFIType } from 'bun:ffi';
|
|
2
|
+
import { Win32 } from '@bun-win32/core';
|
|
2
3
|
|
|
3
4
|
import type {
|
|
4
5
|
BOOL,
|
|
@@ -43,71 +44,11 @@ import type {
|
|
|
43
44
|
* Psapi.Preload(['EnumProcesses', 'GetModuleBaseNameW']);
|
|
44
45
|
* ```
|
|
45
46
|
*/
|
|
46
|
-
class Psapi {
|
|
47
|
-
|
|
48
|
-
* Lazily binds a single `psapi.dll` export and memoizes it on the class.
|
|
49
|
-
*
|
|
50
|
-
* If the symbol has already been bound (property is non-configurable), this is a no-op.
|
|
51
|
-
* Subsequent calls go directly through the memoized native function.
|
|
52
|
-
*
|
|
53
|
-
* @param method Exact export name from `Symbols`.
|
|
54
|
-
* @returns The bound native function, typed to the corresponding static method.
|
|
55
|
-
* @example
|
|
56
|
-
* ```ts
|
|
57
|
-
* // Internal usage: public wrappers call Load on first invocation
|
|
58
|
-
* // return Psapi.Load('EnumProcesses')();
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
private static Load<T extends keyof typeof Psapi.Symbols>(method: T): (typeof Psapi)[T] {
|
|
62
|
-
const skip = Object.getOwnPropertyDescriptor(Psapi, method)?.configurable === false;
|
|
47
|
+
class Psapi extends Win32 {
|
|
48
|
+
protected static override name = 'psapi.dll';
|
|
63
49
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const library = dlopen('psapi.dll', { [method]: Psapi.Symbols[method] });
|
|
69
|
-
|
|
70
|
-
const propertyDescriptor = { configurable: false, value: library.symbols[method] };
|
|
71
|
-
Object.defineProperty(Psapi, method, propertyDescriptor);
|
|
72
|
-
|
|
73
|
-
return Psapi[method];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Eagerly binds one or more `psapi.dll` exports in a single `dlopen` call.
|
|
78
|
-
*
|
|
79
|
-
* When called with no arguments, every symbol in `Symbols` is bound.
|
|
80
|
-
* Already-bound symbols are skipped.
|
|
81
|
-
*
|
|
82
|
-
* @param methods Optional list of export names to preload. Defaults to all symbols.
|
|
83
|
-
* @example
|
|
84
|
-
* ```ts
|
|
85
|
-
* // Preload specific hot-path symbols
|
|
86
|
-
* Psapi.Preload(['EnumProcessModules', 'GetModuleBaseNameW', 'GetModuleFileNameExW']);
|
|
87
|
-
*
|
|
88
|
-
* // Or preload everything
|
|
89
|
-
* Psapi.Preload();
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
public static Preload(methods?: (keyof typeof Psapi.Symbols)[]): void {
|
|
93
|
-
methods ??= Object.keys(Psapi.Symbols) as (keyof typeof Psapi.Symbols)[];
|
|
94
|
-
|
|
95
|
-
const symbols = Object.fromEntries(
|
|
96
|
-
methods
|
|
97
|
-
.filter((method) => Object.getOwnPropertyDescriptor(Psapi, method)?.configurable !== false)
|
|
98
|
-
.map((method) => [method, Psapi.Symbols[method]])
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
const library = dlopen('psapi.dll', symbols);
|
|
102
|
-
|
|
103
|
-
const propertyDescriptorMap = Object.fromEntries(
|
|
104
|
-
Object.entries(library.symbols).map(([key, value]) => [key, { configurable: false, value }])
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
Object.defineProperties(Psapi, propertyDescriptorMap);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
private static readonly Symbols = {
|
|
50
|
+
/** @inheritdoc */
|
|
51
|
+
protected static override readonly Symbols = {
|
|
111
52
|
EmptyWorkingSet: { args: [FFIType.u64], returns: FFIType.i32 },
|
|
112
53
|
EnumDeviceDrivers: { args: [FFIType.ptr, FFIType.u32, FFIType.ptr], returns: FFIType.i32 },
|
|
113
54
|
EnumPageFilesA: { args: [FFIType.ptr, FFIType.ptr], returns: FFIType.i32 },
|
package/types/Psapi.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Pointer } from 'bun:ffi';
|
|
2
|
+
export type { BOOL, DWORD, HANDLE, HMODULE, LPCSTR, LPCWSTR, LPDWORD, LPSTR, LPVOID, LPWSTR, PDWORD, PVOID } from '@bun-win32/core';
|
|
2
3
|
|
|
3
4
|
export enum ListModulesFilterFlag {
|
|
4
5
|
LIST_MODULES_32BIT = 0x01,
|
|
@@ -7,23 +8,11 @@ export enum ListModulesFilterFlag {
|
|
|
7
8
|
LIST_MODULES_DEFAULT = 0x00,
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
export type BOOL = number;
|
|
11
|
-
export type DWORD = number;
|
|
12
|
-
export type HANDLE = bigint;
|
|
13
|
-
export type HMODULE = bigint;
|
|
14
|
-
export type LPCSTR = Pointer;
|
|
15
|
-
export type LPCWSTR = Pointer;
|
|
16
|
-
export type LPDWORD = Pointer;
|
|
17
11
|
export type LPHMODULE = Pointer;
|
|
18
12
|
export type LPMODULEINFO = Pointer;
|
|
19
|
-
export type LPSTR = Pointer;
|
|
20
|
-
export type LPVOID = Pointer;
|
|
21
|
-
export type LPWSTR = Pointer;
|
|
22
|
-
export type PDWORD = Pointer;
|
|
23
13
|
export type PENUM_PAGE_FILE_CALLBACKA = Pointer;
|
|
24
14
|
export type PENUM_PAGE_FILE_CALLBACKW = Pointer;
|
|
25
15
|
export type PPERFORMANCE_INFORMATION = Pointer;
|
|
26
16
|
export type PPROCESS_MEMORY_COUNTERS = Pointer;
|
|
27
17
|
export type PPSAPI_WS_WATCH_INFORMATION = Pointer;
|
|
28
18
|
export type PPSAPI_WS_WATCH_INFORMATION_EX = Pointer;
|
|
29
|
-
export type PVOID = Pointer;
|
package/runtime/extensions.ts
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { type Pointer, ptr } from 'bun:ffi';
|
|
2
|
-
|
|
3
|
-
declare global {
|
|
4
|
-
/**
|
|
5
|
-
* Adds a native pointer property to all ArrayBuffer, Buffer, DataView, and TypedArray types.
|
|
6
|
-
*
|
|
7
|
-
* The `ptr` property returns a native pointer usable with Bun FFI.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* const arr = new Uint8Array([1, 2, 3]);
|
|
12
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
13
|
-
* ```
|
|
14
|
-
*/
|
|
15
|
-
interface ArrayBuffer {
|
|
16
|
-
/**
|
|
17
|
-
* Native pointer to ArrayBuffer memory for Bun FFI.
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* const buf = new ArrayBuffer(8);
|
|
21
|
-
* nativeFunction(buf.ptr, buf.byteLength);
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
readonly ptr: Pointer;
|
|
25
|
-
}
|
|
26
|
-
interface BigInt64Array {
|
|
27
|
-
/**
|
|
28
|
-
* Native pointer to BigInt64Array memory for Bun FFI.
|
|
29
|
-
* @example
|
|
30
|
-
* ```ts
|
|
31
|
-
* const arr = new BigInt64Array([1n, 2n]);
|
|
32
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
readonly ptr: Pointer;
|
|
36
|
-
}
|
|
37
|
-
interface BigUint64Array {
|
|
38
|
-
/**
|
|
39
|
-
* Native pointer to BigUint64Array memory for Bun FFI.
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* const arr = new BigUint64Array([1n, 2n]);
|
|
43
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
readonly ptr: Pointer;
|
|
47
|
-
}
|
|
48
|
-
interface Buffer {
|
|
49
|
-
/**
|
|
50
|
-
* Native pointer to Buffer memory for Bun FFI.
|
|
51
|
-
* @example
|
|
52
|
-
* ```ts
|
|
53
|
-
* const buf = Buffer.from([1, 2, 3]);
|
|
54
|
-
* nativeFunction(buf.ptr, buf.length);
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
readonly ptr: Pointer;
|
|
58
|
-
}
|
|
59
|
-
interface DataView {
|
|
60
|
-
/**
|
|
61
|
-
* Native pointer to DataView memory for Bun FFI.
|
|
62
|
-
* @example
|
|
63
|
-
* ```ts
|
|
64
|
-
* const view = new DataView(new ArrayBuffer(4));
|
|
65
|
-
* nativeFunction(view.ptr, view.byteLength);
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
readonly ptr: Pointer;
|
|
69
|
-
}
|
|
70
|
-
interface Float32Array {
|
|
71
|
-
/**
|
|
72
|
-
* Native pointer to Float32Array memory for Bun FFI.
|
|
73
|
-
* @example
|
|
74
|
-
* ```ts
|
|
75
|
-
* const arr = new Float32Array([1, 2, 3]);
|
|
76
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
77
|
-
* ```
|
|
78
|
-
*/
|
|
79
|
-
readonly ptr: Pointer;
|
|
80
|
-
}
|
|
81
|
-
interface Float64Array {
|
|
82
|
-
/**
|
|
83
|
-
* Native pointer to Float64Array memory for Bun FFI.
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* const arr = new Float64Array([1, 2, 3]);
|
|
87
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
readonly ptr: Pointer;
|
|
91
|
-
}
|
|
92
|
-
interface Int16Array {
|
|
93
|
-
/**
|
|
94
|
-
* Native pointer to Int16Array memory for Bun FFI.
|
|
95
|
-
* @example
|
|
96
|
-
* ```ts
|
|
97
|
-
* const arr = new Int16Array([1, 2, 3]);
|
|
98
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
99
|
-
* ```
|
|
100
|
-
*/
|
|
101
|
-
readonly ptr: Pointer;
|
|
102
|
-
}
|
|
103
|
-
interface Int32Array {
|
|
104
|
-
/**
|
|
105
|
-
* Native pointer to Int32Array memory for Bun FFI.
|
|
106
|
-
* @example
|
|
107
|
-
* ```ts
|
|
108
|
-
* const arr = new Int32Array([1, 2, 3]);
|
|
109
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
readonly ptr: Pointer;
|
|
113
|
-
}
|
|
114
|
-
interface Int8Array {
|
|
115
|
-
/**
|
|
116
|
-
* Native pointer to Int8Array memory for Bun FFI.
|
|
117
|
-
* @example
|
|
118
|
-
* ```ts
|
|
119
|
-
* const arr = new Int8Array([1, 2, 3]);
|
|
120
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
121
|
-
* ```
|
|
122
|
-
*/
|
|
123
|
-
readonly ptr: Pointer;
|
|
124
|
-
}
|
|
125
|
-
interface SharedArrayBuffer {
|
|
126
|
-
/**
|
|
127
|
-
* Native pointer to SharedArrayBuffer memory for Bun FFI.
|
|
128
|
-
* @example
|
|
129
|
-
* ```ts
|
|
130
|
-
* const buf = new SharedArrayBuffer(8);
|
|
131
|
-
* nativeFunction(buf.ptr, buf.byteLength);
|
|
132
|
-
* ```
|
|
133
|
-
*/
|
|
134
|
-
readonly ptr: Pointer;
|
|
135
|
-
}
|
|
136
|
-
interface Uint16Array {
|
|
137
|
-
/**
|
|
138
|
-
* Native pointer to Uint16Array memory for Bun FFI.
|
|
139
|
-
* @example
|
|
140
|
-
* ```ts
|
|
141
|
-
* const arr = new Uint16Array([1, 2, 3]);
|
|
142
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
143
|
-
* ```
|
|
144
|
-
*/
|
|
145
|
-
readonly ptr: Pointer;
|
|
146
|
-
}
|
|
147
|
-
interface Uint32Array {
|
|
148
|
-
/**
|
|
149
|
-
* Native pointer to Uint32Array memory for Bun FFI.
|
|
150
|
-
* @example
|
|
151
|
-
* ```ts
|
|
152
|
-
* const arr = new Uint32Array([1, 2, 3]);
|
|
153
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
154
|
-
* ```
|
|
155
|
-
*/
|
|
156
|
-
readonly ptr: Pointer;
|
|
157
|
-
}
|
|
158
|
-
interface Uint8Array {
|
|
159
|
-
/**
|
|
160
|
-
* Native pointer to Uint8Array memory for Bun FFI.
|
|
161
|
-
* @example
|
|
162
|
-
* ```ts
|
|
163
|
-
* const arr = new Uint8Array([1, 2, 3]);
|
|
164
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
165
|
-
* ```
|
|
166
|
-
*/
|
|
167
|
-
readonly ptr: Pointer;
|
|
168
|
-
}
|
|
169
|
-
interface Uint8ClampedArray {
|
|
170
|
-
/**
|
|
171
|
-
* Native pointer to Uint8ClampedArray memory for Bun FFI.
|
|
172
|
-
* @example
|
|
173
|
-
* ```ts
|
|
174
|
-
* const arr = new Uint8ClampedArray([1, 2, 3]);
|
|
175
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
176
|
-
* ```
|
|
177
|
-
*/
|
|
178
|
-
readonly ptr: Pointer;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Installs the `ptr` property on all supported binary view prototypes.
|
|
184
|
-
*
|
|
185
|
-
* The property is non-enumerable and non-configurable. The getter calls `ptr(this)`.
|
|
186
|
-
*/
|
|
187
|
-
const constructors = [ArrayBuffer, BigInt64Array, BigUint64Array, Buffer, DataView, Float32Array, Float64Array, Int16Array, Int32Array, Int8Array, SharedArrayBuffer, Uint16Array, Uint32Array, Uint8Array, Uint8ClampedArray] as const;
|
|
188
|
-
|
|
189
|
-
constructors.forEach(
|
|
190
|
-
({ prototype }) =>
|
|
191
|
-
!Object.getOwnPropertyDescriptor(prototype, 'ptr') &&
|
|
192
|
-
Object.defineProperty(prototype, 'ptr', {
|
|
193
|
-
configurable: false,
|
|
194
|
-
enumerable: false,
|
|
195
|
-
/**
|
|
196
|
-
* Returns a native pointer to the underlying memory.
|
|
197
|
-
* @returns Native pointer for Bun FFI.
|
|
198
|
-
* @example
|
|
199
|
-
* ```ts
|
|
200
|
-
* const arr = new Uint8Array([1, 2, 3]);
|
|
201
|
-
* nativeFunction(arr.ptr, arr.length);
|
|
202
|
-
* ```
|
|
203
|
-
*/
|
|
204
|
-
get(this): Pointer {
|
|
205
|
-
return ptr(this);
|
|
206
|
-
},
|
|
207
|
-
})
|
|
208
|
-
);
|
|
209
|
-
|
|
210
|
-
export {};
|