@trpc/server 11.0.0-rc.419 → 11.0.0-rc.421

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.
@@ -1,7 +1,7 @@
1
1
  {
2
- "bundleSize": 125779,
3
- "bundleOrigSize": 175288,
4
- "bundleReduction": 28.24,
2
+ "bundleSize": 125989,
3
+ "bundleOrigSize": 175377,
4
+ "bundleReduction": 28.16,
5
5
  "modules": [
6
6
  {
7
7
  "id": "/src/unstable-core-do-not-import/http/resolveResponse.ts",
@@ -14,7 +14,7 @@
14
14
  "dependents": [
15
15
  "/src/unstable-core-do-not-import.ts"
16
16
  ],
17
- "percent": 13.57,
17
+ "percent": 13.55,
18
18
  "reduction": 0
19
19
  },
20
20
  {
@@ -31,7 +31,7 @@
31
31
  "/src/unstable-core-do-not-import.ts",
32
32
  "/src/unstable-core-do-not-import/http/resolveResponse.ts"
33
33
  ],
34
- "percent": 12.46,
34
+ "percent": 12.44,
35
35
  "reduction": 6.72
36
36
  },
37
37
  {
@@ -46,7 +46,7 @@
46
46
  "dependents": [
47
47
  "/src/adapters/fastify/fastifyTRPCPlugin.ts"
48
48
  ],
49
- "percent": 9.79,
49
+ "percent": 9.78,
50
50
  "reduction": 0
51
51
  },
52
52
  {
@@ -61,7 +61,7 @@
61
61
  "/src/unstable-core-do-not-import.ts",
62
62
  "/src/unstable-core-do-not-import/http/resolveResponse.ts"
63
63
  ],
64
- "percent": 5.8,
64
+ "percent": 5.79,
65
65
  "reduction": 0
66
66
  },
67
67
  {
@@ -79,7 +79,7 @@
79
79
  "/src/unstable-core-do-not-import.ts",
80
80
  "/src/unstable-core-do-not-import/initTRPC.ts"
81
81
  ],
82
- "percent": 5.07,
82
+ "percent": 5.06,
83
83
  "reduction": 39.72
84
84
  },
85
85
  {
@@ -94,16 +94,16 @@
94
94
  "/src/unstable-core-do-not-import.ts",
95
95
  "/src/unstable-core-do-not-import/initTRPC.ts"
96
96
  ],
97
- "percent": 5.03,
97
+ "percent": 5.02,
98
98
  "reduction": 60.06
99
99
  },
100
100
  {
101
101
  "id": "/src/unstable-core-do-not-import/stream/sse.ts",
102
- "size": 5427,
103
- "origSize": 7375,
102
+ "size": 5637,
103
+ "origSize": 7464,
104
104
  "renderedExports": [
105
105
  "sse",
106
- "isServerSentEventEnvelope",
106
+ "isSSEMessageEnvelope",
107
107
  "sseStreamProducer",
108
108
  "sseStreamConsumer",
109
109
  "sseHeaders"
@@ -113,8 +113,8 @@
113
113
  "/src/unstable-core-do-not-import.ts",
114
114
  "/src/unstable-core-do-not-import/http/resolveResponse.ts"
115
115
  ],
116
- "percent": 4.31,
117
- "reduction": 26.41
116
+ "percent": 4.47,
117
+ "reduction": 24.48
118
118
  },
119
119
  {
120
120
  "id": "/src/adapters/aws-lambda/getPlanner.ts",
@@ -127,7 +127,7 @@
127
127
  "dependents": [
128
128
  "/src/adapters/aws-lambda/index.ts"
129
129
  ],
130
- "percent": 4.31,
130
+ "percent": 4.3,
131
131
  "reduction": 11.38
132
132
  },
