OverviewFilesCommitsIssuesDocsReleasesPeers

git-remote-punch / lib / git-p2p-client.js

39 lines · javascript ref:
/**
 * Client-side git P2P helper. Sends the opening service header on a
 * P2P connection, then returns it for bidirectional proxying.
 *
 * Protocol: service\0repoName\0
 *   e.g., "git-upload-pack\0myrepo\0" for clone/fetch
 *         "git-receive-pack\0myrepo\0" for push
 *
 * The server side (pear-git-v3's hyper-sync, or pear-git v1's P2P
 * handler) reads this header and spawns the corresponding git process,
 * then proxies its stdin/stdout over the connection.
 */
export class GitP2PClient {
  constructor (options = {}) {
    this.petnames = options.petnames
  }

  /**
   * Initiate a git operation over P2P.
   * @param {Stream} conn - Hyperswarm connection stream
   * @param {string} service - 'git-upload-pack' or 'git-receive-pack'
   * @param {string} repoName - Repository name
   * @returns {Stream} - The same connection, for caller-managed proxying
   */
  async initiate (conn, service, repoName) {
    const header = Buffer.concat([
      Buffer.from(service, 'utf-8'),
      Buffer.from([0]),
      Buffer.from(repoName, 'utf-8'),
      Buffer.from([0])
    ])

    conn.write(header)
    return conn
  }
}

export default GitP2PClient