relapsesdk 0.1.0 → 0.2.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.
- package/README.md +0 -148
- package/dist/channel.js +0 -1
- package/dist/client.js +0 -1
- package/dist/configuration.d.ts +38 -0
- package/dist/configuration.js +251 -0
- package/dist/discovery.js +0 -1
- package/dist/errors.js +0 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -1
- package/dist/internal/json.js +0 -1
- package/dist/internal/operation.js +0 -1
- package/dist/internal/profile-name.d.ts +1 -0
- package/dist/internal/profile-name.js +4 -0
- package/dist/internal/transport.js +0 -1
- package/dist/internal/wait-for.js +0 -1
- package/dist/launcher.js +0 -1
- package/dist/main-channel.js +0 -1
- package/dist/profiles.d.ts +8 -10
- package/dist/profiles.js +45 -36
- package/examples/channel.mjs +0 -1
- package/examples/consumer.ts +0 -1
- package/examples/execute.mjs +0 -1
- package/package.json +5 -2
- package/src/channel.ts +0 -1
- package/src/client.ts +0 -1
- package/src/configuration.ts +273 -0
- package/src/discovery.ts +0 -1
- package/src/errors.ts +0 -1
- package/src/index.ts +5 -1
- package/src/internal/json.ts +0 -1
- package/src/internal/operation.ts +0 -1
- package/src/internal/profile-name.ts +5 -0
- package/src/internal/transport.ts +0 -1
- package/src/internal/wait-for.ts +0 -1
- package/src/launcher.ts +0 -1
- package/src/main-channel.ts +0 -1
- package/src/profiles.ts +49 -41
package/README.md
CHANGED
|
@@ -1,151 +1,3 @@
|
|
|
1
1
|
# relapsesdk
|
|
2
2
|
|
|
3
3
|
Node.js SDK for building Relapse tools and frontends.
|
|
4
|
-
|
|
5
|
-
Discover running Relapse clients, execute Luau scripts, exchange JSON messages
|
|
6
|
-
through named channels, and manage profiles from JavaScript or TypeScript.
|
|
7
|
-
|
|
8
|
-
## Installation
|
|
9
|
-
|
|
10
|
-
```sh
|
|
11
|
-
npm install relapsesdk
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
Requires Node.js 22 or newer. This is an ESM package with TypeScript declarations
|
|
15
|
-
included. It connects to a local Relapse installation on Linux through Unix
|
|
16
|
-
sockets; it does not run in a browser. In a desktop frontend, use it from the
|
|
17
|
-
Node.js process.
|
|
18
|
-
|
|
19
|
-
## Connect and execute a script
|
|
20
|
-
|
|
21
|
-
Start Relapse, then run this from an `.mjs` file or a project with
|
|
22
|
-
`"type": "module"` in its `package.json`:
|
|
23
|
-
|
|
24
|
-
```js
|
|
25
|
-
import { Client, listClients } from "relapsesdk";
|
|
26
|
-
|
|
27
|
-
const [info] = await listClients();
|
|
28
|
-
if (!info) throw new Error("No running Relapse clients found.");
|
|
29
|
-
|
|
30
|
-
const client = await Client.connect({ socketPath: info.socketPath });
|
|
31
|
-
|
|
32
|
-
try {
|
|
33
|
-
const main = client.mainChannel;
|
|
34
|
-
main.on("log", entry => console.log(`[${entry.level}] ${entry.text}`));
|
|
35
|
-
|
|
36
|
-
if (!main.ready) {
|
|
37
|
-
await main.waitFor("status", status => status.ready, { timeoutMs: 30_000 });
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
await main.execute('print("Hello from relapsesdk")', { name: "hello.luau" });
|
|
41
|
-
console.log("Script submitted.");
|
|
42
|
-
} finally {
|
|
43
|
-
client.close();
|
|
44
|
-
}
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
`execute()` resolves when Relapse acknowledges submission, before the script
|
|
48
|
-
necessarily finishes. Keep the connection open if you need later log messages.
|
|
49
|
-
Use `main.executeFile("./script.luau")` to submit a file.
|
|
50
|
-
|
|
51
|
-
## Named channels
|
|
52
|
-
|
|
53
|
-
Create a named channel in your Relapse script before connecting to it from Node.js:
|
|
54
|
-
|
|
55
|
-
```lua
|
|
56
|
-
local channel = RelapseChannel.new("my-tool")
|
|
57
|
-
getgenv().myToolChannel = channel
|
|
58
|
-
|
|
59
|
-
channel.Connected:Connect(function(connection)
|
|
60
|
-
connection.MessageReceived:Connect(function(message)
|
|
61
|
-
connection:Send(message)
|
|
62
|
-
end)
|
|
63
|
-
end)
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
With a connected `client`, send a JSON value and wait for the echoed reply:
|
|
67
|
-
|
|
68
|
-
```js
|
|
69
|
-
const channel = await client.openChannel("my-tool");
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
const [reply] = await Promise.all([
|
|
73
|
-
channel.waitFor("message", undefined, { timeoutMs: 5_000 }),
|
|
74
|
-
channel.send({ action: "hello" }),
|
|
75
|
-
]);
|
|
76
|
-
console.log(reply);
|
|
77
|
-
} finally {
|
|
78
|
-
channel.close();
|
|
79
|
-
}
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
Register listeners or waits before sending a request so an immediate reply is
|
|
83
|
-
observed. `send()` resolves after a local write; use a reply to confirm the peer
|
|
84
|
-
has handled it. Close the Luau channel with `getgenv().myToolChannel:Close()` when
|
|
85
|
-
finished.
|
|
86
|
-
|
|
87
|
-
## Profiles and launching
|
|
88
|
-
|
|
89
|
-
```js
|
|
90
|
-
import { launch, profiles } from "relapsesdk";
|
|
91
|
-
|
|
92
|
-
console.table(await profiles.list());
|
|
93
|
-
|
|
94
|
-
// Choose a new profile name; create() rejects an existing directory.
|
|
95
|
-
await profiles.create("my-tool");
|
|
96
|
-
await launch({ profile: "my-tool" });
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Profiles are stored under `~/.local/share/relapse/profiles` by default. The
|
|
100
|
-
launcher initializes a new profile's state on first use. `launch()` requires
|
|
101
|
-
`relapse-launcher` on `PATH`, or an explicit `launcherPath`; it resolves when the
|
|
102
|
-
process starts. Discover the client and wait for readiness before executing
|
|
103
|
-
scripts.
|
|
104
|
-
|
|
105
|
-
The profile helpers are also available through `import * as profiles from
|
|
106
|
-
"relapsesdk/profiles"`.
|
|
107
|
-
|
|
108
|
-
## API overview
|
|
109
|
-
|
|
110
|
-
| Export | Purpose |
|
|
111
|
-
| --- | --- |
|
|
112
|
-
| `listClients(options?)` | Find responsive clients owned by the current user. |
|
|
113
|
-
| `watchClients(options?)` | Yield an initial client list and changed snapshots as an async iterator. |
|
|
114
|
-
| `Client.connect({ socketPath, ...options })` | Connect to a client and manage its main and named channels. |
|
|
115
|
-
| `MainChannel.connect({ socketPath, ...options })` | Connect directly to the main channel for execution, status, and logs. |
|
|
116
|
-
| `Channel.connect({ socketPath, name, ...options })` | Connect directly to a named JSON channel. |
|
|
117
|
-
| `launch(options)` | Start a Relapse profile through the launcher. |
|
|
118
|
-
| `profiles` | List, create, find, and resolve paths for profiles. |
|
|
119
|
-
| `RelapseError` | SDK error with a machine-readable `code`. |
|
|
120
|
-
|
|
121
|
-
Connection, execution, and event-wait operations accept `timeoutMs` and an
|
|
122
|
-
`AbortSignal` through `signal`. A timeout of `0` disables the timer. Aborting or
|
|
123
|
-
timing out an execution request cannot undo an already submitted script.
|
|
124
|
-
|
|
125
|
-
Always close connections when finished. `client.close()` also closes channels
|
|
126
|
-
opened through that client.
|
|
127
|
-
|
|
128
|
-
## Development
|
|
129
|
-
|
|
130
|
-
```sh
|
|
131
|
-
git clone --branch typescript https://github.com/vpu00000/relapse-sdk.git
|
|
132
|
-
cd relapse-sdk
|
|
133
|
-
npm ci
|
|
134
|
-
npm run check
|
|
135
|
-
npm run build
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
With Relapse running, `npm run example` starts the interactive discovery,
|
|
139
|
-
execution, and channel example. Smaller examples are in
|
|
140
|
-
[`examples/execute.mjs`](examples/execute.mjs) and
|
|
141
|
-
[`examples/channel.mjs`](examples/channel.mjs).
|
|
142
|
-
|
|
143
|
-
The npm archive includes compiled JavaScript, type declarations, TypeScript
|
|
144
|
-
source, examples, and the build configuration.
|
|
145
|
-
|
|
146
|
-
## License
|
|
147
|
-
|
|
148
|
-
Copyright (C) 2026 vpu007.
|
|
149
|
-
|
|
150
|
-
Licensed under the GNU General Public License, version 3 or (at your option) any
|
|
151
|
-
later version (`GPL-3.0-or-later`). See [LICENSE](LICENSE) for the full terms.
|
package/dist/channel.js
CHANGED
package/dist/client.js
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export interface ConfigurationOptions {
|
|
2
|
+
homeDirectory?: string;
|
|
3
|
+
configHome?: string;
|
|
4
|
+
dataHome?: string;
|
|
5
|
+
}
|
|
6
|
+
export type ConfigurationObject = Record<string, unknown>;
|
|
7
|
+
export interface GlobalConfiguration extends ConfigurationObject {
|
|
8
|
+
defaultProfile?: string;
|
|
9
|
+
workspace?: string;
|
|
10
|
+
autoexec?: string;
|
|
11
|
+
autolaunch?: readonly string[] | null;
|
|
12
|
+
}
|
|
13
|
+
export interface ConfigurationOverrides extends ConfigurationObject {
|
|
14
|
+
workspace?: string;
|
|
15
|
+
autoexec?: string;
|
|
16
|
+
autolaunch?: readonly string[] | null;
|
|
17
|
+
}
|
|
18
|
+
export interface ProfileConfiguration extends ConfigurationObject {
|
|
19
|
+
dataDir?: string;
|
|
20
|
+
override?: ConfigurationOverrides;
|
|
21
|
+
}
|
|
22
|
+
export interface ResolvedConfiguration {
|
|
23
|
+
profile: string;
|
|
24
|
+
dataDir: string;
|
|
25
|
+
workspace: string;
|
|
26
|
+
autoexec: string;
|
|
27
|
+
autolaunch: string[] | null;
|
|
28
|
+
environment: Record<string, string>;
|
|
29
|
+
}
|
|
30
|
+
export declare function configDirectory(options?: ConfigurationOptions): string;
|
|
31
|
+
export declare function dataDirectory(options?: ConfigurationOptions): string;
|
|
32
|
+
export declare function globalPath(options?: ConfigurationOptions): string;
|
|
33
|
+
export declare function profilePath(name: string, options?: ConfigurationOptions): string;
|
|
34
|
+
export declare function readGlobal(options?: ConfigurationOptions): Promise<GlobalConfiguration>;
|
|
35
|
+
export declare function readProfile(name: string, options?: ConfigurationOptions): Promise<ProfileConfiguration>;
|
|
36
|
+
export declare function writeGlobal(configuration: GlobalConfiguration, options?: ConfigurationOptions): Promise<void>;
|
|
37
|
+
export declare function writeProfile(name: string, configuration: ProfileConfiguration, options?: ConfigurationOptions): Promise<void>;
|
|
38
|
+
export declare function resolve(requestedProfile?: string, options?: ConfigurationOptions): Promise<ResolvedConfiguration>;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
2
|
+
import { readFile, mkdir, rename, unlink, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
5
|
+
import { isValidName } from "./internal/profile-name.js";
|
|
6
|
+
import { isRecord } from "./internal/json.js";
|
|
7
|
+
export function configDirectory(options = {}) {
|
|
8
|
+
const home = options.homeDirectory ?? process.env.HOME ?? homedir();
|
|
9
|
+
if (!isAbsolute(home))
|
|
10
|
+
throw new TypeError("homeDirectory must be an absolute path.");
|
|
11
|
+
const configured = options.configHome ?? process.env.XDG_CONFIG_HOME;
|
|
12
|
+
const root = configured ? configured : join(home, ".config");
|
|
13
|
+
if (!isAbsolute(root))
|
|
14
|
+
throw new TypeError("configHome must be an absolute path.");
|
|
15
|
+
return join(root, "relapse");
|
|
16
|
+
}
|
|
17
|
+
export function dataDirectory(options = {}) {
|
|
18
|
+
const home = options.homeDirectory ?? process.env.HOME ?? homedir();
|
|
19
|
+
if (!isAbsolute(home))
|
|
20
|
+
throw new TypeError("homeDirectory must be an absolute path.");
|
|
21
|
+
const configured = options.dataHome ?? process.env.XDG_DATA_HOME;
|
|
22
|
+
const root = configured ? configured : join(home, ".local", "share");
|
|
23
|
+
if (!isAbsolute(root))
|
|
24
|
+
throw new TypeError("dataHome must be an absolute path.");
|
|
25
|
+
return join(root, "relapse");
|
|
26
|
+
}
|
|
27
|
+
export function globalPath(options = {}) {
|
|
28
|
+
return join(configDirectory(options), "config.json");
|
|
29
|
+
}
|
|
30
|
+
export function profilePath(name, options = {}) {
|
|
31
|
+
if (!isValidName(name))
|
|
32
|
+
throw new TypeError("Invalid profile name.");
|
|
33
|
+
return join(configDirectory(options), "profiles", name, "config.json");
|
|
34
|
+
}
|
|
35
|
+
function stripComments(text) {
|
|
36
|
+
let result = "";
|
|
37
|
+
let quoted = false;
|
|
38
|
+
let escaped = false;
|
|
39
|
+
let lineComment = false;
|
|
40
|
+
let blockComment = false;
|
|
41
|
+
for (let index = 0; index < text.length; index++) {
|
|
42
|
+
const character = text[index];
|
|
43
|
+
const next = text[index + 1];
|
|
44
|
+
if (lineComment) {
|
|
45
|
+
if (character === "\n") {
|
|
46
|
+
result += character;
|
|
47
|
+
lineComment = false;
|
|
48
|
+
}
|
|
49
|
+
else
|
|
50
|
+
result += " ";
|
|
51
|
+
}
|
|
52
|
+
else if (blockComment) {
|
|
53
|
+
if (character === "*" && next === "/") {
|
|
54
|
+
result += " ";
|
|
55
|
+
index++;
|
|
56
|
+
blockComment = false;
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
result += character === "\n" ? "\n" : " ";
|
|
60
|
+
}
|
|
61
|
+
else if (quoted) {
|
|
62
|
+
result += character;
|
|
63
|
+
if (escaped)
|
|
64
|
+
escaped = false;
|
|
65
|
+
else if (character === "\\")
|
|
66
|
+
escaped = true;
|
|
67
|
+
else if (character === '"')
|
|
68
|
+
quoted = false;
|
|
69
|
+
}
|
|
70
|
+
else if (character === '"') {
|
|
71
|
+
result += character;
|
|
72
|
+
quoted = true;
|
|
73
|
+
}
|
|
74
|
+
else if (character === "/" && next === "/") {
|
|
75
|
+
result += " ";
|
|
76
|
+
index++;
|
|
77
|
+
lineComment = true;
|
|
78
|
+
}
|
|
79
|
+
else if (character === "/" && next === "*") {
|
|
80
|
+
result += " ";
|
|
81
|
+
index++;
|
|
82
|
+
blockComment = true;
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
result += character;
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
async function readObject(path) {
|
|
90
|
+
const text = await readFile(path, "utf8").catch((error) => {
|
|
91
|
+
if (error.code === "ENOENT")
|
|
92
|
+
return undefined;
|
|
93
|
+
throw error;
|
|
94
|
+
});
|
|
95
|
+
if (text === undefined)
|
|
96
|
+
return {};
|
|
97
|
+
if (Buffer.byteLength(text) > 1024 * 1024 || text.includes("\0")) {
|
|
98
|
+
throw new Error("Configuration must be UTF-8 JSON smaller than 1 MiB.");
|
|
99
|
+
}
|
|
100
|
+
const value = JSON.parse(stripComments(text));
|
|
101
|
+
if (!isRecord(value))
|
|
102
|
+
throw new TypeError("Relapse configuration must be a JSON object.");
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
async function writeObject(path, value) {
|
|
106
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
107
|
+
const temporary = `${path}.tmp.${process.pid}.${Date.now()}`;
|
|
108
|
+
try {
|
|
109
|
+
await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, {
|
|
110
|
+
encoding: "utf8", flag: "wx", mode: 0o600,
|
|
111
|
+
});
|
|
112
|
+
await rename(temporary, path);
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
await unlink(temporary).catch((error) => {
|
|
116
|
+
if (error.code !== "ENOENT")
|
|
117
|
+
throw error;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function validatePath(value, key) {
|
|
122
|
+
if (value !== undefined && (typeof value !== "string" || !value || !isAbsolute(value))) {
|
|
123
|
+
throw new TypeError(`${key} must be an absolute path.`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function validateAutolaunch(value) {
|
|
127
|
+
if (value === undefined || value === null)
|
|
128
|
+
return;
|
|
129
|
+
if (!Array.isArray(value) || !value.length || value.some(argument => typeof argument !== "string") || !value[0]) {
|
|
130
|
+
throw new TypeError("autolaunch must be null or a nonempty string array.");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function validateGlobal(configuration) {
|
|
134
|
+
if (configuration.defaultProfile !== undefined && !isValidName(configuration.defaultProfile)) {
|
|
135
|
+
throw new TypeError("defaultProfile must be a valid profile name.");
|
|
136
|
+
}
|
|
137
|
+
validatePath(configuration.workspace, "workspace");
|
|
138
|
+
validatePath(configuration.autoexec, "autoexec");
|
|
139
|
+
validateAutolaunch(configuration.autolaunch);
|
|
140
|
+
}
|
|
141
|
+
function validateProfile(configuration) {
|
|
142
|
+
validatePath(configuration.dataDir, "dataDir");
|
|
143
|
+
if (configuration.override !== undefined) {
|
|
144
|
+
if (!isRecord(configuration.override))
|
|
145
|
+
throw new TypeError("Profile override must be an object.");
|
|
146
|
+
validatePath(configuration.override.workspace, "override.workspace");
|
|
147
|
+
validatePath(configuration.override.autoexec, "override.autoexec");
|
|
148
|
+
validateAutolaunch(configuration.override.autolaunch);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export async function readGlobal(options = {}) {
|
|
152
|
+
return await readObject(globalPath(options));
|
|
153
|
+
}
|
|
154
|
+
export async function readProfile(name, options = {}) {
|
|
155
|
+
return await readObject(profilePath(name, options));
|
|
156
|
+
}
|
|
157
|
+
export async function writeGlobal(configuration, options = {}) {
|
|
158
|
+
if (!isRecord(configuration))
|
|
159
|
+
throw new TypeError("Global configuration must be an object.");
|
|
160
|
+
validateGlobal(configuration);
|
|
161
|
+
await writeObject(globalPath(options), configuration);
|
|
162
|
+
}
|
|
163
|
+
export async function writeProfile(name, configuration, options = {}) {
|
|
164
|
+
if (!isValidName(name))
|
|
165
|
+
throw new TypeError("Invalid profile name.");
|
|
166
|
+
if (!isRecord(configuration))
|
|
167
|
+
throw new TypeError("Profile configuration must be an object.");
|
|
168
|
+
validateProfile(configuration);
|
|
169
|
+
await writeObject(profilePath(name, options), configuration);
|
|
170
|
+
}
|
|
171
|
+
function merge(target, overrides) {
|
|
172
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
173
|
+
const current = target[key];
|
|
174
|
+
target[key] = isRecord(current) && isRecord(value)
|
|
175
|
+
? merge({ ...current }, value)
|
|
176
|
+
: value;
|
|
177
|
+
}
|
|
178
|
+
return target;
|
|
179
|
+
}
|
|
180
|
+
function resolvedPath(object, key, fallback) {
|
|
181
|
+
const value = object[key];
|
|
182
|
+
if (value === undefined)
|
|
183
|
+
return fallback;
|
|
184
|
+
validatePath(value, key);
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
function environmentName(path) {
|
|
188
|
+
let name = "RELAPSE_";
|
|
189
|
+
for (const character of path) {
|
|
190
|
+
if (character === "." || character === "-")
|
|
191
|
+
name += "_";
|
|
192
|
+
else if (/[a-zA-Z0-9_]/u.test(character))
|
|
193
|
+
name += character.toUpperCase();
|
|
194
|
+
else
|
|
195
|
+
throw new TypeError(`Executor configuration key has unsupported characters: ${path}`);
|
|
196
|
+
}
|
|
197
|
+
return name;
|
|
198
|
+
}
|
|
199
|
+
function flatten(object, prefix, environment) {
|
|
200
|
+
for (const [key, value] of Object.entries(object)) {
|
|
201
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
202
|
+
if (isRecord(value)) {
|
|
203
|
+
flatten(value, path, environment);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
let encoded;
|
|
207
|
+
if (typeof value === "boolean")
|
|
208
|
+
encoded = value ? "1" : "0";
|
|
209
|
+
else if (typeof value === "string")
|
|
210
|
+
encoded = value;
|
|
211
|
+
else if (typeof value === "number" && Number.isFinite(value))
|
|
212
|
+
encoded = JSON.stringify(value);
|
|
213
|
+
else
|
|
214
|
+
throw new TypeError(`Executor configuration values must be scalar: ${path}`);
|
|
215
|
+
environment[environmentName(path)] = encoded;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
export async function resolve(requestedProfile, options = {}) {
|
|
219
|
+
const [global, profileConfiguration] = await Promise.all([readGlobal(options),
|
|
220
|
+
requestedProfile ? readProfile(requestedProfile, options) : undefined]);
|
|
221
|
+
let profile = requestedProfile;
|
|
222
|
+
if (!profile) {
|
|
223
|
+
const selected = global.defaultProfile === undefined ? "Default" : global.defaultProfile;
|
|
224
|
+
if (typeof selected !== "string")
|
|
225
|
+
throw new TypeError("defaultProfile must be a string.");
|
|
226
|
+
profile = selected;
|
|
227
|
+
}
|
|
228
|
+
if (!isValidName(profile))
|
|
229
|
+
throw new TypeError("Invalid profile name.");
|
|
230
|
+
const selectedConfiguration = requestedProfile ? profileConfiguration : await readProfile(profile, options);
|
|
231
|
+
const overrides = selectedConfiguration.override === undefined ? {} : selectedConfiguration.override;
|
|
232
|
+
if (!isRecord(overrides))
|
|
233
|
+
throw new TypeError("Profile override must be an object.");
|
|
234
|
+
const defaultDataDir = join(dataDirectory(options), "profiles", profile);
|
|
235
|
+
const dataDir = selectedConfiguration.dataDir === undefined ? defaultDataDir : selectedConfiguration.dataDir;
|
|
236
|
+
validatePath(dataDir, "dataDir");
|
|
237
|
+
const merged = merge({ ...global }, overrides);
|
|
238
|
+
delete merged.defaultProfile;
|
|
239
|
+
const workspace = resolvedPath(merged, "workspace", join(dataDirectory(options), "workspace"));
|
|
240
|
+
const autoexec = resolvedPath(merged, "autoexec", join(configDirectory(options), "autoexec"));
|
|
241
|
+
const command = merged.autolaunch;
|
|
242
|
+
validateAutolaunch(command);
|
|
243
|
+
const autolaunch = command ? Array.from(command) : null;
|
|
244
|
+
delete merged.workspace;
|
|
245
|
+
delete merged.autoexec;
|
|
246
|
+
delete merged.dataDir;
|
|
247
|
+
delete merged.autolaunch;
|
|
248
|
+
const environment = {};
|
|
249
|
+
flatten(merged, "", environment);
|
|
250
|
+
return { profile, dataDir: dataDir, workspace, autoexec, autolaunch, environment };
|
|
251
|
+
}
|
package/dist/discovery.js
CHANGED
package/dist/errors.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ export { MainChannel, type MainChannelOptions, type MainChannelEvents, type Main
|
|
|
5
5
|
export { listClients, watchClients, type DiscoveryOptions, type WatchOptions } from "./discovery.js";
|
|
6
6
|
export { launch, type LaunchOptions } from "./launcher.js";
|
|
7
7
|
export { RelapseError, type ErrorCode } from "./errors.js";
|
|
8
|
+
export * as configuration from "./configuration.js";
|
|
9
|
+
export type { ConfigurationOptions, ConfigurationObject, GlobalConfiguration, ConfigurationOverrides, ProfileConfiguration, ResolvedConfiguration, } from "./configuration.js";
|
|
8
10
|
export * as profiles from "./profiles.js";
|
|
9
11
|
export type { ProfileInfo, ProfileOptions, ProfilePaths } from "./profiles.js";
|
|
10
12
|
export type { JsonValue } from "./internal/json.js";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// Copyright (C) 2026 vpu007
|
|
2
1
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
3
2
|
/// <reference types="node" preserve="true" />
|
|
4
3
|
export { Channel } from "./channel.js";
|
|
@@ -7,4 +6,5 @@ export { MainChannel, } from "./main-channel.js";
|
|
|
7
6
|
export { listClients, watchClients } from "./discovery.js";
|
|
8
7
|
export { launch } from "./launcher.js";
|
|
9
8
|
export { RelapseError } from "./errors.js";
|
|
9
|
+
export * as configuration from "./configuration.js";
|
|
10
10
|
export * as profiles from "./profiles.js";
|
package/dist/internal/json.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isValidName(name: string): boolean;
|
package/dist/launcher.js
CHANGED
package/dist/main-channel.js
CHANGED
package/dist/profiles.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { type ConfigurationOptions } from "./configuration.js";
|
|
2
|
+
export { isValidName } from "./internal/profile-name.js";
|
|
3
|
+
export type ProfileOptions = ConfigurationOptions;
|
|
4
4
|
export interface ProfileInfo {
|
|
5
5
|
name: string;
|
|
6
|
-
|
|
6
|
+
dataDir: string;
|
|
7
|
+
configFile: string;
|
|
7
8
|
}
|
|
8
9
|
export interface ProfilePaths {
|
|
9
|
-
|
|
10
|
+
dataDir: string;
|
|
10
11
|
state: string;
|
|
11
12
|
cookies: string;
|
|
13
|
+
configFile: string;
|
|
12
14
|
}
|
|
13
|
-
export declare function
|
|
14
|
-
export declare function directory(options?: ProfileOptions): string;
|
|
15
|
-
export declare function path(name: string, options?: ProfileOptions): string;
|
|
16
|
-
export declare function paths(name: string, options?: ProfileOptions): ProfilePaths;
|
|
15
|
+
export declare function paths(name: string, options?: ProfileOptions): Promise<ProfilePaths>;
|
|
17
16
|
export declare function get(name: string, options?: ProfileOptions): Promise<ProfileInfo>;
|
|
18
17
|
export declare function list(options?: ProfileOptions): Promise<ProfileInfo[]>;
|
|
19
|
-
/** Creates a fresh profile directory. The launcher initializes its state on first use. */
|
|
20
18
|
export declare function create(name: string, options?: ProfileOptions): Promise<ProfileInfo>;
|
package/dist/profiles.js
CHANGED
|
@@ -1,56 +1,65 @@
|
|
|
1
|
-
// Copyright (C) 2026 vpu007
|
|
2
1
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
3
2
|
import { lstat, mkdir, readdir } from "node:fs/promises";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { configDirectory, dataDirectory, profilePath as configurationPath, resolve as resolveConfiguration, writeProfile, } from "./configuration.js";
|
|
5
|
+
import { isValidName } from "./internal/profile-name.js";
|
|
6
|
+
export { isValidName } from "./internal/profile-name.js";
|
|
7
|
+
function info(name, dataDir, options) {
|
|
8
|
+
return { name, dataDir, configFile: configurationPath(name, options) };
|
|
8
9
|
}
|
|
9
|
-
export function
|
|
10
|
-
const
|
|
11
|
-
if (!isAbsolute(home))
|
|
12
|
-
throw new TypeError("homeDirectory must be an absolute path.");
|
|
13
|
-
return join(home, ".local", "share", "relapse", "profiles");
|
|
14
|
-
}
|
|
15
|
-
export function path(name, options = {}) {
|
|
16
|
-
if (!isValidName(name)) {
|
|
17
|
-
throw new TypeError("Profile names must contain 1–64 letters, digits, dots, underscores or hyphens, excluding . and ..");
|
|
18
|
-
}
|
|
19
|
-
return join(directory(options), name);
|
|
20
|
-
}
|
|
21
|
-
export function paths(name, options = {}) {
|
|
22
|
-
const profile = path(name, options);
|
|
10
|
+
export async function paths(name, options = {}) {
|
|
11
|
+
const configuration = await resolveConfiguration(name, options);
|
|
23
12
|
return {
|
|
24
|
-
|
|
25
|
-
state: join(
|
|
26
|
-
cookies: join(
|
|
13
|
+
dataDir: configuration.dataDir,
|
|
14
|
+
state: join(configuration.dataDir, "state"),
|
|
15
|
+
cookies: join(configuration.dataDir, "state", "app", "data", "sober", "cookies"),
|
|
16
|
+
configFile: configurationPath(name, options),
|
|
27
17
|
};
|
|
28
18
|
}
|
|
29
19
|
export async function get(name, options = {}) {
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
if (!
|
|
33
|
-
throw new Error("Profile must be a
|
|
34
|
-
|
|
20
|
+
const configFile = configurationPath(name, options);
|
|
21
|
+
const configured = await lstat(configFile);
|
|
22
|
+
if (!configured.isFile())
|
|
23
|
+
throw new Error("Profile configuration must be a regular file.");
|
|
24
|
+
const configuration = await resolveConfiguration(name, options);
|
|
25
|
+
return info(name, configuration.dataDir, options);
|
|
35
26
|
}
|
|
36
27
|
export async function list(options = {}) {
|
|
37
|
-
const root =
|
|
28
|
+
const root = join(configDirectory(options), "profiles");
|
|
38
29
|
const entries = await readdir(root, { withFileTypes: true }).catch((error) => {
|
|
39
30
|
if (error.code === "ENOENT")
|
|
40
31
|
return [];
|
|
41
32
|
throw error;
|
|
42
33
|
});
|
|
43
|
-
|
|
44
|
-
.map(entry =>
|
|
45
|
-
|
|
34
|
+
const names = entries.filter(entry => entry.isDirectory() && isValidName(entry.name))
|
|
35
|
+
.map(entry => entry.name).sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
36
|
+
const result = [];
|
|
37
|
+
for (const name of names) {
|
|
38
|
+
try {
|
|
39
|
+
result.push(await get(name, options));
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error.code !== "ENOENT")
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
46
47
|
}
|
|
47
|
-
/** Creates a fresh profile directory. The launcher initializes its state on first use. */
|
|
48
48
|
export async function create(name, options = {}) {
|
|
49
|
-
const
|
|
50
|
-
const root =
|
|
49
|
+
const configFile = configurationPath(name, options);
|
|
50
|
+
const root = join(dataDirectory(options), "profiles");
|
|
51
|
+
const dataDir = join(root, name);
|
|
52
|
+
const configured = await lstat(configFile).catch((error) => {
|
|
53
|
+
if (error.code === "ENOENT")
|
|
54
|
+
return undefined;
|
|
55
|
+
throw error;
|
|
56
|
+
});
|
|
57
|
+
if (configured)
|
|
58
|
+
throw new Error("Profile already exists.");
|
|
51
59
|
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
52
60
|
if (!(await lstat(root)).isDirectory())
|
|
53
61
|
throw new Error("Profiles directory must not be a symlink.");
|
|
54
|
-
await mkdir(
|
|
55
|
-
|
|
62
|
+
await mkdir(dataDir, { mode: 0o700 });
|
|
63
|
+
await writeProfile(name, { dataDir, override: {} }, options);
|
|
64
|
+
return info(name, dataDir, options);
|
|
56
65
|
}
|
package/examples/channel.mjs
CHANGED
package/examples/consumer.ts
CHANGED
package/examples/execute.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "relapsesdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Node.js SDK for building Relapse tools and frontends.",
|
|
5
5
|
"license": "GPL-3.0-or-later",
|
|
6
|
-
"author": "vpu007",
|
|
7
6
|
"repository": {
|
|
8
7
|
"type": "git",
|
|
9
8
|
"url": "git+https://github.com/vpu00000/relapse-sdk.git"
|
|
@@ -30,6 +29,10 @@
|
|
|
30
29
|
"./profiles": {
|
|
31
30
|
"types": "./dist/profiles.d.ts",
|
|
32
31
|
"default": "./dist/profiles.js"
|
|
32
|
+
},
|
|
33
|
+
"./configuration": {
|
|
34
|
+
"types": "./dist/configuration.d.ts",
|
|
35
|
+
"default": "./dist/configuration.js"
|
|
33
36
|
}
|
|
34
37
|
},
|
|
35
38
|
"files": [
|
package/src/channel.ts
CHANGED
package/src/client.ts
CHANGED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
2
|
+
|
|
3
|
+
import { readFile, mkdir, rename, unlink, writeFile } from "node:fs/promises";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
6
|
+
import { isValidName } from "./internal/profile-name.js";
|
|
7
|
+
import { isRecord } from "./internal/json.js";
|
|
8
|
+
|
|
9
|
+
export interface ConfigurationOptions {
|
|
10
|
+
homeDirectory?: string;
|
|
11
|
+
configHome?: string;
|
|
12
|
+
dataHome?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type ConfigurationObject = Record<string, unknown>;
|
|
16
|
+
|
|
17
|
+
export interface GlobalConfiguration extends ConfigurationObject {
|
|
18
|
+
defaultProfile?: string;
|
|
19
|
+
workspace?: string;
|
|
20
|
+
autoexec?: string;
|
|
21
|
+
autolaunch?: readonly string[] | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface ConfigurationOverrides extends ConfigurationObject {
|
|
25
|
+
workspace?: string;
|
|
26
|
+
autoexec?: string;
|
|
27
|
+
autolaunch?: readonly string[] | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ProfileConfiguration extends ConfigurationObject {
|
|
31
|
+
dataDir?: string;
|
|
32
|
+
override?: ConfigurationOverrides;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface ResolvedConfiguration {
|
|
36
|
+
profile: string;
|
|
37
|
+
dataDir: string;
|
|
38
|
+
workspace: string;
|
|
39
|
+
autoexec: string;
|
|
40
|
+
autolaunch: string[] | null;
|
|
41
|
+
environment: Record<string, string>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function configDirectory(options: ConfigurationOptions = {}): string {
|
|
45
|
+
const home = options.homeDirectory ?? process.env.HOME ?? homedir();
|
|
46
|
+
if (!isAbsolute(home)) throw new TypeError("homeDirectory must be an absolute path.");
|
|
47
|
+
const configured = options.configHome ?? process.env.XDG_CONFIG_HOME;
|
|
48
|
+
const root = configured ? configured : join(home, ".config");
|
|
49
|
+
if (!isAbsolute(root)) throw new TypeError("configHome must be an absolute path.");
|
|
50
|
+
return join(root, "relapse");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function dataDirectory(options: ConfigurationOptions = {}): string {
|
|
54
|
+
const home = options.homeDirectory ?? process.env.HOME ?? homedir();
|
|
55
|
+
if (!isAbsolute(home)) throw new TypeError("homeDirectory must be an absolute path.");
|
|
56
|
+
const configured = options.dataHome ?? process.env.XDG_DATA_HOME;
|
|
57
|
+
const root = configured ? configured : join(home, ".local", "share");
|
|
58
|
+
if (!isAbsolute(root)) throw new TypeError("dataHome must be an absolute path.");
|
|
59
|
+
return join(root, "relapse");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function globalPath(options: ConfigurationOptions = {}): string {
|
|
63
|
+
return join(configDirectory(options), "config.json");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function profilePath(name: string, options: ConfigurationOptions = {}): string {
|
|
67
|
+
if (!isValidName(name)) throw new TypeError("Invalid profile name.");
|
|
68
|
+
return join(configDirectory(options), "profiles", name, "config.json");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function stripComments(text: string): string {
|
|
72
|
+
let result = "";
|
|
73
|
+
let quoted = false;
|
|
74
|
+
let escaped = false;
|
|
75
|
+
let lineComment = false;
|
|
76
|
+
let blockComment = false;
|
|
77
|
+
for (let index = 0; index < text.length; index++) {
|
|
78
|
+
const character = text[index]!;
|
|
79
|
+
const next = text[index + 1];
|
|
80
|
+
if (lineComment) {
|
|
81
|
+
if (character === "\n") {
|
|
82
|
+
result += character;
|
|
83
|
+
lineComment = false;
|
|
84
|
+
} else result += " ";
|
|
85
|
+
} else if (blockComment) {
|
|
86
|
+
if (character === "*" && next === "/") {
|
|
87
|
+
result += " ";
|
|
88
|
+
index++;
|
|
89
|
+
blockComment = false;
|
|
90
|
+
} else result += character === "\n" ? "\n" : " ";
|
|
91
|
+
} else if (quoted) {
|
|
92
|
+
result += character;
|
|
93
|
+
if (escaped) escaped = false;
|
|
94
|
+
else if (character === "\\") escaped = true;
|
|
95
|
+
else if (character === '"') quoted = false;
|
|
96
|
+
} else if (character === '"') {
|
|
97
|
+
result += character;
|
|
98
|
+
quoted = true;
|
|
99
|
+
} else if (character === "/" && next === "/") {
|
|
100
|
+
result += " ";
|
|
101
|
+
index++;
|
|
102
|
+
lineComment = true;
|
|
103
|
+
} else if (character === "/" && next === "*") {
|
|
104
|
+
result += " ";
|
|
105
|
+
index++;
|
|
106
|
+
blockComment = true;
|
|
107
|
+
} else result += character;
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function readObject(path: string): Promise<ConfigurationObject> {
|
|
113
|
+
const text = await readFile(path, "utf8").catch((error: NodeJS.ErrnoException) => {
|
|
114
|
+
if (error.code === "ENOENT") return undefined;
|
|
115
|
+
throw error;
|
|
116
|
+
});
|
|
117
|
+
if (text === undefined) return {};
|
|
118
|
+
if (Buffer.byteLength(text) > 1024 * 1024 || text.includes("\0")) {
|
|
119
|
+
throw new Error("Configuration must be UTF-8 JSON smaller than 1 MiB.");
|
|
120
|
+
}
|
|
121
|
+
const value: unknown = JSON.parse(stripComments(text));
|
|
122
|
+
if (!isRecord(value)) throw new TypeError("Relapse configuration must be a JSON object.");
|
|
123
|
+
return value as ConfigurationObject;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function writeObject(path: string, value: ConfigurationObject): Promise<void> {
|
|
127
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
128
|
+
const temporary = `${path}.tmp.${process.pid}.${Date.now()}`;
|
|
129
|
+
try {
|
|
130
|
+
await writeFile(temporary, `${JSON.stringify(value, null, 2)}\n`, {
|
|
131
|
+
encoding: "utf8", flag: "wx", mode: 0o600,
|
|
132
|
+
});
|
|
133
|
+
await rename(temporary, path);
|
|
134
|
+
} finally {
|
|
135
|
+
await unlink(temporary).catch((error: NodeJS.ErrnoException) => {
|
|
136
|
+
if (error.code !== "ENOENT") throw error;
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function validatePath(value: unknown, key: string): void {
|
|
142
|
+
if (value !== undefined && (typeof value !== "string" || !value || !isAbsolute(value))) {
|
|
143
|
+
throw new TypeError(`${key} must be an absolute path.`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function validateAutolaunch(value: unknown): asserts value is readonly string[] | null | undefined {
|
|
148
|
+
if (value === undefined || value === null) return;
|
|
149
|
+
if (!Array.isArray(value) || !value.length || value.some(argument => typeof argument !== "string") || !value[0]) {
|
|
150
|
+
throw new TypeError("autolaunch must be null or a nonempty string array.");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function validateGlobal(configuration: GlobalConfiguration): void {
|
|
155
|
+
if (configuration.defaultProfile !== undefined && !isValidName(configuration.defaultProfile)) {
|
|
156
|
+
throw new TypeError("defaultProfile must be a valid profile name.");
|
|
157
|
+
}
|
|
158
|
+
validatePath(configuration.workspace, "workspace");
|
|
159
|
+
validatePath(configuration.autoexec, "autoexec");
|
|
160
|
+
validateAutolaunch(configuration.autolaunch);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function validateProfile(configuration: ProfileConfiguration): void {
|
|
164
|
+
validatePath(configuration.dataDir, "dataDir");
|
|
165
|
+
if (configuration.override !== undefined) {
|
|
166
|
+
if (!isRecord(configuration.override)) throw new TypeError("Profile override must be an object.");
|
|
167
|
+
validatePath(configuration.override.workspace, "override.workspace");
|
|
168
|
+
validatePath(configuration.override.autoexec, "override.autoexec");
|
|
169
|
+
validateAutolaunch(configuration.override.autolaunch);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function readGlobal(options: ConfigurationOptions = {}): Promise<GlobalConfiguration> {
|
|
174
|
+
return await readObject(globalPath(options)) as GlobalConfiguration;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export async function readProfile(name: string, options: ConfigurationOptions = {}): Promise<ProfileConfiguration> {
|
|
178
|
+
return await readObject(profilePath(name, options)) as ProfileConfiguration;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export async function writeGlobal(
|
|
182
|
+
configuration: GlobalConfiguration, options: ConfigurationOptions = {},
|
|
183
|
+
): Promise<void> {
|
|
184
|
+
if (!isRecord(configuration)) throw new TypeError("Global configuration must be an object.");
|
|
185
|
+
validateGlobal(configuration);
|
|
186
|
+
await writeObject(globalPath(options), configuration);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function writeProfile(
|
|
190
|
+
name: string, configuration: ProfileConfiguration, options: ConfigurationOptions = {},
|
|
191
|
+
): Promise<void> {
|
|
192
|
+
if (!isValidName(name)) throw new TypeError("Invalid profile name.");
|
|
193
|
+
if (!isRecord(configuration)) throw new TypeError("Profile configuration must be an object.");
|
|
194
|
+
validateProfile(configuration);
|
|
195
|
+
await writeObject(profilePath(name, options), configuration);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function merge(target: ConfigurationObject, overrides: ConfigurationOverrides): ConfigurationObject {
|
|
199
|
+
for (const [key, value] of Object.entries(overrides)) {
|
|
200
|
+
const current = target[key];
|
|
201
|
+
target[key] = isRecord(current) && isRecord(value)
|
|
202
|
+
? merge({ ...current }, value as ConfigurationOverrides)
|
|
203
|
+
: value;
|
|
204
|
+
}
|
|
205
|
+
return target;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function resolvedPath(object: ConfigurationObject, key: string, fallback: string): string {
|
|
209
|
+
const value = object[key];
|
|
210
|
+
if (value === undefined) return fallback;
|
|
211
|
+
validatePath(value, key);
|
|
212
|
+
return value as string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function environmentName(path: string): string {
|
|
216
|
+
let name = "RELAPSE_";
|
|
217
|
+
for (const character of path) {
|
|
218
|
+
if (character === "." || character === "-") name += "_";
|
|
219
|
+
else if (/[a-zA-Z0-9_]/u.test(character)) name += character.toUpperCase();
|
|
220
|
+
else throw new TypeError(`Executor configuration key has unsupported characters: ${path}`);
|
|
221
|
+
}
|
|
222
|
+
return name;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function flatten(object: ConfigurationObject, prefix: string, environment: Record<string, string>): void {
|
|
226
|
+
for (const [key, value] of Object.entries(object)) {
|
|
227
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
228
|
+
if (isRecord(value)) {
|
|
229
|
+
flatten(value as ConfigurationObject, path, environment);
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
let encoded: string;
|
|
233
|
+
if (typeof value === "boolean") encoded = value ? "1" : "0";
|
|
234
|
+
else if (typeof value === "string") encoded = value;
|
|
235
|
+
else if (typeof value === "number" && Number.isFinite(value)) encoded = JSON.stringify(value);
|
|
236
|
+
else throw new TypeError(`Executor configuration values must be scalar: ${path}`);
|
|
237
|
+
environment[environmentName(path)] = encoded;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export async function resolve(
|
|
242
|
+
requestedProfile?: string, options: ConfigurationOptions = {},
|
|
243
|
+
): Promise<ResolvedConfiguration> {
|
|
244
|
+
const [global, profileConfiguration] = await Promise.all([readGlobal(options),
|
|
245
|
+
requestedProfile ? readProfile(requestedProfile, options) : undefined]);
|
|
246
|
+
let profile = requestedProfile;
|
|
247
|
+
if (!profile) {
|
|
248
|
+
const selected = global.defaultProfile === undefined ? "Default" : global.defaultProfile;
|
|
249
|
+
if (typeof selected !== "string") throw new TypeError("defaultProfile must be a string.");
|
|
250
|
+
profile = selected;
|
|
251
|
+
}
|
|
252
|
+
if (!isValidName(profile)) throw new TypeError("Invalid profile name.");
|
|
253
|
+
const selectedConfiguration = requestedProfile ? profileConfiguration! : await readProfile(profile, options);
|
|
254
|
+
const overrides = selectedConfiguration.override === undefined ? {} : selectedConfiguration.override;
|
|
255
|
+
if (!isRecord(overrides)) throw new TypeError("Profile override must be an object.");
|
|
256
|
+
const defaultDataDir = join(dataDirectory(options), "profiles", profile);
|
|
257
|
+
const dataDir = selectedConfiguration.dataDir === undefined ? defaultDataDir : selectedConfiguration.dataDir;
|
|
258
|
+
validatePath(dataDir, "dataDir");
|
|
259
|
+
const merged = merge({ ...global }, overrides);
|
|
260
|
+
delete merged.defaultProfile;
|
|
261
|
+
const workspace = resolvedPath(merged, "workspace", join(dataDirectory(options), "workspace"));
|
|
262
|
+
const autoexec = resolvedPath(merged, "autoexec", join(configDirectory(options), "autoexec"));
|
|
263
|
+
const command = merged.autolaunch;
|
|
264
|
+
validateAutolaunch(command);
|
|
265
|
+
const autolaunch = command ? Array.from(command) : null;
|
|
266
|
+
delete merged.workspace;
|
|
267
|
+
delete merged.autoexec;
|
|
268
|
+
delete merged.dataDir;
|
|
269
|
+
delete merged.autolaunch;
|
|
270
|
+
const environment: Record<string, string> = {};
|
|
271
|
+
flatten(merged, "", environment);
|
|
272
|
+
return { profile, dataDir: dataDir as string, workspace, autoexec, autolaunch, environment };
|
|
273
|
+
}
|
package/src/discovery.ts
CHANGED
package/src/errors.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
// Copyright (C) 2026 vpu007
|
|
2
1
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
3
2
|
|
|
4
3
|
/// <reference types="node" preserve="true" />
|
|
@@ -15,6 +14,11 @@ export {
|
|
|
15
14
|
export { listClients, watchClients, type DiscoveryOptions, type WatchOptions } from "./discovery.js";
|
|
16
15
|
export { launch, type LaunchOptions } from "./launcher.js";
|
|
17
16
|
export { RelapseError, type ErrorCode } from "./errors.js";
|
|
17
|
+
export * as configuration from "./configuration.js";
|
|
18
|
+
export type {
|
|
19
|
+
ConfigurationOptions, ConfigurationObject, GlobalConfiguration, ConfigurationOverrides,
|
|
20
|
+
ProfileConfiguration, ResolvedConfiguration,
|
|
21
|
+
} from "./configuration.js";
|
|
18
22
|
export * as profiles from "./profiles.js";
|
|
19
23
|
export type { ProfileInfo, ProfileOptions, ProfilePaths } from "./profiles.js";
|
|
20
24
|
export type { JsonValue } from "./internal/json.js";
|
package/src/internal/json.ts
CHANGED
package/src/internal/wait-for.ts
CHANGED
package/src/launcher.ts
CHANGED
package/src/main-channel.ts
CHANGED
package/src/profiles.ts
CHANGED
|
@@ -1,75 +1,83 @@
|
|
|
1
|
-
// Copyright (C) 2026 vpu007
|
|
2
1
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
3
2
|
|
|
4
3
|
import { lstat, mkdir, readdir } from "node:fs/promises";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
configDirectory, dataDirectory, profilePath as configurationPath,
|
|
7
|
+
resolve as resolveConfiguration, writeProfile, type ConfigurationOptions,
|
|
8
|
+
} from "./configuration.js";
|
|
9
|
+
import { isValidName } from "./internal/profile-name.js";
|
|
7
10
|
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
export { isValidName } from "./internal/profile-name.js";
|
|
12
|
+
|
|
13
|
+
export type ProfileOptions = ConfigurationOptions;
|
|
11
14
|
|
|
12
15
|
export interface ProfileInfo {
|
|
13
16
|
name: string;
|
|
14
|
-
|
|
17
|
+
dataDir: string;
|
|
18
|
+
configFile: string;
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
export interface ProfilePaths {
|
|
18
|
-
|
|
22
|
+
dataDir: string;
|
|
19
23
|
state: string;
|
|
20
24
|
cookies: string;
|
|
25
|
+
configFile: string;
|
|
21
26
|
}
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function directory(options: ProfileOptions = {}): string {
|
|
28
|
-
const home = options.homeDirectory ?? homedir();
|
|
29
|
-
if (!isAbsolute(home)) throw new TypeError("homeDirectory must be an absolute path.");
|
|
30
|
-
return join(home, ".local", "share", "relapse", "profiles");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function path(name: string, options: ProfileOptions = {}): string {
|
|
34
|
-
if (!isValidName(name)) {
|
|
35
|
-
throw new TypeError("Profile names must contain 1–64 letters, digits, dots, underscores or hyphens, excluding . and ..");
|
|
36
|
-
}
|
|
37
|
-
return join(directory(options), name);
|
|
28
|
+
function info(name: string, dataDir: string, options: ProfileOptions): ProfileInfo {
|
|
29
|
+
return { name, dataDir, configFile: configurationPath(name, options) };
|
|
38
30
|
}
|
|
39
31
|
|
|
40
|
-
export function paths(name: string, options: ProfileOptions = {}): ProfilePaths {
|
|
41
|
-
const
|
|
32
|
+
export async function paths(name: string, options: ProfileOptions = {}): Promise<ProfilePaths> {
|
|
33
|
+
const configuration = await resolveConfiguration(name, options);
|
|
42
34
|
return {
|
|
43
|
-
|
|
44
|
-
state: join(
|
|
45
|
-
cookies: join(
|
|
35
|
+
dataDir: configuration.dataDir,
|
|
36
|
+
state: join(configuration.dataDir, "state"),
|
|
37
|
+
cookies: join(configuration.dataDir, "state", "app", "data", "sober", "cookies"),
|
|
38
|
+
configFile: configurationPath(name, options),
|
|
46
39
|
};
|
|
47
40
|
}
|
|
48
41
|
|
|
49
42
|
export async function get(name: string, options: ProfileOptions = {}): Promise<ProfileInfo> {
|
|
50
|
-
const
|
|
51
|
-
const
|
|
52
|
-
if (!
|
|
53
|
-
|
|
43
|
+
const configFile = configurationPath(name, options);
|
|
44
|
+
const configured = await lstat(configFile);
|
|
45
|
+
if (!configured.isFile()) throw new Error("Profile configuration must be a regular file.");
|
|
46
|
+
const configuration = await resolveConfiguration(name, options);
|
|
47
|
+
return info(name, configuration.dataDir, options);
|
|
54
48
|
}
|
|
55
49
|
|
|
56
50
|
export async function list(options: ProfileOptions = {}): Promise<ProfileInfo[]> {
|
|
57
|
-
const root =
|
|
51
|
+
const root = join(configDirectory(options), "profiles");
|
|
58
52
|
const entries = await readdir(root, { withFileTypes: true }).catch((error: NodeJS.ErrnoException) => {
|
|
59
53
|
if (error.code === "ENOENT") return [];
|
|
60
54
|
throw error;
|
|
61
55
|
});
|
|
62
|
-
|
|
63
|
-
.map(entry =>
|
|
64
|
-
|
|
56
|
+
const names = entries.filter(entry => entry.isDirectory() && isValidName(entry.name))
|
|
57
|
+
.map(entry => entry.name).sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
58
|
+
const result: ProfileInfo[] = [];
|
|
59
|
+
for (const name of names) {
|
|
60
|
+
try {
|
|
61
|
+
result.push(await get(name, options));
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
/** Creates a fresh profile directory. The launcher initializes its state on first use. */
|
|
68
69
|
export async function create(name: string, options: ProfileOptions = {}): Promise<ProfileInfo> {
|
|
69
|
-
const
|
|
70
|
-
const root =
|
|
70
|
+
const configFile = configurationPath(name, options);
|
|
71
|
+
const root = join(dataDirectory(options), "profiles");
|
|
72
|
+
const dataDir = join(root, name);
|
|
73
|
+
const configured = await lstat(configFile).catch((error: NodeJS.ErrnoException) => {
|
|
74
|
+
if (error.code === "ENOENT") return undefined;
|
|
75
|
+
throw error;
|
|
76
|
+
});
|
|
77
|
+
if (configured) throw new Error("Profile already exists.");
|
|
71
78
|
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
72
79
|
if (!(await lstat(root)).isDirectory()) throw new Error("Profiles directory must not be a symlink.");
|
|
73
|
-
await mkdir(
|
|
74
|
-
|
|
80
|
+
await mkdir(dataDir, { mode: 0o700 });
|
|
81
|
+
await writeProfile(name, { dataDir, override: {} }, options);
|
|
82
|
+
return info(name, dataDir, options);
|
|
75
83
|
}
|