private cleanDockerStreamChunk()

in app/lib/server/processors/DockerJobExecutor.ts [333:372]


  private cleanDockerStreamChunk(chunk: Buffer): string {
    // Docker streams are multiplexed with 8-byte headers
    // Format: [STREAM_TYPE][0][0][0][SIZE][SIZE][SIZE][SIZE][DATA...]
    if (chunk.length < 8) {
      return chunk.toString();
    }

    let result = "";
    let offset = 0;

    while (offset < chunk.length) {
      if (offset + 8 > chunk.length) {
        // Not enough bytes for a complete header, treat as raw data
        result += chunk.slice(offset).toString();
        break;
      }

      // Read the size from bytes 4-7 (big-endian)
      const size = chunk.readUInt32BE(offset + 4);

      if (size === 0) {
        offset += 8;
        continue;
      }

      if (offset + 8 + size > chunk.length) {
        // Not enough data for the claimed size, treat as raw data
        result += chunk.slice(offset).toString();
        break;
      }

      // Extract the actual data
      const data = chunk.slice(offset + 8, offset + 8 + size);
      result += data.toString();

      offset += 8 + size;
    }

    return result;
  }