main
ply4 git calls27 ms43 KiB2 cachedprotocol v2
141 lines6.1 KiBRaw
1import { spawnSync } from 'node:child_process'
2import { readFileSync } from 'node:fs'
3import { delimiter, dirname } from 'node:path'
4import { fileURLToPath } from 'node:url'
5import { NODE_PROBE_MS } from './baseline.ts'
6import { run } from './proc.ts'
8const SHA = /^[0-9a-f]{40}$/
9const PACKAGE = fileURLToPath(new URL('../../package.json', import.meta.url))
11/**
12 * Node shipped on the operator image next to a default that is older than `engines`.
13 * `npm run cd` uses this binary when `node` on `PATH` does not satisfy `package.json`.
14 */
15export const NODE_IMAGE_BIN = '/workspace/node-v24.22.1-linux-x64/bin/node'
17type Semver = { readonly major: number; readonly minor: number; readonly patch: number }
19export function enginesRange(): string {
20 const pkg = JSON.parse(readFileSync(PACKAGE, 'utf8')) as { engines?: { node?: string } }
21 const node = pkg.engines?.node
22 if (!node) throw new Error('package.json engines.node is missing')
23 return node
26function semver(version: string): Semver | null {
27 const match = /^v?(\d+)\.(\d+)\.(\d+)/.exec(version)
28 if (!match) return null
29 return { major: Number(match[1]), minor: Number(match[2]), patch: Number(match[3]) }
32function compare(left: Semver, right: Semver): number {
33 if (left.major !== right.major) return left.major - right.major
34 if (left.minor !== right.minor) return left.minor - right.minor
35 return left.patch - right.patch
38/** `package.json` `engines.node`: `^X.Y.Z` or `>=X.Y.Z`, joined by `||`. */
39export function nodeSatisfies(version: string, engines: string = enginesRange()): boolean {
40 const got = semver(version)
41 if (!got) return false
42 for (const part of engines.split('||').map(item => item.trim()).filter(Boolean)) {
43 const caret = /^\^(\d+)\.(\d+)\.(\d+)$/.exec(part)
44 if (caret) {
45 const min = { major: Number(caret[1]), minor: Number(caret[2]), patch: Number(caret[3]) }
46 if (got.major === min.major && compare(got, min) >= 0) return true
47 continue
48 }
49 const floor = /^>=(\d+)\.(\d+)\.(\d+)$/.exec(part)
50 if (floor) {
51 const min = { major: Number(floor[1]), minor: Number(floor[2]), patch: Number(floor[3]) }
52 if (compare(got, min) >= 0) return true
53 }
54 }
55 return false
58export function checkNode(version: string, engines: string = enginesRange()): string | null {
59 if (!semver(version)) return `Node ${version} is not a recognized version`
60 if (nodeSatisfies(version, engines)) return null
61 return [
62 `Node ${version} does not satisfy package.json engines (${engines}).`,
63 `The operator image keeps a matching build at ${NODE_IMAGE_BIN}.`,
64 '.nvmrc asks for 24.22.1.',
65 'Set PLY_NODE to a binary that satisfies engines, or run npm run cd so it selects one.',
66 ].join(' ')
69export function binaryNodeVersion(bin: string): string | null {
70 const result = spawnSync(bin, ['-p', 'process.version'], { encoding: 'utf8', timeout: NODE_PROBE_MS })
71 if (result.status !== 0) return null
72 const line = result.stdout.trim()
73 return line.startsWith('v') ? line : null
76/** PATH with `nodeBin`'s directory first, so a child `cf` resolves the same Node. */
77export function pathWithNodeBin(nodeBin: string, path: string | undefined): string {
78 const dir = dirname(nodeBin)
79 if (!path) return dir
80 return `${dir}${delimiter}${path}`
83/** A different binary when the current one misses engines. Null when the current one is fine or none of the candidates is. */
84export function selectNode(
85 current: string,
86 candidates: readonly { readonly bin: string; readonly version: string | null }[],
87 engines: string = enginesRange(),
88): string | null {
89 if (nodeSatisfies(current, engines)) return null
90 for (const candidate of candidates) {
91 if (candidate.version && nodeSatisfies(candidate.version, engines)) return candidate.bin
92 }
93 return null
96export type OriginMain =
97 | { readonly ok: true; readonly sha: string }
98 | { readonly ok: false; readonly kind: 'not-on-origin-main'; readonly head: string; readonly main: string }
99 | { readonly ok: false; readonly kind: 'origin-main-unavailable'; readonly reason: string }
101/** A deploy is allowed only when HEAD and origin/main are the same commit. */
102export function judgeOriginMain(head: string, main: string): OriginMain {
103 if (!SHA.test(head) || !SHA.test(main)) {
104 return { ok: false, kind: 'origin-main-unavailable', reason: 'HEAD or origin/main is not a commit sha' }
105 }
106 if (head !== main) return { ok: false, kind: 'not-on-origin-main', head, main }
107 return { ok: true, sha: head }
110export function originMainReason(result: Exclude<OriginMain, { ok: true }>): string {
111 if (result.kind === 'not-on-origin-main') {
112 return `not-on-origin-main: HEAD ${result.head} is not origin/main ${result.main}`
113 }
114 return `origin-main-unavailable: ${result.reason}`
117/** Fetch `origin main` and return that commit. Does not look at HEAD. */
118export async function fetchOriginMain(cwd: string): Promise<
119 | { readonly ok: true; readonly sha: string }
120 | { readonly ok: false; readonly kind: 'origin-main-unavailable'; readonly reason: string }
121> {
122 const fetched = await run('git', ['fetch', 'origin', 'main'], { cwd, timeout: 60_000 })
123 if (fetched.code !== 0) {
124 const detail = (fetched.stderr || fetched.stdout).trim().slice(0, 300)
125 return { ok: false, kind: 'origin-main-unavailable', reason: detail || `git fetch origin main exited ${fetched.code}` }
126 }
127 const main = await run('git', ['rev-parse', 'origin/main'], { cwd })
128 if (main.code !== 0) return { ok: false, kind: 'origin-main-unavailable', reason: 'git rev-parse origin/main failed' }
129 const sha = main.stdout.trim()
130 if (!SHA.test(sha)) return { ok: false, kind: 'origin-main-unavailable', reason: 'origin/main is not a commit sha' }
131 return { ok: true, sha }
134/** Fetch origin/main, then refuse unless HEAD is that commit. */
135export async function requireOriginMain(cwd: string): Promise<OriginMain> {
136 const main = await fetchOriginMain(cwd)
137 if (!main.ok) return main
138 const head = await run('git', ['rev-parse', 'HEAD'], { cwd })
139 if (head.code !== 0) return { ok: false, kind: 'origin-main-unavailable', reason: 'git rev-parse HEAD failed' }
140 return judgeOriginMain(head.stdout.trim(), main.sha)
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200145 B2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=145
git.response.cache=UPDATING
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=76
git.ls_refs.count=2
git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit8 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=8
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=6
git.pkt.data=4
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2002.1 KiB · hit17 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=17
git.response.bytes=2174
git.response.cache=HIT
git.response.duration_ms=17
git.fetch.wants=7fade7dae3625d9796b263337b3dc9803ed2a6c7
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2174
git.pkt.payload.max=2153
git.sideband.band1.bytes=2152
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2d8f0ffe114c2
Request
2ac5479e…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format