@feng0531/dsh-vcs 0.7.3 → 0.8.2
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/README.md +68 -5
- package/amend-host.mjs +48 -0
- package/branch-create-host.mjs +42 -0
- package/branch-create-preview.mjs +196 -0
- package/branch-delete-preview.mjs +214 -0
- package/branch-switch-host.mjs +13 -33
- package/commit-host.mjs +49 -0
- package/commit-object-publish.mjs +123 -0
- package/commit-preview.mjs +335 -0
- package/confirmed-operation.mjs +50 -0
- package/dist/client.js +25 -2
- package/dist/frontend.json +1 -1
- package/dist/interface.js +25 -2
- package/docs/RELEASING.md +2 -0
- package/git-blob-store.mjs +151 -0
- package/git-branch-create.mjs +156 -0
- package/git-branch-delete.mjs +175 -0
- package/git-lightweight-tag-create.mjs +181 -0
- package/git-object-store.mjs +162 -0
- package/git-ref-cas.mjs +119 -0
- package/git-tag-create.mjs +186 -0
- package/index-change-host.mjs +19 -0
- package/index-change.mjs +418 -0
- package/index-lock.mjs +231 -0
- package/index-stage-host.mjs +19 -0
- package/index.mjs +301 -4
- package/latest-commit.mjs +109 -0
- package/lightweight-tag-host.mjs +42 -0
- package/lightweight-tag-preview.mjs +183 -0
- package/line-index-host.mjs +58 -0
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +47 -2
- package/partial-stage-host.mjs +52 -0
- package/partial-unstage-host.mjs +52 -0
- package/src/amend-plan.mjs +74 -0
- package/src/branch-delete-plan.mjs +113 -0
- package/src/commit-plan.mjs +78 -0
- package/src/git-commit-object.mjs +99 -0
- package/src/git-index.mjs +113 -0
- package/src/git-tag-object.mjs +110 -0
- package/src/index-tree.mjs +77 -0
- package/src/lightweight-tag-plan.mjs +64 -0
- package/src/line-index.mjs +168 -0
- package/src/partial-index.mjs +199 -0
- package/src/reflog.mjs +33 -0
- package/src/settled-reads.mjs +5 -0
- package/src/verified-branch-delete-plan.mjs +95 -0
- package/src/verified-commit-metadata.mjs +57 -0
- package/src/verified-commit-plan.mjs +49 -0
- package/src/verified-commit.mjs +84 -0
- package/src/verified-tag-plan.mjs +72 -0
- package/tag-create-host.mjs +31 -0
- package/tag-create-preview.mjs +215 -0
- package/tag-object-publish.mjs +49 -0
- package/vcs.mjs +29 -0
package/index-lock.mjs
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Live Index locking only; intentionally not connected to an RPC or Git subprocess.
|
|
2
|
+
import {constants} from 'node:fs';
|
|
3
|
+
import {lstat, realpath, open, rename, unlink} from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {createHash} from 'node:crypto';
|
|
6
|
+
import {currentOperationBudget} from './src/operation-budget.mjs';
|
|
7
|
+
|
|
8
|
+
export const MAX_INDEX_BYTES = 8 * 1024 * 1024;
|
|
9
|
+
const failure = (code, message, cause) => Object.assign(new Error(message, {cause}), {code});
|
|
10
|
+
const digest = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
11
|
+
const same = (a, b) => a.dev === b.dev && a.ino === b.ino;
|
|
12
|
+
const plain = stat => stat.isFile() && !stat.isSymbolicLink() && stat.nlink === 1n;
|
|
13
|
+
const normalized = value => process.platform === 'win32' ? value.toLowerCase() : value;
|
|
14
|
+
const unsafe = message => failure('vcs/index-unsafe', message);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Lock an existing, upstream-validated embedded metadata directory and live Index.
|
|
18
|
+
* expectedIndexDigest is a strict 64-hex SHA256 of the complete raw Index (<=8MiB).
|
|
19
|
+
* Returns work's result. work receives async readIndex(), writeCandidate(Buffer),
|
|
20
|
+
* and publish({assertAuthorized, beforePublish, onStart}); await every operation.
|
|
21
|
+
* readIndex returns an independent copy of the original bytes. Candidate writes
|
|
22
|
+
* never modify index. publish requires all three adapter-owned hooks, checks CAS,
|
|
23
|
+
* then marks publishStarted, calls onStart, and attempts exactly one same-directory
|
|
24
|
+
* rename. beforePublish is an awaited pre-start hook, not an authorization bypass.
|
|
25
|
+
* Authorization and cancellation are rechecked after rename and after work.
|
|
26
|
+
* Any error after publishStarted is vcs/index-outcome-unknown, even if rename
|
|
27
|
+
* failed: refresh state, do not retry blindly. There is no automatic publication.
|
|
28
|
+
*
|
|
29
|
+
* No symlinks, multi-link files, missing Index, noncanonical directory aliases,
|
|
30
|
+
* or preexisting locks are accepted. Identity checks are conservative checks, NOT
|
|
31
|
+
* global atomicity or isolation against arbitrary malicious concurrent FS edits.
|
|
32
|
+
* A replacement lock is never intentionally deleted. No old Index unlink/retry.
|
|
33
|
+
*/
|
|
34
|
+
export async function withOwnedIndexLock({gitDirectory, expectedIndexDigest, signal} = {}, work) {
|
|
35
|
+
if (typeof expectedIndexDigest !== 'string' || !/^[a-fA-F0-9]{64}$/.test(expectedIndexDigest)) {
|
|
36
|
+
throw failure('vcs/index-invalid', 'expectedIndexDigest must be exactly 64 hexadecimal characters.');
|
|
37
|
+
}
|
|
38
|
+
if (typeof gitDirectory !== 'string' || !path.isAbsolute(gitDirectory) || typeof work !== 'function') {
|
|
39
|
+
throw failure('vcs/index-invalid', 'An absolute canonical gitDirectory and work callback are required.');
|
|
40
|
+
}
|
|
41
|
+
const budget = currentOperationBudget();
|
|
42
|
+
const checkAbort = () => { signal?.throwIfAborted(); budget?.throwIfExpired(); };
|
|
43
|
+
checkAbort();
|
|
44
|
+
const directory = path.resolve(gitDirectory);
|
|
45
|
+
const directoryStat = await lstat(directory, {bigint: true});
|
|
46
|
+
if (!directoryStat.isDirectory() || directoryStat.isSymbolicLink() ||
|
|
47
|
+
normalized(await realpath(directory)) !== normalized(directory)) throw unsafe('Metadata directory must be canonical and not a link.');
|
|
48
|
+
const indexPath = path.join(directory, 'index');
|
|
49
|
+
const lockPath = path.join(directory, 'index.lock');
|
|
50
|
+
const verifyDirectory = async () => {
|
|
51
|
+
const current = await lstat(directory, {bigint: true});
|
|
52
|
+
if (!current.isDirectory() || current.isSymbolicLink() || !same(current, directoryStat) ||
|
|
53
|
+
normalized(await realpath(directory)) !== normalized(directory)) throw unsafe('Metadata directory identity changed.');
|
|
54
|
+
};
|
|
55
|
+
const readLive = async () => {
|
|
56
|
+
await verifyDirectory();
|
|
57
|
+
const entry = await lstat(indexPath, {bigint: true});
|
|
58
|
+
if (!plain(entry)) throw unsafe('Index must be a plain single-link file.');
|
|
59
|
+
if (entry.size > BigInt(MAX_INDEX_BYTES)) throw failure('vcs/index-too-large', 'Index exceeds 8MiB.');
|
|
60
|
+
const handle = await open(indexPath, constants.O_RDONLY | (constants.O_NOFOLLOW || 0));
|
|
61
|
+
try {
|
|
62
|
+
const opened = await handle.stat({bigint: true});
|
|
63
|
+
if (!plain(opened) || !same(entry, opened)) throw unsafe('Index identity changed while opening.');
|
|
64
|
+
const buffer = Buffer.alloc(MAX_INDEX_BYTES + 1);
|
|
65
|
+
let length = 0;
|
|
66
|
+
while (length < buffer.length) {
|
|
67
|
+
checkAbort();
|
|
68
|
+
const {bytesRead} = await handle.read(buffer, length, buffer.length - length, length);
|
|
69
|
+
if (!bytesRead) break;
|
|
70
|
+
length += bytesRead;
|
|
71
|
+
}
|
|
72
|
+
if (length > MAX_INDEX_BYTES) throw failure('vcs/index-too-large', 'Index exceeds 8MiB.');
|
|
73
|
+
const after = await handle.stat({bigint: true});
|
|
74
|
+
const current = await lstat(indexPath, {bigint: true});
|
|
75
|
+
if (!plain(after) || !plain(current) || !same(opened, current) ||
|
|
76
|
+
after.size !== BigInt(length) || opened.size !== after.size ||
|
|
77
|
+
opened.mtimeNs !== after.mtimeNs || opened.ctimeNs !== after.ctimeNs) throw unsafe('Index changed while reading.');
|
|
78
|
+
await verifyDirectory();
|
|
79
|
+
return {bytes: Buffer.from(buffer.subarray(0, length)), stat: after};
|
|
80
|
+
} finally { await handle.close(); }
|
|
81
|
+
};
|
|
82
|
+
const original = await readLive();
|
|
83
|
+
const expected = expectedIndexDigest.toLowerCase();
|
|
84
|
+
if (digest(original.bytes) !== expected) throw failure('vcs/index-stale', 'Live Index does not match the expected digest.');
|
|
85
|
+
checkAbort();
|
|
86
|
+
await verifyDirectory();
|
|
87
|
+
let handle;
|
|
88
|
+
try {
|
|
89
|
+
handle = await open(lockPath, constants.O_CREAT | constants.O_EXCL | constants.O_RDWR | (constants.O_NOFOLLOW || 0), 0o600);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error.code === 'EEXIST') throw failure('vcs/index-locked', 'An Index lock already exists; it was not touched.', error);
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
let owned, candidateReady = false, publishStarted = false, published = false;
|
|
95
|
+
let active = true, pending = null, authorization, publishError, candidateDigest, candidateLength;
|
|
96
|
+
const verifyOwned = async () => {
|
|
97
|
+
await verifyDirectory();
|
|
98
|
+
const current = await lstat(lockPath, {bigint: true});
|
|
99
|
+
const opened = await handle.stat({bigint: true});
|
|
100
|
+
if (!owned || !plain(current) || !plain(opened) || !same(current, owned) || !same(opened, owned)) {
|
|
101
|
+
throw unsafe('Index lock ownership changed.');
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
const checkLive = async () => {
|
|
105
|
+
const live = await readLive();
|
|
106
|
+
if (!same(live.stat, original.stat) || digest(live.bytes) !== expected) {
|
|
107
|
+
throw failure('vcs/index-stale', 'Live Index changed before publication.');
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const verifyCandidate = async () => {
|
|
111
|
+
await verifyOwned();
|
|
112
|
+
const before = await handle.stat({bigint: true});
|
|
113
|
+
if (before.size !== BigInt(candidateLength)) throw unsafe('Candidate content changed.');
|
|
114
|
+
const bytes = Buffer.alloc(candidateLength); let offset = 0;
|
|
115
|
+
while (offset < bytes.length) {
|
|
116
|
+
checkAbort();
|
|
117
|
+
const {bytesRead} = await handle.read(bytes, offset, bytes.length - offset, offset);
|
|
118
|
+
if (!bytesRead) throw unsafe('Candidate content truncated.');
|
|
119
|
+
offset += bytesRead;
|
|
120
|
+
}
|
|
121
|
+
const after = await handle.stat({bigint: true});
|
|
122
|
+
if (before.size !== after.size || before.mtimeNs !== after.mtimeNs || before.ctimeNs !== after.ctimeNs || digest(bytes) !== candidateDigest) throw unsafe('Candidate content changed.');
|
|
123
|
+
await verifyOwned();
|
|
124
|
+
};
|
|
125
|
+
const verifyPublished = async () => {
|
|
126
|
+
const live = await readLive();
|
|
127
|
+
if (!same(live.stat, owned) || live.bytes.length !== candidateLength || digest(live.bytes) !== candidateDigest) throw unsafe('Published Index no longer matches the approved candidate.');
|
|
128
|
+
};
|
|
129
|
+
const operation = task => {
|
|
130
|
+
if (!active || publishStarted || pending) return Promise.reject(failure('vcs/index-state', 'Index lock operation is closed, published, or busy.'));
|
|
131
|
+
const result = (async () => { checkAbort(); await verifyOwned(); return task(); })();
|
|
132
|
+
pending = result;
|
|
133
|
+
// Observe rejection even if a callback forgets to await an operation.
|
|
134
|
+
result.then(() => { if (pending === result) pending = null; }, caught => {
|
|
135
|
+
if (publishStarted) publishError ||= caught || failure('vcs/index-operation', 'Publication failed.');
|
|
136
|
+
if (pending === result) pending = null;
|
|
137
|
+
});
|
|
138
|
+
return result;
|
|
139
|
+
};
|
|
140
|
+
let result, error;
|
|
141
|
+
try {
|
|
142
|
+
owned = await handle.stat({bigint: true});
|
|
143
|
+
await verifyOwned();
|
|
144
|
+
result = await work(Object.freeze({
|
|
145
|
+
readIndex: () => operation(async () => Buffer.from(original.bytes)),
|
|
146
|
+
writeCandidate: bytes => {
|
|
147
|
+
if (!Buffer.isBuffer(bytes)) return Promise.reject(failure('vcs/index-invalid', 'Candidate must be a Buffer.'));
|
|
148
|
+
if (bytes.length > MAX_INDEX_BYTES) return Promise.reject(failure('vcs/index-too-large', 'Candidate exceeds 8MiB.'));
|
|
149
|
+
const captured = Buffer.from(bytes);
|
|
150
|
+
return operation(async () => {
|
|
151
|
+
if (!Buffer.isBuffer(bytes)) throw failure('vcs/index-invalid', 'Candidate must be a Buffer.');
|
|
152
|
+
if (bytes.length > MAX_INDEX_BYTES) throw failure('vcs/index-too-large', 'Candidate exceeds 8MiB.');
|
|
153
|
+
const candidate = captured;
|
|
154
|
+
candidateReady = false;
|
|
155
|
+
let offset = 0;
|
|
156
|
+
while (offset < candidate.length) {
|
|
157
|
+
checkAbort();
|
|
158
|
+
await verifyOwned();
|
|
159
|
+
const {bytesWritten} = await handle.write(candidate, offset, candidate.length - offset, offset);
|
|
160
|
+
if (!Number.isInteger(bytesWritten) || bytesWritten <= 0) throw failure('vcs/index-write', 'Candidate write made no progress.');
|
|
161
|
+
offset += bytesWritten;
|
|
162
|
+
}
|
|
163
|
+
checkAbort();
|
|
164
|
+
await verifyOwned();
|
|
165
|
+
await handle.truncate(candidate.length);
|
|
166
|
+
await handle.sync();
|
|
167
|
+
await verifyOwned();
|
|
168
|
+
checkAbort();
|
|
169
|
+
candidateDigest = digest(candidate); candidateLength = candidate.length;
|
|
170
|
+
await verifyCandidate();
|
|
171
|
+
candidateReady = true;
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
publish: hooks => operation(async () => {
|
|
175
|
+
if (!candidateReady) throw failure('vcs/index-state', 'Write a complete candidate before publishing.');
|
|
176
|
+
if (!hooks || ['assertAuthorized', 'beforePublish', 'onStart'].some(key => typeof hooks[key] !== 'function')) {
|
|
177
|
+
throw failure('vcs/index-invalid', 'Publish requires assertAuthorized, beforePublish and onStart callbacks.');
|
|
178
|
+
}
|
|
179
|
+
authorization = hooks.assertAuthorized;
|
|
180
|
+
await authorization();
|
|
181
|
+
checkAbort();
|
|
182
|
+
await hooks.beforePublish();
|
|
183
|
+
await authorization();
|
|
184
|
+
checkAbort();
|
|
185
|
+
await checkLive();
|
|
186
|
+
await verifyCandidate();
|
|
187
|
+
checkAbort();
|
|
188
|
+
publishStarted = true;
|
|
189
|
+
// Synchronous notification: the future adapter must not start asynchronous work here.
|
|
190
|
+
const notification = hooks.onStart();
|
|
191
|
+
if (notification && typeof notification.then === 'function') {
|
|
192
|
+
await notification;
|
|
193
|
+
throw failure('vcs/index-invalid', 'onStart must be synchronous.');
|
|
194
|
+
}
|
|
195
|
+
checkAbort();
|
|
196
|
+
await verifyCandidate();
|
|
197
|
+
await checkLive();
|
|
198
|
+
checkAbort();
|
|
199
|
+
await rename(lockPath, indexPath);
|
|
200
|
+
published = true;
|
|
201
|
+
await authorization();
|
|
202
|
+
await verifyPublished();
|
|
203
|
+
checkAbort();
|
|
204
|
+
}),
|
|
205
|
+
}));
|
|
206
|
+
active = false;
|
|
207
|
+
if (pending) await pending;
|
|
208
|
+
if (published) { await authorization(); await verifyPublished(); }
|
|
209
|
+
checkAbort();
|
|
210
|
+
} catch (caught) { error = caught || failure('vcs/index-operation', 'Index callback failed.'); }
|
|
211
|
+
finally {
|
|
212
|
+
active = false;
|
|
213
|
+
if (pending) { try { await pending; } catch (caught) { error ||= caught; } }
|
|
214
|
+
if (!published && owned) {
|
|
215
|
+
try {
|
|
216
|
+
await verifyOwned();
|
|
217
|
+
await unlink(lockPath);
|
|
218
|
+
} catch (caught) {
|
|
219
|
+
// Ownership loss is itself an error, but never permission to unlink a foreign lock.
|
|
220
|
+
error ||= caught;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
try { await handle.close(); } catch (caught) { error ||= caught; }
|
|
224
|
+
}
|
|
225
|
+
error ||= publishError;
|
|
226
|
+
if (error) {
|
|
227
|
+
if (publishStarted) throw failure('vcs/index-outcome-unknown', 'Index publication started; refresh repository state before any further action.', error);
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
return result;
|
|
231
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {createConfirmedOperationCoordinator} from './confirmed-operation.mjs';
|
|
2
|
+
|
|
3
|
+
// Internal host adapter, not an authorization layer: callers must supply the
|
|
4
|
+
// authenticated binding and assertAuthorized callback required by the coordinator.
|
|
5
|
+
// address.path is host-resolved. A future ID-only RPC facade must resolve an
|
|
6
|
+
// authorized repository entry before calling this wrapper, never accept raw paths.
|
|
7
|
+
export function createIndexStageCoordinator({api,now=Date.now,ttlMs=60000,maxTokens=32}){
|
|
8
|
+
return createConfirmedOperationCoordinator({
|
|
9
|
+
operationKind:'index-stage',now,ttlMs,maxTokens,
|
|
10
|
+
prepare:(repository,{address,signal})=>api.prepareIndexStage(repository,{path:address.path,signal}),
|
|
11
|
+
execute:(repository,request)=>api.applyIndexStage(repository,request),
|
|
12
|
+
// Adapter-owned safe summaries only: retain the complete private snapshot on
|
|
13
|
+
// the server, including captured bytes and fingerprints, for exact execution.
|
|
14
|
+
project:snapshot=>({action:'stage',path:snapshot.path,head:snapshot.head,index:snapshot.index,target:snapshot.target}),
|
|
15
|
+
invalidCode:'vcs/index-confirmation-invalid',
|
|
16
|
+
unknownCode:'vcs/index-outcome-unknown',
|
|
17
|
+
unknownMessage:'Staging started but its final result could not be confirmed. Append-only object writes may remain even if the index was not updated. Refresh the repository and inspect the index before preparing another stage.',
|
|
18
|
+
});
|
|
19
|
+
}
|