@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.
Files changed (86) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +32 -0
  3. package/dist/createServer.d.ts +13 -0
  4. package/dist/createServer.js +111 -0
  5. package/dist/createServer.js.map +1 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +3 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/plugins/compiler/compilerPlugin.d.ts +6 -0
  10. package/dist/plugins/compiler/compilerPlugin.js +85 -0
  11. package/dist/plugins/compiler/compilerPlugin.js.map +1 -0
  12. package/dist/plugins/compiler/index.d.ts +2 -0
  13. package/dist/plugins/compiler/index.js +3 -0
  14. package/dist/plugins/compiler/index.js.map +1 -0
  15. package/dist/plugins/compiler/types.d.ts +31 -0
  16. package/dist/plugins/compiler/types.js +2 -0
  17. package/dist/plugins/compiler/types.js.map +1 -0
  18. package/dist/plugins/devtools/devtoolsPlugin.d.ts +6 -0
  19. package/dist/plugins/devtools/devtoolsPlugin.js +109 -0
  20. package/dist/plugins/devtools/devtoolsPlugin.js.map +1 -0
  21. package/dist/plugins/devtools/index.d.ts +1 -0
  22. package/dist/plugins/devtools/index.js +2 -0
  23. package/dist/plugins/devtools/index.js.map +1 -0
  24. package/dist/plugins/multipart/index.d.ts +2 -0
  25. package/dist/plugins/multipart/index.js +3 -0
  26. package/dist/plugins/multipart/index.js.map +1 -0
  27. package/dist/plugins/multipart/multipartPlugin.d.ts +3 -0
  28. package/dist/plugins/multipart/multipartPlugin.js +68 -0
  29. package/dist/plugins/multipart/multipartPlugin.js.map +1 -0
  30. package/dist/plugins/multipart/types.d.ts +12 -0
  31. package/dist/plugins/multipart/types.js +2 -0
  32. package/dist/plugins/multipart/types.js.map +1 -0
  33. package/dist/plugins/symbolicate/Symbolicator.d.ts +46 -0
  34. package/dist/plugins/symbolicate/Symbolicator.js +227 -0
  35. package/dist/plugins/symbolicate/Symbolicator.js.map +1 -0
  36. package/dist/plugins/symbolicate/index.d.ts +3 -0
  37. package/dist/plugins/symbolicate/index.js +4 -0
  38. package/dist/plugins/symbolicate/index.js.map +1 -0
  39. package/dist/plugins/symbolicate/sybmolicatePlugin.d.ts +6 -0
  40. package/dist/plugins/symbolicate/sybmolicatePlugin.js +46 -0
  41. package/dist/plugins/symbolicate/sybmolicatePlugin.js.map +1 -0
  42. package/dist/plugins/symbolicate/types.d.ts +65 -0
  43. package/dist/plugins/symbolicate/types.js +2 -0
  44. package/dist/plugins/symbolicate/types.js.map +1 -0
  45. package/dist/plugins/wss/WebSocketRouter.d.ts +32 -0
  46. package/dist/plugins/wss/WebSocketRouter.js +61 -0
  47. package/dist/plugins/wss/WebSocketRouter.js.map +1 -0
  48. package/dist/plugins/wss/WebSocketServer.d.ts +37 -0
  49. package/dist/plugins/wss/WebSocketServer.js +49 -0
  50. package/dist/plugins/wss/WebSocketServer.js.map +1 -0
  51. package/dist/plugins/wss/index.d.ts +3 -0
  52. package/dist/plugins/wss/index.js +4 -0
  53. package/dist/plugins/wss/index.js.map +1 -0
  54. package/dist/plugins/wss/servers/HermesInspectorProxy.d.ts +24 -0
  55. package/dist/plugins/wss/servers/HermesInspectorProxy.js +138 -0
  56. package/dist/plugins/wss/servers/HermesInspectorProxy.js.map +1 -0
  57. package/dist/plugins/wss/servers/WebSocketDashboardServer.d.ts +34 -0
  58. package/dist/plugins/wss/servers/WebSocketDashboardServer.js +70 -0
  59. package/dist/plugins/wss/servers/WebSocketDashboardServer.js.map +1 -0
  60. package/dist/plugins/wss/servers/WebSocketDebuggerServer.d.ts +64 -0
  61. package/dist/plugins/wss/servers/WebSocketDebuggerServer.js +156 -0
  62. package/dist/plugins/wss/servers/WebSocketDebuggerServer.js.map +1 -0
  63. package/dist/plugins/wss/servers/WebSocketDevClientServer.d.ts +32 -0
  64. package/dist/plugins/wss/servers/WebSocketDevClientServer.js +95 -0
  65. package/dist/plugins/wss/servers/WebSocketDevClientServer.js.map +1 -0
  66. package/dist/plugins/wss/servers/WebSocketEventsServer.d.ts +75 -0
  67. package/dist/plugins/wss/servers/WebSocketEventsServer.js +187 -0
  68. package/dist/plugins/wss/servers/WebSocketEventsServer.js.map +1 -0
  69. package/dist/plugins/wss/servers/WebSocketHMRServer.d.ts +38 -0
  70. package/dist/plugins/wss/servers/WebSocketHMRServer.js +103 -0
  71. package/dist/plugins/wss/servers/WebSocketHMRServer.js.map +1 -0
  72. package/dist/plugins/wss/servers/WebSocketMessageServer.d.ts +140 -0
  73. package/dist/plugins/wss/servers/WebSocketMessageServer.js +380 -0
  74. package/dist/plugins/wss/servers/WebSocketMessageServer.js.map +1 -0
  75. package/dist/plugins/wss/types.d.ts +16 -0
  76. package/dist/plugins/wss/types.js +2 -0
  77. package/dist/plugins/wss/types.js.map +1 -0
  78. package/dist/plugins/wss/wssPlugin.d.ts +29 -0
  79. package/dist/plugins/wss/wssPlugin.js +47 -0
  80. package/dist/plugins/wss/wssPlugin.js.map +1 -0
  81. package/dist/types.d.ts +127 -0
  82. package/dist/types.js +24 -0
  83. package/dist/types.js.map +1 -0
  84. package/package.json +75 -0
  85. package/vendor/metro-inspector-proxy/src/Device.cjs +1 -0
  86. package/vendor/metro-inspector-proxy/src/Device.js +1 -0
