@exhumer/signalr-client 1.0.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.
- package/LICENSE.md +25 -0
- package/README.md +460 -0
- package/dist/index.cjs +2787 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1079 -0
- package/dist/index.d.ts +1079 -0
- package/dist/index.js +2706 -0
- package/dist/index.js.map +1 -0
- package/package.json +79 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,1079 @@
|
|
|
1
|
+
import { Dispatcher } from 'undici';
|
|
2
|
+
export { Dispatcher } from 'undici';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
import { CookieJar } from 'tough-cookie';
|
|
5
|
+
export { CookieJar } from 'tough-cookie';
|
|
6
|
+
import { EventEmitter } from 'node:events';
|
|
7
|
+
export { CookieAgent, cookie } from '@exhumer/undici-cookie-agent';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* constants.ts
|
|
11
|
+
*
|
|
12
|
+
* All SignalR protocol constants, enumerations, and timing defaults.
|
|
13
|
+
* Uses `as const satisfies` (TS 4.9+) so values carry their narrow literal
|
|
14
|
+
* types while being verified against a constraint at compile time.
|
|
15
|
+
*/
|
|
16
|
+
declare const MessageType: Readonly<{
|
|
17
|
+
readonly Invocation: 1;
|
|
18
|
+
readonly StreamItem: 2;
|
|
19
|
+
readonly Completion: 3;
|
|
20
|
+
readonly StreamInvocation: 4;
|
|
21
|
+
readonly CancelInvocation: 5;
|
|
22
|
+
readonly Ping: 6;
|
|
23
|
+
readonly Close: 7;
|
|
24
|
+
}>;
|
|
25
|
+
/** Union of all valid message type numbers. */
|
|
26
|
+
type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
|
|
27
|
+
declare const HubConnectionState: Readonly<{
|
|
28
|
+
readonly Disconnected: "Disconnected";
|
|
29
|
+
readonly Connecting: "Connecting";
|
|
30
|
+
readonly Connected: "Connected";
|
|
31
|
+
readonly Disconnecting: "Disconnecting";
|
|
32
|
+
readonly Reconnecting: "Reconnecting";
|
|
33
|
+
}>;
|
|
34
|
+
/** Union of all valid connection state strings. */
|
|
35
|
+
type HubConnectionState = (typeof HubConnectionState)[keyof typeof HubConnectionState];
|
|
36
|
+
declare const LogLevel: {
|
|
37
|
+
readonly Trace: 0;
|
|
38
|
+
readonly Debug: 1;
|
|
39
|
+
readonly Information: 2;
|
|
40
|
+
readonly Warning: 3;
|
|
41
|
+
readonly Error: 4;
|
|
42
|
+
readonly Critical: 5;
|
|
43
|
+
readonly None: 6;
|
|
44
|
+
};
|
|
45
|
+
type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
|
|
46
|
+
declare const HttpTransportType: {
|
|
47
|
+
readonly None: 0;
|
|
48
|
+
readonly WebSockets: 1;
|
|
49
|
+
readonly ServerSentEvents: 2;
|
|
50
|
+
readonly LongPolling: 4;
|
|
51
|
+
};
|
|
52
|
+
/** Bitmask of enabled transports - values can be OR-ed together. */
|
|
53
|
+
type HttpTransportType = (typeof HttpTransportType)[keyof typeof HttpTransportType];
|
|
54
|
+
declare const TransferFormat: {
|
|
55
|
+
readonly Text: 1;
|
|
56
|
+
readonly Binary: 2;
|
|
57
|
+
};
|
|
58
|
+
type TransferFormat = (typeof TransferFormat)[keyof typeof TransferFormat];
|
|
59
|
+
/** ASCII 30 - the byte that terminates every SignalR JSON message. */
|
|
60
|
+
declare const RECORD_SEPARATOR: "\u001E";
|
|
61
|
+
/** Negotiate version we advertise to the server. */
|
|
62
|
+
declare const NEGOTIATE_VERSION: 1;
|
|
63
|
+
declare const DEFAULT_TIMEOUT_IN_MS: 30000;
|
|
64
|
+
declare const DEFAULT_PING_INTERVAL_IN_MS: 15000;
|
|
65
|
+
declare const DEFAULT_SERVER_TIMEOUT_IN_MS: 30000;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* http-client.ts
|
|
69
|
+
*
|
|
70
|
+
* Five HTTP client implementations backed by undici, each wrapping a different
|
|
71
|
+
* undici primitive. All five satisfy the `IHttpClient` contract so any one
|
|
72
|
+
* can be used wherever an HTTP client is required (negotiate, SSE, polling…).
|
|
73
|
+
*
|
|
74
|
+
* ┌────────────────────┬───────────────────────────────────────────────────┐
|
|
75
|
+
* │ Class │ undici primitive │
|
|
76
|
+
* ├────────────────────┼───────────────────────────────────────────────────┤
|
|
77
|
+
* │ RequestHttpClient │ undici.request() │
|
|
78
|
+
* │ FetchHttpClient │ undici.fetch() (WHATWG-compatible) │
|
|
79
|
+
* │ StreamHttpClient │ undici.stream() (factory-based streaming) │
|
|
80
|
+
* │ PipelineHttpClient │ undici.pipeline() (Duplex pipe chain) │
|
|
81
|
+
* │ DispatchHttpClient │ Dispatcher#dispatch (low-level handler interface) ← DEFAULT │
|
|
82
|
+
* └────────────────────┴───────────────────────────────────────────────────┘
|
|
83
|
+
*
|
|
84
|
+
* Dispatcher (session-level):
|
|
85
|
+
* Every client accepts an optional `dispatcher` option. Pass an undici
|
|
86
|
+
* `Agent`, `Pool`, `Client`, `ProxyAgent`, etc. to customise connection
|
|
87
|
+
* pooling, proxy routing, TLS, or any other transport-level behaviour for
|
|
88
|
+
* the entire client session. When omitted, undici's process-global
|
|
89
|
+
* default agent is used (`getGlobalDispatcher()`).
|
|
90
|
+
*
|
|
91
|
+
* HttpClient:
|
|
92
|
+
* An alias for `DispatchHttpClient` so existing code that imported
|
|
93
|
+
* `HttpClient` continues to work without changes.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
|
|
97
|
+
/**
|
|
98
|
+
* Per-request options accepted by every client method.
|
|
99
|
+
* These mirror the previous `node:http`-based API surface exactly so callers
|
|
100
|
+
* do not need to be changed when switching client implementations.
|
|
101
|
+
*/
|
|
102
|
+
interface RequestOptions {
|
|
103
|
+
/** Per-request headers merged on top of the instance-level defaults. */
|
|
104
|
+
readonly headers?: Record<string, string>;
|
|
105
|
+
/**
|
|
106
|
+
* Request body.
|
|
107
|
+
* Plain objects are serialised to JSON automatically.
|
|
108
|
+
* Strings are sent as-is (set Content-Type explicitly if needed).
|
|
109
|
+
*/
|
|
110
|
+
readonly body?: string | Record<string, unknown> | null;
|
|
111
|
+
/** Override the session-level timeout for this single request (ms). */
|
|
112
|
+
readonly timeout?: number;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Session-level options shared by all client constructors.
|
|
116
|
+
*/
|
|
117
|
+
interface HttpClientOptions {
|
|
118
|
+
/** Headers sent with every request (merged before per-request headers). */
|
|
119
|
+
readonly headers?: Record<string, string>;
|
|
120
|
+
/** Default timeout in milliseconds applied to every request (default: 30 000). */
|
|
121
|
+
readonly timeout?: number;
|
|
122
|
+
/**
|
|
123
|
+
* undici Dispatcher to use for the entire session.
|
|
124
|
+
*
|
|
125
|
+
* You can pass any `Dispatcher` subclass:
|
|
126
|
+
* - `new undici.Agent(opts)` - connection-pool tuning
|
|
127
|
+
* - `new undici.Pool(origin, opts)` - per-origin pool
|
|
128
|
+
* - `new undici.Client(origin, opts)` - single connection
|
|
129
|
+
* - `new undici.ProxyAgent(proxyUrl)` - HTTP proxy
|
|
130
|
+
* - `new undici.MockAgent()` - in-process mocking
|
|
131
|
+
*
|
|
132
|
+
* When omitted the process-global dispatcher (`getGlobalDispatcher()`)
|
|
133
|
+
* is used, which is undici's default `Agent`.
|
|
134
|
+
*/
|
|
135
|
+
readonly dispatcher?: Dispatcher;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Holds the session-level options and exposes the convenience wrappers
|
|
139
|
+
* (`get`, `post`, `delete`) that delegate to the abstract `request` method.
|
|
140
|
+
*
|
|
141
|
+
* Concrete subclasses only need to implement `request()` and `stream()`.
|
|
142
|
+
*/
|
|
143
|
+
declare abstract class BaseUndiciClient implements IHttpClient {
|
|
144
|
+
/** Default headers merged into every outgoing request. */
|
|
145
|
+
protected readonly defaultHeaders: Record<string, string>;
|
|
146
|
+
/** Session-level request timeout in milliseconds. */
|
|
147
|
+
protected readonly defaultTimeout: number;
|
|
148
|
+
/**
|
|
149
|
+
* Shared undici Dispatcher. May be an Agent, Pool, Client, ProxyAgent, etc.
|
|
150
|
+
* Defaults to undici's process-global agent when not specified.
|
|
151
|
+
*/
|
|
152
|
+
protected readonly dispatcher: Dispatcher;
|
|
153
|
+
constructor(options?: HttpClientOptions);
|
|
154
|
+
get(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
155
|
+
post(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
156
|
+
delete(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
157
|
+
abstract request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
158
|
+
abstract stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* HTTP client backed by `undici.request()`.
|
|
162
|
+
*
|
|
163
|
+
* `undici.request` is the recommended high-level API for most use-cases.
|
|
164
|
+
* It returns a Promise that resolves with `{ statusCode, headers, body }`
|
|
165
|
+
* where `body` is an undici `BodyReadable` (a Node `Readable` subclass).
|
|
166
|
+
*
|
|
167
|
+
* For buffered responses (`request()`) we call `body.text()` to collect the
|
|
168
|
+
* full response. For streaming responses (`stream()`) we return `body`
|
|
169
|
+
* directly as the `StreamResult.body` Readable, giving the caller zero-copy
|
|
170
|
+
* access to the response stream.
|
|
171
|
+
*
|
|
172
|
+
*/
|
|
173
|
+
declare class RequestHttpClient extends BaseUndiciClient {
|
|
174
|
+
/**
|
|
175
|
+
* Buffered request - collects the entire response body as a string.
|
|
176
|
+
*
|
|
177
|
+
* Timeout is implemented via undici's built-in `headersTimeout` /
|
|
178
|
+
* `bodyTimeout` options rather than an external `setTimeout`.
|
|
179
|
+
*/
|
|
180
|
+
request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
181
|
+
/**
|
|
182
|
+
* Streaming request - returns the response body `Readable` without
|
|
183
|
+
* buffering. Used by SSE and long-polling transports.
|
|
184
|
+
*
|
|
185
|
+
* The `abort()` method destroys the underlying socket and the body stream.
|
|
186
|
+
*/
|
|
187
|
+
stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* HTTP client backed by `undici.fetch()`.
|
|
191
|
+
*
|
|
192
|
+
* `undici.fetch` is undici's WHATWG-compatible Fetch implementation.
|
|
193
|
+
* It accepts the same options as the browser `fetch()` API, plus an
|
|
194
|
+
* undici-specific `dispatcher` field for custom transport control.
|
|
195
|
+
*
|
|
196
|
+
* Useful when you need browser API compatibility (e.g., tests that run in
|
|
197
|
+
* both Node and a browser-like environment) or when consuming WHATWG `Response`
|
|
198
|
+
* helpers such as `res.json()` / `res.formData()`.
|
|
199
|
+
*
|
|
200
|
+
* For `stream()` we convert the WHATWG `ReadableStream<Uint8Array>` body to a
|
|
201
|
+
* Node.js `Readable` via `Readable.fromWeb()` (Node 17+).
|
|
202
|
+
*/
|
|
203
|
+
declare class FetchHttpClient extends BaseUndiciClient {
|
|
204
|
+
request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
205
|
+
stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* HTTP client backed by `undici.stream()`.
|
|
209
|
+
*
|
|
210
|
+
* `undici.stream` uses a *factory callback* pattern: you supply a function
|
|
211
|
+
* that receives `{ statusCode, headers, body }` and must return a
|
|
212
|
+
* `Writable` to which the response body is piped.
|
|
213
|
+
*
|
|
214
|
+
* This makes it ideal when you want to pipe response bytes directly to a
|
|
215
|
+
* destination (e.g., a file, a transform, or a compression stream) without
|
|
216
|
+
* ever holding the entire response in memory.
|
|
217
|
+
*
|
|
218
|
+
* For buffered `request()` we use a `PassThrough` as the sink and collect
|
|
219
|
+
* its output. For streaming `stream()` we expose the PassThrough's
|
|
220
|
+
* readable side as `StreamResult.body`.
|
|
221
|
+
*/
|
|
222
|
+
declare class StreamHttpClient extends BaseUndiciClient {
|
|
223
|
+
request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
224
|
+
stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* HTTP client backed by `undici.pipeline()`.
|
|
228
|
+
*
|
|
229
|
+
* `undici.pipeline` returns a `stream.Duplex`:
|
|
230
|
+
* - **Writable side**: the request body (write / pipe into it).
|
|
231
|
+
* - **Readable side**: whatever your handler function *returns*.
|
|
232
|
+
*
|
|
233
|
+
* The handler receives `{ statusCode, headers, body }` and must return a
|
|
234
|
+
* `Readable`. Whatever that readable emits becomes the duplex's output.
|
|
235
|
+
*
|
|
236
|
+
* Typical use: response transformation pipelines
|
|
237
|
+
* `pipeline(url, opts, ({ body }) => body.pipe(gunzip()))`
|
|
238
|
+
*
|
|
239
|
+
* For `request()` we return the raw body from the handler, collect the duplex
|
|
240
|
+
* output into a buffer, and resolve with the buffered string.
|
|
241
|
+
*
|
|
242
|
+
* For `stream()` we return the raw body from the handler and give the caller
|
|
243
|
+
* the duplex's readable side directly - zero extra buffering.
|
|
244
|
+
*/
|
|
245
|
+
declare class PipelineHttpClient extends BaseUndiciClient {
|
|
246
|
+
request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
247
|
+
stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* HTTP client backed by `Dispatcher#dispatch()`.
|
|
251
|
+
*
|
|
252
|
+
* `dispatch` is the *lowest-level* undici API. It exposes the full
|
|
253
|
+
* request/response lifecycle as a handler interface (undici 8+):
|
|
254
|
+
*
|
|
255
|
+
* onRequestStart → request is about to be sent; receives DispatchController
|
|
256
|
+
* onResponseStart → response status + headers received
|
|
257
|
+
* onResponseData → response body chunk
|
|
258
|
+
* onResponseEnd → response fully consumed
|
|
259
|
+
* onResponseError → error at any stage
|
|
260
|
+
*
|
|
261
|
+
* This gives you maximum control: you can abort mid-stream, transform
|
|
262
|
+
* chunks on the fly, or implement custom back-pressure logic.
|
|
263
|
+
*
|
|
264
|
+
* Unlike the other clients, `DispatchHttpClient` *requires* a `Dispatcher`
|
|
265
|
+
* (an `Agent`, `Pool`, or `Client` instance). If none is provided the
|
|
266
|
+
* process-global agent is used.
|
|
267
|
+
*
|
|
268
|
+
* Note: `dispatcher.dispatch()` needs the URL split into `origin` + `path`
|
|
269
|
+
* (for `Agent` / `Pool`) or just `path` (for `Client` whose origin is
|
|
270
|
+
* already bound). We always pass `origin` for maximum compatibility.
|
|
271
|
+
*/
|
|
272
|
+
declare class DispatchHttpClient extends BaseUndiciClient {
|
|
273
|
+
request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
274
|
+
stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* messages.ts
|
|
279
|
+
*
|
|
280
|
+
* Full discriminated-union type definitions for every SignalR hub protocol
|
|
281
|
+
* message. The `type` field on each interface carries a numeric literal type
|
|
282
|
+
* derived from the `MessageType` constants so TypeScript narrows correctly
|
|
283
|
+
* inside `switch` / `if` statements.
|
|
284
|
+
*
|
|
285
|
+
* Reference:
|
|
286
|
+
* https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md
|
|
287
|
+
*/
|
|
288
|
+
|
|
289
|
+
declare const __invocationIdBrand: unique symbol;
|
|
290
|
+
/**
|
|
291
|
+
* Opaque wrapper around `string` for invocation IDs.
|
|
292
|
+
* Prevents mixing arbitrary strings with IDs at the type level.
|
|
293
|
+
*/
|
|
294
|
+
type InvocationId = string & {
|
|
295
|
+
readonly [__invocationIdBrand]: void;
|
|
296
|
+
};
|
|
297
|
+
/** Cast a plain string to an InvocationId (only used inside the library). */
|
|
298
|
+
declare function toInvocationId(s: string): InvocationId;
|
|
299
|
+
/** Optional headers that may appear on any hub message. */
|
|
300
|
+
type MessageHeaders = Readonly<Record<string, string>>;
|
|
301
|
+
interface BaseMessage {
|
|
302
|
+
readonly headers?: MessageHeaders;
|
|
303
|
+
}
|
|
304
|
+
type MT = typeof MessageType;
|
|
305
|
+
/** Type 1 - Server→Client or Client→Server method call. */
|
|
306
|
+
interface InvocationMessage extends BaseMessage {
|
|
307
|
+
readonly type: MT['Invocation'];
|
|
308
|
+
/** Absent on fire-and-forget invocations. */
|
|
309
|
+
readonly invocationId?: InvocationId;
|
|
310
|
+
readonly target: string;
|
|
311
|
+
readonly arguments: readonly unknown[];
|
|
312
|
+
readonly streamIds?: readonly string[];
|
|
313
|
+
}
|
|
314
|
+
/** Type 2 - One item from a server-streaming method. */
|
|
315
|
+
interface StreamItemMessage extends BaseMessage {
|
|
316
|
+
readonly type: MT['StreamItem'];
|
|
317
|
+
readonly invocationId: InvocationId;
|
|
318
|
+
readonly item: unknown;
|
|
319
|
+
}
|
|
320
|
+
/** Type 3 - Terminal result or error for an invocation / stream. */
|
|
321
|
+
interface CompletionMessage extends BaseMessage {
|
|
322
|
+
readonly type: MT['Completion'];
|
|
323
|
+
readonly invocationId: InvocationId;
|
|
324
|
+
readonly error?: string;
|
|
325
|
+
readonly result?: unknown;
|
|
326
|
+
}
|
|
327
|
+
/** Type 4 - Client initiates a server-streaming call. */
|
|
328
|
+
interface StreamInvocationMessage extends BaseMessage {
|
|
329
|
+
readonly type: MT['StreamInvocation'];
|
|
330
|
+
readonly invocationId: InvocationId;
|
|
331
|
+
readonly target: string;
|
|
332
|
+
readonly arguments: readonly unknown[];
|
|
333
|
+
readonly streamIds?: readonly string[];
|
|
334
|
+
}
|
|
335
|
+
/** Type 5 - Client cancels an outstanding server stream. */
|
|
336
|
+
interface CancelInvocationMessage extends BaseMessage {
|
|
337
|
+
readonly type: MT['CancelInvocation'];
|
|
338
|
+
readonly invocationId: InvocationId;
|
|
339
|
+
}
|
|
340
|
+
/** Type 6 - Keep-alive ping; expects no response from the other side. */
|
|
341
|
+
interface PingMessage extends BaseMessage {
|
|
342
|
+
readonly type: MT['Ping'];
|
|
343
|
+
}
|
|
344
|
+
/** Type 7 - Graceful close notification from the server. */
|
|
345
|
+
interface CloseMessage extends BaseMessage {
|
|
346
|
+
readonly type: MT['Close'];
|
|
347
|
+
readonly error?: string;
|
|
348
|
+
readonly allowReconnect?: boolean;
|
|
349
|
+
}
|
|
350
|
+
/** Exhaustive discriminated union of all SignalR hub protocol messages. */
|
|
351
|
+
type HubMessage = InvocationMessage | StreamItemMessage | CompletionMessage | StreamInvocationMessage | CancelInvocationMessage | PingMessage | CloseMessage;
|
|
352
|
+
/**
|
|
353
|
+
* Asserts a code path is unreachable at compile time.
|
|
354
|
+
* Use at the `default` branch of a `switch` over `HubMessage['type']`.
|
|
355
|
+
*/
|
|
356
|
+
declare function assertNever(_x: never, label?: string): never;
|
|
357
|
+
declare function isInvocationMessage(msg: HubMessage): msg is InvocationMessage;
|
|
358
|
+
declare function isStreamItemMessage(msg: HubMessage): msg is StreamItemMessage;
|
|
359
|
+
declare function isCompletionMessage(msg: HubMessage): msg is CompletionMessage;
|
|
360
|
+
declare function isStreamInvocationMessage(msg: HubMessage): msg is StreamInvocationMessage;
|
|
361
|
+
declare function isCancelInvocationMessage(msg: HubMessage): msg is CancelInvocationMessage;
|
|
362
|
+
declare function isPingMessage(msg: HubMessage): msg is PingMessage;
|
|
363
|
+
declare function isCloseMessage(msg: HubMessage): msg is CloseMessage;
|
|
364
|
+
|
|
365
|
+
/** Minimal logger contract. Implement this to plug in any logging framework. */
|
|
366
|
+
interface ILogger {
|
|
367
|
+
log(logLevel: LogLevel, message: string): void;
|
|
368
|
+
}
|
|
369
|
+
/** Low-level transport abstraction (WebSocket / SSE / LongPolling). */
|
|
370
|
+
interface ITransport {
|
|
371
|
+
/** Human-readable name used in log messages and error reporting. */
|
|
372
|
+
readonly name: string;
|
|
373
|
+
/**
|
|
374
|
+
* Open the transport connection.
|
|
375
|
+
* @param url Fully-qualified URL including any query parameters.
|
|
376
|
+
* @param transferFormat Text or Binary.
|
|
377
|
+
*/
|
|
378
|
+
connect(url: string, transferFormat: TransferFormat): Promise<void>;
|
|
379
|
+
/** Send a message over the open transport. */
|
|
380
|
+
send(data: string | Uint8Array): Promise<void>;
|
|
381
|
+
/** Tear down the connection cleanly. */
|
|
382
|
+
stop(): Promise<void>;
|
|
383
|
+
/** Called by HubConnection whenever data arrives. */
|
|
384
|
+
onreceive: ((data: string | Uint8Array) => void) | null;
|
|
385
|
+
/** Called by HubConnection when the transport is closed (optionally with error). */
|
|
386
|
+
onclose: ((error?: Error) => void) | null;
|
|
387
|
+
}
|
|
388
|
+
/** Wire-protocol codec (JSON today; MessagePack can be added as an alternate). */
|
|
389
|
+
interface IHubProtocol {
|
|
390
|
+
readonly name: string;
|
|
391
|
+
readonly version: number;
|
|
392
|
+
readonly transferFormat: TransferFormat;
|
|
393
|
+
/**
|
|
394
|
+
* Decode raw bytes/string into zero or more hub messages.
|
|
395
|
+
* Implementations must handle partial/buffered inputs gracefully.
|
|
396
|
+
*/
|
|
397
|
+
parseMessages(input: string | ArrayBuffer, logger: ILogger): HubMessage[];
|
|
398
|
+
/** Encode a single hub message to its wire representation. */
|
|
399
|
+
writeMessage(message: HubMessage): string | ArrayBuffer;
|
|
400
|
+
}
|
|
401
|
+
/** Context passed to the retry policy on each reconnect attempt. */
|
|
402
|
+
interface RetryContext {
|
|
403
|
+
readonly previousRetryCount: number;
|
|
404
|
+
readonly elapsedMilliseconds: number;
|
|
405
|
+
readonly retryReason: Error | null;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Controls the automatic-reconnect delay strategy.
|
|
409
|
+
* Return `null` to stop retrying.
|
|
410
|
+
*/
|
|
411
|
+
interface IRetryPolicy {
|
|
412
|
+
nextRetryDelayInMilliseconds(retryContext: RetryContext): number | null;
|
|
413
|
+
}
|
|
414
|
+
/** Observer passed to `IStreamResult.subscribe`. */
|
|
415
|
+
interface IStreamSubscriber<T> {
|
|
416
|
+
readonly next?: (value: T) => void;
|
|
417
|
+
readonly error?: (err: Error) => void;
|
|
418
|
+
readonly complete?: () => void;
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* A handle returned by `subscribe` that lets the caller cancel the stream.
|
|
422
|
+
* Implements `Symbol.dispose` for use with the `using` keyword (TS 5.2+).
|
|
423
|
+
*/
|
|
424
|
+
interface ISubscription {
|
|
425
|
+
dispose(): void;
|
|
426
|
+
/** Enables `using subscription = connection.stream(...).subscribe(...)` */
|
|
427
|
+
[Symbol.dispose](): void;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Lazy stream result - the server-streaming call is not sent until
|
|
431
|
+
* `subscribe` is called.
|
|
432
|
+
*/
|
|
433
|
+
interface IStreamResult<T> {
|
|
434
|
+
subscribe(subscriber: IStreamSubscriber<T>): ISubscription;
|
|
435
|
+
}
|
|
436
|
+
/** Shape of the /negotiate response from an ASP.NET Core SignalR server. */
|
|
437
|
+
interface NegotiateResponse {
|
|
438
|
+
readonly connectionId?: string;
|
|
439
|
+
readonly connectionToken?: string;
|
|
440
|
+
readonly negotiateVersion?: number;
|
|
441
|
+
readonly url?: string;
|
|
442
|
+
readonly accessToken?: string;
|
|
443
|
+
readonly error?: string;
|
|
444
|
+
readonly availableTransports?: readonly AvailableTransport[];
|
|
445
|
+
}
|
|
446
|
+
interface AvailableTransport {
|
|
447
|
+
readonly transport: string;
|
|
448
|
+
readonly transferFormats: readonly string[];
|
|
449
|
+
}
|
|
450
|
+
/** Buffered HTTP response shared by all client implementations. */
|
|
451
|
+
interface HttpResponse {
|
|
452
|
+
readonly status: number;
|
|
453
|
+
readonly headers: Readonly<Record<string, string | string[] | undefined>>;
|
|
454
|
+
readonly body: string;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Result of a non-buffered (streaming) HTTP request.
|
|
458
|
+
*
|
|
459
|
+
* `body` - Node.js Readable carrying the response bytes.
|
|
460
|
+
* The SSE and long-polling transports consume this stream directly.
|
|
461
|
+
*
|
|
462
|
+
* `abort` - Cancels the in-flight request and destroys the body stream.
|
|
463
|
+
* Replaces the former `req.destroy()` pattern.
|
|
464
|
+
*/
|
|
465
|
+
interface StreamResult {
|
|
466
|
+
readonly statusCode: number;
|
|
467
|
+
readonly headers: Readonly<Record<string, string | string[] | undefined>>;
|
|
468
|
+
readonly body: Readable;
|
|
469
|
+
abort(): void;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Common contract satisfied by all five HTTP client variants
|
|
473
|
+
* (Request, Fetch, Stream, Pipeline, Dispatch).
|
|
474
|
+
*
|
|
475
|
+
* HubConnection depends on this interface so any implementation can be
|
|
476
|
+
* injected via `HubConnectionOptions.httpClient`.
|
|
477
|
+
*/
|
|
478
|
+
interface IHttpClient {
|
|
479
|
+
get(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
480
|
+
post(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
481
|
+
delete(url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
482
|
+
request(method: HttpMethod, url: string, options?: RequestOptions): Promise<HttpResponse>;
|
|
483
|
+
stream(method: HttpMethod, url: string, options?: RequestOptions): Promise<StreamResult>;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/**
|
|
487
|
+
* hub-connection.ts
|
|
488
|
+
*
|
|
489
|
+
* Core HubConnection - the main object users interact with.
|
|
490
|
+
*
|
|
491
|
+
* Mirrors the Microsoft SignalR JavaScript client API with full TypeScript types:
|
|
492
|
+
* start() / stop()
|
|
493
|
+
* invoke<T>(method, ...args) → Promise<T>
|
|
494
|
+
* send(method, ...args) → Promise<void>
|
|
495
|
+
* stream<T>(method, ...args) → IStreamResult<T>
|
|
496
|
+
* on(method, handler)
|
|
497
|
+
* off(method, [handler])
|
|
498
|
+
* onclose / onreconnecting / onreconnected
|
|
499
|
+
*/
|
|
500
|
+
|
|
501
|
+
interface HubConnectionOptions {
|
|
502
|
+
readonly logger?: ILogger;
|
|
503
|
+
readonly accessTokenFactory?: () => Promise<string | null>;
|
|
504
|
+
/** Bitmask of HttpTransportType flags (default: all transports). */
|
|
505
|
+
readonly transport?: number;
|
|
506
|
+
readonly headers?: Record<string, string>;
|
|
507
|
+
readonly skipNegotiation?: boolean;
|
|
508
|
+
readonly serverTimeoutInMilliseconds?: number;
|
|
509
|
+
readonly keepAliveIntervalInMilliseconds?: number;
|
|
510
|
+
readonly reconnectPolicy?: IRetryPolicy | null;
|
|
511
|
+
/**
|
|
512
|
+
* undici Dispatcher shared by all HTTP requests (negotiate, SSE, polling)
|
|
513
|
+
* and the WebSocket upgrade for this connection.
|
|
514
|
+
*
|
|
515
|
+
* Pass an `Agent`, `Pool`, `Client`, `ProxyAgent`, etc. to customise
|
|
516
|
+
* connection pooling, TLS, or proxy routing for the entire SignalR session.
|
|
517
|
+
* When omitted, undici's process-global default agent is used.
|
|
518
|
+
*/
|
|
519
|
+
readonly dispatcher?: Dispatcher;
|
|
520
|
+
/**
|
|
521
|
+
* Custom HTTP client to use for all requests made by this connection
|
|
522
|
+
* (negotiate, SSE body reads, long-polling).
|
|
523
|
+
*
|
|
524
|
+
* When omitted, a `DispatchHttpClient` is created automatically using
|
|
525
|
+
* the `dispatcher` option (if any). Provide this to use a different
|
|
526
|
+
* undici primitive (e.g. `FetchHttpClient`) or to inject a mock in tests.
|
|
527
|
+
*/
|
|
528
|
+
readonly httpClient?: IHttpClient;
|
|
529
|
+
}
|
|
530
|
+
declare class HubConnection {
|
|
531
|
+
#private;
|
|
532
|
+
constructor(url: string, options?: HubConnectionOptions);
|
|
533
|
+
get state(): HubConnectionState;
|
|
534
|
+
get connectionId(): string | null;
|
|
535
|
+
start(): Promise<void>;
|
|
536
|
+
stop(): Promise<void>;
|
|
537
|
+
/**
|
|
538
|
+
* Invoke a hub method and return the server's result.
|
|
539
|
+
*
|
|
540
|
+
* @typeParam T The expected return type. Asserted at runtime - the server
|
|
541
|
+
* decides the actual value.
|
|
542
|
+
*/
|
|
543
|
+
invoke<T = void>(methodName: string, ...args: unknown[]): Promise<T>;
|
|
544
|
+
/**
|
|
545
|
+
* Fire-and-forget hub method invocation.
|
|
546
|
+
* The server does not send a completion message.
|
|
547
|
+
*/
|
|
548
|
+
send(methodName: string, ...args: unknown[]): Promise<void>;
|
|
549
|
+
/**
|
|
550
|
+
* Start a server-streaming invocation.
|
|
551
|
+
* The call is not sent until `subscribe` is called.
|
|
552
|
+
* The returned `ISubscription` implements `Symbol.dispose` for `using`.
|
|
553
|
+
*
|
|
554
|
+
* @typeParam T Type of each streamed item.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```ts
|
|
558
|
+
* using sub = connection.stream<number>('Counter', 10).subscribe({
|
|
559
|
+
* next: (n) => console.log(n),
|
|
560
|
+
* complete: () => console.log('done'),
|
|
561
|
+
* });
|
|
562
|
+
* ```
|
|
563
|
+
*/
|
|
564
|
+
stream<T = unknown>(methodName: string, ...args: unknown[]): IStreamResult<T>;
|
|
565
|
+
/**
|
|
566
|
+
* Register a handler for a server→client hub method.
|
|
567
|
+
* Multiple handlers for the same method are supported.
|
|
568
|
+
*
|
|
569
|
+
* @typeParam TArgs Tuple of argument types for the hub method.
|
|
570
|
+
*/
|
|
571
|
+
on<TArgs extends unknown[] = unknown[]>(methodName: string, handler: (...args: TArgs) => void): void;
|
|
572
|
+
/**
|
|
573
|
+
* Remove a previously registered handler, or all handlers for a method.
|
|
574
|
+
*/
|
|
575
|
+
off(methodName: string, handler?: (...args: unknown[]) => void): void;
|
|
576
|
+
onclose(callback: (error?: Error) => void): void;
|
|
577
|
+
onreconnecting(callback: (error?: Error) => void): void;
|
|
578
|
+
onreconnected(callback: (connectionId: string | null) => void): void;
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
/**
|
|
582
|
+
* hub-connection-builder.ts
|
|
583
|
+
*
|
|
584
|
+
* Fluent builder for HubConnection - mirrors the official Microsoft client API.
|
|
585
|
+
* Uses `const` type parameters (TS 5.0+) and `NoInfer<T>` (TS 5.4+) where
|
|
586
|
+
* applicable to preserve literal types and prevent unintended widening.
|
|
587
|
+
*
|
|
588
|
+
* Usage
|
|
589
|
+
* ─────
|
|
590
|
+
* const connection = new HubConnectionBuilder()
|
|
591
|
+
* .withUrl('https://example.com/hub', {
|
|
592
|
+
* accessTokenFactory: () => fetchToken(),
|
|
593
|
+
* transport: HttpTransportType.WebSockets,
|
|
594
|
+
* })
|
|
595
|
+
* .configureLogging(LogLevel.Information)
|
|
596
|
+
* .withAutomaticReconnect()
|
|
597
|
+
* .build();
|
|
598
|
+
*/
|
|
599
|
+
|
|
600
|
+
interface UrlOptions {
|
|
601
|
+
/** Async function returning a Bearer token (or null for unauthenticated). */
|
|
602
|
+
readonly accessTokenFactory?: () => Promise<string | null>;
|
|
603
|
+
/**
|
|
604
|
+
* Bitmask of {@link HttpTransportType} values.
|
|
605
|
+
* Defaults to all three transports if omitted.
|
|
606
|
+
*/
|
|
607
|
+
readonly transport?: number;
|
|
608
|
+
/** Extra HTTP headers appended to every request (negotiate, upgrade, poll…). */
|
|
609
|
+
readonly headers?: Record<string, string>;
|
|
610
|
+
/**
|
|
611
|
+
* Skip the /negotiate step and connect directly via WebSocket.
|
|
612
|
+
* Only valid when transport is exclusively `HttpTransportType.WebSockets`.
|
|
613
|
+
*/
|
|
614
|
+
readonly skipNegotiation?: boolean;
|
|
615
|
+
/** ms before the connection is considered unresponsive (default: 30 000). */
|
|
616
|
+
readonly serverTimeoutInMilliseconds?: number;
|
|
617
|
+
/** ms between keep-alive pings (default: 15 000). */
|
|
618
|
+
readonly keepAliveIntervalInMilliseconds?: number;
|
|
619
|
+
}
|
|
620
|
+
declare class HubConnectionBuilder {
|
|
621
|
+
#private;
|
|
622
|
+
withUrl(url: string, options?: UrlOptions): this;
|
|
623
|
+
/**
|
|
624
|
+
* Enable automatic cookie handling for the entire connection session using
|
|
625
|
+
* a `tough-cookie` `CookieJar` and an `@exhumer/undici-cookie-agent` `CookieAgent`.
|
|
626
|
+
*
|
|
627
|
+
* When enabled, the `CookieAgent` wraps every outgoing request - including
|
|
628
|
+
* the `/negotiate` POST, WebSocket upgrade, SSE long-lived GET, and all
|
|
629
|
+
* long-polling requests - so that:
|
|
630
|
+
*
|
|
631
|
+
* 1. `Set-Cookie` headers in the **negotiate response** (and any subsequent
|
|
632
|
+
* response) are stored in the jar automatically.
|
|
633
|
+
* 2. All subsequent requests from the same session include the matching
|
|
634
|
+
* `Cookie` header, exactly as a browser would.
|
|
635
|
+
*
|
|
636
|
+
* @param jar
|
|
637
|
+
* An existing `CookieJar` to use. Pass your own jar when you need to
|
|
638
|
+
* pre-seed cookies (e.g. an auth session cookie obtained before calling
|
|
639
|
+
* `start()`) or inspect the jar after the session ends.
|
|
640
|
+
* When omitted a new, empty jar is created automatically.
|
|
641
|
+
*
|
|
642
|
+
* @throws if {@link withDispatcher} has already been called. To combine a
|
|
643
|
+
* custom dispatcher (e.g. `ProxyAgent`) with cookie support, compose the
|
|
644
|
+
* `cookie` interceptor onto your dispatcher and pass it to `withDispatcher()`:
|
|
645
|
+
*
|
|
646
|
+
* ```ts
|
|
647
|
+
* import { ProxyAgent } from 'undici';
|
|
648
|
+
* import { cookie, CookieJar } from '@exhumer/signalr-client';
|
|
649
|
+
*
|
|
650
|
+
* const jar = new CookieJar();
|
|
651
|
+
* const agent = new ProxyAgent('http://proxy:8080').compose(cookie({ jar }));
|
|
652
|
+
*
|
|
653
|
+
* const conn = new HubConnectionBuilder()
|
|
654
|
+
* .withUrl('https://example.com/hub')
|
|
655
|
+
* .withDispatcher(agent)
|
|
656
|
+
* .build();
|
|
657
|
+
* ```
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```ts
|
|
661
|
+
* // Automatic jar - no pre-configuration needed
|
|
662
|
+
* const conn = new HubConnectionBuilder()
|
|
663
|
+
* .withUrl('https://example.com/hub')
|
|
664
|
+
* .withCookies()
|
|
665
|
+
* .build();
|
|
666
|
+
*
|
|
667
|
+
* // Pre-seeded jar - inject a session cookie before connecting
|
|
668
|
+
* import { CookieJar } from '@exhumer/signalr-client';
|
|
669
|
+
* const jar = new CookieJar();
|
|
670
|
+
* await jar.setCookie('session=abc123', 'https://example.com');
|
|
671
|
+
*
|
|
672
|
+
* const conn = new HubConnectionBuilder()
|
|
673
|
+
* .withUrl('https://example.com/hub')
|
|
674
|
+
* .withCookies(jar)
|
|
675
|
+
* .build();
|
|
676
|
+
* ```
|
|
677
|
+
*/
|
|
678
|
+
withCookies(jar?: CookieJar): this;
|
|
679
|
+
/**
|
|
680
|
+
* Set the undici `Dispatcher` for the entire connection session.
|
|
681
|
+
*
|
|
682
|
+
* The same dispatcher is used for:
|
|
683
|
+
* • the `/negotiate` HTTP POST
|
|
684
|
+
* • the WebSocket upgrade request
|
|
685
|
+
* • SSE long-lived GET and send POSTs
|
|
686
|
+
* • long-polling GETs, POSTs, and DELETE
|
|
687
|
+
*
|
|
688
|
+
* Accepts any `Dispatcher` subclass: `Agent`, `Pool`, `Client`,
|
|
689
|
+
* `ProxyAgent`, `MockAgent`, etc.
|
|
690
|
+
*
|
|
691
|
+
* To combine a custom dispatcher with automatic cookie handling, use
|
|
692
|
+
* `createCookieAgentClass()` to build a cookie-aware variant of your
|
|
693
|
+
* dispatcher and pass that here. Do **not** chain both
|
|
694
|
+
* `withDispatcher()` and `withCookies()` - they are mutually exclusive
|
|
695
|
+
* because `withCookies()` creates its own dispatcher internally.
|
|
696
|
+
*
|
|
697
|
+
* @example
|
|
698
|
+
* ```ts
|
|
699
|
+
* import { ProxyAgent } from 'undici';
|
|
700
|
+
*
|
|
701
|
+
* const conn = new HubConnectionBuilder()
|
|
702
|
+
* .withUrl('https://example.com/hub')
|
|
703
|
+
* .withDispatcher(new ProxyAgent('http://proxy:8080'))
|
|
704
|
+
* .build();
|
|
705
|
+
* ```
|
|
706
|
+
*/
|
|
707
|
+
withDispatcher(dispatcher: Dispatcher): this;
|
|
708
|
+
/**
|
|
709
|
+
* Provide a custom HTTP client implementation.
|
|
710
|
+
*
|
|
711
|
+
* By default `HubConnection` uses `DispatchHttpClient` (undici `dispatch`).
|
|
712
|
+
* Use this to substitute any of the other undici-backed clients, or
|
|
713
|
+
* to inject a mock for testing.
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* ```ts
|
|
717
|
+
* import { FetchHttpClient } from '@signalr/client';
|
|
718
|
+
*
|
|
719
|
+
* const conn = new HubConnectionBuilder()
|
|
720
|
+
* .withUrl('https://example.com/hub')
|
|
721
|
+
* .withHttpClient(new FetchHttpClient())
|
|
722
|
+
* .build();
|
|
723
|
+
* ```
|
|
724
|
+
*/
|
|
725
|
+
withHttpClient(httpClient: IHttpClient): this;
|
|
726
|
+
/**
|
|
727
|
+
* Set the minimum log level or provide a custom logger.
|
|
728
|
+
*
|
|
729
|
+
* @param logLevelOrLogger
|
|
730
|
+
* - `LogLevel` number → uses the built-in ConsoleLogger
|
|
731
|
+
* - `ILogger` object → used as-is
|
|
732
|
+
*/
|
|
733
|
+
configureLogging(logLevelOrLogger: LogLevel | ILogger): this;
|
|
734
|
+
/**
|
|
735
|
+
* Enable automatic reconnection on unexpected disconnections.
|
|
736
|
+
*
|
|
737
|
+
* @overload withAutomaticReconnect()
|
|
738
|
+
* Uses the built-in delay sequence: 0 ms, 2 s, 10 s, 30 s.
|
|
739
|
+
*
|
|
740
|
+
* @overload withAutomaticReconnect(retryDelays: readonly number[])
|
|
741
|
+
* Each element is the wait (ms) before the corresponding retry.
|
|
742
|
+
* Reconnection stops after all delays are exhausted.
|
|
743
|
+
* Uses `NoInfer<number>` so literals are not widened unexpectedly.
|
|
744
|
+
*
|
|
745
|
+
* @overload withAutomaticReconnect(policy: IRetryPolicy)
|
|
746
|
+
* Custom policy. Return `null` from
|
|
747
|
+
* `nextRetryDelayInMilliseconds` to stop retrying.
|
|
748
|
+
*/
|
|
749
|
+
withAutomaticReconnect(): this;
|
|
750
|
+
withAutomaticReconnect(retryDelays: readonly NoInfer<number>[]): this;
|
|
751
|
+
withAutomaticReconnect(policy: IRetryPolicy): this;
|
|
752
|
+
build(): HubConnection;
|
|
753
|
+
}
|
|
754
|
+
declare class DefaultReconnectPolicy implements IRetryPolicy {
|
|
755
|
+
#private;
|
|
756
|
+
constructor(retryDelays?: readonly number[]);
|
|
757
|
+
nextRetryDelayInMilliseconds(context: RetryContext): number | null;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* json-hub-protocol.ts
|
|
762
|
+
*
|
|
763
|
+
* Implements the SignalR Hub Protocol over JSON (version 1).
|
|
764
|
+
*
|
|
765
|
+
* Wire format: every message is JSON followed by ASCII 0x1e (RECORD_SEPARATOR).
|
|
766
|
+
* A single transport payload may contain multiple messages.
|
|
767
|
+
*
|
|
768
|
+
* Reference:
|
|
769
|
+
* https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md
|
|
770
|
+
*/
|
|
771
|
+
|
|
772
|
+
declare const HANDSHAKE_REQUEST$1: string;
|
|
773
|
+
interface HandshakeResponse {
|
|
774
|
+
readonly error?: string;
|
|
775
|
+
readonly minorVersion?: number;
|
|
776
|
+
}
|
|
777
|
+
interface HandshakeParseResult {
|
|
778
|
+
/** Remainder of the input string after the handshake record (may be non-empty). */
|
|
779
|
+
readonly remainder: string;
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Parse the first record from `data` as the server handshake response.
|
|
783
|
+
* @throws {Error} if the record separator is missing or the server returned an error.
|
|
784
|
+
*/
|
|
785
|
+
declare function parseHandshakeResponse(data: string): HandshakeParseResult;
|
|
786
|
+
declare class JsonHubProtocol implements IHubProtocol {
|
|
787
|
+
#private;
|
|
788
|
+
readonly name: "json";
|
|
789
|
+
readonly version: 1;
|
|
790
|
+
readonly transferFormat: 1;
|
|
791
|
+
parseMessages(input: string | ArrayBuffer, logger: ILogger): HubMessage[];
|
|
792
|
+
writeMessage(message: HubMessage): string;
|
|
793
|
+
static invocation(invocationId: InvocationId, target: string, args: readonly unknown[], streamIds?: readonly string[]): InvocationMessage;
|
|
794
|
+
/**
|
|
795
|
+
* Fire-and-forget (no `invocationId` → server won't send a Completion).
|
|
796
|
+
*/
|
|
797
|
+
static send(target: string, args: readonly unknown[], streamIds?: readonly string[]): InvocationMessage;
|
|
798
|
+
static streamInvocation(invocationId: InvocationId, target: string, args: readonly unknown[], streamIds?: readonly string[]): StreamInvocationMessage;
|
|
799
|
+
static cancelInvocation(invocationId: InvocationId): CancelInvocationMessage;
|
|
800
|
+
static streamItem(invocationId: InvocationId, item: unknown): StreamItemMessage;
|
|
801
|
+
static completion(invocationId: InvocationId, result: unknown, error: string | null): CompletionMessage;
|
|
802
|
+
static ping(): PingMessage;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/**
|
|
806
|
+
* msgpack-hub-protocol.ts
|
|
807
|
+
*
|
|
808
|
+
* Implements the SignalR Hub Protocol over MessagePack (version 1).
|
|
809
|
+
*
|
|
810
|
+
* Wire format
|
|
811
|
+
* ───────────
|
|
812
|
+
* • Handshake: JSON text terminated by ASCII 0x1e - identical to the JSON
|
|
813
|
+
* protocol, only the "protocol" field changes to "messagepack".
|
|
814
|
+
* • All subsequent messages: binary MessagePack, each frame prefixed by a
|
|
815
|
+
* base-128 VarInt carrying the byte-length of the following MessagePack
|
|
816
|
+
* payload. A single transport payload may contain multiple such frames.
|
|
817
|
+
*
|
|
818
|
+
* MessagePack array layout (per message type)
|
|
819
|
+
* ────────────────────────────────────────────
|
|
820
|
+
* Invocation [1, headers, invocationId|null, target, args, streamIds]
|
|
821
|
+
* StreamItem [2, headers, invocationId, item]
|
|
822
|
+
* Completion [3, headers, invocationId, resultKind, value?]
|
|
823
|
+
* resultKind: 1 = error string, 2 = void, 3 = non-void result value
|
|
824
|
+
* StreamInvocation [4, headers, invocationId, target, args, streamIds]
|
|
825
|
+
* CancelInvocation [5, headers, invocationId]
|
|
826
|
+
* Ping [6]
|
|
827
|
+
* Close [7, error|null, allowReconnect]
|
|
828
|
+
*
|
|
829
|
+
* Reference:
|
|
830
|
+
* https://github.com/dotnet/aspnetcore/blob/main/src/SignalR/docs/specs/HubProtocol.md
|
|
831
|
+
*/
|
|
832
|
+
|
|
833
|
+
/**
|
|
834
|
+
* The JSON-formatted handshake request the client sends once the transport
|
|
835
|
+
* is open. The server always responds with JSON ({} on success), regardless
|
|
836
|
+
* of the chosen hub protocol.
|
|
837
|
+
*/
|
|
838
|
+
declare const HANDSHAKE_REQUEST: string;
|
|
839
|
+
|
|
840
|
+
declare class MsgpackHubProtocol implements IHubProtocol {
|
|
841
|
+
#private;
|
|
842
|
+
readonly name: "messagepack";
|
|
843
|
+
readonly version: 1;
|
|
844
|
+
readonly transferFormat: 2;
|
|
845
|
+
parseMessages(input: string | ArrayBuffer, logger: ILogger): HubMessage[];
|
|
846
|
+
writeMessage(message: HubMessage): ArrayBuffer;
|
|
847
|
+
static invocation(invocationId: InvocationId, target: string, args: readonly unknown[], streamIds?: readonly string[]): InvocationMessage;
|
|
848
|
+
/** Fire-and-forget (no `invocationId`). */
|
|
849
|
+
static send(target: string, args: readonly unknown[], streamIds?: readonly string[]): InvocationMessage;
|
|
850
|
+
static streamInvocation(invocationId: InvocationId, target: string, args: readonly unknown[], streamIds?: readonly string[]): StreamInvocationMessage;
|
|
851
|
+
static cancelInvocation(invocationId: InvocationId): CancelInvocationMessage;
|
|
852
|
+
static streamItem(invocationId: InvocationId, item: unknown): StreamItemMessage;
|
|
853
|
+
static completion(invocationId: InvocationId, result: unknown, error: string | null): CompletionMessage;
|
|
854
|
+
static ping(): PingMessage;
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* logger.ts
|
|
859
|
+
*
|
|
860
|
+
* Built-in ILogger implementations.
|
|
861
|
+
* ConsoleLogger - writes to console with timestamps and severity labels.
|
|
862
|
+
* NullLogger - discards everything (default when no logger is configured).
|
|
863
|
+
*/
|
|
864
|
+
|
|
865
|
+
declare class ConsoleLogger implements ILogger {
|
|
866
|
+
#private;
|
|
867
|
+
constructor(minimumLevel?: LogLevel);
|
|
868
|
+
log(level: LogLevel, message: string): void;
|
|
869
|
+
}
|
|
870
|
+
declare class NullLogger implements ILogger {
|
|
871
|
+
/** Singleton - no state, so one instance is enough. */
|
|
872
|
+
static readonly instance: NullLogger;
|
|
873
|
+
log(_logLevel: LogLevel, _message: string): void;
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* errors.ts
|
|
878
|
+
*
|
|
879
|
+
* Typed error classes for the SignalR client. Each has a distinct `name`
|
|
880
|
+
* so callers can use `instanceof` or switch on `error.name` to distinguish
|
|
881
|
+
* error categories at runtime.
|
|
882
|
+
*/
|
|
883
|
+
/** Thrown when a hub method invocation fails with a server-side error. */
|
|
884
|
+
declare class HubError extends Error {
|
|
885
|
+
readonly name: "HubError";
|
|
886
|
+
constructor(message: string);
|
|
887
|
+
}
|
|
888
|
+
/** Thrown when an in-flight operation is cancelled (e.g. connection closed). */
|
|
889
|
+
declare class AbortError extends Error {
|
|
890
|
+
readonly name: "AbortError";
|
|
891
|
+
constructor(message?: string);
|
|
892
|
+
}
|
|
893
|
+
/** Thrown on network-level transport failures. */
|
|
894
|
+
declare class TransportError extends Error {
|
|
895
|
+
readonly name: "TransportError";
|
|
896
|
+
readonly statusCode: number | undefined;
|
|
897
|
+
constructor(message: string, statusCode?: number);
|
|
898
|
+
}
|
|
899
|
+
/** Thrown when the SignalR protocol handshake is rejected by the server. */
|
|
900
|
+
declare class HandshakeError extends Error {
|
|
901
|
+
readonly name: "HandshakeError";
|
|
902
|
+
constructor(message: string);
|
|
903
|
+
}
|
|
904
|
+
/** Thrown when no acceptable transport could be negotiated. */
|
|
905
|
+
declare class UnsupportedTransportError extends Error {
|
|
906
|
+
readonly name: "UnsupportedTransportError";
|
|
907
|
+
readonly transport: number | null;
|
|
908
|
+
constructor(message: string, transport: number | null);
|
|
909
|
+
}
|
|
910
|
+
declare function isHubError(e: unknown): e is HubError;
|
|
911
|
+
declare function isAbortError(e: unknown): e is AbortError;
|
|
912
|
+
declare function isTransportError(e: unknown): e is TransportError;
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* websocket-transport.ts
|
|
916
|
+
*
|
|
917
|
+
* SignalR WebSocket transport - rewritten to use undici's built-in
|
|
918
|
+
* WHATWG-compatible `WebSocket` implementation.
|
|
919
|
+
*
|
|
920
|
+
* Why undici WebSocket instead of the hand-rolled RFC 6455 client?
|
|
921
|
+
* ─────────────────────────────────────────────────────────────────
|
|
922
|
+
* • undici is the HTTP engine Node.js core ships with (≥ v18).
|
|
923
|
+
* • Its WebSocket implementation passes the Autobahn test suite.
|
|
924
|
+
* • It accepts the same `Dispatcher` used by the HTTP clients, so the
|
|
925
|
+
* entire SignalR session (negotiate, handshake, upgrade) shares a single
|
|
926
|
+
* connection pool / proxy config.
|
|
927
|
+
* • It supports custom headers on the upgrade request (needed for auth
|
|
928
|
+
* and extra headers), which the browser WebSocket API does not.
|
|
929
|
+
*
|
|
930
|
+
* undici WebSocket quirks in Node.js
|
|
931
|
+
* ────────────────────────────────────
|
|
932
|
+
* • The constructor signature is:
|
|
933
|
+
* new WebSocket(url, { headers?, dispatcher?, protocols? })
|
|
934
|
+
* The `headers` and `dispatcher` fields are undici extensions not in the
|
|
935
|
+
* WHATWG spec.
|
|
936
|
+
* • `MessageEvent.data` type depends on `ws.binaryType`:
|
|
937
|
+
* 'arraybuffer' (what we set) → ArrayBuffer
|
|
938
|
+
* 'blob' → Blob
|
|
939
|
+
* We set `binaryType = 'arraybuffer'` and convert ArrayBuffer → Buffer on
|
|
940
|
+
* receipt. ('nodebuffer' is a valid undici runtime value but is absent from
|
|
941
|
+
* the WHATWG BinaryType union in undici's TypeScript declarations.)
|
|
942
|
+
* • `CloseEvent.code` / `.reason` behave identically to the browser.
|
|
943
|
+
*/
|
|
944
|
+
|
|
945
|
+
declare class WebSocketTransport implements ITransport {
|
|
946
|
+
#private;
|
|
947
|
+
readonly name: "WebSockets";
|
|
948
|
+
onreceive: ((data: string | Uint8Array) => void) | null;
|
|
949
|
+
onclose: ((error?: Error) => void) | null;
|
|
950
|
+
/**
|
|
951
|
+
* @param accessTokenFactory Async factory for Bearer tokens, or null.
|
|
952
|
+
* @param logger Logger instance.
|
|
953
|
+
* @param extraHeaders Headers forwarded on every upgrade request.
|
|
954
|
+
* @param dispatcher Optional undici Dispatcher (Agent/Pool/Client/
|
|
955
|
+
* ProxyAgent). When omitted, undici's default
|
|
956
|
+
* global agent is used. Pass the same dispatcher
|
|
957
|
+
* used by your HTTP clients to share the pool.
|
|
958
|
+
*/
|
|
959
|
+
constructor(accessTokenFactory: (() => Promise<string | null>) | null, logger: ILogger, extraHeaders?: Record<string, string>, dispatcher?: Dispatcher);
|
|
960
|
+
connect(url: string, _transferFormat: TransferFormat): Promise<void>;
|
|
961
|
+
send(data: string | Uint8Array): Promise<void>;
|
|
962
|
+
stop(): Promise<void>;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
/**
|
|
966
|
+
* sse-transport.ts
|
|
967
|
+
*
|
|
968
|
+
* SignalR transport using Server-Sent Events (SSE) for receiving and
|
|
969
|
+
* HTTP POST for sending.
|
|
970
|
+
*
|
|
971
|
+
* SSE spec: https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
972
|
+
*
|
|
973
|
+
* Updated to use the new StreamResult shape from http-client.ts:
|
|
974
|
+
* result.body - Node Readable carrying the SSE byte stream
|
|
975
|
+
* result.abort - cancels the in-flight request (replaces req.destroy())
|
|
976
|
+
*/
|
|
977
|
+
|
|
978
|
+
declare class ServerSentEventsTransport implements ITransport {
|
|
979
|
+
#private;
|
|
980
|
+
readonly name: "ServerSentEvents";
|
|
981
|
+
onreceive: ((data: string | Uint8Array) => void) | null;
|
|
982
|
+
onclose: ((error?: Error) => void) | null;
|
|
983
|
+
constructor(httpClient: IHttpClient, accessTokenFactory: (() => Promise<string | null>) | null, logger: ILogger, extraHeaders?: Record<string, string>);
|
|
984
|
+
connect(url: string, transferFormat: TransferFormat): Promise<void>;
|
|
985
|
+
send(data: string | Uint8Array): Promise<void>;
|
|
986
|
+
stop(): Promise<void>;
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* long-polling-transport.ts
|
|
991
|
+
*
|
|
992
|
+
* SignalR Long-Polling transport.
|
|
993
|
+
* HTTP GET (hangs until data arrives) → receive
|
|
994
|
+
* HTTP POST → send
|
|
995
|
+
* HTTP DELETE → disconnect
|
|
996
|
+
*
|
|
997
|
+
* Updated to depend on IHttpClient rather than the concrete HttpClient class
|
|
998
|
+
* so any of the five undici-backed client implementations can be injected.
|
|
999
|
+
*/
|
|
1000
|
+
|
|
1001
|
+
declare class LongPollingTransport implements ITransport {
|
|
1002
|
+
#private;
|
|
1003
|
+
readonly name: "LongPolling";
|
|
1004
|
+
onreceive: ((data: string | Uint8Array) => void) | null;
|
|
1005
|
+
onclose: ((error?: Error) => void) | null;
|
|
1006
|
+
constructor(httpClient: IHttpClient, accessTokenFactory: (() => Promise<string | null>) | null, logger: ILogger, extraHeaders?: Record<string, string>);
|
|
1007
|
+
connect(url: string, _transferFormat: TransferFormat): Promise<void>;
|
|
1008
|
+
send(data: string | Uint8Array): Promise<void>;
|
|
1009
|
+
stop(): Promise<void>;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
/**
|
|
1013
|
+
* ws-client.ts
|
|
1014
|
+
*
|
|
1015
|
+
* From-scratch WebSocket client (RFC 6455) built on Node's built-in `net`
|
|
1016
|
+
* and `tls` modules. Zero external runtime dependencies.
|
|
1017
|
+
*
|
|
1018
|
+
* Typed EventEmitter via interface declaration merging.
|
|
1019
|
+
*
|
|
1020
|
+
* Emitted events
|
|
1021
|
+
* 'open' – handshake complete, ready to send/receive
|
|
1022
|
+
* 'message' (string) – UTF-8 text frame received
|
|
1023
|
+
* 'binary' (Buffer) – binary frame received
|
|
1024
|
+
* 'ping' (Buffer) – ping frame received (pong sent automatically)
|
|
1025
|
+
* 'pong' (Buffer) – pong frame received
|
|
1026
|
+
* 'close' – socket fully closed
|
|
1027
|
+
* 'error' (Error) – socket-level error
|
|
1028
|
+
*/
|
|
1029
|
+
|
|
1030
|
+
declare const enum WebSocketReadyState {
|
|
1031
|
+
Connecting = 0,
|
|
1032
|
+
Open = 1,
|
|
1033
|
+
Closing = 2,
|
|
1034
|
+
Closed = 3
|
|
1035
|
+
}
|
|
1036
|
+
interface WebSocketClient {
|
|
1037
|
+
on(event: 'open', listener: () => void): this;
|
|
1038
|
+
on(event: 'message', listener: (data: string) => void): this;
|
|
1039
|
+
on(event: 'binary', listener: (data: Buffer) => void): this;
|
|
1040
|
+
on(event: 'ping', listener: (data: Buffer) => void): this;
|
|
1041
|
+
on(event: 'pong', listener: (data: Buffer) => void): this;
|
|
1042
|
+
on(event: 'close', listener: () => void): this;
|
|
1043
|
+
on(event: 'error', listener: (err: Error) => void): this;
|
|
1044
|
+
once(event: 'open', listener: () => void): this;
|
|
1045
|
+
once(event: 'message', listener: (data: string) => void): this;
|
|
1046
|
+
once(event: 'close', listener: () => void): this;
|
|
1047
|
+
once(event: 'error', listener: (err: Error) => void): this;
|
|
1048
|
+
emit(event: 'open'): boolean;
|
|
1049
|
+
emit(event: 'message', data: string): boolean;
|
|
1050
|
+
emit(event: 'binary', data: Buffer): boolean;
|
|
1051
|
+
emit(event: 'ping', data: Buffer): boolean;
|
|
1052
|
+
emit(event: 'pong', data: Buffer): boolean;
|
|
1053
|
+
emit(event: 'close'): boolean;
|
|
1054
|
+
emit(event: 'error', err: Error): boolean;
|
|
1055
|
+
removeAllListeners(event?: string): this;
|
|
1056
|
+
}
|
|
1057
|
+
declare class WebSocketClient extends EventEmitter {
|
|
1058
|
+
#private;
|
|
1059
|
+
get readyState(): WebSocketReadyState;
|
|
1060
|
+
/**
|
|
1061
|
+
* Open a WebSocket connection.
|
|
1062
|
+
* @param url ws:// or wss:// URL
|
|
1063
|
+
* @param headers Extra HTTP headers for the upgrade request
|
|
1064
|
+
*/
|
|
1065
|
+
connect(url: string, headers?: Record<string, string>): Promise<void>;
|
|
1066
|
+
/**
|
|
1067
|
+
* Send a text or binary message.
|
|
1068
|
+
* @throws if the socket is not open.
|
|
1069
|
+
*/
|
|
1070
|
+
send(data: string | Buffer | Uint8Array): void;
|
|
1071
|
+
/**
|
|
1072
|
+
* Initiate a clean close handshake.
|
|
1073
|
+
* @param code WebSocket close code (default 1000 = Normal Closure)
|
|
1074
|
+
* @param reason UTF-8 reason string (max 123 bytes)
|
|
1075
|
+
*/
|
|
1076
|
+
close(code?: number, reason?: string): void;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
export { AbortError, type AvailableTransport, type CancelInvocationMessage, type CloseMessage, type CompletionMessage, ConsoleLogger, DEFAULT_PING_INTERVAL_IN_MS, DEFAULT_SERVER_TIMEOUT_IN_MS, DEFAULT_TIMEOUT_IN_MS, DefaultReconnectPolicy, DispatchHttpClient, FetchHttpClient, HANDSHAKE_REQUEST$1 as HANDSHAKE_REQUEST, HandshakeError, type HandshakeParseResult, type HandshakeResponse, DispatchHttpClient as HttpClient, type HttpClientOptions, type HttpMethod, type HttpResponse, HttpTransportType, HubConnection, HubConnectionBuilder, type HubConnectionOptions, HubConnectionState, HubError, type HubMessage, type IHttpClient, type IHubProtocol, type ILogger, type IRetryPolicy, type IStreamResult, type IStreamSubscriber, type ISubscription, type ITransport, type InvocationId, type InvocationMessage, JsonHubProtocol, LogLevel, LongPollingTransport, HANDSHAKE_REQUEST as MSGPACK_HANDSHAKE_REQUEST, type MessageHeaders, MessageType, type MessageTypeValue, MsgpackHubProtocol, NEGOTIATE_VERSION, type NegotiateResponse, NullLogger, type PingMessage, PipelineHttpClient, RECORD_SEPARATOR, RequestHttpClient, type RequestOptions, type RetryContext, ServerSentEventsTransport, StreamHttpClient, type StreamInvocationMessage, type StreamItemMessage, type StreamResult, TransferFormat, TransportError, UnsupportedTransportError, type UrlOptions, WebSocketClient, WebSocketReadyState, WebSocketTransport, assertNever, isAbortError, isCancelInvocationMessage, isCloseMessage, isCompletionMessage, isHubError, isInvocationMessage, isPingMessage, isStreamInvocationMessage, isStreamItemMessage, isTransportError, parseHandshakeResponse, toInvocationId };
|