@ai-sdk/provider-utils 4.0.63 → 4.0.65
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 +14 -0
- package/dist/index.d.mts +13 -1
- package/dist/index.d.ts +13 -1
- package/dist/index.js +62 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +59 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/fetch-with-validated-redirects.ts +13 -0
- package/src/index.ts +2 -0
- package/src/stringify-with-bigint-and-cycles.ts +26 -0
- package/src/uint8-utils.ts +39 -2
package/package.json
CHANGED
|
@@ -80,6 +80,9 @@ export async function fetchWithValidatedEndpoint({
|
|
|
80
80
|
* target (e.g. an internal address) before we ever see its URL, defeating the
|
|
81
81
|
* guard.
|
|
82
82
|
*
|
|
83
|
+
* Manually followed redirects from HTTPS to HTTP are rejected before requesting
|
|
84
|
+
* the target.
|
|
85
|
+
*
|
|
83
86
|
* Request headers are also protected: {@link sanitizeRequestHeaders} strips
|
|
84
87
|
* proxy/metadata/cookie/hop-by-hop headers before the first request, and all
|
|
85
88
|
* caller headers except `User-Agent` are dropped on a cross-origin redirect.
|
|
@@ -192,6 +195,16 @@ export async function fetchWithValidatedRedirects({
|
|
|
192
195
|
void cancelResponseBody(response);
|
|
193
196
|
const nextUrl = new URL(location, currentUrl).toString();
|
|
194
197
|
|
|
198
|
+
if (
|
|
199
|
+
new URL(currentUrl).protocol === 'https:' &&
|
|
200
|
+
new URL(nextUrl).protocol === 'http:'
|
|
201
|
+
) {
|
|
202
|
+
throw new DownloadError({
|
|
203
|
+
url,
|
|
204
|
+
message: 'Redirect from HTTPS to HTTP was blocked',
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
195
208
|
// Drop all caller headers except the user-agent before following a
|
|
196
209
|
// redirect that crosses origin. Only stripping Authorization (as the
|
|
197
210
|
// fetch spec does) is not enough on the server: providers authenticate
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serializes a value as JSON, representing BigInt values as decimal strings and
|
|
3
|
+
* ancestor cycles as "[Circular]". Reused objects, toJSON methods, and other JSON
|
|
4
|
+
* serialization behavior are preserved. Getters and toJSON methods may throw.
|
|
5
|
+
*/
|
|
6
|
+
export function stringifyWithBigIntAndCycles(value: unknown): string {
|
|
7
|
+
const ancestors: object[] = [];
|
|
8
|
+
return JSON.stringify(value, function (_key, value) {
|
|
9
|
+
if (typeof value === 'bigint') {
|
|
10
|
+
return value.toString();
|
|
11
|
+
}
|
|
12
|
+
if (typeof value !== 'object' || value === null) {
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
// JSON.stringify visits siblings after their previous subtree. Only
|
|
16
|
+
// objects on the current ancestor path are circular references.
|
|
17
|
+
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
|
|
18
|
+
ancestors.pop();
|
|
19
|
+
}
|
|
20
|
+
if (ancestors.includes(value)) {
|
|
21
|
+
return '[Circular]';
|
|
22
|
+
}
|
|
23
|
+
ancestors.push(value);
|
|
24
|
+
return value;
|
|
25
|
+
});
|
|
26
|
+
}
|
package/src/uint8-utils.ts
CHANGED
|
@@ -6,7 +6,15 @@ const { btoa, atob } = globalThis;
|
|
|
6
6
|
export function convertBase64ToUint8Array(base64String: string) {
|
|
7
7
|
const base64Url = base64String.replace(/-/g, '+').replace(/_/g, '/');
|
|
8
8
|
const latin1string = atob(base64Url);
|
|
9
|
-
|
|
9
|
+
|
|
10
|
+
// Preallocate and fill: `Uint8Array.from(string, mapFn)` iterates per
|
|
11
|
+
// character and collects the mapped values before allocating the result,
|
|
12
|
+
// which peaks at ~25x the decoded size and blocks for ~1s on 20 MB inputs.
|
|
13
|
+
const bytes = new Uint8Array(latin1string.length);
|
|
14
|
+
for (let i = 0; i < latin1string.length; i++) {
|
|
15
|
+
bytes[i] = latin1string.charCodeAt(i);
|
|
16
|
+
}
|
|
17
|
+
return bytes;
|
|
10
18
|
}
|
|
11
19
|
|
|
12
20
|
export function convertUint8ArrayToBase64(array: Uint8Array): string {
|
|
@@ -30,6 +38,35 @@ export function convertUint8ArrayToBase64(array: Uint8Array): string {
|
|
|
30
38
|
return btoa(chunks.join(''));
|
|
31
39
|
}
|
|
32
40
|
|
|
41
|
+
// Intrinsic getters check the built-in slots across realms without relying on
|
|
42
|
+
// constructor names or user-defined Symbol.toStringTag properties.
|
|
43
|
+
const typedArrayTag = Object.getOwnPropertyDescriptor(
|
|
44
|
+
Object.getPrototypeOf(Uint8Array.prototype),
|
|
45
|
+
Symbol.toStringTag,
|
|
46
|
+
)!.get!;
|
|
47
|
+
const arrayBufferByteLength = Object.getOwnPropertyDescriptor(
|
|
48
|
+
ArrayBuffer.prototype,
|
|
49
|
+
'byteLength',
|
|
50
|
+
)!.get!;
|
|
51
|
+
|
|
52
|
+
export function isUint8Array(value: unknown): value is Uint8Array {
|
|
53
|
+
return (
|
|
54
|
+
ArrayBuffer.isView(value) && typedArrayTag.call(value) === 'Uint8Array'
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function isArrayBuffer(value: unknown): value is ArrayBuffer {
|
|
59
|
+
if (typeof value !== 'object' || value === null || !('byteLength' in value)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
arrayBufferByteLength.call(value);
|
|
64
|
+
return true;
|
|
65
|
+
} catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
33
70
|
export function convertToBase64(value: string | Uint8Array): string {
|
|
34
|
-
return value
|
|
71
|
+
return isUint8Array(value) ? convertUint8ArrayToBase64(value) : value;
|
|
35
72
|
}
|