ditto/src/config.ts

234 lines
6.2 KiB
TypeScript
Raw Normal View History

2024-04-19 03:00:02 +00:00
import url from 'node:url';
2024-04-23 20:03:20 +00:00
import { z } from 'zod';
2024-04-19 03:00:02 +00:00
2024-04-23 20:03:20 +00:00
import { dotenv, getPublicKey, nip19 } from '@/deps.ts';
/** Load environment config from `.env` */
await dotenv.load({
export: true,
defaultsPath: null,
examplePath: null,
});
2023-07-14 03:00:27 +00:00
/** Application-wide configuration. */
2024-03-31 03:56:09 +00:00
class Conf {
/** Ditto admin secret key in nip19 format. This is the way it's configured by an admin. */
2024-03-31 03:56:09 +00:00
static get nsec() {
2023-07-14 03:00:27 +00:00
const value = Deno.env.get('DITTO_NSEC');
if (!value) {
throw new Error('Missing DITTO_NSEC');
}
if (!value.startsWith('nsec1')) {
throw new Error('Invalid DITTO_NSEC');
}
return value as `nsec1${string}`;
2024-03-31 03:56:09 +00:00
}
/** Ditto admin secret key in hex format. */
2024-03-31 03:56:09 +00:00
static get seckey() {
return nip19.decode(Conf.nsec).data;
2024-03-31 03:56:09 +00:00
}
/** Ditto admin public key in hex format. */
2024-03-31 03:56:09 +00:00
static pubkey = getPublicKey(Conf.seckey);
/** Ditto admin secret key as a Web Crypto key. */
2024-03-31 03:56:09 +00:00
static get cryptoKey() {
2023-07-14 03:00:27 +00:00
return crypto.subtle.importKey(
'raw',
2024-02-12 17:40:17 +00:00
Conf.seckey,
2023-07-14 03:00:27 +00:00
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign', 'verify'],
);
2024-03-31 03:56:09 +00:00
}
static get port() {
return parseInt(Deno.env.get('PORT') || '8000');
}
2024-03-31 03:56:09 +00:00
static get relay(): `wss://${string}` | `ws://${string}` {
const { protocol, host } = Conf.url;
return `${protocol === 'https:' ? 'wss:' : 'ws:'}//${host}/relay`;
2024-03-31 03:56:09 +00:00
}
2024-01-04 06:07:45 +00:00
/** Relay to use for NIP-50 `search` queries. */
2024-03-31 03:56:09 +00:00
static get searchRelay() {
2024-01-04 06:07:45 +00:00
return Deno.env.get('SEARCH_RELAY');
2024-03-31 03:56:09 +00:00
}
/** Origin of the Ditto server, including the protocol and port. */
2024-03-31 03:56:09 +00:00
static get localDomain() {
return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
2024-03-31 03:56:09 +00:00
}
2024-04-07 00:36:12 +00:00
/** URL to an external Nostr viewer. */
static get externalDomain() {
return Deno.env.get('NOSTR_EXTERNAL') || Conf.localDomain;
}
/** Path to the main SQLite database which stores users, events, and more. */
2024-03-31 03:56:09 +00:00
static get dbPath() {
2024-04-19 03:00:02 +00:00
if (Deno.env.get('DATABASE_URL') === 'sqlite://:memory:') {
return ':memory:';
}
const { host, pathname } = Conf.databaseUrl;
if (!pathname) return '';
// Get relative path.
if (host === '') {
return pathname;
} else if (host === '.') {
return pathname;
} else if (host) {
return host + pathname;
}
return '';
}
/**
* Heroku-style database URL. This is used in production to connect to the
* database.
*
* Follows the format:
*
* ```txt
* protocol://username:password@host:port/database_name
* ```
*/
static get databaseUrl(): url.UrlWithStringQuery {
return url.parse(Deno.env.get('DATABASE_URL') ?? 'sqlite://data/db.sqlite3');
2024-03-31 03:56:09 +00:00
}
/** Character limit to enforce for posts made through Mastodon API. */
2024-03-31 03:56:09 +00:00
static get postCharLimit() {
return Number(Deno.env.get('POST_CHAR_LIMIT') || 5000);
2024-03-31 03:56:09 +00:00
}
2023-09-06 22:55:46 +00:00
/** S3 media storage configuration. */
2024-03-31 03:56:09 +00:00
static s3 = {
2023-09-06 22:55:46 +00:00
get endPoint() {
return Deno.env.get('S3_ENDPOINT')!;
},
get region() {
return Deno.env.get('S3_REGION')!;
},
get accessKey() {
return Deno.env.get('S3_ACCESS_KEY');
},
get secretKey() {
return Deno.env.get('S3_SECRET_KEY');
},
get bucket() {
return Deno.env.get('S3_BUCKET');
},
get pathStyle() {
return optionalBooleanSchema.parse(Deno.env.get('S3_PATH_STYLE'));
},
get port() {
return optionalNumberSchema.parse(Deno.env.get('S3_PORT'));
},
get sessionToken() {
return Deno.env.get('S3_SESSION_TOKEN');
},
get useSSL() {
return optionalBooleanSchema.parse(Deno.env.get('S3_USE_SSL'));
},
2024-03-31 03:56:09 +00:00
};
/** IPFS uploader configuration. */
2024-03-31 03:56:09 +00:00
static ipfs = {
/** Base URL for private IPFS API calls. */
get apiUrl() {
return Deno.env.get('IPFS_API_URL') || 'http://localhost:5001';
},
2024-03-31 03:56:09 +00:00
};
/** Module to upload files with. */
2024-03-31 03:56:09 +00:00
static get uploader() {
return Deno.env.get('DITTO_UPLOADER');
2024-03-31 03:56:09 +00:00
}
/** Media base URL for uploads. */
2024-03-31 03:56:09 +00:00
static get mediaDomain() {
const value = Deno.env.get('MEDIA_DOMAIN');
if (!value) {
const url = Conf.url;
url.host = `media.${url.host}`;
return url.toString();
}
return value;
2024-03-31 03:56:09 +00:00
}
/** Max upload size for files in number of bytes. Default 100MiB. */
2024-03-31 03:56:09 +00:00
static get maxUploadSize() {
return Number(Deno.env.get('MAX_UPLOAD_SIZE') || 100 * 1024 * 1024);
2024-03-31 03:56:09 +00:00
}
/** Usernames that regular users cannot sign up with. */
2024-03-31 03:56:09 +00:00
static get forbiddenUsernames() {
return Deno.env.get('FORBIDDEN_USERNAMES')?.split(',') || [
'_',
'admin',
'administrator',
'root',
'sysadmin',
'system',
];
2024-03-31 03:56:09 +00:00
}
/** Proof-of-work configuration. */
2024-03-31 03:56:09 +00:00
static pow = {
get registrations() {
return Number(Deno.env.get('DITTO_POW_REGISTRATIONS') ?? 20);
},
2024-03-31 03:56:09 +00:00
};
/** Domain of the Ditto server as a `URL` object, for easily grabbing the `hostname`, etc. */
2024-03-31 03:56:09 +00:00
static get url() {
return new URL(Conf.localDomain);
2024-03-31 03:56:09 +00:00
}
/** Merges the path with the localDomain. */
2024-03-31 03:56:09 +00:00
static local(path: string): string {
return mergePaths(Conf.localDomain, path);
2024-03-31 03:56:09 +00:00
}
2024-04-07 00:36:12 +00:00
/** Get an external URL for the NIP-19 identifier. */
static external(nip19: string): string {
return new URL(`/${nip19}`, Conf.externalDomain).toString();
}
2023-10-12 04:03:56 +00:00
/** URL to send Sentry errors to. */
2024-03-31 03:56:09 +00:00
static get sentryDsn() {
2023-10-05 21:08:12 +00:00
return Deno.env.get('SENTRY_DSN');
2024-03-31 03:56:09 +00:00
}
2023-10-12 04:03:56 +00:00
/** SQLite settings. */
2024-03-31 03:56:09 +00:00
static sqlite = {
2023-10-12 04:03:56 +00:00
/**
* Number of bytes to use for memory-mapped IO.
* https://www.sqlite.org/pragma.html#pragma_mmap_size
*/
get mmapSize(): number {
const value = Deno.env.get('SQLITE_MMAP_SIZE');
if (value) {
return Number(value);
} else {
return 1024 * 1024 * 1024;
}
},
2024-03-31 03:56:09 +00:00
};
}
2023-03-05 04:10:56 +00:00
const optionalBooleanSchema = z
.enum(['true', 'false'])
.optional()
.transform((value) => value !== undefined ? value === 'true' : undefined);
const optionalNumberSchema = z
.string()
.optional()
.transform((value) => value !== undefined ? Number(value) : undefined);
function mergePaths(base: string, path: string) {
const url = new URL(
path.startsWith('/') ? path : new URL(path).pathname,
base,
);
if (!path.startsWith('/')) {
// Copy query parameters from the original URL to the new URL
const originalUrl = new URL(path);
url.search = originalUrl.search;
}
return url.toString();
}
export { Conf };