133
133
  {
@@ -193,9 +193,9 @@
193
193
  "removedExports": [],
194
194
  "dependents": [
195
195
  "/src/unstable-core-do-not-import.ts",
196
+ "/src/unstable-core-do-not-import/http/resolveResponse.ts",
196
197
  "/src/unstable-core-do-not-import/initTRPC.ts",
197
- "/src/unstable-core-do-not-import/router.ts",
198
- "/src/unstable-core-do-not-import/http/resolveResponse.ts"
198
+ "/src/unstable-core-do-not-import/router.ts"
199
199
  ],
200
200
  "percent": 2.18,
201
201
  "reduction": 45.94
@@ -319,10 +319,10 @@
319
319
  "removedExports": [],
320
320
  "dependents": [
321
321
  "/src/unstable-core-do-not-import.ts",
322
+ "/src/unstable-core-do-not-import/http/resolveResponse.ts",
322
323
  "/src/unstable-core-do-not-import/stream/sse.ts",
323
324
  "/src/unstable-core-do-not-import/middleware.ts",
324
325
  "/src/unstable-core-do-not-import/router.ts",
325
- "/src/unstable-core-do-not-import/http/resolveResponse.ts",
326
326
  "/src/unstable-core-do-not-import/http/contentType.ts",
327
327
  "/src/unstable-core-do-not-import/procedureBuilder.ts",
328
328
  "/src/unstable-core-do-not-import/stream/jsonl.ts"
@@ -339,7 +339,7 @@
339
339
  ],
340
340
  "removedExports": [],
341
341
  "dependents": [],
342
- "percent": 1.26,
342
+ "percent": 1.25,
343
343
  "reduction": 21.04
344
344
  },
345
345
  {
@@ -351,7 +351,7 @@
351
351
  ],
352
352
  "removedExports": [],
353
353
  "dependents": [],
354
- "percent": 1.23,
354
+ "percent": 1.22,
355
355
  "reduction": 26.94
356
356
  },
357
357
  {
@@ -392,10 +392,10 @@
392
392
  "removedExports": [],
393
393
  "dependents": [
394
394
  "/src/unstable-core-do-not-import.ts",
395
- "/src/unstable-core-do-not-import/error/getErrorShape.ts",
396
- "/src/unstable-core-do-not-import/http/resolveResponse.ts"
395
+ "/src/unstable-core-do-not-import/http/resolveResponse.ts",
396
+ "/src/unstable-core-do-not-import/error/getErrorShape.ts"
397
397
  ],
398
- "percent": 1.01,
398
+ "percent": 1,
399
399
  "reduction": 23.5
400
400
  },
401
401
  {
@@ -414,15 +414,15 @@
414
414
  "removedExports": [],
415
415
  "dependents": [
416
416
  "/src/unstable-core-do-not-import.ts",
417
+ "/src/unstable-core-do-not-import/http/resolveResponse.ts",
417
418
  "/src/unstable-core-do-not-import/error/TRPCError.ts",
418
419
  "/src/unstable-core-do-not-import/stream/sse.ts",
419
420
  "/src/unstable-core-do-not-import/transformer.ts",
420
421
  "/src/unstable-core-do-not-import/middleware.ts",
421
422
  "/src/unstable-core-do-not-import/router.ts",
422
- "/src/unstable-core-do-not-import/http/resolveResponse.ts",
423
+ "/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts",
423
424
  "/src/unstable-core-do-not-import/http/contentType.ts",
424
425
  "/src/unstable-core-do-not-import/procedureBuilder.ts",
425
- "/src/unstable-core-do-not-import/rpc/parseTRPCMessage.ts",
426
426
  "/src/unstable-core-do-not-import/stream/jsonl.ts"
427
427
  ],
428
428
  "percent": 0.95,
@@ -689,6 +689,16 @@
689
689
  "percent": 0.05,
690
690
  "reduction": 93.8
691
691
  },
692
+ {
693
+ "id": "/src/http.ts",
694
+ "size": 0,
695
+ "origSize": 37,
696
+ "renderedExports": [],
697
+ "removedExports": [],
698
+ "dependents": [],
699
+ "percent": 0,
700
+ "reduction": 100
701
+ },
692
702
  {
693
703
  "id": "/src/index.ts",
694
704
  "size": 0,
@@ -700,9 +710,9 @@
700
710
  "reduction": 100
701
711
  },
