@yaks/git 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +754 -2
- package/_dist/address.d.ts +6 -0
- package/_dist/address.d.ts.map +1 -0
- package/_dist/checkout_vocab.d.ts +3 -0
- package/_dist/checkout_vocab.d.ts.map +1 -0
- package/_dist/cites.d.ts +48 -0
- package/_dist/cites.d.ts.map +1 -0
- package/_dist/commit.d.ts +36 -0
- package/_dist/commit.d.ts.map +1 -0
- package/_dist/comp.d.ts +29 -0
- package/_dist/comp.d.ts.map +1 -0
- package/_dist/door.d.ts +29 -0
- package/_dist/door.d.ts.map +1 -0
- package/_dist/format.d.ts +2 -0
- package/_dist/format.d.ts.map +1 -0
- package/_dist/hold.d.ts +6 -0
- package/_dist/hold.d.ts.map +1 -0
- package/_dist/host.d.ts +126 -0
- package/_dist/host.d.ts.map +1 -0
- package/_dist/http.d.ts +33 -0
- package/_dist/http.d.ts.map +1 -0
- package/_dist/import.d.ts +13 -0
- package/_dist/import.d.ts.map +1 -0
- package/_dist/index.d.ts +80 -0
- package/_dist/index.d.ts.map +1 -0
- package/_dist/land.d.ts +77 -0
- package/_dist/land.d.ts.map +1 -0
- package/_dist/mod.d.ts +73 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/objects.d.ts +40 -0
- package/_dist/objects.d.ts.map +1 -0
- package/_dist/oid.d.ts +24 -0
- package/_dist/oid.d.ts.map +1 -0
- package/_dist/pack-store.d.ts +9 -0
- package/_dist/pack-store.d.ts.map +1 -0
- package/_dist/pack.d.ts +26 -0
- package/_dist/pack.d.ts.map +1 -0
- package/_dist/pkt.d.ts +14 -0
- package/_dist/pkt.d.ts.map +1 -0
- package/_dist/placement.d.ts +4 -0
- package/_dist/placement.d.ts.map +1 -0
- package/_dist/plugin.d.ts +43 -0
- package/_dist/plugin.d.ts.map +1 -0
- package/_dist/project.d.ts +20 -0
- package/_dist/project.d.ts.map +1 -0
- package/_dist/push.d.ts +42 -0
- package/_dist/push.d.ts.map +1 -0
- package/_dist/refs.d.ts +81 -0
- package/_dist/refs.d.ts.map +1 -0
- package/_dist/rewrite.d.ts +35 -0
- package/_dist/rewrite.d.ts.map +1 -0
- package/_dist/service.d.ts +35 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/sha1.d.ts +11 -0
- package/_dist/sha1.d.ts.map +1 -0
- package/_dist/source.d.ts +12 -0
- package/_dist/source.d.ts.map +1 -0
- package/_dist/tools.d.ts +11 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/tree.d.ts +28 -0
- package/_dist/tree.d.ts.map +1 -0
- package/_dist/unpack.d.ts +5 -0
- package/_dist/unpack.d.ts.map +1 -0
- package/_dist/vocab.d.ts +8 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/_dist/workspace.d.ts +17 -0
- package/_dist/workspace.d.ts.map +1 -0
- package/address.js +9 -0
- package/address.js.map +1 -0
- package/address.ts +15 -0
- package/cache.js +9 -0
- package/cache.js.map +1 -0
- package/cache.ts +6 -0
- package/checkout.js +18 -0
- package/checkout.js.map +1 -0
- package/checkout.ts +24 -0
- package/checkout_vocab.js +14 -0
- package/checkout_vocab.js.map +1 -0
- package/checkout_vocab.ts +11 -0
- package/cites.js +189 -0
- package/cites.js.map +1 -0
- package/cites.ts +206 -0
- package/commit.js +128 -0
- package/commit.js.map +1 -0
- package/commit.ts +183 -0
- package/comp.js +94 -0
- package/comp.js.map +1 -0
- package/comp.ts +111 -0
- package/deno.json +1 -0
- package/door.js +88 -0
- package/door.js.map +1 -0
- package/door.ts +142 -0
- package/format.js +43 -0
- package/format.js.map +1 -0
- package/format.ts +49 -0
- package/hold.js +17 -0
- package/hold.js.map +1 -0
- package/hold.ts +21 -0
- package/host.js +1024 -0
- package/host.js.map +1 -0
- package/host.ts +1140 -0
- package/http.js +268 -0
- package/http.js.map +1 -0
- package/http.ts +329 -0
- package/import.js +141 -0
- package/import.js.map +1 -0
- package/import.ts +128 -0
- package/index.js +496 -0
- package/index.js.map +1 -0
- package/index.ts +634 -0
- package/land.js +788 -0
- package/land.js.map +1 -0
- package/land.ts +871 -0
- package/mod.js +73 -0
- package/mod.js.map +1 -0
- package/mod.ts +74 -0
- package/objects.js +331 -0
- package/objects.js.map +1 -0
- package/objects.ts +390 -0
- package/oid.js +50 -0
- package/oid.js.map +1 -0
- package/oid.ts +83 -0
- package/pack-index.js +103 -0
- package/pack-index.js.map +1 -0
- package/pack-index.ts +104 -0
- package/pack-store.js +216 -0
- package/pack-store.js.map +1 -0
- package/pack-store.ts +292 -0
- package/pack.js +121 -0
- package/pack.js.map +1 -0
- package/pack.ts +146 -0
- package/package.json +78 -4
- package/pkt.js +73 -0
- package/pkt.js.map +1 -0
- package/pkt.ts +93 -0
- package/placement.js +44 -0
- package/placement.js.map +1 -0
- package/placement.ts +38 -0
- package/plugin.js +66 -0
- package/plugin.js.map +1 -0
- package/plugin.ts +88 -0
- package/process-cwd.py +57 -0
- package/project.js +142 -0
- package/project.js.map +1 -0
- package/project.ts +151 -0
- package/push.js +401 -0
- package/push.js.map +1 -0
- package/push.ts +451 -0
- package/refs.js +176 -0
- package/refs.js.map +1 -0
- package/refs.ts +285 -0
- package/rewrite.js +176 -0
- package/rewrite.js.map +1 -0
- package/rewrite.ts +173 -0
- package/service.js +104 -0
- package/service.js.map +1 -0
- package/service.ts +194 -0
- package/sha1.js +115 -0
- package/sha1.js.map +1 -0
- package/sha1.ts +146 -0
- package/source.js +100 -0
- package/source.js.map +1 -0
- package/source.ts +116 -0
- package/spare.js +108 -0
- package/spare.js.map +1 -0
- package/spare.ts +94 -0
- package/tools.js +278 -0
- package/tools.js.map +1 -0
- package/tools.ts +310 -0
- package/tree.js +105 -0
- package/tree.js.map +1 -0
- package/tree.ts +132 -0
- package/unpack.js +207 -0
- package/unpack.js.map +1 -0
- package/unpack.ts +210 -0
- package/vocab.js +20 -0
- package/vocab.js.map +1 -0
- package/vocab.json +386 -0
- package/vocab.ts +22 -0
- package/workspace.js +449 -0
- package/workspace.js.map +1 -0
- package/workspace.ts +445 -0
package/http.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// Git's smart HTTP protocol, version 2, read-only: the two endpoints a
|
|
2
|
+
// `git clone` calls, written as a `Request` in and a `Response` out. Whatever
|
|
3
|
+
// mounts them decides what a repository is — a set of refs and an object
|
|
4
|
+
// reader — and this file decides nothing else.
|
|
5
|
+
//
|
|
6
|
+
// GET <repo>/info/refs?service=git-upload-pack what this server supports
|
|
7
|
+
// POST <repo>/git-upload-pack ls-refs, and fetch
|
|
8
|
+
//
|
|
9
|
+
// Version 2 is two round trips and nothing is discovered twice. The
|
|
10
|
+
// advertisement lists only what the server supports — no refs, unlike version
|
|
11
|
+
// 0, which sent every ref to a client that wanted one of them. The refs are
|
|
12
|
+
// the response to `ls-refs`, and a packfile is the response to `fetch`, both
|
|
13
|
+
// POSTed to the one endpoint with a `command=` line saying which.
|
|
14
|
+
//
|
|
15
|
+
// We advertise what we implement. No `shallow`, no `filter`, no
|
|
16
|
+
// `packfile-uris`, no `wait-for-done`, no `object-format=sha256`: a capability
|
|
17
|
+
// named here is one a client may rely on, and a client that is never told
|
|
18
|
+
// about shallow clones never asks for one. `server-option` is named because
|
|
19
|
+
// ignoring the options is the whole of implementing it.
|
|
20
|
+
//
|
|
21
|
+
// A `fetch` gets the objects its wants reach minus the ones its haves reach.
|
|
22
|
+
// Once the client sends `done` or offers a common commit, the response carries
|
|
23
|
+
// a pack. A stateless client that hears only `NAK` can drop the haves it sent.
|
|
24
|
+
//
|
|
25
|
+
// A want must be reachable from A ref. Anything else would serve an object
|
|
26
|
+
// that was unlinked or never published to whoever guessed its id — the same
|
|
27
|
+
// rule as Git's own `uploadpack.allowAnySHA1InWant=false`. The common case is
|
|
28
|
+
// a want that is a ref, checked against a set; only a want that is not a
|
|
29
|
+
// branch tip pays for the walk.
|
|
30
|
+
//
|
|
31
|
+
// The packfile section is side-band framed, as version 2 requires: band 1 is
|
|
32
|
+
// the packfile itself. Band 2 is progress nobody asked for, and band 3 is an
|
|
33
|
+
// error, which we instead send as an `ERR` line before any section, where the
|
|
34
|
+
// client reports it as the server's own words.
|
|
35
|
+
import { concat } from "./oid.js";
|
|
36
|
+
import { BAND, band, DELIM, FLUSH, mark, pkt, read } from "./pkt.js";
|
|
37
|
+
/** What HEAD points at when a repository does not say otherwise. */ export let MAIN = 'refs/heads/main';
|
|
38
|
+
export let DRAFT = 'refs/heads/draft';
|
|
39
|
+
/**
|
|
40
|
+
* What this server supports, in the order Git writes it. `agent` is a courtesy
|
|
41
|
+
* line every implementation sends; the rest is the contract.
|
|
42
|
+
*/ export let CAPS = [
|
|
43
|
+
'version 2',
|
|
44
|
+
'agent=yaks',
|
|
45
|
+
'ls-refs',
|
|
46
|
+
'fetch',
|
|
47
|
+
'object-format=sha1',
|
|
48
|
+
'server-option'
|
|
49
|
+
];
|
|
50
|
+
/** The one service served: a clone's and a fetch's. */ export let SERVICE = 'git-upload-pack';
|
|
51
|
+
export let RECEIVE = 'git-receive-pack';
|
|
52
|
+
let plain = (status, says)=>new Response(says, {
|
|
53
|
+
status,
|
|
54
|
+
headers: {
|
|
55
|
+
'content-type': 'text/plain; charset=utf-8'
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
let git = (type, body)=>new Response(body, {
|
|
59
|
+
headers: {
|
|
60
|
+
'content-type': `application/x-git-upload-pack-${type}`,
|
|
61
|
+
'cache-control': 'no-cache'
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
// The `Git-Protocol` header a version 2 client sends. It holds `key=value`
|
|
65
|
+
// pairs joined by `:`, so the version is looked for among them rather than
|
|
66
|
+
// compared against the whole header value.
|
|
67
|
+
let v2 = (req)=>(req.headers.get('git-protocol') ?? '').split(':').includes('version=2');
|
|
68
|
+
let OLD = 'git: this server speaks protocol v2 only — git 2.26 or newer, or ' + '`git -c protocol.version=2 clone`\n';
|
|
69
|
+
/**
|
|
70
|
+
* The response to `GET <repo>/info/refs?service=git-upload-pack`: the service
|
|
71
|
+
* line smart HTTP opens with, then {@link CAPS}.
|
|
72
|
+
*
|
|
73
|
+
* It has no refs parameter, because a version 2 advertisement carries no refs
|
|
74
|
+
* — that is the point of version 2, and `ls-refs` is where a client asks for
|
|
75
|
+
* them.
|
|
76
|
+
*/ export let advertise = (req)=>{
|
|
77
|
+
if (new URL(req.url).searchParams.get('service') != SERVICE) {
|
|
78
|
+
return plain(404, `git: only ${SERVICE} is served here\n`);
|
|
79
|
+
}
|
|
80
|
+
if (!v2(req)) return plain(400, OLD);
|
|
81
|
+
return git('advertisement', concat([
|
|
82
|
+
pkt(`# service=${SERVICE}\n`),
|
|
83
|
+
mark(FLUSH),
|
|
84
|
+
...CAPS.map((c)=>pkt(c + '\n')),
|
|
85
|
+
mark(FLUSH)
|
|
86
|
+
]));
|
|
87
|
+
};
|
|
88
|
+
// A request body read as packets, un-gzipped if the client gzipped it (Git
|
|
89
|
+
// does, once the haves grow past a kilobyte).
|
|
90
|
+
let asked = async (req)=>{
|
|
91
|
+
let body = req.body ?? new Blob([]).stream();
|
|
92
|
+
if (req.headers.get('content-encoding') == 'gzip') {
|
|
93
|
+
body = body.pipeThrough(new DecompressionStream('gzip'));
|
|
94
|
+
}
|
|
95
|
+
return read(new Uint8Array(await new Response(body).arrayBuffer()));
|
|
96
|
+
};
|
|
97
|
+
let parse = (pkts)=>{
|
|
98
|
+
let caps = [];
|
|
99
|
+
let args = [];
|
|
100
|
+
let past = false;
|
|
101
|
+
for (let p of pkts){
|
|
102
|
+
if (typeof p == 'number') {
|
|
103
|
+
if (p == FLUSH) break;
|
|
104
|
+
if (p == DELIM) past = true;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
;
|
|
108
|
+
(past ? args : caps).push(p);
|
|
109
|
+
}
|
|
110
|
+
let said = (list, key)=>list.find((c)=>c.startsWith(key + '='))?.slice(key.length + 1);
|
|
111
|
+
return {
|
|
112
|
+
command: said(caps, 'command') ?? '',
|
|
113
|
+
caps,
|
|
114
|
+
args
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
let err = (says)=>git('result', pkt(`ERR ${says}`));
|
|
118
|
+
let body = (lines)=>git('result', concat([
|
|
119
|
+
...lines,
|
|
120
|
+
mark(FLUSH)
|
|
121
|
+
]));
|
|
122
|
+
let value = (args, key)=>args.filter((a)=>a.startsWith(key + ' ')).map((a)=>a.slice(key.length + 1));
|
|
123
|
+
// `ls-refs`: every ref, HEAD first. `symrefs` asks what HEAD points at;
|
|
124
|
+
// `peel` asks for a tag's own target, which does nothing until something here
|
|
125
|
+
// writes a tag; `ref-prefix` is a filter the client offers as a hint and the
|
|
126
|
+
// server is free to ignore — we honour it, because it is one comparison.
|
|
127
|
+
let lsRefs = async (args, refs)=>{
|
|
128
|
+
let all = await refs.list();
|
|
129
|
+
let head = refs.head ?? MAIN;
|
|
130
|
+
let tip = all.find((r)=>r.name == head);
|
|
131
|
+
let sorted = [
|
|
132
|
+
...all
|
|
133
|
+
].sort((a, b)=>a.name < b.name ? -1 : 1);
|
|
134
|
+
let listed = tip ? [
|
|
135
|
+
{
|
|
136
|
+
...tip,
|
|
137
|
+
name: 'HEAD',
|
|
138
|
+
sym: head
|
|
139
|
+
},
|
|
140
|
+
...sorted
|
|
141
|
+
] : sorted;
|
|
142
|
+
let prefixes = value(args, 'ref-prefix');
|
|
143
|
+
let symrefs = args.includes('symrefs');
|
|
144
|
+
return body(listed.filter((r)=>!prefixes.length || prefixes.some((p)=>r.name.startsWith(p))).map((r)=>pkt(`${r.oid} ${r.name}` + (symrefs && 'sym' in r ? ` symref-target:${r.sym}` : '') + '\n')));
|
|
145
|
+
};
|
|
146
|
+
// Every byte of the response to a `fetch` that is getting a packfile: the
|
|
147
|
+
// section header, the packfile in band 1, and the flush that ends the
|
|
148
|
+
// response. The packfile arrives in whatever chunks ./pack.ts wrote it in, so
|
|
149
|
+
// it is re-cut here to the largest a packet can hold — one side-band packet
|
|
150
|
+
// per zlib chunk would spend five bytes on each one.
|
|
151
|
+
async function* packfile(prepare, progress, failed, ready = false) {
|
|
152
|
+
if (ready) {
|
|
153
|
+
yield pkt('acknowledgments\n');
|
|
154
|
+
yield pkt('ready\n');
|
|
155
|
+
yield mark(DELIM);
|
|
156
|
+
}
|
|
157
|
+
yield pkt('packfile\n');
|
|
158
|
+
let reader;
|
|
159
|
+
try {
|
|
160
|
+
let packing = prepare();
|
|
161
|
+
// Keep the HTTP connection active while a large history is enumerated.
|
|
162
|
+
// Wait on the same pack computation after each progress packet.
|
|
163
|
+
if (progress) {
|
|
164
|
+
for(;;){
|
|
165
|
+
let cancel = ()=>{};
|
|
166
|
+
let ready = await Promise.race([
|
|
167
|
+
packing,
|
|
168
|
+
new Promise((resolve)=>{
|
|
169
|
+
let timer = setTimeout(resolve, 5_000);
|
|
170
|
+
cancel = ()=>clearTimeout(timer);
|
|
171
|
+
})
|
|
172
|
+
]).finally(cancel);
|
|
173
|
+
if (ready) break;
|
|
174
|
+
yield band(2, new TextEncoder().encode('Enumerating objects…\r'));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
reader = (await packing).getReader();
|
|
178
|
+
let rest = new Uint8Array(0);
|
|
179
|
+
while(true){
|
|
180
|
+
let got = await reader.read();
|
|
181
|
+
if (got.done) break;
|
|
182
|
+
let bytes = got.value;
|
|
183
|
+
if (rest.length) {
|
|
184
|
+
let n = BAND - rest.length;
|
|
185
|
+
rest = concat([
|
|
186
|
+
rest,
|
|
187
|
+
bytes.subarray(0, n)
|
|
188
|
+
]);
|
|
189
|
+
if (rest.length < BAND) continue;
|
|
190
|
+
yield band(1, rest);
|
|
191
|
+
bytes = bytes.subarray(n);
|
|
192
|
+
}
|
|
193
|
+
let i = 0;
|
|
194
|
+
for(; i + BAND <= bytes.length; i += BAND){
|
|
195
|
+
yield band(1, bytes.subarray(i, i + BAND));
|
|
196
|
+
}
|
|
197
|
+
rest = bytes.subarray(i);
|
|
198
|
+
}
|
|
199
|
+
if (rest.length) yield band(1, rest);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
failed?.(error);
|
|
202
|
+
yield band(3, new TextEncoder().encode('Could not prepare the Git pack.\n'));
|
|
203
|
+
} finally{
|
|
204
|
+
if (reader) {
|
|
205
|
+
await reader.cancel().catch(()=>{});
|
|
206
|
+
reader.releaseLock();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
yield mark(FLUSH);
|
|
210
|
+
}
|
|
211
|
+
let streamed = (it)=>new ReadableStream({
|
|
212
|
+
pull: async (c)=>{
|
|
213
|
+
let { value, done } = await it.next();
|
|
214
|
+
done ? c.close() : c.enqueue(value);
|
|
215
|
+
},
|
|
216
|
+
cancel: ()=>void it.return(undefined)
|
|
217
|
+
});
|
|
218
|
+
// `fetch`: the wants minus the haves, as a packfile — or the acknowledgments
|
|
219
|
+
// section, when the client has not finished listing what it holds.
|
|
220
|
+
let fetch = async (args, refs, from, failed)=>{
|
|
221
|
+
let wants = [
|
|
222
|
+
...new Set(value(args, 'want'))
|
|
223
|
+
];
|
|
224
|
+
let haves = value(args, 'have');
|
|
225
|
+
let negotiating = !args.includes('done');
|
|
226
|
+
let nak = ()=>body([
|
|
227
|
+
pkt('acknowledgments\n'),
|
|
228
|
+
pkt('NAK\n')
|
|
229
|
+
]);
|
|
230
|
+
if (negotiating && (!haves.length || !wants.length)) {
|
|
231
|
+
return nak();
|
|
232
|
+
}
|
|
233
|
+
if (!wants.length) return body([]);
|
|
234
|
+
let tips = new Set((await refs.list()).map((r)=>r.oid));
|
|
235
|
+
let stray = wants.filter((w)=>!tips.has(w));
|
|
236
|
+
if (stray.length) {
|
|
237
|
+
let ours = new Set(await from.reach([
|
|
238
|
+
...tips
|
|
239
|
+
]));
|
|
240
|
+
let bad = stray.find((w)=>!ours.has(w));
|
|
241
|
+
if (bad) return err(`upload-pack: not our ref ${bad}`);
|
|
242
|
+
}
|
|
243
|
+
if (negotiating) {
|
|
244
|
+
let known = from.common ? await from.common(wants, haves) : haves.filter(Set.prototype.has, new Set(await from.reach(wants)));
|
|
245
|
+
if (!known.length) return nak();
|
|
246
|
+
haves = known;
|
|
247
|
+
}
|
|
248
|
+
return git('result', streamed(packfile(()=>from.pack(wants, haves), !args.includes('no-progress'), failed, negotiating)));
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* The response to `POST <repo>/git-upload-pack`: one command in, one response
|
|
252
|
+
* out.
|
|
253
|
+
*
|
|
254
|
+
* The caller has already decided that this repository may be read
|
|
255
|
+
* (./door.ts).
|
|
256
|
+
*/ export let uploadPack = async (req, refs, from, failed)=>{
|
|
257
|
+
if (req.method != 'POST') return plain(405, 'git: POST\n');
|
|
258
|
+
if (!v2(req)) return plain(400, OLD);
|
|
259
|
+
let { command, caps, args } = parse(await asked(req));
|
|
260
|
+
let format = caps.find((c)=>c.startsWith('object-format='));
|
|
261
|
+
if (format && format != 'object-format=sha1') {
|
|
262
|
+
return err(`upload-pack: ${format} is not served here, only sha1`);
|
|
263
|
+
}
|
|
264
|
+
if (command == 'ls-refs') return lsRefs(args, refs);
|
|
265
|
+
if (command == 'fetch') return fetch(args, refs, from, failed);
|
|
266
|
+
return err(`upload-pack: unknown command ${command || '(none)'}`);
|
|
267
|
+
};
|
|
268
|
+
//# sourceMappingURL=http.js.map
|
package/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sources":["./http.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,8EAA8E;AAC9E,yEAAyE;AACzE,+CAA+C;AAC/C,EAAE;AACF,8EAA8E;AAC9E,uEAAuE;AACvE,EAAE;AACF,oEAAoE;AACpE,8EAA8E;AAC9E,4EAA4E;AAC5E,6EAA6E;AAC7E,kEAAkE;AAClE,EAAE;AACF,gEAAgE;AAChE,+EAA+E;AAC/E,0EAA0E;AAC1E,4EAA4E;AAC5E,wDAAwD;AACxD,EAAE;AACF,6EAA6E;AAC7E,+EAA+E;AAC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I,IAAI,MAAM,IAAI,QAAQ,OAAO,MAAM,KAAK;EAC5C,IAAI,CAAC,GAAG,MAAM,OAAO,MAAM,KAAK;EAChC,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,MAAM;EAChD,IAAI,SAAS,KAAK,IAAI,CAAC,CAAC,IAAM,EAAE,UAAU,CAAC;EAC3C,IAAI,UAAU,UAAU,sBAAsB;IAC5C,OAAO,IAAI,CAAC,aAAa,EAAE,OAAO,8BAA8B,CAAC;EACnE;EACA,IAAI,WAAW,WAAW,OAAO,OAAO,MAAM;EAC9C,IAAI,WAAW,SAAS,OAAO,MAAM,MAAM,MAAM,MAAM;EACvD,OAAO,IAAI,CAAC,6BAA6B,EAAE,WAAW,UAAU;AAClE,EAAC"}
|
package/http.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// Git's smart HTTP protocol, version 2, read-only: the two endpoints a
|
|
2
|
+
// `git clone` calls, written as a `Request` in and a `Response` out. Whatever
|
|
3
|
+
// mounts them decides what a repository is — a set of refs and an object
|
|
4
|
+
// reader — and this file decides nothing else.
|
|
5
|
+
//
|
|
6
|
+
// GET <repo>/info/refs?service=git-upload-pack what this server supports
|
|
7
|
+
// POST <repo>/git-upload-pack ls-refs, and fetch
|
|
8
|
+
//
|
|
9
|
+
// Version 2 is two round trips and nothing is discovered twice. The
|
|
10
|
+
// advertisement lists only what the server supports — no refs, unlike version
|
|
11
|
+
// 0, which sent every ref to a client that wanted one of them. The refs are
|
|
12
|
+
// the response to `ls-refs`, and a packfile is the response to `fetch`, both
|
|
13
|
+
// POSTed to the one endpoint with a `command=` line saying which.
|
|
14
|
+
//
|
|
15
|
+
// We advertise what we implement. No `shallow`, no `filter`, no
|
|
16
|
+
// `packfile-uris`, no `wait-for-done`, no `object-format=sha256`: a capability
|
|
17
|
+
// named here is one a client may rely on, and a client that is never told
|
|
18
|
+
// about shallow clones never asks for one. `server-option` is named because
|
|
19
|
+
// ignoring the options is the whole of implementing it.
|
|
20
|
+
//
|
|
21
|
+
// A `fetch` gets the objects its wants reach minus the ones its haves reach.
|
|
22
|
+
// Once the client sends `done` or offers a common commit, the response carries
|
|
23
|
+
// a pack. A stateless client that hears only `NAK` can drop the haves it sent.
|
|
24
|
+
//
|
|
25
|
+
// A want must be reachable from A ref. Anything else would serve an object
|
|
26
|
+
// that was unlinked or never published to whoever guessed its id — the same
|
|
27
|
+
// rule as Git's own `uploadpack.allowAnySHA1InWant=false`. The common case is
|
|
28
|
+
// a want that is a ref, checked against a set; only a want that is not a
|
|
29
|
+
// branch tip pays for the walk.
|
|
30
|
+
//
|
|
31
|
+
// The packfile section is side-band framed, as version 2 requires: band 1 is
|
|
32
|
+
// the packfile itself. Band 2 is progress nobody asked for, and band 3 is an
|
|
33
|
+
// error, which we instead send as an `ERR` line before any section, where the
|
|
34
|
+
// client reports it as the server's own words.
|
|
35
|
+
|
|
36
|
+
import type { Objects } from './objects.ts'
|
|
37
|
+
import { concat } from './oid.js'
|
|
38
|
+
import { BAND, band, DELIM, FLUSH, mark, type Pkt, pkt, read } from './pkt.js'
|
|
39
|
+
|
|
40
|
+
/** One ref: its full name, and the object it points at. */
|
|
41
|
+
export type Ref = { name: string; oid: string }
|
|
42
|
+
|
|
43
|
+
/** A repository's refs, in the form these handlers need them. */
|
|
44
|
+
export type Refs = {
|
|
45
|
+
/** every ref, by full name: `refs/heads/main`, not `main` */
|
|
46
|
+
list: () => Promise<Ref[]>
|
|
47
|
+
/** the ref HEAD symbolically points at; `refs/heads/main` by default */
|
|
48
|
+
head?: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What HEAD points at when a repository does not say otherwise. */
|
|
52
|
+
export let MAIN = 'refs/heads/main'
|
|
53
|
+
export let DRAFT = 'refs/heads/draft'
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* What this server supports, in the order Git writes it. `agent` is a courtesy
|
|
57
|
+
* line every implementation sends; the rest is the contract.
|
|
58
|
+
*/
|
|
59
|
+
export let CAPS: string[] = [
|
|
60
|
+
'version 2',
|
|
61
|
+
'agent=yaks',
|
|
62
|
+
'ls-refs',
|
|
63
|
+
'fetch',
|
|
64
|
+
'object-format=sha1',
|
|
65
|
+
'server-option',
|
|
66
|
+
]
|
|
67
|
+
|
|
68
|
+
/** The one service served: a clone's and a fetch's. */
|
|
69
|
+
export let SERVICE = 'git-upload-pack'
|
|
70
|
+
|
|
71
|
+
export let RECEIVE = 'git-receive-pack'
|
|
72
|
+
|
|
73
|
+
let plain = (status: number, says: string) =>
|
|
74
|
+
new Response(says, {
|
|
75
|
+
status,
|
|
76
|
+
headers: { 'content-type': 'text/plain; charset=utf-8' },
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
let git = (type: string, body: BodyInit) =>
|
|
80
|
+
new Response(body, {
|
|
81
|
+
headers: {
|
|
82
|
+
'content-type': `application/x-git-upload-pack-${type}`,
|
|
83
|
+
'cache-control': 'no-cache',
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// The `Git-Protocol` header a version 2 client sends. It holds `key=value`
|
|
88
|
+
// pairs joined by `:`, so the version is looked for among them rather than
|
|
89
|
+
// compared against the whole header value.
|
|
90
|
+
let v2 = (req: Request) =>
|
|
91
|
+
(req.headers.get('git-protocol') ?? '').split(':').includes('version=2')
|
|
92
|
+
|
|
93
|
+
let OLD = 'git: this server speaks protocol v2 only — git 2.26 or newer, or ' +
|
|
94
|
+
'`git -c protocol.version=2 clone`\n'
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The response to `GET <repo>/info/refs?service=git-upload-pack`: the service
|
|
98
|
+
* line smart HTTP opens with, then {@link CAPS}.
|
|
99
|
+
*
|
|
100
|
+
* It has no refs parameter, because a version 2 advertisement carries no refs
|
|
101
|
+
* — that is the point of version 2, and `ls-refs` is where a client asks for
|
|
102
|
+
* them.
|
|
103
|
+
*/
|
|
104
|
+
export let advertise = (req: Request): Response => {
|
|
105
|
+
if (new URL(req.url).searchParams.get('service') != SERVICE) {
|
|
106
|
+
return plain(404, `git: only ${SERVICE} is served here\n`)
|
|
107
|
+
}
|
|
108
|
+
if (!v2(req)) return plain(400, OLD)
|
|
109
|
+
return git(
|
|
110
|
+
'advertisement',
|
|
111
|
+
concat([
|
|
112
|
+
pkt(`# service=${SERVICE}\n`),
|
|
113
|
+
mark(FLUSH),
|
|
114
|
+
...CAPS.map((c) => pkt(c + '\n')),
|
|
115
|
+
mark(FLUSH),
|
|
116
|
+
]),
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// A request body read as packets, un-gzipped if the client gzipped it (Git
|
|
121
|
+
// does, once the haves grow past a kilobyte).
|
|
122
|
+
let asked = async (req: Request): Promise<Pkt[]> => {
|
|
123
|
+
let body = req.body ?? new Blob([]).stream()
|
|
124
|
+
if (req.headers.get('content-encoding') == 'gzip') {
|
|
125
|
+
body = body.pipeThrough(new DecompressionStream('gzip'))
|
|
126
|
+
}
|
|
127
|
+
return read(new Uint8Array(await new Response(body).arrayBuffer()))
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** A command as it arrives: its name, the capabilities before the delimiter,
|
|
131
|
+
* and the arguments after it. */
|
|
132
|
+
type Said = { command: string; caps: string[]; args: string[] }
|
|
133
|
+
|
|
134
|
+
let parse = (pkts: Pkt[]): Said => {
|
|
135
|
+
let caps: string[] = []
|
|
136
|
+
let args: string[] = []
|
|
137
|
+
let past = false
|
|
138
|
+
for (let p of pkts) {
|
|
139
|
+
if (typeof p == 'number') {
|
|
140
|
+
if (p == FLUSH) break
|
|
141
|
+
if (p == DELIM) past = true
|
|
142
|
+
continue
|
|
143
|
+
}
|
|
144
|
+
;(past ? args : caps).push(p)
|
|
145
|
+
}
|
|
146
|
+
let said = (list: string[], key: string) =>
|
|
147
|
+
list.find((c) => c.startsWith(key + '='))?.slice(key.length + 1)
|
|
148
|
+
return { command: said(caps, 'command') ?? '', caps, args }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
let err = (says: string) => git('result', pkt(`ERR ${says}`))
|
|
152
|
+
|
|
153
|
+
let body = (lines: Uint8Array[]) =>
|
|
154
|
+
git('result', concat([...lines, mark(FLUSH)]))
|
|
155
|
+
|
|
156
|
+
let value = (args: string[], key: string) =>
|
|
157
|
+
args.filter((a) => a.startsWith(key + ' ')).map((a) =>
|
|
158
|
+
a.slice(key.length + 1)
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
// `ls-refs`: every ref, HEAD first. `symrefs` asks what HEAD points at;
|
|
162
|
+
// `peel` asks for a tag's own target, which does nothing until something here
|
|
163
|
+
// writes a tag; `ref-prefix` is a filter the client offers as a hint and the
|
|
164
|
+
// server is free to ignore — we honour it, because it is one comparison.
|
|
165
|
+
let lsRefs = async (args: string[], refs: Refs): Promise<Response> => {
|
|
166
|
+
let all = await refs.list()
|
|
167
|
+
let head = refs.head ?? MAIN
|
|
168
|
+
let tip = all.find((r) => r.name == head)
|
|
169
|
+
let sorted = [...all].sort((a, b) => a.name < b.name ? -1 : 1)
|
|
170
|
+
let listed = tip ? [{ ...tip, name: 'HEAD', sym: head }, ...sorted] : sorted
|
|
171
|
+
let prefixes = value(args, 'ref-prefix')
|
|
172
|
+
let symrefs = args.includes('symrefs')
|
|
173
|
+
return body(
|
|
174
|
+
listed
|
|
175
|
+
.filter((r) =>
|
|
176
|
+
!prefixes.length || prefixes.some((p) => r.name.startsWith(p))
|
|
177
|
+
)
|
|
178
|
+
.map((r) =>
|
|
179
|
+
pkt(
|
|
180
|
+
`${r.oid} ${r.name}` +
|
|
181
|
+
(symrefs && 'sym' in r ? ` symref-target:${r.sym}` : '') + '\n',
|
|
182
|
+
)
|
|
183
|
+
),
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Every byte of the response to a `fetch` that is getting a packfile: the
|
|
188
|
+
// section header, the packfile in band 1, and the flush that ends the
|
|
189
|
+
// response. The packfile arrives in whatever chunks ./pack.ts wrote it in, so
|
|
190
|
+
// it is re-cut here to the largest a packet can hold — one side-band packet
|
|
191
|
+
// per zlib chunk would spend five bytes on each one.
|
|
192
|
+
async function* packfile(
|
|
193
|
+
prepare: () => Promise<ReadableStream<Uint8Array>>,
|
|
194
|
+
progress: boolean,
|
|
195
|
+
failed?: (reason: unknown) => void,
|
|
196
|
+
ready = false,
|
|
197
|
+
) {
|
|
198
|
+
if (ready) {
|
|
199
|
+
yield pkt('acknowledgments\n')
|
|
200
|
+
yield pkt('ready\n')
|
|
201
|
+
yield mark(DELIM)
|
|
202
|
+
}
|
|
203
|
+
yield pkt('packfile\n')
|
|
204
|
+
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined
|
|
205
|
+
try {
|
|
206
|
+
let packing = prepare()
|
|
207
|
+
// Keep the HTTP connection active while a large history is enumerated.
|
|
208
|
+
// Wait on the same pack computation after each progress packet.
|
|
209
|
+
if (progress) {
|
|
210
|
+
for (;;) {
|
|
211
|
+
let cancel = () => {}
|
|
212
|
+
let ready = await Promise.race([
|
|
213
|
+
packing,
|
|
214
|
+
new Promise<undefined>((resolve) => {
|
|
215
|
+
let timer = setTimeout(resolve, 5_000)
|
|
216
|
+
cancel = () => clearTimeout(timer)
|
|
217
|
+
}),
|
|
218
|
+
]).finally(cancel)
|
|
219
|
+
if (ready) break
|
|
220
|
+
yield band(2, new TextEncoder().encode('Enumerating objects…\r'))
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
reader = (await packing).getReader()
|
|
224
|
+
let rest: Uint8Array = new Uint8Array(0)
|
|
225
|
+
while (true) {
|
|
226
|
+
let got = await reader.read()
|
|
227
|
+
if (got.done) break
|
|
228
|
+
let bytes = got.value
|
|
229
|
+
if (rest.length) {
|
|
230
|
+
let n = BAND - rest.length
|
|
231
|
+
rest = concat([rest, bytes.subarray(0, n)])
|
|
232
|
+
if (rest.length < BAND) continue
|
|
233
|
+
yield band(1, rest)
|
|
234
|
+
bytes = bytes.subarray(n)
|
|
235
|
+
}
|
|
236
|
+
let i = 0
|
|
237
|
+
for (; i + BAND <= bytes.length; i += BAND) {
|
|
238
|
+
yield band(1, bytes.subarray(i, i + BAND))
|
|
239
|
+
}
|
|
240
|
+
rest = bytes.subarray(i)
|
|
241
|
+
}
|
|
242
|
+
if (rest.length) yield band(1, rest)
|
|
243
|
+
} catch (error) {
|
|
244
|
+
failed?.(error)
|
|
245
|
+
yield band(3, new TextEncoder().encode('Could not prepare the Git pack.\n'))
|
|
246
|
+
} finally {
|
|
247
|
+
if (reader) {
|
|
248
|
+
await reader.cancel().catch(() => {})
|
|
249
|
+
reader.releaseLock()
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
yield mark(FLUSH)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
let streamed = (it: AsyncGenerator<Uint8Array>) =>
|
|
256
|
+
new ReadableStream<Uint8Array>({
|
|
257
|
+
pull: async (c) => {
|
|
258
|
+
let { value, done } = await it.next()
|
|
259
|
+
done ? c.close() : c.enqueue(value)
|
|
260
|
+
},
|
|
261
|
+
cancel: () => void it.return(undefined),
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
// `fetch`: the wants minus the haves, as a packfile — or the acknowledgments
|
|
265
|
+
// section, when the client has not finished listing what it holds.
|
|
266
|
+
let fetch = async (
|
|
267
|
+
args: string[],
|
|
268
|
+
refs: Refs,
|
|
269
|
+
from: Objects,
|
|
270
|
+
failed?: (reason: unknown) => void,
|
|
271
|
+
): Promise<Response> => {
|
|
272
|
+
let wants = [...new Set(value(args, 'want'))]
|
|
273
|
+
let haves = value(args, 'have')
|
|
274
|
+
let negotiating = !args.includes('done')
|
|
275
|
+
let nak = () => body([pkt('acknowledgments\n'), pkt('NAK\n')])
|
|
276
|
+
if (negotiating && (!haves.length || !wants.length)) {
|
|
277
|
+
return nak()
|
|
278
|
+
}
|
|
279
|
+
if (!wants.length) return body([])
|
|
280
|
+
|
|
281
|
+
let tips = new Set((await refs.list()).map((r) => r.oid))
|
|
282
|
+
let stray = wants.filter((w) => !tips.has(w))
|
|
283
|
+
if (stray.length) {
|
|
284
|
+
let ours = new Set(await from.reach([...tips]))
|
|
285
|
+
let bad = stray.find((w) => !ours.has(w))
|
|
286
|
+
if (bad) return err(`upload-pack: not our ref ${bad}`)
|
|
287
|
+
}
|
|
288
|
+
if (negotiating) {
|
|
289
|
+
let known = from.common
|
|
290
|
+
? await from.common(wants, haves)
|
|
291
|
+
: haves.filter(Set.prototype.has, new Set(await from.reach(wants)))
|
|
292
|
+
if (!known.length) return nak()
|
|
293
|
+
haves = known
|
|
294
|
+
}
|
|
295
|
+
return git(
|
|
296
|
+
'result',
|
|
297
|
+
streamed(packfile(
|
|
298
|
+
() => from.pack(wants, haves),
|
|
299
|
+
!args.includes('no-progress'),
|
|
300
|
+
failed,
|
|
301
|
+
negotiating,
|
|
302
|
+
)),
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The response to `POST <repo>/git-upload-pack`: one command in, one response
|
|
308
|
+
* out.
|
|
309
|
+
*
|
|
310
|
+
* The caller has already decided that this repository may be read
|
|
311
|
+
* (./door.ts).
|
|
312
|
+
*/
|
|
313
|
+
export let uploadPack = async (
|
|
314
|
+
req: Request,
|
|
315
|
+
refs: Refs,
|
|
316
|
+
from: Objects,
|
|
317
|
+
failed?: (reason: unknown) => void,
|
|
318
|
+
): Promise<Response> => {
|
|
319
|
+
if (req.method != 'POST') return plain(405, 'git: POST\n')
|
|
320
|
+
if (!v2(req)) return plain(400, OLD)
|
|
321
|
+
let { command, caps, args } = parse(await asked(req))
|
|
322
|
+
let format = caps.find((c) => c.startsWith('object-format='))
|
|
323
|
+
if (format && format != 'object-format=sha1') {
|
|
324
|
+
return err(`upload-pack: ${format} is not served here, only sha1`)
|
|
325
|
+
}
|
|
326
|
+
if (command == 'ls-refs') return lsRefs(args, refs)
|
|
327
|
+
if (command == 'fetch') return fetch(args, refs, from, failed)
|
|
328
|
+
return err(`upload-pack: unknown command ${command || '(none)'}`)
|
|
329
|
+
}
|