Add server events emitter
| @@ -1,8 +1,36 @@ | |||
| 1 | import { UserDirectoryList, User } from "./src/users"; | 1 | import { EventEmitter } from 'node:events'; |
| 2 | import { CommandLineArguments } from "./src/command-line"; | 2 | import { CsrfSyncedToken } from 'csrf-sync'; |
| 3 | import { CsrfSyncedToken } from "csrf-sync"; | 3 | import { UserDirectoryList, User } from './src/users.js'; |
| 4 | import { CommandLineArguments } from './src/command-line.js'; | ||
| 5 | import { EVENT_NAMES } from './src/server-events.js'; | ||
| 6 | |||
| 7 | /** | ||
| 8 | * Event payload for SERVER_STARTED event. | ||
| 9 | */ | ||
| 10 | export interface ServerStartedEvent { | ||
| 11 | /** | ||
| 12 | * The URL the server is listening on. | ||
| 13 | */ | ||
| 14 | url: URL; | ||
| 15 | } | ||
| 16 | |||
| 17 | /** | ||
| 18 | * Map of all server events to their payload types. | ||
| 19 | */ | ||
| 20 | export interface ServerEventMap { | ||
| 21 | [EVENT_NAMES.SERVER_STARTED]: [ServerStartedEvent]; | ||
| 22 | } | ||
| 4 | 23 | ||
| 5 | declare global { | 24 | declare global { |
| 25 | declare namespace NodeJS { | ||
| 26 | export interface Process { | ||
| 27 | /** | ||
| 28 | * A global instance of the server events emitter. | ||
| 29 | */ | ||
| 30 | serverEvents: EventEmitter<ServerEventMap>; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 6 | declare namespace CookieSessionInterfaces { | 34 | declare namespace CookieSessionInterfaces { |
| 7 | export interface CookieSessionObject { | 35 | export interface CookieSessionObject { |
| 8 | /** | 36 | /** |
| @@ -20,6 +20,7 @@ import bodyParser from 'body-parser'; | |||
| 20 | import open from 'open'; | 20 | import open from 'open'; |
| 21 | 21 | ||
| 22 | // local library imports | 22 | // local library imports |
| 23 | import { serverEvents, EVENT_NAMES } from './src/server-events.js'; | ||
| 23 | import { CommandLineParser } from './src/command-line.js'; | 24 | import { CommandLineParser } from './src/command-line.js'; |
| 24 | import { loadPlugins } from './src/plugin-loader.js'; | 25 | import { loadPlugins } from './src/plugin-loader.js'; |
| 25 | import { | 26 | import { |
| @@ -348,6 +349,7 @@ async function postSetupTasks(result) { | |||
| 348 | console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); | 349 | console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); |
| 349 | 350 | ||
| 350 | setupLogLevel(); | 351 | setupLogLevel(); |
| 352 | serverEvents.emit(EVENT_NAMES.SERVER_STARTED, { url: autorunUrl }); | ||
| 351 | } | 353 | } |
| 352 | 354 | ||
| 353 | /** | 355 | /** |
| @@ -0,0 +1,21 @@ | |||
| 1 | import EventEmitter from 'node:events'; | ||
| 2 | import process from 'node:process'; | ||
| 3 | |||
| 4 | /** | ||
| 5 | * @typedef {import('../index').ServerEventMap} ServerEventMap | ||
| 6 | * @type {EventEmitter<ServerEventMap>} The default event source. | ||
| 7 | */ | ||
| 8 | export const serverEvents = new EventEmitter(); | ||
| 9 | process.serverEvents = serverEvents; | ||
| 10 | export default serverEvents; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * @enum {string} | ||
| 14 | * @readonly | ||
| 15 | */ | ||
| 16 | export const EVENT_NAMES = Object.freeze({ | ||
| 17 | /** | ||
| 18 | * Emitted when the server has started. | ||
| 19 | */ | ||
| 20 | SERVER_STARTED: 'server-started', | ||
| 21 | }); | ||