-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipc-contract.js
More file actions
84 lines (74 loc) · 2.6 KB
/
Copy pathipc-contract.js
File metadata and controls
84 lines (74 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// IPC contract — single source of truth for channel names and payload shapes.
// The main process imports this module directly. The sandboxed preload mirrors
// these channel strings as literals because Electron limits sandboxed preloads
// to built-in modules; tests assert the mirror cannot drift. The renderer sees
// the exposed API via `window.api` (typed below for editor autocomplete).
'use strict';
/**
* Channels invoked from the renderer and handled in the main process
* (request/response via `ipcRenderer.invoke` <-> `ipcMain.handle`).
*/
const INVOKE_CHANNELS = /** @type {const} */ (['system-info', 'get-app-version']);
/**
* Channels pushed from the main process to the renderer
* (subscription via `webContents.send` -> `ipcRenderer.on`).
*/
const EVENT_CHANNELS = /** @type {const} */ ([
'power-event',
'update-downloaded',
'update-error',
]);
/**
* Every channel the preload bridge is allowed to touch. Anything else is
* rejected — this is the security crux of the IPC surface.
*/
const ALL_CHANNELS = /** @type {const} */ ([
...INVOKE_CHANNELS,
...EVENT_CHANNELS,
]);
/**
* @typedef {typeof INVOKE_CHANNELS[number]} IpcInvokeChannel
* @typedef {typeof EVENT_CHANNELS[number]} IpcEventChannel
* @typedef {IpcInvokeChannel | IpcEventChannel} IpcChannel
*/
/**
* Payload returned by `api.getSystemInfo()`.
*
* @typedef {Object} SystemInfo
* @property {NodeJS.Platform} platform
* @property {string} arch
* @property {string} hostname
* @property {string} electronVersion
* @property {string} appVersion
*/
/**
* Broadcast whenever the OS power state changes. `kind` mirrors the
* `powerMonitor` events we subscribe to.
*
* @typedef {Object} PowerEvent
* @property {'suspend' | 'resume' | 'on-ac' | 'on-battery'} kind
* @property {number} at Unix epoch millis when the event was emitted.
*/
/**
* Broadcast when electron-updater fails to download or install an update.
* The renderer can surface this so the user knows why the app isn't
* updating instead of wondering at a silent failure.
*
* @typedef {Object} UpdateError
* @property {string} message
* @property {number} attempts Consecutive failure count this session.
*/
/**
* The API exposed on `window.api` in the renderer.
*
* @typedef {Object} ExposedApi
* @property {() => Promise<SystemInfo>} getSystemInfo
* @property {(cb: (event: PowerEvent) => void) => () => void} onPowerEvent
* Subscribe to power events. Returns an `unsubscribe` function that
* removes the listener — always call it on teardown (e.g. `beforeunload`).
*/
module.exports = {
INVOKE_CHANNELS,
EVENT_CHANNELS,
ALL_CHANNELS,
};