dfhack-remote-node 2.0.0

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.
@@ -0,0 +1,228 @@
1
+ /** One RPC method definition. `plugin` is null for core (dfproto) methods. */
2
+ interface MethodDef {
3
+ plugin: string | null;
4
+ input: string;
5
+ output: string;
6
+ }
7
+ declare const METHODS: {
8
+ readonly BindMethod: {
9
+ readonly plugin: null;
10
+ readonly input: "dfproto.CoreBindRequest";
11
+ readonly output: "dfproto.CoreBindReply";
12
+ };
13
+ readonly RunCommand: {
14
+ readonly plugin: null;
15
+ readonly input: "dfproto.CoreRunCommandRequest";
16
+ readonly output: "dfproto.EmptyMessage";
17
+ };
18
+ readonly RunLua: {
19
+ readonly plugin: null;
20
+ readonly input: "dfproto.CoreRunLuaRequest";
21
+ readonly output: "dfproto.StringListMessage";
22
+ };
23
+ readonly GetVersion: {
24
+ readonly plugin: null;
25
+ readonly input: "dfproto.EmptyMessage";
26
+ readonly output: "dfproto.StringMessage";
27
+ };
28
+ readonly GetDFVersion: {
29
+ readonly plugin: null;
30
+ readonly input: "dfproto.EmptyMessage";
31
+ readonly output: "dfproto.StringMessage";
32
+ };
33
+ readonly GetWorldInfo: {
34
+ readonly plugin: null;
35
+ readonly input: "dfproto.EmptyMessage";
36
+ readonly output: "dfproto.GetWorldInfoOut";
37
+ };
38
+ readonly ListUnits: {
39
+ readonly plugin: null;
40
+ readonly input: "dfproto.ListUnitsIn";
41
+ readonly output: "dfproto.ListUnitsOut";
42
+ };
43
+ readonly ListSquads: {
44
+ readonly plugin: null;
45
+ readonly input: "dfproto.ListSquadsIn";
46
+ readonly output: "dfproto.ListSquadsOut";
47
+ };
48
+ readonly GetMapInfo: {
49
+ readonly plugin: "RemoteFortressReader";
50
+ readonly input: "dfproto.EmptyMessage";
51
+ readonly output: "RemoteFortressReader.MapInfo";
52
+ };
53
+ readonly GetUnitList: {
54
+ readonly plugin: "RemoteFortressReader";
55
+ readonly input: "dfproto.EmptyMessage";
56
+ readonly output: "RemoteFortressReader.UnitList";
57
+ };
58
+ readonly GetViewInfo: {
59
+ readonly plugin: "RemoteFortressReader";
60
+ readonly input: "dfproto.EmptyMessage";
61
+ readonly output: "RemoteFortressReader.ViewInfo";
62
+ };
63
+ readonly GetWorldMapCenter: {
64
+ readonly plugin: "RemoteFortressReader";
65
+ readonly input: "dfproto.EmptyMessage";
66
+ readonly output: "RemoteFortressReader.WorldMap";
67
+ };
68
+ };
69
+ /** Names of every method in {@link METHODS}. */
70
+ type MethodName = keyof typeof METHODS;
71
+
72
+ /** Any decoded reply carries the call's captured console output on `_text`. */
73
+ interface DecodedReply {
74
+ _text: string;
75
+ [key: string]: unknown;
76
+ }
77
+ /** A DFHack translated-name sub-message (as decoded by protobufjs). */
78
+ interface NameInfo {
79
+ englishName?: string;
80
+ [key: string]: unknown;
81
+ }
82
+ /** Decoded `dfproto.GetWorldInfoOut`. */
83
+ interface GetWorldInfoReply extends DecodedReply {
84
+ mode?: number;
85
+ worldName?: NameInfo;
86
+ saveDir?: string;
87
+ }
88
+ interface DwarfClientOptions {
89
+ host?: string;
90
+ port?: number;
91
+ }
92
+ declare class DwarfClient {
93
+ private readonly root;
94
+ private readonly conn;
95
+ private readonly textNotification;
96
+ private readonly boundIds;
97
+ constructor({ host, port }?: DwarfClientOptions);
98
+ private decodeText;
99
+ /** True once connected and past the handshake. */
100
+ get connected(): boolean;
101
+ connect(): Promise<this>;
102
+ close(): void;
103
+ /** Resolve (and cache) a method's runtime id via BindMethod. */
104
+ private bind;
105
+ /** Call a bound method by name; returns the decoded reply with `_text` attached. */
106
+ call(name: MethodName, input?: Record<string, unknown>): Promise<DecodedReply>;
107
+ getVersion(): Promise<string>;
108
+ getDFVersion(): Promise<string>;
109
+ getWorldInfo(): Promise<GetWorldInfoReply>;
110
+ /** Run a DFHack command; returns its console (TEXT) output as a string. */
111
+ runCommand(command: string, args?: string[]): Promise<string>;
112
+ /**
113
+ * Run an arbitrary Lua snippet and return whatever it prints.
114
+ * The DFHack `lua` command joins its arguments and runs them as a chunk (the
115
+ * console-only `-e` flag is NOT accepted over RPC — it gets parsed as code),
116
+ * so the snippet is passed as the argument directly. Console output is captured
117
+ * via TEXT frames. This is the workhorse for semantic tools: the snippet builds
118
+ * JSON and prints it.
119
+ */
120
+ runLuaSnippet(snippet: string): Promise<string>;
121
+ /**
122
+ * Invoke a named Lua function via the core RunLua RPC: `module.function(...args)`.
123
+ * Returns the function's string-list result. (Distinct from runLuaSnippet: this
124
+ * calls an existing function, not code.)
125
+ */
126
+ callLua(module: string, fn: string, args?: string[]): Promise<string[]>;
127
+ }
128
+
129
+ /** Turns a TEXT frame body into a string. */
130
+ type TextDecoder = (body: Buffer) => string;
131
+ interface DfConnectionOptions {
132
+ host?: string;
133
+ port?: number;
134
+ textDecoder?: TextDecoder;
135
+ }
136
+ /** Result of one RPC call: the RESULT body plus captured console output. */
137
+ interface CallResult {
138
+ result: Buffer;
139
+ text: string;
140
+ }
141
+ declare class DfConnection {
142
+ readonly host: string;
143
+ readonly port: number;
144
+ private readonly decodeText;
145
+ private sock;
146
+ private ready;
147
+ private buf;
148
+ private shookHands;
149
+ private queue;
150
+ private active;
151
+ private textFrames;
152
+ constructor({ host, port, textDecoder }?: DfConnectionOptions);
153
+ /** True once connected and past the handshake. */
154
+ get connected(): boolean;
155
+ /** Connect and complete the handshake. Resolves once ready for calls. */
156
+ connect(): Promise<void>;
157
+ private onData;
158
+ private handleFrame;
159
+ /** Send the next queued call if the socket is idle. */
160
+ private pump;
161
+ /**
162
+ * Send one RPC call. `id` is the bound method id (0 for BindMethod).
163
+ * Returns { result: Buffer, text: string }.
164
+ */
165
+ call(id: number, body: Uint8Array): Promise<CallResult>;
166
+ private fail;
167
+ close(): void;
168
+ }
169
+
170
+ /** Thrown when the peer violates the wire protocol (bad magic, stray frame, …). */
171
+ declare class ProtocolError extends Error {
172
+ constructor(message: string);
173
+ }
174
+ /**
175
+ * Thrown when an RPC call returns a FAIL frame.
176
+ * `code` is DFHack's command_result (see {@link CR}); `text` is any console
177
+ * (TEXT) output the call produced before failing.
178
+ */
179
+ declare class RpcError extends Error {
180
+ readonly code: number;
181
+ readonly text: string;
182
+ constructor(message: string, code: number, text?: string);
183
+ }
184
+
185
+ /** Reserved reply ids (RPCMessageHeader.id) — real methods use non-negative ids. */
186
+ declare const RPC_REPLY: {
187
+ readonly RESULT: -1;
188
+ readonly FAIL: -2;
189
+ readonly TEXT: -3;
190
+ };
191
+ /** Error codes carried in a FAIL frame's size field (DFHack command_result). */
192
+ declare const CR: {
193
+ readonly LINK_FAILURE: -3;
194
+ readonly NEEDS_CONSOLE: -2;
195
+ readonly NOT_IMPLEMENTED: -1;
196
+ readonly OK: 0;
197
+ readonly FAILURE: 1;
198
+ readonly WRONG_USAGE: 2;
199
+ readonly NOT_FOUND: 3;
200
+ };
201
+ /** One parsed reply frame. `body` is null for FAIL frames (size holds the errno). */
202
+ interface Frame {
203
+ id: number;
204
+ size: number;
205
+ body: Buffer | null;
206
+ }
207
+ /** A frame plus how many bytes it consumed from the front of the buffer. */
208
+ interface ParsedFrame {
209
+ frame: Frame;
210
+ consumed: number;
211
+ }
212
+ /** Encode one request message: 8-byte header (id, pad, size) + body. */
213
+ declare function encodeMessage(id: number, body: Uint8Array): Buffer;
214
+ /**
215
+ * Try to read the handshake reply from the front of `buf`.
216
+ * Returns the number of bytes consumed (HANDSHAKE_LEN) once available, or 0 if
217
+ * more data is needed. Throws ProtocolError if the magic doesn't match.
218
+ */
219
+ declare function readHandshake(buf: Buffer): number;
220
+ /**
221
+ * Try to parse one frame off the front of `buf`.
222
+ * Returns { frame, consumed }, or null if a complete frame isn't available yet.
223
+ * For FAIL frames `size` is the errno and `body` is null; for RESULT/TEXT `body`
224
+ * is a Buffer of length `size`.
225
+ */
226
+ declare function readFrame(buf: Buffer): ParsedFrame | null;
227
+
228
+ export { CR, type CallResult, type DecodedReply, DfConnection, type DfConnectionOptions, DwarfClient, type DwarfClientOptions, type Frame, type GetWorldInfoReply, METHODS, type MethodDef, type MethodName, type NameInfo, type ParsedFrame, ProtocolError, RPC_REPLY, RpcError, type TextDecoder, encodeMessage, readFrame, readHandshake };