insite-ws 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.lock +1082 -0
- package/dist/client/index.js +1 -1
- package/dist/client/index.js.map +4 -4
- package/dist/client/node/index.js +1 -1
- package/dist/client/node/index.js.map +4 -4
- package/dist/server/index.js +1 -5
- package/dist/server/index.js.map +4 -4
- package/package.json +3 -3
package/dist/server/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/server/WSServer.ts", "../../src/server/symbols.ts", "../../src/server/WSServerClient.ts", "
|
|
4
|
-
"sourcesContent": ["import type http from \"node:http\";\nimport type https from \"node:https\";\nimport { WebSocketServer } from \"ws\";\nimport {\n\tcreateServer,\n\tgetRemoteAddress,\n\tresolveSSL,\n\tshowServerListeningMessage\n} from \"insite-common/backend\";\nimport { isQuietSymbol, requestListenersSymbol } from \"./symbols\";\nimport { WSServerClient } from \"./WSServerClient\";\nimport type { Options, RequestListener } from \"./types\";\n\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n\nexport class WSServer<WSSC extends WSServerClient = WSServerClient> extends WebSocketServer<typeof WSServerClient> {\n\tconstructor(options: Options<WSSC>) {\n\t\tconst {\n\t\t\tssl: _,\n\t\t\tport,\n\t\t\tserver = createServer(WSServer.makeProps(options)),\n\t\t\tquiet = false,\n\t\t\t...wssOptions\n\t\t} = options;\n\t\t\n\t\tsuper({\n\t\t\tWebSocket: WSServerClient,\n\t\t\t...wssOptions,\n\t\t\tserver\n\t\t});\n\t\t\n\t\tthis.server = server;\n\t\t\n\t\tif (options.server)\n\t\t\tvoid new Promise<void>(resolve => void (\n\t\t\t\tserver.listening ?\n\t\t\t\t\tresolve() :\n\t\t\t\t\tserver.on(\"listening\", resolve)\n\t\t\t)).then(() => showServerListeningMessage(this));\n\t\telse\n\t\t\tthis.server.listen(\n\t\t\t\ttypeof port == \"string\" ?\n\t\t\t\t\tNumber.parseInt(port) :\n\t\t\t\t\tport ?? (this.isS ? 443 : 80),\n\t\t\t\t() => showServerListeningMessage(this)\n\t\t\t);\n\t\t\n\t\tthis[isQuietSymbol] = quiet;\n\t\t\n\t\tthis.on(\"connection\", this.#handleConnection);\n\t\t\n\t\tif (!this[isQuietSymbol]) {\n\t\t\tthis.on(\"error\", (error: Error) => console.error(`${this.icon}\u2757\uFE0F WS Server:`, error));\n\t\t\t\n\t\t\tthis.on(\"close\", () => console.error(`${this.icon}\u2757\uFE0F WS Server closed`));\n\t\t}\n\t\t\n\t}\n\t\n\ton(event: \"connection\", callback: (this: this, socket: WSSC, request: http.IncomingMessage) => void): this;\n\ton(event: \"error\", callback: (this: this, error: Error) => void): this;\n\ton(event: \"headers\", callback: (this: this, headers: string[], request: http.IncomingMessage) => void): this;\n\ton(event: \"close\" | \"listening\", callback: (this: this) => void): this;\n\t\n\ton(event: \"client-connect\", listener: (this: this, wscc: WSSC, request: http.IncomingMessage) => void): this;\n\ton(event: \"client-error\", listener: (this: this, wscc: WSSC, error: Error | undefined) => void): this;\n\ton(event: `client-${string}`, listener: (this: this, wscc: WSSC, ...args: any[]) => void): this;\n\t\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this {\n\t\treturn super.on(event, listener as (this: WebSocketServer, ...args: any[]) => void);\n\t}\n\t\n\treadonly isWebSocketServer = true;\n\treadonly isWebSocketServerClient = false;\n\treadonly isWebSocket = false;\n\t\n\ticon = \"\uD83D\uDD0C\";\n\tname = \"WS\";\n\tget protocol() {\n\t\treturn `ws${this.isS ? \"s\" : \"\"}`;\n\t}\n\t\n\tserver;\n\t\n\tget isS() {\n\t\treturn \"setSecureContext\" in this.server;\n\t}\n\t\n\t[isQuietSymbol]: boolean;\n\t\n\t[requestListenersSymbol] = new Map<string, RequestListener<WSSC>>();\n\t\n\taddRequestListener(kind: string, listener: RequestListener<WSSC>) {\n\t\tthis[requestListenersSymbol].set(kind, listener);\n\t\t\n\t\treturn this;\n\t}\n\t\n\t/**\n\t * Alias for `addRequestListener()`\n\t */\n\tonRequest = this.addRequestListener;\n\t\n\tremoveRequestListener(kind: string) {\n\t\tthis[requestListenersSymbol].delete(kind);\n\t\t\n\t\treturn this;\n\t}\n\t\n\t/**\n\t * Alias for `removeRequestListener()`\n\t */\n\toffRequest = this.removeRequestListener;\n\t\n\t#handleConnection(wssc: WSSC, request: http.IncomingMessage) {\n\t\t\n\t\t/* Compatibility with Bun */\n\t\tif (!(wssc instanceof WSServerClient))\n\t\t\tWSServerClient.extend.call(wssc);\n\t\t\n\t\tObject.assign(wssc, {\n\t\t\twss: this,\n\t\t\tuserAgent: request.headers[\"user-agent\"] ?? \"\",\n\t\t\tremoteAddress: getRemoteAddress(request)\n\t\t});\n\t\t\n\t\tWSServerClient.handleConnect.call(wssc);\n\t\t\n\t\tthis.emit(\"client-connect\", wssc, request);\n\t\t\n\t\tif (process.env.NODE_ENV === \"development\" && !this[isQuietSymbol])\n\t\t\tconsole.info(\n\t\t\t\t`${this.icon} WS Server:`,\n\t\t\t\t\"user\" in wssc ? `\\x1B[1m${(wssc.user as { email: string }).email}\\x1B[0m` : \"\\x1B[3manonymous\\x1B[0m\",\n\t\t\t\t\"connected\"\n\t\t\t);\n\t\t\n\t}\n\t\n\t\n\tstatic makeProps<SWSSC extends WSServerClient>({ ssl }: Options<SWSSC>): http.ServerOptions | https.ServerOptions {\n\t\treturn {\n\t\t\t...resolveSSL(ssl)\n\t\t};\n\t}\n\t\n}\n", "export const isQuietSymbol = Symbol(\"isQuiet\");\nexport const requestListenersSymbol = Symbol(\"requestListeners\");\nexport const heartbeatIntervalSymbol = Symbol(\"heartbeatInterval\");\nexport const heartbeatTimeoutSymbol = Symbol(\"heartbeatTimeout\");\nexport const defibSymbol = Symbol(\"defib\");\nexport const heartbeatTsSymbol = Symbol(\"heartbeatTs\");\nexport const requestCounterSymbol = Symbol(\"requestCounter\");\n", "import { ClientRequest, IncomingMessage } from \"node:http\";\nimport { RawData, WebSocket } from \"ws\";\nimport { handleMongoError } from \"@nesvet/n\";\nimport {\n\tHEARTBEAT_GAP,\n\tHEARTBEAT_INTERVAL,\n\tREQUEST_COUNTER_LIMIT,\n\tREQUEST_HEADERS\n} from \"../common\";\nimport {\n\tdefibSymbol,\n\theartbeatIntervalSymbol,\n\theartbeatTimeoutSymbol,\n\theartbeatTsSymbol,\n\tisQuietSymbol,\n\trequestCounterSymbol,\n\trequestListenersSymbol\n} from \"./symbols\";\nimport { WSServer } from \"./WSServer\";\n\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n\nexport class WSServerClient extends WebSocket {\n\ton(event: \"close\", listener: (this: this, code: number, reason: Buffer) => void): this;\n\ton(event: \"error\", listener: (this: this, error: Error) => void): this;\n\ton(event: \"upgrade\", listener: (this: this, request: IncomingMessage) => void): this;\n\ton(event: \"message\", listener: (this: this, data: RawData, isBinary: boolean) => void): this;\n\ton(event: \"open\", listener: (this: this) => void): this;\n\ton(event: \"unexpected-response\", listener: (this: this, request: ClientRequest, response: IncomingMessage) => void): this;\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this {\n\t\treturn super.on(event, listener as (this: WebSocket, ...args: any[]) => void);\n\t}\n\t\n\tget isConnecting() {\n\t\treturn this.readyState === this.CONNECTING;\n\t}\n\t\n\tget isOpen() {\n\t\treturn this.readyState === this.OPEN;\n\t}\n\t\n\tget isClosing() {\n\t\treturn this.readyState === this.CLOSING;\n\t}\n\t\n\tget isClosed() {\n\t\treturn this.readyState === this.CLOSED;\n\t}\n\t\n\treadonly isWebSocketServerClient = true;\n\treadonly isWebSocketServer = false;\n\treadonly isWebSocket = false;\n\t\n\twss!: WSServer<this>;\n\t\n\tuserAgent = \"\";\n\t\n\tremoteAddress = \"\";\n\t\n\t[heartbeatTimeoutSymbol]?: ReturnType<typeof setTimeout>;\n\t\n\t[defibSymbol]() {\n\t\t\n\t\tclearTimeout(this[heartbeatTimeoutSymbol]);\n\t\t\n\t\tthis[heartbeatTimeoutSymbol] = setTimeout(\n\t\t\t() => this.terminate(),\n\t\t\tHEARTBEAT_INTERVAL + HEARTBEAT_GAP\n\t\t);\n\t\t\n\t}\n\t\n\tlatency = 0;\n\t\n\t[heartbeatTsSymbol]?: number;\n\t\n\t[heartbeatIntervalSymbol] = WSServerClient.makeHeartbeatInterval.call(this);\n\t\n\tsendMessage(...args: unknown[]) {\n\t\treturn this.send(JSON.stringify(args));\n\t}\n\t\n\t[requestCounterSymbol]: number = 0;\n\t\n\tsendRequest(...args: unknown[]) {\n\t\t\n\t\tconst id = this[requestCounterSymbol]++;\n\t\t\n\t\tif (this[requestCounterSymbol] >= REQUEST_COUNTER_LIMIT)\n\t\t\tthis[requestCounterSymbol] = 0;\n\t\t\n\t\tconst eventName = `message:${REQUEST_HEADERS.RESPONSE}-${id}`;\n\t\t\n\t\tif (typeof args.at(-1) == \"function\") {\n\t\t\tthis.once(eventName, args.splice(-1, 1)[0] as () => void);\n\t\t\t\n\t\t\tthis.sendMessage(REQUEST_HEADERS.REQUEST, id, ...args);\n\t\t\t\n\t\t\treturn this;\n\t\t}\n\t\t\n\t\treturn new Promise((resolve, reject) => {\n\t\t\t\n\t\t\tthis.once(eventName, (error, result) => {\n\t\t\t\tif (error) {\n\t\t\t\t\tconst { message, ...restProps } = error;\n\t\t\t\t\treject(Object.assign(new Error(message), restProps) as Error);\n\t\t\t\t} else\n\t\t\t\t\tresolve(result);\n\t\t\t\t\n\t\t\t});\n\t\t\t\n\t\t\tthis.sendMessage(REQUEST_HEADERS.REQUEST, id, ...args);\n\t\t\t\n\t\t});\n\t}\n\t\n\t\n\tstatic makeHeartbeatInterval(this: WSServerClient) {\n\t\treturn setInterval(() => {\n\t\t\t\n\t\t\tthis[heartbeatTsSymbol] = Date.now();\n\t\t\t\n\t\t\tthis.send(\"\");\n\t\t\t\n\t\t}, HEARTBEAT_INTERVAL);\n\t}\n\t\n\t/* Compatibility with Bun */\n\tstatic extend(this: WSServerClient) {\n\t\t\n\t\tObject.defineProperties(this, {\n\t\t\tisConnecting: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isConnecting\")!.get\n\t\t\t},\n\t\t\tisOpen: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isOpen\")!.get\n\t\t\t},\n\t\t\tisClosing: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isClosing\")!.get\n\t\t\t},\n\t\t\tisClosed: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isClosed\")!.get\n\t\t\t}\n\t\t});\n\t\t\n\t\tconst { terminate } = this as WebSocket;\n\t\t\n\t\tObject.assign(this, {\n\t\t\tisWebSocketServerClient: true,\n\t\t\tisWebSocketServer: false,\n\t\t\tisWebSocket: false,\n\t\t\t[defibSymbol]: WSServerClient.prototype[defibSymbol],\n\t\t\tlatency: 0,\n\t\t\t[heartbeatIntervalSymbol]: WSServerClient.makeHeartbeatInterval.call(this),\n\t\t\tsendMessage: WSServerClient.prototype.sendMessage,\n\t\t\tsendRequest: WSServerClient.prototype.sendRequest,\n\t\t\tterminate() { /* Workaround Bun's WebSocket#terminate bug */\n\t\t\t\t\n\t\t\t\ttry {\n\t\t\t\t\tterminate.call(this);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconsole.error(error);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t});\n\t\t\n\t}\n\t\n\tstatic handleConnect(this: WSServerClient) {\n\t\t\n\t\tthis.on(\"message\", WSServerClient.handleMessage);\n\t\tthis.on(\"error\", WSServerClient.handleError);\n\t\tthis.on(\"close\", WSServerClient.handleClose);\n\t\t\n\t\tthis[defibSymbol]();\n\t\t\n\t}\n\t\n\tstatic handleMessage(this: WSServerClient, data: RawData) {\n\t\tconst message = data.toString();// eslint-disable-line @typescript-eslint/no-base-to-string\n\t\t\n\t\tthis[defibSymbol]();\n\t\t\n\t\tif (message)\n\t\t\tif (message === \"?\")\n\t\t\t\tthis.send(\"!\");\n\t\t\telse\n\t\t\t\ttry {\n\t\t\t\t\tconst [ kind, ...rest ] = JSON.parse(message);\n\t\t\t\t\t\n\t\t\t\t\tswitch (kind) {\n\t\t\t\t\t\tcase REQUEST_HEADERS.REQUEST:\n\t\t\t\t\t\t\tvoid WSServerClient.handleRequest.call(this, ...rest as [ id: number, kind: string, ...rest: unknown[] ]);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthis.emit(`message:${kind}`, ...rest);\n\t\t\t\t\t\t\tthis.wss.emit(\"client-message\", this, kind, ...rest);\n\t\t\t\t\t\t\tthis.wss.emit(`client-message:${kind}`, this, ...rest);\n\t\t\t\t\t}\n\t\t\t\t} catch (error) {\n\t\t\t\t\tWSServerClient.handleError.call(this, error as Error);\n\t\t\t\t}\n\t\telse if (this[heartbeatTsSymbol]) {\n\t\t\tthis.latency = Date.now() - this[heartbeatTsSymbol];\n\t\t\tthis[heartbeatTsSymbol] = undefined;\n\t\t}\n\t\t\n\t}\n\t\n\tstatic async handleRequest(this: WSServerClient, id: number, kind: string, ...rest: unknown[]) {\n\t\tconst listener = this.wss[requestListenersSymbol].get(kind);\n\t\t\n\t\tlet result;\n\t\tlet requestError = null;\n\t\t\n\t\tif (listener)\n\t\t\ttry {\n\t\t\t\tresult = await listener.call(this.wss, this, ...rest);\n\t\t\t} catch (error) {\n\t\t\t\tif (process.env.NODE_ENV === \"development\")\n\t\t\t\t\thandleMongoError(error);\n\t\t\t\t\n\t\t\t\tif (error instanceof Error) {\n\t\t\t\t\tconst { message, ...restProps } = error;\n\t\t\t\t\trequestError = { message, ...restProps };\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (process.env.NODE_ENV === \"development\" && !this.wss[isQuietSymbol])\n\t\t\t\t\tconsole.error(`${this.wss.icon}\u2757\uFE0F WS Server request \"${kind}\" (${id}):`, error);\n\t\t\t}\n\t\telse\n\t\t\trequestError = { message: `Unknown request kind \"${kind}\"` };\n\t\t\n\t\tthis.sendMessage(`${REQUEST_HEADERS.RESPONSE}-${id}`, requestError, result);\n\t\t\n\t}\n\t\n\tstatic handleError(this: WSServerClient, error: Error | Event | undefined) {\n\t\tif (error instanceof Event)\n\t\t\terror = undefined;\n\t\t\n\t\tif (!this.wss[isQuietSymbol])\n\t\t\tconsole.error(\n\t\t\t\t`${this.wss.icon}\u2757\uFE0F WS Server client`,\n\t\t\t\t\"user\" in this ? `\\x1B[1m${(this.user as { email: string }).email}\\x1B[0m:` : \"\\x1B[3manonymous\\x1B[0m:\",\n\t\t\t\terror\n\t\t\t);\n\t\t\n\t\tthis.wss.emit(\"client-error\", this, error);\n\t\t\n\t}\n\t\n\tstatic handleClose(this: WSServerClient, code: number, reason: Buffer) {\n\t\t\n\t\tif (process.env.NODE_ENV === \"development\") {\n\t\t\tconst reasonString = reason.toString();\n\t\t\t\n\t\t\tif (!this.wss[isQuietSymbol])\n\t\t\t\tconsole.info(\n\t\t\t\t\t`${this.wss.icon} WS Server:`,\n\t\t\t\t\t\"user\" in this ? `\\x1B[1m${(this.user as { email: string }).email}\\x1B[0m` : \"\\x1B[3manonymous\\x1B[0m\",\n\t\t\t\t\t`disconnected ${code ? `with code ${code}` : \"\"} ${code && reasonString ? \"and \" : \"\"}${reasonString ? `reason \"${reasonString}\"` : \"\"}`\n\t\t\t\t);\n\t\t}\n\t\t\n\t\tclearTimeout(this[heartbeatTimeoutSymbol]);\n\t\tclearInterval(this[heartbeatIntervalSymbol]);\n\t\t\n\t\tthis.wss.emit(\"client-close\", this);\n\t\tthis.wss.emit(\"client-closed\", this);\n\t\t\n\t}\n\t\n}\n", "export function noop() { /**/ }\n", "/* eslint-disable @typescript-eslint/no-explicit-any */\n\ntype Value<T> = PromiseLike<T> | T;\ntype Resolve<T> = (value: Value<T>) => void;\ntype Reject = (reason?: any) => void;\n\ntype Options = {\n\tsignal?: AbortSignal;\n\ttimeout?: number;\n};\n\nconst nativesMap = new WeakMap<StatefulPromise<any>, { resolve: Resolve<any>; reject: Reject }>();\n\n\nexport class StatefulPromise<T> extends Promise<T> {\n\tconstructor(\n\t\texecutor?: (resolve: Resolve<T>, reject: Reject) => void,\n\t\toptions?: Options\n\t) {\n\t\tlet nativeResolve: Resolve<T>;\n\t\tlet nativeReject: Reject;\n\t\t\n\t\tsuper((resolve, reject) => {\n\t\t\tnativeResolve = resolve;\n\t\t\tnativeReject = reject;\n\t\t\t\n\t\t});\n\t\t\n\t\tnativesMap.set(this, { resolve: nativeResolve!, reject: nativeReject! });\n\t\t\n\t\tif (options?.signal)\n\t\t\tif (options.signal.aborted)\n\t\t\t\tthis.reject(new DOMException(\"Aborted\", \"AbortError\"));\n\t\t\telse {\n\t\t\t\tconst abortListener = () => this.reject(new DOMException(\"Aborted\", \"AbortError\"));\n\t\t\t\t\n\t\t\t\toptions.signal.addEventListener(\"abort\", abortListener);\n\t\t\t\t\n\t\t\t\tthis.finally(() => options.signal?.removeEventListener(\"abort\", abortListener));\n\t\t\t}\n\t\t\n\t\tif (typeof options?.timeout === \"number\" && Number.isFinite(options.timeout)) {\n\t\t\tconst timeout = setTimeout(() => this.reject(new DOMException(\"Timeout exceeded\", \"TimeoutError\")), options.timeout);\n\t\t\t\n\t\t\tthis.finally(() => clearTimeout(timeout));\n\t\t}\n\t\t\n\t\texecutor?.(this.resolve, this.reject);\n\t\t\n\t}\n\t\n\tisPending = true;\n\tisFulfilled = false;\n\tisRejected = false;\n\t\n\tstate: \"fulfilled\" | \"pending\" | \"rejected\" = \"pending\";\n\t\n\tresult?: T | unknown;\n\t\n\treadonly resolve = (value: Value<T>) => {\n\t\t\n\t\tif (this.isPending) {\n\t\t\tthis.isPending = false;\n\t\t\tthis.isFulfilled = true;\n\t\t\tthis.state = \"fulfilled\";\n\t\t\tthis.result = value;\n\t\t\t\n\t\t\tconst native = nativesMap.get(this);\n\t\t\t\n\t\t\tif (native) {\n\t\t\t\tnative.resolve.call(this, value);\n\t\t\t\tnativesMap.delete(this);\n\t\t\t}\n\t\t}\n\t\t\n\t};\n\t\n\treadonly reject = (reason?: any) => {\n\t\t\n\t\tif (this.isPending) {\n\t\t\tthis.isPending = false;\n\t\t\tthis.isRejected = true;\n\t\t\tthis.state = \"rejected\";\n\t\t\tthis.result = reason;\n\t\t\t\n\t\t\tconst native = nativesMap.get(this);\n\t\t\t\n\t\t\tif (native) {\n\t\t\t\tnative.reject.call(this, reason);\n\t\t\t\tnativesMap.delete(this);\n\t\t\t}\n\t\t}\n\t\t\n\t};\n\t\n\t\n\tstatic resolved<U>(value: U): StatefulPromise<U> {\n\t\treturn new StatefulPromise<U>(resolve => resolve(value));\n\t}\n\t\n\tstatic rejected(reason: unknown): StatefulPromise<never> {\n\t\treturn new StatefulPromise((_, reject) => reject(reason));\n\t}\n\t\n}\n", "import { noop } from \"./noop.js\";\nimport { StatefulPromise } from \"./StatefulPromise.js\";\n\n\ntype Callback = (...args: any[]) => unknown;// eslint-disable-line @typescript-eslint/no-explicit-any\ntype FunctionOrBoolean = (() => unknown) | boolean;\ntype Options = { leading?: FunctionOrBoolean; maxWait?: number; trailing?: FunctionOrBoolean };\n\ntype DebouncedFunction<C extends Callback> = {\n\t(this: ThisParameterType<C>, ...args: Parameters<C>): Promise<Awaited<ReturnType<C>>>;\n};\n\nexport type Debounced<C extends Callback, R extends ReturnType<C>> = DebouncedFunction<C> & {\n\tcallback: C;\n\tpromise: StatefulPromise<R>;\n\tresolve: (value: R) => void;\n\treject: (reason?: unknown) => void;\n\trun(): Promise<R>;\n\tclear(shouldRun?: boolean): Promise<void>;\n};\n\n\nexport function debounce<C extends Callback, R extends ReturnType<C>>(callback: C, limit = 0, options: Options = {}) {\n\tlet { leading = false, trailing = true } = options;\n\tconst { maxWait = Infinity } = options;\n\t\n\tlet timeout: ReturnType<typeof setTimeout> | undefined;\n\tlet maxWaitTimeout: ReturnType<typeof setTimeout> | undefined;\n\tlet end: number | undefined;\n\tlet context: ThisParameterType<C>;\n\tlet args: unknown[];\n\tlet initial: FunctionOrBoolean;\n\tlet final: FunctionOrBoolean;\n\tlet result: R;\n\t\n\tconst expired = async () => {\n\t\t\n\t\tconst n = Date.now();\n\t\t\n\t\tif (n >= end!) {\n\t\t\ttimeout = undefined;\n\t\t\tend = undefined;\n\t\t\t\n\t\t\tclearTimeout(maxWaitTimeout);\n\t\t\tmaxWaitTimeout = undefined;\n\t\t\t\n\t\t\tif (typeof final == \"function\")\n\t\t\t\tawait final();\n\t\t\t\n\t\t\tdebounced.resolve(result);\n\t\t\t\n\t\t\tinitial = leading;\n\t\t} else {\n\t\t\tclearTimeout(timeout);\n\t\t\ttimeout = setTimeout(expired, end! - n);\n\t\t}\n\t\t\n\t};\n\t\n\tconst maxWaitExpired = Number.isFinite(maxWait) ? () => {\n\t\t\n\t\tif (final === run)\n\t\t\tfinal = false;\n\t\t\n\t\trun();\n\t\t\n\t\tmaxWaitTimeout = setTimeout(maxWaitExpired as TimerHandler, maxWait);\n\t\t\n\t} : null;\n\t\n\tasync function run() {\n\t\ttry {\n\t\t\tresult = await callback.apply(context, args) as R;\n\t\t\t\n\t\t\treturn result;\n\t\t} catch (error) {\n\t\t\tdebounced.reject(error);\n\t\t\t\n\t\t\tthrow error;\n\t\t}\n\t}\n\t\n\tif (leading && typeof leading == \"boolean\")\n\t\tleading = run;\n\tinitial = leading;\n\t\n\tif (trailing && typeof trailing == \"boolean\")\n\t\ttrailing = run;\n\tfinal = trailing;\n\t\n\tconst debounced: Debounced<C, R> = Object.assign(function (..._args) {\n\t\targs = _args;\n\t\tcontext = this;// eslint-disable-line no-invalid-this\n\t\t\n\t\tend = Date.now() + limit;\n\t\tfinal = trailing;\n\t\t\n\t\tif (!debounced.promise.isPending)\n\t\t\tdebounced.promise = new StatefulPromise<R>((resolve, reject) => {\n\t\t\t\tdebounced.resolve = resolve;\n\t\t\t\tdebounced.reject = reject;\n\t\t\t\t\n\t\t\t\tif (typeof initial == \"function\") {\n\t\t\t\t\tinitial();\n\t\t\t\t\t\n\t\t\t\t\tinitial = false;\n\t\t\t\t\t\n\t\t\t\t\tif (final === run)\n\t\t\t\t\t\tfinal = false;\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (!timeout) {\n\t\t\t\t\ttimeout = setTimeout(expired, limit);\n\t\t\t\t\t\n\t\t\t\t\tif (maxWaitExpired)\n\t\t\t\t\t\tmaxWaitTimeout = setTimeout(maxWaitExpired, maxWait);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t});\n\t\t\n\t\treturn debounced.promise;\n\t} as DebouncedFunction<C>, {\n\t\tcallback,\n\t\tpromise: StatefulPromise.resolved(undefined as unknown as R),\n\t\tresolve: noop,\n\t\treject: noop,\n\t\trun,\n\t\tclear: async (shouldRun?: boolean) => {\n\t\t\t\n\t\t\tclearTimeout(timeout);\n\t\t\ttimeout = undefined;\n\t\t\t\n\t\t\tclearTimeout(maxWaitTimeout);\n\t\t\tmaxWaitTimeout = undefined;\n\t\t\t\n\t\t\tdebounced.resolve(shouldRun ? await run() : result);\n\t\t\t\n\t\t}\n\t});\n\t\n\treturn debounced;\n}\n\n\ndebounce.noop = Object.assign(() => { /**/ }, {\n\tcallback: noop,\n\tpromise: StatefulPromise.resolved(undefined), // eslint-disable-line unicorn/no-useless-undefined\n\tresolve: noop,\n\treject: noop,\n\trun: noop,\n\tclear: noop\n}) as Debounced<() => void, void>;\n\n\nexport function throttle(callback: Callback, limit?: number, options?: Options) {\n\treturn debounce(callback, limit, {\n\t\tmaxWait: limit,\n\t\tleading: true,\n\t\ttrailing: true,\n\t\t...options\n\t});\n}\n", "export function indent(string: string, indentation: string = \"\\t\", times: number = 1): string {\n\tif (!string)\n\t\treturn string;\n\t\n\tconst prefix = indentation.repeat(times);\n\t\n\treturn string.split(\"\\n\").map(line => `${prefix}${line}`).join(\"\\n\");\n}\n", "import { indent } from \"./indent.js\";\n\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n\ntype MongoServerError = {\n\terrorResponse: {\n\t\terrmsg: string;\n\t\terrInfo?: {\n\t\t\t[key: string]: any;\n\t\t};\n\t};\n\t[key: string]: any;\n};\n\nfunction isMongoServerError(error: unknown): error is MongoServerError {\n\treturn (\n\t\t!!error &&\n\t\ttypeof error == \"object\" &&\n\t\t\"errorResponse\" in error &&\n\t\t!!error.errorResponse &&\n\t\ttypeof error.errorResponse == \"object\" &&\n\t\t\"errInfo\" in error.errorResponse\n\t);\n}\n\nexport function handleMongoError<E extends MongoServerError>(error: E): E;\nexport function handleMongoError(error: any): any;\nexport function handleMongoError(error: any) {\n\tif (isMongoServerError(error) && error.errorResponse.errmsg === \"Document failed validation\")\n\t\terror.message = `${error.errorResponse.errmsg}\\n${indent(`failingDocumentId: ${error.errorResponse.errInfo?.failingDocumentId}\\nschemaRulesNotSatisfied: ${JSON.stringify(error.errorResponse.errInfo?.details?.schemaRulesNotSatisfied, null, \" \")}`, \" \")}`;\n\t\n\treturn error;\n}\n", "export function howmuch(number: [] | number, one: string, two: string, five: boolean | string, shouldPrintNumber = true) {\n\tif (Array.isArray(number))\n\t\tnumber = number.length;\n\t\n\tif (typeof five != \"string\") {\n\t\tshouldPrintNumber = five ?? true;\n\t\tfive = two;\n\t}\n\t\n\t\n\treturn (shouldPrintNumber ? `${number}\u00A0` : \"\") + howmuch.raw(number, one, two, five);\n}\n\nhowmuch.raw = (number: number, one: string, two: string, five = two) => {\n\tnumber = Math.abs(number);\n\tconst u = number % 10;\n\tconst d = number % 100;\n\t\n\treturn (\n\t\t(u === 1 && d !== 11) ?\n\t\t\tone :\n\t\t\t((u >= 2 && u <= 4) && !(d >= 12 && d <= 15)) ?\n\t\t\t\ttwo :\n\t\t\t\tfive\n\t);\n};\n", "export const HEARTBEAT_INTERVAL = 5_000;\nexport const HEARTBEAT_GAP = 30_000;\nexport const RESPONSIVENESS_TIMEOUT = 1000;\nexport const OFFLINE_TIMEOUT = 5000;\nexport const REQUEST_COUNTER_LIMIT = Number.MAX_SAFE_INTEGER / 2;\n\nexport const CODES = {\n\tNORMAL: 1000,\n\tPROTOCOL_ERROR: 1002,\n\tABNORMAL: 1006,\n\tNO_HEARTBEAT: 3000,\n\tOFFLINE: 3001,\n\tMANUAL: 3500,\n\tREOPEN: 4000\n} as const;\n\nexport const NON_RECONNECTABLE_CODES = [\n\tCODES.PROTOCOL_ERROR,\n\tCODES.MANUAL,\n\tCODES.REOPEN\n];\n\nexport const REQUEST_HEADERS = {\n\tREQUEST: \"~r-q\",\n\tRESPONSE: \"~r-s\"\n};\n"],
|
|
5
|
-
"mappings": "AAEA,OAAS,mBAAAA,MAAuB,KAChC,OACC,gBAAAC,EACA,oBAAAC,EACA,cAAAC,EACA,8BAAAC,MACM,wBCRA,IAAMC,EAAgB,OAAO,SAAS,EAChCC,EAAyB,OAAO,kBAAkB,EAClDC,EAA0B,OAAO,mBAAmB,EACpDC,EAAyB,OAAO,kBAAkB,EAClDC,EAAc,OAAO,OAAO,EAC5BC,EAAoB,OAAO,aAAa,EACxCC,EAAuB,OAAO,gBAAgB,ECL3D,OAAkB,aAAAC,MAAiB,
|
|
6
|
-
"names": ["WebSocketServer", "createServer", "getRemoteAddress", "resolveSSL", "showServerListeningMessage", "isQuietSymbol", "requestListenersSymbol", "heartbeatIntervalSymbol", "heartbeatTimeoutSymbol", "defibSymbol", "heartbeatTsSymbol", "requestCounterSymbol", "WebSocket", "
|
|
3
|
+
"sources": ["../../src/server/WSServer.ts", "../../src/server/symbols.ts", "../../src/server/WSServerClient.ts", "../../src/common.ts"],
|
|
4
|
+
"sourcesContent": ["import type http from \"node:http\";\nimport type https from \"node:https\";\nimport { WebSocketServer } from \"ws\";\nimport {\n\tcreateServer,\n\tgetRemoteAddress,\n\tresolveSSL,\n\tshowServerListeningMessage\n} from \"insite-common/backend\";\nimport { isQuietSymbol, requestListenersSymbol } from \"./symbols\";\nimport { WSServerClient } from \"./WSServerClient\";\nimport type { Options, RequestListener } from \"./types\";\n\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n\nexport class WSServer<WSSC extends WSServerClient = WSServerClient> extends WebSocketServer<typeof WSServerClient> {\n\tconstructor(options: Options<WSSC>) {\n\t\tconst {\n\t\t\tssl: _,\n\t\t\tport,\n\t\t\tserver = createServer(WSServer.makeProps(options)),\n\t\t\tquiet = false,\n\t\t\t...wssOptions\n\t\t} = options;\n\t\t\n\t\tsuper({\n\t\t\tWebSocket: WSServerClient,\n\t\t\t...wssOptions,\n\t\t\tserver\n\t\t});\n\t\t\n\t\tthis.server = server;\n\t\t\n\t\tif (options.server)\n\t\t\tvoid new Promise<void>(resolve => void (\n\t\t\t\tserver.listening ?\n\t\t\t\t\tresolve() :\n\t\t\t\t\tserver.on(\"listening\", resolve)\n\t\t\t)).then(() => showServerListeningMessage(this));\n\t\telse\n\t\t\tthis.server.listen(\n\t\t\t\ttypeof port == \"string\" ?\n\t\t\t\t\tNumber.parseInt(port) :\n\t\t\t\t\tport ?? (this.isS ? 443 : 80),\n\t\t\t\t() => showServerListeningMessage(this)\n\t\t\t);\n\t\t\n\t\tthis[isQuietSymbol] = quiet;\n\t\t\n\t\tthis.on(\"connection\", this.#handleConnection);\n\t\t\n\t\tif (!this[isQuietSymbol]) {\n\t\t\tthis.on(\"error\", (error: Error) => console.error(`${this.icon}\u2757\uFE0F WS Server:`, error));\n\t\t\t\n\t\t\tthis.on(\"close\", () => console.error(`${this.icon}\u2757\uFE0F WS Server closed`));\n\t\t}\n\t\t\n\t}\n\t\n\ton(event: \"connection\", callback: (this: this, socket: WSSC, request: http.IncomingMessage) => void): this;\n\ton(event: \"error\", callback: (this: this, error: Error) => void): this;\n\ton(event: \"headers\", callback: (this: this, headers: string[], request: http.IncomingMessage) => void): this;\n\ton(event: \"close\" | \"listening\", callback: (this: this) => void): this;\n\t\n\ton(event: \"client-connect\", listener: (this: this, wscc: WSSC, request: http.IncomingMessage) => void): this;\n\ton(event: \"client-error\", listener: (this: this, wscc: WSSC, error: Error | undefined) => void): this;\n\ton(event: `client-${string}`, listener: (this: this, wscc: WSSC, ...args: any[]) => void): this;\n\t\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this {\n\t\treturn super.on(event, listener as (this: WebSocketServer, ...args: any[]) => void);\n\t}\n\t\n\treadonly isWebSocketServer = true;\n\treadonly isWebSocketServerClient = false;\n\treadonly isWebSocket = false;\n\t\n\ticon = \"\uD83D\uDD0C\";\n\tname = \"WS\";\n\tget protocol() {\n\t\treturn `ws${this.isS ? \"s\" : \"\"}`;\n\t}\n\t\n\tserver;\n\t\n\tget isS() {\n\t\treturn \"setSecureContext\" in this.server;\n\t}\n\t\n\t[isQuietSymbol]: boolean;\n\t\n\t[requestListenersSymbol] = new Map<string, RequestListener<WSSC>>();\n\t\n\taddRequestListener(kind: string, listener: RequestListener<WSSC>) {\n\t\tthis[requestListenersSymbol].set(kind, listener);\n\t\t\n\t\treturn this;\n\t}\n\t\n\t/**\n\t * Alias for `addRequestListener()`\n\t */\n\tonRequest = this.addRequestListener;\n\t\n\tremoveRequestListener(kind: string) {\n\t\tthis[requestListenersSymbol].delete(kind);\n\t\t\n\t\treturn this;\n\t}\n\t\n\t/**\n\t * Alias for `removeRequestListener()`\n\t */\n\toffRequest = this.removeRequestListener;\n\t\n\t#handleConnection(wssc: WSSC, request: http.IncomingMessage) {\n\t\t\n\t\t/* Compatibility with Bun */\n\t\tif (!(wssc instanceof WSServerClient))\n\t\t\tWSServerClient.extend.call(wssc);\n\t\t\n\t\tObject.assign(wssc, {\n\t\t\twss: this,\n\t\t\tuserAgent: request.headers[\"user-agent\"] ?? \"\",\n\t\t\tremoteAddress: getRemoteAddress(request)\n\t\t});\n\t\t\n\t\tWSServerClient.handleConnect.call(wssc);\n\t\t\n\t\tthis.emit(\"client-connect\", wssc, request);\n\t\t\n\t\tif (process.env.NODE_ENV === \"development\" && !this[isQuietSymbol])\n\t\t\tconsole.info(\n\t\t\t\t`${this.icon} WS Server:`,\n\t\t\t\t\"user\" in wssc ? `\\x1B[1m${(wssc.user as { email: string }).email}\\x1B[0m` : \"\\x1B[3manonymous\\x1B[0m\",\n\t\t\t\t\"connected\"\n\t\t\t);\n\t\t\n\t}\n\t\n\t\n\tstatic makeProps<SWSSC extends WSServerClient>({ ssl }: Options<SWSSC>): http.ServerOptions | https.ServerOptions {\n\t\treturn {\n\t\t\t...resolveSSL(ssl)\n\t\t};\n\t}\n\t\n}\n", "export const isQuietSymbol = Symbol(\"isQuiet\");\nexport const requestListenersSymbol = Symbol(\"requestListeners\");\nexport const heartbeatIntervalSymbol = Symbol(\"heartbeatInterval\");\nexport const heartbeatTimeoutSymbol = Symbol(\"heartbeatTimeout\");\nexport const defibSymbol = Symbol(\"defib\");\nexport const heartbeatTsSymbol = Symbol(\"heartbeatTs\");\nexport const requestCounterSymbol = Symbol(\"requestCounter\");\n", "import { ClientRequest, IncomingMessage } from \"node:http\";\nimport { RawData, WebSocket } from \"ws\";\nimport { handleMongoError } from \"@nesvet/n\";\nimport {\n\tHEARTBEAT_GAP,\n\tHEARTBEAT_INTERVAL,\n\tREQUEST_COUNTER_LIMIT,\n\tREQUEST_HEADERS\n} from \"../common\";\nimport {\n\tdefibSymbol,\n\theartbeatIntervalSymbol,\n\theartbeatTimeoutSymbol,\n\theartbeatTsSymbol,\n\tisQuietSymbol,\n\trequestCounterSymbol,\n\trequestListenersSymbol\n} from \"./symbols\";\nimport { WSServer } from \"./WSServer\";\n\n\n/* eslint-disable @typescript-eslint/no-explicit-any */\n\n\nexport class WSServerClient extends WebSocket {\n\ton(event: \"close\", listener: (this: this, code: number, reason: Buffer) => void): this;\n\ton(event: \"error\", listener: (this: this, error: Error) => void): this;\n\ton(event: \"upgrade\", listener: (this: this, request: IncomingMessage) => void): this;\n\ton(event: \"message\", listener: (this: this, data: RawData, isBinary: boolean) => void): this;\n\ton(event: \"open\", listener: (this: this) => void): this;\n\ton(event: \"unexpected-response\", listener: (this: this, request: ClientRequest, response: IncomingMessage) => void): this;\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this;\n\ton(event: string | symbol, listener: (this: this, ...args: any[]) => void): this {\n\t\treturn super.on(event, listener as (this: WebSocket, ...args: any[]) => void);\n\t}\n\t\n\tget isConnecting() {\n\t\treturn this.readyState === this.CONNECTING;\n\t}\n\t\n\tget isOpen() {\n\t\treturn this.readyState === this.OPEN;\n\t}\n\t\n\tget isClosing() {\n\t\treturn this.readyState === this.CLOSING;\n\t}\n\t\n\tget isClosed() {\n\t\treturn this.readyState === this.CLOSED;\n\t}\n\t\n\treadonly isWebSocketServerClient = true;\n\treadonly isWebSocketServer = false;\n\treadonly isWebSocket = false;\n\t\n\twss!: WSServer<this>;\n\t\n\tuserAgent = \"\";\n\t\n\tremoteAddress = \"\";\n\t\n\t[heartbeatTimeoutSymbol]?: ReturnType<typeof setTimeout>;\n\t\n\t[defibSymbol]() {\n\t\t\n\t\tclearTimeout(this[heartbeatTimeoutSymbol]);\n\t\t\n\t\tthis[heartbeatTimeoutSymbol] = setTimeout(\n\t\t\t() => this.terminate(),\n\t\t\tHEARTBEAT_INTERVAL + HEARTBEAT_GAP\n\t\t);\n\t\t\n\t}\n\t\n\tlatency = 0;\n\t\n\t[heartbeatTsSymbol]?: number;\n\t\n\t[heartbeatIntervalSymbol] = WSServerClient.makeHeartbeatInterval.call(this);\n\t\n\tsendMessage(...args: unknown[]) {\n\t\treturn this.send(JSON.stringify(args));\n\t}\n\t\n\t[requestCounterSymbol]: number = 0;\n\t\n\tsendRequest(...args: unknown[]) {\n\t\t\n\t\tconst id = this[requestCounterSymbol]++;\n\t\t\n\t\tif (this[requestCounterSymbol] >= REQUEST_COUNTER_LIMIT)\n\t\t\tthis[requestCounterSymbol] = 0;\n\t\t\n\t\tconst eventName = `message:${REQUEST_HEADERS.RESPONSE}-${id}`;\n\t\t\n\t\tif (typeof args.at(-1) == \"function\") {\n\t\t\tthis.once(eventName, args.splice(-1, 1)[0] as () => void);\n\t\t\t\n\t\t\tthis.sendMessage(REQUEST_HEADERS.REQUEST, id, ...args);\n\t\t\t\n\t\t\treturn this;\n\t\t}\n\t\t\n\t\treturn new Promise((resolve, reject) => {\n\t\t\t\n\t\t\tthis.once(eventName, (error, result) => {\n\t\t\t\tif (error) {\n\t\t\t\t\tconst { message, ...restProps } = error;\n\t\t\t\t\treject(Object.assign(new Error(message), restProps) as Error);\n\t\t\t\t} else\n\t\t\t\t\tresolve(result);\n\t\t\t\t\n\t\t\t});\n\t\t\t\n\t\t\tthis.sendMessage(REQUEST_HEADERS.REQUEST, id, ...args);\n\t\t\t\n\t\t});\n\t}\n\t\n\t\n\tstatic makeHeartbeatInterval(this: WSServerClient) {\n\t\treturn setInterval(() => {\n\t\t\t\n\t\t\tthis[heartbeatTsSymbol] = Date.now();\n\t\t\t\n\t\t\tthis.send(\"\");\n\t\t\t\n\t\t}, HEARTBEAT_INTERVAL);\n\t}\n\t\n\t/* Compatibility with Bun */\n\tstatic extend(this: WSServerClient) {\n\t\t\n\t\tObject.defineProperties(this, {\n\t\t\tisConnecting: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isConnecting\")!.get\n\t\t\t},\n\t\t\tisOpen: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isOpen\")!.get\n\t\t\t},\n\t\t\tisClosing: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isClosing\")!.get\n\t\t\t},\n\t\t\tisClosed: {\n\t\t\t\tget: Object.getOwnPropertyDescriptor(WSServerClient.prototype, \"isClosed\")!.get\n\t\t\t}\n\t\t});\n\t\t\n\t\tconst { terminate } = this;\n\t\t\n\t\tObject.assign(this, {\n\t\t\tisWebSocketServerClient: true,\n\t\t\tisWebSocketServer: false,\n\t\t\tisWebSocket: false,\n\t\t\t[defibSymbol]: WSServerClient.prototype[defibSymbol],\n\t\t\tlatency: 0,\n\t\t\t[heartbeatIntervalSymbol]: WSServerClient.makeHeartbeatInterval.call(this),\n\t\t\tsendMessage: WSServerClient.prototype.sendMessage,\n\t\t\tsendRequest: WSServerClient.prototype.sendRequest,\n\t\t\tterminate() { /* Workaround Bun's WebSocket#terminate bug */\n\t\t\t\t\n\t\t\t\ttry {\n\t\t\t\t\tterminate.call(this);\n\t\t\t\t} catch (error) {\n\t\t\t\t\tconsole.error(error);\n\t\t\t\t}\n\t\t\t\t\n\t\t\t}\n\t\t});\n\t\t\n\t}\n\t\n\tstatic handleConnect(this: WSServerClient) {\n\t\t\n\t\tthis.on(\"message\", WSServerClient.handleMessage);\n\t\tthis.on(\"error\", WSServerClient.handleError);\n\t\tthis.on(\"close\", WSServerClient.handleClose);\n\t\t\n\t\tthis[defibSymbol]();\n\t\t\n\t}\n\t\n\tstatic handleMessage(this: WSServerClient, data: RawData) {\n\t\tconst message = data.toString();// eslint-disable-line @typescript-eslint/no-base-to-string\n\t\t\n\t\tthis[defibSymbol]();\n\t\t\n\t\tif (message)\n\t\t\tif (message === \"?\")\n\t\t\t\tthis.send(\"!\");\n\t\t\telse\n\t\t\t\ttry {\n\t\t\t\t\tconst [ kind, ...rest ] = JSON.parse(message);\n\t\t\t\t\t\n\t\t\t\t\tswitch (kind) {\n\t\t\t\t\t\tcase REQUEST_HEADERS.REQUEST:\n\t\t\t\t\t\t\tvoid WSServerClient.handleRequest.call(this, ...rest as [ id: number, kind: string, ...rest: unknown[] ]);\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthis.emit(`message:${kind}`, ...rest);\n\t\t\t\t\t\t\tthis.wss.emit(\"client-message\", this, kind, ...rest);\n\t\t\t\t\t\t\tthis.wss.emit(`client-message:${kind}`, this, ...rest);\n\t\t\t\t\t}\n\t\t\t\t} catch (error) {\n\t\t\t\t\tWSServerClient.handleError.call(this, error as Error);\n\t\t\t\t}\n\t\telse if (this[heartbeatTsSymbol]) {\n\t\t\tthis.latency = Date.now() - this[heartbeatTsSymbol];\n\t\t\tthis[heartbeatTsSymbol] = undefined;\n\t\t}\n\t\t\n\t}\n\t\n\tstatic async handleRequest(this: WSServerClient, id: number, kind: string, ...rest: unknown[]) {\n\t\tconst listener = this.wss[requestListenersSymbol].get(kind);\n\t\t\n\t\tlet result;\n\t\tlet requestError = null;\n\t\t\n\t\tif (listener)\n\t\t\ttry {\n\t\t\t\tresult = await listener.call(this.wss, this, ...rest);\n\t\t\t} catch (error) {\n\t\t\t\tif (process.env.NODE_ENV === \"development\")\n\t\t\t\t\thandleMongoError(error);\n\t\t\t\t\n\t\t\t\tif (error instanceof Error) {\n\t\t\t\t\tconst { message, ...restProps } = error;\n\t\t\t\t\trequestError = { message, ...restProps };\n\t\t\t\t}\n\t\t\t\t\n\t\t\t\tif (process.env.NODE_ENV === \"development\" && !this.wss[isQuietSymbol])\n\t\t\t\t\tconsole.error(`${this.wss.icon}\u2757\uFE0F WS Server request \"${kind}\" (${id}):`, error);\n\t\t\t}\n\t\telse\n\t\t\trequestError = { message: `Unknown request kind \"${kind}\"` };\n\t\t\n\t\tthis.sendMessage(`${REQUEST_HEADERS.RESPONSE}-${id}`, requestError, result);\n\t\t\n\t}\n\t\n\tstatic handleError(this: WSServerClient, error: Error | Event | undefined) {\n\t\tif (error instanceof Event)\n\t\t\terror = undefined;\n\t\t\n\t\tif (!this.wss[isQuietSymbol])\n\t\t\tconsole.error(\n\t\t\t\t`${this.wss.icon}\u2757\uFE0F WS Server client`,\n\t\t\t\t\"user\" in this ? `\\x1B[1m${(this.user as { email: string }).email}\\x1B[0m:` : \"\\x1B[3manonymous\\x1B[0m:\",\n\t\t\t\terror\n\t\t\t);\n\t\t\n\t\tthis.wss.emit(\"client-error\", this, error);\n\t\t\n\t}\n\t\n\tstatic handleClose(this: WSServerClient, code: number, reason: Buffer) {\n\t\t\n\t\tif (process.env.NODE_ENV === \"development\") {\n\t\t\tconst reasonString = reason.toString();\n\t\t\t\n\t\t\tif (!this.wss[isQuietSymbol])\n\t\t\t\tconsole.info(\n\t\t\t\t\t`${this.wss.icon} WS Server:`,\n\t\t\t\t\t\"user\" in this ? `\\x1B[1m${(this.user as { email: string }).email}\\x1B[0m` : \"\\x1B[3manonymous\\x1B[0m\",\n\t\t\t\t\t`disconnected ${code ? `with code ${code}` : \"\"} ${code && reasonString ? \"and \" : \"\"}${reasonString ? `reason \"${reasonString}\"` : \"\"}`\n\t\t\t\t);\n\t\t}\n\t\t\n\t\tclearTimeout(this[heartbeatTimeoutSymbol]);\n\t\tclearInterval(this[heartbeatIntervalSymbol]);\n\t\t\n\t\tthis.wss.emit(\"client-close\", this);\n\t\tthis.wss.emit(\"client-closed\", this);\n\t\t\n\t}\n\t\n}\n", "export const HEARTBEAT_INTERVAL = 5_000;\nexport const HEARTBEAT_GAP = 30_000;\nexport const RESPONSIVENESS_TIMEOUT = 1000;\nexport const OFFLINE_TIMEOUT = 5000;\nexport const REQUEST_COUNTER_LIMIT = Number.MAX_SAFE_INTEGER / 2;\n\nexport const CODES = {\n\tNORMAL: 1000,\n\tPROTOCOL_ERROR: 1002,\n\tABNORMAL: 1006,\n\tNO_HEARTBEAT: 3000,\n\tOFFLINE: 3001,\n\tMANUAL: 3500,\n\tREOPEN: 4000\n} as const;\n\nexport const NON_RECONNECTABLE_CODES = [\n\tCODES.PROTOCOL_ERROR,\n\tCODES.MANUAL,\n\tCODES.REOPEN\n];\n\nexport const REQUEST_HEADERS = {\n\tREQUEST: \"~r-q\",\n\tRESPONSE: \"~r-s\"\n};\n"],
|
|
5
|
+
"mappings": "AAEA,OAAS,mBAAAA,MAAuB,KAChC,OACC,gBAAAC,EACA,oBAAAC,EACA,cAAAC,EACA,8BAAAC,MACM,wBCRA,IAAMC,EAAgB,OAAO,SAAS,EAChCC,EAAyB,OAAO,kBAAkB,EAClDC,EAA0B,OAAO,mBAAmB,EACpDC,EAAyB,OAAO,kBAAkB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AKD,CAAsB,EAAE,OAAOC,CAAI,EAEjC,IACR,CAKA,WAAa,KAAK,sBAElBL,GAAkBM,EAAYC,EAA+B,CAGtDD,aAAgBV,GACrBA,EAAe,OAAO,KAAKU,CAAI,EAEhC,OAAO,OAAOA,EAAM,CACnB,IAAK,KACL,UAAWC,EAAQ,QAAQ,YAAY,GAAK,GAC5C,cAAeC,EAAiBD,CAAO,CACxC,CAAC,EAEDX,EAAe,cAAc,KAAKU,CAAI,EAEtC,KAAK,KAAK,iBAAkBA,EAAMC,CAAO,EAErC,QAAQ,IAAI,WAAa,eAAiB,CAAC,KAAKR,CAAa,GAChE,QAAQ,KACP,GAAG,KAAK,IAAI,cACZ,SAAUO,EAAO,UAAWA,EAAK,KAA2B,KAAK,UAAY,0BAC7E,WACD,CAEF,CAGA,OAAO,UAAwC,CAAE,IAAAG,CAAI,EAA6D,CACjH,MAAO,CACN,GAAGC,EAAWD,CAAG,CAClB,CACD,CAED",
|
|
6
|
+
"names": ["WebSocketServer", "createServer", "getRemoteAddress", "resolveSSL", "showServerListeningMessage", "isQuietSymbol", "requestListenersSymbol", "heartbeatIntervalSymbol", "heartbeatTimeoutSymbol", "defibSymbol", "heartbeatTsSymbol", "requestCounterSymbol", "WebSocket", "handleMongoError", "REQUEST_COUNTER_LIMIT", "CODES", "NON_RECONNECTABLE_CODES", "REQUEST_HEADERS", "WSServerClient", "_WSServerClient", "WebSocket", "event", "listener", "heartbeatTimeoutSymbol", "defibSymbol", "heartbeatTsSymbol", "heartbeatIntervalSymbol", "args", "requestCounterSymbol", "id", "REQUEST_COUNTER_LIMIT", "eventName", "REQUEST_HEADERS", "resolve", "reject", "error", "result", "message", "restProps", "terminate", "data", "kind", "rest", "requestListenersSymbol", "requestError", "handleMongoError", "isQuietSymbol", "code", "reason", "reasonString", "WSServer", "_WSServer", "WebSocketServer", "options", "_", "port", "server", "createServer", "quiet", "wssOptions", "WSServerClient", "resolve", "showServerListeningMessage", "isQuietSymbol", "#handleConnection", "error", "event", "listener", "requestListenersSymbol", "kind", "wssc", "request", "getRemoteAddress", "ssl", "resolveSSL"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "insite-ws",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"description": "inSite WebSockets",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"./client": "./dist/client/index.js",
|
|
9
9
|
"./client/node": "./dist/client/node/index.js"
|
|
10
10
|
},
|
|
11
|
-
"packageManager": "bun@1.2.
|
|
11
|
+
"packageManager": "bun@1.2.13",
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
14
14
|
"build": "bun .conveyer.js",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@nesvet/conveyer": "^2.5.2",
|
|
36
36
|
"@nesvet/eslint-config": "^2.1.1",
|
|
37
|
-
"@types/node": "^22.15.
|
|
37
|
+
"@types/node": "^22.15.18",
|
|
38
38
|
"@types/ws": "^8.18.1",
|
|
39
39
|
"typescript": "^5.8.3"
|
|
40
40
|
}
|