@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
package/dist/risk.js
CHANGED
|
@@ -39,6 +39,13 @@ export const ACTS = {
|
|
|
39
39
|
// below, under the tiers they belong to.
|
|
40
40
|
"ai-account": { tier: "none" },
|
|
41
41
|
"public-code": { tier: "none" },
|
|
42
|
+
"public-code.list": { tier: "none" },
|
|
43
|
+
// Opening a handover file someone passed on: it reads the file, fetches from Walrus and writes
|
|
44
|
+
// one file here. `handover.make`, which gives a file away, is under `high` beside `share`.
|
|
45
|
+
handover: { tier: "none" },
|
|
46
|
+
// Listing this file's public links, and opening somebody's link (no account). `link.make` is under
|
|
47
|
+
// `high` beside `share`; `link.revoke` is under `low` beside `unshare`.
|
|
48
|
+
link: { tier: "none" },
|
|
42
49
|
wallet: { tier: "none" },
|
|
43
50
|
get: { tier: "none" },
|
|
44
51
|
pull: { tier: "none" },
|
|
@@ -83,6 +90,7 @@ export const ACTS = {
|
|
|
83
90
|
rm: { tier: "low", what: "Move these to the trash, restorable for 30 days." },
|
|
84
91
|
restore: { tier: "low", what: "Bring these back out of the trash." },
|
|
85
92
|
unshare: { tier: "low", what: "Withdraw this share. Whoever had it cannot download it again." },
|
|
93
|
+
"link.revoke": { tier: "low", what: "Cut a public link." },
|
|
86
94
|
tip: { tier: "none" },
|
|
87
95
|
"tip.set": { tier: "none" },
|
|
88
96
|
"on-collision.set": { tier: "low", what: "Change what an upload does when its name is already taken." },
|
|
@@ -114,7 +122,8 @@ export const ACTS = {
|
|
|
114
122
|
// once has held the NMTS key — removal cannot unknow that.
|
|
115
123
|
"openers.add": { tier: "medium", what: "Let this wallet open this account and every file in it." },
|
|
116
124
|
"openers.remove": { tier: "medium", what: "Stop this wallet opening this account from now on." },
|
|
117
|
-
"public-code.publish": { tier: "medium", what: "Publish this account's public code
|
|
125
|
+
"public-code.publish": { tier: "medium", what: "Publish this account's public code so other accounts can send files to it." },
|
|
126
|
+
"public-code.new": { tier: "medium", what: "Make a new public code for this account." },
|
|
118
127
|
"losses.dismiss": { tier: "medium", what: "Put this loss notice down. It will not be shown again." },
|
|
119
128
|
// ⛔ MEDIUM AND NOT HIGH. It moves value, so it is not `low`; but it moves it INSIDE one person's
|
|
120
129
|
// own family of accounts — nothing reaches a third party, nothing is spent, and the same
|
|
@@ -123,11 +132,31 @@ export const ACTS = {
|
|
|
123
132
|
rebuild: { tier: "medium", what: "Build a file list from the server's rows and write it as this account's list." },
|
|
124
133
|
// ── high: locked until a person unlocks, asked on every run ──
|
|
125
134
|
share: { tier: "high", lock: "share", what: "Give another account this file.", asksItself: true },
|
|
135
|
+
// ⛔ THE SAME ACT AS `share`, behind the same unlock — it gives another account this file — and it
|
|
136
|
+
// cannot be taken back at all, because no row exists to delete.
|
|
137
|
+
"handover.make": {
|
|
138
|
+
tier: "high",
|
|
139
|
+
lock: "share",
|
|
140
|
+
what: "Give another account this file in a handover file. It cannot be taken back.",
|
|
141
|
+
asksItself: true,
|
|
142
|
+
},
|
|
143
|
+
// ⛔ THE SAME UNLOCK AS `share`: it gives the file to whoever holds the link, account or not.
|
|
144
|
+
"link.make": {
|
|
145
|
+
tier: "high",
|
|
146
|
+
lock: "share",
|
|
147
|
+
what: "Anyone with the link can open this file. Cutting the link stops new downloads, but copies already downloaded stay with whoever has them.",
|
|
148
|
+
},
|
|
126
149
|
"login.plain": { tier: "high", lock: "unsafe-code-storage", what: "Store the NMTS key in the clear.", standing: true },
|
|
127
150
|
"login.env": { tier: "high", lock: "plain-env", what: "Print the NMTS key for an environment variable.", standing: true },
|
|
128
151
|
extend: { tier: "high", lock: "wallet", what: "Sign a transaction that spends WAL from the wallet.", standing: true },
|
|
129
152
|
"put.wallet": { tier: "high", lock: "wallet", what: "Sign a transaction that spends WAL and SUI from the wallet.", standing: true },
|
|
130
153
|
"push.wallet": { tier: "high", lock: "wallet", what: "Sign transactions that spend WAL and SUI from the wallet.", standing: true },
|
|
154
|
+
// ⛔ NMTS HEAVY'S SELF-PAID ROAD (developer mode) SITS BESIDE THE WALLET ROADS: it signs Filecoin
|
|
155
|
+
// transactions from this key's EVM wallet and spends that wallet's deposit, under the same lock.
|
|
156
|
+
"put.evm": { tier: "high", lock: "wallet", what: "Sign Filecoin storage payments in USDFC from the EVM wallet this key derives.", standing: true },
|
|
157
|
+
"push.evm": { tier: "high", lock: "wallet", what: "Sign Filecoin storage payments in USDFC from the EVM wallet this key derives.", standing: true },
|
|
158
|
+
heavy: { tier: "none" },
|
|
159
|
+
"heavy.fund": { tier: "high", lock: "wallet", what: "Deposit USDFC from the EVM wallet this key derives into Filecoin Pay.", asksItself: true },
|
|
131
160
|
"wallet.send": { tier: "high", lock: "wallet", what: "Send coins out of the wallet.", asksItself: true },
|
|
132
161
|
"wallet.swap": { tier: "high", lock: "wallet", what: "Swap one coin for the other on a public venue.", asksItself: true },
|
|
133
162
|
"devices.sign-out": { tier: "high", lock: "sign-out", what: "Sign a device out of this account.", asksItself: true },
|
|
@@ -140,6 +169,14 @@ export const ACTS = {
|
|
|
140
169
|
// itself — there is no version for anybody to type, so the gate takes the yes.
|
|
141
170
|
"ai-account.create": { tier: "high", what: "Make a new account for an AI to work in, under the Terms in force." },
|
|
142
171
|
"accept-terms.accept": { tier: "high", what: "Accept a new version of the Terms for this account.", asksItself: true },
|
|
172
|
+
// ⛔ HIGH, AND EVERY ROAD TO IT IS THIS ACT — `public-code new --replace` too. Nothing brings a
|
|
173
|
+
// revoked code back, and whoever holds it can no longer send to this account through it. It
|
|
174
|
+
// asks itself because the question names the code — and never without a yes.
|
|
175
|
+
"public-code.revoke": {
|
|
176
|
+
tier: "high",
|
|
177
|
+
what: "Revoke a public code of this account. Nobody can send to it again, and nobody can bring it back.",
|
|
178
|
+
asksItself: true,
|
|
179
|
+
},
|
|
143
180
|
// ── ultra-high: permanent destruction ──
|
|
144
181
|
"delete-account": { tier: "ultra-high", what: "Erase this account's server record, permanently.", asksItself: true },
|
|
145
182
|
// ⛔ THE SAME ERASURE, AIMED AT AN ACCOUNT THIS ONE MADE — the row, the files, the keys and the
|
|
@@ -199,9 +236,11 @@ export function actOf(args) {
|
|
|
199
236
|
return "wallet.use";
|
|
200
237
|
return sub === "send" ? "wallet.send" : sub === "swap" ? "wallet.swap" : sub === "donate" ? "wallet.donate" : "wallet";
|
|
201
238
|
case "put":
|
|
202
|
-
return args.pay === "wallet" ? "put.wallet" : "put";
|
|
239
|
+
return args.pay === "wallet" ? "put.wallet" : args.pay === "evm" ? "put.evm" : "put";
|
|
203
240
|
case "push":
|
|
204
|
-
return args.pay === "wallet" ? "push.wallet" : "push";
|
|
241
|
+
return args.pay === "wallet" ? "push.wallet" : args.pay === "evm" ? "push.evm" : "push";
|
|
242
|
+
case "heavy":
|
|
243
|
+
return sub === "fund" ? "heavy.fund" : "heavy";
|
|
205
244
|
case "key":
|
|
206
245
|
return sub === "new" ? "key.new" : sub === "revoke" ? "key.revoke" : "key";
|
|
207
246
|
case "ai-account":
|
|
@@ -215,7 +254,20 @@ export function actOf(args) {
|
|
|
215
254
|
case "losses":
|
|
216
255
|
return args.dismiss !== undefined ? "losses.dismiss" : "losses";
|
|
217
256
|
case "public-code":
|
|
257
|
+
// ⚠ `--save [file]` first: its operand is a file name, not one of the verbs below.
|
|
258
|
+
if (args.save)
|
|
259
|
+
return "public-code";
|
|
260
|
+
if (sub === "list")
|
|
261
|
+
return "public-code.list";
|
|
262
|
+
if (sub === "revoke" || (sub === "new" && args.replace !== undefined))
|
|
263
|
+
return "public-code.revoke";
|
|
264
|
+
if (sub === "new")
|
|
265
|
+
return "public-code.new";
|
|
218
266
|
return args.publish ? "public-code.publish" : "public-code";
|
|
267
|
+
case "handover":
|
|
268
|
+
return sub === "make" ? "handover.make" : "handover";
|
|
269
|
+
case "link":
|
|
270
|
+
return sub === "make" ? "link.make" : sub === "revoke" ? "link.revoke" : "link";
|
|
219
271
|
case "support":
|
|
220
272
|
return sub === "send" || sub === "reply" ? "support.send" : "support";
|
|
221
273
|
case "platform":
|
package/dist/run-log.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export declare function runLogIsOff(): boolean;
|
|
|
47
47
|
* ⚠ The path is recorded as it was requested, query string and all. Anything secret in it is a
|
|
48
48
|
* label by the time it lands: `redact` runs over the whole record before it is written.
|
|
49
49
|
*/
|
|
50
|
-
export declare function noteRequest(method: string,
|
|
50
|
+
export declare function noteRequest(method: string, requested: string, status: number, error?: string): void;
|
|
51
51
|
/** This run failed, with this sentence. Called from the entry point's own catch. */
|
|
52
52
|
export declare function noteFailure(message: string): void;
|
|
53
53
|
/** Throw away what has been collected. For tests, and for a second run inside one process. */
|
package/dist/run-log.js
CHANGED
|
@@ -53,8 +53,12 @@ export const MAX_ATTACHED_RUNS = 20;
|
|
|
53
53
|
* ⛔ `--code`, `--passphrase` AND `--api-key` DO NOT EXIST TODAY, and they are here anyway. The
|
|
54
54
|
* rule in `args.ts` is that no option carries a secret; this table is what makes the log safe
|
|
55
55
|
* on the day somebody argues for an exception to it.
|
|
56
|
+
*
|
|
57
|
+
* ⛔ `--to` IS WHOM. A handover exists so that nobody but the two people knows who gave a file to
|
|
58
|
+
* whom; a record on this disk — and a support attachment made from it — must not say it either.
|
|
56
59
|
*/
|
|
57
60
|
const HIDDEN_VALUES = {
|
|
61
|
+
"--to": "[recipient]",
|
|
58
62
|
"--message": "[message]",
|
|
59
63
|
"--omit": "[omitted]",
|
|
60
64
|
"--code": "[account-code]",
|
|
@@ -87,7 +91,9 @@ function remember(event) {
|
|
|
87
91
|
* ⚠ The path is recorded as it was requested, query string and all. Anything secret in it is a
|
|
88
92
|
* label by the time it lands: `redact` runs over the whole record before it is written.
|
|
89
93
|
*/
|
|
90
|
-
export function noteRequest(method,
|
|
94
|
+
export function noteRequest(method, requested, status, error) {
|
|
95
|
+
// ⛔ A recipient lookup names whom a file is about to go to; the address is not written down.
|
|
96
|
+
const path = requested.replace(/^\/v1\/share-recipients\/[^/?]+/, "/v1/share-recipients/[recipient]");
|
|
91
97
|
remember(error === undefined ? { kind: "http", method, path, status } : { kind: "http", method, path, status, error });
|
|
92
98
|
}
|
|
93
99
|
/** This run failed, with this sentence. Called from the entry point's own catch. */
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Every query parameter S3 treats as naming a sub-resource.
|
|
3
|
+
*
|
|
4
|
+
* ⚠ `uploads`, `uploadId`, `delete`, `location` and `versioning` are answered; `versionId` is
|
|
5
|
+
* listed so that a request for one version is refused rather than answered with the only one.
|
|
6
|
+
*/
|
|
7
|
+
export declare const SUB_RESOURCES: readonly string[];
|
|
8
|
+
/** The sub-resource this query names, or null when it names none. */
|
|
9
|
+
export declare function subResourceOf(query: URLSearchParams): string | null;
|
|
10
|
+
export interface Address {
|
|
11
|
+
/** Empty for the service itself: `ListBuckets`. */
|
|
12
|
+
readonly bucket: string;
|
|
13
|
+
/** Empty for the bucket itself. */
|
|
14
|
+
readonly key: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* `/drive/photos/a.jpg` → bucket `drive`, key `photos/a.jpg`; or, virtual-hosted, `Host:
|
|
18
|
+
* drive.s3.example.com` and `/photos/a.jpg` → the same two.
|
|
19
|
+
*/
|
|
20
|
+
export declare function addressOf(pathname: string, host: string | undefined, virtualHostBase: string | undefined): Address;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Which bucket and which key a request names, and whether it names something this gateway does.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ A SUB-RESOURCE IS NEVER AN OBJECT. `?acl`, `?tagging`, `?policy` and the rest ask about a
|
|
4
|
+
// bucket or a key rather than for its bytes. Falling through to the object branch answered
|
|
5
|
+
// `GET ?acl` with the file, stored the XML of `PUT ?tagging` as the file, and trashed the file
|
|
6
|
+
// on `DELETE ?tagging` — each a 200 for something that did not happen, or something that should
|
|
7
|
+
// not have. Every name below is classified first, and one this gateway does not do is 501.
|
|
8
|
+
//
|
|
9
|
+
// ⚠ THE PRESIGNED `X-Amz-*` PARAMETERS AND THE SDK'S `x-id` ARE NOT SUB-RESOURCES and are not
|
|
10
|
+
// listed; the signature has already consumed them by the time anything here looks.
|
|
11
|
+
import { S3Refusal } from "./answer.js";
|
|
12
|
+
/**
|
|
13
|
+
* Every query parameter S3 treats as naming a sub-resource.
|
|
14
|
+
*
|
|
15
|
+
* ⚠ `uploads`, `uploadId`, `delete`, `location` and `versioning` are answered; `versionId` is
|
|
16
|
+
* listed so that a request for one version is refused rather than answered with the only one.
|
|
17
|
+
*/
|
|
18
|
+
export const SUB_RESOURCES = [
|
|
19
|
+
"accelerate",
|
|
20
|
+
"acl",
|
|
21
|
+
"analytics",
|
|
22
|
+
"attributes",
|
|
23
|
+
"cors",
|
|
24
|
+
"delete",
|
|
25
|
+
"encryption",
|
|
26
|
+
"intelligent-tiering",
|
|
27
|
+
"inventory",
|
|
28
|
+
"legal-hold",
|
|
29
|
+
"lifecycle",
|
|
30
|
+
"location",
|
|
31
|
+
"logging",
|
|
32
|
+
"metrics",
|
|
33
|
+
"notification",
|
|
34
|
+
"object-lock",
|
|
35
|
+
"ownershipControls",
|
|
36
|
+
"policy",
|
|
37
|
+
"policyStatus",
|
|
38
|
+
"publicAccessBlock",
|
|
39
|
+
"replication",
|
|
40
|
+
"requestPayment",
|
|
41
|
+
"restore",
|
|
42
|
+
"retention",
|
|
43
|
+
"select",
|
|
44
|
+
"tagging",
|
|
45
|
+
"torrent",
|
|
46
|
+
"uploads",
|
|
47
|
+
"versioning",
|
|
48
|
+
"versionId",
|
|
49
|
+
"versions",
|
|
50
|
+
"website",
|
|
51
|
+
];
|
|
52
|
+
/** The sub-resource this query names, or null when it names none. */
|
|
53
|
+
export function subResourceOf(query) {
|
|
54
|
+
for (const name of query.keys()) {
|
|
55
|
+
if (SUB_RESOURCES.includes(name))
|
|
56
|
+
return name;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
function decoded(part) {
|
|
61
|
+
try {
|
|
62
|
+
return decodeURIComponent(part);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
throw new S3Refusal(400, "InvalidURI", "The request path is not valid percent-encoding.");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/** `Host` without its port, lower-cased. An IPv6 literal keeps its brackets. */
|
|
69
|
+
function hostOnly(host) {
|
|
70
|
+
const lower = host.trim().toLowerCase();
|
|
71
|
+
if (lower.startsWith("[")) {
|
|
72
|
+
const close = lower.indexOf("]");
|
|
73
|
+
return close < 0 ? lower : lower.slice(0, close + 1);
|
|
74
|
+
}
|
|
75
|
+
const colon = lower.lastIndexOf(":");
|
|
76
|
+
return colon >= 0 && /^\d*$/.test(lower.slice(colon + 1)) ? lower.slice(0, colon) : lower;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* `/drive/photos/a.jpg` → bucket `drive`, key `photos/a.jpg`; or, virtual-hosted, `Host:
|
|
80
|
+
* drive.s3.example.com` and `/photos/a.jpg` → the same two.
|
|
81
|
+
*/
|
|
82
|
+
export function addressOf(pathname, host, virtualHostBase) {
|
|
83
|
+
if (virtualHostBase !== undefined && host !== undefined) {
|
|
84
|
+
const base = hostOnly(virtualHostBase).replace(/^\.+/, "");
|
|
85
|
+
const named = hostOnly(host);
|
|
86
|
+
if (base !== "" && named.endsWith(`.${base}`)) {
|
|
87
|
+
const bucket = named.slice(0, named.length - base.length - 1);
|
|
88
|
+
if (bucket !== "")
|
|
89
|
+
return { bucket, key: decoded(pathname.replace(/^\//, "")) };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const trimmed = pathname.replace(/^\//, "");
|
|
93
|
+
const at = trimmed.indexOf("/");
|
|
94
|
+
if (at < 0)
|
|
95
|
+
return { bucket: decoded(trimmed), key: "" };
|
|
96
|
+
return { bucket: decoded(trimmed.slice(0, at)), key: decoded(trimmed.slice(at + 1)) };
|
|
97
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ServerResponse } from "node:http";
|
|
2
|
+
/** A refusal this gateway decided on, carrying the S3 status and code it is answered with. */
|
|
3
|
+
export declare class S3Refusal extends Error {
|
|
4
|
+
readonly status: number;
|
|
5
|
+
readonly code: string;
|
|
6
|
+
constructor(status: number, code: string, message: string);
|
|
7
|
+
}
|
|
8
|
+
/** One failure, as the error document and headers an S3 client reads. */
|
|
9
|
+
export interface S3Answer {
|
|
10
|
+
readonly status: number;
|
|
11
|
+
readonly code: string;
|
|
12
|
+
readonly message: string;
|
|
13
|
+
/** Seconds a client should wait before sending it again, when anything said. */
|
|
14
|
+
readonly retryAfter: number | null;
|
|
15
|
+
/**
|
|
16
|
+
* What the client was not told: the failure's own code or name and its own words, for whoever
|
|
17
|
+
* runs the gateway. Null when `message` already says everything there is.
|
|
18
|
+
*/
|
|
19
|
+
readonly reason: string | null;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* How long a client is asked to wait after the storage network did not take the bytes, when nothing
|
|
23
|
+
* named a wait. The storage is paid for and the bytes are kept, so the retry costs only the push.
|
|
24
|
+
*/
|
|
25
|
+
export declare const PUSH_RETRY_AFTER_SECONDS = 5;
|
|
26
|
+
/** What an S3 client should be told about a thrown value. */
|
|
27
|
+
export declare function answerFor(error: unknown): S3Answer;
|
|
28
|
+
/**
|
|
29
|
+
* Why this response failed, for whoever runs the gateway: undefined when it did not, or when its
|
|
30
|
+
* client was told everything there was.
|
|
31
|
+
*
|
|
32
|
+
* ⛔ NEVER A KEY. The request's path, and each of its segments, is taken out of the reason, since a
|
|
33
|
+
* failure's words may quote the file it was about and the log never names a file.
|
|
34
|
+
*/
|
|
35
|
+
export declare function failureReasonOf(res: ServerResponse): string | undefined;
|
|
36
|
+
/** Keep why this response failed, when its client was told less than that. */
|
|
37
|
+
export declare function noteFailure(res: ServerResponse, answer: S3Answer, resource: string): void;
|
|
38
|
+
/** Answer with an XML body, or with its headers alone for a HEAD. */
|
|
39
|
+
export declare function sendXml(res: ServerResponse, status: number, body: string, extra?: Record<string, string>): void;
|
|
40
|
+
/** Answer one refusal as the error document S3 sends. */
|
|
41
|
+
export declare function refuse(res: ServerResponse, status: number, code: string, message: string, resource: string, extra?: Record<string, string>): void;
|
|
42
|
+
/**
|
|
43
|
+
* Answer whatever was thrown, if the response can still carry an answer.
|
|
44
|
+
*
|
|
45
|
+
* ⛔ A RESPONSE THAT HAS BEGUN IS DESTROYED, NOT ENDED. Its status and length went out already; a
|
|
46
|
+
* clean end after a short body is what every client files away as a whole file.
|
|
47
|
+
*/
|
|
48
|
+
export declare function failWith(res: ServerResponse, error: unknown, resource: string): void;
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// How a failure reads to an S3 client: one status and one code, chosen for what the client does next.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ THE CODE IS AN INSTRUCTION, NOT A LABEL. S3 clients branch on it: `SlowDown` and a 503 mean
|
|
4
|
+
// "wait and send it again", `AccessDenied` means "stop, this pair cannot", `InternalError` means
|
|
5
|
+
// "something broke". Answering 500 for "the account is out of credits" had a sync tool retry an
|
|
6
|
+
// upload that could never succeed until somebody paid, and answering it for "the storage network
|
|
7
|
+
// did not answer" told the same tool to give up on a request a minute's wait would have carried.
|
|
8
|
+
//
|
|
9
|
+
// ⚠ EVERY FIELD IS READ AS UNKNOWN. What reaches this file was thrown by the command-line tool, by
|
|
10
|
+
// the SDK, by Node or by a business's own resolver; none of them promised a shape, so `code`,
|
|
11
|
+
// `status` and `retryAfter` are each looked up and type-checked rather than assumed.
|
|
12
|
+
import { BodyRefusal } from "./body.js";
|
|
13
|
+
import { isKeyConflict } from "./same-file.js";
|
|
14
|
+
import { errorXml } from "./xml.js";
|
|
15
|
+
/** A refusal this gateway decided on, carrying the S3 status and code it is answered with. */
|
|
16
|
+
export class S3Refusal extends Error {
|
|
17
|
+
status;
|
|
18
|
+
code;
|
|
19
|
+
constructor(status, code, message) {
|
|
20
|
+
super(message);
|
|
21
|
+
this.name = "S3Refusal";
|
|
22
|
+
this.status = status;
|
|
23
|
+
this.code = code;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* What each kind of failure is answered with: the status, the code and one fixed sentence.
|
|
28
|
+
*
|
|
29
|
+
* ⛔ THE SENTENCE IS THIS GATEWAY'S, NOT THE FAILURE'S. What was thrown is written for whoever runs
|
|
30
|
+
* NMTS on the machine: a local path with a process id in it, a database's own error, the next
|
|
31
|
+
* command for a person to type. Through a business's gateway the client is somebody else
|
|
32
|
+
* entirely, and those words are about the business, not about the request. They go to the
|
|
33
|
+
* gateway's log instead (`failureReasonOf`).
|
|
34
|
+
*/
|
|
35
|
+
const KINDS = {
|
|
36
|
+
/** The account cannot pay for this: no retry will change that until somebody acts. */
|
|
37
|
+
account: {
|
|
38
|
+
status: 403,
|
|
39
|
+
code: "AccountProblem",
|
|
40
|
+
message: "The account this bucket belongs to cannot pay for this: it is short of credits or over a " +
|
|
41
|
+
"spending limit. Sending it again will not help until that changes.",
|
|
42
|
+
},
|
|
43
|
+
/** Whoever the account is reached through may not do this, now or with this credential. */
|
|
44
|
+
denied: {
|
|
45
|
+
status: 403,
|
|
46
|
+
code: "AccessDenied",
|
|
47
|
+
message: "The account this bucket belongs to does not allow this request. Sending it again will not " +
|
|
48
|
+
"help until that changes.",
|
|
49
|
+
},
|
|
50
|
+
/** Not now, but later: the request itself was fine. */
|
|
51
|
+
later: {
|
|
52
|
+
status: 503,
|
|
53
|
+
code: "SlowDown",
|
|
54
|
+
message: "NMTS or the storage network could not take this request just now. Send it again after a short wait.",
|
|
55
|
+
},
|
|
56
|
+
/**
|
|
57
|
+
* The upload or the account moved on while this ran; a new request starts from where it is now.
|
|
58
|
+
*
|
|
59
|
+
* ⚠ `OperationAborted` BECAUSE A 409 IS NOT RETRIED BLINDLY. The AWS SDKs and botocore retry by
|
|
60
|
+
* themselves on 500, 502, 503 and 504 and on lists of throttling and transient codes; 409 is not
|
|
61
|
+
* among those statuses and `OperationAborted` is on none of those lists, so the application
|
|
62
|
+
* hears it once. Its meaning in S3 -- a conflicting operation on this resource, to be tried
|
|
63
|
+
* again -- is what these refusals are: the same request sent again is a new upload from the start.
|
|
64
|
+
*/
|
|
65
|
+
conflict: {
|
|
66
|
+
status: 409,
|
|
67
|
+
code: "OperationAborted",
|
|
68
|
+
message: "Something else changed this upload or this account while the request ran. Send it again as a new request.",
|
|
69
|
+
},
|
|
70
|
+
/** Refused as it was sent: the same request again gets the same answer. */
|
|
71
|
+
refused: {
|
|
72
|
+
status: 400,
|
|
73
|
+
code: "InvalidRequest",
|
|
74
|
+
message: "NMTS refused this request as it was sent. Sending it again unchanged will not help.",
|
|
75
|
+
},
|
|
76
|
+
notDone: { status: 501, code: "NotImplemented", message: "NMTS does not do this yet." },
|
|
77
|
+
/** S3's own words for its own 500. */
|
|
78
|
+
internal: { status: 500, code: "InternalError", message: "We encountered an internal error. Please try again." },
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* NMTS's refusals and what each is answered as: by its code first, then by its status.
|
|
82
|
+
*
|
|
83
|
+
* ⛔ ONE TABLE. A code is listed where it says more than its status does -- `CREDITS_SHORT` is a 409
|
|
84
|
+
* on the wire and an account that cannot pay here -- or where it may arrive without a status, as
|
|
85
|
+
* `WALLET_SHORT` does. Every other refusal is placed by its status alone, which is how a code
|
|
86
|
+
* NMTS adds tomorrow still lands somewhere a client acts on sensibly. Rows are read in order.
|
|
87
|
+
*/
|
|
88
|
+
const TABLE = [
|
|
89
|
+
{
|
|
90
|
+
kind: "account",
|
|
91
|
+
codes: ["CREDITS_SHORT", "CREDIT_FILE_CAP", "CREDIT_DAILY_CAP", "WALLET_SHORT", "DEPOSIT_FEE_INSUFFICIENT"],
|
|
92
|
+
status: (s) => s === 402,
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
kind: "denied",
|
|
96
|
+
codes: [
|
|
97
|
+
"DELEGATION_INVALID",
|
|
98
|
+
"DELEGATION_EXPIRED",
|
|
99
|
+
"DELEGATION_SCOPE",
|
|
100
|
+
"NOT_A_MEMBER",
|
|
101
|
+
"API_KEY_REVOKED",
|
|
102
|
+
"API_KEY_EXPIRED",
|
|
103
|
+
"API_KEY_SCOPE",
|
|
104
|
+
"TERMS_ACCEPTANCE_REQUIRED",
|
|
105
|
+
"ACCOUNT_BANNED",
|
|
106
|
+
],
|
|
107
|
+
status: (s) => s === 401 || s === 403,
|
|
108
|
+
},
|
|
109
|
+
{ kind: "later", codes: ["RATE_LIMITED", "CHAIN_SPEND_CAP", "CHAIN_UNCERTAIN"], status: (s) => s === 429 },
|
|
110
|
+
{ kind: "conflict", codes: ["SPONSORED_IDEM_MISMATCH", "SPONSORED_STATE"], status: (s) => s === 409 },
|
|
111
|
+
{ kind: "notDone", codes: ["NOT_IMPLEMENTED"], status: (s) => s === 501 },
|
|
112
|
+
{ kind: "later", codes: [], status: (s) => s >= 500 },
|
|
113
|
+
{ kind: "refused", codes: [], status: (s) => s >= 400 && s < 500 },
|
|
114
|
+
];
|
|
115
|
+
/**
|
|
116
|
+
* How long a client is asked to wait after the storage network did not take the bytes, when nothing
|
|
117
|
+
* named a wait. The storage is paid for and the bytes are kept, so the retry costs only the push.
|
|
118
|
+
*/
|
|
119
|
+
export const PUSH_RETRY_AFTER_SECONDS = 5;
|
|
120
|
+
/** Node's names for a connection that was never made or did not last. */
|
|
121
|
+
const UNREACHABLE_CODES = new Set([
|
|
122
|
+
"ECONNRESET",
|
|
123
|
+
"ECONNREFUSED",
|
|
124
|
+
"ECONNABORTED",
|
|
125
|
+
"EAI_AGAIN",
|
|
126
|
+
"ENOTFOUND",
|
|
127
|
+
"ETIMEDOUT",
|
|
128
|
+
"EPIPE",
|
|
129
|
+
"EHOSTUNREACH",
|
|
130
|
+
"ENETUNREACH",
|
|
131
|
+
"UND_ERR_SOCKET",
|
|
132
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
133
|
+
"UND_ERR_HEADERS_TIMEOUT",
|
|
134
|
+
"UND_ERR_BODY_TIMEOUT",
|
|
135
|
+
]);
|
|
136
|
+
/**
|
|
137
|
+
* The sentences this package throws when NMTS or the storage network did not answer.
|
|
138
|
+
*
|
|
139
|
+
* ⚠ ANCHORED TO THOSE SENTENCES. A looser pattern — the word "network" anywhere — also matches a
|
|
140
|
+
* refusal such as "a file on a network this build cannot read", and answering that with
|
|
141
|
+
* `SlowDown` has a client retry forever something that will never work.
|
|
142
|
+
*/
|
|
143
|
+
const UNREACHABLE_WORDS = /^Could not reach |^fetch failed$|did not answer within \d+ ?ms|could not be read from the .+ storage network/;
|
|
144
|
+
function field(error, name) {
|
|
145
|
+
if (typeof error !== "object" || error === null || !(name in error))
|
|
146
|
+
return undefined;
|
|
147
|
+
return Reflect.get(error, name);
|
|
148
|
+
}
|
|
149
|
+
function stringField(error, name) {
|
|
150
|
+
const value = field(error, name);
|
|
151
|
+
return typeof value === "string" ? value : null;
|
|
152
|
+
}
|
|
153
|
+
function numberField(error, name) {
|
|
154
|
+
const value = field(error, name);
|
|
155
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
156
|
+
}
|
|
157
|
+
/** True when a failure is a connection to NMTS or the storage network that did not happen. */
|
|
158
|
+
function unreachable(error, depth = 0) {
|
|
159
|
+
if (depth > 4 || typeof error !== "object" || error === null)
|
|
160
|
+
return false;
|
|
161
|
+
const code = stringField(error, "code");
|
|
162
|
+
if (code !== null && UNREACHABLE_CODES.has(code))
|
|
163
|
+
return true;
|
|
164
|
+
if (error instanceof Error && UNREACHABLE_WORDS.test(error.message))
|
|
165
|
+
return true;
|
|
166
|
+
return unreachable(field(error, "cause"), depth + 1);
|
|
167
|
+
}
|
|
168
|
+
/** The step of an upload a failure names: "uploading" is the push of paid-for bytes to the relay. */
|
|
169
|
+
function pushFailed(error) {
|
|
170
|
+
return stringField(error, "phase") === "uploading";
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Which kind a failure is, when it is anything other than an accident.
|
|
174
|
+
*
|
|
175
|
+
* ⚠ A PUSH THE STORAGE NETWORK DID NOT TAKE IS READ BY ITS PHASE, NOT ITS WORDS. Its sentence starts
|
|
176
|
+
* with what was being done, which is why no pattern over sentences caught it.
|
|
177
|
+
* ⚠ EXIT CODES 2 AND 4 ARE THIS PACKAGE REFUSING ITS INPUT -- a name the drive cannot hold, an empty
|
|
178
|
+
* file -- so what was refused is the client's request, not something that broke.
|
|
179
|
+
*/
|
|
180
|
+
function kindOf(error) {
|
|
181
|
+
const code = stringField(error, "code");
|
|
182
|
+
if (code !== null) {
|
|
183
|
+
const row = TABLE.find((r) => r.codes.includes(code));
|
|
184
|
+
if (row !== undefined)
|
|
185
|
+
return row.kind;
|
|
186
|
+
}
|
|
187
|
+
const status = numberField(error, "status");
|
|
188
|
+
if (status !== null) {
|
|
189
|
+
const row = TABLE.find((r) => r.status(status));
|
|
190
|
+
if (row !== undefined)
|
|
191
|
+
return row.kind;
|
|
192
|
+
}
|
|
193
|
+
if (pushFailed(error) || unreachable(error))
|
|
194
|
+
return "later";
|
|
195
|
+
const exit = numberField(error, "exitCode");
|
|
196
|
+
if (exit === 2 || exit === 4)
|
|
197
|
+
return "refused";
|
|
198
|
+
return null;
|
|
199
|
+
}
|
|
200
|
+
/** Long enough for any sentence this package throws; a log line is no place for a stack. */
|
|
201
|
+
const MAX_REASON = 400;
|
|
202
|
+
/** The failure's own code or name and its own words, as one line for whoever runs the gateway. */
|
|
203
|
+
function reasonOf(error) {
|
|
204
|
+
const label = stringField(error, "code") ?? (error instanceof Error ? error.name : typeof error);
|
|
205
|
+
const words = error instanceof Error ? error.message : String(error);
|
|
206
|
+
return `${label}: ${words}`.replace(/\s+/g, " ").slice(0, MAX_REASON);
|
|
207
|
+
}
|
|
208
|
+
/** What an S3 client should be told about a thrown value. */
|
|
209
|
+
export function answerFor(error) {
|
|
210
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
211
|
+
// The refusals this gateway phrased itself: their words were written for the client.
|
|
212
|
+
if (error instanceof S3Refusal || error instanceof BodyRefusal) {
|
|
213
|
+
return { status: error.status, code: error.code, message, retryAfter: null, reason: null };
|
|
214
|
+
}
|
|
215
|
+
// ⛔ A KEY THAT HOLDS A DIFFERENT FILE IS 409, NOT 500 — the request was well formed and the
|
|
216
|
+
// drive declined it. Told 500 a sync tool retries the upload forever; told 409 it records a
|
|
217
|
+
// conflict and moves on. ⚠ The code stays `InvalidRequest`: the S3 codes that come with a 409
|
|
218
|
+
// and are not about buckets (`OperationAborted`, `ConditionalRequestConflict`) both mean "try
|
|
219
|
+
// again", which is the one thing that will never help here.
|
|
220
|
+
if (isKeyConflict(error))
|
|
221
|
+
return { status: 409, code: "InvalidRequest", message, retryAfter: null, reason: null };
|
|
222
|
+
const name = kindOf(error) ?? "internal";
|
|
223
|
+
const kind = KINDS[name];
|
|
224
|
+
let retryAfter = null;
|
|
225
|
+
if (name === "later") {
|
|
226
|
+
const wait = numberField(error, "retryAfter");
|
|
227
|
+
if (wait !== null)
|
|
228
|
+
retryAfter = Math.max(0, Math.ceil(wait));
|
|
229
|
+
else if (pushFailed(error))
|
|
230
|
+
retryAfter = PUSH_RETRY_AFTER_SECONDS;
|
|
231
|
+
}
|
|
232
|
+
return { status: kind.status, code: kind.code, message: kind.message, retryAfter, reason: reasonOf(error) };
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Why each failed response failed, in more words than its client was given.
|
|
236
|
+
*
|
|
237
|
+
* ⛔ HELD BESIDE THE RESPONSE, NOT PASSED BACK UP. The code that answers a failure is deep inside an
|
|
238
|
+
* operation and knows nothing of the log; whoever holds the log reads this when the response
|
|
239
|
+
* closes. A weak map, so a response nobody asks about takes its reason with it.
|
|
240
|
+
*/
|
|
241
|
+
const REASONS = new WeakMap();
|
|
242
|
+
/**
|
|
243
|
+
* Why this response failed, for whoever runs the gateway: undefined when it did not, or when its
|
|
244
|
+
* client was told everything there was.
|
|
245
|
+
*
|
|
246
|
+
* ⛔ NEVER A KEY. The request's path, and each of its segments, is taken out of the reason, since a
|
|
247
|
+
* failure's words may quote the file it was about and the log never names a file.
|
|
248
|
+
*/
|
|
249
|
+
export function failureReasonOf(res) {
|
|
250
|
+
return REASONS.get(res);
|
|
251
|
+
}
|
|
252
|
+
function escapeRegExp(text) {
|
|
253
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* `text` with the request's path replaced wherever it stands as a word: every tail of it (the key
|
|
257
|
+
* without its bucket, the name without its folders) and every segment on its own.
|
|
258
|
+
*/
|
|
259
|
+
function withoutPath(text, resource) {
|
|
260
|
+
let decoded = resource;
|
|
261
|
+
try {
|
|
262
|
+
decoded = decodeURIComponent(resource);
|
|
263
|
+
}
|
|
264
|
+
catch {
|
|
265
|
+
// A path with a broken escape is taken out as it arrived.
|
|
266
|
+
}
|
|
267
|
+
const names = new Set();
|
|
268
|
+
for (const path of [resource, decoded]) {
|
|
269
|
+
const segments = path.split("/");
|
|
270
|
+
segments.forEach((segment, at) => {
|
|
271
|
+
names.add(segment);
|
|
272
|
+
names.add(segments.slice(at).join("/"));
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
let out = text;
|
|
276
|
+
for (const name of [...names].filter((n) => n.length > 0).sort((a, b) => b.length - a.length)) {
|
|
277
|
+
out = out.replace(new RegExp(`(?<![\\p{L}\\p{N}])${escapeRegExp(name)}(?![\\p{L}\\p{N}])`, "gu"), "…");
|
|
278
|
+
}
|
|
279
|
+
return out;
|
|
280
|
+
}
|
|
281
|
+
/** Keep why this response failed, when its client was told less than that. */
|
|
282
|
+
export function noteFailure(res, answer, resource) {
|
|
283
|
+
if (answer.reason !== null)
|
|
284
|
+
REASONS.set(res, withoutPath(answer.reason, resource));
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Whether the request is still sending a body that nothing has read.
|
|
288
|
+
*
|
|
289
|
+
* ⛔ SUCH A REQUEST IS ANSWERED WITH `Connection: close`. Kept open, the connection would have to
|
|
290
|
+
* take in every byte of a body that was already refused -- gigabytes of an upload whose
|
|
291
|
+
* signature did not hold -- before it could carry the next request; closed, the reading stops
|
|
292
|
+
* with the answer. A client that sent `Expect: 100-continue` and is refused never sends it at all.
|
|
293
|
+
*/
|
|
294
|
+
function bodyStillComing(res) {
|
|
295
|
+
const req = res.req;
|
|
296
|
+
if (req === undefined || req.complete)
|
|
297
|
+
return false;
|
|
298
|
+
const length = req.headers["content-length"];
|
|
299
|
+
return req.headers["transfer-encoding"] !== undefined || (length !== undefined && length !== "0");
|
|
300
|
+
}
|
|
301
|
+
/** Answer with an XML body, or with its headers alone for a HEAD. */
|
|
302
|
+
export function sendXml(res, status, body, extra = {}) {
|
|
303
|
+
res.writeHead(status, {
|
|
304
|
+
...extra,
|
|
305
|
+
...(bodyStillComing(res) ? { connection: "close" } : {}),
|
|
306
|
+
"content-type": "application/xml",
|
|
307
|
+
"content-length": String(Buffer.byteLength(body)),
|
|
308
|
+
});
|
|
309
|
+
res.end(body);
|
|
310
|
+
}
|
|
311
|
+
/** Answer one refusal as the error document S3 sends. */
|
|
312
|
+
export function refuse(res, status, code, message, resource, extra = {}) {
|
|
313
|
+
sendXml(res, status, errorXml(code, message, resource), extra);
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Answer whatever was thrown, if the response can still carry an answer.
|
|
317
|
+
*
|
|
318
|
+
* ⛔ A RESPONSE THAT HAS BEGUN IS DESTROYED, NOT ENDED. Its status and length went out already; a
|
|
319
|
+
* clean end after a short body is what every client files away as a whole file.
|
|
320
|
+
*/
|
|
321
|
+
export function failWith(res, error, resource) {
|
|
322
|
+
const answer = answerFor(error);
|
|
323
|
+
noteFailure(res, answer, resource);
|
|
324
|
+
if (res.headersSent) {
|
|
325
|
+
if (!res.writableEnded)
|
|
326
|
+
res.destroy();
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
refuse(res, answer.status, answer.code, answer.message, resource, answer.retryAfter === null ? {} : { "retry-after": String(answer.retryAfter) });
|
|
330
|
+
}
|