@@ -0,0 +1,46 @@
1
+ import { SourceMapConsumer } from 'source-map';
2
+ import type { FastifyLoggerInstance } from 'fastify';
3
+ import type { ReactNativeStackFrame, SymbolicatorDelegate, SymbolicatorResults } from './types';
4
+ /**
5
+ * Class for transforming stack traces from React Native application with using Source Map.
6
+ * Raw stack frames produced by React Native, points to some location from the bundle
7
+ * eg `index.bundle?platform=ios:567:1234`. By using Source Map for that bundle `Symbolicator`
8
+ * produces frames that point to source code inside your project eg `Hello.tsx:10:9`.
9
+ */
10
+ export declare class Symbolicator {
11
+ private delegate;
12
+ /**
13
+ * Infer platform from stack frames.
14
+ * Usually at least one frame has `file` field with the bundle URL eg:
15
+ * `http://localhost:8081/index.bundle?platform=ios&...`, which can be used to infer platform.
16
+ *
17
+ * @param stack Array of stack frames.
18
+ * @returns Inferred platform or `undefined` if cannot infer.
19
+ */
20
+ static inferPlatformFromStack(stack: ReactNativeStackFrame[]): string | undefined;
21
+ /**
22
+ * Cache with initialized `SourceMapConsumer` to improve symbolication performance.
23
+ */
24
+ sourceMapConsumerCache: Record<string, SourceMapConsumer>;
25
+ /**
26
+ * Constructs new `Symbolicator` instance.
27
+ *
28
+ * @param delegate Delegate instance with symbolication functions.
29
+ */
30
+ constructor(delegate: SymbolicatorDelegate);
31
+ /**
32
+ * Process raw React Native stack frames and transform them using Source Maps.
33
+ * Method will try to symbolicate as much data as possible, but if the Source Maps
34
+ * are not available, invalid or the original positions/data is not found in Source Maps,
35
+ * the method will return raw values - the same as supplied with `stack` parameter.
36
+ * For example out of 10 frames, it's possible that only first 7 will be symbolicated and the
37
+ * remaining 3 will be unchanged.
38
+ *
39
+ * @param logger Fastify logger instance.
40
+ * @param stack Raw stack frames.
41
+ * @returns Symbolicated stack frames.
42
+ */
43
+ process(logger: FastifyLoggerInstance, stack: ReactNativeStackFrame[]): Promise<SymbolicatorResults>;
44
+ private processFrame;
45
+ private getCodeFrame;
46
+ }
@@ -0,0 +1,227 @@
1
+ import { URL } from 'url';
2
+ import { codeFrameColumns } from '@babel/code-frame';
3
+ import { SourceMapConsumer } from 'source-map';
4
+
5
+ /**
6
+ * Class for transforming stack traces from React Native application with using Source Map.
7
+ * Raw stack frames produced by React Native, points to some location from the bundle
8
+ * eg `index.bundle?platform=ios:567:1234`. By using Source Map for that bundle `Symbolicator`
9
+ * produces frames that point to source code inside your project eg `Hello.tsx:10:9`.
10
+ */
11
+ export class Symbolicator {
12
+ /**
13
+ * Infer platform from stack frames.
14
+ * Usually at least one frame has `file` field with the bundle URL eg:
15
+ * `http://localhost:8081/index.bundle?platform=ios&...`, which can be used to infer platform.
16
+ *
17
+ * @param stack Array of stack frames.
18
+ * @returns Inferred platform or `undefined` if cannot infer.
19
+ */
20
+ static inferPlatformFromStack(stack) {
21
+ for (const frame of stack) {
22
+ if (!frame.file) {
23
+ return;
24
+ }
25
+
26
+ const {
27
+ searchParams,
28
+ pathname
29
+ } = new URL(frame.file, 'file://');
30
+ const platform = searchParams.get('platform');
31
+
32
+ if (platform) {
33
+ return platform;
34
+ } else {
35
+ const [bundleFilename] = pathname.split('/').reverse();
36
+ const [, platformOrExtension, extension] = bundleFilename.split('.');
37
+
38
+ if (extension) {
39
+ return platformOrExtension;
40
+ }
41
+ }
42
+ }
43
+ }
44
+ /**
45
+ * Cache with initialized `SourceMapConsumer` to improve symbolication performance.
46
+ */
47
+
48
+
49
+ sourceMapConsumerCache = {};
50
+ /**
51
+ * Constructs new `Symbolicator` instance.
52
+ *
53
+ * @param delegate Delegate instance with symbolication functions.
54
+ */
55
+
56
+ constructor(delegate) {
57
+ this.delegate = delegate;
58
+ }
59
+ /**
60
+ * Process raw React Native stack frames and transform them using Source Maps.
61
+ * Method will try to symbolicate as much data as possible, but if the Source Maps
62
+ * are not available, invalid or the original positions/data is not found in Source Maps,
63
+ * the method will return raw values - the same as supplied with `stack` parameter.
64
+ * For example out of 10 frames, it's possible that only first 7 will be symbolicated and the
65
+ * remaining 3 will be unchanged.
66
+ *
67
+ * @param logger Fastify logger instance.
68
+ * @param stack Raw stack frames.
69
+ * @returns Symbolicated stack frames.
70
+ */
71
+
72
+
73
+ async process(logger, stack) {
74
+ logger.debug({
75
+ msg: 'Filtering out unnecessary frames'
76
+ });
77
+ const frames = [];
78
+
79
+ for (const frame of stack) {
80
+ const {
81
+ file
82
+ } = frame;
83
+
84
+ if (file !== null && file !== void 0 && file.startsWith('http') && !file.includes('debuggerWorker')) {
85
+ frames.push(frame);
86
+ }
87
+ }
88
+
89
+ try {
90
+ logger.debug({
91
+ msg: 'Processing frames',
92
+ frames
93
+ });
94
+ const processedFrames = [];
95
+
96
+ for (const frame of frames) {
97
+ if (!this.sourceMapConsumerCache[frame.file]) {
98
+ logger.debug({
99
+ msg: 'Loading raw source map data',
100
+ fileUrl: frame.file
101
+ });
102
+ const rawSourceMap = await this.delegate.getSourceMap(frame.file);
103
+ logger.debug({
104
+ msg: 'Creating source map instance',
105
+ fileUrl: frame.file,
106
+ sourceMapLength: rawSourceMap.length
107
+ });
108
+ const sourceMapConsumer = await new SourceMapConsumer(rawSourceMap.toString());
109
+ logger.debug({
110
+ msg: 'Saving source map instance into cache',
111
+ fileUrl: frame.file
112
+ });
113
+ this.sourceMapConsumerCache[frame.file] = sourceMapConsumer;
114
+ }
115
+
116
+ logger.debug({
117
+ msg: 'Symbolicating frame',
118
+ frame
119
+ });
120
+ const processedFrame = this.processFrame(frame);
121
+ logger.debug({
122
+ msg: 'Finished symbolicating frame',
123
+ frame
124
+ });
125
+ processedFrames.push(processedFrame);
126
+ }
127
+
128
+ const codeFrame = (await this.getCodeFrame(logger, processedFrames)) ?? null;
129
+ logger.debug({
130
+ msg: 'Finished symbolicating frames',
131
+ processedFrames,
132
+ codeFrame
133
+ });
134
+ return {
135
+ stack: processedFrames,
136
+ codeFrame
137
+ };
138
+ } finally {
139
+ for (const key in this.sourceMapConsumerCache) {
140
+ this.sourceMapConsumerCache[key].destroy();
141
+ delete this.sourceMapConsumerCache[key];
142
+ }
143
+ }
144
+ }
145
+
146
+ processFrame(frame) {
147
+ if (!frame.lineNumber || !frame.column) {
148
+ return { ...frame,
149
+ collapse: false
150
+ };
151
+ }
152
+
153
+ const consumer = this.sourceMapConsumerCache[frame.file];
154
+
155
+ if (!consumer) {
156
+ return { ...frame,
157
+ collapse: false
158
+ };
159
+ }
160
+
161
+ const lookup = consumer.originalPositionFor({
162
+ line: frame.lineNumber,
163
+ column: frame.column
164
+ }); // If lookup fails, we get the same shape object, but with
165
+ // all values set to null
166
+
167
+ if (!lookup.source) {
168
+ // It is better to gracefully return the original frame
169
+ // than to throw an exception
170
+ return { ...frame,
171
+ collapse: false
172
+ };
173
+ }
174
+
175
+ return {
176
+ lineNumber: lookup.line || frame.lineNumber,
177
+ column: lookup.column || frame.column,
178
+ file: lookup.source,
179
+ methodName: lookup.name || frame.methodName,
180
+ collapse: false
181
+ };
182
+ }
183
+
184
+ async getCodeFrame(logger, processedFrames) {
185
+ for (const frame of processedFrames) {
186
+ if (frame.collapse || !frame.lineNumber || !frame.column) {
187
+ continue;
188
+ }
189
+
190
+ if (!this.delegate.shouldIncludeFrame(frame)) {
191
+ return undefined;
192
+ }
193
+
194
+ logger.debug({
195
+ msg: 'Generating code frame',
196
+ frame
197
+ });
198
+
199
+ try {
200
+ return {
201
+ content: codeFrameColumns((await this.delegate.getSource(frame.file)).toString(), {
202
+ start: {
203
+ column: frame.column,
204
+ line: frame.lineNumber
205
+ }
206
+ }, {
207
+ forceColor: true
208
+ }),
209
+ location: {
210
+ row: frame.lineNumber,
211
+ column: frame.column
212
+ },
213
+ fileName: frame.file
214
+ };
215
+ } catch (error) {
216
+ logger.error({
217
+ msg: 'Failed to create code frame',
218
+ error: error.message
219
+ });
220
+ }
221
+
222
+ return undefined;
223
+ }
224
+ }
225
+
226
+ }
227
+ //# sourceMappingURL=Symbolicator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Symbolicator.js","names":["URL","codeFrameColumns","SourceMapConsumer","Symbolicator","inferPlatformFromStack","stack","frame","file","searchParams","pathname","platform","get","bundleFilename","split","reverse","platformOrExtension","extension","sourceMapConsumerCache","constructor","delegate","process","logger","debug","msg","frames","startsWith","includes","push","processedFrames","fileUrl","rawSourceMap","getSourceMap","sourceMapLength","length","sourceMapConsumer","toString","processedFrame","processFrame","codeFrame","getCodeFrame","key","destroy","lineNumber","column","collapse","consumer","lookup","originalPositionFor","line","source","methodName","name","shouldIncludeFrame","undefined","content","getSource","start","forceColor","location","row","fileName","error","message"],"sources":["../../../src/plugins/symbolicate/Symbolicator.ts"],"sourcesContent":["import { URL } from 'url';\nimport { codeFrameColumns } from '@babel/code-frame';\nimport { SourceMapConsumer } from 'source-map';\nimport type { FastifyLoggerInstance } from 'fastify';\nimport type {\n CodeFrame,\n InputStackFrame,\n ReactNativeStackFrame,\n StackFrame,\n SymbolicatorDelegate,\n SymbolicatorResults,\n} from './types';\n\n/**\n * Class for transforming stack traces from React Native application with using Source Map.\n * Raw stack frames produced by React Native, points to some location from the bundle\n * eg `index.bundle?platform=ios:567:1234`. By using Source Map for that bundle `Symbolicator`\n * produces frames that point to source code inside your project eg `Hello.tsx:10:9`.\n */\nexport class Symbolicator {\n /**\n * Infer platform from stack frames.\n * Usually at least one frame has `file` field with the bundle URL eg:\n * `http://localhost:8081/index.bundle?platform=ios&...`, which can be used to infer platform.\n *\n * @param stack Array of stack frames.\n * @returns Inferred platform or `undefined` if cannot infer.\n */\n static inferPlatformFromStack(stack: ReactNativeStackFrame[]) {\n for (const frame of stack) {\n if (!frame.file) {\n return;\n }\n\n const { searchParams, pathname } = new URL(frame.file, 'file://');\n const platform = searchParams.get('platform');\n if (platform) {\n return platform;\n } else {\n const [bundleFilename] = pathname.split('/').reverse();\n const [, platformOrExtension, extension] = bundleFilename.split('.');\n if (extension) {\n return platformOrExtension;\n }\n }\n }\n }\n\n /**\n * Cache with initialized `SourceMapConsumer` to improve symbolication performance.\n */\n sourceMapConsumerCache: Record<string, SourceMapConsumer> = {};\n\n /**\n * Constructs new `Symbolicator` instance.\n *\n * @param delegate Delegate instance with symbolication functions.\n */\n constructor(private delegate: SymbolicatorDelegate) {}\n\n /**\n * Process raw React Native stack frames and transform them using Source Maps.\n * Method will try to symbolicate as much data as possible, but if the Source Maps\n * are not available, invalid or the original positions/data is not found in Source Maps,\n * the method will return raw values - the same as supplied with `stack` parameter.\n * For example out of 10 frames, it's possible that only first 7 will be symbolicated and the\n * remaining 3 will be unchanged.\n *\n * @param logger Fastify logger instance.\n * @param stack Raw stack frames.\n * @returns Symbolicated stack frames.\n */\n async process(\n logger: FastifyLoggerInstance,\n stack: ReactNativeStackFrame[]\n ): Promise<SymbolicatorResults> {\n logger.debug({ msg: 'Filtering out unnecessary frames' });\n\n const frames: InputStackFrame[] = [];\n for (const frame of stack) {\n const { file } = frame;\n if (file?.startsWith('http') && !file.includes('debuggerWorker')) {\n frames.push(frame as InputStackFrame);\n }\n }\n\n try {\n logger.debug({ msg: 'Processing frames', frames });\n\n const processedFrames: StackFrame[] = [];\n for (const frame of frames) {\n if (!this.sourceMapConsumerCache[frame.file]) {\n logger.debug({\n msg: 'Loading raw source map data',\n fileUrl: frame.file,\n });\n\n const rawSourceMap = await this.delegate.getSourceMap(frame.file);\n\n logger.debug({\n msg: 'Creating source map instance',\n fileUrl: frame.file,\n sourceMapLength: rawSourceMap.length,\n });\n const sourceMapConsumer = await new SourceMapConsumer(\n rawSourceMap.toString()\n );\n\n logger.debug({\n msg: 'Saving source map instance into cache',\n fileUrl: frame.file,\n });\n this.sourceMapConsumerCache[frame.file] = sourceMapConsumer;\n }\n\n logger.debug({\n msg: 'Symbolicating frame',\n frame,\n });\n const processedFrame = this.processFrame(frame);\n\n logger.debug({\n msg: 'Finished symbolicating frame',\n frame,\n });\n processedFrames.push(processedFrame);\n }\n\n const codeFrame =\n (await this.getCodeFrame(logger, processedFrames)) ?? null;\n\n logger.debug({\n msg: 'Finished symbolicating frames',\n processedFrames,\n codeFrame,\n });\n\n return {\n stack: processedFrames,\n codeFrame,\n };\n } finally {\n for (const key in this.sourceMapConsumerCache) {\n this.sourceMapConsumerCache[key].destroy();\n delete this.sourceMapConsumerCache[key];\n }\n }\n }\n\n private processFrame(frame: InputStackFrame): StackFrame {\n if (!frame.lineNumber || !frame.column) {\n return {\n ...frame,\n collapse: false,\n };\n }\n\n const consumer = this.sourceMapConsumerCache[frame.file];\n if (!consumer) {\n return {\n ...frame,\n collapse: false,\n };\n }\n\n const lookup = consumer.originalPositionFor({\n line: frame.lineNumber,\n column: frame.column,\n });\n\n // If lookup fails, we get the same shape object, but with\n // all values set to null\n if (!lookup.source) {\n // It is better to gracefully return the original frame\n // than to throw an exception\n return {\n ...frame,\n collapse: false,\n };\n }\n\n return {\n lineNumber: lookup.line || frame.lineNumber,\n column: lookup.column || frame.column,\n file: lookup.source,\n methodName: lookup.name || frame.methodName,\n collapse: false,\n };\n }\n\n private async getCodeFrame(\n logger: FastifyLoggerInstance,\n processedFrames: StackFrame[]\n ): Promise<CodeFrame | undefined> {\n for (const frame of processedFrames) {\n if (frame.collapse || !frame.lineNumber || !frame.column) {\n continue;\n }\n\n if (!this.delegate.shouldIncludeFrame(frame)) {\n return undefined;\n }\n\n logger.debug({\n msg: 'Generating code frame',\n frame,\n });\n\n try {\n return {\n content: codeFrameColumns(\n (await this.delegate.getSource(frame.file)).toString(),\n {\n start: { column: frame.column, line: frame.lineNumber },\n },\n { forceColor: true }\n ),\n location: {\n row: frame.lineNumber,\n column: frame.column,\n },\n fileName: frame.file,\n };\n } catch (error) {\n logger.error({\n msg: 'Failed to create code frame',\n error: (error as Error).message,\n });\n }\n\n return undefined;\n }\n }\n}\n"],"mappings":"AAAA,SAASA,GAAT,QAAoB,KAApB;AACA,SAASC,gBAAT,QAAiC,mBAAjC;AACA,SAASC,iBAAT,QAAkC,YAAlC;;AAWA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,YAAN,CAAmB;EACxB;AACF;AACA;AACA;AACA;AACA;AACA;AACA;EAC+B,OAAtBC,sBAAsB,CAACC,KAAD,EAAiC;IAC5D,KAAK,MAAMC,KAAX,IAAoBD,KAApB,EAA2B;MACzB,IAAI,CAACC,KAAK,CAACC,IAAX,EAAiB;QACf;MACD;;MAED,MAAM;QAAEC,YAAF;QAAgBC;MAAhB,IAA6B,IAAIT,GAAJ,CAAQM,KAAK,CAACC,IAAd,EAAoB,SAApB,CAAnC;MACA,MAAMG,QAAQ,GAAGF,YAAY,CAACG,GAAb,CAAiB,UAAjB,CAAjB;;MACA,IAAID,QAAJ,EAAc;QACZ,OAAOA,QAAP;MACD,CAFD,MAEO;QACL,MAAM,CAACE,cAAD,IAAmBH,QAAQ,CAACI,KAAT,CAAe,GAAf,EAAoBC,OAApB,EA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;QAEXjB;MAFW,CAAb;;MAKA,IAAI;QACF,OAAO;UACLgD,OAAO,EAAErD,gBAAgB,CACvB,CAAC,MAAM,KAAKkB,QAAL,CAAcoC,SAAd,CAAwBjD,KAAK,CAACC,IAA9B,CAAP,EAA4C4B,QAA5C,EADuB,EAEvB;YACEqB,KAAK,EAAE;cAAEb,MAAM,EAAErC,KAAK,CAACqC,MAAhB;cAAwBK,IAAI,EAAE1C,KAAK,CAACoC;YAApC;UADT,CAFuB,EAKvB;YAAEe,UAAU,EAAE;UAAd,CALuB,CADpB;UAQLC,QAAQ,EAAE;YACRC,GAAG,EAAErD,KAAK,CAACoC,UADH;YAERC,MAAM,EAAErC,KAAK,CAACqC;UAFN,CARL;UAYLiB,QAAQ,EAAEtD,KAAK,CAACC;QAZX,CAAP;MAcD,CAfD,CAeE,OAAOsD,KAAP,EAAc;QACdxC,MAAM,CAACwC,KAAP,CAAa;UACXtC,GAAG,EAAE,6BADM;UAEXsC,KAAK,EAAGA,KAAD,CAAiBC;QAFb,CAAb;MAID;;MAED,OAAOT,SAAP;IACD;EACF;;AArNuB"}
@@ -0,0 +1,3 @@
1
+ export { default } from './sybmolicatePlugin';
2
+ export * from './Symbolicator';
3
+ export * from './types';
@@ -0,0 +1,4 @@
1
+ export { default } from "./sybmolicatePlugin.js";
2
+ export * from "./Symbolicator.js";
3
+ export * from "./types.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["default"],"sources":["../../../src/plugins/symbolicate/index.ts"],"sourcesContent":["export { default } from './sybmolicatePlugin';\nexport * from './Symbolicator';\nexport * from './types';\n"],"mappings":"SAASA,O"}
@@ -0,0 +1,6 @@
1
+ /// <reference types="node" />
2
+ import type { Server } from '../../types';
3
+ declare const _default: import("fastify").FastifyPluginAsync<{
4
+ delegate: Server.Delegate;
5
+ }, import("http").Server>;
6
+ export default _default;
@@ -0,0 +1,46 @@
1
+ import fastifyPlugin from 'fastify-plugin';
2
+ import { Symbolicator } from "./Symbolicator.js";
3
+
4
+ async function symbolicatePlugin(instance, {
5
+ delegate
6
+ }) {
7
+ const symbolicator = new Symbolicator(delegate.symbolicator);
8
+ instance.post('/symbolicate', async (request, reply) => {
9
+ // React Native sends stack as JSON but tests content-type to text/plain, so
10
+ // we cannot use JSON schema to validate the body.
11
+ try {
12
+ const {
13
+ stack
14
+ } = JSON.parse(request.body);
15
+ const platform = Symbolicator.inferPlatformFromStack(stack);
16
+
17
+ if (!platform) {
18
+ request.log.debug({
19
+ msg: 'Received stack',
20
+ stack
21
+ });
22
+ reply.badRequest('Cannot infer platform from stack trace');
23
+ } else {
24
+ request.log.debug({
25
+ msg: 'Starting symbolication',
26
+ platform,
27
+ stack
28
+ });
29
+ const results = await symbolicator.process(request.log, stack);
30
+ reply.send(results);
31
+ }
32
+ } catch (error) {
33
+ request.log.error({
34
+ msg: 'Failed to symbolicate',
35
+ error: error.message
36
+ });
37
+ reply.internalServerError();
38
+ }
39
+ });
40
+ }
41
+
42
+ export default fastifyPlugin(symbolicatePlugin, {
43
+ name: 'symbolicate-plugin',
44
+ dependencies: ['fastify-sensible']
45
+ });
46
+ //# sourceMappingURL=sybmolicatePlugin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sybmolicatePlugin.js","names":["fastifyPlugin","Symbolicator","symbolicatePlugin","instance","delegate","symbolicator","post","request","reply","stack","JSON","parse","body","platform","inferPlatformFromStack","log","debug","msg","badRequest","results","process","send","error","message","internalServerError","name","dependencies"],"sources":["../../../src/plugins/symbolicate/sybmolicatePlugin.ts"],"sourcesContent":["import type { FastifyInstance } from 'fastify';\nimport fastifyPlugin from 'fastify-plugin';\nimport type { Server } from '../../types';\nimport { Symbolicator } from './Symbolicator';\nimport type { ReactNativeStackFrame } from './types';\n\nasync function symbolicatePlugin(\n instance: FastifyInstance,\n {\n delegate,\n }: {\n delegate: Server.Delegate;\n }\n) {\n const symbolicator = new Symbolicator(delegate.symbolicator);\n\n instance.post('/symbolicate', async (request, reply) => {\n // React Native sends stack as JSON but tests content-type to text/plain, so\n // we cannot use JSON schema to validate the body.\n\n try {\n const { stack } = JSON.parse(request.body as string) as {\n stack: ReactNativeStackFrame[];\n };\n const platform = Symbolicator.inferPlatformFromStack(stack);\n if (!platform) {\n request.log.debug({ msg: 'Received stack', stack });\n reply.badRequest('Cannot infer platform from stack trace');\n } else {\n request.log.debug({ msg: 'Starting symbolication', platform, stack });\n const results = await symbolicator.process(request.log, stack);\n reply.send(results);\n }\n } catch (error) {\n request.log.error({\n msg: 'Failed to symbolicate',\n error: (error as Error).message,\n });\n reply.internalServerError();\n }\n });\n}\n\nexport default fastifyPlugin(symbolicatePlugin, {\n name: 'symbolicate-plugin',\n dependencies: ['fastify-sensible'],\n});\n"],"mappings":"AACA,OAAOA,aAAP,MAA0B,gBAA1B;SAESC,Y;;AAGT,eAAeC,iBAAf,CACEC,QADF,EAEE;EACEC;AADF,CAFF,EAOE;EACA,MAAMC,YAAY,GAAG,IAAIJ,YAAJ,CAAiBG,QAAQ,CAACC,YAA1B,CAArB;EAEAF,QAAQ,CAACG,IAAT,CAAc,cAAd,EAA8B,OAAOC,OAAP,EAAgBC,KAAhB,KAA0B;IACtD;IACA;IAEA,IAAI;MACF,MAAM;QAAEC;MAAF,IAAYC,IAAI,CAACC,KAAL,CAAWJ,OAAO,CAACK,IAAnB,CAAlB;MAGA,MAAMC,QAAQ,GAAGZ,YAAY,CAACa,sBAAb,CAAoCL,KAApC,CAAjB;;MACA,IAAI,CAACI,QAAL,EAAe;QACbN,OAAO,CAACQ,GAAR,CAAYC,KAAZ,CAAkB;UAAEC,GAAG,EAAE,gBAAP;UAAyBR;QAAzB,CAAlB;QACAD,KAAK,CAACU,UAAN,CAAiB,wCAAjB;MACD,CAHD,MAGO;QACLX,OAAO,CAACQ,GAAR,CAAYC,KAAZ,CAAkB;UAAEC,GAAG,EAAE,wBAAP;UAAiCJ,QAAjC;UAA2CJ;QAA3C,CAAlB;QACA,MAAMU,OAAO,GAAG,MAAMd,YAAY,CAACe,OAAb,CAAqBb,OAAO,CAACQ,GAA7B,EAAkCN,KAAlC,CAAtB;QACAD,KAAK,CAACa,IAAN,CAAWF,OAAX;MACD;IACF,CAbD,CAaE,OAAOG,KAAP,EAAc;MACdf,OAAO,CAACQ,GAAR,CAAYO,KAAZ,CAAkB;QAChBL,GAAG,EAAE,uBADW;QAEhBK,KAAK,EAAGA,KAAD,CAAiBC;MAFR,CAAlB;MAIAf,KAAK,CAACgB,mBAAN;IACD;EACF,CAxBD;AAyBD;;AAED,eAAexB,aAAa,CAACE,iBAAD,EAAoB;EAC9CuB,IAAI,EAAE,oBADwC;EAE9CC,YAAY,EAAE,CAAC,kBAAD;AAFgC,CAApB,CAA5B"}
@@ -0,0 +1,65 @@
1
+ /// <reference types="node" />
2
+ /**
3
+ * Raw React Native stack frame.
4
+ */
5
+ export interface ReactNativeStackFrame {
6
+ lineNumber: number | null;
7
+ column: number | null;
8
+ file: string | null;
9
+ methodName: string;
10
+ }
11
+ /**
12
+ * React Native stack frame used as input when processing by {@link Symbolicator}.
13
+ */
14
+ export interface InputStackFrame extends ReactNativeStackFrame {
15
+ file: string;
16
+ }
17
+ /**
18
+ * Final symbolicated stack frame.
19
+ */
20
+ export interface StackFrame extends InputStackFrame {
21
+ collapse: boolean;
22
+ }
23
+ /**
24
+ * Represents [@babel/core-frame](https://babeljs.io/docs/en/babel-code-frame).
25
+ */
26
+ export interface CodeFrame {
27
+ content: string;
28
+ location: {
29
+ row: number;
30
+ column: number;
31
+ };
32
+ fileName: string;
33
+ }
34
+ /**
35
+ * Represents results of running {@link process} method on {@link Symbolicator} instance.
36
+ */
37
+ export interface SymbolicatorResults {
38
+ codeFrame: CodeFrame | null;
39
+ stack: StackFrame[];
40
+ }
41
+ /**
42
+ * Delegate with implementation for symbolication functions.
43
+ */
44
+ export interface SymbolicatorDelegate {
45
+ /**
46
+ * Get source code of file in the URL.
47
+ *
48
+ * @param fileUrl A full URL pointing to a file.
49
+ */
50
+ getSource: (fileUrl: string) => Promise<string | Buffer>;
51
+ /**
52
+ * Get source map for the file in the URL.
53
+ *
54
+ * @param fileUrl A full (usually `http:`) URL pointing to a compiled file.
55
+ * The URL points to a file for which to return source map, not to the source map file itself,
56
+ * e.g: `http://localhost:8081/index.bundle?platform=ios`.
57
+ */
58
+ getSourceMap: (fileUrl: string) => Promise<string | Buffer>;
59
+ /**
60
+ * Check if given stack frame should be included in the new symbolicated stack.
61
+ *
62
+ * @param frame Stack frame to check.
63
+ */
64
+ shouldIncludeFrame: (frame: StackFrame) => boolean;
65
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +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":""}
@@ -0,0 +1,32 @@
1
+ import type { FastifyInstance } from 'fastify';
2
+ import { WebSocketServer } from './WebSocketServer';
3
+ /**
4
+ * Class for creating a WebSocket router to forward connections to the
5
+ * respective {@link WebSocketServer} as long as the connection is accepted for the upgrade by the
6
+ * server.
7
+ *
8
+ * If the connection is not accepted by any `WebSocketServer`, it will be destroyed to avoid
9
+ * creating handling connections and potentially throwing `ECONNRESET` errors.
10
+ *
11
+ * @category Development server
12
+ */
13
+ export declare class WebSocketRouter {
14
+ private fastify;
15
+ /** The list of all register WebSocket servers. */
16
+ protected servers: WebSocketServer[];
17
+ /**
18
+ * Create new instance of `WebSocketRouter` and attach it to the given Fastify instance.
19
+ * Any logging information, will be passed through standard `fastify.log` API.
20
+ *
21
+ * @param fastify Fastify instance to attach the WebSocket router to.
22
+ */
23
+ constructor(fastify: FastifyInstance);
24
+ /**
25
+ * Register a new {@link WebSocketServer}. New connection will now
26
+ * check if the given server will accept them and forward them.
27
+ *
28
+ * @param server WebSocket server to register.
29
+ * @returns The same instance of the WebSocket server after it's been registered.
30
+ */
31
+ registerServer<T extends WebSocketServer>(server: T): T;
32
+ }
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Class for creating a WebSocket router to forward connections to the
3
+ * respective {@link WebSocketServer} as long as the connection is accepted for the upgrade by the
4
+ * server.
5
+ *
6
+ * If the connection is not accepted by any `WebSocketServer`, it will be destroyed to avoid
7
+ * creating handling connections and potentially throwing `ECONNRESET` errors.
8
+ *
9
+ * @category Development server
10
+ */
11
+ export class WebSocketRouter {
12
+ /** The list of all register WebSocket servers. */
13
+ servers = [];
14
+ /**
15
+ * Create new instance of `WebSocketRouter` 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 router to.
19
+ */
20
+
21
+ constructor(fastify) {
22
+ this.fastify = fastify;
23
+ this.fastify.server.on('upgrade', (request, socket, head) => {
24
+ const {
25
+ pathname
26
+ } = new URL(request.url || '', 'http://localhost');
27
+ let matched = false;
28
+
29
+ for (const server of this.servers) {
30
+ if (server.shouldUpgrade(pathname)) {
31
+ matched = true;
32
+ server.upgrade(request, socket, head);
33
+ break;
34
+ }
35
+ }
36
+
37
+ if (!matched) {
38
+ this.fastify.log.debug({
39
+ msg: 'Destroying socket connection as no server was matched',
40
+ pathname
41
+ });
42
+ socket.destroy();
43
+ }
44
+ });
45
+ }
46
+ /**
47
+ * Register a new {@link WebSocketServer}. New connection will now
48
+ * check if the given server will accept them and forward them.
49
+ *
50
+ * @param server WebSocket server to register.
51
+ * @returns The same instance of the WebSocket server after it's been registered.
52
+ */
53
+
54
+
55
+ registerServer(server) {
56
+ this.servers.push(server);
57
+ return server;
58
+ }
59
+
60
+ }
61
+ //# sourceMappingURL=WebSocketRouter.js.map
@@ -0,0 +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 'http';\nimport type { Socket } from 'net';\nimport type { FastifyInstance } from 'fastify';\nimport { WebSocketServer } from './WebSocketServer';\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: WebSocketServer[] = [];\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 WebSocketServer>(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,eAAN,CAAsB;EAC3B;EACUC,OAAO,GAAsB,EAAtB;EAEjB;AACF;AACA;AACA;AACA;AACA;;EACEC,WAAW,CAASC,OAAT,EAAmC;IAAA,KAA1BA,OAA0B,GAA1BA,OAA0B;IAC5C,KAAKA,OAAL,CAAaC,MAAb,CAAoBC,EAApB,CACE,SADF,EAEE,CAACC,OAAD,EAA2BC,MAA3B,EAA2CC,IAA3C,KAA4D;MAC1D,MAAM;QAAEC;MAAF,IAAe,IAAIC,GAAJ,CAAQJ,OAAO,CAACK,GAAR,IAAe,EAAvB,EAA2B,kBAA3B,CAArB;MACA,IAAIC,OAAO,GAAG,KAAd;;MACA,KAAK,MAAMR,MAAX,IAAqB,KAAKH,OAA1B,EAAmC;QACjC,IAAIG,MAAM,CAACS,aAAP,CAAqBJ,QAArB,CAAJ,EAAoC;UAClCG,OAAO,GAAG,IAAV;UACAR,MAAM,CAACU,OAAP,CAAeR,OAAf,EAAwBC,MAAxB,EAAgCC,IAAhC;UACA;QACD;MACF;;MAED,IAAI,CAACI,OAAL,EAAc;QACZ,KAAKT,OAAL,CAAaY,GAAb,CAAiBC,KAAjB,CAAuB;UACrBC,GAAG,EAAE,uDADgB;UAErBR;QAFqB,CAAvB;QAIAF,MAAM,CAACW,OAAP;MACD;IACF,CApBH;EAsBD;EAED;AACF;AACA;AACA;AACA;AACA;AACA;;;EACEC,cAAc,CAA4Bf,MAA5B,EAA0C;IACtD,KAAKH,OAAL,CAAamB,IAAb,CAAkBhB,MAAlB;IACA,OAAOA,MAAP;EACD;;AA7C0B"}
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" />
2
+ /// <reference types="node" />
3
+ /// <reference types="node" />
4
+ import type { IncomingMessage } from 'http';
5
+ import type { Socket } from 'net';
6
+ import type { FastifyInstance } from 'fastify';
7
+ import { ServerOptions, WebSocket, WebSocketServer as WebSocketServerImpl } from 'ws';
8
+ /**
9
+ * Abstract class for providing common logic (eg routing) for all WebSocket servers.
10
+ *
11
+ * @category Development server
12
+ */
13
+ export declare abstract class WebSocketServer {
14
+ /** An instance of the underlying WebSocket server. */
15
+ readonly server: WebSocketServerImpl;
16
+ /** Fastify instance from which {@link server} will receive upgrade connections. */
17
+ protected fastify: FastifyInstance;
18
+ readonly paths: string[];
19
+ /**
20
+ * Create a new instance of the WebSocketServer.
21
+ * Any logging information, will be passed through standard `fastify.log` API.
22
+ *
23
+ * @param fastify Fastify instance to which the WebSocket will be attached to.
24
+ * @param path Path on which this WebSocketServer will be accepting connections.
25
+ * @param wssOptions WebSocket Server options.
26
+ */
27
+ constructor(fastify: FastifyInstance, path: string | string[], wssOptions?: Omit<ServerOptions, 'noServer' | 'server' | 'host' | 'port' | 'path'>);
28
+ shouldUpgrade(pathname: string): boolean;
29
+ upgrade(request: IncomingMessage, socket: Socket, head: Buffer): void;
30
+ /**
31
+ * Process incoming WebSocket connection.
32
+ *
33
+ * @param socket Incoming WebSocket connection.
34
+ * @param request Upgrade request for the connection.
35
+ */
36
+ abstract onConnection(socket: WebSocket, request: IncomingMessage): void;
37
+ }
@@ -0,0 +1,49 @@
1
+ import { WebSocketServer as WebSocketServerImpl } from 'ws';
2
+ /**
3
+ * Abstract class for providing common logic (eg routing) for all WebSocket servers.
4
+ *
5
+ * @category Development server
6
+ */
7
+
8
+ export class WebSocketServer {
9
+ /** An instance of the underlying WebSocket server. */
10
+
11
+ /** Fastify instance from which {@link server} will receive upgrade connections. */
12
+
13
+ /**
14
+ * Create a new instance of the WebSocketServer.
15
+ * Any logging information, will be passed through standard `fastify.log` API.
16
+ *
17
+ * @param fastify Fastify instance to which the WebSocket will be attached to.
18
+ * @param path Path on which this WebSocketServer will be accepting connections.
19
+ * @param wssOptions WebSocket Server options.
20
+ */
21
+ constructor(fastify, path, wssOptions = {}) {
22
+ this.fastify = fastify;
23
+ this.server = new WebSocketServerImpl({
24
+ noServer: true,
25
+ ...wssOptions
26
+ });
27
+ this.server.on('connection', this.onConnection.bind(this));
28
+ this.paths = Array.isArray(path) ? path : [path];
29
+ }
30
+
31
+ shouldUpgrade(pathname) {
32
+ return this.paths.includes(pathname);
33
+ }
34
+
35
+ upgrade(request, socket, head) {
36
+ this.server.handleUpgrade(request, socket, head, webSocket => {
37
+ this.server.emit('connection', webSocket, request);
38
+ });
39
+ }
40
+ /**
41
+ * Process incoming WebSocket connection.
42
+ *
43
+ * @param socket Incoming WebSocket connection.
44
+ * @param request Upgrade request for the connection.
45
+ */
46
+
47
+
48
+ }
49
+ //# sourceMappingURL=WebSocketServer.js.map
@@ -0,0 +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';\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 {\n /** An instance of the underlying WebSocket server. */\n public readonly server: WebSocketServerImpl;\n\n /** Fastify instance from which {@link server} will receive upgrade connections. */\n protected fastify: FastifyInstance;\n\n public readonly 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({\n noServer: true,\n ...wssOptions,\n });\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,mBAHrB,QAIO,IAJP;AAMA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAeD,eAAf,CAA+B;EACpC;;EAGA;;EAKA;AACF;AACA;AACA;AACA;AACA;AACA;AACA;EACEE,WAAW,CACTC,OADS,EAETC,IAFS,EAGTC,UAGC,GAAG,EANK,EAOT;IACA,KAAKF,OAAL,GAAeA,OAAf;IACA,KAAKG,MAAL,GAAc,IAAIL,mBAAJ,CAAwB;MACpCM,QAAQ,EAAE,IAD0B;MAEpC,GAAGF;IAFiC,CAAxB,CAAd;IAIA,KAAKC,MAAL,CAAYE,EAAZ,CAAe,YAAf,EAA6B,KAAKC,YAAL,CAAkBC,IAAlB,CAAuB,IAAvB,CAA7B;IACA,KAAKC,KAAL,GAAaC,KAAK,CAACC,OAAN,CAAcT,IAAd,IAAsBA,IAAtB,GAA6B,CAACA,IAAD,CAA1C;EACD;;EAEDU,aAAa,CAACC,QAAD,EAAmB;IAC9B,OAAO,KAAKJ,KAAL,CAAWK,QAAX,CAAoBD,QAApB,CAAP;EACD;;EAEDE,OAAO,CAACC,OAAD,EAA2BC,MAA3B,EAA2CC,IAA3C,EAAyD;IAC9D,KAAKd,MAAL,CAAYe,aAAZ,CAA0BH,OAA1B,EAAmCC,MAAnC,EAA2CC,IAA3C,EAAkDE,SAAD,IAAe;MAC9D,KAAKhB,MAAL,CAAYiB,IAAZ,CAAiB,YAAjB,EAA+BD,SAA/B,EAA0CJ,OAA1C;IACD,CAFD;EAGD;EAED;AACF;AACA;AACA;AACA;AACA;;;AAjDsC"}
@@ -0,0 +1,3 @@
1
+ export { default } from './wssPlugin';
2
+ export * from './WebSocketServer';
3
+ export * from './types';