@softistx/s3 0.0.0-stage → 0.13.2
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/LICENSE +21 -0
- package/README.md +609 -2
- package/dist/bucket/bind-bucket.d.ts +42 -0
- package/dist/bucket/bind-bucket.d.ts.map +1 -0
- package/dist/bucket/bound-bucket.d.ts +119 -0
- package/dist/bucket/bound-bucket.d.ts.map +1 -0
- package/dist/bucket/context.d.ts +55 -0
- package/dist/bucket/context.d.ts.map +1 -0
- package/dist/bucket/define-bucket.d.ts +16 -0
- package/dist/bucket/define-bucket.d.ts.map +1 -0
- package/dist/bucket/disposition.d.ts +9 -0
- package/dist/bucket/disposition.d.ts.map +1 -0
- package/dist/bucket/guards.d.ts +38 -0
- package/dist/bucket/guards.d.ts.map +1 -0
- package/dist/bucket/operations/counted-stream.d.ts +26 -0
- package/dist/bucket/operations/counted-stream.d.ts.map +1 -0
- package/dist/bucket/operations/deadline.d.ts +21 -0
- package/dist/bucket/operations/deadline.d.ts.map +1 -0
- package/dist/bucket/operations/fetch-object.d.ts +68 -0
- package/dist/bucket/operations/fetch-object.d.ts.map +1 -0
- package/dist/bucket/operations/list-xml.d.ts +18 -0
- package/dist/bucket/operations/list-xml.d.ts.map +1 -0
- package/dist/bucket/operations/list.d.ts +12 -0
- package/dist/bucket/operations/list.d.ts.map +1 -0
- package/dist/bucket/operations/open-answer.d.ts +5 -0
- package/dist/bucket/operations/open-answer.d.ts.map +1 -0
- package/dist/bucket/operations/open-range.d.ts +10 -0
- package/dist/bucket/operations/open-range.d.ts.map +1 -0
- package/dist/bucket/operations/open-relay.d.ts +8 -0
- package/dist/bucket/operations/open-relay.d.ts.map +1 -0
- package/dist/bucket/operations/open.d.ts +4 -0
- package/dist/bucket/operations/open.d.ts.map +1 -0
- package/dist/bucket/operations/post-policy.d.ts +77 -0
- package/dist/bucket/operations/post-policy.d.ts.map +1 -0
- package/dist/bucket/operations/post-signer.d.ts +21 -0
- package/dist/bucket/operations/post-signer.d.ts.map +1 -0
- package/dist/bucket/operations/presign-post.d.ts +44 -0
- package/dist/bucket/operations/presign-post.d.ts.map +1 -0
- package/dist/bucket/operations/presign.d.ts +34 -0
- package/dist/bucket/operations/presign.d.ts.map +1 -0
- package/dist/bucket/operations/put-body.d.ts +13 -0
- package/dist/bucket/operations/put-body.d.ts.map +1 -0
- package/dist/bucket/operations/query-sign.d.ts +15 -0
- package/dist/bucket/operations/query-sign.d.ts.map +1 -0
- package/dist/bucket/operations/reads.d.ts +8 -0
- package/dist/bucket/operations/reads.d.ts.map +1 -0
- package/dist/bucket/operations/sized-body.d.ts +24 -0
- package/dist/bucket/operations/sized-body.d.ts.map +1 -0
- package/dist/bucket/operations/slice.d.ts +4 -0
- package/dist/bucket/operations/slice.d.ts.map +1 -0
- package/dist/bucket/operations/writes.d.ts +10 -0
- package/dist/bucket/operations/writes.d.ts.map +1 -0
- package/dist/bucket/shape.d.ts +6 -0
- package/dist/bucket/shape.d.ts.map +1 -0
- package/dist/bucket/types.d.ts +164 -0
- package/dist/bucket/types.d.ts.map +1 -0
- package/dist/chunks/index-jgjey570.js +118 -0
- package/dist/chunks/index-jgjey570.js.map +13 -0
- package/dist/errors/is-bucket-unavailable.d.ts +18 -0
- package/dist/errors/is-bucket-unavailable.d.ts.map +1 -0
- package/dist/errors/s3-error.d.ts +80 -0
- package/dist/errors/s3-error.d.ts.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1288 -0
- package/dist/index.js.map +32 -0
- package/dist/testing/fetch-archive.d.ts +8 -0
- package/dist/testing/fetch-archive.d.ts.map +1 -0
- package/dist/testing/index.d.ts +6 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +416 -0
- package/dist/testing/index.js.map +16 -0
- package/dist/testing/ports.d.ts +15 -0
- package/dist/testing/ports.d.ts.map +1 -0
- package/dist/testing/ready.d.ts +49 -0
- package/dist/testing/ready.d.ts.map +1 -0
- package/dist/testing/reaper.d.ts +4 -0
- package/dist/testing/reaper.d.ts.map +1 -0
- package/dist/testing/seaweedfs.d.ts +64 -0
- package/dist/testing/seaweedfs.d.ts.map +1 -0
- package/dist/testing/test-s3.d.ts +35 -0
- package/dist/testing/test-s3.d.ts.map +1 -0
- package/dist/testing/weed.d.ts +17 -0
- package/dist/testing/weed.d.ts.map +1 -0
- package/docs/README.md +17 -0
- package/docs/guide/buckets.md +220 -0
- package/docs/guide/presigned-urls.md +403 -0
- package/docs/guide/reads.md +417 -0
- package/docs/guide/testing.md +151 -0
- package/docs/guide/writes.md +374 -0
- package/docs/roadmap.md +116 -0
- package/docs/troubleshooting.md +982 -0
- package/package.json +61 -5
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
# Writing
|
|
2
|
+
|
|
3
|
+
Storing an object, saying what it is and how it should be served back, and
|
|
4
|
+
getting a refusal **before** anything goes over the wire.
|
|
5
|
+
|
|
6
|
+
## The smallest thing that works
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { bindBucket, defineBucket } from '@softistx/s3';
|
|
10
|
+
|
|
11
|
+
const avatars = defineBucket({
|
|
12
|
+
bucket: 'avatars',
|
|
13
|
+
key: (p: { userId: string }) => `${p.userId}.png`,
|
|
14
|
+
contentType: ['image/png', 'image/jpeg'],
|
|
15
|
+
maxSize: 2 * 1024 * 1024,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const store = bindBucket(avatars);
|
|
19
|
+
|
|
20
|
+
await store.put({ userId: 'u1' }, png, { type: 'image/png' });
|
|
21
|
+
await store.delete({ userId: 'u1' });
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Describing the bucket is [Buckets](buckets.md). `put` resolves when the
|
|
25
|
+
object is stored, and gives back nothing: S3 has nothing to say beyond that.
|
|
26
|
+
|
|
27
|
+
## The signatures
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import type { S3Client, S3Options } from 'bun';
|
|
31
|
+
|
|
32
|
+
/** A string, bytes, a Blob, a Bun.file, a stream, a Response… */
|
|
33
|
+
type PutBody = Parameters<S3Client['write']>[1];
|
|
34
|
+
|
|
35
|
+
type PutOptions = Pick<
|
|
36
|
+
S3Options,
|
|
37
|
+
'type' | 'acl' | 'storageClass' | 'contentDisposition' | 'contentEncoding'
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
put(
|
|
41
|
+
params: P,
|
|
42
|
+
body: PutBody | ReadableStream<Uint8Array>,
|
|
43
|
+
options?: PutOptions & { timeoutMs?: number | undefined },
|
|
44
|
+
): Promise<void>;
|
|
45
|
+
// a stream with its length: sent as it is read, never held whole
|
|
46
|
+
put(
|
|
47
|
+
params: P,
|
|
48
|
+
body: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>,
|
|
49
|
+
options: PutOptions & { size: number; timeoutMs?: number | undefined },
|
|
50
|
+
): Promise<void>;
|
|
51
|
+
delete(params: P, options?: { timeoutMs?: number | undefined }): Promise<void>;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## What a body may be
|
|
55
|
+
|
|
56
|
+
Anything Bun's own `write` takes:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
await store.put({ userId: 'u1' }, 'a,b\n1,2\n', { type: 'text/csv' });
|
|
60
|
+
await store.put({ userId: 'u1' }, new Uint8Array([1, 2, 3]), { type: 'image/png' });
|
|
61
|
+
await store.put({ userId: 'u1' }, Bun.file('ada.png')); // the type comes from the file
|
|
62
|
+
await store.put({ userId: 'u1' }, await fetch(url)); // only without `maxSize`
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
A `Blob` — `Bun.file` included — carries its own content type, so a write
|
|
66
|
+
that names none is still checked against one. A string is measured in
|
|
67
|
+
**bytes**, not characters.
|
|
68
|
+
|
|
69
|
+
## What a write may say about the object
|
|
70
|
+
|
|
71
|
+
Every option here describes **the object being stored**. A write says nothing
|
|
72
|
+
about where it goes or how it gets there: the bucket, the endpoint, the
|
|
73
|
+
region and the credentials belong to the bound bucket.
|
|
74
|
+
|
|
75
|
+
| Option | Type | Default | Effect |
|
|
76
|
+
| --- | --- | --- | --- |
|
|
77
|
+
| `type` | `string` | the body's own, for a `Blob` | the content type. It is the one the guard checks **and** the one the service receives — they can never be two different answers |
|
|
78
|
+
| `acl` | `'private' \| 'public-read' \| 'public-read-write' \| 'aws-exec-read' \| 'authenticated-read' \| 'bucket-owner-read' \| 'bucket-owner-full-control' \| 'log-delivery-write'` | the client's, else the service's | who may read the object, where the service implements ACLs |
|
|
79
|
+
| `storageClass` | `'STANDARD' \| 'STANDARD_IA' \| 'ONEZONE_IA' \| 'INTELLIGENT_TIERING' \| 'GLACIER' \| 'GLACIER_IR' \| 'DEEP_ARCHIVE' \| 'REDUCED_REDUNDANCY' \| 'EXPRESS_ONEZONE' \| 'OUTPOSTS' \| 'SNOW'` | the client's, else `STANDARD` | what it costs to keep it; comes back as `x-amz-storage-class` |
|
|
80
|
+
| `contentDisposition` | `string` | none | how a reader should present it; comes back on a `GET`. A non-empty string of printable ASCII, no line break or control character (a non-ASCII filename is `filename*=UTF-8''…`), else `WRONG_OPTION` |
|
|
81
|
+
| `contentEncoding` | `string` | none | how the body is encoded; comes back on a `GET` |
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
await store.put({ userId: 'u1' }, png, {
|
|
85
|
+
type: 'image/png',
|
|
86
|
+
contentDisposition: 'attachment; filename="ada.png"',
|
|
87
|
+
contentEncoding: 'identity',
|
|
88
|
+
storageClass: 'STANDARD_IA',
|
|
89
|
+
acl: 'public-read',
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Measured against a real service: `contentDisposition` and `contentEncoding`
|
|
94
|
+
come back as the headers of the same name on a read, and `storageClass` as
|
|
95
|
+
`x-amz-storage-class`.
|
|
96
|
+
|
|
97
|
+
These are Bun's own names, picked out of its `S3Options`, so a Bun release
|
|
98
|
+
that changes one is a compile error here rather than a silent drift. Given
|
|
99
|
+
both here and to `bindBucket`, the one on the write wins: it is the last
|
|
100
|
+
thing handed to the client.
|
|
101
|
+
|
|
102
|
+
### An option's *value* is checked here too
|
|
103
|
+
|
|
104
|
+
`acl` and `storageClass` are unions, and the values listed above are the only
|
|
105
|
+
ones the service takes. A bag off a request body never met those types, so
|
|
106
|
+
the value is checked at run time as well — before anything is sent, with the
|
|
107
|
+
content type and the size, and as the same `S3Error`:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { S3Error, type PutOptions } from '@softistx/s3';
|
|
111
|
+
|
|
112
|
+
const bad = { type: 'text/plain', storageClass: 'CHEAP' } as unknown as PutOptions;
|
|
113
|
+
|
|
114
|
+
const error = (await store
|
|
115
|
+
.put({ userId: 'u1' }, 'a,b\n', bad)
|
|
116
|
+
.catch((reason: unknown) => reason)) as S3Error;
|
|
117
|
+
|
|
118
|
+
error instanceof S3Error; // true
|
|
119
|
+
error.code; // 'WRONG_OPTION'
|
|
120
|
+
error.message; // 'storageClass must be one of STANDARD, DEEP_ARCHIVE, …; got another string (put on "avatars")'
|
|
121
|
+
error.key; // 'u1.png' — the key, never the body
|
|
122
|
+
// Nothing was stored: `await store.exists({ userId: 'u1' })` is still false.
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Bun checks both values as well, and refuses with a plain `TypeError`
|
|
126
|
+
(measured on bun 1.4.2). That left one `put` with two classes of refusal —
|
|
127
|
+
an `S3Error` with a code for the content type and the size, a `TypeError`
|
|
128
|
+
with only a sentence for these two. Checking here means one `put` has one
|
|
129
|
+
class of refusal, with a code to switch on. `contentDisposition` is held to
|
|
130
|
+
one printable-ASCII header value (a line break would split the header), and `contentEncoding` stays a free
|
|
131
|
+
string, never refused by this package or by Bun.
|
|
132
|
+
|
|
133
|
+
The same allowlist holds `acl` on a
|
|
134
|
+
[presigned URL](presigned-urls.md#options), so a wrong value is the same
|
|
135
|
+
`WRONG_OPTION` whichever call a consumer reached for.
|
|
136
|
+
|
|
137
|
+
### A write cannot change where it goes
|
|
138
|
+
|
|
139
|
+
`PutOptions` carries no `bucket`, `endpoint`, `region` or credential, and the
|
|
140
|
+
run time forwards only the five keys above — a key that is not in that list
|
|
141
|
+
is dropped, never sent.
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
// A bag off a request body never met the types.
|
|
145
|
+
const smuggled = {
|
|
146
|
+
type: 'text/plain',
|
|
147
|
+
bucket: 'somewhere-else',
|
|
148
|
+
accessKeyId: 'someone-else',
|
|
149
|
+
} as PutOptions;
|
|
150
|
+
|
|
151
|
+
await store.put({ userId: 'u1' }, 'x', smuggled);
|
|
152
|
+
// Stored in `avatars`, with the credentials the bucket was bound with.
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Measured before that filter existed, spreading the caller's options straight
|
|
156
|
+
through let a `bucket` key store the object in another bucket — and report
|
|
157
|
+
success.
|
|
158
|
+
|
|
159
|
+
### A `put` is one PUT
|
|
160
|
+
|
|
161
|
+
`partSize`, `queueSize` and `retry` are not `PutOptions`: they belong to
|
|
162
|
+
[`bindBucket`](buckets.md), where they are the client's own. Measured on bun
|
|
163
|
+
1.4.2 against a 12 MiB body, a `put` with `partSize` set and one without come
|
|
164
|
+
back with the **same** ETag, and neither carries the `-<parts>` suffix a
|
|
165
|
+
multipart upload leaves. For a body that wants parts, use
|
|
166
|
+
`store.file(params).writer()`.
|
|
167
|
+
|
|
168
|
+
## The guards
|
|
169
|
+
|
|
170
|
+
`contentType` and `maxSize` on the definition are checked in `put`, before
|
|
171
|
+
the request goes out. A refused body is never sent.
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { S3Error } from '@softistx/s3';
|
|
175
|
+
|
|
176
|
+
try {
|
|
177
|
+
await store.put({ userId: 'u1' }, pdf, { type: 'application/pdf' });
|
|
178
|
+
} catch (error) {
|
|
179
|
+
if (error instanceof S3Error) {
|
|
180
|
+
error.code; // 'WRONG_TYPE'
|
|
181
|
+
error.key; // 'u1.png' — the key, never the body
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
| `S3ErrorCode` | When |
|
|
187
|
+
| --- | --- |
|
|
188
|
+
| `WRONG_TYPE` | the type is not one this bucket accepts — or the write named none and the body carries none, while the bucket names some |
|
|
189
|
+
| `TOO_LARGE` | the body is bigger than `maxSize`. The limit is inclusive: with `maxSize: 1024`, 1024 bytes passes and 1025 does not |
|
|
190
|
+
| `UNMEASURABLE` | `maxSize` is set and the body's size cannot be known before sending |
|
|
191
|
+
| `WRONG_OPTION` | the write's `acl` or `storageClass` is not a value the service accepts, or its `size` is not a whole number of bytes, or goes with a string, bytes or a `Blob`, or an `AsyncIterable` has none, or its `contentDisposition` is not a non-empty string of printable ASCII — see [above](#an-options-value-is-checked-here-too) — or a `timeoutMs` that is not a whole number of milliseconds, 1 or more |
|
|
192
|
+
| `SIZE_MISMATCH` | a streamed `put` sent a different number of bytes than its `size`, short or long. The upload was aborted and no object is stored. Raised after the request went out, like `TIMEOUT` |
|
|
193
|
+
| `SOURCE_FAILED` | a streamed `put`'s source threw part-way (a client that disconnected, a file that vanished) or gave a chunk that is not bytes. The upload was aborted, no object is stored, and the source's error is the `cause`. Raised after the request went out |
|
|
194
|
+
| `TIMEOUT` | `put` or `delete` with a `timeoutMs` (its own or the bucket's) got no answer in time. The request was sent and aborted, so the object may be stored or gone all the same; see [Deadlines](reads.md#deadlines) |
|
|
195
|
+
|
|
196
|
+
A content type is compared on its **essence**: parameters and case are
|
|
197
|
+
ignored, and nothing else is. A bucket that accepts `text/csv` accepts
|
|
198
|
+
`text/csv;charset=utf-8` and `TEXT/CSV`, because that is what real bodies
|
|
199
|
+
carry — measured, `Bun.file` labels `.txt` as `text/plain;charset=utf-8` and
|
|
200
|
+
`.csv` as the bare `text/csv`.
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const reports = defineBucket({
|
|
204
|
+
bucket: 'reports',
|
|
205
|
+
key: (id: string) => `${id}.csv`,
|
|
206
|
+
contentType: 'text/csv',
|
|
207
|
+
});
|
|
208
|
+
const csv = bindBucket(reports);
|
|
209
|
+
|
|
210
|
+
await csv.put('q1', Bun.file('q1.csv')); // stored
|
|
211
|
+
await csv.put('q1', Bun.file('note.txt')); // S3Error WRONG_TYPE, nothing sent
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`UNMEASURABLE` is the one that surprises: with `maxSize` set, a `Response`, a
|
|
215
|
+
`Request`, a stream or another `S3File` is **refused, not streamed** —
|
|
216
|
+
nothing can check a length it has not read, and an `S3File` reports its size
|
|
217
|
+
as `NaN` until the service has been asked. Read it into memory first, leave
|
|
218
|
+
`maxSize` out and let the service refuse an oversized body, or — for a stream —
|
|
219
|
+
[declare its `size`](#streaming-a-body-with-a-declared-size).
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
const answer = await fetch(url);
|
|
223
|
+
const body = new Uint8Array(await answer.arrayBuffer()); // now it has a length
|
|
224
|
+
await store.put({ userId: 'u1' }, body, { type: 'image/png' });
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
The guards are `put`'s, not the bucket's: `store.file(params).writer()`,
|
|
228
|
+
`store.client` and anyone holding a [presigned PUT](presigned-urls.md) write
|
|
229
|
+
whatever they are given. For a browser upload that must respect the size and
|
|
230
|
+
the type, hand out a
|
|
231
|
+
[presigned POST](presigned-urls.md#a-presigned-post-the-service-holds-the-size-and-the-type)
|
|
232
|
+
instead: its policy carries `maxSize` and `contentType` to the service, which
|
|
233
|
+
enforces them.
|
|
234
|
+
|
|
235
|
+
## Streaming a body with a declared size
|
|
236
|
+
|
|
237
|
+
A request body, a file read off a disk or a generator can be stored **without
|
|
238
|
+
holding it whole**: say how long it is, and it goes out as it is read.
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
const length = request.headers.get('content-length');
|
|
242
|
+
if (length === null) return new Response('Length required', { status: 411 });
|
|
243
|
+
|
|
244
|
+
await store.put({ userId: 'u1' }, request.body, {
|
|
245
|
+
type: 'image/png',
|
|
246
|
+
size: Number(length),
|
|
247
|
+
timeoutMs: 120_000,
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
`body` is a `ReadableStream` or an `AsyncIterable` of `Uint8Array`, and `size`
|
|
252
|
+
a whole number of bytes, 0 or more. It is sent as the object's
|
|
253
|
+
`Content-Length`, so S3 gets the length it needs without a chunked PUT.
|
|
254
|
+
|
|
255
|
+
- **`size` is checked against the bucket's `maxSize` before anything is
|
|
256
|
+
sent**: a bigger one is `TOO_LARGE`, and the stream is not even read. A
|
|
257
|
+
bucket with a `maxSize` therefore takes a stream that declares its size —
|
|
258
|
+
that is the way out of `UNMEASURABLE`.
|
|
259
|
+
- **The bytes actually sent are counted.** A stream that ends short of `size`
|
|
260
|
+
or has more to give is `SIZE_MISMATCH`: the upload is aborted, the source is
|
|
261
|
+
cancelled, and no object is stored (S3 does not keep a body shorter than its
|
|
262
|
+
`Content-Length`). The last chunk is held back until the source has said it
|
|
263
|
+
is done, so a body that is too long, even by one late byte, is never
|
|
264
|
+
completed.
|
|
265
|
+
- **A source that fails is `SOURCE_FAILED`**, with its own error as the
|
|
266
|
+
`cause`: a client that disconnects mid-upload, a file that vanishes, a
|
|
267
|
+
chunk that is not bytes. Nothing is stored. It is not a connection error
|
|
268
|
+
of the service, and `isBucketUnavailable` is `false` for it.
|
|
269
|
+
- **`timeoutMs` is an idle deadline here.** It bounds each wait: for the
|
|
270
|
+
source's next chunk, for the service to take it, and for the service's answer
|
|
271
|
+
after the last byte, and starts over after each. A slow upload that keeps
|
|
272
|
+
moving is never cut, however long it takes; a stall is `TIMEOUT`, the source
|
|
273
|
+
is cancelled and nothing is stored. **The bucket's default `timeoutMs` does
|
|
274
|
+
not apply** to a streamed put (it is sized for whole requests): only the
|
|
275
|
+
call's own `timeoutMs` does. With none, there is no deadline of its own.
|
|
276
|
+
- **Memory stays at a chunk or two**, whatever the length.
|
|
277
|
+
- `size` with a string, bytes or a `Blob` (they know their own length), a
|
|
278
|
+
`size` that is not a whole number, and an `AsyncIterable` with no `size`
|
|
279
|
+
are `WRONG_OPTION`, and the types refuse them too. A `ReadableStream` with
|
|
280
|
+
no `size` still compiles and is read into memory under a deadline, as
|
|
281
|
+
before.
|
|
282
|
+
- It is one PUT, like every `put` here: S3's 5 GiB ceiling holds.
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
import { S3Error } from '@softistx/s3';
|
|
286
|
+
|
|
287
|
+
try {
|
|
288
|
+
await store.put({ userId }, stream, { type, size });
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (error instanceof S3Error && error.code === 'SIZE_MISMATCH') {
|
|
291
|
+
return c.json({ error: 'The body is not the length it announced' }, 400);
|
|
292
|
+
}
|
|
293
|
+
if (error instanceof S3Error && error.code === 'SOURCE_FAILED') {
|
|
294
|
+
return c.json({ error: 'The upload was interrupted' }, 400);
|
|
295
|
+
}
|
|
296
|
+
if (error instanceof S3Error && error.code === 'TOO_LARGE') {
|
|
297
|
+
return c.json({ error: 'That file is too big' }, 413);
|
|
298
|
+
}
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
## Deleting
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
await store.delete({ userId: 'u1' });
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
S3 does not say whether anything was there, and nor does this. Call `exists`
|
|
310
|
+
first when it matters — see [Reading](reads.md).
|
|
311
|
+
|
|
312
|
+
`delete` takes a deadline: `store.delete(params, { timeoutMs: 5000 })` rejects
|
|
313
|
+
with `S3Error` `TIMEOUT` when the service does not answer, and aborts the
|
|
314
|
+
request. The request was sent, so the object may be gone all the same. See
|
|
315
|
+
[Deadlines](reads.md#deadlines).
|
|
316
|
+
|
|
317
|
+
`put` takes one too, in its options beside `type`: `store.put(params, body, {
|
|
318
|
+
type, timeoutMs: 120_000 })`. It covers the **whole upload**, so a bucket's
|
|
319
|
+
default sized for reads can cut a large `put` — give that one its own. Under a
|
|
320
|
+
deadline a stream given **without** a `size` is read into memory first; give
|
|
321
|
+
it a `size` to [stream it](#streaming-a-body-with-a-declared-size) instead. See
|
|
322
|
+
[`list` and `put` under a deadline](reads.md#list-and-put-under-a-deadline).
|
|
323
|
+
|
|
324
|
+
## A real one: an upload route
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
import { Hono } from 'hono';
|
|
328
|
+
import { bindBucket, defineBucket, S3Error } from '@softistx/s3';
|
|
329
|
+
|
|
330
|
+
const avatars = defineBucket({
|
|
331
|
+
bucket: 'avatars',
|
|
332
|
+
key: (p: { userId: string }) => `${p.userId}.png`,
|
|
333
|
+
contentType: ['image/png', 'image/jpeg'],
|
|
334
|
+
maxSize: 2 * 1024 * 1024,
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
const store = bindBucket(avatars);
|
|
338
|
+
const app = new Hono();
|
|
339
|
+
|
|
340
|
+
app.put('/users/:id/avatar', async (c) => {
|
|
341
|
+
const userId = c.req.param('id');
|
|
342
|
+
const body = await c.req.blob(); // a Blob: it carries its own type
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
await store.put({ userId }, body, {
|
|
346
|
+
contentDisposition: `inline; filename="${userId}.png"`,
|
|
347
|
+
});
|
|
348
|
+
} catch (error) {
|
|
349
|
+
if (error instanceof S3Error && error.code === 'WRONG_TYPE') {
|
|
350
|
+
return c.json({ error: 'Send a PNG or a JPEG' }, 415);
|
|
351
|
+
}
|
|
352
|
+
if (error instanceof S3Error && error.code === 'TOO_LARGE') {
|
|
353
|
+
return c.json({ error: 'That file is too big' }, 413);
|
|
354
|
+
}
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return c.json({ key: store.keyFor({ userId }) }, 201);
|
|
359
|
+
});
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
Every `S3Error` but `TIMEOUT`, `SIZE_MISMATCH` and `SOURCE_FAILED` is thrown
|
|
363
|
+
before the request goes out, so a
|
|
364
|
+
413 costs no bandwidth. Bun names **its own** S3 failures `S3Error` too, with S3's codes:
|
|
365
|
+
discriminate with `instanceof S3Error` on this package's class, never with
|
|
366
|
+
`error.name`.
|
|
367
|
+
|
|
368
|
+
## Next
|
|
369
|
+
|
|
370
|
+
- [Reading](reads.md) — reading back what was written.
|
|
371
|
+
- [Presigned URLs](presigned-urls.md) — letting a browser upload directly:
|
|
372
|
+
what a presigned PUT does *not* constrain, and the presigned POST that
|
|
373
|
+
does.
|
|
374
|
+
- [Troubleshooting](../troubleshooting.md) — each error with its fix.
|
package/docs/roadmap.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# Roadmap
|
|
2
|
+
|
|
3
|
+
Where `@softistx/s3` is going. A direction, not a commitment: the version an item
|
|
4
|
+
shipped in is the only number on this page.
|
|
5
|
+
|
|
6
|
+
## Now
|
|
7
|
+
|
|
8
|
+
_Nothing in progress._
|
|
9
|
+
|
|
10
|
+
## Next
|
|
11
|
+
|
|
12
|
+
_Nothing queued._
|
|
13
|
+
|
|
14
|
+
## Later
|
|
15
|
+
|
|
16
|
+
- **Copy and move** — once Bun's own `S3Client` has them. Until then
|
|
17
|
+
`store.client` is where they live.
|
|
18
|
+
- **A caller's `signal`** — an `AbortSignal` of the caller's own, beside or
|
|
19
|
+
instead of `timeoutMs`.
|
|
20
|
+
|
|
21
|
+
## Not planned
|
|
22
|
+
|
|
23
|
+
- **Running on Node** — the client is Bun's own `S3Client`, which is why this
|
|
24
|
+
package installs no AWS SDK at all. There is nothing to swap for Node, and a
|
|
25
|
+
build for it is not coming: it needs Bun 1.4 or later.
|
|
26
|
+
- **Multipart upload** — a body goes out in one `put`. For anything bigger,
|
|
27
|
+
`store.file(params)` hands back Bun's own file handle and its `writer()`.
|
|
28
|
+
- **Bucket administration** — creating, deleting or configuring a bucket is
|
|
29
|
+
not this package's, and Bun's client has no `createBucket` either.
|
|
30
|
+
- **Retries or a cache of its own** — Bun's client already retries (three
|
|
31
|
+
attempts by default, and `retry` is passed through on `bindBucket`), and a
|
|
32
|
+
failed request comes back with S3's own error, as `S3Error`.
|
|
33
|
+
- **Upload tuning on a `put`** — a `put` is a single PUT: measured against a
|
|
34
|
+
12 MiB body, `partSize` changes nothing, right down to the ETag carrying no
|
|
35
|
+
`-<parts>` suffix. `PutOptions` describes the object, not the transfer.
|
|
36
|
+
- **An option that says where a write goes** — `bucket`, `endpoint`, `region`
|
|
37
|
+
and the credentials belong to the bound bucket, and are refused by the types
|
|
38
|
+
and again at run time. Measured: passing them through let a bag store the
|
|
39
|
+
object in a different bucket and report success, and redirected a signed URL
|
|
40
|
+
the same way.
|
|
41
|
+
|
|
42
|
+
## Shipped
|
|
43
|
+
|
|
44
|
+
- **Streamed read** — `open(params, { range?, timeoutMs })` hands back the
|
|
45
|
+
body as a stream with its size, type, ETag, modification date and
|
|
46
|
+
`Content-Range`, a deadline that covers the headers only, and a typed
|
|
47
|
+
refusal for an unsatisfiable range.
|
|
48
|
+
- **A streamed `put` with a declared size** — `put(params, stream, { size })`
|
|
49
|
+
sends a `ReadableStream` or an `AsyncIterable` as it is read, never held
|
|
50
|
+
whole, under a deadline or not. `size` is checked against `maxSize` before
|
|
51
|
+
sending and against the bytes actually sent (`SIZE_MISMATCH`,
|
|
52
|
+
`SOURCE_FAILED`, nothing stored on a short or long body).
|
|
53
|
+
- **A public origin for presigned URLs** — `bindBucket(…, { publicEndpoint })`
|
|
54
|
+
signs `presignGet`, `presignPut` and `presignPost` for the origin browsers
|
|
55
|
+
reach, while every other call keeps using `endpoint`, so a server behind a
|
|
56
|
+
proxy needs no second bound bucket — 0.9.0.
|
|
57
|
+
- **`isBucketUnavailable(error)`** — tells a failure that means the bucket
|
|
58
|
+
cannot be used (network codes, `TIMEOUT`, deployment-level S3 codes) from the
|
|
59
|
+
caller's own refusals, so a route can answer 503 — 0.7.0.
|
|
60
|
+
- **`parseKey` on `defineBucket`** — the way back from a stored object key to
|
|
61
|
+
the params it was built from, `undefined` for a key that is not the bucket's,
|
|
62
|
+
so a key from `list` maps back to its params without parsing it by hand —
|
|
63
|
+
0.7.0.
|
|
64
|
+
- **A deadline on the test S3 server's `reset` and `createBucket`** —
|
|
65
|
+
`@softistx/s3/testing` takes `timeoutMs` on both, so a stuck server fails a test
|
|
66
|
+
hook with `S3Error` `TIMEOUT` that names the bucket — 0.8.0.
|
|
67
|
+
- **A deadline on `put` and `list`** — `timeoutMs` on both, and the bucket's
|
|
68
|
+
default now bounds them too: no answer in time rejects with `S3Error`
|
|
69
|
+
`TIMEOUT` and aborts the request. A listing signs its own ListObjectsV2 GET
|
|
70
|
+
and a put goes over `fetch` on a URL Bun signs; under a deadline a stream is
|
|
71
|
+
read into memory first, and the deadline covers a whole upload — 0.6.0.
|
|
72
|
+
- **A download with a filename** — `presignGet(params, { contentDisposition })`
|
|
73
|
+
signs `response-content-disposition`, so a link saves the object under a
|
|
74
|
+
name you choose; `presignPost` can pin a `Content-Disposition` field in its
|
|
75
|
+
policy. `put`'s `contentDisposition` is now held to one printable header
|
|
76
|
+
value too — 0.5.0.
|
|
77
|
+
- **A deadline on the calls that wait on the service** — `timeoutMs` on
|
|
78
|
+
`stat`, `exists`, `bytes`, `text` and `delete`, or as the bucket's default on
|
|
79
|
+
`bindBucket`: no answer in time rejects with `S3Error` `TIMEOUT` and aborts
|
|
80
|
+
the request, which Bun's own calls (360 s) cannot do — 0.5.0.
|
|
81
|
+
- **`@softistx/s3/testing`** — `startTestS3()` starts a real S3 API (SeaweedFS) on
|
|
82
|
+
free ports for a suite, with `createBucket`, `reset` and `stop`, and the
|
|
83
|
+
options `bindBucket` takes. The binary is checked against a pinned SHA-256
|
|
84
|
+
before it runs. See the [testing guide](guide/testing.md) — 0.5.0.
|
|
85
|
+
- **Uploads the service holds to a size and a type** — `presignPost` signs an
|
|
86
|
+
S3 POST policy, the form a browser posts straight to the bucket: the key
|
|
87
|
+
fixed, the size held to a `content-length-range` that defaults to the
|
|
88
|
+
bucket's `maxSize`, and the content type fixed or held to a prefix. The
|
|
89
|
+
**service** refuses an upload that breaks it, which a presigned PUT cannot
|
|
90
|
+
do. With it, every refusal reports a value by its **shape** — `another
|
|
91
|
+
string`, `a number above that`, `the type given` — and never quotes it, since
|
|
92
|
+
it can come off a request body; and every refusal names the call and the
|
|
93
|
+
bucket it was on, such as `put on "avatars"` — 0.4.0.
|
|
94
|
+
- **An option's own value is refused before anything is sent or signed** —
|
|
95
|
+
`code: 'WRONG_OPTION'` for an `acl` or a `storageClass` a write names, for
|
|
96
|
+
an `acl` on a presigned URL, and for an `expiresIn` outside the seven days
|
|
97
|
+
S3 itself allows. Every refusal of a write or a signed URL is one class with
|
|
98
|
+
a code to switch on, rather than an `S3Error` for the content type and the
|
|
99
|
+
size and the client's own `TypeError` for the rest — 0.3.0.
|
|
100
|
+
- **Documentation that travels with the package** — a guide page for the
|
|
101
|
+
bucket definition, writes, reads and presigned URLs, a troubleshooting page
|
|
102
|
+
whose headings are the exact error text, and this roadmap, installed in
|
|
103
|
+
`docs/` rather than left on GitHub — 0.2.1.
|
|
104
|
+
- **What a write may say about the object** — `contentDisposition`,
|
|
105
|
+
`contentEncoding`, `acl` and `storageClass` on `put`, with Bun's own names;
|
|
106
|
+
an option naming another bucket, endpoint, region or credential is refused,
|
|
107
|
+
on `put` and on both `presign` calls — 0.2.0.
|
|
108
|
+
- **First release** — `defineBucket` naming the bucket, the key-building
|
|
109
|
+
function, the content types it accepts and the biggest body it takes, and
|
|
110
|
+
`bindBucket` giving `put`, `bytes`, `text`, `exists`, `stat`, `delete`, a
|
|
111
|
+
cursor `list` and `presignGet` / `presignPut`; the content type and the size
|
|
112
|
+
are checked before the request goes out, and its one error is `S3Error` —
|
|
113
|
+
0.1.0.
|
|
114
|
+
|
|
115
|
+
Everything released is in [`CHANGELOG.md`](https://github.com/softistx/data/blob/develop/packages/s3/CHANGELOG.md) — it is not in
|
|
116
|
+
the published package, only in the repository.
|