GitHubivarvong
ply
main
1// HttpOptions are parsed once (#156): parseHttpOptions makes every check the transport makes of its options, and the
2// transport reads only the settings. The limits record is parsed there (parseLimits): a bad key, among them the two
3// it reads on every request (requestTimeoutMs, maxPackBytes), is refused when the transport is made, with no request,
4// and is the same bad_request a request used to fail with. Every refusal keeps its order (the limits come last; among
5// the limits, DEFAULT_LIMITS order). A key left out is the default.
6import { test } from 'vitest';
7import assert from 'node:assert/strict';
8import { createTransport, parseHttpOptions } from '../src/http.js';
9import { GitHttp } from '../src/client.js';
10import { readFile } from '../src/read.js';
11import { DEFAULT_LIMITS, withLimits, parseLimits } from '../src/limits.js';
12import { unwrap } from './unwrap.js';
14const counting = () => { const c = { n: 0 }; c.fetchImpl = async () => { c.n++; return new Response('', { status: 500 }); }; return c; };
16test('a bad request timeout or pack cap is refused when the transport is made, before any request', async () => {
17 for (const [key, value] of [['requestTimeoutMs', NaN], ['requestTimeoutMs', -1], ['maxPackBytes', Infinity], ['maxPackBytes', '1']]) {
18 const c = counting();
19 const r = createTransport('https://h/r.git', { fetchImpl: c.fetchImpl, limits: withLimits({ [key]: value }) });
20 assert.equal(r.ok, false, key);
21 assert.deepEqual([r.error.code, r.error.what, r.error.got], ['bad_request', key, value], key);
22 assert.equal(GitHttp.create('https://h/r.git', { limits: withLimits({ [key]: value }) }).ok, false);
23 // through an operation: the same refusal, still no request
24 const op = await readFile('https://h/r.git', { ref: 'refs/heads/main', path: 'a' }, { fetchImpl: c.fetchImpl, limits: withLimits({ [key]: value }) });
25 assert.deepEqual([op.ok, op.error?.code, op.error?.what], [false, 'bad_request', key]);
26 assert.equal(c.n, 0, key);
27 }
28 // a key given as null is given, and refused (got: null)
29 const partial = createTransport('https://h/r.git', { limits: { ...DEFAULT_LIMITS, requestTimeoutMs: null } });
30 assert.deepEqual([partial.error.code, partial.error.what, partial.error.got], ['bad_request', 'requestTimeoutMs', null]);
31 // a key left out is the default (#156: it used to be refused at its first read)
32 const { requestTimeoutMs: _left, ...rest } = DEFAULT_LIMITS;
33 const filled = parseHttpOptions('https://h/r.git', { limits: { ...rest, maxPackBytes: 7 } }).value;
34 assert.deepEqual([filled.requestTimeoutMs, filled.maxPackBytes, filled.limits.requestTimeoutMs], [DEFAULT_LIMITS.requestTimeoutMs, 7, DEFAULT_LIMITS.requestTimeoutMs]);
35 assert.deepEqual({ ...parseHttpOptions('https://h/r.git', { limits: {} }).value.limits }, { ...DEFAULT_LIMITS });
36});
38test('the limits come after every other refusal, as the first request used to', () => {
39 const bad = withLimits({ requestTimeoutMs: NaN });
40 assert.equal(createTransport('https://h/r', { followRedirects: 'always', limits: bad }).error.what, 'followRedirects');
41 assert.equal(createTransport('https://h/r', { userAgent: 'a\nb', limits: bad }).error.message.startsWith('invalid userAgent'), true);
42 assert.equal(createTransport('https://h/r', { recordResponseHeaders: ['a b'], limits: bad }).error.what, 'recordResponseHeaders');
43 assert.equal(createTransport('not a url', { limits: bad }).error.what, 'url');
44 assert.equal(createTransport('https://h/r', { auth: { user: 'u' }, limits: bad }).error.what, 'auth');
45 assert.equal(createTransport('https://h/r', { allowUrl: () => ({ ok: false, error: { code: 'bad_request', what: 'mine' } }), limits: bad }).error.what, 'mine');
46 // the first bad key in DEFAULT_LIMITS order: maxPackBytes before requestTimeoutMs
47 assert.equal(createTransport('https://h/r', { limits: withLimits({ requestTimeoutMs: NaN, maxPackBytes: NaN }) }).error.what, 'maxPackBytes');
48 assert.equal(createTransport('https://h/r', { limits: withLimits({ requestTimeoutMs: NaN, maxMergeBlobBytes: -1 }) }).error.what, 'maxMergeBlobBytes');
49});
51test('the settings: the URL without userinfo, the credential resolved once, the fetch bound, the stream redirect mode', () => {
52 const s = parseHttpOptions('https://tok@h.example/org/r.git#frag');
53 assert.equal(s.ok, true);
54 const v = s.value;
55 assert.equal(v.url.toString(), 'https://h.example/org/r.git');
56 assert.equal(v.authorization, `Basic ${btoa('tok:')}`);
57 assert.equal(v.crossOrigin, false);
58 assert.deepEqual(v.policy, { follow: 'never', max: 3 });
59 assert.equal(v.userAgent, 'ply/0.2.0');
60 assert.deepEqual([v.requestTimeoutMs, v.maxPackBytes], [DEFAULT_LIMITS.requestTimeoutMs, DEFAULT_LIMITS.maxPackBytes]);
61 assert.equal(v.streamRedirect, 'error', 'undici takes redirect error');
62 assert.notEqual(v.fetch, globalThis.fetch, 'the platform fetch is bound');
63 const own = async () => new Response('');
64 assert.equal(parseHttpOptions('https://h/r', { fetchImpl: own }).value.fetch, own);
65 const moved = parseHttpOptions('https://h/r', { allowUrl: u => ({ ok: true, value: new URL('https://other.example/r') }) }).value;
66 assert.equal(moved.crossOrigin, true);
67 const explicit = parseHttpOptions('https://h/r', { auth: { token: 't', user: 'me' } }).value;
68 assert.deepEqual([explicit.authorization, explicit.explicitBasic], [`Basic ${btoa('me:t')}`, { user: 'me', pass: 't' }]);
69});
71test('the transport reads the parsed timeout: the span carries it and a stalled request ends at it', async () => {
72 const spans = [];
73 const tracer = { span: async (_n, _o, fn) => fn({ set: a => spans.push(a), event() {}, error() {} }) };
74 const stall = (_u, init) => new Promise((_, reject) => init.signal.addEventListener('abort', () => reject(init.signal.reason)));
75 const t = createTransport('https://h/r', { fetchImpl: stall, limits: withLimits({ requestTimeoutMs: 50 }) }).value;
76 const t0 = Date.now();
77 await assert.rejects(async () => { for await (const _ of t.frames('git-upload-pack', new Uint8Array(0), { set: a => spans.push(a), event() {} }, { bypassCache: false })) { /* none */ } }, e => e.code === 'timeout');
78 assert.ok(Date.now() - t0 < 2000);
79 assert.ok(spans.some(a => a['git.limits.request_timeout_ms'] === 50));
80 void tracer;
81});
83import { streamRedirect } from '../src/http.js';
85test('streamRedirect: error where new Request takes it, manual where it throws (workerd) or reads back another mode', () => {
86 assert.equal(streamRedirect(), 'error');
87 const Real = globalThis.Request;
88 try {
89 globalThis.Request = class { constructor() { throw new TypeError('Invalid redirect value'); } };
90 assert.equal(streamRedirect(), 'manual');
91 globalThis.Request = class { constructor() { this.redirect = 'follow'; } };
92 assert.equal(streamRedirect(), 'manual');
93 } finally { globalThis.Request = Real; }
94});
96test('the settings carry the module brand (one symbol key, true), and the base drops one trailing slash', () => {
97 const v = parseHttpOptions('https://h/org/r.git/').value;
98 const keys = Object.getOwnPropertySymbols(v);
99 assert.equal(keys.length, 1);
100 assert.equal(v[keys[0]], true);
101 assert.equal(createTransport('https://h/org/r.git/').value.url, 'https://h/org/r.git');
102 assert.equal(createTransport('https://h/org/r.git').value.url, 'https://h/org/r.git');
103});
105// a fetch double: records each request's init and answers a git result (a flush) or an advertisement
106const recorder = (answer = () => new Response(new Uint8Array([0x30, 0x30, 0x30, 0x30]), { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-result' } })) => {
107 const seen = [];
108 const fetchImpl = async (url, init) => { seen.push({ url: String(url), init }); return answer(url, init); };
109 return { seen, fetchImpl };
110};
111const spanOf = () => { const attrs = {}; return { attrs, set: a => Object.assign(attrs, a), event() {}, error() {} }; };
112const drain = async (t, body, span = spanOf()) => { for await (const _ of t.frames('git-receive-pack', body, span, { bypassCache: false })) { /* frames */ } return span; };
114test('post: bytes go as a POST body with redirect manual; a stream goes with duplex half and the stream redirect mode', async () => {
115 const r = recorder();
116 const t = createTransport('https://h/r', { fetchImpl: r.fetchImpl }).value;
117 const bytes = Uint8Array.of(1, 2, 3);
118 const span = await drain(t, bytes);
119 const [a] = r.seen;
120 assert.equal(a.url, 'https://h/r/git-receive-pack');
121 assert.deepEqual([a.init.method, a.init.redirect, a.init.duplex, Buffer.from(a.init.body).toString('hex')], ['POST', 'manual', undefined, '010203']);
122 assert.deepEqual([span.attrs['git.transport'], span.attrs['http.request.method'], span.attrs['git.request.bytes'], span.attrs['http.request.body.size']], ['http', 'POST', 3, 3]);
123 const stream = new ReadableStream({ start(c) { c.enqueue(Uint8Array.of(9)); c.close(); } });
124 const span2 = await drain(t, { stream, length: 1 });
125 const b = r.seen[1];
126 // the body is the stream watched for upload progress (#255): a stream, never a buffer, reading the caller's bytes
127 assert.deepEqual([b.init.method, b.init.redirect, b.init.duplex, typeof b.init.body.getReader], ['POST', 'error', 'half', 'function']);
128 assert.equal(stream.locked, false, 'the caller stream is not locked until the platform pulls the body');
129 assert.deepEqual([...new Uint8Array(await new Response(b.init.body).arrayBuffer())], [9]);
130 assert.equal(span2.attrs['git.request.bytes'], 1);
131});
133// #232: git sends Git-Protocol only for upload-pack (remote-curl downgrades every other service to v0)
134test('Git-Protocol: version=2 goes on upload-pack GET and POST, and on neither receive-pack request', async () => {
135 const result = service => new Response(new Uint8Array([0x30, 0x30, 0x30, 0x30]), { status: 200, headers: { 'content-type': `application/x-${service}-result` } });
136 const advert = service => { const line = `# service=${service}\n`; return new Response(`${(line.length + 4).toString(16).padStart(4, '0')}${line}0000`, { status: 200, headers: { 'content-type': `application/x-${service}-advertisement` } }); };
137 const serviceOf = url => (String(url).includes('receive-pack') ? 'git-receive-pack' : 'git-upload-pack');
138 const r = recorder(url => (String(url).includes('info/refs') ? advert(serviceOf(url)) : result(serviceOf(url))));
139 const t = createTransport('https://h/r', { fetchImpl: r.fetchImpl }).value;
140 for (const service of ['git-receive-pack', 'git-upload-pack']) {
141 assert.equal((await t.advertise(service, spanOf())).ok, true);
142 for await (const _ of t.frames(service, Uint8Array.of(0), spanOf(), { bypassCache: false })) { /* frames */ }
143 }
144 const header = (url, method) => { const hit = r.seen.find(s => s.url.includes(url) && (s.init.method ?? 'GET') === method); assert.ok(hit, `${method} ${url}`); return new Headers(hit.init.headers).get('git-protocol'); };
145 assert.equal(header('info/refs?service=git-receive-pack', 'GET'), null);
146 assert.equal(header('/git-receive-pack', 'POST'), null);
147 assert.equal(header('info/refs?service=git-upload-pack', 'GET'), 'version=2');
148 assert.equal(header('/git-upload-pack', 'POST'), 'version=2');
149});
151test('a finished exchange clears its timer: the request signal does not fire after it (advertise and post)', async () => {
152 const adv = () => new Response('001e# service=git-upload-pack\n0000', { status: 200, headers: { 'content-type': 'application/x-git-upload-pack-advertisement' } });
153 const r = recorder((url) => (String(url).includes('info/refs') ? adv() : new Response(new Uint8Array([0x30, 0x30, 0x30, 0x30]), { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-result' } })));
154 const t = createTransport('https://h/r', { fetchImpl: r.fetchImpl, limits: withLimits({ requestTimeoutMs: 60 }) }).value;
155 assert.equal((await t.advertise('git-upload-pack', spanOf())).ok, true);
156 await drain(t, Uint8Array.of(0));
157 await new Promise(res => setTimeout(res, 150));
158 assert.deepEqual(r.seen.map(s => s.init.signal.aborted), [false, false]);
159});
161test('a body that fails mid-read is the network error it is', async () => {
162 const failing = () => new Response(new ReadableStream({ start(c) { c.enqueue(new TextEncoder().encode('0008abcd')); }, pull(c) { c.error(new TypeError('socket hang up')); } }), { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-result' } });
163 const t = createTransport('https://h/r', { fetchImpl: recorder(failing).fetchImpl }).value;
164 await assert.rejects(drain(t, Uint8Array.of(0)), e => e.code === 'network');
165});
167test('the advertisement: its GET asks redirect manual; a foreign content type is not_git; the Content-Length cap is inclusive', async () => {
168 const adv = (ct, length = null) => () => new Response('001e# service=git-upload-pack\n0000', { status: 200, headers: { 'content-type': ct, ...(length === null ? {} : { 'content-length': String(length) }) } });
169 const r = recorder(adv('application/x-git-upload-pack-advertisement'));
170 const t = createTransport('https://h/r', { fetchImpl: r.fetchImpl }).value;
171 assert.equal((await t.advertise('git-upload-pack', spanOf())).ok, true);
172 assert.equal(r.seen[0].init.redirect, 'manual');
173 for (const ct of ['text/html', 'text/plain; application/x-git-upload-pack-advertisement', '']) {
174 const bad = await createTransport('https://h/r', { fetchImpl: recorder(adv(ct)).fetchImpl }).value.advertise('git-upload-pack', spanOf());
175 assert.deepEqual([bad.ok, bad.error.code, bad.error.message], [false, 'not_git', 'unexpected content-type from git-upload-pack advertisement'], ct);
176 }
177 const body = '001e# service=git-upload-pack\n0000'.length;
178 const at = await createTransport('https://h/r', { fetchImpl: recorder(adv('application/x-git-upload-pack-advertisement', body)).fetchImpl, limits: withLimits({ maxPackBytes: body }) }).value.advertise('git-upload-pack', spanOf());
179 assert.equal(at.ok, true, 'a Content-Length equal to the cap is read');
180 const over = await createTransport('https://h/r', { fetchImpl: recorder(adv('application/x-git-upload-pack-advertisement', body)).fetchImpl, limits: withLimits({ maxPackBytes: body - 1 }) }).value.advertise('git-upload-pack', spanOf());
181 assert.deepEqual([over.ok, over.error.code, over.error.got, over.error.max], [false, 'limit_exceeded', body, body - 1]);
182});
184// The other places a limits record enters (#156): syncRef parses it once for both remotes, and a transport handed in
185// has its record parsed (transportFrom) before the operation sends anything.
186test('syncRef and a handed-in transport parse their limits before any request; a complete transport is kept as it is', async () => {
187 const { syncRef } = await import('../src/sync.js');
188 const { asTransport, transportFrom } = await import('../src/transport.js');
189 const { parseLimits, lowered } = await import('../src/limits.js');
190 const c = counting();
191 const r = await syncRef('https://h/a.git', 'https://h/b.git', { ref: 'refs/heads/main' }, { fetchImpl: c.fetchImpl, limits: { maxRefs: -1 } });
192 assert.deepEqual([r.ok, r.error.code, r.error.what, c.n], [false, 'bad_request', 'maxRefs', 0]);
193 const t = createTransport('https://h/r.git', { fetchImpl: c.fetchImpl }).value;
194 // every field present and the limits the transport's own: the transport itself
195 assert.equal(asTransport(t, t.limits), t);
196 assert.equal(transportFrom(t).value, t);
197 // other limits: a view of it with those limits, the caller's state shared
198 const low = lowered(t.limits, 'maxRefs', 3);
199 const view = asTransport(t, low);
200 assert.notEqual(view, t);
201 assert.deepEqual([view.limits, view.quirks, view.objects, view.scrub, view.url], [low, t.quirks, t.objects, t.scrub, t.url]);
202 // a hand-built record: its plain limits parsed (a key left out is the default), its missing state filled
203 const hand = { kind: 'http', url: 'x', tracer: t.tracer, parent: null, signal: null, frames: t.frames, advertise: t.advertise, limits: { maxRefs: 5 } };
204 const made = transportFrom(hand).value;
205 assert.deepEqual([made.limits.maxRefs, made.limits.maxObjectSize, unwrap(parseLimits(made.limits)) === made.limits], [5, DEFAULT_LIMITS.maxObjectSize, true]);
206 assert.deepEqual([made.quirks, made.objects.size, made.scrub], [{ treeFilter: 'tree:0', blobWants: 'many' }, 0, null]);
207 // only a record with all three fields and these limits is kept as it is
208 const full = { ...hand, quirks: t.quirks, objects: t.objects, scrub: null, limits: t.limits };
209 assert.equal(asTransport(full, t.limits), full);
210 assert.notEqual(asTransport(full, low), full);
211 for (const drop of ['quirks', 'objects', 'scrub']) {
212 const { [drop]: _gone, ...rec } = full;
213 const v = asTransport(rec, t.limits);
214 assert.notEqual(v, rec, drop);
215 assert.deepEqual([v.quirks, v.objects instanceof Map, v.scrub, v.limits], [drop === 'quirks' ? { treeFilter: 'tree:0', blobWants: 'many' } : t.quirks, true, null, t.limits], drop);
216 }
217 for (const [key, value] of [['quirks', null], ['objects', null]]) assert.notEqual(asTransport({ ...full, [key]: value }, t.limits), full, key);
218 assert.deepEqual([transportFrom({ ...hand, limits: { maxRefs: NaN } }).error.what, c.n], ['maxRefs', 0]);
219});
221test('a URL that does not parse, and a NUL, CR or LF in its userinfo, are refused with their own message and what', () => {
222 const bad = createTransport('not a url', {});
223 assert.deepEqual([bad.error.code, bad.error.message, bad.error.what], ['bad_request', 'invalid url: not an absolute URL', 'url']);
224 for (const url of ['https://u%0a:p@h/r.git', 'https://u:p%0d@h/r.git', 'https://u%00@h/r.git']) {
225 const r = parseHttpOptions(url, {});
226 assert.deepEqual([r.ok, r.error?.code, r.error?.what], [false, 'bad_request', 'url userinfo'], url);
227 }
228 // explicit auth wins: the URL's userinfo is not read, so it is not refused either
229 assert.equal(parseHttpOptions('https://u%0a@h/r.git', { auth: { token: 't' } }).ok, true);
230});
232// security/url-slash-run-regex-quadratic: the memo key was made with /\/+$/ on the caller's path, which rescans a slash
233// run from each of its slashes when the run does not end the path (quadratic). The keys below are worked by hand from
234// the stated rule (origin + path; query, fragment, userinfo and trailing slashes dropped), not read off ply.
235import { quirksKey, quirksKeyOf } from '../src/transport.js';
236test('quirksKeyOf: the key drops only trailing slashes, in time linear in the path (64x the slashes costs under 300x)', () => {
237 assert.equal(quirksKey('http://h.example/a/b///'), 'http://h.example/a/b');
238 assert.equal(quirksKey('http://h.example/'), 'http://h.example');
239 assert.equal(quirksKey('http://h.example////'), 'http://h.example');
240 assert.equal(quirksKey('http://h.example///x'), 'http://h.example///x');
241 assert.equal(quirksKey('http://h.example/a//b/'), 'http://h.example/a//b');
242 assert.equal(quirksKey('https://u:p@h.example:8443/r.git/?q=1#f'), 'https://h.example:8443/r.git');
243 // A run of slashes that does not end the path. One call on 64n slashes against 64 calls on n: linear work is equal,
244 // quadratic work is 64 times more. Best of 5 each, so load only ever narrows the ratio's noise, never widens it.
245 const N = 500;
246 const small = new URL(`http://h.example/${'/'.repeat(N)}x`);
247 const big = new URL(`http://h.example/${'/'.repeat(64 * N)}x`);
248 const best = (fn) => { let b = Infinity; for (let i = 0; i < 5; i++) { const t0 = performance.now(); fn(); b = Math.min(b, performance.now() - t0); } return b; };
249 const smallMs = best(() => { for (let i = 0; i < 64; i++) quirksKeyOf(small); }) / 64;
250 const bigMs = best(() => quirksKeyOf(big));
251 assert.ok(bigMs < 300 * smallMs, `64x the slashes cost ${(bigMs / smallMs).toFixed(0)}x the time (${bigMs.toFixed(3)} ms against ${smallMs.toFixed(5)} ms)`);
252});
254// The redirect memo (ply-web#117), against a fetch double. `moves` maps a path prefix to where its info/refs (and, with
255// `all`, every request) is sent with a 301; everything else answers an advertisement or an empty upload-pack result.
256// Each test uses its own host name, so the module memo is not shared between tests.
257import { forgetQuirks, QUIRKS_MEMO_MAX, recalledRedirect } from '../src/transport.js';
258const UP = 'git-upload-pack';
259const ADV = '000eversion 2\n0000';
260const memoHost = (moves = new Map(), { throwOnStreamRedirect = false } = {}) => {
261 const seen = [];
262 const fetchImpl = async (url, init) => {
263 const u = new URL(String(url));
264 const method = init.method ?? 'GET';
265 seen.push({ line: `${method} ${u.host}${u.pathname}`, auth: new Headers(init.headers).get('authorization'), redirect: init.redirect });
266 const repo = u.pathname.slice(0, u.pathname.lastIndexOf(u.pathname.endsWith('/info/refs') ? '/info/refs' : '/'));
267 const move = moves.get(`${u.host}${repo}`);
268 if (move !== undefined && (move.all || method === 'GET')) {
269 if (throwOnStreamRedirect && init.redirect === 'error') throw new TypeError('fetch failed: redirect mode is error');
270 return new Response(null, { status: 301, headers: { location: `${move.to}${u.pathname.slice(repo.length)}${u.search}` } });
271 }
272 if (method === 'GET') return new Response(ADV, { status: 200, headers: { 'content-type': `application/x-${u.searchParams.get('service')}-advertisement` } });
273 return new Response('0000', { status: 200, headers: { 'content-type': `application/x-${UP}-result` } });
274 };
275 return { seen, fetchImpl, lines: () => seen.map(s => s.line), clear: () => { seen.length = 0; } };
276};
277const memoT = (url, h, opts = {}) => createTransport(url, { fetchImpl: h.fetchImpl, followRedirects: 'initial', ...opts });
278const postUp = async (t, body = Uint8Array.of(0x30, 0x30, 0x30, 0x30), span = spanOf()) => { for await (const _ of t.frames(UP, body, span, { bypassCache: false })) { /* frames */ } return span; };
280test('memo: the second transport for a remote makes no GET and posts to the settled base; its span says it was rebased', async () => {
281 const h = memoHost(new Map([['m1/old', { to: 'https://m1/new', all: false }]]));
282 const first = memoT('https://m1/old', h, { auth: { bearer: 'tok' } }).value;
283 await postUp(first);
284 assert.deepEqual(h.lines(), ['GET m1/old/info/refs', 'GET m1/new/info/refs', 'POST m1/new/git-upload-pack']);
285 h.clear();
286 const second = memoT('https://m1/old/', h, { auth: { bearer: 'tok' } }).value;
287 const span = await postUp(second);
288 assert.deepEqual(h.lines(), ['POST m1/new/git-upload-pack']);
289 assert.equal(second.url, 'https://m1/new');
290 assert.deepEqual(h.seen.map(s => s.auth), ['Bearer tok']);
291 assert.deepEqual([span.attrs['http.redirect.count'], span.attrs['git.remote.rebased_from'], span.attrs['url.full']], [1, 'https://m1/old', 'https://m1/new/git-upload-pack']);
292 // a settled transport does not replay again; an advertise on a memo base that answers is the confirmation
293 h.clear();
294 await postUp(second);
295 assert.equal((await memoT('https://m1/old', h).value.advertise(UP, spanOf())).ok, true);
296 assert.deepEqual(h.lines(), ['POST m1/new/git-upload-pack', 'GET m1/new/info/refs']);
297 // no redirect is remembered too; another service is its own entry, kept beside the first
298 h.clear();
299 await postUp(memoT('https://m1/plain', h).value);
300 await postUp(memoT('https://m1/plain', h).value);
301 const rp = memoT('https://m1/plain', h).value;
302 assert.equal((await rp.advertise('git-receive-pack', spanOf())).ok, true);
303 await postUp(memoT('https://m1/plain', h).value);
304 assert.deepEqual(h.lines(), ['GET m1/plain/info/refs', 'POST m1/plain/git-upload-pack', 'POST m1/plain/git-upload-pack', 'GET m1/plain/info/refs', 'POST m1/plain/git-upload-pack']);
305});
307test('memo: never, and a settled transport, report a 3xx on an advertisement and do not follow or remember it', async () => {
308 const h = memoHost(new Map([['m7/old', { to: 'https://m7/new', all: false }]]));
309 const never = await createTransport('https://m7/old', { fetchImpl: h.fetchImpl }).value.advertise(UP, spanOf());
310 assert.deepEqual([never.ok, never.error.code, never.error.status], [false, 'http', 301]);
311 // a remembered remote: never still asks the original base and reports its 3xx
312 await postUp(memoT('https://m7/old', h).value);
313 h.seen.length = 0;
314 const neverHit = await createTransport('https://m7/old', { fetchImpl: h.fetchImpl }).value.advertise(UP, spanOf());
315 assert.deepEqual([neverHit.ok, neverHit.error.status, h.lines()], [false, 301, ['GET m7/old/info/refs']]);
316 const plain = await createTransport('https://m7/plain', { fetchImpl: h.fetchImpl }).value.advertise(UP, spanOf());
317 assert.equal(plain.ok, true);
318 const settled = memoT('https://m7/flip', h).value;
319 assert.equal((await settled.advertise(UP, spanOf())).ok, true);
320 h.seen.length = 0;
321 await postUp(memoT('https://m7/plain', h).value);
322 assert.deepEqual(h.lines(), ['GET m7/plain/info/refs', 'POST m7/plain/git-upload-pack'], 'never remembered nothing');
323 const moves = new Map([['m7/flip', { to: 'https://m7/new', all: false }]]);
324 const later = memoHost(moves);
325 const again = await createTransport('https://m7/flip', { fetchImpl: later.fetchImpl, followRedirects: 'initial' }).value;
326 assert.equal((await again.advertise(UP, spanOf())).ok, true, 'memo: no redirect, and the GET at the base answers');
327 const t2 = memoT('https://m7/other', later).value;
328 assert.equal((await t2.advertise(UP, spanOf())).ok, true);
329 moves.set('m7/other', { to: 'https://m7/new', all: false });
330 const second = await t2.advertise(UP, spanOf());
331 assert.deepEqual([second.ok, second.error.code], [false, 'http'], 'a settled transport does not follow a later 3xx');
332});
334test('memo: never neither reads nor writes it; a list past maxRedirects is not replayed; a failed advertisement is not remembered', async () => {
335 const h = memoHost(new Map([['m2/old', { to: 'https://m2/a', all: false }], ['m2/a', { to: 'https://m2/b', all: false }]]));
336 await postUp(memoT('https://m2/old', h).value);
337 assert.deepEqual(h.lines(), ['GET m2/old/info/refs', 'GET m2/a/info/refs', 'GET m2/b/info/refs', 'POST m2/b/git-upload-pack']);
338 h.clear();
339 await postUp(createTransport('https://m2/old', { fetchImpl: h.fetchImpl }).value);
340 assert.deepEqual(h.lines(), ['POST m2/old/git-upload-pack'], 'never: no memo');
341 h.clear();
342 const one = memoT('https://m2/old', h, { maxRedirects: 1 }).value;
343 await assert.rejects(postUp(one), e => e.code === 'http');
344 assert.deepEqual(h.lines(), ['GET m2/old/info/refs', 'GET m2/a/info/refs'], 'two hops remembered, one allowed: asked again');
345 h.clear();
346 await postUp(memoT('https://m2/old', h, { maxRedirects: 2 }).value);
347 assert.deepEqual(h.lines(), ['POST m2/b/git-upload-pack'], 'exactly maxRedirects hops: replayed');
348 // never writes: a fresh remote asked through `never` first is still unknown to 'initial'
349 h.clear();
350 await postUp(createTransport('https://m2/fresh', { fetchImpl: h.fetchImpl }).value);
351 await postUp(memoT('https://m2/fresh', h).value);
352 assert.deepEqual(h.lines(), ['POST m2/fresh/git-upload-pack', 'GET m2/fresh/info/refs', 'POST m2/fresh/git-upload-pack']);
353 // a 404 advertisement settles nothing to remember
354 const gone = { ...h, fetchImpl: async (url, init) => (init.method === undefined ? new Response('no', { status: 404 }) : h.fetchImpl(url, init)) };
355 await assert.rejects(postUp(memoT('https://m2/gone', gone).value), e => e.code === 'not_found');
356 h.clear();
357 await postUp(memoT('https://m2/gone', h).value);
358 assert.deepEqual(h.lines(), ['GET m2/gone/info/refs', 'POST m2/gone/git-upload-pack']);
359});
361test('memo: each Location goes through the new transport\'s allowUrl; a refusal or a non-rebase asks the network; a cross-origin replay drops the credential', async () => {
362 const h = memoHost(new Map([['m3/old', { to: 'https://m3x/new', all: false }]]));
363 await postUp(memoT('https://m3/old', h, { auth: { bearer: 'tok' } }).value);
364 assert.deepEqual(h.seen.map(s => [s.line, s.auth]), [['GET m3/old/info/refs', 'Bearer tok'], ['GET m3x/new/info/refs', null], ['POST m3x/new/git-upload-pack', null]]);
365 h.clear();
366 await postUp(memoT('https://m3/old', h, { auth: { bearer: 'tok' } }).value);
367 assert.deepEqual(h.seen.map(s => [s.line, s.auth]), [['POST m3x/new/git-upload-pack', null]], 'replayed: still no credential on the other origin');
368 const calls = [];
369 const deny = url => { calls.push(url.href); return url.host === 'm3x' ? { ok: false, error: Object.assign(new Error('denied'), { code: 'auth' }) } : { ok: true, value: url }; };
370 h.clear();
371 await assert.rejects(postUp(memoT('https://m3/old', h, { allowUrl: deny }).value), e => e.message === 'denied');
372 assert.deepEqual(calls, ['https://m3/old', `https://m3x/new/info/refs?service=${UP}`, `https://m3x/new/info/refs?service=${UP}`]);
373 assert.deepEqual(h.lines(), ['GET m3/old/info/refs']);
374 // an allowUrl whose answer drops the suffix: the replay cannot rebase, so the network is asked (and refuses the same)
375 h.clear();
376 const strip = url => ({ ok: true, value: url.host === 'm3x' ? new URL('https://m3x/elsewhere') : url });
377 await assert.rejects(postUp(memoT('https://m3/old', h, { allowUrl: strip }).value), e => e.code === 'http');
378 assert.deepEqual(h.lines(), ['GET m3/old/info/refs']);
379 // an allowUrl that rewrites: the replay takes its answer
380 h.clear();
381 const rename = url => ({ ok: true, value: url.host === 'm3x' ? new URL(`https://m3x/renamed/info/refs?service=${UP}`) : url });
382 const t = memoT('https://m3/old', h, { allowUrl: rename }).value;
383 await postUp(t);
384 assert.equal(t.url, 'https://m3x/renamed');
385 assert.deepEqual(h.lines(), ['POST m3x/renamed/git-upload-pack']);
386});
388test('memo: stale on a POST (bytes re-sent once after the GET), on a GET (starts over), and on a stream (the error; the entry is dropped)', async () => {
389 const moves = new Map([['m4/old', { to: 'https://m4/a', all: false }]]);
390 const h = memoHost(moves);
391 await postUp(memoT('https://m4/old', h).value);
392 moves.set('m4/old', { to: 'https://m4/b', all: false });
393 moves.set('m4/a', { to: 'https://m4/b', all: true });
394 h.clear();
395 const t = memoT('https://m4/old', h).value;
396 const span = await postUp(t);
397 assert.deepEqual(h.seen.map(s => s.redirect), ['manual', 'manual', 'manual', 'manual'], 'the re-sent POST asks redirect manual too');
398 assert.deepEqual(h.lines(), ['POST m4/a/git-upload-pack', 'GET m4/old/info/refs', 'GET m4/b/info/refs', 'POST m4/b/git-upload-pack']);
399 assert.equal(h.seen[3].line, 'POST m4/b/git-upload-pack');
400 assert.deepEqual([t.url, span.attrs['url.full'], span.attrs['http.redirect.count']], ['https://m4/b', 'https://m4/b/git-upload-pack', 1]);
401 h.clear();
402 await postUp(memoT('https://m4/old', h).value);
403 assert.deepEqual(h.lines(), ['POST m4/b/git-upload-pack'], 're-learned');
404 // a stale memo whose fresh GET fails: that failure is the answer, and nothing is sent twice
405 moves.set('m4/b', { to: 'https://m4/c', all: true });
406 const down = { ...h, fetchImpl: async (url, init) => (init.method === 'POST' || !String(url).includes('m4/old') ? h.fetchImpl(url, init) : new Response('down', { status: 503 })) };
407 h.clear();
408 await assert.rejects(postUp(memoT('https://m4/old', down).value), e => e.code === 'upstream_5xx');
409 assert.deepEqual(h.lines(), ['POST m4/b/git-upload-pack']);
410 assert.equal(recalledRedirect('https://m4/old', UP), null, 'the stale entry is gone although nothing replaced it');
411 // stale on the GET of an advertise: starts over from the original base
412 moves.set('m4/old', { to: 'https://m4/c', all: false });
413 await postUp(memoT('https://m4/old', h).value);
414 moves.set('m4/c', { to: 'https://m4/d', all: true });
415 moves.set('m4/old', { to: 'https://m4/d', all: false });
416 h.clear();
417 const d = memoT('https://m4/old', h).value;
418 assert.equal((await d.advertise(UP, spanOf())).ok, true);
419 assert.deepEqual(h.lines(), ['GET m4/c/info/refs', 'GET m4/old/info/refs', 'GET m4/d/info/refs']);
420 assert.equal(d.url, 'https://m4/d');
421 // a stream is not re-sent: a 3xx answer is the redirect error, and the entry is dropped
422 moves.set('m4/d', { to: 'https://m4/e', all: true });
423 h.clear();
424 const stream = () => ({ stream: new ReadableStream({ start(c) { c.enqueue(Uint8Array.of(0x30, 0x30, 0x30, 0x30)); c.close(); } }), length: 4 });
425 await assert.rejects(postUp(memoT('https://m4/old', h).value, stream()), e => e.code === 'http' && e.status === 301 && e.location === 'https://m4/e/git-upload-pack');
426 assert.deepEqual(h.lines(), ['POST m4/d/git-upload-pack']);
427 moves.set('m4/old', { to: 'https://m4/f', all: false });
428 h.clear();
429 await postUp(memoT('https://m4/old', h).value);
430 assert.deepEqual(h.lines(), ['GET m4/old/info/refs', 'GET m4/f/info/refs', 'POST m4/f/git-upload-pack'], 'dropped');
431 // where a stream's 3xx is a network failure (redirect 'error'), the entry is dropped all the same; bytes that fail on
432 // the network keep it (a dead link is not a stale memo)
433 moves.set('m4/f', { to: 'https://m4/g', all: true });
434 const strict = memoHost(moves, { throwOnStreamRedirect: true });
435 await assert.rejects(postUp(memoT('https://m4/old', strict).value, stream()), e => e.code === 'network');
436 const flaky = { ...strict, fetchImpl: async () => { throw new TypeError('fetch failed'); } };
437 moves.set('m4/old', { to: 'https://m4/h', all: false });
438 strict.clear();
439 await postUp(memoT('https://m4/old', strict).value);
440 assert.deepEqual(strict.lines(), ['GET m4/old/info/refs', 'GET m4/h/info/refs', 'POST m4/h/git-upload-pack'], 'dropped');
441 await assert.rejects(postUp(memoT('https://m4/old', flaky).value), e => e.code === 'network');
442 strict.clear();
443 await postUp(memoT('https://m4/old', strict).value);
444 assert.deepEqual(strict.lines(), ['POST m4/h/git-upload-pack'], 'kept');
445});
447test('memo: forgetQuirks forgets one remote or all; the memo is bounded like the quirks memo, least recently used first', async () => {
448 const h = memoHost();
449 for (const r of ['a', 'b']) await postUp(memoT(`https://m5/${r}`, h).value);
450 forgetQuirks('https://m5/a');
451 h.clear();
452 for (const r of ['a', 'b']) await postUp(memoT(`https://m5/${r}`, h).value);
453 assert.deepEqual(h.lines(), ['GET m5/a/info/refs', 'POST m5/a/git-upload-pack', 'POST m5/b/git-upload-pack']);
454 forgetQuirks('not a url');
455 forgetQuirks();
456 h.clear();
457 await postUp(memoT('https://m5/b', h).value);
458 assert.deepEqual(h.lines(), ['GET m5/b/info/refs', 'POST m5/b/git-upload-pack']);
459 // fill to the bound: touching the first keeps it; the second-oldest is the one dropped
460 forgetQuirks();
461 for (let i = 0; i < QUIRKS_MEMO_MAX; i++) await postUp(memoT(`https://m5/r${i}`, h).value);
462 await postUp(memoT('https://m5/r0', h).value);
463 await postUp(memoT('https://m5/over', h).value);
464 h.clear();
465 for (const r of ['r0', 'over', 'r2', 'r1']) await postUp(memoT(`https://m5/${r}`, h).value);
466 assert.deepEqual(h.lines(), ['POST m5/r0/git-upload-pack', 'POST m5/over/git-upload-pack', 'POST m5/r2/git-upload-pack', 'GET m5/r1/info/refs', 'POST m5/r1/git-upload-pack'], 'exactly one dropped: the least recently used');
467 forgetQuirks();
468});
470test('memo: forgetQuirks clears the quirks record with the redirect memo, for one remote or all', () => {
471 const q = url => createTransport(url).value.quirks;
472 const a = q('https://m8/a');
473 const b = q('https://m8/b');
474 assert.equal(q('https://m8/a'), a);
475 forgetQuirks('https://m8/a');
476 assert.notEqual(q('https://m8/a'), a);
477 assert.equal(q('https://m8/b'), b);
478 forgetQuirks();
479 assert.notEqual(q('https://m8/b'), b);
480});
482test('memo: an entry already gone when its transport finds it stale (forgotten meanwhile) is not an error', async () => {
483 const moves = new Map([['m9/old', { to: 'https://m9/a', all: false }]]);
484 const h = memoHost(moves);
485 await postUp(memoT('https://m9/old', h).value);
486 moves.set('m9/a', { to: 'https://m9/b', all: true });
487 moves.set('m9/old', { to: 'https://m9/b', all: false });
488 const forgetting = { ...h, fetchImpl: async (url, init) => { if (init.method === 'POST' && String(url).includes('m9/a/')) forgetQuirks(); return h.fetchImpl(url, init); } };
489 h.clear();
490 const t = memoT('https://m9/old', forgetting).value;
491 await postUp(t);
492 assert.equal(t.url, 'https://m9/b');
493 const stream = { stream: new ReadableStream({ start(c) { c.enqueue(Uint8Array.of(0x30, 0x30, 0x30, 0x30)); c.close(); } }), length: 4 };
494 moves.set('m9/b', { to: 'https://m9/c', all: true });
495 const forgetting2 = { ...h, fetchImpl: async (url, init) => { if (init.method === 'POST') forgetQuirks(); return h.fetchImpl(url, init); } };
496 await assert.rejects(postUp(memoT('https://m9/old', forgetting2).value, stream), e => e.code === 'http');
497});
499test('memo: re-remembering a remote makes it the most recently used, even after other remotes were used meanwhile', async () => {
500 forgetQuirks();
501 const moves = new Map([['m10/k', { to: 'https://m10/k2', all: false }]]);
502 const h = memoHost(moves);
503 await postUp(memoT('https://m10/k', h).value);
504 for (let i = 1; i < QUIRKS_MEMO_MAX; i++) await postUp(memoT(`https://m10/r${i}`, h).value);
505 // k's next transport refuses the remembered Location once (the replay), so it asks the network; while its GET is out,
506 // every other remote is used, which leaves k the least recently used until its own answer is remembered
507 let refusals = 0;
508 const allowUrl = url => (url.pathname.startsWith('/k2') && refusals++ === 0 ? { ok: false, error: Object.assign(new Error('not now'), { code: 'auth' }) } : { ok: true, value: url });
509 let touched = false;
510 const busy = { ...h, fetchImpl: async (url, init) => {
511 if (!touched && String(url).startsWith('https://m10/k/info/refs')) {
512 touched = true;
513 for (let i = 1; i < QUIRKS_MEMO_MAX; i++) await postUp(memoT(`https://m10/r${i}`, h).value);
514 }
515 return h.fetchImpl(url, init);
516 } };
517 await postUp(memoT('https://m10/k', busy, { allowUrl }).value);
518 assert.deepEqual([touched, refusals], [true, 2]);
519 await postUp(memoT('https://m10/over', h).value);
520 h.clear();
521 await postUp(memoT('https://m10/k', h).value);
522 assert.deepEqual(h.lines(), ['POST m10/k2/git-upload-pack'], 'k was kept: its re-remembered entry was the newest');
523 forgetQuirks();
524});
526test('memo: a Location is kept absolute, without userinfo or fragment, as the server sent it before allowUrl', async () => {
527 const h = memoHost();
528 const odd = { ...h, fetchImpl: async (url, init) => (String(url) === `https://m12/old/info/refs?service=${UP}`
529 ? new Response(null, { status: 302, headers: { location: `//u:p@m12/new/info/refs?service=${UP}#frag` } })
530 : h.fetchImpl(url, init)) };
531 const rename = url => ({ ok: true, value: url.pathname.startsWith('/new') ? new URL(`https://m12/renamed/info/refs?service=${UP}`) : url });
532 const t = memoT('https://m12/old', odd, { allowUrl: rename }).value;
533 await postUp(t);
534 assert.equal(t.url, 'https://m12/renamed');
535 assert.deepEqual(recalledRedirect('https://m12/old', UP), [`https://m12/new/info/refs?service=${UP}`]);
536 assert.equal(recalledRedirect('https://m12/old', 'git-receive-pack'), null);
537 assert.equal(recalledRedirect('https://m12/never', UP), null);
538});
540test('memo: a confirmed base is not stale any more: a later 3xx on it is the redirect error, with no GET and no re-send', async () => {
541 const moves = new Map();
542 const h = memoHost(moves);
543 // no memo: settled by its own GET, then a 3xx on the POST
544 const fresh = memoT('https://m13/a', h).value;
545 assert.equal((await fresh.advertise(UP, spanOf())).ok, true);
546 moves.set('m13/a', { to: 'https://m13/z', all: true });
547 h.clear();
548 await assert.rejects(postUp(fresh), e => e.code === 'http' && e.status === 301);
549 assert.deepEqual(h.lines(), ['POST m13/a/git-upload-pack']);
550 // a memo hit confirmed by its first POST: the second POST's 3xx is the error
551 moves.delete('m13/a');
552 await postUp(memoT('https://m13/b', h).value);
553 const hit = memoT('https://m13/b', h).value;
554 h.clear();
555 await postUp(hit);
556 moves.set('m13/b', { to: 'https://m13/z', all: true });
557 await assert.rejects(postUp(hit), e => e.code === 'http' && e.status === 301);
558 assert.deepEqual(h.lines(), ['POST m13/b/git-upload-pack', 'POST m13/b/git-upload-pack']);
559});
561test('the POST span: status, content type, a time to first byte from the request, and the recorded response headers', async () => {
562 const h = memoHost();
563 const t0 = Date.now();
564 const span = await postUp(memoT('https://m14/r', h).value);
565 assert.equal(span.attrs['http.response.status_code'], 200);
566 assert.equal(span.attrs['http.response.header.content-type'], `application/x-${UP}-result`);
567 assert.ok(span.attrs['http.response.ttfb_ms'] >= 0 && span.attrs['http.response.ttfb_ms'] <= Date.now() - t0 + 1, String(span.attrs['http.response.ttfb_ms']));
568 const tagged = { ...h, fetchImpl: async (url, init) => { const r = await h.fetchImpl(url, init); const out = new Response(r.body, r); out.headers.set('x-tag', 'v'); out.headers.set('x-ply-pack-cache', 'HIT'); return out; } };
569 const recorded = await postUp(createTransport('https://m14/r', { fetchImpl: tagged.fetchImpl, recordResponseHeaders: ['x-tag'] }).value);
570 assert.equal(recorded.attrs['http.response.header.x-tag'], 'v');
571 const plain = await postUp(createTransport('https://m14/r', { fetchImpl: tagged.fetchImpl }).value);
572 assert.equal(plain.attrs['http.response.header.x-tag'], undefined);
573 assert.equal(plain.attrs['http.response.headers.from_cache'], undefined, 'no recorded names: no recorded-header attributes at all');
574 assert.equal(recorded.attrs['http.response.headers.from_cache'], true);
575});
577test('a timeout a timer cannot hold (above 2^31-1 ms) is refused where the options enter; 0 (off) and 2^31-1 pass', () => {
578 // setTimeout's delay is a signed 32-bit count of ms: 2147483647 is the largest it holds (HTML timers; Node warns)
579 for (const key of ['firstByteTimeoutMs', 'idleTimeoutMs', 'heldTimeoutMs', 'requestTimeoutMs']) {
580 for (const bad of [2 ** 31, 2 ** 32, Number.MAX_SAFE_INTEGER]) {
581 const r = createTransport('https://h/r', { limits: { [key]: bad } });
582 assert.deepEqual([r.ok, r.error?.code, r.error?.what, r.error?.got], [false, 'bad_request', key, bad], `${key}=${bad}`);
583 }
584 for (const fine of [0, 1, 2 ** 31 - 1]) assert.equal(createTransport('https://h/r', { limits: { [key]: fine } }).ok, true, `${key}=${fine}`);
585 }
586});
588// The refusal's text reaches the caller: the key by its name, the bound written out (2^31 - 1 = 2147483647), and the
589// way to turn the timer off. Hand-written here, not read from src. The bound is the timers' alone: a limit that is not
590// a timer passes above it.
591test('a timeout above 2^31-1 ms is refused with a message naming the key and the bound; a limit that is no timer passes above it', () => {
592 for (const key of ['firstByteTimeoutMs', 'idleTimeoutMs', 'heldTimeoutMs', 'requestTimeoutMs']) {
593 const message = `limits.${key} must be at most 2147483647 ms (0 turns the timer off)`;
594 for (const r of [createTransport('https://h/r', { limits: { [key]: 2147483648 } }), parseHttpOptions('https://h/r', { limits: { [key]: 2147483648 } }), parseLimits({ [key]: 2147483648 })]) {
595 assert.deepEqual([r.ok, r.error?.code, r.error?.message, r.error?.what, r.error?.got], [false, 'bad_request', message, key, 2147483648], key);
596 }
597 }
598 const big = parseLimits({ maxPackBytes: 2147483648 });
599 assert.deepEqual([big.ok, big.value?.maxPackBytes], [true, 2147483648]);
600});
602// The scheme rule (src/http.ts parseHttpOptions), in the suite the mutation gate runs. ply speaks git smart HTTP only
603// (SAFETY.md), so the rule itself gives every expectation: http: and https: pass, in any letter case (WHATWG URL
604// lowercases the scheme); anything else is a definite bad_request about the url, before any I/O. The message is the
605// fixed text README documents, and the allowUrl rows are the train head's verdicts (607691d1) recorded by the probe in
606// scratch (fix9/http-scheme-mutants-kill/probe-before.txt), not read back from the code under test here.
607// From its module, not the package root: a static import of src/index.js loads every module it re-exports, so each of
608// their module-level mutants is static and runs all 412 mutation tests (errors-doc.ts: 79 of them, #313).
609import { listRefs } from '../src/refops.js';
610const SCHEME_MESSAGE = 'invalid url: only http: and https: remotes are supported';
611const NOT_HTTP = ['file:///etc/passwd', 'data:text/plain,hi', 'blob:nodedata:abc', 'ssh://git@127.0.0.1/repo.git', 'git://127.0.0.1/repo.git', 'ext::sh -c "touch /tmp/pwned"'];
613test('a base URL that is not http(s) is bad_request, what: url, with the fixed message, and the fetch implementation is never called', async () => {
614 for (const url of NOT_HTTP) {
615 const c = counting();
616 for (const r of [createTransport(url, { fetchImpl: c.fetchImpl }), parseHttpOptions(url, { fetchImpl: c.fetchImpl }), await listRefs(url, {}, { fetchImpl: c.fetchImpl })]) {
617 assert.deepEqual([r.ok, r.error?.code, r.error?.what, r.error?.message], [false, 'bad_request', 'url', SCHEME_MESSAGE], url);
618 }
619 assert.equal(c.n, 0, url);
620 }
621});
623test('http: and https: bases pass the scheme rule, in any letter case, and are the URL requested', () => {
624 for (const [url, href] of [['http://127.0.0.1/r.git', 'http://127.0.0.1/r.git'], ['HTTP://127.0.0.1/r.git', 'http://127.0.0.1/r.git'], ['https://127.0.0.1/r.git', 'https://127.0.0.1/r.git'], ['HtTpS://127.0.0.1/r.git', 'https://127.0.0.1/r.git']]) {
625 const settings = parseHttpOptions(url, {});
626 assert.equal(settings.ok, true, url);
627 assert.equal(settings.value.url.href, href, url);
628 assert.equal(createTransport(url, {}).ok, true, url);
629 }
630});
632test('an allowUrl that maps an http(s) base to another scheme is refused as allowUrl\'s fault before any fetch; one that maps a caller\'s scheme to https passes', () => {
633 for (const target of NOT_HTTP) {
634 for (const base of ['https://127.0.0.1/r.git', 'http://127.0.0.1/r.git']) {
635 const c = counting();
636 const allowUrl = () => ({ ok: true, value: new URL(target) });
637 // no transport exists after the refusal, so nothing can fetch (the one-call entry points take no allowUrl)
638 for (const r of [createTransport(base, { fetchImpl: c.fetchImpl, allowUrl }), parseHttpOptions(base, { fetchImpl: c.fetchImpl, allowUrl })]) {
639 assert.deepEqual([r.ok, r.error?.code, r.error?.what, r.error?.message], [false, 'bad_request', 'allowUrl', 'allowUrl must return an http(s) URL'], `${base} -> ${target}`);
640 }
641 assert.equal(c.n, 0, target);
642 }
643 }
644 for (const url of [...NOT_HTTP, 'repo://alias/team/r.git']) {
645 const settings = parseHttpOptions(url, { allowUrl: () => ({ ok: true, value: new URL('https://real.example/team/r.git') }) });
646 assert.equal(settings.ok, true, url);
647 assert.equal(settings.value.url.href, 'https://real.example/team/r.git', url);
648 }
649});
650// #273: heldTimeoutMs is parsed as its sibling timers are: a negative, NaN or infinite value is bad_request naming it.
651test('limits.heldTimeoutMs: not a finite non-negative number is bad_request naming the key', () => {
652 for (const bad of [-1, Number.NaN, Infinity, '1000']) {
653 const r = parseLimits({ heldTimeoutMs: bad });
654 assert.deepEqual([r.ok, r.error?.code, r.error?.what], [false, 'bad_request', 'heldTimeoutMs'], String(bad));
655 }
656 assert.deepEqual([parseLimits({ heldTimeoutMs: 0 }).value?.heldTimeoutMs, parseLimits({}).value?.heldTimeoutMs], [0, 240_000]);
657});
659// #273 review round 4 (owner's rule, DECISION 2): the hold starts at the first clock read after the take, so a cut is
660// never before take + heldTimeoutMs and at most min(idleTimeoutMs, heldTimeoutMs) late (heldTimeoutMs late with idle
661// off). Network progress during the hold (a push whose server answered before the upload ended: upload pieces keep
662// being taken while the consumer holds a chunk) must not move the hold's start. Expected bounds are that rule worked by
663// hand: take at 20, held 4000, so the cut is in [4020, 4020 + min(idle, held)]. Timers are manual: nothing sleeps.
664import { exchangeClock } from '../src/deadline.js';
665for (const idleMs of [1000, 0]) test(`exchangeClock: idle ${idleMs}, upload progress while the consumer holds does not move the hold's start`, () => {
666 const realSet = globalThis.setTimeout, realClear = globalThis.clearTimeout;
667 let T = 0;
668 const q = new Map();
669 globalThis.setTimeout = (fn, ms) => { const id = { unref() {} }; q.set(id, { at: T + (ms ?? 0), fn }); return id; };
670 globalThis.clearTimeout = id => { q.delete(id); };
671 const advance = to => {
672 for (;;) {
673 let best = null;
674 for (const e of q) if (e[1].at <= to && (best === null || e[1].at < best[1].at)) best = e;
675 if (best === null) break;
676 q.delete(best[0]); T = Math.max(T, best[1].at); best[1].fn();
677 }
678 T = to;
679 };
680 try {
681 const heldMs = 4000;
682 let fired = 'none', at = -1;
683 const c = exchangeClock({ firstByteMs: 1000, idleMs, heldMs, totalMs: 0 }, null, k => { fired = k; at = T; }, () => T);
684 advance(10); c.feed('headers'); advance(20); c.feed('consumer_took');
685 for (let x = 100; x <= 60_000 && fired === 'none'; x += 100) { advance(x); c.feed('upload_bytes'); }
686 advance(70_000);
687 c.cancel();
688 const take = 20, late = idleMs > 0 ? Math.min(idleMs, heldMs) : heldMs;
689 assert.equal(fired, 'heldTimeoutMs');
690 assert.ok(at >= take + heldMs && at <= take + heldMs + late, `cut at ${at}, want [${take + heldMs}, ${take + heldMs + late}]`);
691 } finally { globalThis.setTimeout = realSet; globalThis.clearTimeout = realClear; }
692});
694// README "Your own code failing": a fetchImpl you passed that throws or rejects is `network` with what 'fetchImpl'; a
695// failure of the platform fetch (no fetchImpl passed) is your network, not your code, and names no callback. Expected
696// values are that table's, not ply's output. The default fetchImpl is read from globalThis.fetch when the options are
697// parsed (httpDefaults() runs per parseHttpOptions call, not at module load), so the platform fetch is replaced on
698// globalThis for the call and restored after: the transport then holds the stub as its default, unnamed.
699test('a failing fetchImpl you passed is named fetchImpl; a failing platform fetch is not', async () => {
700 const where = { ref: 'refs/heads/main', path: 'a' };
701 for (const [label, fetchImpl] of [['throws', () => { throw new Error('boom'); }], ['rejects', async () => { await null; throw new Error('boom'); }]]) {
702 const r = await readFile('https://h/r.git', where, { fetchImpl });
703 assert.deepEqual([r.ok, r.error?.code, r.error?.what], [false, 'network', 'fetchImpl'], label);
704 }
705 const real = globalThis.fetch;
706 try {
707 globalThis.fetch = async () => { await null; throw new TypeError('fetch failed'); };
708 const r = await readFile('https://h/r.git', where, {});
709 assert.deepEqual([r.ok, r.error?.code, r.error?.what], [false, 'network', undefined]);
710 } finally { globalThis.fetch = real; }
711});
Diagnostics6 git calls · 148 ms
CallStatusSizeTime0148 ms
discover200191 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=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB57 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=57 git.response.bytes=11990 git.response.cache=EXPIRED git.response.duration_ms=57 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit3 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=4347 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 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=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · hit3 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=919 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 KiB · hit3 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=5724 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=5724 git.pkt.payload.max=5697 git.sideband.band1.bytes=5697 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=5697 git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch20013 KiB82 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=82 git.response.bytes=13290 git.response.cache=MISS git.response.duration_ms=82 git.fetch.wants=b3932d7ab2329e8219456be6488f2c0723b37370 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=5 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=13290 git.pkt.payload.max=8192 git.sideband.band1.bytes=13258 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://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e1553cf8b890ce
- Request
- 4998b4fb…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format