refactor(server): fold the ephemeral-token store and the in-app action registry (#1836)

Two small leftovers, moved to the domains that own them.

ephemeralTokens holds the short-lived tokens the WebSocket handshake and the
file-download links use. It already had startTokenCleanup/stopTokenCleanup with
an unref-ed handle — the right shape. What was missing is anyone calling stop:
index.ts kicked the sweep off with a bare require() after listen() and nothing
ever ended it. AuthService owns that lifecycle now, through
onModuleInit/onModuleDestroy, which is where it belongs — it is the service that
issues the tokens.

The store itself stays module state. websocket.ts consumes tokens from outside
the container, and a token store is only useful if the issuer and the consumer
share one. Same reasoning as the weather cache in #1835.

inAppNotificationActions is a 22-line handler registry with exactly one
consumer; it moves next to it as notifications/in-app-actions.ts.

services/ is down to 9 files, from 44.
This commit is contained in:
Maurice
2026-08-07 22:18:14 +02:00
committed by GitHub
parent a7d789fdd5
commit 5eab8128bd
16 changed files with 29 additions and 18 deletions
-2
View File
@@ -107,8 +107,6 @@ const onListen = () => {
scheduler.startTrekPhotoCacheCleanup();
scheduler.startPlacePhotoCacheCleanup();
scheduler.startAirTrailSync();
const { startTokenCleanup } = require('./services/ephemeralTokens');
startTokenCleanup();
import('./websocket').then(({ setupWebSocket }) => {
setupWebSocket(server);
});
+16 -3
View File
@@ -1,4 +1,4 @@
import { Injectable } from '@nestjs/common';
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import path from 'path';
@@ -16,7 +16,7 @@ import { getCountryFromCoords } from '../atlas/atlas-geo';
import { validatePassword } from '../common/passwordPolicy';
import { encryptMfaSecret, decryptMfaSecret } from '../common/crypto/mfaCrypto';
import { decrypt_api_key, maybe_encrypt_api_key, encrypt_api_key } from '../common/crypto/apiKeyCrypto';
import { createEphemeralToken } from '../../services/ephemeralTokens';
import { createEphemeralToken, startTokenCleanup, stopTokenCleanup } from './ephemeral-tokens';
// Import from sessionManager directly, NOT the ../../mcp barrel: the barrel pulls
// the whole tools fan-out (and via the domain bridges, the Nest services) into
// every consumer of this module — a nest→mcp→nest module cycle.
@@ -119,7 +119,20 @@ export interface ResetPasswordOutcome {
* passkeyService) go through auth.bridge.ts.
*/
@Injectable()
export class AuthService {
export class AuthService implements OnModuleInit, OnModuleDestroy {
/**
* The ephemeral-token store sweeps itself every minute. index.ts used to kick
* that off with a bare require() after listen(); it belongs to the domain that
* issues the tokens.
*/
onModuleInit(): void {
startTokenCleanup();
}
onModuleDestroy(): void {
stopTokenCleanup();
}
constructor(
private readonly db: DatabaseService,
private readonly permissions: PermissionsService,
+1 -1
View File
@@ -6,7 +6,7 @@ import type { TrekWsPayload, TrekWsTripEventName } from '@trek/shared';
import { RealtimeService } from '../realtime/realtime.service';
import { PermissionsService } from '../permissions/permissions.service';
import { avatarUrl } from '../common/avatarUrl';
import { consumeEphemeralToken } from '../../services/ephemeralTokens';
import { consumeEphemeralToken } from '../auth/ephemeral-tokens';
import { verifyJwtAndLoadUser } from '../auth/jwt-verify';
import type { User, TripFile } from '../../types';
import { DatabaseService, type TripAccess } from '../database/database.service';
@@ -24,7 +24,7 @@ import {
type NotifEventType,
} from '../../services/notificationPreferencesService';
import { getChannel, listChannels, type ChannelMessage, type ExternalChannel } from '../../services/notifications/channelRegistry';
import { getAction } from '../../services/inAppNotificationActions';
import { getAction } from './in-app-actions';
import { avatarUrl } from '../common/avatarUrl';
import { DatabaseService } from '../database/database.service';
import { RealtimeService } from '../realtime/realtime.service';
+1 -1
View File
@@ -1,7 +1,7 @@
import { WebSocketServer, WebSocket } from 'ws';
import { readEnv } from './app-config';
import { db, canAccessTrip } from './db/database';
import { consumeEphemeralTokenWithMeta } from './services/ephemeralTokens';
import { consumeEphemeralTokenWithMeta } from './nest/auth/ephemeral-tokens';
import { emitPluginEvent, pluginEventMeta } from './plugin-event-sink';
import { User } from './types';
import http from 'node:http';