lean-s3 0.2.2 → 0.3.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/dist/index.d.ts CHANGED
@@ -1,37 +1,343 @@
1
- import type { Readable } from "node:stream";
2
- export { default as S3File, type S3FileDeleteOptions, type S3FileExistsOptions, type S3StatOptions, } from "./S3File.ts";
3
- export { default as S3Client, type ListObjectsOptions, type ListObjectsIteratingOptions, type ListObjectsResponse, type CreateFileInstanceOptions, type OverridableS3ClientOptions, type S3ClientOptions, type S3FilePresignOptions, type BucketCreationOptions, type DeleteObjectsOptions, } from "./S3Client.ts";
4
- export { default as S3Error, type S3ErrorOptions } from "./S3Error.ts";
5
- export { default as S3Stat } from "./S3Stat.ts";
6
- export { default as S3BucketEntry } from "./S3BucketEntry.ts";
7
- export type Acl = "private" | "public-read" | "public-read-write" | "aws-exec-read" | "authenticated-read" | "bucket-owner-read" | "bucket-owner-full-control" | "log-delivery-write";
8
- export type StorageClass = "STANDARD" | "DEEP_ARCHIVE" | "EXPRESS_ONEZONE" | "GLACIER" | "GLACIER_IR" | "INTELLIGENT_TIERING" | "ONEZONE_IA" | "OUTPOSTS" | "REDUCED_REDUNDANCY" | "SNOW" | "STANDARD_IA";
9
- export type ChecksumAlgorithm = "CRC32" | "CRC32C" | "CRC64NVME" | "SHA1" | "SHA256";
10
- export type ChecksumType = "COMPOSITE" | "FULL_OBJECT";
11
- export type PresignableHttpMethod = "GET" | "DELETE" | "PUT" | "HEAD";
12
- export type HttpMethod = PresignableHttpMethod | "POST"; // There are also others, but we don't want to support them yet
1
+ import { Readable } from 'node:stream';
2
+ import * as stream_web from 'stream/web';
3
+ import { Dispatcher } from 'undici';
4
+
5
+ /**
6
+ * @internal Normally, we'd use an interface for that, but having a class with pre-defined fields makes it easier for V8 top optimize hidden classes.
7
+ */
8
+ declare class S3BucketEntry {
9
+ readonly key: string;
10
+ readonly size: number;
11
+ readonly lastModified: Date;
12
+ readonly etag: string;
13
+ readonly storageClass: StorageClass;
14
+ readonly checksumAlgorithm: ChecksumAlgorithm | undefined;
15
+ readonly checksumType: ChecksumType | undefined;
16
+ constructor(key: string, size: number, lastModified: Date, etag: string, storageClass: StorageClass, checksumAlgorithm: ChecksumAlgorithm | undefined, checksumType: ChecksumType | undefined);
17
+ /**
18
+ * @internal
19
+ */
20
+ static parse(source: any): S3BucketEntry;
21
+ }
22
+
23
+ declare const write: unique symbol;
24
+ declare const stream: unique symbol;
25
+ interface S3ClientOptions {
26
+ bucket: string;
27
+ region: string;
28
+ endpoint: string;
29
+ accessKeyId: string;
30
+ secretAccessKey: string;
31
+ sessionToken?: string;
32
+ }
33
+ type OverridableS3ClientOptions = Pick<S3ClientOptions, "region" | "bucket" | "endpoint">;
34
+ type CreateFileInstanceOptions = {};
35
+ type DeleteObjectsOptions = {
36
+ signal?: AbortSignal;
37
+ };
38
+ interface S3FilePresignOptions {
39
+ contentHash: Buffer;
40
+ /** Seconds. */
41
+ expiresIn: number;
42
+ method: PresignableHttpMethod;
43
+ storageClass: StorageClass;
44
+ acl: Acl;
45
+ }
46
+ type ListObjectsOptions = {
47
+ bucket?: string;
48
+ prefix?: string;
49
+ maxKeys?: number;
50
+ startAfter?: string;
51
+ continuationToken?: string;
52
+ signal?: AbortSignal;
53
+ };
54
+ type ListObjectsIteratingOptions = {
55
+ bucket?: string;
56
+ prefix?: string;
57
+ startAfter?: string;
58
+ signal?: AbortSignal;
59
+ internalPageSize?: number;
60
+ };
61
+ type ListObjectsResponse = {
62
+ name: string;
63
+ prefix: string | undefined;
64
+ startAfter: string | undefined;
65
+ isTruncated: boolean;
66
+ continuationToken: string | undefined;
67
+ maxKeys: number;
68
+ keyCount: number;
69
+ nextContinuationToken: string | undefined;
70
+ contents: readonly S3BucketEntry[];
71
+ };
72
+ type BucketCreationOptions = {
73
+ locationConstraint?: string;
74
+ location?: BucketLocationInfo;
75
+ info?: BucketInfo;
76
+ signal?: AbortSignal;
77
+ };
78
+ type BucketDeletionOptions = {
79
+ signal?: AbortSignal;
80
+ };
81
+ type BucketExistsOptions = {
82
+ signal?: AbortSignal;
83
+ };
84
+ /**
85
+ * A configured S3 bucket instance for managing files.
86
+ *
87
+ * @example
88
+ * ```js
89
+ * // Basic bucket setup
90
+ * const bucket = new S3Client({
91
+ * bucket: "my-bucket",
92
+ * accessKeyId: "key",
93
+ * secretAccessKey: "secret"
94
+ * });
95
+ * // Get file instance
96
+ * const file = bucket.file("image.jpg");
97
+ * await file.delete();
98
+ * ```
99
+ */
100
+ declare class S3Client {
101
+ #private;
102
+ /**
103
+ * Create a new instance of an S3 bucket so that credentials can be managed from a single instance instead of being passed to every method.
104
+ *
105
+ * @param options The default options to use for the S3 client.
106
+ */
107
+ constructor(options: S3ClientOptions);
108
+ /**
109
+ * Creates an S3File instance for the given path.
110
+ *
111
+ * @param {string} path The path to the object in the bucket. Also known as [object key](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html).
112
+ * We recommend not using the following characters in a key name because of significant special character handling, which isn't consistent across all applications (see [AWS docs](https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html)):
113
+ * - Backslash (`\\`)
114
+ * - Left brace (`{`)
115
+ * - Non-printable ASCII characters (128–255 decimal characters)
116
+ * - Caret or circumflex (`^`)
117
+ * - Right brace (`}`)
118
+ * - Percent character (`%`)
119
+ * - Grave accent or backtick (`\``)
120
+ * - Right bracket (`]`)
121
+ * - Quotation mark (`"`)
122
+ * - Greater than sign (`>`)
123
+ * - Left bracket (`[`)
124
+ * - Tilde (`~`)
125
+ * - Less than sign (`<`)
126
+ * - Pound sign (`#`)
127
+ * - Vertical bar or pipe (`|`)
128
+ *
129
+ * lean-s3 does not enforce these restrictions.
130
+ *
131
+ * @param {Partial<CreateFileInstanceOptions>} [options] TODO
132
+ * @example
133
+ * ```js
134
+ * const file = client.file("image.jpg");
135
+ * await file.write(imageData);
136
+ *
137
+ * const configFile = client.file("config.json", {
138
+ * type: "application/json",
139
+ * acl: "private"
140
+ * });
141
+ * ```
142
+ */
143
+ file(path: string, options?: Partial<CreateFileInstanceOptions>): S3File;
144
+ /**
145
+ * Generate a presigned URL for temporary access to a file.
146
+ * Useful for generating upload/download URLs without exposing credentials.
147
+ * @returns The operation on {@link S3Client#presign.path} as a pre-signed URL.
148
+ *
149
+ * @example
150
+ * ```js
151
+ * const downloadUrl = client.presign("file.pdf", {
152
+ * expiresIn: 3600 // 1 hour
153
+ * });
154
+ * ```
155
+ */
156
+ presign(path: string, { method, expiresIn, // TODO: Maybe rename this to expiresInSeconds
157
+ storageClass, acl, region: regionOverride, bucket: bucketOverride, endpoint: endpointOverride, }?: Partial<S3FilePresignOptions & OverridableS3ClientOptions>): string;
158
+ /**
159
+ * Uses [`DeleteObjects`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html) to delete multiple objects in a single request.
160
+ */
161
+ deleteObjects(objects: readonly S3BucketEntry[] | readonly string[], options?: DeleteObjectsOptions): Promise<{
162
+ errors: {
163
+ code: any;
164
+ key: any;
165
+ message: any;
166
+ versionId: any;
167
+ }[];
168
+ } | null>;
169
+ /**
170
+ * Creates a new bucket on the S3 server.
171
+ *
172
+ * @param name The name of the bucket to create. AWS the name according to [some rules](https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html). The most important ones are:
173
+ * - Bucket names must be between `3` (min) and `63` (max) characters long.
174
+ * - Bucket names can consist only of lowercase letters, numbers, periods (`.`), and hyphens (`-`).
175
+ * - Bucket names must begin and end with a letter or number.
176
+ * - Bucket names must not contain two adjacent periods.
177
+ * - Bucket names must not be formatted as an IP address (for example, `192.168.5.4`).
178
+ *
179
+ * @throws {Error} If the bucket name is invalid.
180
+ * @throws {S3Error} If the bucket could not be created, e.g. if it already exists.
181
+ * @remarks Uses [`CreateBucket`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html)
182
+ */
183
+ createBucket(name: string, options?: BucketCreationOptions): Promise<void>;
184
+ /**
185
+ * Deletes a bucket from the S3 server.
186
+ * @param name The name of the bucket to delete. Same restrictions as in {@link S3Client#createBucket}.
187
+ * @throws {Error} If the bucket name is invalid.
188
+ * @throws {S3Error} If the bucket could not be deleted, e.g. if it is not empty.
189
+ * @remarks Uses [`DeleteBucket`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteBucket.html).
190
+ */
191
+ deleteBucket(name: string, options?: BucketDeletionOptions): Promise<void>;
192
+ /**
193
+ * Checks if a bucket exists.
194
+ * @param name The name of the bucket to delete. Same restrictions as in {@link S3Client#createBucket}.
195
+ * @throws {Error} If the bucket name is invalid.
196
+ * @remarks Uses [`HeadBucket`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadBucket.html).
197
+ */
198
+ bucketExists(name: string, options?: BucketExistsOptions): Promise<boolean>;
199
+ /**
200
+ * Uses [`ListObjectsV2`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html) to iterate over all keys. Pagination and continuation is handled internally.
201
+ */
202
+ listIterating(options: ListObjectsIteratingOptions): AsyncGenerator<S3BucketEntry>;
203
+ /**
204
+ * Implements [`ListObjectsV2`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html) to iterate over all keys.
205
+ */
206
+ list(options?: ListObjectsOptions): Promise<ListObjectsResponse>;
207
+ /**
208
+ * Do not use this. This is an internal method.
209
+ * TODO: Maybe move this into a separate free function?
210
+ * @internal
211
+ */
212
+ _signedRequest(method: HttpMethod, pathWithoutBucket: string, query: string | undefined, body: UndiciBodyInit | undefined, additionalSignedHeaders: Record<string, string> | undefined, additionalUnsignedHeaders: Record<string, string> | undefined, contentHash: Buffer | undefined, bucket: string | undefined, signal?: AbortSignal | undefined): Promise<Dispatcher.ResponseData<null>>;
213
+ /**
214
+ * @internal
215
+ * @param {import("./index.d.ts").UndiciBodyInit} data TODO
216
+ */
217
+ [write](path: string, data: UndiciBodyInit, contentType: string, contentLength: number | undefined, contentHash: Buffer | undefined, rageStart: number | undefined, rangeEndExclusive: number | undefined, signal?: AbortSignal | undefined): Promise<void>;
218
+ /**
219
+ * @internal
220
+ */
221
+ [stream](path: string, contentHash: Buffer | undefined, rageStart: number | undefined, rangeEndExclusive: number | undefined): stream_web.ReadableStream<Uint8Array<ArrayBufferLike>>;
222
+ }
223
+
224
+ declare class S3Stat {
225
+ readonly etag: string;
226
+ readonly lastModified: Date;
227
+ readonly size: number;
228
+ readonly type: string;
229
+ constructor(etag: string, lastModified: Date, size: number, type: string);
230
+ static tryParseFromHeaders(headers: Record<string, string | string[] | undefined>): S3Stat | undefined;
231
+ }
232
+
233
+ declare class S3File {
234
+ #private;
235
+ /**
236
+ * @internal
237
+ */
238
+ constructor(client: S3Client, path: string, start: number | undefined, end: number | undefined, contentType: string | undefined);
239
+ slice(start?: number | undefined, end?: number | undefined, contentType?: string | undefined): S3File;
240
+ /**
241
+ * Get the stat of a file in the bucket. Uses `HEAD` request to check existence.
242
+ *
243
+ * @remarks Uses [`HeadObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html).
244
+ * @throws {S3Error} If the file does not exist or the server has some other issues.
245
+ * @throws {Error} If the server returns an invalid response.
246
+ */
247
+ stat({ signal }?: Partial<S3StatOptions>): Promise<S3Stat>;
248
+ /**
249
+ * Check if a file exists in the bucket. Uses `HEAD` request to check existence.
250
+ *
251
+ * @remarks Uses [`HeadObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html).
252
+ */
253
+ exists({ signal, }?: Partial<S3FileExistsOptions>): Promise<boolean>;
254
+ /**
255
+ * Delete a file from the bucket.
256
+ *
257
+ * @remarks - Uses [`DeleteObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html).
258
+ * - `versionId` not supported.
259
+ *
260
+ * @param {Partial<S3FileDeleteOptions>} [options]
261
+ *
262
+ * @example
263
+ * ```js
264
+ * // Simple delete
265
+ * await client.unlink("old-file.txt");
266
+ *
267
+ * // With error handling
268
+ * try {
269
+ * await client.unlink("file.dat");
270
+ * console.log("File deleted");
271
+ * } catch (err) {
272
+ * console.error("Delete failed:", err);
273
+ * }
274
+ * ```
275
+ */
276
+ delete({ signal }?: Partial<S3FileDeleteOptions>): Promise<void>;
277
+ toString(): string;
278
+ /** @returns {Promise<unknown>} */
279
+ json(): Promise<unknown>;
280
+ /** @returns {Promise<ArrayBuffer>} */
281
+ arrayBuffer(): Promise<ArrayBuffer>;
282
+ /** @returns {Promise<string>} */
283
+ text(): Promise<string>;
284
+ /** @returns {Promise<Blob>} */
285
+ blob(): Promise<Blob>;
286
+ /** @returns {ReadableStream<Uint8Array>} */
287
+ stream(): ReadableStream<Uint8Array>;
288
+ /**
289
+ * @param {ByteSource} data
290
+ * @returns {Promise<void>}
291
+ */
292
+ write(data: ByteSource): Promise<void>;
293
+ }
294
+ interface S3FileDeleteOptions extends OverridableS3ClientOptions {
295
+ signal: AbortSignal;
296
+ }
297
+ interface S3StatOptions extends OverridableS3ClientOptions {
298
+ signal: AbortSignal;
299
+ }
300
+ interface S3FileExistsOptions extends OverridableS3ClientOptions {
301
+ signal: AbortSignal;
302
+ }
303
+
304
+ declare class S3Error extends Error {
305
+ readonly code: string;
306
+ readonly path: string;
307
+ readonly message: string;
308
+ readonly requestId: string | undefined;
309
+ readonly hostId: string | undefined;
310
+ constructor(code: string, path: string, { message, requestId, hostId, cause, }?: S3ErrorOptions);
311
+ }
312
+ type S3ErrorOptions = {
313
+ message?: string | undefined;
314
+ requestId?: string | undefined;
315
+ hostId?: string | undefined;
316
+ cause?: unknown;
317
+ };
318
+
319
+ type Acl = "private" | "public-read" | "public-read-write" | "aws-exec-read" | "authenticated-read" | "bucket-owner-read" | "bucket-owner-full-control" | "log-delivery-write";
320
+ type StorageClass = "STANDARD" | "DEEP_ARCHIVE" | "EXPRESS_ONEZONE" | "GLACIER" | "GLACIER_IR" | "INTELLIGENT_TIERING" | "ONEZONE_IA" | "OUTPOSTS" | "REDUCED_REDUNDANCY" | "SNOW" | "STANDARD_IA";
321
+ type ChecksumAlgorithm = "CRC32" | "CRC32C" | "CRC64NVME" | "SHA1" | "SHA256";
322
+ type ChecksumType = "COMPOSITE" | "FULL_OBJECT";
323
+ type PresignableHttpMethod = "GET" | "DELETE" | "PUT" | "HEAD";
324
+ type HttpMethod = PresignableHttpMethod | "POST";
13
325
  /** Body values supported by undici. */
