-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
294 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { ISettings } from '../@types/settings' | ||
import { SettingsStatic } from '../utils/settings' | ||
|
||
export const createSettings = (): ISettings => SettingsStatic.createSettings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,99 @@ | ||
import { existsSync, readFileSync, writeFileSync } from 'fs' | ||
import fs from 'fs' | ||
import { homedir } from 'os' | ||
import { join } from 'path' | ||
import { mergeDeepRight } from 'ramda' | ||
|
||
import { ISettings } from '../@types/settings' | ||
import packageJson from '../../package.json' | ||
|
||
export const getSettingsFilePath = (filename = 'settings.json'): string => join( | ||
process.env.NOSTR_CONFIG_DIR ?? join(homedir(), '.nostr'), | ||
filename, | ||
) | ||
export class SettingsStatic { | ||
static _settings: ISettings | ||
|
||
let _settings: ISettings | ||
public static getSettingsFilePath(filename = 'settings.json') { | ||
return join( | ||
process.env.NOSTR_CONFIG_DIR ?? join(homedir(), '.nostr'), | ||
filename | ||
) | ||
} | ||
|
||
export const getDefaultSettings = (): ISettings => ({ | ||
info: { | ||
relay_url: `wss://${packageJson.name}.your-domain.com`, | ||
name: `${packageJson.name}.your-domain.com`, | ||
description: packageJson.description, | ||
pubkey: '', | ||
contact: '[email protected]', | ||
}, | ||
limits: { | ||
event: { | ||
eventId: { | ||
minLeadingZeroBits: 0, | ||
}, | ||
kind: { | ||
whitelist: [], | ||
blacklist: [], | ||
}, | ||
pubkey: { | ||
minLeadingZeroBits: 0, | ||
whitelist: [], | ||
blacklist: [], | ||
}, | ||
createdAt: { | ||
maxPositiveDelta: 900, // +15 min | ||
maxNegativeDelta: 0, // disabled | ||
public static getDefaultSettings(): ISettings { | ||
return { | ||
info: { | ||
relay_url: 'wss://nostr-ts-relay.your-domain.com', | ||
name: `${packageJson.name}.your-domain.com`, | ||
description: packageJson.description, | ||
pubkey: 'replace-with-your-pubkey', | ||
contact: '[email protected]', | ||
}, | ||
}, | ||
client: { | ||
subscription: { | ||
maxSubscriptions: 10, | ||
maxFilters: 10, | ||
limits: { | ||
event: { | ||
eventId: { | ||
minLeadingZeroBits: 0, | ||
}, | ||
kind: { | ||
whitelist: [], | ||
blacklist: [], | ||
}, | ||
pubkey: { | ||
minLeadingZeroBits: 0, | ||
whitelist: [], | ||
blacklist: [], | ||
}, | ||
createdAt: { | ||
maxPositiveDelta: 900, | ||
maxNegativeDelta: 0, // disabled | ||
}, | ||
}, | ||
client: { | ||
subscription: { | ||
maxSubscriptions: 10, | ||
maxFilters: 10, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const loadSettings = (path: string) => { | ||
return JSON.parse( | ||
readFileSync( | ||
path, | ||
{ encoding: 'utf-8' }, | ||
), | ||
) | ||
} | ||
|
||
const createSettings = (): ISettings => { | ||
const path = getSettingsFilePath() | ||
const defaults = getDefaultSettings() | ||
try { | ||
if (_settings) { | ||
return _settings | ||
} | ||
} | ||
|
||
public static loadSettings(path: string) { | ||
return JSON.parse( | ||
fs.readFileSync( | ||
path, | ||
{ encoding: 'utf-8' } | ||
) | ||
) | ||
} | ||
|
||
if (!existsSync(path)) { | ||
saveSettings(path, defaults) | ||
public static createSettings(): ISettings { | ||
if (SettingsStatic._settings) { | ||
return SettingsStatic._settings | ||
} | ||
const path = SettingsStatic.getSettingsFilePath() | ||
const defaults = SettingsStatic.getDefaultSettings() | ||
try { | ||
|
||
_settings = mergeDeepRight(defaults, loadSettings(path)) | ||
if (fs.existsSync(path)) { | ||
SettingsStatic._settings = mergeDeepRight( | ||
defaults, | ||
SettingsStatic.loadSettings(path) | ||
) | ||
} else { | ||
SettingsStatic.saveSettings(path, defaults) | ||
SettingsStatic._settings = mergeDeepRight({}, defaults) | ||
} | ||
|
||
return _settings | ||
} catch (error) { | ||
console.error('Unable to read config file. Reason: %s', error.message) | ||
return SettingsStatic._settings | ||
} catch (error) { | ||
console.error('Unable to read config file. Reason: %s', error.message) | ||
|
||
return defaults | ||
return defaults | ||
} | ||
} | ||
} | ||
|
||
export const saveSettings = (path: string, settings: ISettings) => { | ||
return writeFileSync( | ||
path, | ||
JSON.stringify(settings, null, 2), | ||
{ encoding: 'utf-8' } | ||
) | ||
public static saveSettings(path: string, settings: ISettings) { | ||
return fs.writeFileSync( | ||
path, | ||
JSON.stringify(settings, null, 2), | ||
{ encoding: 'utf-8' } | ||
) | ||
} | ||
} | ||
export const Settings = createSettings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect } from 'chai' | ||
import Sinon from 'sinon' | ||
|
||
import { createSettings } from '../../../src/factories/settings-factory' | ||
import { SettingsStatic } from '../../../src/utils/settings' | ||
|
||
describe('getSettings', () => { | ||
let createSettingsStub: Sinon.SinonStub | ||
|
||
beforeEach(() => { | ||
createSettingsStub = Sinon.stub(SettingsStatic, 'createSettings') | ||
}) | ||
|
||
afterEach(() => { | ||
createSettingsStub.restore() | ||
}) | ||
|
||
it('calls createSettings and returns', () => { | ||
const settings = Symbol() | ||
createSettingsStub.returns(settings) | ||
|
||
expect(createSettings()).to.equal(settings) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.