@sandbox-kit/modal 0.1.0 → 0.1.2

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.
@@ -1,161 +0,0 @@
1
- // Code generated by protoc-gen-kit-typescript. DO NOT EDIT.
2
- // source: kit/providers/modal/v1/provider.proto + specs/providers/modal.yaml
3
- import { durationMs } from "@bufbuild/protobuf/wkt";
4
- import { ErrorKind, PolicyMode, ValueOrigin, sandboxError } from "@sandbox-kit/sandbox";
5
- export function mapSandboxIdentity(source, operation = "create") {
6
- const target = {};
7
- if (source === undefined)
8
- return target;
9
- const origins = {};
10
- target.origins = origins;
11
- {
12
- if (source.sandboxId !== undefined) {
13
- const value = source.sandboxId;
14
- target.id = value;
15
- origins["id"] = ValueOrigin.Provider;
16
- }
17
- }
18
- target.provider = "modal";
19
- return target;
20
- }
21
- export function mapClientScope(source, operation = "initialize") {
22
- const target = {};
23
- if (source === undefined)
24
- return target;
25
- {
26
- if (source.environment !== undefined) {
27
- const value = source.environment;
28
- target.environment = value;
29
- }
30
- }
31
- return target;
32
- }
33
- export function mapCommonCreateFields(source, operation = "create") {
34
- const target = {};
35
- if (source === undefined)
36
- return target;
37
- {
38
- if (source.name !== undefined) {
39
- const value = source.name;
40
- target.name = value;
41
- }
42
- }
43
- {
44
- if (source.environment !== undefined) {
45
- const value = source.environment;
46
- target.env = { ...value };
47
- }
48
- }
49
- {
50
- if (source.labels !== undefined) {
51
- const value = source.labels;
52
- target.tags = { ...value };
53
- }
54
- }
55
- return target;
56
- }
57
- export function mapResources(source, operation = "create") {
58
- const target = {};
59
- if (source === undefined)
60
- return target;
61
- {
62
- if (source.cpuCores !== undefined) {
63
- const value = source.cpuCores;
64
- target.cpu = value;
65
- }
66
- }
67
- {
68
- if (source.cpuLimitCores !== undefined) {
69
- const value = source.cpuLimitCores;
70
- target.cpuLimit = value;
71
- }
72
- }
73
- {
74
- if (source.memoryMib !== undefined) {
75
- const value = source.memoryMib;
76
- if (value > 2147483647n)
77
- mappingError(ErrorKind.InvalidArgument, "resources.memory_mib", "memory_mib cannot be represented", operation);
78
- target.memoryMiB = Number(value);
79
- }
80
- }
81
- {
82
- if (source.memoryLimitMib !== undefined) {
83
- const value = source.memoryLimitMib;
84
- if (value > 2147483647n)
85
- mappingError(ErrorKind.InvalidArgument, "resources.memory_limit_mib", "memory_limit_mib cannot be represented", operation);
86
- target.memoryLimitMiB = Number(value);
87
- }
88
- }
89
- return target;
90
- }
91
- export function mapLifetimeDuration(source, operation = "create") {
92
- const target = {};
93
- if (source === undefined)
94
- return target;
95
- {
96
- if (source.maximumLifetime !== undefined) {
97
- const value = durationMs(source.maximumLifetime);
98
- if (value > 86400000)
99
- mappingError(ErrorKind.InvalidArgument, "lifetime.maximum_lifetime", "maximum_lifetime cannot be represented", operation);
100
- target.timeoutMs = value;
101
- }
102
- }
103
- return target;
104
- }
105
- export function mapIdleDuration(source, operation = "create") {
106
- const target = {};
107
- if (source === undefined)
108
- return target;
109
- {
110
- if (source.after !== undefined) {
111
- const value = durationMs(source.after);
112
- if (value > 4294967295000)
113
- mappingError(ErrorKind.InvalidArgument, "lifetime.idle_terminate.after", "after cannot be represented", operation);
114
- target.idleTimeoutMs = value;
115
- }
116
- }
117
- return target;
118
- }
119
- export function mapTokenPairCredentials(source, operation = "initialize") {
120
- const target = {};
121
- if (source === undefined)
122
- return target;
123
- {
124
- const value = source.id;
125
- target.tokenId = value;
126
- }
127
- {
128
- const value = source.secret;
129
- target.tokenSecret = value;
130
- }
131
- return target;
132
- }
133
- export function mapOAuthAuth(source, operation = "create") {
134
- const target = {};
135
- if (source === undefined)
136
- return target;
137
- {
138
- const value = source.refreshToken;
139
- target.oauthRefreshToken = value;
140
- }
141
- {
142
- const value = source.clientId;
143
- target.oauthClientId = value;
144
- }
145
- {
146
- if (source.clientSecret !== undefined) {
147
- const value = source.clientSecret;
148
- target.oauthClientSecret = value;
149
- }
150
- }
151
- {
152
- if (source.jwtKey !== undefined) {
153
- const value = source.jwtKey;
154
- target.oauthJwtKey = value;
155
- }
156
- }
157
- return target;
158
- }
159
- function mappingError(kind, field, message, operation) {
160
- throw sandboxError({ kind, provider: "modal", operation, field, message: "sandbox-kit modal: " + message });
161
- }
@@ -1,3 +0,0 @@
1
- import type { CreateOptions } from "@sandbox-kit/sandbox";
2
- export declare function validateProviderCreate(request: CreateOptions): void;
3
- //# sourceMappingURL=provider_checks.d.ts.map
@@ -1,106 +0,0 @@
1
- import { ErrorKind, sandboxError } from "@sandbox-kit/sandbox";
2
- // validateProviderCreate rejects creation settings this provider cannot honor.
3
- // Rules are inventoried in specs/providers/modal.yaml checks.
4
- export function validateProviderCreate(request) {
5
- const cpuCores = request.resources?.cpuCores;
6
- if (cpuCores !== undefined && cpuCores < 0.125) {
7
- fail(ErrorKind.InvalidArgument, "resources.cpuCores", "resources.cpu_cores is outside supported values");
8
- }
9
- const memoryMib = request.resources?.memoryMib;
10
- if (memoryMib !== undefined && memoryMib < BigInt("128")) {
11
- fail(ErrorKind.InvalidArgument, "resources.memoryMib", "resources.memory_mib is outside supported values");
12
- }
13
- const workingDirectory = request.runtime?.workingDirectory;
14
- if (workingDirectory !== undefined && !workingDirectory.startsWith("/")) {
15
- fail(ErrorKind.InvalidArgument, "runtime.workingDirectory", "runtime.working_directory is outside supported values");
16
- }
17
- const cloud = request.placement?.cloud;
18
- if (cloud !== undefined &&
19
- cloud !== "aws" &&
20
- cloud !== "gcp" &&
21
- cloud !== "oci" &&
22
- cloud !== "auto") {
23
- fail(ErrorKind.Unsupported, "placement.cloud", "placement.cloud is outside supported values");
24
- }
25
- for (const value of request.network?.outboundCidrs ?? []) {
26
- if (value !== "" && !validCidr(value)) {
27
- fail(ErrorKind.InvalidArgument, "network.outboundCidrs.entries", "network.outbound_cidrs.entries is outside supported values");
28
- }
29
- }
30
- for (const value of request.network?.inboundCidrs ?? []) {
31
- if (!validCidr(value)) {
32
- fail(ErrorKind.InvalidArgument, "network.inboundCidrs.entries", "network.inbound_cidrs.entries is outside supported values");
33
- }
34
- }
35
- for (const value of request.network?.outboundDomains ?? []) {
36
- if (value !== "" && !validDomain(value)) {
37
- fail(ErrorKind.InvalidArgument, "network.outboundDomains.entries", "network.outbound_domains.entries is outside supported values");
38
- }
39
- }
40
- for (const value of Object.keys(request.environment ?? {})) {
41
- if (!validEnvName(value)) {
42
- fail(ErrorKind.InvalidArgument, "environment", "environment is outside supported values");
43
- }
44
- }
45
- }
46
- function validEnvName(value) {
47
- return /^[A-Za-z_][A-Za-z0-9_]*$/.test(value);
48
- }
49
- function validDomain(value) {
50
- const domain = value.startsWith("*.") ? value.slice(2) : value;
51
- return (domain.length <= 253 &&
52
- /^(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.?$/i.test(domain));
53
- }
54
- function validCidr(value) {
55
- const separator = value.lastIndexOf("/");
56
- if (separator <= 0)
57
- return false;
58
- const address = value.slice(0, separator);
59
- const prefix = Number(value.slice(separator + 1));
60
- if (!Number.isInteger(prefix))
61
- return false;
62
- return address.includes(":")
63
- ? prefix >= 0 && prefix <= 128 && validIpv6(address)
64
- : prefix >= 0 && prefix <= 32 && validIpv4(address);
65
- }
66
- function validIpv4(value) {
67
- const parts = value.split(".");
68
- return (parts.length === 4 &&
69
- parts.every((part) => /^(0|[1-9][0-9]{0,2})$/.test(part) && Number(part) <= 255));
70
- }
71
- function validIpv6(value) {
72
- if ((value.match(/::/g)?.length ?? 0) > 1)
73
- return false;
74
- const halves = value.split("::");
75
- if (halves.length > 2)
76
- return false;
77
- const count = (part) => {
78
- if (part === "")
79
- return 0;
80
- const groups = part.split(":");
81
- let total = 0;
82
- for (const group of groups) {
83
- if (/^[0-9a-f]{1,4}$/i.test(group))
84
- total++;
85
- else if (validIpv4(group))
86
- total += 2;
87
- else
88
- return undefined;
89
- }
90
- return total;
91
- };
92
- const left = count(halves[0] ?? "");
93
- const right = count(halves[1] ?? "");
94
- return (left !== undefined &&
95
- right !== undefined &&
96
- (halves.length === 2 ? left + right < 8 : left === 8));
97
- }
98
- function fail(kind, field, message) {
99
- throw sandboxError({
100
- kind,
101
- provider: "modal",
102
- operation: "create",
103
- field,
104
- message: "sandbox-kit modal: " + message,
105
- });
106
- }
@@ -1,9 +0,0 @@
1
- import type { ModalClientParams as NativeClientConfig } from "modal";
2
- import { type Config, type Scope } from "@sandbox-kit/sandbox";
3
- export declare function mapGeneratedClientConfig(config: Config): NativeClientConfig;
4
- export interface ProviderState {
5
- readonly scope?: Scope;
6
- readonly region?: string;
7
- }
8
- export declare function captureProviderState(config: Config): ProviderState;
9
- //# sourceMappingURL=provider_config.d.ts.map
@@ -1,46 +0,0 @@
1
- // Client configuration composition for the native SDK constructor.
2
- // Kept in sync with specs/providers/modal.yaml client: rules (not emitted).
3
- import { ErrorKind, sandboxError, validateConfig, } from "@sandbox-kit/sandbox";
4
- import { mapClientScope, mapOAuthAuth, mapTokenPairCredentials, } from "./provider_mappings.js";
5
- export function mapGeneratedClientConfig(config) {
6
- const source = config;
7
- validateConfig(source, "modal", "initialize");
8
- const target = {};
9
- if (source.endpoint !== undefined)
10
- unsupported("endpoint", "Endpoint cannot be configured through this SDK's public client constructor");
11
- if (source.scope !== undefined) {
12
- const mapped = mapClientScope(source.scope, "initialize");
13
- Object.assign(target, mapped);
14
- if (source.scope.organizationId !== undefined)
15
- unsupported("scope.organizationId", "unsupported client configuration field");
16
- if (source.scope.projectId !== undefined)
17
- unsupported("scope.projectId", "unsupported client configuration field");
18
- }
19
- if (source.auth?.tokenPair !== undefined) {
20
- Object.assign(target, mapTokenPairCredentials(source.auth.tokenPair, "initialize"));
21
- }
22
- else if (source.auth?.oauthRefresh !== undefined) {
23
- Object.assign(target, mapOAuthAuth(source.auth.oauthRefresh, "initialize"));
24
- }
25
- else if (source.auth !== undefined) {
26
- unsupported("auth", "unsupported authentication mode");
27
- }
28
- return target;
29
- }
30
- export function captureProviderState(config) {
31
- return {
32
- ...(config.scope === undefined
33
- ? {}
34
- : { scope: structuredClone(config.scope) }),
35
- ...(config.region === undefined ? {} : { region: config.region }),
36
- };
37
- }
38
- function unsupported(field, message) {
39
- throw sandboxError({
40
- kind: ErrorKind.Unsupported,
41
- provider: "modal",
42
- operation: "initialize",
43
- field,
44
- message: "sandbox-kit modal: " + message,
45
- });
46
- }
@@ -1,15 +0,0 @@
1
- import { type SandboxInfo, type Scope } from "@sandbox-kit/sandbox";
2
- import type { CreateOptions, OAuthCredentials, Resources, TokenPairCredentials } from "@sandbox-kit/sandbox";
3
- import type { ModalClientParams as NativeModalClientParams, Sandbox as NativeSandbox, SandboxCreateParams as NativeSandboxCreateParams } from "modal";
4
- type PublicLifetime = NonNullable<CreateOptions["lifetime"]>;
5
- type PublicIdleTerminate = NonNullable<PublicLifetime["idleTerminate"]>;
6
- export declare function mapSandboxIdentity(source: NativeSandbox | undefined, operation?: string): Partial<SandboxInfo>;
7
- export declare function mapClientScope(source: Scope | undefined, operation?: string): NativeModalClientParams;
8
- export declare function mapCommonCreateFields(source: CreateOptions | undefined, operation?: string): NativeSandboxCreateParams;
9
- export declare function mapResources(source: Resources | undefined, operation?: string): NativeSandboxCreateParams;
10
- export declare function mapLifetimeDuration(source: PublicLifetime | undefined, operation?: string): NativeSandboxCreateParams;
11
- export declare function mapIdleDuration(source: PublicIdleTerminate | undefined, operation?: string): NativeSandboxCreateParams;
12
- export declare function mapTokenPairCredentials(source: TokenPairCredentials | undefined, operation?: string): NativeModalClientParams;
13
- export declare function mapOAuthAuth(source: OAuthCredentials | undefined, operation?: string): NativeModalClientParams;
14
- export {};
15
- //# sourceMappingURL=provider_mappings.d.ts.map
@@ -1,179 +0,0 @@
1
- // Provider field mappings between shared CreateOptions/Config and the native SDK.
2
- // Inventoried in specs/providers/modal.yaml groups; kept in sync by hand (not emitted).
3
- import { ErrorKind, ValueOrigin, sandboxError } from "@sandbox-kit/sandbox";
4
- export function mapSandboxIdentity(source, operation = "create") {
5
- const target = {};
6
- if (source === undefined)
7
- return target;
8
- const origins = {};
9
- target.origins = origins;
10
- {
11
- if (source.sandboxId !== undefined) {
12
- const value = source.sandboxId;
13
- target.id = value;
14
- origins["id"] = ValueOrigin.Provider;
15
- }
16
- }
17
- target.provider = "modal";
18
- return target;
19
- }
20
- export function mapClientScope(source, operation = "initialize") {
21
- const target = {};
22
- if (source === undefined)
23
- return target;
24
- {
25
- if (source.environment !== undefined) {
26
- const value = source.environment;
27
- target.environment = value;
28
- }
29
- }
30
- return target;
31
- }
32
- export function mapCommonCreateFields(source, operation = "create") {
33
- const target = {};
34
- if (source === undefined)
35
- return target;
36
- {
37
- if (source.name !== undefined) {
38
- const value = source.name;
39
- target.name = value;
40
- }
41
- }
42
- {
43
- if (source.environment !== undefined) {
44
- const value = source.environment;
45
- target.env = { ...value };
46
- }
47
- }
48
- {
49
- if (source.labels !== undefined) {
50
- const value = source.labels;
51
- target.tags = { ...value };
52
- }
53
- }
54
- return target;
55
- }
56
- export function mapResources(source, operation = "create") {
57
- const target = {};
58
- if (source === undefined)
59
- return target;
60
- {
61
- if (source.cpuCores !== undefined) {
62
- target.cpu = scaledCpuCores(source.cpuCores, "resources.cpu_cores", operation);
63
- }
64
- }
65
- {
66
- if (source.cpuLimitCores !== undefined) {
67
- target.cpuLimit = scaledCpuCores(source.cpuLimitCores, "resources.cpu_limit_cores", operation);
68
- }
69
- }
70
- {
71
- if (source.memoryMib !== undefined) {
72
- const value = source.memoryMib;
73
- if (value > 2147483647n)
74
- mappingError(ErrorKind.InvalidArgument, "resources.memory_mib", "memory_mib cannot be represented", operation);
75
- target.memoryMiB = Number(value);
76
- }
77
- }
78
- {
79
- if (source.memoryLimitMib !== undefined) {
80
- const value = source.memoryLimitMib;
81
- if (value > 2147483647n)
82
- mappingError(ErrorKind.InvalidArgument, "resources.memory_limit_mib", "memory_limit_mib cannot be represented", operation);
83
- target.memoryLimitMiB = Number(value);
84
- }
85
- }
86
- return target;
87
- }
88
- export function mapLifetimeDuration(source, operation = "create") {
89
- const target = {};
90
- if (source === undefined)
91
- return target;
92
- {
93
- if (source.maximumLifetime !== undefined) {
94
- target.timeoutMs = durationSecondsMs(source.maximumLifetime, "lifetime.maximum_lifetime", 86_400, operation);
95
- }
96
- }
97
- return target;
98
- }
99
- export function mapIdleDuration(source, operation = "create") {
100
- const target = {};
101
- if (source === undefined)
102
- return target;
103
- {
104
- if (source.after !== undefined) {
105
- target.idleTimeoutMs = durationSecondsMs(source.after, "lifetime.idle_terminate.after", 4_294_967_295, operation);
106
- }
107
- }
108
- return target;
109
- }
110
- /** Modal CPU fields are millis-scaled: value must equal round(value*1000)/1000. */
111
- function scaledCpuCores(value, field, operation) {
112
- const scaled = Math.round(value * 1000);
113
- if (!Number.isFinite(value) ||
114
- value < 0 ||
115
- value !== scaled / 1000 ||
116
- scaled > 4_294_967_295) {
117
- mappingError(ErrorKind.InvalidArgument, field, field.slice(field.lastIndexOf(".") + 1) + " cannot be represented", operation);
118
- }
119
- return value * 1000 < scaled ? scaled / 1000 : value;
120
- }
121
- /** Modal lifetime/idle timeouts are whole seconds expressed as milliseconds. */
122
- function durationSecondsMs(value, field, maximumSeconds, operation) {
123
- if (!Number.isFinite(value) ||
124
- value <= 0 ||
125
- value % 1000 !== 0 ||
126
- value / 1000 > maximumSeconds) {
127
- mappingError(ErrorKind.InvalidArgument, field, field.slice(field.lastIndexOf(".") + 1) + " cannot be represented", operation);
128
- }
129
- return value;
130
- }
131
- export function mapTokenPairCredentials(source, operation = "initialize") {
132
- const target = {};
133
- if (source === undefined)
134
- return target;
135
- {
136
- const value = source.id;
137
- target.tokenId = value;
138
- }
139
- {
140
- const value = source.secret;
141
- target.tokenSecret = value;
142
- }
143
- return target;
144
- }
145
- export function mapOAuthAuth(source, operation = "create") {
146
- const target = {};
147
- if (source === undefined)
148
- return target;
149
- {
150
- const value = source.refreshToken;
151
- target.oauthRefreshToken = value;
152
- }
153
- {
154
- const value = source.clientId;
155
- target.oauthClientId = value;
156
- }
157
- {
158
- if (source.clientSecret !== undefined) {
159
- const value = source.clientSecret;
160
- target.oauthClientSecret = value;
161
- }
162
- }
163
- {
164
- if (source.jwtKey !== undefined) {
165
- const value = source.jwtKey;
166
- target.oauthJwtKey = value;
167
- }
168
- }
169
- return target;
170
- }
171
- function mappingError(kind, field, message, operation) {
172
- throw sandboxError({
173
- kind,
174
- provider: "modal",
175
- operation,
176
- field,
177
- message: "sandbox-kit modal: " + message,
178
- });
179
- }