@ai-sdk/provider-utils 5.0.79 → 5.0.81
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 +21 -0
- package/dist/experimental-extraction/index.d.ts +38 -0
- package/dist/experimental-extraction/index.d.ts.map +1 -0
- package/dist/experimental-extraction/index.js +1400 -0
- package/dist/experimental-extraction/index.js.map +1 -0
- package/dist/index.d.ts +14 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +63 -150
- package/dist/index.js.map +1 -1
- package/package.json +8 -4
- package/src/download-blob.ts +8 -1
- package/src/experimental-extraction/index.ts +1 -0
- package/src/extraction-language-model.ts +359 -0
- package/src/fetch-untrusted-url.ts +2 -2
- package/src/fetch-with-validated-redirects.ts +10 -34
- package/src/validate-download-url.ts +2 -30
- package/src/safe-node-fetch.ts +0 -201
package/src/safe-node-fetch.ts
DELETED
|
@@ -1,201 +0,0 @@
|
|
|
1
|
-
import type * as nodeDnsModule from 'node:dns';
|
|
2
|
-
import type * as nodeModule from 'node:module';
|
|
3
|
-
import type * as undiciModule from 'undici';
|
|
4
|
-
import type { FetchFunction } from './fetch-function';
|
|
5
|
-
import { validateDownloadAddress } from './validate-download-url';
|
|
6
|
-
|
|
7
|
-
type NodeDns = typeof nodeDnsModule;
|
|
8
|
-
type NodeModule = typeof nodeModule;
|
|
9
|
-
type Undici = typeof undiciModule;
|
|
10
|
-
|
|
11
|
-
type LookupAddress = {
|
|
12
|
-
address: string;
|
|
13
|
-
family: number;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type LookupOptions = {
|
|
17
|
-
all?: boolean;
|
|
18
|
-
family?: number;
|
|
19
|
-
hints?: number;
|
|
20
|
-
order?: 'ipv4first' | 'ipv6first' | 'verbatim';
|
|
21
|
-
verbatim?: boolean;
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
type Lookup = (
|
|
25
|
-
hostname: string,
|
|
26
|
-
options: LookupOptions & { all: true },
|
|
27
|
-
callback: (
|
|
28
|
-
error: NodeJS.ErrnoException | null,
|
|
29
|
-
addresses: LookupAddress[],
|
|
30
|
-
) => void,
|
|
31
|
-
) => void;
|
|
32
|
-
|
|
33
|
-
type LookupAllCallback = (
|
|
34
|
-
error: NodeJS.ErrnoException | null,
|
|
35
|
-
addresses: LookupAddress[],
|
|
36
|
-
) => void;
|
|
37
|
-
|
|
38
|
-
type LookupOneCallback = (
|
|
39
|
-
error: NodeJS.ErrnoException | null,
|
|
40
|
-
address: string,
|
|
41
|
-
family: number,
|
|
42
|
-
) => void;
|
|
43
|
-
|
|
44
|
-
type SafeLookup = {
|
|
45
|
-
(
|
|
46
|
-
hostname: string,
|
|
47
|
-
options: LookupOptions & { all: true },
|
|
48
|
-
callback: LookupAllCallback,
|
|
49
|
-
): void;
|
|
50
|
-
(
|
|
51
|
-
hostname: string,
|
|
52
|
-
options: LookupOptions & { all?: false },
|
|
53
|
-
callback: LookupOneCallback,
|
|
54
|
-
): void;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Creates a DNS lookup hook that validates every returned address before
|
|
59
|
-
* returning the callback shape requested by the HTTP connector. Because
|
|
60
|
-
* resolution and validation happen inside the connector, the socket is pinned
|
|
61
|
-
* to the validated result and DNS rebinding cannot introduce a second lookup.
|
|
62
|
-
*/
|
|
63
|
-
export function createSafeLookup(lookup: Lookup): SafeLookup {
|
|
64
|
-
return ((
|
|
65
|
-
hostname: string,
|
|
66
|
-
options: LookupOptions,
|
|
67
|
-
callback: LookupAllCallback | LookupOneCallback,
|
|
68
|
-
): void => {
|
|
69
|
-
lookup(hostname, { ...options, all: true }, (error, addresses) => {
|
|
70
|
-
if (error) {
|
|
71
|
-
(callback as (error: Error) => void)(error);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
const [firstAddress] = addresses;
|
|
77
|
-
|
|
78
|
-
if (firstAddress == null) {
|
|
79
|
-
throw new Error(`Hostname ${hostname} did not resolve to an address`);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
for (const { address, family } of addresses) {
|
|
83
|
-
validateDownloadAddress({ address, family, hostname });
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (options.all === true) {
|
|
87
|
-
(callback as LookupAllCallback)(null, addresses);
|
|
88
|
-
} else {
|
|
89
|
-
(callback as LookupOneCallback)(
|
|
90
|
-
null,
|
|
91
|
-
firstAddress.address,
|
|
92
|
-
firstAddress.family,
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
} catch (error) {
|
|
96
|
-
(callback as (error: Error) => void)(
|
|
97
|
-
error instanceof Error ? error : new Error(String(error)),
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}) as SafeLookup;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
let safeNodeFetchPromise: Promise<FetchFunction> | undefined;
|
|
105
|
-
|
|
106
|
-
export function isNodeRuntime(): boolean {
|
|
107
|
-
const runtimeProcess = globalThis.process as
|
|
108
|
-
| {
|
|
109
|
-
release?: { name?: string };
|
|
110
|
-
title?: string;
|
|
111
|
-
versions?: { bun?: string; deno?: string };
|
|
112
|
-
}
|
|
113
|
-
| undefined;
|
|
114
|
-
|
|
115
|
-
// Node-compatible process objects do not imply support for Node DNS/socket
|
|
116
|
-
// hooks. Workers identifies itself as workerd, including without navigator.
|
|
117
|
-
return (
|
|
118
|
-
runtimeProcess?.release?.name === 'node' &&
|
|
119
|
-
runtimeProcess.versions?.bun == null &&
|
|
120
|
-
runtimeProcess.versions?.deno == null &&
|
|
121
|
-
runtimeProcess.title !== 'workerd' &&
|
|
122
|
-
(globalThis as { EdgeRuntime?: unknown }).EdgeRuntime == null
|
|
123
|
-
);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export async function getDefaultDownloadFetch(): Promise<FetchFunction> {
|
|
127
|
-
if (!isNodeRuntime()) {
|
|
128
|
-
return globalThis.fetch;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Global fetch wrappers cannot be relied on to preserve the dispatcher
|
|
132
|
-
// that pins connections to validated DNS results.
|
|
133
|
-
return (safeNodeFetchPromise ??= Promise.resolve().then(createSafeNodeFetch));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function createSafeNodeFetch(): FetchFunction {
|
|
137
|
-
// @vercel/nft (node file trace) only recognizes an indirectly loaded createRequire when its receiver
|
|
138
|
-
// is named `module` and the returned require function is assigned.
|
|
139
|
-
// eslint-disable-next-line @next/next/no-assign-module-variable
|
|
140
|
-
const module = loadBuiltinModule<NodeModule>('node:module');
|
|
141
|
-
const { lookup } = loadBuiltinModule<NodeDns>('node:dns');
|
|
142
|
-
|
|
143
|
-
// Assign the created require function so deployment tracers can recognize
|
|
144
|
-
// the static dependency without bundlers inlining undici.
|
|
145
|
-
const nodeRequire = module.createRequire(getCurrentModulePath());
|
|
146
|
-
const { Agent, fetch } = nodeRequire('undici') as Undici;
|
|
147
|
-
|
|
148
|
-
const dispatcher = new Agent({
|
|
149
|
-
connect: {
|
|
150
|
-
lookup: createSafeLookup(lookup as Lookup) as never,
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
return ((input, init) =>
|
|
155
|
-
fetch(
|
|
156
|
-
input as Parameters<typeof fetch>[0],
|
|
157
|
-
{
|
|
158
|
-
...init,
|
|
159
|
-
dispatcher,
|
|
160
|
-
} as Parameters<typeof fetch>[1],
|
|
161
|
-
) as unknown as Promise<Response>) satisfies FetchFunction;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function loadBuiltinModule<T>(id: string): T {
|
|
165
|
-
const processWithBuiltins = globalThis.process as
|
|
166
|
-
| {
|
|
167
|
-
getBuiltinModule?: (id: string) => unknown;
|
|
168
|
-
}
|
|
169
|
-
| undefined;
|
|
170
|
-
const builtinModule = processWithBuiltins?.getBuiltinModule?.(id);
|
|
171
|
-
|
|
172
|
-
if (builtinModule == null) {
|
|
173
|
-
throw new Error(`Node.js built-in module ${id} is unavailable`);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return builtinModule as T;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function getCurrentModulePath(): string {
|
|
180
|
-
// `import.meta.url` breaks when provider-utils is rebundled as CommonJS.
|
|
181
|
-
// The caller frame points at this package when loaded directly and at the
|
|
182
|
-
// consuming bundle when inlined, giving createRequire the correct base path.
|
|
183
|
-
const originalPrepareStackTrace = Error.prepareStackTrace;
|
|
184
|
-
|
|
185
|
-
try {
|
|
186
|
-
Error.prepareStackTrace = (_error, callSites) => callSites as never;
|
|
187
|
-
|
|
188
|
-
const error = new Error('Capture current module path');
|
|
189
|
-
Error.captureStackTrace(error, getCurrentModulePath);
|
|
190
|
-
const [caller] = error.stack as unknown as NodeJS.CallSite[];
|
|
191
|
-
const fileName = caller?.getFileName();
|
|
192
|
-
|
|
193
|
-
if (fileName == null) {
|
|
194
|
-
throw new Error('Unable to determine the current module path');
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return fileName;
|
|
198
|
-
} finally {
|
|
199
|
-
Error.prepareStackTrace = originalPrepareStackTrace;
|
|
200
|
-
}
|
|
201
|
-
}
|