diff options
Diffstat (limited to 'subprojects/frontend/src/xtext/webSocketMachine.ts')
-rw-r--r-- | subprojects/frontend/src/xtext/webSocketMachine.ts | 36 |
1 files changed, 1 insertions, 35 deletions
diff --git a/subprojects/frontend/src/xtext/webSocketMachine.ts b/subprojects/frontend/src/xtext/webSocketMachine.ts index b5b40d11..5f6bc604 100644 --- a/subprojects/frontend/src/xtext/webSocketMachine.ts +++ b/subprojects/frontend/src/xtext/webSocketMachine.ts | |||
@@ -6,12 +6,10 @@ const { raise } = actions; | |||
6 | const ERROR_WAIT_TIMES = ['200', '1s', '5s', '30s'].map(ms); | 6 | const ERROR_WAIT_TIMES = ['200', '1s', '5s', '30s'].map(ms); |
7 | 7 | ||
8 | export interface WebSocketContext { | 8 | export interface WebSocketContext { |
9 | webSocketURL: string | undefined; | ||
10 | errors: string[]; | 9 | errors: string[]; |
11 | } | 10 | } |
12 | 11 | ||
13 | export type WebSocketEvent = | 12 | export type WebSocketEvent = |
14 | | { type: 'CONFIGURE'; webSocketURL: string } | ||
15 | | { type: 'CONNECT' } | 13 | | { type: 'CONNECT' } |
16 | | { type: 'DISCONNECT' } | 14 | | { type: 'DISCONNECT' } |
17 | | { type: 'OPENED' } | 15 | | { type: 'OPENED' } |
@@ -25,24 +23,6 @@ export type WebSocketEvent = | |||
25 | | { type: 'OFFLINE' } | 23 | | { type: 'OFFLINE' } |
26 | | { type: 'ERROR'; message: string }; | 24 | | { type: 'ERROR'; message: string }; |
27 | 25 | ||
28 | export function isWebSocketURLLocal(webSocketURL: string | undefined): boolean { | ||
29 | if (webSocketURL === undefined) { | ||
30 | return false; | ||
31 | } | ||
32 | let hostname: string; | ||
33 | try { | ||
34 | ({ hostname } = new URL(webSocketURL)); | ||
35 | } catch { | ||
36 | return false; | ||
37 | } | ||
38 | // https://stackoverflow.com/a/57949518 | ||
39 | return ( | ||
40 | hostname === 'localhost' || | ||
41 | hostname === '[::1]' || | ||
42 | hostname.match(/^127(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$/) !== null | ||
43 | ); | ||
44 | } | ||
45 | |||
46 | export default createMachine( | 26 | export default createMachine( |
47 | { | 27 | { |
48 | id: 'webSocket', | 28 | id: 'webSocket', |
@@ -53,7 +33,6 @@ export default createMachine( | |||
53 | }, | 33 | }, |
54 | tsTypes: {} as import('./webSocketMachine.typegen').Typegen0, | 34 | tsTypes: {} as import('./webSocketMachine.typegen').Typegen0, |
55 | context: { | 35 | context: { |
56 | webSocketURL: undefined, | ||
57 | errors: [], | 36 | errors: [], |
58 | }, | 37 | }, |
59 | type: 'parallel', | 38 | type: 'parallel', |
@@ -65,16 +44,12 @@ export default createMachine( | |||
65 | disconnected: { | 44 | disconnected: { |
66 | id: 'disconnected', | 45 | id: 'disconnected', |
67 | entry: ['clearErrors', 'notifyDisconnect'], | 46 | entry: ['clearErrors', 'notifyDisconnect'], |
68 | on: { | ||
69 | CONFIGURE: { actions: 'configure' }, | ||
70 | }, | ||
71 | }, | 47 | }, |
72 | timedOut: { | 48 | timedOut: { |
73 | id: 'timedOut', | 49 | id: 'timedOut', |
74 | always: [ | 50 | always: [ |
75 | { | 51 | { |
76 | target: 'temporarilyOffline', | 52 | target: 'temporarilyOffline', |
77 | cond: 'needsNetwork', | ||
78 | in: '#offline', | 53 | in: '#offline', |
79 | }, | 54 | }, |
80 | { target: 'socketCreated', in: '#tabVisible' }, | 55 | { target: 'socketCreated', in: '#tabVisible' }, |
@@ -89,7 +64,6 @@ export default createMachine( | |||
89 | always: [ | 64 | always: [ |
90 | { | 65 | { |
91 | target: 'temporarilyOffline', | 66 | target: 'temporarilyOffline', |
92 | cond: 'needsNetwork', | ||
93 | in: '#offline', | 67 | in: '#offline', |
94 | }, | 68 | }, |
95 | ], | 69 | ], |
@@ -183,7 +157,7 @@ export default createMachine( | |||
183 | }, | 157 | }, |
184 | }, | 158 | }, |
185 | on: { | 159 | on: { |
186 | CONNECT: { target: '.timedOut', cond: 'hasWebSocketURL' }, | 160 | CONNECT: '.timedOut', |
187 | DISCONNECT: '.disconnected', | 161 | DISCONNECT: '.disconnected', |
188 | }, | 162 | }, |
189 | }, | 163 | }, |
@@ -224,10 +198,6 @@ export default createMachine( | |||
224 | }, | 198 | }, |
225 | }, | 199 | }, |
226 | { | 200 | { |
227 | guards: { | ||
228 | hasWebSocketURL: ({ webSocketURL }) => webSocketURL !== undefined, | ||
229 | needsNetwork: ({ webSocketURL }) => !isWebSocketURLLocal(webSocketURL), | ||
230 | }, | ||
231 | delays: { | 201 | delays: { |
232 | IDLE_TIMEOUT: ms('5m'), | 202 | IDLE_TIMEOUT: ms('5m'), |
233 | OPEN_TIMEOUT: ms('10s'), | 203 | OPEN_TIMEOUT: ms('10s'), |
@@ -239,10 +209,6 @@ export default createMachine( | |||
239 | }, | 209 | }, |
240 | }, | 210 | }, |
241 | actions: { | 211 | actions: { |
242 | configure: assign((context, { webSocketURL }) => ({ | ||
243 | ...context, | ||
244 | webSocketURL, | ||
245 | })), | ||
246 | pushError: assign((context, { message }) => ({ | 212 | pushError: assign((context, { message }) => ({ |
247 | ...context, | 213 | ...context, |
248 | errors: [...context.errors, message], | 214 | errors: [...context.errors, message], |