@ai-sdk/provider-utils 4.0.13 → 4.0.15
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 +17 -0
- package/dist/index.d.mts +40 -3
- package/dist/index.d.ts +40 -3
- package/dist/index.js +72 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/download-blob.ts +23 -4
- package/src/index.ts +4 -0
- package/src/read-response-with-size-limit.ts +97 -0
- package/src/response-handler.ts +0 -1
package/dist/index.mjs
CHANGED
|
@@ -228,10 +228,68 @@ var DownloadError = class extends (_b = AISDKError, _a = symbol, _b) {
|
|
|
228
228
|
}
|
|
229
229
|
};
|
|
230
230
|
|
|
231
|
+
// src/read-response-with-size-limit.ts
|
|
232
|
+
var DEFAULT_MAX_DOWNLOAD_SIZE = 2 * 1024 * 1024 * 1024;
|
|
233
|
+
async function readResponseWithSizeLimit({
|
|
234
|
+
response,
|
|
235
|
+
url,
|
|
236
|
+
maxBytes = DEFAULT_MAX_DOWNLOAD_SIZE
|
|
237
|
+
}) {
|
|
238
|
+
const contentLength = response.headers.get("content-length");
|
|
239
|
+
if (contentLength != null) {
|
|
240
|
+
const length = parseInt(contentLength, 10);
|
|
241
|
+
if (!isNaN(length) && length > maxBytes) {
|
|
242
|
+
throw new DownloadError({
|
|
243
|
+
url,
|
|
244
|
+
message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes (Content-Length: ${length}).`
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const body = response.body;
|
|
249
|
+
if (body == null) {
|
|
250
|
+
return new Uint8Array(0);
|
|
251
|
+
}
|
|
252
|
+
const reader = body.getReader();
|
|
253
|
+
const chunks = [];
|
|
254
|
+
let totalBytes = 0;
|
|
255
|
+
try {
|
|
256
|
+
while (true) {
|
|
257
|
+
const { done, value } = await reader.read();
|
|
258
|
+
if (done) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
totalBytes += value.length;
|
|
262
|
+
if (totalBytes > maxBytes) {
|
|
263
|
+
throw new DownloadError({
|
|
264
|
+
url,
|
|
265
|
+
message: `Download of ${url} exceeded maximum size of ${maxBytes} bytes.`
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
chunks.push(value);
|
|
269
|
+
}
|
|
270
|
+
} finally {
|
|
271
|
+
try {
|
|
272
|
+
await reader.cancel();
|
|
273
|
+
} finally {
|
|
274
|
+
reader.releaseLock();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
const result = new Uint8Array(totalBytes);
|
|
278
|
+
let offset = 0;
|
|
279
|
+
for (const chunk of chunks) {
|
|
280
|
+
result.set(chunk, offset);
|
|
281
|
+
offset += chunk.length;
|
|
282
|
+
}
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
|
|
231
286
|
// src/download-blob.ts
|
|
232
|
-
async function downloadBlob(url) {
|
|
287
|
+
async function downloadBlob(url, options) {
|
|
288
|
+
var _a2, _b2;
|
|
233
289
|
try {
|
|
234
|
-
const response = await fetch(url
|
|
290
|
+
const response = await fetch(url, {
|
|
291
|
+
signal: options == null ? void 0 : options.abortSignal
|
|
292
|
+
});
|
|
235
293
|
if (!response.ok) {
|
|
236
294
|
throw new DownloadError({
|
|
237
295
|
url,
|
|
@@ -239,7 +297,13 @@ async function downloadBlob(url) {
|
|
|
239
297
|
statusText: response.statusText
|
|
240
298
|
});
|
|
241
299
|
}
|
|
242
|
-
|
|
300
|
+
const data = await readResponseWithSizeLimit({
|
|
301
|
+
response,
|
|
302
|
+
url,
|
|
303
|
+
maxBytes: (_a2 = options == null ? void 0 : options.maxBytes) != null ? _a2 : DEFAULT_MAX_DOWNLOAD_SIZE
|
|
304
|
+
});
|
|
305
|
+
const contentType = (_b2 = response.headers.get("content-type")) != null ? _b2 : void 0;
|
|
306
|
+
return new Blob([data], contentType ? { type: contentType } : void 0);
|
|
243
307
|
} catch (error) {
|
|
244
308
|
if (DownloadError.isInstance(error)) {
|
|
245
309
|
throw error;
|
|
@@ -410,7 +474,7 @@ function withUserAgentSuffix(headers, ...userAgentSuffixParts) {
|
|
|
410
474
|
}
|
|
411
475
|
|
|
412
476
|
// src/version.ts
|
|
413
|
-
var VERSION = true ? "4.0.
|
|
477
|
+
var VERSION = true ? "4.0.15" : "0.0.0-test";
|
|
414
478
|
|
|
415
479
|
// src/get-from-api.ts
|
|
416
480
|
var getOriginalFetch = () => globalThis.fetch;
|
|
@@ -2519,6 +2583,7 @@ import {
|
|
|
2519
2583
|
EventSourceParserStream as EventSourceParserStream2
|
|
2520
2584
|
} from "eventsource-parser/stream";
|
|
2521
2585
|
export {
|
|
2586
|
+
DEFAULT_MAX_DOWNLOAD_SIZE,
|
|
2522
2587
|
DelayedPromise,
|
|
2523
2588
|
DownloadError,
|
|
2524
2589
|
EventSourceParserStream2 as EventSourceParserStream,
|
|
@@ -2567,6 +2632,7 @@ export {
|
|
|
2567
2632
|
postFormDataToApi,
|
|
2568
2633
|
postJsonToApi,
|
|
2569
2634
|
postToApi,
|
|
2635
|
+
readResponseWithSizeLimit,
|
|
2570
2636
|
removeUndefinedEntries,
|
|
2571
2637
|
resolve,
|
|
2572
2638
|
safeParseJSON,
|