modal 0.2.0 → 0.3.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/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # modal-js development
2
+
3
+ Setup after cloning the repo with submodules:
4
+
5
+ ```bash
6
+ npm install
7
+ ```
8
+
9
+ Then run a script with:
10
+
11
+ ```bash
12
+ node --import tsx path/to/script.ts
13
+ ```
14
+
15
+ ## gRPC support
16
+
17
+ We're using `nice-grpc` because the `@grpc/grpc-js` library doesn't support promises and is difficult to customize with types.
18
+
19
+ This gRPC library depends on the `protobuf-ts` package, which is not compatible with tree shaking because `ModalClientDefinition` transitively references every type. However, since `modal-js` is a server-side package, having a larger bundled library is not a huge issue.
@@ -0,0 +1,96 @@
1
+ declare class Image {
2
+ readonly imageId: string;
3
+ constructor(imageId: string);
4
+ }
5
+
6
+ /**
7
+ * Wrapper around `ReadableStream` with convenience functions.
8
+ *
9
+ * The Stream API is a modern standard for asynchronous data streams across
10
+ * network and process boundaries. It allows you to read data in chunks, pipe
11
+ * and transform it, and handle backpressure.
12
+ *
13
+ * This wrapper adds some extra functions like `.readText()` to read the entire
14
+ * stream as a string, or `readBytes()` to read binary data.
15
+ *
16
+ * Background: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API
17
+ */
18
+ interface ModalReadStream<R = any> extends ReadableStream<R> {
19
+ /** Read the entire stream as a string. */
20
+ readText(): Promise<string>;
21
+ /** Read the entire stream as a byte array. */
22
+ readBytes(): Promise<Uint8Array>;
23
+ }
24
+ /**
25
+ * Wrapper around `WritableStream` with convenience functions.
26
+ *
27
+ * The Stream API is a modern standard for asynchronous data streams across
28
+ * network and process boundaries. It allows you to read data in chunks, pipe
29
+ * and transform it, and handle backpressure.
30
+ *
31
+ * This wrapper adds some extra functions like `.writeText()` to write a string
32
+ * to the stream, or `writeBytes()` to write binary data.
33
+ *
34
+ * Background: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API
35
+ */
36
+ interface ModalWriteStream<R = any> extends WritableStream<R> {
37
+ /** Write a string to the stream. Only if this is a text stream. */
38
+ writeText(text: string): Promise<void>;
39
+ /** Write a byte array to the stream. Only if this is a byte stream. */
40
+ writeBytes(bytes: Uint8Array): Promise<void>;
41
+ }
42
+
43
+ type StdioBehavior = "pipe" | "ignore";
44
+ type StreamMode = "text" | "binary";
45
+ type ExecOptions = {
46
+ mode?: StreamMode;
47
+ stdout?: StdioBehavior;
48
+ stderr?: StdioBehavior;
49
+ };
50
+ declare class Sandbox {
51
+ #private;
52
+ readonly sandboxId: string;
53
+ stdin: ModalWriteStream<string>;
54
+ stdout: ModalReadStream<string>;
55
+ stderr: ModalReadStream<string>;
56
+ constructor(sandboxId: string);
57
+ exec(command: string[], options?: ExecOptions & {
58
+ mode?: "text";
59
+ }): Promise<ContainerProcess<string>>;
60
+ exec(command: string[], options: ExecOptions & {
61
+ mode: "binary";
62
+ }): Promise<ContainerProcess<Uint8Array>>;
63
+ terminate(): Promise<void>;
64
+ wait(): Promise<number>;
65
+ }
66
+ declare class ContainerProcess<R extends string | Uint8Array = any> {
67
+ #private;
68
+ stdin: ModalWriteStream<R>;
69
+ stdout: ModalReadStream<R>;
70
+ stderr: ModalReadStream<R>;
71
+ returncode: number | null;
72
+ constructor(execId: string, options?: ExecOptions);
73
+ /** Wait for process completion and return the exit code. */
74
+ wait(): Promise<number>;
75
+ }
76
+
77
+ type LookupOptions = {
78
+ environment?: string;
79
+ createIfMissing?: boolean;
80
+ };
81
+ type SandboxCreateOptions = {
82
+ cpu?: number;
83
+ memory?: number;
84
+ timeout?: number;
85
+ command?: string[];
86
+ };
87
+ declare class App {
88
+ readonly appId: string;
89
+ constructor(appId: string);
90
+ /** Lookup a deployed app by name, or create if it does not exist. */
91
+ static lookup(name: string, options?: LookupOptions): Promise<App>;
92
+ createSandbox(image: Image, options?: SandboxCreateOptions): Promise<Sandbox>;
93
+ imageFromRegistry(tag: string): Promise<Image>;
94
+ }
95
+
96
+ export { App, Image, type LookupOptions, Sandbox, type SandboxCreateOptions, type StdioBehavior, type StreamMode };