Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/cli-bootstrap-keeps-legacy-settings.md
Original file line number Diff line number Diff line change
@@ -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`.
14 changes: 12 additions & 2 deletions src/cli/utils/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/cli/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
78 changes: 78 additions & 0 deletions test/unit/cli/bootstrap.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
Loading