UNPKG

sui-direct

Version:

Decentralized version control system on SUI blockchain

116 lines (115 loc) 4.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ora_1 = __importDefault(require("ora")); const path_1 = require("path"); const fs_1 = require("fs"); const p2p_1 = __importDefault(require("./p2p")); const colors_1 = require("../utils/colors"); const helpers_1 = require("../utils/helpers"); const zip_1 = require("../utils/zip"); class Remote extends p2p_1.default { constructor(_) { super(); this.connection = _.connection; this.nodePeerID = _.nodePeerID; this.peerID = _.peerID; } async pull(path) { } async push(path) { const pushStream = await this.connection.newStream(this.PUSH_PROTOCOL); const [[{ source }], { name: zipName }] = await Promise.all([ (0, helpers_1.initDynamicImports)(["stream-to-it"]), (0, zip_1.zipFolderIgnoringGitignore)(path), ]); const spinner = (0, ora_1.default)(`Pushing to repository`).start(); const zipPath = (0, path_1.join)(path, zipName); try { const fileStream = (0, fs_1.createReadStream)(zipPath); const source_ = source(fileStream); await pushStream.sink(source_); const response = await this.parseChunk(pushStream); if ((response === null || response === void 0 ? void 0 : response.status) === false) throw new Error(response.message || "An error occurred while pushing."); console.log("\n"); console.log(colors_1.colorize.successIcon("Pushed successfully!")); console.log(`\nBlob ID: ${colors_1.colorize.highlight(response.blobId)}\nRepository ID: ${colors_1.colorize.highlight(response.id)}`); } catch (err) { console.error(colors_1.colorize.errorIcon("Failed to push")); console.error(colors_1.colorize.error(err)); } finally { spinner.stop(); try { (0, fs_1.rmSync)(zipPath, { force: true }); } catch (e) { // Ignore cleanup errors console.warn("Failed to clean up zip file:", e); } } } async clone(id) { const spinner = (0, ora_1.default)(`Cloning repository ${id}`).start(); let cloneStream = null; try { // Create stream with retry logic cloneStream = await this.connection.newStream(this.PULL_PROTOCOL); // Send request const requestData = JSON.stringify({ id }); await this.sink(cloneStream, requestData); // Receive streamed response const zipBuffer = await this.receiveStreamedContent(cloneStream); console.log(`Received ${colors_1.colorize.info(zipBuffer.length.toString())} bytes`); // Process the received content await this.processClonedContent(id, zipBuffer); spinner.stop(); console.log("\n"); console.log(colors_1.colorize.successIcon(`Repository cloned successfully!`)); console.log(`Repository extracted to: ${colors_1.colorize.highlight((0, path_1.join)(process.cwd(), id))}`); } catch (err) { spinner.stop(); console.error(colors_1.colorize.errorIcon("Failed to clone repository")); console.error(colors_1.colorize.error(err)); } finally { // Clean up stream if (cloneStream) { try { await cloneStream.close(); } catch (e) { console.log(colors_1.colorize.warning("Failed to close clone stream")); } } } } async rename(newName, blobId, id) { const renameStream = await this.connection.newStream(this.RENAME_PROTOCOL); const message = JSON.stringify({ name: newName, blobId, id, }); await this.sink(renameStream, message); const response = await this.parseChunk(renameStream); if ((response === null || response === void 0 ? void 0 : response.status) === false) { throw new Error(response.message || "An error occurred while renaming."); } return response; } async list(owner) { const res = await fetch(`${this.config.nodehttp}/list/${owner}`); if (!res.ok) return []; const data = await res.json(); if (data.status === false) return []; return data.repositories || []; } } exports.default = Remote;