-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: add a hard sequence in SSE and WS requests #6569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
5703571
0ce0352
ce5ebe6
aa9b3cf
094f115
411ef82
49f09aa
8af13e3
e012bf6
b3062a8
687a309
88e47d6
5498ba5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
sid-bruno marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,36 @@ const normalizeMessageByFormat = (message, format) => { | |
| } | ||
| }; | ||
|
|
||
| const createSequencer = () => { | ||
| const seq = {}; | ||
|
|
||
| const nextSeq = (requestId, collectionId) => { | ||
| seq[requestId] ||= {}; | ||
| seq[requestId][collectionId] ||= 0; | ||
| return ++seq[requestId][collectionId]; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {string} requestId | ||
| * @param {string} [collectionId] | ||
| */ | ||
| const clean = (requestId, collectionId = undefined) => { | ||
| if (collectionId) { | ||
| delete seq[requestId][collectionId]; | ||
| } | ||
| if (!Object.keys(seq[requestId]).length) { | ||
| delete seq[requestId]; | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| next: nextSeq, | ||
| clean | ||
| }; | ||
| }; | ||
|
Comment on lines
+52
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add guard checks to prevent runtime errors in clean(). The 🔎 Proposed fix const clean = (requestId, collectionId = undefined) => {
+ if (!seq[requestId]) {
+ return;
+ }
+
if (collectionId) {
delete seq[requestId][collectionId];
}
+
if (!Object.keys(seq[requestId]).length) {
delete seq[requestId];
}
};🤖 Prompt for AI Agents |
||
|
|
||
| const seq = createSequencer(); | ||
sid-bruno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| class WsClient { | ||
| messageQueues = {}; | ||
| activeConnections = new Map(); | ||
|
|
@@ -181,6 +211,7 @@ class WsClient { | |
| message: payload, | ||
| messageHexdump: hexdump(payload), | ||
| type: 'outgoing', | ||
| seq: seq.next(requestId, collectionUid), | ||
| timestamp: Date.now() | ||
| }); | ||
| } | ||
|
|
@@ -204,6 +235,7 @@ class WsClient { | |
| if (connectionMeta?.connection) { | ||
| connectionMeta.connection.close(code, reason); | ||
| this.#removeConnection(requestId); | ||
| seq.clean(requestId); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -283,7 +315,8 @@ class WsClient { | |
|
|
||
| this.eventCallback('main:ws:open', requestId, collectionUid, { | ||
| timestamp: Date.now(), | ||
| url: ws.url | ||
| url: ws.url, | ||
| seq: seq.next(requestId, collectionUid) | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -294,14 +327,16 @@ class WsClient { | |
| message: `Redirected to ${url}`, | ||
| type: 'info', | ||
| timestamp: Date.now(), | ||
| headers: headers | ||
| headers: headers, | ||
| seq: seq.next(requestId, collectionUid) | ||
| }); | ||
| }); | ||
|
|
||
| ws.on('upgrade', (response) => { | ||
| this.eventCallback('main:ws:upgrade', requestId, collectionUid, { | ||
| type: 'info', | ||
| timestamp: Date.now(), | ||
| seq: seq.next(requestId, collectionUid), | ||
| headers: { ...response.headers } | ||
| }); | ||
| }); | ||
|
|
@@ -313,6 +348,7 @@ class WsClient { | |
| message, | ||
| messageHexdump: hexdump(Buffer.from(data)), | ||
| type: 'incoming', | ||
| seq: seq.next(requestId, collectionUid), | ||
| timestamp: Date.now() | ||
| }); | ||
| } catch (error) { | ||
|
|
@@ -321,6 +357,7 @@ class WsClient { | |
| message: data.toString(), | ||
| messageHexdump: hexdump(data), | ||
| type: 'incoming', | ||
| seq: seq.next(requestId, collectionUid), | ||
| timestamp: Date.now() | ||
| }); | ||
| } | ||
|
|
@@ -330,14 +367,17 @@ class WsClient { | |
| this.eventCallback('main:ws:close', requestId, collectionUid, { | ||
| code, | ||
| reason: Buffer.from(reason).toString(), | ||
| seq: seq.next(requestId, collectionUid), | ||
| timestamp: Date.now() | ||
| }); | ||
| seq.clean(requestId, collectionUid); | ||
| this.#removeConnection(requestId); | ||
| }); | ||
|
|
||
| ws.on('error', (error) => { | ||
| this.eventCallback('main:ws:error', requestId, collectionUid, { | ||
| error: error.message, | ||
| seq: seq.next(requestId, collectionUid), | ||
| timestamp: Date.now() | ||
| }); | ||
| }); | ||
|
|
@@ -356,6 +396,7 @@ class WsClient { | |
| this.eventCallback('main:ws:connections-changed', { | ||
| type: 'added', | ||
| requestId, | ||
| seq: seq.next(requestId, collectionUid), | ||
| activeConnectionIds: this.getActiveConnectionIds() | ||
| }); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.