| 266 | ) => WorkspaceRuntimeExecHandle<E> | Promise<WorkspaceRuntimeExecHandle<E>>; |
| 267 | |
| 268 | function makeRuntimeClient( |
| 269 | runtime: UnderlyingRuntime, |
| 270 | // Adapts the handle the underlying `exec` resolves to: identity on |
| 271 | // the local path (already a host handle), rebuild on the remote path. |
| 272 | rehydrate: RehydrateRuntimeHandle, |
| 273 | ): WorkspaceRuntimeClient { |
| 274 | async function exec( |
| 275 | commandOrStrings: string | TemplateStringsArray, |
| 276 | optionsOrValue?: RuntimeExecOptions | ShellValue, |
| 277 | ...rest: ShellValue[] |
| 278 | ): Promise<WorkspaceRuntimeExecHandle<ExecEncoding>> { |
| 279 | if (isTemplateStringsArray(commandOrStrings)) { |
| 280 | const values = optionsOrValue === undefined ? rest : [optionsOrValue as ShellValue, ...rest]; |
| 281 | const command = sh(commandOrStrings, ...values); |
| 282 | const id = crypto.randomUUID(); |
| 283 | return rehydrate<"utf8">(await runtime.exec(command, { id, encoding: "utf8" }), { id }); |
| 284 | } |
| 285 | const options = withExecutionId(optionsOrValue as RuntimeExecOptions | undefined); |
| 286 | const handle = await runtime.exec( |
| 287 | commandOrStrings, |
| 288 | options as unknown as Record<string, unknown>, |
| 289 | ); |
| 290 | const metadata = { id: options.id, backend: options.backend }; |
| 291 | return options.encoding === "utf8" |
| 292 | ? rehydrate<"utf8">(handle, metadata) |
| 293 | : rehydrate<undefined>(handle, metadata); |
| 294 | } |
| 295 | const getExec = async (id: string, options?: RuntimeGetOptions) => { |
| 296 | const handle = await runtime.getExec(id, options as Record<string, unknown> | undefined); |
| 297 | const metadata = { id, backend: options?.backend }; |
| 298 | return options?.encoding === "utf8" |
| 299 | ? rehydrate<"utf8">(handle, metadata) |
| 300 | : rehydrate<undefined>(handle, metadata); |
| 301 | }; |
| 302 | const killExec = (id: string, options?: RuntimeKillOptions) => runtime.killExec(id, options); |
| 303 | const disposeExec = (id: string, options?: { backend?: string }) => |
| 304 | runtime.disposeExec(id, options); |
| 305 | return { exec, getExec, killExec, disposeExec } as WorkspaceRuntimeClient; |
| 306 | } |
| 307 | |
| 308 | function withExecutionId( |
| 309 | options: RuntimeExecOptions | undefined, |