Repository navigation
Expand file tree
/
Copy pathsourcemaps
More file actions
executable file
·105 lines (100 loc) · 4.75 KB
/
Copy pathsourcemaps
File metadata and controls
executable file
·105 lines (100 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
# sourcemaps — hand a built export's sourcemaps to cloud, privately, and take them
# out of the export before it is published.
#
# sourcemaps <dir> [base]
#
# <dir> is the export a site publishes (hanzo.yml `site.dir`), and [base] the path
# it is served under, `/` unless the site lives below one (hanzo.app/post is
# `/post`). Every `*.js.map` / `*.mjs.map` / `*.cjs.map` whose script is beside it
# is uploaded to PUT $HANZO_API/v1/event/sourcemaps under the release the bundle was
# stamped with, keyed by the path that script is served at — the same path a
# browser's stack frame names. Cloud maps that release's error frames back to the
# original source when it ingests them, before it groups them, so one fault keeps
# one issue across deploys.
#
# THEN EVERY .map IS REMOVED FROM THE EXPORT, uploaded or not, and the
# `sourceMappingURL` comment that pointed at it is dropped. A map is the site's
# source; it is kept in the org's storage, where nothing serves it, and never on
# the edge. Removal does not depend on the upload succeeding: a site whose upload
# failed ships unreadable stacks for one release, never its source.
#
# NO SECRET OF ITS OWN. The bearer is HANZO_API_TOKEN, the org identity the run
# already minted (on GitHub, the run's ID token exchanged for the org's CI
# application; on Hanzo Git, the org's client). Cloud admits exactly that kind of
# credential and files the maps under its org.
#
# An upload that fails is a ::warning:: and the publish goes on: symbolication is
# worth a warning, not a site that stops updating.
#
# env: HANZO_API (default https://api.hanzo.ai), HANZO_API_TOKEN, HANZO_RELEASE
# (else GITHUB_SHA). SOURCEMAPS_PLAN=1 prints each PUT instead of sending it.
set -euo pipefail
dir=${1:-}
base=${2:-/}
[ -n "$dir" ] || { echo "usage: sourcemaps <dir> [base]" >&2; exit 2; }
[ -d "$dir" ] || { echo "::error::sourcemaps: $dir is not a directory" >&2; exit 1; }
case "$base" in /*) ;; *) echo "::error::sourcemaps: base '$base' must start with /" >&2; exit 1 ;; esac
base=${base%/}
dir=${dir%/}
api=${HANZO_API:-https://api.hanzo.ai}
release=${HANZO_RELEASE:-${GITHUB_SHA:-}}
plan=${SOURCEMAPS_PLAN:-}
enc() { jq -rn --arg v "$1" '$v|@uri'; }
maps=()
while IFS= read -r -d '' m; do maps+=("$m"); done < <(find "$dir" -type f -name '*.map' -print0 | sort -z)
if [ "${#maps[@]}" = 0 ]; then
echo "::notice::sourcemaps: no .map files under $dir"
exit 0
fi
upload=1
if [ -z "$release" ]; then
echo "::warning::sourcemaps: no release (HANZO_RELEASE / GITHUB_SHA) to file the maps under; removing them from $dir without uploading"
upload=0
elif [ -z "$plan" ] && [ -z "${HANZO_API_TOKEN:-}" ]; then
echo "::warning::sourcemaps: no HANZO_API_TOKEN (the run's org identity was not minted); removing the maps from $dir without uploading"
upload=0
fi
sent=0 failed=0 skipped=0
out=$(mktemp); trap 'rm -f "$out"' EXIT
for m in "${maps[@]}"; do
gen=${m%.map}
case "$gen" in
*.js|*.mjs|*.cjs)
if [ "$upload" = 1 ] && [ -f "$gen" ]; then
file="$base/${gen#"$dir"/}"
url="$api/v1/event/sourcemaps?release=$(enc "$release")&file=$(enc "$file")"
if [ -n "$plan" ]; then
printf 'PUT %s <- %s\n' "$url" "${m#"$dir"/}"
sent=$((sent + 1))
else
code=$(curl -sS -o "$out" -w '%{http_code}' --max-time 120 --retry 2 --retry-delay 3 \
-X PUT -H "Authorization: Bearer $HANZO_API_TOKEN" -H 'Content-Type: application/json' \
--data-binary "@$m" "$url" 2>/dev/null) || code=000
if [ "$code" = 201 ]; then
sent=$((sent + 1))
else
failed=$((failed + 1))
echo "::warning::sourcemaps: $file was not uploaded (HTTP $code): $(head -c 300 "$out" 2>/dev/null)"
fi
fi
else
skipped=$((skipped + 1))
fi
;;
esac
rm -f "$m"
# The comment that names the map goes with it. It is the file's last line in
# every bundler's output, so dropping it moves no position a map describes.
if [ -f "$gen" ]; then
name=$(basename "$m")
case "$gen" in
*.js|*.mjs|*.cjs) NAME="$name" perl -0777 -pi -e 's{\n?//[#@] sourceMappingURL=\Q$ENV{NAME}\E[ \t]*\n?\z}{\n}' "$gen" ;;
*.css) NAME="$name" perl -0777 -pi -e 's{\n?/\*[#@] sourceMappingURL=\Q$ENV{NAME}\E[ \t]*\*/[ \t]*\n?\z}{\n}' "$gen" ;;
esac
fi
done
if grep -rlE 'sourceMappingURL=data:' "$dir" --include='*.js' --include='*.mjs' --include='*.cjs' >/dev/null 2>&1; then
echo "::warning::sourcemaps: $dir carries INLINE sourcemaps (sourceMappingURL=data:), which publish the source with the script. Build with external maps (Vite build.sourcemap: 'hidden' or true) so they can be kept private."
fi
echo "sourcemaps: $sent uploaded, $failed failed, $skipped not uploaded, ${#maps[@]} removed from $dir (release ${release:-none})"