@cloudflare/sandbox 0.0.9 ā 0.1.1
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 +18 -0
- package/Dockerfile +1 -14
- package/container_src/handler/exec.ts +337 -0
- package/container_src/handler/file.ts +844 -0
- package/container_src/handler/git.ts +182 -0
- package/container_src/handler/ports.ts +314 -0
- package/container_src/handler/process.ts +640 -0
- package/container_src/index.ts +82 -2973
- package/container_src/types.ts +103 -0
- package/dist/chunk-6THNBO4S.js +46 -0
- package/dist/chunk-6THNBO4S.js.map +1 -0
- package/dist/chunk-6UAWTJ5S.js +85 -0
- package/dist/chunk-6UAWTJ5S.js.map +1 -0
- package/dist/chunk-G4XT4SP7.js +638 -0
- package/dist/chunk-G4XT4SP7.js.map +1 -0
- package/dist/chunk-ISFOIYQC.js +585 -0
- package/dist/chunk-ISFOIYQC.js.map +1 -0
- package/dist/chunk-NNGBXDMY.js +89 -0
- package/dist/chunk-NNGBXDMY.js.map +1 -0
- package/dist/client-Da-mLX4p.d.ts +210 -0
- package/dist/client.d.ts +2 -1
- package/dist/client.js +3 -37
- package/dist/index.d.ts +3 -1
- package/dist/index.js +13 -3
- package/dist/request-handler.d.ts +2 -1
- package/dist/request-handler.js +4 -2
- package/dist/sandbox.d.ts +2 -1
- package/dist/sandbox.js +4 -2
- package/dist/security.d.ts +30 -0
- package/dist/security.js +13 -0
- package/dist/security.js.map +1 -0
- package/dist/sse-parser.d.ts +28 -0
- package/dist/sse-parser.js +11 -0
- package/dist/sse-parser.js.map +1 -0
- package/dist/types.d.ts +284 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +2 -7
- package/src/client.ts +235 -1286
- package/src/index.ts +6 -0
- package/src/request-handler.ts +69 -20
- package/src/sandbox.ts +463 -70
- package/src/security.ts +113 -0
- package/src/sse-parser.ts +147 -0
- package/src/types.ts +386 -0
- package/tsconfig.json +1 -1
- package/README.md +0 -65
- package/dist/chunk-4J5LQCCN.js +0 -1446
- package/dist/chunk-4J5LQCCN.js.map +0 -1
- package/dist/chunk-5SZ3RVJZ.js +0 -250
- package/dist/chunk-5SZ3RVJZ.js.map +0 -1
- package/dist/client-BuVjqV00.d.ts +0 -247
- package/tests/client.example.ts +0 -308
- package/tests/connection-test.ts +0 -81
- package/tests/simple-test.ts +0 -81
- package/tests/test1.ts +0 -281
- package/tests/test2.ts +0 -929
|
@@ -1,247 +0,0 @@
|
|
|
1
|
-
import { Container } from '@cloudflare/containers';
|
|
2
|
-
|
|
3
|
-
declare function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string): DurableObjectStub<Sandbox<unknown>>;
|
|
4
|
-
declare class Sandbox<Env = unknown> extends Container<Env> {
|
|
5
|
-
sleepAfter: string;
|
|
6
|
-
client: HttpClient;
|
|
7
|
-
private workerHostname;
|
|
8
|
-
private sandboxName;
|
|
9
|
-
constructor(ctx: DurableObjectState, env: Env);
|
|
10
|
-
setSandboxName(name: string): Promise<void>;
|
|
11
|
-
onStart(): void;
|
|
12
|
-
onStop(): void;
|
|
13
|
-
onError(error: unknown): void;
|
|
14
|
-
fetch(request: Request): Promise<Response>;
|
|
15
|
-
private determinePort;
|
|
16
|
-
exec(command: string, args: string[], options?: {
|
|
17
|
-
stream?: boolean;
|
|
18
|
-
background?: boolean;
|
|
19
|
-
}): Promise<void | ExecuteResponse>;
|
|
20
|
-
gitCheckout(repoUrl: string, options: {
|
|
21
|
-
branch?: string;
|
|
22
|
-
targetDir?: string;
|
|
23
|
-
stream?: boolean;
|
|
24
|
-
}): Promise<void | GitCheckoutResponse>;
|
|
25
|
-
mkdir(path: string, options?: {
|
|
26
|
-
recursive?: boolean;
|
|
27
|
-
stream?: boolean;
|
|
28
|
-
}): Promise<void | MkdirResponse>;
|
|
29
|
-
writeFile(path: string, content: string, options?: {
|
|
30
|
-
encoding?: string;
|
|
31
|
-
stream?: boolean;
|
|
32
|
-
}): Promise<void | WriteFileResponse>;
|
|
33
|
-
deleteFile(path: string, options?: {
|
|
34
|
-
stream?: boolean;
|
|
35
|
-
}): Promise<void | DeleteFileResponse>;
|
|
36
|
-
renameFile(oldPath: string, newPath: string, options?: {
|
|
37
|
-
stream?: boolean;
|
|
38
|
-
}): Promise<void | RenameFileResponse>;
|
|
39
|
-
moveFile(sourcePath: string, destinationPath: string, options?: {
|
|
40
|
-
stream?: boolean;
|
|
41
|
-
}): Promise<void | MoveFileResponse>;
|
|
42
|
-
readFile(path: string, options?: {
|
|
43
|
-
encoding?: string;
|
|
44
|
-
stream?: boolean;
|
|
45
|
-
}): Promise<void | ReadFileResponse>;
|
|
46
|
-
exposePort(port: number, options?: {
|
|
47
|
-
name?: string;
|
|
48
|
-
}): Promise<{
|
|
49
|
-
url: string;
|
|
50
|
-
port: number;
|
|
51
|
-
name: string | undefined;
|
|
52
|
-
}>;
|
|
53
|
-
unexposePort(port: number): Promise<void>;
|
|
54
|
-
getExposedPorts(): Promise<{
|
|
55
|
-
url: string;
|
|
56
|
-
port: number;
|
|
57
|
-
name: string | undefined;
|
|
58
|
-
exposedAt: string;
|
|
59
|
-
}[]>;
|
|
60
|
-
private getHostname;
|
|
61
|
-
private constructPreviewUrl;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
interface ExecuteResponse {
|
|
65
|
-
success: boolean;
|
|
66
|
-
stdout: string;
|
|
67
|
-
stderr: string;
|
|
68
|
-
exitCode: number;
|
|
69
|
-
command: string;
|
|
70
|
-
args: string[];
|
|
71
|
-
timestamp: string;
|
|
72
|
-
}
|
|
73
|
-
interface SessionListResponse {
|
|
74
|
-
sessions: Array<{
|
|
75
|
-
sessionId: string;
|
|
76
|
-
hasActiveProcess: boolean;
|
|
77
|
-
createdAt: string;
|
|
78
|
-
}>;
|
|
79
|
-
count: number;
|
|
80
|
-
timestamp: string;
|
|
81
|
-
}
|
|
82
|
-
interface GitCheckoutResponse {
|
|
83
|
-
success: boolean;
|
|
84
|
-
stdout: string;
|
|
85
|
-
stderr: string;
|
|
86
|
-
exitCode: number;
|
|
87
|
-
repoUrl: string;
|
|
88
|
-
branch: string;
|
|
89
|
-
targetDir: string;
|
|
90
|
-
timestamp: string;
|
|
91
|
-
}
|
|
92
|
-
interface MkdirResponse {
|
|
93
|
-
success: boolean;
|
|
94
|
-
stdout: string;
|
|
95
|
-
stderr: string;
|
|
96
|
-
exitCode: number;
|
|
97
|
-
path: string;
|
|
98
|
-
recursive: boolean;
|
|
99
|
-
timestamp: string;
|
|
100
|
-
}
|
|
101
|
-
interface WriteFileResponse {
|
|
102
|
-
success: boolean;
|
|
103
|
-
exitCode: number;
|
|
104
|
-
path: string;
|
|
105
|
-
timestamp: string;
|
|
106
|
-
}
|
|
107
|
-
interface ReadFileResponse {
|
|
108
|
-
success: boolean;
|
|
109
|
-
exitCode: number;
|
|
110
|
-
path: string;
|
|
111
|
-
content: string;
|
|
112
|
-
timestamp: string;
|
|
113
|
-
}
|
|
114
|
-
interface DeleteFileResponse {
|
|
115
|
-
success: boolean;
|
|
116
|
-
exitCode: number;
|
|
117
|
-
path: string;
|
|
118
|
-
timestamp: string;
|
|
119
|
-
}
|
|
120
|
-
interface RenameFileResponse {
|
|
121
|
-
success: boolean;
|
|
122
|
-
exitCode: number;
|
|
123
|
-
oldPath: string;
|
|
124
|
-
newPath: string;
|
|
125
|
-
timestamp: string;
|
|
126
|
-
}
|
|
127
|
-
interface MoveFileResponse {
|
|
128
|
-
success: boolean;
|
|
129
|
-
exitCode: number;
|
|
130
|
-
sourcePath: string;
|
|
131
|
-
destinationPath: string;
|
|
132
|
-
timestamp: string;
|
|
133
|
-
}
|
|
134
|
-
interface PreviewInfo {
|
|
135
|
-
url: string;
|
|
136
|
-
port: number;
|
|
137
|
-
name?: string;
|
|
138
|
-
}
|
|
139
|
-
interface ExposedPort extends PreviewInfo {
|
|
140
|
-
exposedAt: string;
|
|
141
|
-
timestamp: string;
|
|
142
|
-
}
|
|
143
|
-
interface ExposePortResponse {
|
|
144
|
-
success: boolean;
|
|
145
|
-
port: number;
|
|
146
|
-
name?: string;
|
|
147
|
-
exposedAt: string;
|
|
148
|
-
timestamp: string;
|
|
149
|
-
}
|
|
150
|
-
interface UnexposePortResponse {
|
|
151
|
-
success: boolean;
|
|
152
|
-
port: number;
|
|
153
|
-
timestamp: string;
|
|
154
|
-
}
|
|
155
|
-
interface GetExposedPortsResponse {
|
|
156
|
-
ports: ExposedPort[];
|
|
157
|
-
count: number;
|
|
158
|
-
timestamp: string;
|
|
159
|
-
}
|
|
160
|
-
interface StreamEvent {
|
|
161
|
-
type: "command_start" | "output" | "command_complete" | "error";
|
|
162
|
-
command?: string;
|
|
163
|
-
args?: string[];
|
|
164
|
-
stream?: "stdout" | "stderr";
|
|
165
|
-
data?: string;
|
|
166
|
-
message?: string;
|
|
167
|
-
path?: string;
|
|
168
|
-
oldPath?: string;
|
|
169
|
-
newPath?: string;
|
|
170
|
-
sourcePath?: string;
|
|
171
|
-
destinationPath?: string;
|
|
172
|
-
content?: string;
|
|
173
|
-
success?: boolean;
|
|
174
|
-
exitCode?: number;
|
|
175
|
-
stdout?: string;
|
|
176
|
-
stderr?: string;
|
|
177
|
-
error?: string;
|
|
178
|
-
timestamp?: string;
|
|
179
|
-
}
|
|
180
|
-
interface HttpClientOptions {
|
|
181
|
-
stub?: Sandbox;
|
|
182
|
-
baseUrl?: string;
|
|
183
|
-
port?: number;
|
|
184
|
-
onCommandStart?: (command: string, args: string[]) => void;
|
|
185
|
-
onOutput?: (stream: "stdout" | "stderr", data: string, command: string) => void;
|
|
186
|
-
onCommandComplete?: (success: boolean, exitCode: number, stdout: string, stderr: string, command: string, args: string[]) => void;
|
|
187
|
-
onError?: (error: string, command?: string, args?: string[]) => void;
|
|
188
|
-
onStreamEvent?: (event: StreamEvent) => void;
|
|
189
|
-
}
|
|
190
|
-
declare class HttpClient {
|
|
191
|
-
private baseUrl;
|
|
192
|
-
private options;
|
|
193
|
-
private sessionId;
|
|
194
|
-
constructor(options?: HttpClientOptions);
|
|
195
|
-
private doFetch;
|
|
196
|
-
setOnOutput(handler: (stream: "stdout" | "stderr", data: string, command: string) => void): void;
|
|
197
|
-
setOnCommandComplete(handler: (success: boolean, exitCode: number, stdout: string, stderr: string, command: string, args: string[]) => void): void;
|
|
198
|
-
setOnStreamEvent(handler: (event: StreamEvent) => void): void;
|
|
199
|
-
getOnOutput(): ((stream: "stdout" | "stderr", data: string, command: string) => void) | undefined;
|
|
200
|
-
getOnCommandComplete(): ((success: boolean, exitCode: number, stdout: string, stderr: string, command: string, args: string[]) => void) | undefined;
|
|
201
|
-
getOnStreamEvent(): ((event: StreamEvent) => void) | undefined;
|
|
202
|
-
createSession(): Promise<string>;
|
|
203
|
-
listSessions(): Promise<SessionListResponse>;
|
|
204
|
-
execute(command: string, args?: string[], sessionId?: string, background?: boolean): Promise<ExecuteResponse>;
|
|
205
|
-
executeStream(command: string, args?: string[], sessionId?: string, background?: boolean): Promise<void>;
|
|
206
|
-
gitCheckout(repoUrl: string, branch?: string, targetDir?: string, sessionId?: string): Promise<GitCheckoutResponse>;
|
|
207
|
-
gitCheckoutStream(repoUrl: string, branch?: string, targetDir?: string, sessionId?: string): Promise<void>;
|
|
208
|
-
mkdir(path: string, recursive?: boolean, sessionId?: string): Promise<MkdirResponse>;
|
|
209
|
-
mkdirStream(path: string, recursive?: boolean, sessionId?: string): Promise<void>;
|
|
210
|
-
writeFile(path: string, content: string, encoding?: string, sessionId?: string): Promise<WriteFileResponse>;
|
|
211
|
-
writeFileStream(path: string, content: string, encoding?: string, sessionId?: string): Promise<void>;
|
|
212
|
-
readFile(path: string, encoding?: string, sessionId?: string): Promise<ReadFileResponse>;
|
|
213
|
-
readFileStream(path: string, encoding?: string, sessionId?: string): Promise<void>;
|
|
214
|
-
deleteFile(path: string, sessionId?: string): Promise<DeleteFileResponse>;
|
|
215
|
-
deleteFileStream(path: string, sessionId?: string): Promise<void>;
|
|
216
|
-
renameFile(oldPath: string, newPath: string, sessionId?: string): Promise<RenameFileResponse>;
|
|
217
|
-
renameFileStream(oldPath: string, newPath: string, sessionId?: string): Promise<void>;
|
|
218
|
-
moveFile(sourcePath: string, destinationPath: string, sessionId?: string): Promise<MoveFileResponse>;
|
|
219
|
-
moveFileStream(sourcePath: string, destinationPath: string, sessionId?: string): Promise<void>;
|
|
220
|
-
exposePort(port: number, name?: string): Promise<ExposePortResponse>;
|
|
221
|
-
unexposePort(port: number): Promise<UnexposePortResponse>;
|
|
222
|
-
getExposedPorts(): Promise<GetExposedPortsResponse>;
|
|
223
|
-
ping(): Promise<string>;
|
|
224
|
-
getCommands(): Promise<string[]>;
|
|
225
|
-
getSessionId(): string | null;
|
|
226
|
-
setSessionId(sessionId: string): void;
|
|
227
|
-
clearSession(): void;
|
|
228
|
-
}
|
|
229
|
-
declare function createClient(options?: HttpClientOptions): HttpClient;
|
|
230
|
-
declare function quickExecute(command: string, args?: string[], options?: HttpClientOptions): Promise<ExecuteResponse>;
|
|
231
|
-
declare function quickExecuteStream(command: string, args?: string[], options?: HttpClientOptions): Promise<void>;
|
|
232
|
-
declare function quickGitCheckout(repoUrl: string, branch?: string, targetDir?: string, options?: HttpClientOptions): Promise<GitCheckoutResponse>;
|
|
233
|
-
declare function quickMkdir(path: string, recursive?: boolean, options?: HttpClientOptions): Promise<MkdirResponse>;
|
|
234
|
-
declare function quickGitCheckoutStream(repoUrl: string, branch?: string, targetDir?: string, options?: HttpClientOptions): Promise<void>;
|
|
235
|
-
declare function quickMkdirStream(path: string, recursive?: boolean, options?: HttpClientOptions): Promise<void>;
|
|
236
|
-
declare function quickWriteFile(path: string, content: string, encoding?: string, options?: HttpClientOptions): Promise<WriteFileResponse>;
|
|
237
|
-
declare function quickWriteFileStream(path: string, content: string, encoding?: string, options?: HttpClientOptions): Promise<void>;
|
|
238
|
-
declare function quickReadFile(path: string, encoding?: string, options?: HttpClientOptions): Promise<ReadFileResponse>;
|
|
239
|
-
declare function quickReadFileStream(path: string, encoding?: string, options?: HttpClientOptions): Promise<void>;
|
|
240
|
-
declare function quickDeleteFile(path: string, options?: HttpClientOptions): Promise<DeleteFileResponse>;
|
|
241
|
-
declare function quickDeleteFileStream(path: string, options?: HttpClientOptions): Promise<void>;
|
|
242
|
-
declare function quickRenameFile(oldPath: string, newPath: string, options?: HttpClientOptions): Promise<RenameFileResponse>;
|
|
243
|
-
declare function quickRenameFileStream(oldPath: string, newPath: string, options?: HttpClientOptions): Promise<void>;
|
|
244
|
-
declare function quickMoveFile(sourcePath: string, destinationPath: string, options?: HttpClientOptions): Promise<MoveFileResponse>;
|
|
245
|
-
declare function quickMoveFileStream(sourcePath: string, destinationPath: string, options?: HttpClientOptions): Promise<void>;
|
|
246
|
-
|
|
247
|
-
export { type DeleteFileResponse as D, type ExecuteResponse as E, type GitCheckoutResponse as G, HttpClient as H, type MkdirResponse as M, type ReadFileResponse as R, Sandbox as S, type WriteFileResponse as W, type MoveFileResponse as a, type RenameFileResponse as b, createClient as c, quickExecuteStream as d, quickGitCheckout as e, quickMkdir as f, getSandbox as g, quickGitCheckoutStream as h, quickMkdirStream as i, quickWriteFile as j, quickWriteFileStream as k, quickReadFile as l, quickReadFileStream as m, quickDeleteFile as n, quickDeleteFileStream as o, quickRenameFile as p, quickExecute as q, quickRenameFileStream as r, quickMoveFile as s, quickMoveFileStream as t };
|
package/tests/client.example.ts
DELETED
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createClient,
|
|
3
|
-
quickExecute,
|
|
4
|
-
quickExecuteStream,
|
|
5
|
-
quickGitCheckout,
|
|
6
|
-
quickGitCheckoutStream,
|
|
7
|
-
quickMkdir,
|
|
8
|
-
quickMkdirStream,
|
|
9
|
-
} from "../../sandbox/src/client";
|
|
10
|
-
|
|
11
|
-
// Example 1: Basic client usage
|
|
12
|
-
async function basicExample() {
|
|
13
|
-
console.log("=== Basic Client Example ===");
|
|
14
|
-
|
|
15
|
-
const client = createClient({
|
|
16
|
-
baseUrl: "http://localhost:3000",
|
|
17
|
-
onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
|
|
18
|
-
console.log(
|
|
19
|
-
`Command completed: ${command}, success=${success}, exitCode=${exitCode}`
|
|
20
|
-
);
|
|
21
|
-
if (stderr) console.log(`Stderr: ${stderr}`);
|
|
22
|
-
},
|
|
23
|
-
onCommandStart: (command, args) => {
|
|
24
|
-
console.log(`Starting command: ${command} ${args.join(" ")}`);
|
|
25
|
-
},
|
|
26
|
-
onError: (error, command, args) => {
|
|
27
|
-
console.error(`Error in ${command}: ${error}`);
|
|
28
|
-
},
|
|
29
|
-
onOutput: (stream, data, command) => {
|
|
30
|
-
console.log(`[${stream}] ${data}`);
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
// Create a session
|
|
36
|
-
const sessionId = await client.createSession();
|
|
37
|
-
console.log(`Created session: ${sessionId}`);
|
|
38
|
-
|
|
39
|
-
// Execute some commands
|
|
40
|
-
const lsResult = await client.execute("ls", ["-la"]);
|
|
41
|
-
console.log("LS result:", lsResult.stdout);
|
|
42
|
-
|
|
43
|
-
const pwdResult = await client.ping();
|
|
44
|
-
console.log("Ping result:", pwdResult);
|
|
45
|
-
|
|
46
|
-
const commands = await client.getCommands();
|
|
47
|
-
console.log("Available commands:", commands.slice(0, 5));
|
|
48
|
-
|
|
49
|
-
// List sessions
|
|
50
|
-
const sessions = await client.listSessions();
|
|
51
|
-
console.log(`Active sessions: ${sessions.count}`);
|
|
52
|
-
} catch (error) {
|
|
53
|
-
console.error("Operation failed:", error);
|
|
54
|
-
} finally {
|
|
55
|
-
client.clearSession();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Example 2: Streaming command execution
|
|
60
|
-
async function streamingExample() {
|
|
61
|
-
console.log("\n=== Streaming Command Example ===");
|
|
62
|
-
|
|
63
|
-
const client = createClient({
|
|
64
|
-
baseUrl: "http://localhost:3000",
|
|
65
|
-
onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
|
|
66
|
-
console.log(
|
|
67
|
-
`\nā
Completed: ${command}, success=${success}, exitCode=${exitCode}`
|
|
68
|
-
);
|
|
69
|
-
},
|
|
70
|
-
onCommandStart: (command, args) => {
|
|
71
|
-
console.log(`š Starting: ${command} ${args.join(" ")}`);
|
|
72
|
-
},
|
|
73
|
-
onOutput: (stream, data, command) => {
|
|
74
|
-
process.stdout.write(data);
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
try {
|
|
79
|
-
await client.createSession();
|
|
80
|
-
|
|
81
|
-
// Execute a long-running command with streaming
|
|
82
|
-
console.log("Executing 'find . -name '*.ts' -type f' with streaming...");
|
|
83
|
-
await client.executeStream("find", [".", "-name", "*.ts", "-type", "f"]);
|
|
84
|
-
|
|
85
|
-
console.log("\nExecuting 'ls -la' with streaming...");
|
|
86
|
-
await client.executeStream("ls", ["-la"]);
|
|
87
|
-
} catch (error) {
|
|
88
|
-
console.error("Streaming failed:", error);
|
|
89
|
-
} finally {
|
|
90
|
-
client.clearSession();
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Example 3: Git operations
|
|
95
|
-
async function gitExample() {
|
|
96
|
-
console.log("\n=== Git Operations Example ===");
|
|
97
|
-
|
|
98
|
-
const client = createClient({
|
|
99
|
-
baseUrl: "http://localhost:3000",
|
|
100
|
-
onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
|
|
101
|
-
console.log(`ā
Git operation completed: ${command}, success=${success}`);
|
|
102
|
-
},
|
|
103
|
-
onCommandStart: (command, args) => {
|
|
104
|
-
console.log(`š§ Starting: ${command} ${args.join(" ")}`);
|
|
105
|
-
},
|
|
106
|
-
onOutput: (stream, data, command) => {
|
|
107
|
-
console.log(`[${stream}] ${data.trim()}`);
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
await client.createSession();
|
|
113
|
-
|
|
114
|
-
// Create a directory for the repository
|
|
115
|
-
console.log("Creating directory for repository...");
|
|
116
|
-
const mkdirResult = await client.mkdir("test-repo", true);
|
|
117
|
-
console.log(`Directory created: ${mkdirResult.success}`);
|
|
118
|
-
|
|
119
|
-
// Checkout a small test repository
|
|
120
|
-
console.log("Checking out a test repository...");
|
|
121
|
-
const gitResult = await client.gitCheckout(
|
|
122
|
-
"https://github.com/octocat/Hello-World.git",
|
|
123
|
-
"main",
|
|
124
|
-
"test-repo/hello-world"
|
|
125
|
-
);
|
|
126
|
-
console.log(
|
|
127
|
-
`Repository cloned: ${gitResult.success}, target: ${gitResult.targetDir}`
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
// List the contents of the cloned repository
|
|
131
|
-
console.log("Listing repository contents...");
|
|
132
|
-
const lsResult = await client.execute("ls", [
|
|
133
|
-
"-la",
|
|
134
|
-
"test-repo/hello-world",
|
|
135
|
-
]);
|
|
136
|
-
console.log("Repository contents:", lsResult.stdout);
|
|
137
|
-
} catch (error) {
|
|
138
|
-
console.error("Git operations failed:", error);
|
|
139
|
-
} finally {
|
|
140
|
-
client.clearSession();
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// Example 4: Streaming git operations
|
|
145
|
-
async function streamingGitExample() {
|
|
146
|
-
console.log("\n=== Streaming Git Operations Example ===");
|
|
147
|
-
|
|
148
|
-
const client = createClient({
|
|
149
|
-
baseUrl: "http://localhost:3000",
|
|
150
|
-
onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
|
|
151
|
-
console.log(
|
|
152
|
-
`\nš Git operation completed: ${command}, success=${success}`
|
|
153
|
-
);
|
|
154
|
-
},
|
|
155
|
-
onCommandStart: (command, args) => {
|
|
156
|
-
console.log(`š Starting git operation: ${command} ${args.join(" ")}`);
|
|
157
|
-
},
|
|
158
|
-
onOutput: (stream, data, command) => {
|
|
159
|
-
if (stream === "stderr") {
|
|
160
|
-
process.stderr.write(data);
|
|
161
|
-
} else {
|
|
162
|
-
process.stdout.write(data);
|
|
163
|
-
}
|
|
164
|
-
},
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
try {
|
|
168
|
-
await client.createSession();
|
|
169
|
-
|
|
170
|
-
// Create directory with streaming
|
|
171
|
-
console.log("Creating directory with streaming...");
|
|
172
|
-
await client.mkdirStream("streaming-test", true);
|
|
173
|
-
|
|
174
|
-
// Checkout repository with streaming (real-time progress)
|
|
175
|
-
console.log("Checking out repository with streaming...");
|
|
176
|
-
await client.gitCheckoutStream(
|
|
177
|
-
"https://github.com/octocat/Hello-World.git",
|
|
178
|
-
"main",
|
|
179
|
-
"streaming-test/hello-world"
|
|
180
|
-
);
|
|
181
|
-
} catch (error) {
|
|
182
|
-
console.error("Streaming git operations failed:", error);
|
|
183
|
-
} finally {
|
|
184
|
-
client.clearSession();
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Example 5: Quick execute utilities
|
|
189
|
-
async function quickExecuteExample() {
|
|
190
|
-
console.log("\n=== Quick Execute Utilities Example ===");
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
// Quick command execution
|
|
194
|
-
console.log("Quick command execution...");
|
|
195
|
-
const dateResult = await quickExecute("date");
|
|
196
|
-
console.log("Date:", dateResult.stdout.trim());
|
|
197
|
-
|
|
198
|
-
const whoamiResult = await quickExecute("whoami");
|
|
199
|
-
console.log("User:", whoamiResult.stdout.trim());
|
|
200
|
-
|
|
201
|
-
// Quick directory creation
|
|
202
|
-
console.log("Quick directory creation...");
|
|
203
|
-
const mkdirResult = await quickMkdir("quick-test", true);
|
|
204
|
-
console.log(`Directory created: ${mkdirResult.success}`);
|
|
205
|
-
|
|
206
|
-
// Quick git checkout
|
|
207
|
-
console.log("Quick git checkout...");
|
|
208
|
-
const gitResult = await quickGitCheckout(
|
|
209
|
-
"https://github.com/octocat/Hello-World.git",
|
|
210
|
-
"main",
|
|
211
|
-
"quick-test/repo"
|
|
212
|
-
);
|
|
213
|
-
console.log(`Repository cloned: ${gitResult.success}`);
|
|
214
|
-
|
|
215
|
-
// Quick streaming execution
|
|
216
|
-
console.log("Quick streaming execution...");
|
|
217
|
-
await quickExecuteStream("ls", ["-la", "quick-test"]);
|
|
218
|
-
} catch (error) {
|
|
219
|
-
console.error("Quick execute failed:", error);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
// Example 6: Error handling
|
|
224
|
-
async function errorHandlingExample() {
|
|
225
|
-
console.log("\n=== Error Handling Example ===");
|
|
226
|
-
|
|
227
|
-
const client = createClient({
|
|
228
|
-
baseUrl: "http://localhost:3000",
|
|
229
|
-
onError: (error, command, args) => {
|
|
230
|
-
console.error(`ā Error in ${command}: ${error}`);
|
|
231
|
-
},
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
try {
|
|
235
|
-
await client.createSession();
|
|
236
|
-
|
|
237
|
-
// Try to execute a non-existent command
|
|
238
|
-
console.log("Trying to execute non-existent command...");
|
|
239
|
-
try {
|
|
240
|
-
await client.execute("nonexistentcommand");
|
|
241
|
-
} catch (error) {
|
|
242
|
-
console.log(
|
|
243
|
-
"Expected error caught:",
|
|
244
|
-
error instanceof Error ? error.message : error
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// Try to create a directory in a protected location
|
|
249
|
-
console.log("Trying to create directory in protected location...");
|
|
250
|
-
try {
|
|
251
|
-
await client.mkdir("/etc/test", false);
|
|
252
|
-
} catch (error) {
|
|
253
|
-
console.log(
|
|
254
|
-
"Expected error caught:",
|
|
255
|
-
error instanceof Error ? error.message : error
|
|
256
|
-
);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
// Try to checkout an invalid repository
|
|
260
|
-
console.log("Trying to checkout invalid repository...");
|
|
261
|
-
try {
|
|
262
|
-
await client.gitCheckout("invalid-url");
|
|
263
|
-
} catch (error) {
|
|
264
|
-
console.log(
|
|
265
|
-
"Expected error caught:",
|
|
266
|
-
error instanceof Error ? error.message : error
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
} catch (error) {
|
|
270
|
-
console.error("Error handling example failed:", error);
|
|
271
|
-
} finally {
|
|
272
|
-
client.clearSession();
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
// Run examples
|
|
277
|
-
async function runExamples() {
|
|
278
|
-
console.log("š HTTP Client Examples\n");
|
|
279
|
-
|
|
280
|
-
try {
|
|
281
|
-
await basicExample();
|
|
282
|
-
await streamingExample();
|
|
283
|
-
await gitExample();
|
|
284
|
-
await streamingGitExample();
|
|
285
|
-
await quickExecuteExample();
|
|
286
|
-
await errorHandlingExample();
|
|
287
|
-
|
|
288
|
-
console.log("\nā
All examples completed!");
|
|
289
|
-
} catch (error) {
|
|
290
|
-
console.error("ā Example failed:", error);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Export for use in other modules
|
|
295
|
-
export {
|
|
296
|
-
createClient,
|
|
297
|
-
quickExecute,
|
|
298
|
-
quickExecuteStream,
|
|
299
|
-
quickGitCheckout,
|
|
300
|
-
quickMkdir,
|
|
301
|
-
quickGitCheckoutStream,
|
|
302
|
-
quickMkdirStream,
|
|
303
|
-
};
|
|
304
|
-
|
|
305
|
-
// Run examples if this file is executed directly
|
|
306
|
-
if (require.main === module) {
|
|
307
|
-
runExamples();
|
|
308
|
-
}
|
package/tests/connection-test.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { HttpClient } from "../../sandbox/src/client";
|
|
2
|
-
|
|
3
|
-
async function connectionTest() {
|
|
4
|
-
console.log("š Testing HTTP Connection Only");
|
|
5
|
-
|
|
6
|
-
const client = new HttpClient({
|
|
7
|
-
baseUrl: "http://localhost:3000",
|
|
8
|
-
onCommandComplete: (
|
|
9
|
-
success: boolean,
|
|
10
|
-
exitCode: number,
|
|
11
|
-
stdout: string,
|
|
12
|
-
stderr: string,
|
|
13
|
-
command: string,
|
|
14
|
-
args: string[]
|
|
15
|
-
) => {
|
|
16
|
-
console.log(
|
|
17
|
-
`ā
Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
|
|
18
|
-
);
|
|
19
|
-
},
|
|
20
|
-
onCommandStart: (command: string, args: string[]) => {
|
|
21
|
-
console.log("š Command started:", command, args);
|
|
22
|
-
},
|
|
23
|
-
onError: (error: string, command?: string, args?: string[]) => {
|
|
24
|
-
console.error(`ā Error: ${error}`);
|
|
25
|
-
},
|
|
26
|
-
onOutput: (stream: "stdout" | "stderr", data: string, command: string) => {
|
|
27
|
-
console.log(`š¤ [${stream}] ${data.trim()}`);
|
|
28
|
-
},
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
try {
|
|
32
|
-
// Test ping to verify server is reachable
|
|
33
|
-
console.log("š Testing ping...");
|
|
34
|
-
const pingResult = await client.ping();
|
|
35
|
-
console.log("ā
Ping successful:", pingResult);
|
|
36
|
-
|
|
37
|
-
// Create a session
|
|
38
|
-
console.log("š Creating session...");
|
|
39
|
-
const sessionId = await client.createSession();
|
|
40
|
-
console.log("ā
Session created:", sessionId);
|
|
41
|
-
|
|
42
|
-
// Test getting available commands
|
|
43
|
-
console.log("š Getting available commands...");
|
|
44
|
-
const commands = await client.getCommands();
|
|
45
|
-
console.log("ā
Available commands:", commands.length);
|
|
46
|
-
|
|
47
|
-
// Test listing sessions
|
|
48
|
-
console.log("š Listing sessions...");
|
|
49
|
-
const sessions = await client.listSessions();
|
|
50
|
-
console.log("ā
Active sessions:", sessions.count);
|
|
51
|
-
|
|
52
|
-
// Wait a moment
|
|
53
|
-
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
54
|
-
|
|
55
|
-
console.log("ā
Connection test successful");
|
|
56
|
-
} catch (error) {
|
|
57
|
-
console.error("ā Connection test failed:", error);
|
|
58
|
-
throw error;
|
|
59
|
-
} finally {
|
|
60
|
-
client.clearSession();
|
|
61
|
-
console.log("š Session cleared");
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Add a timeout to prevent hanging
|
|
66
|
-
const timeout = setTimeout(() => {
|
|
67
|
-
console.error("ā Connection test timed out after 15 seconds");
|
|
68
|
-
process.exit(1);
|
|
69
|
-
}, 15000);
|
|
70
|
-
|
|
71
|
-
connectionTest()
|
|
72
|
-
.then(() => {
|
|
73
|
-
clearTimeout(timeout);
|
|
74
|
-
console.log("ā
Connection test finished successfully");
|
|
75
|
-
process.exit(0);
|
|
76
|
-
})
|
|
77
|
-
.catch((error) => {
|
|
78
|
-
clearTimeout(timeout);
|
|
79
|
-
console.error("ā Connection test failed:", error);
|
|
80
|
-
process.exit(1);
|
|
81
|
-
});
|