@ai-sdk/provider-utils 3.0.46 → 3.0.47

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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @ai-sdk/provider-utils
2
2
 
3
+ ## 3.0.47
4
+
5
+ ### Patch Changes
6
+
7
+ - d4deb3b: fix(provider-utils): limit the size of a single server-sent event
8
+
9
+ `parseJsonEventStream` and `createEventSourceResponseHandler` buffered an event until it was complete, without a limit. A streaming response that never completed an event could exhaust the memory and crash the process. A single event is now limited to 67,108,864 characters (64 MiB of ASCII text). A stream that exceeds the limit fails with an error and the response body is cancelled.
10
+
11
+ The limit also applies to the streams that `useChat` and `useCompletion` read. `parseJsonEventStream` has a new `maxEventSize` option to change it.
12
+
3
13
  ## 3.0.46
4
14
 
5
15
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -502,9 +502,15 @@ declare function normalizeHeaders(headers: HeadersInit | Record<string, string |
502
502
  /**
503
503
  * Parses a JSON event stream into a stream of parsed JSON objects.
504
504
  */
505
- declare function parseJsonEventStream<T>({ stream, schema, }: {
505
+ declare function parseJsonEventStream<T>({ stream, schema, maxEventSize, }: {
506
506
  stream: ReadableStream<Uint8Array>;
507
507
  schema: FlexibleValidator<T>;
508
+ /**
509
+ * Maximum size of a single event in characters. The returned stream fails
510
+ * with an error when an event is larger. Defaults to 64 * 1024 * 1024
511
+ * characters.
512
+ */
513
+ maxEventSize?: number;
508
514
  }): ReadableStream<ParseResult<T>>;
509
515
 
510
516
  declare function parseProviderOptions<OPTIONS>({ provider, providerOptions, schema, }: {
package/dist/index.d.ts CHANGED
@@ -502,9 +502,15 @@ declare function normalizeHeaders(headers: HeadersInit | Record<string, string |
502
502
  /**
503
503
  * Parses a JSON event stream into a stream of parsed JSON objects.
504
504
  */
505
- declare function parseJsonEventStream<T>({ stream, schema, }: {
505
+ declare function parseJsonEventStream<T>({ stream, schema, maxEventSize, }: {
506
506
  stream: ReadableStream<Uint8Array>;
507
507
  schema: FlexibleValidator<T>;
508
+ /**
509
+ * Maximum size of a single event in characters. The returned stream fails
510
+ * with an error when an event is larger. Defaults to 64 * 1024 * 1024
511
+ * characters.
512
+ */
513
+ maxEventSize?: number;
508
514
  }): ReadableStream<ParseResult<T>>;
509
515
 
510
516
  declare function parseProviderOptions<OPTIONS>({ provider, providerOptions, schema, }: {
package/dist/index.js CHANGED
@@ -869,7 +869,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
869
869
  }
870
870
 
871
871
  // src/version.ts
872
- var VERSION = true ? "3.0.46" : "0.0.0-test";
872
+ var VERSION = true ? "3.0.47" : "0.0.0-test";
873
873
 
874
874
  // src/get-from-api.ts
875
875
  var getOriginalFetch = () => globalThis.fetch;
@@ -1281,11 +1281,13 @@ function isParsableJson(input) {
1281
1281
 
1282
1282
  // src/parse-json-event-stream.ts
1283
1283
  var import_stream = require("eventsource-parser/stream");
1284
+ var DEFAULT_MAX_EVENT_SIZE = 64 * 1024 * 1024;
1284
1285
  function parseJsonEventStream({
1285
1286
  stream,
1286
- schema
1287
+ schema,
1288
+ maxEventSize = DEFAULT_MAX_EVENT_SIZE
1287
1289
  }) {
1288
- return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream()).pipeThrough(
1290
+ return stream.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream({ maxBufferSize: maxEventSize })).pipeThrough(
1289
1291
  new TransformStream({
1290
1292
  async transform({ data }, controller) {
1291
1293
  if (data === "[DONE]") {