UNPKG

@wocker/ws

Version:

Docker workspace for web projects

158 lines (157 loc) 6.87 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModemService = void 0; const core_1 = require("@wocker/core"); const formatSizeUnits_1 = require("../utils/formatSizeUnits"); const ProtoService_1 = require("./ProtoService"); let ModemService = class ModemService extends core_1.ModemService { constructor(protoService, logService) { super(); this.protoService = protoService; this.logService = logService; this.record = true; } get modem() { if (!this._modem) { const Modem = require("docker-modem"); this._modem = new Modem({ socketPath: "/var/run/docker.sock" }); } return this._modem; } get docker() { if (!this._docker) { const Docker = require("dockerode"); this._docker = new Docker({ modem: this.modem }); } return this._docker; } async attachStream(stream) { if (process.stdin.isTTY) { process.stdin.setRawMode(true); } process.stdin.resume(); process.stdin.setEncoding("utf8"); process.stdin.pipe(stream); stream.setEncoding("utf8"); stream.pipe(process.stdout); try { await new Promise((resolve, reject) => { stream.on("end", resolve); stream.on("error", reject); }); } finally { process.stdin.pause(); if (process.stdin.isTTY) { process.stdin.setRawMode(false); } process.stdin.unpipe(stream); stream.unpipe(process.stdout); } return stream; } async followProgress(stream) { let isEnded = false, line = 0; const mapLines = {}; return new Promise((resolve, reject) => { const handleEnd = () => { if (!isEnded) { resolve(); } isEnded = true; }; stream.on("data", (chunk) => { const text = chunk.toString().replace(/}\s*\{/g, "},{"), items = JSON.parse(`[${text}]`); for (const item of items) { if (item.id === "moby.buildkit.trace") { const StatusResponse = this.protoService.lookupType("moby.buildkit.v1.StatusResponse"); const buffer = Buffer.from(item.aux, "base64"); const decoded = StatusResponse.decode(buffer); const obj = StatusResponse.toObject(decoded, { enums: String, longs: String, bytes: String, defaults: true }); console.dir(obj, { depth: null }); } else if (item.id === "moby.image.id") { console.dir(item, { depth: null }); } else if (item.stream) { process.stdout.write(`${item.stream}`); line += item.stream.split("\n").length - 1; } else if (item.id) { const { id, status, processDetail: { current, total } = {} } = item; if (typeof mapLines[id] === "undefined") { mapLines[id] = line; } const targetLine = typeof mapLines[id] !== "undefined" ? mapLines[id] : line; const dy = line - targetLine; if (dy > 0) { process.stdout.write("\x1b[s"); process.stdout.write(`\x1b[${dy}A`); } process.stdout.write("\x1b[2K"); let str = `${id}: ${status}\n`; if (status === "Downloading") { const width = process.stdout.columns; const sizeWidth = 19, totalWidth = width - id.length - status.length - sizeWidth - 7, currentWidth = Math.floor(totalWidth * (current / total)), formatSize = `${(0, formatSizeUnits_1.formatSizeUnits)(current)}/${(0, formatSizeUnits_1.formatSizeUnits)(total)}`; str = `${id}: ${status} [${"█".repeat(currentWidth)}${"░".repeat(totalWidth - currentWidth)}] ${formatSize}\n`; } process.stdout.write(str); if (dy > 0) { process.stdout.write("\x1b[u"); } else { line++; } } else if (typeof item.aux === "object") { const str = `auxID: ${item.aux.ID}`; process.stdout.write(`${str}\n`); line += Math.ceil(str.length / process.stdout.columns); } else if (item.status) { process.stdout.write(`${item.status}\n`); line += Math.ceil(item.status.length / process.stdout.columns); } else { console.info("Unexpected data", item); } } }); stream.on("end", handleEnd); stream.on("close", handleEnd); stream.on("error", (err) => { reject(err); }); }); } }; exports.ModemService = ModemService; exports.ModemService = ModemService = __decorate([ (0, core_1.Injectable)("DOCKER_MODEM_SERVICE"), __metadata("design:paramtypes", [ProtoService_1.ProtoService, core_1.LogService]) ], ModemService);