@needmoretruth/nmts-cli 0.45.0 → 0.47.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 +8 -8
- package/CHANGELOG.md +78 -0
- package/README.ko.md +6 -3
- package/README.md +31 -11
- 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 +19 -1
- package/dist/args.js +14 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/autonomy.js +3 -3
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +15 -0
- package/dist/commands/handover.js +376 -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/link.d.ts +12 -0
- package/dist/commands/link.js +121 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code-manage.d.ts +24 -0
- package/dist/commands/public-code-manage.js +217 -0
- package/dist/commands/public-code.d.ts +12 -1
- package/dist/commands/public-code.js +140 -45
- 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.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +14 -8
- package/dist/commands/s3.js +44 -10
- package/dist/commands/share-dispatch.js +14 -1
- package/dist/commands/share.d.ts +2 -0
- package/dist/commands/share.js +45 -35
- package/dist/commands/shares-sent.js +8 -3
- package/dist/crypto-surface.d.ts +35 -0
- package/dist/crypto-surface.js +13 -0
- package/dist/crypto.d.ts +8 -0
- package/dist/crypto.js +8 -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/handover-codes.d.ts +25 -0
- package/dist/handover-codes.js +106 -0
- package/dist/handover.d.ts +62 -0
- package/dist/handover.js +217 -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 +13 -4
- package/dist/help.js +24 -3
- package/dist/links.d.ts +58 -0
- package/dist/links.js +206 -0
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +13 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/reads.js +20 -6
- package/dist/mcp-tools/share.js +47 -1
- package/dist/mcp-tools/tiers.js +16 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +23 -0
- package/dist/portable.js +19 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/public-code-activity.d.ts +35 -0
- package/dist/public-code-activity.js +88 -0
- package/dist/public-code-refusals.d.ts +25 -0
- package/dist/public-code-refusals.js +87 -0
- package/dist/public-codes.d.ts +76 -0
- package/dist/public-codes.js +182 -0
- 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 +55 -1
- package/dist/risk.js +55 -3
- 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-codes.d.ts +40 -0
- package/dist/share-codes.js +151 -0
- package/dist/share.d.ts +39 -0
- package/dist/share.js +54 -13
- 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/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/public-link.d.ts +41 -0
- package/dist/shared/lib/share/public-link.js +107 -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 +9 -4
- package/dist/upload-wallet-put.js +16 -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/handover.md +54 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/link.md +27 -0
- package/docs/commands/mcp.md +2 -1
- package/docs/commands/public-code.md +25 -6
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +8 -3
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// What a write asks of the key before it will happen: `If-Match` and `If-None-Match` on an upload,
|
|
2
|
+
// a finish, a copy's destination or a delete, and the four `x-amz-copy-source-if-*` on a copy's
|
|
3
|
+
// source.
|
|
4
|
+
//
|
|
5
|
+
// ⛔ A CONDITION THIS GATEWAY CANNOT HONOUR IS REFUSED WITH 501, NEVER IGNORED. A client that sends
|
|
6
|
+
// `If-None-Match: *` is relying on it to stop two writers overwriting each other; answering 200
|
|
7
|
+
// while ignoring it tells that client the one thing that did not happen.
|
|
8
|
+
//
|
|
9
|
+
// ⛔ JUDGED AGAINST THE LIST READ INSIDE THE KEY'S LOCK (`drive-lock.ts`), where the drive decides
|
|
10
|
+
// the write. A check made earlier, on a list cached for seconds, only lets an answer that is
|
|
11
|
+
// already known keep its own status; it is never the one that decides.
|
|
12
|
+
import { S3Refusal } from "./answer.js";
|
|
13
|
+
import { headerOf } from "./call.js";
|
|
14
|
+
export const UNCONDITIONAL = { ifMatch: null, ifNoneMatch: false };
|
|
15
|
+
function notHonoured(header, operation) {
|
|
16
|
+
return new S3Refusal(501, "NotImplemented", `This gateway does not honour ${header} on ${operation}. Nothing was changed.`);
|
|
17
|
+
}
|
|
18
|
+
function preconditionFailed() {
|
|
19
|
+
return new S3Refusal(412, "PreconditionFailed", "At least one of the preconditions you specified did not hold.");
|
|
20
|
+
}
|
|
21
|
+
function bareTag(tag) {
|
|
22
|
+
return tag.trim().replace(/^W\//, "").replace(/^"|"$/g, "");
|
|
23
|
+
}
|
|
24
|
+
/** Whether a list of tags in a header names this one. `*` names any object that exists. */
|
|
25
|
+
function tagListHas(header, etag) {
|
|
26
|
+
const mine = bareTag(etag);
|
|
27
|
+
return header.split(",").some((tag) => tag.trim() === "*" || bareTag(tag) === mine);
|
|
28
|
+
}
|
|
29
|
+
/** The HTTP conditions a request carries. */
|
|
30
|
+
const CONDITIONAL = ["if-match", "if-none-match", "if-modified-since", "if-unmodified-since"];
|
|
31
|
+
/**
|
|
32
|
+
* Refuse a request that carries a condition, for an operation that honours none.
|
|
33
|
+
*
|
|
34
|
+
* ⚠ `UploadPart`, `CreateMultipartUpload`, `AbortMultipartUpload` and `DeleteObjects` take no
|
|
35
|
+
* conditions in S3 either; a client that sends one anyway is told so rather than answered as if
|
|
36
|
+
* it had held.
|
|
37
|
+
*/
|
|
38
|
+
export function refuseConditions(req, operation) {
|
|
39
|
+
for (const name of CONDITIONAL) {
|
|
40
|
+
if (headerOf(req, name) !== undefined)
|
|
41
|
+
throw notHonoured(name, operation);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/** What `If-Match` and `If-None-Match` ask of a write's destination. */
|
|
45
|
+
export function writeConditionOf(req, operation) {
|
|
46
|
+
for (const name of ["if-modified-since", "if-unmodified-since"]) {
|
|
47
|
+
if (headerOf(req, name) !== undefined)
|
|
48
|
+
throw notHonoured(name, operation);
|
|
49
|
+
}
|
|
50
|
+
const noneMatch = headerOf(req, "if-none-match")?.trim();
|
|
51
|
+
if (noneMatch !== undefined && noneMatch !== "*")
|
|
52
|
+
throw notHonoured("If-None-Match other than `*`", operation);
|
|
53
|
+
const match = headerOf(req, "if-match")?.trim();
|
|
54
|
+
return { ifMatch: match === undefined || match === "" ? null : match, ifNoneMatch: noneMatch === "*" };
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* What a `DeleteObject` asks: `If-Match` is honoured; the rest of S3's conditions on a delete are
|
|
58
|
+
* refused, since nothing here can judge them.
|
|
59
|
+
*/
|
|
60
|
+
export function deleteConditionOf(req) {
|
|
61
|
+
for (const name of [
|
|
62
|
+
"if-none-match",
|
|
63
|
+
"if-modified-since",
|
|
64
|
+
"if-unmodified-since",
|
|
65
|
+
"x-amz-if-match-last-modified-time",
|
|
66
|
+
"x-amz-if-match-size",
|
|
67
|
+
]) {
|
|
68
|
+
if (headerOf(req, name) !== undefined)
|
|
69
|
+
throw notHonoured(name, "DeleteObject");
|
|
70
|
+
}
|
|
71
|
+
const match = headerOf(req, "if-match")?.trim();
|
|
72
|
+
return { ifMatch: match === undefined || match === "" ? null : match, ifNoneMatch: false };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Whether what stands at the key now meets the condition. Throws 412 when it does not, and 404 for
|
|
76
|
+
* `If-Match` on a key that holds nothing — S3's two answers.
|
|
77
|
+
*/
|
|
78
|
+
export function checkWriteCondition(standing, condition) {
|
|
79
|
+
if (condition.ifNoneMatch && standing !== undefined)
|
|
80
|
+
throw preconditionFailed();
|
|
81
|
+
if (condition.ifMatch !== null) {
|
|
82
|
+
if (standing === undefined) {
|
|
83
|
+
throw new S3Refusal(404, "NoSuchKey", "If-Match names a tag, and nothing is at that key. Nothing was changed.");
|
|
84
|
+
}
|
|
85
|
+
if (!tagListHas(condition.ifMatch, standing.etag))
|
|
86
|
+
throw preconditionFailed();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/** An HTTP date as whole seconds. An unreadable one is refused: a condition nobody can read is not held. */
|
|
90
|
+
function secondsOf(name, value) {
|
|
91
|
+
if (value === undefined)
|
|
92
|
+
return null;
|
|
93
|
+
const ms = Date.parse(value);
|
|
94
|
+
if (Number.isNaN(ms))
|
|
95
|
+
throw new S3Refusal(400, "InvalidArgument", `${name} is not a date.`);
|
|
96
|
+
return Math.floor(ms / 1000);
|
|
97
|
+
}
|
|
98
|
+
/** The four `x-amz-copy-source-if-*` headers. */
|
|
99
|
+
export function copySourceConditionOf(req) {
|
|
100
|
+
const tag = (name) => {
|
|
101
|
+
const value = headerOf(req, name)?.trim();
|
|
102
|
+
return value === undefined || value === "" ? null : value;
|
|
103
|
+
};
|
|
104
|
+
return {
|
|
105
|
+
ifMatch: tag("x-amz-copy-source-if-match"),
|
|
106
|
+
ifNoneMatch: tag("x-amz-copy-source-if-none-match"),
|
|
107
|
+
ifModifiedSince: secondsOf("x-amz-copy-source-if-modified-since", headerOf(req, "x-amz-copy-source-if-modified-since")),
|
|
108
|
+
ifUnmodifiedSince: secondsOf("x-amz-copy-source-if-unmodified-since", headerOf(req, "x-amz-copy-source-if-unmodified-since")),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Whether a copy's source meets its conditions. Throws 412 when it does not.
|
|
113
|
+
*
|
|
114
|
+
* ⚠ S3'S PAIRINGS: a matching `if-match` copies whatever `if-unmodified-since` says, and a failing
|
|
115
|
+
* `if-none-match` refuses whatever `if-modified-since` says — each tag condition, when present,
|
|
116
|
+
* decides in place of its date.
|
|
117
|
+
*/
|
|
118
|
+
export function checkCopySource(object, condition) {
|
|
119
|
+
const changed = Math.floor(object.entry.updatedAt / 1000);
|
|
120
|
+
if (condition.ifMatch !== null) {
|
|
121
|
+
if (!tagListHas(condition.ifMatch, object.etag))
|
|
122
|
+
throw preconditionFailed();
|
|
123
|
+
}
|
|
124
|
+
else if (condition.ifUnmodifiedSince !== null && changed > condition.ifUnmodifiedSince) {
|
|
125
|
+
throw preconditionFailed();
|
|
126
|
+
}
|
|
127
|
+
if (condition.ifNoneMatch !== null) {
|
|
128
|
+
if (tagListHas(condition.ifNoneMatch, object.etag))
|
|
129
|
+
throw preconditionFailed();
|
|
130
|
+
}
|
|
131
|
+
else if (condition.ifModifiedSince !== null && changed <= condition.ifModifiedSince) {
|
|
132
|
+
throw preconditionFailed();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** S3's own ceiling on a key, in UTF-8 bytes. */
|
|
2
|
+
export declare const MAX_KEY_BYTES = 1024;
|
|
3
|
+
/** The form two keys are compared in: the drive's own folding of names. */
|
|
4
|
+
export declare function sameKey(key: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Refuse a key this drive cannot hold as the path it names. A key ending in `/` is a folder marker
|
|
7
|
+
* and is judged as the folder it names.
|
|
8
|
+
*
|
|
9
|
+
* ⛔ EVERY CHECK IS ON THE KEY ALONE, so this runs before the body is read and before any list is.
|
|
10
|
+
*/
|
|
11
|
+
export declare function checkKey(key: string): void;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Which S3 keys a drive can hold, and when two keys are the same key.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ A KEY IS A PATH IN A DRIVE, AND NOT EVERY S3 KEY IS ONE. S3 takes any bytes up to 1,024 of
|
|
4
|
+
// them; a drive has folders and names, so `/x`, `a//b`, `a/./b` and a name made of control
|
|
5
|
+
// characters have nowhere to go. Stored anyway, they came back under a different key — `/x`
|
|
6
|
+
// listed as `x` — and the gateway answered 500 "stored but not shown" for a file it had paid
|
|
7
|
+
// for, which a client retries, paying again each time. So they are refused BEFORE the body is
|
|
8
|
+
// read, with a 400 a client does not retry.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ TWO SPELLINGS OF ONE NAME ARE ONE KEY. The drive folds names to Unicode NFC when it compares
|
|
11
|
+
// them — macOS writes `café` decomposed and a browser composed — so a key is compared the same
|
|
12
|
+
// way here. Compared byte for byte instead, an upload to the other spelling was "free", stored
|
|
13
|
+
// beside the file a person sees as the same one, or replaced it without asking whether its bytes
|
|
14
|
+
// were the same.
|
|
15
|
+
import { S3Refusal } from "./answer.js";
|
|
16
|
+
import { isFolderKey } from "./listing.js";
|
|
17
|
+
/** S3's own ceiling on a key, in UTF-8 bytes. */
|
|
18
|
+
export const MAX_KEY_BYTES = 1024;
|
|
19
|
+
/** The form two keys are compared in: the drive's own folding of names. */
|
|
20
|
+
export function sameKey(key) {
|
|
21
|
+
return key.normalize("NFC");
|
|
22
|
+
}
|
|
23
|
+
function badKey(why) {
|
|
24
|
+
return new S3Refusal(400, "InvalidArgument", `${why} Nothing was read or stored.`);
|
|
25
|
+
}
|
|
26
|
+
/** True when a string holds a C0 control character (U+0000 to U+001F). */
|
|
27
|
+
function hasControl(text) {
|
|
28
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
29
|
+
if (text.charCodeAt(i) < 0x20)
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Refuse a key this drive cannot hold as the path it names. A key ending in `/` is a folder marker
|
|
36
|
+
* and is judged as the folder it names.
|
|
37
|
+
*
|
|
38
|
+
* ⛔ EVERY CHECK IS ON THE KEY ALONE, so this runs before the body is read and before any list is.
|
|
39
|
+
*/
|
|
40
|
+
export function checkKey(key) {
|
|
41
|
+
if (Buffer.byteLength(key, "utf8") > MAX_KEY_BYTES) {
|
|
42
|
+
throw new S3Refusal(400, "KeyTooLongError", `The key is longer than ${MAX_KEY_BYTES} bytes. Nothing was read or stored.`);
|
|
43
|
+
}
|
|
44
|
+
if (hasControl(key))
|
|
45
|
+
throw badKey("The key holds a control character, which no name in a drive can hold.");
|
|
46
|
+
if (key.startsWith("/")) {
|
|
47
|
+
throw badKey("The key begins with `/`, and a path in a drive has no empty name at its start.");
|
|
48
|
+
}
|
|
49
|
+
const path = isFolderKey(key) ? key.slice(0, -1) : key;
|
|
50
|
+
for (const name of path.split("/")) {
|
|
51
|
+
if (name === "")
|
|
52
|
+
throw badKey("The key has an empty name between two `/`.");
|
|
53
|
+
if (name === "." || name === "..")
|
|
54
|
+
throw badKey("The key has `.` or `..` as a name, which a path in a drive cannot.");
|
|
55
|
+
if (name.trim() === "")
|
|
56
|
+
throw badKey("The key has a name that is only spaces.");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { S3Refusal } from "./answer.ts";
|
|
2
|
+
/** How many writes one gateway runs at once before it answers `SlowDown`. */
|
|
3
|
+
export declare const MAX_CONCURRENT_WRITES = 16;
|
|
4
|
+
/** How many uploads in pieces one bucket may have begun and not finished. */
|
|
5
|
+
export declare const MAX_UPLOADS_PER_BUCKET = 1000;
|
|
6
|
+
/** How long a client told `SlowDown` is asked to wait, in seconds. */
|
|
7
|
+
export declare const RETRY_AFTER_SECONDS = 1;
|
|
8
|
+
/**
|
|
9
|
+
* `SlowDown`, with the wait it names.
|
|
10
|
+
*
|
|
11
|
+
* ⚠ NOT AN `S3Refusal`, which carries no wait: this is read by its `status` (503) and `retryAfter`,
|
|
12
|
+
* which is how the answer layer turns any 503 into `SlowDown` with a `Retry-After` header.
|
|
13
|
+
*/
|
|
14
|
+
export declare class SlowDown extends Error {
|
|
15
|
+
readonly status = 503;
|
|
16
|
+
readonly retryAfter = 1;
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
/** The object, or the part of it that arrived, is larger than this gateway takes. */
|
|
20
|
+
export declare function tooLarge(max: number): S3Refusal;
|
|
21
|
+
/** Refuse a declared size over the limit, before a byte of it is read. */
|
|
22
|
+
export declare function checkDeclaredSize(size: number | null, max: number | undefined): void;
|
|
23
|
+
/**
|
|
24
|
+
* Whether NMTS stores a file with no bytes, which is not the same thing as a folder marker.
|
|
25
|
+
*
|
|
26
|
+
* ⛔ THE ONE PLACE THAT DECIDES IT. The upload path stores an empty file (sealed, it is a header and
|
|
27
|
+
* one empty final chunk, and costs one credit), so an empty object is stored like any other.
|
|
28
|
+
* Set this to `false` and an empty object is refused before anything is spent, with a 400 a
|
|
29
|
+
* client does not retry — never handed to a store that would fail with a 500 naming a path.
|
|
30
|
+
*/
|
|
31
|
+
export declare const STORES_EMPTY_FILES: boolean;
|
|
32
|
+
/** Refuse an object with no bytes, unless empty files are stored. A folder marker never comes here. */
|
|
33
|
+
export declare function checkNotEmpty(size: number | null): void;
|
|
@@ -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;
|