@hydranium/client-theia 1.0.0-next.38 → 1.0.0-next.381
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/README.md +53 -81
- package/lib/browser/browser-capture.js.map +1 -1
- package/lib/browser/channel-logger.d.ts +4 -3
- package/lib/browser/channel-logger.d.ts.map +1 -1
- package/lib/browser/channel-logger.js +11 -19
- package/lib/browser/channel-logger.js.map +1 -1
- package/lib/browser/connection-diagnostics-contribution.d.ts +73 -0
- package/lib/browser/connection-diagnostics-contribution.d.ts.map +1 -0
- package/lib/browser/connection-diagnostics-contribution.js +162 -0
- package/lib/browser/connection-diagnostics-contribution.js.map +1 -0
- package/lib/browser/connection-reporter.d.ts +57 -0
- package/lib/browser/connection-reporter.d.ts.map +1 -0
- package/lib/browser/connection-reporter.js +104 -0
- package/lib/browser/connection-reporter.js.map +1 -0
- package/lib/browser/editor-disk-sync.d.ts +142 -0
- package/lib/browser/editor-disk-sync.d.ts.map +1 -0
- package/lib/browser/editor-disk-sync.js +250 -0
- package/lib/browser/editor-disk-sync.js.map +1 -0
- package/lib/browser/hydranium-file-service.d.ts +44 -0
- package/lib/browser/hydranium-file-service.d.ts.map +1 -0
- package/lib/browser/hydranium-file-service.js +61 -0
- package/lib/browser/hydranium-file-service.js.map +1 -0
- package/lib/browser/index.d.ts +5 -0
- package/lib/browser/index.d.ts.map +1 -1
- package/lib/browser/index.js +5 -0
- package/lib/browser/index.js.map +1 -1
- package/lib/browser/log-level-preference.d.ts.map +1 -1
- package/lib/browser/log-level-preference.js +11 -0
- package/lib/browser/log-level-preference.js.map +1 -1
- package/lib/browser/memory-diagnostics-contribution.d.ts +1 -1
- package/lib/browser/memory-diagnostics-contribution.d.ts.map +1 -1
- package/lib/browser/memory-diagnostics-contribution.js +1 -1
- package/lib/browser/memory-diagnostics-contribution.js.map +1 -1
- package/lib/browser/session-aware-connection-source.d.ts +104 -0
- package/lib/browser/session-aware-connection-source.d.ts.map +1 -0
- package/lib/browser/session-aware-connection-source.js +193 -0
- package/lib/browser/session-aware-connection-source.js.map +1 -0
- package/lib/common/clock.d.ts +19 -0
- package/lib/common/clock.d.ts.map +1 -0
- package/lib/common/clock.js +13 -0
- package/lib/common/clock.js.map +1 -0
- package/lib/common/connection-resilience-options.d.ts +30 -0
- package/lib/common/connection-resilience-options.d.ts.map +1 -0
- package/lib/common/connection-resilience-options.js +11 -0
- package/lib/common/connection-resilience-options.js.map +1 -0
- package/lib/common/framed-socket-write-buffer.d.ts +173 -0
- package/lib/common/framed-socket-write-buffer.d.ts.map +1 -0
- package/lib/common/framed-socket-write-buffer.js +293 -0
- package/lib/common/framed-socket-write-buffer.js.map +1 -0
- package/lib/common/inbound-message-sequence.d.ts +56 -0
- package/lib/common/inbound-message-sequence.d.ts.map +1 -0
- package/lib/common/inbound-message-sequence.js +98 -0
- package/lib/common/inbound-message-sequence.js.map +1 -0
- package/lib/common/index.d.ts +13 -0
- package/lib/common/index.d.ts.map +1 -0
- package/lib/common/index.js +32 -0
- package/lib/common/index.js.map +1 -0
- package/lib/node/abstract-socket-forwarding-connection-handler.d.ts +16 -12
- package/lib/node/abstract-socket-forwarding-connection-handler.d.ts.map +1 -1
- package/lib/node/abstract-socket-forwarding-connection-handler.js +94 -31
- package/lib/node/abstract-socket-forwarding-connection-handler.js.map +1 -1
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +2 -0
- package/lib/node/index.js.map +1 -1
- package/lib/node/session-bound-frontend-connection-service.d.ts +122 -0
- package/lib/node/session-bound-frontend-connection-service.d.ts.map +1 -0
- package/lib/node/session-bound-frontend-connection-service.js +236 -0
- package/lib/node/session-bound-frontend-connection-service.js.map +1 -0
- package/lib/node/socket-channel-forwarder.d.ts +54 -0
- package/lib/node/socket-channel-forwarder.d.ts.map +1 -0
- package/lib/node/socket-channel-forwarder.js +95 -0
- package/lib/node/socket-channel-forwarder.js.map +1 -0
- package/lib/testing/stub-inversify-context.js.map +1 -1
- package/lib/testing/stub-output-channel.js.map +1 -1
- package/package.json +33 -34
- package/src/browser/browser-capture.ts +1 -1
- package/src/browser/channel-logger.ts +11 -19
- package/src/browser/connection-diagnostics-contribution.ts +146 -0
- package/src/browser/connection-reporter.ts +121 -0
- package/src/browser/editor-disk-sync.ts +253 -0
- package/src/browser/hydranium-file-service.ts +56 -0
- package/src/browser/index.ts +5 -0
- package/src/browser/log-level-preference.ts +11 -0
- package/src/browser/memory-diagnostics-contribution.ts +4 -3
- package/src/browser/session-aware-connection-source.ts +205 -0
- package/src/common/clock.ts +20 -0
- package/src/common/connection-resilience-options.ts +30 -0
- package/src/common/framed-socket-write-buffer.ts +345 -0
- package/src/common/inbound-message-sequence.ts +102 -0
- package/src/common/index.ts +16 -0
- package/src/node/abstract-socket-forwarding-connection-handler.ts +85 -36
- package/src/node/index.ts +2 -0
- package/src/node/session-bound-frontend-connection-service.ts +264 -0
- package/src/node/socket-channel-forwarder.ts +101 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
import { Emitter, type Event } from '@theia/core/lib/common/event';
|
|
10
|
+
import { SocketWriteBuffer } from '@theia/core/lib/common/messaging/socket-write-buffer';
|
|
11
|
+
import { type ConnectionResilienceOptions } from './connection-resilience-options';
|
|
12
|
+
/**
|
|
13
|
+
* Prefix on every connection log line, deliberately shared by the browser and the
|
|
14
|
+
* server so one pattern greps a console and a pod log at once.
|
|
15
|
+
*/
|
|
16
|
+
export declare const CONNECTION_LOG_PREFIX = "[connection]";
|
|
17
|
+
/**
|
|
18
|
+
* The part of a socket this buffer uses. Narrower than the socket type Theia
|
|
19
|
+
* declares, which keeps the dependency honest and lets the class be tested
|
|
20
|
+
* without a connection.
|
|
21
|
+
*/
|
|
22
|
+
export interface MessageSink {
|
|
23
|
+
/** `sequence` travels as a second socket.io argument, which a reader that does not expect it ignores. */
|
|
24
|
+
send(data: Uint8Array, sequence: number): void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Reported when the buffer runs out of room. The message that did not fit is
|
|
28
|
+
* rejected, and so is every further one for as long as the buffer stays full.
|
|
29
|
+
* Reconnecting drains it and sending resumes, but whatever was rejected
|
|
30
|
+
* meanwhile is gone, so the two sides no longer agree about the session.
|
|
31
|
+
*/
|
|
32
|
+
export interface ConnectionBufferOverflow {
|
|
33
|
+
/** Messages held when the limit was reached. */
|
|
34
|
+
readonly messages: number;
|
|
35
|
+
/** Bytes held when the limit was reached. */
|
|
36
|
+
readonly bytes: number;
|
|
37
|
+
/** The limit in force, so a reader can tell what to raise it to. */
|
|
38
|
+
readonly maxBytes: number;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Holds messages while the peer is away and re-sends them when it returns.
|
|
42
|
+
*
|
|
43
|
+
* Theia copies them into one growing byte array and flushes that with a single
|
|
44
|
+
* `send`. A websocket delivers whole messages rather than a byte stream, so
|
|
45
|
+
* Theia's encoding adds no length prefix and every reader assumes one delivery
|
|
46
|
+
* is one message. The concatenated flush breaks that assumption: the reader
|
|
47
|
+
* decodes the first message and throws away the bytes behind it.
|
|
48
|
+
*
|
|
49
|
+
* Changed here: one entry per message, sent one at a time, so each arrives as
|
|
50
|
+
* its own delivery. The buffer also decides whether a message may go out
|
|
51
|
+
* directly — see {@link sendOrQueue} — because a caller that decides for itself
|
|
52
|
+
* is how messages end up overtaking a backlog.
|
|
53
|
+
*
|
|
54
|
+
* Unchanged from Theia: exceeding the limit rejects the message rather than
|
|
55
|
+
* dropping something silently. The limit is settable, since the right value
|
|
56
|
+
* depends on how long an outage has to survive.
|
|
57
|
+
*
|
|
58
|
+
* Added here: every message sent is numbered and kept until the peer
|
|
59
|
+
* acknowledges it, and {@link flush} resends what is still unacknowledged ahead
|
|
60
|
+
* of the backlog. A socket whose far end died unnoticed still accepts writes, so
|
|
61
|
+
* a message is not delivered just because it was sent.
|
|
62
|
+
*
|
|
63
|
+
* Logging goes to `console` rather than through a `Logger`. On the browser side
|
|
64
|
+
* Theia's preloader builds this before any Output channel exists, so there is
|
|
65
|
+
* nothing else to write to; the {@link CONNECTION_LOG_PREFIX} keeps the lines
|
|
66
|
+
* findable on both sides.
|
|
67
|
+
*/
|
|
68
|
+
export declare class FramedSocketWriteBuffer extends SocketWriteBuffer {
|
|
69
|
+
/** Theia's fixed limit, kept as the default. */
|
|
70
|
+
static readonly DEFAULT_MAX_BYTES: number;
|
|
71
|
+
/**
|
|
72
|
+
* Settable so a deployment can trade memory for longer outages.
|
|
73
|
+
*
|
|
74
|
+
* Consulted directly rather than through the base class's `maxBufferSize`,
|
|
75
|
+
* which does not exist before Theia 1.71 — an `override` of it would not
|
|
76
|
+
* compile against the oldest release this package supports. Nothing is lost:
|
|
77
|
+
* `buffer`, `flush` and `drain` are all replaced here, so the base's own
|
|
78
|
+
* limit is never reached.
|
|
79
|
+
*/
|
|
80
|
+
maxBytes: number;
|
|
81
|
+
/** Kept copies past this are discarded, so a peer that never acknowledges cannot grow them without bound. */
|
|
82
|
+
static readonly DEFAULT_MAX_UNACKNOWLEDGED_BYTES: number;
|
|
83
|
+
/**
|
|
84
|
+
* Separate from {@link maxBytes}, which counts only what is produced while
|
|
85
|
+
* the peer is away: copies kept on a healthy connection must not use up the
|
|
86
|
+
* room an outage needs.
|
|
87
|
+
*/
|
|
88
|
+
maxUnacknowledgedBytes: number;
|
|
89
|
+
protected pending: Uint8Array[];
|
|
90
|
+
protected pendingBytes: number;
|
|
91
|
+
protected overflowReported: boolean;
|
|
92
|
+
protected readonly onOverflowEmitter: Emitter<ConnectionBufferOverflow>;
|
|
93
|
+
protected nextSequence: number;
|
|
94
|
+
/** Oldest first: {@link acknowledge} stops at the first entry the peer has not accepted. */
|
|
95
|
+
protected unacknowledged: {
|
|
96
|
+
readonly sequence: number;
|
|
97
|
+
readonly data: Uint8Array;
|
|
98
|
+
}[];
|
|
99
|
+
protected unacknowledgedBytes: number;
|
|
100
|
+
protected unacknowledgedOverflowReported: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Whether the peer drops a message it already has. One without the hardening
|
|
103
|
+
* applies a resent message a second time, so nothing is resent until the
|
|
104
|
+
* peer has shown otherwise.
|
|
105
|
+
*/
|
|
106
|
+
protected peerDeduplicates: boolean;
|
|
107
|
+
/** Fires once per outage when the limit is reached. */
|
|
108
|
+
get onOverflow(): Event<ConnectionBufferOverflow>;
|
|
109
|
+
/**
|
|
110
|
+
* Sends `data` now only if nothing is waiting, and queues it otherwise.
|
|
111
|
+
*
|
|
112
|
+
* Callers say whether the peer is ready for a message; the buffer decides
|
|
113
|
+
* whether it may go ahead of one already waiting. Splitting it this way is
|
|
114
|
+
* the point: a caller that answers both questions from one flag is how a
|
|
115
|
+
* fresh message ends up overtaking a backlog.
|
|
116
|
+
*/
|
|
117
|
+
sendOrQueue(socket: MessageSink | undefined, data: Uint8Array): void;
|
|
118
|
+
buffer(data: Uint8Array): void;
|
|
119
|
+
/**
|
|
120
|
+
* Kept only once `send` returns: a message whose send threw stays in the
|
|
121
|
+
* backlog, and keeping it as well would send it again under a second number,
|
|
122
|
+
* which the receiver cannot recognise as a duplicate.
|
|
123
|
+
*/
|
|
124
|
+
protected send(socket: MessageSink, data: Uint8Array): void;
|
|
125
|
+
protected keep(sequence: number, data: Uint8Array): void;
|
|
126
|
+
/**
|
|
127
|
+
* Records that the peer numbers its messages, which only a peer that also
|
|
128
|
+
* drops duplicates does, so resending to it is safe.
|
|
129
|
+
*/
|
|
130
|
+
markPeerDeduplicates(): void;
|
|
131
|
+
/** Releases every kept message numbered up to `sequence`, which the peer has accepted. */
|
|
132
|
+
acknowledge(sequence: number): void;
|
|
133
|
+
/**
|
|
134
|
+
* Resends what the peer has not acknowledged, under the same numbers, then
|
|
135
|
+
* sends the backlog. Some of the resent messages may have arrived already;
|
|
136
|
+
* the receiver recognises them by number and drops them.
|
|
137
|
+
*
|
|
138
|
+
* Resends only to a peer known to drop duplicates, and otherwise discards the
|
|
139
|
+
* copies, leaving any message lost with the old socket lost.
|
|
140
|
+
*/
|
|
141
|
+
flush(socket: MessageSink): void;
|
|
142
|
+
drain(): void;
|
|
143
|
+
/** Whether messages are still waiting to go out. */
|
|
144
|
+
get hasBacklog(): boolean;
|
|
145
|
+
protected reportOverflow(): void;
|
|
146
|
+
/** Also discards the kept copies: a drain ends the session, and resending them would replay it into the next one. */
|
|
147
|
+
protected reset(): void;
|
|
148
|
+
}
|
|
149
|
+
/** Named in the warning a skipped install emits, so the reader is told what to upgrade to. */
|
|
150
|
+
export declare const REQUIRED_THEIA_HINT = "@theia/core 1.71 or newer";
|
|
151
|
+
/**
|
|
152
|
+
* Whether this Theia exposes the seam the reconnect hardening replaces.
|
|
153
|
+
*
|
|
154
|
+
* Theia only began binding {@link SocketWriteBuffer} in 1.71; before that each
|
|
155
|
+
* connection built one privately, so there is no binding to rebind and no way
|
|
156
|
+
* in. Every other API involved has been there throughout, which makes this one
|
|
157
|
+
* probe the whole compatibility question — and it is a binding check rather
|
|
158
|
+
* than a version parse, so it answers about the container actually in front of
|
|
159
|
+
* us instead of a number that may have been overridden or vendored.
|
|
160
|
+
*
|
|
161
|
+
* Callers that get `false` must leave Theia's own wiring alone: rebinding half
|
|
162
|
+
* of it would be worse than not rebinding at all.
|
|
163
|
+
*/
|
|
164
|
+
export declare function supportsConnectionResilience(isBound: (identifier: typeof SocketWriteBuffer) => boolean): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* The warning a skipped install emits. Says what is lost rather than only what
|
|
167
|
+
* is missing: the failures this guards against are silent, so an adopter who
|
|
168
|
+
* never sees this line has no other way to learn the guard is absent.
|
|
169
|
+
*/
|
|
170
|
+
export declare function warnConnectionResilienceUnavailable(tier: 'frontend' | 'backend'): void;
|
|
171
|
+
/** Builds a buffer with the configured limits, or the defaults for any not configured. */
|
|
172
|
+
export declare function createFramedSocketWriteBuffer(options?: ConnectionResilienceOptions): FramedSocketWriteBuffer;
|
|
173
|
+
//# sourceMappingURL=framed-socket-write-buffer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"framed-socket-write-buffer.d.ts","sourceRoot":"","sources":["../../src/common/framed-socket-write-buffer.ts"],"names":[],"mappings":"AAAA;;;;;;;kFAOkF;AAElF,OAAO,EAAE,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,8BAA8B,CAAC;AACnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,sDAAsD,CAAC;AACzF,OAAO,EAAE,KAAK,2BAA2B,EAAE,MAAM,iCAAiC,CAAC;AAEnF;;;GAGG;AACH,eAAO,MAAM,qBAAqB,iBAAiB,CAAC;AAEpD;;;;GAIG;AACH,MAAM,WAAW,WAAW;IACzB,yGAAyG;IACzG,IAAI,CAAC,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACjD;AAED;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB;IACtC,gDAAgD;IAChD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,oEAAoE;IACpE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,uBAAwB,SAAQ,iBAAiB;IAC3D,gDAAgD;IAChD,MAAM,CAAC,QAAQ,CAAC,iBAAiB,SAAc;IAE/C;;;;;;;;OAQG;IACH,QAAQ,SAA6C;IAErD,6GAA6G;IAC7G,MAAM,CAAC,QAAQ,CAAC,gCAAgC,SAAe;IAE/D;;;;OAIG;IACH,sBAAsB,SAA4D;IAElF,SAAS,C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AAI,IAAI,CAStB;CACH;AAED,8FAA8F;AAC9F,eAAO,MAAM,mBAAmB,8BAA8B,CAAC;AAE/D;;;;;;;;;;;;GAYG;AACH,wBAAgB,4BAA4B,CAAC,OAAO,EAAE,CAAC,UAAU,EAAE,OAAO,iBAAiB,KAAK,OAAO,GAAG,OAAO,CAEhH;AAED;;;;GAIG;AACH,wBAAgB,mCAAmC,CAAC,IAAI,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI,CAMtF;AAED,0FAA0F;AAC1F,wBAAgB,6BAA6B,CAAC,OAAO,GAAE,2BAAgC,GAAG,uBAAuB,CAShH"}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/********************************************************************************
|
|
3
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
4
|
+
*
|
|
5
|
+
* This program and the accompanying materials are made available under the
|
|
6
|
+
* terms of the MIT License which is available in the project root.
|
|
7
|
+
*
|
|
8
|
+
* SPDX-License-Identifier: MIT
|
|
9
|
+
********************************************************************************/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.REQUIRED_THEIA_HINT = exports.FramedSocketWriteBuffer = exports.CONNECTION_LOG_PREFIX = void 0;
|
|
12
|
+
exports.supportsConnectionResilience = supportsConnectionResilience;
|
|
13
|
+
exports.warnConnectionResilienceUnavailable = warnConnectionResilienceUnavailable;
|
|
14
|
+
exports.createFramedSocketWriteBuffer = createFramedSocketWriteBuffer;
|
|
15
|
+
const event_1 = require("@theia/core/lib/common/event");
|
|
16
|
+
const socket_write_buffer_1 = require("@theia/core/lib/common/messaging/socket-write-buffer");
|
|
17
|
+
/**
|
|
18
|
+
* Prefix on every connection log line, deliberately shared by the browser and the
|
|
19
|
+
* server so one pattern greps a console and a pod log at once.
|
|
20
|
+
*/
|
|
21
|
+
exports.CONNECTION_LOG_PREFIX = '[connection]';
|
|
22
|
+
/**
|
|
23
|
+
* Holds messages while the peer is away and re-sends them when it returns.
|
|
24
|
+
*
|
|
25
|
+
* Theia copies them into one growing byte array and flushes that with a single
|
|
26
|
+
* `send`. A websocket delivers whole messages rather than a byte stream, so
|
|
27
|
+
* Theia's encoding adds no length prefix and every reader assumes one delivery
|
|
28
|
+
* is one message. The concatenated flush breaks that assumption: the reader
|
|
29
|
+
* decodes the first message and throws away the bytes behind it.
|
|
30
|
+
*
|
|
31
|
+
* Changed here: one entry per message, sent one at a time, so each arrives as
|
|
32
|
+
* its own delivery. The buffer also decides whether a message may go out
|
|
33
|
+
* directly — see {@link sendOrQueue} — because a caller that decides for itself
|
|
34
|
+
* is how messages end up overtaking a backlog.
|
|
35
|
+
*
|
|
36
|
+
* Unchanged from Theia: exceeding the limit rejects the message rather than
|
|
37
|
+
* dropping something silently. The limit is settable, since the right value
|
|
38
|
+
* depends on how long an outage has to survive.
|
|
39
|
+
*
|
|
40
|
+
* Added here: every message sent is numbered and kept until the peer
|
|
41
|
+
* acknowledges it, and {@link flush} resends what is still unacknowledged ahead
|
|
42
|
+
* of the backlog. A socket whose far end died unnoticed still accepts writes, so
|
|
43
|
+
* a message is not delivered just because it was sent.
|
|
44
|
+
*
|
|
45
|
+
* Logging goes to `console` rather than through a `Logger`. On the browser side
|
|
46
|
+
* Theia's preloader builds this before any Output channel exists, so there is
|
|
47
|
+
* nothing else to write to; the {@link CONNECTION_LOG_PREFIX} keeps the lines
|
|
48
|
+
* findable on both sides.
|
|
49
|
+
*/
|
|
50
|
+
class FramedSocketWriteBuffer extends socket_write_buffer_1.SocketWriteBuffer {
|
|
51
|
+
/** Theia's fixed limit, kept as the default. */
|
|
52
|
+
static DEFAULT_MAX_BYTES = 100 * 1024;
|
|
53
|
+
/**
|
|
54
|
+
* Settable so a deployment can trade memory for longer outages.
|
|
55
|
+
*
|
|
56
|
+
* Consulted directly rather than through the base class's `maxBufferSize`,
|
|
57
|
+
* which does not exist before Theia 1.71 — an `override` of it would not
|
|
58
|
+
* compile against the oldest release this package supports. Nothing is lost:
|
|
59
|
+
* `buffer`, `flush` and `drain` are all replaced here, so the base's own
|
|
60
|
+
* limit is never reached.
|
|
61
|
+
*/
|
|
62
|
+
maxBytes = FramedSocketWriteBuffer.DEFAULT_MAX_BYTES;
|
|
63
|
+
/** Kept copies past this are discarded, so a peer that never acknowledges cannot grow them without bound. */
|
|
64
|
+
static DEFAULT_MAX_UNACKNOWLEDGED_BYTES = 1024 * 1024;
|
|
65
|
+
/**
|
|
66
|
+
* Separate from {@link maxBytes}, which counts only what is produced while
|
|
67
|
+
* the peer is away: copies kept on a healthy connection must not use up the
|
|
68
|
+
* room an outage needs.
|
|
69
|
+
*/
|
|
70
|
+
maxUnacknowledgedBytes = FramedSocketWriteBuffer.DEFAULT_MAX_UNACKNOWLEDGED_BYTES;
|
|
71
|
+
pending = [];
|
|
72
|
+
pendingBytes = 0;
|
|
73
|
+
overflowReported = false;
|
|
74
|
+
onOverflowEmitter = new event_1.Emitter();
|
|
75
|
+
nextSequence = 1;
|
|
76
|
+
/** Oldest first: {@link acknowledge} stops at the first entry the peer has not accepted. */
|
|
77
|
+
unacknowledged = [];
|
|
78
|
+
unacknowledgedBytes = 0;
|
|
79
|
+
unacknowledgedOverflowReported = false;
|
|
80
|
+
/**
|
|
81
|
+
* Whether the peer drops a message it already has. One without the hardening
|
|
82
|
+
* applies a resent message a second time, so nothing is resent until the
|
|
83
|
+
* peer has shown otherwise.
|
|
84
|
+
*/
|
|
85
|
+
peerDeduplicates = false;
|
|
86
|
+
/** Fires once per outage when the limit is reached. */
|
|
87
|
+
get onOverflow() {
|
|
88
|
+
return this.onOverflowEmitter.event;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Sends `data` now only if nothing is waiting, and queues it otherwise.
|
|
92
|
+
*
|
|
93
|
+
* Callers say whether the peer is ready for a message; the buffer decides
|
|
94
|
+
* whether it may go ahead of one already waiting. Splitting it this way is
|
|
95
|
+
* the point: a caller that answers both questions from one flag is how a
|
|
96
|
+
* fresh message ends up overtaking a backlog.
|
|
97
|
+
*/
|
|
98
|
+
sendOrQueue(socket, data) {
|
|
99
|
+
if (socket && !this.hasBacklog) {
|
|
100
|
+
this.send(socket, data);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
this.buffer(data);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
buffer(data) {
|
|
107
|
+
if (this.pendingBytes + data.byteLength > this.maxBytes) {
|
|
108
|
+
this.reportOverflow();
|
|
109
|
+
throw new Error(`Max disconnected buffer size exceeded by adding ${data.byteLength} bytes`);
|
|
110
|
+
}
|
|
111
|
+
// Copied for the same reason the base class copies: the caller owns `data`.
|
|
112
|
+
this.pending.push(data.slice());
|
|
113
|
+
this.pendingBytes += data.byteLength;
|
|
114
|
+
if (this.pending.length === 1) {
|
|
115
|
+
console.info(`${exports.CONNECTION_LOG_PREFIX} buffering messages, the peer is disconnected`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Kept only once `send` returns: a message whose send threw stays in the
|
|
120
|
+
* backlog, and keeping it as well would send it again under a second number,
|
|
121
|
+
* which the receiver cannot recognise as a duplicate.
|
|
122
|
+
*/
|
|
123
|
+
send(socket, data) {
|
|
124
|
+
const sequence = this.nextSequence++;
|
|
125
|
+
socket.send(data, sequence);
|
|
126
|
+
this.keep(sequence, data);
|
|
127
|
+
}
|
|
128
|
+
keep(sequence, data) {
|
|
129
|
+
if (this.unacknowledgedBytes + data.byteLength > this.maxUnacknowledgedBytes) {
|
|
130
|
+
// Said only to a peer known to acknowledge: one without the hardening never does, and
|
|
131
|
+
// nothing would be resent to it anyway.
|
|
132
|
+
if (this.peerDeduplicates && !this.unacknowledgedOverflowReported) {
|
|
133
|
+
this.unacknowledgedOverflowReported = true;
|
|
134
|
+
console.warn(`${exports.CONNECTION_LOG_PREFIX} the peer has not acknowledged ${this.unacknowledged.length} message(s), ` +
|
|
135
|
+
`${this.unacknowledgedBytes} bytes; discarding their copies, so a connection dropped now loses them`);
|
|
136
|
+
}
|
|
137
|
+
this.unacknowledged = [];
|
|
138
|
+
this.unacknowledgedBytes = 0;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// Not copied: a committed write buffer hands over a fresh array and is disposed.
|
|
142
|
+
this.unacknowledged.push({ sequence, data });
|
|
143
|
+
this.unacknowledgedBytes += data.byteLength;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Records that the peer numbers its messages, which only a peer that also
|
|
147
|
+
* drops duplicates does, so resending to it is safe.
|
|
148
|
+
*/
|
|
149
|
+
markPeerDeduplicates() {
|
|
150
|
+
this.peerDeduplicates = true;
|
|
151
|
+
}
|
|
152
|
+
/** Releases every kept message numbered up to `sequence`, which the peer has accepted. */
|
|
153
|
+
acknowledge(sequence) {
|
|
154
|
+
this.peerDeduplicates = true;
|
|
155
|
+
let index = 0;
|
|
156
|
+
while (index < this.unacknowledged.length && this.unacknowledged[index].sequence <= sequence) {
|
|
157
|
+
this.unacknowledgedBytes -= this.unacknowledged[index].data.byteLength;
|
|
158
|
+
index++;
|
|
159
|
+
}
|
|
160
|
+
if (index > 0) {
|
|
161
|
+
this.unacknowledged = this.unacknowledged.slice(index);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Resends what the peer has not acknowledged, under the same numbers, then
|
|
166
|
+
* sends the backlog. Some of the resent messages may have arrived already;
|
|
167
|
+
* the receiver recognises them by number and drops them.
|
|
168
|
+
*
|
|
169
|
+
* Resends only to a peer known to drop duplicates, and otherwise discards the
|
|
170
|
+
* copies, leaving any message lost with the old socket lost.
|
|
171
|
+
*/
|
|
172
|
+
flush(socket) {
|
|
173
|
+
if (this.peerDeduplicates) {
|
|
174
|
+
for (const { sequence, data } of this.unacknowledged) {
|
|
175
|
+
socket.send(data, sequence);
|
|
176
|
+
}
|
|
177
|
+
if (this.unacknowledged.length > 0) {
|
|
178
|
+
console.info(`${exports.CONNECTION_LOG_PREFIX} resent ${this.unacknowledged.length} unacknowledged message(s), ${this.unacknowledgedBytes} bytes`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
this.unacknowledged = [];
|
|
183
|
+
this.unacknowledgedBytes = 0;
|
|
184
|
+
}
|
|
185
|
+
if (this.pending.length === 0) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const count = this.pending.length;
|
|
189
|
+
const bytes = this.pendingBytes;
|
|
190
|
+
// Walked with an index and dropped in one slice, not shifted per message: a shift re-indexes
|
|
191
|
+
// the whole array, so the drain would cost the square of a backlog `bufferBytes` invites an
|
|
192
|
+
// adopter to enlarge. Leaving the queue in place is what keeps `hasBacklog` true until the
|
|
193
|
+
// last message is out, and re-reading the length is what drains one appended by a nested send.
|
|
194
|
+
let index = 0;
|
|
195
|
+
try {
|
|
196
|
+
while (index < this.pending.length) {
|
|
197
|
+
const message = this.pending[index];
|
|
198
|
+
this.send(socket, message);
|
|
199
|
+
index++;
|
|
200
|
+
this.pendingBytes -= message.byteLength;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
// Said here because nothing retries: Theia flushes once, from a reconnect handler that has
|
|
205
|
+
// deregistered by now, so the stall would otherwise surface much later as an overflow —
|
|
206
|
+
// which names the wrong cause, the socket being up rather than absent.
|
|
207
|
+
console.error(`${exports.CONNECTION_LOG_PREFIX} send failed ${index} message(s) into a ${count}-message backlog; ` +
|
|
208
|
+
`${this.pending.length - index} still queued and nothing will retry them`, error);
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
finally {
|
|
212
|
+
this.pending = this.pending.slice(index);
|
|
213
|
+
}
|
|
214
|
+
this.overflowReported = false;
|
|
215
|
+
console.info(`${exports.CONNECTION_LOG_PREFIX} sent ${count} buffered message(s), ${bytes} bytes`);
|
|
216
|
+
}
|
|
217
|
+
drain() {
|
|
218
|
+
if (this.pending.length > 0 || this.unacknowledged.length > 0) {
|
|
219
|
+
console.warn(`${exports.CONNECTION_LOG_PREFIX} discarded ${this.pending.length} buffered message(s), ${this.pendingBytes} bytes, ` +
|
|
220
|
+
`and ${this.unacknowledged.length} sent but unacknowledged, ${this.unacknowledgedBytes} bytes`);
|
|
221
|
+
}
|
|
222
|
+
this.reset();
|
|
223
|
+
}
|
|
224
|
+
/** Whether messages are still waiting to go out. */
|
|
225
|
+
get hasBacklog() {
|
|
226
|
+
return this.pending.length > 0;
|
|
227
|
+
}
|
|
228
|
+
reportOverflow() {
|
|
229
|
+
if (this.overflowReported) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
this.overflowReported = true;
|
|
233
|
+
const overflow = { messages: this.pending.length, bytes: this.pendingBytes, maxBytes: this.maxBytes };
|
|
234
|
+
console.error(`${exports.CONNECTION_LOG_PREFIX} buffer full at ${overflow.maxBytes} bytes after ${overflow.messages} message(s); ` +
|
|
235
|
+
'this message is rejected, and so is every further one until the peer returns');
|
|
236
|
+
this.onOverflowEmitter.fire(overflow);
|
|
237
|
+
}
|
|
238
|
+
/** Also discards the kept copies: a drain ends the session, and resending them would replay it into the next one. */
|
|
239
|
+
reset() {
|
|
240
|
+
this.pending = [];
|
|
241
|
+
this.pendingBytes = 0;
|
|
242
|
+
this.overflowReported = false;
|
|
243
|
+
this.nextSequence = 1;
|
|
244
|
+
this.unacknowledged = [];
|
|
245
|
+
this.unacknowledgedBytes = 0;
|
|
246
|
+
this.unacknowledgedOverflowReported = false;
|
|
247
|
+
this.peerDeduplicates = false;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
exports.FramedSocketWriteBuffer = FramedSocketWriteBuffer;
|
|
251
|
+
/** Named in the warning a skipped install emits, so the reader is told what to upgrade to. */
|
|
252
|
+
exports.REQUIRED_THEIA_HINT = '@theia/core 1.71 or newer';
|
|
253
|
+
/**
|
|
254
|
+
* Whether this Theia exposes the seam the reconnect hardening replaces.
|
|
255
|
+
*
|
|
256
|
+
* Theia only began binding {@link SocketWriteBuffer} in 1.71; before that each
|
|
257
|
+
* connection built one privately, so there is no binding to rebind and no way
|
|
258
|
+
* in. Every other API involved has been there throughout, which makes this one
|
|
259
|
+
* probe the whole compatibility question — and it is a binding check rather
|
|
260
|
+
* than a version parse, so it answers about the container actually in front of
|
|
261
|
+
* us instead of a number that may have been overridden or vendored.
|
|
262
|
+
*
|
|
263
|
+
* Callers that get `false` must leave Theia's own wiring alone: rebinding half
|
|
264
|
+
* of it would be worse than not rebinding at all.
|
|
265
|
+
*/
|
|
266
|
+
function supportsConnectionResilience(isBound) {
|
|
267
|
+
return isBound(socket_write_buffer_1.SocketWriteBuffer);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The warning a skipped install emits. Says what is lost rather than only what
|
|
271
|
+
* is missing: the failures this guards against are silent, so an adopter who
|
|
272
|
+
* never sees this line has no other way to learn the guard is absent.
|
|
273
|
+
*/
|
|
274
|
+
function warnConnectionResilienceUnavailable(tier) {
|
|
275
|
+
console.warn(`${exports.CONNECTION_LOG_PREFIX} ${tier} reconnect hardening NOT installed: this Theia does not expose an injectable ` +
|
|
276
|
+
`socket write buffer (needs ${exports.REQUIRED_THEIA_HINT}). Theia's own behaviour is left in place, which can lose or ` +
|
|
277
|
+
'reorder messages when a socket drops and reconnects.');
|
|
278
|
+
}
|
|
279
|
+
/** Builds a buffer with the configured limits, or the defaults for any not configured. */
|
|
280
|
+
function createFramedSocketWriteBuffer(options = {}) {
|
|
281
|
+
const buffer = new FramedSocketWriteBuffer();
|
|
282
|
+
if (isPositive(options.bufferBytes)) {
|
|
283
|
+
buffer.maxBytes = options.bufferBytes;
|
|
284
|
+
}
|
|
285
|
+
if (isPositive(options.unacknowledgedBytes)) {
|
|
286
|
+
buffer.maxUnacknowledgedBytes = options.unacknowledgedBytes;
|
|
287
|
+
}
|
|
288
|
+
return buffer;
|
|
289
|
+
}
|
|
290
|
+
function isPositive(value) {
|
|
291
|
+
return typeof value === 'number' && value > 0;
|
|
292
|
+
}
|
|
293
|
+
//# sourceMappingURL=framed-socket-write-buffer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"framed-socket-write-buffer.js","sourceRoot":"","sources":["../../src/common/framed-socket-write-buffer.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF;;;;;;AAElF,wDAAmE;AACnE,8FAAyF;AAGzF;;;GAGG;AACU,QAAA,qBAAqB,GAAG,cAAc,CAAC;AA2BpD;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,6BAAqC,SAAQ,uCAAiB;IAC3D,gDAAgD;IAChD,MAAM,CAAU,iBAAiB,GAAG,GAAG,GAAG,IAAI,CAAC;IAE/C;;;;;;;;OAQG;IACH,QAAQ,GAAG,uBAAuB,CAAC,iBAAiB,CAAC;IAErD,6GAA6G;IAC7G,MAAM,CAAU,gCAAgC,GAAG,IAAI,GAAG,IAAI,CAAC;IAE/D;;;;OAIG;IACH,sBAAsB,GAAG,uBAAuB,CAAC,gCAAgC,CAAC;IAExE,OAAO,GAAiB,EAAE,CAAC;IAC3B,YAAY,GAAG,CAAC,CAAC;IACjB,gBAAgB,GAAG,KAAK,CAAC;IAChB,iBAAiB,GAAG,IAAI,eAAO,EAA4B,CAAC;IAErE,YAAY,GAAG,CAAC,CAAC;IAC3B,4FAA4F;IAClF,cAAc,GAA+D,EAAE,CAAC;IAChF,mBAAmB,GAAG,CAAC,CAAC;IACxB,8BAA8B,GAAG,KAAK,CAAC;IACjD;;;;OAIG;IACO,gBAAgB,GAAG,KAAK,CAAC;IAEnC,uDAAuD;IACvD,IAAI,UAAU;QACX,OAAO,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC;IACvC,CAAC;IAED;;;;;;;OAOG;IACH,WAAW,CAAC,MAA+B,EAAE,IAAgB;QAC1D,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,CAAC;YAC9B,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC3B,CAAC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,QAAQ,IAAI,KAAK,GAAG,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
/** The socket.io event that carries the highest message number a side has accepted. */
|
|
10
|
+
export declare const ACKNOWLEDGEMENT_EVENT = "hydranium:ack";
|
|
11
|
+
/**
|
|
12
|
+
* What one side has received on a session: drops a message it already accepted
|
|
13
|
+
* and acknowledges the rest.
|
|
14
|
+
*
|
|
15
|
+
* The peer resends everything unacknowledged when a session resumes, because it
|
|
16
|
+
* cannot tell which of its messages died with the old socket, so every resumed
|
|
17
|
+
* session can repeat messages already delivered. Delivering one twice corrupts
|
|
18
|
+
* anything applied by position, just as losing one does.
|
|
19
|
+
*
|
|
20
|
+
* Acknowledges once enough has arrived, and otherwise after a short delay. A
|
|
21
|
+
* timer alone is not enough: a background tab throttles timers, and the peer
|
|
22
|
+
* holds every unacknowledged message until the acknowledgement arrives. The
|
|
23
|
+
* first message of a session is acknowledged at once, because the peer resends
|
|
24
|
+
* nothing until an acknowledgement shows it may.
|
|
25
|
+
*
|
|
26
|
+
* Belongs to one session. A new session numbers from the start again, so it
|
|
27
|
+
* needs a new instance.
|
|
28
|
+
*/
|
|
29
|
+
export declare class InboundMessageSequence {
|
|
30
|
+
protected readonly sendAcknowledgement: (sequence: number) => void;
|
|
31
|
+
static readonly ACKNOWLEDGE_AFTER_MESSAGES = 64;
|
|
32
|
+
static readonly ACKNOWLEDGE_AFTER_BYTES: number;
|
|
33
|
+
static readonly ACKNOWLEDGE_DELAY_MS = 100;
|
|
34
|
+
protected lastAccepted: number;
|
|
35
|
+
protected messagesSinceAcknowledgement: number;
|
|
36
|
+
protected bytesSinceAcknowledgement: number;
|
|
37
|
+
protected acknowledgementTimer: ReturnType<typeof setTimeout> | undefined;
|
|
38
|
+
constructor(sendAcknowledgement: (sequence: number) => void);
|
|
39
|
+
/**
|
|
40
|
+
* Whether to deliver a message that arrived with `sequence`.
|
|
41
|
+
*
|
|
42
|
+
* A message without a number comes from a peer that does not number them,
|
|
43
|
+
* and is delivered as it is. A duplicate is still acknowledged: the peer
|
|
44
|
+
* resent it because an earlier acknowledgement was lost, and keeps it until
|
|
45
|
+
* one arrives.
|
|
46
|
+
*
|
|
47
|
+
* A skipped number is delivered past, with a warning: the skipped messages
|
|
48
|
+
* are lost, and this is the one place that can tell.
|
|
49
|
+
*/
|
|
50
|
+
accept(sequence: unknown, bytes: number): boolean;
|
|
51
|
+
/** Sends the acknowledgement now rather than waiting for more traffic. */
|
|
52
|
+
acknowledge(): void;
|
|
53
|
+
dispose(): void;
|
|
54
|
+
protected cancelTimer(): void;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=inbound-message-sequence.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inbound-message-sequence.d.ts","sourceRoot":"","sources":["../../src/common/inbound-message-sequence.ts"],"names":[],"mappings":"AAAA;;;;;;;kFAOkF;AAIlF,uFAAuF;AACvF,eAAO,MAAM,qBAAqB,kBAAkB,CAAC;AAErD;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,sBAAsB;IAUpB,SAAS,CAAC,QAAQ,CAAC,mBAAmB,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI;IAT9E,MAAM,CAAC,QAAQ,CAAC,0BAA0B,MAAM;IAChD,MAAM,CAAC,QAAQ,CAAC,uBAAuB,SAAa;IACpD,MAAM,CAAC,QAAQ,CAAC,oBAAoB,OAAO;IAE3C,SAAS,CAAC,YAAY,SAAK;IAC3B,SAAS,CAAC,4BAA4B,SAAK;IAC3C,SAAS,CAAC,yBAAyB,SAAK;IACxC,SAAS,CAAC,oBAAoB,EAAE,UAAU,CAAC,OAAO,UAAU,CAAC,GAAG,SAAS,CAAC;IAE1E,YAA+B,mBAAmB,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,EAAI;IAElF;;;;;;;;;;OAUG;IACH,MAAM,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CA4BhD;IAED,0EAA0E;IAC1E,WAAW,IAAI,IAAI,CAKlB;IAED,OAAO,IAAI,IAAI,CAEd;IAED,SAAS,CAAC,WAAW,IAAI,IAAI,CAG5B;CACH"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/********************************************************************************
|
|
3
|
+
* Copyright (c) 2026 EclipseSource and others.
|
|
4
|
+
*
|
|
5
|
+
* This program and the accompanying materials are made available under the
|
|
6
|
+
* terms of the MIT License which is available in the project root.
|
|
7
|
+
*
|
|
8
|
+
* SPDX-License-Identifier: MIT
|
|
9
|
+
********************************************************************************/
|
|
10
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.InboundMessageSequence = exports.ACKNOWLEDGEMENT_EVENT = void 0;
|
|
12
|
+
const framed_socket_write_buffer_1 = require("./framed-socket-write-buffer");
|
|
13
|
+
/** The socket.io event that carries the highest message number a side has accepted. */
|
|
14
|
+
exports.ACKNOWLEDGEMENT_EVENT = 'hydranium:ack';
|
|
15
|
+
/**
|
|
16
|
+
* What one side has received on a session: drops a message it already accepted
|
|
17
|
+
* and acknowledges the rest.
|
|
18
|
+
*
|
|
19
|
+
* The peer resends everything unacknowledged when a session resumes, because it
|
|
20
|
+
* cannot tell which of its messages died with the old socket, so every resumed
|
|
21
|
+
* session can repeat messages already delivered. Delivering one twice corrupts
|
|
22
|
+
* anything applied by position, just as losing one does.
|
|
23
|
+
*
|
|
24
|
+
* Acknowledges once enough has arrived, and otherwise after a short delay. A
|
|
25
|
+
* timer alone is not enough: a background tab throttles timers, and the peer
|
|
26
|
+
* holds every unacknowledged message until the acknowledgement arrives. The
|
|
27
|
+
* first message of a session is acknowledged at once, because the peer resends
|
|
28
|
+
* nothing until an acknowledgement shows it may.
|
|
29
|
+
*
|
|
30
|
+
* Belongs to one session. A new session numbers from the start again, so it
|
|
31
|
+
* needs a new instance.
|
|
32
|
+
*/
|
|
33
|
+
class InboundMessageSequence {
|
|
34
|
+
sendAcknowledgement;
|
|
35
|
+
static ACKNOWLEDGE_AFTER_MESSAGES = 64;
|
|
36
|
+
static ACKNOWLEDGE_AFTER_BYTES = 32 * 1024;
|
|
37
|
+
static ACKNOWLEDGE_DELAY_MS = 100;
|
|
38
|
+
lastAccepted = 0;
|
|
39
|
+
messagesSinceAcknowledgement = 0;
|
|
40
|
+
bytesSinceAcknowledgement = 0;
|
|
41
|
+
acknowledgementTimer;
|
|
42
|
+
constructor(sendAcknowledgement) {
|
|
43
|
+
this.sendAcknowledgement = sendAcknowledgement;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Whether to deliver a message that arrived with `sequence`.
|
|
47
|
+
*
|
|
48
|
+
* A message without a number comes from a peer that does not number them,
|
|
49
|
+
* and is delivered as it is. A duplicate is still acknowledged: the peer
|
|
50
|
+
* resent it because an earlier acknowledgement was lost, and keeps it until
|
|
51
|
+
* one arrives.
|
|
52
|
+
*
|
|
53
|
+
* A skipped number is delivered past, with a warning: the skipped messages
|
|
54
|
+
* are lost, and this is the one place that can tell.
|
|
55
|
+
*/
|
|
56
|
+
accept(sequence, bytes) {
|
|
57
|
+
if (typeof sequence !== 'number') {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
const isFirst = this.lastAccepted === 0;
|
|
61
|
+
const isNew = sequence > this.lastAccepted;
|
|
62
|
+
if (isNew) {
|
|
63
|
+
if (sequence > this.lastAccepted + 1) {
|
|
64
|
+
const missing = sequence - this.lastAccepted - 1;
|
|
65
|
+
console.warn(`${framed_socket_write_buffer_1.CONNECTION_LOG_PREFIX} lost ${missing} message(s), numbered ${this.lastAccepted + 1} to ${sequence - 1}; ` +
|
|
66
|
+
'they never arrived and nothing will resend them');
|
|
67
|
+
}
|
|
68
|
+
this.lastAccepted = sequence;
|
|
69
|
+
}
|
|
70
|
+
this.messagesSinceAcknowledgement++;
|
|
71
|
+
this.bytesSinceAcknowledgement += bytes;
|
|
72
|
+
if (isFirst ||
|
|
73
|
+
this.messagesSinceAcknowledgement >= InboundMessageSequence.ACKNOWLEDGE_AFTER_MESSAGES ||
|
|
74
|
+
this.bytesSinceAcknowledgement >= InboundMessageSequence.ACKNOWLEDGE_AFTER_BYTES) {
|
|
75
|
+
this.acknowledge();
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.acknowledgementTimer ??= setTimeout(() => this.acknowledge(), InboundMessageSequence.ACKNOWLEDGE_DELAY_MS);
|
|
79
|
+
}
|
|
80
|
+
return isNew;
|
|
81
|
+
}
|
|
82
|
+
/** Sends the acknowledgement now rather than waiting for more traffic. */
|
|
83
|
+
acknowledge() {
|
|
84
|
+
this.cancelTimer();
|
|
85
|
+
this.messagesSinceAcknowledgement = 0;
|
|
86
|
+
this.bytesSinceAcknowledgement = 0;
|
|
87
|
+
this.sendAcknowledgement(this.lastAccepted);
|
|
88
|
+
}
|
|
89
|
+
dispose() {
|
|
90
|
+
this.cancelTimer();
|
|
91
|
+
}
|
|
92
|
+
cancelTimer() {
|
|
93
|
+
clearTimeout(this.acknowledgementTimer);
|
|
94
|
+
this.acknowledgementTimer = undefined;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.InboundMessageSequence = InboundMessageSequence;
|
|
98
|
+
//# sourceMappingURL=inbound-message-sequence.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inbound-message-sequence.js","sourceRoot":"","sources":["../../src/common/inbound-message-sequence.ts"],"names":[],"mappings":";AAAA;;;;;;;kFAOkF;;;AAElF,6EAAqE;AAErE,uFAAuF;AAC1E,QAAA,qBAAqB,GAA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sB,CAAC,uBAAuB,EACjF,CAAC;YACA,IAAI,CAAC,WAAW,EAAE,CAAC;QACtB,CAAC;aAAM,CAAC;YACL,IAAI,CAAC,oBAAoB,KAAK,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,sBAAsB,CAAC,oBAAoB,CAAC,CAAC;QACnH,CAAC;QACD,OAAO,KAAK,CAAC;IAChB,CAAC;IAED,0EAA0E;IAC1E,WAAW;QACR,IAAI,CAAC,WAAW,EAAE,CAAC;QACnB,IAAI,CAAC,4BAA4B,GAAG,CAAC,CAAC;QACtC,IAAI,CAAC,yBAAyB,GAAG,CAAC,CAAC;QACnC,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO;QACJ,IAAI,CAAC,WAAW,EAAE,CAAC;IACtB,CAAC;IAES,WAAW;QAClB,YAAY,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;QACxC,IAAI,CAAC,oBAAoB,GAAG,SAAS,CAAC;IACzC,CAAC;CACH"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* Copyright (c) 2026 CrossBreeze, EclipseSource and others.
|
|
3
|
+
*
|
|
4
|
+
* This program and the accompanying materials are made available under the
|
|
5
|
+
* terms of the MIT License which is available in the project root.
|
|
6
|
+
*
|
|
7
|
+
* SPDX-License-Identifier: MIT
|
|
8
|
+
********************************************************************************/
|
|
9
|
+
export * from './framed-socket-write-buffer';
|
|
10
|
+
export * from './inbound-message-sequence';
|
|
11
|
+
export * from './connection-resilience-options';
|
|
12
|
+
export * from './clock';
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|