702
712
  {
703
- "id": "/src/http.ts",
713
+ "id": "/src/rpc.ts",
704
714
  "size": 0,
705
- "origSize": 37,
715
+ "origSize": 36,
706
716
  "renderedExports": [],
707
717
  "removedExports": [],
708
718
  "dependents": [],
@@ -731,16 +741,6 @@
731
741
  "percent": 0,
732
742
  "reduction": 100
733
743
  },
734
- {
735
- "id": "/src/rpc.ts",
736
- "size": 0,
737
- "origSize": 36,
738
- "renderedExports": [],
739
- "removedExports": [],
740
- "dependents": [],
741
- "percent": 0,
742
- "reduction": 100
743
- },
744
744
  {
745
745
  "id": "/src/adapters/next-app-dir.ts",
746
746
  "size": 0,
@@ -791,8 +791,8 @@
791
791
  "removedExports": [],
792
792
  "dependents": [
793
793
  "/src/adapters/express.ts",
794
- "/src/adapters/next.ts",
795
794
  "/src/adapters/standalone.ts",
795
+ "/src/adapters/next.ts",
796
796
  "/src/adapters/fastify/fastifyRequestHandler.ts"
797
797
  ],
798
798
  "percent": 0,
@@ -2,7 +2,12 @@ import type { ValidateShape } from '../types';
2
2
  import type { ConsumerOnError } from './jsonl';
3
3
  type Serialize = (value: any) => any;
4
4
  type Deserialize = (value: any) => any;
5
- interface SSEventWithId {
5
+ /**
6
+ * Server-sent Event Message
7
+ * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
8
+ * @public
9
+ */
10
+ export interface SSEMessage {
6
11
  /**
7
12
  * The data field of the message - this can be anything
8
13
  */
@@ -12,30 +17,16 @@ interface SSEventWithId {
12
17
  * Passing this id will allow the client to resume the connection from this point if the connection is lost
13
18
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-last-event-id-header
14
19
  */
15
- id: string | number;
16
- /**
17
- * Event name for the message
18
- */
19
- event?: string;
20
- /**
21
- * A comment for the event
22
- */
23
- comment?: string;
20
+ id: string;
24
21
  }
25
- /**
26
- * Server-sent Event
27
- * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
28
- * @public
29
- */
30
- export type SSEvent = Partial<SSEventWithId>;
31
22
  declare const sseSymbol: unique symbol;
32
- export type ServerSentEventEnvelope<TData> = [typeof sseSymbol, TData];
23
+ export type SSEMessageEnvelope<TData> = [typeof sseSymbol, TData];
33
24
  /**
34
- * Produce a typed server-sent event
25
+ * Produce a typed server-sent event message
35
26
  */
36
- export declare function sse<TData extends SSEvent>(event: ValidateShape<TData, SSEvent>): ServerSentEventEnvelope<TData>;
37
- export declare function isServerSentEventEnvelope<TData extends SSEvent>(value: unknown): value is ServerSentEventEnvelope<TData>;
38
- export type SerializedSSEvent = Omit<SSEvent, 'data'> & {
27
+ export declare function sse<TData extends SSEMessage>(event: ValidateShape<TData, SSEMessage>): SSEMessageEnvelope<TData>;
28
+ export declare function isSSEMessageEnvelope<TData extends SSEMessage>(value: unknown): value is SSEMessageEnvelope<TData>;
29
+ export type SerializedSSEvent = Omit<SSEMessage, 'data'> & {
39
30
  data?: string;
40
31
  };
41
32
  /**
@@ -76,7 +67,7 @@ export interface SSEStreamProducerOptions {
76
67
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
77
68
  */
78
69
  export declare function sseStreamProducer(opts: SSEStreamProducerOptions): ReadableStream<string>;
79
- export type inferSSEOutput<TData> = TData extends ServerSentEventEnvelope<infer $Data> ? $Data : TData;
70
+ export type inferSSEOutput<TData> = TData extends SSEMessageEnvelope<infer $Data> ? $Data : TData;
80
71
  /**
81
72
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
82
73
  */
@@ -1 +1 @@
1
- {"version":3,"file":"sse.d.ts","sourceRoot":"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CAAC,EAAE,eAAe,CAAC;IAC1B,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B,GAAG,aAAa,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CA0DvC;AAED,eAAO,MAAM,UAAU;;;;;CAKb,CAAC"}
@@ -5,16 +5,20 @@ var utils = require('../utils.js');
5
5
  var createDeferred = require('./utils/createDeferred.js');
6
6
  var createReadableStream = require('./utils/createReadableStream.js');
7
7
 
8
- const sseSymbol = Symbol('SSEventEnvelope');
8
+ const sseSymbol = Symbol('SSEMessageEnvelope');
9
9
  /**
10
- * Produce a typed server-sent event
10
+ * Produce a typed server-sent event message
11
11
  */ function sse(event) {
12
+ if (event.id === '') {
13
+ // This could be removed by using different event names for `yield sse(x)`-emitted events and `yield y`-emitted events
14
+ throw new Error('`id` must not be an empty string as empty string is the same as not setting the id at all');
15
+ }
12
16
  return [
13
17
  sseSymbol,
14
18
  event
15
19
  ];
16
20
  }
17
- function isServerSentEventEnvelope(value) {
21
+ function isSSEMessageEnvelope(value) {
18
22
  return Array.isArray(value) && value[0] === sseSymbol;
19
23
  }
20
24
  /**
@@ -64,11 +68,11 @@ function isServerSentEventEnvelope(value) {
64
68
  break;
65
69
  }
66
70
  const value = next.value;
67
- const data = isServerSentEventEnvelope(value) ? value[1] : {
71
+ const chunk = isSSEMessageEnvelope(value) ? {
72
+ ...value[1]
73
+ } : {
68
74
  data: value
69
75
  };
70
- const chunk = {};
71
- Object.assign(chunk, data);
72
76
  if ('data' in chunk) {
73
77
  chunk.data = JSON.stringify(serialize(chunk.data));
74
78
  }
@@ -98,6 +102,9 @@ function isServerSentEventEnvelope(value) {
98
102
  if ('id' in chunk) {
99
103
  controller.enqueue(`id: ${chunk.id}\n`);
100
104
  }
105
+ if ('comment' in chunk) {
106
+ controller.enqueue(`: ${chunk.comment}\n`);
107
+ }
101
108
  controller.enqueue('\n\n');
102
109
  }
103
110
  }));
@@ -110,17 +117,13 @@ function isServerSentEventEnvelope(value) {
110
117
  const stream = createReadableStream.createReadableStream();
111
118
  const transform = new TransformStream({
112
119
  async transform (chunk, controller) {
113
- if (chunk.data) {
114
- const def = {};
115
- def.data = deserialize(JSON.parse(chunk.data));
116
- if ('id' in chunk) {
117
- def.id = chunk.id;
118
- }
119
- if ('event' in chunk) {
120
- def.event = chunk.event;
121
- }
122
- controller.enqueue(def);
120
+ const def = {
121
+ data: deserialize(JSON.parse(chunk.data))
122
+ };
123
+ if (chunk.lastEventId) {
124
+ def.id = chunk.lastEventId;
123
125
  }
126
+ controller.enqueue(def);
124
127
  }
125
128
  });
126
129
  eventSource.addEventListener('message', (msg)=>{
@@ -168,7 +171,7 @@ const sseHeaders = {
168
171
  Connection: 'keep-alive'
169
172
  };
170
173
 
171
- exports.isServerSentEventEnvelope = isServerSentEventEnvelope;
174
+ exports.isSSEMessageEnvelope = isSSEMessageEnvelope;
172
175
  exports.sse = sse;
173
176
  exports.sseHeaders = sseHeaders;
174
177
  exports.sseStreamConsumer = sseStreamConsumer;
@@ -3,16 +3,20 @@ import { run } from '../utils.mjs';
3
3
  import { createTimeoutPromise } from './utils/createDeferred.mjs';
4
4
  import { createReadableStream } from './utils/createReadableStream.mjs';
5
5
 
6
- const sseSymbol = Symbol('SSEventEnvelope');
6
+ const sseSymbol = Symbol('SSEMessageEnvelope');
7
7
  /**
8
- * Produce a typed server-sent event
8
+ * Produce a typed server-sent event message
9
9
  */ function sse(event) {
10
+ if (event.id === '') {
11
+ // This could be removed by using different event names for `yield sse(x)`-emitted events and `yield y`-emitted events
12
+ throw new Error('`id` must not be an empty string as empty string is the same as not setting the id at all');
13
+ }
10
14
  return [
11
15
  sseSymbol,
12
16
  event
13
17
  ];
14
18
  }
15
- function isServerSentEventEnvelope(value) {
19
+ function isSSEMessageEnvelope(value) {
16
20
  return Array.isArray(value) && value[0] === sseSymbol;
17
21
  }
18
22
  /**
@@ -62,11 +66,11 @@ function isServerSentEventEnvelope(value) {
62
66
  break;
63
67
  }
64
68
  const value = next.value;
65
- const data = isServerSentEventEnvelope(value) ? value[1] : {
69
+ const chunk = isSSEMessageEnvelope(value) ? {
70
+ ...value[1]
71
+ } : {
66
72
  data: value
67
73
  };
68
- const chunk = {};
69
- Object.assign(chunk, data);
70
74
  if ('data' in chunk) {
71
75
  chunk.data = JSON.stringify(serialize(chunk.data));
72
76
  }
@@ -96,6 +100,9 @@ function isServerSentEventEnvelope(value) {
96
100
  if ('id' in chunk) {
97
101
  controller.enqueue(`id: ${chunk.id}\n`);
98
102
  }
103
+ if ('comment' in chunk) {
104
+ controller.enqueue(`: ${chunk.comment}\n`);
105
+ }
99
106
  controller.enqueue('\n\n');
100
107
  }
101
108
  }));
@@ -108,17 +115,13 @@ function isServerSentEventEnvelope(value) {
108
115
  const stream = createReadableStream();
109
116
  const transform = new TransformStream({
110
117
  async transform (chunk, controller) {
111
- if (chunk.data) {
112
- const def = {};
113
- def.data = deserialize(JSON.parse(chunk.data));
114
- if ('id' in chunk) {
115
- def.id = chunk.id;
116
- }
117
- if ('event' in chunk) {
118
- def.event = chunk.event;
119
- }
120
- controller.enqueue(def);
118
+ const def = {
119
+ data: deserialize(JSON.parse(chunk.data))
120
+ };
121
+ if (chunk.lastEventId) {
122
+ def.id = chunk.lastEventId;
121
123
  }
124
+ controller.enqueue(def);
122
125
  }
123
126
  });
124
127
  eventSource.addEventListener('message', (msg)=>{
@@ -166,4 +169,4 @@ const sseHeaders = {
166
169
  Connection: 'keep-alive'
167
170
  };
168
171
 
169
- export { isServerSentEventEnvelope, sse, sseHeaders, sseStreamConsumer, sseStreamProducer };
172
+ export { isSSEMessageEnvelope, sse, sseHeaders, sseStreamConsumer, sseStreamProducer };
@@ -73,7 +73,7 @@ exports.unsetMarker = utils.unsetMarker;
73
73
  exports.isPromise = jsonl.isPromise;
74
74
  exports.jsonlStreamConsumer = jsonl.jsonlStreamConsumer;
75
75
  exports.jsonlStreamProducer = jsonl.jsonlStreamProducer;
76
- exports.isServerSentEventEnvelope = sse.isServerSentEventEnvelope;
76
+ exports.isSSEMessageEnvelope = sse.isSSEMessageEnvelope;
77
77
  exports.sse = sse.sse;
78
78
  exports.sseHeaders = sse.sseHeaders;
79
79
  exports.sseStreamConsumer = sse.sseStreamConsumer;
@@ -21,4 +21,4 @@ export { defaultTransformer, getDataTransformer, transformResult, transformTRPCR
21
21
  export { ERROR_SYMBOL } from './unstable-core-do-not-import/types.mjs';
22
22
  export { isAsyncIterable, isFunction, isObject, mergeWithoutOverrides, omitPrototype, run, unsetMarker } from './unstable-core-do-not-import/utils.mjs';
23
23
  export { isPromise, jsonlStreamConsumer, jsonlStreamProducer } from './unstable-core-do-not-import/stream/jsonl.mjs';
24
- export { isServerSentEventEnvelope, sse, sseHeaders, sseStreamConsumer, sseStreamProducer } from './unstable-core-do-not-import/stream/sse.mjs';
24
+ export { isSSEMessageEnvelope, sse, sseHeaders, sseStreamConsumer, sseStreamProducer } from './unstable-core-do-not-import/stream/sse.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "11.0.0-rc.419+343b6de2e",
3
+ "version": "11.0.0-rc.421+bce4f97e9",
4
4
  "description": "The tRPC server library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -149,5 +149,5 @@
149
149
  "funding": [
150
150
  "https://trpc.io/sponsor"
151
151
  ],
152
- "gitHead": "343b6de2ea719347bffe8332f37589c66c323349"
152
+ "gitHead": "bce4f97e94841e1ed3cb48f9116a672a6bad6695"
153
153
  }
@@ -8,7 +8,12 @@ import { createReadableStream } from './utils/createReadableStream';
8
8
  type Serialize = (value: any) => any;
9
9
  type Deserialize = (value: any) => any;
10
10
 
11
- interface SSEventWithId {
11
+ /**
12
+ * Server-sent Event Message
13
+ * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
14
+ * @public
15
+ */
16
+ export interface SSEMessage {
12
17
  /**
13
18
  * The data field of the message - this can be anything
14
19
  */
@@ -18,43 +23,34 @@ interface SSEventWithId {
18
23
  * Passing this id will allow the client to resume the connection from this point if the connection is lost
19
24
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-last-event-id-header
20
25
  */
21
- id: string | number;
22
- /**
23
- * Event name for the message
24
- */
25
- event?: string;
26
- /**
27
- * A comment for the event
28
- */
29
- comment?: string;
26
+ id: string;
30
27
  }
31
28
 
32
- /**
33
- * Server-sent Event
34
- * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
35
- * @public
36
- */
37
- export type SSEvent = Partial<SSEventWithId>;
38
-
39
- const sseSymbol = Symbol('SSEventEnvelope');
40
- export type ServerSentEventEnvelope<TData> = [typeof sseSymbol, TData];
29
+ const sseSymbol = Symbol('SSEMessageEnvelope');
30
+ export type SSEMessageEnvelope<TData> = [typeof sseSymbol, TData];
41
31
 
42
32
  /**
43
- * Produce a typed server-sent event
33
+ * Produce a typed server-sent event message
44
34
  */
45
- export function sse<TData extends SSEvent>(
46
- event: ValidateShape<TData, SSEvent>,
47
- ): ServerSentEventEnvelope<TData> {
35
+ export function sse<TData extends SSEMessage>(
36
+ event: ValidateShape<TData, SSEMessage>,
37
+ ): SSEMessageEnvelope<TData> {
38
+ if (event.id === '') {
39
+ // This could be removed by using different event names for `yield sse(x)`-emitted events and `yield y`-emitted events
40
+ throw new Error(
41
+ '`id` must not be an empty string as empty string is the same as not setting the id at all',
42
+ );
43
+ }
48
44
  return [sseSymbol, event as TData];
49
45
  }
50
46
 
51
- export function isServerSentEventEnvelope<TData extends SSEvent>(
47
+ export function isSSEMessageEnvelope<TData extends SSEMessage>(
52
48
  value: unknown,
53
- ): value is ServerSentEventEnvelope<TData> {
49
+ ): value is SSEMessageEnvelope<TData> {
54
50
  return Array.isArray(value) && value[0] === sseSymbol;
55
51
  }
56
52
 
57
- export type SerializedSSEvent = Omit<SSEvent, 'data'> & {
53
+ export type SerializedSSEvent = Omit<SSEMessage, 'data'> & {
58
54
  data?: string;
59
55
  };
60
56
 
@@ -92,12 +88,19 @@ export interface SSEStreamProducerOptions {
92
88
  */
93
89
  emitAndEndImmediately?: boolean;
94
90
  }
91
+
92
+ type SSEvent = Partial<
93
+ SSEMessage & {
94
+ comment: string;
95
+ event: string;
96
+ }
97
+ >;
95
98
  /**
96
99
  *
97
100
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html
98
101
  */
99
102
  export function sseStreamProducer(opts: SSEStreamProducerOptions) {
100
- const stream = createReadableStream<SerializedSSEvent>();
103
+ const stream = createReadableStream<SSEvent>();
101
104
  stream.controller.enqueue({
102
105
  comment: 'connected',
103
106
  });
@@ -157,13 +160,11 @@ export function sseStreamProducer(opts: SSEStreamProducerOptions) {
157
160
 
158
161
  const value = next.value;
159
162
 
160
- const data: SSEvent = isServerSentEventEnvelope(value)
161
- ? value[1]
163
+ const chunk: SSEvent = isSSEMessageEnvelope(value)
164
+ ? { ...value[1] }
162
165
  : {
163
166
  data: value,
164
167
  };
165
- const chunk: SerializedSSEvent = {};
166
- Object.assign(chunk, data);
167
168
  if ('data' in chunk) {
168
169
  chunk.data = JSON.stringify(serialize(chunk.data));
169
170
  }
@@ -187,7 +188,7 @@ export function sseStreamProducer(opts: SSEStreamProducerOptions) {
187
188
  });
188
189
 
189
190
  return stream.readable.pipeThrough(
190
- new TransformStream<SerializedSSEvent, string>({
191
+ new TransformStream<SSEvent, string>({
191
192
  transform(chunk, controller) {
192
193
  if ('event' in chunk) {
193
194
  controller.enqueue(`event: ${chunk.event}\n`);
@@ -198,12 +199,15 @@ export function sseStreamProducer(opts: SSEStreamProducerOptions) {
198
199
  if ('id' in chunk) {
199
200
  controller.enqueue(`id: ${chunk.id}\n`);
200
201
  }
202
+ if ('comment' in chunk) {
203
+ controller.enqueue(`: ${chunk.comment}\n`);
204
+ }
201
205
  controller.enqueue('\n\n');
202
206
  },
203
207
  }),
204
208
  );
205
209
  }
206
- export type inferSSEOutput<TData> = TData extends ServerSentEventEnvelope<
210
+ export type inferSSEOutput<TData> = TData extends SSEMessageEnvelope<
207
211
  infer $Data
208
212
  >
209
213
  ? $Data
@@ -220,25 +224,18 @@ export function sseStreamConsumer<TData>(opts: {
220
224
  const { deserialize = (v) => v } = opts;
221
225
  const eventSource = opts.from;
222
226
 
223
- const stream = createReadableStream<SerializedSSEvent>();
227
+ const stream = createReadableStream<MessageEvent>();
224
228
 
225
- const transform = new TransformStream<
226
- SerializedSSEvent,
227
- inferSSEOutput<TData>
228
- >({
229
+ const transform = new TransformStream<MessageEvent, inferSSEOutput<TData>>({
229
230
  async transform(chunk, controller) {
230
- if (chunk.data) {
231
- const def: SSEvent = {};
232
- def.data = deserialize(JSON.parse(chunk.data));
233
- if ('id' in chunk) {
234
- def.id = chunk.id;
235
- }
236
- if ('event' in chunk) {
237
- def.event = chunk.event;
238
- }
231
+ const def: Partial<SSEMessage> = {
232
+ data: deserialize(JSON.parse(chunk.data)),
233
+ };
239
234
 
240
- controller.enqueue(def as inferSSEOutput<TData>);
235
+ if (chunk.lastEventId) {
236
+ def.id = chunk.lastEventId;
241
237
  }
238
+ controller.enqueue(def as inferSSEOutput<TData>);
242
239
  },
243
240
  });
244
241