@ai-sdk/provider-utils 4.0.63 → 4.0.64

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.63",
3
+ "version": "4.0.64",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
package/src/index.ts CHANGED
@@ -91,3 +91,5 @@ export {
91
91
  EventSourceParserStream,
92
92
  type EventSourceMessage,
93
93
  } from 'eventsource-parser/stream';
94
+
95
+ export { stringifyWithBigIntAndCycles } from './stringify-with-bigint-and-cycles';
@@ -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
+ }
@@ -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
- return Uint8Array.from(latin1string, byte => byte.codePointAt(0)!);
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 instanceof Uint8Array ? convertUint8ArrayToBase64(value) : value;
71
+ return isUint8Array(value) ? convertUint8ArrayToBase64(value) : value;
35
72
  }