|
1 | | -import * as logger from 'next/dist/build/output/log.js'; |
2 | | - |
3 | | -function getEnvironmentMeta() { |
4 | | - const isCustomServer = !process.title.startsWith('next-'); |
5 | | - const isMainProcess = process.env.NEXT_WS_MAIN_PROCESS === '1'; |
6 | | - const isDevelopment = process.env.NODE_ENV === 'development'; |
7 | | - return { isCustomServer, isMainProcess, isDevelopment }; |
8 | | -} |
9 | | - |
10 | | -function mainProcessOnly(callerName: string) { |
11 | | - if (process.env.NEXT_WS_SKIP_ENVIRONMENT_CHECK === '1') return; |
12 | | - |
13 | | - const meta = getEnvironmentMeta(); |
14 | | - if (!meta.isCustomServer && !meta.isMainProcess) { |
15 | | - throw new Error( |
16 | | - `[next-ws] Attempt to call '${callerName}' outside the main process. |
17 | | -You may be attempting to interact with the WebSocket server outside of an UPGRADE handler. This will fail in production, as Next.js employs a worker process for routing, which do not have access to the WebSocket server on the main process. |
18 | | -You can resolve this by using a custom server.`, |
19 | | - ); |
20 | | - } else if (!meta.isCustomServer) { |
21 | | - logger.warnOnce( |
22 | | - `[next-ws] The function '${callerName}' was called without a custom server. |
23 | | -This could lead to unintended behaviour, especially if you're attempting to interact with the WebSocket server outside of an UPGRADE handler. |
24 | | -Please note, while such configurations might function during development, they will fail in production. This is because Next.js employs a worker process for routing in production, which do not have access to the WebSocket server on the main process. |
25 | | -You can resolve this by using a custom server.`, |
26 | | - ); |
27 | | - } |
28 | | -} |
29 | | - |
30 | | -// |
31 | | - |
32 | | -import type { Server as HttpServer } from 'node:http'; |
33 | | -export const kHttpServer = Symbol.for('kHttpServer'); |
34 | | - |
35 | | -export function getHttpServer() { |
36 | | - mainProcessOnly('getHttpServer'); |
37 | | - return Reflect.get(globalThis, kHttpServer) as HttpServer | undefined; |
38 | | -} |
39 | | - |
40 | | -export function setHttpServer<T extends HttpServer | undefined>(server: T) { |
41 | | - mainProcessOnly('setHttpServer'); |
42 | | - Reflect.set(globalThis, kHttpServer, server); |
43 | | - return getHttpServer() as T; |
44 | | -} |
45 | | - |
46 | | -export function useHttpServer<T extends HttpServer | undefined>( |
47 | | - server: () => T, |
48 | | -): T { |
49 | | - mainProcessOnly('useHttpServer'); |
50 | | - const existing = getHttpServer(); |
51 | | - if (existing) return existing as T; |
52 | | - return setHttpServer(server()); |
53 | | -} |
54 | | - |
55 | | -// |
56 | | - |
57 | | -import type { WebSocketServer } from 'ws'; |
58 | | -export const kWebSocketServer = Symbol.for('kWebSocketServer'); |
59 | | - |
60 | | -export function getWebSocketServer() { |
61 | | - mainProcessOnly('getWebSocketServer'); |
62 | | - return Reflect.get(globalThis, kWebSocketServer) as |
63 | | - | WebSocketServer |
64 | | - | undefined; |
65 | | -} |
66 | | - |
67 | | -export function setWebSocketServer<T extends WebSocketServer | undefined>( |
68 | | - server: T, |
69 | | -) { |
70 | | - mainProcessOnly('setWebSocketServer'); |
71 | | - Reflect.set(globalThis, kWebSocketServer, server); |
72 | | - return getWebSocketServer() as T; |
73 | | -} |
74 | | - |
75 | | -export function useWebSocketServer<T extends WebSocketServer | undefined>( |
76 | | - server: () => T, |
77 | | -): T { |
78 | | - mainProcessOnly('useWebSocketServer'); |
79 | | - const existing = getWebSocketServer(); |
80 | | - if (existing) return existing as T; |
81 | | - return setWebSocketServer(server()) as T; |
82 | | -} |
| 1 | +function useGlobal<T>(key: PropertyKey) { |
| 2 | + return [ |
| 3 | + function get() { |
| 4 | + return Reflect.get(globalThis, key) as T | undefined; |
| 5 | + }, |
| 6 | + function set(value: T) { |
| 7 | + return Reflect.set(globalThis, key, value); |
| 8 | + }, |
| 9 | + function use(getter: () => T) { |
| 10 | + const existing = Reflect.get(globalThis, key); |
| 11 | + if (existing) return existing as T; |
| 12 | + Reflect.set(globalThis, key, getter()); |
| 13 | + return Reflect.get(globalThis, key) as T; |
| 14 | + }, |
| 15 | + ] as const; |
| 16 | +} |
| 17 | + |
| 18 | +// ===== HTTP Server ===== // |
| 19 | + |
| 20 | +export const [getHttpServer, setHttpServer, useHttpServer] = // |
| 21 | + useGlobal<import('node:http').Server>( |
| 22 | + Symbol.for('next-ws.http-server'), // |
| 23 | + ); |
| 24 | + |
| 25 | +// ===== WebSocket Server ===== // |
| 26 | + |
| 27 | +export const [getWebSocketServer, setWebSocketServer, useWebSocketServer] = |
| 28 | + useGlobal<import('ws').WebSocketServer>( |
| 29 | + Symbol.for('next-ws.websocket-server'), |
| 30 | + ); |
0 commit comments