@domicile-desktop/system-network 0.0.0-alpha-a2a1a1a59e77

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Connor Prussin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # @domicile-desktop/system-network
2
+
3
+ NetworkManager for a Domicile shell: connectivity and the primary connection,
4
+ with the Wi-Fi network's name and signal. Built on
5
+ [`@domicile-desktop/sdk/system`](../chrome-sdk/README.md).
6
+
7
+ ## Usage
8
+
9
+ ```ts
10
+ import { system } from "@domicile-desktop/sdk/system";
11
+ import { watchNetwork } from "@domicile-desktop/system-network/network";
12
+
13
+ const stop = watchNetwork(system(domicile), (network) => {
14
+ network.match({
15
+ Err: () => hide(),
16
+ Ok: ({ connectivity, link }) => show(connectivity, link),
17
+ });
18
+ });
19
+ ```
20
+
21
+ - Reports the network once, then after each change NetworkManager signals
22
+ (`PropertiesChanged`) on the objects it read: the manager, the primary
23
+ connection and its access point.
24
+ - `link` is `None`, `Wired`, `Wifi` (SSID and strength, 0 through 1) or
25
+ `Other` (VPNs and the rest), by NetworkManager's connection type.
26
+ - An `Err` is a D-Bus failure, such as NetworkManager not running.
27
+ - A bug or an unexpected reply shape throws, logged to the console.
28
+
29
+ See [SHELL-SYSTEM-ACCESS.md](/docs/SHELL-SYSTEM-ACCESS.md).
30
+
31
+ ## Test
32
+
33
+ ```sh
34
+ bun run turbo test --filter @domicile-desktop/system-network
35
+ ```
36
+
37
+ Tests replay D-Bus replies, in the compositor's JSON, through an injected
38
+ `System`.
@@ -0,0 +1,54 @@
1
+ import type { Result } from "@cprussin/option-result";
2
+ import type { System, SystemError } from "@domicile-desktop/sdk/system";
3
+ /** Whether the host reaches the internet. */
4
+ export declare enum Connectivity {
5
+ Unknown = 0,
6
+ None = 1,
7
+ /** Behind a captive portal. */
8
+ Portal = 2,
9
+ /** On a network, without the internet. */
10
+ Limited = 3,
11
+ Full = 4
12
+ }
13
+ export declare enum LinkKind {
14
+ None = 0,
15
+ Wired = 1,
16
+ Wifi = 2,
17
+ Other = 3
18
+ }
19
+ /** The primary connection. `name` is the connection's name in NetworkManager. */
20
+ export declare const Link: {
21
+ None: () => {
22
+ kind: LinkKind.None;
23
+ };
24
+ /** A VPN, a bridge, a modem and the rest. */
25
+ Other: (name: string) => {
26
+ kind: LinkKind.Other;
27
+ name: string;
28
+ };
29
+ /** `strength` is 0 through 1. */
30
+ Wifi: (ssid: string, strength: number) => {
31
+ kind: LinkKind.Wifi;
32
+ ssid: string;
33
+ strength: number;
34
+ };
35
+ Wired: (name: string) => {
36
+ kind: LinkKind.Wired;
37
+ name: string;
38
+ };
39
+ };
40
+ export type Link = ReturnType<(typeof Link)[keyof typeof Link]>;
41
+ export type Network = {
42
+ connectivity: Connectivity;
43
+ link: Link;
44
+ };
45
+ /** The calls this library makes. */
46
+ export type NetworkSystem = Pick<System, "dbusCall" | "dbusMatch">;
47
+ /**
48
+ * Calls `onNetwork` with the network now and after each change, and returns a
49
+ * function that stops watching.
50
+ *
51
+ * An `Err` is a D-Bus failure, such as NetworkManager not running. The watch
52
+ * keeps listening after one, so a later change reads again.
53
+ */
54
+ export declare const watchNetwork: (system: NetworkSystem, onNetwork: (network: Result<Network, SystemError>) => void) => (() => void);
@@ -0,0 +1,206 @@
1
+ // NetworkManager over the system bus: connectivity and the primary
2
+ // connection, kept current. See docs/SHELL-SYSTEM-ACCESS.md.
3
+ import { Err, Ok } from "@cprussin/option-result";
4
+ import { Bus } from "@domicile-desktop/sdk/system";
5
+ import { z } from "zod";
6
+ const SERVICE = "org.freedesktop.NetworkManager";
7
+ const ROOT = "/org/freedesktop/NetworkManager";
8
+ /** The object path NetworkManager uses for "none". */
9
+ const NO_OBJECT = "/";
10
+ /** Whether the host reaches the internet. */
11
+ export var Connectivity;
12
+ (function (Connectivity) {
13
+ Connectivity[Connectivity["Unknown"] = 0] = "Unknown";
14
+ Connectivity[Connectivity["None"] = 1] = "None";
15
+ /** Behind a captive portal. */
16
+ Connectivity[Connectivity["Portal"] = 2] = "Portal";
17
+ /** On a network, without the internet. */
18
+ Connectivity[Connectivity["Limited"] = 3] = "Limited";
19
+ Connectivity[Connectivity["Full"] = 4] = "Full";
20
+ })(Connectivity || (Connectivity = {}));
21
+ export var LinkKind;
22
+ (function (LinkKind) {
23
+ LinkKind[LinkKind["None"] = 0] = "None";
24
+ LinkKind[LinkKind["Wired"] = 1] = "Wired";
25
+ LinkKind[LinkKind["Wifi"] = 2] = "Wifi";
26
+ LinkKind[LinkKind["Other"] = 3] = "Other";
27
+ })(LinkKind || (LinkKind = {}));
28
+ /** The primary connection. `name` is the connection's name in NetworkManager. */
29
+ export const Link = {
30
+ None: () => ({ kind: LinkKind.None }),
31
+ /** A VPN, a bridge, a modem and the rest. */
32
+ Other: (name) => ({ kind: LinkKind.Other, name }),
33
+ /** `strength` is 0 through 1. */
34
+ Wifi: (ssid, strength) => ({
35
+ kind: LinkKind.Wifi,
36
+ ssid,
37
+ strength,
38
+ }),
39
+ Wired: (name) => ({ kind: LinkKind.Wired, name }),
40
+ };
41
+ /**
42
+ * Calls `onNetwork` with the network now and after each change, and returns a
43
+ * function that stops watching.
44
+ *
45
+ * An `Err` is a D-Bus failure, such as NetworkManager not running. The watch
46
+ * keeps listening after one, so a later change reads again.
47
+ */
48
+ export const watchNetwork = (system, onNetwork) => {
49
+ const watch = { listening: undefined, stopped: false };
50
+ const report = (network) => {
51
+ if (!watch.stopped) {
52
+ onNetwork(network);
53
+ }
54
+ };
55
+ follow(system, watch, report).catch((error) => {
56
+ // biome-ignore lint/suspicious/noConsole: surfacing a background failure
57
+ console.error("Failed to watch the network", error);
58
+ });
59
+ return () => {
60
+ watch.stopped = true;
61
+ watch.listening?.stop();
62
+ };
63
+ };
64
+ /** Listen, read, then read again on each change until the match ends. */
65
+ const follow = async (system, watch, report) => {
66
+ const matched = await system.dbusMatch({
67
+ bus: Bus.System,
68
+ interface: "org.freedesktop.DBus.Properties",
69
+ member: "PropertiesChanged",
70
+ sender: SERVICE,
71
+ });
72
+ await matched.match({
73
+ Err: (error) => {
74
+ report(Err(error));
75
+ return Promise.resolve();
76
+ },
77
+ Ok: async (listening) => {
78
+ watch.listening = listening;
79
+ if (watch.stopped) {
80
+ listening.stop();
81
+ }
82
+ await changes(system, listening, report);
83
+ const ended = await listening.ended;
84
+ ended.match({
85
+ Err: (error) => {
86
+ report(Err(error));
87
+ },
88
+ Ok: () => {
89
+ /* stopped by the caller */
90
+ },
91
+ });
92
+ },
93
+ });
94
+ };
95
+ /** Read now, and again on each signal from an object the network uses. */
96
+ const changes = async (system, listening, report) => {
97
+ const reader = listening.items.getReader();
98
+ const first = await read(system);
99
+ report(first.network);
100
+ let paths = first.paths;
101
+ for (let next = await reader.read(); !next.done; next = await reader.read()) {
102
+ if (paths.has(next.value.path)) {
103
+ const again = await read(system);
104
+ report(again.network);
105
+ paths = again.paths;
106
+ }
107
+ }
108
+ };
109
+ const read = async (system) => {
110
+ const root = await properties(system, ROOT, SERVICE, rootSchema);
111
+ return root.match({
112
+ Err: (error) => Promise.resolve({ network: Err(error), paths: new Set([ROOT]) }),
113
+ Ok: async ({ Connectivity: connectivity, PrimaryConnection: primary }) => {
114
+ const link = await linkOf(system, primary);
115
+ return {
116
+ network: link.result.map((found) => ({
117
+ connectivity,
118
+ link: found,
119
+ })),
120
+ paths: new Set([ROOT, ...link.paths]),
121
+ };
122
+ },
123
+ });
124
+ };
125
+ /** The primary connection at `path`. */
126
+ const linkOf = async (system, path) => {
127
+ if (path === NO_OBJECT) {
128
+ return { paths: [], result: Ok(Link.None()) };
129
+ }
130
+ else {
131
+ const active = await properties(system, path, `${SERVICE}.Connection.Active`, activeSchema);
132
+ return active.match({
133
+ Err: (error) => Promise.resolve({ paths: [path], result: Err(error) }),
134
+ Ok: async ({ Id: name, SpecificObject: specific, Type: type, }) => {
135
+ switch (type) {
136
+ case "802-11-wireless": {
137
+ const point = await properties(system, specific, `${SERVICE}.AccessPoint`, accessPointSchema);
138
+ return {
139
+ paths: [path, specific],
140
+ result: point.map(({ Ssid: ssid, Strength: strength }) => Link.Wifi(new TextDecoder().decode(new Uint8Array(ssid)), strength / 100)),
141
+ };
142
+ }
143
+ case "802-3-ethernet": {
144
+ return { paths: [path], result: Ok(Link.Wired(name)) };
145
+ }
146
+ default: {
147
+ return { paths: [path], result: Ok(Link.Other(name)) };
148
+ }
149
+ }
150
+ },
151
+ });
152
+ }
153
+ };
154
+ /** `Properties.GetAll` on one of NetworkManager's objects, parsed. */
155
+ const properties = async (system, path, interfaceName, schema) => (await system.dbusCall({
156
+ body: [interfaceName],
157
+ bus: Bus.System,
158
+ destination: SERVICE,
159
+ interface: "org.freedesktop.DBus.Properties",
160
+ member: "GetAll",
161
+ path,
162
+ signature: "s",
163
+ })).map(({ body }) => z.tuple([schema]).parse(body)[0]);
164
+ /** A `v`, as `domicile_host::dbus_json` writes it, read as its value. */
165
+ const variant = (value) => z.object({ value }).transform(({ value: held }) => held);
166
+ /** NetworkManager's `NMConnectivityState`. */
167
+ const connectivityOf = (value) => {
168
+ switch (value) {
169
+ case 0: {
170
+ return Connectivity.Unknown;
171
+ }
172
+ case 1: {
173
+ return Connectivity.None;
174
+ }
175
+ case 2: {
176
+ return Connectivity.Portal;
177
+ }
178
+ case 3: {
179
+ return Connectivity.Limited;
180
+ }
181
+ case 4: {
182
+ return Connectivity.Full;
183
+ }
184
+ }
185
+ };
186
+ const rootSchema = z.object({
187
+ Connectivity: variant(z
188
+ .union([
189
+ z.literal(0),
190
+ z.literal(1),
191
+ z.literal(2),
192
+ z.literal(3),
193
+ z.literal(4),
194
+ ])
195
+ .transform(connectivityOf)),
196
+ PrimaryConnection: variant(z.string()),
197
+ });
198
+ const activeSchema = z.object({
199
+ Id: variant(z.string()),
200
+ SpecificObject: variant(z.string()),
201
+ Type: variant(z.string()),
202
+ });
203
+ const accessPointSchema = z.object({
204
+ Ssid: variant(z.array(z.number())),
205
+ Strength: variant(z.number()),
206
+ });
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "dependencies": {
3
+ "@cprussin/option-result": "^2.0.1",
4
+ "@domicile-desktop/sdk": "0.0.0-alpha-a2a1a1a59e77",
5
+ "zod": "^4.5.4"
6
+ },
7
+ "description": "NetworkManager for a Domicile shell: connectivity and the primary connection, with its Wi-Fi network and signal, kept current.",
8
+ "devDependencies": {
9
+ "@cprussin/tsconfig": "^5.0.0",
10
+ "@types/bun": "^1.4.2",
11
+ "typescript": "^7.0.2"
12
+ },
13
+ "exports": {
14
+ "./network": {
15
+ "types": "./dist/network.d.ts",
16
+ "default": "./dist/network.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist/**"
21
+ ],
22
+ "license": "MIT",
23
+ "name": "@domicile-desktop/system-network",
24
+ "private": false,
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "directory": "packages/system-network",
30
+ "type": "git",
31
+ "url": "git+https://github.com/cprussin/domicile.git"
32
+ },
33
+ "scripts": {
34
+ "build": "tsc -p tsconfig.build.json",
35
+ "test:types": "tsc --noEmit",
36
+ "test:unit": "bun test"
37
+ },
38
+ "type": "module",
39
+ "version": "0.0.0-alpha-a2a1a1a59e77"
40
+ }