@ai-sdk/sandbox-vercel 1.0.71 → 1.0.73
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 +19 -0
- package/README.md +8 -1
- package/dist/index.js +531 -37
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/vercel-network-policy-manager.ts +760 -0
- package/src/vercel-network-sandbox-session.ts +34 -42
|
@@ -2,9 +2,12 @@ import {
|
|
|
2
2
|
HarnessCapabilityUnsupportedError,
|
|
3
3
|
type HarnessV1NetworkPolicy,
|
|
4
4
|
type HarnessV1NetworkSandboxSession,
|
|
5
|
+
type HarnessV1PortEndpoint,
|
|
6
|
+
type HarnessV1RequestTransformation,
|
|
5
7
|
} from '@ai-sdk/harness';
|
|
6
8
|
import type { Experimental_SandboxSession as SandboxSession } from '@ai-sdk/provider-utils';
|
|
7
|
-
import type { Sandbox
|
|
9
|
+
import type { Sandbox } from '@vercel/sandbox';
|
|
10
|
+
import { VercelNetworkPolicyManager } from './vercel-network-policy-manager';
|
|
8
11
|
import { VercelSandboxSession } from './vercel-sandbox-session';
|
|
9
12
|
|
|
10
13
|
const VERCEL_PROVIDER_ID = 'vercel-sandbox';
|
|
@@ -24,12 +27,16 @@ export class VercelNetworkSandboxSession
|
|
|
24
27
|
readonly id: string;
|
|
25
28
|
readonly defaultWorkingDirectory: string;
|
|
26
29
|
private readonly ownsLifecycle: boolean;
|
|
30
|
+
readonly #networkPolicyManager: VercelNetworkPolicyManager;
|
|
27
31
|
|
|
28
32
|
constructor(input: { sandbox: Sandbox; ownsLifecycle: boolean }) {
|
|
29
33
|
super(input.sandbox);
|
|
30
34
|
this.ownsLifecycle = input.ownsLifecycle;
|
|
31
35
|
this.id = input.sandbox.name;
|
|
32
36
|
this.defaultWorkingDirectory = input.sandbox.currentSession().cwd;
|
|
37
|
+
this.#networkPolicyManager = new VercelNetworkPolicyManager({
|
|
38
|
+
sandbox: input.sandbox,
|
|
39
|
+
});
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
get ports(): ReadonlyArray<number> {
|
|
@@ -40,10 +47,10 @@ export class VercelNetworkSandboxSession
|
|
|
40
47
|
return new VercelSandboxSession(this.sandbox);
|
|
41
48
|
}
|
|
42
49
|
|
|
43
|
-
|
|
50
|
+
getPortEndpoint = async (options: {
|
|
44
51
|
port: number;
|
|
45
52
|
protocol?: 'http' | 'https' | 'ws';
|
|
46
|
-
}): Promise<
|
|
53
|
+
}): Promise<HarnessV1PortEndpoint> => {
|
|
47
54
|
const exposedPorts = this.ports;
|
|
48
55
|
if (!exposedPorts.includes(options.port)) {
|
|
49
56
|
throw new HarnessCapabilityUnsupportedError({
|
|
@@ -65,11 +72,33 @@ export class VercelNetworkSandboxSession
|
|
|
65
72
|
url.protocol = isSecure ? 'wss:' : 'ws:';
|
|
66
73
|
break;
|
|
67
74
|
}
|
|
68
|
-
return url.toString();
|
|
75
|
+
return { url: url.toString() };
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Use `getPortEndpoint` instead.
|
|
80
|
+
*/
|
|
81
|
+
getPortUrl = async (options: {
|
|
82
|
+
port: number;
|
|
83
|
+
protocol?: 'http' | 'https' | 'ws';
|
|
84
|
+
}): Promise<string> => {
|
|
85
|
+
return (await this.getPortEndpoint(options)).url;
|
|
69
86
|
};
|
|
70
87
|
|
|
71
88
|
setNetworkPolicy = async (policy: HarnessV1NetworkPolicy): Promise<void> => {
|
|
72
|
-
await this.
|
|
89
|
+
await this.#networkPolicyManager.setNetworkPolicy(policy);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
setRequestTransformations = async (
|
|
93
|
+
transformations: ReadonlyArray<HarnessV1RequestTransformation>,
|
|
94
|
+
): Promise<void> => {
|
|
95
|
+
await this.#networkPolicyManager.setRequestTransformations(transformations);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
addRequestTransformations = async (
|
|
99
|
+
transformations: ReadonlyArray<HarnessV1RequestTransformation>,
|
|
100
|
+
): Promise<void> => {
|
|
101
|
+
await this.#networkPolicyManager.addRequestTransformations(transformations);
|
|
73
102
|
};
|
|
74
103
|
|
|
75
104
|
setPorts = async (
|
|
@@ -93,40 +122,3 @@ export class VercelNetworkSandboxSession
|
|
|
93
122
|
await this.sandbox.delete();
|
|
94
123
|
};
|
|
95
124
|
}
|
|
96
|
-
|
|
97
|
-
export function toVercelPolicy(policy: HarnessV1NetworkPolicy): NetworkPolicy {
|
|
98
|
-
switch (policy.mode) {
|
|
99
|
-
case 'allow-all':
|
|
100
|
-
return 'allow-all';
|
|
101
|
-
case 'deny-all':
|
|
102
|
-
return 'deny-all';
|
|
103
|
-
case 'custom': {
|
|
104
|
-
const result: Extract<NetworkPolicy, { allow?: unknown }> = {};
|
|
105
|
-
const { allowedHosts, allowedCIDRs, deniedCIDRs } = policy;
|
|
106
|
-
if (allowedHosts != null && allowedHosts.length > 0) {
|
|
107
|
-
result.allow = [...allowedHosts];
|
|
108
|
-
}
|
|
109
|
-
if (
|
|
110
|
-
(allowedCIDRs != null && allowedCIDRs.length > 0) ||
|
|
111
|
-
(deniedCIDRs != null && deniedCIDRs.length > 0)
|
|
112
|
-
) {
|
|
113
|
-
result.subnets = {
|
|
114
|
-
...(allowedCIDRs != null && allowedCIDRs.length > 0
|
|
115
|
-
? { allow: [...allowedCIDRs] }
|
|
116
|
-
: {}),
|
|
117
|
-
...(deniedCIDRs != null && deniedCIDRs.length > 0
|
|
118
|
-
? { deny: [...deniedCIDRs] }
|
|
119
|
-
: {}),
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
if (result.allow == null && result.subnets == null) {
|
|
123
|
-
throw new HarnessCapabilityUnsupportedError({
|
|
124
|
-
harnessId: VERCEL_PROVIDER_ID,
|
|
125
|
-
message:
|
|
126
|
-
'Custom network policy requires at least one of allowedHosts or allowedCIDRs to be non-empty.',
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
return result;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|