chromium/third_party/node/update_node_binaries

#!/bin/bash

# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Script for updating Node binaries.
# 1) Update NODE_VERSION variable below to the desired version.
# 2) Run this script without any flags first to download all the binaries
#    locally.
# 3) Authenticate using your @google account by running `gsutil.py config`
#    (prerequisite for step #4 below).
# 4) To upload the binaries to the Google Storage bucket, use the
#    `--upload` or `-u` flag. This will upload the binaries to
#    the Google Storage bucket and update the DEPS file.
# 5) Land a CL with the changes generated by this script.

set -eu
cd "$(dirname "$0")"

BASE_URL="https://nodejs.org/dist"
NODE_VERSION="v20.11.0"

upload=false  # Default value

# The script can take a command line flag and check if it is valid.
# If the flag `--upload` | `-u` is passed in, then the script
# uploads the binaries and update the DEPS.
while [[ $# -gt 0 ]]; do
  case $1 in
    -u|--upload)
      upload=true
      shift
      ;;
    *)
      echo "Unknown option: $1"
      exit 1
      ;;
  esac
done

upload_unix(){
  local SUFFIX="$1"
  local FOLDER="$2"
  local UPLOAD_FILE="${FOLDER}/node-${SUFFIX}.tar.gz"

  if [[ -e "${UPLOAD_FILE}" ]]; then
    if [[ "$SUFFIX" == "darwin-x64" ]]; then
      local PATHNAME="src/third_party/node/mac"
    fi

    if [[ "$SUFFIX" == "darwin-arm64" ]]; then
      local PATHNAME="src/third_party/node/mac_arm64"
    fi

    if [[ "$SUFFIX" == "linux-x64" ]]; then
      local PATHNAME="src/third_party/node/linux"
    fi
    ./upload_to_gcs_and_update_deps "${UPLOAD_FILE}" "${PATHNAME}"
    echo "DONE uploading ${SUFFIX} binaries and updating DEPS file.."
  else
    echo "Error: File '${UPLOAD_FILE}' does not exist."
    echo "Please run ./update_node_binaries to download binaries locally first."
  fi
}

upload_win(){
  local FILENAME="node.exe"
  local FOLDER="win"
  local UPLOAD_FILE="${FOLDER}/${FILENAME}"

  if [[ -e "${UPLOAD_FILE}" ]]; then
    ./upload_to_gcs_and_update_deps "${UPLOAD_FILE}" src/third_party/node/win
    echo "DONE uploading Windows binaries and updating DEPS file.."
  else
    echo "Error: File '$UPLOAD_FILE' does not exist."
    echo "Please run ./update_node_binaries to download binaries locally first."
  fi
}

download_unix() {
  local SUFFIX="$1"
  local FOLDER="$2"
  local FILENAME="node-${NODE_VERSION}-${SUFFIX}.tar.gz"
  local URL="${BASE_URL}/${NODE_VERSION}/${FILENAME}"

  rm -f "${FOLDER}/${FILENAME}"
  wget -P "${FOLDER}/" "${URL}"

  # Check SHASUMS256 of downloaded binary.
  local sha256_expected
  sha256_expected="$(grep "$FILENAME" SHASUMS256.txt | cut -d ' ' -f1)"
  local sha256_actual
  sha256_actual="$(sha256sum "${FOLDER}/${FILENAME}" | cut -d ' ' -f1)"

  if [ "${sha256_expected}" != "${sha256_actual}" ]; then
    echo "SHA256 mismatch. Exiting..."
    exit 1
  fi

  # Unpack temporarily, delete npm, npx, corepack and re-pack.
  tar xfz "${FOLDER}/${FILENAME}" -C "${FOLDER}/"
  rm "${FOLDER}/${FILENAME}"
  rm "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/bin/corepack"
  rm -rf "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/lib/node_modules/corepack"
  rm "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/bin/npm"
  rm -rf "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/lib/node_modules/npm"
  rm "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/bin/npx"

  # Drop the version info from the name, since it is redundant and would make
  # rolling new versions more involved.
  rm -rf "${FOLDER}/node-${SUFFIX}/"
  mv "${FOLDER}/node-${NODE_VERSION}-${SUFFIX}/" "${FOLDER}/node-${SUFFIX}/"
  tar cfz "${FOLDER}/node-${SUFFIX}.tar.gz" -C "${FOLDER}" "node-${SUFFIX}/"
}

download_win() {
  local FILENAME="node.exe"
  local FOLDER="win"
  local WINDOWS_URL="${BASE_URL}/${NODE_VERSION}/win-x64/${FILENAME}"
  rm -f "${FOLDER}/${FILENAME}"
  wget -P "${FOLDER}/" "${WINDOWS_URL}"

  # Check SHASUMS256 of downloaded binary.
  local sha256_expected
  sha256_expected="$(grep "win-x64/$FILENAME" SHASUMS256.txt | cut -d ' ' -f1)"
  local sha256_actual
  sha256_actual="$(sha256sum "${FOLDER}/${FILENAME}" | cut -d ' ' -f1)"

  if [ "${sha256_expected}" != "${sha256_actual}" ]; then
    echo "SHA256 mismatch. Exiting..."
    exit 1
  fi
}


if [[ $upload != true ]]; then
  # First download checksum file.
  rm -f "SHASUMS256.txt"
  wget "https://nodejs.org/dist/${NODE_VERSION}/SHASUMS256.txt"
  download_unix "darwin-x64" "mac"
  download_unix "darwin-arm64" "mac"
  download_unix "linux-x64" "linux"
  download_win
  echo "To upload the binaries, please run ./update_node_binaries --upload"
else
  echo "Uploading to GCS and updating DEPS files..."
  upload_unix "darwin-x64" "mac"
  upload_unix "darwin-arm64" "mac"
  upload_unix "linux-x64" "linux"
  upload_win
fi