@blaxel/core 0.2.49-dev.214 → 0.2.49-dev1
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/dist/agents/index.d.ts +15 -0
- package/dist/agents/index.js +109 -0
- package/dist/authentication/apikey.d.ts +10 -0
- package/dist/authentication/apikey.js +24 -0
- package/dist/authentication/clientcredentials.d.ts +17 -0
- package/dist/authentication/clientcredentials.js +85 -0
- package/dist/authentication/credentials.d.ts +6 -0
- package/dist/authentication/credentials.js +17 -0
- package/dist/authentication/deviceMode.d.ts +15 -0
- package/dist/authentication/deviceMode.js +70 -0
- package/dist/authentication/index.d.ts +3 -0
- package/dist/authentication/index.js +62 -0
- package/dist/authentication/types.d.ts +9 -0
- package/dist/authentication/types.js +2 -0
- package/dist/cache/index.d.ts +1 -0
- package/dist/cache/index.js +26 -0
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/agents/index.js +2 -2
- package/dist/cjs/authentication/clientcredentials.js +1 -4
- package/dist/cjs/client/responseInterceptor.js +53 -0
- package/dist/cjs/client/sdk.gen.js +143 -3
- package/dist/cjs/common/autoload.js +7 -0
- package/dist/cjs/common/env.js +3 -0
- package/dist/cjs/common/internal.js +0 -40
- package/dist/cjs/common/settings.js +2 -5
- package/dist/cjs/common/version.js +6 -0
- package/dist/cjs/common/webhook.js +101 -0
- package/dist/cjs/jobs/executions.js +119 -0
- package/dist/cjs/jobs/jobs.js +2 -2
- package/dist/cjs/jobs/start.js +1 -32
- package/dist/cjs/sandbox/action.js +2 -1
- package/dist/cjs/sandbox/index.js +1 -0
- package/dist/cjs/sandbox/interpreter.js +400 -0
- package/dist/cjs/sandbox/sandbox.js +10 -67
- package/dist/cjs/tools/mcpTool.js +2 -6
- package/dist/cjs/types/client/responseInterceptor.d.ts +12 -0
- package/dist/cjs/types/client/sdk.gen.d.ts +41 -1
- package/dist/cjs/types/client/types.gen.d.ts +599 -16
- package/dist/cjs/types/common/internal.d.ts +0 -2
- package/dist/cjs/types/common/settings.d.ts +0 -1
- package/dist/cjs/types/common/version.d.ts +2 -0
- package/dist/cjs/types/common/webhook.d.ts +89 -0
- package/dist/cjs/types/jobs/executions.d.ts +42 -0
- package/dist/cjs/types/sandbox/client/sdk.gen.d.ts +1 -1
- package/dist/cjs/types/sandbox/index.d.ts +1 -0
- package/dist/cjs/types/sandbox/interpreter.d.ts +71 -0
- package/dist/cjs/types/sandbox/sandbox.d.ts +6 -12
- package/dist/cjs/types/sandbox/types.d.ts +0 -3
- package/dist/cjs-browser/.tsbuildinfo +1 -1
- package/dist/cjs-browser/agents/index.js +2 -2
- package/dist/cjs-browser/authentication/clientcredentials.js +1 -4
- package/dist/cjs-browser/client/responseInterceptor.js +53 -0
- package/dist/cjs-browser/client/sdk.gen.js +143 -3
- package/dist/cjs-browser/common/autoload.js +7 -0
- package/dist/cjs-browser/common/env.js +3 -0
- package/dist/cjs-browser/common/internal.js +0 -40
- package/dist/cjs-browser/common/settings.js +2 -5
- package/dist/cjs-browser/common/version.js +6 -0
- package/dist/cjs-browser/common/webhook.js +101 -0
- package/dist/cjs-browser/jobs/executions.js +119 -0
- package/dist/cjs-browser/jobs/jobs.js +2 -2
- package/dist/cjs-browser/jobs/start.js +1 -32
- package/dist/cjs-browser/sandbox/action.js +2 -1
- package/dist/cjs-browser/sandbox/index.js +1 -0
- package/dist/cjs-browser/sandbox/interpreter.js +400 -0
- package/dist/cjs-browser/sandbox/sandbox.js +10 -67
- package/dist/cjs-browser/tools/mcpTool.js +2 -6
- package/dist/cjs-browser/types/client/responseInterceptor.d.ts +12 -0
- package/dist/cjs-browser/types/client/sdk.gen.d.ts +41 -1
- package/dist/cjs-browser/types/client/types.gen.d.ts +599 -16
- package/dist/cjs-browser/types/common/internal.d.ts +0 -2
- package/dist/cjs-browser/types/common/settings.d.ts +0 -1
- package/dist/cjs-browser/types/common/version.d.ts +2 -0
- package/dist/cjs-browser/types/common/webhook.d.ts +89 -0
- package/dist/cjs-browser/types/jobs/executions.d.ts +42 -0
- package/dist/cjs-browser/types/sandbox/client/sdk.gen.d.ts +1 -1
- package/dist/cjs-browser/types/sandbox/index.d.ts +1 -0
- package/dist/cjs-browser/types/sandbox/interpreter.d.ts +71 -0
- package/dist/cjs-browser/types/sandbox/sandbox.d.ts +6 -12
- package/dist/cjs-browser/types/sandbox/types.d.ts +0 -3
- package/dist/client/authentication.d.ts +25 -0
- package/dist/client/authentication.js +15 -0
- package/dist/client/client.d.ts +2 -0
- package/dist/client/client.gen.d.ts +12 -0
- package/dist/client/client.gen.js +8 -0
- package/dist/{cjs/sandbox/websocket/index.js → client/client.js} +1 -1
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +19 -0
- package/dist/client/interceptors.d.ts +3 -0
- package/dist/client/interceptors.js +17 -0
- package/dist/client/sdk.gen.d.ts +499 -0
- package/dist/client/sdk.gen.js +1751 -0
- package/dist/client/types.gen.d.ts +4546 -0
- package/dist/client/types.gen.js +4 -0
- package/dist/common/autoload.d.ts +3 -0
- package/dist/common/autoload.js +27 -0
- package/dist/common/env.d.ts +5 -0
- package/dist/common/env.js +42 -0
- package/dist/common/errors.d.ts +1 -0
- package/dist/common/errors.js +17 -0
- package/dist/common/internal.d.ts +4 -0
- package/dist/common/internal.js +188 -0
- package/dist/common/internal.test.d.ts +1 -0
- package/dist/common/internal.test.js +39 -0
- package/dist/common/logger.d.ts +25 -0
- package/dist/common/logger.js +69 -0
- package/dist/common/node.d.ts +5 -0
- package/dist/common/node.js +26 -0
- package/dist/common/settings.d.ts +31 -0
- package/dist/common/settings.js +170 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/agents/index.js +3 -3
- package/dist/esm/authentication/clientcredentials.js +1 -4
- package/dist/esm/client/responseInterceptor.js +49 -0
- package/dist/esm/client/sdk.gen.js +132 -0
- package/dist/esm/common/autoload.js +7 -0
- package/dist/esm/common/env.js +3 -0
- package/dist/esm/common/internal.js +0 -38
- package/dist/esm/common/settings.js +2 -5
- package/dist/esm/common/version.js +3 -0
- package/dist/esm/common/webhook.js +97 -0
- package/dist/esm/jobs/executions.js +114 -0
- package/dist/esm/jobs/jobs.js +3 -3
- package/dist/esm/jobs/start.js +1 -32
- package/dist/esm/sandbox/action.js +3 -2
- package/dist/esm/sandbox/index.js +1 -0
- package/dist/esm/sandbox/interpreter.js +396 -0
- package/dist/esm/sandbox/sandbox.js +10 -67
- package/dist/esm/tools/mcpTool.js +3 -7
- package/dist/esm-browser/.tsbuildinfo +1 -1
- package/dist/esm-browser/agents/index.js +3 -3
- package/dist/esm-browser/authentication/clientcredentials.js +1 -4
- package/dist/esm-browser/client/responseInterceptor.js +49 -0
- package/dist/esm-browser/client/sdk.gen.js +132 -0
- package/dist/esm-browser/common/autoload.js +7 -0
- package/dist/esm-browser/common/env.js +3 -0
- package/dist/esm-browser/common/internal.js +0 -38
- package/dist/esm-browser/common/settings.js +2 -5
- package/dist/esm-browser/common/version.js +3 -0
- package/dist/esm-browser/common/webhook.js +97 -0
- package/dist/esm-browser/jobs/executions.js +114 -0
- package/dist/esm-browser/jobs/jobs.js +3 -3
- package/dist/esm-browser/jobs/start.js +1 -32
- package/dist/esm-browser/sandbox/action.js +3 -2
- package/dist/esm-browser/sandbox/index.js +1 -0
- package/dist/esm-browser/sandbox/interpreter.js +396 -0
- package/dist/esm-browser/sandbox/sandbox.js +10 -67
- package/dist/esm-browser/tools/mcpTool.js +3 -7
- package/dist/index.browser.test.d.ts +1 -0
- package/dist/index.browser.test.js +45 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +33 -0
- package/dist/jobs/index.d.ts +3 -0
- package/dist/jobs/index.js +19 -0
- package/dist/jobs/jobs.d.ts +13 -0
- package/dist/jobs/jobs.js +90 -0
- package/dist/jobs/start.d.ts +1 -0
- package/dist/jobs/start.js +66 -0
- package/dist/jobs/types.d.ts +3 -0
- package/dist/jobs/types.js +2 -0
- package/dist/mcp/client.d.ts +28 -0
- package/dist/mcp/client.js +247 -0
- package/dist/{cjs-browser/types/sandbox/websocket → mcp}/index.d.ts +1 -0
- package/dist/{cjs-browser/sandbox/websocket → mcp}/index.js +1 -0
- package/dist/mcp/server.d.ts +24 -0
- package/dist/mcp/server.js +213 -0
- package/dist/models/index.d.ts +8 -0
- package/dist/models/index.js +31 -0
- package/dist/sandbox/action.d.ts +19 -0
- package/dist/sandbox/action.js +84 -0
- package/dist/sandbox/client/client.gen.d.ts +12 -0
- package/dist/sandbox/client/client.gen.js +6 -0
- package/dist/sandbox/client/index.d.ts +2 -0
- package/dist/sandbox/client/index.js +19 -0
- package/dist/sandbox/client/sdk.gen.d.ts +101 -0
- package/dist/sandbox/client/sdk.gen.js +289 -0
- package/dist/sandbox/client/types.gen.d.ts +578 -0
- package/dist/sandbox/client/types.gen.js +3 -0
- package/dist/{cjs/types/sandbox/filesystem/filesystem-ws.d.ts → sandbox/filesystem/filesystem.d.ts} +6 -18
- package/dist/sandbox/filesystem/filesystem.js +276 -0
- package/dist/sandbox/filesystem/index.d.ts +2 -0
- package/dist/sandbox/filesystem/index.js +18 -0
- package/dist/sandbox/filesystem/types.d.ts +15 -0
- package/dist/sandbox/filesystem/types.js +2 -0
- package/dist/sandbox/index.d.ts +4 -0
- package/dist/sandbox/index.js +36 -0
- package/dist/sandbox/network/index.d.ts +1 -0
- package/dist/sandbox/network/index.js +17 -0
- package/dist/sandbox/network/network.d.ts +5 -0
- package/dist/sandbox/network/network.js +10 -0
- package/dist/sandbox/preview.d.ts +37 -0
- package/dist/sandbox/preview.js +148 -0
- package/dist/sandbox/process/index.d.ts +1 -0
- package/dist/sandbox/process/index.js +17 -0
- package/dist/sandbox/process/process.d.ts +24 -0
- package/dist/sandbox/process/process.js +189 -0
- package/dist/sandbox/sandbox.d.ts +33 -0
- package/dist/sandbox/sandbox.js +178 -0
- package/dist/sandbox/session.d.ts +27 -0
- package/dist/sandbox/session.js +123 -0
- package/dist/sandbox/types.d.ts +51 -0
- package/dist/sandbox/types.js +81 -0
- package/dist/telemetry/telemetry.d.ts +62 -0
- package/dist/telemetry/telemetry.js +80 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.js +52 -0
- package/dist/tools/mcpTool.d.ts +28 -0
- package/dist/tools/mcpTool.js +218 -0
- package/dist/tools/types.d.ts +8 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/zodSchema.d.ts +9 -0
- package/dist/tools/zodSchema.js +50 -0
- package/dist/volume/index.d.ts +23 -0
- package/dist/volume/index.js +113 -0
- package/package.json +2 -2
- package/dist/cjs/sandbox/codegen/codegen-ws.js +0 -30
- package/dist/cjs/sandbox/filesystem/filesystem-ws.js +0 -106
- package/dist/cjs/sandbox/network/network-ws.js +0 -12
- package/dist/cjs/sandbox/process/process-ws.js +0 -139
- package/dist/cjs/sandbox/websocket/client.js +0 -269
- package/dist/cjs/types/sandbox/codegen/codegen-ws.d.ts +0 -10
- package/dist/cjs/types/sandbox/network/network-ws.d.ts +0 -7
- package/dist/cjs/types/sandbox/process/process-ws.d.ts +0 -27
- package/dist/cjs/types/sandbox/websocket/client.d.ts +0 -48
- package/dist/cjs/types/sandbox/websocket/index.d.ts +0 -1
- package/dist/cjs-browser/sandbox/codegen/codegen-ws.js +0 -30
- package/dist/cjs-browser/sandbox/filesystem/filesystem-ws.js +0 -106
- package/dist/cjs-browser/sandbox/network/network-ws.js +0 -12
- package/dist/cjs-browser/sandbox/process/process-ws.js +0 -139
- package/dist/cjs-browser/sandbox/websocket/client.js +0 -269
- package/dist/cjs-browser/types/sandbox/codegen/codegen-ws.d.ts +0 -10
- package/dist/cjs-browser/types/sandbox/filesystem/filesystem-ws.d.ts +0 -35
- package/dist/cjs-browser/types/sandbox/network/network-ws.d.ts +0 -7
- package/dist/cjs-browser/types/sandbox/process/process-ws.d.ts +0 -27
- package/dist/cjs-browser/types/sandbox/websocket/client.d.ts +0 -48
- package/dist/esm/sandbox/codegen/codegen-ws.js +0 -26
- package/dist/esm/sandbox/filesystem/filesystem-ws.js +0 -102
- package/dist/esm/sandbox/network/network-ws.js +0 -8
- package/dist/esm/sandbox/process/process-ws.js +0 -135
- package/dist/esm/sandbox/websocket/client.js +0 -265
- package/dist/esm/sandbox/websocket/index.js +0 -1
- package/dist/esm-browser/sandbox/codegen/codegen-ws.js +0 -26
- package/dist/esm-browser/sandbox/filesystem/filesystem-ws.js +0 -102
- package/dist/esm-browser/sandbox/network/network-ws.js +0 -8
- package/dist/esm-browser/sandbox/process/process-ws.js +0 -135
- package/dist/esm-browser/sandbox/websocket/client.js +0 -265
- package/dist/esm-browser/sandbox/websocket/index.js +0 -1
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxProcess = void 0;
|
|
4
|
+
const settings_js_1 = require("../../common/settings.js");
|
|
5
|
+
const action_js_1 = require("../action.js");
|
|
6
|
+
const index_js_1 = require("../client/index.js");
|
|
7
|
+
class SandboxProcess extends action_js_1.SandboxAction {
|
|
8
|
+
constructor(sandbox) {
|
|
9
|
+
super(sandbox);
|
|
10
|
+
}
|
|
11
|
+
streamLogs(identifier, options) {
|
|
12
|
+
const controller = new AbortController();
|
|
13
|
+
void (async () => {
|
|
14
|
+
try {
|
|
15
|
+
const headers = this.sandbox.forceUrl ? this.sandbox.headers : settings_js_1.settings.headers;
|
|
16
|
+
const stream = await fetch(`${this.url}/process/${identifier}/logs/stream`, {
|
|
17
|
+
method: 'GET',
|
|
18
|
+
signal: controller.signal,
|
|
19
|
+
headers,
|
|
20
|
+
});
|
|
21
|
+
if (stream.status !== 200) {
|
|
22
|
+
throw new Error(`Failed to stream logs: ${await stream.text()}`);
|
|
23
|
+
}
|
|
24
|
+
if (!stream.body)
|
|
25
|
+
throw new Error('No stream body');
|
|
26
|
+
const reader = stream.body.getReader();
|
|
27
|
+
const decoder = new TextDecoder();
|
|
28
|
+
let buffer = '';
|
|
29
|
+
while (true) {
|
|
30
|
+
const result = await reader.read();
|
|
31
|
+
if (result.done)
|
|
32
|
+
break;
|
|
33
|
+
if (result.value && result.value instanceof Uint8Array) {
|
|
34
|
+
buffer += decoder.decode(result.value, { stream: true });
|
|
35
|
+
}
|
|
36
|
+
const lines = buffer.split(/\r?\n/);
|
|
37
|
+
buffer = lines.pop();
|
|
38
|
+
for (const line of lines) {
|
|
39
|
+
if (line.startsWith("[keepalive]")) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (line.startsWith('stdout:')) {
|
|
43
|
+
options.onStdout?.(line.slice(7));
|
|
44
|
+
options.onLog?.(line.slice(7));
|
|
45
|
+
}
|
|
46
|
+
else if (line.startsWith('stderr:')) {
|
|
47
|
+
options.onStderr?.(line.slice(7));
|
|
48
|
+
options.onLog?.(line.slice(7));
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
options.onLog?.(line);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
catch (err) {
|
|
57
|
+
if (err && typeof err === 'object' && 'name' in err && err.name !== 'AbortError') {
|
|
58
|
+
console.error("Stream error:", err);
|
|
59
|
+
throw new Error(err instanceof Error ? err.message : 'Unknown stream error');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
})();
|
|
63
|
+
return {
|
|
64
|
+
close: () => controller.abort(),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async exec(process) {
|
|
68
|
+
let onLog;
|
|
69
|
+
if ('onLog' in process && process.onLog) {
|
|
70
|
+
onLog = process.onLog;
|
|
71
|
+
delete process.onLog;
|
|
72
|
+
}
|
|
73
|
+
// Store original wait_for_completion setting
|
|
74
|
+
const shouldWaitForCompletion = process.waitForCompletion;
|
|
75
|
+
// Always start process without wait_for_completion to avoid server-side blocking
|
|
76
|
+
if (shouldWaitForCompletion && onLog) {
|
|
77
|
+
process.waitForCompletion = false;
|
|
78
|
+
}
|
|
79
|
+
const { response, data, error } = await (0, index_js_1.postProcess)({
|
|
80
|
+
body: process,
|
|
81
|
+
baseUrl: this.url,
|
|
82
|
+
client: this.client,
|
|
83
|
+
});
|
|
84
|
+
this.handleResponseError(response, data, error);
|
|
85
|
+
let result = data;
|
|
86
|
+
// Handle wait_for_completion with parallel log streaming
|
|
87
|
+
if (shouldWaitForCompletion && onLog) {
|
|
88
|
+
const streamControl = this.streamLogs(result.pid, { onLog });
|
|
89
|
+
try {
|
|
90
|
+
// Wait for process completion
|
|
91
|
+
result = await this.wait(result.pid, { interval: 500, maxWait: 1000 * 60 * 60 });
|
|
92
|
+
}
|
|
93
|
+
finally {
|
|
94
|
+
// Clean up log streaming
|
|
95
|
+
if (streamControl) {
|
|
96
|
+
streamControl.close();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// For non-blocking execution, set up log streaming immediately if requested
|
|
102
|
+
if (onLog) {
|
|
103
|
+
const streamControl = this.streamLogs(result.pid, { onLog });
|
|
104
|
+
return {
|
|
105
|
+
...result,
|
|
106
|
+
close() {
|
|
107
|
+
if (streamControl) {
|
|
108
|
+
streamControl.close();
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { ...result, close: () => { } };
|
|
115
|
+
}
|
|
116
|
+
async wait(identifier, { maxWait = 60000, interval = 1000 } = {}) {
|
|
117
|
+
const startTime = Date.now();
|
|
118
|
+
let status = "running";
|
|
119
|
+
let data = await this.get(identifier);
|
|
120
|
+
while (status === "running") {
|
|
121
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
122
|
+
try {
|
|
123
|
+
data = await this.get(identifier);
|
|
124
|
+
status = data.status ?? "running";
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
if (Date.now() - startTime > maxWait) {
|
|
130
|
+
throw new Error("Process did not finish in time");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return data;
|
|
134
|
+
}
|
|
135
|
+
async get(identifier) {
|
|
136
|
+
const { response, data, error } = await (0, index_js_1.getProcessByIdentifier)({
|
|
137
|
+
path: { identifier },
|
|
138
|
+
baseUrl: this.url,
|
|
139
|
+
client: this.client,
|
|
140
|
+
});
|
|
141
|
+
this.handleResponseError(response, data, error);
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
144
|
+
async list() {
|
|
145
|
+
const { response, data, error } = await (0, index_js_1.getProcess)({
|
|
146
|
+
baseUrl: this.url,
|
|
147
|
+
client: this.client,
|
|
148
|
+
});
|
|
149
|
+
this.handleResponseError(response, data, error);
|
|
150
|
+
return data;
|
|
151
|
+
}
|
|
152
|
+
async stop(identifier) {
|
|
153
|
+
const { response, data, error } = await (0, index_js_1.deleteProcessByIdentifier)({
|
|
154
|
+
path: { identifier },
|
|
155
|
+
baseUrl: this.url,
|
|
156
|
+
client: this.client,
|
|
157
|
+
});
|
|
158
|
+
this.handleResponseError(response, data, error);
|
|
159
|
+
return data;
|
|
160
|
+
}
|
|
161
|
+
async kill(identifier) {
|
|
162
|
+
const { response, data, error } = await (0, index_js_1.deleteProcessByIdentifierKill)({
|
|
163
|
+
path: { identifier },
|
|
164
|
+
baseUrl: this.url,
|
|
165
|
+
client: this.client,
|
|
166
|
+
});
|
|
167
|
+
this.handleResponseError(response, data, error);
|
|
168
|
+
return data;
|
|
169
|
+
}
|
|
170
|
+
async logs(identifier, type = "all") {
|
|
171
|
+
const { response, data, error } = await (0, index_js_1.getProcessByIdentifierLogs)({
|
|
172
|
+
path: { identifier },
|
|
173
|
+
baseUrl: this.url,
|
|
174
|
+
client: this.client,
|
|
175
|
+
});
|
|
176
|
+
this.handleResponseError(response, data, error);
|
|
177
|
+
if (type === "all") {
|
|
178
|
+
return data?.logs || "";
|
|
179
|
+
}
|
|
180
|
+
else if (type === "stdout") {
|
|
181
|
+
return data?.stdout || "";
|
|
182
|
+
}
|
|
183
|
+
else if (type === "stderr") {
|
|
184
|
+
return data?.stderr || "";
|
|
185
|
+
}
|
|
186
|
+
throw new Error("Unsupported log type");
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
exports.SandboxProcess = SandboxProcess;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Sandbox as SandboxModel } from "../client/index.js";
|
|
2
|
+
import { SandboxFileSystem } from "./filesystem/index.js";
|
|
3
|
+
import { SandboxNetwork } from "./network/index.js";
|
|
4
|
+
import { SandboxPreviews } from "./preview.js";
|
|
5
|
+
import { SandboxProcess } from "./process/index.js";
|
|
6
|
+
import { SandboxSessions } from "./session.js";
|
|
7
|
+
import { SandboxConfiguration, SandboxCreateConfiguration, SandboxUpdateMetadata, SessionWithToken } from "./types.js";
|
|
8
|
+
export declare class SandboxInstance {
|
|
9
|
+
private sandbox;
|
|
10
|
+
fs: SandboxFileSystem;
|
|
11
|
+
network: SandboxNetwork;
|
|
12
|
+
process: SandboxProcess;
|
|
13
|
+
previews: SandboxPreviews;
|
|
14
|
+
sessions: SandboxSessions;
|
|
15
|
+
constructor(sandbox: SandboxConfiguration);
|
|
16
|
+
get metadata(): import("../client/types.gen.js").Metadata | undefined;
|
|
17
|
+
get status(): string | undefined;
|
|
18
|
+
get events(): import("../client/types.gen.js").CoreEvents | undefined;
|
|
19
|
+
get spec(): import("../client/types.gen.js").SandboxSpec | undefined;
|
|
20
|
+
wait({ maxWait, interval }?: {
|
|
21
|
+
maxWait?: number;
|
|
22
|
+
interval?: number;
|
|
23
|
+
}): Promise<this>;
|
|
24
|
+
static create(sandbox?: SandboxModel | SandboxCreateConfiguration, { safe }?: {
|
|
25
|
+
safe?: boolean;
|
|
26
|
+
}): Promise<SandboxInstance>;
|
|
27
|
+
static get(sandboxName: string): Promise<SandboxInstance>;
|
|
28
|
+
static list(): Promise<SandboxInstance[]>;
|
|
29
|
+
static delete(sandboxName: string): Promise<SandboxModel>;
|
|
30
|
+
static updateMetadata(sandboxName: string, metadata: SandboxUpdateMetadata): Promise<SandboxInstance>;
|
|
31
|
+
static createIfNotExists(sandbox: SandboxModel | SandboxCreateConfiguration): Promise<SandboxInstance>;
|
|
32
|
+
static fromSession(session: SessionWithToken): Promise<SandboxInstance>;
|
|
33
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxInstance = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const index_js_1 = require("../client/index.js");
|
|
6
|
+
const logger_js_1 = require("../common/logger.js");
|
|
7
|
+
const settings_js_1 = require("../common/settings.js");
|
|
8
|
+
const index_js_2 = require("./filesystem/index.js");
|
|
9
|
+
const index_js_3 = require("./network/index.js");
|
|
10
|
+
const preview_js_1 = require("./preview.js");
|
|
11
|
+
const index_js_4 = require("./process/index.js");
|
|
12
|
+
const session_js_1 = require("./session.js");
|
|
13
|
+
const types_js_1 = require("./types.js");
|
|
14
|
+
class SandboxInstance {
|
|
15
|
+
sandbox;
|
|
16
|
+
fs;
|
|
17
|
+
network;
|
|
18
|
+
process;
|
|
19
|
+
previews;
|
|
20
|
+
sessions;
|
|
21
|
+
constructor(sandbox) {
|
|
22
|
+
this.sandbox = sandbox;
|
|
23
|
+
this.fs = new index_js_2.SandboxFileSystem(sandbox);
|
|
24
|
+
this.network = new index_js_3.SandboxNetwork(sandbox);
|
|
25
|
+
this.process = new index_js_4.SandboxProcess(sandbox);
|
|
26
|
+
this.previews = new preview_js_1.SandboxPreviews(sandbox);
|
|
27
|
+
this.sessions = new session_js_1.SandboxSessions(sandbox);
|
|
28
|
+
}
|
|
29
|
+
get metadata() {
|
|
30
|
+
return this.sandbox.metadata;
|
|
31
|
+
}
|
|
32
|
+
get status() {
|
|
33
|
+
return this.sandbox.status;
|
|
34
|
+
}
|
|
35
|
+
get events() {
|
|
36
|
+
return this.sandbox.events;
|
|
37
|
+
}
|
|
38
|
+
get spec() {
|
|
39
|
+
return this.sandbox.spec;
|
|
40
|
+
}
|
|
41
|
+
/* eslint-disable */
|
|
42
|
+
async wait({ maxWait = 60000, interval = 1000 } = {}) {
|
|
43
|
+
logger_js_1.logger.warn("⚠️ Warning: sandbox.wait() is deprecated. You don't need to wait for the sandbox to be deployed anymore.");
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
static async create(sandbox, { safe = true } = {}) {
|
|
47
|
+
const env = settings_js_1.settings.env;
|
|
48
|
+
const defaultName = `sandbox-${(0, uuid_1.v4)().replace(/-/g, '').substring(0, 8)}`;
|
|
49
|
+
const defaultImage = `blaxel/${env}-base:latest`;
|
|
50
|
+
const defaultMemory = 4096;
|
|
51
|
+
// Handle SandboxCreateConfiguration or simple dict with name/image/memory/ports/envs/volumes keys
|
|
52
|
+
if (!sandbox || 'name' in sandbox || 'image' in sandbox || 'memory' in sandbox || 'ports' in sandbox || 'envs' in sandbox || 'volumes' in sandbox) {
|
|
53
|
+
if (!sandbox)
|
|
54
|
+
sandbox = {};
|
|
55
|
+
if (!sandbox.name)
|
|
56
|
+
sandbox.name = defaultName;
|
|
57
|
+
if (!sandbox.image)
|
|
58
|
+
sandbox.image = defaultImage;
|
|
59
|
+
if (!sandbox.memory)
|
|
60
|
+
sandbox.memory = defaultMemory;
|
|
61
|
+
const ports = (0, types_js_1.normalizePorts)(sandbox.ports);
|
|
62
|
+
const envs = (0, types_js_1.normalizeEnvs)(sandbox.envs);
|
|
63
|
+
const volumes = (0, types_js_1.normalizeVolumes)(sandbox.volumes);
|
|
64
|
+
const ttl = sandbox.ttl;
|
|
65
|
+
const expires = sandbox.expires;
|
|
66
|
+
const region = sandbox.region;
|
|
67
|
+
sandbox = {
|
|
68
|
+
metadata: { name: sandbox.name },
|
|
69
|
+
spec: {
|
|
70
|
+
region: region,
|
|
71
|
+
runtime: {
|
|
72
|
+
image: sandbox.image,
|
|
73
|
+
memory: sandbox.memory,
|
|
74
|
+
ports: ports,
|
|
75
|
+
envs: envs,
|
|
76
|
+
generation: "mk3",
|
|
77
|
+
},
|
|
78
|
+
volumes: volumes
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
if (ttl) {
|
|
82
|
+
sandbox.spec.runtime.ttl = ttl;
|
|
83
|
+
}
|
|
84
|
+
if (expires) {
|
|
85
|
+
sandbox.spec.runtime.expires = expires.toISOString();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
sandbox = sandbox;
|
|
89
|
+
if (!sandbox.metadata) {
|
|
90
|
+
sandbox.metadata = { name: defaultName };
|
|
91
|
+
}
|
|
92
|
+
if (!sandbox.spec) {
|
|
93
|
+
sandbox.spec = { runtime: { image: defaultImage, memory: defaultMemory } };
|
|
94
|
+
}
|
|
95
|
+
if (!sandbox.spec.runtime) {
|
|
96
|
+
sandbox.spec.runtime = { image: defaultImage, memory: defaultMemory };
|
|
97
|
+
}
|
|
98
|
+
sandbox.spec.runtime.image = sandbox.spec.runtime.image || defaultImage;
|
|
99
|
+
sandbox.spec.runtime.memory = sandbox.spec.runtime.memory || defaultMemory;
|
|
100
|
+
sandbox.spec.runtime.generation = sandbox.spec.runtime.generation || "mk3";
|
|
101
|
+
const { data } = await (0, index_js_1.createSandbox)({
|
|
102
|
+
body: sandbox,
|
|
103
|
+
throwOnError: true,
|
|
104
|
+
});
|
|
105
|
+
const instance = new SandboxInstance(data);
|
|
106
|
+
// TODO remove this part once we have a better way to handle this
|
|
107
|
+
if (safe) {
|
|
108
|
+
try {
|
|
109
|
+
await instance.fs.ls('/');
|
|
110
|
+
}
|
|
111
|
+
catch { }
|
|
112
|
+
}
|
|
113
|
+
return instance;
|
|
114
|
+
}
|
|
115
|
+
static async get(sandboxName) {
|
|
116
|
+
const { data } = await (0, index_js_1.getSandbox)({
|
|
117
|
+
path: {
|
|
118
|
+
sandboxName,
|
|
119
|
+
},
|
|
120
|
+
throwOnError: true,
|
|
121
|
+
});
|
|
122
|
+
return new SandboxInstance(data);
|
|
123
|
+
}
|
|
124
|
+
static async list() {
|
|
125
|
+
const { data } = await (0, index_js_1.listSandboxes)({ throwOnError: true });
|
|
126
|
+
return data.map((sandbox) => new SandboxInstance(sandbox));
|
|
127
|
+
}
|
|
128
|
+
static async delete(sandboxName) {
|
|
129
|
+
const { data } = await (0, index_js_1.deleteSandbox)({
|
|
130
|
+
path: {
|
|
131
|
+
sandboxName,
|
|
132
|
+
},
|
|
133
|
+
throwOnError: true,
|
|
134
|
+
});
|
|
135
|
+
return data;
|
|
136
|
+
}
|
|
137
|
+
static async updateMetadata(sandboxName, metadata) {
|
|
138
|
+
const sandbox = await SandboxInstance.get(sandboxName);
|
|
139
|
+
const body = { ...sandbox.sandbox, metadata: { ...sandbox.metadata, ...metadata } };
|
|
140
|
+
const { data } = await (0, index_js_1.updateSandbox)({
|
|
141
|
+
path: { sandboxName },
|
|
142
|
+
body,
|
|
143
|
+
throwOnError: true,
|
|
144
|
+
});
|
|
145
|
+
const instance = new SandboxInstance(data);
|
|
146
|
+
return instance;
|
|
147
|
+
}
|
|
148
|
+
static async createIfNotExists(sandbox) {
|
|
149
|
+
try {
|
|
150
|
+
return await SandboxInstance.create(sandbox);
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
if (typeof e === "object" && e !== null && "code" in e && (e.code === 409 || e.code === 'SANDBOX_ALREADY_EXISTS')) {
|
|
154
|
+
const name = 'name' in sandbox ? sandbox.name : sandbox.metadata?.name;
|
|
155
|
+
if (!name) {
|
|
156
|
+
throw new Error("Sandbox name is required");
|
|
157
|
+
}
|
|
158
|
+
const sandboxInstance = await SandboxInstance.get(name);
|
|
159
|
+
return sandboxInstance;
|
|
160
|
+
}
|
|
161
|
+
throw e;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/* eslint-disable */
|
|
165
|
+
static async fromSession(session) {
|
|
166
|
+
// Create a minimal sandbox configuration for session-based access
|
|
167
|
+
const sandboxName = session.name.includes("-") ? session.name.split("-")[0] : session.name;
|
|
168
|
+
const sandbox = {
|
|
169
|
+
metadata: { name: sandboxName },
|
|
170
|
+
forceUrl: session.url,
|
|
171
|
+
headers: { "X-Blaxel-Preview-Token": session.token },
|
|
172
|
+
params: { bl_preview_token: session.token }
|
|
173
|
+
};
|
|
174
|
+
// Create instance using constructor instead of direct property assignment
|
|
175
|
+
return new SandboxInstance(sandbox);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
exports.SandboxInstance = SandboxInstance;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Sandbox } from "../client/index.js";
|
|
2
|
+
import { SessionCreateOptions, SessionWithToken } from "./types.js";
|
|
3
|
+
export declare class SandboxSessions {
|
|
4
|
+
private sandbox;
|
|
5
|
+
constructor(sandbox: Sandbox);
|
|
6
|
+
get sandboxName(): string;
|
|
7
|
+
create(options?: SessionCreateOptions): Promise<SessionWithToken>;
|
|
8
|
+
createIfExpired(options?: SessionCreateOptions, delta?: number): Promise<{
|
|
9
|
+
name: string;
|
|
10
|
+
url: string;
|
|
11
|
+
token: string;
|
|
12
|
+
expiresAt: string | Date;
|
|
13
|
+
}>;
|
|
14
|
+
list(): Promise<{
|
|
15
|
+
name: string;
|
|
16
|
+
url: string;
|
|
17
|
+
token: string;
|
|
18
|
+
expiresAt: string | Date;
|
|
19
|
+
}[]>;
|
|
20
|
+
get(name: string): Promise<{
|
|
21
|
+
url: string;
|
|
22
|
+
token: string;
|
|
23
|
+
expiresAt: string | Date;
|
|
24
|
+
}>;
|
|
25
|
+
delete(name: string): Promise<import("../client/types.gen.js").Preview>;
|
|
26
|
+
getToken(previewName: string): Promise<import("../client/types.gen.js").PreviewToken | null>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SandboxSessions = void 0;
|
|
4
|
+
const index_js_1 = require("../client/index.js");
|
|
5
|
+
const preview_js_1 = require("./preview.js");
|
|
6
|
+
class SandboxSessions {
|
|
7
|
+
sandbox;
|
|
8
|
+
constructor(sandbox) {
|
|
9
|
+
this.sandbox = sandbox;
|
|
10
|
+
}
|
|
11
|
+
get sandboxName() {
|
|
12
|
+
return this.sandbox.metadata?.name ?? "";
|
|
13
|
+
}
|
|
14
|
+
async create(options = {}) {
|
|
15
|
+
const expiresAt = options.expiresAt ?? new Date(Date.now() + 24 * 60 * 60 * 1000); // 1 day from now
|
|
16
|
+
const body = {
|
|
17
|
+
metadata: {
|
|
18
|
+
name: "session-" + Date.now(),
|
|
19
|
+
},
|
|
20
|
+
spec: {
|
|
21
|
+
port: 443,
|
|
22
|
+
public: false,
|
|
23
|
+
expires: expiresAt.toISOString(),
|
|
24
|
+
requestHeaders: options.requestHeaders,
|
|
25
|
+
responseHeaders: options.responseHeaders,
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const { data } = await (0, index_js_1.createSandboxPreview)({
|
|
29
|
+
path: {
|
|
30
|
+
sandboxName: this.sandboxName,
|
|
31
|
+
},
|
|
32
|
+
body,
|
|
33
|
+
throwOnError: true,
|
|
34
|
+
});
|
|
35
|
+
const preview = new preview_js_1.SandboxPreview(data);
|
|
36
|
+
// Create a token for the preview with the given expiresAt
|
|
37
|
+
const tokenObj = await preview.tokens.create(expiresAt);
|
|
38
|
+
return {
|
|
39
|
+
name: body.metadata.name,
|
|
40
|
+
url: preview.spec?.url ?? "",
|
|
41
|
+
token: tokenObj.value,
|
|
42
|
+
expiresAt: typeof tokenObj.expiresAt === 'string' ? new Date(tokenObj.expiresAt) : tokenObj.expiresAt,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
async createIfExpired(options = {}, delta = 1000 * 60 * 60) {
|
|
46
|
+
// First, list all sessions
|
|
47
|
+
const allSessions = await this.list();
|
|
48
|
+
// Variable to hold our final session
|
|
49
|
+
let sessionData;
|
|
50
|
+
const now = new Date();
|
|
51
|
+
const threshold = new Date(now.getTime() + delta);
|
|
52
|
+
// If no valid session exists, create a new one
|
|
53
|
+
if (allSessions.length > 0) {
|
|
54
|
+
sessionData = allSessions[0];
|
|
55
|
+
if (new Date(sessionData.expiresAt) < threshold) {
|
|
56
|
+
await this.delete(sessionData.name);
|
|
57
|
+
sessionData = await this.create(options);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
// Create a new session
|
|
62
|
+
sessionData = await this.create(options);
|
|
63
|
+
}
|
|
64
|
+
return sessionData;
|
|
65
|
+
}
|
|
66
|
+
async list() {
|
|
67
|
+
const { data } = await (0, index_js_1.listSandboxPreviews)({
|
|
68
|
+
path: {
|
|
69
|
+
sandboxName: this.sandboxName,
|
|
70
|
+
},
|
|
71
|
+
throwOnError: true,
|
|
72
|
+
});
|
|
73
|
+
if (data === null)
|
|
74
|
+
return [];
|
|
75
|
+
return await Promise.all(data.filter((preview) => preview.metadata?.name?.includes("session-")).map(async (preview) => {
|
|
76
|
+
const token = await this.getToken(preview.metadata?.name ?? "");
|
|
77
|
+
return {
|
|
78
|
+
name: preview.metadata?.name ?? "",
|
|
79
|
+
url: preview.spec?.url ?? "",
|
|
80
|
+
token: token?.spec?.token ?? "",
|
|
81
|
+
expiresAt: token?.spec?.expiresAt ?? new Date(),
|
|
82
|
+
};
|
|
83
|
+
}));
|
|
84
|
+
}
|
|
85
|
+
async get(name) {
|
|
86
|
+
const { data } = await (0, index_js_1.getSandboxPreview)({
|
|
87
|
+
path: {
|
|
88
|
+
sandboxName: this.sandboxName,
|
|
89
|
+
previewName: name,
|
|
90
|
+
},
|
|
91
|
+
throwOnError: true,
|
|
92
|
+
});
|
|
93
|
+
const token = await this.getToken(name);
|
|
94
|
+
return {
|
|
95
|
+
url: data.spec?.url ?? "",
|
|
96
|
+
token: token?.spec?.token ?? "",
|
|
97
|
+
expiresAt: token?.spec?.expiresAt ?? new Date(),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
async delete(name) {
|
|
101
|
+
const { data } = await (0, index_js_1.deleteSandboxPreview)({
|
|
102
|
+
path: {
|
|
103
|
+
sandboxName: this.sandboxName,
|
|
104
|
+
previewName: name,
|
|
105
|
+
},
|
|
106
|
+
throwOnError: true,
|
|
107
|
+
});
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
async getToken(previewName) {
|
|
111
|
+
const { data } = await (0, index_js_1.listSandboxPreviewTokens)({
|
|
112
|
+
path: {
|
|
113
|
+
sandboxName: this.sandboxName,
|
|
114
|
+
previewName,
|
|
115
|
+
},
|
|
116
|
+
throwOnError: true,
|
|
117
|
+
});
|
|
118
|
+
if (data.length === 0)
|
|
119
|
+
return null;
|
|
120
|
+
return data[0];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
exports.SandboxSessions = SandboxSessions;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Port, Sandbox, VolumeAttachment } from "../client/types.gen";
|
|
2
|
+
import { PostProcessResponse, ProcessRequest } from "./client";
|
|
3
|
+
export interface SessionCreateOptions {
|
|
4
|
+
expiresAt?: Date;
|
|
5
|
+
responseHeaders?: Record<string, string>;
|
|
6
|
+
requestHeaders?: Record<string, string>;
|
|
7
|
+
}
|
|
8
|
+
export interface SessionWithToken {
|
|
9
|
+
name: string;
|
|
10
|
+
url: string;
|
|
11
|
+
token: string;
|
|
12
|
+
expiresAt: Date;
|
|
13
|
+
}
|
|
14
|
+
export interface EnvVar {
|
|
15
|
+
name: string;
|
|
16
|
+
value: string;
|
|
17
|
+
}
|
|
18
|
+
export interface VolumeBinding {
|
|
19
|
+
name: string;
|
|
20
|
+
mountPath: string;
|
|
21
|
+
readOnly?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export type SandboxConfiguration = {
|
|
24
|
+
forceUrl?: string;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
params?: Record<string, string>;
|
|
27
|
+
} & Sandbox;
|
|
28
|
+
export type SandboxUpdateMetadata = {
|
|
29
|
+
labels?: Record<string, string>;
|
|
30
|
+
displayName?: string;
|
|
31
|
+
};
|
|
32
|
+
export type SandboxCreateConfiguration = {
|
|
33
|
+
name?: string;
|
|
34
|
+
image?: string;
|
|
35
|
+
memory?: number;
|
|
36
|
+
ports?: (Port | Record<string, any>)[];
|
|
37
|
+
envs?: EnvVar[];
|
|
38
|
+
volumes?: (VolumeBinding | VolumeAttachment)[];
|
|
39
|
+
ttl?: string;
|
|
40
|
+
expires?: Date;
|
|
41
|
+
region?: string;
|
|
42
|
+
};
|
|
43
|
+
export declare function normalizePorts(ports?: (Port | Record<string, any>)[]): Port[] | undefined;
|
|
44
|
+
export declare function normalizeEnvs(envs?: EnvVar[]): EnvVar[] | undefined;
|
|
45
|
+
export declare function normalizeVolumes(volumes?: (VolumeBinding | VolumeAttachment)[]): VolumeAttachment[] | undefined;
|
|
46
|
+
export type ProcessRequestWithLog = ProcessRequest & {
|
|
47
|
+
onLog?: (log: string) => void;
|
|
48
|
+
};
|
|
49
|
+
export type ProcessResponseWithLog = PostProcessResponse & {
|
|
50
|
+
close: () => void;
|
|
51
|
+
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizePorts = normalizePorts;
|
|
4
|
+
exports.normalizeEnvs = normalizeEnvs;
|
|
5
|
+
exports.normalizeVolumes = normalizeVolumes;
|
|
6
|
+
function normalizePorts(ports) {
|
|
7
|
+
if (!ports || ports.length === 0) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const portObjects = [];
|
|
11
|
+
for (const port of ports) {
|
|
12
|
+
if (typeof port === 'object' && port !== null) {
|
|
13
|
+
if ('name' in port || 'target' in port || 'protocol' in port) {
|
|
14
|
+
// It's a Port-like object, ensure protocol defaults to HTTP
|
|
15
|
+
const normalizedPort = {
|
|
16
|
+
name: typeof port.name === 'string' ? port.name : undefined,
|
|
17
|
+
target: typeof port.target === 'number' ? port.target : undefined,
|
|
18
|
+
protocol: typeof port.protocol === 'string' ? port.protocol : "HTTP"
|
|
19
|
+
};
|
|
20
|
+
portObjects.push(normalizedPort);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
throw new Error(`Invalid port type: ${typeof port}. Expected Port object or object with port properties.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
throw new Error(`Invalid port type: ${typeof port}. Expected Port object or object with port properties.`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return portObjects;
|
|
31
|
+
}
|
|
32
|
+
function normalizeEnvs(envs) {
|
|
33
|
+
if (!envs || envs.length === 0) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const envObjects = [];
|
|
37
|
+
for (const env of envs) {
|
|
38
|
+
if (typeof env === 'object' && env !== null) {
|
|
39
|
+
// Validate that the object has the required keys
|
|
40
|
+
if (!('name' in env) || !('value' in env)) {
|
|
41
|
+
throw new Error(`Environment variable object must have 'name' and 'value' keys: ${JSON.stringify(env)}`);
|
|
42
|
+
}
|
|
43
|
+
if (typeof env.name !== 'string' || typeof env.value !== 'string') {
|
|
44
|
+
throw new Error(`Environment variable 'name' and 'value' must be strings: ${JSON.stringify(env)}`);
|
|
45
|
+
}
|
|
46
|
+
envObjects.push({ name: env.name, value: env.value });
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error(`Invalid env type: ${typeof env}. Expected object with 'name' and 'value' keys.`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return envObjects;
|
|
53
|
+
}
|
|
54
|
+
function normalizeVolumes(volumes) {
|
|
55
|
+
if (!volumes || volumes.length === 0) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
const volumeObjects = [];
|
|
59
|
+
for (const volume of volumes) {
|
|
60
|
+
if (typeof volume === 'object' && volume !== null) {
|
|
61
|
+
// Validate that the object has the required keys
|
|
62
|
+
if (!('name' in volume) || !('mountPath' in volume)) {
|
|
63
|
+
throw new Error(`Volume binding object must have 'name' and 'mountPath' keys: ${JSON.stringify(volume)}`);
|
|
64
|
+
}
|
|
65
|
+
if (typeof volume.name !== 'string' || typeof volume.mountPath !== 'string') {
|
|
66
|
+
throw new Error(`Volume binding 'name' and 'mountPath' must be strings: ${JSON.stringify(volume)}`);
|
|
67
|
+
}
|
|
68
|
+
// Convert VolumeBinding to VolumeAttachment format
|
|
69
|
+
const volumeAttachment = {
|
|
70
|
+
name: volume.name,
|
|
71
|
+
mountPath: volume.mountPath,
|
|
72
|
+
readOnly: 'readOnly' in volume ? volume.readOnly : false
|
|
73
|
+
};
|
|
74
|
+
volumeObjects.push(volumeAttachment);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
throw new Error(`Invalid volume type: ${typeof volume}. Expected object with 'name' and 'mountPath' keys.`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return volumeObjects;
|
|
81
|
+
}
|