@ai-sdk/provider-utils 4.0.67 → 4.0.68

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/provider-utils",
3
- "version": "4.0.67",
3
+ "version": "4.0.68",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -34,7 +34,7 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@standard-schema/spec": "^1.1.0",
37
- "eventsource-parser": "^3.0.8",
37
+ "eventsource-parser": "^3.1.1",
38
38
  "undici": "^6.28.0",
39
39
  "@ai-sdk/provider": "3.0.19"
40
40
  },
@@ -5,19 +5,36 @@ import {
5
5
  import { safeParseJSON, type ParseResult } from './parse-json';
6
6
  import type { FlexibleSchema } from './schema';
7
7
 
8
+ /**
9
+ * Default maximum size of a single event: 64 * 1024 * 1024 characters.
10
+ *
11
+ * An event is buffered until it is complete, so a stream that never completes
12
+ * an event would grow the buffer until the process runs out of memory. With
13
+ * this limit, such a stream fails with an error that can be handled instead.
14
+ */
15
+ const DEFAULT_MAX_EVENT_SIZE = 64 * 1024 * 1024;
16
+
8
17
  /**
9
18
  * Parses a JSON event stream into a stream of parsed JSON objects.
10
19
  */
11
20
  export function parseJsonEventStream<T>({
12
21
  stream,
13
22
  schema,
23
+ maxEventSize = DEFAULT_MAX_EVENT_SIZE,
14
24
  }: {
15
25
  stream: ReadableStream<Uint8Array>;
16
26
  schema: FlexibleSchema<T>;
27
+
28
+ /**
29
+ * Maximum size of a single event in characters. The returned stream fails
30
+ * with an error when an event is larger. Defaults to 64 * 1024 * 1024
31
+ * characters.
32
+ */
33
+ maxEventSize?: number;
17
34
  }): ReadableStream<ParseResult<T>> {
18
35
  return stream
19
36
  .pipeThrough(new TextDecoderStream())
20
- .pipeThrough(new EventSourceParserStream())
37
+ .pipeThrough(new EventSourceParserStream({ maxBufferSize: maxEventSize }))
21
38
  .pipeThrough(
22
39
  new TransformStream<EventSourceMessage, ParseResult<T>>({
23
40
  async transform({ data }, controller) {