@needmoretruth/nmts-cli 0.44.0 → 0.46.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/AGENTS.md +2 -2
- package/CHANGELOG.md +73 -0
- package/README.ko.md +6 -3
- package/README.md +15 -3
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +14 -0
- package/dist/args.js +11 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/get.d.ts +2 -0
- package/dist/commands/get.js +13 -1
- package/dist/commands/handover.d.ts +13 -0
- package/dist/commands/handover.js +328 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/ls.d.ts +7 -0
- package/dist/commands/ls.js +27 -6
- package/dist/commands/public-code.d.ts +6 -0
- package/dist/commands/public-code.js +46 -0
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put-thumbnail.d.ts +4 -0
- package/dist/commands/put-thumbnail.js +119 -0
- package/dist/commands/put-wallet.d.ts +2 -0
- package/dist/commands/put-wallet.js +3 -2
- package/dist/commands/put.d.ts +10 -2
- package/dist/commands/put.js +13 -7
- package/dist/commands/receive.js +3 -3
- package/dist/commands/s3.js +44 -10
- package/dist/crypto-surface.d.ts +8 -0
- package/dist/crypto-surface.js +3 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.js +3 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/drive-edit/trash.js +5 -3
- package/dist/drive-edit/tree.d.ts +6 -0
- package/dist/drive-edit/tree.js +10 -0
- package/dist/drive-erase.js +2 -2
- package/dist/drive-paths.d.ts +2 -2
- package/dist/drive-paths.js +2 -2
- package/dist/handover.d.ts +44 -0
- package/dist/handover.js +199 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +8 -2
- package/dist/help.js +16 -3
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +14 -9
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/files.js +21 -2
- package/dist/mcp-tools/reads.js +9 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +17 -1
- package/dist/portable.js +13 -1
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +30 -0
- package/dist/risk.js +23 -2
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share.d.ts +5 -0
- package/dist/share.js +1 -1
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/manifest-codec.d.ts +11 -31
- package/dist/shared/lib/drive/manifest-codec.js +15 -23
- package/dist/shared/lib/drive/manifest-index.d.ts +10 -1
- package/dist/shared/lib/drive/manifest-index.js +30 -8
- package/dist/shared/lib/drive/manifest-wire-extras.d.ts +50 -0
- package/dist/shared/lib/drive/manifest-wire-extras.js +65 -0
- package/dist/shared/lib/drive/preview-classify.d.ts +17 -0
- package/dist/shared/lib/drive/preview-classify.js +96 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +11 -4
- package/dist/upload-wallet-put.js +17 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/get.md +3 -0
- package/docs/commands/handover.md +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/ls.md +5 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +23 -1
- package/docs/commands/share.md +3 -1
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// The limits a gateway puts on what one client can make it hold, and the answers for going past them.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ A GATEWAY IN FRONT OF MANY ACCOUNTS HAS TO SAY NO. Every byte of an upload lands on this
|
|
4
|
+
// machine's disk before it is sealed, and every upload in pieces keeps a directory of plaintext
|
|
5
|
+
// until it is finished; with no ceiling, one client with a loop fills the disk every other
|
|
6
|
+
// account's uploads need. The answers are S3's own, so a client does what it already does with
|
|
7
|
+
// them: `EntityTooLarge` stops, `SlowDown` waits and sends again.
|
|
8
|
+
import { S3Refusal } from "./answer.js";
|
|
9
|
+
/** How many writes one gateway runs at once before it answers `SlowDown`. */
|
|
10
|
+
export const MAX_CONCURRENT_WRITES = 16;
|
|
11
|
+
/** How many uploads in pieces one bucket may have begun and not finished. */
|
|
12
|
+
export const MAX_UPLOADS_PER_BUCKET = 1000;
|
|
13
|
+
/** How long a client told `SlowDown` is asked to wait, in seconds. */
|
|
14
|
+
export const RETRY_AFTER_SECONDS = 1;
|
|
15
|
+
/**
|
|
16
|
+
* `SlowDown`, with the wait it names.
|
|
17
|
+
*
|
|
18
|
+
* ⚠ NOT AN `S3Refusal`, which carries no wait: this is read by its `status` (503) and `retryAfter`,
|
|
19
|
+
* which is how the answer layer turns any 503 into `SlowDown` with a `Retry-After` header.
|
|
20
|
+
*/
|
|
21
|
+
export class SlowDown extends Error {
|
|
22
|
+
status = 503;
|
|
23
|
+
retryAfter = RETRY_AFTER_SECONDS;
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "SlowDown";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** The object, or the part of it that arrived, is larger than this gateway takes. */
|
|
30
|
+
export function tooLarge(max) {
|
|
31
|
+
return new S3Refusal(400, "EntityTooLarge", `Your proposed upload exceeds the maximum allowed size: this gateway takes objects of at most ${max} bytes. Nothing was stored.`);
|
|
32
|
+
}
|
|
33
|
+
/** Refuse a declared size over the limit, before a byte of it is read. */
|
|
34
|
+
export function checkDeclaredSize(size, max) {
|
|
35
|
+
if (max !== undefined && size !== null && size > max)
|
|
36
|
+
throw tooLarge(max);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether NMTS stores a file with no bytes, which is not the same thing as a folder marker.
|
|
40
|
+
*
|
|
41
|
+
* ⛔ THE ONE PLACE THAT DECIDES IT. The upload path stores an empty file (sealed, it is a header and
|
|
42
|
+
* one empty final chunk, and costs one credit), so an empty object is stored like any other.
|
|
43
|
+
* Set this to `false` and an empty object is refused before anything is spent, with a 400 a
|
|
44
|
+
* client does not retry — never handed to a store that would fail with a 500 naming a path.
|
|
45
|
+
*/
|
|
46
|
+
export const STORES_EMPTY_FILES = true;
|
|
47
|
+
/** Refuse an object with no bytes, unless empty files are stored. A folder marker never comes here. */
|
|
48
|
+
export function checkNotEmpty(size) {
|
|
49
|
+
if (STORES_EMPTY_FILES || size !== 0)
|
|
50
|
+
return;
|
|
51
|
+
throw new S3Refusal(400, "InvalidRequest", "NMTS does not store empty files: an object with no bytes has nothing for the storage network to hold. " +
|
|
52
|
+
"A key ending in `/` with no bytes makes a folder. Nothing was stored.");
|
|
53
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** A lock per name. */
|
|
2
|
+
export interface KeyLocks {
|
|
3
|
+
/**
|
|
4
|
+
* Run `work` holding every name in `names`, and release them whatever it does.
|
|
5
|
+
*
|
|
6
|
+
* ⚠ Several names are taken in one order, sorted, so two runs that share names cannot each hold
|
|
7
|
+
* one the other is waiting for.
|
|
8
|
+
*/
|
|
9
|
+
run<T>(names: readonly string[], work: () => Promise<T>): Promise<T>;
|
|
10
|
+
}
|
|
11
|
+
export declare function createKeyLocks(): KeyLocks;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// One write at a time per key, inside one gateway process.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ WHY. Two PUTs to one key used to decide from the same list, cached for seconds: under
|
|
4
|
+
// `refuse` the loser was stored as `name (2)` and told 200 with the winner's tag, and under
|
|
5
|
+
// `replace` the older request could land last and the newer client held a tag that no longer
|
|
6
|
+
// existed. A client that gave up waiting and sent the same PUT again started a second store of
|
|
7
|
+
// the same file. Held one at a time, each write reads the list fresh after the one before it has
|
|
8
|
+
// finished, so the second of two identical uploads finds its bytes already there.
|
|
9
|
+
//
|
|
10
|
+
// ⚠ ONE PROCESS. Two processes, or another device, can still write the same key at the same
|
|
11
|
+
// moment; the file list's own compare-and-swap is what keeps both of those, and the write that
|
|
12
|
+
// finds its file under another name says so rather than answering a tag that is not its own.
|
|
13
|
+
export function createKeyLocks() {
|
|
14
|
+
/** The promise the next holder of a name waits for: the last holder letting go. */
|
|
15
|
+
const tails = new Map();
|
|
16
|
+
const take = async (name) => {
|
|
17
|
+
const before = tails.get(name) ?? Promise.resolve();
|
|
18
|
+
let letGo = () => undefined;
|
|
19
|
+
const held = new Promise((resolve) => {
|
|
20
|
+
letGo = resolve;
|
|
21
|
+
});
|
|
22
|
+
const tail = before.then(() => held);
|
|
23
|
+
tails.set(name, tail);
|
|
24
|
+
await before;
|
|
25
|
+
return () => {
|
|
26
|
+
letGo();
|
|
27
|
+
// Nobody queued behind this one: the name is forgotten, so the map holds only names in use.
|
|
28
|
+
if (tails.get(name) === tail)
|
|
29
|
+
tails.delete(name);
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
async run(names, work) {
|
|
34
|
+
const ordered = [...new Set(names)].sort();
|
|
35
|
+
const releases = [];
|
|
36
|
+
try {
|
|
37
|
+
for (const name of ordered)
|
|
38
|
+
releases.push(await take(name));
|
|
39
|
+
return await work();
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
for (const release of releases.reverse())
|
|
43
|
+
release();
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ListEditInput } from "../manifest-write.ts";
|
|
2
|
+
import type { ManifestEntry } from "../shared/lib/drive/manifest-codec.ts";
|
|
3
|
+
import type { ManifestIntent } from "../shared/lib/drive/manifest-ops.ts";
|
|
4
|
+
/** The entries in the trash that hold this name in this folder, compared as the drive compares names. */
|
|
5
|
+
export declare function trashedHolders(entries: readonly ManifestEntry[], folder: string | undefined, name: string): ManifestEntry[];
|
|
6
|
+
/** The renames that free the name: each trashed holder to the first numbered name nothing holds. */
|
|
7
|
+
export declare function freeingIntents(entries: readonly ManifestEntry[], folder: string | undefined, name: string, now: number): ManifestIntent[];
|
|
8
|
+
/**
|
|
9
|
+
* Rename whatever in the trash holds `name` in `folder`, in one write to the file list.
|
|
10
|
+
*
|
|
11
|
+
* ⛔ DECIDED AGAIN ON EVERY ATTEMPT, from the list that attempt read, as every edit of the list is.
|
|
12
|
+
*/
|
|
13
|
+
export declare function freeTrashedName(input: ListEditInput, folder: string | undefined, name: string): Promise<void>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Making room for a file whose name something in the trash still holds.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ WHY. The drive keeps a trashed entry's name taken, so that restoring it can never land on top
|
|
4
|
+
// of a live file — and the upload path, seeing the name taken, stores the new file as
|
|
5
|
+
// `report (2).pdf`. In S3 a deleted key is free. So a PUT to `report.pdf` after a DELETE of it
|
|
6
|
+
// was stored under another name, answered 500 because the key still held nothing, and stored
|
|
7
|
+
// again (and paid for again) on every retry.
|
|
8
|
+
//
|
|
9
|
+
// ⛔ THE TRASHED ONE MOVES, INSIDE THE TRASH; THE NEW ONE TAKES THE NAME. The holder is renamed to
|
|
10
|
+
// the first free numbered name, so a restore later brings it back as `report (2).pdf` beside the
|
|
11
|
+
// new file rather than on top of it. Nothing leaves the trash and nothing is destroyed. This is
|
|
12
|
+
// the gateway's rule for a key; a person's own upload in the browser or with `nmts put` still
|
|
13
|
+
// numbers the new file, which is what they see happen.
|
|
14
|
+
import { buildIndex, fullPathOf, isLive, KIND_FOLDER, namesIn, normaliseName, normalisePath } from "../drive-paths.js";
|
|
15
|
+
import { applyManyToList } from "../manifest-write.js";
|
|
16
|
+
import { uniqueFileName } from "../shared/lib/drive/unique-name.js";
|
|
17
|
+
/** The live folder a drive path names — `null` for the top — or undefined when there is none. */
|
|
18
|
+
function parentAt(entries, folder) {
|
|
19
|
+
if (folder === undefined)
|
|
20
|
+
return null;
|
|
21
|
+
const index = buildIndex(entries);
|
|
22
|
+
const wanted = normalisePath(folder);
|
|
23
|
+
const found = entries.find((e) => e.kind === KIND_FOLDER && isLive(index, e) && normalisePath(fullPathOf(index, e)) === wanted);
|
|
24
|
+
return found?.id;
|
|
25
|
+
}
|
|
26
|
+
/** The entries in the trash that hold this name in this folder, compared as the drive compares names. */
|
|
27
|
+
export function trashedHolders(entries, folder, name) {
|
|
28
|
+
const parentId = parentAt(entries, folder);
|
|
29
|
+
if (parentId === undefined)
|
|
30
|
+
return [];
|
|
31
|
+
const index = buildIndex(entries);
|
|
32
|
+
const folded = normaliseName(name);
|
|
33
|
+
return entries.filter((e) => e.parentId === parentId && normaliseName(e.name) === folded && !isLive(index, e));
|
|
34
|
+
}
|
|
35
|
+
/** The renames that free the name: each trashed holder to the first numbered name nothing holds. */
|
|
36
|
+
export function freeingIntents(entries, folder, name, now) {
|
|
37
|
+
const holders = trashedHolders(entries, folder, name);
|
|
38
|
+
const first = holders[0];
|
|
39
|
+
if (first === undefined)
|
|
40
|
+
return [];
|
|
41
|
+
const taken = namesIn(entries, first.parentId);
|
|
42
|
+
const intents = [];
|
|
43
|
+
for (const holder of holders) {
|
|
44
|
+
// Folded before asking, because `taken` is folded: asked with the other spelling, the name would
|
|
45
|
+
// look free and the "rename" would give the holder the name it already has.
|
|
46
|
+
const renamed = uniqueFileName(normaliseName(holder.name), taken);
|
|
47
|
+
taken.add(normaliseName(renamed));
|
|
48
|
+
intents.push({ op: "rename", id: holder.id, name: renamed, at: now });
|
|
49
|
+
}
|
|
50
|
+
return intents;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Rename whatever in the trash holds `name` in `folder`, in one write to the file list.
|
|
54
|
+
*
|
|
55
|
+
* ⛔ DECIDED AGAIN ON EVERY ATTEMPT, from the list that attempt read, as every edit of the list is.
|
|
56
|
+
*/
|
|
57
|
+
export async function freeTrashedName(input, folder, name) {
|
|
58
|
+
await applyManyToList(input, (entries) => freeingIntents(entries, folder, name, Date.now()));
|
|
59
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ManifestEntry } from "../shared/lib/drive/manifest-codec.ts";
|
|
2
|
+
import type { DriveWriter } from "./contract.ts";
|
|
3
|
+
import type { DriveAccount } from "./drive.ts";
|
|
4
|
+
import { type WriteCondition } from "./drive-conditions.ts";
|
|
5
|
+
import type { KeyLocks } from "./drive-lock.ts";
|
|
6
|
+
import { type DriveObject } from "./listing.ts";
|
|
7
|
+
import type { StagingStore } from "./staging.ts";
|
|
8
|
+
/** Everything the writer needs from the drive it writes. */
|
|
9
|
+
export interface WriterSetup {
|
|
10
|
+
readonly account: DriveAccount;
|
|
11
|
+
/** The drive's list: cached, or `fresh` past the cache. */
|
|
12
|
+
readonly entries: (asked?: {
|
|
13
|
+
readonly fresh?: boolean;
|
|
14
|
+
}) => Promise<readonly ManifestEntry[]>;
|
|
15
|
+
/** Drop the cached list, after a write. */
|
|
16
|
+
readonly forget: () => void;
|
|
17
|
+
readonly stagingRoot: string;
|
|
18
|
+
readonly staging: StagingStore;
|
|
19
|
+
readonly bucket: string;
|
|
20
|
+
readonly owner: () => Promise<string | null>;
|
|
21
|
+
readonly locks: KeyLocks;
|
|
22
|
+
readonly overwrite: "replace" | "refuse";
|
|
23
|
+
readonly maxObjectBytes: number | undefined;
|
|
24
|
+
readonly onAlreadyStored: ((key: string) => void) | undefined;
|
|
25
|
+
}
|
|
26
|
+
/** `photos/2026/a.jpg` → the folder to make and the name to store under. */
|
|
27
|
+
export declare function placeOf(key: string): {
|
|
28
|
+
folder: string | undefined;
|
|
29
|
+
name: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Refuse, on the list as it is cached, a write whose answer is already known: a folder in the way,
|
|
33
|
+
* or a condition that does not hold. The protocol layer asks this before reading a body, so a
|
|
34
|
+
* refusal it can know keeps its own status and costs nobody an upload; the writer asks again,
|
|
35
|
+
* inside the key's lock, and that answer is the one that decides.
|
|
36
|
+
*/
|
|
37
|
+
export declare function precheck(objects: readonly DriveObject[], key: string, condition: WriteCondition): void;
|
|
38
|
+
export declare function createWriter(setup: WriterSetup): DriveWriter;
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// How a drive is changed through the gateway: a file stored at a key, a folder made by its marker,
|
|
2
|
+
// what is at a set of keys sent to the trash. `drive.ts` builds one of these for every drive that
|
|
3
|
+
// may be written.
|
|
4
|
+
//
|
|
5
|
+
// ⛔ EVERY DECISION IS TAKEN INSIDE THE KEY'S LOCK, ON A LIST READ THERE (`drive-lock.ts`). Whether
|
|
6
|
+
// the key is free, whether a condition holds, whether the bytes are the ones already stored —
|
|
7
|
+
// each is a fact about the list at one moment, and a list cached for seconds, or read before
|
|
8
|
+
// another write to the same key finished, answers them for a moment that has passed.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ THE TAG ANSWERED IS THE TAG OF THE FILE THIS REQUEST STORED, found by the id the store
|
|
11
|
+
// returned. "Whatever is at the key afterwards" was somebody else's file whenever another write
|
|
12
|
+
// landed in between — and a client that holds a tag which never belonged to its own upload
|
|
13
|
+
// compares every later listing against the wrong thing.
|
|
14
|
+
import { mkdir as makeDir, rm as removeFile, stat } from "node:fs/promises";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { fileSink } from "../download-sink-node.js";
|
|
17
|
+
import { NmtsError } from "../errors.js";
|
|
18
|
+
import { S3Refusal } from "./answer.js";
|
|
19
|
+
import { checkWriteCondition, UNCONDITIONAL } from "./drive-conditions.js";
|
|
20
|
+
import { checkKey, sameKey } from "./drive-key.js";
|
|
21
|
+
import { checkDeclaredSize, checkNotEmpty, tooLarge } from "./drive-limits.js";
|
|
22
|
+
import { trashedHolders } from "./drive-trashed-name.js";
|
|
23
|
+
import { EMPTY_ETAG, isFolderKey, objectsOf } from "./listing.js";
|
|
24
|
+
import { spoolFileName } from "./multipart-sweep.js";
|
|
25
|
+
import { KeyConflict, refusalFor, verdictFor } from "./same-file.js";
|
|
26
|
+
import { readBodyText, spoolBody } from "./spool.js";
|
|
27
|
+
/** `photos/2026/a.jpg` → the folder to make and the name to store under. */
|
|
28
|
+
export function placeOf(key) {
|
|
29
|
+
const at = key.lastIndexOf("/");
|
|
30
|
+
if (at < 0)
|
|
31
|
+
return { folder: undefined, name: key };
|
|
32
|
+
const folder = key.slice(0, at);
|
|
33
|
+
return { folder: folder === "" ? undefined : folder, name: key.slice(at + 1) };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Refuse a key that a folder, or a file above it, already stands in the way of.
|
|
37
|
+
*
|
|
38
|
+
* ⛔ A FILE AND A FOLDER CANNOT SHARE A PATH, and the upload path, handed one that did, stored the
|
|
39
|
+
* file as `photos (2)` — a 500 for a key that still held nothing, and another stored copy on
|
|
40
|
+
* every retry. That is a conflict about the key, so it is a 409.
|
|
41
|
+
*/
|
|
42
|
+
function refuseClash(objects, key) {
|
|
43
|
+
const byKey = new Map(objects.map((o) => [sameKey(o.key), o]));
|
|
44
|
+
const path = isFolderKey(key) ? key.slice(0, -1) : key;
|
|
45
|
+
if (!isFolderKey(key) && byKey.has(sameKey(`${key}/`))) {
|
|
46
|
+
throw new KeyConflict(`A folder is already at ${key}, so no file can be stored at that key.`);
|
|
47
|
+
}
|
|
48
|
+
const names = path.split("/");
|
|
49
|
+
for (let depth = isFolderKey(key) ? names.length : names.length - 1; depth > 0; depth -= 1) {
|
|
50
|
+
const above = names.slice(0, depth).join("/");
|
|
51
|
+
const standing = byKey.get(sameKey(above));
|
|
52
|
+
if (standing !== undefined && !isFolderKey(standing.key)) {
|
|
53
|
+
throw new KeyConflict(`A file is already at ${above}, so nothing can be stored inside it.`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Refuse, on the list as it is cached, a write whose answer is already known: a folder in the way,
|
|
59
|
+
* or a condition that does not hold. The protocol layer asks this before reading a body, so a
|
|
60
|
+
* refusal it can know keeps its own status and costs nobody an upload; the writer asks again,
|
|
61
|
+
* inside the key's lock, and that answer is the one that decides.
|
|
62
|
+
*/
|
|
63
|
+
export function precheck(objects, key, condition) {
|
|
64
|
+
refuseClash(objects, key);
|
|
65
|
+
const folder = isFolderKey(key);
|
|
66
|
+
checkWriteCondition(objects.find((o) => isFolderKey(o.key) === folder && sameKey(o.key) === sameKey(key)), condition);
|
|
67
|
+
}
|
|
68
|
+
/** The id `store` answered with, when it answered one. */
|
|
69
|
+
function storedId(stored) {
|
|
70
|
+
if (typeof stored !== "object" || stored === null)
|
|
71
|
+
return null;
|
|
72
|
+
const id = Reflect.get(stored, "id");
|
|
73
|
+
return typeof id === "string" && id !== "" ? id : null;
|
|
74
|
+
}
|
|
75
|
+
export function createWriter(setup) {
|
|
76
|
+
const { account, locks, overwrite } = setup;
|
|
77
|
+
const max = setup.maxObjectBytes;
|
|
78
|
+
const lockName = (key) => `${setup.bucket}\u0000${sameKey(key)}`;
|
|
79
|
+
const fresh = async () => objectsOf(await setup.entries({ fresh: true }));
|
|
80
|
+
/**
|
|
81
|
+
* Store one local file at a drive key, making the folders above it if they are missing.
|
|
82
|
+
*
|
|
83
|
+
* ⭐ IDENTICAL CONTENT IS NOT AN ERROR. Nothing is sent and nothing is charged, and the caller is
|
|
84
|
+
* told the upload finished — because the statement it was making, "that file is at that key",
|
|
85
|
+
* is true. Answering 409 there is what made every backup run fail on every file it had already
|
|
86
|
+
* stored, and a sync tool writes 409 down as a failure.
|
|
87
|
+
*
|
|
88
|
+
* ⛔ UNDER `replace`, EVERY STORE ASKS FOR THE OVERWRITE RULE, not only the ones this list says
|
|
89
|
+
* are taken: another device can put a file at the key between this read and the store, and the
|
|
90
|
+
* rename rule would then store this one beside it as `name (2)`.
|
|
91
|
+
*/
|
|
92
|
+
const storeFile = (key, path, meta, condition = UNCONDITIONAL) => locks.run([lockName(key)], async () => {
|
|
93
|
+
checkKey(key);
|
|
94
|
+
const { folder, name } = placeOf(key);
|
|
95
|
+
const known = await setup.entries({ fresh: true });
|
|
96
|
+
const objects = objectsOf(known);
|
|
97
|
+
refuseClash(objects, key);
|
|
98
|
+
const standing = objects.find((o) => !isFolderKey(o.key) && sameKey(o.key) === sameKey(key));
|
|
99
|
+
checkWriteCondition(standing, condition);
|
|
100
|
+
const size = (await stat(path)).size;
|
|
101
|
+
checkNotEmpty(size);
|
|
102
|
+
if (max !== undefined && size > max)
|
|
103
|
+
throw tooLarge(max);
|
|
104
|
+
if (standing !== undefined) {
|
|
105
|
+
if (standing.key !== key) {
|
|
106
|
+
if (overwrite !== "replace")
|
|
107
|
+
throw refusalFor("spelling", key, standing.key);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const verdict = await account.withCode((code) => verdictFor(standing, code, path));
|
|
111
|
+
if (verdict === "same") {
|
|
112
|
+
setup.onAlreadyStored?.(key);
|
|
113
|
+
return { etag: standing.etag, outcome: "unchanged" };
|
|
114
|
+
}
|
|
115
|
+
if (overwrite !== "replace")
|
|
116
|
+
throw refusalFor(verdict === "free" ? "differs" : verdict, key);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (folder !== undefined)
|
|
120
|
+
await account.makeFolder(folder);
|
|
121
|
+
// ⛔ A NAME ONLY THE TRASH HOLDS IS FREE TO S3, so the trashed holder moves aside first
|
|
122
|
+
// (`drive-trashed-name.ts`) — otherwise the upload path numbers this file instead.
|
|
123
|
+
if (account.freeTrashedName !== undefined && trashedHolders(known, folder, name).length > 0) {
|
|
124
|
+
await account.freeTrashedName(folder, name);
|
|
125
|
+
}
|
|
126
|
+
const stored = await account.store(path, name, folder, { replace: overwrite === "replace", meta });
|
|
127
|
+
setup.forget();
|
|
128
|
+
const after = await fresh();
|
|
129
|
+
const id = storedId(stored);
|
|
130
|
+
const mine = id === null ? after.find((o) => o.key === key) : after.find((o) => o.entry.id === id);
|
|
131
|
+
if (mine === undefined) {
|
|
132
|
+
throw new NmtsError(`The file was stored, but the file list does not show it at ${key}.`);
|
|
133
|
+
}
|
|
134
|
+
if (mine.key !== key) {
|
|
135
|
+
throw new KeyConflict(`Something else was stored at ${key} while this upload was, so this one was stored as ${mine.key}.`);
|
|
136
|
+
}
|
|
137
|
+
return { etag: mine.etag, outcome: standing === undefined ? "stored" : "replaced" };
|
|
138
|
+
});
|
|
139
|
+
/** A zero-byte PUT to `photos/`: the folder, made if it is missing, and the empty object's tag. */
|
|
140
|
+
const makeMarker = (key, condition) => locks.run([lockName(key)], async () => {
|
|
141
|
+
const objects = await fresh();
|
|
142
|
+
refuseClash(objects, key);
|
|
143
|
+
const standing = objects.find((o) => isFolderKey(o.key) && sameKey(o.key) === sameKey(key));
|
|
144
|
+
checkWriteCondition(standing, condition);
|
|
145
|
+
if (standing !== undefined)
|
|
146
|
+
return { etag: EMPTY_ETAG, outcome: "unchanged" };
|
|
147
|
+
await account.makeFolder(key.slice(0, -1));
|
|
148
|
+
setup.forget();
|
|
149
|
+
return { etag: EMPTY_ETAG, outcome: "stored" };
|
|
150
|
+
});
|
|
151
|
+
/** A fresh 0600 file under the staging root, removed whatever `use` does. */
|
|
152
|
+
const withSpool = async (use) => {
|
|
153
|
+
await makeDir(setup.stagingRoot, { recursive: true, mode: 0o700 });
|
|
154
|
+
const spool = join(setup.stagingRoot, spoolFileName());
|
|
155
|
+
try {
|
|
156
|
+
return await use(spool);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
await removeFile(spool, { force: true });
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
const remove = (keys, condition = UNCONDITIONAL) => locks.run(keys.map(lockName), async () => {
|
|
163
|
+
const objects = await fresh();
|
|
164
|
+
const outcomes = keys.map((key) => ({ key, error: null }));
|
|
165
|
+
const going = [];
|
|
166
|
+
const seen = new Set();
|
|
167
|
+
keys.forEach((key, at) => {
|
|
168
|
+
const object = objects.find((o) => o.key === key) ?? objects.find((o) => sameKey(o.key) === sameKey(key));
|
|
169
|
+
try {
|
|
170
|
+
checkWriteCondition(object, condition);
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
outcomes[at] = { key, error };
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
// Not there, or named twice: gone either way, which is what S3 answers.
|
|
177
|
+
if (object === undefined || seen.has(object.key))
|
|
178
|
+
return;
|
|
179
|
+
seen.add(object.key);
|
|
180
|
+
// ⛔ A FOLDER MARKER NAMES THE FOLDER, NOT WHAT IS IN IT. Deleting `photos/` while files are
|
|
181
|
+
// under it would trash every one of them; in S3 it removes an empty object and leaves the
|
|
182
|
+
// files. So a folder goes to the trash only once nothing live is left inside it.
|
|
183
|
+
if (isFolderKey(object.key) && objects.some((o) => o.key !== object.key && o.key.startsWith(object.key)))
|
|
184
|
+
return;
|
|
185
|
+
going.push({ at, key, path: `/${isFolderKey(object.key) ? object.key.slice(0, -1) : object.key}` });
|
|
186
|
+
});
|
|
187
|
+
let failed = going;
|
|
188
|
+
if (going.length > 1 && account.trashMany !== undefined) {
|
|
189
|
+
try {
|
|
190
|
+
await account.trashMany(going.map((g) => g.path));
|
|
191
|
+
failed = [];
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
// One key the batch could not take refuses the whole write; each is tried on its own.
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const errors = new Map();
|
|
198
|
+
for (const one of failed) {
|
|
199
|
+
try {
|
|
200
|
+
await account.trash(one.path);
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
errors.set(one.at, error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
setup.forget();
|
|
207
|
+
if (errors.size > 0) {
|
|
208
|
+
// ⛔ A KEY ALREADY GONE IS DELETED, whoever sent it to the trash first.
|
|
209
|
+
const now = await fresh();
|
|
210
|
+
for (const [at, error] of errors) {
|
|
211
|
+
const key = keys[at] ?? "";
|
|
212
|
+
if (now.some((o) => o.key === key))
|
|
213
|
+
outcomes[at] = { key, error };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return outcomes;
|
|
217
|
+
});
|
|
218
|
+
return {
|
|
219
|
+
put: async (key, body, meta, condition = UNCONDITIONAL) => {
|
|
220
|
+
checkKey(key);
|
|
221
|
+
if (isFolderKey(key)) {
|
|
222
|
+
if (body.size !== 0) {
|
|
223
|
+
throw new S3Refusal(400, "InvalidArgument", "A key ending in `/` names a folder, and a folder holds no bytes of its own. Nothing was stored.");
|
|
224
|
+
}
|
|
225
|
+
await readBodyText(body, 0, "A folder marker");
|
|
226
|
+
return await makeMarker(key, condition);
|
|
227
|
+
}
|
|
228
|
+
checkNotEmpty(body.size);
|
|
229
|
+
checkDeclaredSize(body.size, max);
|
|
230
|
+
// ⛔ THE BODY IS SPOOLED TO A FILE FIRST, 0600, and deleted whatever happens. The upload
|
|
231
|
+
// path reserves storage, cuts parts and seals them from a file; ⛔ `spoolBody` returns
|
|
232
|
+
// only once the body's own rules held, so nothing reaches `storeFile` before that.
|
|
233
|
+
return await withSpool(async (spool) => {
|
|
234
|
+
await spoolBody(body, spool, { md5: false, limit: max });
|
|
235
|
+
return await storeFile(key, spool, meta, condition);
|
|
236
|
+
});
|
|
237
|
+
},
|
|
238
|
+
// ⚠ A COPY IS A DOWNLOAD AND AN UPLOAD. The bytes are sealed under the source file's own key,
|
|
239
|
+
// which the destination does not hold, so there is nothing to copy but the plaintext —
|
|
240
|
+
// fetched into a spool, checked against its recorded hash on the way, and stored like any
|
|
241
|
+
// other upload, same-file rule and all.
|
|
242
|
+
copy: async (from, key, meta, condition = UNCONDITIONAL) => {
|
|
243
|
+
checkKey(key);
|
|
244
|
+
if (isFolderKey(key)) {
|
|
245
|
+
throw new S3Refusal(400, "InvalidArgument", "A copy cannot be stored at a key ending in `/`, which names a folder.");
|
|
246
|
+
}
|
|
247
|
+
checkNotEmpty(from.object.size);
|
|
248
|
+
checkDeclaredSize(from.object.size, max);
|
|
249
|
+
return await withSpool(async (spool) => {
|
|
250
|
+
await from.source.fetch(from.object, fileSink(spool, { force: false }));
|
|
251
|
+
return await storeFile(key, spool, meta, condition);
|
|
252
|
+
});
|
|
253
|
+
},
|
|
254
|
+
remove,
|
|
255
|
+
multipart: setup.staging.view(setup.bucket, { store: storeFile, owner: setup.owner, maxObjectBytes: max }),
|
|
256
|
+
maxObjectBytes: max,
|
|
257
|
+
};
|
|
258
|
+
}
|
package/dist/s3/drive.d.ts
CHANGED
|
@@ -2,9 +2,11 @@ import type { PlaintextSink } from "../download-sink.ts";
|
|
|
2
2
|
import type { Network } from "../network.ts";
|
|
3
3
|
import type { ManifestEntry } from "../shared/lib/drive/manifest-codec.ts";
|
|
4
4
|
import type { ReadOptions } from "../walrus.ts";
|
|
5
|
+
import type { DriveSource, WriteMeta } from "./contract.ts";
|
|
6
|
+
import { type KeyLocks } from "./drive-lock.ts";
|
|
5
7
|
import type { DriveObject } from "./listing.ts";
|
|
6
|
-
import type
|
|
7
|
-
|
|
8
|
+
import { type StagingStore } from "./staging.ts";
|
|
9
|
+
export { placeOf } from "./drive-write.ts";
|
|
8
10
|
/**
|
|
9
11
|
* How long a file list may be reused before it is fetched again.
|
|
10
12
|
*
|
|
@@ -12,16 +14,20 @@ import { type Staging } from "./staging.ts";
|
|
|
12
14
|
* mean a server round trip and a decryption for each one, so a listing of a large drive would
|
|
13
15
|
* take minutes and cost the account's rate budget. ⚠ It also means a file uploaded from another
|
|
14
16
|
* device can be up to this long in appearing here, which is the trade and is written in the
|
|
15
|
-
* tool's own words when it starts.
|
|
17
|
+
* tool's own words when it starts. Every write reads the list fresh, past this cache.
|
|
16
18
|
*/
|
|
17
19
|
export declare const LIST_CACHE_MS = 5000;
|
|
20
|
+
/** What `store` may answer: the id of the file it stored, so the tag answered is that file's. */
|
|
21
|
+
export interface StoredFile {
|
|
22
|
+
readonly id?: string | undefined;
|
|
23
|
+
}
|
|
18
24
|
/**
|
|
19
25
|
* Where a drive comes from, for whoever is running the gateway.
|
|
20
26
|
*
|
|
21
|
-
* ⛔
|
|
22
|
-
* client whose key may be in a business's sealed store and is borrowed one call at a
|
|
23
|
-
* Nothing in this file may care which, so nothing in this file is handed a key --
|
|
24
|
-
* borrows one for the length of a comparison and the caller decides what that costs.
|
|
27
|
+
* ⛔ A HANDFUL OF FUNCTIONS AND NO CREDENTIAL. The command-line tool holds an open session; the SDK
|
|
28
|
+
* holds a client whose key may be in a business's sealed store and is borrowed one call at a
|
|
29
|
+
* time. Nothing in this file may care which, so nothing in this file is handed a key --
|
|
30
|
+
* `withCode` borrows one for the length of a comparison and the caller decides what that costs.
|
|
25
31
|
*/
|
|
26
32
|
export interface DriveAccount {
|
|
27
33
|
/** The account's file list, read fresh from the server. Empty for an account that has none. */
|
|
@@ -35,17 +41,34 @@ export interface DriveAccount {
|
|
|
35
41
|
withCode<T>(use: (code: string) => Promise<T>): Promise<T>;
|
|
36
42
|
/** Make this folder path, and any folder above it that is missing. */
|
|
37
43
|
makeFolder(path: string): Promise<void>;
|
|
38
|
-
/**
|
|
39
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Store one local file under `name`, in `folder` — the top of the account when undefined — and
|
|
46
|
+
* answer the stored file's id when it can (see `StoredFile`).
|
|
47
|
+
*
|
|
48
|
+
* `how.replace` true: a file already at that name goes to the trash (the upload path's
|
|
49
|
+
* "overwrite" rule). False: the upload path's "rename" rule. `how.meta` is what the client sent.
|
|
50
|
+
*/
|
|
51
|
+
store(local: string, name: string, folder: string | undefined, how: {
|
|
52
|
+
readonly replace: boolean;
|
|
53
|
+
readonly meta: WriteMeta;
|
|
54
|
+
}): Promise<StoredFile | void>;
|
|
40
55
|
/** Send one path — with its leading slash — to the trash, where it stays for thirty days. */
|
|
41
56
|
trash(path: string): Promise<void>;
|
|
57
|
+
/** Send several paths to the trash in one write to the file list. Absent: one `trash` each. */
|
|
58
|
+
trashMany?(paths: readonly string[]): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Rename, inside the trash, whatever there holds `name` in `folder`, so a new file can take the
|
|
61
|
+
* name (`drive-trashed-name.ts`). Absent: the upload path numbers the new file instead, and the
|
|
62
|
+
* gateway answers that it landed under another name.
|
|
63
|
+
*/
|
|
64
|
+
freeTrashedName?(folder: string | undefined, name: string): Promise<void>;
|
|
42
65
|
/** Fetch, decrypt and deliver one file into the sink. `fetchObject` below is how both do it. */
|
|
43
66
|
fetch(object: DriveObject, sink: PlaintextSink): Promise<void>;
|
|
44
67
|
}
|
|
45
68
|
export interface DriveSourceOptions {
|
|
46
69
|
readonly account: DriveAccount;
|
|
47
70
|
/**
|
|
48
|
-
* Where the pieces of a multipart upload wait until they are
|
|
71
|
+
* Where the pieces of a multipart upload, and each upload's body, wait until they are stored.
|
|
49
72
|
*
|
|
50
73
|
* ⛔ 0700, AND MADE WHEN IT IS FIRST NEEDED. Pieces are somebody's plaintext; leaving them in a
|
|
51
74
|
* shared temporary directory under a predictable name would put them where any other account
|
|
@@ -58,15 +81,36 @@ export interface DriveSourceOptions {
|
|
|
58
81
|
*/
|
|
59
82
|
readonly writable: boolean;
|
|
60
83
|
/**
|
|
61
|
-
* The
|
|
84
|
+
* The gateway's multipart staging, shared by every drive it builds. Absent: one of this drive's
|
|
85
|
+
* own, under `stagingRoot`.
|
|
62
86
|
*
|
|
63
|
-
* ⛔
|
|
64
|
-
*
|
|
65
|
-
*
|
|
87
|
+
* ⛔ SHARED, NOT CARRIED. An upload in pieces outlives the drive it began under — a gateway
|
|
88
|
+
* re-asking whose a bucket is builds a new one — and the staging is where it lives meanwhile.
|
|
89
|
+
* What finishes it is the drive the bucket has when the finish arrives (`staging.ts`).
|
|
90
|
+
*/
|
|
91
|
+
readonly staging?: StagingStore | undefined;
|
|
92
|
+
/** The bucket this drive answers to, which is what an upload in the shared staging is filed under. */
|
|
93
|
+
readonly bucket?: string | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* Which account this drive is — its id. An upload begun under another account is not this
|
|
96
|
+
* drive's to finish. Absent: the account behind the bucket never changes.
|
|
66
97
|
*/
|
|
67
|
-
readonly
|
|
98
|
+
readonly owner?: (() => Promise<string>) | undefined;
|
|
99
|
+
/** The gateway's per-key locks, shared by every drive it builds. Absent: this drive's own. */
|
|
100
|
+
readonly locks?: KeyLocks | undefined;
|
|
101
|
+
/** The most bytes one object may have. Absent: no limit but the upload path's own. */
|
|
102
|
+
readonly maxObjectBytes?: number | undefined;
|
|
68
103
|
/** How long a file list may be reused. `LIST_CACHE_MS` unless a caller has a reason. */
|
|
69
104
|
readonly listCacheMs?: number | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* What a write onto a key holding DIFFERENT bytes does. `refuse` unless a caller says otherwise.
|
|
107
|
+
*
|
|
108
|
+
* `refuse` answers 409 and stores nothing: the drive keeps what it has, and a person deletes it
|
|
109
|
+
* first if they mean to replace it. `replace` stores the new bytes and sends the old file to the
|
|
110
|
+
* trash, where it stays recoverable for thirty days — which is what every S3 client expects a PUT
|
|
111
|
+
* to do. Identical bytes are `unchanged` either way, and nothing is sent.
|
|
112
|
+
*/
|
|
113
|
+
readonly overwrite?: "replace" | "refuse" | undefined;
|
|
70
114
|
/**
|
|
71
115
|
* Told the key when it already held exactly these bytes, so nothing was sent.
|
|
72
116
|
*
|
|
@@ -92,16 +136,6 @@ export interface ObjectReader {
|
|
|
92
136
|
/** Hosts to read stored bytes from, instead of the network's own aggregators. */
|
|
93
137
|
readonly read?: ReadOptions | undefined;
|
|
94
138
|
}
|
|
95
|
-
/**
|
|
96
|
-
* `photos/2026/a.jpg` → the folder to make and the name to store under.
|
|
97
|
-
*
|
|
98
|
-
* A key with no slash lands at the top of the account, which is `undefined` rather than `""`: the
|
|
99
|
-
* two mean the same thing to a person and different things to the upload path.
|
|
100
|
-
*/
|
|
101
|
-
export declare function placeOf(key: string): {
|
|
102
|
-
folder: string | undefined;
|
|
103
|
-
name: string;
|
|
104
|
-
};
|
|
105
139
|
/** The real reader: the stored bytes, opened with this account's key and delivered to the sink. */
|
|
106
140
|
export declare function fetchObject(reader: ObjectReader, object: DriveObject, sink: PlaintextSink): Promise<void>;
|
|
107
141
|
/** One account as the protocol layer sees it: a cached list, a reader, and a writer when allowed. */
|