node 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bash
  2. # Run a command with our local node install, rather than any globally installed
  3. # instance.
  4. set -euo pipefail
  5. if [[ "${CI:-}" == "true" ]]; then
  6. set -x
  7. fi
  8. (
  9. if [[ "${CI:-}" == "true" ]]; then
  10. set -x
  11. fi
  12. repo_root="${BASH_SOURCE%/*}/../"
  13. cd "$repo_root"
  14. cachedir="$HOME/.cache/tailscale-node"
  15. tarball="${cachedir}.tar.gz"
  16. read -r want_rev < "$(dirname "$0")/node.rev"
  17. got_rev=""
  18. if [[ -x "${cachedir}/bin/node" ]]; then
  19. got_rev=$("${cachedir}/bin/node" --version)
  20. got_rev="${got_rev#v}" # trim the leading 'v'
  21. fi
  22. if [[ "$want_rev" != "$got_rev" ]]; then
  23. rm -rf "$cachedir" "$tarball"
  24. if [[ -n "${IN_NIX_SHELL:-}" ]]; then
  25. nix_node="$(which -a node | grep /nix/store | head -1)"
  26. nix_node="${nix_node%/bin/node}"
  27. nix_node_rev="${nix_node##*-}"
  28. if [[ "$nix_node_rev" != "$want_rev" ]]; then
  29. echo "Wrong node version in Nix, got $nix_node_rev want $want_rev" >&2
  30. exit 1
  31. fi
  32. ln -sf "$nix_node" "$cachedir"
  33. else
  34. # works for "linux" and "darwin"
  35. OS=$(uname -s | tr A-Z a-z)
  36. ARCH=$(uname -m)
  37. if [ "$ARCH" = "x86_64" ]; then
  38. ARCH="x64"
  39. fi
  40. if [ "$ARCH" = "aarch64" ]; then
  41. ARCH="arm64"
  42. fi
  43. mkdir -p "$cachedir"
  44. # When running on GitHub in CI, the below curl sometimes fails with
  45. # INTERNAL_ERROR after finishing the download. The most common cause
  46. # of INTERNAL_ERROR is glitches in intermediate hosts handling of
  47. # HTTP/2 forwarding, so forcing HTTP 1.1 often fixes the issue. See
  48. # https://github.com/tailscale/tailscale/issues/8988
  49. curl -f -L --http1.1 -o "$tarball" "https://nodejs.org/dist/v${want_rev}/node-v${want_rev}-${OS}-${ARCH}.tar.gz"
  50. (cd "$cachedir" && tar --strip-components=1 -xf "$tarball")
  51. rm -f "$tarball"
  52. fi
  53. fi
  54. )
  55. export PATH="$HOME/.cache/tailscale-node/bin:$PATH"
  56. exec "$HOME/.cache/tailscale-node/bin/node" "$@"