@trpc/server 11.0.0-alpha-tmp-subscription-connection-state.489 → 11.0.0-alpha-tmp-12-06-react.667
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/dist/@trpc/server/http.d.ts +1 -1
- package/dist/@trpc/server/http.d.ts.map +1 -1
- package/dist/@trpc/server/index.d.ts +1 -1
- package/dist/@trpc/server/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/getPlanner.js +2 -2
- package/dist/adapters/aws-lambda/getPlanner.mjs +2 -2
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.js +2 -0
- package/dist/adapters/aws-lambda/index.mjs +2 -0
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +27 -7
- package/dist/adapters/express.mjs +27 -7
- package/dist/adapters/fastify/fastifyRequestHandler.js +3 -1
- package/dist/adapters/fastify/fastifyRequestHandler.mjs +3 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.js +9 -2
- package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +10 -3
- package/dist/adapters/fetch/fetchRequestHandler.js +9 -8
- package/dist/adapters/fetch/fetchRequestHandler.mjs +9 -8
- package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/nextAppDirCaller.js +10 -5
- package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +10 -5
- package/dist/adapters/next-app-dir/notFound.js +2 -0
- package/dist/adapters/next-app-dir/notFound.mjs +2 -0
- package/dist/adapters/next-app-dir/redirect.js +16 -1
- package/dist/adapters/next-app-dir/redirect.mjs +16 -1
- package/dist/adapters/next-app-dir/rethrowNextErrors.d.ts.map +1 -1
- package/dist/adapters/next-app-dir/rethrowNextErrors.js +4 -3
- package/dist/adapters/next-app-dir/rethrowNextErrors.mjs +4 -3
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +23 -30
- package/dist/adapters/next.mjs +24 -31
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts +2 -1
- package/dist/adapters/node-http/incomingMessageToRequest.d.ts.map +1 -1
- package/dist/adapters/node-http/incomingMessageToRequest.js +83 -40
- package/dist/adapters/node-http/incomingMessageToRequest.mjs +83 -41
- package/dist/adapters/node-http/index.js +2 -0
- package/dist/adapters/node-http/index.mjs +2 -2
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +7 -0
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.js +70 -58
- package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +70 -59
- package/dist/adapters/node-http/writeResponse.d.ts +18 -0
- package/dist/adapters/node-http/writeResponse.d.ts.map +1 -0
- package/dist/adapters/node-http/writeResponse.js +82 -0
- package/dist/adapters/node-http/writeResponse.mjs +79 -0
- package/dist/adapters/standalone.d.ts +5 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +25 -13
- package/dist/adapters/standalone.mjs +26 -14
- package/dist/adapters/ws.d.ts +14 -4
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +217 -101
- package/dist/adapters/ws.mjs +217 -102
- package/dist/bundle-analysis.json +415 -313
- package/dist/http.js +2 -4
- package/dist/http.mjs +2 -2
- package/dist/index.js +3 -0
- package/dist/index.mjs +3 -1
- package/dist/node_modules/.pnpm/@rollup_plugin-typescript@12.1.1_rollup@4.27.4_tslib@2.8.1_typescript@5.7.2/node_modules/tslib/tslib.es6.js +73 -0
- package/dist/observable/behaviorSubject.d.ts +15 -0
- package/dist/observable/behaviorSubject.d.ts.map +1 -0
- package/dist/observable/behaviorSubject.js +40 -0
- package/dist/observable/behaviorSubject.mjs +38 -0
- package/dist/observable/index.d.ts +3 -3
- package/dist/observable/index.d.ts.map +1 -1
- package/dist/observable/index.js +5 -0
- package/dist/observable/index.mjs +3 -2
- package/dist/observable/observable.d.ts +2 -1
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/observable/observable.js +31 -8
- package/dist/observable/observable.mjs +31 -8
- package/dist/observable/operators.d.ts +2 -0
- package/dist/observable/operators.d.ts.map +1 -1
- package/dist/observable/operators.js +35 -0
- package/dist/observable/operators.mjs +34 -1
- package/dist/rpc.js +2 -0
- package/dist/rpc.mjs +2 -0
- package/dist/shared.js +2 -0
- package/dist/shared.mjs +2 -0
- package/dist/unstable-core-do-not-import/clientish/inference.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/clientish/inferrable.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/clientish/serialize.d.ts +2 -2
- package/dist/unstable-core-do-not-import/clientish/serialize.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/createProxy.js +1 -1
- package/dist/unstable-core-do-not-import/createProxy.mjs +1 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.d.ts +1 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.js +16 -1
- package/dist/unstable-core-do-not-import/error/TRPCError.mjs +16 -1
- package/dist/unstable-core-do-not-import/error/formatter.js +1 -1
- package/dist/unstable-core-do-not-import/error/formatter.mjs +1 -1
- package/dist/unstable-core-do-not-import/error/getErrorShape.js +2 -2
- package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +2 -2
- package/dist/unstable-core-do-not-import/http/contentType.js +3 -3
- package/dist/unstable-core-do-not-import/http/contentType.mjs +3 -3
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts +6 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +28 -0
- package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +25 -1
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts +4 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.js +9 -0
- package/dist/unstable-core-do-not-import/http/isAbortError.mjs +7 -0
- package/dist/unstable-core-do-not-import/http/resolveResponse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/http/resolveResponse.js +180 -96
- package/dist/unstable-core-do-not-import/http/resolveResponse.mjs +182 -98
- package/dist/unstable-core-do-not-import/http/types.d.ts +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.d.ts +11 -11
- package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/initTRPC.js +11 -11
- package/dist/unstable-core-do-not-import/initTRPC.mjs +11 -11
- package/dist/unstable-core-do-not-import/middleware.d.ts +2 -1
- package/dist/unstable-core-do-not-import/middleware.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/middleware.js +2 -2
- package/dist/unstable-core-do-not-import/middleware.mjs +2 -2
- package/dist/unstable-core-do-not-import/parser.d.ts +5 -1
- package/dist/unstable-core-do-not-import/parser.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/parser.js +4 -0
- package/dist/unstable-core-do-not-import/parser.mjs +4 -0
- package/dist/unstable-core-do-not-import/procedure.d.ts +8 -2
- package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +31 -12
- package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/procedureBuilder.js +33 -39
- package/dist/unstable-core-do-not-import/procedureBuilder.mjs +33 -39
- package/dist/unstable-core-do-not-import/rootConfig.d.ts +17 -17
- package/dist/unstable-core-do-not-import/rootConfig.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.d.ts +4 -3
- package/dist/unstable-core-do-not-import/router.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/router.js +6 -5
- package/dist/unstable-core-do-not-import/router.mjs +6 -5
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts +2 -9
- package/dist/unstable-core-do-not-import/rpc/codes.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts +7 -10
- package/dist/unstable-core-do-not-import/rpc/envelopes.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.js +2 -2
- package/dist/unstable-core-do-not-import/rpc/parseTRPCMessage.mjs +2 -2
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts +25 -14
- package/dist/unstable-core-do-not-import/stream/jsonl.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/jsonl.js +439 -318
- package/dist/unstable-core-do-not-import/stream/jsonl.mjs +439 -318
- package/dist/unstable-core-do-not-import/stream/sse.d.ts +58 -18
- package/dist/unstable-core-do-not-import/stream/sse.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/sse.js +297 -122
- package/dist/unstable-core-do-not-import/stream/sse.mjs +298 -123
- package/dist/unstable-core-do-not-import/stream/sse.types.d.ts +31 -0
- package/dist/unstable-core-do-not-import/stream/sse.types.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/tracked.d.ts +2 -2
- package/dist/unstable-core-do-not-import/stream/tracked.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/tracked.js +1 -1
- package/dist/unstable-core-do-not-import/stream/tracked.mjs +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts +17 -0
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.js +171 -0
- package/dist/unstable-core-do-not-import/stream/utils/asyncIterable.mjs +167 -0
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts +0 -11
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.js +0 -29
- package/dist/unstable-core-do-not-import/stream/utils/createDeferred.mjs +1 -29
- package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts +17 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposable.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposable.js +44 -0
- package/dist/unstable-core-do-not-import/stream/utils/disposable.mjs +41 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.d.ts +8 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.js +25 -0
- package/dist/unstable-core-do-not-import/stream/utils/readableStreamFrom.mjs +23 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts +5 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.js +26 -0
- package/dist/unstable-core-do-not-import/stream/utils/timerResource.mjs +23 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts +7 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.d.ts.map +1 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.js +130 -0
- package/dist/unstable-core-do-not-import/stream/utils/withPing.mjs +127 -0
- package/dist/unstable-core-do-not-import/transformer.d.ts +2 -5
- package/dist/unstable-core-do-not-import/transformer.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/transformer.js +1 -1
- package/dist/unstable-core-do-not-import/transformer.mjs +1 -1
- package/dist/unstable-core-do-not-import/types.d.ts +26 -3
- package/dist/unstable-core-do-not-import/types.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.d.ts +15 -0
- package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import/utils.js +49 -1
- package/dist/unstable-core-do-not-import/utils.mjs +45 -2
- package/dist/unstable-core-do-not-import.d.ts +3 -2
- package/dist/unstable-core-do-not-import.d.ts.map +1 -1
- package/dist/unstable-core-do-not-import.js +14 -7
- package/dist/unstable-core-do-not-import.mjs +5 -6
- package/dist/vendor/unpromise/index.d.ts +3 -0
- package/dist/vendor/unpromise/index.d.ts.map +1 -0
- package/dist/vendor/unpromise/types.d.ts +28 -0
- package/dist/vendor/unpromise/types.d.ts.map +1 -0
- package/dist/vendor/unpromise/unpromise.d.ts +121 -0
- package/dist/vendor/unpromise/unpromise.d.ts.map +1 -0
- package/dist/vendor/unpromise/unpromise.js +292 -0
- package/dist/vendor/unpromise/unpromise.mjs +289 -0
- package/package.json +20 -17
- package/src/@trpc/server/http.ts +0 -2
- package/src/@trpc/server/index.ts +2 -0
- package/src/adapters/aws-lambda/getPlanner.ts +2 -2
- package/src/adapters/aws-lambda/index.ts +5 -6
- package/src/adapters/express.ts +21 -9
- package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +16 -5
- package/src/adapters/fetch/fetchRequestHandler.ts +2 -2
- package/src/adapters/next-app-dir/nextAppDirCaller.ts +3 -0
- package/src/adapters/next-app-dir/rethrowNextErrors.ts +3 -2
- package/src/adapters/next.ts +29 -34
- package/src/adapters/node-http/incomingMessageToRequest.ts +110 -40
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +85 -63
- package/src/adapters/node-http/writeResponse.ts +94 -0
- package/src/adapters/standalone.ts +35 -18
- package/src/adapters/ws.ts +111 -57
- package/src/observable/behaviorSubject.ts +55 -0
- package/src/observable/index.ts +22 -3
- package/src/observable/observable.ts +32 -14
- package/src/observable/operators.ts +47 -0
- package/src/unstable-core-do-not-import/clientish/inference.ts +4 -4
- package/src/unstable-core-do-not-import/clientish/inferrable.ts +6 -6
- package/src/unstable-core-do-not-import/clientish/serialize.ts +5 -5
- package/src/unstable-core-do-not-import/http/getHTTPStatusCode.ts +37 -4
- package/src/unstable-core-do-not-import/http/isAbortError.ts +7 -0
- package/src/unstable-core-do-not-import/http/resolveResponse.ts +185 -90
- package/src/unstable-core-do-not-import/http/types.ts +1 -1
- package/src/unstable-core-do-not-import/initTRPC.ts +14 -20
- package/src/unstable-core-do-not-import/middleware.ts +5 -4
- package/src/unstable-core-do-not-import/parser.ts +17 -6
- package/src/unstable-core-do-not-import/procedure.ts +17 -2
- package/src/unstable-core-do-not-import/procedureBuilder.ts +137 -122
- package/src/unstable-core-do-not-import/rootConfig.ts +20 -20
- package/src/unstable-core-do-not-import/router.ts +15 -16
- package/src/unstable-core-do-not-import/rpc/codes.ts +2 -10
- package/src/unstable-core-do-not-import/rpc/envelopes.ts +7 -12
- package/src/unstable-core-do-not-import/stream/jsonl.ts +287 -296
- package/src/unstable-core-do-not-import/stream/sse.ts +338 -171
- package/src/unstable-core-do-not-import/stream/sse.types.ts +44 -0
- package/src/unstable-core-do-not-import/stream/tracked.ts +5 -11
- package/src/unstable-core-do-not-import/stream/utils/asyncIterable.ts +89 -0
- package/src/unstable-core-do-not-import/stream/utils/createDeferred.ts +0 -36
- package/src/unstable-core-do-not-import/stream/utils/disposable.ts +52 -0
- package/src/unstable-core-do-not-import/stream/utils/readableStreamFrom.ts +28 -0
- package/src/unstable-core-do-not-import/stream/utils/timerResource.ts +29 -0
- package/src/unstable-core-do-not-import/stream/utils/withPing.ts +47 -0
- package/src/unstable-core-do-not-import/transformer.ts +1 -1
- package/src/unstable-core-do-not-import/types.ts +35 -10
- package/src/unstable-core-do-not-import/utils.ts +56 -1
- package/src/unstable-core-do-not-import.ts +3 -2
- package/src/vendor/unpromise/ATTRIBUTION.txt +1 -0
- package/src/vendor/unpromise/LICENSE +20 -0
- package/src/vendor/unpromise/index.ts +7 -0
- package/src/vendor/unpromise/types.ts +55 -0
- package/src/vendor/unpromise/unpromise.ts +380 -0
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts +0 -24
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.js +0 -32
- package/dist/unstable-core-do-not-import/http/batchStreamFormatter.mjs +0 -30
- package/dist/unstable-core-do-not-import/http/toURL.d.ts +0 -2
- package/dist/unstable-core-do-not-import/http/toURL.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/http/toURL.js +0 -8
- package/dist/unstable-core-do-not-import/http/toURL.mjs +0 -6
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts +0 -10
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.js +0 -31
- package/dist/unstable-core-do-not-import/stream/utils/createReadableStream.mjs +0 -29
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts +0 -7
- package/dist/unstable-core-do-not-import/stream/utils/createServer.d.ts.map +0 -1
- package/dist/unstable-core-do-not-import/types.js +0 -11
- package/dist/unstable-core-do-not-import/types.mjs +0 -9
- package/src/unstable-core-do-not-import/http/batchStreamFormatter.ts +0 -29
- package/src/unstable-core-do-not-import/http/toURL.ts +0 -7
- package/src/unstable-core-do-not-import/stream/utils/createReadableStream.ts +0 -31
- package/src/unstable-core-do-not-import/stream/utils/createServer.ts +0 -44
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MaybePromise } from '../types';
|
|
2
|
+
import type { EventSourceLike } from './sse.types';
|
|
2
3
|
import type { inferTrackedOutput } from './tracked';
|
|
3
4
|
type Serialize = (value: any) => any;
|
|
4
5
|
type Deserialize = (value: any) => any;
|
|
5
6
|
/**
|
|
6
7
|
* @internal
|
|
7
8
|
*/
|
|
8
|
-
export interface
|
|
9
|
+
export interface SSEPingOptions {
|
|
9
10
|
/**
|
|
10
11
|
* Enable ping comments sent from the server
|
|
11
12
|
* @default false
|
|
@@ -17,14 +18,20 @@ export interface PingOptions {
|
|
|
17
18
|
*/
|
|
18
19
|
intervalMs?: number;
|
|
19
20
|
}
|
|
20
|
-
export interface
|
|
21
|
+
export interface SSEClientOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Timeout and reconnect after inactivity in milliseconds
|
|
24
|
+
* @default undefined
|
|
25
|
+
*/
|
|
26
|
+
reconnectAfterInactivityMs?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface SSEStreamProducerOptions<TValue = unknown> {
|
|
21
29
|
serialize?: Serialize;
|
|
22
|
-
data: AsyncIterable<
|
|
30
|
+
data: AsyncIterable<TValue>;
|
|
23
31
|
maxDepth?: number;
|
|
24
|
-
ping?:
|
|
32
|
+
ping?: SSEPingOptions;
|
|
25
33
|
/**
|
|
26
34
|
* Maximum duration in milliseconds for the request before ending the stream
|
|
27
|
-
* Only useful for serverless runtimes
|
|
28
35
|
* @default undefined
|
|
29
36
|
*/
|
|
30
37
|
maxDurationMs?: number;
|
|
@@ -37,27 +44,60 @@ export interface SSEStreamProducerOptions {
|
|
|
37
44
|
formatError?: (opts: {
|
|
38
45
|
error: unknown;
|
|
39
46
|
}) => unknown;
|
|
47
|
+
/**
|
|
48
|
+
* Client-specific options - these will be sent to the client as part of the first message
|
|
49
|
+
* @default {}
|
|
50
|
+
*/
|
|
51
|
+
client?: SSEClientOptions;
|
|
40
52
|
}
|
|
41
53
|
/**
|
|
42
54
|
*
|
|
43
55
|
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
44
56
|
*/
|
|
45
|
-
export declare function sseStreamProducer(opts: SSEStreamProducerOptions): ReadableStream<string>;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
export declare function sseStreamProducer<TValue = unknown>(opts: SSEStreamProducerOptions<TValue>): ReadableStream<string>;
|
|
58
|
+
interface ConsumerStreamResultBase<TConfig extends ConsumerConfig> {
|
|
59
|
+
eventSource: InstanceType<TConfig['EventSource']> | null;
|
|
60
|
+
}
|
|
61
|
+
interface ConsumerStreamResultData<TConfig extends ConsumerConfig> extends ConsumerStreamResultBase<TConfig> {
|
|
62
|
+
type: 'data';
|
|
63
|
+
data: inferTrackedOutput<TConfig['data']>;
|
|
64
|
+
}
|
|
65
|
+
interface ConsumerStreamResultError<TConfig extends ConsumerConfig> extends ConsumerStreamResultBase<TConfig> {
|
|
66
|
+
type: 'serialized-error';
|
|
67
|
+
error: TConfig['error'];
|
|
68
|
+
}
|
|
69
|
+
interface ConsumerStreamResultConnecting<TConfig extends ConsumerConfig> extends ConsumerStreamResultBase<TConfig> {
|
|
70
|
+
type: 'connecting';
|
|
71
|
+
event: EventSourceLike.EventOf<TConfig['EventSource']> | null;
|
|
72
|
+
}
|
|
73
|
+
interface ConsumerStreamResultTimeout<TConfig extends ConsumerConfig> extends ConsumerStreamResultBase<TConfig> {
|
|
74
|
+
type: 'timeout';
|
|
75
|
+
ms: number;
|
|
76
|
+
}
|
|
77
|
+
interface ConsumerStreamResultPing<TConfig extends ConsumerConfig> extends ConsumerStreamResultBase<TConfig> {
|
|
78
|
+
type: 'ping';
|
|
79
|
+
}
|
|
80
|
+
interface ConsumerStreamResultConnected<TConfig extends ConsumerConfig> extends ConsumerStreamResultBase<TConfig> {
|
|
81
|
+
type: 'connected';
|
|
82
|
+
options: SSEClientOptions;
|
|
83
|
+
}
|
|
84
|
+
type ConsumerStreamResult<TConfig extends ConsumerConfig> = ConsumerStreamResultData<TConfig> | ConsumerStreamResultError<TConfig> | ConsumerStreamResultConnecting<TConfig> | ConsumerStreamResultTimeout<TConfig> | ConsumerStreamResultPing<TConfig> | ConsumerStreamResultConnected<TConfig>;
|
|
85
|
+
export interface SSEStreamConsumerOptions<TConfig extends ConsumerConfig> {
|
|
86
|
+
url: () => MaybePromise<string>;
|
|
87
|
+
init: () => MaybePromise<EventSourceLike.InitDictOf<TConfig['EventSource']>> | undefined;
|
|
88
|
+
signal: AbortSignal;
|
|
89
|
+
deserialize?: Deserialize;
|
|
90
|
+
EventSource: TConfig['EventSource'];
|
|
91
|
+
}
|
|
92
|
+
interface ConsumerConfig {
|
|
93
|
+
data: unknown;
|
|
51
94
|
error: unknown;
|
|
52
|
-
|
|
95
|
+
EventSource: EventSourceLike.AnyConstructor;
|
|
96
|
+
}
|
|
53
97
|
/**
|
|
54
98
|
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
55
99
|
*/
|
|
56
|
-
export declare function sseStreamConsumer<
|
|
57
|
-
from: EventSource;
|
|
58
|
-
onError?: ConsumerOnError;
|
|
59
|
-
deserialize?: Deserialize;
|
|
60
|
-
}): AsyncIterable<ConsumerStreamResult<TData>>;
|
|
100
|
+
export declare function sseStreamConsumer<TConfig extends ConsumerConfig>(opts: SSEStreamConsumerOptions<TConfig>): AsyncIterable<ConsumerStreamResult<TConfig>>;
|
|
61
101
|
export declare const sseHeaders: {
|
|
62
102
|
readonly 'Content-Type': "text/event-stream";
|
|
63
103
|
readonly 'Cache-Control': "no-cache, no-transform";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/stream/sse.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../../src/unstable-core-do-not-import/stream/sse.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAWpD,KAAK,SAAS,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC;AACrC,KAAK,WAAW,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK,GAAG,CAAC;AAEvC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,OAAO,CAAC;IACjB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,0BAA0B,CAAC,EAAE,MAAM,CAAC;CACrC;AAED,MAAM,WAAW,wBAAwB,CAAC,MAAM,GAAG,OAAO;IACxD,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IAE5B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,IAAI,CAAC,EAAE,cAAc,CAAC;IACtB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;OAIG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,KAAK,EAAE,OAAO,CAAA;KAAE,KAAK,OAAO,CAAC;IACpD;;;OAGG;IACH,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B;AAYD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,GAAG,OAAO,EAChD,IAAI,EAAE,wBAAwB,CAAC,MAAM,CAAC,0BAmHvC;AAED,UAAU,wBAAwB,CAAC,OAAO,SAAS,cAAc;IAC/D,WAAW,EAAE,YAAY,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,GAAG,IAAI,CAAC;CAC1D;AAED,UAAU,wBAAwB,CAAC,OAAO,SAAS,cAAc,CAC/D,SAAQ,wBAAwB,CAAC,OAAO,CAAC;IACzC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,kBAAkB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;CAC3C;AAED,UAAU,yBAAyB,CAAC,OAAO,SAAS,cAAc,CAChE,SAAQ,wBAAwB,CAAC,OAAO,CAAC;IACzC,IAAI,EAAE,kBAAkB,CAAC;IACzB,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;CACzB;AAED,UAAU,8BAA8B,CAAC,OAAO,SAAS,cAAc,CACrE,SAAQ,wBAAwB,CAAC,OAAO,CAAC;IACzC,IAAI,EAAE,YAAY,CAAC;IACnB,KAAK,EAAE,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,GAAG,IAAI,CAAC;CAC/D;AACD,UAAU,2BAA2B,CAAC,OAAO,SAAS,cAAc,CAClE,SAAQ,wBAAwB,CAAC,OAAO,CAAC;IACzC,IAAI,EAAE,SAAS,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;CACZ;AACD,UAAU,wBAAwB,CAAC,OAAO,SAAS,cAAc,CAC/D,SAAQ,wBAAwB,CAAC,OAAO,CAAC;IACzC,IAAI,EAAE,MAAM,CAAC;CACd;AAED,UAAU,6BAA6B,CAAC,OAAO,SAAS,cAAc,CACpE,SAAQ,wBAAwB,CAAC,OAAO,CAAC;IACzC,IAAI,EAAE,WAAW,CAAC;IAClB,OAAO,EAAE,gBAAgB,CAAC;CAC3B;AAED,KAAK,oBAAoB,CAAC,OAAO,SAAS,cAAc,IACpD,wBAAwB,CAAC,OAAO,CAAC,GACjC,yBAAyB,CAAC,OAAO,CAAC,GAClC,8BAA8B,CAAC,OAAO,CAAC,GACvC,2BAA2B,CAAC,OAAO,CAAC,GACpC,wBAAwB,CAAC,OAAO,CAAC,GACjC,6BAA6B,CAAC,OAAO,CAAC,CAAC;AAE3C,MAAM,WAAW,wBAAwB,CAAC,OAAO,SAAS,cAAc;IACtE,GAAG,EAAE,MAAM,YAAY,CAAC,MAAM,CAAC,CAAC;IAChC,IAAI,EAAE,MACF,YAAY,CAAC,eAAe,CAAC,UAAU,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,GAChE,SAAS,CAAC;IACd,MAAM,EAAE,WAAW,CAAC;IACpB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,WAAW,EAAE,OAAO,CAAC,aAAa,CAAC,CAAC;CACrC;AAED,UAAU,cAAc;IACtB,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,OAAO,CAAC;IACf,WAAW,EAAE,eAAe,CAAC,cAAc,CAAC;CAC7C;AAgBD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,SAAS,cAAc,EAC9D,IAAI,EAAE,wBAAwB,CAAC,OAAO,CAAC,GACtC,aAAa,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC,CA6J9C;AAED,eAAO,MAAM,UAAU;;;;;CAKb,CAAC"}
|
|
@@ -1,90 +1,164 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var unpromise = require('../../vendor/unpromise/unpromise.js');
|
|
3
4
|
var TRPCError = require('../error/TRPCError.js');
|
|
5
|
+
var isAbortError = require('../http/isAbortError.js');
|
|
4
6
|
var utils = require('../utils.js');
|
|
5
7
|
var tracked = require('./tracked.js');
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
+
var asyncIterable = require('./utils/asyncIterable.js');
|
|
9
|
+
var disposable = require('./utils/disposable.js');
|
|
10
|
+
var readableStreamFrom = require('./utils/readableStreamFrom.js');
|
|
11
|
+
var timerResource = require('./utils/timerResource.js');
|
|
12
|
+
var withPing = require('./utils/withPing.js');
|
|
8
13
|
|
|
14
|
+
function _ts_add_disposable_resource(env, value, async) {
|
|
15
|
+
if (value !== null && value !== void 0) {
|
|
16
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
17
|
+
var dispose, inner;
|
|
18
|
+
if (async) {
|
|
19
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
20
|
+
dispose = value[Symbol.asyncDispose];
|
|
21
|
+
}
|
|
22
|
+
if (dispose === void 0) {
|
|
23
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
24
|
+
dispose = value[Symbol.dispose];
|
|
25
|
+
if (async) inner = dispose;
|
|
26
|
+
}
|
|
27
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
28
|
+
if (inner) dispose = function() {
|
|
29
|
+
try {
|
|
30
|
+
inner.call(this);
|
|
31
|
+
} catch (e) {
|
|
32
|
+
return Promise.reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
env.stack.push({
|
|
36
|
+
value: value,
|
|
37
|
+
dispose: dispose,
|
|
38
|
+
async: async
|
|
39
|
+
});
|
|
40
|
+
} else if (async) {
|
|
41
|
+
env.stack.push({
|
|
42
|
+
async: true
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
function _ts_dispose_resources(env) {
|
|
48
|
+
var _SuppressedError = typeof SuppressedError === "function" ? SuppressedError : function(error, suppressed, message) {
|
|
49
|
+
var e = new Error(message);
|
|
50
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
51
|
+
};
|
|
52
|
+
return (_ts_dispose_resources = function _ts_dispose_resources(env) {
|
|
53
|
+
function fail(e) {
|
|
54
|
+
env.error = env.hasError ? new _SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
55
|
+
env.hasError = true;
|
|
56
|
+
}
|
|
57
|
+
var r, s = 0;
|
|
58
|
+
function next() {
|
|
59
|
+
while(r = env.stack.pop()){
|
|
60
|
+
try {
|
|
61
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
62
|
+
if (r.dispose) {
|
|
63
|
+
var result = r.dispose.call(r.value);
|
|
64
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) {
|
|
65
|
+
fail(e);
|
|
66
|
+
return next();
|
|
67
|
+
});
|
|
68
|
+
} else s |= 1;
|
|
69
|
+
} catch (e) {
|
|
70
|
+
fail(e);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
74
|
+
if (env.hasError) throw env.error;
|
|
75
|
+
}
|
|
76
|
+
return next();
|
|
77
|
+
})(env);
|
|
78
|
+
}
|
|
79
|
+
const PING_EVENT = 'ping';
|
|
9
80
|
const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
81
|
+
const CONNECTED_EVENT = 'connected';
|
|
10
82
|
/**
|
|
11
83
|
*
|
|
12
84
|
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
13
85
|
*/ function sseStreamProducer(opts) {
|
|
14
|
-
const
|
|
15
|
-
stream.controller.enqueue({
|
|
16
|
-
comment: 'connected'
|
|
17
|
-
});
|
|
18
|
-
const { serialize =(v)=>v } = opts;
|
|
86
|
+
const { serialize = utils.identity } = opts;
|
|
19
87
|
const ping = {
|
|
20
88
|
enabled: opts.ping?.enabled ?? false,
|
|
21
89
|
intervalMs: opts.ping?.intervalMs ?? 1000
|
|
22
90
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
91
|
+
const client = opts.client ?? {};
|
|
92
|
+
if (ping.enabled && client.reconnectAfterInactivityMs && ping.intervalMs > client.reconnectAfterInactivityMs) {
|
|
93
|
+
throw new Error(`Ping interval must be less than client reconnect interval to prevent unnecessary reconnection - ping.intervalMs: ${ping.intervalMs} client.reconnectAfterInactivityMs: ${client.reconnectAfterInactivityMs}`);
|
|
94
|
+
}
|
|
95
|
+
async function* generator() {
|
|
96
|
+
yield {
|
|
97
|
+
event: CONNECTED_EVENT,
|
|
98
|
+
data: JSON.stringify(client)
|
|
99
|
+
};
|
|
100
|
+
let iterable = opts.data;
|
|
101
|
+
if (opts.emitAndEndImmediately) {
|
|
102
|
+
iterable = asyncIterable.takeWithGrace(iterable, {
|
|
103
|
+
count: 1,
|
|
104
|
+
gracePeriodMs: 1
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
if (opts.maxDurationMs && opts.maxDurationMs > 0 && opts.maxDurationMs !== Infinity) {
|
|
108
|
+
iterable = asyncIterable.withMaxDuration(iterable, {
|
|
109
|
+
maxDurationMs: opts.maxDurationMs
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (ping.enabled && ping.intervalMs !== Infinity && ping.intervalMs > 0) {
|
|
113
|
+
iterable = withPing.withPing(iterable, ping.intervalMs);
|
|
114
|
+
}
|
|
115
|
+
// We need those declarations outside the loop for garbage collection reasons. If they were
|
|
116
|
+
// declared inside, they would not be freed until the next value is present.
|
|
117
|
+
let value;
|
|
118
|
+
let chunk;
|
|
119
|
+
for await (value of iterable){
|
|
120
|
+
if (value === withPing.PING_SYM) {
|
|
121
|
+
yield {
|
|
122
|
+
event: PING_EVENT,
|
|
123
|
+
data: ''
|
|
124
|
+
};
|
|
47
125
|
continue;
|
|
48
126
|
}
|
|
49
|
-
|
|
50
|
-
const data = opts.formatError ? opts.formatError({
|
|
51
|
-
error: next
|
|
52
|
-
}) : null;
|
|
53
|
-
stream.controller.enqueue({
|
|
54
|
-
event: SERIALIZED_ERROR_EVENT,
|
|
55
|
-
data: JSON.stringify(serialize(data))
|
|
56
|
-
});
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
if (next.done) {
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
const value = next.value;
|
|
63
|
-
const chunk = tracked.isTrackedEnvelope(value) ? {
|
|
127
|
+
chunk = tracked.isTrackedEnvelope(value) ? {
|
|
64
128
|
id: value[0],
|
|
65
129
|
data: value[1]
|
|
66
130
|
} : {
|
|
67
131
|
data: value
|
|
68
132
|
};
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
// end the stream in the next tick so that we can send a few more events from the queue
|
|
75
|
-
setTimeout(maxDurationPromise.resolve, 1);
|
|
76
|
-
}
|
|
77
|
-
nextPromise = iterator.next();
|
|
133
|
+
chunk.data = JSON.stringify(serialize(chunk.data));
|
|
134
|
+
yield chunk;
|
|
135
|
+
// free up references for garbage collection
|
|
136
|
+
value = null;
|
|
137
|
+
chunk = null;
|
|
78
138
|
}
|
|
79
|
-
|
|
80
|
-
|
|
139
|
+
}
|
|
140
|
+
async function* generatorWithErrorHandling() {
|
|
81
141
|
try {
|
|
82
|
-
|
|
83
|
-
} catch
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
142
|
+
yield* generator();
|
|
143
|
+
} catch (cause) {
|
|
144
|
+
if (isAbortError.isAbortError(cause)) {
|
|
145
|
+
// ignore abort errors, send any other errors
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// `err` must be caused by `opts.data`, `JSON.stringify` or `serialize`.
|
|
149
|
+
// So, a user error in any case.
|
|
150
|
+
const error = TRPCError.getTRPCErrorFromUnknown(cause);
|
|
151
|
+
const data = opts.formatError?.({
|
|
152
|
+
error
|
|
153
|
+
}) ?? null;
|
|
154
|
+
yield {
|
|
155
|
+
event: SERIALIZED_ERROR_EVENT,
|
|
156
|
+
data: JSON.stringify(serialize(data))
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const stream = readableStreamFrom.readableStreamFrom(generatorWithErrorHandling());
|
|
161
|
+
return stream.pipeThrough(new TransformStream({
|
|
88
162
|
transform (chunk, controller) {
|
|
89
163
|
if ('event' in chunk) {
|
|
90
164
|
controller.enqueue(`event: ${chunk.event}\n`);
|
|
@@ -102,75 +176,176 @@ const SERIALIZED_ERROR_EVENT = 'serialized-error';
|
|
|
102
176
|
}
|
|
103
177
|
}));
|
|
104
178
|
}
|
|
179
|
+
async function withTimeout(opts) {
|
|
180
|
+
const env = {
|
|
181
|
+
stack: [],
|
|
182
|
+
error: void 0,
|
|
183
|
+
hasError: false
|
|
184
|
+
};
|
|
185
|
+
try {
|
|
186
|
+
const timeoutPromise = _ts_add_disposable_resource(env, timerResource.timerResource(opts.timeoutMs), false);
|
|
187
|
+
;
|
|
188
|
+
const res = await unpromise.Unpromise.race([
|
|
189
|
+
opts.promise,
|
|
190
|
+
timeoutPromise.start()
|
|
191
|
+
]);
|
|
192
|
+
if (res === timerResource.disposablePromiseTimerResult) {
|
|
193
|
+
return await opts.onTimeout();
|
|
194
|
+
}
|
|
195
|
+
return res;
|
|
196
|
+
} catch (e) {
|
|
197
|
+
env.error = e;
|
|
198
|
+
env.hasError = true;
|
|
199
|
+
} finally{
|
|
200
|
+
_ts_dispose_resources(env);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
105
203
|
/**
|
|
106
204
|
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html
|
|
107
205
|
*/ function sseStreamConsumer(opts) {
|
|
108
|
-
const { deserialize =(v)=>v
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
206
|
+
const { deserialize = (v)=>v } = opts;
|
|
207
|
+
let clientOptions = {};
|
|
208
|
+
const signal = opts.signal;
|
|
209
|
+
let _es = null;
|
|
210
|
+
const createStream = ()=>new ReadableStream({
|
|
211
|
+
async start (controller) {
|
|
212
|
+
const [url, init] = await Promise.all([
|
|
213
|
+
opts.url(),
|
|
214
|
+
opts.init()
|
|
215
|
+
]);
|
|
216
|
+
const eventSource = _es = new opts.EventSource(url, init);
|
|
115
217
|
controller.enqueue({
|
|
116
|
-
|
|
117
|
-
|
|
218
|
+
type: 'connecting',
|
|
219
|
+
eventSource: _es,
|
|
220
|
+
event: null
|
|
118
221
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (value.done) {
|
|
153
|
-
return {
|
|
154
|
-
value: undefined,
|
|
155
|
-
done: true
|
|
156
|
-
};
|
|
222
|
+
eventSource.addEventListener(CONNECTED_EVENT, (_msg)=>{
|
|
223
|
+
const msg = _msg;
|
|
224
|
+
const options = JSON.parse(msg.data);
|
|
225
|
+
clientOptions = options;
|
|
226
|
+
controller.enqueue({
|
|
227
|
+
type: 'connected',
|
|
228
|
+
options,
|
|
229
|
+
eventSource
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
eventSource.addEventListener(SERIALIZED_ERROR_EVENT, (_msg)=>{
|
|
233
|
+
const msg = _msg;
|
|
234
|
+
controller.enqueue({
|
|
235
|
+
type: 'serialized-error',
|
|
236
|
+
error: deserialize(JSON.parse(msg.data)),
|
|
237
|
+
eventSource
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
eventSource.addEventListener(PING_EVENT, ()=>{
|
|
241
|
+
controller.enqueue({
|
|
242
|
+
type: 'ping',
|
|
243
|
+
eventSource
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
eventSource.addEventListener('error', (event)=>{
|
|
247
|
+
if (eventSource.readyState === EventSource.CLOSED) {
|
|
248
|
+
controller.error(event);
|
|
249
|
+
} else {
|
|
250
|
+
controller.enqueue({
|
|
251
|
+
type: 'connecting',
|
|
252
|
+
eventSource,
|
|
253
|
+
event
|
|
254
|
+
});
|
|
157
255
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
reader.releaseLock();
|
|
165
|
-
return {
|
|
166
|
-
value: undefined,
|
|
167
|
-
done: true
|
|
256
|
+
});
|
|
257
|
+
eventSource.addEventListener('message', (_msg)=>{
|
|
258
|
+
const msg = _msg;
|
|
259
|
+
const chunk = deserialize(JSON.parse(msg.data));
|
|
260
|
+
const def = {
|
|
261
|
+
data: chunk
|
|
168
262
|
};
|
|
263
|
+
if (msg.lastEventId) {
|
|
264
|
+
def.id = msg.lastEventId;
|
|
265
|
+
}
|
|
266
|
+
controller.enqueue({
|
|
267
|
+
type: 'data',
|
|
268
|
+
data: def,
|
|
269
|
+
eventSource
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
const onAbort = ()=>{
|
|
273
|
+
controller.close();
|
|
274
|
+
eventSource.close();
|
|
275
|
+
};
|
|
276
|
+
if (signal.aborted) {
|
|
277
|
+
onAbort();
|
|
278
|
+
} else {
|
|
279
|
+
signal.addEventListener('abort', onAbort);
|
|
169
280
|
}
|
|
170
|
-
}
|
|
171
|
-
|
|
281
|
+
},
|
|
282
|
+
cancel () {
|
|
283
|
+
_es?.close();
|
|
284
|
+
}
|
|
285
|
+
});
|
|
286
|
+
const getStreamResource = ()=>{
|
|
287
|
+
let stream = createStream();
|
|
288
|
+
let reader = stream.getReader();
|
|
289
|
+
async function dispose() {
|
|
290
|
+
await reader.cancel();
|
|
291
|
+
_es = null;
|
|
172
292
|
}
|
|
293
|
+
return disposable.makeAsyncResource({
|
|
294
|
+
read () {
|
|
295
|
+
return reader.read();
|
|
296
|
+
},
|
|
297
|
+
async recreate () {
|
|
298
|
+
await dispose();
|
|
299
|
+
stream = createStream();
|
|
300
|
+
reader = stream.getReader();
|
|
301
|
+
}
|
|
302
|
+
}, dispose);
|
|
173
303
|
};
|
|
304
|
+
return utils.run(async function*() {
|
|
305
|
+
const env = {
|
|
306
|
+
stack: [],
|
|
307
|
+
error: void 0,
|
|
308
|
+
hasError: false
|
|
309
|
+
};
|
|
310
|
+
try {
|
|
311
|
+
const stream = _ts_add_disposable_resource(env, getStreamResource(), true);
|
|
312
|
+
;
|
|
313
|
+
while(true){
|
|
314
|
+
let promise = stream.read();
|
|
315
|
+
const timeoutMs = clientOptions.reconnectAfterInactivityMs;
|
|
316
|
+
if (timeoutMs) {
|
|
317
|
+
promise = withTimeout({
|
|
318
|
+
promise,
|
|
319
|
+
timeoutMs,
|
|
320
|
+
onTimeout: async ()=>{
|
|
321
|
+
const res = {
|
|
322
|
+
value: {
|
|
323
|
+
type: 'timeout',
|
|
324
|
+
ms: timeoutMs,
|
|
325
|
+
eventSource: _es
|
|
326
|
+
},
|
|
327
|
+
done: false
|
|
328
|
+
};
|
|
329
|
+
// Close and release old reader
|
|
330
|
+
await stream.recreate();
|
|
331
|
+
return res;
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
const result = await promise;
|
|
336
|
+
if (result.done) {
|
|
337
|
+
return result.value;
|
|
338
|
+
}
|
|
339
|
+
yield result.value;
|
|
340
|
+
}
|
|
341
|
+
} catch (e) {
|
|
342
|
+
env.error = e;
|
|
343
|
+
env.hasError = true;
|
|
344
|
+
} finally{
|
|
345
|
+
const result = _ts_dispose_resources(env);
|
|
346
|
+
if (result) await result;
|
|
347
|
+
}
|
|
348
|
+
});
|
|
174
349
|
}
|
|
175
350
|
const sseHeaders = {
|
|
176
351
|
'Content-Type': 'text/event-stream',
|