@softistx/s3 0.0.0-stage → 0.13.1
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,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The SeaweedFS `weed` binary `@softistx/s3/testing` starts: where it comes from,
|
|
3
|
+
* and how it is checked.
|
|
4
|
+
*
|
|
5
|
+
* SeaweedFS is the server because **MinIO no longer is** — measured on
|
|
6
|
+
* 2026-09-18, `dl.min.io` answers `410 Gone` and the latest GitHub release
|
|
7
|
+
* carries no binary at all. SeaweedFS publishes one per platform, and
|
|
8
|
+
* `weed server -s3` serves the S3 API that `Bun.S3Client` talks to.
|
|
9
|
+
*
|
|
10
|
+
* The binary is, in order: the `binary` option, `$WEED_BIN`, the cached one,
|
|
11
|
+
* or one downloaded now into the cache and checked against the SHA-256 pinned
|
|
12
|
+
* below. A download that does not match is refused and never cached.
|
|
13
|
+
*/
|
|
14
|
+
/** The SeaweedFS release the test server runs: the one place it is pinned. */
|
|
15
|
+
export declare const TEST_SEAWEEDFS_VERSION = "4.47";
|
|
16
|
+
/**
|
|
17
|
+
* The SHA-256 of each release asset of `TEST_SEAWEEDFS_VERSION`, computed from
|
|
18
|
+
* the downloaded assets (and equal to the digest GitHub shows for them).
|
|
19
|
+
* Raise the version and this table together.
|
|
20
|
+
*/
|
|
21
|
+
export declare const TEST_SEAWEEDFS_SHA256: Readonly<Record<string, string>>;
|
|
22
|
+
export interface SeaweedfsOptions {
|
|
23
|
+
/** A `weed` binary to use. Wins over `$WEED_BIN` and the cache. */
|
|
24
|
+
binary?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Where the download is cached; the binary lives in `<cacheDir>/<version>/weed`.
|
|
27
|
+
* Defaults to `$XDG_CACHE_HOME/nxgt/seaweedfs`, else `~/.cache/nxgt/seaweedfs`.
|
|
28
|
+
*/
|
|
29
|
+
cacheDir?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The release asset for a platform and an architecture, as `process.platform`
|
|
33
|
+
* and `process.arch` name them. Throws for one the release does not ship.
|
|
34
|
+
*/
|
|
35
|
+
export declare function assetFor(platform: string, arch: string): string;
|
|
36
|
+
/**
|
|
37
|
+
* Refuses an archive whose SHA-256 is not the pinned one. The message names
|
|
38
|
+
* both, so a tampered download and a stale table read differently.
|
|
39
|
+
*/
|
|
40
|
+
export declare function verifyArchive(asset: string, bytes: Uint8Array, expected?: string | undefined): void;
|
|
41
|
+
/**
|
|
42
|
+
* `$XDG_CACHE_HOME/nxgt/seaweedfs`, else `~/.cache/nxgt/seaweedfs`: a per-user
|
|
43
|
+
* folder outside `node_modules`, so a clean install keeps the binary and every
|
|
44
|
+
* repository on the machine shares one copy. A relative `$XDG_CACHE_HOME` is
|
|
45
|
+
* ignored, as the XDG specification says.
|
|
46
|
+
*/
|
|
47
|
+
export declare function defaultCacheDir(env?: Record<string, string | undefined>, home?: string): string;
|
|
48
|
+
/** What `download` takes from outside; the specs replace both. */
|
|
49
|
+
export interface DownloadDeps {
|
|
50
|
+
fetch?: typeof fetch;
|
|
51
|
+
/** The SHA-256 the archive must have. Defaults to the pinned one. */
|
|
52
|
+
sha256?: string;
|
|
53
|
+
/** Tries at fetching and reading the archive, the first included. Defaults to 4. */
|
|
54
|
+
attempts?: number;
|
|
55
|
+
/** Waits `ms` between two tries. Defaults to a timer; the specs pass a no-op. */
|
|
56
|
+
delay?: (ms: number) => Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
export declare function download(asset: string, target: string, deps?: DownloadDeps): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* The path of a `weed` binary that runs: the `binary` option, `$WEED_BIN`, the
|
|
61
|
+
* cached one, or one downloaded now and checked against its pinned SHA-256.
|
|
62
|
+
*/
|
|
63
|
+
export declare function seaweedfsBinary(options?: SeaweedfsOptions): Promise<string>;
|
|
64
|
+
//# sourceMappingURL=seaweedfs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seaweedfs.d.ts","sourceRoot":"","sources":["../../src/testing/seaweedfs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAQH,8EAA8E;AAC9E,eAAO,MAAM,sBAAsB,SAAS,CAAC;AAE7C;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAuBlE,CAAC;AAEF,MAAM,WAAW,gBAAgB;IAChC,mEAAmE;IACnE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAsB/D;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC5B,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,UAAU,EACjB,QAAQ,qBAA+B,GACrC,IAAI,CAcN;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAC9B,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAe,EACrD,IAAI,GAAE,MAAkB,GACtB,MAAM,CAIR;AAED,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC5B,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,qEAAqE;IACrE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,oFAAoF;IACpF,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,iFAAiF;IACjF,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACtC;AAED,wBAAsB,QAAQ,CAC7B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,YAAiB,GACrB,OAAO,CAAC,IAAI,CAAC,CAoCf;AAED;;;GAGG;AACH,wBAAsB,eAAe,CACpC,OAAO,GAAE,gBAAqB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAoBjB"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type DeadlineOptions, type ServerAddress } from './ready.js';
|
|
2
|
+
import { type SeaweedfsOptions } from './seaweedfs.js';
|
|
3
|
+
export type TestS3Options = SeaweedfsOptions;
|
|
4
|
+
export interface TestS3 {
|
|
5
|
+
endpoint: string;
|
|
6
|
+
accessKeyId: string;
|
|
7
|
+
secretAccessKey: string;
|
|
8
|
+
region: string;
|
|
9
|
+
/** Options ready for `bindBucket`, pointing at this server. */
|
|
10
|
+
options: ServerAddress;
|
|
11
|
+
/**
|
|
12
|
+
* Creates `name` and resolves once it takes writes. With `timeoutMs` a
|
|
13
|
+
* server that is not ready in time fails with `S3Error` `TIMEOUT`; without
|
|
14
|
+
* it the wait is 90 s.
|
|
15
|
+
*/
|
|
16
|
+
createBucket(name: string, options?: DeadlineOptions): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Empties a bucket, so each test starts from nothing. With `timeoutMs`, a
|
|
19
|
+
* list or delete round that gets no answer in time fails with `S3Error`
|
|
20
|
+
* `TIMEOUT`, instead of hanging until the test runner's hook timeout.
|
|
21
|
+
*/
|
|
22
|
+
reset(bucket: string, options?: DeadlineOptions): Promise<void>;
|
|
23
|
+
/** Kills the server and removes its files. Calling it again does nothing. */
|
|
24
|
+
stop(): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A real S3 API for a suite: SeaweedFS's own gateway, started by
|
|
28
|
+
* `weed server -s3` on free ports, with no Docker. The first run downloads
|
|
29
|
+
* the binary into the cache.
|
|
30
|
+
*
|
|
31
|
+
* A server nobody stops does not keep the process alive, and is killed when
|
|
32
|
+
* the process exits or receives SIGINT or SIGTERM.
|
|
33
|
+
*/
|
|
34
|
+
export declare function startTestS3(options?: TestS3Options): Promise<TestS3>;
|
|
35
|
+
//# sourceMappingURL=test-s3.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-s3.d.ts","sourceRoot":"","sources":["../../src/testing/test-s3.ts"],"names":[],"mappings":"AAKA,OAAO,EAEN,KAAK,eAAe,EAEpB,KAAK,aAAa,EAClB,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,KAAK,gBAAgB,EAAmB,MAAM,aAAa,CAAC;AASrE,MAAM,MAAM,aAAa,GAAG,gBAAgB,CAAC;AAE7C,MAAM,WAAW,MAAM;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,+DAA+D;IAC/D,OAAO,EAAE,aAAa,CAAC;IACvB;;;;OAIG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACrE;;;;OAIG;IACH,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChE,6EAA6E;IAC7E,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB;AAED;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAChC,OAAO,GAAE,aAAkB,GACzB,OAAO,CAAC,MAAM,CAAC,CAsDjB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Subprocess } from 'bun';
|
|
2
|
+
/** The credentials the server signs with. Local only, and never a real key. */
|
|
3
|
+
export declare const ACCESS_KEY_ID = "nxgt-test-key";
|
|
4
|
+
export declare const SECRET_ACCESS_KEY = "nxgt-test-secret";
|
|
5
|
+
/**
|
|
6
|
+
* Without an identities file there are no credentials, and every signed
|
|
7
|
+
* request is refused.
|
|
8
|
+
*/
|
|
9
|
+
export declare function identities(): string;
|
|
10
|
+
/**
|
|
11
|
+
* `weed server -s3` on free ports. Each flag is a measured trap; see
|
|
12
|
+
* AGENTS.md, Tests.
|
|
13
|
+
*/
|
|
14
|
+
export declare function spawnWeed(binary: string, data: string, logDir: string, config: string, ports: number[], log: ReturnType<typeof Bun.file>): Subprocess;
|
|
15
|
+
/** The unix sockets `weed` leaves in the temp directory, one per port. */
|
|
16
|
+
export declare function socketsOf(ports: number[]): string[];
|
|
17
|
+
//# sourceMappingURL=weed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"weed.d.ts","sourceRoot":"","sources":["../../src/testing/weed.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEtC,+EAA+E;AAC/E,eAAO,MAAM,aAAa,kBAAkB,CAAC;AAC7C,eAAO,MAAM,iBAAiB,qBAAqB,CAAC;AAEpD;;;GAGG;AACH,wBAAgB,UAAU,IAAI,MAAM,CAYnC;AAED;;;GAGG;AACH,wBAAgB,SAAS,CACxB,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,MAAM,EAAE,EACf,GAAG,EAAE,UAAU,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,GAC9B,UAAU,CAuDZ;AAED,0EAA0E;AAC1E,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAUnD"}
|
package/docs/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# `@softistx/s3` documentation
|
|
2
|
+
|
|
3
|
+
S3 on Bun's own `S3Client`. `S3Client` is built into Bun, so this package has
|
|
4
|
+
**no dependency at all** — no AWS SDK — and does not run on Node.
|
|
5
|
+
|
|
6
|
+
| Page | Read it when |
|
|
7
|
+
| --- | --- |
|
|
8
|
+
| [Buckets](guide/buckets.md) | you are describing a bucket, building its keys, or binding it to credentials |
|
|
9
|
+
| [Reading](guide/reads.md) | you want an object's bytes, its text, a streamed read with a byte range (`open`), whether it is there, what the service knows about it, or a page of the bucket — and how to give a read, a listing or a write a deadline (`timeoutMs`) |
|
|
10
|
+
| [Writing](guide/writes.md) | you are storing an object, choosing what a write says about it, handling a refusal, or deleting an object (`put` and `delete` take a deadline) |
|
|
11
|
+
| [Presigned URLs](guide/presigned-urls.md) | a browser or another service should read or write an object directly, without your credentials — and a presigned POST, when the service must hold the upload to a size and a type |
|
|
12
|
+
| [Testing](guide/testing.md) | your suite needs a real S3 API: `@softistx/s3/testing` starts SeaweedFS, creates buckets and empties them between tests |
|
|
13
|
+
| [Troubleshooting](troubleshooting.md) | a call threw, an upload was refused, the bucket cannot be reached (`isBucketUnavailable`), or a signed URL did not do what you expected |
|
|
14
|
+
| [Roadmap](roadmap.md) | you want to know what is coming, and what has been ruled out |
|
|
15
|
+
|
|
16
|
+
The [README](../README.md) is the short version: install, one example per
|
|
17
|
+
area, and the traps in one line each.
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# Buckets
|
|
2
|
+
|
|
3
|
+
Describing a bucket once — which bucket, how a key is built, and what this
|
|
4
|
+
application is willing to put there — then binding that description to
|
|
5
|
+
credentials.
|
|
6
|
+
|
|
7
|
+
## The smallest thing that works
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { bindBucket, defineBucket } from '@softistx/s3';
|
|
11
|
+
|
|
12
|
+
export const avatars = defineBucket({
|
|
13
|
+
bucket: 'avatars',
|
|
14
|
+
key: (p: { userId: string }) => `${p.userId}.png`,
|
|
15
|
+
contentType: ['image/png', 'image/jpeg'],
|
|
16
|
+
maxSize: 2 * 1024 * 1024, // bytes
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const store = bindBucket(avatars, {
|
|
20
|
+
endpoint: process.env.S3_ENDPOINT,
|
|
21
|
+
accessKeyId: process.env.S3_KEY,
|
|
22
|
+
secretAccessKey: process.env.S3_SECRET,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
store.keyFor({ userId: 'u1' }); // 'u1.png'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`defineBucket` talks to nothing, so the definition is what a server, a worker
|
|
29
|
+
and a test import; `bindBucket` is the half that needs credentials.
|
|
30
|
+
`S3Client` is built into Bun, which is why there is no SDK in `bun add` — and
|
|
31
|
+
why this package does not run on Node.
|
|
32
|
+
|
|
33
|
+
## The signatures
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import type { S3Client, S3File, S3Options, S3Stats } from 'bun';
|
|
37
|
+
|
|
38
|
+
interface BucketDefinition<P> {
|
|
39
|
+
readonly bucket: string;
|
|
40
|
+
readonly key: (params: P) => string;
|
|
41
|
+
readonly contentType?: string | readonly string[] | undefined;
|
|
42
|
+
readonly maxSize?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Bun's `S3Options` without `bucket`, plus a default deadline and the origin
|
|
47
|
+
* browsers reach the service at.
|
|
48
|
+
*/
|
|
49
|
+
type BindOptions = Omit<S3Options, 'bucket'> &
|
|
50
|
+
CallOptions & {
|
|
51
|
+
publicEndpoint?: string | undefined;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
interface CallOptions {
|
|
55
|
+
timeoutMs?: number | undefined;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function defineBucket<P>(definition: BucketDefinition<P>): BucketDefinition<P>;
|
|
59
|
+
|
|
60
|
+
function bindBucket<P>(
|
|
61
|
+
definition: BucketDefinition<P>,
|
|
62
|
+
options?: BindOptions,
|
|
63
|
+
): BoundBucket<P>;
|
|
64
|
+
|
|
65
|
+
interface BoundBucket<P> {
|
|
66
|
+
readonly client: S3Client;
|
|
67
|
+
keyFor(params: P): string;
|
|
68
|
+
parseKey(key: string): P | undefined;
|
|
69
|
+
file(params: P): S3File;
|
|
70
|
+
put(params: P, body: PutBody, options?: PutOptions): Promise<void>;
|
|
71
|
+
bytes(params: P, options?: CallOptions): Promise<Uint8Array | undefined>;
|
|
72
|
+
text(params: P, options?: CallOptions): Promise<string | undefined>;
|
|
73
|
+
exists(params: P, options?: CallOptions): Promise<boolean>;
|
|
74
|
+
stat(params: P, options?: CallOptions): Promise<S3Stats | undefined>;
|
|
75
|
+
delete(params: P, options?: CallOptions): Promise<void>;
|
|
76
|
+
list(options?: {
|
|
77
|
+
prefix?: string;
|
|
78
|
+
limit?: number;
|
|
79
|
+
cursor?: string | null;
|
|
80
|
+
startAfter?: string;
|
|
81
|
+
}): Promise<ObjectPage>;
|
|
82
|
+
presignGet(params: P, options?: PresignGetOptions): string;
|
|
83
|
+
presignPut(params: P, options?: PresignOptions): string;
|
|
84
|
+
presignPost(params: P, options?: PresignPostOptions): PresignedPost;
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## The definition
|
|
89
|
+
|
|
90
|
+
| Option | Type | Default | Effect |
|
|
91
|
+
| --- | --- | --- | --- |
|
|
92
|
+
| `bucket` | `string` | required | the bucket's name on the service |
|
|
93
|
+
| `key` | `(params: P) => string` | required | the object's key, from whatever identifies it |
|
|
94
|
+
| `parseKey` | `(key: string) => P \| undefined` | none | the inverse of `key`: the params a stored key was built from, `undefined` when it is not one of this bucket's. Optional; `BoundBucket.parseKey` needs it |
|
|
95
|
+
| `contentType` | `string \| readonly string[]` | anything goes | the content types this bucket accepts; a write of anything else is refused **before it is sent**. A single type is also `presignPost`'s default `type` |
|
|
96
|
+
| `maxSize` | `number` | anything goes | the biggest body, in **bytes**, refused before it is sent. Also `presignPost`'s default `maxSize`, and its ceiling |
|
|
97
|
+
|
|
98
|
+
The key is a **function**, not a template, so nothing is spelled by hand at a
|
|
99
|
+
call site and a renamed parameter is a compile error. `P` is whatever that
|
|
100
|
+
function takes:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const uploads = defineBucket({
|
|
104
|
+
bucket: 'uploads',
|
|
105
|
+
key: (p: { folder: string; name: string }) => `${p.folder}/${p.name}`,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
bindBucket(uploads).keyFor({ folder: 'a', name: 'b.txt' }); // 'a/b.txt'
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`keyFor` exists so a caller that needs the string gets *the* string. Building
|
|
112
|
+
one by hand somewhere else is how a bucket ends up with two spellings of the
|
|
113
|
+
same object.
|
|
114
|
+
|
|
115
|
+
A caller that stored the key (a database column) and later needs to `stat`,
|
|
116
|
+
`delete` or `presignGet` it goes back to the params with `parseKey`, written
|
|
117
|
+
beside `key` so the two cannot drift apart:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const items = defineBucket({
|
|
121
|
+
bucket: 'files',
|
|
122
|
+
key: (p: { id: string }) => `items/${p.id}`,
|
|
123
|
+
parseKey: (key) => {
|
|
124
|
+
const id = /^items\/([0-9a-f-]+)$/.exec(key)?.[1];
|
|
125
|
+
return id === undefined ? undefined : { id };
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
const store = bindBucket(items);
|
|
130
|
+
const params = store.parseKey(storedKey); // { id } or undefined
|
|
131
|
+
if (params) await store.delete(params);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The bound `parseKey` also rebuilds the key from what the definition parsed and
|
|
135
|
+
answers `undefined` when it differs, so a parser looser than `key` cannot
|
|
136
|
+
accept a foreign key. Without a `parseKey` on the definition it throws a
|
|
137
|
+
`TypeError`, and a throw inside the definition's own `parseKey` propagates.
|
|
138
|
+
|
|
139
|
+
If `P` itself includes `undefined`, a miss and a valid key cannot be told
|
|
140
|
+
apart: the answer `undefined` is both. Keep `undefined` out of `P`.
|
|
141
|
+
|
|
142
|
+
What `contentType` and `maxSize` do is [Writing](writes.md); they are guards
|
|
143
|
+
on `put`, and nothing else on the object goes through them.
|
|
144
|
+
|
|
145
|
+
`defineBucket` refuses a definition that could never work, at import time:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
defineBucket({ bucket: '', key: () => 'k' });
|
|
149
|
+
// TypeError: defineBucket: a bucket definition needs a bucket
|
|
150
|
+
|
|
151
|
+
defineBucket({ bucket: 'b', key: () => 'k', maxSize: 0 });
|
|
152
|
+
// TypeError: defineBucket: "b" has a maxSize of 0; it is a number of bytes…
|
|
153
|
+
|
|
154
|
+
defineBucket({ bucket: 'b', key: () => 'k', contentType: [] });
|
|
155
|
+
// TypeError: … accepts an empty list of content types, so nothing could ever
|
|
156
|
+
// be written. Leave `contentType` out to accept anything
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The definition it gives back is frozen, so it cannot drift once it is shared.
|
|
160
|
+
|
|
161
|
+
## Binding it
|
|
162
|
+
|
|
163
|
+
`options` is Bun's own `S3Options` without `bucket` — the bucket comes from
|
|
164
|
+
the definition, and a call may never change it — plus `timeoutMs`, which is
|
|
165
|
+
this package's own. It is optional: given none, Bun reads its own `S3_*` /
|
|
166
|
+
`AWS_*` environment variables.
|
|
167
|
+
|
|
168
|
+
| option | |
|
|
169
|
+
| --- | --- |
|
|
170
|
+
| `timeoutMs` | `number`, whole milliseconds, 1 or more. Default: none, so Bun's own limit of 360 s (measured). The deadline of `put`, `stat`, `exists`, `bytes`, `slice`, `text`, `delete` and `list`: no answer in time is an `S3Error` coded `TIMEOUT`, and the request is aborted. It covers a whole `put`, an upload included, so a default sized for reads can cut a large one: give that `put` its own `timeoutMs`. A call's own `{ timeoutMs }` wins. It never reaches the `S3Client`. A bad value is a bare `TypeError` from `bindBucket` (wiring), and a `WRONG_OPTION` on a call — see [Deadlines](reads.md#deadlines) |
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
const store = bindBucket(avatars); // credentials from the environment
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
| Option | Type | Default | Effect |
|
|
177
|
+
| --- | --- | --- | --- |
|
|
178
|
+
| `endpoint` | `string` | AWS | the service's URL; anything S3-compatible Bun can sign for |
|
|
179
|
+
| `publicEndpoint` | `string` | `endpoint` | the origin browsers reach the service at, when it is not `endpoint`. `presignGet`, `presignPut` and `presignPost` are signed for it; every other call goes to `endpoint`. See [Presigned URLs](presigned-urls.md#a-public-origin-for-browsers-an-internal-one-for-the-server). A value that is not an absolute URL is a `TypeError` from `bindBucket` |
|
|
180
|
+
| `accessKeyId` / `secretAccessKey` | `string` | `$S3_*` / `$AWS_*` | the credentials |
|
|
181
|
+
| `sessionToken` | `string` | `$AWS_SESSION_TOKEN` | for temporary credentials |
|
|
182
|
+
| `region` | `string` | `$S3_REGION` / `$AWS_REGION` | the region to sign for |
|
|
183
|
+
| `virtualHostedStyle` | `boolean` | `false` | `bucket.host` URLs instead of `host/bucket`; a service that is not AWS usually wants it left off |
|
|
184
|
+
| `acl`, `storageClass` | see [Writing](writes.md) | — | a default for every object this client writes |
|
|
185
|
+
| `retry`, `partSize`, `queueSize` | `number` | Bun's | the client's own transfer tuning. A [`put`](writes.md) takes none of these: it is one PUT |
|
|
186
|
+
|
|
187
|
+
Each bound bucket holds an `S3Client` of its own. S3 is stateless HTTP —
|
|
188
|
+
there is no connection to share, and **nothing to close**.
|
|
189
|
+
|
|
190
|
+
## Everything this package does not wrap
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
store.client; // Bun's S3Client
|
|
194
|
+
store.file({ userId: 'u1' }); // Bun's lazy S3File
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
`file(params)` is the handle for `.stream()`, `.slice()`, `.writer()` and the
|
|
198
|
+
rest, keyed by the definition rather than by a string you typed. It is also
|
|
199
|
+
the way to write a body in parts, which `put` does not do.
|
|
200
|
+
|
|
201
|
+
## Types a caller names
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
import type { BoundBucket, ParamsOf } from '@softistx/s3';
|
|
205
|
+
|
|
206
|
+
type AvatarParams = ParamsOf<typeof avatars>; // { userId: string }
|
|
207
|
+
|
|
208
|
+
async function purge(bucket: BoundBucket<AvatarParams>, userId: string) {
|
|
209
|
+
await bucket.delete({ userId });
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
`stat` gives back Bun's own `S3Stats`, which this package does not re-export:
|
|
214
|
+
import it from `bun` where you need to name it.
|
|
215
|
+
|
|
216
|
+
## Next
|
|
217
|
+
|
|
218
|
+
- [Writing](writes.md) — `put`, its options, and the guards.
|
|
219
|
+
- [Reading](reads.md) — `bytes`, `slice`, `text`, `stat`, `exists` and `list`.
|
|
220
|
+
- [Presigned URLs](presigned-urls.md) — the same definition, signed.
|