diff --git a/.changeset/cli-bootstrap-keeps-legacy-settings.md b/.changeset/cli-bootstrap-keeps-legacy-settings.md new file mode 100644 index 000000000..90c2de4a5 --- /dev/null +++ b/.changeset/cli-bootstrap-keeps-legacy-settings.md @@ -0,0 +1,7 @@ +--- +'nostream': patch +--- + +fix(cli): do not shadow settings.json with the defaults + +`nostream start` and `nostream setup` copy the default settings to `.nostr/settings.yaml` when that file is missing. The relay reads `settings.yaml` first and falls back to the deprecated `settings.json` only when there is no yaml, so on a deployment that still had only a `settings.json` the copy replaced every override in it with the defaults: an admission policy such as `nip43.enabled`, payment settings and limits stopped applying, silently. The defaults are now copied only when neither file exists, so a `settings.json` keeps working until the admin console or `nostream config` writes a `settings.yaml`. diff --git a/src/cli/utils/bootstrap.ts b/src/cli/utils/bootstrap.ts index 3fce9904a..d92f1944e 100644 --- a/src/cli/utils/bootstrap.ts +++ b/src/cli/utils/bootstrap.ts @@ -2,7 +2,13 @@ import fs from 'fs' import { homedir } from 'os' import { join } from 'path' -import { getConfigBaseDir, getDefaultSettingsFilePath, getProjectPath, getSettingsFilePath } from './paths' +import { + getConfigBaseDir, + getDefaultSettingsFilePath, + getLegacySettingsFilePath, + getProjectPath, + getSettingsFilePath, +} from './paths' export const ensureNotRoot = (): void => { if (typeof process.geteuid === 'function' && process.geteuid() === 0) { @@ -19,7 +25,11 @@ export const ensureConfigBootstrap = (): void => { fs.mkdirSync(configDir, { recursive: true }) } - if (!fs.existsSync(settingsFile)) { + // settings.yaml takes precedence over the deprecated settings.json, so writing the defaults next to an existing + // settings.json would shadow every override in it (an admission policy, payment settings, limits) without a word. + // A deployment that still has only settings.json keeps working on it, and the admin console writes settings.yaml + // itself on the first save. + if (!fs.existsSync(settingsFile) && !fs.existsSync(getLegacySettingsFilePath())) { fs.copyFileSync(defaultsFile, settingsFile) } } diff --git a/src/cli/utils/paths.ts b/src/cli/utils/paths.ts index d44432b02..75e5c7c88 100644 --- a/src/cli/utils/paths.ts +++ b/src/cli/utils/paths.ts @@ -5,11 +5,12 @@ import { getDefaultSettingsFilePath, getSettingsFilePath, } from '../../utils/settings-config' +import { getLegacySettingsFilePath } from '../../utils/settings-paths' export const getProjectRoot = (): string => process.cwd() export const getProjectPath = (...parts: string[]): string => join(getProjectRoot(), ...parts) -export { getConfigBaseDir, getDefaultSettingsFilePath, getSettingsFilePath } +export { getConfigBaseDir, getDefaultSettingsFilePath, getLegacySettingsFilePath, getSettingsFilePath } export const getEnvFilePath = (): string => getProjectPath('.env') diff --git a/test/unit/cli/bootstrap.spec.ts b/test/unit/cli/bootstrap.spec.ts new file mode 100644 index 000000000..44d5553b2 --- /dev/null +++ b/test/unit/cli/bootstrap.spec.ts @@ -0,0 +1,78 @@ +import { expect } from 'chai' +import fs from 'fs' +import { tmpdir } from 'os' +import { join } from 'path' + +import { ensureConfigBootstrap } from '../../../src/cli/utils/bootstrap' +import { getDefaultSettingsFilePath } from '../../../src/cli/utils/paths' + +describe('ensureConfigBootstrap', () => { + let root: string + let configDir: string + let previousConfigDir: string | undefined + + beforeEach(() => { + root = fs.mkdtempSync(join(tmpdir(), 'nostream-bootstrap-')) + configDir = join(root, '.nostr') + previousConfigDir = process.env.NOSTR_CONFIG_DIR + process.env.NOSTR_CONFIG_DIR = configDir + }) + + afterEach(() => { + if (previousConfigDir === undefined) { + delete process.env.NOSTR_CONFIG_DIR + } else { + process.env.NOSTR_CONFIG_DIR = previousConfigDir + } + fs.rmSync(root, { recursive: true, force: true }) + }) + + it('creates the config directory and copies the defaults when there are no settings', () => { + ensureConfigBootstrap() + + expect(fs.readFileSync(join(configDir, 'settings.yaml'), 'utf-8')).to.equal( + fs.readFileSync(getDefaultSettingsFilePath(), 'utf-8'), + ) + }) + + it('leaves an existing settings.yaml alone', () => { + fs.mkdirSync(configDir, { recursive: true }) + fs.writeFileSync(join(configDir, 'settings.yaml'), 'info:\n name: mine\n') + + ensureConfigBootstrap() + + expect(fs.readFileSync(join(configDir, 'settings.yaml'), 'utf-8')).to.equal('info:\n name: mine\n') + }) + + it('does not write the defaults next to a settings.json, which they would shadow', () => { + fs.mkdirSync(configDir, { recursive: true }) + const legacy = JSON.stringify({ nip43: { enabled: true } }) + fs.writeFileSync(join(configDir, 'settings.json'), legacy) + + ensureConfigBootstrap() + + expect(fs.existsSync(join(configDir, 'settings.yaml'))).to.equal(false) + expect(fs.readFileSync(join(configDir, 'settings.json'), 'utf-8')).to.equal(legacy) + }) + + it('keeps the settings.json overrides in effect after bootstrapping', () => { + fs.mkdirSync(configDir, { recursive: true }) + fs.writeFileSync(join(configDir, 'settings.json'), JSON.stringify({ nip43: { enabled: true } })) + + ensureConfigBootstrap() + + // eslint-disable-next-line @typescript-eslint/no-var-requires + const { loadMergedSettings } = require('../../../src/utils/settings-config') + expect(loadMergedSettings().nip43?.enabled).to.equal(true) + }) + + it('leaves settings.yaml alone when a settings.json is there too', () => { + fs.mkdirSync(configDir, { recursive: true }) + fs.writeFileSync(join(configDir, 'settings.yaml'), 'info:\n name: mine\n') + fs.writeFileSync(join(configDir, 'settings.json'), '{}') + + ensureConfigBootstrap() + + expect(fs.readFileSync(join(configDir, 'settings.yaml'), 'utf-8')).to.equal('info:\n name: mine\n') + }) +})