@ultimat3/storage 31.35.0 → 31.36.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/package.json +2 -2
- package/src/driver-local-remove.ts +1 -1
- package/src/driver-s3-client.ts +2 -2
- package/src/driver-s3-region.ts +7 -2
- package/src/driver-s3-signed.ts +2 -1
- package/src/driver-s3.ts +3 -1
- package/src/errors-promote.ts +1 -1
- package/src/errors.ts +13 -15
- package/src/storage.ts +5 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/storage",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.36.0",
|
|
4
4
|
"description": "Named disks over Bun.file and Bun.s3: safe keys, signed URLs, sniffed uploads",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -43,6 +43,6 @@
|
|
|
43
43
|
"test": "bun test"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@ultimat3/core": "31.
|
|
46
|
+
"@ultimat3/core": "31.36.0"
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -33,7 +33,7 @@ export async function removeIfPresent(root: string, path: string, key: string):
|
|
|
33
33
|
key,
|
|
34
34
|
error,
|
|
35
35
|
// Screened: a key may carry `$(…)` and a root is configuration, and a fix: is pasted.
|
|
36
|
-
`
|
|
36
|
+
`ls -ld ${renderFixShellArg(root, "'<the disk root>'")} && rm -f ${renderFixShellArg(path, "'<the object file>'")} # make the disk root writable by this process, then retry`,
|
|
37
37
|
);
|
|
38
38
|
}
|
|
39
39
|
}
|
package/src/driver-s3-client.ts
CHANGED
|
@@ -75,7 +75,7 @@ export function buildClient(options: S3DriverOptions): S3ClientLike {
|
|
|
75
75
|
if (options.bucket === '') {
|
|
76
76
|
throw new ConfigInvalidError({
|
|
77
77
|
cause: 's3 disk was defined without a bucket',
|
|
78
|
-
fix:
|
|
78
|
+
fix: "s3Driver({ bucket: '<bucket>' }) // the s3 disk's entry in defineStorage({ disks }), app.config.ts",
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
const env = options.env ?? process.env;
|
|
@@ -85,7 +85,7 @@ export function buildClient(options: S3DriverOptions): S3ClientLike {
|
|
|
85
85
|
if (Client === undefined) {
|
|
86
86
|
throw new ConfigInvalidError({
|
|
87
87
|
cause: 'Bun.S3Client is unavailable in this runtime',
|
|
88
|
-
fix: '
|
|
88
|
+
fix: 'bun upgrade # the s3 disk needs bun >= 1.3',
|
|
89
89
|
});
|
|
90
90
|
}
|
|
91
91
|
return new Client({
|
package/src/driver-s3-region.ts
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
// signed for the wrong region — and turn it into the disk-misconfigured error it is. Split from
|
|
3
3
|
// driver-s3.ts because every call that sends a signed request asks this before its own verdict.
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
ConfigInvalidError,
|
|
7
|
+
renderFixLiteral,
|
|
8
|
+
renderFixShellArg,
|
|
9
|
+
stringField,
|
|
10
|
+
} from '@ultimat3/core';
|
|
6
11
|
|
|
7
12
|
/** What S3 and every S3-compatible gateway answer a SigV4 scope with the wrong region in it. */
|
|
8
13
|
const REGION_MISMATCH_CODE = 'AuthorizationHeaderMalformed';
|
|
@@ -33,7 +38,7 @@ export function regionMismatch(bucket: string, error: unknown): ConfigInvalidErr
|
|
|
33
38
|
if (sent === undefined || expected === undefined) return undefined;
|
|
34
39
|
return new ConfigInvalidError({
|
|
35
40
|
cause: `the s3 disk for bucket "${bucket}" signed a request for region "${sent}" and its endpoint expects "${expected}"`,
|
|
36
|
-
fix: `
|
|
41
|
+
fix: `S3_REGION=${renderFixShellArg(expected, '<region>')} # in .env, then re-run — or pass region: ${renderFixLiteral(expected, "'<region>'")} to s3Driver`,
|
|
37
42
|
meta: { bucket, sent, expected },
|
|
38
43
|
});
|
|
39
44
|
}
|
package/src/driver-s3-signed.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
ConfigInvalidError,
|
|
13
13
|
EnvMissingError,
|
|
14
14
|
presignAwsUrl,
|
|
15
|
+
renderFixShellArg,
|
|
15
16
|
signAwsRequest,
|
|
16
17
|
systemClock,
|
|
17
18
|
trimTrailingSlashes,
|
|
@@ -80,7 +81,7 @@ export function requireEnv(
|
|
|
80
81
|
if (value === undefined || value === '') {
|
|
81
82
|
throw new EnvMissingError({
|
|
82
83
|
cause: `${name} is not set, so the s3 disk cannot authenticate`,
|
|
83
|
-
fix: `
|
|
84
|
+
fix: `export ${renderFixShellArg(name, '<the variable cause names>')}='<value>' ${renderFixShellArg(partner, '<its partner>')}='<value>' # or both in .env or the container's secret store, then re-run`,
|
|
84
85
|
meta: { missing: name },
|
|
85
86
|
});
|
|
86
87
|
}
|
package/src/driver-s3.ts
CHANGED
|
@@ -395,7 +395,9 @@ export function s3Driver(options: S3DriverOptions): StorageDriver {
|
|
|
395
395
|
safe,
|
|
396
396
|
error,
|
|
397
397
|
// `isSafeKey` admits `$(…)`, a backtick and `;` — legal key bytes, live shell syntax.
|
|
398
|
-
|
|
398
|
+
// Never a delete-object to "reproduce" it: run with a credential that CAN delete, it
|
|
399
|
+
// deletes a real object. The grant is the fix, and it changes no data.
|
|
400
|
+
`{ "Effect": "Allow", "Action": "s3:DeleteObject", "Resource": "arn:aws:s3:::${options.bucket}/*" } // add to the app's role: the provider refused the delete the cause names`,
|
|
399
401
|
);
|
|
400
402
|
}
|
|
401
403
|
},
|
package/src/errors-promote.ts
CHANGED
|
@@ -15,7 +15,7 @@ export const notPending = (key: string, orgId: string): StorageError =>
|
|
|
15
15
|
new StorageError({
|
|
16
16
|
code: 'X_STORAGE_NOT_PENDING',
|
|
17
17
|
cause: `"${key}" is not under org "${orgId}"'s pending/ prefix, so it is not an upload waiting for a row — it may already belong to one`,
|
|
18
|
-
fix: '
|
|
18
|
+
fix: 'promoteAttachment({ disk, key: pendingKey(orgId, name), orgId, target }) // promote the key grantUpload returned with no target',
|
|
19
19
|
meta: { key, orgId },
|
|
20
20
|
});
|
|
21
21
|
|
package/src/errors.ts
CHANGED
|
@@ -137,9 +137,7 @@ export const diskUnknown = (name: string, configured: readonly string[]): Storag
|
|
|
137
137
|
cause: `no disk named "${name}" (configured: ${
|
|
138
138
|
configured.length > 0 ? configured.join(', ') : 'none'
|
|
139
139
|
})`,
|
|
140
|
-
fix: `
|
|
141
|
-
configured[0] ?? 'local'
|
|
142
|
-
}')`,
|
|
140
|
+
fix: `disk('${configured[0] ?? 'local'}') // a configured disk — or declare "${name}" in defineStorage({ disks }) in app.config.ts`,
|
|
143
141
|
meta: { name, configured },
|
|
144
142
|
});
|
|
145
143
|
|
|
@@ -160,7 +158,7 @@ export const pathUnsafe = (key: string, reason: string): StorageError =>
|
|
|
160
158
|
new StorageError({
|
|
161
159
|
code: 'X_STORAGE_PATH_UNSAFE',
|
|
162
160
|
cause: `key ${JSON.stringify(key)} ${reason}`,
|
|
163
|
-
fix: '
|
|
161
|
+
fix: 'scopedKey(orgId, ...parts) // builds the key relative, with forward slashes and no ".."',
|
|
164
162
|
meta: { key, reason },
|
|
165
163
|
});
|
|
166
164
|
|
|
@@ -168,7 +166,7 @@ export const tooLarge = (key: string, bytes: number, maxBytes: number): StorageE
|
|
|
168
166
|
new StorageError({
|
|
169
167
|
code: 'X_STORAGE_TOO_LARGE',
|
|
170
168
|
cause: `"${key}" is ${bytes}B, over the policy limit of ${maxBytes}B`,
|
|
171
|
-
fix: `
|
|
169
|
+
fix: `uploadPolicy({ maxBytes: ${bytes} }) // a higher limit in the upload policy — or compress the file first`,
|
|
172
170
|
meta: { key, bytes, maxBytes },
|
|
173
171
|
});
|
|
174
172
|
|
|
@@ -192,7 +190,7 @@ export const putTooLarge = (
|
|
|
192
190
|
code: 'X_STORAGE_TOO_LARGE',
|
|
193
191
|
cause: `"${key}" measured ${bytes}B against the ${disk} disk's put ceiling of ${maxBytes}B, and put() buffers the whole body`,
|
|
194
192
|
fix:
|
|
195
|
-
`
|
|
193
|
+
`grantUpload({ disk, orgId, request }) // send it direct instead of put() — or raise the ceiling: ` +
|
|
196
194
|
(disk === 's3'
|
|
197
195
|
? `s3Driver({ bucket, maxPutBytes: ${bytes} })`
|
|
198
196
|
: disk === 'memory'
|
|
@@ -254,7 +252,7 @@ export const contentTypeNotAllowed = (
|
|
|
254
252
|
new StorageError({
|
|
255
253
|
code: 'X_STORAGE_TYPE_REJECTED',
|
|
256
254
|
cause: `"${key}" declares ${declared}, which is not in the policy allowlist (${allowed.join(', ')})`,
|
|
257
|
-
fix: `
|
|
255
|
+
fix: `uploadPolicy({ allowedContentTypes: [...allowed, ${renderFixLiteral(declared, "'<type>'")}] }) // or upload one of: ${allowed.join(', ')}`,
|
|
258
256
|
meta: { key, declared, allowed },
|
|
259
257
|
});
|
|
260
258
|
|
|
@@ -263,7 +261,7 @@ export const contentTypeMismatch = (key: string, declared: string, sniffed: stri
|
|
|
263
261
|
new StorageError({
|
|
264
262
|
code: 'X_STORAGE_TYPE_REJECTED',
|
|
265
263
|
cause: `"${key}" declares ${declared} but its magic bytes are ${sniffed}`,
|
|
266
|
-
fix: `
|
|
264
|
+
fix: `validateUpload({ key, declaredContentType: ${renderFixLiteral(sniffed, "'<sniffed type>'")}, bytes }, policy) // re-upload declaring what the bytes are — or upload a genuine ${declared} file`,
|
|
267
265
|
meta: { key, declared, sniffed },
|
|
268
266
|
});
|
|
269
267
|
|
|
@@ -284,7 +282,7 @@ export const contentTypeUnrecognised = (key: string, declared: string): StorageE
|
|
|
284
282
|
new StorageError({
|
|
285
283
|
code: 'X_STORAGE_TYPE_REJECTED',
|
|
286
284
|
cause: `"${key}" declares ${declared}, and its first bytes match no known signature — every ${declared} carries one`,
|
|
287
|
-
fix: `
|
|
285
|
+
fix: `validateUpload({ key: ${renderFixLiteral(key, "'<key>'")}, declaredContentType: ${renderFixLiteral(declared, "'<type>'")}, bytes }, policy) // re-sent with the bytes of a real ${declared}: the ones sent open with no ${declared} signature, so they are another format or truncated`,
|
|
288
286
|
meta: { key, declared },
|
|
289
287
|
});
|
|
290
288
|
|
|
@@ -306,7 +304,7 @@ export const checksumMismatch = (key: string, declared: string, actual: string):
|
|
|
306
304
|
new StorageError({
|
|
307
305
|
code: 'X_STORAGE_CHECKSUM_MISMATCH',
|
|
308
306
|
cause: `"${key}" declared sha256 ${declared} but the bytes hash to ${actual}`,
|
|
309
|
-
fix: '
|
|
307
|
+
fix: 'checksum: sha256Base64(bytes) // over the exact bytes you send — or omit checksum and let the driver hash',
|
|
310
308
|
meta: { key, declared, actual },
|
|
311
309
|
});
|
|
312
310
|
|
|
@@ -320,7 +318,7 @@ export const signedUrlRejected = (reason: string, detail: string): StorageError
|
|
|
320
318
|
new StorageError({
|
|
321
319
|
code: 'X_STORAGE_URL_INVALID',
|
|
322
320
|
cause: `the signed request was rejected as ${reason}: ${detail}`,
|
|
323
|
-
fix: '
|
|
321
|
+
fix: 'grantUpload({ disk, orgId, request }) // mint a fresh one and send it unedited — every constraint is inside the signature',
|
|
324
322
|
meta: { reason, detail },
|
|
325
323
|
});
|
|
326
324
|
|
|
@@ -328,7 +326,7 @@ export const signedUrlExpired = (key: string, detail: string): StorageError =>
|
|
|
328
326
|
new StorageError({
|
|
329
327
|
code: 'X_STORAGE_URL_EXPIRED',
|
|
330
328
|
cause: `the signed request for "${key}" ${detail}`,
|
|
331
|
-
fix: '
|
|
329
|
+
fix: 'grantUpload({ disk, orgId, request, expiresInMs }) // again — a longer expiresInMs widens the window the signature grants',
|
|
332
330
|
meta: { key, detail },
|
|
333
331
|
});
|
|
334
332
|
|
|
@@ -426,7 +424,7 @@ export const orgMismatch = (key: string, orgId: string): StorageError =>
|
|
|
426
424
|
new StorageError({
|
|
427
425
|
code: 'X_STORAGE_ORG_MISMATCH',
|
|
428
426
|
cause: `key "${key}" is not inside org "${orgId}"`,
|
|
429
|
-
fix: `
|
|
427
|
+
fix: `scopedKey(${renderFixLiteral(orgId, 'orgId')}, ...parts) // and pass the ACTOR's org as orgId — never one read off the request`,
|
|
430
428
|
meta: { key, orgId },
|
|
431
429
|
});
|
|
432
430
|
|
|
@@ -450,7 +448,7 @@ export const uploadFailed = (path: string, status: number, detail: string): Stor
|
|
|
450
448
|
new StorageError({
|
|
451
449
|
code: 'X_STORAGE_UPLOAD_FAILED',
|
|
452
450
|
cause: `PUT ${path} answered ${status}: ${detail === '' ? 'no body' : detail}`,
|
|
453
|
-
fix: '
|
|
451
|
+
fix: 'await uploadFile({ file, grant }) // again, for a fresh grant; a 4xx here is the constraint named in the body, not a transport fault',
|
|
454
452
|
meta: { path, status, detail },
|
|
455
453
|
});
|
|
456
454
|
|
|
@@ -474,7 +472,7 @@ export const signedUrlUnverifiable = (diskName: string): StorageError =>
|
|
|
474
472
|
new StorageError({
|
|
475
473
|
code: 'X_STORAGE_URL_UNVERIFIABLE',
|
|
476
474
|
cause: `the "${diskName}" disk was asked to verify a signed URL with no secret in the call, and its driver implements no verifySigned()`,
|
|
477
|
-
fix: '
|
|
475
|
+
fix: 'await acceptSignedUpload({ ...input, secret }) // or a driver that signs its own URLs (localDriver) — a provider-signed disk (s3) verifies at the provider and never reaches this path',
|
|
478
476
|
meta: { disk: diskName },
|
|
479
477
|
});
|
|
480
478
|
|
package/src/storage.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// only holder of that fact and a signed URL's `:disk` segment has to be the name the mounted route
|
|
7
7
|
// resolves.
|
|
8
8
|
|
|
9
|
-
import { ConfigInvalidError } from '@ultimat3/core';
|
|
9
|
+
import { ConfigInvalidError, renderFixLiteral } from '@ultimat3/core';
|
|
10
10
|
import type { StorageDriver } from './driver';
|
|
11
11
|
import { diskUnknown } from './errors';
|
|
12
12
|
import { isSafeKey, isTenantScoped } from './path';
|
|
@@ -83,7 +83,7 @@ export function defineStorage(config: StorageConfig): Storage {
|
|
|
83
83
|
if (names.length === 0) {
|
|
84
84
|
throw new ConfigInvalidError({
|
|
85
85
|
cause: 'defineStorage() was called with no disks',
|
|
86
|
-
fix: "
|
|
86
|
+
fix: "defineStorage({ disks: { local: localDriver({ root: '.storage' }) } }) // at least one disk",
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
const first = names[0] ?? '';
|
|
@@ -91,7 +91,7 @@ export function defineStorage(config: StorageConfig): Storage {
|
|
|
91
91
|
if (!names.includes(defaultDisk)) {
|
|
92
92
|
throw new ConfigInvalidError({
|
|
93
93
|
cause: `storage.default is "${defaultDisk}" but the configured disks are: ${names.join(', ')}`,
|
|
94
|
-
fix: `
|
|
94
|
+
fix: `defineStorage({ disks, default: ${renderFixLiteral(first, "'local'")} }) // in app.config.ts — default names one of: ${names.join(', ')}`,
|
|
95
95
|
});
|
|
96
96
|
}
|
|
97
97
|
// A Map, and the SAME own-keys read `names` above is built from. `config.disks[wanted]` walked
|
|
@@ -109,7 +109,7 @@ export function defineStorage(config: StorageConfig): Storage {
|
|
|
109
109
|
if (already !== undefined) {
|
|
110
110
|
throw new ConfigInvalidError({
|
|
111
111
|
cause: `storage disks "${already}" and "${diskName}" are the same driver instance, and a driver can only mint URLs under one name`,
|
|
112
|
-
fix: `
|
|
112
|
+
fix: `disks: { ${already}: localDriver({ root: '.storage/${already}' }), ${diskName}: localDriver({ root: '.storage/${diskName}' }) } // in app.config.ts: "${diskName}" gets its own driver`,
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
registered.set(driver, diskName);
|
|
@@ -144,7 +144,7 @@ export function storage(): Storage {
|
|
|
144
144
|
if (current === undefined) {
|
|
145
145
|
throw new ConfigInvalidError({
|
|
146
146
|
cause: 'storage() was called before defineStorage()',
|
|
147
|
-
fix: "
|
|
147
|
+
fix: "defineStorage({ disks: { local: localDriver({ root: '.storage' }) } }) // in app.config.ts, before the first storage()",
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
150
|
return current;
|