@needmoretruth/nmts-cli 0.38.0 → 0.38.1
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/AGENTS.md +22 -0
- package/CHANGELOG.md +8 -0
- package/README.ko.md +1 -1
- package/README.md +1 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/drive-edit/errors.d.ts +44 -0
- package/dist/drive-edit/errors.js +65 -0
- package/dist/drive-edit/folders.d.ts +29 -0
- package/dist/drive-edit/folders.js +98 -0
- package/dist/drive-edit/move.d.ts +66 -0
- package/dist/drive-edit/move.js +119 -0
- package/dist/drive-edit/trash.d.ts +53 -0
- package/dist/drive-edit/trash.js +190 -0
- package/dist/drive-edit/tree.d.ts +15 -0
- package/dist/drive-edit/tree.js +77 -0
- package/dist/drive-edit.d.ts +9 -189
- package/dist/drive-edit.js +9 -527
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/s3/contract.d.ts +80 -0
- package/dist/s3/contract.js +3 -0
- package/dist/s3/routes.d.ts +4 -0
- package/dist/s3/routes.js +249 -0
- package/dist/s3/server.d.ts +3 -80
- package/dist/s3/server.js +4 -247
- package/package.json +1 -1
package/dist/drive-edit.js
CHANGED
|
@@ -12,530 +12,12 @@
|
|
|
12
12
|
// ⛔ NOTHING HERE WRITES TO A STREAM OR PICKS AN EXIT CODE. Every refusal is thrown and every
|
|
13
13
|
// outcome is returned; the words a person reads are the caller's.
|
|
14
14
|
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
import { buildIndex, entryAt, folderIdFor, fullPathOf, isLive, KIND_FILE, KIND_FOLDER, namesIn, normaliseName, normalisePath, } from "./drive-paths.js";
|
|
25
|
-
import { NmtsError } from "./errors.js";
|
|
26
|
-
import { setTrashed } from "./item-trash.js";
|
|
27
|
-
import { readFileList } from "./manifest.js";
|
|
28
|
-
import { applyManyToList, applyToList, batchTargets } from "./manifest-write.js";
|
|
29
|
-
import { applyIntent } from "./shared/lib/drive/manifest-ops.js";
|
|
30
|
-
/** A refusal about the list, with the sentence a person reads and the word a program reads. */
|
|
31
|
-
export class DriveEditError extends NmtsError {
|
|
32
|
-
code;
|
|
33
|
-
constructor(code, message, options = {}) {
|
|
34
|
-
super(message, options);
|
|
35
|
-
this.name = "DriveEditError";
|
|
36
|
-
this.code = code;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Run a path lookup, and label whatever it refused as `NOT_FOUND`.
|
|
41
|
-
*
|
|
42
|
-
* ⛔ THE SENTENCE, THE EXIT CODE AND THE NEXT STEP ARE CARRIED THROUGH UNTOUCHED. `drive-paths.ts`
|
|
43
|
-
* words three different failures — nothing there, it is in the trash, it names two things — and
|
|
44
|
-
* each of them is better than anything this file could say about them. What is added is the one
|
|
45
|
-
* thing it cannot carry: a code, so a program does not have to read English to know a path did
|
|
46
|
-
* not resolve.
|
|
47
|
-
*/
|
|
48
|
-
function resolving(body) {
|
|
49
|
-
try {
|
|
50
|
-
return body();
|
|
51
|
-
}
|
|
52
|
-
catch (error) {
|
|
53
|
-
if (error instanceof NmtsError && !(error instanceof DriveEditError)) {
|
|
54
|
-
throw new DriveEditError("NOT_FOUND", error.message, {
|
|
55
|
-
exitCode: error.exitCode,
|
|
56
|
-
nextStep: error.nextStep,
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
throw error;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* A new name is a name and not a path.
|
|
64
|
-
*
|
|
65
|
-
* ⛔ REFUSED RATHER THAN SPLIT. A name with a `/` in it is somebody asking for a move while typing
|
|
66
|
-
* a rename, and quietly doing the move would put the file somewhere they did not look.
|
|
67
|
-
*/
|
|
68
|
-
export function requireNewName(name) {
|
|
69
|
-
if (name.trim() === "") {
|
|
70
|
-
throw new DriveEditError("BAD_NAME", "A name cannot be empty.", {
|
|
71
|
-
exitCode: 2,
|
|
72
|
-
nextStep: "Nothing was renamed.",
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
if (name.includes("/")) {
|
|
76
|
-
throw new DriveEditError("BAD_NAME", `A name cannot contain "/" — that is what makes it a path.`, {
|
|
77
|
-
exitCode: 2,
|
|
78
|
-
nextStep: `To move it, use \`nmts mv\`. Nothing was renamed.`,
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
return name;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Make a folder path, and every folder above it that is missing, for an account already opened.
|
|
85
|
-
*
|
|
86
|
-
* ⛔ THE RULES BELOW ARE THE ONES A SECOND COPY WOULD GET SUBTLY WRONG: a folder that is already
|
|
87
|
-
* there IS the folder asked for (never a numbered one), the decision is taken inside each
|
|
88
|
-
* attempt so a lost race cannot make two, and what was made before a failure is named rather
|
|
89
|
-
* than silently kept.
|
|
90
|
-
*
|
|
91
|
-
* ⚠ MISSING PARENTS ARE CREATED, and that is a decision rather than a convenience. A folder costs
|
|
92
|
-
* nothing, holds nothing and can be moved to the trash, so the failure mode of creating one too
|
|
93
|
-
* many is a tidy-up; the failure mode of refusing is a caller that has to discover the tree one
|
|
94
|
-
* call at a time. Every folder made is named in the result, so it is never a surprise.
|
|
95
|
-
*/
|
|
96
|
-
export async function ensureFolderPath(input, wanted) {
|
|
97
|
-
const made = [];
|
|
98
|
-
let parentId = null;
|
|
99
|
-
let walked = "";
|
|
100
|
-
for (const name of wanted.split("/")) {
|
|
101
|
-
// ⚠ A name that is only spaces is refused too. `mkdir` used to accept it, and then `rm` and
|
|
102
|
-
// `restore` rejected the very path `ls` printed for it as "no path given" — a code-2 message
|
|
103
|
-
// blaming the caller for an argument they had supplied (2026-08-23).
|
|
104
|
-
if (name.trim() === "" || name === "." || name === "..") {
|
|
105
|
-
throw new DriveEditError("BAD_NAME", `"${wanted}" is not a folder path this tool will make.`, {
|
|
106
|
-
exitCode: 2,
|
|
107
|
-
nextStep: `Empty names, "." and ".." are not folder names in a drive. Nothing was made.`,
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
walked = walked === "" ? name : `${walked}/${name}`;
|
|
111
|
-
const under = parentId;
|
|
112
|
-
const here = walked;
|
|
113
|
-
const fresh = randomUUID();
|
|
114
|
-
let landedOn = fresh;
|
|
115
|
-
// ⛔ ONE WRITE PER FOLDER, and the check that decides whether to write happens INSIDE the
|
|
116
|
-
// attempt. Two things went wrong when it sat outside (2026-08-23):
|
|
117
|
-
// · running `mkdir` twice at the same moment made `shared` AND `shared (2)`, because the
|
|
118
|
-
// loser of the compare-and-swap re-applied a decision taken against the older list;
|
|
119
|
-
// · a trashed folder of the same name made the second `mkdir` produce `photos (2)` while
|
|
120
|
-
// printing `Made "photos"`, because it went through the upload helper — and picking a
|
|
121
|
-
// free name is the right rule for BYTES and the wrong rule for a folder. A folder with
|
|
122
|
-
// that name in that parent IS the folder that was asked for.
|
|
123
|
-
// Building the whole chain in memory and writing once would be fewer round trips and would
|
|
124
|
-
// also mean a lost compare-and-swap threw away folders the ones below already point at.
|
|
125
|
-
const result = await applyToList(input, (entries) => {
|
|
126
|
-
const there = entries.find((e) => e.parentId === under &&
|
|
127
|
-
normaliseName(e.name) === normaliseName(name) &&
|
|
128
|
-
e.deletedAt === undefined);
|
|
129
|
-
if (there !== undefined) {
|
|
130
|
-
if (there.kind !== KIND_FOLDER) {
|
|
131
|
-
throw new DriveEditError("NAME_TAKEN", `"${here}" is a file, so nothing can be made inside it.`, {
|
|
132
|
-
exitCode: 4,
|
|
133
|
-
nextStep: made.length > 0 ? `The folders made so far are kept: ${made.join(", ")}.` : "Nothing was made.",
|
|
134
|
-
});
|
|
135
|
-
}
|
|
136
|
-
landedOn = there.id;
|
|
137
|
-
return null;
|
|
138
|
-
}
|
|
139
|
-
landedOn = fresh;
|
|
140
|
-
const at = Date.now();
|
|
141
|
-
return {
|
|
142
|
-
op: "add",
|
|
143
|
-
entry: { id: fresh, parentId: under, kind: KIND_FOLDER, name, size: 0, createdAt: at, updatedAt: at },
|
|
144
|
-
};
|
|
145
|
-
}).catch((error) => {
|
|
146
|
-
// ⛔ WHAT SURVIVED IS NAMED. A run that stops half way leaves real folders behind, and the
|
|
147
|
-
// message that says so was attached only to the "that is a file" refusal.
|
|
148
|
-
if (error instanceof NmtsError || made.length === 0)
|
|
149
|
-
throw error;
|
|
150
|
-
const because = error instanceof Error ? error.message : "the server refused";
|
|
151
|
-
throw new NmtsError(because, {
|
|
152
|
-
exitCode: 1,
|
|
153
|
-
nextStep: `The folders made so far are kept: ${made.join(", ")}. Running the same command again ` +
|
|
154
|
-
`makes the rest — nothing is lost.`,
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
if (result.changed)
|
|
158
|
-
made.push(here);
|
|
159
|
-
parentId = landedOn;
|
|
160
|
-
}
|
|
161
|
-
return { parentId, made };
|
|
162
|
-
}
|
|
163
|
-
/** Make one folder path. A path that is already there is a success with nothing made. */
|
|
164
|
-
export async function makeFolder(input, path) {
|
|
165
|
-
const wanted = normalisePath(path);
|
|
166
|
-
if (wanted === "") {
|
|
167
|
-
throw new DriveEditError("BAD_NAME", `"${path}" names the whole drive, not a folder in it.`, {
|
|
168
|
-
exitCode: 2,
|
|
169
|
-
nextStep: "Nothing was made.",
|
|
170
|
-
});
|
|
171
|
-
}
|
|
172
|
-
const { parentId, made } = await ensureFolderPath(input, wanted);
|
|
173
|
-
return { path: wanted, parentId, made };
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Move things into a folder. An empty destination is the top of the drive.
|
|
177
|
-
*
|
|
178
|
-
* ⛔ ONE WRITE FOR THE WHOLE RUN, however many things are named. The list is rewritten whole on
|
|
179
|
-
* every save, so a second thing costs nothing extra — while a second WRITE is a second chance
|
|
180
|
-
* to lose the compare-and-swap, and losing it half way through a run leaves some things moved
|
|
181
|
-
* and some not, which is a state the caller cannot tell apart from the one it asked for.
|
|
182
|
-
*
|
|
183
|
-
* ⛔ AND THE NAME CHECK RUNS AGAINST WHAT THIS RUN HAS ALREADY MOVED, not against the list as it
|
|
184
|
-
* was read. Two files called `notes.txt` in two folders, moved into one folder by one call,
|
|
185
|
-
* would otherwise both be written — two entries at one path, which nothing can address
|
|
186
|
-
* afterwards: every lookup answers "names 2 things in this account". So the loop folds each
|
|
187
|
-
* move onto a working copy and asks the working copy the next question.
|
|
188
|
-
*/
|
|
189
|
-
export async function moveEntries(input, paths, destination) {
|
|
190
|
-
const at = Date.now();
|
|
191
|
-
let moved = [];
|
|
192
|
-
let already = [];
|
|
193
|
-
let parentId = null;
|
|
194
|
-
// ⛔ EVERY GUARD RUNS INSIDE THE ATTEMPT, INCLUDING WHICH ENTRY EACH PATH NAMES. A lost
|
|
195
|
-
// compare-and-swap re-applies the intent to a list that changed underneath — and when the
|
|
196
|
-
// winner had just taken this name, the loser landed on top of it and produced two entries at
|
|
197
|
-
// one path. Meanwhile the caller was told the move had been made.
|
|
198
|
-
const result = await applyManyToList(input, (now) => {
|
|
199
|
-
const targets = resolving(() => batchTargets(now, paths, { nothingHappened: "Nothing was moved." }));
|
|
200
|
-
const into = resolving(() => folderIdFor(destination, now, "Nothing was moved."));
|
|
201
|
-
const index = buildIndex(now);
|
|
202
|
-
const intents = [];
|
|
203
|
-
const carried = [];
|
|
204
|
-
const there = [];
|
|
205
|
-
let working = now;
|
|
206
|
-
for (const target of targets) {
|
|
207
|
-
if (into !== null && (into === target.id || isUnder(working, into, target.id))) {
|
|
208
|
-
throw new DriveEditError("INTO_ITSELF", `A folder cannot be moved inside itself.`, {
|
|
209
|
-
exitCode: 4,
|
|
210
|
-
nextStep: "Nothing was moved.",
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
if (into === target.parentId) {
|
|
214
|
-
there.push(target.name);
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
if (namesIn(working, into).has(normaliseName(target.name))) {
|
|
218
|
-
throw new DriveEditError("NAME_TAKEN", `Something called "${target.name}" is already in that folder.`, {
|
|
219
|
-
exitCode: 4,
|
|
220
|
-
nextStep: `Nothing was moved. Rename it first: nmts rename "${target.name}" <new name>`,
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
const intent = { op: "move", id: target.id, parentId: into, at };
|
|
224
|
-
intents.push(intent);
|
|
225
|
-
working = applyIntent(working, intent);
|
|
226
|
-
carried.push({ id: target.id, name: target.name, from: fullPathOf(index, target) });
|
|
227
|
-
}
|
|
228
|
-
moved = carried;
|
|
229
|
-
already = there;
|
|
230
|
-
parentId = into;
|
|
231
|
-
return intents;
|
|
232
|
-
});
|
|
233
|
-
// ⚠ Read off the list AS WRITTEN, not off the intents: an id another device took out of the list
|
|
234
|
-
// meanwhile has no path any more, and claiming one would name a place nothing is at.
|
|
235
|
-
const after = buildIndex(result.entries);
|
|
236
|
-
return {
|
|
237
|
-
moved: moved.map((m) => {
|
|
238
|
-
const live = result.entries.find((e) => e.id === m.id);
|
|
239
|
-
return { id: m.id, name: m.name, from: m.from, path: live === undefined ? null : fullPathOf(after, live) };
|
|
240
|
-
}),
|
|
241
|
-
already,
|
|
242
|
-
parentId,
|
|
243
|
-
changed: result.changed,
|
|
244
|
-
reappliedAfterConflict: result.reappliedAfterConflict,
|
|
245
|
-
seq: result.seq,
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Give one thing a new name. The path stays the same otherwise.
|
|
250
|
-
*
|
|
251
|
-
* ⛔ REFUSED RATHER THAN NUMBERED, AND THE REFUSAL IS RE-DECIDED ON EVERY ATTEMPT. An upload picks
|
|
252
|
-
* `report (2).pdf` because nobody was watching; a rename is somebody choosing a name on purpose,
|
|
253
|
-
* and silently giving them a different one is how two files end up looking like a mistake nobody
|
|
254
|
-
* made. Checking once, before the write, was not enough: when another device took the name in
|
|
255
|
-
* between, the retry re-applied the old decision and produced two entries at one path, which
|
|
256
|
-
* nothing can address afterwards (2026-08-23).
|
|
257
|
-
*/
|
|
258
|
-
export async function renameEntry(input, path, name) {
|
|
259
|
-
requireNewName(name);
|
|
260
|
-
const list = await readFileList(input.server, input.apiKey, input.code, input.accountId);
|
|
261
|
-
const entries = list.manifest?.entries ?? [];
|
|
262
|
-
const target = resolving(() => entryAt(entries, path, { nothingHappened: "Nothing was renamed." }));
|
|
263
|
-
const at = Date.now();
|
|
264
|
-
const fromPath = fullPathOf(buildIndex(entries), target);
|
|
265
|
-
const result = await applyToList(input, (now) => {
|
|
266
|
-
const live = now.find((e) => e.id === target.id);
|
|
267
|
-
if (live === undefined)
|
|
268
|
-
return null;
|
|
269
|
-
if (normaliseName(name) !== normaliseName(live.name) && namesIn(now, live.parentId).has(normaliseName(name))) {
|
|
270
|
-
throw new DriveEditError("NAME_TAKEN", `Something called "${name}" is already in that folder.`, {
|
|
271
|
-
exitCode: 4,
|
|
272
|
-
nextStep: "Nothing was renamed.",
|
|
273
|
-
});
|
|
274
|
-
}
|
|
275
|
-
return { op: "rename", id: target.id, name, at };
|
|
276
|
-
});
|
|
277
|
-
return {
|
|
278
|
-
id: target.id,
|
|
279
|
-
from: target.name,
|
|
280
|
-
fromPath,
|
|
281
|
-
to: name,
|
|
282
|
-
changed: result.changed,
|
|
283
|
-
reappliedAfterConflict: result.reappliedAfterConflict,
|
|
284
|
-
seq: result.seq,
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Move things to the trash, or bring them back.
|
|
289
|
-
*
|
|
290
|
-
* ⛔ NEITHER HALF DESTROYS ANYTHING. `rm` moves everything it is given to the trash, where it stays
|
|
291
|
-
* restorable for thirty days; the endpoint that erases a stored row for good is closed to an API
|
|
292
|
-
* key and stays closed, so nothing here can reach it.
|
|
293
|
-
*
|
|
294
|
-
* ⛔ THE SERVER ROW GOES FIRST, AND "ALREADY DONE" COUNTS AS DONE. A trashed item's bytes cannot be
|
|
295
|
-
* fetched, so the state to avoid above all others is a list that shows a file as live when the
|
|
296
|
-
* server has already trashed it: the person sees it, asks for it, and is told it does not exist.
|
|
297
|
-
* Writing the list only after the server agreed means a failed server call leaves the drive
|
|
298
|
-
* exactly as it was — the state a caller can act on.
|
|
299
|
-
*
|
|
300
|
-
* ⛔ AND ONE PATH THAT WILL NOT RESOLVE REFUSES THE WHOLE RUN, before a single server row is
|
|
301
|
-
* touched. Trashing four of the five things somebody named and answering success is worse than
|
|
302
|
-
* trashing none: the run reads as done, and finding the odd one out means diffing the drive.
|
|
303
|
-
*/
|
|
304
|
-
export async function trashPaths(input, verb, paths, options = {}) {
|
|
305
|
-
const list = await readFileList(input.server, input.apiKey, input.code, input.accountId);
|
|
306
|
-
const entries = list.manifest?.entries ?? [];
|
|
307
|
-
// ⛔ `rm` REFUSES what is already in the trash rather than quietly doing nothing, so the caller
|
|
308
|
-
// learns nothing was needed; `restore` has to be able to SEE the trash to act on it. That is
|
|
309
|
-
// why the two lookups differ.
|
|
310
|
-
const index = buildIndex(entries);
|
|
311
|
-
const found = resolving(() => batchTargets(entries, paths, {
|
|
312
|
-
...(verb === "restore" ? { includeTrashed: true } : {}),
|
|
313
|
-
nothingHappened: "Nothing changed.",
|
|
314
|
-
}));
|
|
315
|
-
const acting = [];
|
|
316
|
-
const skipped = [];
|
|
317
|
-
for (const entry of found) {
|
|
318
|
-
const at = fullPathOf(index, entry);
|
|
319
|
-
if (verb === "restore" && isLive(index, entry)) {
|
|
320
|
-
// Already in the state being asked for. Named, and left alone — unless the caller is a
|
|
321
|
-
// program, which cannot read a line about it.
|
|
322
|
-
if (options.strict === true) {
|
|
323
|
-
throw new DriveEditError("NOT_IN_TRASH", `"${at}" is not in the trash.`, {
|
|
324
|
-
exitCode: 4,
|
|
325
|
-
nextStep: "Nothing changed. Only something in the trash can be restored.",
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
skipped.push(at);
|
|
329
|
-
continue;
|
|
330
|
-
}
|
|
331
|
-
if (verb === "restore" && entry.deletedAt === undefined) {
|
|
332
|
-
// In the trash, but only because something above it is. Restoring this row would clear a
|
|
333
|
-
// `deletedAt` it does not have and leave the person exactly where they were.
|
|
334
|
-
throw new DriveEditError("NOT_IN_TRASH", `"${at}" is in the trash because a folder above it is.`, {
|
|
335
|
-
exitCode: 4,
|
|
336
|
-
nextStep: `Nothing changed. Restore that folder instead — \`nmts ls --all\` shows which one carries the trash.`,
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
acting.push({ entry, path: at });
|
|
340
|
-
}
|
|
341
|
-
// ⛔ NAMING A FOLDER AND SOMETHING INSIDE IT IS NAMING ONE TRASHING TWICE, and only for `rm` is
|
|
342
|
-
// that a problem worth solving here: stamping the child as well would give it a thirty-day
|
|
343
|
-
// clock of its own, and then restoring the folder would leave it behind — the person would
|
|
344
|
-
// have to remember they had also named it to ever find it again. Its bytes are covered either
|
|
345
|
-
// way, because the rows are read from the folder. `restore` is the opposite case: a child with
|
|
346
|
-
// its own instant needs its own clearing, so nothing is dropped there.
|
|
347
|
-
const named = new Set(acting.map((t) => t.entry.id));
|
|
348
|
-
const covered = verb === "rm" ? acting.filter((t) => hasNamedAncestor(entries, t.entry, named)) : [];
|
|
349
|
-
const targets = acting.filter((t) => !covered.includes(t));
|
|
350
|
-
for (const t of covered)
|
|
351
|
-
skipped.push(t.path);
|
|
352
|
-
if (targets.length === 0) {
|
|
353
|
-
// Everything named was already where it was asked to be. A no-op is a success: writing the
|
|
354
|
-
// list would cost every other device a download for nothing.
|
|
355
|
-
return { paths: [], ids: [], files: 0, skipped, changed: false, reappliedAfterConflict: false, seq: list.seq ?? 0 };
|
|
356
|
-
}
|
|
357
|
-
// Every FILE at or under the targets — a folder holds no bytes and has no server row, so the
|
|
358
|
-
// rows to move are its file descendants.
|
|
359
|
-
//
|
|
360
|
-
// ⛔ THE ROWS TO MOVE ARE THE ONES THE EDIT WILL MAKE REACHABLE, so the set is read off a PREVIEW
|
|
361
|
-
// of the list rather than guessed (2026-08-23). `rm` is easy — everything under the target
|
|
362
|
-
// loses its bytes. `restore` is not: a file the person deleted separately last week keeps its
|
|
363
|
-
// own `deletedAt`, stays in the trash after the folder comes back, and its row must stay
|
|
364
|
-
// deleted with it. Restoring that row would cancel its own thirty-day sweep, go on costing
|
|
365
|
-
// storage, and leave the list saying "trashed" while the server says "live" — after which
|
|
366
|
-
// `rm` refuses to put it back and there is no way out.
|
|
367
|
-
const at = Date.now();
|
|
368
|
-
const ids = targets.map((t) => t.entry.id);
|
|
369
|
-
const preview = buildIndex(applyIntent(entries, intentFor(verb, ids, at)));
|
|
370
|
-
const under = uniqueById(targets.flatMap((t) => filesUnder(entries, t.entry.id)));
|
|
371
|
-
// ⚠ Judged on the PREVIEW's own row, not on the one in hand: `applyIntent` returns new objects,
|
|
372
|
-
// so asking the preview about the old object reads the old `deletedAt` and answers "still
|
|
373
|
-
// trashed" for the very thing being restored.
|
|
374
|
-
const files = verb === "rm"
|
|
375
|
-
? under
|
|
376
|
-
: under.filter((f) => {
|
|
377
|
-
const after = preview.byId.get(f.id);
|
|
378
|
-
return after !== undefined && isLive(preview, after);
|
|
379
|
-
});
|
|
380
|
-
let done = 0;
|
|
381
|
-
try {
|
|
382
|
-
for (const file of files) {
|
|
383
|
-
await setTrashed(input.server, input.apiKey, file.id, verb === "rm");
|
|
384
|
-
done += 1;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
catch (error) {
|
|
388
|
-
// ⛔ A HALF-FINISHED RUN MUST NAME ITSELF. Without this an agent sees six words of stderr and
|
|
389
|
-
// the tool's own guidance ("a refusal is not a transient error, do not retry in a loop")
|
|
390
|
-
// steers it away from the one thing that fixes this — running the same command again.
|
|
391
|
-
const because = error instanceof Error ? error.message : "the server refused";
|
|
392
|
-
throw new NmtsError(because, {
|
|
393
|
-
exitCode: 1,
|
|
394
|
-
nextStep: `${done} of ${files.length} file rows were moved before this stopped, and the file list was ` +
|
|
395
|
-
`not written. Running \`nmts ${verb}\` on the same paths again finishes the job — nothing is lost.`,
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
// ⛔ THE IDS ARE DECIDED AGAIN ON EVERY ATTEMPT, and this is not ceremony. Between the read above
|
|
399
|
-
// and the write below another device can put one of these targets in the trash — by trashing
|
|
400
|
-
// it, or by moving it under a folder that already is. Re-applying the intent we built earlier
|
|
401
|
-
// would then stamp `deletedAt` on something that is ALREADY in the trash by inheritance,
|
|
402
|
-
// giving it a clock of its own and quietly detaching it from the folder it came with:
|
|
403
|
-
// restoring that folder afterwards would leave it behind. An id that has left the list
|
|
404
|
-
// entirely is dropped for the reason `manifest-ops.ts` gives — the other device removing it is
|
|
405
|
-
// newer information than our edit, and putting it back would undo a deletion somebody made on
|
|
406
|
-
// purpose.
|
|
407
|
-
const writing = applyManyToList(input, (now) => {
|
|
408
|
-
const nowIndex = buildIndex(now);
|
|
409
|
-
const still = ids.filter((id) => {
|
|
410
|
-
const live = nowIndex.byId.get(id);
|
|
411
|
-
if (live === undefined)
|
|
412
|
-
return false;
|
|
413
|
-
return verb === "rm" ? isLive(nowIndex, live) : live.deletedAt !== undefined;
|
|
414
|
-
});
|
|
415
|
-
if (verb === "restore" && options.strict === true)
|
|
416
|
-
refuseTakenNames(now, nowIndex.byId, still);
|
|
417
|
-
return still.length === 0 ? [] : [intentFor(verb, still, at)];
|
|
418
|
-
});
|
|
419
|
-
// ⛔ A STRICT RESTORE REFUSED HERE HAS ALREADY MOVED ITS ROWS. The name was free when the trash
|
|
420
|
-
// was read and taken by the time the list was written, so the rows above are live while the
|
|
421
|
-
// list still says trashed — the state the note on `files` calls no way out. Put the rows back
|
|
422
|
-
// before the refusal leaves. If putting them back fails too, the refusal still leaves as it
|
|
423
|
-
// is: restoring again after the rename moves the same rows and writes the list.
|
|
424
|
-
const result = await writing.catch(async (error) => {
|
|
425
|
-
if (verb === "restore" && error instanceof DriveEditError && error.code === "NAME_TAKEN") {
|
|
426
|
-
for (const file of files) {
|
|
427
|
-
await setTrashed(input.server, input.apiKey, file.id, true).catch(() => undefined);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
throw error;
|
|
431
|
-
});
|
|
432
|
-
return {
|
|
433
|
-
paths: targets.map((t) => t.path),
|
|
434
|
-
ids,
|
|
435
|
-
files: files.length,
|
|
436
|
-
skipped,
|
|
437
|
-
changed: result.changed,
|
|
438
|
-
reappliedAfterConflict: result.reappliedAfterConflict,
|
|
439
|
-
seq: result.seq,
|
|
440
|
-
};
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Refuse a restore that would land beside a live thing of the same name.
|
|
444
|
-
*
|
|
445
|
-
* ⛔ DECIDED INSIDE THE ATTEMPT like every other guard here: the name that was free when the trash
|
|
446
|
-
* was read can be taken by the time the list is written, and two entries at one path is a state
|
|
447
|
-
* no lookup can get out of.
|
|
448
|
-
*/
|
|
449
|
-
function refuseTakenNames(entries, byId, ids) {
|
|
450
|
-
const index = buildIndex(entries);
|
|
451
|
-
for (const id of ids) {
|
|
452
|
-
const entry = byId.get(id);
|
|
453
|
-
if (entry === undefined)
|
|
454
|
-
continue;
|
|
455
|
-
const holder = entries.find((e) => e.id !== id && e.parentId === entry.parentId && normaliseName(e.name) === normaliseName(entry.name) && isLive(index, e));
|
|
456
|
-
if (holder === undefined)
|
|
457
|
-
continue;
|
|
458
|
-
throw new DriveEditError("NAME_TAKEN", `Something called "${entry.name}" is already in that folder.`, {
|
|
459
|
-
exitCode: 4,
|
|
460
|
-
nextStep: "The file list was not changed. Rename the one that is there, then restore again.",
|
|
461
|
-
});
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
/** The one intent either half of the trash writes. Built in two places, so it is spelled in one. */
|
|
465
|
-
function intentFor(verb, ids, at) {
|
|
466
|
-
return verb === "rm" ? { op: "trash", ids, at } : { op: "restore", ids, at };
|
|
467
|
-
}
|
|
468
|
-
/** Is `id` at or under `rootId`? Used to refuse moving a folder into its own subtree. */
|
|
469
|
-
function isUnder(entries, id, rootId) {
|
|
470
|
-
const byId = buildIndex(entries).byId;
|
|
471
|
-
const seen = new Set();
|
|
472
|
-
let at = id;
|
|
473
|
-
while (at !== null && !seen.has(at)) {
|
|
474
|
-
if (at === rootId)
|
|
475
|
-
return true;
|
|
476
|
-
seen.add(at);
|
|
477
|
-
at = byId.get(at)?.parentId ?? null;
|
|
478
|
-
}
|
|
479
|
-
return false;
|
|
480
|
-
}
|
|
481
|
-
/** Is any ancestor of this entry in the set? Used to drop a target a named folder already covers. */
|
|
482
|
-
function hasNamedAncestor(entries, entry, named) {
|
|
483
|
-
const byId = buildIndex(entries).byId;
|
|
484
|
-
const seen = new Set([entry.id]);
|
|
485
|
-
let at = entry.parentId;
|
|
486
|
-
while (at !== null && !seen.has(at)) {
|
|
487
|
-
if (named.has(at))
|
|
488
|
-
return true;
|
|
489
|
-
seen.add(at);
|
|
490
|
-
at = byId.get(at)?.parentId ?? null;
|
|
491
|
-
}
|
|
492
|
-
return false;
|
|
493
|
-
}
|
|
494
|
-
/** One entry per id, keeping the first. Two named folders can hold the same file only once. */
|
|
495
|
-
function uniqueById(files) {
|
|
496
|
-
const byId = new Map();
|
|
497
|
-
for (const file of files)
|
|
498
|
-
if (!byId.has(file.id))
|
|
499
|
-
byId.set(file.id, file);
|
|
500
|
-
return [...byId.values()];
|
|
501
|
-
}
|
|
502
|
-
/**
|
|
503
|
-
* Every file at or under one entry.
|
|
504
|
-
*
|
|
505
|
-
* ⚠ Trashed descendants are INCLUDED HERE, and the CALLER filters. Somebody who trashed one file
|
|
506
|
-
* last week and then trashes its folder expects the folder to be gone from the server too — so
|
|
507
|
-
* `rm` takes this set whole. `restore` cannot: see the note at the call site.
|
|
508
|
-
*/
|
|
509
|
-
export function filesUnder(entries, rootId) {
|
|
510
|
-
const root = entries.find((e) => e.id === rootId);
|
|
511
|
-
if (root === undefined)
|
|
512
|
-
return [];
|
|
513
|
-
if (root.kind === KIND_FILE)
|
|
514
|
-
return [root];
|
|
515
|
-
const childrenOf = new Map();
|
|
516
|
-
for (const e of entries) {
|
|
517
|
-
const list = childrenOf.get(e.parentId);
|
|
518
|
-
if (list === undefined)
|
|
519
|
-
childrenOf.set(e.parentId, [e]);
|
|
520
|
-
else
|
|
521
|
-
list.push(e);
|
|
522
|
-
}
|
|
523
|
-
const out = [];
|
|
524
|
-
const seen = new Set([rootId]);
|
|
525
|
-
const queue = [rootId];
|
|
526
|
-
while (queue.length > 0) {
|
|
527
|
-
const id = queue.pop();
|
|
528
|
-
if (id === undefined)
|
|
529
|
-
break;
|
|
530
|
-
for (const child of childrenOf.get(id) ?? []) {
|
|
531
|
-
if (seen.has(child.id))
|
|
532
|
-
continue;
|
|
533
|
-
seen.add(child.id);
|
|
534
|
-
if (child.kind === KIND_FILE)
|
|
535
|
-
out.push(child);
|
|
536
|
-
else
|
|
537
|
-
queue.push(child.id);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
return out;
|
|
541
|
-
}
|
|
15
|
+
// ⚠ THE VERBS LIVE IN `drive-edit/`, ONE FILE EACH, AND THIS IS THE DOOR TO THEM. What a caller
|
|
16
|
+
// imports is this name — `@needmoretruth/nmts-cli/drive-edit` — so the pieces can be split and
|
|
17
|
+
// joined without a single caller changing. Nothing in the folder reaches for `node:`: the SDK's
|
|
18
|
+
// browser entry bundles what this exports.
|
|
19
|
+
export { DriveEditError, requireNewName } from "./drive-edit/errors.js";
|
|
20
|
+
export { ensureFolderPath, makeFolder } from "./drive-edit/folders.js";
|
|
21
|
+
export { moveEntries, renameEntry } from "./drive-edit/move.js";
|
|
22
|
+
export { trashPaths } from "./drive-edit/trash.js";
|
|
23
|
+
export { filesUnder } from "./drive-edit/tree.js";
|
package/dist/product.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare const BINARY_NAME = "nmts";
|
|
|
9
9
|
* beside it. Here rather than in `main.ts` because the MCP server has to say it too, and a
|
|
10
10
|
* command importing the entry point is a cycle waiting to bite.
|
|
11
11
|
*/
|
|
12
|
-
export declare const VERSION = "0.38.
|
|
12
|
+
export declare const VERSION = "0.38.1";
|
|
13
13
|
/** Where the product lives, for messages that need to send somebody somewhere real. */
|
|
14
14
|
export declare const HOME_URL = "https://nmts.me";
|
|
15
15
|
/** The source, so a person holding only the built program can find what it was built from. */
|
package/dist/product.js
CHANGED
|
@@ -18,7 +18,7 @@ export const BINARY_NAME = "nmts";
|
|
|
18
18
|
* beside it. Here rather than in `main.ts` because the MCP server has to say it too, and a
|
|
19
19
|
* command importing the entry point is a cycle waiting to bite.
|
|
20
20
|
*/
|
|
21
|
-
export const VERSION = "0.38.
|
|
21
|
+
export const VERSION = "0.38.1";
|
|
22
22
|
/** Where the product lives, for messages that need to send somebody somewhere real. */
|
|
23
23
|
export const HOME_URL = "https://nmts.me";
|
|
24
24
|
/** The source, so a person holding only the built program can find what it was built from. */
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Readable } from "node:stream";
|
|
2
|
+
import type { PlaintextSink } from "../download-sink.ts";
|
|
3
|
+
import type { ManifestEntry } from "../shared/lib/drive/manifest-codec.ts";
|
|
4
|
+
import type { DriveObject } from "./listing.ts";
|
|
5
|
+
import type { GatewayCredential } from "./sigv4.ts";
|
|
6
|
+
export interface DriveSource {
|
|
7
|
+
/** The account's live file list. Called per request; the caller decides what to cache. */
|
|
8
|
+
entries(): Promise<readonly ManifestEntry[]>;
|
|
9
|
+
/**
|
|
10
|
+
* Fetch, decrypt and deliver one file into the sink.
|
|
11
|
+
*
|
|
12
|
+
* ⛔ INJECTED RATHER THAN IMPORTED so this server can be driven by a real S3 client in a test
|
|
13
|
+
* without an account, a network and somebody's credits. A gateway whose only test is an
|
|
14
|
+
* end-to-end one is a gateway whose refusals are never tested at all.
|
|
15
|
+
*/
|
|
16
|
+
fetch(object: DriveObject, sink: PlaintextSink): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* How to change the drive, when this machine has agreed to spending.
|
|
19
|
+
*
|
|
20
|
+
* ⛔ ABSENT MEANS READ ONLY, AND THAT IS A REFUSAL RATHER THAN A GAP. Uploading spends credits,
|
|
21
|
+
* which is one of the three things this tool asks a person about once per machine, and a
|
|
22
|
+
* gateway cannot ask: its stdin is not a terminal and the caller is a program. So the
|
|
23
|
+
* agreement has to exist beforehand, and where it does not, every write says so.
|
|
24
|
+
*/
|
|
25
|
+
readonly write?: DriveWriter;
|
|
26
|
+
}
|
|
27
|
+
export interface DriveWriter {
|
|
28
|
+
/** Store `body` at this key. `size` is the byte count the client declared. */
|
|
29
|
+
put(key: string, body: Readable, size: number): Promise<void>;
|
|
30
|
+
/** Send one file to the trash, where it stays recoverable for thirty days. */
|
|
31
|
+
trash(object: DriveObject): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Staging for uploads that arrive in pieces. Absent means this gateway refuses them.
|
|
34
|
+
*
|
|
35
|
+
* ⚠ Separate from `put` because the pieces have to land somewhere before they are one file, and
|
|
36
|
+
* where that is belongs to whoever is running this rather than to the protocol.
|
|
37
|
+
*/
|
|
38
|
+
readonly multipart?: {
|
|
39
|
+
begin(key: string): Promise<string>;
|
|
40
|
+
part(uploadId: string, partNumber: number, body: Readable, size: number, expectedSha256: string | null): Promise<string>;
|
|
41
|
+
complete(uploadId: string): Promise<string>;
|
|
42
|
+
abort(uploadId: string): Promise<void>;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export interface GatewayOptions {
|
|
46
|
+
/**
|
|
47
|
+
* Every pair that may sign a request here, each optionally held to named buckets.
|
|
48
|
+
*
|
|
49
|
+
* ⛔ A LIST RATHER THAN ONE PAIR BECAUSE A BUCKET IS AN ACCOUNT. `nmts s3` makes one pair for one
|
|
50
|
+
* drive; a business serving many of its users' accounts hands each of them a pair of their
|
|
51
|
+
* own, and the restriction on the pair is what stops one customer reading another's bucket.
|
|
52
|
+
*/
|
|
53
|
+
readonly credentials: readonly GatewayCredential[];
|
|
54
|
+
/**
|
|
55
|
+
* Which drive answers to this bucket name, or null when none does.
|
|
56
|
+
*
|
|
57
|
+
* ⛔ THE GATEWAY DOES NOT KNOW WHAT A BUCKET IS. It was one name and one drive for as long as the
|
|
58
|
+
* only caller was the command-line tool; asked by a business's server it is a lookup that
|
|
59
|
+
* server does, and one it may do differently per name. What must not change is that a name
|
|
60
|
+
* this resolver refuses looks exactly like a name the caller may not touch (see below).
|
|
61
|
+
*/
|
|
62
|
+
readonly bucketOf: (name: string) => DriveSource | null | Promise<DriveSource | null>;
|
|
63
|
+
/**
|
|
64
|
+
* The names `ListBuckets` answers with, before the signing pair's own restriction is applied.
|
|
65
|
+
*
|
|
66
|
+
* ⚠ ABSENT IS A REAL ANSWER RATHER THAN A GAP. A gateway in front of a business's own lookup
|
|
67
|
+
* cannot enumerate its customers, so what it can honestly name is what the presented pair is
|
|
68
|
+
* held to — and an unrestricted pair on such a gateway is told nothing, which is true.
|
|
69
|
+
*/
|
|
70
|
+
readonly bucketNames?: () => readonly string[] | Promise<readonly string[]>;
|
|
71
|
+
/** Called with one line whenever a request is answered, so a person can watch what a tool does. */
|
|
72
|
+
readonly log?: (line: string) => void;
|
|
73
|
+
/** Passed in so a test can hold the clock still. */
|
|
74
|
+
readonly now?: () => number;
|
|
75
|
+
/**
|
|
76
|
+
* The sentence a write gets from a read-only drive. `nmts s3` says what a person runs on this
|
|
77
|
+
* machine to allow spending; a gateway somebody else runs has a different way in, and says its own.
|
|
78
|
+
*/
|
|
79
|
+
readonly readOnlyBecause?: string | undefined;
|
|
80
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import type { GatewayOptions } from "./contract.ts";
|
|
3
|
+
export declare function fail(res: ServerResponse, status: number, code: string, message: string, resource: string): void;
|
|
4
|
+
export declare function handle(req: IncomingMessage, res: ServerResponse, options: GatewayOptions): Promise<void>;
|