@leaven-graphql/ws 0.2.0 → 0.2.2
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 +33 -2
- package/dist/handler.d.ts +22 -1
- package/dist/handler.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +156 -9362
- package/dist/logger.d.ts +20 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/manager.d.ts +24 -1
- package/dist/manager.d.ts.map +1 -1
- package/dist/pubsub.d.ts +4 -0
- package/dist/pubsub.d.ts.map +1 -1
- package/package.json +13 -8
package/README.md
CHANGED
|
@@ -283,6 +283,11 @@ interface WebSocketHandlerConfig<TContext = unknown> {
|
|
|
283
283
|
keepAliveInterval?: number;
|
|
284
284
|
/** Forwarded to the SubscriptionManager (minus `schema`) */
|
|
285
285
|
subscriptionManager?: Omit<SubscriptionManagerConfig, 'schema'>;
|
|
286
|
+
/**
|
|
287
|
+
* Destination for diagnostics such as failing hooks (default `console`).
|
|
288
|
+
* Passed on to the SubscriptionManager unless it sets its own.
|
|
289
|
+
*/
|
|
290
|
+
logger?: WebSocketLogger;
|
|
286
291
|
/** Return false to reject the connection with 4403 */
|
|
287
292
|
onConnect?: (
|
|
288
293
|
socket: ServerWebSocket<WebSocketContext>,
|
|
@@ -313,7 +318,22 @@ interface WebSocketContext {
|
|
|
313
318
|
Close codes follow the protocol: `4400` invalid message, `4401` unauthorized
|
|
314
319
|
(operation before `connection_init`), `4403` rejected by `onConnect`, `4408`
|
|
315
320
|
init timeout, `4409` duplicate subscription id, `4429` repeated
|
|
316
|
-
`connection_init
|
|
321
|
+
`connection_init` (including a second one sent while `onConnect` for the
|
|
322
|
+
first is still running).
|
|
323
|
+
|
|
324
|
+
An operation is tracked from the moment its `subscribe` frame arrives. A
|
|
325
|
+
client `complete` for an operation that is still setting up cancels it, and
|
|
326
|
+
an operation still setting up when the socket closes is abandoned: in both
|
|
327
|
+
cases nothing is registered and any source stream it obtained is returned.
|
|
328
|
+
Operations still setting up count toward `maxSubscriptionsPerConnection`.
|
|
329
|
+
|
|
330
|
+
```typescript
|
|
331
|
+
interface WebSocketLogger {
|
|
332
|
+
error(...args: unknown[]): void;
|
|
333
|
+
/** Optional; warnings go to `error` without it */
|
|
334
|
+
warn?(...args: unknown[]): void;
|
|
335
|
+
}
|
|
336
|
+
```
|
|
317
337
|
|
|
318
338
|
### SubscriptionManager
|
|
319
339
|
|
|
@@ -334,6 +354,9 @@ class SubscriptionManager {
|
|
|
334
354
|
onError: (errors: readonly { message: string }[]) => void
|
|
335
355
|
): Promise<Subscription>;
|
|
336
356
|
|
|
357
|
+
/** Parse with the executor's limits (maxTokens, maxDepth) */
|
|
358
|
+
parse(query: string): Promise<DocumentNode>;
|
|
359
|
+
|
|
337
360
|
/** Run a query or mutation on the same executor */
|
|
338
361
|
execute<TContext = unknown>(
|
|
339
362
|
request: GraphQLRequest,
|
|
@@ -348,6 +371,8 @@ class SubscriptionManager {
|
|
|
348
371
|
|
|
349
372
|
readonly subscriptionCount: number;
|
|
350
373
|
readonly connectionCount: number;
|
|
374
|
+
/** maxSubscriptionsPerConnection */
|
|
375
|
+
readonly subscriptionLimit: number;
|
|
351
376
|
}
|
|
352
377
|
|
|
353
378
|
function createSubscriptionManager(config: SubscriptionManagerConfig): SubscriptionManager;
|
|
@@ -365,6 +390,8 @@ interface SubscriptionManagerConfig {
|
|
|
365
390
|
* `introspection: false`, `maxDepth` or `maxComplexity`.
|
|
366
391
|
*/
|
|
367
392
|
executor?: LeavenExecutor | Omit<ExecutorConfig, 'schema'>;
|
|
393
|
+
/** Destination for timeout and callback diagnostics (default `console`) */
|
|
394
|
+
logger?: WebSocketLogger;
|
|
368
395
|
}
|
|
369
396
|
|
|
370
397
|
interface Subscription {
|
|
@@ -380,7 +407,9 @@ interface Subscription {
|
|
|
380
407
|
|
|
381
408
|
Subscription ids are scoped to a connection: `graphql-ws` clients number their
|
|
382
409
|
operations per connection (starting at `"1"`), so every lookup and teardown
|
|
383
|
-
takes **both** the connection id and the subscription id.
|
|
410
|
+
takes **both** the connection id and the subscription id. `subscribe` throws
|
|
411
|
+
when the id is already registered on that connection rather than overwriting
|
|
412
|
+
it.
|
|
384
413
|
|
|
385
414
|
### PubSub
|
|
386
415
|
|
|
@@ -409,6 +438,8 @@ interface PubSubConfig {
|
|
|
409
438
|
* events instead of an unbounded queue. Unlimited by default.
|
|
410
439
|
*/
|
|
411
440
|
maxQueueSize?: number;
|
|
441
|
+
/** Destination for subscriber callback errors (default `console`) */
|
|
442
|
+
logger?: WebSocketLogger;
|
|
412
443
|
}
|
|
413
444
|
```
|
|
414
445
|
|
package/dist/handler.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { type GraphQLSchema } from 'graphql';
|
|
|
8
8
|
import type { ServerWebSocket } from 'bun';
|
|
9
9
|
import type { GraphQLRequest } from '@leaven-graphql/core';
|
|
10
10
|
import { type SubscriptionManagerConfig } from './manager';
|
|
11
|
+
import type { WebSocketLogger } from './logger';
|
|
11
12
|
/**
|
|
12
13
|
* WebSocket context data
|
|
13
14
|
*/
|
|
@@ -39,6 +40,12 @@ export interface WebSocketHandlerConfig<TContext = unknown> {
|
|
|
39
40
|
keepAliveInterval?: number;
|
|
40
41
|
/** Subscription manager config */
|
|
41
42
|
subscriptionManager?: Omit<SubscriptionManagerConfig, 'schema'>;
|
|
43
|
+
/**
|
|
44
|
+
* Where diagnostics (failing hooks and callbacks) are logged. Defaults to
|
|
45
|
+
* `console`. Also passed to the subscription manager unless
|
|
46
|
+
* `subscriptionManager.logger` is set.
|
|
47
|
+
*/
|
|
48
|
+
logger?: WebSocketLogger;
|
|
42
49
|
/** Called when a connection is initialized */
|
|
43
50
|
onConnect?: (socket: ServerWebSocket<WebSocketContext>, params?: Record<string, unknown>) => boolean | Promise<boolean>;
|
|
44
51
|
/** Called when a connection is closed */
|
|
@@ -61,9 +68,19 @@ export declare class WebSocketHandler<TContext = unknown> {
|
|
|
61
68
|
private readonly onDisconnect?;
|
|
62
69
|
private readonly onSubscribe?;
|
|
63
70
|
private readonly onComplete?;
|
|
71
|
+
private readonly logger;
|
|
64
72
|
private readonly initTimeouts;
|
|
65
73
|
private readonly keepAliveTimers;
|
|
74
|
+
private readonly states;
|
|
66
75
|
constructor(config: WebSocketHandlerConfig<TContext>);
|
|
76
|
+
/**
|
|
77
|
+
* Lifecycle state for a socket, created on first use.
|
|
78
|
+
*/
|
|
79
|
+
private stateOf;
|
|
80
|
+
/**
|
|
81
|
+
* Stop and forget a connection's keep-alive timer, if it has one.
|
|
82
|
+
*/
|
|
83
|
+
private clearKeepAlive;
|
|
67
84
|
/**
|
|
68
85
|
* Generate a unique connection ID
|
|
69
86
|
*/
|
|
@@ -104,7 +121,11 @@ export declare class WebSocketHandler<TContext = unknown> {
|
|
|
104
121
|
* A document graphql-js cannot resolve to a single operation (no operations
|
|
105
122
|
* at all, or several with no `operationName`) is reported as a subscription
|
|
106
123
|
* so the executor produces the precise error rather than this method
|
|
107
|
-
* guessing at one.
|
|
124
|
+
* guessing at one. The same goes for a document that fails to parse.
|
|
125
|
+
*
|
|
126
|
+
* Parsing goes through the executor, so the configured `maxTokens` and
|
|
127
|
+
* depth limits apply here too; a plain `parse()` would lex an arbitrarily
|
|
128
|
+
* large document before the executor's limits ever ran.
|
|
108
129
|
*/
|
|
109
130
|
private isSubscriptionOperation;
|
|
110
131
|
/**
|
package/dist/handler.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,
|
|
1
|
+
{"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../src/handler.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAyD,KAAK,aAAa,EAAE,MAAM,SAAS,CAAC;AACpG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,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,CAAC;IAqBpD;;OAEG;IACH,OAAO,CAAC,OAAO;IASf;;OAEG;IACH,OAAO,CAAC,cAAc;IAQtB;;OAEG;IACH,OAAO,CAAC,oBAAoB;IAI5B;;;;;;;;OAQG;IACH,OAAO,CAAC,UAAU;IAalB;;OAEG;IACI,UAAU,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,GAAG,IAAI;IAoBlE;;OAEG;IACU,aAAa,CACxB,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,EACzC,OAAO,EAAE,MAAM,GAAG,MAAM,GACvB,OAAO,CAAC,IAAI,CAAC;IAYhB;;OAEG;YACW,cAAc;IAqC5B;;OAEG;YACW,oBAAoB;IAwDlC;;OAEG;YACW,eAAe;IAkI7B;;;;;;;;;;;OAWG;YACW,uBAAuB;IAWrC;;OAEG;IACH,OAAO,CAAC,cAAc;IAoBtB;;OAEG;IACU,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IA8BlF;;OAEG;IACI,kBAAkB,IAAI;QAC3B,IAAI,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAC;QAC1D,OAAO,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,KAAK,IAAI,CAAC;QACvF,KAAK,EAAE,CAAC,MAAM,EAAE,eAAe,CAAC,gBAAgB,CAAC,KAAK,IAAI,CAAC;KAC5D;CAkBF;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,QAAQ,GAAG,OAAO,EACvD,MAAM,EAAE,sBAAsB,CAAC,QAAQ,CAAC,GACvC,gBAAgB,CAAC,QAAQ,CAAC,CAE5B"}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export type { WebSocketHandlerConfig, WebSocketContext } from './handler';
|
|
|
9
9
|
export { SubscriptionManager, createSubscriptionManager } from './manager';
|
|
10
10
|
export type { SubscriptionManagerConfig, Subscription, SubscriptionStatus } from './manager';
|
|
11
11
|
export { PubSub, createPubSub } from './pubsub';
|
|
12
|
+
export type { WebSocketLogger } from './logger';
|
|
12
13
|
export type { PubSubConfig, PubSubEngine, SubscribeFn, PublishFn } from './pubsub';
|
|
13
14
|
export { MessageType, parseMessage, formatMessage, createConnectionAck, createNextMessage, createErrorMessage, createCompleteMessage, createPongMessage, type ConnectionInitMessage, type ConnectionAckMessage, type SubscribeMessage, type NextMessage, type ErrorMessage, type CompleteMessage, type PingMessage, type PongMessage, type ParseMessageOptions, } from './protocol';
|
|
14
15
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC;AACrE,YAAY,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAE1E,OAAO,EAAE,mBAAmB,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAC3E,YAAY,EAAE,yBAAyB,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAE7F,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAChD,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEnF,OAAO,EACL,WAAW,EACX,YAAY,EACZ,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,kBAAkB,EAClB,qBAAqB,EACrB,iBAAiB,EACjB,KAAK,qBAAqB,EAC1B,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,EACrB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAA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