@geekmidas/storage 10.0.0-alpha.13 → 10.0.0-alpha.130
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 +49 -13
- package/dist/aws.cjs +88 -10
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +5 -2
- package/dist/aws.d.cts.map +1 -1
- package/dist/aws.d.mts +5 -2
- package/dist/aws.d.mts.map +1 -1
- package/dist/aws.mjs +88 -10
- package/dist/aws.mjs.map +1 -1
- package/dist/{errors-Bexjg-j-.cjs → errors-BB8jdHif.cjs} +52 -3
- package/dist/errors-BB8jdHif.cjs.map +1 -0
- package/dist/{errors-CpYcpJCY.mjs → errors-COXs1iNg.mjs} +41 -4
- package/dist/errors-COXs1iNg.mjs.map +1 -0
- package/dist/events.cjs +98 -0
- package/dist/events.cjs.map +1 -0
- package/dist/events.d.cts +58 -0
- package/dist/events.d.cts.map +1 -0
- package/dist/events.d.mts +58 -0
- package/dist/events.d.mts.map +1 -0
- package/dist/events.mjs +94 -0
- package/dist/events.mjs.map +1 -0
- package/dist/index.cjs +3 -1
- package/dist/index.d.cts +27 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +27 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/{registry-ClEvHGpM.d.cts → registry-DyBOtv6n.d.cts} +44 -2
- package/dist/registry-DyBOtv6n.d.cts.map +1 -0
- package/dist/{registry-ClEvHGpM.d.mts → registry-DyBOtv6n.d.mts} +44 -2
- package/dist/registry-DyBOtv6n.d.mts.map +1 -0
- package/dist/s3Url.cjs +37 -8
- package/dist/s3Url.cjs.map +1 -1
- package/dist/s3Url.d.cts +20 -6
- package/dist/s3Url.d.cts.map +1 -1
- package/dist/s3Url.d.mts +20 -6
- package/dist/s3Url.d.mts.map +1 -1
- package/dist/s3Url.mjs +37 -8
- package/dist/s3Url.mjs.map +1 -1
- package/package.json +20 -5
- package/dist/errors-Bexjg-j-.cjs.map +0 -1
- package/dist/errors-CpYcpJCY.mjs.map +0 -1
- package/dist/registry-ClEvHGpM.d.cts.map +0 -1
- package/dist/registry-ClEvHGpM.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -85,6 +85,49 @@ const storage = AmazonStorageClient.create({
|
|
|
85
85
|
});
|
|
86
86
|
```
|
|
87
87
|
|
|
88
|
+
## Storage URLs and credentials
|
|
89
|
+
|
|
90
|
+
A declared bucket reaches an app as one URL, `<ID>_URL`, and
|
|
91
|
+
`createStorageClient(url)` picks the driver by its scheme. An `s3://` URL
|
|
92
|
+
names the bucket as its host and the rest as query parameters:
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
s3://uploads?region=eu-west-1
|
|
96
|
+
s3://uploads?region=auto&endpoint=https://<account>.r2.cloudflarestorage.com
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Credentials are optional, and come from one of two places:
|
|
100
|
+
|
|
101
|
+
- **The URL's userinfo** — `s3://KEY:SECRET@uploads?region=eu-west-1`. A key
|
|
102
|
+
here signs for this bucket only and wins over anything in the environment.
|
|
103
|
+
Percent-encode both halves: AWS secrets often contain `/` (`%2F`) and `+`
|
|
104
|
+
(`%2B`). `s3Url.build` does this for you, and `s3Url.parse` decodes them.
|
|
105
|
+
- **The SDK's default chain** — for a URL with no userinfo: the shared
|
|
106
|
+
`AWS_ACCESS_KEY_ID`/`AWS_SECRET_ACCESS_KEY`, a profile, or an execution role.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
import { s3Url } from '@geekmidas/storage/aws';
|
|
110
|
+
|
|
111
|
+
s3Url.build({
|
|
112
|
+
bucket: 'uploads',
|
|
113
|
+
region: 'eu-west-1',
|
|
114
|
+
accessKeyId: 'AKIA…',
|
|
115
|
+
secretAccessKey: 'wJal/rXUt+nFEMI…',
|
|
116
|
+
});
|
|
117
|
+
// 's3://AKIA…:wJal%2FrXUt%2BnFEMI…@uploads?region=eu-west-1'
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Prefer a key per bucket, scoped to that bucket, over one shared pair: an app
|
|
121
|
+
that reads two buckets then holds two narrow keys rather than one that opens
|
|
122
|
+
both. A URL with only one half of a pair (`s3://KEY@uploads`) is refused with
|
|
123
|
+
`IncompleteStorageCredentials` — it is never completed from the environment.
|
|
124
|
+
|
|
125
|
+
Every storage error that carries a URL (`MalformedStorageUrl`,
|
|
126
|
+
`UnexpectedStorageScheme`, `MissingStorageBucket`,
|
|
127
|
+
`UnregisteredStorageScheme`, `IncompleteStorageCredentials`) holds it with the
|
|
128
|
+
userinfo replaced by `REDACTED`, and its message never includes the URL.
|
|
129
|
+
`redactStorageUrl(url)` does the same for your own log lines.
|
|
130
|
+
|
|
88
131
|
## API Reference
|
|
89
132
|
|
|
90
133
|
### StorageClient Interface
|
|
@@ -400,20 +443,13 @@ npm run test:once
|
|
|
400
443
|
|
|
401
444
|
### Local Development with MinIO
|
|
402
445
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
```
|
|
407
|
-
|
|
408
|
-
2. MinIO will be available at:
|
|
409
|
-
- API: http://localhost:9000
|
|
410
|
-
- Console: http://localhost:9001
|
|
411
|
-
- Credentials: minioadmin/minioadmin
|
|
446
|
+
The suite starts the repo compose stack's MinIO itself (`geekmidas` /
|
|
447
|
+
`geekmidas`, with a `geekmidas` bucket). It publishes on port 9000; when
|
|
448
|
+
another project holds that port, move it:
|
|
412
449
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
```
|
|
450
|
+
```bash
|
|
451
|
+
MINIO_API_HOST_PORT=29000 MINIO_CONSOLE_HOST_PORT=29001 pnpm vitest run --project storage
|
|
452
|
+
```
|
|
417
453
|
|
|
418
454
|
### Project Structure
|
|
419
455
|
|
package/dist/aws.cjs
CHANGED
|
@@ -3,6 +3,37 @@ const require_s3Url = require("./s3Url.cjs");
|
|
|
3
3
|
let _aws_sdk_client_s3 = require("@aws-sdk/client-s3");
|
|
4
4
|
let _aws_sdk_s3_presigned_post = require("@aws-sdk/s3-presigned-post");
|
|
5
5
|
let _aws_sdk_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
|
|
6
|
+
let _opentelemetry_api = require("@opentelemetry/api");
|
|
7
|
+
//#region src/telemetry.ts
|
|
8
|
+
/**
|
|
9
|
+
* A span per storage call, through the global OpenTelemetry tracer — a no-op
|
|
10
|
+
* without a registered provider.
|
|
11
|
+
*
|
|
12
|
+
* `storage.presign`, `storage.put`, `storage.delete` and `storage.list`, with
|
|
13
|
+
* the backend and the bucket. Never the object's key: keys often name a user.
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
/** Run `fn` inside a `storage.<operation>` span. */
|
|
18
|
+
async function traceStorage(operation, attributes, fn) {
|
|
19
|
+
const span = _opentelemetry_api.trace.getTracer("@geekmidas/storage").startSpan(`storage.${operation}`, {
|
|
20
|
+
kind: _opentelemetry_api.SpanKind.CLIENT,
|
|
21
|
+
attributes: {
|
|
22
|
+
"storage.operation": operation,
|
|
23
|
+
...attributes
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
try {
|
|
27
|
+
return await fn();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error instanceof Error) span.recordException(error);
|
|
30
|
+
span.setStatus({ code: _opentelemetry_api.SpanStatusCode.ERROR });
|
|
31
|
+
throw error;
|
|
32
|
+
} finally {
|
|
33
|
+
span.end();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
6
37
|
//#region src/AmazonStorageClient.ts
|
|
7
38
|
var AmazonStorageClient = class AmazonStorageClient {
|
|
8
39
|
client;
|
|
@@ -29,6 +60,14 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
29
60
|
this.acl = acl;
|
|
30
61
|
this.cache = cache;
|
|
31
62
|
}
|
|
63
|
+
/** A storage span on this bucket. */
|
|
64
|
+
traced(operation, fn, attributes = {}) {
|
|
65
|
+
return traceStorage(operation, {
|
|
66
|
+
"storage.system": "s3",
|
|
67
|
+
"storage.bucket": this.bucket,
|
|
68
|
+
...attributes
|
|
69
|
+
}, fn);
|
|
70
|
+
}
|
|
32
71
|
createGetObjectCommand(file, overrides) {
|
|
33
72
|
const disposition = file.disposition ?? (file.name ? "attachment" : void 0);
|
|
34
73
|
const ResponseContentDisposition = disposition ? file.name ? `${disposition}; filename=${encodeURIComponent(file.name)}` : disposition : void 0;
|
|
@@ -42,14 +81,14 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
42
81
|
}
|
|
43
82
|
getVersionDownloadURL(file, versionId) {
|
|
44
83
|
const command = this.createGetObjectCommand(file, { VersionId: versionId });
|
|
45
|
-
return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: 86400 });
|
|
84
|
+
return this.traced("presign", () => (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: 86400 }), { "storage.presign.method": "GET" });
|
|
46
85
|
}
|
|
47
86
|
async getVersions(key) {
|
|
48
87
|
const command = new _aws_sdk_client_s3.ListObjectVersionsCommand({
|
|
49
88
|
Bucket: this.bucket,
|
|
50
89
|
Prefix: key
|
|
51
90
|
});
|
|
52
|
-
const { Versions = [] } = await this.client.send(command);
|
|
91
|
+
const { Versions = [] } = await this.traced("list", () => this.client.send(command));
|
|
53
92
|
return Versions.map((version) => ({
|
|
54
93
|
id: version.VersionId || "",
|
|
55
94
|
createdAt: version.LastModified || /* @__PURE__ */ new Date()
|
|
@@ -60,7 +99,7 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
60
99
|
const cachedURL = await this.cache?.get(cacheKey);
|
|
61
100
|
if (cachedURL) return cachedURL;
|
|
62
101
|
const command = this.createGetObjectCommand(file);
|
|
63
|
-
const url = await (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn });
|
|
102
|
+
const url = await this.traced("presign", () => (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn }), { "storage.presign.method": "GET" });
|
|
64
103
|
const cacheExpiration = Math.max(expiresIn - 60, 0);
|
|
65
104
|
if (cacheExpiration) await this.cache?.set(cacheKey, url, cacheExpiration);
|
|
66
105
|
return url;
|
|
@@ -72,17 +111,17 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
72
111
|
ContentType: params.contentType,
|
|
73
112
|
ContentLength: params.contentLength
|
|
74
113
|
});
|
|
75
|
-
return (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn });
|
|
114
|
+
return this.traced("presign", () => (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn }), { "storage.presign.method": "PUT" });
|
|
76
115
|
}
|
|
77
116
|
async getUpload(params, expiresIn = 5) {
|
|
78
117
|
const { path } = params;
|
|
79
|
-
const { fields: values, url } = await (0, _aws_sdk_s3_presigned_post.createPresignedPost)(this.client, {
|
|
118
|
+
const { fields: values, url } = await this.traced("presign", () => (0, _aws_sdk_s3_presigned_post.createPresignedPost)(this.client, {
|
|
80
119
|
Expires: expiresIn * 60,
|
|
81
120
|
Bucket: this.bucket,
|
|
82
121
|
Fields: { acl: this.acl },
|
|
83
122
|
Conditions: [],
|
|
84
123
|
Key: path
|
|
85
|
-
});
|
|
124
|
+
}), { "storage.presign.method": "POST" });
|
|
86
125
|
return {
|
|
87
126
|
url,
|
|
88
127
|
fields: Object.keys(values).map((key) => ({
|
|
@@ -91,6 +130,40 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
91
130
|
}))
|
|
92
131
|
};
|
|
93
132
|
}
|
|
133
|
+
async getUploadPost({ key, maxSize, contentType, expiresIn = 300 }) {
|
|
134
|
+
const fields = {};
|
|
135
|
+
const conditions = [[
|
|
136
|
+
"content-length-range",
|
|
137
|
+
0,
|
|
138
|
+
maxSize
|
|
139
|
+
]];
|
|
140
|
+
if (typeof contentType === "string") {
|
|
141
|
+
fields["Content-Type"] = contentType;
|
|
142
|
+
conditions.push([
|
|
143
|
+
"eq",
|
|
144
|
+
"$Content-Type",
|
|
145
|
+
contentType
|
|
146
|
+
]);
|
|
147
|
+
} else if (contentType) conditions.push([
|
|
148
|
+
"starts-with",
|
|
149
|
+
"$Content-Type",
|
|
150
|
+
contentType.startsWith
|
|
151
|
+
]);
|
|
152
|
+
const expiresAt = new Date(Date.now() + expiresIn * 1e3).toISOString();
|
|
153
|
+
const post = await this.traced("presign", () => (0, _aws_sdk_s3_presigned_post.createPresignedPost)(this.client, {
|
|
154
|
+
Bucket: this.bucket,
|
|
155
|
+
Key: key,
|
|
156
|
+
Expires: expiresIn,
|
|
157
|
+
Fields: fields,
|
|
158
|
+
Conditions: conditions
|
|
159
|
+
}), { "storage.presign.method": "POST" });
|
|
160
|
+
return {
|
|
161
|
+
url: post.url,
|
|
162
|
+
fields: post.fields,
|
|
163
|
+
key,
|
|
164
|
+
expiresAt
|
|
165
|
+
};
|
|
166
|
+
}
|
|
94
167
|
async upload(key, data, contentType) {
|
|
95
168
|
const Body = typeof data === "string" ? Buffer.from(data, "base64") : data;
|
|
96
169
|
const params = {
|
|
@@ -100,14 +173,17 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
100
173
|
ContentType: contentType
|
|
101
174
|
};
|
|
102
175
|
const command = new _aws_sdk_client_s3.PutObjectCommand(params);
|
|
103
|
-
await this.client.send(command)
|
|
176
|
+
await this.traced("put", () => this.client.send(command), {
|
|
177
|
+
"storage.object.size": Body.length,
|
|
178
|
+
"storage.object.content_type": contentType
|
|
179
|
+
});
|
|
104
180
|
}
|
|
105
181
|
async delete(key) {
|
|
106
182
|
const command = new _aws_sdk_client_s3.DeleteObjectCommand({
|
|
107
183
|
Bucket: this.bucket,
|
|
108
184
|
Key: key
|
|
109
185
|
});
|
|
110
|
-
await this.client.send(command);
|
|
186
|
+
await this.traced("delete", () => this.client.send(command));
|
|
111
187
|
}
|
|
112
188
|
};
|
|
113
189
|
let AmazonCannedAccessControlList = /* @__PURE__ */ function(AmazonCannedAccessControlList) {
|
|
@@ -132,12 +208,14 @@ let AmazonCannedAccessControlList = /* @__PURE__ */ function(AmazonCannedAccessC
|
|
|
132
208
|
const s3Driver = {
|
|
133
209
|
scheme: "s3:",
|
|
134
210
|
create(url) {
|
|
135
|
-
const { bucket, region, endpoint, forcePathStyle } = require_s3Url.parse(url);
|
|
211
|
+
const { bucket, region, endpoint, forcePathStyle, accessKeyId, secretAccessKey } = require_s3Url.parse(url);
|
|
136
212
|
return AmazonStorageClient.create({
|
|
137
213
|
bucket,
|
|
138
214
|
region,
|
|
139
215
|
endpoint,
|
|
140
|
-
forcePathStyle
|
|
216
|
+
forcePathStyle,
|
|
217
|
+
accessKeyId,
|
|
218
|
+
secretAccessKey
|
|
141
219
|
});
|
|
142
220
|
}
|
|
143
221
|
};
|
package/dist/aws.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aws.cjs","names":["S3Client","GetObjectCommand","getSignedUrl","ListObjectVersionsCommand","PutObjectCommand","createPresignedPost","DeleteObjectCommand","parse"],"sources":["../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"sourcesContent":["import {\n\tDeleteObjectCommand,\n\tGetObjectCommand,\n\tListObjectVersionsCommand,\n\tPutObjectCommand,\n\tS3Client,\n} from '@aws-sdk/client-s3';\nimport { createPresignedPost } from '@aws-sdk/s3-presigned-post';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\n\nimport type { Cache } from '@geekmidas/cache';\nimport {\n\ttype DocumentVersion,\n\ttype File,\n\ttype GetUploadParams,\n\ttype GetUploadResponse,\n\ttype StorageClient,\n\tStorageProvider,\n} from './StorageClient';\n\nexport class AmazonStorageClient implements StorageClient {\n\treadonly provider = StorageProvider.AWSS3;\n\n\tstatic create(\n\t\toptions: AmazonStorageClientCreateOptions,\n\t): AmazonStorageClient {\n\t\tconst {\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\taccessKeyId,\n\t\t\tacl,\n\t\t\tendpoint,\n\t\t\tsecretAccessKey,\n\t\t\tforcePathStyle = false,\n\t\t\tcache,\n\t\t} = options;\n\t\tconst hasCredentials = accessKeyId && secretAccessKey;\n\t\tconst credentials = hasCredentials\n\t\t\t? { accessKeyId, secretAccessKey }\n\t\t\t: undefined;\n\n\t\tconst client = new S3Client({\n\t\t\tregion,\n\t\t\tcredentials,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t});\n\n\t\treturn new AmazonStorageClient(client, bucket, acl, cache);\n\t}\n\tconstructor(\n\t\tprivate readonly client: S3Client,\n\t\tprivate readonly bucket: string,\n\t\tprivate readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,\n\t\treadonly cache?: Cache,\n\t) {}\n\n\tprivate createGetObjectCommand(\n\t\tfile: File,\n\t\toverrides?: { VersionId?: string },\n\t): GetObjectCommand {\n\t\tconst disposition =\n\t\t\tfile.disposition ?? (file.name ? 'attachment' : undefined);\n\t\tconst ResponseContentDisposition = disposition\n\t\t\t? file.name\n\t\t\t\t? `${disposition}; filename=${encodeURIComponent(file.name)}`\n\t\t\t\t: disposition\n\t\t\t: undefined;\n\n\t\treturn new GetObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: file.path,\n\t\t\tResponseContentDisposition,\n\t\t\tResponseContentType: file.responseContentType,\n\t\t\t...overrides,\n\t\t});\n\t}\n\n\tgetVersionDownloadURL(file: File, versionId: string): Promise<string> {\n\t\tconst command = this.createGetObjectCommand(file, { VersionId: versionId });\n\t\treturn getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 });\n\t}\n\n\tasync getVersions(key: string): Promise<DocumentVersion[]> {\n\t\tconst command = new ListObjectVersionsCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tPrefix: key,\n\t\t});\n\n\t\tconst { Versions = [] } = await this.client.send(command);\n\n\t\treturn Versions.map((version) => ({\n\t\t\tid: version.VersionId || '',\n\t\t\tcreatedAt: version.LastModified || new Date(),\n\t\t}));\n\t}\n\n\tasync getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {\n\t\tconst cacheKey = `download-url:${file.path}`;\n\t\tconst cachedURL = await this.cache?.get<string>(cacheKey);\n\n\t\tif (cachedURL) {\n\t\t\treturn cachedURL;\n\t\t}\n\n\t\tconst command = this.createGetObjectCommand(file);\n\t\tconst url = await getSignedUrl(this.client, command, { expiresIn });\n\t\tconst cacheExpiration = Math.max(expiresIn - 60, 0);\n\n\t\tif (cacheExpiration) {\n\t\t\tawait this.cache?.set(cacheKey, url, cacheExpiration);\n\t\t}\n\n\t\treturn url;\n\t}\n\n\tasync getUploadURL(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 60 * 60,\n\t): Promise<string> {\n\t\tconst command = new PutObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: params.path,\n\t\t\tContentType: params.contentType,\n\t\t\tContentLength: params.contentLength,\n\t\t});\n\n\t\treturn getSignedUrl(this.client, command, { expiresIn });\n\t}\n\n\tasync getUpload(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 5,\n\t): Promise<GetUploadResponse> {\n\t\tconst { path } = params;\n\t\tconst { fields: values, url } = await createPresignedPost(this.client, {\n\t\t\tExpires: expiresIn * 60,\n\t\t\tBucket: this.bucket,\n\t\t\tFields: {\n\t\t\t\tacl: this.acl,\n\t\t\t},\n\t\t\tConditions: [\n\t\t\t\t// content length restrictions: 0-1MB]\n\t\t\t\t// ['content-length-range', 0, contentLength],\n\t\t\t\t// specify content-type to be more generic- images only\n\t\t\t\t// ['starts-with', '$Content-Type', 'image/'],\n\t\t\t\t// ['starts-with', '$Content-Type', contentType],\n\t\t\t],\n\t\t\tKey: path,\n\t\t});\n\n\t\tconst keys = Object.keys(values);\n\t\tconst fields = keys.map((key) => ({ key, value: values[key] || '' }));\n\n\t\treturn { url, fields };\n\t}\n\n\tasync upload(\n\t\tkey: string,\n\t\tdata: string | Buffer,\n\t\tcontentType: string,\n\t): Promise<void> {\n\t\tconst Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;\n\n\t\tconst params = {\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t\tBody,\n\t\t\tContentType: contentType,\n\t\t};\n\n\t\tconst command = new PutObjectCommand(params);\n\n\t\tawait this.client.send(command);\n\t}\n\n\tasync delete(key: string): Promise<void> {\n\t\tconst command = new DeleteObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t});\n\n\t\tawait this.client.send(command);\n\t}\n}\n\nexport enum AmazonCannedAccessControlList {\n\tAuthenticatedRead = 'authenticated-read',\n\tPrivate = 'private',\n\tPublicRead = 'public-read',\n\tPublicReadWrite = 'public-read-write',\n\tAwsExecRead = 'aws-exec-read',\n\tBucketOwnerRead = 'bucket-owner-read',\n\tBucketOwnerFullControl = 'bucket-owner-full-control',\n\tLogDeliveryWrite = 'log-delivery-write',\n}\n\ninterface AmazonStorageClientCreateOptions {\n\tbucket: string;\n\tregion?: string;\n\tacl?: AmazonCannedAccessControlList;\n\taccessKeyId?: string;\n\tsecretAccessKey?: string;\n\tendpoint?: string;\n\tforcePathStyle?: boolean;\n\tcache?: Cache;\n}\n","/**\n * The S3 driver — `s3://` URLs to an {@link AmazonStorageClient}.\n *\n * Registered by whoever assembles the application, so neutral code can build a\n * client from a URL without importing this file or the AWS SDK behind it.\n */\n\nimport { AmazonStorageClient } from './AmazonStorageClient';\nimport type { StorageDriver } from './registry';\nimport { parse } from './s3Url';\n\nexport const s3Driver: StorageDriver = {\n\tscheme: 's3:',\n\tcreate(url) {\n\t\tconst { bucket, region, endpoint, forcePathStyle } = parse(url);\n\t\treturn AmazonStorageClient.create({\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t});\n\t},\n};\n"],"mappings":";;;;;;AAoBA,IAAa,sBAAb,MAAa,oBAA6C;CA+BvC;CACA;CACA;CACR;CAjCV,WAAS;CAET,OAAO,OACN,SACsB;EACtB,MAAM,EACL,QACA,QACA,aACA,KAC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C,QAAQ,KAAK;GACb,KAAK;EACN,CAAC;EAED,MAAM,KAAK,OAAO,KAAK,OAAO;CAC/B;AACD;AAEA,IAAY,gCAAL,yBAAA,+BAAA;CACN,8BAAA,uBAAA;CACA,8BAAA,aAAA;CACA,8BAAA,gBAAA;CACA,8BAAA,qBAAA;CACA,8BAAA,iBAAA;CACA,8BAAA,qBAAA;CACA,8BAAA,4BAAA;CACA,8BAAA,sBAAA;;AACD,EAAA,CAAA,CAAA;;;;;;;;;ACxLA,MAAa,WAA0B;CACtC,QAAQ;CACR,OAAO,KAAK;EACX,MAAM,EAAE,QAAQ,QAAQ,UAAU,mBAAmBC,cAAAA,MAAM,GAAG;EAC9D,OAAO,oBAAoB,OAAO;GACjC;GACA;GACA;GACA;EACD,CAAC;CACF;AACD"}
|
|
1
|
+
{"version":3,"file":"aws.cjs","names":["trace","SpanKind","SpanStatusCode","S3Client","GetObjectCommand","getSignedUrl","ListObjectVersionsCommand","PutObjectCommand","createPresignedPost","DeleteObjectCommand","parse"],"sources":["../src/telemetry.ts","../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"sourcesContent":["/**\n * A span per storage call, through the global OpenTelemetry tracer — a no-op\n * without a registered provider.\n *\n * `storage.presign`, `storage.put`, `storage.delete` and `storage.list`, with\n * the backend and the bucket. Never the object's key: keys often name a user.\n *\n * @module\n */\nimport {\n\ttype Attributes,\n\tSpanKind,\n\tSpanStatusCode,\n\ttrace,\n} from '@opentelemetry/api';\n\nexport type StorageOperation = 'presign' | 'put' | 'delete' | 'list';\n\n/** Run `fn` inside a `storage.<operation>` span. */\nexport async function traceStorage<T>(\n\toperation: StorageOperation,\n\tattributes: Attributes,\n\tfn: () => Promise<T>,\n): Promise<T> {\n\tconst span = trace\n\t\t.getTracer('@geekmidas/storage')\n\t\t.startSpan(`storage.${operation}`, {\n\t\t\tkind: SpanKind.CLIENT,\n\t\t\tattributes: { 'storage.operation': operation, ...attributes },\n\t\t});\n\ttry {\n\t\treturn await fn();\n\t} catch (error) {\n\t\tif (error instanceof Error) span.recordException(error);\n\t\tspan.setStatus({ code: SpanStatusCode.ERROR });\n\t\tthrow error;\n\t} finally {\n\t\tspan.end();\n\t}\n}\n","import {\n\tDeleteObjectCommand,\n\tGetObjectCommand,\n\tListObjectVersionsCommand,\n\tPutObjectCommand,\n\tS3Client,\n} from '@aws-sdk/client-s3';\nimport {\n\tcreatePresignedPost,\n\ttype PresignedPostOptions,\n} from '@aws-sdk/s3-presigned-post';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\n\nimport type { Cache } from '@geekmidas/cache';\nimport type { Attributes } from '@opentelemetry/api';\nimport {\n\ttype DocumentVersion,\n\ttype File,\n\ttype GetUploadParams,\n\ttype GetUploadResponse,\n\ttype StorageClient,\n\tStorageProvider,\n\ttype UploadPost,\n\ttype UploadPostParams,\n} from './StorageClient';\nimport { type StorageOperation, traceStorage } from './telemetry';\n\nexport class AmazonStorageClient implements StorageClient {\n\treadonly provider = StorageProvider.AWSS3;\n\n\tstatic create(\n\t\toptions: AmazonStorageClientCreateOptions,\n\t): AmazonStorageClient {\n\t\tconst {\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\taccessKeyId,\n\t\t\tacl,\n\t\t\tendpoint,\n\t\t\tsecretAccessKey,\n\t\t\tforcePathStyle = false,\n\t\t\tcache,\n\t\t} = options;\n\t\tconst hasCredentials = accessKeyId && secretAccessKey;\n\t\tconst credentials = hasCredentials\n\t\t\t? { accessKeyId, secretAccessKey }\n\t\t\t: undefined;\n\n\t\tconst client = new S3Client({\n\t\t\tregion,\n\t\t\tcredentials,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t});\n\n\t\treturn new AmazonStorageClient(client, bucket, acl, cache);\n\t}\n\tconstructor(\n\t\tprivate readonly client: S3Client,\n\t\tprivate readonly bucket: string,\n\t\tprivate readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,\n\t\treadonly cache?: Cache,\n\t) {}\n\n\t/** A storage span on this bucket. */\n\tprivate traced<T>(\n\t\toperation: StorageOperation,\n\t\tfn: () => Promise<T>,\n\t\tattributes: Attributes = {},\n\t): Promise<T> {\n\t\treturn traceStorage(\n\t\t\toperation,\n\t\t\t{ 'storage.system': 's3', 'storage.bucket': this.bucket, ...attributes },\n\t\t\tfn,\n\t\t);\n\t}\n\n\tprivate createGetObjectCommand(\n\t\tfile: File,\n\t\toverrides?: { VersionId?: string },\n\t): GetObjectCommand {\n\t\tconst disposition =\n\t\t\tfile.disposition ?? (file.name ? 'attachment' : undefined);\n\t\tconst ResponseContentDisposition = disposition\n\t\t\t? file.name\n\t\t\t\t? `${disposition}; filename=${encodeURIComponent(file.name)}`\n\t\t\t\t: disposition\n\t\t\t: undefined;\n\n\t\treturn new GetObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: file.path,\n\t\t\tResponseContentDisposition,\n\t\t\tResponseContentType: file.responseContentType,\n\t\t\t...overrides,\n\t\t});\n\t}\n\n\tgetVersionDownloadURL(file: File, versionId: string): Promise<string> {\n\t\tconst command = this.createGetObjectCommand(file, { VersionId: versionId });\n\t\treturn this.traced(\n\t\t\t'presign',\n\t\t\t() => getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 }),\n\t\t\t{ 'storage.presign.method': 'GET' },\n\t\t);\n\t}\n\n\tasync getVersions(key: string): Promise<DocumentVersion[]> {\n\t\tconst command = new ListObjectVersionsCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tPrefix: key,\n\t\t});\n\n\t\tconst { Versions = [] } = await this.traced('list', () =>\n\t\t\tthis.client.send(command),\n\t\t);\n\n\t\treturn Versions.map((version) => ({\n\t\t\tid: version.VersionId || '',\n\t\t\tcreatedAt: version.LastModified || new Date(),\n\t\t}));\n\t}\n\n\tasync getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {\n\t\tconst cacheKey = `download-url:${file.path}`;\n\t\tconst cachedURL = await this.cache?.get<string>(cacheKey);\n\n\t\tif (cachedURL) {\n\t\t\treturn cachedURL;\n\t\t}\n\n\t\tconst command = this.createGetObjectCommand(file);\n\t\tconst url = await this.traced(\n\t\t\t'presign',\n\t\t\t() => getSignedUrl(this.client, command, { expiresIn }),\n\t\t\t{ 'storage.presign.method': 'GET' },\n\t\t);\n\t\tconst cacheExpiration = Math.max(expiresIn - 60, 0);\n\n\t\tif (cacheExpiration) {\n\t\t\tawait this.cache?.set(cacheKey, url, cacheExpiration);\n\t\t}\n\n\t\treturn url;\n\t}\n\n\tasync getUploadURL(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 60 * 60,\n\t): Promise<string> {\n\t\tconst command = new PutObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: params.path,\n\t\t\tContentType: params.contentType,\n\t\t\tContentLength: params.contentLength,\n\t\t});\n\n\t\treturn this.traced(\n\t\t\t'presign',\n\t\t\t() => getSignedUrl(this.client, command, { expiresIn }),\n\t\t\t{ 'storage.presign.method': 'PUT' },\n\t\t);\n\t}\n\n\tasync getUpload(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 5,\n\t): Promise<GetUploadResponse> {\n\t\tconst { path } = params;\n\t\tconst { fields: values, url } = await this.traced(\n\t\t\t'presign',\n\t\t\t() =>\n\t\t\t\tcreatePresignedPost(this.client, {\n\t\t\t\t\tExpires: expiresIn * 60,\n\t\t\t\t\tBucket: this.bucket,\n\t\t\t\t\tFields: {\n\t\t\t\t\t\tacl: this.acl,\n\t\t\t\t\t},\n\t\t\t\t\tConditions: [\n\t\t\t\t\t\t// content length restrictions: 0-1MB]\n\t\t\t\t\t\t// ['content-length-range', 0, contentLength],\n\t\t\t\t\t\t// specify content-type to be more generic- images only\n\t\t\t\t\t\t// ['starts-with', '$Content-Type', 'image/'],\n\t\t\t\t\t\t// ['starts-with', '$Content-Type', contentType],\n\t\t\t\t\t],\n\t\t\t\t\tKey: path,\n\t\t\t\t}),\n\t\t\t{ 'storage.presign.method': 'POST' },\n\t\t);\n\n\t\tconst keys = Object.keys(values);\n\t\tconst fields = keys.map((key) => ({ key, value: values[key] || '' }));\n\n\t\treturn { url, fields };\n\t}\n\n\tasync getUploadPost({\n\t\tkey,\n\t\tmaxSize,\n\t\tcontentType,\n\t\texpiresIn = 300,\n\t}: UploadPostParams): Promise<UploadPost> {\n\t\tconst fields: Record<string, string> = {};\n\t\tconst conditions: NonNullable<PresignedPostOptions['Conditions']> = [\n\t\t\t['content-length-range', 0, maxSize],\n\t\t];\n\t\tif (typeof contentType === 'string') {\n\t\t\tfields['Content-Type'] = contentType;\n\t\t\tconditions.push(['eq', '$Content-Type', contentType]);\n\t\t} else if (contentType) {\n\t\t\tconditions.push(['starts-with', '$Content-Type', contentType.startsWith]);\n\t\t}\n\n\t\t// Read before signing, so the timestamp is never later than the policy's.\n\t\tconst expiresAt = new Date(Date.now() + expiresIn * 1000).toISOString();\n\t\tconst post = await this.traced(\n\t\t\t'presign',\n\t\t\t() =>\n\t\t\t\tcreatePresignedPost(this.client, {\n\t\t\t\t\tBucket: this.bucket,\n\t\t\t\t\tKey: key,\n\t\t\t\t\tExpires: expiresIn,\n\t\t\t\t\tFields: fields,\n\t\t\t\t\tConditions: conditions,\n\t\t\t\t}),\n\t\t\t{ 'storage.presign.method': 'POST' },\n\t\t);\n\n\t\treturn { url: post.url, fields: post.fields, key, expiresAt };\n\t}\n\n\tasync upload(\n\t\tkey: string,\n\t\tdata: string | Buffer,\n\t\tcontentType: string,\n\t): Promise<void> {\n\t\tconst Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;\n\n\t\tconst params = {\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t\tBody,\n\t\t\tContentType: contentType,\n\t\t};\n\n\t\tconst command = new PutObjectCommand(params);\n\n\t\tawait this.traced('put', () => this.client.send(command), {\n\t\t\t'storage.object.size': Body.length,\n\t\t\t'storage.object.content_type': contentType,\n\t\t});\n\t}\n\n\tasync delete(key: string): Promise<void> {\n\t\tconst command = new DeleteObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t});\n\n\t\tawait this.traced('delete', () => this.client.send(command));\n\t}\n}\n\nexport enum AmazonCannedAccessControlList {\n\tAuthenticatedRead = 'authenticated-read',\n\tPrivate = 'private',\n\tPublicRead = 'public-read',\n\tPublicReadWrite = 'public-read-write',\n\tAwsExecRead = 'aws-exec-read',\n\tBucketOwnerRead = 'bucket-owner-read',\n\tBucketOwnerFullControl = 'bucket-owner-full-control',\n\tLogDeliveryWrite = 'log-delivery-write',\n}\n\ninterface AmazonStorageClientCreateOptions {\n\tbucket: string;\n\tregion?: string;\n\tacl?: AmazonCannedAccessControlList;\n\taccessKeyId?: string;\n\tsecretAccessKey?: string;\n\tendpoint?: string;\n\tforcePathStyle?: boolean;\n\tcache?: Cache;\n}\n","/**\n * The S3 driver — `s3://` URLs to an {@link AmazonStorageClient}.\n *\n * Registered by whoever assembles the application, so neutral code can build a\n * client from a URL without importing this file or the AWS SDK behind it.\n */\n\nimport { AmazonStorageClient } from './AmazonStorageClient';\nimport type { StorageDriver } from './registry';\nimport { parse } from './s3Url';\n\nexport const s3Driver: StorageDriver = {\n\tscheme: 's3:',\n\tcreate(url) {\n\t\t// The URL's own key pair, when it has one, wins over the SDK's default\n\t\t// chain; without one the client resolves credentials as it always has.\n\t\tconst {\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t\taccessKeyId,\n\t\t\tsecretAccessKey,\n\t\t} = parse(url);\n\t\treturn AmazonStorageClient.create({\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t\taccessKeyId,\n\t\t\tsecretAccessKey,\n\t\t});\n\t},\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAmBA,eAAsB,aACrB,WACA,YACA,IACa;CACb,MAAM,OAAOA,mBAAAA,MACX,UAAU,oBAAoB,CAAC,CAC/B,UAAU,WAAW,aAAa;EAClC,MAAMC,mBAAAA,SAAS;EACf,YAAY;GA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UAAU,IAAID,mBAAAA,iBAAiB,MAAM;EAE3C,MAAM,KAAK,OAAO,aAAa,KAAK,OAAO,KAAK,OAAO,GAAG;GACzD,uBAAuB,KAAK;GAC5B,+BAA+B;EAChC,CAAC;CACF;CAEA,MAAM,OAAO,KAA4B;EACxC,MAAM,UAAU,IAAIE,mBAAAA,oBAAoB;GACvC,QAAQ,KAAK;GACb,KAAK;EACN,CAAC;EAED,MAAM,KAAK,OAAO,gBAAgB,KAAK,OAAO,KAAK,OAAO,CAAC;CAC5D;AACD;AAEA,IAAY,gCAAL,yBAAA,+BAAA;CACN,8BAAA,uBAAA;CACA,8BAAA,aAAA;CACA,8BAAA,gBAAA;CACA,8BAAA,qBAAA;CACA,8BAAA,iBAAA;CACA,8BAAA,qBAAA;CACA,8BAAA,4BAAA;CACA,8BAAA,sBAAA;;AACD,EAAA,CAAA,CAAA;;;;;;;;;ACrQA,MAAa,WAA0B;CACtC,QAAQ;CACR,OAAO,KAAK;EAGX,MAAM,EACL,QACA,QACA,UACA,gBACA,aACA,oBACGC,cAAAA,MAAM,GAAG;EACb,OAAO,oBAAoB,OAAO;GACjC;GACA;GACA;GACA;GACA;GACA;EACD,CAAC;CACF;AACD"}
|
package/dist/aws.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as DocumentVersion, c as GetUploadResponse, l as StorageClient, o as File, s as GetUploadParams, t as StorageDriver, u as StorageProvider } from "./registry-
|
|
1
|
+
import { a as DocumentVersion, c as GetUploadResponse, d as UploadPost, f as UploadPostParams, l as StorageClient, o as File, s as GetUploadParams, t as StorageDriver, u as StorageProvider } from "./registry-DyBOtv6n.cjs";
|
|
2
2
|
import { S3Address, t as s3Url_d_exports } from "./s3Url.cjs";
|
|
3
3
|
import { S3Client } from "@aws-sdk/client-s3";
|
|
4
4
|
import { Cache } from "@geekmidas/cache";
|
|
@@ -11,12 +11,15 @@ export declare class AmazonStorageClient implements StorageClient {
|
|
|
11
11
|
readonly provider = StorageProvider.AWSS3;
|
|
12
12
|
static create(options: AmazonStorageClientCreateOptions): AmazonStorageClient;
|
|
13
13
|
constructor(client: S3Client, bucket: string, acl?: AmazonCannedAccessControlList, cache?: Cache);
|
|
14
|
+
/** A storage span on this bucket. */
|
|
15
|
+
private traced;
|
|
14
16
|
private createGetObjectCommand;
|
|
15
17
|
getVersionDownloadURL(file: File, versionId: string): Promise<string>;
|
|
16
18
|
getVersions(key: string): Promise<DocumentVersion[]>;
|
|
17
19
|
getDownloadURL(file: File, expiresIn?: number): Promise<string>;
|
|
18
20
|
getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
|
|
19
21
|
getUpload(params: GetUploadParams, expiresIn?: number): Promise<GetUploadResponse>;
|
|
22
|
+
getUploadPost({ key, maxSize, contentType, expiresIn }: UploadPostParams): Promise<UploadPost>;
|
|
20
23
|
upload(key: string, data: string | Buffer, contentType: string): Promise<void>;
|
|
21
24
|
delete(key: string): Promise<void>;
|
|
22
25
|
}
|
|
@@ -44,5 +47,5 @@ interface AmazonStorageClientCreateOptions {
|
|
|
44
47
|
//#region src/s3Driver.d.ts
|
|
45
48
|
export declare const s3Driver: StorageDriver;
|
|
46
49
|
//#endregion
|
|
47
|
-
export { type DocumentVersion, type File, type GetUploadParams, type GetUploadResponse, type S3Address, type StorageClient, s3Url_d_exports as s3Url };
|
|
50
|
+
export { type DocumentVersion, type File, type GetUploadParams, type GetUploadResponse, type S3Address, type StorageClient, type UploadPost, type UploadPostParams, s3Url_d_exports as s3Url };
|
|
48
51
|
//# sourceMappingURL=aws.d.cts.map
|
package/dist/aws.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aws.d.cts","names":[],"sources":["../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"aws.d.cts","names":[],"sources":["../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"mappings":";;;;;qBA2Ba,+BAA+B;mBA+BzB;mBACA;mBACA;WACR,QAAQ;WAjCT,WAAQ,gBAAA;SAEV,OACN,SAAS,mCACP;EAyBH,YACkB,QAAQ,UACR,gBACA,MAAG,+BACX,QAAQ;;UAIV;UAYA;EAqBR,sBAAsB,MAAM,MAAM,oBAAoB;EAShD,YAAY,cAAc,QAAQ;EAgBlC,eAAe,MAAM,MAAM,qBAAsB;EAuBjD,aACL,QAAQ,iBACR,qBACE;EAeG,UACL,QAAQ,iBACR,qBACE,QAAQ;EA6BL,gBACL,KACA,SACA,aACA,aACE,mBAAmB,QAAQ;EA8BxB,OACL,aACA,eAAe,QACf,sBACE;EAkBG,OAAO,cAAc;;oBAUhB;EACX;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;UAGS;EACT;EACA;EACA,MAAM;EACN;EACA;EACA;EACA;EACA,QAAQ;;;;qBC/QI,UAAU"}
|
package/dist/aws.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as DocumentVersion, c as GetUploadResponse, l as StorageClient, o as File, s as GetUploadParams, t as StorageDriver, u as StorageProvider } from "./registry-
|
|
1
|
+
import { a as DocumentVersion, c as GetUploadResponse, d as UploadPost, f as UploadPostParams, l as StorageClient, o as File, s as GetUploadParams, t as StorageDriver, u as StorageProvider } from "./registry-DyBOtv6n.mjs";
|
|
2
2
|
import { S3Address, t as s3Url_d_exports } from "./s3Url.mjs";
|
|
3
3
|
import { S3Client } from "@aws-sdk/client-s3";
|
|
4
4
|
import { Cache } from "@geekmidas/cache";
|
|
@@ -11,12 +11,15 @@ export declare class AmazonStorageClient implements StorageClient {
|
|
|
11
11
|
readonly provider = StorageProvider.AWSS3;
|
|
12
12
|
static create(options: AmazonStorageClientCreateOptions): AmazonStorageClient;
|
|
13
13
|
constructor(client: S3Client, bucket: string, acl?: AmazonCannedAccessControlList, cache?: Cache);
|
|
14
|
+
/** A storage span on this bucket. */
|
|
15
|
+
private traced;
|
|
14
16
|
private createGetObjectCommand;
|
|
15
17
|
getVersionDownloadURL(file: File, versionId: string): Promise<string>;
|
|
16
18
|
getVersions(key: string): Promise<DocumentVersion[]>;
|
|
17
19
|
getDownloadURL(file: File, expiresIn?: number): Promise<string>;
|
|
18
20
|
getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
|
|
19
21
|
getUpload(params: GetUploadParams, expiresIn?: number): Promise<GetUploadResponse>;
|
|
22
|
+
getUploadPost({ key, maxSize, contentType, expiresIn }: UploadPostParams): Promise<UploadPost>;
|
|
20
23
|
upload(key: string, data: string | Buffer, contentType: string): Promise<void>;
|
|
21
24
|
delete(key: string): Promise<void>;
|
|
22
25
|
}
|
|
@@ -44,5 +47,5 @@ interface AmazonStorageClientCreateOptions {
|
|
|
44
47
|
//#region src/s3Driver.d.ts
|
|
45
48
|
export declare const s3Driver: StorageDriver;
|
|
46
49
|
//#endregion
|
|
47
|
-
export { type DocumentVersion, type File, type GetUploadParams, type GetUploadResponse, type S3Address, type StorageClient, s3Url_d_exports as s3Url };
|
|
50
|
+
export { type DocumentVersion, type File, type GetUploadParams, type GetUploadResponse, type S3Address, type StorageClient, type UploadPost, type UploadPostParams, s3Url_d_exports as s3Url };
|
|
48
51
|
//# sourceMappingURL=aws.d.mts.map
|
package/dist/aws.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aws.d.mts","names":[],"sources":["../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"aws.d.mts","names":[],"sources":["../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"mappings":";;;;;qBA2Ba,+BAA+B;mBA+BzB;mBACA;mBACA;WACR,QAAQ;WAjCT,WAAQ,gBAAA;SAEV,OACN,SAAS,mCACP;EAyBH,YACkB,QAAQ,UACR,gBACA,MAAG,+BACX,QAAQ;;UAIV;UAYA;EAqBR,sBAAsB,MAAM,MAAM,oBAAoB;EAShD,YAAY,cAAc,QAAQ;EAgBlC,eAAe,MAAM,MAAM,qBAAsB;EAuBjD,aACL,QAAQ,iBACR,qBACE;EAeG,UACL,QAAQ,iBACR,qBACE,QAAQ;EA6BL,gBACL,KACA,SACA,aACA,aACE,mBAAmB,QAAQ;EA8BxB,OACL,aACA,eAAe,QACf,sBACE;EAkBG,OAAO,cAAc;;oBAUhB;EACX;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;UAGS;EACT;EACA;EACA,MAAM;EACN;EACA;EACA;EACA;EACA,QAAQ;;;;qBC/QI,UAAU"}
|
package/dist/aws.mjs
CHANGED
|
@@ -2,6 +2,37 @@ import { parse, t as s3Url_exports } from "./s3Url.mjs";
|
|
|
2
2
|
import { DeleteObjectCommand, GetObjectCommand, ListObjectVersionsCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
|
|
3
3
|
import { createPresignedPost } from "@aws-sdk/s3-presigned-post";
|
|
4
4
|
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
|
|
5
|
+
import { SpanKind, SpanStatusCode, trace } from "@opentelemetry/api";
|
|
6
|
+
//#region src/telemetry.ts
|
|
7
|
+
/**
|
|
8
|
+
* A span per storage call, through the global OpenTelemetry tracer — a no-op
|
|
9
|
+
* without a registered provider.
|
|
10
|
+
*
|
|
11
|
+
* `storage.presign`, `storage.put`, `storage.delete` and `storage.list`, with
|
|
12
|
+
* the backend and the bucket. Never the object's key: keys often name a user.
|
|
13
|
+
*
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
/** Run `fn` inside a `storage.<operation>` span. */
|
|
17
|
+
async function traceStorage(operation, attributes, fn) {
|
|
18
|
+
const span = trace.getTracer("@geekmidas/storage").startSpan(`storage.${operation}`, {
|
|
19
|
+
kind: SpanKind.CLIENT,
|
|
20
|
+
attributes: {
|
|
21
|
+
"storage.operation": operation,
|
|
22
|
+
...attributes
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
try {
|
|
26
|
+
return await fn();
|
|
27
|
+
} catch (error) {
|
|
28
|
+
if (error instanceof Error) span.recordException(error);
|
|
29
|
+
span.setStatus({ code: SpanStatusCode.ERROR });
|
|
30
|
+
throw error;
|
|
31
|
+
} finally {
|
|
32
|
+
span.end();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
5
36
|
//#region src/AmazonStorageClient.ts
|
|
6
37
|
var AmazonStorageClient = class AmazonStorageClient {
|
|
7
38
|
client;
|
|
@@ -28,6 +59,14 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
28
59
|
this.acl = acl;
|
|
29
60
|
this.cache = cache;
|
|
30
61
|
}
|
|
62
|
+
/** A storage span on this bucket. */
|
|
63
|
+
traced(operation, fn, attributes = {}) {
|
|
64
|
+
return traceStorage(operation, {
|
|
65
|
+
"storage.system": "s3",
|
|
66
|
+
"storage.bucket": this.bucket,
|
|
67
|
+
...attributes
|
|
68
|
+
}, fn);
|
|
69
|
+
}
|
|
31
70
|
createGetObjectCommand(file, overrides) {
|
|
32
71
|
const disposition = file.disposition ?? (file.name ? "attachment" : void 0);
|
|
33
72
|
const ResponseContentDisposition = disposition ? file.name ? `${disposition}; filename=${encodeURIComponent(file.name)}` : disposition : void 0;
|
|
@@ -41,14 +80,14 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
41
80
|
}
|
|
42
81
|
getVersionDownloadURL(file, versionId) {
|
|
43
82
|
const command = this.createGetObjectCommand(file, { VersionId: versionId });
|
|
44
|
-
return getSignedUrl(this.client, command, { expiresIn: 86400 });
|
|
83
|
+
return this.traced("presign", () => getSignedUrl(this.client, command, { expiresIn: 86400 }), { "storage.presign.method": "GET" });
|
|
45
84
|
}
|
|
46
85
|
async getVersions(key) {
|
|
47
86
|
const command = new ListObjectVersionsCommand({
|
|
48
87
|
Bucket: this.bucket,
|
|
49
88
|
Prefix: key
|
|
50
89
|
});
|
|
51
|
-
const { Versions = [] } = await this.client.send(command);
|
|
90
|
+
const { Versions = [] } = await this.traced("list", () => this.client.send(command));
|
|
52
91
|
return Versions.map((version) => ({
|
|
53
92
|
id: version.VersionId || "",
|
|
54
93
|
createdAt: version.LastModified || /* @__PURE__ */ new Date()
|
|
@@ -59,7 +98,7 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
59
98
|
const cachedURL = await this.cache?.get(cacheKey);
|
|
60
99
|
if (cachedURL) return cachedURL;
|
|
61
100
|
const command = this.createGetObjectCommand(file);
|
|
62
|
-
const url = await getSignedUrl(this.client, command, { expiresIn });
|
|
101
|
+
const url = await this.traced("presign", () => getSignedUrl(this.client, command, { expiresIn }), { "storage.presign.method": "GET" });
|
|
63
102
|
const cacheExpiration = Math.max(expiresIn - 60, 0);
|
|
64
103
|
if (cacheExpiration) await this.cache?.set(cacheKey, url, cacheExpiration);
|
|
65
104
|
return url;
|
|
@@ -71,17 +110,17 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
71
110
|
ContentType: params.contentType,
|
|
72
111
|
ContentLength: params.contentLength
|
|
73
112
|
});
|
|
74
|
-
return getSignedUrl(this.client, command, { expiresIn });
|
|
113
|
+
return this.traced("presign", () => getSignedUrl(this.client, command, { expiresIn }), { "storage.presign.method": "PUT" });
|
|
75
114
|
}
|
|
76
115
|
async getUpload(params, expiresIn = 5) {
|
|
77
116
|
const { path } = params;
|
|
78
|
-
const { fields: values, url } = await createPresignedPost(this.client, {
|
|
117
|
+
const { fields: values, url } = await this.traced("presign", () => createPresignedPost(this.client, {
|
|
79
118
|
Expires: expiresIn * 60,
|
|
80
119
|
Bucket: this.bucket,
|
|
81
120
|
Fields: { acl: this.acl },
|
|
82
121
|
Conditions: [],
|
|
83
122
|
Key: path
|
|
84
|
-
});
|
|
123
|
+
}), { "storage.presign.method": "POST" });
|
|
85
124
|
return {
|
|
86
125
|
url,
|
|
87
126
|
fields: Object.keys(values).map((key) => ({
|
|
@@ -90,6 +129,40 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
90
129
|
}))
|
|
91
130
|
};
|
|
92
131
|
}
|
|
132
|
+
async getUploadPost({ key, maxSize, contentType, expiresIn = 300 }) {
|
|
133
|
+
const fields = {};
|
|
134
|
+
const conditions = [[
|
|
135
|
+
"content-length-range",
|
|
136
|
+
0,
|
|
137
|
+
maxSize
|
|
138
|
+
]];
|
|
139
|
+
if (typeof contentType === "string") {
|
|
140
|
+
fields["Content-Type"] = contentType;
|
|
141
|
+
conditions.push([
|
|
142
|
+
"eq",
|
|
143
|
+
"$Content-Type",
|
|
144
|
+
contentType
|
|
145
|
+
]);
|
|
146
|
+
} else if (contentType) conditions.push([
|
|
147
|
+
"starts-with",
|
|
148
|
+
"$Content-Type",
|
|
149
|
+
contentType.startsWith
|
|
150
|
+
]);
|
|
151
|
+
const expiresAt = new Date(Date.now() + expiresIn * 1e3).toISOString();
|
|
152
|
+
const post = await this.traced("presign", () => createPresignedPost(this.client, {
|
|
153
|
+
Bucket: this.bucket,
|
|
154
|
+
Key: key,
|
|
155
|
+
Expires: expiresIn,
|
|
156
|
+
Fields: fields,
|
|
157
|
+
Conditions: conditions
|
|
158
|
+
}), { "storage.presign.method": "POST" });
|
|
159
|
+
return {
|
|
160
|
+
url: post.url,
|
|
161
|
+
fields: post.fields,
|
|
162
|
+
key,
|
|
163
|
+
expiresAt
|
|
164
|
+
};
|
|
165
|
+
}
|
|
93
166
|
async upload(key, data, contentType) {
|
|
94
167
|
const Body = typeof data === "string" ? Buffer.from(data, "base64") : data;
|
|
95
168
|
const params = {
|
|
@@ -99,14 +172,17 @@ var AmazonStorageClient = class AmazonStorageClient {
|
|
|
99
172
|
ContentType: contentType
|
|
100
173
|
};
|
|
101
174
|
const command = new PutObjectCommand(params);
|
|
102
|
-
await this.client.send(command)
|
|
175
|
+
await this.traced("put", () => this.client.send(command), {
|
|
176
|
+
"storage.object.size": Body.length,
|
|
177
|
+
"storage.object.content_type": contentType
|
|
178
|
+
});
|
|
103
179
|
}
|
|
104
180
|
async delete(key) {
|
|
105
181
|
const command = new DeleteObjectCommand({
|
|
106
182
|
Bucket: this.bucket,
|
|
107
183
|
Key: key
|
|
108
184
|
});
|
|
109
|
-
await this.client.send(command);
|
|
185
|
+
await this.traced("delete", () => this.client.send(command));
|
|
110
186
|
}
|
|
111
187
|
};
|
|
112
188
|
let AmazonCannedAccessControlList = /* @__PURE__ */ function(AmazonCannedAccessControlList) {
|
|
@@ -131,12 +207,14 @@ let AmazonCannedAccessControlList = /* @__PURE__ */ function(AmazonCannedAccessC
|
|
|
131
207
|
const s3Driver = {
|
|
132
208
|
scheme: "s3:",
|
|
133
209
|
create(url) {
|
|
134
|
-
const { bucket, region, endpoint, forcePathStyle } = parse(url);
|
|
210
|
+
const { bucket, region, endpoint, forcePathStyle, accessKeyId, secretAccessKey } = parse(url);
|
|
135
211
|
return AmazonStorageClient.create({
|
|
136
212
|
bucket,
|
|
137
213
|
region,
|
|
138
214
|
endpoint,
|
|
139
|
-
forcePathStyle
|
|
215
|
+
forcePathStyle,
|
|
216
|
+
accessKeyId,
|
|
217
|
+
secretAccessKey
|
|
140
218
|
});
|
|
141
219
|
}
|
|
142
220
|
};
|
package/dist/aws.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aws.mjs","names":[],"sources":["../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"sourcesContent":["import {\n\tDeleteObjectCommand,\n\tGetObjectCommand,\n\tListObjectVersionsCommand,\n\tPutObjectCommand,\n\tS3Client,\n} from '@aws-sdk/client-s3';\nimport { createPresignedPost } from '@aws-sdk/s3-presigned-post';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\n\nimport type { Cache } from '@geekmidas/cache';\nimport {\n\ttype DocumentVersion,\n\ttype File,\n\ttype GetUploadParams,\n\ttype GetUploadResponse,\n\ttype StorageClient,\n\tStorageProvider,\n} from './StorageClient';\n\nexport class AmazonStorageClient implements StorageClient {\n\treadonly provider = StorageProvider.AWSS3;\n\n\tstatic create(\n\t\toptions: AmazonStorageClientCreateOptions,\n\t): AmazonStorageClient {\n\t\tconst {\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\taccessKeyId,\n\t\t\tacl,\n\t\t\tendpoint,\n\t\t\tsecretAccessKey,\n\t\t\tforcePathStyle = false,\n\t\t\tcache,\n\t\t} = options;\n\t\tconst hasCredentials = accessKeyId && secretAccessKey;\n\t\tconst credentials = hasCredentials\n\t\t\t? { accessKeyId, secretAccessKey }\n\t\t\t: undefined;\n\n\t\tconst client = new S3Client({\n\t\t\tregion,\n\t\t\tcredentials,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t});\n\n\t\treturn new AmazonStorageClient(client, bucket, acl, cache);\n\t}\n\tconstructor(\n\t\tprivate readonly client: S3Client,\n\t\tprivate readonly bucket: string,\n\t\tprivate readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,\n\t\treadonly cache?: Cache,\n\t) {}\n\n\tprivate createGetObjectCommand(\n\t\tfile: File,\n\t\toverrides?: { VersionId?: string },\n\t): GetObjectCommand {\n\t\tconst disposition =\n\t\t\tfile.disposition ?? (file.name ? 'attachment' : undefined);\n\t\tconst ResponseContentDisposition = disposition\n\t\t\t? file.name\n\t\t\t\t? `${disposition}; filename=${encodeURIComponent(file.name)}`\n\t\t\t\t: disposition\n\t\t\t: undefined;\n\n\t\treturn new GetObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: file.path,\n\t\t\tResponseContentDisposition,\n\t\t\tResponseContentType: file.responseContentType,\n\t\t\t...overrides,\n\t\t});\n\t}\n\n\tgetVersionDownloadURL(file: File, versionId: string): Promise<string> {\n\t\tconst command = this.createGetObjectCommand(file, { VersionId: versionId });\n\t\treturn getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 });\n\t}\n\n\tasync getVersions(key: string): Promise<DocumentVersion[]> {\n\t\tconst command = new ListObjectVersionsCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tPrefix: key,\n\t\t});\n\n\t\tconst { Versions = [] } = await this.client.send(command);\n\n\t\treturn Versions.map((version) => ({\n\t\t\tid: version.VersionId || '',\n\t\t\tcreatedAt: version.LastModified || new Date(),\n\t\t}));\n\t}\n\n\tasync getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {\n\t\tconst cacheKey = `download-url:${file.path}`;\n\t\tconst cachedURL = await this.cache?.get<string>(cacheKey);\n\n\t\tif (cachedURL) {\n\t\t\treturn cachedURL;\n\t\t}\n\n\t\tconst command = this.createGetObjectCommand(file);\n\t\tconst url = await getSignedUrl(this.client, command, { expiresIn });\n\t\tconst cacheExpiration = Math.max(expiresIn - 60, 0);\n\n\t\tif (cacheExpiration) {\n\t\t\tawait this.cache?.set(cacheKey, url, cacheExpiration);\n\t\t}\n\n\t\treturn url;\n\t}\n\n\tasync getUploadURL(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 60 * 60,\n\t): Promise<string> {\n\t\tconst command = new PutObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: params.path,\n\t\t\tContentType: params.contentType,\n\t\t\tContentLength: params.contentLength,\n\t\t});\n\n\t\treturn getSignedUrl(this.client, command, { expiresIn });\n\t}\n\n\tasync getUpload(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 5,\n\t): Promise<GetUploadResponse> {\n\t\tconst { path } = params;\n\t\tconst { fields: values, url } = await createPresignedPost(this.client, {\n\t\t\tExpires: expiresIn * 60,\n\t\t\tBucket: this.bucket,\n\t\t\tFields: {\n\t\t\t\tacl: this.acl,\n\t\t\t},\n\t\t\tConditions: [\n\t\t\t\t// content length restrictions: 0-1MB]\n\t\t\t\t// ['content-length-range', 0, contentLength],\n\t\t\t\t// specify content-type to be more generic- images only\n\t\t\t\t// ['starts-with', '$Content-Type', 'image/'],\n\t\t\t\t// ['starts-with', '$Content-Type', contentType],\n\t\t\t],\n\t\t\tKey: path,\n\t\t});\n\n\t\tconst keys = Object.keys(values);\n\t\tconst fields = keys.map((key) => ({ key, value: values[key] || '' }));\n\n\t\treturn { url, fields };\n\t}\n\n\tasync upload(\n\t\tkey: string,\n\t\tdata: string | Buffer,\n\t\tcontentType: string,\n\t): Promise<void> {\n\t\tconst Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;\n\n\t\tconst params = {\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t\tBody,\n\t\t\tContentType: contentType,\n\t\t};\n\n\t\tconst command = new PutObjectCommand(params);\n\n\t\tawait this.client.send(command);\n\t}\n\n\tasync delete(key: string): Promise<void> {\n\t\tconst command = new DeleteObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t});\n\n\t\tawait this.client.send(command);\n\t}\n}\n\nexport enum AmazonCannedAccessControlList {\n\tAuthenticatedRead = 'authenticated-read',\n\tPrivate = 'private',\n\tPublicRead = 'public-read',\n\tPublicReadWrite = 'public-read-write',\n\tAwsExecRead = 'aws-exec-read',\n\tBucketOwnerRead = 'bucket-owner-read',\n\tBucketOwnerFullControl = 'bucket-owner-full-control',\n\tLogDeliveryWrite = 'log-delivery-write',\n}\n\ninterface AmazonStorageClientCreateOptions {\n\tbucket: string;\n\tregion?: string;\n\tacl?: AmazonCannedAccessControlList;\n\taccessKeyId?: string;\n\tsecretAccessKey?: string;\n\tendpoint?: string;\n\tforcePathStyle?: boolean;\n\tcache?: Cache;\n}\n","/**\n * The S3 driver — `s3://` URLs to an {@link AmazonStorageClient}.\n *\n * Registered by whoever assembles the application, so neutral code can build a\n * client from a URL without importing this file or the AWS SDK behind it.\n */\n\nimport { AmazonStorageClient } from './AmazonStorageClient';\nimport type { StorageDriver } from './registry';\nimport { parse } from './s3Url';\n\nexport const s3Driver: StorageDriver = {\n\tscheme: 's3:',\n\tcreate(url) {\n\t\tconst { bucket, region, endpoint, forcePathStyle } = parse(url);\n\t\treturn AmazonStorageClient.create({\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t});\n\t},\n};\n"],"mappings":";;;;;AAoBA,IAAa,sBAAb,MAAa,oBAA6C;CA+BvC;CACA;CACA;CACR;CAjCV,WA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,IAAY,gCAAL,yBAAA,+BAAA;CACN,8BAAA,uBAAA;CACA,8BAAA,aAAA;CACA,8BAAA,gBAAA;CACA,8BAAA,qBAAA;CACA,8BAAA,iBAAA;CACA,8BAAA,qBAAA;CACA,8BAAA,4BAAA;CACA,8BAAA,sBAAA;;AACD,EAAA,CAAA,CAAA;;;;;;;;;ACxLA,MAAa,WAA0B;CACtC,QAAQ;CACR,OAAO,KAAK;EACX,MAAM,EAAE,QAAQ,QAAQ,UAAU,mBAAmB,MAAM,GAAG;EAC9D,OAAO,oBAAoB,OAAO;GACjC;GACA;GACA;GACA;EACD,CAAC;CACF;AACD"}
|
|
1
|
+
{"version":3,"file":"aws.mjs","names":[],"sources":["../src/telemetry.ts","../src/AmazonStorageClient.ts","../src/s3Driver.ts"],"sourcesContent":["/**\n * A span per storage call, through the global OpenTelemetry tracer — a no-op\n * without a registered provider.\n *\n * `storage.presign`, `storage.put`, `storage.delete` and `storage.list`, with\n * the backend and the bucket. Never the object's key: keys often name a user.\n *\n * @module\n */\nimport {\n\ttype Attributes,\n\tSpanKind,\n\tSpanStatusCode,\n\ttrace,\n} from '@opentelemetry/api';\n\nexport type StorageOperation = 'presign' | 'put' | 'delete' | 'list';\n\n/** Run `fn` inside a `storage.<operation>` span. */\nexport async function traceStorage<T>(\n\toperation: StorageOperation,\n\tattributes: Attributes,\n\tfn: () => Promise<T>,\n): Promise<T> {\n\tconst span = trace\n\t\t.getTracer('@geekmidas/storage')\n\t\t.startSpan(`storage.${operation}`, {\n\t\t\tkind: SpanKind.CLIENT,\n\t\t\tattributes: { 'storage.operation': operation, ...attributes },\n\t\t});\n\ttry {\n\t\treturn await fn();\n\t} catch (error) {\n\t\tif (error instanceof Error) span.recordException(error);\n\t\tspan.setStatus({ code: SpanStatusCode.ERROR });\n\t\tthrow error;\n\t} finally {\n\t\tspan.end();\n\t}\n}\n","import {\n\tDeleteObjectCommand,\n\tGetObjectCommand,\n\tListObjectVersionsCommand,\n\tPutObjectCommand,\n\tS3Client,\n} from '@aws-sdk/client-s3';\nimport {\n\tcreatePresignedPost,\n\ttype PresignedPostOptions,\n} from '@aws-sdk/s3-presigned-post';\nimport { getSignedUrl } from '@aws-sdk/s3-request-presigner';\n\nimport type { Cache } from '@geekmidas/cache';\nimport type { Attributes } from '@opentelemetry/api';\nimport {\n\ttype DocumentVersion,\n\ttype File,\n\ttype GetUploadParams,\n\ttype GetUploadResponse,\n\ttype StorageClient,\n\tStorageProvider,\n\ttype UploadPost,\n\ttype UploadPostParams,\n} from './StorageClient';\nimport { type StorageOperation, traceStorage } from './telemetry';\n\nexport class AmazonStorageClient implements StorageClient {\n\treadonly provider = StorageProvider.AWSS3;\n\n\tstatic create(\n\t\toptions: AmazonStorageClientCreateOptions,\n\t): AmazonStorageClient {\n\t\tconst {\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\taccessKeyId,\n\t\t\tacl,\n\t\t\tendpoint,\n\t\t\tsecretAccessKey,\n\t\t\tforcePathStyle = false,\n\t\t\tcache,\n\t\t} = options;\n\t\tconst hasCredentials = accessKeyId && secretAccessKey;\n\t\tconst credentials = hasCredentials\n\t\t\t? { accessKeyId, secretAccessKey }\n\t\t\t: undefined;\n\n\t\tconst client = new S3Client({\n\t\t\tregion,\n\t\t\tcredentials,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t});\n\n\t\treturn new AmazonStorageClient(client, bucket, acl, cache);\n\t}\n\tconstructor(\n\t\tprivate readonly client: S3Client,\n\t\tprivate readonly bucket: string,\n\t\tprivate readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,\n\t\treadonly cache?: Cache,\n\t) {}\n\n\t/** A storage span on this bucket. */\n\tprivate traced<T>(\n\t\toperation: StorageOperation,\n\t\tfn: () => Promise<T>,\n\t\tattributes: Attributes = {},\n\t): Promise<T> {\n\t\treturn traceStorage(\n\t\t\toperation,\n\t\t\t{ 'storage.system': 's3', 'storage.bucket': this.bucket, ...attributes },\n\t\t\tfn,\n\t\t);\n\t}\n\n\tprivate createGetObjectCommand(\n\t\tfile: File,\n\t\toverrides?: { VersionId?: string },\n\t): GetObjectCommand {\n\t\tconst disposition =\n\t\t\tfile.disposition ?? (file.name ? 'attachment' : undefined);\n\t\tconst ResponseContentDisposition = disposition\n\t\t\t? file.name\n\t\t\t\t? `${disposition}; filename=${encodeURIComponent(file.name)}`\n\t\t\t\t: disposition\n\t\t\t: undefined;\n\n\t\treturn new GetObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: file.path,\n\t\t\tResponseContentDisposition,\n\t\t\tResponseContentType: file.responseContentType,\n\t\t\t...overrides,\n\t\t});\n\t}\n\n\tgetVersionDownloadURL(file: File, versionId: string): Promise<string> {\n\t\tconst command = this.createGetObjectCommand(file, { VersionId: versionId });\n\t\treturn this.traced(\n\t\t\t'presign',\n\t\t\t() => getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 }),\n\t\t\t{ 'storage.presign.method': 'GET' },\n\t\t);\n\t}\n\n\tasync getVersions(key: string): Promise<DocumentVersion[]> {\n\t\tconst command = new ListObjectVersionsCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tPrefix: key,\n\t\t});\n\n\t\tconst { Versions = [] } = await this.traced('list', () =>\n\t\t\tthis.client.send(command),\n\t\t);\n\n\t\treturn Versions.map((version) => ({\n\t\t\tid: version.VersionId || '',\n\t\t\tcreatedAt: version.LastModified || new Date(),\n\t\t}));\n\t}\n\n\tasync getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {\n\t\tconst cacheKey = `download-url:${file.path}`;\n\t\tconst cachedURL = await this.cache?.get<string>(cacheKey);\n\n\t\tif (cachedURL) {\n\t\t\treturn cachedURL;\n\t\t}\n\n\t\tconst command = this.createGetObjectCommand(file);\n\t\tconst url = await this.traced(\n\t\t\t'presign',\n\t\t\t() => getSignedUrl(this.client, command, { expiresIn }),\n\t\t\t{ 'storage.presign.method': 'GET' },\n\t\t);\n\t\tconst cacheExpiration = Math.max(expiresIn - 60, 0);\n\n\t\tif (cacheExpiration) {\n\t\t\tawait this.cache?.set(cacheKey, url, cacheExpiration);\n\t\t}\n\n\t\treturn url;\n\t}\n\n\tasync getUploadURL(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 60 * 60,\n\t): Promise<string> {\n\t\tconst command = new PutObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: params.path,\n\t\t\tContentType: params.contentType,\n\t\t\tContentLength: params.contentLength,\n\t\t});\n\n\t\treturn this.traced(\n\t\t\t'presign',\n\t\t\t() => getSignedUrl(this.client, command, { expiresIn }),\n\t\t\t{ 'storage.presign.method': 'PUT' },\n\t\t);\n\t}\n\n\tasync getUpload(\n\t\tparams: GetUploadParams,\n\t\texpiresIn = 5,\n\t): Promise<GetUploadResponse> {\n\t\tconst { path } = params;\n\t\tconst { fields: values, url } = await this.traced(\n\t\t\t'presign',\n\t\t\t() =>\n\t\t\t\tcreatePresignedPost(this.client, {\n\t\t\t\t\tExpires: expiresIn * 60,\n\t\t\t\t\tBucket: this.bucket,\n\t\t\t\t\tFields: {\n\t\t\t\t\t\tacl: this.acl,\n\t\t\t\t\t},\n\t\t\t\t\tConditions: [\n\t\t\t\t\t\t// content length restrictions: 0-1MB]\n\t\t\t\t\t\t// ['content-length-range', 0, contentLength],\n\t\t\t\t\t\t// specify content-type to be more generic- images only\n\t\t\t\t\t\t// ['starts-with', '$Content-Type', 'image/'],\n\t\t\t\t\t\t// ['starts-with', '$Content-Type', contentType],\n\t\t\t\t\t],\n\t\t\t\t\tKey: path,\n\t\t\t\t}),\n\t\t\t{ 'storage.presign.method': 'POST' },\n\t\t);\n\n\t\tconst keys = Object.keys(values);\n\t\tconst fields = keys.map((key) => ({ key, value: values[key] || '' }));\n\n\t\treturn { url, fields };\n\t}\n\n\tasync getUploadPost({\n\t\tkey,\n\t\tmaxSize,\n\t\tcontentType,\n\t\texpiresIn = 300,\n\t}: UploadPostParams): Promise<UploadPost> {\n\t\tconst fields: Record<string, string> = {};\n\t\tconst conditions: NonNullable<PresignedPostOptions['Conditions']> = [\n\t\t\t['content-length-range', 0, maxSize],\n\t\t];\n\t\tif (typeof contentType === 'string') {\n\t\t\tfields['Content-Type'] = contentType;\n\t\t\tconditions.push(['eq', '$Content-Type', contentType]);\n\t\t} else if (contentType) {\n\t\t\tconditions.push(['starts-with', '$Content-Type', contentType.startsWith]);\n\t\t}\n\n\t\t// Read before signing, so the timestamp is never later than the policy's.\n\t\tconst expiresAt = new Date(Date.now() + expiresIn * 1000).toISOString();\n\t\tconst post = await this.traced(\n\t\t\t'presign',\n\t\t\t() =>\n\t\t\t\tcreatePresignedPost(this.client, {\n\t\t\t\t\tBucket: this.bucket,\n\t\t\t\t\tKey: key,\n\t\t\t\t\tExpires: expiresIn,\n\t\t\t\t\tFields: fields,\n\t\t\t\t\tConditions: conditions,\n\t\t\t\t}),\n\t\t\t{ 'storage.presign.method': 'POST' },\n\t\t);\n\n\t\treturn { url: post.url, fields: post.fields, key, expiresAt };\n\t}\n\n\tasync upload(\n\t\tkey: string,\n\t\tdata: string | Buffer,\n\t\tcontentType: string,\n\t): Promise<void> {\n\t\tconst Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;\n\n\t\tconst params = {\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t\tBody,\n\t\t\tContentType: contentType,\n\t\t};\n\n\t\tconst command = new PutObjectCommand(params);\n\n\t\tawait this.traced('put', () => this.client.send(command), {\n\t\t\t'storage.object.size': Body.length,\n\t\t\t'storage.object.content_type': contentType,\n\t\t});\n\t}\n\n\tasync delete(key: string): Promise<void> {\n\t\tconst command = new DeleteObjectCommand({\n\t\t\tBucket: this.bucket,\n\t\t\tKey: key,\n\t\t});\n\n\t\tawait this.traced('delete', () => this.client.send(command));\n\t}\n}\n\nexport enum AmazonCannedAccessControlList {\n\tAuthenticatedRead = 'authenticated-read',\n\tPrivate = 'private',\n\tPublicRead = 'public-read',\n\tPublicReadWrite = 'public-read-write',\n\tAwsExecRead = 'aws-exec-read',\n\tBucketOwnerRead = 'bucket-owner-read',\n\tBucketOwnerFullControl = 'bucket-owner-full-control',\n\tLogDeliveryWrite = 'log-delivery-write',\n}\n\ninterface AmazonStorageClientCreateOptions {\n\tbucket: string;\n\tregion?: string;\n\tacl?: AmazonCannedAccessControlList;\n\taccessKeyId?: string;\n\tsecretAccessKey?: string;\n\tendpoint?: string;\n\tforcePathStyle?: boolean;\n\tcache?: Cache;\n}\n","/**\n * The S3 driver — `s3://` URLs to an {@link AmazonStorageClient}.\n *\n * Registered by whoever assembles the application, so neutral code can build a\n * client from a URL without importing this file or the AWS SDK behind it.\n */\n\nimport { AmazonStorageClient } from './AmazonStorageClient';\nimport type { StorageDriver } from './registry';\nimport { parse } from './s3Url';\n\nexport const s3Driver: StorageDriver = {\n\tscheme: 's3:',\n\tcreate(url) {\n\t\t// The URL's own key pair, when it has one, wins over the SDK's default\n\t\t// chain; without one the client resolves credentials as it always has.\n\t\tconst {\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t\taccessKeyId,\n\t\t\tsecretAccessKey,\n\t\t} = parse(url);\n\t\treturn AmazonStorageClient.create({\n\t\t\tbucket,\n\t\t\tregion,\n\t\t\tendpoint,\n\t\t\tforcePathStyle,\n\t\t\taccessKeyId,\n\t\t\tsecretAccessKey,\n\t\t});\n\t},\n};\n"],"mappings":";;;;;;;;;;;;;;;;AAmBA,eAAsB,aACrB,WACA,YACA,IACa;CACb,MAAM,OAAO,MACX,UAAU,oBAAoB,CAAC,CAC/B,UAAU,WAAW,aAAa;EAClC,MAAM,SAAS;EACf,YAAY;GAAE,qBAAqB;GAAW,GAAG;EAAW;CAC7D,CAAC;CACF,IAAI;EACH,OAAO,MAAM,GAAG;CACjB,SAAS,OAAO;EACf,IAAI,iBAAiB,OAAO,KAAK,gBAAgB,KAAK;EACtD,KAAK,UAAU,EAAE,MAAM,eAAe,MAAM,CAAC;EAC7C,MAAM;CACP,UAAU;EACT,K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