Skip to content

Commit fe4535a

Browse files
opencode-agent[bot]Brendan
andauthored
fix(release): publish blockmaps for signed desktop installers (anomalyco#66042)
Co-authored-by: Brendan <brendan@anoma.ly>
1 parent eec153e commit fe4535a

3 files changed

Lines changed: 239 additions & 1 deletion

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# backfill-blockmaps.sh <version>...: builds the .blockmap for each published Windows installer and macOS zip of
3+
# the given stable versions from the signed files on opencode.ai/files and uploads it next to them. Releases before
4+
# publish-desktop.sh built blockmaps shipped without them, so electron-updater could not update differentially
5+
# from them. A blockmap that already exists is left alone; a file the version never shipped is skipped.
6+
. "$(dirname "$0")/lib.sh"
7+
8+
actor=${GITHUB_TRIGGERING_ACTOR:-${GITHUB_ACTOR:-}}
9+
if ! (shopt -s nocasematch; [[ "$actor" = thdxr || "$actor" = Hona || "$actor" = rekram1-node ]]); then
10+
die "${actor:-An unknown actor} is not allowed to backfill release files"
11+
fi
12+
[ $# -gt 0 ] || die "No versions given"
13+
for version in "$@"; do
14+
[[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]] && [[ "$version" != 0.0.0-* ]] ||
15+
die "Invalid release version: $version"
16+
done
17+
18+
names=(opencode-desktop-win-arm64.exe opencode-desktop-win-x64.exe opencode-desktop-mac-arm64.zip opencode-desktop-mac-x64.zip)
19+
status() {
20+
curl -sS -o /dev/null -w '%{http_code}' --head "$1"
21+
}
22+
23+
credentials=false
24+
for version in "$@"; do
25+
dir=$(mktemp -d)
26+
for name in "${names[@]}"; do
27+
url="https://opencode.ai/files/bin/$version/$name"
28+
case "$(status "$url.blockmap")" in
29+
200) echo "$version/$name.blockmap exists" && continue ;;
30+
404) ;;
31+
*) die "Could not check $url.blockmap" ;;
32+
esac
33+
case "$(status "$url")" in
34+
200) ;;
35+
404) echo "$version/$name was not published" && continue ;;
36+
*) die "Could not check $url" ;;
37+
esac
38+
curl -fsSL --retry 3 -o "$dir/$name" "$url"
39+
node "$release_dir/blockmap.mjs" "$dir/$name"
40+
if dry_run; then
41+
echo "dry-run upload: bin/$version/$name.blockmap ($(size "$dir/$name.blockmap") bytes)"
42+
continue
43+
fi
44+
if [ "$credentials" = false ]; then
45+
r2_credentials
46+
credentials=true
47+
fi
48+
OPENCODE_VERSION=$version upload_file "$dir/$name.blockmap" "$name.blockmap" application/octet-stream ||
49+
die "Upload failed: $version/$name.blockmap"
50+
echo "uploaded bin/$version/$name.blockmap"
51+
done
52+
rm -rf "$dir"
53+
done

