@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,417 @@
|
|
|
1
|
+
# Reading
|
|
2
|
+
|
|
3
|
+
Getting an object back — its bytes, its text, whether it is there, what the
|
|
4
|
+
service knows about it — and walking a bucket page by page.
|
|
5
|
+
|
|
6
|
+
## The smallest thing that works
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { bindBucket, defineBucket } from '@softistx/s3';
|
|
10
|
+
|
|
11
|
+
const reports = defineBucket({
|
|
12
|
+
bucket: 'reports',
|
|
13
|
+
key: (id: string) => `${id}.csv`,
|
|
14
|
+
contentType: 'text/csv',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const store = bindBucket(reports);
|
|
18
|
+
|
|
19
|
+
const csv = await store.text('q1'); // string, or undefined
|
|
20
|
+
if (csv === undefined) throw new Error('no such report'); // nothing stored
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Describing and binding a bucket is [Buckets](buckets.md).
|
|
24
|
+
|
|
25
|
+
## The signatures
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import type { S3File, S3Stats } from 'bun';
|
|
29
|
+
|
|
30
|
+
interface CallOptions {
|
|
31
|
+
timeoutMs?: number | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface ByteRange {
|
|
35
|
+
start: number; // first byte read
|
|
36
|
+
end: number; // first byte NOT read
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
bytes(params: P, options?: CallOptions): Promise<Uint8Array | undefined>;
|
|
40
|
+
slice(
|
|
41
|
+
params: P,
|
|
42
|
+
range: ByteRange,
|
|
43
|
+
options?: CallOptions,
|
|
44
|
+
): Promise<ReadableStream<Uint8Array> | undefined>;
|
|
45
|
+
open(
|
|
46
|
+
params: P,
|
|
47
|
+
options?: { range?: OpenRange; timeoutMs?: number },
|
|
48
|
+
): Promise<OpenedObject | undefined>;
|
|
49
|
+
text(params: P, options?: CallOptions): Promise<string | undefined>;
|
|
50
|
+
stat(params: P, options?: CallOptions): Promise<S3Stats | undefined>;
|
|
51
|
+
exists(params: P, options?: CallOptions): Promise<boolean>;
|
|
52
|
+
file(params: P): S3File;
|
|
53
|
+
|
|
54
|
+
list(
|
|
55
|
+
options?: {
|
|
56
|
+
prefix?: string;
|
|
57
|
+
limit?: number;
|
|
58
|
+
cursor?: string | null;
|
|
59
|
+
startAfter?: string;
|
|
60
|
+
} & CallOptions,
|
|
61
|
+
): Promise<ObjectPage>;
|
|
62
|
+
|
|
63
|
+
interface ObjectPage {
|
|
64
|
+
items: StoredObject[];
|
|
65
|
+
nextCursor: string | null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
interface StoredObject {
|
|
69
|
+
key: string;
|
|
70
|
+
size: number | undefined;
|
|
71
|
+
lastModified: Date | undefined;
|
|
72
|
+
eTag: string | undefined;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## A byte range with `slice`
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
// bytes 100..199: 100 bytes. `end` is exclusive, like Blob.slice.
|
|
80
|
+
const part = await store.slice('big', { start: 100, end: 200 }, { timeoutMs: 5000 });
|
|
81
|
+
if (part === undefined) throw new Error('no such object');
|
|
82
|
+
const bytes = new Uint8Array(await new Response(part).arrayBuffer());
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- **`end` is exclusive**: the stream holds exactly bytes `[start, end)`.
|
|
86
|
+
`start` and `end` are whole numbers with `0 <= start <= end`, else a
|
|
87
|
+
`WRONG_OPTION` before anything is sent.
|
|
88
|
+
- **Past the end**: an `end` beyond the object is cut at the object's last
|
|
89
|
+
byte; a `start` at or past its end gives an **empty stream**, as does
|
|
90
|
+
`start === end`. It never throws for that.
|
|
91
|
+
- **A missing object** is `undefined`, as for `bytes`.
|
|
92
|
+
- **With a deadline** (`timeoutMs`, or the bucket's) it goes over `fetch` on a
|
|
93
|
+
signed URL with an HTTP `Range` header, and a stalled service rejects with
|
|
94
|
+
`TIMEOUT` (`slice on "reports": no answer in 5000ms`). The range is read
|
|
95
|
+
**into memory** inside the deadline and the stream replays it, so keep
|
|
96
|
+
ranges modest. Without a deadline it is Bun's `file.slice(start, end)`,
|
|
97
|
+
streamed as it arrives.
|
|
98
|
+
|
|
99
|
+
## Streaming a read with `open`
|
|
100
|
+
|
|
101
|
+
`bytes` and `text` hold the whole object in memory, and `slice` under a
|
|
102
|
+
deadline holds its range. `open` holds nothing: it resolves as soon as the
|
|
103
|
+
service has sent its **headers**, and the body then reaches `stream` chunk by
|
|
104
|
+
chunk. It is what a route that serves an object to a browser wants.
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const opened = await store.open('q1', { timeoutMs: 5000 });
|
|
108
|
+
if (opened === undefined) return new Response('not found', { status: 404 });
|
|
109
|
+
|
|
110
|
+
return new Response(opened.stream, {
|
|
111
|
+
headers: {
|
|
112
|
+
'content-type': opened.contentType ?? 'application/octet-stream',
|
|
113
|
+
...(opened.contentLength === undefined
|
|
114
|
+
? {}
|
|
115
|
+
: { 'content-length': String(opened.contentLength) }),
|
|
116
|
+
...(opened.contentEncoding
|
|
117
|
+
? { 'content-encoding': opened.contentEncoding }
|
|
118
|
+
: {}),
|
|
119
|
+
...(opened.eTag ? { etag: opened.eTag } : {}),
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`OpenedObject` is `{ stream, size, contentLength, contentType,
|
|
125
|
+
contentEncoding, contentDisposition, cacheControl, eTag, lastModified,
|
|
126
|
+
contentRange }`: `size` is the whole object's size, and `contentLength` the
|
|
127
|
+
bytes `stream` carries (the same, without a range). Both are `undefined` when
|
|
128
|
+
the service did not send a length, never `NaN`. `eTag` is spelled as in
|
|
129
|
+
`StoredObject` (`stat`'s field is `etag`) and is **quoted, as the service sent
|
|
130
|
+
it**.
|
|
131
|
+
|
|
132
|
+
The bytes are **the ones stored**: an object put with `contentEncoding:
|
|
133
|
+
'gzip'` comes back gzipped, with `contentEncoding: 'gzip'` beside it, and a
|
|
134
|
+
`Range` counts those stored bytes. Pass the header on to the client and let it
|
|
135
|
+
decode; nothing here undoes it.
|
|
136
|
+
|
|
137
|
+
`open` goes through `fetch` on a URL Bun signs, like every call under a
|
|
138
|
+
deadline, so `bindBucket`'s `retry` does not apply to it: a failed `open` is
|
|
139
|
+
not retried.
|
|
140
|
+
|
|
141
|
+
### A range
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
await store.open('big', { range: { start: 100, end: 200 } }); // bytes [100, 200)
|
|
145
|
+
await store.open('big', { range: { start: 100 } }); // from 100 to the end
|
|
146
|
+
await store.open('big', { range: { suffix: 500 } }); // the last 500 bytes
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
`end` is **exclusive**, like `slice` and `Blob.slice`: translate an HTTP
|
|
150
|
+
`Range: bytes=100-199` to `{ start: 100, end: 200 }`. An `end` past the object
|
|
151
|
+
is cut at its end, as HTTP does, and a `suffix` longer than the object is the
|
|
152
|
+
whole object. Whatever was asked, the answer has 206 semantics:
|
|
153
|
+
`contentRange` is `bytes 100-199/<size>`, and `contentLength` is 100. A service
|
|
154
|
+
that ignores `Range` and sends the whole object is cut here, so this holds on
|
|
155
|
+
any S3 API. Without a range, `contentRange` is `undefined`.
|
|
156
|
+
|
|
157
|
+
One case is cut differently: a service that ignores `Range` and also sends no
|
|
158
|
+
`Content-Length` (a chunked 200). A `{ start, end? }` range is still cut while
|
|
159
|
+
streaming, with `contentLength` set when `end` is given, but `size` and
|
|
160
|
+
`contentRange` are `undefined`. A `{ suffix }` range cannot be cut, since the
|
|
161
|
+
end of the object is not known: it throws `RANGE_NOT_SATISFIABLE` with no
|
|
162
|
+
`objectSize`. Ask for `{ start: size - n }` after a `stat` instead.
|
|
163
|
+
|
|
164
|
+
A range that starts at or past the end of the object (any range of an empty
|
|
165
|
+
one) throws an `S3Error` with code `RANGE_NOT_SATISFIABLE`, and
|
|
166
|
+
`error.objectSize` is the object's size when the service said it (absent
|
|
167
|
+
otherwise, and on every other error), for a 416 with `Content-Range: bytes */<size>`:
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
try {
|
|
171
|
+
const opened = await store.open('big', { range: { start: 9_999_999 } });
|
|
172
|
+
} catch (error) {
|
|
173
|
+
if (error instanceof S3Error && error.code === 'RANGE_NOT_SATISFIABLE') {
|
|
174
|
+
return new Response(null, {
|
|
175
|
+
status: 416,
|
|
176
|
+
headers:
|
|
177
|
+
error.objectSize === undefined
|
|
178
|
+
? {}
|
|
179
|
+
: { 'content-range': `bytes */${error.objectSize}` },
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
A range that is not whole numbers (`end <= start`, a negative, a `suffix` of
|
|
187
|
+
0) is a `WRONG_OPTION` before anything is sent (note `start < end` here, where `slice` allows `start === end`). A missing object is
|
|
188
|
+
`undefined`, with or without a range.
|
|
189
|
+
|
|
190
|
+
### The deadline stops at the headers
|
|
191
|
+
|
|
192
|
+
`timeoutMs` (or the bucket's) covers the request **up to the response
|
|
193
|
+
headers**: a service that never answers rejects with `TIMEOUT`
|
|
194
|
+
(`open on "reports": no answer in 5000ms`). Once they arrive the timer is
|
|
195
|
+
gone and the body streams with none, so a slow client is never cut off.
|
|
196
|
+
Cancelling the stream (`opened.stream.cancel()`, or a `Response` whose client
|
|
197
|
+
went away) aborts the request. Read the stream to its end or cancel it: an
|
|
198
|
+
unread body keeps its connection open.
|
|
199
|
+
|
|
200
|
+
## `undefined` means "no such object", and nothing else
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
await store.bytes('nobody'); // undefined
|
|
204
|
+
await store.slice('nobody', { start: 0, end: 10 }); // undefined
|
|
205
|
+
await store.open('nobody'); // undefined
|
|
206
|
+
await store.text('nobody'); // undefined
|
|
207
|
+
await store.stat('nobody'); // undefined
|
|
208
|
+
await store.exists('nobody'); // false
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Each read goes straight at the object and turns S3's own `NoSuchKey` into
|
|
212
|
+
`undefined`: one round trip, and no window in which an object deleted between
|
|
213
|
+
a check and a read turns a promised `undefined` into a throw. **Every other
|
|
214
|
+
failure comes back as the error it is** — a wrong secret, a refused request,
|
|
215
|
+
a service that is down. A missing object and a missing permission must not
|
|
216
|
+
read the same way.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const wrong = bindBucket(reports, { secretAccessKey: 'wrong-secret' });
|
|
220
|
+
await wrong.text('q1'); // throws Bun's own S3Error, not undefined
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## What the service knows
|
|
224
|
+
|
|
225
|
+
```ts
|
|
226
|
+
const found = await store.stat('q1');
|
|
227
|
+
if (found) {
|
|
228
|
+
found.size; // bytes
|
|
229
|
+
found.type; // 'text/csv'
|
|
230
|
+
found.lastModified;
|
|
231
|
+
found.etag; // note: lower case — that is Bun's field
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
`stat` is Bun's own `S3Stats`, which this package does not re-export; import
|
|
236
|
+
it from `bun` where you need to name it. A listing's `StoredObject` says
|
|
237
|
+
`eTag`, because that is what S3 calls it there.
|
|
238
|
+
|
|
239
|
+
## Deadlines
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
import { S3Error } from '@softistx/s3';
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
await store.stat('q1', { timeoutMs: 5000 });
|
|
246
|
+
} catch (error) {
|
|
247
|
+
if (error instanceof S3Error && error.code === 'TIMEOUT') {
|
|
248
|
+
// stat on "reports": no answer in 5000ms
|
|
249
|
+
}
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
`stat`, `exists`, `bytes`, `slice`, `text`, `delete`, `list` and `put` take
|
|
255
|
+
`{ timeoutMs }`: whole milliseconds, 1 or more. No answer in time rejects with an `S3Error` coded
|
|
256
|
+
`TIMEOUT`, and the request is aborted. Bind a default for the whole bucket
|
|
257
|
+
with `bindBucket(definition, { ...credentials, timeoutMs })`; a call's own
|
|
258
|
+
value wins. A bad per-call value is a `WRONG_OPTION`, before anything is sent;
|
|
259
|
+
a bad default on `bindBucket` is a bare `TypeError`, since it is wiring.
|
|
260
|
+
|
|
261
|
+
Why it is not just Bun's call: measured on Bun 1.4.3, Bun's S3 calls ignore an
|
|
262
|
+
`AbortSignal`, and its own limit is 360 s. So a call that has a deadline signs
|
|
263
|
+
a URL with Bun's client and sends it with `fetch`, which does abort — the
|
|
264
|
+
socket is closed. A call without one is Bun's, unchanged. The results are the
|
|
265
|
+
same either way: `undefined` or `false` for a missing object, the same
|
|
266
|
+
`S3Stats` fields, and a service failure comes back as Bun's own `Error`
|
|
267
|
+
(named `S3Error` too, but not this package's class) with S3's `code` (for a
|
|
268
|
+
`stat` or `exists`, `UnknownError`, as Bun's own HEAD does). A refused or
|
|
269
|
+
dropped connection, even in the middle of the body, is the same Bun error
|
|
270
|
+
given without a deadline (`ConnectionRefused` or `ConnectionClosed`,
|
|
271
|
+
`DNSResolveFailed` for a host that does not resolve, `path` the key; any other
|
|
272
|
+
failure nobody mapped is reported as `ConnectionClosed`) and never carries the
|
|
273
|
+
signed URL. The deadline path does not retry; Bun's client does, per `retry`. A redirect is
|
|
274
|
+
followed on both paths, to whatever `location` the service names: the deadline
|
|
275
|
+
path sends a plain request there, without the signed query, and Bun's sends no
|
|
276
|
+
`Authorization` either.
|
|
277
|
+
`TIMEOUT` is raised **after** the request was sent.
|
|
278
|
+
|
|
279
|
+
### `list` and `put` under a deadline
|
|
280
|
+
|
|
281
|
+
`list({ prefix, limit, cursor, startAfter, timeoutMs })` rejects with `list on "reports":
|
|
282
|
+
no answer in 5000ms`. Bun cannot presign a bucket-level request, so the
|
|
283
|
+
listing signs its own ListObjectsV2 GET (SigV4, in the query, with the
|
|
284
|
+
credentials Bun's signer holds) and sends it with `fetch`; the XML answer is
|
|
285
|
+
read into the same `ObjectPage`, field for field, as Bun's `list` gives
|
|
286
|
+
(measured against SeaweedFS on a prefix with a space, a `+`, a `/` and a
|
|
287
|
+
non-ASCII letter, and across pages).
|
|
288
|
+
|
|
289
|
+
`put(params, body, { type, timeoutMs })` goes over `fetch` on a URL Bun signs
|
|
290
|
+
for a PUT, with `acl` and `storageClass` signed into it and the type,
|
|
291
|
+
`contentDisposition` and `contentEncoding` sent as plain headers (SeaweedFS
|
|
292
|
+
4.47 stores them although they are not signed). Every guard (`type`, `maxSize`,
|
|
293
|
+
the options) still answers before anything is sent.
|
|
294
|
+
|
|
295
|
+
- **The deadline covers the whole upload** (not a streamed `put` with a
|
|
296
|
+
`size`: its own `timeoutMs` is an idle deadline, and the bucket's default
|
|
297
|
+
does not apply). A bucket default sized for reads
|
|
298
|
+
can cut a large `put`: give it its own `timeoutMs`. A timed-out `put` was
|
|
299
|
+
sent, and the object may be stored all the same.
|
|
300
|
+
- **A body that is not bytes is read into memory**, inside the deadline: a
|
|
301
|
+
string, an `ArrayBuffer`, a typed array and a `Blob` of known size go as
|
|
302
|
+
they are; a stream, a `Response` or an `S3File` is read whole first, since
|
|
303
|
+
S3 refuses a chunked PUT. A bucket with `maxSize` refuses those before. A
|
|
304
|
+
stream given a `size` is the exception: it is
|
|
305
|
+
[streamed](writes.md#streaming-a-body-with-a-declared-size), not read whole.
|
|
306
|
+
- **A put under a deadline is one PUT.** The bucket's `retry` does not apply
|
|
307
|
+
(the deadline path sends one `fetch`), there is no multipart, and S3's 5 GiB
|
|
308
|
+
ceiling on a single PUT holds.
|
|
309
|
+
|
|
310
|
+
## Big bodies, and everything else
|
|
311
|
+
|
|
312
|
+
```ts
|
|
313
|
+
const stream = store.file('q1').stream();
|
|
314
|
+
const head = await store.file('q1').slice(0, 1024).text();
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
`file(params)` is Bun's lazy `S3File`, keyed by the definition. Use it to
|
|
318
|
+
stream a body rather than hold it in memory, and to reach anything this
|
|
319
|
+
package does not wrap.
|
|
320
|
+
|
|
321
|
+
## Listing a bucket
|
|
322
|
+
|
|
323
|
+
```ts
|
|
324
|
+
let cursor: string | null = null;
|
|
325
|
+
|
|
326
|
+
do {
|
|
327
|
+
const page = await store.list({ prefix: 'q1/', limit: 100, cursor });
|
|
328
|
+
for (const object of page.items) console.log(object.key, object.size);
|
|
329
|
+
cursor = page.nextCursor;
|
|
330
|
+
} while (cursor !== null);
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
| Option | Type | Default | Effect |
|
|
334
|
+
| --- | --- | --- | --- |
|
|
335
|
+
| `prefix` | `string` | the whole bucket | only keys that start with it |
|
|
336
|
+
| `limit` | `number` | the service's | the **maximum** number of objects in a page, not a promise |
|
|
337
|
+
| `cursor` | `string \| null` | `null` | the `nextCursor` of the previous page |
|
|
338
|
+
|
|
339
|
+
`ObjectPage` is the same shape as the `CursorPage` of `@softistx/drizzle` and
|
|
340
|
+
`@softistx/mongo`, so a caller pages the same way everywhere. S3's
|
|
341
|
+
`continuationToken` is what `nextCursor` carries, and it is `null` on the
|
|
342
|
+
last page.
|
|
343
|
+
|
|
344
|
+
Page **until `nextCursor` is `null`**, never until a page is short: a listing
|
|
345
|
+
is eventually consistent on some services, and a page may come back with
|
|
346
|
+
fewer items than `limit` while there is still more to read. A prefix with
|
|
347
|
+
nothing under it gives an empty page rather than nothing:
|
|
348
|
+
|
|
349
|
+
```ts
|
|
350
|
+
await store.list({ prefix: 'nothing-here/' }); // { items: [], nextCursor: null }
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
`startAfter` lists only the keys after a key you already know (ListObjectsV2
|
|
354
|
+
`start-after`), for example to resume a sweep after the last key handled. It
|
|
355
|
+
goes through the same paths as the rest, so a `timeoutMs` bounds it too, and
|
|
356
|
+
`nextCursor` pages on from there:
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
const page = await store.list({
|
|
360
|
+
prefix: 'items/',
|
|
361
|
+
startAfter: 'items/0042',
|
|
362
|
+
timeoutMs: 5000,
|
|
363
|
+
});
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
`list` is the bucket's, not an object's, so it takes no key parameters — and
|
|
367
|
+
it does not count: S3 does not say how many objects there are, so there is no
|
|
368
|
+
`total` and no page number.
|
|
369
|
+
|
|
370
|
+
## A real one: a download route
|
|
371
|
+
|
|
372
|
+
```ts
|
|
373
|
+
import { Hono } from 'hono';
|
|
374
|
+
import { bindBucket, defineBucket } from '@softistx/s3';
|
|
375
|
+
|
|
376
|
+
const reports = defineBucket({
|
|
377
|
+
bucket: 'reports',
|
|
378
|
+
key: (id: string) => `${id}.csv`,
|
|
379
|
+
contentType: 'text/csv',
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
const store = bindBucket(reports);
|
|
383
|
+
const app = new Hono();
|
|
384
|
+
|
|
385
|
+
app.get('/reports/:id', async (c) => {
|
|
386
|
+
const id = c.req.param('id');
|
|
387
|
+
const found = await store.stat(id);
|
|
388
|
+
if (!found) return c.json({ error: 'No such report' }, 404);
|
|
389
|
+
|
|
390
|
+
return new Response(store.file(id).stream(), {
|
|
391
|
+
headers: {
|
|
392
|
+
'content-type': found.type,
|
|
393
|
+
'content-length': String(found.size),
|
|
394
|
+
'content-disposition': `attachment; filename="${id}.csv"`,
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
app.get('/reports', async (c) => {
|
|
400
|
+
const page = await store.list({
|
|
401
|
+
limit: 50,
|
|
402
|
+
cursor: c.req.query('cursor') ?? null,
|
|
403
|
+
});
|
|
404
|
+
return c.json(page);
|
|
405
|
+
});
|
|
406
|
+
```
|
|
407
|
+
|
|
408
|
+
`stat` is a HEAD, so this costs one round trip before the body is streamed —
|
|
409
|
+
and it is what turns a missing object into a 404 rather than a broken stream.
|
|
410
|
+
|
|
411
|
+
## Next
|
|
412
|
+
|
|
413
|
+
- [Writing](writes.md) — `put`, its options and the guards.
|
|
414
|
+
- [Presigned URLs](presigned-urls.md) — handing the download to the client
|
|
415
|
+
instead of streaming it through your server.
|
|
416
|
+
- [Troubleshooting](../troubleshooting.md) — a read that threw instead of
|
|
417
|
+
answering `undefined`.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# Testing against a real S3 API
|
|
2
|
+
|
|
3
|
+
`@softistx/s3/testing` starts SeaweedFS for a suite: its S3 gateway, on free
|
|
4
|
+
ports, with no Docker and nothing to `bun add`. Specs then run against a real
|
|
5
|
+
service — real signatures, real refusals, real listings — instead of a mock
|
|
6
|
+
that agrees with whatever you wrote.
|
|
7
|
+
|
|
8
|
+
It is a subpath: an application that never imports it never loads it, and the
|
|
9
|
+
root entry (`@softistx/s3`) has no trace of it.
|
|
10
|
+
|
|
11
|
+
## A whole spec
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { afterAll, beforeAll, beforeEach, expect, test } from 'bun:test';
|
|
15
|
+
import { bindBucket, defineBucket } from '@softistx/s3';
|
|
16
|
+
import { startTestS3, type TestS3 } from '@softistx/s3/testing';
|
|
17
|
+
|
|
18
|
+
const avatars = defineBucket({
|
|
19
|
+
bucket: 'avatars',
|
|
20
|
+
key: (params: { userId: string }) => `${params.userId}.png`,
|
|
21
|
+
contentType: 'image/png',
|
|
22
|
+
maxSize: 1024,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
let s3: TestS3;
|
|
26
|
+
|
|
27
|
+
beforeAll(async () => {
|
|
28
|
+
s3 = await startTestS3();
|
|
29
|
+
await s3.createBucket('avatars');
|
|
30
|
+
}, 120_000);
|
|
31
|
+
|
|
32
|
+
beforeEach(() => s3.reset('avatars'));
|
|
33
|
+
|
|
34
|
+
afterAll(() => s3.stop());
|
|
35
|
+
|
|
36
|
+
test('stores and reads back an avatar', async () => {
|
|
37
|
+
const store = bindBucket(avatars, s3.options);
|
|
38
|
+
await store.put({ userId: 'u1' }, new Uint8Array([1, 2, 3]), {
|
|
39
|
+
type: 'image/png',
|
|
40
|
+
});
|
|
41
|
+
expect(await store.bytes({ userId: 'u1' })).toEqual(new Uint8Array([1, 2, 3]));
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## What you get
|
|
46
|
+
|
|
47
|
+
`startTestS3(options?)` resolves to:
|
|
48
|
+
|
|
49
|
+
| field | what it is |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| `endpoint` | `http://127.0.0.1:<port>` |
|
|
52
|
+
| `accessKeyId`, `secretAccessKey`, `region` | the credentials the server accepts (local only), and `us-east-1` |
|
|
53
|
+
| `options` | all of the above plus `virtualHostedStyle: false`: pass it as the second argument of `bindBucket` |
|
|
54
|
+
| `createBucket(name, { timeoutMs }?)` | creates the bucket and resolves once it takes writes; waits 90 s unless `timeoutMs` is given |
|
|
55
|
+
| `reset(bucket, { timeoutMs }?)` | deletes every object of the bucket, so each test starts from nothing; no deadline unless `timeoutMs` is given |
|
|
56
|
+
| `stop()` | kills the server and removes its files; a second call does nothing |
|
|
57
|
+
|
|
58
|
+
A bucket must be created before it is used, and its name must follow S3's
|
|
59
|
+
rules (3 to 63 characters, lowercase letters, digits, dots and hyphens,
|
|
60
|
+
starting and ending with a letter or digit), or `createBucket` throws at
|
|
61
|
+
once. SeaweedFS makes a bucket on the first
|
|
62
|
+
write, and readiness is per bucket, so `createBucket` is what waits for it.
|
|
63
|
+
Two servers started at once are separate: each has its own ports, data and
|
|
64
|
+
buckets.
|
|
65
|
+
|
|
66
|
+
Forgetting `stop()` does not leave a process behind. The server does not keep
|
|
67
|
+
your process alive, so a script that never stops it still ends on its own, and
|
|
68
|
+
the server is killed with it. On SIGINT or SIGTERM the server is killed too,
|
|
69
|
+
and when yours is the only handler for that signal the process then ends with
|
|
70
|
+
the exit code the default would give (`128 + n`). If you register your own
|
|
71
|
+
handler, the process keeps your policy: the server is still killed, and
|
|
72
|
+
ending the process is yours to do. On those paths the server's temp directory and its unix sockets (which `weed`
|
|
73
|
+
puts in `/tmp`, whatever `$TMPDIR` is) are removed too. A `SIGKILL` of your process cannot be
|
|
74
|
+
caught and does leave the `weed` running: stop it yourself.
|
|
75
|
+
|
|
76
|
+
`reset` throws if it cannot list or delete, for a bucket that was never
|
|
77
|
+
created included, so create each bucket first.
|
|
78
|
+
|
|
79
|
+
## Failing fast on a stuck server
|
|
80
|
+
|
|
81
|
+
Without a deadline, a stuck server hangs `reset` until your runner's hook
|
|
82
|
+
timeout (15 s in `bun test`), and the failure says nothing about S3. Pass
|
|
83
|
+
`timeoutMs`, a whole number of milliseconds, 1 or more, to get an `S3Error`
|
|
84
|
+
with code `TIMEOUT` that names the bucket instead:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
beforeEach(() => s3.reset('avatars', { timeoutMs: 3_000 }));
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
On `reset` the deadline applies to each list and each round of deletes; on
|
|
91
|
+
`createBucket` it replaces the 90 s readiness wait, as a total. Keep it under
|
|
92
|
+
your hook timeout. A value that is not whole milliseconds throws `WRONG_OPTION`.
|
|
93
|
+
|
|
94
|
+
## Where the binary comes from
|
|
95
|
+
|
|
96
|
+
In this order:
|
|
97
|
+
|
|
98
|
+
1. the `binary` option: `startTestS3({ binary: '/usr/local/bin/weed' })`;
|
|
99
|
+
2. the `$WEED_BIN` environment variable;
|
|
100
|
+
3. the cache, `<cacheDir>/<version>/weed`, where `version` is
|
|
101
|
+
`TEST_SEAWEEDFS_VERSION` (4.47);
|
|
102
|
+
4. a download, once, into that cache.
|
|
103
|
+
|
|
104
|
+
`cacheDir` defaults to `$XDG_CACHE_HOME/nxgt/seaweedfs`, else
|
|
105
|
+
`~/.cache/nxgt/seaweedfs`: a per-user folder outside `node_modules`, so a clean
|
|
106
|
+
install keeps the binary and every repository on the machine shares one copy.
|
|
107
|
+
Pass `cacheDir` to put it elsewhere.
|
|
108
|
+
|
|
109
|
+
### Caching it in CI
|
|
110
|
+
|
|
111
|
+
A CI checkout starts empty, so cache the folder, keyed on the SeaweedFS
|
|
112
|
+
version (`TEST_SEAWEEDFS_VERSION`, 4.47) and the runner OS. With
|
|
113
|
+
`actions/cache`:
|
|
114
|
+
|
|
115
|
+
```yaml
|
|
116
|
+
- uses: actions/cache@v4
|
|
117
|
+
with:
|
|
118
|
+
path: ~/.cache/nxgt/seaweedfs
|
|
119
|
+
key: seaweedfs-${{ runner.os }}-${{ runner.arch }}-4.47
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Bump the version in the key when `@softistx/s3` moves `TEST_SEAWEEDFS_VERSION`. If
|
|
123
|
+
the job sets `XDG_CACHE_HOME` or passes `cacheDir`, give `path` that folder.
|
|
124
|
+
Two processes downloading at once is safe: each unpacks into its own folder and
|
|
125
|
+
renames the checked binary into place.
|
|
126
|
+
|
|
127
|
+
The first run downloads the release archive. Give `beforeAll` a timeout that allows
|
|
128
|
+
it (120 s above), or download ahead of the suite from a script your `test`
|
|
129
|
+
runs first:
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
// test/seaweedfs.ts: "test": "bun run test/seaweedfs.ts && bun test"
|
|
133
|
+
import { seaweedfsBinary } from '@softistx/s3/testing';
|
|
134
|
+
console.log(await seaweedfsBinary());
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## The download is checked
|
|
138
|
+
|
|
139
|
+
Every release asset of the pinned SeaweedFS version has its SHA-256 pinned in
|
|
140
|
+
the package. A download that does not match is refused with an error that
|
|
141
|
+
names both hashes, and nothing is unpacked or cached. Raising the version
|
|
142
|
+
means raising the table with it. Platforms the release does not ship
|
|
143
|
+
(Windows, 32-bit ARM macOS) get an error asking for `$WEED_BIN`.
|
|
144
|
+
|
|
145
|
+
## Why SeaweedFS
|
|
146
|
+
|
|
147
|
+
MinIO, the usual choice, no longer publishes a binary (`dl.min.io` answers
|
|
148
|
+
`410 Gone`). SeaweedFS does, for Linux, macOS, FreeBSD and OpenBSD, and
|
|
149
|
+
`weed server -s3` serves the API Bun's `S3Client` speaks.
|
|
150
|
+
|
|
151
|
+
Errors you can meet are in [Troubleshooting](../troubleshooting.md#testing).
|