main
1import assert from 'node:assert/strict'
2import { test } from 'node:test'
3import {
4 CAS_AUTH_HEADER,
5 casPath,
6 encodeRefPath,
7 encodeResolveRest,
8 gitAuthorizationValue,
9 isBlobEntry,
10 layerHeader,
11 mergeRayHeaderValues,
12 parseCommitPath,
13 parseObjectPath,
14 parseRefPath,
15 parseResolvePath,
16 parseTreePath,
17 purgeRefTags,
18 REF_PATH_PREFIX,
19 serveObject,
20 serveRef,
21 splitFilePath,
22 viaCas,
23 type CasBinding,
24 type CasLayer,
25} from '../src/cas-cache.ts'
26import { CACHE_VERSION } from '../src/cache-version.ts'
27import { chainGet } from '../src/absent.ts'
29test('cas paths round-trip per layer', () => {
30 const oid = 'a'.repeat(40)
31 assert.equal(parseObjectPath(casPath('object', oid)), oid)
32 assert.equal(parseTreePath(casPath('tree', oid)), oid)
33 assert.equal(parseCommitPath(casPath('commit', oid)), oid)
34 assert.deepEqual(parseResolvePath(casPath('resolve', encodeResolveRest(oid, 'src/a.txt'))), { commit: oid, path: 'src/a.txt' })
35 assert.equal(parseRefPath(casPath('ref', encodeRefPath('refs/heads/main'))), 'refs/heads/main')
36 assert.equal(parseObjectPath(`/v${CACHE_VERSION + 1}/object/${oid}`), null)
37})
39test('splitFilePath and isBlobEntry', () => {
40 assert.deepEqual(splitFilePath('README.md'), { parent: '', name: 'README.md' })
41 assert.equal(isBlobEntry({ type: 'blob' }), true)
42 assert.equal(isBlobEntry({ type: 'tree' }), false)
43})
45test('gitAuthorizationValue', () => {
46 assert.equal(gitAuthorizationValue({ header: 'Bearer x' }), 'Bearer x')
47 assert.equal(gitAuthorizationValue({ bearer: 'tok' }), 'Bearer tok')
48})
50test('serveObject / serveRef: missing scope is no-store', async () => {
51 const oid = 'b'.repeat(40)
52 const bad = await serveObject(new Request(`https://cas.internal${casPath('object', oid)}`), { scope: '' }, null)
53 assert.equal(bad.status, 400)
54 assert.equal(bad.headers.get('cache-control'), 'no-store')
55 const refBad = await serveRef(new Request(`https://cas.internal${casPath('ref', encodeRefPath('HEAD'))}`), { scope: '' }, null)
56 assert.equal(refBad.status, 400)
57 assert.equal(refBad.headers.get('cache-control'), 'no-store')
58})
60function memoryTier(): { binding: (layer: CasLayer) => CasBinding; fills: Map<string, number> } {
61 const store = new Map<string, { status: number; headers: Headers; body: Uint8Array }>()
62 const fills = new Map<string, number>()
63 function binding(layer: CasLayer): CasBinding {
64 return (opts) => ({
65 async fetch(input) {
66 const req = input instanceof Request ? input : new Request(String(input))
67 assert.equal(req.headers.get('authorization'), null)
68 const props = opts.props
69 const key = `${layer}\0${new URL(req.url).pathname}\0${props.scope}`
70 const hit = store.get(key)
71 if (hit) {
72 const h = new Headers(hit.headers)
73 h.set('cf-cache-status', 'HIT')
74 return new Response(hit.body.slice(), { status: hit.status, headers: h })
75 }
76 const prev = fills.get(layer)
77 fills.set(layer, (prev === void 0 ? 0 : prev) + 1)
78 const body = new TextEncoder().encode(JSON.stringify({ layer, scope: props.scope, path: new URL(req.url).pathname }))
79 const headers = new Headers({
80 'content-type': 'application/json',
81 'cache-control': 'public, max-age=60',
82 'x-ply-size': String(body.byteLength),
83 })
84 store.set(key, { status: 200, headers: new Headers(headers), body })
85 headers.set('cf-cache-status', 'MISS')
86 return new Response(body.slice(), { status: 200, headers })
87 },
88 })
89 }
90 return { binding, fills }
91}
93test('cross-scope isolation at every layer', async () => {
94 const { binding, fills } = memoryTier()
95 const oid = 'c'.repeat(40)
96 const layers: { layer: CasLayer; rest: string }[] = [
97 { layer: 'object', rest: oid },
98 { layer: 'tree', rest: oid },
99 { layer: 'commit', rest: oid },
100 { layer: 'resolve', rest: encodeResolveRest(oid, 'README.md') },
101 { layer: 'ref', rest: encodeRefPath('HEAD') },
102 ]
103 const scopeA = 'https://ex.example/git/ns/a.git'
104 const scopeB = 'https://ex.example/git/ns/b.git'
105 for (const { layer, rest } of layers) {
106 const cold = await viaCas(binding(layer), layer, rest, scopeA, 'Bearer a')
107 assert.equal(cold.headers.get(layerHeader(layer)), 'MISS')
108 const warm = await viaCas(binding(layer), layer, rest, scopeA, 'Bearer a')
109 assert.equal(warm.headers.get(layerHeader(layer)), 'HIT')
110 const other = await viaCas(binding(layer), layer, rest, scopeB, 'Bearer b')
111 assert.equal(other.headers.get(layerHeader(layer)), 'MISS', `${layer} must not share across scopes`)
112 const warmBody = await warm.text()
113 const bBody = await other.text()
114 assert.notEqual(warmBody, bBody)
115 assert.match(warmBody, /\/a\.git/)
116 assert.match(bBody, /\/b\.git/)
117 const coldBody = cold.body
118 if (coldBody !== null) await coldBody.cancel().catch(() => {})
119 }
120 assert.equal(fills.get('object'), 2)
121 assert.equal(fills.get('ref'), 2)
122})
124test('viaCas strips Authorization and surfaces cf-cache-status', async () => {
125 const seen: string[] = []
126 const binding: CasBinding = (opts) => ({
127 async fetch(input) {
128 const req = input instanceof Request ? input : new Request(String(input))
129 const auth = req.headers.get(CAS_AUTH_HEADER)
130 seen.push(auth === null ? '' : auth)
131 assert.equal(req.headers.get('authorization'), null)
132 assert.deepEqual(opts.props, { scope: 'https://ex.example/r.git' })
133 return new Response('x', { status: 200, headers: { 'cf-cache-status': 'HIT', 'x-ply-size': '1' } })
134 },
135 })
136 const res = await viaCas(binding, 'object', 'd'.repeat(40), 'https://ex.example/r.git', 'Bearer secret')
137 assert.equal(res.headers.get(layerHeader('object')), 'HIT')
138 assert.equal(res.headers.get('cf-cache-status'), null)
139 assert.equal(seen[0], 'Bearer secret')
140 const body = chainGet(res, 'body')
141 if (body.tag === 'some' && body.value !== null) await (body.value as ReadableStream).cancel()
142})
144import { RAY_HEADER } from '../src/rays.ts'
146test('mergeRayHeaderValues concatenates fill rays in order', () => {
147 assert.equal(mergeRayHeaderValues(null, ''), '')
148 assert.equal(
149 mergeRayHeaderValues('ls-refs=aaaaaaaaaaaaaaaa-IAD;dur=1', 'fetch=bbbbbbbbbbbbbbbb-IAD;dur=2'),
150 'ls-refs=aaaaaaaaaaaaaaaa-IAD;dur=1, fetch=bbbbbbbbbbbbbbbb-IAD;dur=2',
151 )
152 assert.equal(
153 mergeRayHeaderValues('ls-refs=-;dur=1, fetch=-;dur=2', 'ls-refs=-;dur=3'),
154 'ls-refs=-;dur=1, fetch=-;dur=2, ls-refs=-;dur=3',
155 )
156})
158test('purgeRefTags uses purgeEverything on RefCache (entrypoint-scoped)', async () => {
159 const calls: unknown[] = []
160 const cache = {
161 purge: async function (this: unknown, opts: unknown) {
162 // Workers Cache throws Illegal invocation when purge is unbound (CD 2026-11-08).
163 if (this !== cache) throw new TypeError('Illegal invocation')
164 calls.push(opts)
165 return { success: true }
166 },
167 }
168 const out = await purgeRefTags({ cache }, ['ply-cas-ref'])
169 assert.equal(out.ok, true)
170 assert.deepEqual(calls, [{ purgeEverything: true }])
171 assert.equal(REF_PATH_PREFIX, `/v${CACHE_VERSION}/ref/`)
172})
174test('purgeRefTags fails when purge is extracted unbound (Illegal invocation)', async () => {
175 const cache = {
176 purge: async function (this: unknown, _opts: unknown) {
177 if (this !== cache) throw new TypeError('Illegal invocation')
178 return { success: true }
179 },
180 }
181 // Simulate the old bug: extract then call unbound.
182 const run = cache.purge
183 await assert.rejects(async () => run({ purgeEverything: true }), /Illegal invocation/)
184})
186test('purgeRefTags reports missing ctx.cache (RefCache live / uncached)', async () => {
187 const out = await purgeRefTags({}, ['ply-cas-ref'])
188 assert.equal(out.ok, false)
189 assert.equal(out.error, 'no ctx.cache')
190})
192test('purgeRefTags surfaces success:false from Workers Cache', async () => {
193 const ctx = {
194 cache: {
195 purge: async () => ({ success: false, errors: [{ code: 1, message: 'rate limited' }] }),
196 },
197 }
198 const out = await purgeRefTags(ctx, ['ply-cas-ref'])
199 assert.equal(out.ok, false)
200 assert.match(out.error, /rate limited/)
201})
203test('HIT preserves stored fill x-artifacts-ray (memory tier)', async () => {
204 const store = new Map<string, { status: number; headers: Headers; body: Uint8Array }>()
205 const binding: CasBinding = () => ({
206 async fetch(input) {
207 const req = input instanceof Request ? input : new Request(String(input))
208 const key = new URL(req.url).pathname
209 const hit = store.get(key)
210 if (hit) {
211 const h = new Headers(hit.headers)
212 h.set('cf-cache-status', 'HIT')
213 return new Response(hit.body.slice(), { status: hit.status, headers: h })
214 }
215 const body = new TextEncoder().encode(JSON.stringify({ commit: 'c'.repeat(40) }))
216 const headers = new Headers({
217 'content-type': 'application/json',
218 'cache-control': 'public, max-age=60',
219 [RAY_HEADER]: 'ls-refs=cccccccccccccccc-IAD;dur=9, fetch=dddddddddddddddd-IAD;dur=11',
220 })
221 store.set(key, { status: 200, headers: new Headers(headers), body })
222 headers.set('cf-cache-status', 'MISS')
223 return new Response(body.slice(), { status: 200, headers })
224 },
225 })
226 const miss = await viaCas(binding, 'commit', 'c'.repeat(40), 'https://ex.example/a.git', null)
227 assert.equal(miss.headers.get('x-ply-cache-commit'), 'MISS')
228 assert.equal(miss.headers.get(RAY_HEADER), 'ls-refs=cccccccccccccccc-IAD;dur=9, fetch=dddddddddddddddd-IAD;dur=11')
229 await miss.arrayBuffer()
230 const hit = await viaCas(binding, 'commit', 'c'.repeat(40), 'https://ex.example/a.git', null)
231 assert.equal(hit.headers.get('x-ply-cache-commit'), 'HIT')
232 assert.equal(hit.headers.get(RAY_HEADER), 'ls-refs=cccccccccccccccc-IAD;dur=9, fetch=dddddddddddddddd-IAD;dur=11')
233 await hit.arrayBuffer()
234})
Diagnostics4 git calls · 177 ms
CallStatusSizeTime0177 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=2 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=61Open pack map
fetch2002.7 KiB172 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=172 git.response.bytes=2769 git.response.cache=MISS git.response.duration_ms=172 git.fetch.wants=c6e04e9fa280a9501de4c8734b8d74c4645d8125 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=2769 git.pkt.payload.max=2748 git.sideband.band1.bytes=2747 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
- a5e2d5be9c19689d
- Request
- a65bc1dd…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format