‎.github/release/blockmap.mjs‎

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// blockmap.mjs <file>...: writes <file>.blockmap next to each file, the gzipped block map electron-updater fetches
2+
// for a differential download. It matches electron-builder's buildBlockMap (app-builder-lib 26): Rabin
3+
// content-defined chunks of 8-32 KB and a 18-byte BLAKE2b checksum per chunk. electron-builder builds it at pack
4+
// time, but signing changes the installer, so the release builds it here from the final signed files.
5+
//
6+
// It runs in publish jobs that hold secrets, so it uses Node's standard library only.
7+
import { createReadStream, writeFileSync } from "node:fs"
8+
import { gzipSync } from "node:zlib"
9+
10+
const MIN = 8192
11+
const MAX = 32768
12+
const MASK = 16384 - 1
13+
const WINDOW = 64
14+
15+
// Rabin fingerprint tables for the degree-63 polynomial 0xbfe6b8a5bf378d83 (github.com/aclements/go-rabin), each
16+
// 64-bit entry split into signed 32-bit hi and lo words.
17+
const POLY = 0xbfe6b8a5bf378d83n
18+
const PUSH_HI = new Int32Array(256)
19+
const PUSH_LO = new Int32Array(256)
20+
const POP_HI = new Int32Array(256)
21+
const POP_LO = new Int32Array(256)
22+
{
23+
const mod = (a) => {
24+
for (let d = a.toString(2).length - 1; a !== 0n && d >= 63; d = a.toString(2).length - 1)
25+
a ^= POLY << BigInt(d - 63)
26+
return a
27+
}
28+
const set = (hi, lo, i, x) => {
29+
hi[i] = Number(BigInt.asIntN(32, x >> 32n))
30+
lo[i] = Number(BigInt.asIntN(32, x))
31+
}
32+
for (let i = 0; i < 256; i++) {
33+
const b = BigInt(i)
34+
set(PUSH_HI, PUSH_LO, i, BigInt.asUintN(64, (b << 63n) ^ mod(b << 63n)))
35+
set(POP_HI, POP_LO, i, mod(b << BigInt((WINDOW - 1) * 8)))
36+
}
37+
}
38+
39+
// BLAKE2b (RFC 7693) with an 18-byte digest and no key, on 32-bit word pairs. Node's crypto offers only BLAKE2b-512,
40+
// whose digest length is part of the parameter block, so truncating it gives a different hash.
41+
const IV = new Uint32Array([
42+
0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a, 0xade682d1,
43+
0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
44+
])
45+
const SIGMA = [
46+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
47+
[14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3],
48+
[11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4],
49+
[7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8],
50+
[9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13],
51+
[2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9],
52+
[12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11],
53+
[13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10],
54+
[6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5],
55+
[10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0],
56+
].map((row) => row.map((i) => i * 2))
57+
const ROUNDS = [...SIGMA, SIGMA[0], SIGMA[1]]
58+
const DIGEST = 18
59+
const v = new Uint32Array(32)
60+
const m = new Uint32Array(32)
61+
const h = new Uint32Array(16)
62+
const block = new Uint8Array(128)
63+
const view = new DataView(block.buffer)
64+
65+
function add(a, b0, b1) {
66+
const lo = v[a] + b0
67+
v[a + 1] = v[a + 1] + b1 + (lo >= 0x100000000 ? 1 : 0)
68+
v[a] = lo
69+
}
70+
71+
function mix(a, b, c, d, x, y) {
72+
add(a, v[b], v[b + 1])
73+
add(a, m[x], m[x + 1])
74+
let lo = v[d] ^ v[a]
75+
let hi = v[d + 1] ^ v[a + 1]
76+
v[d] = hi
77+
v[d + 1] = lo
78+
add(c, v[d], v[d + 1])
79+
lo = v[b] ^ v[c]
80+
hi = v[b + 1] ^ v[c + 1]
81+
v[b] = (lo >>> 24) ^ (hi << 8)
82+
v[b + 1] = (hi >>> 24) ^ (lo << 8)
83+
add(a, v[b], v[b + 1])
84+
add(a, m[y], m[y + 1])
85+
lo = v[d] ^ v[a]
86+
hi = v[d + 1] ^ v[a + 1]
87+
v[d] = (lo >>> 16) ^ (hi << 16)
88+
v[d + 1] = (hi >>> 16) ^ (lo << 16)
89+
add(c, v[d], v[d + 1])
90+
lo = v[b] ^ v[c]
91+
hi = v[b + 1] ^ v[c + 1]
92+
v[b] = (hi >>> 31) ^ (lo << 1)
93+
v[b + 1] = (lo >>> 31) ^ (hi << 1)
94+
}
95+
96+
function compress(count, last) {
97+
for (let i = 0; i < 16; i++) {
98+
v[i] = h[i]
99+
v[i + 16] = IV[i]
100+
}
101+
v[24] ^= count
102+
v[25] ^= Math.floor(count / 0x100000000)
103+
if (last) {
104+
v[28] = ~v[28]
105+
v[29] = ~v[29]
106+
}
107+
for (let i = 0; i < 32; i++) m[i] = view.getUint32(i * 4, true)
108+
for (const s of ROUNDS) {
109+
mix(0, 8, 16, 24, s[0], s[1])
110+
mix(2, 10, 18, 26, s[2], s[3])
111+
mix(4, 12, 20, 28, s[4], s[5])
112+
mix(6, 14, 22, 30, s[6], s[7])
113+
mix(0, 10, 20, 30, s[8], s[9])
114+
mix(2, 12, 22, 24, s[10], s[11])
115+
mix(4, 14, 16, 26, s[12], s[13])
116+
mix(6, 8, 18, 28, s[14], s[15])
117+
}
118+
for (let i = 0; i < 16; i++) h[i] ^= v[i] ^ v[i + 16]
119+
}
120+
121+
function blake2b(data) {
122+
h.set(IV)
123+
h[0] ^= 0x01010000 ^ DIGEST
124+
let offset = 0
125+
while (data.length - offset > 128) {
126+
block.set(data.subarray(offset, offset + 128))
127+
offset += 128
128+
compress(offset, false)
129+
}
130+
block.fill(0)
131+
block.set(data.subarray(offset))
132+
compress(data.length, true)
133+
return Buffer.from(h.buffer, h.byteOffset, DIGEST).toString("base64")
134+
}
135+
136+
async function blockmap(file) {
137+
const checksums = []
138+
const sizes = []
139+
const chunk = Buffer.allocUnsafe(MAX)
140+
const win = new Uint8Array(WINDOW)
141+
let n = 0
142+
let hi = 0
143+
let lo = 0
144+
let pos = 0
145+
146+
const emit = () => {
147+
checksums.push(blake2b(chunk.subarray(0, n)))
148+
sizes.push(n)
149+
n = hi = lo = pos = 0
150+
win.fill(0)
151+
}
152+
153+
for await (const data of createReadStream(file, { highWaterMark: 256 * 1024 })) {
154+
for (let i = 0; i < data.length; i++) {
155+
const b = data[i]
156+
chunk[n++] = b
157+
if (n <= MIN - WINDOW) continue
158+
if (n > MIN) {
159+
const old = win[pos]
160+
hi ^= POP_HI[old]
161+
lo ^= POP_LO[old]
162+
}
163+
win[pos] = b
164+
pos = (pos + 1) % WINDOW
165+
const top = (hi >>> 23) & 0xff
166+
const nhi = ((hi << 8) | (lo >>> 24)) & 0x7fffffff
167+
const nlo = (lo << 8) | b
168+
hi = nhi ^ PUSH_HI[top]
169+
lo = nlo ^ PUSH_LO[top]
170+
if (n >= MIN && ((lo & MASK) === MASK || n >= MAX)) emit()
171+
}
172+
}
173+
if (n > 0) emit()
174+
175+
const map = { version: "2", files: [{ name: "file", offset: 0, checksums, sizes }] }
176+
writeFileSync(`${file}.blockmap`, gzipSync(Buffer.from(JSON.stringify(map)), { level: 9 }))
177+
}
178+
179+
for (const file of process.argv.slice(2)) await blockmap(file)

