@cortexkit/opencode-antigravity-auth 3.0.39 → 3.0.41
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/dist/cli.js +535 -112
- package/dist/cli.js.map +4 -4
- package/dist/cli.meta.json +67 -38
- package/dist/index.js +6899 -3288
- package/dist/index.js.map +4 -4
- package/dist/index.meta.json +1503 -465
- package/dist/server.js +7028 -3363
- package/dist/server.js.map +4 -4
- package/dist/server.meta.json +1 -1
- package/dist/src/common-auth-embedded/claustrum/enrollment.d.ts +25 -4
- package/dist/src/common-auth-embedded/claustrum/enrollment.js +81 -4
- package/dist/src/common-auth-embedded/claustrum/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/claustrum/index.js +1 -1
- package/dist/src/common-auth-embedded/commands/builtins.d.ts +20 -1
- package/dist/src/common-auth-embedded/commands/builtins.js +28 -12
- package/dist/src/common-auth-embedded/commands/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/commands/menu.d.ts +6 -1
- package/dist/src/common-auth-embedded/commands/menu.js +1 -0
- package/dist/src/common-auth-embedded/fs/file-read-cache.js +9 -5
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.d.ts +11 -0
- package/dist/src/common-auth-embedded/fs/refresh-file-lock.js +146 -2
- package/dist/src/common-auth-embedded/fs/with-lock.d.ts +2 -0
- package/dist/src/common-auth-embedded/fs/with-lock.js +1 -1
- package/dist/src/common-auth-embedded/settings/define.d.ts +35 -0
- package/dist/src/common-auth-embedded/settings/define.js +52 -0
- package/dist/src/common-auth-embedded/settings/descriptors.d.ts +97 -0
- package/dist/src/common-auth-embedded/settings/descriptors.js +81 -0
- package/dist/src/common-auth-embedded/settings/edit.d.ts +24 -0
- package/dist/src/common-auth-embedded/settings/edit.js +229 -0
- package/dist/src/common-auth-embedded/settings/fs.js +19 -0
- package/dist/src/common-auth-embedded/settings/handle.d.ts +30 -0
- package/dist/src/common-auth-embedded/settings/handle.js +228 -0
- package/dist/src/common-auth-embedded/settings/index.d.ts +10 -0
- package/dist/src/common-auth-embedded/settings/index.js +7 -0
- package/dist/src/common-auth-embedded/settings/lock-errors.js +15 -0
- package/dist/src/common-auth-embedded/settings/move.d.ts +76 -0
- package/dist/src/common-auth-embedded/settings/move.js +351 -0
- package/dist/src/common-auth-embedded/settings/parse.d.ts +24 -0
- package/dist/src/common-auth-embedded/settings/parse.js +65 -0
- package/dist/src/common-auth-embedded/settings/paths.d.ts +12 -0
- package/dist/src/common-auth-embedded/settings/paths.js +48 -0
- package/dist/src/common-auth-embedded/settings/refusal.d.ts +7 -0
- package/dist/src/common-auth-embedded/settings/refusal.js +15 -0
- package/dist/src/common-auth-embedded/settings/resolve.d.ts +15 -0
- package/dist/src/common-auth-embedded/settings/resolve.js +51 -0
- package/dist/src/common-auth-embedded/settings/schema.d.ts +29 -0
- package/dist/src/common-auth-embedded/settings/schema.js +81 -0
- package/dist/src/common-auth-embedded/settings/snapshot.d.ts +24 -0
- package/dist/src/common-auth-embedded/settings/snapshot.js +150 -0
- package/dist/src/common-auth-embedded/settings/update.js +139 -0
- package/dist/src/common-auth-embedded/settings/write.js +88 -0
- package/dist/src/common-auth-embedded/setup/command-error.d.ts +1 -1
- package/dist/src/common-auth-embedded/setup/command-error.js +4 -2
- package/dist/src/common-auth-embedded/setup/command.js +23 -6
- package/dist/src/common-auth-embedded/setup/enroll.d.ts +2 -2
- package/dist/src/common-auth-embedded/setup/enroll.js +13 -3
- package/dist/src/common-auth-embedded/setup/enrollment-access.js +86 -0
- package/dist/src/common-auth-embedded/setup/index.d.ts +1 -1
- package/dist/src/common-auth-embedded/setup/types.d.ts +16 -1
- package/dist/src/common-auth-embedded/source-output.json +314 -70
- package/dist/src/common-auth-embedded/tui-prefs/watcher.js +196 -0
- package/dist/src/ga/tui/host-ga.js +1 -1
- package/dist/src/ga/tui/host-ga.js.map +1 -1
- package/dist/src/plugin/__fixtures__/verified-store.js +2 -2
- package/dist/src/plugin/command-apply.d.ts +43 -3
- package/dist/src/plugin/command-apply.d.ts.map +1 -1
- package/dist/src/plugin/command-apply.js +92 -12
- package/dist/src/plugin/command-apply.js.map +1 -1
- package/dist/src/plugin/commands.d.ts +5 -1
- package/dist/src/plugin/commands.d.ts.map +1 -1
- package/dist/src/plugin/commands.js +4 -3
- package/dist/src/plugin/commands.js.map +1 -1
- package/dist/src/plugin/config/writer.d.ts +10 -3
- package/dist/src/plugin/config/writer.d.ts.map +1 -1
- package/dist/src/plugin/config/writer.js +30 -22
- package/dist/src/plugin/config/writer.js.map +1 -1
- package/dist/src/plugin/fetch-interceptor.d.ts +8 -3
- package/dist/src/plugin/fetch-interceptor.d.ts.map +1 -1
- package/dist/src/plugin/fetch-interceptor.js +1 -0
- package/dist/src/plugin/fetch-interceptor.js.map +1 -1
- package/dist/src/plugin/ga-location-services.d.ts +3 -1
- package/dist/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/src/plugin/ga-location-services.js +35 -5
- package/dist/src/plugin/ga-location-services.js.map +1 -1
- package/dist/src/plugin/index.d.ts.map +1 -1
- package/dist/src/plugin/index.js +65 -15
- package/dist/src/plugin/index.js.map +1 -1
- package/dist/src/plugin/killswitch.d.ts +20 -0
- package/dist/src/plugin/killswitch.d.ts.map +1 -1
- package/dist/src/plugin/killswitch.js +14 -4
- package/dist/src/plugin/killswitch.js.map +1 -1
- package/dist/src/plugin/logger.d.ts +3 -1
- package/dist/src/plugin/logger.d.ts.map +1 -1
- package/dist/src/plugin/logger.js +15 -7
- package/dist/src/plugin/logger.js.map +1 -1
- package/dist/src/plugin/operator-settings.d.ts +6 -0
- package/dist/src/plugin/operator-settings.d.ts.map +1 -1
- package/dist/src/plugin/operator-settings.js.map +1 -1
- package/dist/src/plugin/request-helpers.d.ts +5 -0
- package/dist/src/plugin/request-helpers.d.ts.map +1 -1
- package/dist/src/plugin/request-helpers.js +11 -2
- package/dist/src/plugin/request-helpers.js.map +1 -1
- package/dist/src/plugin/shared/request-services.d.ts +10 -1
- package/dist/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/src/plugin/shared/request-services.js +125 -22
- package/dist/src/plugin/shared/request-services.js.map +1 -1
- package/dist/src/plugin/shared/runtime.d.ts +15 -0
- package/dist/src/plugin/shared/runtime.d.ts.map +1 -1
- package/dist/src/plugin/shared/runtime.js +43 -11
- package/dist/src/plugin/shared/runtime.js.map +1 -1
- package/dist/src/plugin/shared/vault-billing.d.ts +30 -0
- package/dist/src/plugin/shared/vault-billing.d.ts.map +1 -0
- package/dist/src/plugin/shared/vault-billing.js +196 -0
- package/dist/src/plugin/shared/vault-billing.js.map +1 -0
- package/dist/src/plugin/shared/vault-menu.d.ts +11 -1
- package/dist/src/plugin/shared/vault-menu.d.ts.map +1 -1
- package/dist/src/plugin/shared/vault-menu.js +33 -7
- package/dist/src/plugin/shared/vault-menu.js.map +1 -1
- package/dist/src/plugin/vault-custody.d.ts.map +1 -1
- package/dist/src/plugin/vault-custody.js +3 -0
- package/dist/src/plugin/vault-custody.js.map +1 -1
- package/dist/src/settings/core-settings.d.ts +7 -0
- package/dist/src/settings/core-settings.d.ts.map +1 -0
- package/dist/src/settings/core-settings.js +7 -0
- package/dist/src/settings/core-settings.js.map +1 -0
- package/dist/src/settings/legacy.d.ts +89 -0
- package/dist/src/settings/legacy.d.ts.map +1 -0
- package/dist/src/settings/legacy.js +184 -0
- package/dist/src/settings/legacy.js.map +1 -0
- package/dist/src/settings/location.d.ts +22 -0
- package/dist/src/settings/location.d.ts.map +1 -0
- package/dist/src/settings/location.js +49 -0
- package/dist/src/settings/location.js.map +1 -0
- package/dist/src/settings/move.d.ts +38 -0
- package/dist/src/settings/move.d.ts.map +1 -0
- package/dist/src/settings/move.js +83 -0
- package/dist/src/settings/move.js.map +1 -0
- package/dist/src/settings/protection.d.ts +11 -0
- package/dist/src/settings/protection.d.ts.map +1 -0
- package/dist/src/settings/protection.js +17 -0
- package/dist/src/settings/protection.js.map +1 -0
- package/dist/src/settings/report.d.ts +33 -0
- package/dist/src/settings/report.d.ts.map +1 -0
- package/dist/src/settings/report.js +103 -0
- package/dist/src/settings/report.js.map +1 -0
- package/dist/src/settings/runtime.d.ts +145 -0
- package/dist/src/settings/runtime.d.ts.map +1 -0
- package/dist/src/settings/runtime.js +697 -0
- package/dist/src/settings/runtime.js.map +1 -0
- package/dist/src/setup.d.ts.map +1 -1
- package/dist/src/setup.js +12 -0
- package/dist/src/setup.js.map +1 -1
- package/dist/tui-build-map.json +1 -1
- package/dist/tui-ga-build-map.json +5 -5
- package/dist/types/src/common-auth-embedded/claustrum/enrollment.d.ts +25 -4
- package/dist/types/src/common-auth-embedded/claustrum/index.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/commands/builtins.d.ts +20 -1
- package/dist/types/src/common-auth-embedded/commands/index.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/commands/menu.d.ts +6 -1
- package/dist/types/src/common-auth-embedded/fs/refresh-file-lock.d.ts +11 -0
- package/dist/types/src/common-auth-embedded/setup/command-error.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/setup/enroll.d.ts +2 -2
- package/dist/types/src/common-auth-embedded/setup/index.d.ts +1 -1
- package/dist/types/src/common-auth-embedded/setup/types.d.ts +16 -1
- package/dist/types/src/plugin/command-apply.d.ts +43 -3
- package/dist/types/src/plugin/command-apply.d.ts.map +1 -1
- package/dist/types/src/plugin/commands.d.ts +5 -1
- package/dist/types/src/plugin/commands.d.ts.map +1 -1
- package/dist/types/src/plugin/config/writer.d.ts +10 -3
- package/dist/types/src/plugin/config/writer.d.ts.map +1 -1
- package/dist/types/src/plugin/fetch-interceptor.d.ts +8 -3
- package/dist/types/src/plugin/fetch-interceptor.d.ts.map +1 -1
- package/dist/types/src/plugin/ga-location-services.d.ts +3 -1
- package/dist/types/src/plugin/ga-location-services.d.ts.map +1 -1
- package/dist/types/src/plugin/index.d.ts.map +1 -1
- package/dist/types/src/plugin/killswitch.d.ts +20 -0
- package/dist/types/src/plugin/killswitch.d.ts.map +1 -1
- package/dist/types/src/plugin/logger.d.ts +3 -1
- package/dist/types/src/plugin/logger.d.ts.map +1 -1
- package/dist/types/src/plugin/operator-settings.d.ts +6 -0
- package/dist/types/src/plugin/operator-settings.d.ts.map +1 -1
- package/dist/types/src/plugin/request-helpers.d.ts +5 -0
- package/dist/types/src/plugin/request-helpers.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/request-services.d.ts +10 -1
- package/dist/types/src/plugin/shared/request-services.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/runtime.d.ts +15 -0
- package/dist/types/src/plugin/shared/runtime.d.ts.map +1 -1
- package/dist/types/src/plugin/shared/vault-billing.d.ts +30 -0
- package/dist/types/src/plugin/shared/vault-billing.d.ts.map +1 -0
- package/dist/types/src/plugin/shared/vault-menu.d.ts +11 -1
- package/dist/types/src/plugin/shared/vault-menu.d.ts.map +1 -1
- package/dist/types/src/plugin/vault-custody.d.ts.map +1 -1
- package/dist/types/src/settings/core-settings.d.ts +7 -0
- package/dist/types/src/settings/core-settings.d.ts.map +1 -0
- package/dist/types/src/settings/legacy.d.ts +89 -0
- package/dist/types/src/settings/legacy.d.ts.map +1 -0
- package/dist/types/src/settings/location.d.ts +22 -0
- package/dist/types/src/settings/location.d.ts.map +1 -0
- package/dist/types/src/settings/move.d.ts +38 -0
- package/dist/types/src/settings/move.d.ts.map +1 -0
- package/dist/types/src/settings/protection.d.ts +11 -0
- package/dist/types/src/settings/protection.d.ts.map +1 -0
- package/dist/types/src/settings/report.d.ts +33 -0
- package/dist/types/src/settings/report.d.ts.map +1 -0
- package/dist/types/src/settings/runtime.d.ts +145 -0
- package/dist/types/src/settings/runtime.d.ts.map +1 -0
- package/dist/types/src/setup.d.ts.map +1 -1
- package/package.json +4 -3
- package/src/common-auth-embedded/claustrum/enrollment.d.ts +25 -4
- package/src/common-auth-embedded/claustrum/enrollment.js +81 -4
- package/src/common-auth-embedded/claustrum/index.d.ts +1 -1
- package/src/common-auth-embedded/claustrum/index.js +1 -1
- package/src/common-auth-embedded/commands/builtins.d.ts +20 -1
- package/src/common-auth-embedded/commands/builtins.js +28 -12
- package/src/common-auth-embedded/commands/index.d.ts +1 -1
- package/src/common-auth-embedded/commands/menu.d.ts +6 -1
- package/src/common-auth-embedded/commands/menu.js +1 -0
- package/src/common-auth-embedded/fs/file-read-cache.js +9 -5
- package/src/common-auth-embedded/fs/refresh-file-lock.d.ts +11 -0
- package/src/common-auth-embedded/fs/refresh-file-lock.js +146 -2
- package/src/common-auth-embedded/fs/with-lock.d.ts +2 -0
- package/src/common-auth-embedded/fs/with-lock.js +1 -1
- package/src/common-auth-embedded/settings/define.d.ts +35 -0
- package/src/common-auth-embedded/settings/define.js +52 -0
- package/src/common-auth-embedded/settings/descriptors.d.ts +97 -0
- package/src/common-auth-embedded/settings/descriptors.js +81 -0
- package/src/common-auth-embedded/settings/edit.d.ts +24 -0
- package/src/common-auth-embedded/settings/edit.js +229 -0
- package/src/common-auth-embedded/settings/fs.js +19 -0
- package/src/common-auth-embedded/settings/handle.d.ts +30 -0
- package/src/common-auth-embedded/settings/handle.js +228 -0
- package/src/common-auth-embedded/settings/index.d.ts +10 -0
- package/src/common-auth-embedded/settings/index.js +7 -0
- package/src/common-auth-embedded/settings/lock-errors.js +15 -0
- package/src/common-auth-embedded/settings/move.d.ts +76 -0
- package/src/common-auth-embedded/settings/move.js +351 -0
- package/src/common-auth-embedded/settings/parse.d.ts +24 -0
- package/src/common-auth-embedded/settings/parse.js +65 -0
- package/src/common-auth-embedded/settings/paths.d.ts +12 -0
- package/src/common-auth-embedded/settings/paths.js +48 -0
- package/src/common-auth-embedded/settings/refusal.d.ts +7 -0
- package/src/common-auth-embedded/settings/refusal.js +15 -0
- package/src/common-auth-embedded/settings/resolve.d.ts +15 -0
- package/src/common-auth-embedded/settings/resolve.js +51 -0
- package/src/common-auth-embedded/settings/schema.d.ts +29 -0
- package/src/common-auth-embedded/settings/schema.js +81 -0
- package/src/common-auth-embedded/settings/snapshot.d.ts +24 -0
- package/src/common-auth-embedded/settings/snapshot.js +150 -0
- package/src/common-auth-embedded/settings/update.js +139 -0
- package/src/common-auth-embedded/settings/write.js +88 -0
- package/src/common-auth-embedded/setup/command-error.d.ts +1 -1
- package/src/common-auth-embedded/setup/command-error.js +4 -2
- package/src/common-auth-embedded/setup/command.js +23 -6
- package/src/common-auth-embedded/setup/enroll.d.ts +2 -2
- package/src/common-auth-embedded/setup/enroll.js +13 -3
- package/src/common-auth-embedded/setup/enrollment-access.js +86 -0
- package/src/common-auth-embedded/setup/index.d.ts +1 -1
- package/src/common-auth-embedded/setup/types.d.ts +16 -1
- package/src/common-auth-embedded/source-output.json +314 -70
- package/src/common-auth-embedded/tui-prefs/watcher.js +196 -0
- package/src/tui-ga-compiled/host-ga.js +1 -1
- package/src/tui-ga-raw/host-ga.tsx +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Node, type ParseError } from 'jsonc-parser';
|
|
2
|
+
interface ParsedText {
|
|
3
|
+
/** Text without the optional leading BOM; tree offsets refer to this text. */
|
|
4
|
+
text: string;
|
|
5
|
+
hasBom: boolean;
|
|
6
|
+
tree: Node | undefined;
|
|
7
|
+
}
|
|
8
|
+
export interface ParsedSettings extends ParsedText {
|
|
9
|
+
parseable: true;
|
|
10
|
+
value: Record<string, unknown>;
|
|
11
|
+
duplicateKeyPaths: string[][];
|
|
12
|
+
hasDuplicates: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface UnparseableSettings extends ParsedText {
|
|
15
|
+
parseable: false;
|
|
16
|
+
errors: ParseError[];
|
|
17
|
+
}
|
|
18
|
+
export type SettingsParseResult = ParsedSettings | UnparseableSettings;
|
|
19
|
+
/** Define own data properties so JSON keys never invoke prototype setters. */
|
|
20
|
+
export declare function defineSettingsKey(target: Record<string, unknown>, key: string, value: unknown): void;
|
|
21
|
+
/** Convert in document order, retaining the last occurrence of a repeated key. */
|
|
22
|
+
export declare function settingsValueFromTree(node: Node, onDuplicate?: (keyPath: string[]) => void, keyPath?: string[]): unknown;
|
|
23
|
+
export declare function parseSettings(source: string): SettingsParseResult;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { parseTree } from 'jsonc-parser';
|
|
2
|
+
/** Define own data properties so JSON keys never invoke prototype setters. */
|
|
3
|
+
export function defineSettingsKey(target, key, value) {
|
|
4
|
+
Object.defineProperty(target, key, {
|
|
5
|
+
value,
|
|
6
|
+
enumerable: true,
|
|
7
|
+
writable: true,
|
|
8
|
+
configurable: true,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
/** Convert in document order, retaining the last occurrence of a repeated key. */
|
|
12
|
+
export function settingsValueFromTree(node, onDuplicate = () => { }, keyPath = []) {
|
|
13
|
+
if (node.type === 'object') {
|
|
14
|
+
const value = {};
|
|
15
|
+
for (const property of node.children ?? []) {
|
|
16
|
+
const [keyNode, valueNode] = property.children ?? [];
|
|
17
|
+
if (!keyNode || !valueNode)
|
|
18
|
+
continue;
|
|
19
|
+
const key = keyNode.value;
|
|
20
|
+
const path = [...keyPath, key];
|
|
21
|
+
if (Object.hasOwn(value, key))
|
|
22
|
+
onDuplicate(path);
|
|
23
|
+
defineSettingsKey(value, key, settingsValueFromTree(valueNode, onDuplicate, path));
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
if (node.type === 'array') {
|
|
28
|
+
return (node.children ?? []).map((child, index) => settingsValueFromTree(child, onDuplicate, [...keyPath, String(index)]));
|
|
29
|
+
}
|
|
30
|
+
return node.value;
|
|
31
|
+
}
|
|
32
|
+
export function parseSettings(source) {
|
|
33
|
+
const hasBom = source.startsWith('\uFEFF');
|
|
34
|
+
const text = hasBom ? source.slice(1) : source;
|
|
35
|
+
// Do not use trim(): it would silently discard a second or misplaced BOM.
|
|
36
|
+
if (/^[\x20\t\r\n]*$/.test(text)) {
|
|
37
|
+
return {
|
|
38
|
+
parseable: true,
|
|
39
|
+
text,
|
|
40
|
+
hasBom,
|
|
41
|
+
tree: undefined,
|
|
42
|
+
value: {},
|
|
43
|
+
duplicateKeyPaths: [],
|
|
44
|
+
hasDuplicates: false,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const errors = [];
|
|
48
|
+
const tree = parseTree(text, errors, { allowTrailingComma: true });
|
|
49
|
+
if (errors.length || tree?.type !== 'object') {
|
|
50
|
+
return { parseable: false, text, hasBom, tree, errors };
|
|
51
|
+
}
|
|
52
|
+
const paths = new Map();
|
|
53
|
+
const value = settingsValueFromTree(tree, (path) => {
|
|
54
|
+
paths.set(JSON.stringify(path), path);
|
|
55
|
+
});
|
|
56
|
+
return {
|
|
57
|
+
parseable: true,
|
|
58
|
+
text,
|
|
59
|
+
hasBom,
|
|
60
|
+
tree,
|
|
61
|
+
value,
|
|
62
|
+
duplicateKeyPaths: [...paths.values()],
|
|
63
|
+
hasDuplicates: paths.size > 0,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
interface ConfigDirOptions {
|
|
2
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
3
|
+
platform: NodeJS.Platform;
|
|
4
|
+
homedir: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function isAbsoluteForPlatform(path: string, platform: NodeJS.Platform): boolean;
|
|
7
|
+
export declare function cortexkitConfigDir({ env, platform, homedir, }: ConfigDirOptions): string;
|
|
8
|
+
export declare function settingsFilePath({ plugin, override, ...options }: ConfigDirOptions & {
|
|
9
|
+
plugin: string;
|
|
10
|
+
override?: string;
|
|
11
|
+
}): string;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { posix, win32 } from 'node:path';
|
|
2
|
+
import { SettingsRefusal } from './refusal.js';
|
|
3
|
+
export function isAbsoluteForPlatform(path, platform) {
|
|
4
|
+
if (platform !== 'win32')
|
|
5
|
+
return path.startsWith('/');
|
|
6
|
+
// Verbatim and device prefixes have an implicit root in Rust's path model.
|
|
7
|
+
if (path.startsWith('\\\\?\\') || path.startsWith('\\\\.\\'))
|
|
8
|
+
return true;
|
|
9
|
+
if (/^[a-zA-Z]:[\\/]/.test(path))
|
|
10
|
+
return true;
|
|
11
|
+
// Ordinary UNC prefixes accept either separator, but need server and share.
|
|
12
|
+
return /^[\\/]{2}[^\\/]+[\\/][^\\/]+/.test(path);
|
|
13
|
+
}
|
|
14
|
+
export function cortexkitConfigDir({ env, platform, homedir, }) {
|
|
15
|
+
const paths = platform === 'win32' ? win32 : posix;
|
|
16
|
+
const absolute = (value) => value !== undefined && isAbsoluteForPlatform(value, platform);
|
|
17
|
+
let base;
|
|
18
|
+
if (absolute(env.XDG_CONFIG_HOME)) {
|
|
19
|
+
base = env.XDG_CONFIG_HOME;
|
|
20
|
+
}
|
|
21
|
+
else if (platform === 'win32' && absolute(env.APPDATA)) {
|
|
22
|
+
base = env.APPDATA;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
let home = platform === 'win32' ? env.USERPROFILE : env.HOME;
|
|
26
|
+
if (!absolute(home)) {
|
|
27
|
+
// commons cortexkit-paths' dirs.rs reports no directory when every home
|
|
28
|
+
// and config variable is unusable. Accepting the caller's absolute
|
|
29
|
+
// homedir instead is the one deliberate addition to that Rust rule.
|
|
30
|
+
if (!absolute(homedir)) {
|
|
31
|
+
throw new SettingsRefusal('settings-unavailable', 'No absolute home or config directory is available');
|
|
32
|
+
}
|
|
33
|
+
home = homedir;
|
|
34
|
+
}
|
|
35
|
+
base =
|
|
36
|
+
platform === 'win32'
|
|
37
|
+
? paths.join(paths.join(home, 'AppData'), 'Roaming')
|
|
38
|
+
: paths.join(home, '.config');
|
|
39
|
+
}
|
|
40
|
+
return paths.join(base, 'cortexkit');
|
|
41
|
+
}
|
|
42
|
+
export function settingsFilePath({ plugin, override, ...options }) {
|
|
43
|
+
if (override && isAbsoluteForPlatform(override, options.platform)) {
|
|
44
|
+
return override;
|
|
45
|
+
}
|
|
46
|
+
const paths = options.platform === 'win32' ? win32 : posix;
|
|
47
|
+
return paths.join(cortexkitConfigDir(options), `${plugin}.jsonc`);
|
|
48
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const SETTINGS_REFUSAL_CODES: readonly ["settings-unparseable", "settings-newer-version", "settings-invalid-value", "settings-busy", "settings-unavailable"];
|
|
2
|
+
export type SettingsRefusalCode = (typeof SETTINGS_REFUSAL_CODES)[number];
|
|
3
|
+
export declare class SettingsRefusal extends Error {
|
|
4
|
+
readonly code: SettingsRefusalCode;
|
|
5
|
+
readonly name = "SettingsRefusal";
|
|
6
|
+
constructor(code: SettingsRefusalCode, message: string, options?: ErrorOptions);
|
|
7
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const SETTINGS_REFUSAL_CODES = Object.freeze([
|
|
2
|
+
'settings-unparseable',
|
|
3
|
+
'settings-newer-version',
|
|
4
|
+
'settings-invalid-value',
|
|
5
|
+
'settings-busy',
|
|
6
|
+
'settings-unavailable',
|
|
7
|
+
]);
|
|
8
|
+
export class SettingsRefusal extends Error {
|
|
9
|
+
code;
|
|
10
|
+
name = 'SettingsRefusal';
|
|
11
|
+
constructor(code, message, options) {
|
|
12
|
+
super(message, options);
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { SettingDeclaration, SettingsDefinition } from './define.js';
|
|
2
|
+
import { type ValueOf } from './descriptors.js';
|
|
3
|
+
import type { SettingsSnapshot } from './snapshot.js';
|
|
4
|
+
export interface GetOptions {
|
|
5
|
+
readonly host: string;
|
|
6
|
+
readonly model?: string;
|
|
7
|
+
readonly project?: string;
|
|
8
|
+
}
|
|
9
|
+
/** The caller selects the project snapshot for the requested project, if any. */
|
|
10
|
+
export interface ResolutionSnapshots {
|
|
11
|
+
readonly user?: SettingsSnapshot;
|
|
12
|
+
readonly project?: SettingsSnapshot;
|
|
13
|
+
}
|
|
14
|
+
/** Resolve sanitized snapshots without reading files or adding report entries. */
|
|
15
|
+
export declare function resolveSetting<S extends Readonly<Record<string, SettingDeclaration>>, K extends keyof S & string>(definition: SettingsDefinition<S>, name: K, options: GetOptions, snapshots?: ResolutionSnapshots): ValueOf<S[K]['value']>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { isJsonObject, } from './descriptors.js';
|
|
2
|
+
import { defineSettingsKey } from './parse.js';
|
|
3
|
+
function valueAtLevel(declaration, name, snapshot, options) {
|
|
4
|
+
if (!snapshot || !Object.hasOwn(snapshot.settings, name))
|
|
5
|
+
return undefined;
|
|
6
|
+
const value = snapshot.settings[name];
|
|
7
|
+
if (declaration.overrides === undefined || !isJsonObject(value))
|
|
8
|
+
return value;
|
|
9
|
+
const { model, host } = options;
|
|
10
|
+
if (declaration.overrides.model &&
|
|
11
|
+
model !== undefined &&
|
|
12
|
+
model !== 'default' &&
|
|
13
|
+
model !== 'opencode' &&
|
|
14
|
+
model !== 'pi' &&
|
|
15
|
+
Object.hasOwn(value, model)) {
|
|
16
|
+
return value[model];
|
|
17
|
+
}
|
|
18
|
+
if (declaration.overrides.host &&
|
|
19
|
+
(host === 'opencode' || host === 'pi') &&
|
|
20
|
+
Object.hasOwn(value, host)) {
|
|
21
|
+
return value[host];
|
|
22
|
+
}
|
|
23
|
+
return Object.hasOwn(value, 'default') ? value.default : undefined;
|
|
24
|
+
}
|
|
25
|
+
/** Snapshot validation has already removed unusable values and unknown fields. */
|
|
26
|
+
function resolveValue(descriptor, levels) {
|
|
27
|
+
if (descriptor.kind !== 'object') {
|
|
28
|
+
return levels.find((value) => value !== undefined);
|
|
29
|
+
}
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const [key, field] of Object.entries(descriptor.fields)) {
|
|
32
|
+
const candidates = levels.map((value) => isJsonObject(value) && Object.hasOwn(value, key) ? value[key] : undefined);
|
|
33
|
+
defineSettingsKey(result, key, resolveValue(field, candidates));
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
/** Resolve sanitized snapshots without reading files or adding report entries. */
|
|
38
|
+
export function resolveSetting(definition, name, options, snapshots = {}) {
|
|
39
|
+
if (!Object.hasOwn(definition.settings, name)) {
|
|
40
|
+
throw new TypeError(`Undeclared setting: ${name}`);
|
|
41
|
+
}
|
|
42
|
+
const declaration = definition.settings[name];
|
|
43
|
+
const levels = [];
|
|
44
|
+
if (declaration.project === 'allowed' && options.project !== undefined) {
|
|
45
|
+
levels.push(valueAtLevel(declaration, name, snapshots.project, options));
|
|
46
|
+
}
|
|
47
|
+
levels.push(valueAtLevel(declaration, name, snapshots.user, options));
|
|
48
|
+
levels.push(declaration.default);
|
|
49
|
+
// Clone even a whole-list or whole-record winner so callers cannot mutate it.
|
|
50
|
+
return structuredClone(resolveValue(declaration.value, levels));
|
|
51
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { SettingsDefinition } from './define.js';
|
|
2
|
+
interface JsonSchema {
|
|
3
|
+
type?: 'boolean' | 'integer' | 'number' | 'string' | 'array' | 'object';
|
|
4
|
+
minimum?: number;
|
|
5
|
+
maximum?: number;
|
|
6
|
+
enum?: string[];
|
|
7
|
+
items?: JsonSchema;
|
|
8
|
+
properties?: Record<string, JsonSchema>;
|
|
9
|
+
additionalProperties?: boolean | JsonSchema;
|
|
10
|
+
required?: string[];
|
|
11
|
+
anyOf?: JsonSchema[];
|
|
12
|
+
propertyNames?: {
|
|
13
|
+
not: {
|
|
14
|
+
enum: string[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Generate the plugin's JSON Schema deterministically from its settings definition.
|
|
20
|
+
* Plugins ship the generated schema and check their parsed shipped file with:
|
|
21
|
+
* `expect(shippedSchema).toEqual(settingsJsonSchema(definition))`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function settingsJsonSchema(definition: SettingsDefinition): {
|
|
24
|
+
$schema: string;
|
|
25
|
+
type: 'object';
|
|
26
|
+
properties: Record<string, JsonSchema>;
|
|
27
|
+
additionalProperties: false;
|
|
28
|
+
};
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
function descriptorSchema(descriptor, completeObjects = false) {
|
|
2
|
+
switch (descriptor.kind) {
|
|
3
|
+
case 'boolean':
|
|
4
|
+
case 'string':
|
|
5
|
+
return { type: descriptor.kind };
|
|
6
|
+
case 'integer':
|
|
7
|
+
case 'number':
|
|
8
|
+
return {
|
|
9
|
+
type: descriptor.kind,
|
|
10
|
+
...(descriptor.min !== undefined ? { minimum: descriptor.min } : {}),
|
|
11
|
+
...(descriptor.max !== undefined ? { maximum: descriptor.max } : {}),
|
|
12
|
+
};
|
|
13
|
+
case 'oneOf':
|
|
14
|
+
return { enum: [...descriptor.values] };
|
|
15
|
+
case 'list':
|
|
16
|
+
return { type: 'array', items: descriptorSchema(descriptor.item, true) };
|
|
17
|
+
case 'object':
|
|
18
|
+
return {
|
|
19
|
+
type: 'object',
|
|
20
|
+
properties: Object.fromEntries(Object.entries(descriptor.fields).map(([name, field]) => [
|
|
21
|
+
name,
|
|
22
|
+
descriptorSchema(field, completeObjects),
|
|
23
|
+
])),
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
// Objects merge field by field, except in lists and records, which replace whole values.
|
|
26
|
+
...(completeObjects
|
|
27
|
+
? { required: Object.keys(descriptor.fields) }
|
|
28
|
+
: {}),
|
|
29
|
+
};
|
|
30
|
+
case 'record':
|
|
31
|
+
return {
|
|
32
|
+
type: 'object',
|
|
33
|
+
additionalProperties: descriptorSchema(descriptor.value, true),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function settingSchema(declaration) {
|
|
38
|
+
const value = descriptorSchema(declaration.value);
|
|
39
|
+
if (declaration.overrides === undefined)
|
|
40
|
+
return value;
|
|
41
|
+
const { host, model } = declaration.overrides;
|
|
42
|
+
const properties = { default: value };
|
|
43
|
+
if (host) {
|
|
44
|
+
properties.opencode = value;
|
|
45
|
+
properties.pi = value;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
anyOf: [
|
|
49
|
+
value,
|
|
50
|
+
{
|
|
51
|
+
type: 'object',
|
|
52
|
+
properties,
|
|
53
|
+
additionalProperties: model ? value : false,
|
|
54
|
+
...(model && !host
|
|
55
|
+
? { propertyNames: { not: { enum: ['opencode', 'pi'] } } }
|
|
56
|
+
: {}),
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Generate the plugin's JSON Schema deterministically from its settings definition.
|
|
63
|
+
* Plugins ship the generated schema and check their parsed shipped file with:
|
|
64
|
+
* `expect(shippedSchema).toEqual(settingsJsonSchema(definition))`.
|
|
65
|
+
*/
|
|
66
|
+
export function settingsJsonSchema(definition) {
|
|
67
|
+
const properties = {
|
|
68
|
+
schema_version: { type: 'integer', minimum: 1 },
|
|
69
|
+
$schema: { type: 'string' },
|
|
70
|
+
...Object.fromEntries(Object.entries(definition.settings).map(([name, declaration]) => [
|
|
71
|
+
name,
|
|
72
|
+
settingSchema(declaration),
|
|
73
|
+
])),
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
77
|
+
type: 'object',
|
|
78
|
+
properties,
|
|
79
|
+
additionalProperties: false,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type SettingsDefinition } from './define.js';
|
|
2
|
+
import { type SettingsParseResult } from './parse.js';
|
|
3
|
+
export type ReportReason = 'unknown-key' | 'override-not-allowed' | 'user-only-in-project' | 'invalid-value' | 'invalid-schema-version' | 'newer-version-key-ignored' | 'duplicate-key' | 'unparseable' | 'unreadable';
|
|
4
|
+
export interface ReportEntry {
|
|
5
|
+
file: {
|
|
6
|
+
kind: 'user' | 'project' | 'legacy-project';
|
|
7
|
+
path: string;
|
|
8
|
+
};
|
|
9
|
+
keyPath: string[];
|
|
10
|
+
reason: ReportReason;
|
|
11
|
+
detail?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface SettingsSnapshot {
|
|
14
|
+
file: ReportEntry['file'];
|
|
15
|
+
/** Only usable values remain; override objects retain their allowed keys. */
|
|
16
|
+
settings: Record<string, unknown>;
|
|
17
|
+
entries: ReportEntry[];
|
|
18
|
+
schemaVersion: number;
|
|
19
|
+
newer: boolean;
|
|
20
|
+
hasDuplicates: boolean;
|
|
21
|
+
parsed: SettingsParseResult;
|
|
22
|
+
}
|
|
23
|
+
/** Validate all overrides now, including values for hosts not requested later. */
|
|
24
|
+
export declare function buildSettingsSnapshot(definition: SettingsDefinition, file: ReportEntry['file'], parsed: SettingsParseResult): SettingsSnapshot;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { isOverrideKeyAllowed } from './define.js';
|
|
2
|
+
import { isJsonObject, validateValue, } from './descriptors.js';
|
|
3
|
+
import { defineSettingsKey } from './parse.js';
|
|
4
|
+
/** Validate all children even when a list or record will be skipped as a whole. */
|
|
5
|
+
function validateFileValue(descriptor, value, keyPath, completeObjects, unknownReason, report) {
|
|
6
|
+
const invalid = () => {
|
|
7
|
+
report(keyPath, 'invalid-value');
|
|
8
|
+
return { valid: false };
|
|
9
|
+
};
|
|
10
|
+
if (descriptor.kind === 'object') {
|
|
11
|
+
if (!isJsonObject(value))
|
|
12
|
+
return invalid();
|
|
13
|
+
const cleaned = {};
|
|
14
|
+
let valid = true;
|
|
15
|
+
for (const [key, field] of Object.entries(value)) {
|
|
16
|
+
if (!Object.hasOwn(descriptor.fields, key)) {
|
|
17
|
+
report([...keyPath, key], unknownReason);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const result = validateFileValue(descriptor.fields[key], field, [...keyPath, key], completeObjects, unknownReason, report);
|
|
21
|
+
if (result.valid)
|
|
22
|
+
defineSettingsKey(cleaned, key, result.value);
|
|
23
|
+
else
|
|
24
|
+
valid = false;
|
|
25
|
+
}
|
|
26
|
+
if (completeObjects) {
|
|
27
|
+
for (const key of Object.keys(descriptor.fields)) {
|
|
28
|
+
if (!Object.hasOwn(value, key)) {
|
|
29
|
+
report([...keyPath, key], 'invalid-value');
|
|
30
|
+
valid = false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Partial objects keep valid siblings; resolution supplies skipped fields.
|
|
35
|
+
return valid || !completeObjects
|
|
36
|
+
? { valid: true, value: cleaned }
|
|
37
|
+
: { valid: false };
|
|
38
|
+
}
|
|
39
|
+
if (descriptor.kind === 'list') {
|
|
40
|
+
if (!Array.isArray(value))
|
|
41
|
+
return invalid();
|
|
42
|
+
const cleaned = [];
|
|
43
|
+
let valid = true;
|
|
44
|
+
for (const [index, item] of value.entries()) {
|
|
45
|
+
const result = validateFileValue(descriptor.item, item, [...keyPath, String(index)], true, unknownReason, report);
|
|
46
|
+
if (result.valid)
|
|
47
|
+
cleaned.push(result.value);
|
|
48
|
+
else
|
|
49
|
+
valid = false;
|
|
50
|
+
}
|
|
51
|
+
return valid ? { valid: true, value: cleaned } : { valid: false };
|
|
52
|
+
}
|
|
53
|
+
if (descriptor.kind === 'record') {
|
|
54
|
+
if (!isJsonObject(value))
|
|
55
|
+
return invalid();
|
|
56
|
+
const cleaned = {};
|
|
57
|
+
let valid = true;
|
|
58
|
+
for (const [key, item] of Object.entries(value)) {
|
|
59
|
+
const result = validateFileValue(descriptor.value, item, [...keyPath, key], true, unknownReason, report);
|
|
60
|
+
if (result.valid)
|
|
61
|
+
defineSettingsKey(cleaned, key, result.value);
|
|
62
|
+
else
|
|
63
|
+
valid = false;
|
|
64
|
+
}
|
|
65
|
+
return valid ? { valid: true, value: cleaned } : { valid: false };
|
|
66
|
+
}
|
|
67
|
+
return validateValue(descriptor, value) ? { valid: true, value } : invalid();
|
|
68
|
+
}
|
|
69
|
+
/** Validate all overrides now, including values for hosts not requested later. */
|
|
70
|
+
export function buildSettingsSnapshot(definition, file, parsed) {
|
|
71
|
+
const entries = [];
|
|
72
|
+
const reported = new Set();
|
|
73
|
+
const report = (keyPath, reason) => {
|
|
74
|
+
const identity = JSON.stringify([keyPath, reason]);
|
|
75
|
+
if (reported.has(identity))
|
|
76
|
+
return;
|
|
77
|
+
reported.add(identity);
|
|
78
|
+
entries.push({ file: { ...file }, keyPath: [...keyPath], reason });
|
|
79
|
+
};
|
|
80
|
+
const snapshot = {
|
|
81
|
+
file: { ...file },
|
|
82
|
+
settings: {},
|
|
83
|
+
entries,
|
|
84
|
+
schemaVersion: 1,
|
|
85
|
+
newer: false,
|
|
86
|
+
hasDuplicates: parsed.parseable && parsed.hasDuplicates,
|
|
87
|
+
parsed,
|
|
88
|
+
};
|
|
89
|
+
if (!parsed.parseable) {
|
|
90
|
+
report([], 'unparseable');
|
|
91
|
+
return snapshot;
|
|
92
|
+
}
|
|
93
|
+
for (const keyPath of parsed.duplicateKeyPaths)
|
|
94
|
+
report(keyPath, 'duplicate-key');
|
|
95
|
+
const input = parsed.value;
|
|
96
|
+
if (Object.hasOwn(input, 'schema_version')) {
|
|
97
|
+
const version = input.schema_version;
|
|
98
|
+
if (typeof version === 'number' &&
|
|
99
|
+
Number.isInteger(version) &&
|
|
100
|
+
version >= 1) {
|
|
101
|
+
snapshot.schemaVersion = version;
|
|
102
|
+
snapshot.newer = version > 1;
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
report(['schema_version'], 'invalid-schema-version');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const unknownReason = snapshot.newer
|
|
109
|
+
? 'newer-version-key-ignored'
|
|
110
|
+
: 'unknown-key';
|
|
111
|
+
for (const [name, value] of Object.entries(input)) {
|
|
112
|
+
if (name === 'schema_version' || name === '$schema')
|
|
113
|
+
continue;
|
|
114
|
+
if (!Object.hasOwn(definition.settings, name)) {
|
|
115
|
+
report([name], unknownReason);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const declaration = definition.settings[name];
|
|
119
|
+
if (!declaration)
|
|
120
|
+
continue;
|
|
121
|
+
if (file.kind !== 'user' && declaration.project !== 'allowed') {
|
|
122
|
+
report([name], 'user-only-in-project');
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (isJsonObject(value) && declaration.overrides !== undefined) {
|
|
126
|
+
const overrides = {};
|
|
127
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
128
|
+
if (!isOverrideKeyAllowed(declaration.overrides, key)) {
|
|
129
|
+
report([name, key], 'override-not-allowed');
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const result = validateFileValue(declaration.value, entry, [name, key], false, unknownReason, report);
|
|
133
|
+
if (result.valid)
|
|
134
|
+
defineSettingsKey(overrides, key, result.value);
|
|
135
|
+
}
|
|
136
|
+
defineSettingsKey(snapshot.settings, name, overrides);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (isJsonObject(value) &&
|
|
140
|
+
declaration.value.kind !== 'object' &&
|
|
141
|
+
declaration.value.kind !== 'record') {
|
|
142
|
+
report([name], 'override-not-allowed');
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
const result = validateFileValue(declaration.value, value, [name], false, unknownReason, report);
|
|
146
|
+
if (result.valid)
|
|
147
|
+
defineSettingsKey(snapshot.settings, name, result.value);
|
|
148
|
+
}
|
|
149
|
+
return snapshot;
|
|
150
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { basename, dirname, join } from 'node:path';
|
|
2
|
+
import { ensurePrivateDirectory } from '../fs/private-files.js';
|
|
3
|
+
import { withLock } from '../fs/with-lock.js';
|
|
4
|
+
import { applySettingsEdits, } from './edit.js';
|
|
5
|
+
import { SettingsFs } from './fs.js';
|
|
6
|
+
import { rethrowSettingsLockError } from './lock-errors.js';
|
|
7
|
+
import { parseSettings } from './parse.js';
|
|
8
|
+
import { SettingsRefusal } from './refusal.js';
|
|
9
|
+
import { writeSettings } from './write.js';
|
|
10
|
+
const SETTINGS_WRITE_LOCK = Object.freeze({
|
|
11
|
+
name: 'settings-write',
|
|
12
|
+
ttlMs: 10_000,
|
|
13
|
+
timeoutMs: 2_000,
|
|
14
|
+
renew: true,
|
|
15
|
+
});
|
|
16
|
+
function unavailable(file, cause) {
|
|
17
|
+
throw new SettingsRefusal('settings-unavailable', `Settings file is unavailable: ${file}`, { cause });
|
|
18
|
+
}
|
|
19
|
+
async function writeTarget(file) {
|
|
20
|
+
try {
|
|
21
|
+
return await SettingsFs.realpath(file);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
if (error.code !== 'ENOENT')
|
|
25
|
+
unavailable(file, error);
|
|
26
|
+
try {
|
|
27
|
+
await SettingsFs.lstat(file);
|
|
28
|
+
}
|
|
29
|
+
catch (lstatError) {
|
|
30
|
+
if (lstatError.code !== 'ENOENT')
|
|
31
|
+
unavailable(file, lstatError);
|
|
32
|
+
try {
|
|
33
|
+
return join(await SettingsFs.realpath(dirname(file)), basename(file));
|
|
34
|
+
}
|
|
35
|
+
catch (parentError) {
|
|
36
|
+
unavailable(file, parentError);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// A directory entry whose realpath is missing is a dangling symlink.
|
|
40
|
+
unavailable(file, error);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Lock the canonical user file so symlink aliases serialize the same edits. */
|
|
44
|
+
export async function updateSettings(definition, options, edits, context) {
|
|
45
|
+
if (options.separateInstall) {
|
|
46
|
+
throw new SettingsRefusal('settings-unavailable', 'Settings are unavailable for a separate install');
|
|
47
|
+
}
|
|
48
|
+
const folder = dirname(options.file);
|
|
49
|
+
try {
|
|
50
|
+
if (!options.createMissing)
|
|
51
|
+
await SettingsFs.stat(folder);
|
|
52
|
+
else if (process.platform === 'win32')
|
|
53
|
+
await ensurePrivateDirectory(folder);
|
|
54
|
+
else
|
|
55
|
+
await SettingsFs.mkdir(folder, { recursive: true, mode: 0o700 });
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
unavailable(options.file, error);
|
|
59
|
+
}
|
|
60
|
+
const target = await writeTarget(options.file);
|
|
61
|
+
let settled;
|
|
62
|
+
try {
|
|
63
|
+
return await withLock(target, {
|
|
64
|
+
...SETTINGS_WRITE_LOCK,
|
|
65
|
+
timeoutMs: options.lockTimeoutMs ?? SETTINGS_WRITE_LOCK.timeoutMs,
|
|
66
|
+
}, async (lock) => {
|
|
67
|
+
try {
|
|
68
|
+
if ((await writeTarget(options.file)) !== target) {
|
|
69
|
+
throw new SettingsRefusal('settings-busy', `Settings target changed while locking: ${options.file}`);
|
|
70
|
+
}
|
|
71
|
+
let source;
|
|
72
|
+
let existingMode;
|
|
73
|
+
let missing = false;
|
|
74
|
+
try {
|
|
75
|
+
source = await SettingsFs.readFile(target, 'utf8');
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error.code !== 'ENOENT' ||
|
|
79
|
+
!options.createMissing)
|
|
80
|
+
unavailable(target, error);
|
|
81
|
+
missing = true;
|
|
82
|
+
source = '';
|
|
83
|
+
}
|
|
84
|
+
if (!missing) {
|
|
85
|
+
try {
|
|
86
|
+
existingMode = (await SettingsFs.stat(target)).mode;
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
unavailable(target, error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const parsed = parseSettings(source);
|
|
93
|
+
if (!parsed.parseable || parsed.hasDuplicates) {
|
|
94
|
+
throw new SettingsRefusal('settings-unparseable', 'Settings must be an object without duplicate keys');
|
|
95
|
+
}
|
|
96
|
+
const version = parsed.value.schema_version;
|
|
97
|
+
if (Number.isInteger(version) && version > 1) {
|
|
98
|
+
throw new SettingsRefusal('settings-newer-version', 'Settings were written by a newer version');
|
|
99
|
+
}
|
|
100
|
+
if (!parsed.tree) {
|
|
101
|
+
source = `${parsed.hasBom ? '\uFEFF' : ''}// ${definition.plugin} settings\n{\n "schema_version": 1\n}\n`;
|
|
102
|
+
}
|
|
103
|
+
const edited = applySettingsEdits(definition, source, edits, context);
|
|
104
|
+
const result = edited.outcome === 'unchanged'
|
|
105
|
+
? { outcome: 'unchanged' }
|
|
106
|
+
: await writeSettings({
|
|
107
|
+
target,
|
|
108
|
+
text: edited.text,
|
|
109
|
+
hasBom: edited.hasBom,
|
|
110
|
+
existingMode,
|
|
111
|
+
lock,
|
|
112
|
+
logger: options.log,
|
|
113
|
+
});
|
|
114
|
+
settled = { outcome: 'fulfilled', result };
|
|
115
|
+
return result;
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
settled = { outcome: 'rejected', error };
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
if (!settled)
|
|
125
|
+
rethrowSettingsLockError(error, target, SETTINGS_WRITE_LOCK.name);
|
|
126
|
+
// Release runs after the callback; it cannot undo a settings write or refusal.
|
|
127
|
+
if (settled.outcome === 'fulfilled' || error !== settled.error) {
|
|
128
|
+
try {
|
|
129
|
+
options.log.warn('Settings lock release failed', { target, error });
|
|
130
|
+
}
|
|
131
|
+
catch {
|
|
132
|
+
// A logger failure must not replace the callback's own outcome either.
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (settled.outcome === 'rejected')
|
|
136
|
+
throw settled.error;
|
|
137
|
+
return settled.result;
|
|
138
|
+
}
|
|
139
|
+
}
|