14
- export type UndiciBodyInit = string | Buffer | Uint8Array | Readable;
15
- export type ByteSource = UndiciBodyInit | Blob;
16
- // TODO
17
- // | ArrayBufferView
18
- // | ArrayBuffer
19
- // | SharedArrayBuffer
20
- // | Request
21
- // | Response
22
- // | S3File
23
- // | ReadableStream<Uint8Array>
326
+ type UndiciBodyInit = string | Buffer | Uint8Array | Readable;
327
+ type ByteSource = UndiciBodyInit | Blob;
24
328
  /**
25
329
  * Implements [LocationInfo](https://docs.aws.amazon.com/AmazonS3/latest/API/API_LocationInfo.html)
26
330
  */
27
- export type BucketLocationInfo = {
331
+ type BucketLocationInfo = {
28
332
  name?: string;
29
333
  type?: string;
30
334
  };
31
335
  /**
32
336
  * Implements [BucketInfo](https://docs.aws.amazon.com/AmazonS3/latest/API/API_BucketInfo.html)
33
337
  */
34
- export type BucketInfo = {
338
+ type BucketInfo = {
35
339
  dataRedundancy?: string;
36
340
  type?: string;
37
341
  };
342
+
343
+ export { type Acl, type BucketCreationOptions, type BucketDeletionOptions, type BucketExistsOptions, type BucketInfo, type BucketLocationInfo, type ByteSource, type ChecksumAlgorithm, type ChecksumType, type CreateFileInstanceOptions, type DeleteObjectsOptions, type HttpMethod, type ListObjectsIteratingOptions, type ListObjectsOptions, type ListObjectsResponse, type OverridableS3ClientOptions, type PresignableHttpMethod, S3BucketEntry, S3Client, type S3ClientOptions, S3Error, type S3ErrorOptions, S3File, type S3FileDeleteOptions, type S3FileExistsOptions, type S3FilePresignOptions, S3Stat, type S3StatOptions, type StorageClass, type UndiciBodyInit };