OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / scripts / old-apply / deploy-operations.js

64 lines · javascript ref:
/**
 * Autobase Operation Types for SharedDeployRegistry
 *
 * Defines operation constants, JSON encode/decode, and validation
 * for deploy-related operations across linked pear-git instances.
 *
 * Operations are appended to Autobase writer cores and processed
 * by the deterministic apply function to build the Hyperbee view.
 */

// Server management
export const OP_REGISTER_SERVER = 'register-server'

// Deploy lifecycle
export const OP_DEPLOY_COMPLETE = 'deploy-complete'

// Writer management (Autobase multi-writer)
export const OP_ADD_WRITER = 'add-writer'

/**
 * Encode an operation to a Buffer for Autobase append
 */
export function encodeOp (op) {
  return Buffer.from(JSON.stringify(op))
}

/**
 * Decode a Buffer back to an operation object
 */
export function decodeOp (buf) {
  return JSON.parse(buf.toString())
}

/**
 * Validate an operation has required fields for its type
 * @returns {{ valid: boolean, error?: string }}
 */
export function validateOp (op) {
  if (!op || !op.type) {
    return { valid: false, error: 'Missing operation type' }
  }

  switch (op.type) {
    case OP_REGISTER_SERVER:
      if (!op.serverKey) return { valid: false, error: 'register-server requires serverKey' }
      break

    case OP_DEPLOY_COMPLETE:
      if (!op.repoName) return { valid: false, error: 'deploy-complete requires repoName' }
      if (!op.commitHash) return { valid: false, error: 'deploy-complete requires commitHash' }
      if (!op.branch) return { valid: false, error: 'deploy-complete requires branch' }
      break

    case OP_ADD_WRITER:
      if (!op.writerKey) return { valid: false, error: 'add-writer requires writerKey' }
      break

    default:
      return { valid: false, error: `Unknown operation type: ${op.type}` }
  }

  return { valid: true }
}