@leed-ai/git-gateway-testkit 0.0.0-stage → 0.1.0
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 +201 -0
- package/README.md +19 -2
- package/dist/contract-suite/ancestry-cases.d.ts +4 -0
- package/dist/contract-suite/ancestry-cases.js +49 -0
- package/dist/contract-suite/ancestry-cases.js.map +1 -0
- package/dist/contract-suite/backend-case.d.ts +35 -0
- package/dist/contract-suite/backend-case.js +17 -0
- package/dist/contract-suite/backend-case.js.map +1 -0
- package/dist/contract-suite/backend-contract-suite.d.ts +44 -0
- package/dist/contract-suite/backend-contract-suite.js +59 -0
- package/dist/contract-suite/backend-contract-suite.js.map +1 -0
- package/dist/contract-suite/backend-fixtures.d.ts +25 -0
- package/dist/contract-suite/backend-fixtures.js +79 -0
- package/dist/contract-suite/backend-fixtures.js.map +1 -0
- package/dist/contract-suite/content-cases.d.ts +4 -0
- package/dist/contract-suite/content-cases.js +31 -0
- package/dist/contract-suite/content-cases.js.map +1 -0
- package/dist/contract-suite/lifecycle-cases.d.ts +4 -0
- package/dist/contract-suite/lifecycle-cases.js +86 -0
- package/dist/contract-suite/lifecycle-cases.js.map +1 -0
- package/dist/contract-suite/push-cases.d.ts +4 -0
- package/dist/contract-suite/push-cases.js +92 -0
- package/dist/contract-suite/push-cases.js.map +1 -0
- package/dist/contract-suite/push-helpers.d.ts +17 -0
- package/dist/contract-suite/push-helpers.js +47 -0
- package/dist/contract-suite/push-helpers.js.map +1 -0
- package/dist/contract-suite/read-cases.d.ts +4 -0
- package/dist/contract-suite/read-cases.js +186 -0
- package/dist/contract-suite/read-cases.js.map +1 -0
- package/dist/contract-suite/transport-cases.d.ts +4 -0
- package/dist/contract-suite/transport-cases.js +105 -0
- package/dist/contract-suite/transport-cases.js.map +1 -0
- package/dist/git-binary.d.ts +19 -0
- package/dist/git-binary.js +54 -0
- package/dist/git-binary.js.map +1 -0
- package/dist/git-sandbox.d.ts +33 -0
- package/dist/git-sandbox.js +76 -0
- package/dist/git-sandbox.js.map +1 -0
- package/dist/memory/memory-backend.d.ts +45 -0
- package/dist/memory/memory-backend.js +154 -0
- package/dist/memory/memory-backend.js.map +1 -0
- package/dist/memory/memory-reads.d.ts +9 -0
- package/dist/memory/memory-reads.js +39 -0
- package/dist/memory/memory-reads.js.map +1 -0
- package/dist/memory/memory-repository.d.ts +38 -0
- package/dist/memory/memory-repository.js +97 -0
- package/dist/memory/memory-repository.js.map +1 -0
- package/dist/memory/memory-tokens.d.ts +28 -0
- package/dist/memory/memory-tokens.js +59 -0
- package/dist/memory/memory-tokens.js.map +1 -0
- package/dist/memory/pack-selection.d.ts +30 -0
- package/dist/memory/pack-selection.js +104 -0
- package/dist/memory/pack-selection.js.map +1 -0
- package/dist/memory/receive-pack-service.d.ts +15 -0
- package/dist/memory/receive-pack-service.js +126 -0
- package/dist/memory/receive-pack-service.js.map +1 -0
- package/dist/memory/smart-http-server.d.ts +15 -0
- package/dist/memory/smart-http-server.js +65 -0
- package/dist/memory/smart-http-server.js.map +1 -0
- package/dist/memory/upload-pack-service.d.ts +15 -0
- package/dist/memory/upload-pack-service.js +123 -0
- package/dist/memory/upload-pack-service.js.map +1 -0
- package/dist/node/serve-fetch-handler.d.ts +16 -0
- package/dist/node/serve-fetch-handler.js +52 -0
- package/dist/node/serve-fetch-handler.js.map +1 -0
- package/dist/node/serve-streaming.d.ts +16 -0
- package/dist/node/serve-streaming.js +87 -0
- package/dist/node/serve-streaming.js.map +1 -0
- package/package.json +175 -4
- package/src/contract-suite/ancestry-cases.ts +56 -0
- package/src/contract-suite/backend-case.ts +53 -0
- package/src/contract-suite/backend-contract-suite.ts +89 -0
- package/src/contract-suite/backend-fixtures.ts +89 -0
- package/src/contract-suite/content-cases.ts +55 -0
- package/src/contract-suite/lifecycle-cases.ts +121 -0
- package/src/contract-suite/push-cases.ts +123 -0
- package/src/contract-suite/push-helpers.ts +63 -0
- package/src/contract-suite/read-cases.ts +213 -0
- package/src/contract-suite/transport-cases.ts +134 -0
- package/src/git-binary.ts +65 -0
- package/src/git-sandbox.ts +87 -0
- package/src/memory/memory-backend.ts +188 -0
- package/src/memory/memory-reads.ts +52 -0
- package/src/memory/memory-repository.ts +115 -0
- package/src/memory/memory-tokens.ts +64 -0
- package/src/memory/pack-selection.ts +127 -0
- package/src/memory/receive-pack-service.ts +141 -0
- package/src/memory/smart-http-server.ts +78 -0
- package/src/memory/upload-pack-service.ts +132 -0
- package/src/node/serve-fetch-handler.ts +71 -0
- package/src/node/serve-streaming.ts +107 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { ZERO_SHA } from '@leed-ai/git-gateway-contract/schemas/sha';
|
|
3
|
+
import { streamOf } from '@leed-ai/git-gateway-core/bytes';
|
|
4
|
+
import { isCoreError } from '@leed-ai/git-gateway-core/core-error';
|
|
5
|
+
import { readPack } from '@leed-ai/git-gateway-core/pack/read-pack';
|
|
6
|
+
import { readLengthHeader } from '@leed-ai/git-gateway-core/pkt-line/length-header';
|
|
7
|
+
import { parseReceivePackRequest } from '@leed-ai/git-gateway-core/receive-pack/parse-request';
|
|
8
|
+
import {
|
|
9
|
+
encodeErrPacket,
|
|
10
|
+
encodeReportStatus,
|
|
11
|
+
reportStatusFraming,
|
|
12
|
+
} from '@leed-ai/git-gateway-core/receive-pack/report-status';
|
|
13
|
+
import type { ReceivePackCommand, ReceivePackRequest } from '@leed-ai/git-gateway-core/schemas/receive-pack';
|
|
14
|
+
import type { RefStatus } from '@leed-ai/git-gateway-core/schemas/report-status';
|
|
15
|
+
import { encodeRefAdvertisement } from '@leed-ai/git-gateway-core/smart-http/ref-advertisement';
|
|
16
|
+
|
|
17
|
+
import type { MemoryRepository } from './memory-repository.ts';
|
|
18
|
+
|
|
19
|
+
/** The capabilities the in-memory receive-pack offers, as git's own receive-pack does. */
|
|
20
|
+
export const RECEIVE_PACK_CAPABILITIES: readonly string[] = [
|
|
21
|
+
'report-status',
|
|
22
|
+
'delete-refs',
|
|
23
|
+
'side-band-64k',
|
|
24
|
+
'quiet',
|
|
25
|
+
'atomic',
|
|
26
|
+
'ofs-delta',
|
|
27
|
+
'push-options',
|
|
28
|
+
'object-format=sha1',
|
|
29
|
+
'agent=git-gateway-testkit',
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
/** The receive-pack ref advertisement: every ref in name order (no HEAD, as git sends it). */
|
|
33
|
+
export function receivePackAdvertisement(repo: MemoryRepository): Uint8Array {
|
|
34
|
+
return encodeRefAdvertisement({
|
|
35
|
+
service: 'git-receive-pack',
|
|
36
|
+
refs: [...repo.refs.keys()]
|
|
37
|
+
.toSorted()
|
|
38
|
+
.map((name) => ({ name, sha: repo.refs.get(name) ?? '', peeled: null })),
|
|
39
|
+
capabilities: [...RECEIVE_PACK_CAPABILITIES],
|
|
40
|
+
symrefs: {},
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Where the pack starts: after the shallow lines, the command list and the push options. */
|
|
45
|
+
function packOffset(body: Uint8Array, request: ReceivePackRequest): number {
|
|
46
|
+
let flushes = request.capabilities.includes('push-options') ? 2 : 1;
|
|
47
|
+
let offset = 0;
|
|
48
|
+
while (flushes > 0 && offset + 4 <= body.length) {
|
|
49
|
+
const header = readLengthHeader(body.subarray(offset, offset + 4), offset);
|
|
50
|
+
if (header.kind === 'special') {
|
|
51
|
+
flushes -= 1;
|
|
52
|
+
offset += 4;
|
|
53
|
+
} else offset += 4 + header.payloadLength;
|
|
54
|
+
}
|
|
55
|
+
return offset;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Why a command cannot apply to the repository as it stands, or null when it can. */
|
|
59
|
+
function refuse(repo: MemoryRepository, command: ReceivePackCommand): string | null {
|
|
60
|
+
const current = repo.refs.get(command.ref) ?? ZERO_SHA;
|
|
61
|
+
if (command.oldSha === ZERO_SHA && command.newSha === ZERO_SHA) return 'nothing to do';
|
|
62
|
+
if (current !== command.oldSha) {
|
|
63
|
+
return `cannot lock ref '${command.ref}': is at ${current} but expected ${command.oldSha}`;
|
|
64
|
+
}
|
|
65
|
+
if (command.newSha === ZERO_SHA) return null;
|
|
66
|
+
const target = repo.object(command.newSha);
|
|
67
|
+
if (target === null) return 'missing necessary objects';
|
|
68
|
+
if (command.ref.startsWith('refs/heads/') && target.type !== 'commit')
|
|
69
|
+
return 'branch must point at a commit';
|
|
70
|
+
if (repo.missingFrom([command.newSha]).length > 0) return 'missing necessary objects';
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Unpacks the pack, if any, into the repository; returns the unpack status line. */
|
|
75
|
+
async function unpack(
|
|
76
|
+
repo: MemoryRepository,
|
|
77
|
+
body: Uint8Array,
|
|
78
|
+
offset: number,
|
|
79
|
+
needsPack: boolean,
|
|
80
|
+
): Promise<string> {
|
|
81
|
+
const pack = body.subarray(offset);
|
|
82
|
+
if (pack.length === 0) return needsPack ? 'unpack-objects abnormal exit' : 'ok';
|
|
83
|
+
try {
|
|
84
|
+
repo.put(await readPack(pack, { lookupBase: async (sha) => repo.object(sha) }));
|
|
85
|
+
return 'ok';
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return isCoreError(error) ? error.message.replaceAll('\n', ' ') : 'unpack-objects abnormal exit';
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Applies one receive-pack request (protocol v0/v1) to an in-memory repository, the way git's
|
|
93
|
+
* receive-pack does on a bare repository: unpack the pack, then update each ref only if it still
|
|
94
|
+
* names the command's old id (a stale old id is refused with "cannot lock ref"). Non-fast-forward
|
|
95
|
+
* updates with a matching old id are accepted, as git accepts them unless
|
|
96
|
+
* receive.denyNonFastForwards is set. "atomic" applies all commands or none. Ref checks and
|
|
97
|
+
* updates run without an await between them, so concurrent pushes to one ref cannot both win.
|
|
98
|
+
*/
|
|
99
|
+
export async function answerReceivePack(repo: MemoryRepository, body: Uint8Array): Promise<Uint8Array> {
|
|
100
|
+
let request: ReceivePackRequest;
|
|
101
|
+
try {
|
|
102
|
+
request = (await parseReceivePackRequest(streamOf(body))).request;
|
|
103
|
+
} catch (error) {
|
|
104
|
+
return encodeErrPacket(error instanceof Error ? error.message : 'malformed request');
|
|
105
|
+
}
|
|
106
|
+
const framing = reportStatusFraming(
|
|
107
|
+
request.capabilities.filter((capability) => RECEIVE_PACK_CAPABILITIES.includes(capability)),
|
|
108
|
+
);
|
|
109
|
+
const needsPack = request.commands.some((command) => command.newSha !== ZERO_SHA);
|
|
110
|
+
const unpackStatus = await unpack(repo, body, packOffset(body, request), needsPack);
|
|
111
|
+
let refs: RefStatus[];
|
|
112
|
+
if (unpackStatus !== 'ok') {
|
|
113
|
+
refs = request.commands.map((command) => ({ ref: command.ref, status: 'ng', reason: 'unpacker error' }));
|
|
114
|
+
} else {
|
|
115
|
+
refs = applyCommands(repo, request.commands, request.capabilities.includes('atomic'));
|
|
116
|
+
}
|
|
117
|
+
if (!request.capabilities.includes('report-status')) return new Uint8Array(0);
|
|
118
|
+
return encodeReportStatus({ unpack: unpackStatus, refs }, framing);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function applyCommands(
|
|
122
|
+
repo: MemoryRepository,
|
|
123
|
+
commands: readonly ReceivePackCommand[],
|
|
124
|
+
atomic: boolean,
|
|
125
|
+
): RefStatus[] {
|
|
126
|
+
const reasons = commands.map((command) => refuse(repo, command));
|
|
127
|
+
if (atomic && reasons.some((reason) => reason !== null)) {
|
|
128
|
+
return commands.map((command, index) => ({
|
|
129
|
+
ref: command.ref,
|
|
130
|
+
status: 'ng',
|
|
131
|
+
reason: reasons[index] ?? 'atomic transaction failed',
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
return commands.map((command, index) => {
|
|
135
|
+
const reason = reasons[index];
|
|
136
|
+
if (reason !== null && reason !== undefined) return { ref: command.ref, status: 'ng', reason };
|
|
137
|
+
if (command.newSha === ZERO_SHA) repo.refs.delete(command.ref);
|
|
138
|
+
else repo.refs.set(command.ref, command.newSha);
|
|
139
|
+
return { ref: command.ref, status: 'ok' };
|
|
140
|
+
});
|
|
141
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import type { GitAccess } from '@leed-ai/git-gateway-contract/storage/git-remote';
|
|
3
|
+
import { readAllBytes } from '@leed-ai/git-gateway-core/bytes';
|
|
4
|
+
|
|
5
|
+
import type { MemoryRepository } from './memory-repository.ts';
|
|
6
|
+
import { credentialOf, type MemoryTokens } from './memory-tokens.ts';
|
|
7
|
+
import { answerReceivePack, receivePackAdvertisement } from './receive-pack-service.ts';
|
|
8
|
+
import { answerUploadPack, uploadPackAdvertisement } from './upload-pack-service.ts';
|
|
9
|
+
|
|
10
|
+
const ROUTE = /^\/(?<repo>[^/]+)\.git\/(?<rest>info\/refs|git-upload-pack|git-receive-pack)$/;
|
|
11
|
+
const NO_CACHE = { 'Cache-Control': 'no-cache, max-age=0, must-revalidate' };
|
|
12
|
+
|
|
13
|
+
/** What the smart-HTTP server needs from the backend. */
|
|
14
|
+
export interface SmartHttpHost {
|
|
15
|
+
readonly tokens: MemoryTokens;
|
|
16
|
+
repository(name: string): MemoryRepository | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const text = (status: number, body: string, headers: Record<string, string> = {}): Response =>
|
|
20
|
+
new Response(body, { status, headers: { 'Content-Type': 'text/plain; charset=utf-8', ...headers } });
|
|
21
|
+
|
|
22
|
+
function gitResponse(body: Uint8Array, type: string): Response {
|
|
23
|
+
return new Response(body, { status: 200, headers: { 'Content-Type': type, ...NO_CACHE } });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async function requestBody(request: Request): Promise<Uint8Array> {
|
|
27
|
+
if (request.body === null) return new Uint8Array(0);
|
|
28
|
+
const encoding = request.headers.get('Content-Encoding');
|
|
29
|
+
const stream =
|
|
30
|
+
encoding === 'gzip' ? request.body.pipeThrough(new DecompressionStream('gzip')) : request.body;
|
|
31
|
+
return readAllBytes(stream);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Checks the request's credential opens `repo` for `access`; returns an error response when not. */
|
|
35
|
+
function authorize(host: SmartHttpHost, request: Request, repo: string, access: GitAccess): Response | null {
|
|
36
|
+
const credential = credentialOf(request);
|
|
37
|
+
const grant = credential === null ? null : host.tokens.check(credential);
|
|
38
|
+
if (grant?.repo !== repo) {
|
|
39
|
+
return text(401, 'authentication required\n', {
|
|
40
|
+
'WWW-Authenticate': 'Basic realm="git-gateway-testkit"',
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
if (access === 'write' && grant.access !== 'write') return text(403, 'read-only credential\n');
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Serves git smart HTTP (protocol v0/v1) for the in-memory repositories: GET
|
|
49
|
+
* /<repo>.git/info/refs?service=…, POST /<repo>.git/git-upload-pack and
|
|
50
|
+
* /git-receive-pack. Every request needs a token minted by gitRemote() for that repository —
|
|
51
|
+
* as a Bearer header or the password of HTTP Basic — and a read token cannot push.
|
|
52
|
+
*/
|
|
53
|
+
export async function serveSmartHttp(host: SmartHttpHost, request: Request): Promise<Response> {
|
|
54
|
+
const url = new URL(request.url);
|
|
55
|
+
const match = ROUTE.exec(url.pathname);
|
|
56
|
+
const name = match?.groups?.['repo'];
|
|
57
|
+
const rest = match?.groups?.['rest'];
|
|
58
|
+
if (name === undefined || rest === undefined) return text(404, 'not found\n');
|
|
59
|
+
const service = rest === 'info/refs' ? url.searchParams.get('service') : rest;
|
|
60
|
+
if (service !== 'git-upload-pack' && service !== 'git-receive-pack') {
|
|
61
|
+
return text(403, 'dumb HTTP is not supported\n');
|
|
62
|
+
}
|
|
63
|
+
const denied = authorize(host, request, name, service === 'git-receive-pack' ? 'write' : 'read');
|
|
64
|
+
if (denied !== null) return denied;
|
|
65
|
+
const repo = host.repository(name);
|
|
66
|
+
if (repo === undefined) return text(404, 'repository not found\n');
|
|
67
|
+
if (rest === 'info/refs') {
|
|
68
|
+
if (request.method !== 'GET') return text(405, 'method not allowed\n');
|
|
69
|
+
const body =
|
|
70
|
+
service === 'git-upload-pack' ? uploadPackAdvertisement(repo) : receivePackAdvertisement(repo);
|
|
71
|
+
return gitResponse(body, `application/x-${service}-advertisement`);
|
|
72
|
+
}
|
|
73
|
+
if (request.method !== 'POST') return text(405, 'method not allowed\n');
|
|
74
|
+
const body = await requestBody(request);
|
|
75
|
+
const answer =
|
|
76
|
+
service === 'git-upload-pack' ? await answerUploadPack(repo, body) : await answerReceivePack(repo, body);
|
|
77
|
+
return gitResponse(answer, `application/x-${service}-result`);
|
|
78
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { concatBytes } from '@leed-ai/git-gateway-core/bytes';
|
|
3
|
+
import { decodePktLines, packetText } from '@leed-ai/git-gateway-core/pkt-line/decode';
|
|
4
|
+
import { encodePktLine, FLUSH_PKT } from '@leed-ai/git-gateway-core/pkt-line/encode';
|
|
5
|
+
import { writePack } from '@leed-ai/git-gateway-core/pack/write-pack';
|
|
6
|
+
import { encodeErrPacket } from '@leed-ai/git-gateway-core/receive-pack/report-status';
|
|
7
|
+
import {
|
|
8
|
+
BAND_DATA,
|
|
9
|
+
encodeSideBand,
|
|
10
|
+
SIDE_BAND_64K_MAX_PAYLOAD,
|
|
11
|
+
SIDE_BAND_MAX_PAYLOAD,
|
|
12
|
+
} from '@leed-ai/git-gateway-core/receive-pack/side-band';
|
|
13
|
+
import { encodeRefAdvertisement } from '@leed-ai/git-gateway-core/smart-http/ref-advertisement';
|
|
14
|
+
|
|
15
|
+
import type { MemoryRepository } from './memory-repository.ts';
|
|
16
|
+
import { selectPack } from './pack-selection.ts';
|
|
17
|
+
|
|
18
|
+
/** The capabilities the in-memory upload-pack offers: whole-object packs, shallow (deepen) and filter. */
|
|
19
|
+
export const UPLOAD_PACK_CAPABILITIES: readonly string[] = [
|
|
20
|
+
'side-band',
|
|
21
|
+
'side-band-64k',
|
|
22
|
+
'no-progress',
|
|
23
|
+
'shallow',
|
|
24
|
+
'filter',
|
|
25
|
+
'allow-reachable-sha1-in-want',
|
|
26
|
+
'object-format=sha1',
|
|
27
|
+
'agent=git-gateway-testkit',
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/** The upload-pack ref advertisement: HEAD (when the default branch exists), then every ref in name order. */
|
|
31
|
+
export function uploadPackAdvertisement(repo: MemoryRepository): Uint8Array {
|
|
32
|
+
const names = [...repo.refs.keys()].toSorted();
|
|
33
|
+
const head = repo.refs.get(repo.headRef);
|
|
34
|
+
const refs = names.map((name) => ({ name, sha: repo.refs.get(name) ?? '', peeled: null }));
|
|
35
|
+
return encodeRefAdvertisement({
|
|
36
|
+
service: 'git-upload-pack',
|
|
37
|
+
refs: head === undefined ? refs : [{ name: 'HEAD', sha: head, peeled: null }, ...refs],
|
|
38
|
+
capabilities: [...UPLOAD_PACK_CAPABILITIES],
|
|
39
|
+
symrefs: head === undefined ? {} : { HEAD: repo.headRef },
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface Negotiation {
|
|
44
|
+
readonly wants: string[];
|
|
45
|
+
readonly haves: string[];
|
|
46
|
+
readonly capabilities: string[];
|
|
47
|
+
readonly clientShallow: string[];
|
|
48
|
+
readonly depth: number | undefined;
|
|
49
|
+
readonly filter: string | undefined;
|
|
50
|
+
/** How the request ended after the wants: "done", a flush closing a round of haves, or nothing. */
|
|
51
|
+
readonly end: 'done' | 'flush' | 'none';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Reads a stateless upload-pack request: the wants (with capabilities, shallow, deepen and filter
|
|
56
|
+
* lines) up to a flush, then have lines up to a flush or "done". A request that stops right after
|
|
57
|
+
* the wants' flush only asks for the shallow list.
|
|
58
|
+
*/
|
|
59
|
+
function readNegotiation(body: Uint8Array): Negotiation | string {
|
|
60
|
+
const wants: string[] = [];
|
|
61
|
+
const haves: string[] = [];
|
|
62
|
+
const capabilities: string[] = [];
|
|
63
|
+
const clientShallow: string[] = [];
|
|
64
|
+
let depth: number | undefined;
|
|
65
|
+
let filter: string | undefined;
|
|
66
|
+
let section: 'needs' | 'haves' = 'needs';
|
|
67
|
+
let end: Negotiation['end'] = 'none';
|
|
68
|
+
for (const packet of decodePktLines(body)) {
|
|
69
|
+
const line = packetText(packet);
|
|
70
|
+
if (line === null) {
|
|
71
|
+
if (section === 'needs') section = 'haves';
|
|
72
|
+
else end = 'flush';
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const [verb, argument = '', ...rest] = line.split(' ');
|
|
76
|
+
if (section === 'needs' && verb === 'want') {
|
|
77
|
+
if (wants.length === 0) capabilities.push(...rest);
|
|
78
|
+
wants.push(argument);
|
|
79
|
+
} else if (section === 'needs' && verb === 'shallow') clientShallow.push(argument);
|
|
80
|
+
else if (section === 'needs' && verb === 'deepen' && /^[1-9]\d*$/.test(argument))
|
|
81
|
+
depth = Number(argument);
|
|
82
|
+
else if (section === 'needs' && verb === 'filter') filter = argument;
|
|
83
|
+
else if (section === 'haves' && verb === 'have') haves.push(argument);
|
|
84
|
+
else if (section === 'haves' && line === 'done') end = 'done';
|
|
85
|
+
else return `unsupported upload-pack line: ${line.slice(0, 60)}`;
|
|
86
|
+
}
|
|
87
|
+
return { wants, haves, capabilities, clientShallow, depth, filter, end };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function frame(capabilities: readonly string[], pack: Uint8Array): Uint8Array {
|
|
91
|
+
if (capabilities.includes('side-band-64k')) {
|
|
92
|
+
return concatBytes([encodeSideBand(BAND_DATA, pack, SIDE_BAND_64K_MAX_PAYLOAD), FLUSH_PKT]);
|
|
93
|
+
}
|
|
94
|
+
if (capabilities.includes('side-band'))
|
|
95
|
+
return concatBytes([encodeSideBand(BAND_DATA, pack, SIDE_BAND_MAX_PAYLOAD), FLUSH_PKT]);
|
|
96
|
+
return pack;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Answers one stateless upload-pack request. A deepen first gets its shallow lines and a flush;
|
|
101
|
+
* a request that ends right after its wants gets nothing more. Otherwise, without "done" the
|
|
102
|
+
* answer acknowledges the first common have ("ACK <id>") or sends "NAK", and the client goes on; with "done" it sends that acknowledgement and a pack of every object
|
|
103
|
+
* reachable from the wants but not from the common haves — cut at the deepen, filtered — on
|
|
104
|
+
* side-band channel 1 when the client asked for side-band. Any object reachable from a ref may be
|
|
105
|
+
* wanted (allow-reachable-sha1-in-want).
|
|
106
|
+
*/
|
|
107
|
+
export async function answerUploadPack(repo: MemoryRepository, body: Uint8Array): Promise<Uint8Array> {
|
|
108
|
+
const negotiation = readNegotiation(body);
|
|
109
|
+
if (typeof negotiation === 'string') return encodeErrPacket(negotiation);
|
|
110
|
+
const reachableFromRefs = new Set(repo.reachable([...repo.refs.values()], []).map((object) => object.sha));
|
|
111
|
+
const unknown = negotiation.wants.find((want) => !reachableFromRefs.has(want));
|
|
112
|
+
if (unknown !== undefined) return encodeErrPacket(`upload-pack: not our ref ${unknown}`);
|
|
113
|
+
const common = negotiation.haves.filter((have) => repo.objects.has(have));
|
|
114
|
+
const selected = selectPack(repo, {
|
|
115
|
+
wants: negotiation.wants,
|
|
116
|
+
haves: common,
|
|
117
|
+
clientShallow: negotiation.clientShallow,
|
|
118
|
+
...(negotiation.depth === undefined ? {} : { depth: negotiation.depth }),
|
|
119
|
+
...(negotiation.filter === undefined ? {} : { filter: negotiation.filter }),
|
|
120
|
+
});
|
|
121
|
+
const parts: Uint8Array[] = [];
|
|
122
|
+
if (negotiation.depth !== undefined) {
|
|
123
|
+
for (const sha of selected.shallow) parts.push(encodePktLine(`shallow ${sha}\n`));
|
|
124
|
+
for (const sha of selected.unshallow) parts.push(encodePktLine(`unshallow ${sha}\n`));
|
|
125
|
+
parts.push(FLUSH_PKT);
|
|
126
|
+
}
|
|
127
|
+
if (negotiation.end === 'none') return concatBytes(parts);
|
|
128
|
+
parts.push(common[0] === undefined ? encodePktLine('NAK\n') : encodePktLine(`ACK ${common[0]}\n`));
|
|
129
|
+
if (negotiation.end === 'done')
|
|
130
|
+
parts.push(frame(negotiation.capabilities, await writePack(selected.objects)));
|
|
131
|
+
return concatBytes(parts);
|
|
132
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
|
|
3
|
+
|
|
4
|
+
/** A fetch-style request handler, such as `MemoryBackend.handle` or a Worker's `fetch`. */
|
|
5
|
+
export type FetchHandler = (request: Request) => Promise<Response>;
|
|
6
|
+
|
|
7
|
+
/** A running local HTTP server. */
|
|
8
|
+
export interface LocalServer {
|
|
9
|
+
/** Base URL, e.g. "http://127.0.0.1:43210". */
|
|
10
|
+
readonly url: string;
|
|
11
|
+
/** Stops accepting connections and resolves once the server has closed. */
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async function readBody(message: IncomingMessage): Promise<Uint8Array | null> {
|
|
16
|
+
if (message.method === 'GET' || message.method === 'HEAD') return null;
|
|
17
|
+
const chunks: Uint8Array[] = [];
|
|
18
|
+
for await (const chunk of message)
|
|
19
|
+
chunks.push(chunk instanceof Uint8Array ? chunk : new TextEncoder().encode(String(chunk)));
|
|
20
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function toRequest(message: IncomingMessage, base: string): Promise<Request> {
|
|
24
|
+
const headers = new Headers();
|
|
25
|
+
for (const [name, value] of Object.entries(message.headers)) {
|
|
26
|
+
if (Array.isArray(value)) for (const item of value) headers.append(name, item);
|
|
27
|
+
else if (value !== undefined) headers.set(name, value);
|
|
28
|
+
}
|
|
29
|
+
const body = await readBody(message);
|
|
30
|
+
return new Request(new URL(message.url ?? '/', base), {
|
|
31
|
+
method: message.method ?? 'GET',
|
|
32
|
+
headers,
|
|
33
|
+
...(body === null ? {} : { body }),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function send(response: Response, out: ServerResponse): Promise<void> {
|
|
38
|
+
const headers: Record<string, string> = {};
|
|
39
|
+
response.headers.forEach((value, name) => {
|
|
40
|
+
headers[name] = value;
|
|
41
|
+
});
|
|
42
|
+
out.writeHead(response.status, headers);
|
|
43
|
+
out.end(Buffer.from(await response.arrayBuffer()));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Serves a fetch-style handler over plain HTTP on 127.0.0.1 (a random free port unless one is
|
|
48
|
+
* given), so a real git client can talk to an in-process server such as the in-memory backend.
|
|
49
|
+
* Request bodies are buffered before the handler runs; a thrown handler answers 500.
|
|
50
|
+
*/
|
|
51
|
+
export async function serveFetchHandler(handler: FetchHandler, port = 0): Promise<LocalServer> {
|
|
52
|
+
let base = 'http://127.0.0.1';
|
|
53
|
+
const server = createServer((message, out) => {
|
|
54
|
+
toRequest(message, base)
|
|
55
|
+
.then(handler)
|
|
56
|
+
.then(async (response) => send(response, out))
|
|
57
|
+
.catch((error: unknown) => {
|
|
58
|
+
out.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
59
|
+
out.end(error instanceof Error ? error.message : 'internal error');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
await new Promise<void>((resolve) => server.listen(port, '127.0.0.1', resolve));
|
|
63
|
+
const address = server.address();
|
|
64
|
+
if (address === null || typeof address === 'string') throw new Error('the server has no TCP address');
|
|
65
|
+
base = `http://127.0.0.1:${address.port}`;
|
|
66
|
+
return {
|
|
67
|
+
url: base,
|
|
68
|
+
close: async () =>
|
|
69
|
+
new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
|
|
4
|
+
|
|
5
|
+
import type { FetchHandler, LocalServer } from './serve-fetch-handler.ts';
|
|
6
|
+
|
|
7
|
+
/** Byte counts a {@link serveStreaming} server reports as traffic flows. */
|
|
8
|
+
export interface StreamingTraffic {
|
|
9
|
+
/** Called with each chunk of a request body as it is handed to the handler. */
|
|
10
|
+
readonly onRequestChunk?: (bytes: number) => void;
|
|
11
|
+
/** Called with each chunk of a response body once the socket has accepted it. */
|
|
12
|
+
readonly onResponseChunk?: (bytes: number) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The request body as a stream that pulls from the socket only as the handler reads: the socket
|
|
17
|
+
* is paused whenever a chunk is waiting, so nothing piles up in memory.
|
|
18
|
+
*/
|
|
19
|
+
function requestBody(message: IncomingMessage, traffic: StreamingTraffic): ReadableStream<Uint8Array> {
|
|
20
|
+
return new ReadableStream<Uint8Array>(
|
|
21
|
+
{
|
|
22
|
+
start(controller) {
|
|
23
|
+
message.pause();
|
|
24
|
+
message.on('data', (chunk: Buffer) => {
|
|
25
|
+
message.pause();
|
|
26
|
+
traffic.onRequestChunk?.(chunk.length);
|
|
27
|
+
controller.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.length));
|
|
28
|
+
});
|
|
29
|
+
message.on('end', () => controller.close());
|
|
30
|
+
message.on('error', (error) => controller.error(error));
|
|
31
|
+
},
|
|
32
|
+
pull() {
|
|
33
|
+
message.resume();
|
|
34
|
+
},
|
|
35
|
+
cancel() {
|
|
36
|
+
message.destroy();
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
{ highWaterMark: 0 },
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function toRequest(message: IncomingMessage, base: string, traffic: StreamingTraffic): Request {
|
|
44
|
+
const headers = new Headers();
|
|
45
|
+
for (const [name, value] of Object.entries(message.headers)) {
|
|
46
|
+
if (Array.isArray(value)) for (const item of value) headers.append(name, item);
|
|
47
|
+
else if (value !== undefined) headers.set(name, value);
|
|
48
|
+
}
|
|
49
|
+
const method = message.method ?? 'GET';
|
|
50
|
+
const init: RequestInit & { duplex?: 'half' } = { method, headers };
|
|
51
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
52
|
+
init.body = requestBody(message, traffic);
|
|
53
|
+
init.duplex = 'half';
|
|
54
|
+
}
|
|
55
|
+
return new Request(new URL(message.url ?? '/', base), init);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Writes a response chunk by chunk, waiting for the socket to drain, so it is never buffered whole. */
|
|
59
|
+
async function send(response: Response, out: ServerResponse, traffic: StreamingTraffic): Promise<void> {
|
|
60
|
+
const headers: Record<string, string> = {};
|
|
61
|
+
response.headers.forEach((value, name) => {
|
|
62
|
+
headers[name] = value;
|
|
63
|
+
});
|
|
64
|
+
out.writeHead(response.status, headers);
|
|
65
|
+
if (response.body !== null) {
|
|
66
|
+
const reader = response.body.getReader();
|
|
67
|
+
for (let next = await reader.read(); !next.done; next = await reader.read()) {
|
|
68
|
+
if (!out.write(next.value)) await once(out, 'drain');
|
|
69
|
+
traffic.onResponseChunk?.(next.value.length);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
out.end();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Serves a fetch-style handler (a Worker's `fetch`, a Hono app) over plain HTTP on 127.0.0.1 with
|
|
77
|
+
* request and response bodies streamed in both directions under backpressure — as the Workers
|
|
78
|
+
* runtime streams them — so a real git client can drive the gateway and tests can observe how much
|
|
79
|
+
* it holds at once. A thrown handler answers 500.
|
|
80
|
+
*/
|
|
81
|
+
export async function serveStreaming(
|
|
82
|
+
handler: FetchHandler,
|
|
83
|
+
traffic: StreamingTraffic = {},
|
|
84
|
+
port = 0,
|
|
85
|
+
): Promise<LocalServer> {
|
|
86
|
+
let base = 'http://127.0.0.1';
|
|
87
|
+
const server = createServer((message, out) => {
|
|
88
|
+
handler(toRequest(message, base, traffic))
|
|
89
|
+
.then(async (response) => send(response, out, traffic))
|
|
90
|
+
.catch((error: unknown) => {
|
|
91
|
+
if (!out.headersSent) out.writeHead(500, { 'Content-Type': 'text/plain' });
|
|
92
|
+
out.end(error instanceof Error ? error.message : 'internal error');
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
await new Promise<void>((resolve) => server.listen(port, '127.0.0.1', resolve));
|
|
96
|
+
const address = server.address();
|
|
97
|
+
if (address === null || typeof address === 'string') throw new Error('the server has no TCP address');
|
|
98
|
+
base = `http://127.0.0.1:${address.port}`;
|
|
99
|
+
return {
|
|
100
|
+
url: base,
|
|
101
|
+
close: async () =>
|
|
102
|
+
new Promise<void>((resolve, reject) => {
|
|
103
|
+
server.closeAllConnections();
|
|
104
|
+
server.close((error) => (error ? reject(error) : resolve()));
|
|
105
|
+
}),
|
|
106
|
+
};
|
|
107
|
+
}
|