feat(runtime): override deploy settings from the environment
SNOWLUMA_ variables can set the factory OneBot listen ports, WebUI TLS, packet and hook timeouts, the stream directory, file download timeout, and notification HTTP timeout. The values apply in memory and are not written back to saved config.
This commit is contained in:
19 files changed
+342
-14
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { positiveIntEnv } from '@snowluma/common/env';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { FriendDressError } from '@snowluma/protocol/web/friend-dress';
|
||||
import { formatGroupRequestFlag } from '@snowluma/protocol/qq-info';
|
||||
@@ -21,7 +22,11 @@ import { groupInfoReturnsSchema } from './group-info';
|
||||
import { GROUP_MESSAGE_EVENT, hashMessageIdInt32 } from '../message-id';
|
||||
|
||||
const DOWNLOAD_FILE_MAX_BYTES = 1024 * 1024 * 1024; // 1 GiB
|
||||
const DOWNLOAD_FILE_TIMEOUT_MS = 60_000;
|
||||
const DEFAULT_DOWNLOAD_FILE_TIMEOUT_MS = 60_000;
|
||||
|
||||
function downloadFileTimeoutMs(): number {
|
||||
return positiveIntEnv('SNOWLUMA_DOWNLOAD_FILE_TIMEOUT_MS', DEFAULT_DOWNLOAD_FILE_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
const profileLikeUserSchema = {
|
||||
type: 'object',
|
||||
@@ -2592,7 +2597,7 @@ export const actions = [
|
||||
buf = Buffer.from(base64, 'base64');
|
||||
if (buf.length > DOWNLOAD_FILE_MAX_BYTES) return failedResponse(RETCODE.BAD_REQUEST, `base64 payload too large: ${buf.length} > ${DOWNLOAD_FILE_MAX_BYTES} bytes`);
|
||||
} else {
|
||||
buf = await fetchDownloadFile(url, parseDownloadHeaders(p.headers), DOWNLOAD_FILE_MAX_BYTES, DOWNLOAD_FILE_TIMEOUT_MS);
|
||||
buf = await fetchDownloadFile(url, parseDownloadHeaders(p.headers), DOWNLOAD_FILE_MAX_BYTES, downloadFileTimeoutMs());
|
||||
}
|
||||
try {
|
||||
const safe = await saveDownloadBuffer(buf, name);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { positiveIntEnv } from '@snowluma/common/env';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { isRealUin } from '@snowluma/common/uin';
|
||||
import { randomBytes } from 'crypto';
|
||||
@@ -110,7 +111,69 @@ export function loadOneBotConfig(uin: string, options: LoadOneBotConfigOptions =
|
||||
saveOneBotConfig(uin, config, { mode: globalRaw ? 'overlay' : 'snapshot' });
|
||||
}
|
||||
|
||||
return config;
|
||||
// Listen-port overrides stay in memory. Persisting them would bake a
|
||||
// machine-specific port into onebot_<uin>.json.
|
||||
return applyOneBotListenPortOverrides(config);
|
||||
}
|
||||
|
||||
const HTTP_DEFAULT_SERVER = 'http-default';
|
||||
const WS_DEFAULT_SERVER = 'ws-default';
|
||||
|
||||
/** Override only the factory listeners. Other adapters keep their saved ports. */
|
||||
export function applyOneBotListenPortOverrides(
|
||||
config: OneBotConfig,
|
||||
env: NodeJS.ProcessEnv = process.env,
|
||||
): OneBotConfig {
|
||||
const httpPort = readListenPort('SNOWLUMA_ONEBOT_HTTP_PORT', env);
|
||||
const wsPort = readListenPort('SNOWLUMA_ONEBOT_WS_PORT', env);
|
||||
if (httpPort === undefined && wsPort === undefined) return config;
|
||||
|
||||
const next = structuredClone(config);
|
||||
const httpApplied = httpPort !== undefined && assignFactoryPort(
|
||||
next.networks.httpServers,
|
||||
HTTP_DEFAULT_SERVER,
|
||||
httpPort,
|
||||
'SNOWLUMA_ONEBOT_HTTP_PORT',
|
||||
);
|
||||
const wsApplied = wsPort !== undefined && assignFactoryPort(
|
||||
next.networks.wsServers,
|
||||
WS_DEFAULT_SERVER,
|
||||
wsPort,
|
||||
'SNOWLUMA_ONEBOT_WS_PORT',
|
||||
);
|
||||
if (!httpApplied && !wsApplied) return config;
|
||||
try {
|
||||
assertValidOneBotConfig(next);
|
||||
} catch (error) {
|
||||
log.warn(
|
||||
'OneBot listen port override ignored: %s',
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
return config;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
function readListenPort(name: string, env: NodeJS.ProcessEnv): number | undefined {
|
||||
const raw = env[name];
|
||||
if (typeof raw !== 'string' || !raw.trim()) return undefined;
|
||||
const port = positiveIntEnv(name, 0, { max: 65535, env });
|
||||
return port > 0 ? port : undefined;
|
||||
}
|
||||
|
||||
function assignFactoryPort(
|
||||
servers: Array<{ name: string; port: number }>,
|
||||
name: string,
|
||||
port: number,
|
||||
envName: string,
|
||||
): boolean {
|
||||
const server = servers.find((entry) => entry.name === name);
|
||||
if (!server) {
|
||||
log.warn('%s is ignored; no %s listener', envName, name);
|
||||
return false;
|
||||
}
|
||||
server.port = port;
|
||||
return true;
|
||||
}
|
||||
|
||||
export interface SaveOneBotConfigOptions {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
|
||||
const log = createLogger('OneBot.Stream');
|
||||
|
||||
export interface StreamStorageSnapshot {
|
||||
totalBytes: number;
|
||||
fileCount: number;
|
||||
@@ -273,7 +276,23 @@ function resolveManagedRoot(input: string): string {
|
||||
return path.join(fs.realpathSync(parent), path.basename(absolute));
|
||||
}
|
||||
|
||||
export const streamStorage = new StreamStorage(path.join(os.tmpdir(), 'onebot-stream'));
|
||||
export function createStreamStorage(env: NodeJS.ProcessEnv = process.env): StreamStorage {
|
||||
const fallback = path.join(os.tmpdir(), 'onebot-stream');
|
||||
const raw = env.SNOWLUMA_STREAM_DIR?.trim();
|
||||
if (!raw) return new StreamStorage(fallback);
|
||||
try {
|
||||
return new StreamStorage(raw);
|
||||
} catch (error) {
|
||||
log.warn(
|
||||
'SNOWLUMA_STREAM_DIR=%j is ignored: %s',
|
||||
raw,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
return new StreamStorage(fallback);
|
||||
}
|
||||
}
|
||||
|
||||
export const streamStorage = createStreamStorage();
|
||||
export const STREAM_ROOT = streamStorage.root;
|
||||
export const STREAM_UPLOAD_DIR = streamStorage.uploadDir;
|
||||
export const STREAM_DOWNLOAD_DIR = streamStorage.downloadDir;
|
||||
|
||||
Reference in new issue
Block a user