@callstack/repack-dev-server 5.0.0-next.2 → 5.0.0-rc.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/CHANGELOG.md +17 -3
- package/README.md +1 -1
- package/dist/createServer.d.ts +1 -2
- package/dist/createServer.js +11 -22
- package/dist/createServer.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/plugins/api/apiPlugin.js.map +1 -1
- package/dist/plugins/api/index.js.map +1 -1
- package/dist/plugins/compiler/compilerPlugin.js +16 -14
- package/dist/plugins/compiler/compilerPlugin.js.map +1 -1
- package/dist/plugins/compiler/index.js.map +1 -1
- package/dist/plugins/compiler/types.d.ts +4 -3
- package/dist/plugins/compiler/types.js.map +1 -1
- package/dist/plugins/devtools/devtoolsPlugin.js +5 -29
- package/dist/plugins/devtools/devtoolsPlugin.js.map +1 -1
- package/dist/plugins/devtools/index.js.map +1 -1
- package/dist/plugins/favicon/faviconPlugin.js +2 -2
- package/dist/plugins/favicon/faviconPlugin.js.map +1 -1
- package/dist/plugins/favicon/index.js.map +1 -1
- package/dist/plugins/multipart/index.js.map +1 -1
- package/dist/plugins/multipart/multipartPlugin.js +1 -1
- package/dist/plugins/multipart/multipartPlugin.js.map +1 -1
- package/dist/plugins/multipart/types.d.ts +1 -2
- package/dist/plugins/multipart/types.js.map +1 -1
- package/dist/plugins/symbolicate/Symbolicator.d.ts +1 -1
- package/dist/plugins/symbolicate/Symbolicator.js +7 -8
- package/dist/plugins/symbolicate/Symbolicator.js.map +1 -1
- package/dist/plugins/symbolicate/index.js.map +1 -1
- package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +1 -1
- package/dist/plugins/symbolicate/types.d.ts +0 -1
- package/dist/plugins/symbolicate/types.js.map +1 -1
- package/dist/plugins/wss/WebSocketRouter.d.ts +1 -1
- package/dist/plugins/wss/WebSocketRouter.js.map +1 -1
- package/dist/plugins/wss/WebSocketServer.d.ts +4 -7
- package/dist/plugins/wss/WebSocketServer.js.map +1 -1
- package/dist/plugins/wss/WebSocketServerAdapter.d.ts +1 -4
- package/dist/plugins/wss/WebSocketServerAdapter.js +2 -2
- package/dist/plugins/wss/WebSocketServerAdapter.js.map +1 -1
- package/dist/plugins/wss/index.js.map +1 -1
- package/dist/plugins/wss/servers/WebSocketApiServer.d.ts +2 -2
- package/dist/plugins/wss/servers/WebSocketApiServer.js.map +1 -1
- package/dist/plugins/wss/servers/WebSocketDevClientServer.d.ts +1 -1
- package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +1 -1
- package/dist/plugins/wss/servers/WebSocketEventsServer.d.ts +2 -2
- package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +1 -1
- package/dist/plugins/wss/servers/WebSocketHMRServer.d.ts +4 -5
- package/dist/plugins/wss/servers/WebSocketHMRServer.js +1 -1
- package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +1 -1
- package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +2 -3
- package/dist/plugins/wss/servers/WebSocketMessageServer.js +7 -7
- package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +1 -1
- package/dist/plugins/wss/types.d.ts +2 -5
- package/dist/plugins/wss/types.js.map +1 -1
- package/dist/plugins/wss/wssPlugin.d.ts +4 -6
- package/dist/plugins/wss/wssPlugin.js +4 -8
- package/dist/plugins/wss/wssPlugin.js.map +1 -1
- package/dist/types.d.ts +15 -5
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -1
- package/package.json +6 -19
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.d.ts +0 -64
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.js +0 -142
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","names":[],"sources":["../../../src/plugins/symbolicate/types.ts"],"sourcesContent":["/**\n * Raw React Native stack frame.\n */\nexport interface ReactNativeStackFrame {\n lineNumber: number | null;\n column: number | null;\n file: string | null;\n methodName: string;\n}\n\n/**\n * React Native stack frame used as input when processing by {@link Symbolicator}.\n */\nexport interface InputStackFrame extends ReactNativeStackFrame {\n file: string;\n}\n\n/**\n * Final symbolicated stack frame.\n */\nexport interface StackFrame extends InputStackFrame {\n collapse: boolean;\n}\n\n/**\n * Represents [@babel/core-frame](https://babeljs.io/docs/en/babel-code-frame).\n */\nexport interface CodeFrame {\n content: string;\n location: {\n row: number;\n column: number;\n };\n fileName: string;\n}\n\n/**\n * Represents results of running {@link process} method on {@link Symbolicator} instance.\n */\nexport interface SymbolicatorResults {\n codeFrame: CodeFrame | null;\n stack: StackFrame[];\n}\n\n/**\n * Delegate with implementation for symbolication functions.\n */\nexport interface SymbolicatorDelegate {\n /**\n * Get source code of file in the URL.\n *\n * @param fileUrl A full URL pointing to a file.\n */\n getSource: (fileUrl: string) => Promise<string | Buffer>;\n\n /**\n * Get source map for the file in the URL.\n *\n * @param fileUrl A full (usually `http:`) URL pointing to a compiled file.\n * The URL points to a file for which to return source map, not to the source map file itself,\n * e.g: `http://localhost:8081/index.bundle?platform=ios`.\n */\n getSourceMap: (fileUrl: string) => Promise<string | Buffer>;\n\n /**\n * Check if given stack frame should be included in the new symbolicated stack.\n *\n * @param frame Stack frame to check.\n */\n shouldIncludeFrame: (frame: StackFrame) => boolean;\n}\n"],"mappings":""}
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../../../src/plugins/symbolicate/types.ts"],"sourcesContent":["/**\n * Raw React Native stack frame.\n */\nexport interface ReactNativeStackFrame {\n lineNumber: number | null;\n column: number | null;\n file: string | null;\n methodName: string;\n}\n\n/**\n * React Native stack frame used as input when processing by {@link Symbolicator}.\n */\nexport interface InputStackFrame extends ReactNativeStackFrame {\n file: string;\n}\n\n/**\n * Final symbolicated stack frame.\n */\nexport interface StackFrame extends InputStackFrame {\n collapse: boolean;\n}\n\n/**\n * Represents [@babel/core-frame](https://babeljs.io/docs/en/babel-code-frame).\n */\nexport interface CodeFrame {\n content: string;\n location: {\n row: number;\n column: number;\n };\n fileName: string;\n}\n\n/**\n * Represents results of running {@link process} method on {@link Symbolicator} instance.\n */\nexport interface SymbolicatorResults {\n codeFrame: CodeFrame | null;\n stack: StackFrame[];\n}\n\n/**\n * Delegate with implementation for symbolication functions.\n */\nexport interface SymbolicatorDelegate {\n /**\n * Get source code of file in the URL.\n *\n * @param fileUrl A full URL pointing to a file.\n */\n getSource: (fileUrl: string) => Promise<string | Buffer>;\n\n /**\n * Get source map for the file in the URL.\n *\n * @param fileUrl A full (usually `http:`) URL pointing to a compiled file.\n * The URL points to a file for which to return source map, not to the source map file itself,\n * e.g: `http://localhost:8081/index.bundle?platform=ios`.\n */\n getSourceMap: (fileUrl: string) => Promise<string | Buffer>;\n\n /**\n * Check if given stack frame should be included in the new symbolicated stack.\n *\n * @param frame Stack frame to check.\n */\n shouldIncludeFrame: (frame: StackFrame) => boolean;\n}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { FastifyInstance } from 'fastify';
|
|
2
|
-
import { WebSocketServerInterface } from './types';
|
|
2
|
+
import type { WebSocketServerInterface } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Class for creating a WebSocket router to forward connections to the
|
|
5
5
|
* respective {@link WebSocketServer} as long as the connection is accepted for the upgrade by the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketRouter.js","names":["WebSocketRouter","servers","constructor","fastify","server","on","request","socket","head","pathname","URL","url","matched","shouldUpgrade","upgrade","log","debug","msg","destroy","registerServer","push"],"sources":["../../../src/plugins/wss/WebSocketRouter.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport type { Socket } from 'node:net';\nimport type { FastifyInstance } from 'fastify';\nimport { WebSocketServerInterface } from './types';\n\n/**\n * Class for creating a WebSocket router to forward connections to the\n * respective {@link WebSocketServer} as long as the connection is accepted for the upgrade by the\n * server.\n *\n * If the connection is not accepted by any `WebSocketServer`, it will be destroyed to avoid\n * creating handling connections and potentially throwing `ECONNRESET` errors.\n *\n * @category Development server\n */\nexport class WebSocketRouter {\n /** The list of all register WebSocket servers. */\n protected servers: WebSocketServerInterface[] = [];\n\n /**\n * Create new instance of `WebSocketRouter` and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket router to.\n */\n constructor(private fastify: FastifyInstance) {\n this.fastify.server.on(\n 'upgrade',\n (request: IncomingMessage, socket: Socket, head: Buffer) => {\n const { pathname } = new URL(request.url || '', 'http://localhost');\n let matched = false;\n for (const server of this.servers) {\n if (server.shouldUpgrade(pathname)) {\n matched = true;\n server.upgrade(request, socket, head);\n break;\n }\n }\n\n if (!matched) {\n this.fastify.log.debug({\n msg: 'Destroying socket connection as no server was matched',\n pathname,\n });\n socket.destroy();\n }\n }\n );\n }\n\n /**\n * Register a new {@link WebSocketServer}. New connection will now\n * check if the given server will accept them and forward them.\n *\n * @param server WebSocket server to register.\n * @returns The same instance of the WebSocket server after it's been registered.\n */\n registerServer<T extends WebSocketServerInterface>(server: T): T {\n this.servers.push(server);\n return server;\n }\n}\n"],"mappings":"AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,eAAe,CAAC;EAC3B;EACUC,OAAO,GAA+B,EAAE;;EAElD;AACF;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAASC,OAAwB,EAAE;IAAA,KAA1BA,OAAwB,GAAxBA,OAAwB;IAC1C,IAAI,CAACA,OAAO,CAACC,MAAM,CAACC,EAAE,CACpB,SAAS,EACT,CAACC,OAAwB,EAAEC,MAAc,EAAEC,IAAY,KAAK;MAC1D,MAAM;QAAEC;MAAS,CAAC,GAAG,IAAIC,GAAG,CAACJ,OAAO,CAACK,GAAG,IAAI,EAAE,EAAE,kBAAkB,CAAC;MACnE,IAAIC,OAAO,GAAG,KAAK;MACnB,KAAK,MAAMR,MAAM,IAAI,IAAI,CAACH,OAAO,EAAE;QACjC,IAAIG,MAAM,CAACS,aAAa,CAACJ,QAAQ,CAAC,EAAE;UAClCG,OAAO,GAAG,IAAI;UACdR,MAAM,CAACU,OAAO,CAACR,OAAO,EAAEC,MAAM,EAAEC,IAAI,CAAC;UACrC;QACF;MACF;MAEA,IAAI,CAACI,OAAO,EAAE;QACZ,IAAI,CAACT,OAAO,CAACY,GAAG,CAACC,KAAK,CAAC;UACrBC,GAAG,EAAE,uDAAuD;UAC5DR;QACF,CAAC,CAAC;QACFF,MAAM,CAACW,OAAO,CAAC,CAAC;MAClB;IACF,CACF,CAAC;EACH;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,cAAcA,CAAqCf,MAAS,EAAK;IAC/D,IAAI,CAACH,OAAO,CAACmB,IAAI,CAAChB,MAAM,CAAC;IACzB,OAAOA,MAAM;EACf;AACF"}
|
|
1
|
+
{"version":3,"file":"WebSocketRouter.js","names":["WebSocketRouter","servers","constructor","fastify","server","on","request","socket","head","pathname","URL","url","matched","shouldUpgrade","upgrade","log","debug","msg","destroy","registerServer","push"],"sources":["../../../src/plugins/wss/WebSocketRouter.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport type { Socket } from 'node:net';\nimport type { FastifyInstance } from 'fastify';\nimport type { WebSocketServerInterface } from './types';\n\n/**\n * Class for creating a WebSocket router to forward connections to the\n * respective {@link WebSocketServer} as long as the connection is accepted for the upgrade by the\n * server.\n *\n * If the connection is not accepted by any `WebSocketServer`, it will be destroyed to avoid\n * creating handling connections and potentially throwing `ECONNRESET` errors.\n *\n * @category Development server\n */\nexport class WebSocketRouter {\n /** The list of all register WebSocket servers. */\n protected servers: WebSocketServerInterface[] = [];\n\n /**\n * Create new instance of `WebSocketRouter` and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket router to.\n */\n constructor(private fastify: FastifyInstance) {\n this.fastify.server.on(\n 'upgrade',\n (request: IncomingMessage, socket: Socket, head: Buffer) => {\n const { pathname } = new URL(request.url || '', 'http://localhost');\n let matched = false;\n for (const server of this.servers) {\n if (server.shouldUpgrade(pathname)) {\n matched = true;\n server.upgrade(request, socket, head);\n break;\n }\n }\n\n if (!matched) {\n this.fastify.log.debug({\n msg: 'Destroying socket connection as no server was matched',\n pathname,\n });\n socket.destroy();\n }\n }\n );\n }\n\n /**\n * Register a new {@link WebSocketServer}. New connection will now\n * check if the given server will accept them and forward them.\n *\n * @param server WebSocket server to register.\n * @returns The same instance of the WebSocket server after it's been registered.\n */\n registerServer<T extends WebSocketServerInterface>(server: T): T {\n this.servers.push(server);\n return server;\n }\n}\n"],"mappings":"AAKA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMA,eAAe,CAAC;EAC3B;EACUC,OAAO,GAA+B,EAAE;;EAElD;AACF;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAASC,OAAwB,EAAE;IAAA,KAA1BA,OAAwB,GAAxBA,OAAwB;IAC1C,IAAI,CAACA,OAAO,CAACC,MAAM,CAACC,EAAE,CACpB,SAAS,EACT,CAACC,OAAwB,EAAEC,MAAc,EAAEC,IAAY,KAAK;MAC1D,MAAM;QAAEC;MAAS,CAAC,GAAG,IAAIC,GAAG,CAACJ,OAAO,CAACK,GAAG,IAAI,EAAE,EAAE,kBAAkB,CAAC;MACnE,IAAIC,OAAO,GAAG,KAAK;MACnB,KAAK,MAAMR,MAAM,IAAI,IAAI,CAACH,OAAO,EAAE;QACjC,IAAIG,MAAM,CAACS,aAAa,CAACJ,QAAQ,CAAC,EAAE;UAClCG,OAAO,GAAG,IAAI;UACdR,MAAM,CAACU,OAAO,CAACR,OAAO,EAAEC,MAAM,EAAEC,IAAI,CAAC;UACrC;QACF;MACF;MAEA,IAAI,CAACI,OAAO,EAAE;QACZ,IAAI,CAACT,OAAO,CAACY,GAAG,CAACC,KAAK,CAAC;UACrBC,GAAG,EAAE,uDAAuD;UAC5DR;QACF,CAAC,CAAC;QACFF,MAAM,CAACW,OAAO,CAAC,CAAC;MAClB;IACF,CACF,CAAC;EACH;;EAEA;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,cAAcA,CAAqCf,MAAS,EAAK;IAC/D,IAAI,CAACH,OAAO,CAACmB,IAAI,CAAChB,MAAM,CAAC;IACzB,OAAOA,MAAM;EACf;AACF","ignoreList":[]}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/// <reference types="node" />
|
|
4
|
-
import type { IncomingMessage } from 'http';
|
|
5
|
-
import type { Socket } from 'net';
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { Socket } from 'node:net';
|
|
6
3
|
import type { FastifyInstance } from 'fastify';
|
|
7
|
-
import { ServerOptions, WebSocket, WebSocketServer as WebSocketServerImpl } from 'ws';
|
|
8
|
-
import { WebSocketServerInterface } from './types';
|
|
4
|
+
import { type ServerOptions, type WebSocket, WebSocketServer as WebSocketServerImpl } from 'ws';
|
|
5
|
+
import type { WebSocketServerInterface } from './types';
|
|
9
6
|
/**
|
|
10
7
|
* Abstract class for providing common logic (eg routing) for all WebSocket servers.
|
|
11
8
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketServer.js","names":["WebSocketServer","WebSocketServerImpl","constructor","fastify","path","wssOptions","server","noServer","on","onConnection","bind","paths","Array","isArray","shouldUpgrade","pathname","includes","upgrade","request","socket","head","handleUpgrade","webSocket","emit"],"sources":["../../../src/plugins/wss/WebSocketServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\nimport type { Socket } from 'net';\nimport type { FastifyInstance } from 'fastify';\nimport {\n ServerOptions,\n WebSocket,\n WebSocketServer as WebSocketServerImpl,\n} from 'ws';\nimport { WebSocketServerInterface } from './types';\n\n/**\n * Abstract class for providing common logic (eg routing) for all WebSocket servers.\n *\n * @category Development server\n */\nexport abstract class WebSocketServer implements WebSocketServerInterface {\n /** An instance of the underlying WebSocket server. */\n protected server: WebSocketServerImpl;\n\n /** Fastify instance from which {@link server} will receive upgrade connections. */\n protected fastify: FastifyInstance;\n\n protected paths: string[];\n\n /**\n * Create a new instance of the WebSocketServer.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to which the WebSocket will be attached to.\n * @param path Path on which this WebSocketServer will be accepting connections.\n * @param wssOptions WebSocket Server options.\n */\n constructor(\n fastify: FastifyInstance,\n path: string | string[],\n wssOptions: Omit<\n ServerOptions,\n 'noServer' | 'server' | 'host' | 'port' | 'path'\n > = {}\n ) {\n this.fastify = fastify;\n this.server = new WebSocketServerImpl({ noServer: true, ...wssOptions });\n this.server.on('connection', this.onConnection.bind(this));\n this.paths = Array.isArray(path) ? path : [path];\n }\n\n shouldUpgrade(pathname: string) {\n return this.paths.includes(pathname);\n }\n\n upgrade(request: IncomingMessage, socket: Socket, head: Buffer) {\n this.server.handleUpgrade(request, socket, head, (webSocket) => {\n this.server.emit('connection', webSocket, request);\n });\n }\n\n /**\n * Process incoming WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n * @param request Upgrade request for the connection.\n */\n abstract onConnection(socket: WebSocket, request: IncomingMessage): void;\n}\n"],"mappings":"AAGA,SAGEA,eAAe,IAAIC,mBAAmB,QACjC,IAAI;AAGX;AACA;AACA;AACA;AACA;AACA,OAAO,MAAeD,eAAe,CAAqC;EACxE;;EAGA;;EAKA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,WAAWA,CACTC,OAAwB,EACxBC,IAAuB,EACvBC,UAGC,GAAG,CAAC,CAAC,EACN;IACA,IAAI,CAACF,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACG,MAAM,GAAG,IAAIL,mBAAmB,CAAC;MAAEM,QAAQ,EAAE,IAAI;MAAE,GAAGF;IAAW,CAAC,CAAC;IACxE,IAAI,CAACC,MAAM,CAACE,EAAE,CAAC,YAAY,EAAE,IAAI,CAACC,YAAY,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1D,IAAI,CAACC,KAAK,GAAGC,KAAK,CAACC,OAAO,CAACT,IAAI,CAAC,GAAGA,IAAI,GAAG,CAACA,IAAI,CAAC;EAClD;EAEAU,aAAaA,CAACC,QAAgB,EAAE;IAC9B,OAAO,IAAI,CAACJ,KAAK,CAACK,QAAQ,CAACD,QAAQ,CAAC;EACtC;EAEAE,OAAOA,CAACC,OAAwB,EAAEC,MAAc,EAAEC,IAAY,EAAE;IAC9D,IAAI,CAACd,MAAM,CAACe,aAAa,CAACH,OAAO,EAAEC,MAAM,EAAEC,IAAI,EAAGE,SAAS,IAAK;MAC9D,IAAI,CAAChB,MAAM,CAACiB,IAAI,CAAC,YAAY,EAAED,SAAS,EAAEJ,OAAO,CAAC;IACpD,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;AAEA"}
|
|
1
|
+
{"version":3,"file":"WebSocketServer.js","names":["WebSocketServer","WebSocketServerImpl","constructor","fastify","path","wssOptions","server","noServer","on","onConnection","bind","paths","Array","isArray","shouldUpgrade","pathname","includes","upgrade","request","socket","head","handleUpgrade","webSocket","emit"],"sources":["../../../src/plugins/wss/WebSocketServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport type { Socket } from 'node:net';\nimport type { FastifyInstance } from 'fastify';\nimport {\n type ServerOptions,\n type WebSocket,\n WebSocketServer as WebSocketServerImpl,\n} from 'ws';\nimport type { WebSocketServerInterface } from './types';\n\n/**\n * Abstract class for providing common logic (eg routing) for all WebSocket servers.\n *\n * @category Development server\n */\nexport abstract class WebSocketServer implements WebSocketServerInterface {\n /** An instance of the underlying WebSocket server. */\n protected server: WebSocketServerImpl;\n\n /** Fastify instance from which {@link server} will receive upgrade connections. */\n protected fastify: FastifyInstance;\n\n protected paths: string[];\n\n /**\n * Create a new instance of the WebSocketServer.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to which the WebSocket will be attached to.\n * @param path Path on which this WebSocketServer will be accepting connections.\n * @param wssOptions WebSocket Server options.\n */\n constructor(\n fastify: FastifyInstance,\n path: string | string[],\n wssOptions: Omit<\n ServerOptions,\n 'noServer' | 'server' | 'host' | 'port' | 'path'\n > = {}\n ) {\n this.fastify = fastify;\n this.server = new WebSocketServerImpl({ noServer: true, ...wssOptions });\n this.server.on('connection', this.onConnection.bind(this));\n this.paths = Array.isArray(path) ? path : [path];\n }\n\n shouldUpgrade(pathname: string) {\n return this.paths.includes(pathname);\n }\n\n upgrade(request: IncomingMessage, socket: Socket, head: Buffer) {\n this.server.handleUpgrade(request, socket, head, (webSocket) => {\n this.server.emit('connection', webSocket, request);\n });\n }\n\n /**\n * Process incoming WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n * @param request Upgrade request for the connection.\n */\n abstract onConnection(socket: WebSocket, request: IncomingMessage): void;\n}\n"],"mappings":"AAGA,SAGEA,eAAe,IAAIC,mBAAmB,QACjC,IAAI;AAGX;AACA;AACA;AACA;AACA;AACA,OAAO,MAAeD,eAAe,CAAqC;EACxE;;EAGA;;EAKA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,WAAWA,CACTC,OAAwB,EACxBC,IAAuB,EACvBC,UAGC,GAAG,CAAC,CAAC,EACN;IACA,IAAI,CAACF,OAAO,GAAGA,OAAO;IACtB,IAAI,CAACG,MAAM,GAAG,IAAIL,mBAAmB,CAAC;MAAEM,QAAQ,EAAE,IAAI;MAAE,GAAGF;IAAW,CAAC,CAAC;IACxE,IAAI,CAACC,MAAM,CAACE,EAAE,CAAC,YAAY,EAAE,IAAI,CAACC,YAAY,CAACC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC1D,IAAI,CAACC,KAAK,GAAGC,KAAK,CAACC,OAAO,CAACT,IAAI,CAAC,GAAGA,IAAI,GAAG,CAACA,IAAI,CAAC;EAClD;EAEAU,aAAaA,CAACC,QAAgB,EAAE;IAC9B,OAAO,IAAI,CAACJ,KAAK,CAACK,QAAQ,CAACD,QAAQ,CAAC;EACtC;EAEAE,OAAOA,CAACC,OAAwB,EAAEC,MAAc,EAAEC,IAAY,EAAE;IAC9D,IAAI,CAACd,MAAM,CAACe,aAAa,CAACH,OAAO,EAAEC,MAAM,EAAEC,IAAI,EAAGE,SAAS,IAAK;MAC9D,IAAI,CAAChB,MAAM,CAACiB,IAAI,CAAC,YAAY,EAAED,SAAS,EAAEJ,OAAO,CAAC;IACpD,CAAC,CAAC;EACJ;;EAEA;AACF;AACA;AACA;AACA;AACA;AAEA","ignoreList":[]}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/// <reference types="node" />
|
|
4
1
|
import type { IncomingMessage } from 'node:http';
|
|
5
2
|
import type { Socket } from 'node:net';
|
|
6
3
|
import type { FastifyInstance } from 'fastify';
|
|
7
|
-
import type { WebSocketServer
|
|
4
|
+
import type { WebSocketServer } from 'ws';
|
|
8
5
|
import type { WebSocketServerInterface } from './types';
|
|
9
6
|
export declare class WebSocketServerAdapter implements WebSocketServerInterface {
|
|
10
7
|
private fastify;
|
|
@@ -14,8 +14,8 @@ export class WebSocketServerAdapter {
|
|
|
14
14
|
return this.path === pathname;
|
|
15
15
|
}
|
|
16
16
|
upgrade(request, socket, head) {
|
|
17
|
-
this.server
|
|
18
|
-
this.server
|
|
17
|
+
this.server?.handleUpgrade(request, socket, head, webSocket => {
|
|
18
|
+
this.server?.emit('connection', webSocket, request);
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
21
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketServerAdapter.js","names":["WebSocketServerAdapter","constructor","fastify","path","server","shouldUpgrade","pathname","log","warn","msg","upgrade","request","socket","head","handleUpgrade","webSocket","emit"],"sources":["../../../src/plugins/wss/WebSocketServerAdapter.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport type { Socket } from 'node:net';\nimport type { FastifyInstance } from 'fastify';\nimport type { WebSocketServer
|
|
1
|
+
{"version":3,"file":"WebSocketServerAdapter.js","names":["WebSocketServerAdapter","constructor","fastify","path","server","shouldUpgrade","pathname","log","warn","msg","upgrade","request","socket","head","handleUpgrade","webSocket","emit"],"sources":["../../../src/plugins/wss/WebSocketServerAdapter.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport type { Socket } from 'node:net';\nimport type { FastifyInstance } from 'fastify';\nimport type { WebSocketServer } from 'ws';\nimport type { WebSocketServerInterface } from './types';\n\nexport class WebSocketServerAdapter implements WebSocketServerInterface {\n constructor(\n private fastify: FastifyInstance,\n private path: string,\n private server?: WebSocketServer\n ) {}\n\n shouldUpgrade(pathname: string) {\n if (!this.server) {\n this.fastify.log.warn({ msg: `No handler active for ${this.path}` });\n return false;\n }\n return this.path === pathname;\n }\n\n upgrade(request: IncomingMessage, socket: Socket, head: Buffer) {\n this.server?.handleUpgrade(request, socket, head, (webSocket) => {\n this.server?.emit('connection', webSocket, request);\n });\n }\n}\n"],"mappings":"AAMA,OAAO,MAAMA,sBAAsB,CAAqC;EACtEC,WAAWA,CACDC,OAAwB,EACxBC,IAAY,EACZC,MAAwB,EAChC;IAAA,KAHQF,OAAwB,GAAxBA,OAAwB;IAAA,KACxBC,IAAY,GAAZA,IAAY;IAAA,KACZC,MAAwB,GAAxBA,MAAwB;EAC/B;EAEHC,aAAaA,CAACC,QAAgB,EAAE;IAC9B,IAAI,CAAC,IAAI,CAACF,MAAM,EAAE;MAChB,IAAI,CAACF,OAAO,CAACK,GAAG,CAACC,IAAI,CAAC;QAAEC,GAAG,EAAE,yBAAyB,IAAI,CAACN,IAAI;MAAG,CAAC,CAAC;MACpE,OAAO,KAAK;IACd;IACA,OAAO,IAAI,CAACA,IAAI,KAAKG,QAAQ;EAC/B;EAEAI,OAAOA,CAACC,OAAwB,EAAEC,MAAc,EAAEC,IAAY,EAAE;IAC9D,IAAI,CAACT,MAAM,EAAEU,aAAa,CAACH,OAAO,EAAEC,MAAM,EAAEC,IAAI,EAAGE,SAAS,IAAK;MAC/D,IAAI,CAACX,MAAM,EAAEY,IAAI,CAAC,YAAY,EAAED,SAAS,EAAEJ,OAAO,CAAC;IACrD,CAAC,CAAC;EACJ;AACF","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["default"],"sources":["../../../src/plugins/wss/index.ts"],"sourcesContent":["export { default } from './wssPlugin';\nexport * from './WebSocketServer';\nexport * from './types';\n"],"mappings":"SAASA,OAAO;AAAA;AAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["default"],"sources":["../../../src/plugins/wss/index.ts"],"sourcesContent":["export { default } from './wssPlugin';\nexport * from './WebSocketServer';\nexport * from './types';\n"],"mappings":"SAASA,OAAO;AAAA;AAAA","ignoreList":[]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { FastifyInstance } from 'fastify';
|
|
2
|
-
import WebSocket from 'ws';
|
|
1
|
+
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import type WebSocket from 'ws';
|
|
3
3
|
import { WebSocketServer } from '../WebSocketServer';
|
|
4
4
|
/**
|
|
5
5
|
* Class for creating a WebSocket server for API clients.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketApiServer.js","names":["WebSocketServer","WebSocketApiServer","clients","Map","nextClientId","constructor","fastify","send","event","data","JSON","stringify","socket","entries","onConnection","clientId","set","log","debug","msg","onClose","delete","addEventListener"],"sources":["../../../../src/plugins/wss/servers/WebSocketApiServer.ts"],"sourcesContent":["import { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server for API clients.\n * Useful to listening for compilation events and new logs.\n *\n * @category Development server\n */\nexport class WebSocketApiServer extends WebSocketServer {\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketApiServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/api');\n }\n\n /**\n * Send message to all connected API clients.\n *\n * @param event Event string or object to send.\n */\n send(event: any) {\n const data = typeof event === 'string' ? event : JSON.stringify(event);\n\n for (const [, socket] of this.clients.entries()) {\n try {\n socket.send(data);\n } catch {\n // NOOP\n }\n }\n }\n\n /**\n * Process new WebSocket connection from client application.\n *\n * @param socket Incoming client's WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n\n this.fastify.log.debug({ msg: 'API client connected', clientId });\n this.clients.set(clientId, socket);\n\n const onClose = () => {\n this.fastify.log.debug({\n msg: 'API client disconnected',\n clientId,\n });\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n }\n}\n"],"mappings":"SAESA,eAAe;AAExB;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,SAASD,eAAe,CAAC;EAC9CE,OAAO,GAAG,IAAIC,GAAG,CAAoB,CAAC;EACtCC,YAAY,GAAG,CAAC;;EAExB;AACF;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAACC,OAAwB,EAAE;IACpC,KAAK,CAACA,OAAO,EAAE,MAAM,CAAC;EACxB;;EAEA;AACF;AACA;AACA;AACA;EACEC,IAAIA,CAACC,KAAU,EAAE;IACf,MAAMC,IAAI,GAAG,OAAOD,KAAK,KAAK,QAAQ,GAAGA,KAAK,GAAGE,IAAI,CAACC,SAAS,CAACH,KAAK,CAAC;IAEtE,KAAK,MAAM,GAAGI,MAAM,CAAC,IAAI,IAAI,CAACV,OAAO,CAACW,OAAO,CAAC,CAAC,EAAE;MAC/C,IAAI;QACFD,MAAM,CAACL,IAAI,CAACE,IAAI,CAAC;MACnB,CAAC,CAAC,MAAM;QACN;MAAA;IAEJ;EACF;;EAEA;AACF;AACA;AACA;AACA;EACEK,YAAYA,CAACF,MAAiB,EAAE;IAC9B,MAAMG,QAAQ,
|
|
1
|
+
{"version":3,"file":"WebSocketApiServer.js","names":["WebSocketServer","WebSocketApiServer","clients","Map","nextClientId","constructor","fastify","send","event","data","JSON","stringify","socket","entries","onConnection","clientId","set","log","debug","msg","onClose","delete","addEventListener"],"sources":["../../../../src/plugins/wss/servers/WebSocketApiServer.ts"],"sourcesContent":["import type { FastifyInstance } from 'fastify';\nimport type WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server for API clients.\n * Useful to listening for compilation events and new logs.\n *\n * @category Development server\n */\nexport class WebSocketApiServer extends WebSocketServer {\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketApiServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/api');\n }\n\n /**\n * Send message to all connected API clients.\n *\n * @param event Event string or object to send.\n */\n send(event: any) {\n const data = typeof event === 'string' ? event : JSON.stringify(event);\n\n for (const [, socket] of this.clients.entries()) {\n try {\n socket.send(data);\n } catch {\n // NOOP\n }\n }\n }\n\n /**\n * Process new WebSocket connection from client application.\n *\n * @param socket Incoming client's WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n\n this.fastify.log.debug({ msg: 'API client connected', clientId });\n this.clients.set(clientId, socket);\n\n const onClose = () => {\n this.fastify.log.debug({\n msg: 'API client disconnected',\n clientId,\n });\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n }\n}\n"],"mappings":"SAESA,eAAe;AAExB;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,SAASD,eAAe,CAAC;EAC9CE,OAAO,GAAG,IAAI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","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketDevClientServer.js","names":["WebSocketServer","WebSocketDevClientServer","clients","Map","nextClientId","constructor","fastify","processMessage","message","type","body","JSON","parse","level","log","error","issuer","msg","data","warn","info","debug","onConnection","socket","clientId","set","onClose","delete","addEventListener","event","toString"],"sources":["../../../../src/plugins/wss/servers/WebSocketDevClientServer.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"WebSocketDevClientServer.js","names":["WebSocketServer","WebSocketDevClientServer","clients","Map","nextClientId","constructor","fastify","processMessage","message","type","body","JSON","parse","level","log","error","issuer","msg","data","warn","info","debug","onConnection","socket","clientId","set","onClose","delete","addEventListener","event","toString"],"sources":["../../../../src/plugins/wss/servers/WebSocketDevClientServer.ts"],"sourcesContent":["import type { FastifyInstance } from 'fastify';\nimport type WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server for communication with React Native clients.\n * All client logs - logs from React Native application - are processed here.\n *\n * @category Development server\n */\nexport class WebSocketDevClientServer extends WebSocketServer {\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketDevClientServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/__client');\n }\n\n /**\n * Process client message.\n *\n * @param message Stringified client message.\n */\n processMessage(message: string) {\n const { type, ...body } = JSON.parse(message);\n switch (type) {\n case 'client-log':\n if (body.level === 'error') {\n this.fastify.log.error({ issuer: 'Console', msg: body.data });\n } else if (body.level === 'warn') {\n this.fastify.log.warn({ issuer: 'Console', msg: body.data });\n } else if (body.level === 'info' || body.level === 'log') {\n this.fastify.log.info({ issuer: 'Console', msg: body.data });\n } else {\n // body.level === 'debug' || body.level === 'trace'\n this.fastify.log.debug({ issuer: 'Console', msg: body.data });\n }\n break;\n default:\n this.fastify.log.warn({ msg: 'Unknown client message', message });\n }\n }\n\n /**\n * Process new WebSocket connection from client application.\n *\n * @param socket Incoming client's WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n this.fastify.log.debug({ msg: 'React Native client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({\n msg: 'React Native client disconnected',\n clientId,\n });\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n socket.addEventListener('message', (event) => {\n this.processMessage(event.data.toString());\n });\n }\n}\n"],"mappings":"SAESA,eAAe;AAExB;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,wBAAwB,SAASD,eAAe,CAAC;EACpDE,OAAO,GAAG,IAAIC,GAAG,CAAoB,CAAC;EACtCC,YAAY,GAAG,CAAC;;EAExB;AACF;AACA;AACA;AACA;AACA;EACEC,WAAWA,CAACC,OAAwB,EAAE;IACpC,KAAK,CAACA,OAAO,EAAE,WAAW,CAAC;EAC7B;;EAEA;AACF;AACA;AACA;AACA;EACEC,cAAcA,CAACC,OAAe,EAAE;IAC9B,MAAM;MAAEC,IAAI;MAAE,GAAGC;IAAK,CAAC,GAAGC,IAAI,CAACC,KAAK,CAACJ,OAAO,CAAC;IAC7C,QAAQC,IAAI;MACV,KAAK,YAAY;QACf,IAAIC,IAAI,CAACG,KAAK,KAAK,OAAO,EAAE;UAC1B,IAAI,CAACP,OAAO,CAACQ,GAAG,CAACC,KAAK,CAAC;YAAEC,MAAM,EAAE,SAAS;YAAEC,GAAG,EAAEP,IAAI,CAACQ;UAAK,CAAC,CAAC;QAC/D,CAAC,MAAM,IAAIR,IAAI,CAACG,KAAK,KAAK,MAAM,EAAE;UAChC,IAAI,CAACP,OAAO,CAACQ,GAAG,CAACK,IAAI,CAAC;YAAEH,MAAM,EAAE,SAAS;YAAEC,GAAG,EAAEP,IAAI,CAACQ;UAAK,CAAC,CA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C,IAAI,CAACtB,cAAc,CAACsB,KAAK,CAACX,IAAI,CAACY,QAAQ,CAAC,CAAC,CAAC;IAC5C,CAAC,CAAC;EACJ;AACF","ignoreList":[]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import WebSocket from 'ws';
|
|
2
1
|
import type { FastifyInstance } from 'fastify';
|
|
2
|
+
import type WebSocket from 'ws';
|
|
3
3
|
import { WebSocketServer } from '../WebSocketServer';
|
|
4
|
-
import { WebSocketMessageServer } from './WebSocketMessageServer';
|
|
4
|
+
import type { WebSocketMessageServer } from './WebSocketMessageServer';
|
|
5
5
|
/**
|
|
6
6
|
* {@link WebSocketEventsServer} configuration options.
|
|
7
7
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketEventsServer.js","names":["prettyFormat","WebSocketServer","WebSocketEventsServer","PROTOCOL_VERSION","clients","Map","nextClientId","constructor","fastify","config","verifyClient","origin","test","parseMessage","data","message","JSON","parse","version","log","error","msg","undefined","serializeMessage","toSerialize","Error","default","escapeString","highlight","maxDepth","min","type","map","item","plugins","ReactElement","stringify","broadcastEvent","event","size","serialized","clientId","socket","entries","send","_skipBroadcast","onConnection","set","debug","onClose","removeAllListeners","delete","addEventListener","toString","webSocketMessageServer","broadcast","command","params"],"sources":["../../../../src/plugins/wss/servers/WebSocketEventsServer.ts"],"sourcesContent":["import WebSocket from 'ws';\nimport * as prettyFormat from 'pretty-format';\nimport type { FastifyInstance } from 'fastify';\nimport { WebSocketServer } from '../WebSocketServer';\nimport { WebSocketMessageServer } from './WebSocketMessageServer';\n\n/**\n * {@link WebSocketEventsServer} configuration options.\n */\nexport interface WebSocketEventsServerConfig {\n /** Instance of a {@link WebSocketMessageServer} which can be used for broadcasting. */\n webSocketMessageServer: WebSocketMessageServer;\n}\n\n/**\n * Represents a command that connected clients can send to the {@link WebSocketEventsServer}.\n */\nexport interface Command {\n version: number;\n type: 'command';\n command: string;\n params?: any;\n}\n\n/**\n * Represents an event message.\n */\nexport interface EventMessage {\n error?: Error | string;\n type?: string;\n data?: any;\n}\n\n/**\n * Class for creating a WebSocket server to process events and reports.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/eventsSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketEventsServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n * @param config Configuration object.\n */\n constructor(\n fastify: FastifyInstance,\n private config: WebSocketEventsServerConfig\n ) {\n super(fastify, '/events', {\n verifyClient: (({ origin }) => {\n return /^(https?:\\/\\/localhost|file:\\/\\/)/.test(origin);\n }) as WebSocket.VerifyClientCallbackSync,\n });\n }\n\n /**\n * Parse received command message from connected client.\n *\n * @param data Stringified command message to parse.\n * @returns Parsed command or `undefined` if parsing failed.\n */\n parseMessage(data: string): Command | undefined {\n try {\n const message = JSON.parse(data);\n if (message.version === WebSocketEventsServer.PROTOCOL_VERSION) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n\n return undefined;\n }\n\n /**\n * Stringify `message` into a format that can be transported as a `string`.\n *\n * @param message Message to serialize.\n * @returns String representation of a `message` or `undefined` if serialization failed.\n */\n serializeMessage(message: EventMessage) {\n let toSerialize = message;\n if (message.error && message.error instanceof Error) {\n toSerialize = {\n ...message,\n error: prettyFormat.default(message.error, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n }),\n };\n } else if (message && message.type === 'client_log') {\n toSerialize = {\n ...message,\n data: message.data.map((item: any) =>\n typeof item === 'string'\n ? item\n : prettyFormat.default(item, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n plugins: [prettyFormat.plugins.ReactElement],\n })\n ),\n };\n }\n try {\n return JSON.stringify(toSerialize);\n } catch (error) {\n this.fastify.log.error({ msg: 'Failed to serialize', error });\n return undefined;\n }\n }\n\n /**\n * Broadcast event to all connected clients.\n *\n * @param event Event message to broadcast.\n */\n broadcastEvent(event: EventMessage) {\n if (!this.clients.size) {\n return;\n }\n\n const serialized = this.serializeMessage(event);\n if (!serialized) {\n return;\n }\n\n for (const [clientId, socket] of this.clients.entries()) {\n try {\n socket.send(serialized);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast to client',\n clientId,\n error,\n _skipBroadcast: true,\n });\n }\n }\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n this.fastify.log.debug({ msg: 'Events client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Events client disconnected', clientId });\n socket.removeAllListeners();\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n socket.addEventListener('message', (event) => {\n const message = this.parseMessage(event.data.toString());\n\n if (!message) {\n return;\n }\n\n if (message.type === 'command') {\n try {\n this.config.webSocketMessageServer.broadcast(\n message.command,\n message.params\n );\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to forward message to clients',\n error,\n });\n }\n } else {\n this.fastify.log.error({\n msg: 'Unknown message type',\n message,\n });\n }\n });\n }\n}\n"],"mappings":"AACA,OAAO,KAAKA,YAAY,MAAM,eAAe;AAAC,SAErCC,eAAe;AAGxB;AACA;AACA;AAMA;AACA;AACA;AAQA;AACA;AACA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,qBAAqB,SAASD,eAAe,CAAC;EACzD,OAAgBE,gBAAgB,GAAG,CAAC;EAE5BC,OAAO,GAAG,IAAIC,GAAG,CAAoB,CAAC;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G,EAAE,sCAAsC;YAC3CD;UACF,CAAC,CAAC;QACJ;MACF,CAAC,MAAM;QACL,IAAI,CAACZ,OAAO,CAACW,GAAG,CAACC,KAAK,CAAC;UACrBC,GAAG,EAAE,sBAAsB;UAC3BN;QACF,CAAC,CAAC;MACJ;IACF,CAAC,CAAC;EACJ;AACF"}
|
|
1
|
+
{"version":3,"file":"WebSocketEventsServer.js","names":["prettyFormat","WebSocketServer","WebSocketEventsServer","PROTOCOL_VERSION","clients","Map","nextClientId","constructor","fastify","config","verifyClient","origin","test","parseMessage","data","message","JSON","parse","version","log","error","msg","undefined","serializeMessage","toSerialize","Error","default","escapeString","highlight","maxDepth","min","type","map","item","plugins","ReactElement","stringify","broadcastEvent","event","size","serialized","clientId","socket","entries","send","_skipBroadcast","onConnection","set","debug","onClose","removeAllListeners","delete","addEventListener","toString","webSocketMessageServer","broadcast","command","params"],"sources":["../../../../src/plugins/wss/servers/WebSocketEventsServer.ts"],"sourcesContent":["import type { FastifyInstance } from 'fastify';\nimport * as prettyFormat from 'pretty-format';\nimport type WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\nimport type { WebSocketMessageServer } from './WebSocketMessageServer';\n\n/**\n * {@link WebSocketEventsServer} configuration options.\n */\nexport interface WebSocketEventsServerConfig {\n /** Instance of a {@link WebSocketMessageServer} which can be used for broadcasting. */\n webSocketMessageServer: WebSocketMessageServer;\n}\n\n/**\n * Represents a command that connected clients can send to the {@link WebSocketEventsServer}.\n */\nexport interface Command {\n version: number;\n type: 'command';\n command: string;\n params?: any;\n}\n\n/**\n * Represents an event message.\n */\nexport interface EventMessage {\n error?: Error | string;\n type?: string;\n data?: any;\n}\n\n/**\n * Class for creating a WebSocket server to process events and reports.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/eventsSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketEventsServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n * @param config Configuration object.\n */\n constructor(\n fastify: FastifyInstance,\n private config: WebSocketEventsServerConfig\n ) {\n super(fastify, '/events', {\n verifyClient: (({ origin }) => {\n return /^(https?:\\/\\/localhost|file:\\/\\/)/.test(origin);\n }) as WebSocket.VerifyClientCallbackSync,\n });\n }\n\n /**\n * Parse received command message from connected client.\n *\n * @param data Stringified command message to parse.\n * @returns Parsed command or `undefined` if parsing failed.\n */\n parseMessage(data: string): Command | undefined {\n try {\n const message = JSON.parse(data);\n if (message.version === WebSocketEventsServer.PROTOCOL_VERSION) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n\n return undefined;\n }\n\n /**\n * Stringify `message` into a format that can be transported as a `string`.\n *\n * @param message Message to serialize.\n * @returns String representation of a `message` or `undefined` if serialization failed.\n */\n serializeMessage(message: EventMessage) {\n let toSerialize = message;\n if (message.error && message.error instanceof Error) {\n toSerialize = {\n ...message,\n error: prettyFormat.default(message.error, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n }),\n };\n } else if (message && message.type === 'client_log') {\n toSerialize = {\n ...message,\n data: message.data.map((item: any) =>\n typeof item === 'string'\n ? item\n : prettyFormat.default(item, {\n escapeString: true,\n highlight: true,\n maxDepth: 3,\n min: true,\n plugins: [prettyFormat.plugins.ReactElement],\n })\n ),\n };\n }\n try {\n return JSON.stringify(toSerialize);\n } catch (error) {\n this.fastify.log.error({ msg: 'Failed to serialize', error });\n return undefined;\n }\n }\n\n /**\n * Broadcast event to all connected clients.\n *\n * @param event Event message to broadcast.\n */\n broadcastEvent(event: EventMessage) {\n if (!this.clients.size) {\n return;\n }\n\n const serialized = this.serializeMessage(event);\n if (!serialized) {\n return;\n }\n\n for (const [clientId, socket] of this.clients.entries()) {\n try {\n socket.send(serialized);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast to client',\n clientId,\n error,\n _skipBroadcast: true,\n });\n }\n }\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n */\n onConnection(socket: WebSocket) {\n const clientId = `client#${this.nextClientId++}`;\n this.clients.set(clientId, socket);\n this.fastify.log.debug({ msg: 'Events client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Events client disconnected', clientId });\n socket.removeAllListeners();\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n socket.addEventListener('message', (event) => {\n const message = this.parseMessage(event.data.toString());\n\n if (!message) {\n return;\n }\n\n if (message.type === 'command') {\n try {\n this.config.webSocketMessageServer.broadcast(\n message.command,\n message.params\n );\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to forward message to clients',\n error,\n });\n }\n } else {\n this.fastify.log.error({\n msg: 'Unknown message type',\n message,\n });\n }\n });\n }\n}\n"],"mappings":"AACA,OAAO,KAAKA,YAAY,MAAM,eAAe;AAAC,SAErCC,eAAe;AAGxB;AACA;AACA;AAMA;AACA;AACA;AAQA;AACA;AACA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,qBAAqB,SAASD,eAAe,CAAC;EACzD,OAAgBE,gBAAgB,GAAG,CAAC;EAE5BC,OAAO,GAAG,IAAIC,GAAG,CAAoB,CAAC;EACtCC,YAAY,GAAG,CAAC;;EAExB;AACF;AACA;AACA;AACA;AACA;AACA;EACEC,WAAWA,CACTC,OAAwB,EAChBC,MAAmC,EAC3C;IACA,KAAK,CAACD,OAAO,EAAE,SAAS,EAAE;MACxBE,YAAY,EAAGA,CAAC;QAAEC;MAAO,CAAC,KAAK;QAC7B,OAAO,mCAAmC,CAACC,IAAI,CAACD,MAAM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|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import WebSocket from 'ws';
|
|
5
|
-
import { HmrDelegate } from '../types';
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { FastifyInstance } from 'fastify';
|
|
3
|
+
import type WebSocket from 'ws';
|
|
6
4
|
import { WebSocketServer } from '../WebSocketServer';
|
|
5
|
+
import type { HmrDelegate } from '../types';
|
|
7
6
|
/**
|
|
8
7
|
* Class for creating a WebSocket server for Hot Module Replacement.
|
|
9
8
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketHMRServer.js","names":["URL","WebSocketServer","WebSocketHMRServer","clients","Map","nextClientId","constructor","fastify","delegate","getUriPath","send","event","platform","clientIds","data","JSON","stringify","key","socket","clientId","includes","error","log","msg","onConnection","request","searchParams","url","get","debug","close","client","set","onClose","delete","addEventListener","onClientConnected"],"sources":["../../../../src/plugins/wss/servers/WebSocketHMRServer.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"WebSocketHMRServer.js","names":["URL","WebSocketServer","WebSocketHMRServer","clients","Map","nextClientId","constructor","fastify","delegate","getUriPath","send","event","platform","clientIds","data","JSON","stringify","key","socket","clientId","includes","error","log","msg","onConnection","request","searchParams","url","get","debug","close","client","set","onClose","delete","addEventListener","onClientConnected"],"sources":["../../../../src/plugins/wss/servers/WebSocketHMRServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport { URL } from 'node:url';\nimport type { FastifyInstance } from 'fastify';\nimport type WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\nimport type { HmrDelegate } from '../types';\n\n/**\n * Class for creating a WebSocket server for Hot Module Replacement.\n *\n * @category Development server\n */\nexport class WebSocketHMRServer extends WebSocketServer {\n private clients = new Map<\n { clientId: string; platform: string },\n WebSocket\n >();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketHMRServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n * @param delegate HMR delegate instance.\n */\n constructor(\n fastify: FastifyInstance,\n private delegate: HmrDelegate\n ) {\n super(fastify, delegate.getUriPath());\n }\n\n /**\n * Send action to all connected HMR clients.\n *\n * @param event Event to send to the clients.\n * @param platform Platform of clients to send the event to.\n * @param clientIds Ids of clients who should receive the event.\n */\n send(event: any, platform: string, clientIds?: string[]) {\n const data = typeof event === 'string' ? event : JSON.stringify(event);\n\n for (const [key, socket] of this.clients) {\n if (\n key.platform !== platform ||\n !(clientIds ?? [key.clientId]).includes(key.clientId)\n ) {\n continue;\n }\n\n try {\n socket.send(data);\n } catch (error) {\n this.fastify.log.error({\n msg: 'Cannot send action to client',\n event,\n error,\n ...key,\n });\n }\n }\n }\n\n /**\n * Process new WebSocket connection from HMR client.\n *\n * @param socket Incoming HMR client's WebSocket connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n const { searchParams } = new URL(request.url || '', 'http://localhost');\n const platform = searchParams.get('platform');\n\n if (!platform) {\n this.fastify.log.debug({\n msg: 'HMR connection disconnected - missing platform',\n });\n socket.close();\n return;\n }\n\n const clientId = `client#${this.nextClientId++}`;\n\n const client = {\n clientId,\n platform,\n };\n\n this.clients.set(client, socket);\n\n this.fastify.log.debug({ msg: 'HMR client connected', ...client });\n\n const onClose = () => {\n this.fastify.log.debug({\n msg: 'HMR client disconnected',\n ...client,\n });\n this.clients.delete(client);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n\n this.delegate.onClientConnected(platform, clientId);\n }\n}\n"],"mappings":"AACA,SAASA,GAAG,QAAQ,UAAU;AAAC,SAGtBC,eAAe;AAGxB;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,kBAAkB,SAASD,eAAe,CAAC;EAC9CE,OAAO,GAAG,IAAIC,GAAG,CAGvB,CAAC;EACKC,YAAY,GAAG,CAAC;;EAExB;AACF;AACA;AACA;AACA;AACA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MACFL,MAAM,CAACY,KAAK,CAAC,CAAC;MACd;IACF;IAEA,MAAMX,QAAQ,GAAG,UAAU,IAAI,CAACd,YAAY,EAAE,EAAE;IAEhD,MAAM0B,MAAM,GAAG;MACbZ,QAAQ;MACRP;IACF,CAAC;IAED,IAAI,CAACT,OAAO,CAAC6B,GAAG,CAACD,MAAM,EAAEb,MAAM,CAAC;IAEhC,IAAI,CAACX,OAAO,CAACe,GAAG,CAACO,KAAK,CAAC;MAAEN,GAAG,EAAE,sBAAsB;MAAE,GAAGQ;IAAO,CAAC,CAAC;IAElE,MAAME,OAAO,GAAGA,CAAA,KAAM;MACpB,IAAI,CAAC1B,OAAO,CAACe,GAAG,CAACO,KAAK,CAAC;QACrBN,GAAG,EAAE,yBAAyB;QAC9B,GAAGQ;MACL,CAAC,CAAC;MACF,IAAI,CAAC5B,OAAO,CAAC+B,MAAM,CAACH,MAAM,CAAC;IAC7B,CAAC;IAEDb,MAAM,CAACiB,gBAAgB,CAAC,OAAO,EAAEF,OAAO,CAAC;IACzCf,MAAM,CAACiB,gBAAgB,CAAC,OAAO,EAAEF,OAAO,CAAC;IAEzC,IAAI,CAACzB,QAAQ,CAAC4B,iBAAiB,CAACxB,QAAQ,EAAEO,QAAQ,CAAC;EACrD;AACF","ignoreList":[]}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import type { IncomingMessage } from 'http';
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
3
2
|
import type { FastifyInstance } from 'fastify';
|
|
4
|
-
import WebSocket from 'ws';
|
|
3
|
+
import type WebSocket from 'ws';
|
|
5
4
|
import { WebSocketServer } from '../WebSocketServer';
|
|
6
5
|
/**
|
|
7
6
|
* Holds {@link ReactNativeMessage} `id` data.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { URL } from 'url';
|
|
1
|
+
import { URL } from 'node:url';
|
|
2
2
|
import { WebSocketServer } from "../WebSocketServer.js";
|
|
3
3
|
/**
|
|
4
4
|
* Holds {@link ReactNativeMessage} `id` data.
|
|
@@ -83,7 +83,7 @@ export class WebSocketMessageServer extends WebSocketServer {
|
|
|
83
83
|
msg: 'Received message had wrong protocol version',
|
|
84
84
|
message
|
|
85
85
|
});
|
|
86
|
-
} catch
|
|
86
|
+
} catch {
|
|
87
87
|
this.fastify.log.error({
|
|
88
88
|
msg: 'Failed to parse the message as JSON',
|
|
89
89
|
data
|
|
@@ -218,10 +218,10 @@ export class WebSocketMessageServer extends WebSocketServer {
|
|
|
218
218
|
const {
|
|
219
219
|
searchParams
|
|
220
220
|
} = new URL(peerSocket.upgradeReq?.url || '');
|
|
221
|
-
output[peerId] = [...searchParams.entries()].reduce((acc, [key, value]) =>
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
}
|
|
221
|
+
output[peerId] = [...searchParams.entries()].reduce((acc, [key, value]) => {
|
|
222
|
+
acc[key] = value;
|
|
223
|
+
return acc;
|
|
224
|
+
}, {});
|
|
225
225
|
}
|
|
226
226
|
});
|
|
227
227
|
result = output;
|
|
@@ -295,7 +295,7 @@ export class WebSocketMessageServer extends WebSocketServer {
|
|
|
295
295
|
*/
|
|
296
296
|
onConnection(socket, request) {
|
|
297
297
|
const clientId = `client#${this.nextClientId++}`;
|
|
298
|
-
|
|
298
|
+
const client = socket;
|
|
299
299
|
client.upgradeReq = request;
|
|
300
300
|
this.clients.set(clientId, client);
|
|
301
301
|
this.fastify.log.debug({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WebSocketMessageServer.js","names":["URL","WebSocketServer","WebSocketMessageServer","PROTOCOL_VERSION","isBroadcast","message","method","id","undefined","target","isRequest","isResponse","requestId","clientId","result","error","clients","Map","nextClientId","constructor","fastify","parseMessage","data","binary","log","msg","JSON","parse","version","toString","e","getClientSocket","socket","get","Error","handleError","errorMessage","params","send","stringify","forwardRequest","forwardResponse","processServerRequest","output","forEach","peerSocket","peerId","searchParams","upgradeReq","url","entries","reduce","acc","key","value","sendBroadcast","broadcasterId","forwarded","size","warn","broadcast","onConnection","request","client","set","debug","onClose","removeAllListeners","delete","addEventListener","event"],"sources":["../../../../src/plugins/wss/servers/WebSocketMessageServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\nimport { URL } from 'url';\nimport type { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Holds {@link ReactNativeMessage} `id` data.\n */\nexport interface ReactNativeIdObject {\n requestId: string;\n clientId: string;\n}\n\n/**\n * Message representation used by {@link WebSocketMessageServer}.\n */\nexport interface ReactNativeMessage {\n version?: string;\n id?: ReactNativeIdObject;\n method?: string;\n target: string;\n result?: any;\n error?: Error;\n params?: Record<string, any>;\n}\n\ntype WebSocketWithUpgradeReq = WebSocket & { upgradeReq?: IncomingMessage };\n\n/**\n * Class for creating a WebSocket server and sending messages between development server\n * and the React Native applications.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketMessageServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n /**\n * Check if message is a broadcast request.\n *\n * @param message Message to check.\n * @returns True if message is a broadcast request and should be broadcasted\n * with {@link sendBroadcast}.\n */\n static isBroadcast(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.method === 'string' &&\n message.id === undefined &&\n message.target === undefined\n );\n }\n\n /**\n * Check if message is a method request.\n *\n * @param message Message to check.\n * @returns True if message is a request.\n */\n static isRequest(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.method === 'string' && typeof message.target === 'string'\n );\n }\n\n /**\n * Check if message is a response with results of performing some request.\n *\n * @param message Message to check.\n * @returns True if message is a response.\n */\n static isResponse(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.id === 'object' &&\n typeof message.id.requestId !== 'undefined' &&\n typeof message.id.clientId === 'string' &&\n (message.result !== undefined || message.error !== undefined)\n );\n }\n\n private clients = new Map<string, WebSocketWithUpgradeReq>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/message');\n }\n\n /**\n * Parse stringified message into a {@link ReactNativeMessage}.\n *\n * @param data Stringified message.\n * @param binary Additional binary data if any.\n * @returns Parsed message or `undefined` if parsing failed.\n */\n parseMessage(\n data: string,\n binary: any\n ): Partial<ReactNativeMessage> | undefined {\n if (binary) {\n this.fastify.log.error({\n msg: 'Failed to parse message - expected text message, got binary',\n });\n return undefined;\n }\n try {\n const message = JSON.parse(data) as Partial<ReactNativeMessage>;\n if (\n message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()\n ) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch (e) {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n return undefined;\n }\n\n /**\n * Get client's WebSocket connection for given `clientId`.\n * Throws if no such client is connected.\n *\n * @param clientId Id of the client.\n * @returns WebSocket connection.\n */\n getClientSocket(clientId: string) {\n const socket = this.clients.get(clientId);\n if (socket === undefined) {\n throw new Error(`Could not find client with id \"${clientId}\"`);\n }\n return socket;\n }\n\n /**\n * Process error by sending an error message to the client whose message caused the error\n * to occur.\n *\n * @param clientId Id of the client whose message caused an error.\n * @param message Original message which caused the error.\n * @param error Concrete instance of an error that occurred.\n */\n handleError(\n clientId: string,\n message: Partial<ReactNativeMessage>,\n error: Error\n ) {\n const errorMessage = {\n id: message.id,\n method: message.method,\n target: message.target,\n error: message.error === undefined ? 'undefined' : 'defined',\n params: message.params === undefined ? 'undefined' : 'defined',\n result: message.result === undefined ? 'undefined' : 'defined',\n };\n\n if (message.id === undefined) {\n this.fastify.log.error({\n msg: 'Handling message failed',\n clientId,\n error,\n errorMessage,\n });\n } else {\n try {\n const socket = this.getClientSocket(clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n error,\n id: message.id,\n })\n );\n } catch (error) {\n this.fastify.log.error('Failed to reply', {\n clientId,\n error,\n errorMessage,\n });\n }\n }\n }\n\n /**\n * Send given request `message` to it's designated client's socket based on `message.target`.\n * The target client must be connected, otherwise it will throw an error.\n *\n * @param clientId Id of the client that requested the forward.\n * @param message Message to forward.\n */\n forwardRequest(clientId: string, message: Partial<ReactNativeMessage>) {\n if (!message.target) {\n this.fastify.log.error({\n msg: 'Failed to forward request - message.target is missing',\n clientId,\n message,\n });\n return;\n }\n\n const socket = this.getClientSocket(message.target);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n id:\n message.id === undefined\n ? undefined\n : { requestId: message.id, clientId },\n })\n );\n }\n\n /**\n * Send given response `message` to it's designated client's socket based\n * on `message.id.clientId`.\n * The target client must be connected, otherwise it will throw an error.\n *\n * @param message Message to forward.\n */\n forwardResponse(message: Partial<ReactNativeMessage>) {\n if (!message.id) {\n return;\n }\n\n const socket = this.getClientSocket(message.id.clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n result: message.result,\n error: message.error,\n id: message.id.requestId,\n })\n );\n }\n\n /**\n * Process request message targeted towards this {@link WebSocketMessageServer}\n * and send back the results.\n *\n * @param clientId Id of the client who send the message.\n * @param message The message to process by the server.\n */\n processServerRequest(clientId: string, message: Partial<ReactNativeMessage>) {\n let result: string | Record<string, Record<string, string>>;\n\n switch (message.method) {\n case 'getid':\n result = clientId;\n break;\n case 'getpeers': {\n const output: Record<string, Record<string, string>> = {};\n this.clients.forEach((peerSocket, peerId) => {\n if (clientId !== peerId) {\n const { searchParams } = new URL(peerSocket.upgradeReq?.url || '');\n output[peerId] = [...searchParams.entries()].reduce(\n (acc, [key, value]) => ({\n ...acc,\n [key]: value,\n }),\n {}\n );\n }\n });\n result = output;\n break;\n }\n default:\n throw new Error(\n `Cannot process server request - unknown method ${JSON.stringify({\n clientId,\n message,\n })}`\n );\n }\n\n const socket = this.getClientSocket(clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n result,\n id: message.id,\n })\n );\n }\n\n /**\n * Broadcast given message to all connected clients.\n *\n * @param broadcasterId Id of the client who is broadcasting.\n * @param message Message to broadcast.\n */\n sendBroadcast(\n broadcasterId: string | undefined,\n message: Partial<ReactNativeMessage>\n ) {\n const forwarded = {\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n };\n\n if (this.clients.size === 0) {\n this.fastify.log.warn({\n msg:\n 'No apps connected. ' +\n `Sending \"${message.method}\" to all React Native apps failed. ` +\n 'Make sure your app is running in the simulator or on a phone connected via USB.',\n });\n }\n\n for (const [clientId, socket] of this.clients) {\n if (clientId !== broadcasterId) {\n try {\n socket.send(JSON.stringify(forwarded));\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast',\n clientId,\n error,\n forwarded,\n });\n }\n }\n }\n }\n\n /**\n * Send method broadcast to all connected clients.\n *\n * @param method Method name to broadcast.\n * @param params Method parameters.\n */\n broadcast(method: string, params?: Record<string, any>) {\n this.sendBroadcast(undefined, { method, params });\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n * @param request Upgrade request for the connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n const clientId = `client#${this.nextClientId++}`;\n let client: WebSocketWithUpgradeReq = socket;\n client.upgradeReq = request;\n this.clients.set(clientId, client);\n this.fastify.log.debug({ msg: 'Message client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Message client disconnected', clientId });\n socket.removeAllListeners();\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n socket.addEventListener('message', (event) => {\n const message = this.parseMessage(\n event.data.toString(),\n // @ts-ignore\n event.binary\n );\n\n if (!message) {\n this.fastify.log.error({\n msg: 'Received message not matching protocol',\n clientId,\n message,\n });\n return;\n }\n\n try {\n if (WebSocketMessageServer.isBroadcast(message)) {\n this.sendBroadcast(clientId, message);\n } else if (WebSocketMessageServer.isRequest(message)) {\n if (message.target === 'server') {\n this.processServerRequest(clientId, message);\n } else {\n this.forwardRequest(clientId, message);\n }\n } else if (WebSocketMessageServer.isResponse(message)) {\n this.forwardResponse(message);\n } else {\n throw new Error(\n `Invalid message, did not match the protocol ${JSON.stringify({\n clientId,\n message,\n })}`\n );\n }\n } catch (error) {\n this.handleError(clientId, message, error as Error);\n }\n });\n }\n}\n"],"mappings":"AACA,SAASA,GAAG,QAAQ,KAAK;AAAC,SAGjBC,eAAe;AAExB;AACA;AACA;AAMA;AACA;AACA;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,sBAAsB,SAASD,eAAe,CAAC;EAC1D,OAAgBE,gBAAgB,GAAG,CAAC;;EAEpC;AACF;AACA;AACA;AACA;AACA;AACA;EACE,OAAOC,WAAWA,CAACC,OAAoC,EAAE;IACvD,OACE,OAAOA,OAAO,CAACC,MAAM,KAAK,QAAQ,IAClCD,OAAO,CAACE,EAAE,KAAKC,SAAS,IACxBH,OAAO,CAACI,MAAM,KAAKD,SAAS;EAEhC;;EAEA;AACF;AACA;AACA;AACA;AACA;EACE,OAAOE,SAASA,CAACL,OAAoC,EAAE;IACrD,OACE,OAAOA,OAAO,CAACC,MAAM,KAAK,QAAQ,IAAI,OAAOD,OAAO,CAACI,MAAM,KAAK,QAAQ;EAE5E;;EAEA;AACF;AACA;AACA;AACA;AACA;EACE,OAAOE,UAAUA,CAACN,OAAoC,EAAE;IACtD,OACE,OAAOA,OAAO,CAACE,EAAE,KAAK,QAAQ,IAC9B,OAAOF,OAAO,CAACE,EAAE,CAACK,SAAS,KAAK,WAAW,IAC3C,OAAOP,OAAO,CAACE,EAAE,CAACM,QAAQ,KAAK,QAA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,+CAA8CR,IAAI,CAACa,SAAS,CAAC;YAC5D1B,QAAQ;YACRR;UACF,CAAC,CAAE,EACL,CAAC;QACH;MACF,CAAC,CAAC,OAAOU,KAAK,EAAE;QACd,IAAI,CAACoB,WAAW,CAACtB,QAAQ,EAAER,OAAO,EAAEU,KAAc,CAAC;MACrD;IACF,CAAC,CAAC;EACJ;AACF"}
|
|
1
|
+
{"version":3,"file":"WebSocketMessageServer.js","names":["URL","WebSocketServer","WebSocketMessageServer","PROTOCOL_VERSION","isBroadcast","message","method","id","undefined","target","isRequest","isResponse","requestId","clientId","result","error","clients","Map","nextClientId","constructor","fastify","parseMessage","data","binary","log","msg","JSON","parse","version","toString","getClientSocket","socket","get","Error","handleError","errorMessage","params","send","stringify","forwardRequest","forwardResponse","processServerRequest","output","forEach","peerSocket","peerId","searchParams","upgradeReq","url","entries","reduce","acc","key","value","sendBroadcast","broadcasterId","forwarded","size","warn","broadcast","onConnection","request","client","set","debug","onClose","removeAllListeners","delete","addEventListener","event"],"sources":["../../../../src/plugins/wss/servers/WebSocketMessageServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport { URL } from 'node:url';\nimport type { FastifyInstance } from 'fastify';\nimport type WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Holds {@link ReactNativeMessage} `id` data.\n */\nexport interface ReactNativeIdObject {\n requestId: string;\n clientId: string;\n}\n\n/**\n * Message representation used by {@link WebSocketMessageServer}.\n */\nexport interface ReactNativeMessage {\n version?: string;\n id?: ReactNativeIdObject;\n method?: string;\n target: string;\n result?: any;\n error?: Error;\n params?: Record<string, any>;\n}\n\ntype WebSocketWithUpgradeReq = WebSocket & { upgradeReq?: IncomingMessage };\n\n/**\n * Class for creating a WebSocket server and sending messages between development server\n * and the React Native applications.\n *\n * Based on: https://github.com/react-native-community/cli/blob/v4.14.0/packages/cli-server-api/src/websocket/messageSocketServer.ts\n *\n * @category Development server\n */\nexport class WebSocketMessageServer extends WebSocketServer {\n static readonly PROTOCOL_VERSION = 2;\n\n /**\n * Check if message is a broadcast request.\n *\n * @param message Message to check.\n * @returns True if message is a broadcast request and should be broadcasted\n * with {@link sendBroadcast}.\n */\n static isBroadcast(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.method === 'string' &&\n message.id === undefined &&\n message.target === undefined\n );\n }\n\n /**\n * Check if message is a method request.\n *\n * @param message Message to check.\n * @returns True if message is a request.\n */\n static isRequest(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.method === 'string' && typeof message.target === 'string'\n );\n }\n\n /**\n * Check if message is a response with results of performing some request.\n *\n * @param message Message to check.\n * @returns True if message is a response.\n */\n static isResponse(message: Partial<ReactNativeMessage>) {\n return (\n typeof message.id === 'object' &&\n typeof message.id.requestId !== 'undefined' &&\n typeof message.id.clientId === 'string' &&\n (message.result !== undefined || message.error !== undefined)\n );\n }\n\n private clients = new Map<string, WebSocketWithUpgradeReq>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketMessageServer and attach it to the given Fastify instance.\n * Any logging information, will be passed through standard `fastify.log` API.\n *\n * @param fastify Fastify instance to attach the WebSocket server to.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/message');\n }\n\n /**\n * Parse stringified message into a {@link ReactNativeMessage}.\n *\n * @param data Stringified message.\n * @param binary Additional binary data if any.\n * @returns Parsed message or `undefined` if parsing failed.\n */\n parseMessage(\n data: string,\n binary: any\n ): Partial<ReactNativeMessage> | undefined {\n if (binary) {\n this.fastify.log.error({\n msg: 'Failed to parse message - expected text message, got binary',\n });\n return undefined;\n }\n try {\n const message = JSON.parse(data) as Partial<ReactNativeMessage>;\n if (\n message.version === WebSocketMessageServer.PROTOCOL_VERSION.toString()\n ) {\n return message;\n }\n this.fastify.log.error({\n msg: 'Received message had wrong protocol version',\n message,\n });\n } catch {\n this.fastify.log.error({\n msg: 'Failed to parse the message as JSON',\n data,\n });\n }\n return undefined;\n }\n\n /**\n * Get client's WebSocket connection for given `clientId`.\n * Throws if no such client is connected.\n *\n * @param clientId Id of the client.\n * @returns WebSocket connection.\n */\n getClientSocket(clientId: string) {\n const socket = this.clients.get(clientId);\n if (socket === undefined) {\n throw new Error(`Could not find client with id \"${clientId}\"`);\n }\n return socket;\n }\n\n /**\n * Process error by sending an error message to the client whose message caused the error\n * to occur.\n *\n * @param clientId Id of the client whose message caused an error.\n * @param message Original message which caused the error.\n * @param error Concrete instance of an error that occurred.\n */\n handleError(\n clientId: string,\n message: Partial<ReactNativeMessage>,\n error: Error\n ) {\n const errorMessage = {\n id: message.id,\n method: message.method,\n target: message.target,\n error: message.error === undefined ? 'undefined' : 'defined',\n params: message.params === undefined ? 'undefined' : 'defined',\n result: message.result === undefined ? 'undefined' : 'defined',\n };\n\n if (message.id === undefined) {\n this.fastify.log.error({\n msg: 'Handling message failed',\n clientId,\n error,\n errorMessage,\n });\n } else {\n try {\n const socket = this.getClientSocket(clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n error,\n id: message.id,\n })\n );\n } catch (error) {\n this.fastify.log.error('Failed to reply', {\n clientId,\n error,\n errorMessage,\n });\n }\n }\n }\n\n /**\n * Send given request `message` to it's designated client's socket based on `message.target`.\n * The target client must be connected, otherwise it will throw an error.\n *\n * @param clientId Id of the client that requested the forward.\n * @param message Message to forward.\n */\n forwardRequest(clientId: string, message: Partial<ReactNativeMessage>) {\n if (!message.target) {\n this.fastify.log.error({\n msg: 'Failed to forward request - message.target is missing',\n clientId,\n message,\n });\n return;\n }\n\n const socket = this.getClientSocket(message.target);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n id:\n message.id === undefined\n ? undefined\n : { requestId: message.id, clientId },\n })\n );\n }\n\n /**\n * Send given response `message` to it's designated client's socket based\n * on `message.id.clientId`.\n * The target client must be connected, otherwise it will throw an error.\n *\n * @param message Message to forward.\n */\n forwardResponse(message: Partial<ReactNativeMessage>) {\n if (!message.id) {\n return;\n }\n\n const socket = this.getClientSocket(message.id.clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n result: message.result,\n error: message.error,\n id: message.id.requestId,\n })\n );\n }\n\n /**\n * Process request message targeted towards this {@link WebSocketMessageServer}\n * and send back the results.\n *\n * @param clientId Id of the client who send the message.\n * @param message The message to process by the server.\n */\n processServerRequest(clientId: string, message: Partial<ReactNativeMessage>) {\n let result: string | Record<string, Record<string, string>>;\n\n switch (message.method) {\n case 'getid':\n result = clientId;\n break;\n case 'getpeers': {\n const output: Record<string, Record<string, string>> = {};\n this.clients.forEach((peerSocket, peerId) => {\n if (clientId !== peerId) {\n const { searchParams } = new URL(peerSocket.upgradeReq?.url || '');\n output[peerId] = [...searchParams.entries()].reduce(\n (acc, [key, value]) => {\n acc[key] = value;\n return acc;\n },\n {} as Record<string, string>\n );\n }\n });\n result = output;\n break;\n }\n default:\n throw new Error(\n `Cannot process server request - unknown method ${JSON.stringify({\n clientId,\n message,\n })}`\n );\n }\n\n const socket = this.getClientSocket(clientId);\n socket.send(\n JSON.stringify({\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n result,\n id: message.id,\n })\n );\n }\n\n /**\n * Broadcast given message to all connected clients.\n *\n * @param broadcasterId Id of the client who is broadcasting.\n * @param message Message to broadcast.\n */\n sendBroadcast(\n broadcasterId: string | undefined,\n message: Partial<ReactNativeMessage>\n ) {\n const forwarded = {\n version: WebSocketMessageServer.PROTOCOL_VERSION,\n method: message.method,\n params: message.params,\n };\n\n if (this.clients.size === 0) {\n this.fastify.log.warn({\n msg:\n 'No apps connected. ' +\n `Sending \"${message.method}\" to all React Native apps failed. ` +\n 'Make sure your app is running in the simulator or on a phone connected via USB.',\n });\n }\n\n for (const [clientId, socket] of this.clients) {\n if (clientId !== broadcasterId) {\n try {\n socket.send(JSON.stringify(forwarded));\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to send broadcast',\n clientId,\n error,\n forwarded,\n });\n }\n }\n }\n }\n\n /**\n * Send method broadcast to all connected clients.\n *\n * @param method Method name to broadcast.\n * @param params Method parameters.\n */\n broadcast(method: string, params?: Record<string, any>) {\n this.sendBroadcast(undefined, { method, params });\n }\n\n /**\n * Process new client's WebSocket connection.\n *\n * @param socket Incoming WebSocket connection.\n * @param request Upgrade request for the connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n const clientId = `client#${this.nextClientId++}`;\n const client: WebSocketWithUpgradeReq = socket;\n client.upgradeReq = request;\n this.clients.set(clientId, client);\n this.fastify.log.debug({ msg: 'Message client connected', clientId });\n\n const onClose = () => {\n this.fastify.log.debug({ msg: 'Message client disconnected', clientId });\n socket.removeAllListeners();\n this.clients.delete(clientId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n socket.addEventListener('message', (event) => {\n const message = this.parseMessage(\n event.data.toString(),\n // @ts-ignore\n event.binary\n );\n\n if (!message) {\n this.fastify.log.error({\n msg: 'Received message not matching protocol',\n clientId,\n message,\n });\n return;\n }\n\n try {\n if (WebSocketMessageServer.isBroadcast(message)) {\n this.sendBroadcast(clientId, message);\n } else if (WebSocketMessageServer.isRequest(message)) {\n if (message.target === 'server') {\n this.processServerRequest(clientId, message);\n } else {\n this.forwardRequest(clientId, message);\n }\n } else if (WebSocketMessageServer.isResponse(message)) {\n this.forwardResponse(message);\n } else {\n throw new Error(\n `Invalid message, did not match the protocol ${JSON.stringify({\n clientId,\n message,\n })}`\n );\n }\n } catch (error) {\n this.handleError(clientId, message, error as Error);\n }\n });\n }\n}\n"],"mappings":"AACA,SAASA,GAAG,QAAQ,UAAU;AAAC,SAGtBC,eAAe;AAExB;AACA;AACA;AAMA;AACA;AACA;AAaA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,sBAAsB,SAASD,eAAe,CAAC;EAC1D,OAAgBE,gBAAgB,GAAG,CAAC;;EAEpC;AACF;AACA;AACA;AACA;AACA;AACA;EACE,OAAO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ACF;MAEA,IAAI;QACF,IAAIH,sBAAsB,CAACE,WAAW,CAACC,OAAO,CAAC,EAAE;UAC/C,IAAI,CAACiD,aAAa,CAACzC,QAAQ,EAAER,OAAO,CAAC;QACvC,CAAC,MAAM,IAAIH,sBAAsB,CAACQ,SAAS,CAACL,OAAO,CAAC,EAAE;UACpD,IAAIA,OAAO,CAACI,MAAM,KAAK,QAAQ,EAAE;YAC/B,IAAI,CAACgC,oBAAoB,CAAC5B,QAAQ,EAAER,OAAO,CAAC;UAC9C,CAAC,MAAM;YACL,IAAI,CAACkC,cAAc,CAAC1B,QAAQ,EAAER,OAAO,CAAC;UACxC;QACF,CAAC,MAAM,IAAIH,sBAAsB,CAACS,UAAU,CAACN,OAAO,CAAC,EAAE;UACrD,IAAI,CAACmC,eAAe,CAACnC,OAAO,CAAC;QAC/B,CAAC,MAAM;UACL,MAAM,IAAI4B,KAAK,CACb,+CAA+CP,IAAI,CAACY,SAAS,CAAC;YAC5DzB,QAAQ;YACRR;UACF,CAAC,CAAC,EACJ,CAAC;QACH;MACF,CAAC,CAAC,OAAOU,KAAK,EAAE;QACd,IAAI,CAACmB,WAAW,CAACrB,QAAQ,EAAER,OAAO,EAAEU,KAAc,CAAC;MACrD;IACF,CAAC,CAAC;EACJ;AACF","ignoreList":[]}
|
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/// <reference types="node" />
|
|
4
|
-
import { IncomingMessage } from 'node:http';
|
|
5
|
-
import { Socket } from 'node:net';
|
|
1
|
+
import type { IncomingMessage } from 'node:http';
|
|
2
|
+
import type { Socket } from 'node:net';
|
|
6
3
|
/**
|
|
7
4
|
* Delegate with implementation for HMR-specific functions.
|
|
8
5
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","names":[],"sources":["../../../src/plugins/wss/types.ts"],"sourcesContent":["import { IncomingMessage } from 'node:http';\nimport { Socket } from 'node:net';\n\n/**\n * Delegate with implementation for HMR-specific functions.\n */\nexport interface HmrDelegate {\n /** Get URI under which HMR server will be running, e.g: `/hmr` */\n getUriPath: () => string;\n\n /**\n * Callback for when the new HMR client is connected.\n *\n * Useful for running initial synchronization or any other side effect.\n *\n * @param platform Platform of the connected client.\n * @param clientId Id of the connected client.\n */\n onClientConnected: (platform: string, clientId: string) => void;\n}\n\nexport interface WebSocketServerInterface {\n shouldUpgrade(pathname: string): boolean;\n upgrade(request: IncomingMessage, socket: Socket, head: Buffer): void;\n}\n"],"mappings":""}
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../../../src/plugins/wss/types.ts"],"sourcesContent":["import type { IncomingMessage } from 'node:http';\nimport type { Socket } from 'node:net';\n\n/**\n * Delegate with implementation for HMR-specific functions.\n */\nexport interface HmrDelegate {\n /** Get URI under which HMR server will be running, e.g: `/hmr` */\n getUriPath: () => string;\n\n /**\n * Callback for when the new HMR client is connected.\n *\n * Useful for running initial synchronization or any other side effect.\n *\n * @param platform Platform of the connected client.\n * @param clientId Id of the connected client.\n */\n onClientConnected: (platform: string, clientId: string) => void;\n}\n\nexport interface WebSocketServerInterface {\n shouldUpgrade(pathname: string): boolean;\n upgrade(request: IncomingMessage, socket: Socket, head: Buffer): void;\n}\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import type { FastifyInstance } from 'fastify';
|
|
2
2
|
import type { Server } from '../../types';
|
|
3
|
-
import {
|
|
3
|
+
import { WebSocketRouter } from './WebSocketRouter';
|
|
4
|
+
import { WebSocketServerAdapter } from './WebSocketServerAdapter';
|
|
5
|
+
import { WebSocketApiServer } from './servers/WebSocketApiServer';
|
|
4
6
|
import { WebSocketDevClientServer } from './servers/WebSocketDevClientServer';
|
|
5
|
-
import { WebSocketMessageServer } from './servers/WebSocketMessageServer';
|
|
6
7
|
import { WebSocketEventsServer } from './servers/WebSocketEventsServer';
|
|
7
|
-
import { WebSocketApiServer } from './servers/WebSocketApiServer';
|
|
8
8
|
import { WebSocketHMRServer } from './servers/WebSocketHMRServer';
|
|
9
|
-
import {
|
|
10
|
-
import { WebSocketServerAdapter } from './WebSocketServerAdapter';
|
|
9
|
+
import { WebSocketMessageServer } from './servers/WebSocketMessageServer';
|
|
11
10
|
declare module 'fastify' {
|
|
12
11
|
interface FastifyInstance {
|
|
13
12
|
wss: {
|
|
14
|
-
debuggerServer: WebSocketDebuggerServer;
|
|
15
13
|
devClientServer: WebSocketDevClientServer;
|
|
16
14
|
messageServer: WebSocketMessageServer;
|
|
17
15
|
eventsServer: WebSocketEventsServer;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
-
import {
|
|
2
|
+
import { WebSocketRouter } from "./WebSocketRouter.js";
|
|
3
|
+
import { WebSocketServerAdapter } from "./WebSocketServerAdapter.js";
|
|
4
|
+
import { WebSocketApiServer } from "./servers/WebSocketApiServer.js";
|
|
3
5
|
import { WebSocketDevClientServer } from "./servers/WebSocketDevClientServer.js";
|
|
4
|
-
import { WebSocketMessageServer } from "./servers/WebSocketMessageServer.js";
|
|
5
6
|
import { WebSocketEventsServer } from "./servers/WebSocketEventsServer.js";
|
|
6
|
-
import { WebSocketApiServer } from "./servers/WebSocketApiServer.js";
|
|
7
7
|
import { WebSocketHMRServer } from "./servers/WebSocketHMRServer.js";
|
|
8
|
-
import {
|
|
9
|
-
import { WebSocketServerAdapter } from "./WebSocketServerAdapter.js";
|
|
8
|
+
import { WebSocketMessageServer } from "./servers/WebSocketMessageServer.js";
|
|
10
9
|
/**
|
|
11
10
|
* Defined in @react-native/dev-middleware
|
|
12
11
|
* Reference: https://github.com/facebook/react-native/blob/main/packages/dev-middleware/src/inspector-proxy/InspectorProxy.js
|
|
@@ -18,7 +17,6 @@ async function wssPlugin(instance, {
|
|
|
18
17
|
delegate
|
|
19
18
|
}) {
|
|
20
19
|
const router = new WebSocketRouter(instance);
|
|
21
|
-
const debuggerServer = new WebSocketDebuggerServer(instance);
|
|
22
20
|
const devClientServer = new WebSocketDevClientServer(instance);
|
|
23
21
|
const messageServer = new WebSocketMessageServer(instance);
|
|
24
22
|
const eventsServer = new WebSocketEventsServer(instance, {
|
|
@@ -30,7 +28,6 @@ async function wssPlugin(instance, {
|
|
|
30
28
|
// @react-native/dev-middleware servers
|
|
31
29
|
const deviceConnectionServer = new WebSocketServerAdapter(instance, WS_DEVICE_URL, options.endpoints?.[WS_DEVICE_URL]);
|
|
32
30
|
const debuggerConnectionServer = new WebSocketServerAdapter(instance, WS_DEBUGGER_URL, options.endpoints?.[WS_DEBUGGER_URL]);
|
|
33
|
-
router.registerServer(debuggerServer);
|
|
34
31
|
router.registerServer(devClientServer);
|
|
35
32
|
router.registerServer(messageServer);
|
|
36
33
|
router.registerServer(eventsServer);
|
|
@@ -39,7 +36,6 @@ async function wssPlugin(instance, {
|
|
|
39
36
|
router.registerServer(deviceConnectionServer);
|
|
40
37
|
router.registerServer(debuggerConnectionServer);
|
|
41
38
|
instance.decorate('wss', {
|
|
42
|
-
debuggerServer,
|
|
43
39
|
devClientServer,
|
|
44
40
|
messageServer,
|
|
45
41
|
eventsServer,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wssPlugin.js","names":["fastifyPlugin","
|
|
1
|
+
{"version":3,"file":"wssPlugin.js","names":["fastifyPlugin","WebSocketRouter","WebSocketServerAdapter","WebSocketApiServer","WebSocketDevClientServer","WebSocketEventsServer","WebSocketHMRServer","WebSocketMessageServer","WS_DEVICE_URL","WS_DEBUGGER_URL","wssPlugin","instance","options","delegate","router","devClientServer","messageServer","eventsServer","webSocketMessageServer","apiServer","hmrServer","hmr","deviceConnectionServer","endpoints","debuggerConnectionServer","registerServer","decorate","name"],"sources":["../../../src/plugins/wss/wssPlugin.ts"],"sourcesContent":["import type { FastifyInstance } from 'fastify';\nimport fastifyPlugin from 'fastify-plugin';\nimport type { Server } from '../../types';\nimport { WebSocketRouter } from './WebSocketRouter';\nimport { WebSocketServerAdapter } from './WebSocketServerAdapter';\nimport { WebSocketApiServer } from './servers/WebSocketApiServer';\nimport { WebSocketDevClientServer } from './servers/WebSocketDevClientServer';\nimport { WebSocketEventsServer } from './servers/WebSocketEventsServer';\nimport { WebSocketHMRServer } from './servers/WebSocketHMRServer';\nimport { WebSocketMessageServer } from './servers/WebSocketMessageServer';\n\ndeclare module 'fastify' {\n interface FastifyInstance {\n wss: {\n devClientServer: WebSocketDevClientServer;\n messageServer: WebSocketMessageServer;\n eventsServer: WebSocketEventsServer;\n apiServer: WebSocketApiServer;\n hmrServer: WebSocketHMRServer;\n deviceConnectionServer: WebSocketServerAdapter;\n debuggerConnectionServer: WebSocketServerAdapter;\n router: WebSocketRouter;\n };\n }\n}\n\n/**\n * Defined in @react-native/dev-middleware\n * Reference: https://github.com/facebook/react-native/blob/main/packages/dev-middleware/src/inspector-proxy/InspectorProxy.js\n */\nconst WS_DEVICE_URL = '/inspector/device';\nconst WS_DEBUGGER_URL = '/inspector/debug';\n\nasync function wssPlugin(\n instance: FastifyInstance,\n {\n options,\n delegate,\n }: {\n options: Server.Options;\n delegate: Server.Delegate;\n }\n) {\n const router = new WebSocketRouter(instance);\n\n const devClientServer = new WebSocketDevClientServer(instance);\n const messageServer = new WebSocketMessageServer(instance);\n const eventsServer = new WebSocketEventsServer(instance, {\n webSocketMessageServer: messageServer,\n });\n const apiServer = new WebSocketApiServer(instance);\n const hmrServer = new WebSocketHMRServer(instance, delegate.hmr);\n\n // @react-native/dev-middleware servers\n const deviceConnectionServer = new WebSocketServerAdapter(\n instance,\n WS_DEVICE_URL,\n options.endpoints?.[WS_DEVICE_URL]\n );\n\n const debuggerConnectionServer = new WebSocketServerAdapter(\n instance,\n WS_DEBUGGER_URL,\n options.endpoints?.[WS_DEBUGGER_URL]\n );\n\n router.registerServer(devClientServer);\n router.registerServer(messageServer);\n router.registerServer(eventsServer);\n router.registerServer(apiServer);\n router.registerServer(hmrServer);\n router.registerServer(deviceConnectionServer);\n router.registerServer(debuggerConnectionServer);\n\n instance.decorate('wss', {\n devClientServer,\n messageServer,\n eventsServer,\n apiServer,\n hmrServer,\n deviceConnectionServer,\n debuggerConnectionServer,\n router,\n });\n}\n\nexport default fastifyPlugin(wssPlugin, {\n name: 'wss-plugin',\n});\n"],"mappings":"AACA,OAAOA,aAAa,MAAM,gBAAgB;AAAC,SAElCC,eAAe;AAAA,SACfC,sBAAsB;AAAA,SACtBC,kBAAkB;AAAA,SAClBC,wBAAwB;AAAA,SACxBC,qBAAqB;AAAA,SACrBC,kBAAkB;AAAA,SAClBC,sBAAsB;AAiB/B;AACA;AACA;AACA;AACA,MAAMC,aAAa,GAAG,mBAAmB;AACzC,MAAMC,eAAe,GAAG,kBAAkB;AAE1C,eAAeC,SAASA,CACtBC,QAAyB,EACzB;EACEC,OAAO;EACPC;AAIF,CAAC,EACD;EACA,MAAMC,MAAM,GAAG,IAAIb,eAAe,CAACU,QAAQ,CAAC;EAE5C,MAAMI,eAAe,GAAG,IAAIX,wBAAwB,CAACO,QAAQ,CAAC;EAC9D,MAAMK,aAAa,GAAG,IAAIT,sBAAsB,CAACI,QAAQ,CAAC;EAC1D,MAAMM,YAAY,GAAG,IAAIZ,qBAAqB,CAACM,QAAQ,EAAE;IACvDO,sBAAsB,EAAEF;EAC1B,CAAC,CAAC;EACF,MAAMG,SAAS,GAAG,IAAIhB,kBAAkB,CAACQ,QAAQ,CAAC;EAClD,MAAMS,SAAS,GAAG,IAAId,kBAAkB,CAACK,QAAQ,EAAEE,QAAQ,CAACQ,GAAG,CAAC;;EAEhE;EACA,MAAMC,sBAAsB,GAAG,IAAIpB,sBAAsB,CACvDS,QAAQ,EACRH,aAAa,EACbI,OAAO,CAACW,SAAS,GAAGf,aAAa,CACnC,CAAC;EAED,MAAMgB,wBAAwB,GAAG,IAAItB,sBAAsB,CACzDS,QAAQ,EACRF,eAAe,EACfG,OAAO,CAACW,SAAS,GAAGd,eAAe,CACrC,CAAC;EAEDK,MAAM,CAACW,cAAc,CAACV,eAAe,C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