更新 lib/store.js
This commit is contained in:
parent
6806a5bcf2
commit
9c4872f12a
36
lib/store.js
36
lib/store.js
|
|
@ -11,10 +11,10 @@
|
||||||
* The file holds the two bearer tokens and the UI-editable overrides. It is
|
* The file holds the two bearer tokens and the UI-editable overrides. It is
|
||||||
* NEVER uploaded, never printed, and `describe()` only exposes a mask.
|
* NEVER uploaded, never printed, and `describe()` only exposes a mask.
|
||||||
*/
|
*/
|
||||||
import { copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, chmodSync } from 'node:fs';
|
import { existsSync, copyFileSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, chmodSync } from 'node:fs';
|
||||||
import { dirname, resolve } from 'node:path';
|
import { dirname, resolve } from 'node:path';
|
||||||
import { homedir } from 'node:os';
|
import { homedir } from 'node:os';
|
||||||
import { DEFAULT_SETTINGS, normalizeBaseUrl, settingsFromConfig } from "./config.js";
|
import { DEFAULT_SETTINGS, isPlaceholderBaseUrl, normalizeBaseUrl, originOf, settingsFromConfig } from "./config.js";
|
||||||
/** `$DSH_HOME` (or `~/.dsh`) — the same resolution DSH core uses. */
|
/** `$DSH_HOME` (or `~/.dsh`) — the same resolution DSH core uses. */
|
||||||
export function dshHome() {
|
export function dshHome() {
|
||||||
const fromEnv = process.env.DSH_HOME;
|
const fromEnv = process.env.DSH_HOME;
|
||||||
|
|
@ -26,7 +26,7 @@ export function dshHome() {
|
||||||
export function settingsPath() {
|
export function settingsPath() {
|
||||||
return resolve(dshHome(), 'dsh-brain.json');
|
return resolve(dshHome(), 'dsh-brain.json');
|
||||||
}
|
}
|
||||||
/** Pre-rename (<=0.4.x) settings sidecar name. */
|
/** Pre-rename (<=0.7.x) settings sidecar name. */
|
||||||
export function legacySettingsPath() {
|
export function legacySettingsPath() {
|
||||||
return resolve(dshHome(), 'cloud-memory.json');
|
return resolve(dshHome(), 'cloud-memory.json');
|
||||||
}
|
}
|
||||||
|
|
@ -202,6 +202,8 @@ export class SettingsStore {
|
||||||
memoryToken: { configured: memory !== '', masked: memory === '' ? '' : maskToken(memory) },
|
memoryToken: { configured: memory !== '', masked: memory === '' ? '' : maskToken(memory) },
|
||||||
summaryToken: { configured: summary !== '', masked: summary === '' ? '' : maskToken(summary) },
|
summaryToken: { configured: summary !== '', masked: summary === '' ? '' : maskToken(summary) },
|
||||||
statePath: this.file,
|
statePath: this.file,
|
||||||
|
baseUrlConfigured: !isPlaceholderBaseUrl(settings.baseUrl),
|
||||||
|
baseUrlOrigin: originOf(settings.baseUrl),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
@ -213,6 +215,9 @@ export class SettingsStore {
|
||||||
*/
|
*/
|
||||||
patch(patch) {
|
patch(patch) {
|
||||||
const next = { ...this.state };
|
const next = { ...this.state };
|
||||||
|
const beforeOrigin = originOf(this.settings().baseUrl);
|
||||||
|
/** Tokens this very request supplied; they survive an address change. */
|
||||||
|
const provided = [];
|
||||||
if (patch.enabled !== undefined) {
|
if (patch.enabled !== undefined) {
|
||||||
if (typeof patch.enabled !== 'boolean')
|
if (typeof patch.enabled !== 'boolean')
|
||||||
return { ok: false, error: 'enabled must be a boolean' };
|
return { ok: false, error: 'enabled must be a boolean' };
|
||||||
|
|
@ -235,12 +240,14 @@ export class SettingsStore {
|
||||||
if (!activityToken.startsWith('cmt_'))
|
if (!activityToken.startsWith('cmt_'))
|
||||||
return { ok: false, error: 'activity token must start with cmt_' };
|
return { ok: false, error: 'activity token must start with cmt_' };
|
||||||
next.activityToken = activityToken;
|
next.activityToken = activityToken;
|
||||||
|
provided.push('activity');
|
||||||
}
|
}
|
||||||
const memoryToken = cleanString(patch.memoryToken);
|
const memoryToken = cleanString(patch.memoryToken);
|
||||||
if (memoryToken !== undefined) {
|
if (memoryToken !== undefined) {
|
||||||
if (!memoryToken.startsWith('cmt_'))
|
if (!memoryToken.startsWith('cmt_'))
|
||||||
return { ok: false, error: 'memory token must start with cmt_' };
|
return { ok: false, error: 'memory token must start with cmt_' };
|
||||||
next.memoryToken = memoryToken;
|
next.memoryToken = memoryToken;
|
||||||
|
provided.push('memory');
|
||||||
}
|
}
|
||||||
if (patch.requestTimeoutMs !== undefined) {
|
if (patch.requestTimeoutMs !== undefined) {
|
||||||
const requestTimeoutMs = clampInt(patch.requestTimeoutMs, 1000, 60_000);
|
const requestTimeoutMs = clampInt(patch.requestTimeoutMs, 1000, 60_000);
|
||||||
|
|
@ -270,6 +277,7 @@ export class SettingsStore {
|
||||||
if (!summaryToken.startsWith('cmt_'))
|
if (!summaryToken.startsWith('cmt_'))
|
||||||
return { ok: false, error: 'summary token must start with cmt_' };
|
return { ok: false, error: 'summary token must start with cmt_' };
|
||||||
next.summaryToken = summaryToken;
|
next.summaryToken = summaryToken;
|
||||||
|
provided.push('summary');
|
||||||
}
|
}
|
||||||
if (patch.clearSummaryToken === true)
|
if (patch.clearSummaryToken === true)
|
||||||
delete next.summaryToken;
|
delete next.summaryToken;
|
||||||
|
|
@ -277,9 +285,29 @@ export class SettingsStore {
|
||||||
delete next.activityToken;
|
delete next.activityToken;
|
||||||
if (patch.clearMemoryToken === true)
|
if (patch.clearMemoryToken === true)
|
||||||
delete next.memoryToken;
|
delete next.memoryToken;
|
||||||
|
// Switching the service origin must not hand the previous service's
|
||||||
|
// bearer tokens to the new one: everything this request did not explicitly
|
||||||
|
// replace is dropped, and the caller is told so it can ask to re-authenticate.
|
||||||
|
const afterOrigin = originOf(next.baseUrl ?? this.configDefaults.baseUrl);
|
||||||
|
const clearedTokens = [];
|
||||||
|
if (beforeOrigin !== '' && afterOrigin !== '' && beforeOrigin !== afterOrigin) {
|
||||||
|
const credentials = [
|
||||||
|
['activity', 'activityToken'],
|
||||||
|
['memory', 'memoryToken'],
|
||||||
|
['summary', 'summaryToken'],
|
||||||
|
];
|
||||||
|
for (const [label, key] of credentials) {
|
||||||
|
if (next[key] === undefined)
|
||||||
|
continue;
|
||||||
|
if (provided.includes(label))
|
||||||
|
continue;
|
||||||
|
delete next[key];
|
||||||
|
clearedTokens.push(label);
|
||||||
|
}
|
||||||
|
}
|
||||||
this.write(next);
|
this.write(next);
|
||||||
this.state = next;
|
this.state = next;
|
||||||
return { ok: true };
|
return clearedTokens.length === 0 ? { ok: true } : { ok: true, clearedTokens };
|
||||||
}
|
}
|
||||||
/** File size in bytes, for a "sidecar exists" hint in the card. */
|
/** File size in bytes, for a "sidecar exists" hint in the card. */
|
||||||
size() {
|
size() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue