@cloudflare/containers 0.0.27 → 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { ContainerOptions, ContainerStartOptions, ContainerStartConfigOptions, Schedule, StopParams, State } from '../types';
1
+ import type { ContainerOptions, ContainerStartOptions, ContainerStartConfigOptions, Schedule, StopParams, State, WaitOptions, CancellationOptions, StartAndWaitForPortsOptions } from '../types';
2
2
  import { DurableObject } from 'cloudflare:workers';
3
3
  export type Signal = 'SIGKILL' | 'SIGINT' | 'SIGTERM';
4
4
  export type SignalInteger = number;
@@ -16,84 +16,59 @@ export declare class Container<Env = unknown> extends DurableObject<Env> {
16
16
  */
17
17
  getState(): Promise<State>;
18
18
  /**
19
- * Start the container if it's not running and set up monitoring
19
+ * Start the container if it's not running and set up monitoring and lifecycle hooks,
20
+ * without waiting for ports to be ready.
20
21
  *
21
- * This method handles the core container startup process without waiting for ports to be ready.
22
- * It will automatically retry if the container fails to start, up to maxTries attempts.
22
+ * It will automatically retry if the container fails to start, using the specified waitOptions
23
23
  *
24
- * It's useful when you need to:
25
- * - Start a container without blocking until a port is available
26
- * - Initialize a container that doesn't expose ports
27
- * - Perform custom port availability checks separately
28
- *
29
- * The method applies the container configuration from your instance properties by default, but allows
30
- * overriding these values for this specific startup:
31
- * - Environment variables (defaults to this.envVars)
32
- * - Custom entrypoint commands (defaults to this.entrypoint)
33
- * - Internet access settings (defaults to this.enableInternet)
34
- *
35
- * It also sets up monitoring to track container lifecycle events and automatically
36
- * calls the onStop handler when the container terminates.
37
24
  *
38
25
  * @example
39
- * // Basic usage in a custom Container implementation
40
- * async customInitialize() {
41
- * // Start the container without waiting for a port
42
- * await this.start();
43
- *
44
- * // Perform additional initialization steps
45
- * // that don't require port access
46
- * }
47
- *
48
- * @example
49
- * // Start with custom configuration
50
26
  * await this.start({
51
27
  * envVars: { DEBUG: 'true', NODE_ENV: 'development' },
52
28
  * entrypoint: ['npm', 'run', 'dev'],
53
29
  * enableInternet: false
54
30
  * });
55
31
  *
56
- * @param options - Optional configuration to override instance defaults
57
- * @param waitOptions - Optional wait configuration with abort signal for cancellation
32
+ * @param startOptions - Override `envVars`, `entrypoint` and `enableInternet` on a per-instance basis
33
+ * @param waitOptions - Optional wait configuration with abort signal for cancellation. Default ~8s timeout.
58
34
  * @returns A promise that resolves when the container start command has been issued
59
35
  * @throws Error if no container context is available or if all start attempts fail
60
36
  */
61
- start(options?: ContainerStartConfigOptions, waitOptions?: {
62
- signal?: AbortSignal;
63
- }): Promise<void>;
37
+ start(startOptions?: ContainerStartConfigOptions, waitOptions?: WaitOptions): Promise<void>;
64
38
  /**
65
- * Start the container and wait for ports to be available
66
- *
67
- * This method builds on start() by adding port availability verification:
68
- * 1. Calls start() to ensure the container is running
69
- * 2. If no ports are specified and requiredPorts is not set, it uses defaultPort (if set)
70
- * 3. If no ports can be determined, it calls onStart and renewActivityTimeout immediately
71
- * 4. For each specified port, it polls until the port is available or maxTries is reached
72
- * 5. When all ports are available, it triggers onStart and renewActivityTimeout
39
+ * Start the container and wait for ports to be available.
73
40
  *
74
- * The method prioritizes port sources in this order:
75
- * 1. Ports specified directly in the method call
76
- * 2. requiredPorts class property (if set)
77
- * 3. defaultPort (if neither of the above is specified)
41
+ * For each specified port, it polls until the port is available or `cancellationOptions.portReadyTimeoutMS` is reached.
78
42
  *
79
43
  * @param ports - The ports to wait for (if undefined, uses requiredPorts or defaultPort)
80
- * @param cancellationOptions
81
- * @param startOptions Override configuration on a per instance for env vars, entrypoint command and internet access
82
- * @throws Error if port checks fail after maxTries attempts
83
- */
84
- startAndWaitForPorts(ports?: number | number[], cancellationOptions?: {
85
- abort?: AbortSignal;
86
- instanceGetTimeoutMS?: number;
87
- portReadyTimeoutMS?: number;
88
- waitInterval?: number;
89
- }, startOptions?: ContainerStartConfigOptions): Promise<void>;
90
- /**
91
- * Shuts down the container.
44
+ * @param cancellationOptions - Options to configure timeouts, polling intereva, and abort signal
45
+ * @param startOptions Override configuration on a per-instance basis for env vars, entrypoint command and internet access
46
+ * @returns A promise that resolves when the container has been started and the ports are listening
47
+ * @throws Error if port checks fail after the specified timeout or if the container fails to start.
48
+ */
49
+ startAndWaitForPorts(args: StartAndWaitForPortsOptions): Promise<void>;
50
+ startAndWaitForPorts(ports?: number | number[], cancellationOptions?: CancellationOptions, startOptions?: ContainerStartConfigOptions): Promise<void>;
51
+ startAndWaitForPorts(portsOrArgs?: number | number[] | StartAndWaitForPortsOptions, cancellationOptions?: CancellationOptions, startOptions?: ContainerStartConfigOptions): Promise<void>;
52
+ /**
53
+ *
54
+ * Waits for a specified port to be ready
55
+ *
56
+ * Returns the number of tries used to get the port, or throws if it couldn't get the port within the specified retry limits.
57
+ *
58
+ * @param waitOptions -
59
+ * - `portToCheck`: The port number to check
60
+ * - `abort`: Optional AbortSignal to cancel waiting
61
+ * - `retries`: Number of retries before giving up (default: TRIES_TO_GET_PORTS)
62
+ * - `waitInterval`: Interval between retries in milliseconds (default: INSTANCE_POLL_INTERVAL_MS)
63
+ */
64
+ waitForPort(waitOptions: WaitOptions): Promise<number>;
65
+ /**
66
+ * Send a signal to the container.
92
67
  * @param signal - The signal to send to the container (default: 15 for SIGTERM)
93
68
  */
94
69
  stop(signal?: Signal | SignalInteger): Promise<void>;
95
70
  /**
96
- * Destroys the container. It will trigger onError instead of onStop.
71
+ * Destroys the container with a SIGKILL. Triggers onStop.
97
72
  */
98
73
  destroy(): Promise<void>;
99
74
  /**
@@ -109,7 +84,7 @@ export declare class Container<Env = unknown> extends DurableObject<Env> {
109
84
  onStop(_: StopParams): void | Promise<void>;
110
85
  /**
111
86
  * Lifecycle method called when the container is running, and the activity timeout
112
- * expiration has been reached.
87
+ * expiration (set by `sleepAfter`) has been reached.
113
88
  *
114
89
  * If you want to shutdown the container, you should call this.stop() here
115
90
  *
@@ -130,7 +105,10 @@ export declare class Container<Env = unknown> extends DurableObject<Env> {
130
105
  */
131
106
  renewActivityTimeout(): void;
132
107
  /**
133
- * Schedule a task to be executed in the future
108
+ * Schedule a task to be executed in the future.
109
+ *
110
+ * We strongly recommend using this instead of the `alarm` handler.
111
+ *
134
112
  * @template T Type of the payload data
135
113
  * @param when When to execute the task (Date object or number of seconds delay)
136
114
  * @param callback Name of the method to call
@@ -140,26 +118,26 @@ export declare class Container<Env = unknown> extends DurableObject<Env> {
140
118
  schedule<T = string>(when: Date | number, callback: string, payload?: T): Promise<Schedule<T>>;
141
119
  /**
142
120
  * Send a request to the container (HTTP or WebSocket) using standard fetch API signature
143
- * Based on containers-starter-go implementation
144
121
  *
145
- * This method handles HTTP requests to the container. WebSocket requests done outside the DO*
146
- * won't work until https://github.com/cloudflare/workerd/issues/2319 is addressed. Until then, please use `switchPort` + `fetch()`.
122
+ * This method handles HTTP requests to the container.
123
+ *
124
+ * WebSocket requests done outside the DO won't work until https://github.com/cloudflare/workerd/issues/2319 is addressed.
125
+ * Until then, please use `switchPort` + `fetch()`.
147
126
  *
148
127
  * Method supports multiple signatures to match standard fetch API:
149
128
  * - containerFetch(request: Request, port?: number)
150
129
  * - containerFetch(url: string | URL, init?: RequestInit, port?: number)
151
130
  *
152
- * @param requestOrUrl The request object or URL string/object to send to the container
153
- * @param portOrInit Port number or fetch RequestInit options
154
- * @param portParam Optional port number when using URL+init signature
155
- * @returns A Response from the container, or WebSocket connection
131
+ * Starts the container if not already running, and waits for the target port to be ready.
132
+ *
133
+ * @returns A Response from the container
156
134
  */
157
135
  containerFetch(requestOrUrl: Request | string | URL, portOrInit?: number | RequestInit, portParam?: number): Promise<Response>;
158
136
  /**
159
- * Handle fetch requests to the Container
160
- * Default implementation forwards all HTTP and WebSocket requests to the container
161
- * Override this in your subclass to specify a port or implement custom request handling
162
137
  *
138
+ * Fetch handler on the Container class.
139
+ * By default this forwards all requests to the container by calling `containerFetch`.
140
+ * Use `switchPort` to specify which port on the container to target, or this will use `defaultPort`.
163
141
  * @param request The request to handle
164
142
  */
165
143
  fetch(request: Request): Promise<Response>;
@@ -174,7 +152,19 @@ export declare class Container<Env = unknown> extends DurableObject<Env> {
174
152
  */
175
153
  private sql;
176
154
  private requestAndPortFromContainerFetchArgs;
155
+ /**
156
+ *
157
+ * The method prioritizes port sources in this order:
158
+ * 1. Ports specified directly in the method call
159
+ * 2. `requiredPorts` class property (if set)
160
+ * 3. `defaultPort` (if neither of the above is specified)
161
+ * 4. Falls back to port 33 if none of the above are set
162
+ */
177
163
  private getPortsToCheck;
164
+ /**
165
+ * Tries to start a container if it's not already running
166
+ * Returns the number of tries used
167
+ */
178
168
  private startContainerIfNotRunning;
179
169
  private setupMonitorCallbacks;
180
170
  deleteSchedules(name: string): void;