@ai-sdk/provider-utils 5.0.4 → 5.0.5
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 +8 -0
- package/dist/index.d.ts +26 -1
- package/dist/index.js +34 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +7 -0
- package/src/websocket.ts +61 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/provider-utils",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"@standard-schema/spec": "^1.1.0",
|
|
36
36
|
"@workflow/serde": "4.1.0",
|
|
37
37
|
"eventsource-parser": "^3.0.8",
|
|
38
|
-
"@ai-sdk/provider": "4.0.
|
|
38
|
+
"@ai-sdk/provider": "4.0.2"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "22.19.19",
|
package/src/index.ts
CHANGED
|
@@ -95,6 +95,13 @@ export * from './uint8-utils';
|
|
|
95
95
|
export { validateDownloadUrl } from './validate-download-url';
|
|
96
96
|
export * from './validate-types';
|
|
97
97
|
export { VERSION } from './version';
|
|
98
|
+
export {
|
|
99
|
+
getWebSocketConstructor,
|
|
100
|
+
readWebSocketMessageText,
|
|
101
|
+
toWebSocketUrl,
|
|
102
|
+
type WebSocketConstructor,
|
|
103
|
+
type WebSocketLike,
|
|
104
|
+
} from './websocket';
|
|
98
105
|
export { withUserAgentSuffix } from './with-user-agent-suffix';
|
|
99
106
|
export * from './without-trailing-slash';
|
|
100
107
|
|
package/src/websocket.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export type WebSocketLike = {
|
|
2
|
+
readyState: number;
|
|
3
|
+
send(data: string | Uint8Array | ArrayBuffer): void;
|
|
4
|
+
close(code?: number, reason?: string): void;
|
|
5
|
+
onopen: ((event: unknown) => void) | null;
|
|
6
|
+
onmessage: ((event: { data: unknown }) => void) | null;
|
|
7
|
+
onerror: ((event: unknown) => void) | null;
|
|
8
|
+
onclose: ((event: unknown) => void) | null;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type WebSocketConstructor = new (
|
|
12
|
+
url: string | URL,
|
|
13
|
+
protocols?: string | string[],
|
|
14
|
+
options?: {
|
|
15
|
+
headers?: Record<string, string | undefined>;
|
|
16
|
+
},
|
|
17
|
+
) => WebSocketLike;
|
|
18
|
+
|
|
19
|
+
export function getWebSocketConstructor(
|
|
20
|
+
webSocket: WebSocketConstructor | undefined,
|
|
21
|
+
): WebSocketConstructor {
|
|
22
|
+
const WebSocketConstructor =
|
|
23
|
+
webSocket ?? (globalThis.WebSocket as unknown as WebSocketConstructor);
|
|
24
|
+
|
|
25
|
+
if (WebSocketConstructor == null) {
|
|
26
|
+
throw new Error('No WebSocket implementation available.');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return WebSocketConstructor;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Converts an http(s) URL to the corresponding ws(s) URL.
|
|
34
|
+
*/
|
|
35
|
+
export function toWebSocketUrl(url: string | URL): URL {
|
|
36
|
+
const wsUrl = new URL(url);
|
|
37
|
+
if (wsUrl.protocol === 'http:') {
|
|
38
|
+
wsUrl.protocol = 'ws:';
|
|
39
|
+
} else if (wsUrl.protocol === 'https:') {
|
|
40
|
+
wsUrl.protocol = 'wss:';
|
|
41
|
+
}
|
|
42
|
+
return wsUrl;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const textDecoder = new TextDecoder();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Reads WebSocket message data as text, handling string, binary,
|
|
49
|
+
* and Blob payloads.
|
|
50
|
+
*/
|
|
51
|
+
export async function readWebSocketMessageText(data: unknown): Promise<string> {
|
|
52
|
+
if (typeof data === 'string') return data;
|
|
53
|
+
if (data instanceof ArrayBuffer) return textDecoder.decode(data);
|
|
54
|
+
if (ArrayBuffer.isView(data)) {
|
|
55
|
+
return textDecoder.decode(data);
|
|
56
|
+
}
|
|
57
|
+
if (typeof Blob !== 'undefined' && data instanceof Blob) {
|
|
58
|
+
return data.text();
|
|
59
|
+
}
|
|
60
|
+
return String(data);
|
|
61
|
+
}
|