lean-s3 0.1.6 → 0.2.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/README.md +14 -2
- package/dist/AmzDate.d.ts +7 -0
- package/dist/AmzDate.js +29 -0
- package/dist/KeyCache.d.ts +5 -0
- package/dist/KeyCache.js +21 -0
- package/dist/S3BucketEntry.d.ts +18 -0
- package/dist/S3BucketEntry.js +29 -0
- package/dist/S3Client.d.ts +175 -0
- package/dist/S3Client.js +610 -0
- package/dist/S3Error.d.ts +14 -0
- package/dist/S3Error.js +15 -0
- package/dist/S3File.d.ts +72 -0
- package/dist/S3File.js +195 -0
- package/dist/S3Stat.d.ts +9 -0
- package/dist/S3Stat.js +35 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +5 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/sign.d.ts +16 -0
- package/dist/sign.js +77 -0
- package/dist/sign.test.d.ts +1 -0
- package/dist/test-common.d.ts +4 -0
- package/dist/test.integration.d.ts +1 -0
- package/dist/test.integration.js +19 -0
- package/dist/url.d.ts +9 -0
- package/dist/url.js +52 -0
- package/dist/url.test.d.ts +1 -0
- package/package.json +18 -14
- package/src/AmzDate.js +0 -56
- package/src/KeyCache.js +0 -36
- package/src/S3BucketEntry.js +0 -91
- package/src/S3Client.js +0 -800
- package/src/S3Error.js +0 -55
- package/src/S3File.js +0 -291
- package/src/S3Stat.js +0 -68
- package/src/index.d.ts +0 -105
- package/src/index.js +0 -4
- package/src/sign.js +0 -145
- package/src/url.js +0 -87
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ $ du -sh node_modules
|
|
|
77
77
|
`lean-s3` is _so_ lean that it is ~1.8MB just to do a couple of HTTP requests <img src="https://cdn.frankerfacez.com/emoticon/480839/1" width="20" height="20">
|
|
78
78
|
BUT...
|
|
79
79
|
|
|
80
|
-
Due to
|
|
80
|
+
Due to the scalability, portability and AWS integrations of @aws-sdk/client-s3, pre-signing URLs is `async` and performs poorly in high-performance scenarios. By taking different trade-offs, lean-s3 can presign URLs much faster. I promise! This is the reason you cannot use lean-s3 in the browser.
|
|
81
81
|
|
|
82
82
|
lean-s3 is currently about 30x faster than AWS SDK when it comes to pre-signing URLs[^1]:
|
|
83
83
|
```
|
|
@@ -121,6 +121,18 @@ We try to keep this library small. If you happen to need something that is not s
|
|
|
121
121
|
|
|
122
122
|
See [DESIGN_DECISIONS.md](./DESIGN_DECISIONS.md) to read about why this library is the way it is.
|
|
123
123
|
|
|
124
|
+
## Supported Operations
|
|
125
|
+
|
|
126
|
+
### Bucket Operations
|
|
127
|
+
- ✅ [`CreateBucket`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html) via `.createBucket`
|
|
128
|
+
|
|
129
|
+
### Object Operations
|
|
130
|
+
- ✅ [`ListObjectsV2`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html) via `.list`/`.listIterating`
|
|
131
|
+
- ✅ [`DeleteObjects`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html) via `.deleteObjects`
|
|
132
|
+
- ✅ [`DeleteObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObject.html) via `S3File.delete`
|
|
133
|
+
- ✅ [`PutObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutObject.html) via `S3File.write`
|
|
134
|
+
- ✅ [`HeadObject`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_HeadObject.html) via `S3File.exists`/`S3File.stat`
|
|
135
|
+
|
|
124
136
|
## Example Configurations
|
|
125
137
|
### Hetzner Object Storage
|
|
126
138
|
```js
|
|
@@ -159,4 +171,4 @@ const client = new S3Client({
|
|
|
159
171
|
Popular S3 provider missing? Open an issue or file a PR!
|
|
160
172
|
|
|
161
173
|
[^1]: Benchmark ran on a `13th Gen Intel(R) Core(TM) i7-1370P` using Node.js `23.11.0`. See `bench/` directory for the used benchmark.
|
|
162
|
-
[^2]: `git clone git@github.com:nikeee/lean-s3.git && cd lean-s3 && npm ci && cd bench && npm ci && npm start`
|
|
174
|
+
[^2]: `git clone git@github.com:nikeee/lean-s3.git && cd lean-s3 && npm ci && npm run build && cd bench && npm ci && npm start`
|
package/dist/AmzDate.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const ONE_DAY = 1000 * 60 * 60 * 24;
|
|
2
|
+
export function getAmzDate(dateTime) {
|
|
3
|
+
const date = pad4(dateTime.getUTCFullYear()) +
|
|
4
|
+
pad2(dateTime.getUTCMonth() + 1) +
|
|
5
|
+
pad2(dateTime.getUTCDate());
|
|
6
|
+
const time = pad2(dateTime.getUTCHours()) +
|
|
7
|
+
pad2(dateTime.getUTCMinutes()) +
|
|
8
|
+
pad2(dateTime.getUTCSeconds()); // it seems that we dont support milliseconds
|
|
9
|
+
return {
|
|
10
|
+
numericDayStart: (dateTime.getTime() / ONE_DAY) | 0,
|
|
11
|
+
date,
|
|
12
|
+
dateTime: `${date}T${time}Z`,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function now() {
|
|
16
|
+
return getAmzDate(new Date());
|
|
17
|
+
}
|
|
18
|
+
function pad4(v) {
|
|
19
|
+
return v < 10
|
|
20
|
+
? `000${v}`
|
|
21
|
+
: v < 100
|
|
22
|
+
? `00${v}`
|
|
23
|
+
: v < 1000
|
|
24
|
+
? `0${v}`
|
|
25
|
+
: v.toString();
|
|
26
|
+
}
|
|
27
|
+
function pad2(v) {
|
|
28
|
+
return v < 10 ? `0${v}` : v.toString();
|
|
29
|
+
}
|
package/dist/KeyCache.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import * as sign from "./sign.js";
|
|
2
|
+
export default class KeyCache {
|
|
3
|
+
#lastNumericDay = -1;
|
|
4
|
+
#keys = new Map();
|
|
5
|
+
computeIfAbsent(date, region, accessKeyId, secretAccessKey) {
|
|
6
|
+
if (date.numericDayStart !== this.#lastNumericDay) {
|
|
7
|
+
this.#keys.clear();
|
|
8
|
+
this.#lastNumericDay = date.numericDayStart;
|
|
9
|
+
// TODO: Add mechanism to clear the cache after some time
|
|
10
|
+
}
|
|
11
|
+
// using accessKeyId to prevent keeping the secretAccessKey somewhere
|
|
12
|
+
const cacheKey = `${date.date}:${region}:${accessKeyId}`;
|
|
13
|
+
const key = this.#keys.get(cacheKey);
|
|
14
|
+
if (key) {
|
|
15
|
+
return key;
|
|
16
|
+
}
|
|
17
|
+
const newKey = sign.deriveSigningKey(date.date, region, secretAccessKey);
|
|
18
|
+
this.#keys.set(cacheKey, newKey);
|
|
19
|
+
return newKey;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ChecksumType, ChecksumAlgorithm, StorageClass } from "./index.ts";
|
|
2
|
+
/**
|
|
3
|
+
* @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.
|
|
4
|
+
*/
|
|
5
|
+
export default class S3BucketEntry {
|
|
6
|
+
readonly key: string;
|
|
7
|
+
readonly size: number;
|
|
8
|
+
readonly lastModified: Date;
|
|
9
|
+
readonly etag: string;
|
|
10
|
+
readonly storageClass: StorageClass;
|
|
11
|
+
readonly checksumAlgorithm: ChecksumAlgorithm | undefined;
|
|
12
|
+
readonly checksumType: ChecksumType | undefined;
|
|
13
|
+
constructor(key: string, size: number, lastModified: Date, etag: string, storageClass: StorageClass, checksumAlgorithm: ChecksumAlgorithm | undefined, checksumType: ChecksumType | undefined);
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
static parse(source: any): S3BucketEntry;
|
|
18
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @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.
|
|
3
|
+
*/
|
|
4
|
+
export default class S3BucketEntry {
|
|
5
|
+
key;
|
|
6
|
+
size;
|
|
7
|
+
lastModified;
|
|
8
|
+
etag;
|
|
9
|
+
storageClass;
|
|
10
|
+
checksumAlgorithm;
|
|
11
|
+
checksumType;
|
|
12
|
+
constructor(key, size, lastModified, etag, storageClass, checksumAlgorithm, checksumType) {
|
|
13
|
+
this.key = key;
|
|
14
|
+
this.size = size;
|
|
15
|
+
this.lastModified = lastModified;
|
|
16
|
+
this.etag = etag;
|
|
17
|
+
this.storageClass = storageClass;
|
|
18
|
+
this.checksumAlgorithm = checksumAlgorithm;
|
|
19
|
+
this.checksumType = checksumType;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
// biome-ignore lint/suspicious/noExplicitAny: internal use only, any is ok here
|
|
25
|
+
static parse(source) {
|
|
26
|
+
// TODO: check values and throw exceptions
|
|
27
|
+
return new S3BucketEntry(source.Key, source.Size, new Date(source.LastModified), source.ETag, source.StorageClass, source.ChecksumAlgorithm, source.ChecksumType);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import S3File from "./S3File.ts";
|
|
2
|
+
import S3BucketEntry from "./S3BucketEntry.ts";
|
|
3
|
+
import * as amzDate from "./AmzDate.ts";
|
|
4
|
+
import type { Acl, BucketInfo, BucketLocationInfo, PresignableHttpMethod, StorageClass, UndiciBodyInit } from "./index.ts";
|
|
5
|
+
export declare const write: unique symbol;
|
|
6
|
+
export declare const stream: unique symbol;
|
|
7
|
+
export interface S3ClientOptions {
|
|
8
|
+
bucket: string;
|
|
9
|
+
region: string;
|
|
10
|
+
endpoint: string;
|
|
11
|
+
accessKeyId: string;
|
|
12
|
+
secretAccessKey: string;
|
|
13
|
+
sessionToken?: string;
|
|
14
|
+
}
|
|
15
|
+
export type OverridableS3ClientOptions = Pick<S3ClientOptions, "region" | "bucket" | "endpoint">;
|
|
16
|
+
export type CreateFileInstanceOptions = {};
|
|
17
|
+
export type DeleteObjectsOptions = {
|
|
18
|
+
signal?: AbortSignal;
|
|
19
|
+
};
|
|
20
|
+
export interface S3FilePresignOptions {
|
|
21
|
+
contentHash: Buffer;
|
|
22
|
+
/** Seconds. */
|
|
23
|
+
expiresIn: number;
|
|
24
|
+
method: PresignableHttpMethod;
|
|
25
|
+
storageClass: StorageClass;
|
|
26
|
+
acl: Acl;
|
|
27
|
+
}
|
|
28
|
+
export type ListObjectsOptions = {
|
|
29
|
+
prefix?: string;
|
|
30
|
+
maxKeys?: number;
|
|
31
|
+
startAfter?: string;
|
|
32
|
+
continuationToken?: string;
|
|
33
|
+
signal?: AbortSignal;
|
|
34
|
+
};
|
|
35
|
+
export type ListObjectsIteratingOptions = {
|
|
36
|
+
prefix?: string;
|
|
37
|
+
startAfter?: string;
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
internalPageSize?: number;
|
|
40
|
+
};
|
|
41
|
+
export type ListObjectsResponse = {
|
|
42
|
+
name: string;
|
|
43
|
+
prefix: string | undefined;
|
|
44
|
+
startAfter: string | undefined;
|
|
45
|
+
isTruncated: boolean;
|
|
46
|
+
continuationToken: string | undefined;
|
|
47
|
+
maxKeys: number;
|
|
48
|
+
keyCount: number;
|
|
49
|
+
nextContinuationToken: string | undefined;
|
|
50
|
+
contents: readonly S3BucketEntry[];
|
|
51
|
+
};
|
|
52
|
+
export type BucketCreationOptions = {
|
|
53
|
+
locationConstraint?: string;
|
|
54
|
+
location?: BucketLocationInfo;
|
|
55
|
+
info?: BucketInfo;
|
|
56
|
+
signal?: AbortSignal;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* A configured S3 bucket instance for managing files.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```js
|
|
63
|
+
* // Basic bucket setup
|
|
64
|
+
* const bucket = new S3Client({
|
|
65
|
+
* bucket: "my-bucket",
|
|
66
|
+
* accessKeyId: "key",
|
|
67
|
+
* secretAccessKey: "secret"
|
|
68
|
+
* });
|
|
69
|
+
* // Get file instance
|
|
70
|
+
* const file = bucket.file("image.jpg");
|
|
71
|
+
* await file.delete();
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export default class S3Client {
|
|
75
|
+
#private;
|
|
76
|
+
/**
|
|
77
|
+
* 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.
|
|
78
|
+
*
|
|
79
|
+
* @param options The default options to use for the S3 client.
|
|
80
|
+
*/
|
|
81
|
+
constructor(options: S3ClientOptions);
|
|
82
|
+
/**
|
|
83
|
+
* Creates an S3File instance for the given path.
|
|
84
|
+
*
|
|
85
|
+
* @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).
|
|
86
|
+
* 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)):
|
|
87
|
+
* - Backslash (`\\`)
|
|
88
|
+
* - Left brace (`{`)
|
|
89
|
+
* - Non-printable ASCII characters (128–255 decimal characters)
|
|
90
|
+
* - Caret or circumflex (`^`)
|
|
91
|
+
* - Right brace (`}`)
|
|
92
|
+
* - Percent character (`%`)
|
|
93
|
+
* - Grave accent or backtick (`\``)
|
|
94
|
+
* - Right bracket (`]`)
|
|
95
|
+
* - Quotation mark (`"`)
|
|
96
|
+
* - Greater than sign (`>`)
|
|
97
|
+
* - Left bracket (`[`)
|
|
98
|
+
* - Tilde (`~`)
|
|
99
|
+
* - Less than sign (`<`)
|
|
100
|
+
* - Pound sign (`#`)
|
|
101
|
+
* - Vertical bar or pipe (`|`)
|
|
102
|
+
*
|
|
103
|
+
* lean-s3 does not enforce these restrictions.
|
|
104
|
+
*
|
|
105
|
+
* @param {Partial<CreateFileInstanceOptions>} [options] TODO
|
|
106
|
+
* @example
|
|
107
|
+
* ```js
|
|
108
|
+
* const file = client.file("image.jpg");
|
|
109
|
+
* await file.write(imageData);
|
|
110
|
+
*
|
|
111
|
+
* const configFile = client.file("config.json", {
|
|
112
|
+
* type: "application/json",
|
|
113
|
+
* acl: "private"
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
file(path: string, options?: Partial<CreateFileInstanceOptions>): S3File;
|
|
118
|
+
/**
|
|
119
|
+
* Generate a presigned URL for temporary access to a file.
|
|
120
|
+
* Useful for generating upload/download URLs without exposing credentials.
|
|
121
|
+
* @returns The operation on {@link S3Client#presign.path} as a pre-signed URL.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```js
|
|
125
|
+
* const downloadUrl = client.presign("file.pdf", {
|
|
126
|
+
* expiresIn: 3600 // 1 hour
|
|
127
|
+
* });
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
presign(path: string, { method, expiresIn, // TODO: Maybe rename this to expiresInSeconds
|
|
131
|
+
storageClass, acl, region: regionOverride, bucket: bucketOverride, endpoint: endpointOverride, }?: Partial<S3FilePresignOptions & OverridableS3ClientOptions>): string;
|
|
132
|
+
/**
|
|
133
|
+
* Uses [`DeleteObjects`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html) to delete multiple objects in a single request.
|
|
134
|
+
*/
|
|
135
|
+
deleteObjects(objects: readonly S3BucketEntry[] | readonly string[], options?: DeleteObjectsOptions): Promise<{
|
|
136
|
+
errors: {
|
|
137
|
+
code: any;
|
|
138
|
+
key: any;
|
|
139
|
+
message: any;
|
|
140
|
+
versionId: any;
|
|
141
|
+
}[];
|
|
142
|
+
} | null>;
|
|
143
|
+
/**
|
|
144
|
+
* Creates a new bucket on the S3 server.
|
|
145
|
+
*
|
|
146
|
+
* @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:
|
|
147
|
+
* - Bucket names must be between `3` (min) and `63` (max) characters long.
|
|
148
|
+
* - Bucket names can consist only of lowercase letters, numbers, periods (`.`), and hyphens (`-`).
|
|
149
|
+
* - Bucket names must begin and end with a letter or number.
|
|
150
|
+
* - Bucket names must not contain two adjacent periods.
|
|
151
|
+
* - Bucket names must not be formatted as an IP address (for example, `192.168.5.4`).
|
|
152
|
+
*
|
|
153
|
+
* @throws {Error} If the bucket name is invalid.
|
|
154
|
+
* @remarks Uses [`CreateBucket`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html)
|
|
155
|
+
*/
|
|
156
|
+
createBucket(name: string, options?: BucketCreationOptions): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Uses [`ListObjectsV2`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html) to iterate over all keys. Pagination and continuation is handled internally.
|
|
159
|
+
*/
|
|
160
|
+
listIterating(options: ListObjectsIteratingOptions): AsyncGenerator<S3BucketEntry>;
|
|
161
|
+
/**
|
|
162
|
+
* Implements [`ListObjectsV2`](https://docs.aws.amazon.com/AmazonS3/latest/API/API_ListObjectsV2.html) to iterate over all keys.
|
|
163
|
+
*/
|
|
164
|
+
list(options?: ListObjectsOptions): Promise<ListObjectsResponse>;
|
|
165
|
+
/**
|
|
166
|
+
* @internal
|
|
167
|
+
* @param {import("./index.d.ts").UndiciBodyInit} data TODO
|
|
168
|
+
*/
|
|
169
|
+
[write](path: string, data: UndiciBodyInit, contentType: string, contentLength: number | undefined, contentHash: Buffer | undefined, rageStart: number | undefined, rangeEndExclusive: number | undefined, signal?: AbortSignal | undefined): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* @internal
|
|
172
|
+
*/
|
|
173
|
+
[stream](path: string, contentHash: Buffer | undefined, rageStart: number | undefined, rangeEndExclusive: number | undefined): import("stream/web").ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
174
|
+
}
|
|
175
|
+
export declare function buildSearchParams(amzCredential: string, date: amzDate.AmzDate, expiresIn: number, headerList: string, contentHashStr: string | null | undefined, storageClass: StorageClass | null | undefined, sessionToken: string | null | undefined, acl: Acl | null | undefined): string;
|