OverviewFilesCommitsIssuesDocsReleasesPeers

hyper-wallet / scripts / setup-pear-compat.sh

55 lines · bash ref:
#!/bin/bash
# Setup Pear + Bare compatibility shims
# Run this after npm install to create necessary compatibility layers

set -e

echo "🔧 Setting up Pear + Bare compatibility shims..."

# Create buffer compatibility shim
BUFFER_DIR="node_modules/buffer"
mkdir -p "$BUFFER_DIR"

cat > "$BUFFER_DIR/package.json" << 'EOF'
{
  "name": "buffer",
  "version": "1.0.0-shim",
  "description": "Compatibility shim mapping Node.js buffer to bare-buffer",
  "main": "index.js"
}
EOF

cat > "$BUFFER_DIR/index.js" << 'EOF'
// Buffer shim: Map Node.js buffer API to bare-buffer
// This provides missing APIs that some packages (like ws) expect
const bareBuffer = require('bare-buffer')

// Add missing isUtf8 function
// Based on Node.js buffer.isUtf8() - validates UTF-8 encoding
function isUtf8(input) {
  if (bareBuffer.Buffer.isBuffer(input)) {
    try {
      input.toString('utf8')
      return true
    } catch {
      return false
    }
  }
  return false
}

// Export all bare-buffer exports plus our additions
module.exports = {
  ...bareBuffer,
  isUtf8
}
EOF

echo "✅ Buffer compatibility shim created at $BUFFER_DIR"
echo ""
echo "This shim provides:"
echo "  - Mapping of 'buffer' → 'bare-buffer'"
echo "  - buffer.isUtf8() polyfill (required by ws package)"
echo ""
echo "See PEAR_BARE_COMPAT.md for technical details"