@vestra-player/sdk 1.6.20 → 1.6.22
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/CHANGELOG.md +19 -0
- package/dist/commands.d.ts +1 -1
- package/dist/commands.js +8 -8
- package/dist/docs-tree.json.gz +0 -0
- package/dist/i18n-tools.js +7 -2
- package/dist/plugin/testing/commands.js +3 -3
- package/dist/plugin/types/base.d.ts +1097 -0
- package/dist/plugin/types/base.js +1 -0
- package/dist/plugin/types/commands.d.ts +1189 -0
- package/dist/plugin/types/commands.js +1 -0
- package/dist/plugin/types/packages.d.ts +661 -0
- package/dist/plugin/types/packages.js +1 -0
- package/dist/plugin/types/playback.d.ts +1654 -0
- package/dist/plugin/types/playback.js +1 -0
- package/dist/plugin/types/processing.d.ts +974 -0
- package/dist/plugin/types/processing.js +1 -0
- package/dist/plugin/types/services.d.ts +1132 -0
- package/dist/plugin/types/services.js +1 -0
- package/dist/plugin/types/snapshots.d.ts +491 -0
- package/dist/plugin/types/snapshots.js +1 -0
- package/dist/plugin/types/sources.d.ts +302 -0
- package/dist/plugin/types/sources.js +1 -0
- package/dist/plugin/types/subtitles.d.ts +327 -0
- package/dist/plugin/types/subtitles.js +1 -0
- package/dist/plugin/types/ui-content.d.ts +692 -0
- package/dist/plugin/types/ui-content.js +1 -0
- package/dist/plugin/types/ui-layout.d.ts +1079 -0
- package/dist/plugin/types/ui-layout.js +1 -0
- package/dist/plugin/types/ui-locale.d.ts +490 -0
- package/dist/plugin/types/ui-locale.js +1 -0
- package/dist/plugin/types/ui-shell.d.ts +1072 -0
- package/dist/plugin/types/ui-shell.js +1 -0
- package/dist/plugin/types.d.ts +18 -11072
- package/llms.txt +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,1132 @@
|
|
|
1
|
+
import type { Plugin, SystemInfo } from "./base.ts";
|
|
2
|
+
import type { RenderOptions } from "./processing.ts";
|
|
3
|
+
import type { Disposable } from "./base.ts";
|
|
4
|
+
/**
|
|
5
|
+
* The system clipboard (`clipboard`). Unavailable without a user interface.
|
|
6
|
+
*
|
|
7
|
+
* Platforms: macOS writes text as UTF-8 and images as their original
|
|
8
|
+
* PNG/JPEG bytes; Windows writes Unicode text and images as a bitmap plus
|
|
9
|
+
* PNG. Other systems: `unsupported`. Failures: `invalid`, `missing`,
|
|
10
|
+
* `unavailable`, `failed` (the system refused).
|
|
11
|
+
*
|
|
12
|
+
* @example Copy the current file and position
|
|
13
|
+
* The clipboard holds "path @ seconds" for pasting into a note. The
|
|
14
|
+
* player writes the system clipboard; it rejects with `unsupported` on
|
|
15
|
+
* systems other than macOS and Windows and `failed` when the system
|
|
16
|
+
* refuses, which the plugin reports.
|
|
17
|
+
* ```ts
|
|
18
|
+
* const { source, position } = plugin.player.state;
|
|
19
|
+
* try {
|
|
20
|
+
* await plugin.clipboard.writeText(`${source} @ ${position}`); // data, not shown
|
|
21
|
+
* } catch (error) {
|
|
22
|
+
* void plugin.osd.show("Could not copy: {}", [(error as PluginError).code]);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* The player reads and writes the system clipboard on the plugin's behalf.
|
|
28
|
+
* Text is plain text; images are written from a PNG or JPEG file.
|
|
29
|
+
*
|
|
30
|
+
* @capability clipboard
|
|
31
|
+
* @since 1.0.0
|
|
32
|
+
* @category Services
|
|
33
|
+
*/
|
|
34
|
+
export interface Clipboard {
|
|
35
|
+
/**
|
|
36
|
+
* Plain text on the clipboard; `null` when it holds none.
|
|
37
|
+
*
|
|
38
|
+
* @capability clipboard
|
|
39
|
+
* @throws `unavailable` (rejects) The player runs without a user interface.
|
|
40
|
+
* @throws `unsupported` (rejects) A system other than macOS and Windows.
|
|
41
|
+
* @throws `failed` (rejects) The system refused (Windows).
|
|
42
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
43
|
+
* `clipboard`.
|
|
44
|
+
*
|
|
45
|
+
* @example Open a URL the user copied
|
|
46
|
+
* A "Paste and play" command opens the address on the clipboard. `readText`
|
|
47
|
+
* resolves with the clipboard's text, or `null` when it holds none (an
|
|
48
|
+
* image, nothing).
|
|
49
|
+
* ```ts
|
|
50
|
+
* const text = await plugin.clipboard.readText();
|
|
51
|
+
* if (text?.startsWith("https://")) await plugin.player.open(text.trim());
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @since 1.0.0
|
|
55
|
+
*/
|
|
56
|
+
readText(): Promise<string | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Replace the clipboard with text (at most 1 MiB of UTF-8).
|
|
59
|
+
*
|
|
60
|
+
* @capability clipboard
|
|
61
|
+
* @throws `invalid` (rejects) More than 1 MiB of UTF-8.
|
|
62
|
+
* @throws `unavailable` (rejects) The player runs without a user interface.
|
|
63
|
+
* @throws `unsupported` (rejects) A system other than macOS and Windows.
|
|
64
|
+
* @throws `failed` (rejects) The system refused.
|
|
65
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
66
|
+
* `clipboard`.
|
|
67
|
+
*
|
|
68
|
+
* @example Copy the playing file's path
|
|
69
|
+
* The path of the playing file is put on the clipboard, replacing what was
|
|
70
|
+
* there. It resolves once the system has it.
|
|
71
|
+
* ```ts
|
|
72
|
+
* const { source } = plugin.player.state;
|
|
73
|
+
* if (source) await plugin.clipboard.writeText(source);
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @since 1.0.0
|
|
77
|
+
*/
|
|
78
|
+
writeText(text: string): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Replace the clipboard with a PNG or JPEG file (absolute path, at most 16 MB).
|
|
81
|
+
*
|
|
82
|
+
* @capability clipboard
|
|
83
|
+
* @throws `missing` (rejects) The file does not exist.
|
|
84
|
+
* @throws `invalid` (rejects) Not an absolute path, not PNG or JPEG, over
|
|
85
|
+
* 16 MB, or (Windows) an image the system cannot decode.
|
|
86
|
+
* @throws `unavailable` (rejects) The player runs without a user interface.
|
|
87
|
+
* @throws `unsupported` (rejects) A system other than macOS and Windows.
|
|
88
|
+
* @throws `failed` (rejects) The system refused.
|
|
89
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
90
|
+
* `clipboard`.
|
|
91
|
+
*
|
|
92
|
+
* @example Copy a snapshot to the clipboard
|
|
93
|
+
* A frame the plugin saved as PNG is put on the clipboard, ready to paste
|
|
94
|
+
* into another application. The player reads the file and writes the
|
|
95
|
+
* image (macOS: its original bytes; Windows: a bitmap plus PNG).
|
|
96
|
+
* ```ts
|
|
97
|
+
* declare const pngPath: string;
|
|
98
|
+
* // ---cut---
|
|
99
|
+
* await plugin.clipboard.writeImage(pngPath); // absolute path, at most 16 MB
|
|
100
|
+
* await plugin.osd.show("Snapshot copied");
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @since 1.0.0
|
|
104
|
+
*/
|
|
105
|
+
writeImage(path: string): Promise<void>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Keeping the computer awake (`playback.control`).
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* A request, not a command: the player keeps the system awake while any
|
|
112
|
+
* plugin asks for it and a source plays, and withdraws a plugin's request
|
|
113
|
+
* when it stops.
|
|
114
|
+
*
|
|
115
|
+
* @capability playback.control
|
|
116
|
+
*
|
|
117
|
+
* @example Keep the display on while playing
|
|
118
|
+
* The screen does not dim or sleep while a video plays, and may again when
|
|
119
|
+
* it is paused or ends. The player takes the system's assertion only while
|
|
120
|
+
* a source is ready and not paused.
|
|
121
|
+
* ```ts
|
|
122
|
+
* await plugin.power.preventSleep("playing"); // "off" withdraws the request
|
|
123
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @since 1.0.0
|
|
126
|
+
* @category Services
|
|
127
|
+
*/
|
|
128
|
+
export interface Power {
|
|
129
|
+
/**
|
|
130
|
+
* `"playing"`: while a source is ready and not paused, keep the display
|
|
131
|
+
* (video) or the system (audio only) from sleeping. Any plugin asking is
|
|
132
|
+
* enough; a stopped plugin's request is withdrawn.
|
|
133
|
+
*
|
|
134
|
+
* It only registers the request (`invalid` for a bad mode); failures to
|
|
135
|
+
* take the system assertion are logged by the player, not reported.
|
|
136
|
+
* macOS uses `caffeinate`, Windows the thread execution state; elsewhere
|
|
137
|
+
* it has no effect.
|
|
138
|
+
*
|
|
139
|
+
* @capability playback.control
|
|
140
|
+
* @throws `invalid` (rejects) A mode other than `"off"` or `"playing"`.
|
|
141
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
142
|
+
* `playback.control`.
|
|
143
|
+
*
|
|
144
|
+
* @example Follow the plugin's own "Keep awake" setting
|
|
145
|
+
* The plugin keeps the display awake during playback only while its
|
|
146
|
+
* setting is on. Each call replaces this plugin's request; `"off"`
|
|
147
|
+
* withdraws it, and other plugins' requests still count.
|
|
148
|
+
* ```ts
|
|
149
|
+
* const store = plugin.storage.json("power", { defaults: { keepAwake: true } });
|
|
150
|
+
* await plugin.power.preventSleep(store.value.keepAwake ? "playing" : "off");
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @since 1.0.0
|
|
154
|
+
*/
|
|
155
|
+
preventSleep(mode: "off" | "playing"): Promise<void>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* The output of {@link Tools.run}.
|
|
159
|
+
*
|
|
160
|
+
* @see {@link Tools.run}
|
|
161
|
+
* @since 1.0.0
|
|
162
|
+
* @category Services
|
|
163
|
+
*/
|
|
164
|
+
export interface ToolResult {
|
|
165
|
+
/** Standard output (at most `maxOutput` bytes). */
|
|
166
|
+
stdout: Uint8Array;
|
|
167
|
+
/** The last 64 KiB of standard error. */
|
|
168
|
+
stderr: string;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* `plugin.tools`: ffmpeg and ffprobe, run in this plugin's process. No
|
|
172
|
+
* capability.
|
|
173
|
+
*
|
|
174
|
+
* @example Read a file's format with ffprobe
|
|
175
|
+
* The plugin gets a media file's container details as JSON. `available`
|
|
176
|
+
* says whether ffprobe can be found (`VESTRA_FFPROBE`, then `PATH`); `run`
|
|
177
|
+
* starts it from the plugin's own process without a shell and resolves
|
|
178
|
+
* with its output as bytes once it exits, or rejects when it fails
|
|
179
|
+
* (`failed`, with the last stderr line) or takes longer than `timeoutMs`.
|
|
180
|
+
* ```ts
|
|
181
|
+
* declare const path: string;
|
|
182
|
+
* // ---cut---
|
|
183
|
+
* if (await plugin.tools.available("ffprobe")) {
|
|
184
|
+
* const { stdout } = await plugin.tools.run(
|
|
185
|
+
* "ffprobe",
|
|
186
|
+
* ["-v", "error", "-show_format", "-of", "json", path],
|
|
187
|
+
* { timeoutMs: 10_000 }, // give up on a stuck file after 10 s
|
|
188
|
+
* );
|
|
189
|
+
* const info = JSON.parse(new TextDecoder().decode(stdout));
|
|
190
|
+
* void info.format; // container, duration, bit rate…
|
|
191
|
+
* }
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* @remarks
|
|
195
|
+
* ffmpeg and ffprobe run as child processes of the plugin. When
|
|
196
|
+
* `VESTRA_FFMPEG` / `VESTRA_FFPROBE` is set, only that path is used (an
|
|
197
|
+
* invalid one means the tool is not found); otherwise the tool is searched
|
|
198
|
+
* on `PATH`. A found path is kept for the plugin's run, a miss is looked up
|
|
199
|
+
* again next time. Disposing the
|
|
200
|
+
* scope ends a running process.
|
|
201
|
+
*
|
|
202
|
+
* @capability None.
|
|
203
|
+
* @since 1.0.0
|
|
204
|
+
* @category Services
|
|
205
|
+
*/
|
|
206
|
+
export interface Tools {
|
|
207
|
+
/**
|
|
208
|
+
* Run ffmpeg or ffprobe (`VESTRA_FFMPEG` / `VESTRA_FFPROBE` when set, else
|
|
209
|
+
* found on `PATH`) without a shell. Rejects `unavailable` (not found), `timeout`,
|
|
210
|
+
* `failed` (non-zero exit; `message` is the last stderr line), `invalid`
|
|
211
|
+
* (output over `maxOutput`, default 16 MiB), `aborted` (`signal`). Timeout,
|
|
212
|
+
* `signal` and disposing the scope end the process.
|
|
213
|
+
*
|
|
214
|
+
* Arguments are passed as-is (no quoting needed). On Windows `PATHEXT` is
|
|
215
|
+
* honoured. Ending sends SIGTERM, then SIGKILL after 300 ms (immediate on
|
|
216
|
+
* Windows).
|
|
217
|
+
*
|
|
218
|
+
* @capability None.
|
|
219
|
+
* @throws `unavailable` (rejects) The tool was not found.
|
|
220
|
+
* @throws `timeout` (rejects) It ran longer than `timeoutMs`; the process is
|
|
221
|
+
* ended.
|
|
222
|
+
* @throws `failed` (rejects) A non-zero exit; `message` is the last stderr
|
|
223
|
+
* line.
|
|
224
|
+
* @throws `invalid` (rejects) Bad arguments or options, or output over
|
|
225
|
+
* `maxOutput`.
|
|
226
|
+
* @throws `aborted` (rejects) `signal` aborted, or the scope was disposed.
|
|
227
|
+
*
|
|
228
|
+
* @example Cut a clip without re-encoding
|
|
229
|
+
* The plugin writes the first ten seconds of the file to a new file. `run`
|
|
230
|
+
* starts ffmpeg without a shell (arguments pass as they are, no quoting)
|
|
231
|
+
* and resolves with its output once it exits with 0; a failure rejects
|
|
232
|
+
* with the last line ffmpeg printed.
|
|
233
|
+
* ```ts
|
|
234
|
+
* declare const input: string;
|
|
235
|
+
* declare const output: string;
|
|
236
|
+
* // ---cut---
|
|
237
|
+
* await plugin.tools.run(
|
|
238
|
+
* "ffmpeg",
|
|
239
|
+
* ["-y", "-i", input, "-t", "10", "-c", "copy", output],
|
|
240
|
+
* { timeoutMs: 60_000 }, // give up after a minute
|
|
241
|
+
* );
|
|
242
|
+
* ```
|
|
243
|
+
*
|
|
244
|
+
* @since 1.0.0
|
|
245
|
+
*/
|
|
246
|
+
run(tool: "ffmpeg" | "ffprobe", args: string[], options: {
|
|
247
|
+
timeoutMs: number;
|
|
248
|
+
maxOutput?: number;
|
|
249
|
+
signal?: AbortSignal;
|
|
250
|
+
}): Promise<ToolResult>;
|
|
251
|
+
/**
|
|
252
|
+
* Whether the tool can be found.
|
|
253
|
+
*
|
|
254
|
+
* @capability None.
|
|
255
|
+
* @throws Never; it resolves `false` when the tool cannot be found.
|
|
256
|
+
*
|
|
257
|
+
* @example Grey out a command when ffmpeg is missing
|
|
258
|
+
* The Export item is disabled with a reason when ffmpeg is not installed.
|
|
259
|
+
* `available` looks the tool up (a found path is kept, a miss is looked
|
|
260
|
+
* up again on the next call, so installing it later is noticed).
|
|
261
|
+
* ```ts
|
|
262
|
+
* const hasFfmpeg = await plugin.tools.available("ffmpeg");
|
|
263
|
+
* plugin.commands.add({
|
|
264
|
+
* id: "export",
|
|
265
|
+
* path: ["@video", "Export…"],
|
|
266
|
+
* enabled: hasFfmpeg,
|
|
267
|
+
* // Shown with the greyed-out item.
|
|
268
|
+
* reason: hasFfmpeg ? null : "Install ffmpeg to export",
|
|
269
|
+
* run() {},
|
|
270
|
+
* });
|
|
271
|
+
* ```
|
|
272
|
+
*
|
|
273
|
+
* @since 1.0.0
|
|
274
|
+
*/
|
|
275
|
+
available(tool: "ffmpeg" | "ffprobe"): Promise<boolean>;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* A JSON file `dataDirectory/<name>.json`, loaded once and saved debounced.
|
|
279
|
+
*
|
|
280
|
+
* @remarks
|
|
281
|
+
* One JSON file, `<dataDirectory>/<name>.json`, made by
|
|
282
|
+
* {@link Storage.json}. `value` is read once when the store is made;
|
|
283
|
+
* `save` replaces it in memory at once and writes the file `debounceMs`
|
|
284
|
+
* later (300 by default) through a temporary file and a rename, so a
|
|
285
|
+
* crash never leaves half a file. Writes to one file run one at a time,
|
|
286
|
+
* newest last. Disposing the store, its scope or stopping the plugin
|
|
287
|
+
* writes a pending save first.
|
|
288
|
+
*
|
|
289
|
+
* @capability None.
|
|
290
|
+
*
|
|
291
|
+
* @example Count how often the plugin was started
|
|
292
|
+
* The counter grows by one at every start and is kept in `stats.json`. The
|
|
293
|
+
* store reads the file when it is made (the defaults when there is none),
|
|
294
|
+
* and `save` writes the new count a moment later.
|
|
295
|
+
* ```ts
|
|
296
|
+
* const stats = plugin.storage.json("stats", { defaults: { starts: 0 } });
|
|
297
|
+
* stats.save({ starts: stats.value.starts + 1 }); // written 300 ms later
|
|
298
|
+
* ```
|
|
299
|
+
*
|
|
300
|
+
* @since 1.0.0
|
|
301
|
+
* @category Services
|
|
302
|
+
*/
|
|
303
|
+
export interface JsonStore<T> extends Disposable {
|
|
304
|
+
/** The loaded value, or `defaults` when the file was missing or broken. */
|
|
305
|
+
readonly value: T;
|
|
306
|
+
/**
|
|
307
|
+
* Replace the value now and write it after `debounceMs`.
|
|
308
|
+
* Only `save` writes: mutating `value` in place is not saved.
|
|
309
|
+
*
|
|
310
|
+
* @capability None.
|
|
311
|
+
*
|
|
312
|
+
* @example Save a slider value without writing on every move
|
|
313
|
+
* Dragging the plugin's volume slider updates the stored value at once but
|
|
314
|
+
* writes the file only once the user stops for 300 ms. Each `save`
|
|
315
|
+
* replaces the value and restarts the debounce timer.
|
|
316
|
+
* ```ts
|
|
317
|
+
* declare const volumeWindow: Window;
|
|
318
|
+
* // ---cut---
|
|
319
|
+
* const store = plugin.storage.json("prefs", { defaults: { volume: 100 } });
|
|
320
|
+
* volumeWindow.on("volumeChanged", (volume: number) => store.save({ ...store.value, volume }));
|
|
321
|
+
* ```
|
|
322
|
+
*
|
|
323
|
+
* @since 1.0.0
|
|
324
|
+
*/
|
|
325
|
+
save(next: T): void;
|
|
326
|
+
/**
|
|
327
|
+
* Write a pending save now.
|
|
328
|
+
*
|
|
329
|
+
* @capability None.
|
|
330
|
+
* @throws `Error` (rejects) Node's file system error when the file cannot
|
|
331
|
+
* be written; its `code` is Node's (`ENOSPC`, `EACCES` and so on), not a
|
|
332
|
+
* player error code.
|
|
333
|
+
*
|
|
334
|
+
* @example Write the file now before copying it
|
|
335
|
+
* Before exporting its settings, the plugin makes sure the file on disk has
|
|
336
|
+
* the latest value. `flush` writes a pending save at once (or waits for
|
|
337
|
+
* the write in progress) and resolves when the file is written.
|
|
338
|
+
* ```ts
|
|
339
|
+
* import { copyFile } from "node:fs/promises";
|
|
340
|
+
* import { join } from "node:path";
|
|
341
|
+
* declare const exportPath: string;
|
|
342
|
+
* // ---cut---
|
|
343
|
+
* const store = plugin.storage.json("settings", { defaults: { step: 5 } });
|
|
344
|
+
* await store.flush(); // the file now has the newest value
|
|
345
|
+
* await copyFile(join(plugin.dataDirectory, "settings.json"), exportPath);
|
|
346
|
+
* ```
|
|
347
|
+
*
|
|
348
|
+
* @since 1.0.0
|
|
349
|
+
*/
|
|
350
|
+
flush(): Promise<void>;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* `plugin.storage`: JSON settings files. No capability.
|
|
354
|
+
*
|
|
355
|
+
* @remarks
|
|
356
|
+
* Two kinds of storage, both private to the plugin and needing no
|
|
357
|
+
* capability. {@link Storage.json} keeps settings in JSON files in the
|
|
358
|
+
* plugin's data directory: they last across player restarts and upgrades,
|
|
359
|
+
* and are written by the plugin's own process. {@link Storage.session}
|
|
360
|
+
* keeps values in the player's memory for one player run: they survive a
|
|
361
|
+
* restart of the plugin's process but not of the player. Secrets belong in
|
|
362
|
+
* {@link Secrets}, never in either.
|
|
363
|
+
*
|
|
364
|
+
* @capability None.
|
|
365
|
+
*
|
|
366
|
+
* @example Keep a setting across restarts and a queue for this run
|
|
367
|
+
* The plugin's step size survives a restart of the player; its work queue
|
|
368
|
+
* survives a reload of the plugin but starts empty with the next player
|
|
369
|
+
* run. The JSON store reads its file at once and writes it shortly after
|
|
370
|
+
* each `save`; session values live in the player process.
|
|
371
|
+
* ```ts
|
|
372
|
+
* const settings = plugin.storage.json("settings", { defaults: { step: 5 } });
|
|
373
|
+
* const queue = (await plugin.storage.session.get<string[]>("queue")) ?? [];
|
|
374
|
+
* void settings.value.step; // read synchronously, from the file or the defaults
|
|
375
|
+
* void queue;
|
|
376
|
+
* ```
|
|
377
|
+
*
|
|
378
|
+
* @see [Store a plugin's data](../../../docs/how-to/store-data.md), [Remember a window's state between runs](../../../docs/how-to/window-state.md)
|
|
379
|
+
* @since 1.0.0
|
|
380
|
+
* @category Services
|
|
381
|
+
*/
|
|
382
|
+
export interface Storage {
|
|
383
|
+
/**
|
|
384
|
+
* Open `<name>.json` (`name`: 1–64 of `a-z 0-9 -`). A file that cannot be
|
|
385
|
+
* parsed or fails `validate` is renamed `<name>.json.bad-<timestamp>` and
|
|
386
|
+
* `defaults` is used. Writes go to a temporary file, then a rename. Disposal
|
|
387
|
+
* (and stopping the plugin) flushes first.
|
|
388
|
+
*
|
|
389
|
+
* The file is read synchronously on creation; `debounceMs` defaults to
|
|
390
|
+
* 300. Files are created with mode 0600. `invalid` for a bad name.
|
|
391
|
+
*
|
|
392
|
+
* @example Keep settings and a recent list in a JSON file
|
|
393
|
+
* The plugin remembers whether to resume and the last ten files across
|
|
394
|
+
* restarts. `json` reads `settings.json` from the data directory at once
|
|
395
|
+
* (or uses `defaults` when it is missing or fails `validate`); `save`
|
|
396
|
+
* replaces the value and writes the file 300 ms later, and stopping the
|
|
397
|
+
* plugin writes a pending save first.
|
|
398
|
+
* ```ts
|
|
399
|
+
* declare const path: string;
|
|
400
|
+
* // ---cut---
|
|
401
|
+
* type Settings = { resume: boolean; recent: string[] };
|
|
402
|
+
* const store = plugin.storage.json<Settings>("settings", {
|
|
403
|
+
* defaults: { resume: true, recent: [] },
|
|
404
|
+
* validate: (v): v is Settings =>
|
|
405
|
+
* typeof v === "object" && v !== null && typeof (v as Settings).resume === "boolean",
|
|
406
|
+
* });
|
|
407
|
+
* // Only `save` writes: changing `store.value` in place is not saved.
|
|
408
|
+
* store.save({ ...store.value, recent: [path, ...store.value.recent].slice(0, 10) });
|
|
409
|
+
* ```
|
|
410
|
+
*
|
|
411
|
+
* @capability None.
|
|
412
|
+
* @throws `invalid` (throws) A bad `name`, or a `debounceMs` that is not a
|
|
413
|
+
* non-negative number.
|
|
414
|
+
* @since 1.0.0
|
|
415
|
+
*/
|
|
416
|
+
json<T>(name: string, options: {
|
|
417
|
+
defaults: T;
|
|
418
|
+
validate?: (value: unknown) => value is T;
|
|
419
|
+
debounceMs?: number;
|
|
420
|
+
}): JsonStore<T>;
|
|
421
|
+
/**
|
|
422
|
+
* Values the player keeps in its memory for this plugin until the player
|
|
423
|
+
* quits: they survive a restart of the plugin's process (reload, upgrade,
|
|
424
|
+
* crash, failed start) but not the player. See {@link SessionStorage}.
|
|
425
|
+
*
|
|
426
|
+
* @since 1.5.25
|
|
427
|
+
*/
|
|
428
|
+
readonly session: SessionStorage;
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* `plugin.storage.session`: JSON values that live as long as the **player**,
|
|
432
|
+
* not as long as the plugin's process (like `chrome.storage.session`). No
|
|
433
|
+
* capability; needs player 0.5.5 (older players reject the calls with
|
|
434
|
+
* `unsupported`).
|
|
435
|
+
*
|
|
436
|
+
* `activate` can run more than once in one player session: the player stops
|
|
437
|
+
* and starts a plugin's process again when the plugin is reloaded (also a
|
|
438
|
+
* development load with `--plugin`) or upgraded, and after a crash or a
|
|
439
|
+
* failed start. A later player may also restart plugins that share a
|
|
440
|
+
* process, or stop an idle plugin and start it on its next use. Variables do
|
|
441
|
+
* not survive that; keep state that must survive a restart here (or in
|
|
442
|
+
* {@link Storage.json | storage.json} when it must also survive the player
|
|
443
|
+
* quitting).
|
|
444
|
+
*
|
|
445
|
+
* | | Values |
|
|
446
|
+
* | --- | --- |
|
|
447
|
+
* | The plugin's process restarts: reload, upgrade, crash | kept |
|
|
448
|
+
* | The plugin failed to start or failed, and starts again | kept |
|
|
449
|
+
* | The user, or a plugin with `plugins.manage` (`packages.setEnabled(id, false)`), disables the plugin | cleared |
|
|
450
|
+
* | An update or reinstall with `enabled: false` switches an enabled plugin off (player 0.6.4) | cleared |
|
|
451
|
+
* | The plugin is uninstalled | cleared |
|
|
452
|
+
* | An earlier installation kept for a later reinstall (after a `replace`) is dropped because the player keeps only so many | cleared |
|
|
453
|
+
* | The player quits | cleared |
|
|
454
|
+
*
|
|
455
|
+
* - **Per plugin.** A plugin sees only its own keys. Like
|
|
456
|
+
* {@link Plugin.dataDirectory | dataDirectory}, the values belong to the
|
|
457
|
+
* installation: a development load (`--plugin`) has its own.
|
|
458
|
+
* - **JSON.** `set` stores what `JSON.stringify(value)` writes, and `get`
|
|
459
|
+
* returns that, parsed: `undefined` object properties are dropped, a `Date`
|
|
460
|
+
* comes back as a string, `NaN` as `null`. **The order of an object's
|
|
461
|
+
* properties is not kept**: they may come back in another order. A value
|
|
462
|
+
* `JSON.stringify` cannot write (`undefined`, a function, a cycle, a
|
|
463
|
+
* `BigInt`) or a string with a lone surrogate (`"\uD800"`) fails with
|
|
464
|
+
* `invalid`. The value is not checked when it comes back: after an upgrade
|
|
465
|
+
* an older version's value may be read, so check its shape when it matters.
|
|
466
|
+
* - **Bounded.** Keys are 1–1024 bytes of text. A plugin holds at most 1 MiB,
|
|
467
|
+
* counting every key, its value as compact JSON and 128 bytes per key
|
|
468
|
+
* ({@link "@vestra-player/sdk/testing"!SESSION_STORAGE_LIMITS | SESSION_STORAGE_LIMITS}); a
|
|
469
|
+
* `set` beyond it fails with `limit` and changes nothing. One `set` is
|
|
470
|
+
* also one message to the player, at most 1 MiB with its key, so a single
|
|
471
|
+
* value is a little smaller than that; a larger one fails with `limit`.
|
|
472
|
+
* Split large state over several keys.
|
|
473
|
+
* - **Each call is one request.** A `set` is kept once its promise
|
|
474
|
+
* resolves, so a crash right after loses nothing; there is no debounce.
|
|
475
|
+
* Calls answer in order.
|
|
476
|
+
*
|
|
477
|
+
* @example Keep a work queue across a plugin restart
|
|
478
|
+
* A download queue survives the plugin being reloaded or crashing, and is
|
|
479
|
+
* empty again when the player starts next time. Session storage lives in
|
|
480
|
+
* the player process for one player run; each `set` is kept once its
|
|
481
|
+
* promise resolves, so nothing is lost if the plugin dies right after.
|
|
482
|
+
* ```ts
|
|
483
|
+
* // A queue the plugin is working through: after a reload or a crash it
|
|
484
|
+
* // goes on where it stopped, after the player quits it starts afresh.
|
|
485
|
+
* const pending = (await plugin.storage.session.get<string[]>("queue")) ?? [];
|
|
486
|
+
* pending.push("https://example.com/a.mkv");
|
|
487
|
+
* await plugin.storage.session.set("queue", pending);
|
|
488
|
+
* ```
|
|
489
|
+
*
|
|
490
|
+
* @since 1.5.25
|
|
491
|
+
*
|
|
492
|
+
* @remarks
|
|
493
|
+
* The player keeps the values in its own memory, per plugin, until it
|
|
494
|
+
* quits; the plugin's process may restart in between. Each call is one
|
|
495
|
+
* request to the player, answered in order, with no debounce.
|
|
496
|
+
*
|
|
497
|
+
* @capability None.
|
|
498
|
+
* @category Services
|
|
499
|
+
*/
|
|
500
|
+
export interface SessionStorage {
|
|
501
|
+
/**
|
|
502
|
+
* The value stored under `key`, or `undefined` when there is none.
|
|
503
|
+
*
|
|
504
|
+
* @capability None.
|
|
505
|
+
* @throws `invalid` (rejects) A bad key (empty, over 1024 bytes, with NUL
|
|
506
|
+
* or a lone surrogate).
|
|
507
|
+
* @throws `disabled` (rejects) The plugin is stopping.
|
|
508
|
+
* @throws `unsupported` (rejects) A player before 0.5.5.
|
|
509
|
+
*
|
|
510
|
+
* @example Pick up where the plugin stopped after a reload
|
|
511
|
+
* After a reload the plugin resumes the item it was working on. `get`
|
|
512
|
+
* asks the player and resolves with the value parsed from JSON, or
|
|
513
|
+
* `undefined` when nothing is stored; the value is not checked, so its
|
|
514
|
+
* shape is checked here.
|
|
515
|
+
* ```ts
|
|
516
|
+
* const current = await plugin.storage.session.get<{ url: string }>("current");
|
|
517
|
+
* if (typeof current?.url === "string") await plugin.osd.show("Resuming {}", [current.url]);
|
|
518
|
+
* ```
|
|
519
|
+
*
|
|
520
|
+
* @since 1.5.25
|
|
521
|
+
*/
|
|
522
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
523
|
+
/**
|
|
524
|
+
* Store or replace `value` under `key`. `invalid` for a bad key or a value
|
|
525
|
+
* that is not JSON, `limit` when the plugin's values would be over 1 MiB
|
|
526
|
+
* or the request over the 1 MiB message limit.
|
|
527
|
+
*
|
|
528
|
+
* @capability None.
|
|
529
|
+
* @throws `invalid` (rejects) A bad key, or a value JSON cannot hold (or a
|
|
530
|
+
* string with a lone surrogate).
|
|
531
|
+
* @throws `disabled` (rejects) The plugin is stopping.
|
|
532
|
+
* @throws `unsupported` (rejects) A player before 0.5.5.
|
|
533
|
+
* @throws `limit` (rejects) The plugin's values would pass 1 MiB, or the
|
|
534
|
+
* request the 1 MiB message limit; nothing changes.
|
|
535
|
+
*
|
|
536
|
+
* @example Remember the item being worked on
|
|
537
|
+
* The plugin records the URL it is processing, so a reload can continue
|
|
538
|
+
* with it. The value is stored as `JSON.stringify` writes it, and is kept
|
|
539
|
+
* once the promise resolves.
|
|
540
|
+
* ```ts
|
|
541
|
+
* await plugin.storage.session.set("current", { url: "https://example.com/a.mkv" });
|
|
542
|
+
* ```
|
|
543
|
+
*
|
|
544
|
+
* @since 1.5.25
|
|
545
|
+
*/
|
|
546
|
+
set(key: string, value: unknown): Promise<void>;
|
|
547
|
+
/**
|
|
548
|
+
* Remove a key; removing a missing one is fine.
|
|
549
|
+
*
|
|
550
|
+
* @capability None.
|
|
551
|
+
* @throws `invalid` (rejects) A bad key.
|
|
552
|
+
* @throws `disabled` (rejects) The plugin is stopping.
|
|
553
|
+
* @throws `unsupported` (rejects) A player before 0.5.5.
|
|
554
|
+
*
|
|
555
|
+
* @example Forget the item once it is done
|
|
556
|
+
* When the work finishes, the plugin removes the entry so that a later
|
|
557
|
+
* reload does not resume it. Removing a key that is not there is fine.
|
|
558
|
+
* ```ts
|
|
559
|
+
* await plugin.storage.session.delete("current");
|
|
560
|
+
* ```
|
|
561
|
+
*
|
|
562
|
+
* @since 1.5.25
|
|
563
|
+
*/
|
|
564
|
+
delete(key: string): Promise<void>;
|
|
565
|
+
/**
|
|
566
|
+
* The keys stored, in code point order.
|
|
567
|
+
*
|
|
568
|
+
* @capability None.
|
|
569
|
+
* @throws `disabled` (rejects) The plugin is stopping.
|
|
570
|
+
* @throws `unsupported` (rejects) A player before 0.5.5.
|
|
571
|
+
*
|
|
572
|
+
* @example Clear everything the plugin stored for this run
|
|
573
|
+
* The plugin starts its session state over. `keys` lists the plugin's own
|
|
574
|
+
* keys in code point order; each `delete` is a request of its own.
|
|
575
|
+
* ```ts
|
|
576
|
+
* const session = plugin.storage.session;
|
|
577
|
+
* for (const key of await session.keys()) await session.delete(key);
|
|
578
|
+
* ```
|
|
579
|
+
*
|
|
580
|
+
* @since 1.5.25
|
|
581
|
+
*/
|
|
582
|
+
keys(): Promise<string[]>;
|
|
583
|
+
}
|
|
584
|
+
/**
|
|
585
|
+
* `plugin.system`: files and links, executed by the player. No capability.
|
|
586
|
+
* Other systems than macOS and Windows: `unsupported`.
|
|
587
|
+
*
|
|
588
|
+
* @example Show an exported file and open the help page
|
|
589
|
+
* The file manager opens with the exported file selected, then the
|
|
590
|
+
* browser opens the plugin's help page. The player performs both on the
|
|
591
|
+
* plugin's behalf; they reject with `unsupported` on systems other than
|
|
592
|
+
* macOS and Windows, which the plugin turns into a message.
|
|
593
|
+
* ```ts
|
|
594
|
+
* declare const exportedFile: string;
|
|
595
|
+
* // ---cut---
|
|
596
|
+
* try {
|
|
597
|
+
* await plugin.system.reveal(exportedFile);
|
|
598
|
+
* await plugin.system.openUrl("https://example.com/help");
|
|
599
|
+
* } catch (error) {
|
|
600
|
+
* if ((error as PluginError).code !== "unsupported") throw error;
|
|
601
|
+
* await plugin.osd.show(`Saved to ${exportedFile}`); // no file manager to open
|
|
602
|
+
* }
|
|
603
|
+
* ```
|
|
604
|
+
*
|
|
605
|
+
* @remarks
|
|
606
|
+
* The player opens files, folders and links with the system's own
|
|
607
|
+
* applications for the plugin, so the plugin never runs `open`,
|
|
608
|
+
* `explorer` or a shell itself.
|
|
609
|
+
*
|
|
610
|
+
* @capability None.
|
|
611
|
+
* @since 1.0.0
|
|
612
|
+
* @category Services
|
|
613
|
+
*/
|
|
614
|
+
export interface System {
|
|
615
|
+
/**
|
|
616
|
+
* Select files or folders in the file manager (macOS: one Finder window
|
|
617
|
+
* for all, so several files of one folder are all highlighted; Windows:
|
|
618
|
+
* one Explorer window per folder, selecting its first path). Paths that do
|
|
619
|
+
* not exist are skipped; `missing` when none exists; at most 256.
|
|
620
|
+
*
|
|
621
|
+
* @capability None.
|
|
622
|
+
* @throws `missing` (rejects) None of the paths exists.
|
|
623
|
+
* @throws `invalid` (rejects) No paths, or more than 256.
|
|
624
|
+
* @throws `failed` (rejects) The system refused.
|
|
625
|
+
* @throws `timeout` (rejects) macOS did not answer within 10 s.
|
|
626
|
+
* @throws `unsupported` (rejects) A system other than macOS and Windows.
|
|
627
|
+
*
|
|
628
|
+
* @example Show the exported files in the file manager
|
|
629
|
+
* After an export, the file manager opens with the new files selected.
|
|
630
|
+
* Paths that no longer exist are skipped; the call rejects with `missing`
|
|
631
|
+
* only when none of them exists.
|
|
632
|
+
* ```ts
|
|
633
|
+
* declare const exported: string[];
|
|
634
|
+
* // ---cut---
|
|
635
|
+
* await plugin.system.reveal(exported); // macOS: one Finder window for all
|
|
636
|
+
* ```
|
|
637
|
+
*
|
|
638
|
+
* @since 1.0.0
|
|
639
|
+
*/
|
|
640
|
+
reveal(paths: string | string[]): Promise<void>;
|
|
641
|
+
/**
|
|
642
|
+
* Open the file or folder with its default application. `missing` if absent.
|
|
643
|
+
*
|
|
644
|
+
* @capability None.
|
|
645
|
+
* @throws `missing` (rejects) The path does not exist.
|
|
646
|
+
* @throws `invalid` (rejects) An empty path.
|
|
647
|
+
* @throws `failed` (rejects) The system refused.
|
|
648
|
+
* @throws `timeout` (rejects) macOS did not answer within 10 s.
|
|
649
|
+
* @throws `unsupported` (rejects) A system other than macOS and Windows.
|
|
650
|
+
*
|
|
651
|
+
* @example Open the plugin's data folder
|
|
652
|
+
* A "Show data folder" command opens the plugin's data directory in the
|
|
653
|
+
* file manager, which is the default application for a folder.
|
|
654
|
+
* ```ts
|
|
655
|
+
* await plugin.system.openPath(plugin.dataDirectory);
|
|
656
|
+
* ```
|
|
657
|
+
*
|
|
658
|
+
* @since 1.0.0
|
|
659
|
+
*/
|
|
660
|
+
openPath(path: string): Promise<void>;
|
|
661
|
+
/**
|
|
662
|
+
* Open an http(s) address in the default browser. `invalid` for another
|
|
663
|
+
* protocol or a malformed address; `timeout` after 10 s, `failed` otherwise.
|
|
664
|
+
*
|
|
665
|
+
* @capability None.
|
|
666
|
+
* @throws `invalid` (rejects) Not an `http:` or `https:` address.
|
|
667
|
+
* @throws `timeout` (rejects) macOS did not hand it to the browser within
|
|
668
|
+
* 10 s.
|
|
669
|
+
* @throws `failed` (rejects) The system refused.
|
|
670
|
+
* @throws `unsupported` (rejects) A system other than macOS and Windows.
|
|
671
|
+
*
|
|
672
|
+
* @example Open the plugin's help page
|
|
673
|
+
* A Help command opens the plugin's documentation in the default browser.
|
|
674
|
+
* Only `http:` and `https:` addresses are opened.
|
|
675
|
+
* ```ts
|
|
676
|
+
* await plugin.system.openUrl("https://example.com/plugin/help");
|
|
677
|
+
* ```
|
|
678
|
+
*
|
|
679
|
+
* @since 1.0.0
|
|
680
|
+
*/
|
|
681
|
+
openUrl(url: string): Promise<void>;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* Per-plugin secrets (API keys, passwords) in the system's secret store:
|
|
685
|
+
* the login keychain on macOS, the Credential Manager on Windows. Needs
|
|
686
|
+
* `secrets`. `unsupported` on systems without a store, `unavailable` when
|
|
687
|
+
* the store cannot be reached (no keychain, a read-only store, no logged-in
|
|
688
|
+
* session on Windows): keep a fallback (or ask again).
|
|
689
|
+
*
|
|
690
|
+
* The user denying access and other system errors reject `failed`. Never
|
|
691
|
+
* write secrets to settings files.
|
|
692
|
+
*
|
|
693
|
+
* @example Ask for an API token once and keep it in the keychain
|
|
694
|
+
* The user types the token the first time; later runs read it from the
|
|
695
|
+
* system's secret store. `get` resolves `null` when nothing is stored; a
|
|
696
|
+
* store that is missing or refuses access rejects (`unsupported`,
|
|
697
|
+
* `unavailable`, `failed`), and then the plugin keeps the token for this
|
|
698
|
+
* run only and asks again next time.
|
|
699
|
+
* ```ts
|
|
700
|
+
* declare function askUser(): Promise<string | null>;
|
|
701
|
+
* // ---cut---
|
|
702
|
+
* let token: string | null = null;
|
|
703
|
+
* try {
|
|
704
|
+
* token = await plugin.secrets.get("api-token");
|
|
705
|
+
* } catch {
|
|
706
|
+
* // No usable store: fall through and ask.
|
|
707
|
+
* }
|
|
708
|
+
* if (token === null) {
|
|
709
|
+
* token = await askUser();
|
|
710
|
+
* if (token)
|
|
711
|
+
* await plugin.secrets.set("api-token", token).catch(() => {}); // best effort
|
|
712
|
+
* }
|
|
713
|
+
* ```
|
|
714
|
+
*
|
|
715
|
+
* @remarks
|
|
716
|
+
* Values are stored by the player in the system's secret store under this
|
|
717
|
+
* plugin's id: other plugins cannot read them. Names are 1–64 of
|
|
718
|
+
* `a-z 0-9 . _ -`, values 1–1024 bytes of text, at most 256 names per
|
|
719
|
+
* plugin (`SECRET_LIMITS`).
|
|
720
|
+
*
|
|
721
|
+
* @capability secrets
|
|
722
|
+
* @since 1.0.0
|
|
723
|
+
* @category Services
|
|
724
|
+
*/
|
|
725
|
+
export interface Secrets {
|
|
726
|
+
/**
|
|
727
|
+
* The stored value, `null` when there is none.
|
|
728
|
+
*
|
|
729
|
+
* @capability secrets
|
|
730
|
+
* @throws `unsupported` (rejects) The system has no secret store.
|
|
731
|
+
* @throws `unavailable` (rejects) The store cannot be reached (no keychain,
|
|
732
|
+
* a read-only store, no logged-in session on Windows).
|
|
733
|
+
* @throws `failed` (rejects) The user denied access, or another system
|
|
734
|
+
* error.
|
|
735
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
736
|
+
* `secrets`.
|
|
737
|
+
* @throws `invalid` (rejects) A bad name.
|
|
738
|
+
*
|
|
739
|
+
* @example Read a stored token
|
|
740
|
+
* The plugin uses the token the user saved earlier, or asks for one when
|
|
741
|
+
* there is none. `get` resolves with the stored text or `null`.
|
|
742
|
+
* ```ts
|
|
743
|
+
* const token = await plugin.secrets.get("api-token");
|
|
744
|
+
* if (token === null) await plugin.osd.show("Sign in first");
|
|
745
|
+
* ```
|
|
746
|
+
*
|
|
747
|
+
* @since 1.0.0
|
|
748
|
+
*/
|
|
749
|
+
get(name: string): Promise<string | null>;
|
|
750
|
+
/**
|
|
751
|
+
* Store or replace (`name`: 1–64 of `a-z 0-9 . _ -`; value 1–1024 bytes).
|
|
752
|
+
* At most 256 different names per plugin: a new name beyond that rejects
|
|
753
|
+
* `limit`. Uninstalling with `removeData` deletes them.
|
|
754
|
+
*
|
|
755
|
+
* @capability secrets
|
|
756
|
+
* @throws `invalid` (rejects) A bad name, or a value that is empty, has NUL
|
|
757
|
+
* or is over 1024 bytes.
|
|
758
|
+
* @throws `limit` (rejects) A new name beyond 256.
|
|
759
|
+
* @throws `unsupported` (rejects) The system has no secret store.
|
|
760
|
+
* @throws `unavailable` (rejects) The store cannot be reached (no keychain,
|
|
761
|
+
* a read-only store, no logged-in session on Windows).
|
|
762
|
+
* @throws `failed` (rejects) The user denied access, or another system
|
|
763
|
+
* error.
|
|
764
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
765
|
+
* `secrets`.
|
|
766
|
+
*
|
|
767
|
+
* @example Save the token the user entered
|
|
768
|
+
* The token the user typed is kept in the system's secret store for later
|
|
769
|
+
* runs. `set` stores or replaces it; the store can refuse, in which case
|
|
770
|
+
* the plugin keeps the token for this run only.
|
|
771
|
+
* ```ts
|
|
772
|
+
* declare const token: string;
|
|
773
|
+
* // ---cut---
|
|
774
|
+
* await plugin.secrets.set("api-token", token).catch((error: PluginError) => {
|
|
775
|
+
* // No store, or access denied: the token is not kept for next time.
|
|
776
|
+
* void plugin.osd.show("The token could not be saved ({})", [error.code]);
|
|
777
|
+
* });
|
|
778
|
+
* ```
|
|
779
|
+
*
|
|
780
|
+
* @since 1.0.0
|
|
781
|
+
*/
|
|
782
|
+
set(name: string, value: string): Promise<void>;
|
|
783
|
+
/**
|
|
784
|
+
* Remove; removing a missing secret is fine.
|
|
785
|
+
*
|
|
786
|
+
* @capability secrets
|
|
787
|
+
* @throws `invalid` (rejects) A bad name.
|
|
788
|
+
* @throws `unsupported` (rejects) The system has no secret store.
|
|
789
|
+
* @throws `unavailable` (rejects) The store cannot be reached (no keychain,
|
|
790
|
+
* a read-only store, no logged-in session on Windows).
|
|
791
|
+
* @throws `failed` (rejects) The user denied access, or another system
|
|
792
|
+
* error.
|
|
793
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
794
|
+
* `secrets`.
|
|
795
|
+
*
|
|
796
|
+
* @example Sign out by removing the token
|
|
797
|
+
* Signing out removes the stored token, so the next start asks again.
|
|
798
|
+
* Removing a name that is not stored is fine.
|
|
799
|
+
* ```ts
|
|
800
|
+
* await plugin.secrets.delete("api-token");
|
|
801
|
+
* ```
|
|
802
|
+
*
|
|
803
|
+
* @since 1.0.0
|
|
804
|
+
*/
|
|
805
|
+
delete(name: string): Promise<void>;
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Which renderer draws the video: `"gpu"` (the GPU renderer, where
|
|
809
|
+
* {@link RenderOptions | render options} apply) or `"software"` (they fail
|
|
810
|
+
* with `unsupported`). The same values as {@link SystemInfo.rendering}.
|
|
811
|
+
*
|
|
812
|
+
* @example Tell the user which renderer draws the video
|
|
813
|
+
* The plugin shows "Rendering: GPU" or "Rendering: software" on the
|
|
814
|
+
* video. `state()` asks the player; `mode` is the renderer drawing now,
|
|
815
|
+
* which can differ from the saved choice until the next start.
|
|
816
|
+
* ```ts
|
|
817
|
+
* const { mode } = await plugin.rendering.state();
|
|
818
|
+
* const label: Record<RenderingMode, string> = { gpu: "GPU", software: "software" };
|
|
819
|
+
* await plugin.osd.show("Rendering: {}", [label[mode]]);
|
|
820
|
+
* ```
|
|
821
|
+
*
|
|
822
|
+
* @player 0.5.13
|
|
823
|
+
* @since 1.5.35
|
|
824
|
+
* @category Services
|
|
825
|
+
*/
|
|
826
|
+
export type RenderingMode = "gpu" | "software";
|
|
827
|
+
/**
|
|
828
|
+
* The user's saved rendering choice: `"auto"` (the player decides: the GPU
|
|
829
|
+
* where this system has a GPU renderer that is not ruled out, software
|
|
830
|
+
* elsewhere), `"gpu"` or `"software"`. It applies from the next start.
|
|
831
|
+
*
|
|
832
|
+
* @example Describe the saved choice on a settings page
|
|
833
|
+
* The plugin turns the saved choice into the words its settings page
|
|
834
|
+
* shows. `preference` is what the user chose, not what draws now:
|
|
835
|
+
* {@link RenderingState.nextMode} says what the next start will use.
|
|
836
|
+
* ```ts
|
|
837
|
+
* const { preference } = await plugin.rendering.state();
|
|
838
|
+
* // One word per choice; the Record type makes sure none is missing.
|
|
839
|
+
* const words: Record<RenderingPreference, string> = {
|
|
840
|
+
* auto: "Automatic",
|
|
841
|
+
* gpu: "GPU",
|
|
842
|
+
* software: "Software",
|
|
843
|
+
* };
|
|
844
|
+
* console.log(words[preference]);
|
|
845
|
+
* ```
|
|
846
|
+
*
|
|
847
|
+
* @player 0.5.13
|
|
848
|
+
* @since 1.5.35
|
|
849
|
+
* @category Services
|
|
850
|
+
*/
|
|
851
|
+
export type RenderingPreference = "auto" | "gpu" | "software";
|
|
852
|
+
/**
|
|
853
|
+
* Why the player renders the way it does, as a stable kebab-case code. A
|
|
854
|
+
* newer player may add codes: compare with the ones you handle and show
|
|
855
|
+
* {@link RenderingReason.message} for the rest. The codes of player 0.5.13:
|
|
856
|
+
*
|
|
857
|
+
* | Code | Meaning |
|
|
858
|
+
* | --- | --- |
|
|
859
|
+
* | `default` | Nothing ruled the GPU out; the platform's default (macOS: GPU; Windows since version 0.5.14: software until the user turns on GPU rendering). |
|
|
860
|
+
* | `pending` | GPU rendering is starting. |
|
|
861
|
+
* | `headless` | The player runs without a user interface. |
|
|
862
|
+
* | `no-gpu-backend` | This player has no GPU renderer on this system (Linux today; Windows has one since version 0.5.14, off by default, which reports `default`). |
|
|
863
|
+
* | `env` | The environment chose it (`VESTRA_RENDER`, `VESTRA_SOFTWARE_RENDER`). |
|
|
864
|
+
* | `preference` | The user chose software rendering ({@link Rendering.setPreference}); GPU chosen and available reports `default`. |
|
|
865
|
+
* | `denylist` | The graphics driver is on the player's denylist. |
|
|
866
|
+
* | `previous-crash` | GPU rendering crashed at an earlier start; it is tried again after a driver or player update, or after 7 days. |
|
|
867
|
+
* | `init-failed` | GPU rendering could not start. |
|
|
868
|
+
* | `window-fallback` | A window could not show GPU frames; the player switched to software during the run. |
|
|
869
|
+
*
|
|
870
|
+
* @example Explain a driver problem differently from a user choice
|
|
871
|
+
* The plugin adds advice only when the graphics driver is the cause.
|
|
872
|
+
* `reason.code` is looked up among the codes the plugin knows; an unknown
|
|
873
|
+
* code from a newer player falls through to the player's own message.
|
|
874
|
+
* ```ts
|
|
875
|
+
* const { reason } = await plugin.rendering.state();
|
|
876
|
+
* // Advice for the codes this plugin knows; any other code shows the message.
|
|
877
|
+
* const advice: Partial<Record<RenderingReasonCode, string>> = {
|
|
878
|
+
* denylist: "Update the graphics driver to use the GPU.",
|
|
879
|
+
* "previous-crash": "The GPU renderer crashed earlier; it is retried after an update.",
|
|
880
|
+
* };
|
|
881
|
+
* console.log(advice[reason.code] ?? reason.message);
|
|
882
|
+
* ```
|
|
883
|
+
*
|
|
884
|
+
* @player 0.5.13
|
|
885
|
+
* @since 1.5.35
|
|
886
|
+
* @category Services
|
|
887
|
+
*/
|
|
888
|
+
export type RenderingReasonCode = "default" | "pending" | "headless" | "no-gpu-backend" | "env" | "preference" | "denylist" | "previous-crash" | "init-failed" | "window-fallback" | (string & {});
|
|
889
|
+
/**
|
|
890
|
+
* Why the player renders, or will render, the way it does: a stable
|
|
891
|
+
* {@link RenderingReasonCode} to branch on and a sentence to show.
|
|
892
|
+
*
|
|
893
|
+
* @example Show why GPU rendering is off
|
|
894
|
+
* The plugin logs the player's explanation when the video is drawn in
|
|
895
|
+
* software. `message` is the player's English text and may name the
|
|
896
|
+
* graphics adapter and driver version; `code` is for decisions.
|
|
897
|
+
* ```ts
|
|
898
|
+
* const { mode, reason } = await plugin.rendering.state();
|
|
899
|
+
* if (mode === "software") console.log(`${reason.code}: ${reason.message}`);
|
|
900
|
+
* ```
|
|
901
|
+
*
|
|
902
|
+
* @player 0.5.13
|
|
903
|
+
* @since 1.5.35
|
|
904
|
+
* @category Services
|
|
905
|
+
*/
|
|
906
|
+
export interface RenderingReason {
|
|
907
|
+
/** The reason as a stable kebab-case code; branch on this. */
|
|
908
|
+
readonly code: RenderingReasonCode;
|
|
909
|
+
/**
|
|
910
|
+
* The reason as English text for people, for example with the graphics
|
|
911
|
+
* adapter's name and driver version; never branch on it.
|
|
912
|
+
*/
|
|
913
|
+
readonly message: string;
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* What {@link Rendering.state} and {@link Rendering.setPreference} report:
|
|
917
|
+
* the renderer drawing now and why, the saved choice, what the next start
|
|
918
|
+
* will use, and whether `"gpu"` can be chosen.
|
|
919
|
+
*
|
|
920
|
+
* @example Label a menu item while a change waits for a restart
|
|
921
|
+
* The plugin adds "(Restart to Apply)" to its GPU item after the user
|
|
922
|
+
* changed the choice. `restartPending` compares the mode the next start
|
|
923
|
+
* will use with the mode this run started with, so it turns false again
|
|
924
|
+
* when the user changes the choice back.
|
|
925
|
+
* ```ts
|
|
926
|
+
* const state = await plugin.rendering.state();
|
|
927
|
+
* const title = state.restartPending
|
|
928
|
+
* ? "GPU Rendering (Experimental) (Restart to Apply)"
|
|
929
|
+
* : "GPU Rendering (Experimental)";
|
|
930
|
+
* console.log(title, state.nextMode, state.nextReason.message);
|
|
931
|
+
* ```
|
|
932
|
+
*
|
|
933
|
+
* @player 0.5.13
|
|
934
|
+
* @since 1.5.35
|
|
935
|
+
* @category Services
|
|
936
|
+
*/
|
|
937
|
+
export interface RenderingState {
|
|
938
|
+
/** The renderer drawing the video now; the same as {@link SystemInfo.rendering}. */
|
|
939
|
+
readonly mode: RenderingMode;
|
|
940
|
+
/** Why {@link RenderingState.mode} is what it is now. */
|
|
941
|
+
readonly reason: RenderingReason;
|
|
942
|
+
/** The saved choice; it applies from the next start. */
|
|
943
|
+
readonly preference: RenderingPreference;
|
|
944
|
+
/**
|
|
945
|
+
* The renderer the next start will use, as far as the player knows now:
|
|
946
|
+
* from the saved choice, the environment, the driver denylist and an
|
|
947
|
+
* earlier GPU crash.
|
|
948
|
+
*/
|
|
949
|
+
readonly nextMode: RenderingMode;
|
|
950
|
+
/** Why the next start will use {@link RenderingState.nextMode}. */
|
|
951
|
+
readonly nextReason: RenderingReason;
|
|
952
|
+
/**
|
|
953
|
+
* `true` while {@link RenderingState.nextMode} differs from the mode this
|
|
954
|
+
* run started with, so a restart would change the renderer; `false` again
|
|
955
|
+
* when the user changes the choice back.
|
|
956
|
+
*/
|
|
957
|
+
readonly restartPending: boolean;
|
|
958
|
+
/** Whether the user can choose `"gpu"` now. */
|
|
959
|
+
readonly gpu: {
|
|
960
|
+
/** `true` when `setPreference("gpu")` would be accepted. */
|
|
961
|
+
readonly available: boolean;
|
|
962
|
+
/**
|
|
963
|
+
* Why `"gpu"` cannot be chosen (`no-gpu-backend`, `headless`,
|
|
964
|
+
* `denylist`, `previous-crash`…); `null` while it is available.
|
|
965
|
+
*/
|
|
966
|
+
readonly reason: RenderingReason | null;
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
/**
|
|
970
|
+
* `plugin.rendering`: the player's rendering choice, GPU or software.
|
|
971
|
+
* Reading it needs no capability; changing the saved choice and restarting
|
|
972
|
+
* the player need `rendering.manage`.
|
|
973
|
+
*
|
|
974
|
+
* @remarks
|
|
975
|
+
* The choice works like a browser's hardware acceleration switch: the
|
|
976
|
+
* player saves it in its own settings at once, and the running player
|
|
977
|
+
* keeps its renderer. The next start uses it, whether the user restarts
|
|
978
|
+
* now through {@link Rendering.restartPlayer} or quits and starts the
|
|
979
|
+
* player later. The player can still rule the GPU out at a start (an
|
|
980
|
+
* environment variable, a graphics driver on its denylist, a GPU crash at
|
|
981
|
+
* an earlier start); {@link RenderingState.nextMode} and
|
|
982
|
+
* {@link RenderingState.nextReason} say what the next start will do.
|
|
983
|
+
*
|
|
984
|
+
* The renderer is a choice for the whole player and a restart stops every
|
|
985
|
+
* plugin, so change it only when the user asks. A plugin that only needs
|
|
986
|
+
* to know whether render options work reads {@link SystemInfo.rendering}.
|
|
987
|
+
*
|
|
988
|
+
* Players before 0.5.13 do not know these requests: `state()` rejects with
|
|
989
|
+
* `unsupported`, and they do not load a plugin that declares
|
|
990
|
+
* `rendering.manage` (an unknown capability). Declare
|
|
991
|
+
* `"minPlayerVersion": "0.5.13"` when the plugin needs this.
|
|
992
|
+
*
|
|
993
|
+
* @example Offer GPU rendering as a menu item
|
|
994
|
+
* Settings ▸ GPU Rendering (Experimental) carries a check mark while the
|
|
995
|
+
* next start will draw on the GPU, is greyed out with the player's reason
|
|
996
|
+
* where the GPU cannot be chosen, and saves the other choice when picked.
|
|
997
|
+
* `state()` reads the player's view once; `setPreference` saves the choice
|
|
998
|
+
* and resolves with the new state, which the item's bound values read
|
|
999
|
+
* again after the handler. The how-to guide adds the restart.
|
|
1000
|
+
* ```ts
|
|
1001
|
+
* let state = await plugin.rendering.state();
|
|
1002
|
+
* plugin.commands.add({
|
|
1003
|
+
* id: "gpu-rendering",
|
|
1004
|
+
* path: ["@settings", "GPU Rendering (Experimental)"],
|
|
1005
|
+
* checked: () => state.nextMode === "gpu", // also for "auto" on a GPU system
|
|
1006
|
+
* enabled: () => state.gpu.available,
|
|
1007
|
+
* reason: () => state.gpu.reason?.message ?? null, // shown while disabled
|
|
1008
|
+
* run: async () => {
|
|
1009
|
+
* const choice = state.nextMode === "gpu" ? "software" : "gpu";
|
|
1010
|
+
* state = await plugin.rendering.setPreference(choice);
|
|
1011
|
+
* },
|
|
1012
|
+
* });
|
|
1013
|
+
* ```
|
|
1014
|
+
*
|
|
1015
|
+
* @capability None to read the state; `rendering.manage` to change the
|
|
1016
|
+
* choice or restart the player.
|
|
1017
|
+
* @see [Offer a GPU rendering switch](../../../docs/how-to/gpu-rendering.md)
|
|
1018
|
+
* @player 0.5.13; older players reject the requests as unknown
|
|
1019
|
+
* (`unsupported`).
|
|
1020
|
+
* @since 1.5.35
|
|
1021
|
+
* @category Services
|
|
1022
|
+
*/
|
|
1023
|
+
export interface Rendering {
|
|
1024
|
+
/**
|
|
1025
|
+
* Read the renderer drawing now, the saved choice, what the next start
|
|
1026
|
+
* will use and whether `"gpu"` can be chosen. No capability.
|
|
1027
|
+
*
|
|
1028
|
+
* @capability None.
|
|
1029
|
+
* @throws `unsupported` (rejects) A player before 0.5.13, which does not
|
|
1030
|
+
* know the request.
|
|
1031
|
+
*
|
|
1032
|
+
* @example Find out whether the GPU can be chosen
|
|
1033
|
+
* The plugin reads the state once while it starts and keeps it for its
|
|
1034
|
+
* menu. Each call asks the player; `gpu.available` is false, with a
|
|
1035
|
+
* reason, where this player has no GPU renderer, without a user
|
|
1036
|
+
* interface, or when the driver is ruled out. An older player rejects
|
|
1037
|
+
* with `unsupported`, and the plugin then offers nothing.
|
|
1038
|
+
* ```ts
|
|
1039
|
+
* const state = await plugin.rendering.state().catch((error: PluginError) => {
|
|
1040
|
+
* if (error.code === "unsupported") return null; // a player before 0.5.13
|
|
1041
|
+
* throw error;
|
|
1042
|
+
* });
|
|
1043
|
+
* if (state && !state.gpu.available) console.log(state.gpu.reason?.message);
|
|
1044
|
+
* ```
|
|
1045
|
+
*
|
|
1046
|
+
* @since 1.5.35
|
|
1047
|
+
*/
|
|
1048
|
+
state(): Promise<RenderingState>;
|
|
1049
|
+
/**
|
|
1050
|
+
* Save the user's rendering choice. The player writes it to its settings
|
|
1051
|
+
* at once; the running player keeps its renderer, and the next start
|
|
1052
|
+
* uses the choice whether or not the user restarts now. `"auto"` and
|
|
1053
|
+
* `"software"` are always accepted.
|
|
1054
|
+
*
|
|
1055
|
+
* @param preference - `"auto"`, `"gpu"` or `"software"`.
|
|
1056
|
+
* @returns The new state; {@link RenderingState.restartPending} tells
|
|
1057
|
+
* whether a restart would change the renderer.
|
|
1058
|
+
*
|
|
1059
|
+
* @capability rendering.manage
|
|
1060
|
+
* @throws `invalid` (rejects) `preference` is not `"auto"`, `"gpu"` or
|
|
1061
|
+
* `"software"`.
|
|
1062
|
+
* @throws `unsupported` (rejects) `"gpu"` where this player has no GPU
|
|
1063
|
+
* renderer on this system, or runs without a user interface; also any
|
|
1064
|
+
* value on a player before 0.5.13.
|
|
1065
|
+
* @throws `unavailable` (rejects) `"gpu"` while it is ruled out: the
|
|
1066
|
+
* graphics driver is on the player's denylist, or GPU rendering crashed
|
|
1067
|
+
* at an earlier start. The message says which, with the adapter's name
|
|
1068
|
+
* and the driver version.
|
|
1069
|
+
* @throws `failed` (rejects) The player could not write its settings file.
|
|
1070
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
1071
|
+
* `rendering.manage`.
|
|
1072
|
+
*
|
|
1073
|
+
* @example Switch back to the automatic choice
|
|
1074
|
+
* A Reset item returns the renderer to the player's own choice and tells
|
|
1075
|
+
* the user when it takes effect. `setPreference` saves the choice and
|
|
1076
|
+
* resolves with the new state; `restartPending` is true when the next
|
|
1077
|
+
* start will draw differently from this run. A refused save leaves the
|
|
1078
|
+
* old choice in place.
|
|
1079
|
+
* ```ts
|
|
1080
|
+
* try {
|
|
1081
|
+
* const state = await plugin.rendering.setPreference("auto");
|
|
1082
|
+
* if (state.restartPending) await plugin.osd.show("Applies after a restart");
|
|
1083
|
+
* } catch (error) {
|
|
1084
|
+
* // `failed`: the player's settings file could not be written.
|
|
1085
|
+
* await plugin.osd.show("Not saved ({})", [(error as PluginError).code]);
|
|
1086
|
+
* }
|
|
1087
|
+
* ```
|
|
1088
|
+
*
|
|
1089
|
+
* @since 1.5.35
|
|
1090
|
+
*/
|
|
1091
|
+
setPreference(preference: RenderingPreference): Promise<RenderingState>;
|
|
1092
|
+
/**
|
|
1093
|
+
* Restart the player so that a changed choice applies now. Resolves once
|
|
1094
|
+
* the player has accepted; the player then quits normally, which stops
|
|
1095
|
+
* every plugin (this one included), and starts again with the same
|
|
1096
|
+
* plugins. A local file that was open continues at its position, paused
|
|
1097
|
+
* or playing as it was; a stream or another source that is not a file is
|
|
1098
|
+
* not reopened.
|
|
1099
|
+
*
|
|
1100
|
+
* @capability rendering.manage
|
|
1101
|
+
* @throws `unsupported` (rejects) The player runs without a user
|
|
1102
|
+
* interface; also a player before 0.5.13.
|
|
1103
|
+
* @throws `busy` (rejects) A restart is already under way, or this player
|
|
1104
|
+
* was itself started by a restart less than 30 seconds ago (so a plugin
|
|
1105
|
+
* that restarts on activation cannot make a loop).
|
|
1106
|
+
* @throws `failed` (rejects) The new player process could not be started;
|
|
1107
|
+
* the player keeps running.
|
|
1108
|
+
* @throws `capability` (rejects) The manifest does not declare
|
|
1109
|
+
* `rendering.manage`.
|
|
1110
|
+
*
|
|
1111
|
+
* @example Restart after asking the user
|
|
1112
|
+
* After the user changed the renderer, the plugin offers to restart now.
|
|
1113
|
+
* `confirm` asks first, because a restart closes every window (it needs
|
|
1114
|
+
* `ui.dialog`); once `restartPlayer` resolves, the player is quitting and
|
|
1115
|
+
* will stop this plugin, so nothing else is started after it.
|
|
1116
|
+
* ```ts
|
|
1117
|
+
* const { confirmed } = await plugin.dialogs.confirm({
|
|
1118
|
+
* message: "Restart the player now?",
|
|
1119
|
+
* detail: "The new renderer is used from the restart on. The video continues where it is.",
|
|
1120
|
+
* confirm: "Restart",
|
|
1121
|
+
* });
|
|
1122
|
+
* if (confirmed)
|
|
1123
|
+
* await plugin.rendering.restartPlayer().catch((error: PluginError) => {
|
|
1124
|
+
* // `failed`: the new player could not start; this one keeps running.
|
|
1125
|
+
* void plugin.osd.show("Could not restart ({})", [error.code]);
|
|
1126
|
+
* });
|
|
1127
|
+
* ```
|
|
1128
|
+
*
|
|
1129
|
+
* @since 1.5.35
|
|
1130
|
+
*/
|
|
1131
|
+
restartPlayer(): Promise<void>;
|
|
1132
|
+
}
|