-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsandbox.py
More file actions
1200 lines (1031 loc) · 42.2 KB
/
sandbox.py
File metadata and controls
1200 lines (1031 loc) · 42.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import base64
import builtins
from contextlib import asynccontextmanager, contextmanager
from datetime import datetime, timedelta, timezone
import json
from typing import (
Any,
AsyncIterator,
BinaryIO,
Iterable,
Optional,
TypedDict,
Union,
cast,
)
from typing_extensions import Literal, NotRequired, TypeAlias
import httpx
from .stream import Streamable, complete_stream, start_stream
from .utils import convert_to_snake_case
from .env import AsyncSandboxEnv, SandboxEnv
from .fs import AsyncSandboxFs, SandboxFs
from .process import (
AbortSignal,
AsyncChildProcess,
AsyncDenoProcess,
AsyncDenoRepl,
ChildProcess,
DenoProcess,
DenoRepl,
ProcessSpawnResult,
RemoteProcessOptions,
)
from .bridge import AsyncBridge
from .console import (
AsyncConsoleClient,
AsyncPaginatedList,
ExposeSSHResult,
PaginatedList,
)
from .rpc import AsyncFetchResponse, AsyncRpcClient, FetchResponse
from .transport import (
WebSocketTransport,
)
from .revisions import Revision
Mode: TypeAlias = Literal["connect", "create"]
StdIo: TypeAlias = Literal["piped", "null"]
class SandboxMeta(TypedDict):
id: str
"""The unique identifier for the sandbox."""
created_at: str
"""The ISO 8601 timestamp when the sandbox was created."""
region: str
"""The region the sandbox is located in."""
status: Literal["running", "stopped"]
"""The status of the sandbox."""
stopped_at: NotRequired[str | None]
"""The ISO 8601 timestamp when the sandbox was stopped."""
class SecretConfig(TypedDict):
"""List of hostnames where this secret can be used. Must have at least one host."""
hosts: builtins.list[str]
value: str
class VolumeInfo(TypedDict):
volume: str
path: str
class AppConfigVolume(TypedDict):
volume: str
class AppConfig(TypedDict):
stop_at_ms: NotRequired[int | None]
labels: NotRequired[dict[str, str] | None]
memory_mb: NotRequired[int | None]
volumes: NotRequired[builtins.list[VolumeInfo] | None]
allow_net: NotRequired[builtins.list[str] | None]
root: NotRequired[AppConfigVolume | None]
secrets: NotRequired[dict[str, SecretConfig] | None]
class ExposeHTTPResult(TypedDict):
domain: str
class BuildLog(TypedDict):
"""A build log entry from app deployment."""
timestamp: str
"""The timestamp of the build log."""
level: Literal["info", "error"]
"""The level of the build log."""
message: str
"""The message of the build log."""
class AsyncSandboxApi:
def __init__(
self,
client: AsyncConsoleClient,
):
self._client = client
@asynccontextmanager
async def create(
self,
*,
region: Optional[str] = None,
env: Optional[dict[str, str]] = None,
timeout: Optional[str] = None,
memory_mb: Optional[int] = None,
debug: Optional[bool] = None,
labels: Optional[dict[str, str]] = None,
root: Optional[str] = None,
volumes: Optional[dict[str, str]] = None,
allow_net: Optional[builtins.list[str]] = None,
secrets: Optional[dict[str, SecretConfig]] = None,
ssh: Optional[bool] = None,
port: Optional[int] = None,
) -> AsyncIterator[AsyncSandbox]:
"""Creates a new sandbox instance.
Args:
region: The region where the sandbox should be created.
env: Environment variables to start the sandbox with.
timeout: The timeout of the sandbox. Defaults to "session". Other values like "30s" or "2m" are supported.
memory_mb: The memory size in MiB of the sandbox. Defaults to 1280.
debug: Enable debug logging for the sandbox connection.
labels: Labels to set on the sandbox. Up to 5 labels can be specified.
root: A volume or snapshot to use as the root filesystem of the sandbox.
volumes: Volumes to mount on the sandbox. The key is the mount path, the value is the volume ID or slug.
allow_net: List of hostnames/IP addresses the sandbox can make outbound requests to.
secrets: Secret environment variables injected on the wire for HTTPS requests.
ssh: Whether to expose SSH access to the sandbox.
port: The port number to expose for HTTP access.
"""
config_dict: dict[str, Any] = {
"memory_mb": memory_mb if memory_mb is not None else 1280,
"debug": debug if debug is not None else False,
}
if region is not None:
config_dict["region"] = region
if env is not None:
config_dict["env"] = env
if timeout is not None:
config_dict["timeout"] = timeout
if labels is not None:
config_dict["labels"] = labels
if root is not None:
config_dict["root"] = {"volume": root}
if volumes is not None:
config_dict["volumes"] = volumes
if allow_net is not None:
config_dict["allow_net"] = allow_net
if secrets is not None:
config_dict["secrets"] = secrets
if ssh is not None:
config_dict["ssh"] = ssh
if port is not None:
config_dict["port"] = port
app_config = cast(AppConfig, config_dict)
json_config = json.dumps(app_config, separators=(",", ":")).encode("utf-8")
url = self._client._options["sandbox_ws_url"].join("/api/v3/sandboxes/create")
token = self._client._options["token"]
transport = WebSocketTransport()
ws = await transport.connect(
url=url,
headers={
"Authorization": f"Bearer {token}",
"x-deno-sandbox-config": base64.b64encode(json_config).decode("utf-8"),
},
)
response = ws.response
if response is None:
raise Exception("No response received")
if response.headers is None:
raise Exception("No response headers received")
sandbox_id = response.headers.get("x-deno-sandbox-id")
if sandbox_id is None:
raise Exception("Sandbox ID not found in response headers")
sandbox = None
try:
rpc = AsyncRpcClient(transport)
sandbox = AsyncSandbox(self._client, rpc, sandbox_id)
yield sandbox
finally:
if sandbox is not None:
await sandbox.close()
@asynccontextmanager
async def connect(
self,
sandbox_id: str,
) -> AsyncIterator[AsyncSandbox]:
"""Connects to an existing sandbox instance.
Args:
sandbox_id: The unique id of the sandbox to connect to.
"""
url = self._client._options["sandbox_ws_url"].join(
f"/api/v3/sandbox/{sandbox_id}/connect"
)
token = self._client._options["token"]
transport = WebSocketTransport()
await transport.connect(
url=url,
headers={
"Authorization": f"Bearer {token}",
},
)
sandbox = None
try:
rpc = AsyncRpcClient(transport)
sandbox = AsyncSandbox(self._client, rpc, sandbox_id)
yield sandbox
finally:
if sandbox is not None:
await sandbox.close()
async def list(
self,
*,
labels: Optional[dict[str, str]] = None,
) -> AsyncPaginatedList[SandboxMeta]:
"""List sandboxes.
Args:
labels: Filter sandboxes by labels.
"""
options: dict[str, Any] = {}
if labels is not None:
options["labels"] = labels
return await self._client.get_paginated(
path="/api/v3/sandboxes", cursor=None, params=options if options else None
)
class SandboxApi:
def __init__(self, client: AsyncConsoleClient, bridge: AsyncBridge):
self._bridge = bridge
self._client = client
self._async = AsyncSandboxApi(client)
@contextmanager
def create(
self,
*,
region: Optional[str] = None,
env: Optional[dict[str, str]] = None,
timeout: Optional[str] = None,
memory_mb: Optional[int] = None,
debug: Optional[bool] = None,
labels: Optional[dict[str, str]] = None,
root: Optional[str] = None,
volumes: Optional[dict[str, str]] = None,
allow_net: Optional[builtins.list[str]] = None,
secrets: Optional[dict[str, SecretConfig]] = None,
ssh: Optional[bool] = None,
port: Optional[int] = None,
):
"""Creates a new sandbox instance.
Args:
region: The region where the sandbox should be created.
env: Environment variables to start the sandbox with.
timeout: The timeout of the sandbox. Defaults to "session". Other values like "30s" or "2m" are supported.
memory_mb: The memory size in MiB of the sandbox. Defaults to 1280.
debug: Enable debug logging for the sandbox connection.
labels: Labels to set on the sandbox. Up to 5 labels can be specified.
root: A volume or snapshot to use as the root filesystem of the sandbox.
volumes: Volumes to mount on the sandbox. The key is the mount path, the value is the volume ID or slug.
allow_net: List of hostnames/IP addresses the sandbox can make outbound requests to.
secrets: Secret environment variables injected on the wire for HTTPS requests.
ssh: Whether to expose SSH access to the sandbox.
port: The port number to expose for HTTP access.
"""
async_cm = self._async.create(
region=region,
env=env,
timeout=timeout,
memory_mb=memory_mb,
debug=debug,
labels=labels,
root=root,
volumes=volumes,
allow_net=allow_net,
secrets=secrets,
ssh=ssh,
port=port,
)
async_handle = self._bridge.run(async_cm.__aenter__())
try:
yield Sandbox(self._client, self._bridge, async_handle._rpc, async_handle)
except Exception:
import sys
self._bridge.run(async_cm.__aexit__(*sys.exc_info()))
raise
finally:
self._bridge.run(async_cm.__aexit__(None, None, None))
@contextmanager
def connect(
self,
sandbox_id: str,
):
"""Connects to an existing sandbox instance.
Args:
sandbox_id: The unique id of the sandbox to connect to.
region: If the sandbox was created in a non-default region, the region where the sandbox is running.
debug: Enable debug logging for the sandbox connection.
ssh: Whether to expose SSH access to the sandbox.
"""
async_cm = self._async.connect(sandbox_id)
async_handle = self._bridge.run(async_cm.__aenter__())
try:
yield Sandbox(self._client, self._bridge, async_handle._rpc, async_handle)
except Exception:
import sys
self._bridge.run(async_cm.__aexit__(*sys.exc_info()))
raise
finally:
self._bridge.run(async_cm.__aexit__(None, None, None))
def list(
self,
*,
labels: Optional[dict[str, str]] = None,
) -> PaginatedList[SandboxMeta]:
"""List sandboxes.
Args:
labels: Filter sandboxes by labels.
"""
paginated = self._bridge.run(self._async.list(labels=labels))
return PaginatedList(self._bridge, paginated)
async def _parse_sse_stream(stream: AsyncIterator[bytes]) -> AsyncIterator[str]:
"""Parse Server-Sent Events from a byte stream."""
buffer = b""
async for chunk in stream:
buffer += chunk
while b"\n" in buffer:
line, buffer = buffer.split(b"\n", 1)
line = line.rstrip(b"\r")
if line.startswith(b"data: "):
data = line[6:].decode("utf-8")
yield data
class AsyncBuild:
"""The result of a deno.deploy() operation."""
def __init__(
self,
revision_id: str,
app: str,
client: AsyncConsoleClient,
):
self.id = revision_id
"""The ID of the build."""
self._app = app
self._client = client
async def wait(self) -> Revision:
"""A coroutine that resolves when the build is complete, returning the revision."""
url = self._client._options["console_url"].join(
f"/api/v2/apps/{self._app}/revisions/{self.id}/status"
)
headers = {
"Authorization": f"Bearer {self._client._options['token']}",
}
async with httpx.AsyncClient() as http_client:
response = await http_client.get(str(url), headers=headers)
response.raise_for_status()
revision_data = response.json()
# Fetch timelines
timelines_url = self._client._options["console_url"].join(
f"/api/v2/apps/{self._app}/revisions/{self.id}/timelines"
)
timelines_response = await http_client.get(
str(timelines_url), headers=headers
)
timelines_response.raise_for_status()
timelines_data = timelines_response.json()
result = convert_to_snake_case(revision_data)
result["timelines"] = convert_to_snake_case(timelines_data)
return cast(Revision, result)
async def logs(self) -> AsyncIterator[BuildLog]:
"""An async iterator of build logs."""
url = self._client._options["console_url"].join(
f"/api/v2/apps/{self._app}/revisions/{self.id}/logs"
)
headers = {
"Authorization": f"Bearer {self._client._options['token']}",
}
async with httpx.AsyncClient() as http_client:
async with http_client.stream("GET", str(url), headers=headers) as response:
response.raise_for_status()
async for data in _parse_sse_stream(response.aiter_bytes()):
try:
yield cast(BuildLog, json.loads(data))
except json.JSONDecodeError:
# Skip malformed log entries
continue
class Build:
"""The result of a deno.deploy() operation (sync version)."""
def __init__(
self,
revision_id: str,
app: str,
client: AsyncConsoleClient,
bridge: AsyncBridge,
):
self.id = revision_id
"""The ID of the build."""
self._async_build = AsyncBuild(revision_id, app, client)
self._bridge = bridge
def wait(self) -> Revision:
"""Returns the revision when the build is complete."""
return self._bridge.run(self._async_build.wait())
def logs(self) -> builtins.list[BuildLog]:
"""Returns a list of build logs."""
async def _collect_logs():
logs = []
async for log in self._async_build.logs():
logs.append(log)
return logs
return self._bridge.run(_collect_logs())
class AsyncSandboxDeno:
def __init__(
self,
rpc: AsyncRpcClient,
processes: builtins.list[AsyncChildProcess],
client: AsyncConsoleClient,
sandbox_id: str,
):
self._rpc = rpc
self._processes = processes
self._client = client
self._sandbox_id = sandbox_id
async def run(
self,
*,
args: Optional[builtins.list[str]] = None,
cwd: Optional[str] = None,
clear_env: Optional[bool] = None,
env: Optional[dict[str, str]] = None,
signal: Optional[AbortSignal] = None,
stdin: Optional[Literal["piped", "null"]] = None,
stdout: Optional[Literal["piped", "null", "inherit"]] = None,
stderr: Optional[Literal["piped", "null", "inherit"]] = None,
script_args: Optional[list[str]] = None,
entrypoint: Optional[str] = None,
code: Optional[str] = None,
extension: Optional[
Literal["js", "cjs", "mjs", "ts", "cts", "mts", "jsx", "tsx"]
] = None,
stdin_data: Optional[Streamable] = None,
) -> AsyncDenoProcess:
"""Create a new Deno process from the specified entrypoint file or code.
Args:
args: Arguments to pass to the process.
cwd: The working directory of the process.
clear_env: Clear environment variables from parent process.
env: Environment variables to pass to the subprocess.
signal: An abort signal to cancel the process.
stdin: How stdin of the spawned process should be handled.
stdout: How stdout of the spawned process should be handled.
stderr: How stderr of the spawned process should be handled.
script_args: Arguments to pass to the Deno runtime, available as Deno.args.
entrypoint: A module to read from disk and execute as the entrypoint.
code: Deno code to execute as the entrypoint.
extension: File extension to use when executing code. Default is 'ts'.
stdin_data: Data to write to stdin of the process.
"""
params: dict[str, Any] = {
"stdout": stdout if stdout is not None else "inherit",
"stderr": stderr if stderr is not None else "inherit",
}
if args is not None:
params["args"] = args
if cwd is not None:
params["cwd"] = cwd
if clear_env is not None:
params["clear_env"] = clear_env
if env is not None:
params["env"] = env
if signal is not None:
params["signal"] = signal
if stdin is not None:
params["stdin"] = stdin
if script_args is not None:
params["script_args"] = script_args
if entrypoint is not None:
params["entrypoint"] = entrypoint
if code is not None:
params["code"] = code
if extension is not None:
params["extension"] = extension
if "code" in params and "extension" not in params:
params["extension"] = "ts"
# If stdin data is provided, start stream first (but don't send data yet)
stdin_writer = None
if stdin_data is not None:
params["stdin"] = "piped"
stdin_stream_id, stdin_writer = await start_stream(self._rpc)
params["stdinStreamId"] = stdin_stream_id
opts = RemoteProcessOptions(
stdout_inherit=params["stdout"] == "inherit",
stderr_inherit=params["stderr"] == "inherit",
)
if params["stdout"] == "inherit":
params["stdout"] = "piped"
if params["stderr"] == "inherit":
params["stderr"] = "piped"
result = await self._rpc.call("spawnDeno", params)
# Now that process is spawned, complete the stdin stream
if stdin_writer is not None and stdin_data is not None:
await complete_stream(stdin_writer, stdin_data)
process = await AsyncDenoProcess.create(
result, self._rpc, opts, self._processes
)
self._processes.append(process)
return process
async def eval(self, code: str) -> Any:
repl = await self.repl()
result = await repl.eval(code)
await repl.close()
return result
async def repl(
self,
*,
args: Optional[builtins.list[str]] = None,
cwd: Optional[str] = None,
clear_env: Optional[bool] = None,
env: Optional[dict[str, str]] = None,
signal: Optional[AbortSignal] = None,
stdin: Optional[Literal["piped", "null"]] = None,
stdout: Optional[Literal["piped", "null", "inherit"]] = None,
stderr: Optional[Literal["piped", "null", "inherit"]] = None,
script_args: Optional[builtins.list[str]] = None,
) -> AsyncDenoRepl:
"""Create a new Deno REPL process.
Args:
args: Arguments to pass to the process.
cwd: The working directory of the process.
clear_env: Clear environment variables from parent process.
env: Environment variables to pass to the subprocess.
signal: An abort signal to cancel the process.
stdin: How stdin of the spawned process should be handled.
stdout: How stdout of the spawned process should be handled.
stderr: How stderr of the spawned process should be handled.
script_args: Arguments to pass to the Deno runtime, available as Deno.args.
"""
params: dict[str, Any] = {"stdout": "piped", "stderr": "piped"}
opts = RemoteProcessOptions(stdout_inherit=True, stderr_inherit=True)
if args is not None:
params["args"] = args
if cwd is not None:
params["cwd"] = cwd
if clear_env is not None:
params["clearEnv"] = clear_env
if env is not None:
params["env"] = env
if signal is not None:
params["signal"] = signal
if stdin is not None:
params["stdin"] = stdin
if stdout is not None:
params["stdout"] = stdout
if stdout != "inherit":
opts["stdout_inherit"] = False
if stderr is not None:
params["stderr"] = stderr
if stderr != "inherit":
opts["stderr_inherit"] = False
if script_args is not None:
params["scriptArgs"] = script_args
result: ProcessSpawnResult = await self._rpc.call("spawnDenoRepl", params)
process = await AsyncDenoRepl.create(result, self._rpc, opts, self._processes)
self._processes.append(process)
return process
async def deploy(
self,
app: str,
*,
entrypoint: Optional[str] = None,
args: Optional[builtins.list[str]] = None,
path: Optional[str] = None,
production: Optional[bool] = None,
preview: Optional[bool] = None,
) -> AsyncBuild:
"""Deploy the contents of the sandbox to the specified app in Deno Deploy platform.
Args:
app: The app ID or slug to deploy to.
entrypoint: The entrypoint file path relative to the path option. Defaults to 'main.ts'.
args: Arguments to pass to the entrypoint script.
path: The path to the directory to deploy. If relative, it is relative to /app. Defaults to '/app'.
production: Whether to deploy in production mode. Defaults to True.
preview: Whether to deploy a preview deployment. Defaults to False.
Returns:
An AsyncBuild object with the revision ID and methods to check status and logs.
Example:
```python
from deno_sandbox import AsyncDenoDeploy
async with AsyncDenoDeploy() as client:
async with client.sandbox.create() as sandbox:
await sandbox.fs.write_text_file(
"main.ts",
'Deno.serve(() => new Response("Hi from sandbox.deploy()"))',
)
build = await sandbox.deno.deploy("my-deno-app", entrypoint="main.ts")
print(f"Deployed revision ID: {build.id}")
revision = await build.done
print(f"Revision status: {revision['status']}")
```
"""
url = self._client._options["console_url"].join(f"/api/v2/apps/{app}/deploy")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self._client._options['token']}",
}
# Build request body
body: dict[str, Any] = {
"entrypoint": entrypoint if entrypoint is not None else "main.ts",
"sandboxId": self._sandbox_id,
"path": path if path is not None else "/app",
}
if args is not None:
body["args"] = args
if production is not None:
body["production"] = production
if preview is not None:
body["preview"] = preview
# Make the deploy request
async with httpx.AsyncClient() as http_client:
response = await http_client.post(
str(url), headers=headers, json=body, timeout=30.0
)
response.raise_for_status()
result = response.json()
revision_id = result["revisionId"]
return AsyncBuild(revision_id, app, self._client)
class SandboxDeno:
def __init__(
self,
rpc: AsyncRpcClient,
bridge: AsyncBridge,
processes: builtins.list[AsyncChildProcess],
client: AsyncConsoleClient,
sandbox_id: str,
):
self._rpc = rpc
self._bridge = bridge
self._client = client
self._async = AsyncSandboxDeno(rpc, processes, client, sandbox_id)
def run(
self,
*,
args: Optional[builtins.list[str]] = None,
cwd: Optional[str] = None,
clear_env: Optional[bool] = None,
env: Optional[dict[str, str]] = None,
signal: Optional[AbortSignal] = None,
stdin: Optional[Literal["piped", "null"]] = None,
stdout: Optional[Literal["piped", "null", "inherit"]] = None,
stderr: Optional[Literal["piped", "null", "inherit"]] = None,
script_args: Optional[builtins.list[str]] = None,
entrypoint: Optional[str] = None,
code: Optional[str] = None,
extension: Optional[
Literal["js", "cjs", "mjs", "ts", "cts", "mts", "jsx", "tsx"]
] = None,
stdin_data: Optional[Union[Iterable[bytes], BinaryIO]] = None,
) -> DenoProcess:
"""Create a new Deno process from the specified entrypoint file or code.
Args:
args: Arguments to pass to the process.
cwd: The working directory of the process.
clear_env: Clear environment variables from parent process.
env: Environment variables to pass to the subprocess.
signal: An abort signal to cancel the process.
stdin: How stdin of the spawned process should be handled.
stdout: How stdout of the spawned process should be handled.
stderr: How stderr of the spawned process should be handled.
script_args: Arguments to pass to the Deno runtime, available as Deno.args.
entrypoint: A module to read from disk and execute as the entrypoint.
code: Deno code to execute as the entrypoint.
extension: File extension to use when executing code. Default is 'ts'.
stdin_data: Data to write to stdin of the process.
"""
async_deno = self._bridge.run(
self._async.run(
args=args,
cwd=cwd,
clear_env=clear_env,
env=env,
signal=signal,
stdin=stdin,
stdout=stdout,
stderr=stderr,
script_args=script_args,
entrypoint=entrypoint,
code=code,
extension=extension,
stdin_data=stdin_data,
)
)
return DenoProcess(self._rpc, self._bridge, async_deno)
def eval(self, code: str) -> Any:
return self._bridge.run(self._async.eval(code))
def repl(
self,
*,
args: Optional[builtins.list[str]] = None,
cwd: Optional[str] = None,
clear_env: Optional[bool] = None,
env: Optional[dict[str, str]] = None,
signal: Optional[AbortSignal] = None,
stdin: Optional[Literal["piped", "null"]] = None,
stdout: Optional[Literal["piped", "null", "inherit"]] = None,
stderr: Optional[Literal["piped", "null", "inherit"]] = None,
script_args: Optional[builtins.list[str]] = None,
) -> DenoRepl:
"""Create a new Deno REPL process.
Args:
args: Arguments to pass to the process.
cwd: The working directory of the process.
clear_env: Clear environment variables from parent process.
env: Environment variables to pass to the subprocess.
signal: An abort signal to cancel the process.
stdin: How stdin of the spawned process should be handled.
stdout: How stdout of the spawned process should be handled.
stderr: How stderr of the spawned process should be handled.
script_args: Arguments to pass to the Deno runtime, available as Deno.args.
"""
async_repl = self._bridge.run(
self._async.repl(
args=args,
cwd=cwd,
clear_env=clear_env,
env=env,
signal=signal,
stdin=stdin,
stdout=stdout,
stderr=stderr,
script_args=script_args,
)
)
return DenoRepl(self._rpc, self._bridge, async_repl)
def deploy(
self,
app: str,
*,
entrypoint: Optional[str] = None,
args: Optional[builtins.list[str]] = None,
path: Optional[str] = None,
production: Optional[bool] = None,
preview: Optional[bool] = None,
) -> Build:
"""Deploy the contents of the sandbox to the specified app in Deno Deploy platform.
Args:
app: The app ID or slug to deploy to.
entrypoint: The entrypoint file path relative to the path option. Defaults to 'main.ts'.
args: Arguments to pass to the entrypoint script.
path: The path to the directory to deploy. If relative, it is relative to /app. Defaults to '/app'.
production: Whether to deploy in production mode. Defaults to True.
preview: Whether to deploy a preview deployment. Defaults to False.
Returns:
A Build object with the revision ID and methods to check status and logs.
Example:
```python
from deno_sandbox import DenoDeploy
client = DenoDeploy()
with client.sandbox.create() as sandbox:
sandbox.fs.write_text_file(
"main.ts",
'Deno.serve(() => new Response("Hi from sandbox.deploy()"))',
)
build = sandbox.deno.deploy("my-deno-app", entrypoint="main.ts")
print(f"Deployed revision ID: {build.id}")
revision = build.done
print(f"Revision status: {revision['status']}")
```
"""
async_build = self._bridge.run(
self._async.deploy(
app,
entrypoint=entrypoint,
args=args,
path=path,
production=production,
preview=preview,
)
)
return Build(async_build.id, app, self._client, self._bridge)
class AsyncSandbox:
def __init__(
self, client: AsyncConsoleClient, rpc: AsyncRpcClient, sandbox_id: str
):
self._client = client
self._rpc = rpc
self._processes: builtins.list[AsyncChildProcess] = []
self.url: str | None = None
self.ssh: None = None
self.id = sandbox_id
self.fs = AsyncSandboxFs(rpc)
self.deno = AsyncSandboxDeno(rpc, self._processes, client, sandbox_id)
self.env = AsyncSandboxEnv(rpc)
@property
def closed(self) -> bool:
return self._rpc._transport.closed
async def spawn(
self,
command: str,
*,
args: Optional[list[str]] = None,
cwd: Optional[str] = None,
clear_env: Optional[bool] = None,
env: Optional[dict[str, str]] = None,
signal: Optional[AbortSignal] = None,
stdin: Optional[Literal["piped", "null"]] = None,
stdout: Optional[Literal["piped", "null", "inherit"]] = None,
stderr: Optional[Literal["piped", "null", "inherit"]] = None,
stdin_data: Optional[Streamable] = None,
) -> AsyncChildProcess:
"""Spawn a new child process.
Args:
command: The command to execute.
args: Arguments to pass to the process.
cwd: The working directory of the process.
clear_env: Clear environment variables from parent process.
env: Environment variables to pass to the subprocess.
signal: An abort signal to cancel the process.
stdin: How stdin of the spawned process should be handled.
stdout: How stdout of the spawned process should be handled.
stderr: How stderr of the spawned process should be handled.
stdin_data: Data to write to stdin of the process.
"""
params: dict[str, Any] = {
"command": command,
"stdout": stdout if stdout is not None else "inherit",
"stderr": stderr if stderr is not None else "inherit",
}
if args is not None:
params["args"] = args
if cwd is not None:
params["cwd"] = cwd
if clear_env is not None:
params["clear_env"] = clear_env
if env is not None:
params["env"] = env
if signal is not None:
params["signal"] = signal
if stdin is not None:
params["stdin"] = stdin
# If stdin data is provided, start stream first (but don't send data yet)
stdin_writer = None
if stdin_data is not None:
params["stdin"] = "piped"
stdin_stream_id, stdin_writer = await start_stream(self._rpc)
params["stdinStreamId"] = stdin_stream_id
opts = RemoteProcessOptions(
stdout_inherit=params["stdout"] == "inherit",
stderr_inherit=params["stderr"] == "inherit",
)
if params["stdout"] == "inherit":
params["stdout"] = "piped"
if params["stderr"] == "inherit":
params["stderr"] = "piped"
result: ProcessSpawnResult = await self._rpc.call("spawn", params)
# Now that process is spawned, complete the stdin stream
if stdin_writer is not None and stdin_data is not None:
await complete_stream(stdin_writer, stdin_data)
process = await AsyncChildProcess.create(
result, self._rpc, opts, self._processes
)
self._processes.append(process)
return process
async def fetch(
self,
url: str,
*,
method: Optional[str] = "GET",
headers: Optional[dict[str, str]] = None,
redirect: Optional[Literal["follow", "manual"]] = None,
) -> AsyncFetchResponse:
return await self._rpc.fetch(url, method, headers, redirect)
async def close(self) -> None:
# Kill all tracked processes
for process in self._processes:
await process.kill()
self._processes.clear()
await self._rpc.close()
async def kill(self) -> None:
await self._client.delete(f"/api/v3/sandboxes/{self.id}")