@cortexkit/common-auth 0.44.0 → 0.46.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/dist/commands/builtins.d.ts +20 -1
- package/dist/commands/builtins.js +28 -12
- package/dist/commands/index.d.ts +1 -1
- package/dist/commands/menu.d.ts +6 -1
- package/dist/commands/menu.js +1 -0
- package/dist/fs/file-read-cache.d.ts +3 -0
- package/dist/fs/file-read-cache.js +9 -5
- package/dist/fs/refresh-file-lock.d.ts +11 -0
- package/dist/fs/refresh-file-lock.js +159 -11
- package/dist/fs/with-lock.d.ts +2 -0
- package/dist/fs/with-lock.js +1 -1
- package/dist/routing-accessor/index.d.ts +17 -0
- package/dist/routing-accessor/index.js +13 -0
- package/dist/settings/define.d.ts +35 -0
- package/dist/settings/define.js +52 -0
- package/dist/settings/descriptors.d.ts +97 -0
- package/dist/settings/descriptors.js +81 -0
- package/dist/settings/edit.d.ts +24 -0
- package/dist/settings/edit.js +229 -0
- package/dist/settings/fs.d.ts +6 -0
- package/dist/settings/fs.js +19 -0
- package/dist/settings/handle.d.ts +30 -0
- package/dist/settings/handle.js +228 -0
- package/dist/settings/index.d.ts +10 -0
- package/dist/settings/index.js +7 -0
- package/dist/settings/lock-errors.d.ts +2 -0
- package/dist/settings/lock-errors.js +15 -0
- package/dist/settings/move.d.ts +76 -0
- package/dist/settings/move.js +351 -0
- package/dist/settings/parse.d.ts +24 -0
- package/dist/settings/parse.js +65 -0
- package/dist/settings/paths.d.ts +12 -0
- package/dist/settings/paths.js +48 -0
- package/dist/settings/refusal.d.ts +7 -0
- package/dist/settings/refusal.js +15 -0
- package/dist/settings/resolve.d.ts +15 -0
- package/dist/settings/resolve.js +51 -0
- package/dist/settings/schema.d.ts +29 -0
- package/dist/settings/schema.js +81 -0
- package/dist/settings/snapshot.d.ts +24 -0
- package/dist/settings/snapshot.js +150 -0
- package/dist/settings/update.d.ts +6 -0
- package/dist/settings/update.js +139 -0
- package/dist/settings/write.d.ts +26 -0
- package/dist/settings/write.js +88 -0
- package/dist/setup/command-error.d.ts +1 -1
- package/dist/setup/command-error.js +3 -2
- package/dist/setup/command.js +5 -1
- package/dist/setup/enroll.js +3 -3
- package/dist/setup/enrollment-access.d.ts +1 -1
- package/dist/setup/enrollment-access.js +13 -4
- package/dist/setup/types.d.ts +4 -0
- package/dist/tui-prefs/watcher.d.ts +3 -1
- package/dist/tui-prefs/watcher.js +41 -18
- package/package.json +5 -1
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { join, resolve } from 'node:path';
|
|
2
|
+
import { FileReadCache } from '../fs/file-read-cache.js';
|
|
3
|
+
import { watchSettings } from '../tui-prefs/watcher.js';
|
|
4
|
+
import { isJsonObject } from './descriptors.js';
|
|
5
|
+
import { SettingsFs } from './fs.js';
|
|
6
|
+
import { parseSettings } from './parse.js';
|
|
7
|
+
import { resolveSetting } from './resolve.js';
|
|
8
|
+
import { buildSettingsSnapshot, } from './snapshot.js';
|
|
9
|
+
import { updateSettings } from './update.js';
|
|
10
|
+
const reportedKey = Symbol.for('cortexkit.common-auth.settings.reported.v1');
|
|
11
|
+
function reportedEntries() {
|
|
12
|
+
let reported = Reflect.get(globalThis, reportedKey);
|
|
13
|
+
if (!reported) {
|
|
14
|
+
reported = new Set();
|
|
15
|
+
Reflect.set(globalThis, reportedKey, reported);
|
|
16
|
+
}
|
|
17
|
+
return reported;
|
|
18
|
+
}
|
|
19
|
+
/** Internal test hook; not part of the settings package exports. */
|
|
20
|
+
export function resetSettingsReportedForTest() {
|
|
21
|
+
reportedEntries().clear();
|
|
22
|
+
}
|
|
23
|
+
export function openSettings(definition, options) {
|
|
24
|
+
options = { createMissing: false, lockTimeoutMs: 2_000, ...options };
|
|
25
|
+
const userPath = resolve(options.file);
|
|
26
|
+
const cacheOptions = {
|
|
27
|
+
followSymlinks: true,
|
|
28
|
+
// Read SettingsFs at call time so setSettingsFsForTest reaches open handles.
|
|
29
|
+
stat: (path) => SettingsFs.stat(path),
|
|
30
|
+
lstat: (path) => SettingsFs.lstat(path),
|
|
31
|
+
};
|
|
32
|
+
const userCache = new FileReadCache(cacheOptions);
|
|
33
|
+
const projectCache = new FileReadCache(cacheOptions);
|
|
34
|
+
let user;
|
|
35
|
+
const projects = new Map();
|
|
36
|
+
// A project's report and resolution use only the source selected by its read.
|
|
37
|
+
const projectLevels = new Map();
|
|
38
|
+
const projectSequences = new Map();
|
|
39
|
+
let sequence = 0;
|
|
40
|
+
// Keep ordering metadata after eviction so an older in-flight read cannot win.
|
|
41
|
+
const committedSequences = new Map();
|
|
42
|
+
const projectPath = (project) => resolve(join(project, '.cortexkit', `${definition.plugin}.jsonc`));
|
|
43
|
+
// Windows reads every time (below), so remember each file's last bytes to
|
|
44
|
+
// tell a changed file from an unchanged one, as the cache does elsewhere.
|
|
45
|
+
const windows = process.platform === 'win32';
|
|
46
|
+
const windowsSeen = new Map();
|
|
47
|
+
const unreadFile = {
|
|
48
|
+
missing: Symbol('missing'),
|
|
49
|
+
unreadable: Symbol('unreadable'),
|
|
50
|
+
};
|
|
51
|
+
async function load(file, reader) {
|
|
52
|
+
const currentSequence = ++sequence;
|
|
53
|
+
const cache = file.kind === 'user' ? userCache : projectCache;
|
|
54
|
+
let loaded = false;
|
|
55
|
+
const seen = (path, content) => {
|
|
56
|
+
if (!windows) {
|
|
57
|
+
// The cache calls this reader only when the file changed.
|
|
58
|
+
loaded = true;
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
loaded = windowsSeen.get(path) !== content;
|
|
62
|
+
windowsSeen.set(path, content);
|
|
63
|
+
};
|
|
64
|
+
const readFile = async ([path]) => {
|
|
65
|
+
let text;
|
|
66
|
+
try {
|
|
67
|
+
text = await SettingsFs.readFile(path, 'utf8');
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
const status = error.code === 'ENOENT'
|
|
71
|
+
? 'missing'
|
|
72
|
+
: 'unreadable';
|
|
73
|
+
seen(path, unreadFile[status]);
|
|
74
|
+
return { status };
|
|
75
|
+
}
|
|
76
|
+
seen(path, text);
|
|
77
|
+
let parsed;
|
|
78
|
+
if (reader) {
|
|
79
|
+
let value;
|
|
80
|
+
try {
|
|
81
|
+
value = reader(text);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return { status: 'unreadable' };
|
|
85
|
+
}
|
|
86
|
+
if (!isJsonObject(value))
|
|
87
|
+
return { status: 'unparseable' };
|
|
88
|
+
// Legacy readers return an object keyed by setting name, not JSONC text.
|
|
89
|
+
parsed = {
|
|
90
|
+
parseable: true,
|
|
91
|
+
text,
|
|
92
|
+
hasBom: false,
|
|
93
|
+
tree: undefined,
|
|
94
|
+
value,
|
|
95
|
+
duplicateKeyPaths: [],
|
|
96
|
+
hasDuplicates: false,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
parsed = parseSettings(text);
|
|
101
|
+
}
|
|
102
|
+
return parsed.parseable
|
|
103
|
+
? {
|
|
104
|
+
status: 'snapshot',
|
|
105
|
+
snapshot: buildSettingsSnapshot(definition, file, parsed),
|
|
106
|
+
}
|
|
107
|
+
: { status: 'unparseable' };
|
|
108
|
+
};
|
|
109
|
+
// On Windows a quick same-size rewrite can keep the size and timestamps
|
|
110
|
+
// the cache compares, so read the file every time instead of using the cache.
|
|
111
|
+
const outcome = process.platform === 'win32'
|
|
112
|
+
? await readFile([file.path])
|
|
113
|
+
: await cache.read([file.path], readFile);
|
|
114
|
+
const key = JSON.stringify([file.kind, file.path]);
|
|
115
|
+
if (currentSequence <= (committedSequences.get(key) ?? 0))
|
|
116
|
+
return;
|
|
117
|
+
committedSequences.set(key, currentSequence);
|
|
118
|
+
const previous = file.kind === 'user' ? user : projects.get(file.path);
|
|
119
|
+
const snapshot = outcome.status === 'snapshot'
|
|
120
|
+
? outcome.snapshot
|
|
121
|
+
: outcome.status === 'missing'
|
|
122
|
+
? undefined
|
|
123
|
+
: previous?.snapshot;
|
|
124
|
+
const entries = [...(snapshot?.entries ?? [])];
|
|
125
|
+
if (outcome.status === 'unparseable' || outcome.status === 'unreadable') {
|
|
126
|
+
entries.unshift({
|
|
127
|
+
file: { ...file },
|
|
128
|
+
keyPath: [],
|
|
129
|
+
reason: outcome.status,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const state = { status: outcome.status, snapshot, entries };
|
|
133
|
+
if (file.kind === 'user') {
|
|
134
|
+
user = state;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
// Cache hits do not refresh insertion order; changed files do.
|
|
138
|
+
if (loaded)
|
|
139
|
+
projects.delete(file.path);
|
|
140
|
+
if (!projects.has(file.path) && projects.size >= 64) {
|
|
141
|
+
const oldest = projects.keys().next().value;
|
|
142
|
+
if (oldest !== undefined) {
|
|
143
|
+
projects.delete(oldest);
|
|
144
|
+
windowsSeen.delete(oldest);
|
|
145
|
+
for (const [project, selected] of projectLevels) {
|
|
146
|
+
if (selected === oldest)
|
|
147
|
+
projectLevels.delete(project);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
projects.set(file.path, state);
|
|
152
|
+
}
|
|
153
|
+
const reported = reportedEntries();
|
|
154
|
+
for (const entry of entries) {
|
|
155
|
+
const identity = JSON.stringify([
|
|
156
|
+
entry.file.path,
|
|
157
|
+
entry.keyPath,
|
|
158
|
+
entry.reason,
|
|
159
|
+
]);
|
|
160
|
+
if (reported.has(identity))
|
|
161
|
+
continue;
|
|
162
|
+
reported.add(identity);
|
|
163
|
+
options.log.warn(`Settings ${entry.reason}: ${entry.file.path} (${entry.keyPath.join('.') || '<root>'})`, structuredClone(entry));
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async function readProject(project) {
|
|
167
|
+
const path = projectPath(project);
|
|
168
|
+
const currentSequence = ++sequence;
|
|
169
|
+
await load({ kind: 'project', path });
|
|
170
|
+
if (currentSequence <= (projectSequences.get(path) ?? 0))
|
|
171
|
+
return;
|
|
172
|
+
let selected = path;
|
|
173
|
+
if (projects.get(path)?.status === 'missing') {
|
|
174
|
+
const source = options.legacyProject?.(project);
|
|
175
|
+
if (source) {
|
|
176
|
+
selected = resolve(source.path);
|
|
177
|
+
await load({ kind: 'legacy-project', path: selected }, (text) => source.read(text));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
// A slow legacy reader must not replace a newer read's project-file choice.
|
|
181
|
+
if (currentSequence <= (projectSequences.get(path) ?? 0))
|
|
182
|
+
return;
|
|
183
|
+
projectSequences.set(path, currentSequence);
|
|
184
|
+
projectLevels.set(path, selected);
|
|
185
|
+
}
|
|
186
|
+
const handle = {
|
|
187
|
+
async read({ project } = {}) {
|
|
188
|
+
if (options.separateInstall)
|
|
189
|
+
return;
|
|
190
|
+
await Promise.all([
|
|
191
|
+
load({ kind: 'user', path: userPath }),
|
|
192
|
+
...(project === undefined ? [] : [readProject(project)]),
|
|
193
|
+
]);
|
|
194
|
+
},
|
|
195
|
+
async get(name, getOptions) {
|
|
196
|
+
if (!Object.hasOwn(definition.settings, name)) {
|
|
197
|
+
throw new TypeError(`Undeclared setting: ${name}`);
|
|
198
|
+
}
|
|
199
|
+
await handle.read({ project: getOptions.project });
|
|
200
|
+
return resolveSetting(definition, name, getOptions, {
|
|
201
|
+
user: user?.snapshot,
|
|
202
|
+
project: getOptions.project === undefined
|
|
203
|
+
? undefined
|
|
204
|
+
: projects.get(projectLevels.get(projectPath(getOptions.project)) ?? '')?.snapshot,
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
report() {
|
|
208
|
+
return structuredClone([
|
|
209
|
+
...(user?.entries ?? []),
|
|
210
|
+
...Array.from(new Set(projectLevels.values())).flatMap((path) => projects.get(path)?.entries ?? []),
|
|
211
|
+
]);
|
|
212
|
+
},
|
|
213
|
+
update(edits, context) {
|
|
214
|
+
return updateSettings(definition, { ...options, file: userPath }, edits, context);
|
|
215
|
+
},
|
|
216
|
+
watch(onChange) {
|
|
217
|
+
if (options.separateInstall)
|
|
218
|
+
return () => { };
|
|
219
|
+
return watchSettings(userPath, onChange, {
|
|
220
|
+
fs: {
|
|
221
|
+
stat: (path) => SettingsFs.stat(path),
|
|
222
|
+
readFile: (path) => SettingsFs.readFile(path, 'utf8'),
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
return handle;
|
|
228
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { defineSettings, type SettingDeclaration, type SettingsDefinition, } from './define.js';
|
|
2
|
+
export { boolean, integer, list, number, object, oneOf, record, string, type ValueDescriptor, type ValueOf, } from './descriptors.js';
|
|
3
|
+
export type { SettingsEdit, UpdateResult } from './edit.js';
|
|
4
|
+
export { type LegacyProjectSource, type OpenSettingsOptions, openSettings, type SettingsHandle, } from './handle.js';
|
|
5
|
+
export { type LegacySource, type MapResult, type MoveOptions, type MoveOutcome, type MoveResult, type MoveSource, moveSettingsOnce, } from './move.js';
|
|
6
|
+
export { cortexkitConfigDir, isAbsoluteForPlatform, settingsFilePath, } from './paths.js';
|
|
7
|
+
export { SETTINGS_REFUSAL_CODES, SettingsRefusal, type SettingsRefusalCode, } from './refusal.js';
|
|
8
|
+
export type { GetOptions } from './resolve.js';
|
|
9
|
+
export { settingsJsonSchema } from './schema.js';
|
|
10
|
+
export type { ReportEntry, ReportReason } from './snapshot.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { defineSettings, } from './define.js';
|
|
2
|
+
export { boolean, integer, list, number, object, oneOf, record, string, } from './descriptors.js';
|
|
3
|
+
export { openSettings, } from './handle.js';
|
|
4
|
+
export { moveSettingsOnce, } from './move.js';
|
|
5
|
+
export { cortexkitConfigDir, isAbsoluteForPlatform, settingsFilePath, } from './paths.js';
|
|
6
|
+
export { SETTINGS_REFUSAL_CODES, SettingsRefusal, } from './refusal.js';
|
|
7
|
+
export { settingsJsonSchema } from './schema.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PrivateFileError } from '../fs/private-files.js';
|
|
2
|
+
import { isChangingLockRecord, LockContentionError } from '../fs/with-lock.js';
|
|
3
|
+
import { SettingsRefusal } from './refusal.js';
|
|
4
|
+
/** Classify against the failing lock, not the settings destination it protects. */
|
|
5
|
+
export function rethrowSettingsLockError(error, target, name) {
|
|
6
|
+
if (error instanceof LockContentionError ||
|
|
7
|
+
isChangingLockRecord(error, target, name)) {
|
|
8
|
+
throw new SettingsRefusal('settings-busy', `Settings lock is busy: ${target}`, { cause: error });
|
|
9
|
+
}
|
|
10
|
+
if (error instanceof PrivateFileError &&
|
|
11
|
+
(error.stage === 'safe-parent' || error.stage === 'directory')) {
|
|
12
|
+
throw new SettingsRefusal('settings-unavailable', `Settings lock folder is unavailable: ${target}`, { cause: error });
|
|
13
|
+
}
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { ChannelLogger } from '../logger/engine.js';
|
|
2
|
+
import { type PoolLockSpec } from '../store/refresh-lock.js';
|
|
3
|
+
import type { SettingsDefinition } from './define.js';
|
|
4
|
+
export interface MoveSource {
|
|
5
|
+
host: string;
|
|
6
|
+
path: string;
|
|
7
|
+
locks: PoolLockSpec[];
|
|
8
|
+
read(text: string): unknown;
|
|
9
|
+
}
|
|
10
|
+
export type LegacySource = {
|
|
11
|
+
host: string;
|
|
12
|
+
path: string;
|
|
13
|
+
} & ({
|
|
14
|
+
present: true;
|
|
15
|
+
value: unknown;
|
|
16
|
+
} | {
|
|
17
|
+
present: false;
|
|
18
|
+
});
|
|
19
|
+
export interface MapResult {
|
|
20
|
+
entries: {
|
|
21
|
+
source: number;
|
|
22
|
+
setting: string;
|
|
23
|
+
value: unknown;
|
|
24
|
+
}[];
|
|
25
|
+
unmapped: {
|
|
26
|
+
source: number;
|
|
27
|
+
key: string;
|
|
28
|
+
reason: string;
|
|
29
|
+
}[];
|
|
30
|
+
}
|
|
31
|
+
export interface MoveOptions {
|
|
32
|
+
definition: SettingsDefinition;
|
|
33
|
+
target: string;
|
|
34
|
+
separateInstall: boolean;
|
|
35
|
+
sources: MoveSource[];
|
|
36
|
+
/**
|
|
37
|
+
* Runs under the source locks. Do not acquire settings-move or any lock in
|
|
38
|
+
* sources[].locks, directly or through a store operation. A second acquisition
|
|
39
|
+
* waits for the first to release, so the move would wait on itself. Read live
|
|
40
|
+
* account records without locking.
|
|
41
|
+
*/
|
|
42
|
+
map(legacy: LegacySource[]): MapResult | Promise<MapResult>;
|
|
43
|
+
fence(): boolean | Promise<boolean>;
|
|
44
|
+
log: Pick<ChannelLogger, 'warn' | 'info'>;
|
|
45
|
+
}
|
|
46
|
+
export type MoveOutcome = 'moved' | 'done-already' | 'fence-closed' | 'aborted-target-appeared' | 'busy' | 'separate-install' | 'failed';
|
|
47
|
+
export interface MoveResult {
|
|
48
|
+
outcome: MoveOutcome;
|
|
49
|
+
unmapped: MapResult['unmapped'];
|
|
50
|
+
differing: {
|
|
51
|
+
setting: string;
|
|
52
|
+
values: {
|
|
53
|
+
source: number;
|
|
54
|
+
host: string;
|
|
55
|
+
value: unknown;
|
|
56
|
+
}[];
|
|
57
|
+
}[];
|
|
58
|
+
noteFailures: {
|
|
59
|
+
path: string;
|
|
60
|
+
error: unknown;
|
|
61
|
+
}[];
|
|
62
|
+
postPublishFailures: {
|
|
63
|
+
step: 'temp-unlink' | 'directory-sync' | 'lock-release';
|
|
64
|
+
error: unknown;
|
|
65
|
+
}[];
|
|
66
|
+
cause?: unknown;
|
|
67
|
+
}
|
|
68
|
+
type StepHook = (step: 'locked' | 'staged' | 'before-publish', paths: {
|
|
69
|
+
target: string;
|
|
70
|
+
temp?: string;
|
|
71
|
+
}) => void | Promise<void>;
|
|
72
|
+
/** Internal test hook, not part of the settings subpath's public exports. */
|
|
73
|
+
export declare function setSettingsMoveStepHookForTest(hook?: StepHook): void;
|
|
74
|
+
/** Move legacy settings without replacing an existing target or changing a source. */
|
|
75
|
+
export declare function moveSettingsOnce(options: MoveOptions): Promise<MoveResult>;
|
|
76
|
+
export {};
|