@lunora/storage 1.0.0-alpha.3 → 1.0.0-alpha.30
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.md +6 -0
- package/dist/index.d.mts +130 -253
- package/dist/index.d.ts +130 -253
- package/dist/index.mjs +1 -4
- package/dist/packem_shared/DEFAULT_MAX_UPLOAD_BYTES-ByeD2zWk.mjs +1 -0
- package/dist/packem_shared/buildPresignedUrl-pN4iJAXf.mjs +4 -0
- package/dist/packem_shared/buildSignedUrl-jnr4ykB0.mjs +1 -0
- package/dist/packem_shared/createBucketStorage-BE3H6x-j.mjs +1 -0
- package/dist/packem_shared/createStorage-BGxUJCQr.mjs +1 -0
- package/dist/packem_shared/internal-B_cLiXkR.mjs +1 -0
- package/dist/packem_shared/signed-url-C_cjNfJG.mjs +5 -0
- package/dist/upload.d.mts +172 -0
- package/dist/upload.d.ts +172 -0
- package/dist/upload.mjs +1 -0
- package/package.json +11 -1
- package/dist/packem_shared/buildPresignedUrl-DzPwi1bY.mjs +0 -70
- package/dist/packem_shared/buildSignedUrl-ZzB16yPl.mjs +0 -107
- package/dist/packem_shared/createBucketStorage-4Xk7-5CN.mjs +0 -26
- package/dist/packem_shared/createStorage-Bs_iJ1Mx.mjs +0 -251
package/LICENSE.md
CHANGED
|
@@ -103,3 +103,9 @@ Unless required by applicable law or agreed to in writing, software distributed
|
|
|
103
103
|
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
104
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
105
|
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/dist/index.d.mts
CHANGED
|
@@ -1,140 +1,11 @@
|
|
|
1
|
+
import { R2MultipartUploadLike, R2RangeLike, R2ObjectBodyLike, R2ObjectLike, R2BucketLike } from '@lunora/platform';
|
|
2
|
+
export type { R2BucketLike, R2MultipartUploadLike, R2ObjectBodyLike, R2ObjectLike, R2RangeLike, R2UploadedPartLike } from '@lunora/platform';
|
|
1
3
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*/
|
|
7
|
-
type R2RangeLike = {
|
|
8
|
-
length: number;
|
|
9
|
-
offset?: number;
|
|
10
|
-
} | {
|
|
11
|
-
length?: number;
|
|
12
|
-
offset: number;
|
|
13
|
-
} | {
|
|
14
|
-
suffix: number;
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Minimal projection of `R2Bucket`. Declared structurally so unit tests can
|
|
18
|
-
* pass a plain object double; the real binding satisfies the same shape.
|
|
19
|
-
*/
|
|
20
|
-
interface R2BucketLike {
|
|
21
|
-
/**
|
|
22
|
-
* Begin a multipart upload (R2 `createMultipartUpload`). Optional so existing
|
|
23
|
-
* test doubles still satisfy the type; {@link Storage.createMultipartUpload}
|
|
24
|
-
* throws a clear error when the binding lacks it.
|
|
25
|
-
*/
|
|
26
|
-
createMultipartUpload?: (key: string, options?: {
|
|
27
|
-
customMetadata?: Record<string, string>;
|
|
28
|
-
httpMetadata?: {
|
|
29
|
-
contentType?: string;
|
|
30
|
-
};
|
|
31
|
-
}) => Promise<R2MultipartUploadLike>;
|
|
32
|
-
delete: (key: string) => Promise<void>;
|
|
33
|
-
get: (key: string, options?: {
|
|
34
|
-
range?: R2RangeLike;
|
|
35
|
-
}) => Promise<R2ObjectBodyLike | null>;
|
|
36
|
-
/**
|
|
37
|
-
* Fetch an object's metadata without its body (R2 HEAD). Returns `null` when
|
|
38
|
-
* the object is absent. Declared optional so existing test doubles that only
|
|
39
|
-
* implement `get`/`put`/`list`/`delete` still satisfy the type; callers that
|
|
40
|
-
* need metadata fall back to a 0-length ranged `get()` when `head` is absent.
|
|
41
|
-
*/
|
|
42
|
-
head?: (key: string) => Promise<R2ObjectLike | null>;
|
|
43
|
-
list: (options?: {
|
|
44
|
-
cursor?: string;
|
|
45
|
-
delimiter?: string;
|
|
46
|
-
limit?: number;
|
|
47
|
-
prefix?: string;
|
|
48
|
-
}) => Promise<{
|
|
49
|
-
cursor?: string;
|
|
50
|
-
objects: R2ObjectLike[];
|
|
51
|
-
truncated?: boolean;
|
|
52
|
-
}>;
|
|
53
|
-
put: (key: string, body: ReadableStream | ArrayBuffer | Blob | string | null, options?: {
|
|
54
|
-
customMetadata?: Record<string, string>;
|
|
55
|
-
httpMetadata?: {
|
|
56
|
-
contentType?: string;
|
|
57
|
-
};
|
|
58
|
-
}) => Promise<R2ObjectLike>;
|
|
59
|
-
/** Resume an in-progress multipart upload by id (R2 `resumeMultipartUpload`). Optional; see {@link Storage.resumeMultipartUpload}. */
|
|
60
|
-
resumeMultipartUpload?: (key: string, uploadId: string) => R2MultipartUploadLike;
|
|
61
|
-
}
|
|
62
|
-
/** One uploaded multipart part — returned by `uploadPart`, required to `complete`. Mirrors R2's `R2UploadedPart`. */
|
|
63
|
-
interface R2UploadedPartLike {
|
|
64
|
-
etag: string;
|
|
65
|
-
partNumber: number;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* An in-progress multipart upload, mirroring R2's `R2MultipartUpload`. Each part
|
|
69
|
-
* (except the last) must be uniform in size. The object does not guarantee the
|
|
70
|
-
* underlying upload still exists — a parallel `complete`/`abort` can invalidate
|
|
71
|
-
* it — so wrap each call in error handling.
|
|
72
|
-
*/
|
|
73
|
-
interface R2MultipartUploadLike {
|
|
74
|
-
/** Abort the upload, discarding any uploaded parts. */
|
|
75
|
-
abort: () => Promise<void>;
|
|
76
|
-
/** Finish the upload from the collected parts; resolves to the stored object. */
|
|
77
|
-
complete: (uploadedParts: R2UploadedPartLike[]) => Promise<R2ObjectLike>;
|
|
78
|
-
/** The object key being assembled. */
|
|
79
|
-
readonly key: string;
|
|
80
|
-
/** The R2 upload id (persist it to resume across requests). */
|
|
81
|
-
readonly uploadId: string;
|
|
82
|
-
/** Upload one part (1-indexed); returns the `{ partNumber, etag }` to pass to `complete`. */
|
|
83
|
-
uploadPart: (partNumber: number, value: ArrayBuffer | ArrayBufferView | Blob | ReadableStream | string) => Promise<R2UploadedPartLike>;
|
|
84
|
-
}
|
|
85
|
-
interface R2ObjectLike {
|
|
86
|
-
/**
|
|
87
|
-
* R2-computed checksums. The real binding exposes `sha256` as an
|
|
88
|
-
* `ArrayBuffer` (present only when R2 stored a SHA-256 for the object);
|
|
89
|
-
* declared optional so fakes and non-checksummed objects type-check.
|
|
90
|
-
*/
|
|
91
|
-
checksums?: {
|
|
92
|
-
sha256?: ArrayBuffer;
|
|
93
|
-
};
|
|
94
|
-
customMetadata?: Record<string, string>;
|
|
95
|
-
etag: string;
|
|
96
|
-
/**
|
|
97
|
-
* The quoted form of {@link R2ObjectLike.etag} (e.g. `"abc123"`), suitable
|
|
98
|
-
* for emitting directly as an HTTP `ETag` header. The real binding always
|
|
99
|
-
* provides it; declared optional so existing doubles that only set `etag`
|
|
100
|
-
* still type-check (callers fall back to quoting `etag`).
|
|
101
|
-
*/
|
|
102
|
-
httpEtag?: string;
|
|
103
|
-
httpMetadata?: {
|
|
104
|
-
contentType?: string;
|
|
105
|
-
};
|
|
106
|
-
key: string;
|
|
107
|
-
/**
|
|
108
|
-
* Hex-encoded SHA-256 of the object body, surfaced by `download()`/`list()`
|
|
109
|
-
* when R2 carries a checksum (derived from {@link R2ObjectLike.checksums}).
|
|
110
|
-
*/
|
|
111
|
-
sha256?: string;
|
|
112
|
-
/**
|
|
113
|
-
* Base64-encoded SHA-256 of the object body, surfaced alongside
|
|
114
|
-
* {@link R2ObjectLike.sha256} from the same checksum. Base64 is the encoding
|
|
115
|
-
* RFC 9530 digest headers (`Repr-Digest`/`Content-Digest`) require, so HTTP
|
|
116
|
-
* layers can emit a spec-compliant digest without re-deriving it.
|
|
117
|
-
*/
|
|
118
|
-
sha256Base64?: string;
|
|
119
|
-
size: number;
|
|
120
|
-
/**
|
|
121
|
-
* When the object was written. The real binding exposes this as a `Date`;
|
|
122
|
-
* declared optional so fakes that omit it still type-check.
|
|
123
|
-
* {@link Storage.getMetadata} normalises it to epoch ms.
|
|
124
|
-
*/
|
|
125
|
-
uploaded?: Date;
|
|
126
|
-
}
|
|
127
|
-
interface R2ObjectBodyLike extends R2ObjectLike {
|
|
128
|
-
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
129
|
-
body: ReadableStream | null;
|
|
130
|
-
text: () => Promise<string>;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* R2 S3-API credentials for {@link Storage.getPresignedUrl}. These are an R2 API
|
|
134
|
-
* token's Access Key ID / Secret Access Key (NOT a Cloudflare API token), plus
|
|
135
|
-
* the account id and bucket name. Required only if you call `getPresignedUrl`;
|
|
136
|
-
* the worker-signed URL path (`getSignedUrl`) needs none of this.
|
|
137
|
-
*/
|
|
4
|
+
* R2 S3-API credentials for {@link Storage.getPresignedUrl}. These are an R2 API
|
|
5
|
+
* token's Access Key ID / Secret Access Key (NOT a Cloudflare API token), plus
|
|
6
|
+
* the account id and bucket name. Required only if you call `getPresignedUrl`;
|
|
7
|
+
* the worker-signed URL path (`getSignedUrl`) needs none of this.
|
|
8
|
+
*/
|
|
138
9
|
interface R2S3Credentials {
|
|
139
10
|
/** R2 S3 Access Key ID. */
|
|
140
11
|
accessKeyId: string;
|
|
@@ -159,10 +30,10 @@ interface LunoraStorageOptions {
|
|
|
159
30
|
/** Public base URL used by `getSignedUrl()`. Required for signed URLs. */
|
|
160
31
|
publicBaseUrl?: string;
|
|
161
32
|
/**
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
33
|
+
* R2 S3-API credentials enabling {@link Storage.getPresignedUrl} (native S3
|
|
34
|
+
* presigned URLs that hit R2 directly, bypassing the Worker). Omit to use
|
|
35
|
+
* only the worker-signed URL path.
|
|
36
|
+
*/
|
|
166
37
|
s3?: R2S3Credentials;
|
|
167
38
|
/** HMAC secret used by the worker-signed URL helper. Required for signed URLs. */
|
|
168
39
|
signingSecret?: string;
|
|
@@ -173,14 +44,23 @@ interface UploadOptions {
|
|
|
173
44
|
contentType?: string;
|
|
174
45
|
customMetadata?: Record<string, string>;
|
|
175
46
|
/**
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
47
|
+
* Maximum body size in bytes. For `ArrayBuffer`/`Blob` sources the length is
|
|
48
|
+
* known up front and rejected before the upload starts. For a
|
|
49
|
+
* `ReadableStream` the length isn't known synchronously, so the stream is
|
|
50
|
+
* piped through a byte counter that aborts the upload once the limit is
|
|
51
|
+
* exceeded — this also guards against R2 silently accepting/truncating an
|
|
52
|
+
* unbounded stream.
|
|
53
|
+
*/
|
|
183
54
|
maxSize?: number;
|
|
55
|
+
/**
|
|
56
|
+
* SHA-256 of the body (hex or a 32-byte buffer), recorded with the object.
|
|
57
|
+
*
|
|
58
|
+
* R2 only reports a checksum it was given: without this, `list()`/`head()`
|
|
59
|
+
* return no `sha256` and any later integrity check degrades to comparing
|
|
60
|
+
* sizes. R2 also verifies the digest itself on write, so supplying it turns
|
|
61
|
+
* the upload into a checked one.
|
|
62
|
+
*/
|
|
63
|
+
sha256?: ArrayBuffer | string;
|
|
184
64
|
}
|
|
185
65
|
interface ListOptions {
|
|
186
66
|
cursor?: string;
|
|
@@ -191,20 +71,20 @@ interface ListOptions {
|
|
|
191
71
|
}
|
|
192
72
|
interface SignedUrlOptions {
|
|
193
73
|
/**
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
74
|
+
* Pin the `Content-Type` an uploader must send on a `method: "PUT"` URL.
|
|
75
|
+
* Baked into the HMAC canonical so the signature only authorizes a PUT with
|
|
76
|
+
* exactly this content-type; mirrored on the URL as `&ct=...`. Ignored for
|
|
77
|
+
* `GET` URLs (a download has no request body content-type to pin).
|
|
78
|
+
*/
|
|
199
79
|
contentType?: string;
|
|
200
80
|
expiresInSeconds?: number;
|
|
201
81
|
method?: "GET" | "PUT";
|
|
202
82
|
}
|
|
203
83
|
/**
|
|
204
|
-
* Per-object metadata returned by {@link Storage.getMetadata} — a flat,
|
|
205
|
-
* body-free projection of {@link R2ObjectLike}. Mirrors the shape Convex
|
|
206
|
-
* surfaces for `ctx.storage.getMetadata` / the `_storage` system table.
|
|
207
|
-
*/
|
|
84
|
+
* Per-object metadata returned by {@link Storage.getMetadata} — a flat,
|
|
85
|
+
* body-free projection of {@link R2ObjectLike}. Mirrors the shape Convex
|
|
86
|
+
* surfaces for `ctx.storage.getMetadata` / the `_storage` system table.
|
|
87
|
+
*/
|
|
208
88
|
interface ObjectMetadata {
|
|
209
89
|
/** The object's `Content-Type` (R2 `httpMetadata.contentType`), if recorded. */
|
|
210
90
|
contentType?: string;
|
|
@@ -221,49 +101,49 @@ interface ObjectMetadata {
|
|
|
221
101
|
}
|
|
222
102
|
interface Storage {
|
|
223
103
|
/**
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
104
|
+
* Begin a native R2 **multipart upload** for very large objects — upload
|
|
105
|
+
* parts (each uniform in size except the last), then `complete` with the
|
|
106
|
+
* returned parts (or `abort`). Wraps R2's `createMultipartUpload`; throws if
|
|
107
|
+
* the bound bucket doesn't support it. For ordinary uploads use
|
|
108
|
+
* {@link Storage.upload} / {@link Storage.store}.
|
|
109
|
+
*/
|
|
230
110
|
createMultipartUpload: (key: string, options?: {
|
|
231
111
|
contentType?: string;
|
|
232
112
|
customMetadata?: Record<string, string>;
|
|
233
113
|
}) => Promise<R2MultipartUploadLike>;
|
|
234
114
|
delete: (key: string) => Promise<void>;
|
|
235
115
|
/**
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
116
|
+
* Fetch a stored object's metadata + body. Pass `options.range` to stream
|
|
117
|
+
* only a byte window (R2 resolves the range server-side, so the unwanted
|
|
118
|
+
* bytes never reach the Worker) — `download(key)` reads the whole object.
|
|
119
|
+
*/
|
|
240
120
|
download: (key: string, options?: {
|
|
241
121
|
range?: R2RangeLike;
|
|
242
122
|
}) => Promise<R2ObjectBodyLike | null>;
|
|
243
123
|
/**
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
124
|
+
* Mint a short-lived signed `PUT` URL a client can upload directly to,
|
|
125
|
+
* optionally pinning the request `Content-Type`. Convex-compatible alias
|
|
126
|
+
* built on {@link Storage.getSignedUrl} with `method: "PUT"`.
|
|
127
|
+
*/
|
|
248
128
|
generateUploadUrl: (key: string, options?: {
|
|
249
129
|
contentType?: string;
|
|
250
130
|
expiresInSeconds?: number;
|
|
251
131
|
}) => Promise<string>;
|
|
252
132
|
/**
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
133
|
+
* Read a stored object's metadata (size, content-type, sha256, upload time,
|
|
134
|
+
* custom metadata) without fetching its body. Returns `null` when the object
|
|
135
|
+
* is absent. Backed by an R2 HEAD (`bucket.head`) when available, falling
|
|
136
|
+
* back to a 0-length ranged `get()` otherwise. Mirrors Convex's
|
|
137
|
+
* `ctx.storage.getMetadata`.
|
|
138
|
+
*/
|
|
259
139
|
getMetadata: (key: string) => Promise<ObjectMetadata | null>;
|
|
260
140
|
/**
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
141
|
+
* Mint a native S3 **presigned URL** (SigV4) that hits R2 directly, bypassing
|
|
142
|
+
* the Worker. Use for large downloads/uploads where you don't need per-request
|
|
143
|
+
* app gating and want the bytes off the Worker's CPU/bandwidth budget. Requires
|
|
144
|
+
* {@link LunoraStorageOptions.s3} credentials; throws if they're absent. For
|
|
145
|
+
* app-gated access (auth/policy/rate-limit) prefer {@link Storage.getSignedUrl}.
|
|
146
|
+
*/
|
|
267
147
|
getPresignedUrl: (key: string, options?: PresignedUrlOptions) => Promise<string>;
|
|
268
148
|
getSignedUrl: (key: string, options?: SignedUrlOptions) => Promise<string>;
|
|
269
149
|
getUrl: (key: string) => string;
|
|
@@ -273,16 +153,16 @@ interface Storage {
|
|
|
273
153
|
truncated?: boolean;
|
|
274
154
|
}>;
|
|
275
155
|
/**
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
156
|
+
* Resume an in-progress multipart upload by its `uploadId` (e.g. across
|
|
157
|
+
* requests). Wraps R2's `resumeMultipartUpload`; the id is not validated by
|
|
158
|
+
* R2, so a stale id surfaces as an error on the first `uploadPart`/`complete`.
|
|
159
|
+
*/
|
|
280
160
|
resumeMultipartUpload: (key: string, uploadId: string) => R2MultipartUploadLike;
|
|
281
161
|
/**
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
162
|
+
* Upload `body` to `key`, returning the stored key + etag. Convex-compatible
|
|
163
|
+
* alias for {@link Storage.upload} — it accepts the same {@link UploadOptions}
|
|
164
|
+
* so the `maxSize` / `allowedContentTypes` guards aren't lost behind the alias.
|
|
165
|
+
*/
|
|
286
166
|
store: (key: string, body: ReadableStream | ArrayBuffer | Blob, options?: UploadOptions) => Promise<{
|
|
287
167
|
etag: string;
|
|
288
168
|
httpEtag: string;
|
|
@@ -295,11 +175,11 @@ interface Storage {
|
|
|
295
175
|
}>;
|
|
296
176
|
}
|
|
297
177
|
/**
|
|
298
|
-
* A bucket-aware {@link Storage}: the methods target a default bucket, and
|
|
299
|
-
* `bucket(name)` selects a different named bucket (declared via
|
|
300
|
-
* `v.storage("name")`). `bucketName` is the bucket the current accessor targets
|
|
301
|
-
* — the storage-rules middleware reads it to scope `(bucket, operation)` rules.
|
|
302
|
-
*/
|
|
178
|
+
* A bucket-aware {@link Storage}: the methods target a default bucket, and
|
|
179
|
+
* `bucket(name)` selects a different named bucket (declared via
|
|
180
|
+
* `v.storage("name")`). `bucketName` is the bucket the current accessor targets
|
|
181
|
+
* — the storage-rules middleware reads it to scope `(bucket, operation)` rules.
|
|
182
|
+
*/
|
|
303
183
|
interface BucketStorage extends Storage {
|
|
304
184
|
/** Select a named bucket. Unknown names throw with the list of registered buckets. */
|
|
305
185
|
bucket: (name: string) => BucketStorage;
|
|
@@ -307,36 +187,30 @@ interface BucketStorage extends Storage {
|
|
|
307
187
|
readonly bucketName: string;
|
|
308
188
|
}
|
|
309
189
|
/**
|
|
310
|
-
* Compose several per-bucket {@link Storage} instances into one bucket-aware
|
|
311
|
-
* accessor. The bare methods (`download` / `store` / …) target the default
|
|
312
|
-
* bucket; `bucket(name)` switches to another. Each accessor is tagged with its
|
|
313
|
-
* `bucketName` so `storageRules(...)` can enforce per-bucket.
|
|
314
|
-
*
|
|
315
|
-
* ```ts
|
|
316
|
-
* storage: (env) => createBucketStorage({
|
|
317
|
-
* default: createStorage({ bucket: env.FILES }),
|
|
318
|
-
* avatars: createStorage({ bucket: env.AVATARS }),
|
|
319
|
-
* }),
|
|
320
|
-
* // → ctx.storage.download(key) // default bucket
|
|
321
|
-
* // → ctx.storage.bucket("avatars").store() // the avatars bucket
|
|
322
|
-
* ```
|
|
323
|
-
*
|
|
324
|
-
* The bare accessor is tagged `"default"` — the canonical name a
|
|
325
|
-
* `defineStorageRule({ bucket: "default" })` rule and the generated
|
|
326
|
-
* `StorageBucketName` union both use — unless `options.default` names another
|
|
327
|
-
* bucket (then the bare accessor takes that name). The binding it delegates to is
|
|
328
|
-
* `options.default`, else the `"default"` key when present, else the first
|
|
329
|
-
* registered bucket. Named buckets are reached with `bucket(name)`.
|
|
330
|
-
*/
|
|
190
|
+
* Compose several per-bucket {@link Storage} instances into one bucket-aware
|
|
191
|
+
* accessor. The bare methods (`download` / `store` / …) target the default
|
|
192
|
+
* bucket; `bucket(name)` switches to another. Each accessor is tagged with its
|
|
193
|
+
* `bucketName` so `storageRules(...)` can enforce per-bucket.
|
|
194
|
+
*
|
|
195
|
+
* ```ts
|
|
196
|
+
* storage: (env) => createBucketStorage({
|
|
197
|
+
* default: createStorage({ bucket: env.FILES }),
|
|
198
|
+
* avatars: createStorage({ bucket: env.AVATARS }),
|
|
199
|
+
* }),
|
|
200
|
+
* // → ctx.storage.download(key) // default bucket
|
|
201
|
+
* // → ctx.storage.bucket("avatars").store() // the avatars bucket
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* The bare accessor is tagged `"default"` — the canonical name a
|
|
205
|
+
* `defineStorageRule({ bucket: "default" })` rule and the generated
|
|
206
|
+
* `StorageBucketName` union both use — unless `options.default` names another
|
|
207
|
+
* bucket (then the bare accessor takes that name). The binding it delegates to is
|
|
208
|
+
* `options.default`, else the `"default"` key when present, else the first
|
|
209
|
+
* registered bucket. Named buckets are reached with `bucket(name)`.
|
|
210
|
+
*/
|
|
331
211
|
declare const createBucketStorage: (buckets: Record<string, Storage>, options?: {
|
|
332
212
|
default?: string;
|
|
333
213
|
}) => BucketStorage;
|
|
334
|
-
/**
|
|
335
|
-
* Compose a per-tenant key from a scope prefix and a caller-supplied key.
|
|
336
|
-
* Both halves are validated — the prefix may not contain `..` or NUL either,
|
|
337
|
-
* and the resulting key must stay under R2's length ceiling. Recommended for
|
|
338
|
-
* any multi-tenant deployment so client-supplied keys can't address peer data.
|
|
339
|
-
*/
|
|
340
214
|
declare const scopeKey: (prefix: string, key: string) => string;
|
|
341
215
|
declare const createStorage: (options: LunoraStorageOptions) => Storage;
|
|
342
216
|
/** Parameters accepted by {@link buildPresignedUrl}. */
|
|
@@ -353,25 +227,28 @@ interface PresignedUrlParams {
|
|
|
353
227
|
now?: () => number;
|
|
354
228
|
}
|
|
355
229
|
/**
|
|
356
|
-
* Build a native S3 presigned URL for an R2 object using SigV4 query-string
|
|
357
|
-
* auth. The returned URL points at R2's S3 endpoint and carries the full
|
|
358
|
-
* signature, so it authorizes a single `GET`/`PUT` on `key` until it expires —
|
|
359
|
-
* no Worker round-trip.
|
|
360
|
-
*/
|
|
230
|
+
* Build a native S3 presigned URL for an R2 object using SigV4 query-string
|
|
231
|
+
* auth. The returned URL points at R2's S3 endpoint and carries the full
|
|
232
|
+
* signature, so it authorizes a single `GET`/`PUT` on `key` until it expires —
|
|
233
|
+
* no Worker round-trip.
|
|
234
|
+
*/
|
|
361
235
|
declare const buildPresignedUrl: (parameters: PresignedUrlParams) => Promise<string>;
|
|
362
236
|
/**
|
|
363
|
-
* Worker-signed URL: the `publicBaseUrl` joined to the object `key`, plus a query
|
|
364
|
-
* string carrying `exp` (unix seconds), `method` (`GET` or `PUT`) and `sig`
|
|
365
|
-
* (a base64url HMAC).
|
|
366
|
-
*
|
|
367
|
-
* The HMAC canonical includes the URL host so a signature minted for one bucket
|
|
368
|
-
* cannot be replayed against another host on the same signing secret. Even so,
|
|
369
|
-
* the signing secret MUST NOT be shared across buckets/tenants — host binding
|
|
370
|
-
* narrows replay surface but is not a substitute for per-tenant key isolation.
|
|
371
|
-
*
|
|
372
|
-
* The Worker handling
|
|
373
|
-
*
|
|
374
|
-
|
|
237
|
+
* Worker-signed URL: the `publicBaseUrl` joined to the object `key`, plus a query
|
|
238
|
+
* string carrying `exp` (unix seconds), `method` (`GET` or `PUT`) and `sig`
|
|
239
|
+
* (a base64url HMAC).
|
|
240
|
+
*
|
|
241
|
+
* The HMAC canonical includes the URL host so a signature minted for one bucket
|
|
242
|
+
* cannot be replayed against another host on the same signing secret. Even so,
|
|
243
|
+
* the signing secret MUST NOT be shared across buckets/tenants — host binding
|
|
244
|
+
* narrows replay surface but is not a substitute for per-tenant key isolation.
|
|
245
|
+
*
|
|
246
|
+
* The Worker route handling the signed download/upload (mounted at
|
|
247
|
+
* `publicBaseUrl`'s origin, e.g. `GET /:key`) should call
|
|
248
|
+
* {@link verifySignedUrl} to validate the signature + expiry before streaming
|
|
249
|
+
* the R2 body. `baseUrl` must be a bare origin (no path) — see the module
|
|
250
|
+
* docstring.
|
|
251
|
+
*/
|
|
375
252
|
declare const buildSignedUrl: (args: SignedUrlOptions & {
|
|
376
253
|
baseUrl: string;
|
|
377
254
|
key: string;
|
|
@@ -383,24 +260,24 @@ interface VerifyResult {
|
|
|
383
260
|
key?: string;
|
|
384
261
|
method?: "GET" | "PUT";
|
|
385
262
|
/**
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
263
|
+
* Internal-only failure reason for server logs/diagnostics. **Do not echo
|
|
264
|
+
* to clients** — a precise reason ("expired" vs "bad_signature") is a
|
|
265
|
+
* signing oracle. Public responses should expose only `valid`.
|
|
266
|
+
*/
|
|
390
267
|
reason?: "bad_signature" | "expired" | "malformed";
|
|
391
268
|
valid: boolean;
|
|
392
269
|
}
|
|
393
270
|
/**
|
|
394
|
-
* Verify a {@link buildSignedUrl} output. By default the signature is
|
|
395
|
-
* canonicalized against the inbound `url.host`, which matches the build-side
|
|
396
|
-
* host whenever the URL being verified is the URL that was minted. In a
|
|
397
|
-
* topology where the host the Worker sees differs from the configured
|
|
398
|
-
* `publicBaseUrl` host (e.g. a CDN host vs a Worker route that rewrites
|
|
399
|
-
* `Host`), pass `expectedHost` (the `publicBaseUrl` host) so verification
|
|
400
|
-
* canonicalizes against the same host the signature was minted for instead of
|
|
401
|
-
* failing every request as `bad_signature`.
|
|
402
|
-
*/
|
|
271
|
+
* Verify a {@link buildSignedUrl} output. By default the signature is
|
|
272
|
+
* canonicalized against the inbound `url.host`, which matches the build-side
|
|
273
|
+
* host whenever the URL being verified is the URL that was minted. In a
|
|
274
|
+
* topology where the host the Worker sees differs from the configured
|
|
275
|
+
* `publicBaseUrl` host (e.g. a CDN host vs a Worker route that rewrites
|
|
276
|
+
* `Host`), pass `expectedHost` (the `publicBaseUrl` host) so verification
|
|
277
|
+
* canonicalizes against the same host the signature was minted for instead of
|
|
278
|
+
* failing every request as `bad_signature`.
|
|
279
|
+
*/
|
|
403
280
|
declare const verifySignedUrl: (input: string | URL, secret: string, options?: {
|
|
404
281
|
expectedHost?: string;
|
|
405
282
|
}) => Promise<VerifyResult>;
|
|
406
|
-
export { type BucketStorage, type ListOptions, type LunoraStorageOptions, type ObjectMetadata, type PresignedUrlOptions, type PresignedUrlParams, type
|
|
283
|
+
export { type BucketStorage, type ListOptions, type LunoraStorageOptions, type ObjectMetadata, type PresignedUrlOptions, type PresignedUrlParams, type R2S3Credentials, type SignedUrlOptions, type Storage, type UploadOptions, type VerifyResult, buildPresignedUrl, buildSignedUrl, createBucketStorage, createStorage, scopeKey, verifySignedUrl };
|