‎.github/release/publish-desktop.sh‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ feed() {
2424
done | jq -s --arg date "$(date -u +%Y-%m-%dT%H:%M:%S.000Z)" '{files: ., releaseDate: $date}'
2525
}
2626

27-
files=$(upload_files "${win[@]}" "${mac[@]}" "${linux[@]}" "${linux_arm[@]}" "${bundles[@]}")
27+
# electron-updater fetches <file>.blockmap next to the Windows installer and the macOS zip for a differential
28+
# download. Signing changes these files, so the blockmaps electron-builder made at pack time no longer match.
29+
differential=("${win[@]}" "$macos/opencode-desktop-mac-arm64.zip" "$macos/opencode-desktop-mac-x64.zip")
30+
node "$release_dir/blockmap.mjs" "${differential[@]}"
31+
blockmaps=("${differential[@]/%/.blockmap}")
32+
33+
files=$(upload_files "${win[@]}" "${mac[@]}" "${linux[@]}" "${linux_arm[@]}" "${bundles[@]}" "${blockmaps[@]}")
2834
manifests=$(jq -n --argjson win "$(feed "${win[@]}")" --argjson mac "$(feed "${mac[@]}")" \
2935
--argjson linux "$(feed "${linux[@]}")" --argjson linux_arm64 "$(feed "${linux_arm[@]}")" \
3036
'{"desktop.yml": $win, "desktop-mac.yml": $mac, "desktop-linux.yml": $linux, "desktop-linux-arm64.yml": $linux_arm64}')

0 commit comments

Comments
 (0)