@callstack/repack-dev-server 1.0.0-next.0
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 +14 -0
- package/README.md +32 -0
- package/dist/createServer.d.ts +13 -0
- package/dist/createServer.js +111 -0
- package/dist/createServer.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/compiler/compilerPlugin.d.ts +6 -0
- package/dist/plugins/compiler/compilerPlugin.js +85 -0
- package/dist/plugins/compiler/compilerPlugin.js.map +1 -0
- package/dist/plugins/compiler/index.d.ts +2 -0
- package/dist/plugins/compiler/index.js +3 -0
- package/dist/plugins/compiler/index.js.map +1 -0
- package/dist/plugins/compiler/types.d.ts +31 -0
- package/dist/plugins/compiler/types.js +2 -0
- package/dist/plugins/compiler/types.js.map +1 -0
- package/dist/plugins/devtools/devtoolsPlugin.d.ts +6 -0
- package/dist/plugins/devtools/devtoolsPlugin.js +109 -0
- package/dist/plugins/devtools/devtoolsPlugin.js.map +1 -0
- package/dist/plugins/devtools/index.d.ts +1 -0
- package/dist/plugins/devtools/index.js +2 -0
- package/dist/plugins/devtools/index.js.map +1 -0
- package/dist/plugins/multipart/index.d.ts +2 -0
- package/dist/plugins/multipart/index.js +3 -0
- package/dist/plugins/multipart/index.js.map +1 -0
- package/dist/plugins/multipart/multipartPlugin.d.ts +3 -0
- package/dist/plugins/multipart/multipartPlugin.js +68 -0
- package/dist/plugins/multipart/multipartPlugin.js.map +1 -0
- package/dist/plugins/multipart/types.d.ts +12 -0
- package/dist/plugins/multipart/types.js +2 -0
- package/dist/plugins/multipart/types.js.map +1 -0
- package/dist/plugins/symbolicate/Symbolicator.d.ts +46 -0
- package/dist/plugins/symbolicate/Symbolicator.js +227 -0
- package/dist/plugins/symbolicate/Symbolicator.js.map +1 -0
- package/dist/plugins/symbolicate/index.d.ts +3 -0
- package/dist/plugins/symbolicate/index.js +4 -0
- package/dist/plugins/symbolicate/index.js.map +1 -0
- package/dist/plugins/symbolicate/sybmolicatePlugin.d.ts +6 -0
- package/dist/plugins/symbolicate/sybmolicatePlugin.js +46 -0
- package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +1 -0
- package/dist/plugins/symbolicate/types.d.ts +65 -0
- package/dist/plugins/symbolicate/types.js +2 -0
- package/dist/plugins/symbolicate/types.js.map +1 -0
- package/dist/plugins/wss/WebSocketRouter.d.ts +32 -0
- package/dist/plugins/wss/WebSocketRouter.js +61 -0
- package/dist/plugins/wss/WebSocketRouter.js.map +1 -0
- package/dist/plugins/wss/WebSocketServer.d.ts +37 -0
- package/dist/plugins/wss/WebSocketServer.js +49 -0
- package/dist/plugins/wss/WebSocketServer.js.map +1 -0
- package/dist/plugins/wss/index.d.ts +3 -0
- package/dist/plugins/wss/index.js +4 -0
- package/dist/plugins/wss/index.js.map +1 -0
- package/dist/plugins/wss/servers/HermesInspectorProxy.d.ts +24 -0
- package/dist/plugins/wss/servers/HermesInspectorProxy.js +138 -0
- package/dist/plugins/wss/servers/HermesInspectorProxy.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketDashboardServer.d.ts +34 -0
- package/dist/plugins/wss/servers/WebSocketDashboardServer.js +70 -0
- package/dist/plugins/wss/servers/WebSocketDashboardServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.d.ts +64 -0
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.js +156 -0
- package/dist/plugins/wss/servers/WebSocketDebuggerServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketDevClientServer.d.ts +32 -0
- package/dist/plugins/wss/servers/WebSocketDevClientServer.js +95 -0
- package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketEventsServer.d.ts +75 -0
- package/dist/plugins/wss/servers/WebSocketEventsServer.js +187 -0
- package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketHMRServer.d.ts +38 -0
- package/dist/plugins/wss/servers/WebSocketHMRServer.js +103 -0
- package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +1 -0
- package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +140 -0
- package/dist/plugins/wss/servers/WebSocketMessageServer.js +380 -0
- package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +1 -0
- package/dist/plugins/wss/types.d.ts +16 -0
- package/dist/plugins/wss/types.js +2 -0
- package/dist/plugins/wss/types.js.map +1 -0
- package/dist/plugins/wss/wssPlugin.d.ts +29 -0
- package/dist/plugins/wss/wssPlugin.js +47 -0
- package/dist/plugins/wss/wssPlugin.js.map +1 -0
- package/dist/types.d.ts +127 -0
- package/dist/types.js +24 -0
- package/dist/types.js.map +1 -0
- package/package.json +75 -0
- package/vendor/metro-inspector-proxy/src/Device.cjs +1 -0
- package/vendor/metro-inspector-proxy/src/Device.js +1 -0
|
@@ -0,0 +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,O"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { IncomingMessage } from 'http';
|
|
3
|
+
import WebSocket from 'ws';
|
|
4
|
+
import type { FastifyInstance } from 'fastify';
|
|
5
|
+
import { WebSocketServer } from '../WebSocketServer';
|
|
6
|
+
import { Server } from '../../../types';
|
|
7
|
+
export interface InspectorProxyConfig extends Pick<Server.Options, 'port' | 'host' | 'rootDir'> {
|
|
8
|
+
}
|
|
9
|
+
export declare class HermesInspectorProxy extends WebSocketServer {
|
|
10
|
+
private config;
|
|
11
|
+
private devices;
|
|
12
|
+
private deviceCounter;
|
|
13
|
+
readonly serverHost: string;
|
|
14
|
+
constructor(fastify: FastifyInstance, config: InspectorProxyConfig);
|
|
15
|
+
private setup;
|
|
16
|
+
private buildPageDescription;
|
|
17
|
+
/**
|
|
18
|
+
* Process new WebSocket connection from device.
|
|
19
|
+
*
|
|
20
|
+
* @param socket Incoming device's WebSocket connection.
|
|
21
|
+
* @param request Upgrade request for the connection.
|
|
22
|
+
*/
|
|
23
|
+
onConnection(socket: WebSocket, request: IncomingMessage): void;
|
|
24
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { URL } from 'url';
|
|
2
|
+
import { WebSocketServer } from "../WebSocketServer.js";
|
|
3
|
+
// @ts-ignore
|
|
4
|
+
import Device from '../../../../vendor/metro-inspector-proxy/src/Device.js';
|
|
5
|
+
const WS_DEVICE_URL = '/inspector/device';
|
|
6
|
+
const WS_DEBUGGER_URL = '/inspector/debug';
|
|
7
|
+
export class HermesInspectorProxy extends WebSocketServer {
|
|
8
|
+
devices = new Map();
|
|
9
|
+
deviceCounter = 0;
|
|
10
|
+
|
|
11
|
+
constructor(fastify, config) {
|
|
12
|
+
super(fastify, [WS_DEVICE_URL, WS_DEBUGGER_URL]);
|
|
13
|
+
this.config = config;
|
|
14
|
+
this.serverHost = `${this.config.host || 'localhost'}:${this.config.port}`;
|
|
15
|
+
this.setup();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
setup() {
|
|
19
|
+
const onSend = (_request, reply, _payload, done) => {
|
|
20
|
+
reply.headers({
|
|
21
|
+
'Content-Type': 'application/json; charset=UTF-8',
|
|
22
|
+
'Cache-Control': 'no-cache',
|
|
23
|
+
Connection: 'close'
|
|
24
|
+
});
|
|
25
|
+
done();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
this.fastify.get('/json/version', {
|
|
29
|
+
onSend
|
|
30
|
+
}, async () => {
|
|
31
|
+
return {
|
|
32
|
+
Browser: 'Mobile JavaScript',
|
|
33
|
+
'Protocol-Version': '1.1'
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const pageListHandler = async () => {
|
|
38
|
+
const pages = [];
|
|
39
|
+
|
|
40
|
+
for (const [deviceId, device] of this.devices) {
|
|
41
|
+
const devicePages = device.getPagesList();
|
|
42
|
+
|
|
43
|
+
for (const page of devicePages) {
|
|
44
|
+
pages.push(this.buildPageDescription(deviceId, page));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return pages;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
this.fastify.get('/json/list', {
|
|
52
|
+
onSend
|
|
53
|
+
}, pageListHandler);
|
|
54
|
+
this.fastify.get('/json', {
|
|
55
|
+
onSend,
|
|
56
|
+
logLevel: 'silent'
|
|
57
|
+
}, pageListHandler);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
buildPageDescription(deviceId, page) {
|
|
61
|
+
const debuggerUrl = `${this.serverHost}${WS_DEBUGGER_URL}?device=${deviceId}&page=${page.id}`;
|
|
62
|
+
const webSocketDebuggerUrl = 'ws://' + debuggerUrl;
|
|
63
|
+
const devtoolsFrontendUrl = 'chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=' + encodeURIComponent(debuggerUrl);
|
|
64
|
+
return {
|
|
65
|
+
id: `${deviceId}-${page.id}`,
|
|
66
|
+
description: page.app,
|
|
67
|
+
title: page.title,
|
|
68
|
+
faviconUrl: 'https://reactjs.org/favicon.ico',
|
|
69
|
+
devtoolsFrontendUrl,
|
|
70
|
+
type: 'node',
|
|
71
|
+
webSocketDebuggerUrl,
|
|
72
|
+
vm: page.vm
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Process new WebSocket connection from device.
|
|
77
|
+
*
|
|
78
|
+
* @param socket Incoming device's WebSocket connection.
|
|
79
|
+
* @param request Upgrade request for the connection.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
onConnection(socket, request) {
|
|
84
|
+
try {
|
|
85
|
+
const {
|
|
86
|
+
url = ''
|
|
87
|
+
} = request;
|
|
88
|
+
const {
|
|
89
|
+
searchParams
|
|
90
|
+
} = new URL(url, 'http://localhost');
|
|
91
|
+
|
|
92
|
+
if (url.startsWith('/inspector/device')) {
|
|
93
|
+
const deviceName = searchParams.get('name') ?? 'Unknown';
|
|
94
|
+
const appName = searchParams.get('app') ?? 'Unknown';
|
|
95
|
+
const deviceId = this.deviceCounter++;
|
|
96
|
+
this.devices.set(deviceId, new Device(deviceId, deviceName, appName, socket, this.config.rootDir));
|
|
97
|
+
this.fastify.log.info({
|
|
98
|
+
msg: 'Hermes device connected',
|
|
99
|
+
deviceId
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
const onClose = () => {
|
|
103
|
+
this.fastify.log.info({
|
|
104
|
+
msg: 'Hermes device disconnected',
|
|
105
|
+
deviceId
|
|
106
|
+
});
|
|
107
|
+
this.devices.delete(deviceId);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
socket.addEventListener('error', onClose);
|
|
111
|
+
socket.addEventListener('close', onClose);
|
|
112
|
+
} else {
|
|
113
|
+
const deviceId = searchParams.get('device') ?? undefined;
|
|
114
|
+
const pageId = searchParams.get('page') ?? undefined;
|
|
115
|
+
|
|
116
|
+
if (deviceId === undefined || pageId === undefined) {
|
|
117
|
+
throw new Error('Incorrect URL - must provide device and page IDs');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const device = this.devices.get(parseInt(deviceId, 10));
|
|
121
|
+
|
|
122
|
+
if (!device) {
|
|
123
|
+
throw new Error('Unknown device with ID ' + deviceId);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
device.handleDebuggerConnection(socket, pageId);
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
this.fastify.log.error({
|
|
130
|
+
msg: 'Failed to establish connection with Hermes device',
|
|
131
|
+
error: error.message
|
|
132
|
+
});
|
|
133
|
+
socket.close(1011, error.toString());
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=HermesInspectorProxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HermesInspectorProxy.js","names":["URL","WebSocketServer","Device","WS_DEVICE_URL","WS_DEBUGGER_URL","HermesInspectorProxy","devices","Map","deviceCounter","constructor","fastify","config","serverHost","host","port","setup","onSend","_request","reply","_payload","done","headers","Connection","get","Browser","pageListHandler","pages","deviceId","device","devicePages","getPagesList","page","push","buildPageDescription","logLevel","debuggerUrl","id","webSocketDebuggerUrl","devtoolsFrontendUrl","encodeURIComponent","description","app","title","faviconUrl","type","vm","onConnection","socket","request","url","searchParams","startsWith","deviceName","appName","set","rootDir","log","info","msg","onClose","delete","addEventListener","undefined","pageId","Error","parseInt","handleDebuggerConnection","error","message","close","toString"],"sources":["../../../../src/plugins/wss/servers/HermesInspectorProxy.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\nimport { URL } from 'url';\nimport WebSocket from 'ws';\nimport type {\n FastifyInstance,\n FastifyReply,\n FastifyRequest,\n LogLevel,\n} from 'fastify';\nimport { WebSocketServer } from '../WebSocketServer';\nimport { Server } from '../../../types';\n// @ts-ignore\nimport Device from '../../../../vendor/metro-inspector-proxy/src/Device.js';\n\nconst WS_DEVICE_URL = '/inspector/device';\nconst WS_DEBUGGER_URL = '/inspector/debug';\n\ninterface PageDescription {\n id: string;\n description: string;\n title: string;\n faviconUrl: string;\n devtoolsFrontendUrl: string;\n type: string;\n webSocketDebuggerUrl: string;\n vm?: string;\n}\n\ninterface Page {\n id: string;\n title: string;\n app: string;\n vm?: string;\n}\n\nexport interface InspectorProxyConfig\n extends Pick<Server.Options, 'port' | 'host' | 'rootDir'> {}\n\nexport class HermesInspectorProxy extends WebSocketServer {\n private devices = new Map<number, any>();\n private deviceCounter = 0;\n public readonly serverHost: string;\n\n constructor(fastify: FastifyInstance, private config: InspectorProxyConfig) {\n super(fastify, [WS_DEVICE_URL, WS_DEBUGGER_URL]);\n this.serverHost = `${this.config.host || 'localhost'}:${this.config.port}`;\n this.setup();\n }\n\n private setup() {\n const onSend = (\n _request: FastifyRequest,\n reply: FastifyReply,\n _payload: unknown,\n done: () => void\n ) => {\n reply.headers({\n 'Content-Type': 'application/json; charset=UTF-8',\n 'Cache-Control': 'no-cache',\n Connection: 'close',\n });\n done();\n };\n\n this.fastify.get('/json/version', { onSend }, async () => {\n return {\n Browser: 'Mobile JavaScript',\n 'Protocol-Version': '1.1',\n };\n });\n\n const pageListHandler = async () => {\n const pages: PageDescription[] = [];\n for (const [deviceId, device] of this.devices) {\n const devicePages = device.getPagesList();\n for (const page of devicePages) {\n pages.push(this.buildPageDescription(deviceId, page));\n }\n }\n\n return pages;\n };\n\n this.fastify.get('/json/list', { onSend }, pageListHandler);\n this.fastify.get(\n '/json',\n { onSend, logLevel: 'silent' as LogLevel },\n pageListHandler\n );\n }\n\n private buildPageDescription(deviceId: number, page: Page) {\n const debuggerUrl = `${this.serverHost}${WS_DEBUGGER_URL}?device=${deviceId}&page=${page.id}`;\n const webSocketDebuggerUrl = 'ws://' + debuggerUrl;\n const devtoolsFrontendUrl =\n 'chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=' +\n encodeURIComponent(debuggerUrl);\n return {\n id: `${deviceId}-${page.id}`,\n description: page.app,\n title: page.title,\n faviconUrl: 'https://reactjs.org/favicon.ico',\n devtoolsFrontendUrl,\n type: 'node',\n webSocketDebuggerUrl,\n vm: page.vm,\n };\n }\n\n /**\n * Process new WebSocket connection from device.\n *\n * @param socket Incoming device's WebSocket connection.\n * @param request Upgrade request for the connection.\n */\n onConnection(socket: WebSocket, request: IncomingMessage) {\n try {\n const { url = '' } = request;\n const { searchParams } = new URL(url, 'http://localhost');\n\n if (url.startsWith('/inspector/device')) {\n const deviceName = searchParams.get('name') ?? 'Unknown';\n const appName = searchParams.get('app') ?? 'Unknown';\n const deviceId = this.deviceCounter++;\n\n this.devices.set(\n deviceId,\n new Device(deviceId, deviceName, appName, socket, this.config.rootDir)\n );\n\n this.fastify.log.info({ msg: 'Hermes device connected', deviceId });\n\n const onClose = () => {\n this.fastify.log.info({\n msg: 'Hermes device disconnected',\n deviceId,\n });\n this.devices.delete(deviceId);\n };\n\n socket.addEventListener('error', onClose);\n socket.addEventListener('close', onClose);\n } else {\n const deviceId = searchParams.get('device') ?? undefined;\n const pageId = searchParams.get('page') ?? undefined;\n\n if (deviceId === undefined || pageId === undefined) {\n throw new Error('Incorrect URL - must provide device and page IDs');\n }\n\n const device = this.devices.get(parseInt(deviceId, 10));\n if (!device) {\n throw new Error('Unknown device with ID ' + deviceId);\n }\n\n device.handleDebuggerConnection(socket, pageId);\n }\n } catch (error) {\n this.fastify.log.error({\n msg: 'Failed to establish connection with Hermes device',\n error: (error as Error).message,\n });\n socket.close(1011, (error as Error).toString());\n }\n }\n}\n"],"mappings":"AACA,SAASA,GAAT,QAAoB,KAApB;SAQSC,e;AAET;AACA,OAAOC,MAAP,MAAmB,wDAAnB;AAEA,MAAMC,aAAa,GAAG,mBAAtB;AACA,MAAMC,eAAe,GAAG,kBAAxB;AAuBA,OAAO,MAAMC,oBAAN,SAAmCJ,eAAnC,CAAmD;EAChDK,OAAO,GAAG,IAAIC,GAAJ,EAAH;EACPC,aAAa,GAAG,CAAH;;EAGrBC,WAAW,CAACC,OAAD,EAAmCC,MAAnC,EAAiE;IAC1E,MAAMD,OAAN,EAAe,CAACP,aAAD,EAAgBC,eAAhB,CAAf;IAD0E,KAA9BO,MAA8B,GAA9BA,MAA8B;IAE1E,KAAKC,UAAL,GAAmB,GAAE,KAAKD,MAAL,CAAYE,IAAZ,IAAoB,WAAY,IAAG,KAAKF,MAAL,CAAYG,IAAK,EAAzE;IACA,KAAKC,KAAL;EACD;;EAEOA,KAAK,GAAG;IACd,MAAMC,MAAM,GAAG,CACbC,QADa,EAEbC,KAFa,EAGbC,QAHa,EAIbC,IAJa,KAKV;MACHF,KAAK,CAACG,OAAN,CAAc;QACZ,gBAAgB,iCADJ;QAEZ,iBAAiB,UAFL;QAGZC,UAA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AAiB,QAAjB,KAA8BuC,SAA/C;QACA,MAAMC,MAAM,GAAGb,YAAY,CAAC3B,GAAb,CAAiB,MAAjB,KAA4BuC,SAA3C;;QAEA,IAAInC,QAAQ,KAAKmC,SAAb,IAA0BC,MAAM,KAAKD,SAAzC,EAAoD;UAClD,MAAM,IAAIE,KAAJ,CAAU,kDAAV,CAAN;QACD;;QAED,MAAMpC,MAAM,GAAG,KAAKtB,OAAL,CAAaiB,GAAb,CAAiB0C,QAAQ,CAACtC,QAAD,EAAW,EAAX,CAAzB,CAAf;;QACA,IAAI,CAACC,MAAL,EAAa;UACX,MAAM,IAAIoC,KAAJ,CAAU,4BAA4BrC,QAAtC,CAAN;QACD;;QAEDC,MAAM,CAACsC,wBAAP,CAAgCnB,MAAhC,EAAwCgB,MAAxC;MACD;IACF,CAzCD,CAyCE,OAAOI,KAAP,EAAc;MACd,KAAKzD,OAAL,CAAa8C,GAAb,CAAiBW,KAAjB,CAAuB;QACrBT,GAAG,EAAE,mDADgB;QAErBS,KAAK,EAAGA,KAAD,CAAiBC;MAFH,CAAvB;MAIArB,MAAM,CAACsB,KAAP,CAAa,IAAb,EAAoBF,KAAD,CAAiBG,QAAjB,EAAnB;IACD;EACF;;AA9HuD"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FastifyInstance } from 'fastify';
|
|
2
|
+
import WebSocket from 'ws';
|
|
3
|
+
import { WebSocketServer } from '../WebSocketServer';
|
|
4
|
+
/**
|
|
5
|
+
* Class for creating a WebSocket server for Dashboard client.
|
|
6
|
+
* It's used by built-in Dashboard web-app to receive compilation
|
|
7
|
+
* events, logs and other necessary messages.
|
|
8
|
+
*
|
|
9
|
+
* @category Development server
|
|
10
|
+
*/
|
|
11
|
+
export declare class WebSocketDashboardServer extends WebSocketServer {
|
|
12
|
+
private clients;
|
|
13
|
+
private nextClientId;
|
|
14
|
+
/**
|
|
15
|
+
* Create new instance of WebSocketDashboardServer and attach it to the given Fastify instance.
|
|
16
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
17
|
+
*
|
|
18
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
19
|
+
* @param emitter Event emitter instance.
|
|
20
|
+
*/
|
|
21
|
+
constructor(fastify: FastifyInstance);
|
|
22
|
+
/**
|
|
23
|
+
* Send message to all connected Dashboard clients.
|
|
24
|
+
*
|
|
25
|
+
* @param event Event string or object to send.
|
|
26
|
+
*/
|
|
27
|
+
send(event: any): void;
|
|
28
|
+
/**
|
|
29
|
+
* Process new WebSocket connection from client application.
|
|
30
|
+
*
|
|
31
|
+
* @param socket Incoming client's WebSocket connection.
|
|
32
|
+
*/
|
|
33
|
+
onConnection(socket: WebSocket): void;
|
|
34
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { WebSocketServer } from "../WebSocketServer.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class for creating a WebSocket server for Dashboard client.
|
|
4
|
+
* It's used by built-in Dashboard web-app to receive compilation
|
|
5
|
+
* events, logs and other necessary messages.
|
|
6
|
+
*
|
|
7
|
+
* @category Development server
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export class WebSocketDashboardServer extends WebSocketServer {
|
|
11
|
+
clients = new Map();
|
|
12
|
+
nextClientId = 0;
|
|
13
|
+
/**
|
|
14
|
+
* Create new instance of WebSocketDashboardServer and attach it to the given Fastify instance.
|
|
15
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
16
|
+
*
|
|
17
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
18
|
+
* @param emitter Event emitter instance.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
constructor(fastify) {
|
|
22
|
+
super(fastify, '/api/dashboard');
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Send message to all connected Dashboard clients.
|
|
26
|
+
*
|
|
27
|
+
* @param event Event string or object to send.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
send(event) {
|
|
32
|
+
const data = typeof event === 'string' ? event : JSON.stringify(event);
|
|
33
|
+
|
|
34
|
+
for (const [, socket] of this.clients.entries()) {
|
|
35
|
+
try {
|
|
36
|
+
socket.send(data);
|
|
37
|
+
} catch {// NOOP
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Process new WebSocket connection from client application.
|
|
43
|
+
*
|
|
44
|
+
* @param socket Incoming client's WebSocket connection.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
onConnection(socket) {
|
|
49
|
+
const clientId = `client#${this.nextClientId++}`;
|
|
50
|
+
this.clients.set(clientId, socket);
|
|
51
|
+
this.fastify.log.info({
|
|
52
|
+
msg: 'Dashboard client connected',
|
|
53
|
+
clientId
|
|
54
|
+
});
|
|
55
|
+
this.clients.set(clientId, socket);
|
|
56
|
+
|
|
57
|
+
const onClose = () => {
|
|
58
|
+
this.fastify.log.info({
|
|
59
|
+
msg: 'Dashboard client disconnected',
|
|
60
|
+
clientId
|
|
61
|
+
});
|
|
62
|
+
this.clients.delete(clientId);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
socket.addEventListener('error', onClose);
|
|
66
|
+
socket.addEventListener('close', onClose);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=WebSocketDashboardServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocketDashboardServer.js","names":["WebSocketServer","WebSocketDashboardServer","clients","Map","nextClientId","constructor","fastify","send","event","data","JSON","stringify","socket","entries","onConnection","clientId","set","log","info","msg","onClose","delete","addEventListener"],"sources":["../../../../src/plugins/wss/servers/WebSocketDashboardServer.ts"],"sourcesContent":["import { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server for Dashboard client.\n * It's used by built-in Dashboard web-app to receive compilation\n * events, logs and other necessary messages.\n *\n * @category Development server\n */\nexport class WebSocketDashboardServer extends WebSocketServer {\n private clients = new Map<string, WebSocket>();\n private nextClientId = 0;\n\n /**\n * Create new instance of WebSocketDashboardServer 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 emitter Event emitter instance.\n */\n constructor(fastify: FastifyInstance) {\n super(fastify, '/api/dashboard');\n }\n\n /**\n * Send message to all connected Dashboard 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.info({ msg: 'Dashboard client connected', clientId });\n this.clients.set(clientId, socket);\n\n const onClose = () => {\n this.fastify.log.info({\n msg: 'Dashboard 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,e;AAET;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,wBAAN,SAAuCD,eAAvC,CAAuD;EACpDE,OAAO,GAAG,IAAIC,GAAJ,EAAH;EACPC,YAAY,GAAG,CAAH;EAEpB;AACF;AACA;AACA;AACA;AACA;AACA;;EACEC,WAAW,CAACC,OAAD,EAA2B;IACpC,MAAMA,OAAN,EAAe,gBAAf;EACD;EAED;AACF;AACA;AACA;AACA;;;EACEC,IAAI,CAACC,KAAD,EAAa;IACf,MAAMC,IAAI,GAAG,OAAOD,KAAP,KAAiB,QAAjB,GAA4BA,KAA5B,GAAoCE,IAAI,CAACC,SAAL,CAAeH,KAAf,CAAjD;;IAEA,KAAK,MAAM,GAAGI,MAAH,CAAX,IAAyB,KAAKV,OAAL,CAAaW,OAAb,EAAzB,EAAiD;MAC/C,IAAI;QACFD,MAAM,CAACL,IAAP,CAAYE,IAAZ;MACD,CAFD,CAEE,MAAM,CACN;MACD;IACF;EACF;EAED;AACF;AACA;AACA;AACA;;;EACEK,YAAY,CAACF,MAAD,EAAoB;IAC9B,MAAMG,QAAQ,GAAI,UAAS,KAAKX,YAAL,EAAoB,EAA/C;IACA,KAAKF,OAAL,CAAac,GAAb,CAAiBD,QAAjB,EAA2BH,MAA3B;IAEA,KAAKN,OAAL,CAAaW,GAAb,CAAiBC,IAAjB,CAAsB;MAAEC,GAAG,EAAE,4BAAP;MAAqCJ;IAArC,CAAtB;IACA,KAAKb,OAAL,CAAac,GAAb,CAAiBD,QAAjB,EAA2BH,MAA3B;;IAEA,MAAMQ,OAAO,GAAG,MAAM;MACpB,KAAKd,OAAL,CAAaW,GAAb,CAAiBC,IAAjB,CAAsB;QACpBC,GAAG,EAAE,+BADe;QAEpBJ;MAFoB,CAAtB;MAIA,KAAKb,OAAL,CAAamB,MAAb,CAAoBN,QAApB;IACD,CAND;;IAQAH,MAAM,CAACU,gBAAP,CAAwB,OAAxB,EAAiCF,OAAjC;IACAR,MAAM,CAACU,gBAAP,CAAwB,OAAxB,EAAiCF,OAAjC;EACD;;AAtD2D"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import type { IncomingMessage } from 'http';
|
|
3
|
+
import type { FastifyInstance } from 'fastify';
|
|
4
|
+
import WebSocket from 'ws';
|
|
5
|
+
import { WebSocketServer } from '../WebSocketServer';
|
|
6
|
+
/**
|
|
7
|
+
* Class for creating a WebSocket server and providing a bridge between
|
|
8
|
+
* debugger UI (Remote JS debugger) and the running React Native application.
|
|
9
|
+
*
|
|
10
|
+
* React Native application (aka client) will send and receive messages from the debugger UI
|
|
11
|
+
* which runs inside a browser.
|
|
12
|
+
*
|
|
13
|
+
* @category Development server
|
|
14
|
+
*/
|
|
15
|
+
export declare class WebSocketDebuggerServer extends WebSocketServer {
|
|
16
|
+
/**
|
|
17
|
+
* A WebSocket connection with the debugger UI.
|
|
18
|
+
*/
|
|
19
|
+
private debuggerSocket;
|
|
20
|
+
/**
|
|
21
|
+
* A WebSocket connection with the client (React Native app).
|
|
22
|
+
*/
|
|
23
|
+
private clientSocket;
|
|
24
|
+
/**
|
|
25
|
+
* Create new instance of WebSocketDebuggerServer and attach it to the given Fastify instance.
|
|
26
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
27
|
+
*
|
|
28
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
29
|
+
*/
|
|
30
|
+
constructor(fastify: FastifyInstance);
|
|
31
|
+
/**
|
|
32
|
+
* Check if debugger UI is connected to the WebSocketDebuggerServer.
|
|
33
|
+
*/
|
|
34
|
+
isDebuggerConnected(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Send a message to a given WebSocket connection.
|
|
37
|
+
*
|
|
38
|
+
* @param socket WebSocket connection to send the message to.
|
|
39
|
+
* @param message Message to send.
|
|
40
|
+
*/
|
|
41
|
+
send(socket: WebSocket | undefined, message: string): void;
|
|
42
|
+
/**
|
|
43
|
+
* Process new WebSocket connection. The upgrade request should contain `role` query param
|
|
44
|
+
* for determining the type of the connection.
|
|
45
|
+
*
|
|
46
|
+
* @param socket Incoming WebSocket connection.
|
|
47
|
+
* @param request Upgrade request for the connection.
|
|
48
|
+
*/
|
|
49
|
+
onConnection(socket: WebSocket, request: IncomingMessage): void;
|
|
50
|
+
/**
|
|
51
|
+
* Process new WebSocket connection from Debugger UI (Remote JS Debugger).
|
|
52
|
+
* If there's already open connection, the new one gets closed automatically.
|
|
53
|
+
*
|
|
54
|
+
* @param socket Incoming debugger WebSocket connection.
|
|
55
|
+
*/
|
|
56
|
+
onDebuggerConnection(socket: WebSocket): void;
|
|
57
|
+
/**
|
|
58
|
+
* Process new WebSocket connection from React Native app (client)
|
|
59
|
+
* and close any previous connection.
|
|
60
|
+
*
|
|
61
|
+
* @param socket Incoming client WebSocket connection.
|
|
62
|
+
*/
|
|
63
|
+
onClientConnection(socket: WebSocket): void;
|
|
64
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { WebSocketServer } from "../WebSocketServer.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class for creating a WebSocket server and providing a bridge between
|
|
4
|
+
* debugger UI (Remote JS debugger) and the running React Native application.
|
|
5
|
+
*
|
|
6
|
+
* React Native application (aka client) will send and receive messages from the debugger UI
|
|
7
|
+
* which runs inside a browser.
|
|
8
|
+
*
|
|
9
|
+
* @category Development server
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export class WebSocketDebuggerServer extends WebSocketServer {
|
|
13
|
+
/**
|
|
14
|
+
* A WebSocket connection with the debugger UI.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A WebSocket connection with the client (React Native app).
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create new instance of WebSocketDebuggerServer and attach it to the given Fastify instance.
|
|
23
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
24
|
+
*
|
|
25
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
26
|
+
*/
|
|
27
|
+
constructor(fastify) {
|
|
28
|
+
super(fastify, '/debugger-proxy');
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check if debugger UI is connected to the WebSocketDebuggerServer.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
isDebuggerConnected() {
|
|
36
|
+
return Boolean(this.debuggerSocket);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Send a message to a given WebSocket connection.
|
|
40
|
+
*
|
|
41
|
+
* @param socket WebSocket connection to send the message to.
|
|
42
|
+
* @param message Message to send.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
send(socket, message) {
|
|
47
|
+
try {
|
|
48
|
+
socket === null || socket === void 0 ? void 0 : socket.send(message);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
this.fastify.log.warn({
|
|
51
|
+
msg: 'Failed to send data to socket',
|
|
52
|
+
error
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Process new WebSocket connection. The upgrade request should contain `role` query param
|
|
58
|
+
* for determining the type of the connection.
|
|
59
|
+
*
|
|
60
|
+
* @param socket Incoming WebSocket connection.
|
|
61
|
+
* @param request Upgrade request for the connection.
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
onConnection(socket, request) {
|
|
66
|
+
const {
|
|
67
|
+
url = ''
|
|
68
|
+
} = request;
|
|
69
|
+
|
|
70
|
+
if (url.indexOf('role=debugger') >= 0) {
|
|
71
|
+
this.fastify.log.info({
|
|
72
|
+
msg: 'Chrome Remote JS debugger connected'
|
|
73
|
+
});
|
|
74
|
+
this.onDebuggerConnection(socket);
|
|
75
|
+
} else if (url.indexOf('role=client') >= 0) {
|
|
76
|
+
this.fastify.log.info({
|
|
77
|
+
msg: 'React Native app connected to debugger'
|
|
78
|
+
});
|
|
79
|
+
this.onClientConnection(socket);
|
|
80
|
+
} else {
|
|
81
|
+
socket.close(1011, 'Missing role param');
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Process new WebSocket connection from Debugger UI (Remote JS Debugger).
|
|
86
|
+
* If there's already open connection, the new one gets closed automatically.
|
|
87
|
+
*
|
|
88
|
+
* @param socket Incoming debugger WebSocket connection.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
onDebuggerConnection(socket) {
|
|
93
|
+
if (this.debuggerSocket) {
|
|
94
|
+
socket.close(1011, 'Another debugger is already connected');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.debuggerSocket = socket;
|
|
99
|
+
|
|
100
|
+
const onClose = () => {
|
|
101
|
+
this.fastify.log.info({
|
|
102
|
+
msg: 'Chrome Remote JS debugger disconnected'
|
|
103
|
+
});
|
|
104
|
+
this.debuggerSocket = undefined;
|
|
105
|
+
|
|
106
|
+
if (this.clientSocket) {
|
|
107
|
+
this.clientSocket.removeAllListeners();
|
|
108
|
+
this.clientSocket.close(1011, 'Debugger was disconnected');
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
this.debuggerSocket.addEventListener('error', onClose);
|
|
113
|
+
this.debuggerSocket.addEventListener('close', onClose);
|
|
114
|
+
this.debuggerSocket.addEventListener('message', ({
|
|
115
|
+
data
|
|
116
|
+
}) => {
|
|
117
|
+
this.send(this.clientSocket, data.toString());
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Process new WebSocket connection from React Native app (client)
|
|
122
|
+
* and close any previous connection.
|
|
123
|
+
*
|
|
124
|
+
* @param socket Incoming client WebSocket connection.
|
|
125
|
+
*/
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
onClientConnection(socket) {
|
|
129
|
+
if (this.clientSocket) {
|
|
130
|
+
this.clientSocket.removeAllListeners();
|
|
131
|
+
this.clientSocket.close(1011, 'Another client is connected');
|
|
132
|
+
this.clientSocket = undefined;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const onClose = () => {
|
|
136
|
+
this.fastify.log.info({
|
|
137
|
+
msg: 'React Native app disconnected from debugger'
|
|
138
|
+
});
|
|
139
|
+
this.clientSocket = undefined;
|
|
140
|
+
this.send(this.debuggerSocket, JSON.stringify({
|
|
141
|
+
method: '$disconnected'
|
|
142
|
+
}));
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
this.clientSocket = socket;
|
|
146
|
+
this.clientSocket.addEventListener('error', onClose);
|
|
147
|
+
this.clientSocket.addEventListener('close', onClose);
|
|
148
|
+
this.clientSocket.addEventListener('message', ({
|
|
149
|
+
data
|
|
150
|
+
}) => {
|
|
151
|
+
this.send(this.debuggerSocket, data.toString());
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=WebSocketDebuggerServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WebSocketDebuggerServer.js","names":["WebSocketServer","WebSocketDebuggerServer","constructor","fastify","isDebuggerConnected","Boolean","debuggerSocket","send","socket","message","error","log","warn","msg","onConnection","request","url","indexOf","info","onDebuggerConnection","onClientConnection","close","onClose","undefined","clientSocket","removeAllListeners","addEventListener","data","toString","JSON","stringify","method"],"sources":["../../../../src/plugins/wss/servers/WebSocketDebuggerServer.ts"],"sourcesContent":["import type { IncomingMessage } from 'http';\nimport type { FastifyInstance } from 'fastify';\nimport WebSocket from 'ws';\nimport { WebSocketServer } from '../WebSocketServer';\n\n/**\n * Class for creating a WebSocket server and providing a bridge between\n * debugger UI (Remote JS debugger) and the running React Native application.\n *\n * React Native application (aka client) will send and receive messages from the debugger UI\n * which runs inside a browser.\n *\n * @category Development server\n */\nexport class WebSocketDebuggerServer extends WebSocketServer {\n /**\n * A WebSocket connection with the debugger UI.\n */\n private debuggerSocket: WebSocket | undefined;\n\n /**\n * A WebSocket connection with the client (React Native app).\n */\n private clientSocket: WebSocket | undefined;\n\n /**\n * Create new instance of WebSocketDebuggerServer 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, '/debugger-proxy');\n }\n\n /**\n * Check if debugger UI is connected to the WebSocketDebuggerServer.\n */\n isDebuggerConnected() {\n return Boolean(this.debuggerSocket);\n }\n\n /**\n * Send a message to a given WebSocket connection.\n *\n * @param socket WebSocket connection to send the message to.\n * @param message Message to send.\n */\n send(socket: WebSocket | undefined, message: string) {\n try {\n socket?.send(message);\n } catch (error) {\n this.fastify.log.warn({ msg: 'Failed to send data to socket', error });\n }\n }\n\n /**\n * Process new WebSocket connection. The upgrade request should contain `role` query param\n * for determining the type of the 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 { url = '' } = request;\n if (url.indexOf('role=debugger') >= 0) {\n this.fastify.log.info({ msg: 'Chrome Remote JS debugger connected' });\n this.onDebuggerConnection(socket);\n } else if (url.indexOf('role=client') >= 0) {\n this.fastify.log.info({ msg: 'React Native app connected to debugger' });\n this.onClientConnection(socket);\n } else {\n socket.close(1011, 'Missing role param');\n }\n }\n\n /**\n * Process new WebSocket connection from Debugger UI (Remote JS Debugger).\n * If there's already open connection, the new one gets closed automatically.\n *\n * @param socket Incoming debugger WebSocket connection.\n */\n onDebuggerConnection(socket: WebSocket) {\n if (this.debuggerSocket) {\n socket.close(1011, 'Another debugger is already connected');\n return;\n }\n this.debuggerSocket = socket;\n const onClose = () => {\n this.fastify.log.info({ msg: 'Chrome Remote JS debugger disconnected' });\n this.debuggerSocket = undefined;\n if (this.clientSocket) {\n this.clientSocket.removeAllListeners();\n this.clientSocket.close(1011, 'Debugger was disconnected');\n }\n };\n this.debuggerSocket.addEventListener('error', onClose);\n this.debuggerSocket.addEventListener('close', onClose);\n this.debuggerSocket.addEventListener('message', ({ data }) => {\n this.send(this.clientSocket, data.toString());\n });\n }\n\n /**\n * Process new WebSocket connection from React Native app (client)\n * and close any previous connection.\n *\n * @param socket Incoming client WebSocket connection.\n */\n onClientConnection(socket: WebSocket) {\n if (this.clientSocket) {\n this.clientSocket.removeAllListeners();\n this.clientSocket.close(1011, 'Another client is connected');\n this.clientSocket = undefined;\n }\n\n const onClose = () => {\n this.fastify.log.info({\n msg: 'React Native app disconnected from debugger',\n });\n this.clientSocket = undefined;\n this.send(\n this.debuggerSocket,\n JSON.stringify({ method: '$disconnected' })\n );\n };\n\n this.clientSocket = socket;\n this.clientSocket.addEventListener('error', onClose);\n this.clientSocket.addEventListener('close', onClose);\n this.clientSocket.addEventListener('message', ({ data }) => {\n this.send(this.debuggerSocket, data.toString());\n });\n }\n}\n"],"mappings":"SAGSA,e;AAET;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,uBAAN,SAAsCD,eAAtC,CAAsD;EAC3D;AACF;AACA;;EAGE;AACF;AACA;;EAGE;AACF;AACA;AACA;AACA;AACA;EACEE,WAAW,CAACC,OAAD,EAA2B;IACpC,MAAMA,OAAN,EAAe,iBAAf;EACD;EAED;AACF;AACA;;;EACEC,mBAAmB,GAAG;IACpB,OAAOC,OAAO,CAAC,KAAKC,cAAN,CAAd;EACD;EAED;AACF;AACA;AACA;AACA;AACA;;;EACEC,IAAI,CAACC,MAAD,EAAgCC,OAAhC,EAAiD;IACnD,IAAI;MACFD,MAAM,SAAN,IAAAA,MAAM,WAAN,YAAAA,MAAM,CAAED,IAAR,CAAaE,OAAb;IACD,CAFD,CAEE,OAAOC,KAAP,EAAc;MACd,KAAKP,OAAL,CAAaQ,GAAb,CAAiBC,IAAjB,CAAsB;QAAEC,GAAG,EAAE,+BAAP;QAAwCH;MAAxC,CAAtB;IACD;EACF;EAED;AACF;AACA;AACA;AACA;AACA;AACA;;;EACEI,YAAY,CAACN,MAAD,EAAoBO,OAApB,EAA8C;IACxD,MAAM;MAAEC,GAAG,GAAG;IAAR,IAAeD,OAArB;;IACA,IAAIC,GAAG,CAACC,OAAJ,CAAY,eAAZ,KAAgC,CAApC,EAAuC;MACrC,KAAKd,OAAL,CAAaQ,GAAb,CAAiBO,IAAjB,CAAsB;QAAEL,GAAG,EAAE;MAAP,CAAtB;MACA,KAAKM,oBAAL,CAA0BX,MAA1B;IACD,CAHD,MAGO,IAAIQ,GAAG,CAACC,OAAJ,CAAY,aAAZ,KAA8B,CAAlC,EAAqC;MAC1C,KAAKd,OAAL,CAAaQ,GAAb,CAAiBO,IAAjB,CAAsB;QAAEL,GAAG,EAAE;MAAP,CAAtB;MACA,KAAKO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B,CAAsB;QACpBL,GAAG,EAAE;MADe,CAAtB;MAGA,KAAKW,YAAL,GAAoBD,SAApB;MACA,KAAKhB,IAAL,CACE,KAAKD,cADP,EAEEuB,IAAI,CAACC,SAAL,CAAe;QAAEC,MAAM,EAAE;MAAV,CAAf,CAFF;IAID,CATD;;IAWA,KAAKP,YAAL,GAAoBhB,MAApB;IACA,KAAKgB,YAAL,CAAkBE,gBAAlB,CAAmC,OAAnC,EAA4CJ,OAA5C;IACA,KAAKE,YAAL,CAAkBE,gBAAlB,CAAmC,OAAnC,EAA4CJ,OAA5C;IACA,KAAKE,YAAL,CAAkBE,gBAAlB,CAAmC,SAAnC,EAA8C,CAAC;MAAEC;IAAF,CAAD,KAAc;MAC1D,KAAKpB,IAAL,CAAU,KAAKD,cAAf,EAA+BqB,IAAI,CAACC,QAAL,EAA/B;IACD,CAFD;EAGD;;AAvH0D"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import type { FastifyInstance } from 'fastify';
|
|
3
|
+
import { WebSocketServer } from '../WebSocketServer';
|
|
4
|
+
/**
|
|
5
|
+
* Class for creating a WebSocket server for communication with React Native clients.
|
|
6
|
+
* All client logs - logs from React Native application - are processed here.
|
|
7
|
+
*
|
|
8
|
+
* @category Development server
|
|
9
|
+
*/
|
|
10
|
+
export declare class WebSocketDevClientServer extends WebSocketServer {
|
|
11
|
+
private clients;
|
|
12
|
+
private nextClientId;
|
|
13
|
+
/**
|
|
14
|
+
* Create new instance of WebSocketDevClientServer and attach it to the given Fastify instance.
|
|
15
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
16
|
+
*
|
|
17
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
18
|
+
*/
|
|
19
|
+
constructor(fastify: FastifyInstance);
|
|
20
|
+
/**
|
|
21
|
+
* Process client message.
|
|
22
|
+
*
|
|
23
|
+
* @param message Stringified client message.
|
|
24
|
+
*/
|
|
25
|
+
processMessage(message: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Process new WebSocket connection from client application.
|
|
28
|
+
*
|
|
29
|
+
* @param socket Incoming client's WebSocket connection.
|
|
30
|
+
*/
|
|
31
|
+
onConnection(socket: WebSocket): void;
|
|
32
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { WebSocketServer } from "../WebSocketServer.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class for creating a WebSocket server for communication with React Native clients.
|
|
4
|
+
* All client logs - logs from React Native application - are processed here.
|
|
5
|
+
*
|
|
6
|
+
* @category Development server
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export class WebSocketDevClientServer extends WebSocketServer {
|
|
10
|
+
clients = new Map();
|
|
11
|
+
nextClientId = 0;
|
|
12
|
+
/**
|
|
13
|
+
* Create new instance of WebSocketDevClientServer and attach it to the given Fastify instance.
|
|
14
|
+
* Any logging information, will be passed through standard `fastify.log` API.
|
|
15
|
+
*
|
|
16
|
+
* @param fastify Fastify instance to attach the WebSocket server to.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
constructor(fastify) {
|
|
20
|
+
super(fastify, '/__client');
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Process client message.
|
|
24
|
+
*
|
|
25
|
+
* @param message Stringified client message.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
processMessage(message) {
|
|
30
|
+
const {
|
|
31
|
+
type,
|
|
32
|
+
...body
|
|
33
|
+
} = JSON.parse(message);
|
|
34
|
+
|
|
35
|
+
switch (type) {
|
|
36
|
+
case 'client-log':
|
|
37
|
+
if (body.level === 'error') {
|
|
38
|
+
this.fastify.log.error({
|
|
39
|
+
issuer: 'Console',
|
|
40
|
+
msg: body.data
|
|
41
|
+
});
|
|
42
|
+
} else if (body.level === 'warn') {
|
|
43
|
+
this.fastify.log.warn({
|
|
44
|
+
issuer: 'Console',
|
|
45
|
+
msg: body.data
|
|
46
|
+
});
|
|
47
|
+
} else {
|
|
48
|
+
this.fastify.log.info({
|
|
49
|
+
issuer: 'Console',
|
|
50
|
+
msg: body.data
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
break;
|
|
55
|
+
|
|
56
|
+
default:
|
|
57
|
+
this.fastify.log.warn({
|
|
58
|
+
msg: 'Unknown client message',
|
|
59
|
+
message
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Process new WebSocket connection from client application.
|
|
65
|
+
*
|
|
66
|
+
* @param socket Incoming client's WebSocket connection.
|
|
67
|
+
*/
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
onConnection(socket) {
|
|
71
|
+
const clientId = `client#${this.nextClientId++}`;
|
|
72
|
+
this.clients.set(clientId, socket);
|
|
73
|
+
this.fastify.log.info({
|
|
74
|
+
msg: 'React Native client connected',
|
|
75
|
+
clientId
|
|
76
|
+
});
|
|
77
|
+
this.clients.set(clientId, socket);
|
|
78
|
+
|
|
79
|
+
const onClose = () => {
|
|
80
|
+
this.fastify.log.info({
|
|
81
|
+
msg: 'React Native client disconnected',
|
|
82
|
+
clientId
|
|
83
|
+
});
|
|
84
|
+
this.clients.delete(clientId);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
socket.addEventListener('error', onClose);
|
|
88
|
+
socket.addEventListener('close', onClose);
|
|
89
|
+
socket.addEventListener('message', event => {
|
|
90
|
+
this.processMessage(event.data.toString());
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=WebSocketDevClientServer.js.map
|