@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.1
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/ARCHITECTURE.md +43 -0
- package/LICENSE +202 -0
- package/README.md +142 -2
- package/assets/openllm-light.svg +18 -0
- package/assets/openllm.svg +18 -0
- package/client/chat.ts +140 -0
- package/client/devices.ts +269 -0
- package/client/http.ts +72 -0
- package/client/index.ts +129 -0
- package/client/relay-mux.ts +547 -0
- package/client/types.ts +51 -0
- package/client/vault.ts +239 -0
- package/connect/chat.ts +58 -0
- package/connect/client.ts +240 -0
- package/connect/config.ts +47 -0
- package/connect/errors.ts +121 -0
- package/connect/index.ts +23 -0
- package/connect/oauth.ts +265 -0
- package/connect/routes.ts +234 -0
- package/connect/store.ts +107 -0
- package/mcp/handle.ts +24 -0
- package/mcp/index.ts +76 -0
- package/mcp/server.ts +319 -0
- package/mcp/tools/marketplace.ts +439 -0
- package/mcp/tools/memory-client.ts +297 -0
- package/mcp/tools/memory-config.ts +32 -0
- package/mcp/tools/memory.ts +369 -0
- package/mcp/tools/model-priority.ts +88 -0
- package/mcp/tools/openllm.ts +883 -0
- package/mcp/types.ts +28 -0
- package/openapi/call.ts +90 -0
- package/openapi/index.ts +8 -0
- package/openapi/openapi.json +80213 -0
- package/openapi/operations.ts +2075 -0
- package/package.json +39 -4
- package/protocol/CONTRIBUTING.md +21 -0
- package/protocol/README.md +134 -0
- package/protocol/access.ts +186 -0
- package/protocol/agent-prompt-text.ts +32 -0
- package/protocol/agent.ts +991 -0
- package/protocol/api-key-syntax.ts +105 -0
- package/protocol/assets/openllm-light.svg +18 -0
- package/protocol/assets/openllm.svg +18 -0
- package/protocol/audio.ts +29 -0
- package/protocol/auth.ts +142 -0
- package/protocol/changelog.ts +155 -0
- package/protocol/chat.ts +458 -0
- package/protocol/client-registry.ts +356 -0
- package/protocol/config.ts +345 -0
- package/protocol/control-channel.ts +115 -0
- package/protocol/cooldown-reason.ts +78 -0
- package/protocol/cost.ts +10 -0
- package/protocol/daemon-port.ts +31 -0
- package/protocol/daemon.ts +1524 -0
- package/protocol/default-chains.ts +66 -0
- package/protocol/device-state.ts +85 -0
- package/protocol/doctor-report-local.ts +443 -0
- package/protocol/doctor-report-policy.ts +119 -0
- package/protocol/doctor-report.ts +947 -0
- package/protocol/errors.ts +14 -0
- package/protocol/execution-selection.ts +141 -0
- package/protocol/hop-grant.ts +33 -0
- package/protocol/image-edit-parse.ts +263 -0
- package/protocol/images.ts +298 -0
- package/protocol/index.ts +74 -0
- package/protocol/inference-headers.ts +3 -0
- package/protocol/keepalive.ts +69 -0
- package/protocol/local-auth.ts +300 -0
- package/protocol/local-device-control.ts +133 -0
- package/protocol/local-provider.ts +288 -0
- package/protocol/local-session.ts +118 -0
- package/protocol/marketplace.ts +187 -0
- package/protocol/media-attempt.ts +81 -0
- package/protocol/media-bytes.ts +28 -0
- package/protocol/media-default.ts +357 -0
- package/protocol/media-dimensions.ts +132 -0
- package/protocol/media-input.ts +61 -0
- package/protocol/media-model.ts +142 -0
- package/protocol/media-option-issue.ts +90 -0
- package/protocol/media-types.ts +70 -0
- package/protocol/model-match.ts +1051 -0
- package/protocol/model-settings.ts +327 -0
- package/protocol/models.ts +767 -0
- package/protocol/mux.ts +722 -0
- package/protocol/plan-surface.ts +59 -0
- package/protocol/provider-models.ts +16 -0
- package/protocol/provider-status.ts +206 -0
- package/protocol/provider-tier.ts +67 -0
- package/protocol/provider-usage.ts +263 -0
- package/protocol/providers.ts +901 -0
- package/protocol/public-usage-index.ts +354 -0
- package/protocol/realtime-generated.ts +31 -0
- package/protocol/realtime.ts +324 -0
- package/protocol/registry-pointer.ts +118 -0
- package/protocol/relay.ts +702 -0
- package/protocol/replay-session.ts +11 -0
- package/protocol/responses.ts +280 -0
- package/protocol/semver.ts +182 -0
- package/protocol/setup-mode.ts +22 -0
- package/protocol/sha256-hex.ts +98 -0
- package/protocol/stats.ts +406 -0
- package/protocol/streaming.ts +19 -0
- package/protocol/subscription-daemon-floors.ts +33 -0
- package/protocol/subscription-provider-compat.ts +104 -0
- package/protocol/subscription-provider.ts +32 -0
- package/protocol/team-grant.ts +17 -0
- package/protocol/team-shareable.ts +61 -0
- package/protocol/transcription-encoding.ts +76 -0
- package/protocol/url-locality-dns.ts +37 -0
- package/protocol/url-locality.ts +288 -0
- package/protocol/usage-snapshots.ts +9 -0
- package/protocol/vendor-cli-install.ts +29 -0
- package/protocol/video-job-plan.ts +50 -0
- package/protocol/videos.ts +227 -0
- package/tsconfig.json +14 -0
- package/tunnel/ARCHITECTURE.md +111 -0
- package/tunnel/LICENSE +33 -0
- package/tunnel/README.md +45 -0
- package/tunnel/assets/openllm-light.svg +18 -0
- package/tunnel/assets/openllm.svg +18 -0
- package/tunnel/channel-envelope.ts +139 -0
- package/tunnel/codec.ts +191 -0
- package/tunnel/device-anchor.ts +229 -0
- package/tunnel/device-control.ts +118 -0
- package/tunnel/device-grant.ts +173 -0
- package/tunnel/fleet.ts +77 -0
- package/tunnel/index.ts +22 -0
- package/tunnel/mux.ts +580 -0
- package/tunnel/peer-link.ts +390 -0
- package/tunnel/rtc-auth.ts +415 -0
- package/tunnel/rtc-duplex.ts +187 -0
- package/tunnel/rtc-ice.ts +136 -0
- package/tunnel/secure.ts +1298 -0
- package/tunnel/stream-reset-error.ts +14 -0
- package/tunnel/streams.ts +1430 -0
- package/tunnel/team-grant.ts +302 -0
- package/tunnel/wire-bytes.ts +102 -0
- package/vault/ARCHITECTURE.md +470 -0
- package/vault/LICENSE +33 -0
- package/vault/api-key.ts +190 -0
- package/vault/argon2.ts +106 -0
- package/vault/cli-auth-seal.ts +157 -0
- package/vault/cred-seal.ts +146 -0
- package/vault/credential.ts +55 -0
- package/vault/crypto.ts +210 -0
- package/vault/daemon-attest.ts +229 -0
- package/vault/derive.ts +113 -0
- package/vault/device-access.ts +87 -0
- package/vault/device-anchor.ts +185 -0
- package/vault/device-control.ts +174 -0
- package/vault/handoff.ts +396 -0
- package/vault/index.ts +21 -0
- package/vault/passkey.ts +243 -0
- package/vault/phrase.ts +65 -0
- package/vault/pin.ts +153 -0
- package/vault/public-key-seal.ts +131 -0
- package/vault/seed.ts +272 -0
- package/vault/team.ts +355 -0
- package/vault/vault-item.ts +131 -0
- package/vault/vault-migrate-digest.ts +29 -0
- package/vault/wrapped.ts +77 -0
- package/wire/ARCHITECTURE.md +90 -0
- package/wire/CONTRIBUTING.md +19 -0
- package/wire/LICENSE +33 -0
- package/wire/README.md +51 -0
- package/wire/adapters/messages/anthropic-map.ts +37 -0
- package/wire/adapters/messages/index.ts +7 -0
- package/wire/adapters/messages/reasoning-from-items.ts +38 -0
- package/wire/adapters/messages/reasoning-signature.ts +243 -0
- package/wire/adapters/messages/request.ts +574 -0
- package/wire/adapters/messages/response.ts +215 -0
- package/wire/adapters/messages/streaming.ts +1027 -0
- package/wire/adapters/messages/usage.ts +43 -0
- package/wire/adapters/responses/index.ts +3 -0
- package/wire/adapters/responses/request.ts +395 -0
- package/wire/adapters/responses/response.ts +88 -0
- package/wire/adapters/responses/streaming.ts +344 -0
- package/wire/assets/openllm-light.svg +18 -0
- package/wire/assets/openllm.svg +18 -0
- package/wire/features/account-rank.ts +166 -0
- package/wire/features/compaction/compact-request.ts +862 -0
- package/wire/features/compaction/compaction-text.ts +43 -0
- package/wire/features/context-demote.ts +47 -0
- package/wire/features/context-overflow-strategy.ts +26 -0
- package/wire/features/context-skip.ts +170 -0
- package/wire/features/conversation-affinity.ts +144 -0
- package/wire/features/max-tokens-backfill.ts +141 -0
- package/wire/features/model-caps.ts +53 -0
- package/wire/features/model-settings.ts +24 -0
- package/wire/features/output-limit-clamp.ts +64 -0
- package/wire/features/quota-gate.ts +85 -0
- package/wire/index.ts +44 -0
- package/wire/lib/attempt-outcome.ts +43 -0
- package/wire/lib/canonical/content-part.ts +38 -0
- package/wire/lib/canonical/count-tokens-capability.ts +138 -0
- package/wire/lib/canonical/encoding-select.ts +82 -0
- package/wire/lib/canonical/image-signature.ts +202 -0
- package/wire/lib/canonical/image-sse.ts +216 -0
- package/wire/lib/canonical/message.ts +43 -0
- package/wire/lib/canonical/token-estimate.ts +266 -0
- package/wire/lib/encrypted-content.ts +66 -0
- package/wire/lib/error-class.ts +154 -0
- package/wire/lib/error-envelope.ts +56 -0
- package/wire/lib/forwarded-headers.ts +180 -0
- package/wire/lib/provider-error-rules.ts +48 -0
- package/wire/lib/refusal.ts +38 -0
- package/wire/lib/responses-apply-patch.ts +104 -0
- package/wire/lib/responses-namespace.ts +53 -0
- package/wire/lib/secret-scrub.ts +101 -0
- package/wire/lib/streaming/accumulate.ts +221 -0
- package/wire/lib/streaming/finish-reason.ts +33 -0
- package/wire/lib/streaming/peek.ts +154 -0
- package/wire/lib/streaming/provider-decode.ts +259 -0
- package/wire/lib/streaming/response-stream.ts +71 -0
- package/wire/lib/streaming/sse.ts +699 -0
- package/wire/lib/streaming/strip-tool-calls.ts +95 -0
- package/wire/lib/streaming/upstream-error.ts +58 -0
- package/wire/lib/strip-subagent-isolation.ts +284 -0
- package/wire/lib/thinking-signature.ts +156 -0
- package/wire/lib/token-ruler/bpe-core.ts +426 -0
- package/wire/lib/token-ruler/index.ts +2 -0
- package/wire/lib/token-ruler/ruler.ts +79 -0
- package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
- package/wire/lib/token-ruler/vocab/claude.ts +22 -0
- package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
- package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
- package/wire/lib/tool-name-map.ts +271 -0
- package/wire/lib/tool-schema.ts +353 -0
- package/wire/lib/usage-observer.ts +125 -0
- package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
- package/wire/providers/anthropic/beta-headers.ts +165 -0
- package/wire/providers/anthropic/index.ts +10 -0
- package/wire/providers/anthropic/request.ts +607 -0
- package/wire/providers/anthropic/response.ts +155 -0
- package/wire/providers/anthropic/streaming.ts +315 -0
- package/wire/providers/anthropic/web-search-domains.ts +49 -0
- package/wire/providers/chatgpt/common.ts +136 -0
- package/wire/providers/chatgpt/index.ts +9 -0
- package/wire/providers/chatgpt/request.ts +959 -0
- package/wire/providers/chatgpt/streaming.ts +1007 -0
- package/wire/providers/chatgpt/web-search.ts +63 -0
- package/wire/providers/grok/web-search.ts +87 -0
- package/wire/providers/kimi/web-search.ts +145 -0
- package/wire/providers/upstream-deny.ts +355 -0
- package/wire/providers/upstream-request.ts +619 -0
package/tunnel/mux.ts
ADDED
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
import type { TFrame, TFrameType } from "./codec";
|
|
2
|
+
import {
|
|
3
|
+
decodeFrame,
|
|
4
|
+
decodeWindowPayload,
|
|
5
|
+
encodeFrame,
|
|
6
|
+
FRAME_TYPE,
|
|
7
|
+
INITIAL_STREAM_WINDOW_BYTES,
|
|
8
|
+
isConsumerStreamId,
|
|
9
|
+
MAX_PAYLOAD_BYTES,
|
|
10
|
+
STREAM_ID_CTRL,
|
|
11
|
+
STREAM_WINDOW_BOOST_BYTES,
|
|
12
|
+
TARGET_STREAM_WINDOW_BYTES,
|
|
13
|
+
WINDOW_REPLENISH_THRESHOLD,
|
|
14
|
+
windowPayload,
|
|
15
|
+
} from "./codec";
|
|
16
|
+
|
|
17
|
+
/** A host-owned message transport. `null` passed to `onBytes` means the peer died. */
|
|
18
|
+
export type TDuplex = {
|
|
19
|
+
readonly send: (bytes: Uint8Array) => void;
|
|
20
|
+
readonly onBytes: (callback: (bytes: Uint8Array | null) => void) => void;
|
|
21
|
+
readonly close: () => void;
|
|
22
|
+
/** Outbound-transport backpressure contract. */
|
|
23
|
+
readonly bufferedAmount: () => number;
|
|
24
|
+
readonly highWaterMark: number;
|
|
25
|
+
readonly onDrain: (callback: () => void) => () => void;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type TChannelSide = "consumer" | "daemon";
|
|
29
|
+
/**
|
|
30
|
+
* Local mux lifecycle detail. It deliberately remains open-vocabulary: hosts
|
|
31
|
+
* add transport-specific teardown context which is not part of the protocol.
|
|
32
|
+
*/
|
|
33
|
+
export type TMuxCloseReason = string;
|
|
34
|
+
/** Return `false` to defer receive-window credit until `stream.consume()`. */
|
|
35
|
+
export type TMuxEventHandler = (payload: Uint8Array) => unknown;
|
|
36
|
+
|
|
37
|
+
export type TMuxStream = {
|
|
38
|
+
readonly id: number;
|
|
39
|
+
readonly write: (bytes: Uint8Array) => Promise<void>;
|
|
40
|
+
readonly end: () => void;
|
|
41
|
+
readonly reset: (payload?: Uint8Array) => void;
|
|
42
|
+
readonly sendCtrl: (payload: Uint8Array) => void;
|
|
43
|
+
/** Replenish receive-window credit after a deferred DATA delivery is consumed. */
|
|
44
|
+
readonly consume: (bytes: number) => void;
|
|
45
|
+
readonly onData: (handler: TMuxEventHandler) => () => void;
|
|
46
|
+
readonly onEnd: (handler: () => void) => () => void;
|
|
47
|
+
readonly onReset: (handler: TMuxEventHandler) => () => void;
|
|
48
|
+
readonly onCtrl: (handler: TMuxEventHandler) => () => void;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type TMuxChannel = {
|
|
52
|
+
readonly openStream: (openPayload: Uint8Array) => TMuxStream;
|
|
53
|
+
readonly sendCtrl: (payload: Uint8Array) => void;
|
|
54
|
+
readonly onCtrl: (handler: TMuxEventHandler) => () => void;
|
|
55
|
+
readonly onStream: (
|
|
56
|
+
handler: (stream: TMuxStream, openPayload: Uint8Array) => void,
|
|
57
|
+
) => () => void;
|
|
58
|
+
readonly close: (reason?: TMuxCloseReason) => void;
|
|
59
|
+
readonly openStreamCount: () => number;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type TCreateChannelOptions = {
|
|
63
|
+
readonly duplex: TDuplex;
|
|
64
|
+
readonly side: TChannelSide;
|
|
65
|
+
readonly onStream?: (stream: TMuxStream, openPayload: Uint8Array) => void;
|
|
66
|
+
readonly onClose?: (reason: TMuxCloseReason) => void;
|
|
67
|
+
/**
|
|
68
|
+
* Sender-side DATA payload cap (bytes). Defaults to {@link MAX_PAYLOAD_BYTES}.
|
|
69
|
+
* Used to stay under transport message-size limits (e.g. SCTP `maxMessageSize`
|
|
70
|
+
* on a WebRTC data channel). Decode still accepts up to {@link MAX_PAYLOAD_BYTES}.
|
|
71
|
+
* Finite values below 1 clamp to 1; undefined / non-finite / oversized fall
|
|
72
|
+
* back to {@link MAX_PAYLOAD_BYTES}.
|
|
73
|
+
*/
|
|
74
|
+
readonly maxPayloadBytes?: number;
|
|
75
|
+
/**
|
|
76
|
+
* Maximum number of live peer-opened streams.
|
|
77
|
+
* Defaults to 256.
|
|
78
|
+
*/
|
|
79
|
+
readonly maxStreams?: number;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
type TPendingWrite = {
|
|
83
|
+
readonly bytes: Uint8Array;
|
|
84
|
+
offset: number;
|
|
85
|
+
readonly resolve: () => void;
|
|
86
|
+
readonly reject: (reason: Error) => void;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
type TStreamState = {
|
|
90
|
+
readonly id: number;
|
|
91
|
+
readonly dataHandlers: Set<TMuxEventHandler>;
|
|
92
|
+
readonly endHandlers: Set<() => void>;
|
|
93
|
+
readonly resetHandlers: Set<TMuxEventHandler>;
|
|
94
|
+
readonly ctrlHandlers: Set<TMuxEventHandler>;
|
|
95
|
+
readonly pending: TPendingWrite[];
|
|
96
|
+
sendCredit: number;
|
|
97
|
+
receiveCredit: number;
|
|
98
|
+
receivedUnconsumed: number;
|
|
99
|
+
consumedSinceWindow: number;
|
|
100
|
+
localEnded: boolean;
|
|
101
|
+
remoteEnded: boolean;
|
|
102
|
+
endRequested: boolean;
|
|
103
|
+
reset: boolean;
|
|
104
|
+
/** True only when the peer initiated the OPEN for this stream. */
|
|
105
|
+
openedByPeer: boolean;
|
|
106
|
+
stream: TMuxStream | null;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const on = <T>(handlers: Set<T>, handler: T): (() => void) => {
|
|
110
|
+
handlers.add(handler);
|
|
111
|
+
return () => handlers.delete(handler);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const emit = <T>(
|
|
115
|
+
handlers: ReadonlySet<T>,
|
|
116
|
+
invoke: (handler: T) => void,
|
|
117
|
+
): void => {
|
|
118
|
+
for (const handler of handlers) {
|
|
119
|
+
try {
|
|
120
|
+
invoke(handler);
|
|
121
|
+
} catch {
|
|
122
|
+
// User event handlers cannot destabilize the wire state machine.
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const resetError = (payload: Uint8Array | undefined): Error => {
|
|
128
|
+
const detail =
|
|
129
|
+
payload === undefined || payload.byteLength === 0
|
|
130
|
+
? "stream reset"
|
|
131
|
+
: "stream reset by peer";
|
|
132
|
+
return new Error(detail);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Stateful, sans-I/O stream multiplexer. A WebSocket host gives it one complete
|
|
137
|
+
* binary message at a time; no timers, DOM APIs, Bun APIs, or transport policy
|
|
138
|
+
* live here.
|
|
139
|
+
*/
|
|
140
|
+
export const createChannel = (options: TCreateChannelOptions): TMuxChannel => {
|
|
141
|
+
const streams = new Map<number, TStreamState>();
|
|
142
|
+
const channelCtrlHandlers = new Set<TMuxEventHandler>();
|
|
143
|
+
const streamHandlers = new Set<
|
|
144
|
+
(stream: TMuxStream, openPayload: Uint8Array) => void
|
|
145
|
+
>();
|
|
146
|
+
if (options.onStream !== undefined) streamHandlers.add(options.onStream);
|
|
147
|
+
let nextStreamId = options.side === "consumer" ? 1 : 2;
|
|
148
|
+
let closed = false;
|
|
149
|
+
// Track how many peer-opened streams are currently live on this side.
|
|
150
|
+
let peerOpenStreams = 0;
|
|
151
|
+
// Sender-only chunk size. Keep decode-side MAX_PAYLOAD_BYTES as the wire max.
|
|
152
|
+
// Floor before validating so fractional values like 0.9 don't become 0 and
|
|
153
|
+
// permanently stall drain() (Math.min(..., 0) never advances pending writes).
|
|
154
|
+
const maxPayloadBytes = (() => {
|
|
155
|
+
const requested = options.maxPayloadBytes;
|
|
156
|
+
if (requested === undefined) return MAX_PAYLOAD_BYTES;
|
|
157
|
+
if (!Number.isFinite(requested)) return MAX_PAYLOAD_BYTES;
|
|
158
|
+
const floored = Math.floor(requested);
|
|
159
|
+
// Finite values below 1 clamp to 1 so drain() still advances; only
|
|
160
|
+
// undefined / non-finite / oversized fall back to MAX_PAYLOAD_BYTES.
|
|
161
|
+
if (floored > MAX_PAYLOAD_BYTES) return MAX_PAYLOAD_BYTES;
|
|
162
|
+
if (floored < 1) return 1;
|
|
163
|
+
return floored;
|
|
164
|
+
})();
|
|
165
|
+
|
|
166
|
+
const maxStreams = (() => {
|
|
167
|
+
const requested = options.maxStreams;
|
|
168
|
+
if (requested === undefined) return 256;
|
|
169
|
+
if (!Number.isFinite(requested)) return 256;
|
|
170
|
+
const floored = Math.floor(requested);
|
|
171
|
+
if (floored < 1) return 1;
|
|
172
|
+
return floored;
|
|
173
|
+
})();
|
|
174
|
+
|
|
175
|
+
const sendResetForExcessOpen = (streamId: number): void => {
|
|
176
|
+
send(FRAME_TYPE.reset, streamId, new Uint8Array());
|
|
177
|
+
};
|
|
178
|
+
const send = (
|
|
179
|
+
type: TFrameType,
|
|
180
|
+
streamId: number,
|
|
181
|
+
payload: Uint8Array,
|
|
182
|
+
): void => {
|
|
183
|
+
if (closed) return;
|
|
184
|
+
options.duplex.send(encodeFrame({ type, streamId, payload }));
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Advertise {@link TARGET_STREAM_WINDOW_BYTES} without changing the frozen
|
|
189
|
+
* implicit OPEN window. Old peers just add the WINDOW delta to sendCredit.
|
|
190
|
+
*/
|
|
191
|
+
const grantWindowBoost = (streamId: number): void => {
|
|
192
|
+
if (STREAM_WINDOW_BOOST_BYTES <= 0) return;
|
|
193
|
+
send(FRAME_TYPE.window, streamId, windowPayload(STREAM_WINDOW_BOOST_BYTES));
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const retireStreamState = (state: TStreamState): void => {
|
|
197
|
+
const current = streams.get(state.id);
|
|
198
|
+
if (current !== state) return;
|
|
199
|
+
streams.delete(state.id);
|
|
200
|
+
if (state.openedByPeer) {
|
|
201
|
+
peerOpenStreams -= 1;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const localReset = (state: TStreamState, payload?: Uint8Array): void => {
|
|
206
|
+
if (state.reset) return;
|
|
207
|
+
state.reset = true;
|
|
208
|
+
for (const pending of state.pending.splice(0)) {
|
|
209
|
+
pending.reject(resetError(payload));
|
|
210
|
+
}
|
|
211
|
+
retireStreamState(state);
|
|
212
|
+
emit(state.resetHandlers, (handler) =>
|
|
213
|
+
handler(payload ?? new Uint8Array()),
|
|
214
|
+
);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
let removeDrainListener: (() => void) | undefined;
|
|
218
|
+
const close = (reason: TMuxCloseReason = "done"): void => {
|
|
219
|
+
if (closed) return;
|
|
220
|
+
closed = true;
|
|
221
|
+
removeDrainListener?.();
|
|
222
|
+
for (const state of [...streams.values()]) localReset(state);
|
|
223
|
+
try {
|
|
224
|
+
options.duplex.close();
|
|
225
|
+
} catch {
|
|
226
|
+
// A dead transport is already closed from the mux's perspective.
|
|
227
|
+
}
|
|
228
|
+
try {
|
|
229
|
+
options.onClose?.(reason);
|
|
230
|
+
} catch {
|
|
231
|
+
// close is terminal regardless of observer failures.
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const protocolError = (): void => close("protocol_error");
|
|
236
|
+
|
|
237
|
+
const tryRetireStreamState = (state: TStreamState): void => {
|
|
238
|
+
if (!state.localEnded || !state.remoteEnded || state.pending.length > 0)
|
|
239
|
+
return;
|
|
240
|
+
retireStreamState(state);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const sendEndIfReady = (state: TStreamState): void => {
|
|
244
|
+
if (
|
|
245
|
+
!state.endRequested ||
|
|
246
|
+
state.localEnded ||
|
|
247
|
+
state.pending.length > 0 ||
|
|
248
|
+
state.reset ||
|
|
249
|
+
closed
|
|
250
|
+
)
|
|
251
|
+
return;
|
|
252
|
+
state.localEnded = true;
|
|
253
|
+
send(FRAME_TYPE.end, state.id, new Uint8Array());
|
|
254
|
+
tryRetireStreamState(state);
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const transportBackpressured = (): boolean =>
|
|
258
|
+
options.duplex.bufferedAmount() >= options.duplex.highWaterMark;
|
|
259
|
+
|
|
260
|
+
let draining = false;
|
|
261
|
+
let roundRobinStart = 0;
|
|
262
|
+
const stopDrainListener = (): void => {
|
|
263
|
+
removeDrainListener?.();
|
|
264
|
+
removeDrainListener = undefined;
|
|
265
|
+
};
|
|
266
|
+
const waitForDrain = (): void => {
|
|
267
|
+
if (removeDrainListener !== undefined || closed) return;
|
|
268
|
+
removeDrainListener = options.duplex.onDrain(() => {
|
|
269
|
+
stopDrainListener();
|
|
270
|
+
drainPendingWrites();
|
|
271
|
+
});
|
|
272
|
+
};
|
|
273
|
+
const drainPendingWrites = (): void => {
|
|
274
|
+
if (draining || closed) return;
|
|
275
|
+
if (transportBackpressured()) {
|
|
276
|
+
waitForDrain();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
draining = true;
|
|
280
|
+
try {
|
|
281
|
+
while (!closed && !transportBackpressured()) {
|
|
282
|
+
const pendingStates = [...streams.values()].filter(
|
|
283
|
+
(state) =>
|
|
284
|
+
!state.reset && state.sendCredit > 0 && state.pending.length > 0,
|
|
285
|
+
);
|
|
286
|
+
if (pendingStates.length === 0) break;
|
|
287
|
+
const start = roundRobinStart % pendingStates.length;
|
|
288
|
+
let sent = false;
|
|
289
|
+
let lastSentIndex: number | null = null;
|
|
290
|
+
for (let offset = 0; offset < pendingStates.length; offset += 1) {
|
|
291
|
+
if (closed || transportBackpressured()) break;
|
|
292
|
+
const index = (start + offset) % pendingStates.length;
|
|
293
|
+
const state = pendingStates[index];
|
|
294
|
+
const pending = state.pending[0];
|
|
295
|
+
const remaining = pending.bytes.byteLength - pending.offset;
|
|
296
|
+
const size = Math.min(remaining, state.sendCredit, maxPayloadBytes);
|
|
297
|
+
if (size <= 0) continue;
|
|
298
|
+
send(
|
|
299
|
+
FRAME_TYPE.data,
|
|
300
|
+
state.id,
|
|
301
|
+
pending.bytes.subarray(pending.offset, pending.offset + size),
|
|
302
|
+
);
|
|
303
|
+
pending.offset += size;
|
|
304
|
+
state.sendCredit -= size;
|
|
305
|
+
if (pending.offset === pending.bytes.byteLength) {
|
|
306
|
+
state.pending.shift();
|
|
307
|
+
pending.resolve();
|
|
308
|
+
}
|
|
309
|
+
sendEndIfReady(state);
|
|
310
|
+
sent = true;
|
|
311
|
+
lastSentIndex = index;
|
|
312
|
+
}
|
|
313
|
+
// A complete pass keeps its logical head (A,B,A,B). If the transport
|
|
314
|
+
// stopped us mid-pass, resume after the last stream that sent so later
|
|
315
|
+
// streams cannot be starved by repeated drain events.
|
|
316
|
+
roundRobinStart =
|
|
317
|
+
transportBackpressured() && lastSentIndex !== null
|
|
318
|
+
? (lastSentIndex + 1) % pendingStates.length
|
|
319
|
+
: start;
|
|
320
|
+
if (!sent) break;
|
|
321
|
+
}
|
|
322
|
+
} finally {
|
|
323
|
+
draining = false;
|
|
324
|
+
if (transportBackpressured()) waitForDrain();
|
|
325
|
+
else stopDrainListener();
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
const drain = (_state: TStreamState): void => drainPendingWrites();
|
|
330
|
+
|
|
331
|
+
const makeState = (id: number, openedByPeer: boolean): TStreamState => ({
|
|
332
|
+
id,
|
|
333
|
+
dataHandlers: new Set(),
|
|
334
|
+
endHandlers: new Set(),
|
|
335
|
+
resetHandlers: new Set(),
|
|
336
|
+
ctrlHandlers: new Set(),
|
|
337
|
+
pending: [],
|
|
338
|
+
// Sender starts at the frozen implicit OPEN window; peer WINDOW frames
|
|
339
|
+
// (including the post-OPEN boost) raise this.
|
|
340
|
+
sendCredit: INITIAL_STREAM_WINDOW_BYTES,
|
|
341
|
+
// Receiver accepts the boosted target immediately and advertises it with
|
|
342
|
+
// grantWindowBoost after OPEN so old peers stay interoperable.
|
|
343
|
+
receiveCredit: TARGET_STREAM_WINDOW_BYTES,
|
|
344
|
+
receivedUnconsumed: 0,
|
|
345
|
+
consumedSinceWindow: 0,
|
|
346
|
+
localEnded: false,
|
|
347
|
+
remoteEnded: false,
|
|
348
|
+
endRequested: false,
|
|
349
|
+
reset: false,
|
|
350
|
+
openedByPeer,
|
|
351
|
+
stream: null,
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
const makeStream = (state: TStreamState): TMuxStream => {
|
|
355
|
+
const stream: TMuxStream = {
|
|
356
|
+
id: state.id,
|
|
357
|
+
write: (bytes) => {
|
|
358
|
+
if (state.localEnded || state.endRequested)
|
|
359
|
+
return Promise.reject(new Error("write after end"));
|
|
360
|
+
if (state.reset || closed)
|
|
361
|
+
return Promise.reject(new Error("stream is closed"));
|
|
362
|
+
if (bytes.byteLength === 0) return Promise.resolve();
|
|
363
|
+
// Send at most one immediate frame. The remainder joins the fair
|
|
364
|
+
// round-robin queue, so a large first writer cannot monopolize the
|
|
365
|
+
// transport before another stream gets a turn.
|
|
366
|
+
let offset = 0;
|
|
367
|
+
if (
|
|
368
|
+
state.pending.length === 0 &&
|
|
369
|
+
state.sendCredit > 0 &&
|
|
370
|
+
!transportBackpressured()
|
|
371
|
+
) {
|
|
372
|
+
const size = Math.min(
|
|
373
|
+
bytes.byteLength,
|
|
374
|
+
state.sendCredit,
|
|
375
|
+
maxPayloadBytes,
|
|
376
|
+
);
|
|
377
|
+
try {
|
|
378
|
+
send(FRAME_TYPE.data, state.id, bytes.subarray(0, size));
|
|
379
|
+
} catch (error) {
|
|
380
|
+
return Promise.reject(
|
|
381
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
offset = size;
|
|
385
|
+
state.sendCredit -= size;
|
|
386
|
+
if (state.reset || closed)
|
|
387
|
+
return Promise.reject(new Error("stream is closed"));
|
|
388
|
+
if (offset === bytes.byteLength) return Promise.resolve();
|
|
389
|
+
}
|
|
390
|
+
const queued = new Uint8Array(bytes.subarray(offset));
|
|
391
|
+
return new Promise<void>((resolve, reject) => {
|
|
392
|
+
state.pending.push({ bytes: queued, offset: 0, resolve, reject });
|
|
393
|
+
try {
|
|
394
|
+
drain(state);
|
|
395
|
+
} catch (error) {
|
|
396
|
+
const index = state.pending.findIndex(
|
|
397
|
+
(item) => item.reject === reject,
|
|
398
|
+
);
|
|
399
|
+
if (index >= 0) state.pending.splice(index, 1);
|
|
400
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
401
|
+
}
|
|
402
|
+
});
|
|
403
|
+
},
|
|
404
|
+
end: () => {
|
|
405
|
+
if (state.localEnded || state.endRequested || state.reset || closed)
|
|
406
|
+
return;
|
|
407
|
+
state.endRequested = true;
|
|
408
|
+
sendEndIfReady(state);
|
|
409
|
+
},
|
|
410
|
+
reset: (payload = new Uint8Array()) => {
|
|
411
|
+
if (state.reset || closed) return;
|
|
412
|
+
send(FRAME_TYPE.reset, state.id, payload);
|
|
413
|
+
localReset(state, payload);
|
|
414
|
+
},
|
|
415
|
+
sendCtrl: (payload) => {
|
|
416
|
+
if (!state.reset && !closed) send(FRAME_TYPE.ctrl, state.id, payload);
|
|
417
|
+
},
|
|
418
|
+
consume: (bytes) => {
|
|
419
|
+
if (state.reset || closed) return;
|
|
420
|
+
if (!Number.isSafeInteger(bytes) || bytes < 0) {
|
|
421
|
+
throw new RangeError(
|
|
422
|
+
"consumed byte count must be a non-negative integer",
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
if (bytes > state.receivedUnconsumed) {
|
|
426
|
+
throw new RangeError("cannot consume more bytes than received");
|
|
427
|
+
}
|
|
428
|
+
state.receivedUnconsumed -= bytes;
|
|
429
|
+
state.consumedSinceWindow += bytes;
|
|
430
|
+
if (state.consumedSinceWindow < WINDOW_REPLENISH_THRESHOLD) return;
|
|
431
|
+
const delta = state.consumedSinceWindow;
|
|
432
|
+
state.receiveCredit += delta;
|
|
433
|
+
state.consumedSinceWindow = 0;
|
|
434
|
+
send(FRAME_TYPE.window, state.id, windowPayload(delta));
|
|
435
|
+
},
|
|
436
|
+
onData: (handler) => on(state.dataHandlers, handler),
|
|
437
|
+
onEnd: (handler) => on(state.endHandlers, handler),
|
|
438
|
+
onReset: (handler) => on(state.resetHandlers, handler),
|
|
439
|
+
onCtrl: (handler) => on(state.ctrlHandlers, handler),
|
|
440
|
+
};
|
|
441
|
+
state.stream = stream;
|
|
442
|
+
return stream;
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
const openStream = (openPayload: Uint8Array): TMuxStream => {
|
|
446
|
+
if (closed) throw new Error("channel is closed");
|
|
447
|
+
if (nextStreamId > 0xffff_ffff) throw new Error("stream ids exhausted");
|
|
448
|
+
const id = nextStreamId;
|
|
449
|
+
nextStreamId += 2;
|
|
450
|
+
const state = makeState(id, false);
|
|
451
|
+
const stream = makeStream(state);
|
|
452
|
+
streams.set(id, state);
|
|
453
|
+
send(FRAME_TYPE.open, id, openPayload);
|
|
454
|
+
grantWindowBoost(id);
|
|
455
|
+
return stream;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
const onFrame = (frame: TFrame): void => {
|
|
459
|
+
if (closed) return;
|
|
460
|
+
if (frame.type === FRAME_TYPE.ctrl && frame.streamId === STREAM_ID_CTRL) {
|
|
461
|
+
emit(channelCtrlHandlers, (handler) => handler(frame.payload));
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
if (frame.streamId === STREAM_ID_CTRL) {
|
|
465
|
+
protocolError();
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (frame.type === FRAME_TYPE.open) {
|
|
470
|
+
const expectedConsumer = options.side === "daemon";
|
|
471
|
+
if (
|
|
472
|
+
isConsumerStreamId(frame.streamId) !== expectedConsumer ||
|
|
473
|
+
streams.has(frame.streamId)
|
|
474
|
+
) {
|
|
475
|
+
protocolError();
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
if (peerOpenStreams >= maxStreams) {
|
|
479
|
+
sendResetForExcessOpen(frame.streamId);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
const state = makeState(frame.streamId, true);
|
|
483
|
+
const stream = makeStream(state);
|
|
484
|
+
peerOpenStreams += 1;
|
|
485
|
+
streams.set(frame.streamId, state);
|
|
486
|
+
grantWindowBoost(frame.streamId);
|
|
487
|
+
emit(streamHandlers, (handler) => handler(stream, frame.payload));
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const state = streams.get(frame.streamId);
|
|
492
|
+
if (state === undefined) {
|
|
493
|
+
// RESET removes state immediately; delayed frames must never recreate it.
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
switch (frame.type) {
|
|
498
|
+
case FRAME_TYPE.data: {
|
|
499
|
+
if (
|
|
500
|
+
state.remoteEnded ||
|
|
501
|
+
frame.payload.byteLength > state.receiveCredit
|
|
502
|
+
) {
|
|
503
|
+
protocolError();
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
state.receiveCredit -= frame.payload.byteLength;
|
|
507
|
+
state.receivedUnconsumed += frame.payload.byteLength;
|
|
508
|
+
let deferConsume = false;
|
|
509
|
+
emit(state.dataHandlers, (handler) => {
|
|
510
|
+
if (handler(frame.payload) === false) deferConsume = true;
|
|
511
|
+
});
|
|
512
|
+
if (!deferConsume) {
|
|
513
|
+
const unconsumed = Math.min(
|
|
514
|
+
frame.payload.byteLength,
|
|
515
|
+
state.receivedUnconsumed,
|
|
516
|
+
);
|
|
517
|
+
if (unconsumed > 0) state.stream?.consume(unconsumed);
|
|
518
|
+
}
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
case FRAME_TYPE.end:
|
|
522
|
+
if (frame.payload.byteLength !== 0 || state.remoteEnded) {
|
|
523
|
+
protocolError();
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
state.remoteEnded = true;
|
|
527
|
+
emit(state.endHandlers, (handler) => handler());
|
|
528
|
+
if (state.localEnded && state.pending.length === 0) {
|
|
529
|
+
retireStreamState(state);
|
|
530
|
+
}
|
|
531
|
+
return;
|
|
532
|
+
case FRAME_TYPE.reset:
|
|
533
|
+
localReset(state, frame.payload);
|
|
534
|
+
return;
|
|
535
|
+
case FRAME_TYPE.window: {
|
|
536
|
+
const delta = decodeWindowPayload(frame.payload);
|
|
537
|
+
if (delta === null || delta > 0x7fff_ffff) {
|
|
538
|
+
protocolError();
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
state.sendCredit += delta;
|
|
542
|
+
drain(state);
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
case FRAME_TYPE.ctrl:
|
|
546
|
+
emit(state.ctrlHandlers, (handler) => handler(frame.payload));
|
|
547
|
+
return;
|
|
548
|
+
default:
|
|
549
|
+
protocolError();
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
options.duplex.onBytes((bytes) => {
|
|
555
|
+
try {
|
|
556
|
+
if (bytes === null) {
|
|
557
|
+
close("peer_gone");
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const decoded = decodeFrame(bytes);
|
|
561
|
+
if (!decoded.ok) {
|
|
562
|
+
protocolError();
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
onFrame(decoded.frame);
|
|
566
|
+
} catch {
|
|
567
|
+
// The public onBytes boundary must never throw into the WebSocket host.
|
|
568
|
+
protocolError();
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
return {
|
|
573
|
+
openStream,
|
|
574
|
+
sendCtrl: (payload) => send(FRAME_TYPE.ctrl, STREAM_ID_CTRL, payload),
|
|
575
|
+
onCtrl: (handler) => on(channelCtrlHandlers, handler),
|
|
576
|
+
onStream: (handler) => on(streamHandlers, handler),
|
|
577
|
+
close,
|
|
578
|
+
openStreamCount: () => streams.size,
|
|
579
|
+
};
|
|
580
|
+
};
|