MCPcopy Create free account
hub / github.com/cloudflare/computer / exec

Method exec

packages/computer/src/shell.ts:159–204  ·  view source on GitHub ↗
(
    command: string,
    options: ExecOptions<E> = {},
  )

Source from the content-addressed store, hash-verified

157 exec(command: string, options: ExecOptions<undefined>): Promise<ExecHandle<undefined>>;
158 exec(command: string, options: ExecOptions<"utf8">): Promise<ExecHandle<"utf8">>;
159 async exec<E extends ExecEncoding>(
160 command: string,
161 options: ExecOptions<E> = {},
162 ): Promise<ExecHandle<E>> {
163 assertNotTemplate(command);
164 // Pre-exec push: ship anything the host wrote since the last
165 // push so the spawned command sees it. Failures non-fatal per
166 // docs/05 — the command still runs; pushed reports 0.
167 let pushed = 0;
168 try {
169 pushed = await this.#sync.push();
170 } catch {
171 // pushed stays 0
172 }
173 const envelope = await withSpan(
174 this.#observer,
175 "workspace.runtime.exec.spawn",
176 {
177 "workspace.runtime.cwd": options.cwd,
178 "workspace.runtime.timeout_ms": options.timeoutMs,
179 "workspace.runtime.id": options.id,
180 },
181 () =>
182 this.#shell.exec({
183 command,
184 id: options.id,
185 cwd: options.cwd,
186 timeoutMs: options.timeoutMs,
187 env: options.env,
188 stdin:
189 typeof options.stdin === "string"
190 ? new TextEncoder().encode(options.stdin)
191 : options.stdin,
192 }),
193 (span, outcome) => {
194 if (outcome.ok) span.setAttribute("workspace.runtime.id", outcome.value.id);
195 },
196 );
197 // Dispose the result envelope when the event stream finishes
198 // draining. Without this, capnweb's exports table holds onto
199 // the envelope for the life of the session — one entry per
200 // exec call — because we hand the inner stream off to the
201 // caller and can't `using` the envelope ourselves.
202 const events = disposeOnDone(envelope.events, () => maybeDispose(envelope));
203 return wrapHandle<E>(this.#shell, this.#sync, envelope.id, events, options.encoding, pushed);
204 }
205
206 get(id: string): Promise<ExecHandle<undefined>>;
207 get(id: string, options: GetExecOptions<undefined>): Promise<ExecHandle<undefined>>;

Callers

nothing calls this directly

Calls 8

assertNotTemplateFunction · 0.85
withSpanFunction · 0.85
disposeOnDoneFunction · 0.85
wrapHandleFunction · 0.85
maybeDisposeFunction · 0.70
pushMethod · 0.65
execMethod · 0.65
setAttributeMethod · 0.65

Tested by

no test coverage detected