| 63 | } |
| 64 | |
| 65 | export class DockerHandle implements SandboxHandle { |
| 66 | readonly id: string |
| 67 | readonly provider = 'docker' |
| 68 | readonly workspaceRoot: string |
| 69 | readonly capabilities = DOCKER_CAPS |
| 70 | readonly fs: SandboxHandle['fs'] |
| 71 | readonly git: SandboxHandle['git'] |
| 72 | readonly process: SandboxHandle['process'] |
| 73 | readonly ports: SandboxHandle['ports'] |
| 74 | readonly env: SandboxHandle['env'] |
| 75 | |
| 76 | private readonly docker: Dockerode |
| 77 | private readonly container: Dockerode.Container |
| 78 | private readonly workdir: string |
| 79 | private readonly deps: DockerHandleDeps |
| 80 | private readonly envVars: Record<string, string> = {} |
| 81 | |
| 82 | constructor(deps: DockerHandleDeps) { |
| 83 | this.docker = deps.docker |
| 84 | this.container = deps.container |
| 85 | this.workdir = deps.workdir |
| 86 | this.workspaceRoot = deps.workdir |
| 87 | this.deps = deps |
| 88 | this.id = deps.container.id |
| 89 | |
| 90 | this.process = { |
| 91 | exec: (command, opts) => this.exec(command, opts), |
| 92 | spawn: (command, opts) => this.spawnProcess(command, opts), |
| 93 | } |
| 94 | |
| 95 | this.fs = { |
| 96 | read: async (p) => { |
| 97 | const r = await this.exec(`base64 ${q(this.abs(p))}`) |
| 98 | if (r.exitCode !== 0) throw new Error(`read failed: ${r.stderr.trim()}`) |
| 99 | return Buffer.from(r.stdout, 'base64').toString('utf8') |
| 100 | }, |
| 101 | readBytes: async (p) => { |
| 102 | const r = await this.exec(`base64 ${q(this.abs(p))}`) |
| 103 | if (r.exitCode !== 0) throw new Error(`read failed: ${r.stderr.trim()}`) |
| 104 | return new Uint8Array(Buffer.from(r.stdout, 'base64')) |
| 105 | }, |
| 106 | write: async (p, data) => { |
| 107 | const abs = this.abs(p) |
| 108 | const b64 = Buffer.from( |
| 109 | typeof data === 'string' ? Buffer.from(data, 'utf8') : data, |
| 110 | ).toString('base64') |
| 111 | const dir = abs.replace(/\/[^/]*$/, '') || '/' |
| 112 | const r = await this.exec( |
| 113 | `mkdir -p ${q(dir)} && printf %s ${q(b64)} | base64 -d > ${q(abs)}`, |
| 114 | ) |
| 115 | if (r.exitCode !== 0) |
| 116 | throw new Error(`write failed: ${r.stderr.trim()}`) |
| 117 | }, |
| 118 | list: async (p) => { |
| 119 | const r = await this.exec(`ls -1Ap ${q(this.abs(p))}`) |
| 120 | if (r.exitCode !== 0) throw new Error(`list failed: ${r.stderr.trim()}`) |
| 121 | return r.stdout |
| 122 | .split('\n') |
nothing calls this directly
no outgoing calls
no test coverage detected