@geekmidas/storage 10.0.0-alpha.98 → 10.0.0

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.
Files changed (66) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/README.md +13 -49
  3. package/dist/AmazonStorageClient-BHpWawGD.d.mts +45 -0
  4. package/dist/AmazonStorageClient-BHpWawGD.d.mts.map +1 -0
  5. package/dist/AmazonStorageClient-BNoY2PL6.d.cts +45 -0
  6. package/dist/AmazonStorageClient-BNoY2PL6.d.cts.map +1 -0
  7. package/dist/AmazonStorageClient-BvoZepKJ.cjs +161 -0
  8. package/dist/AmazonStorageClient-BvoZepKJ.cjs.map +1 -0
  9. package/dist/AmazonStorageClient-Dz-KCJ_4.mjs +127 -0
  10. package/dist/AmazonStorageClient-Dz-KCJ_4.mjs.map +1 -0
  11. package/dist/AmazonStorageClient.cjs +5 -0
  12. package/dist/AmazonStorageClient.d.cts +3 -0
  13. package/dist/AmazonStorageClient.d.mts +3 -0
  14. package/dist/AmazonStorageClient.mjs +4 -0
  15. package/dist/StorageClient-4ADnTPnJ.mjs +11 -0
  16. package/dist/StorageClient-4ADnTPnJ.mjs.map +1 -0
  17. package/dist/StorageClient-BDRVrMJj.cjs +17 -0
  18. package/dist/StorageClient-BDRVrMJj.cjs.map +1 -0
  19. package/dist/{registry-ClEvHGpM.d.cts → StorageClient-COs9SOsU.d.cts} +5 -27
  20. package/dist/StorageClient-COs9SOsU.d.cts.map +1 -0
  21. package/dist/{registry-ClEvHGpM.d.mts → StorageClient-DH4tP-Mf.d.mts} +5 -27
  22. package/dist/StorageClient-DH4tP-Mf.d.mts.map +1 -0
  23. package/dist/StorageClient.cjs +3 -0
  24. package/dist/StorageClient.d.cts +2 -0
  25. package/dist/StorageClient.d.mts +2 -0
  26. package/dist/StorageClient.mjs +3 -0
  27. package/dist/aws.cjs +4 -198
  28. package/dist/aws.d.cts +3 -50
  29. package/dist/aws.d.mts +3 -50
  30. package/dist/aws.mjs +3 -189
  31. package/dist/index.cjs +0 -55
  32. package/dist/index.d.cts +2 -87
  33. package/dist/index.d.mts +2 -87
  34. package/dist/index.mjs +0 -45
  35. package/package.json +9 -27
  36. package/src/AmazonStorageClient.ts +207 -0
  37. package/src/StorageClient.ts +108 -0
  38. package/src/__tests__/AmazonStorageClient.spec.ts +606 -0
  39. package/src/__tests__/StorageClient.spec.ts +98 -0
  40. package/src/aws.ts +11 -0
  41. package/src/index.ts +1 -0
  42. package/tsconfig.json +9 -0
  43. package/tsdown.config.ts +9 -0
  44. package/dist/aws.cjs.map +0 -1
  45. package/dist/aws.d.cts.map +0 -1
  46. package/dist/aws.d.mts.map +0 -1
  47. package/dist/aws.mjs.map +0 -1
  48. package/dist/errors-BB8jdHif.cjs +0 -157
  49. package/dist/errors-BB8jdHif.cjs.map +0 -1
  50. package/dist/errors-COXs1iNg.mjs +0 -116
  51. package/dist/errors-COXs1iNg.mjs.map +0 -1
  52. package/dist/index.cjs.map +0 -1
  53. package/dist/index.d.cts.map +0 -1
  54. package/dist/index.d.mts.map +0 -1
  55. package/dist/index.mjs.map +0 -1
  56. package/dist/registry-ClEvHGpM.d.cts.map +0 -1
  57. package/dist/registry-ClEvHGpM.d.mts.map +0 -1
  58. package/dist/rolldown-runtime-D7D4PA-g.mjs +0 -13
  59. package/dist/s3Url.cjs +0 -109
  60. package/dist/s3Url.cjs.map +0 -1
  61. package/dist/s3Url.d.cts +0 -52
  62. package/dist/s3Url.d.cts.map +0 -1
  63. package/dist/s3Url.d.mts +0 -52
  64. package/dist/s3Url.d.mts.map +0 -1
  65. package/dist/s3Url.mjs +0 -90
  66. package/dist/s3Url.mjs.map +0 -1
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageClient-BDRVrMJj.cjs","names":[],"sources":["../src/StorageClient.ts"],"sourcesContent":["import type { Cache } from '@geekmidas/cache';\nexport interface DocumentVersion {\n\tid: string;\n\tcreatedAt: Date;\n}\n\nexport enum StorageProvider {\n\tAWSS3 = 'geekimdas.toolbox.storage.aws.s3',\n\tGCP = 'geekimdas.toolbox.storage.gcp',\n\tAZURE = 'geekimdas.toolbox.storage.azure',\n}\n\nexport interface StorageClient {\n\treadonly provider: StorageProvider;\n\treadonly cache?: Cache;\n\t/**\n\t * Get a URL to upload a file to.\n\t *\n\t * @param params - The parameters to get the upload URL for.\n\t * @param expiresIn - The number of minutes the URL should be valid for.\n\t * Defaults to 60 * 60 (1 hour).\n\t * @returns A promise that resolves to the upload URL.\n\t */\n\tgetUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;\n\n\t/**\n\t * Get a URL to download a file from.\n\t *\n\t * @param file - The file to get the download URL for.\n\t * @param expiresIn - The number of seconds the URL should be valid for.\n\t * Defaults to 60 * 60 (1 hour).\n\t * @returns A promise that resolves to the download URL.\n\t */\n\tgetDownloadURL(file: File, expiresIn?: number): Promise<string>;\n\t/**\n\t * Get the versions for a key.\n\t *\n\t * @param key - The key to get the versions for.\n\t */\n\tgetVersions(key: string): Promise<DocumentVersion[]>;\n\n\t/**\n\t * Get a URL to download a version of a file from.\n\t *\n\t * @param file - The file to get the download URL for.\n\t * @param versionId - The version ID to get the download URL for.\n\t */\n\tgetVersionDownloadURL(file: File, versionId: string): Promise<string>;\n\t/**\n\t *\n\t * @param key - The key to upload the file to.\n\t * @param data - The data to upload.\n\t * @param contentType - The content type of the data.\n\t */\n\tupload(\n\t\tkey: string,\n\t\tdata: string | Buffer,\n\t\tcontentType: string,\n\t): Promise<void>;\n\t/**\n\t * Get a URL to upload a file to.\n\t *\n\t * @param params - The parameters to get the upload URL for.\n\t * @param expiresIn - The number of minutes the URL should be valid for.\n\t */\n\tgetUpload(\n\t\tparams: GetUploadParams,\n\t\texpiresIn?: number,\n\t): Promise<GetUploadResponse>;\n\t/**\n\t * Delete a file from storage.\n\t *\n\t * @param key - The key (path) of the file to delete.\n\t */\n\tdelete(key: string): Promise<void>;\n}\n\nexport interface GetUploadParams {\n\tpath: string;\n\tcontentType: string;\n\tcontentLength: number;\n}\n\nexport type UploadField = {\n\tkey: string;\n\tvalue: string;\n};\n\nexport type GetUploadResponse = {\n\turl: string;\n\tfields: UploadField[];\n};\n\nexport interface File {\n\tname?: string;\n\tpath: string;\n\t/**\n\t * Override the Content-Type of the response.\n\t * Useful when the stored object's content type differs from what the browser should receive.\n\t */\n\tresponseContentType?: string;\n\t/**\n\t * Control how the browser handles the file.\n\t * - `'attachment'` — prompts download (default when `name` is set)\n\t * - `'inline'` — displays in the browser\n\t */\n\tdisposition?: 'inline' | 'attachment';\n}\n"],"mappings":";;AAMA,IAAY,8DAAL;AACN;AACA;AACA;;AACA"}
@@ -1,4 +1,5 @@
1
1
  import { Cache } from "@geekmidas/cache";
2
+
2
3
  //#region src/StorageClient.d.ts
3
4
  interface DocumentVersion {
4
5
  id: string;
@@ -7,7 +8,7 @@ interface DocumentVersion {
7
8
  declare enum StorageProvider {
8
9
  AWSS3 = "geekimdas.toolbox.storage.aws.s3",
9
10
  GCP = "geekimdas.toolbox.storage.gcp",
10
- AZURE = "geekimdas.toolbox.storage.azure"
11
+ AZURE = "geekimdas.toolbox.storage.azure",
11
12
  }
12
13
  interface StorageClient {
13
14
  readonly provider: StorageProvider;
@@ -92,30 +93,7 @@ interface File {
92
93
  */
93
94
  disposition?: 'inline' | 'attachment';
94
95
  }
96
+ //# sourceMappingURL=StorageClient.d.ts.map
95
97
  //#endregion
96
- //#region src/registry.d.ts
97
- /**
98
- * Builds a client from a URL it recognises.
99
- *
100
- * The driver owns parsing: the shape of an `s3://` URL is S3's business, and a
101
- * `gs://` driver shares nothing with it beyond this signature.
102
- */
103
- interface StorageDriver {
104
- /** The URL scheme this driver handles, including the colon — e.g. `'s3:'`. */
105
- readonly scheme: string;
106
- create(url: string): StorageClient;
107
- }
108
- /** Make a driver available to {@link createStorageClient}. Idempotent. */
109
- declare function registerStorageDriver(driver: StorageDriver): void;
110
- /** Which schemes are currently registered — the useful half of a failure. */
111
- declare function registeredStorageSchemes(): string[];
112
- /**
113
- * Build a client for a URL.
114
- *
115
- * @throws {UnregisteredStorageScheme} when no driver handles the scheme, which
116
- * in practice means the build pinned a different one, or nothing registered.
117
- */
118
- declare function createStorageClient(url: string): StorageClient;
119
- //#endregion
120
- export { DocumentVersion as a, GetUploadResponse as c, registeredStorageSchemes as i, StorageClient as l, createStorageClient as n, File as o, registerStorageDriver as r, GetUploadParams as s, StorageDriver as t, StorageProvider as u };
121
- //# sourceMappingURL=registry-ClEvHGpM.d.cts.map
98
+ export { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient, StorageProvider, UploadField };
99
+ //# sourceMappingURL=StorageClient-COs9SOsU.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageClient-COs9SOsU.d.cts","names":[],"sources":["../src/StorageClient.ts"],"sourcesContent":[],"mappings":";;;UACiB,eAAA;;EAAA,SAAA,EAEL,IAFK;AAKjB;AAMiB,aANL,eAAA;EAMkB,KAAA,GAAA,kCAAA;EAAA,GACV,GAAA,+BAAA;EAAe,KACjB,GAAA,iCAAA;;AAS0C,UAX3C,aAAA,CAW2C;EAAO,SAU7C,QAAA,EApBF,eAoBE;EAAI,SAAuB,KAAA,CAAA,EAnB/B,KAmB+B;EAAO;;;;;;;;EAmC3B,YAAzB,CAAA,MAAA,EA7CkB,eA6ClB,EAAA,SAAA,CAAA,EAAA,MAAA,CAAA,EA7CwD,OA6CxD,CAAA,MAAA,CAAA;EAAO;AAMkB;AAG7B;AAMA;AAKA;AAKA;;;uBA5DsB,2BAA2B;;;;;;4BAMtB,QAAQ;;;;;;;8BAQN,0BAA0B;;;;;;;qCAStC,8BAEb;;;;;;;oBAQM,sCAEN,QAAQ;;;;;;uBAMU;;UAGL,eAAA;;;;;KAML,WAAA;;;;KAKA,iBAAA;;UAEH;;UAGQ,IAAA"}
@@ -1,4 +1,5 @@
1
1
  import { Cache } from "@geekmidas/cache";
2
+
2
3
  //#region src/StorageClient.d.ts
3
4
  interface DocumentVersion {
4
5
  id: string;
@@ -7,7 +8,7 @@ interface DocumentVersion {
7
8
  declare enum StorageProvider {
8
9
  AWSS3 = "geekimdas.toolbox.storage.aws.s3",
9
10
  GCP = "geekimdas.toolbox.storage.gcp",
10
- AZURE = "geekimdas.toolbox.storage.azure"
11
+ AZURE = "geekimdas.toolbox.storage.azure",
11
12
  }
12
13
  interface StorageClient {
13
14
  readonly provider: StorageProvider;
@@ -92,30 +93,7 @@ interface File {
92
93
  */
93
94
  disposition?: 'inline' | 'attachment';
94
95
  }
96
+ //# sourceMappingURL=StorageClient.d.ts.map
95
97
  //#endregion
96
- //#region src/registry.d.ts
97
- /**
98
- * Builds a client from a URL it recognises.
99
- *
100
- * The driver owns parsing: the shape of an `s3://` URL is S3's business, and a
101
- * `gs://` driver shares nothing with it beyond this signature.
102
- */
103
- interface StorageDriver {
104
- /** The URL scheme this driver handles, including the colon — e.g. `'s3:'`. */
105
- readonly scheme: string;
106
- create(url: string): StorageClient;
107
- }
108
- /** Make a driver available to {@link createStorageClient}. Idempotent. */
109
- declare function registerStorageDriver(driver: StorageDriver): void;
110
- /** Which schemes are currently registered — the useful half of a failure. */
111
- declare function registeredStorageSchemes(): string[];
112
- /**
113
- * Build a client for a URL.
114
- *
115
- * @throws {UnregisteredStorageScheme} when no driver handles the scheme, which
116
- * in practice means the build pinned a different one, or nothing registered.
117
- */
118
- declare function createStorageClient(url: string): StorageClient;
119
- //#endregion
120
- export { DocumentVersion as a, GetUploadResponse as c, registeredStorageSchemes as i, StorageClient as l, createStorageClient as n, File as o, registerStorageDriver as r, GetUploadParams as s, StorageDriver as t, StorageProvider as u };
121
- //# sourceMappingURL=registry-ClEvHGpM.d.mts.map
98
+ export { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient, StorageProvider, UploadField };
99
+ //# sourceMappingURL=StorageClient-DH4tP-Mf.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"StorageClient-DH4tP-Mf.d.mts","names":[],"sources":["../src/StorageClient.ts"],"sourcesContent":[],"mappings":";;;UACiB,eAAA;;EAAA,SAAA,EAEL,IAFK;AAKjB;AAMiB,aANL,eAAA;EAMkB,KAAA,GAAA,kCAAA;EAAA,GACV,GAAA,+BAAA;EAAe,KACjB,GAAA,iCAAA;;AAS0C,UAX3C,aAAA,CAW2C;EAAO,SAU7C,QAAA,EApBF,eAoBE;EAAI,SAAuB,KAAA,CAAA,EAnB/B,KAmB+B;EAAO;;;;;;;;EAmC3B,YAAzB,CAAA,MAAA,EA7CkB,eA6ClB,EAAA,SAAA,CAAA,EAAA,MAAA,CAAA,EA7CwD,OA6CxD,CAAA,MAAA,CAAA;EAAO;AAMkB;AAG7B;AAMA;AAKA;AAKA;;;uBA5DsB,2BAA2B;;;;;;4BAMtB,QAAQ;;;;;;;8BAQN,0BAA0B;;;;;;;qCAStC,8BAEb;;;;;;;oBAQM,sCAEN,QAAQ;;;;;;uBAMU;;UAGL,eAAA;;;;;KAML,WAAA;;;;KAKA,iBAAA;;UAEH;;UAGQ,IAAA"}
@@ -0,0 +1,3 @@
1
+ const require_StorageClient = require('./StorageClient-BDRVrMJj.cjs');
2
+
3
+ exports.StorageProvider = require_StorageClient.StorageProvider;
@@ -0,0 +1,2 @@
1
+ import { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient, StorageProvider, UploadField } from "./StorageClient-COs9SOsU.cjs";
2
+ export { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient, StorageProvider, UploadField };
@@ -0,0 +1,2 @@
1
+ import { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient, StorageProvider, UploadField } from "./StorageClient-DH4tP-Mf.mjs";
2
+ export { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient, StorageProvider, UploadField };
@@ -0,0 +1,3 @@
1
+ import { StorageProvider } from "./StorageClient-4ADnTPnJ.mjs";
2
+
3
+ export { StorageProvider };
package/dist/aws.cjs CHANGED
@@ -1,199 +1,5 @@
1
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_s3Url = require("./s3Url.cjs");
3
- let _aws_sdk_client_s3 = require("@aws-sdk/client-s3");
4
- let _aws_sdk_s3_presigned_post = require("@aws-sdk/s3-presigned-post");
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
37
- //#region src/AmazonStorageClient.ts
38
- var AmazonStorageClient = class AmazonStorageClient {
39
- client;
40
- bucket;
41
- acl;
42
- cache;
43
- provider = "geekimdas.toolbox.storage.aws.s3";
44
- static create(options) {
45
- const { bucket, region, accessKeyId, acl, endpoint, secretAccessKey, forcePathStyle = false, cache } = options;
46
- const client = new _aws_sdk_client_s3.S3Client({
47
- region,
48
- credentials: accessKeyId && secretAccessKey ? {
49
- accessKeyId,
50
- secretAccessKey
51
- } : void 0,
52
- endpoint,
53
- forcePathStyle
54
- });
55
- return new AmazonStorageClient(client, bucket, acl, cache);
56
- }
57
- constructor(client, bucket, acl = "authenticated-read", cache) {
58
- this.client = client;
59
- this.bucket = bucket;
60
- this.acl = acl;
61
- this.cache = cache;
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
- }
71
- createGetObjectCommand(file, overrides) {
72
- const disposition = file.disposition ?? (file.name ? "attachment" : void 0);
73
- const ResponseContentDisposition = disposition ? file.name ? `${disposition}; filename=${encodeURIComponent(file.name)}` : disposition : void 0;
74
- return new _aws_sdk_client_s3.GetObjectCommand({
75
- Bucket: this.bucket,
76
- Key: file.path,
77
- ResponseContentDisposition,
78
- ResponseContentType: file.responseContentType,
79
- ...overrides
80
- });
81
- }
82
- getVersionDownloadURL(file, versionId) {
83
- const command = this.createGetObjectCommand(file, { VersionId: versionId });
84
- return this.traced("presign", () => (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn: 86400 }), { "storage.presign.method": "GET" });
85
- }
86
- async getVersions(key) {
87
- const command = new _aws_sdk_client_s3.ListObjectVersionsCommand({
88
- Bucket: this.bucket,
89
- Prefix: key
90
- });
91
- const { Versions = [] } = await this.traced("list", () => this.client.send(command));
92
- return Versions.map((version) => ({
93
- id: version.VersionId || "",
94
- createdAt: version.LastModified || /* @__PURE__ */ new Date()
95
- }));
96
- }
97
- async getDownloadURL(file, expiresIn = 3600) {
98
- const cacheKey = `download-url:${file.path}`;
99
- const cachedURL = await this.cache?.get(cacheKey);
100
- if (cachedURL) return cachedURL;
101
- const command = this.createGetObjectCommand(file);
102
- const url = await this.traced("presign", () => (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn }), { "storage.presign.method": "GET" });
103
- const cacheExpiration = Math.max(expiresIn - 60, 0);
104
- if (cacheExpiration) await this.cache?.set(cacheKey, url, cacheExpiration);
105
- return url;
106
- }
107
- async getUploadURL(params, expiresIn = 3600) {
108
- const command = new _aws_sdk_client_s3.PutObjectCommand({
109
- Bucket: this.bucket,
110
- Key: params.path,
111
- ContentType: params.contentType,
112
- ContentLength: params.contentLength
113
- });
114
- return this.traced("presign", () => (0, _aws_sdk_s3_request_presigner.getSignedUrl)(this.client, command, { expiresIn }), { "storage.presign.method": "PUT" });
115
- }
116
- async getUpload(params, expiresIn = 5) {
117
- const { path } = params;
118
- const { fields: values, url } = await this.traced("presign", () => (0, _aws_sdk_s3_presigned_post.createPresignedPost)(this.client, {
119
- Expires: expiresIn * 60,
120
- Bucket: this.bucket,
121
- Fields: { acl: this.acl },
122
- Conditions: [],
123
- Key: path
124
- }), { "storage.presign.method": "POST" });
125
- return {
126
- url,
127
- fields: Object.keys(values).map((key) => ({
128
- key,
129
- value: values[key] || ""
130
- }))
131
- };
132
- }
133
- async upload(key, data, contentType) {
134
- const Body = typeof data === "string" ? Buffer.from(data, "base64") : data;
135
- const params = {
136
- Bucket: this.bucket,
137
- Key: key,
138
- Body,
139
- ContentType: contentType
140
- };
141
- const command = new _aws_sdk_client_s3.PutObjectCommand(params);
142
- await this.traced("put", () => this.client.send(command), {
143
- "storage.object.size": Body.length,
144
- "storage.object.content_type": contentType
145
- });
146
- }
147
- async delete(key) {
148
- const command = new _aws_sdk_client_s3.DeleteObjectCommand({
149
- Bucket: this.bucket,
150
- Key: key
151
- });
152
- await this.traced("delete", () => this.client.send(command));
153
- }
154
- };
155
- let AmazonCannedAccessControlList = /* @__PURE__ */ function(AmazonCannedAccessControlList) {
156
- AmazonCannedAccessControlList["AuthenticatedRead"] = "authenticated-read";
157
- AmazonCannedAccessControlList["Private"] = "private";
158
- AmazonCannedAccessControlList["PublicRead"] = "public-read";
159
- AmazonCannedAccessControlList["PublicReadWrite"] = "public-read-write";
160
- AmazonCannedAccessControlList["AwsExecRead"] = "aws-exec-read";
161
- AmazonCannedAccessControlList["BucketOwnerRead"] = "bucket-owner-read";
162
- AmazonCannedAccessControlList["BucketOwnerFullControl"] = "bucket-owner-full-control";
163
- AmazonCannedAccessControlList["LogDeliveryWrite"] = "log-delivery-write";
164
- return AmazonCannedAccessControlList;
165
- }({});
166
- //#endregion
167
- //#region src/s3Driver.ts
168
- /**
169
- * The S3 driver — `s3://` URLs to an {@link AmazonStorageClient}.
170
- *
171
- * Registered by whoever assembles the application, so neutral code can build a
172
- * client from a URL without importing this file or the AWS SDK behind it.
173
- */
174
- const s3Driver = {
175
- scheme: "s3:",
176
- create(url) {
177
- const { bucket, region, endpoint, forcePathStyle, accessKeyId, secretAccessKey } = require_s3Url.parse(url);
178
- return AmazonStorageClient.create({
179
- bucket,
180
- region,
181
- endpoint,
182
- forcePathStyle,
183
- accessKeyId,
184
- secretAccessKey
185
- });
186
- }
187
- };
188
- //#endregion
189
- exports.AmazonCannedAccessControlList = AmazonCannedAccessControlList;
190
- exports.AmazonStorageClient = AmazonStorageClient;
191
- exports.s3Driver = s3Driver;
192
- Object.defineProperty(exports, "s3Url", {
193
- enumerable: true,
194
- get: function() {
195
- return require_s3Url.s3Url_exports;
196
- }
197
- });
1
+ const require_AmazonStorageClient = require('./AmazonStorageClient-BvoZepKJ.cjs');
2
+ require('./StorageClient-BDRVrMJj.cjs');
198
3
 
199
- //# sourceMappingURL=aws.cjs.map
4
+ exports.AmazonCannedAccessControlList = require_AmazonStorageClient.AmazonCannedAccessControlList;
5
+ exports.AmazonStorageClient = require_AmazonStorageClient.AmazonStorageClient;
package/dist/aws.d.cts CHANGED
@@ -1,50 +1,3 @@
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-ClEvHGpM.cjs";
2
- import { S3Address, t as s3Url_d_exports } from "./s3Url.cjs";
3
- import { S3Client } from "@aws-sdk/client-s3";
4
- import { Cache } from "@geekmidas/cache";
5
- //#region src/AmazonStorageClient.d.ts
6
- export declare class AmazonStorageClient implements StorageClient {
7
- private readonly client;
8
- private readonly bucket;
9
- private readonly acl;
10
- readonly cache?: Cache;
11
- readonly provider = StorageProvider.AWSS3;
12
- static create(options: AmazonStorageClientCreateOptions): AmazonStorageClient;
13
- constructor(client: S3Client, bucket: string, acl?: AmazonCannedAccessControlList, cache?: Cache);
14
- /** A storage span on this bucket. */
15
- private traced;
16
- private createGetObjectCommand;
17
- getVersionDownloadURL(file: File, versionId: string): Promise<string>;
18
- getVersions(key: string): Promise<DocumentVersion[]>;
19
- getDownloadURL(file: File, expiresIn?: number): Promise<string>;
20
- getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
21
- getUpload(params: GetUploadParams, expiresIn?: number): Promise<GetUploadResponse>;
22
- upload(key: string, data: string | Buffer, contentType: string): Promise<void>;
23
- delete(key: string): Promise<void>;
24
- }
25
- export declare enum AmazonCannedAccessControlList {
26
- AuthenticatedRead = "authenticated-read",
27
- Private = "private",
28
- PublicRead = "public-read",
29
- PublicReadWrite = "public-read-write",
30
- AwsExecRead = "aws-exec-read",
31
- BucketOwnerRead = "bucket-owner-read",
32
- BucketOwnerFullControl = "bucket-owner-full-control",
33
- LogDeliveryWrite = "log-delivery-write"
34
- }
35
- interface AmazonStorageClientCreateOptions {
36
- bucket: string;
37
- region?: string;
38
- acl?: AmazonCannedAccessControlList;
39
- accessKeyId?: string;
40
- secretAccessKey?: string;
41
- endpoint?: string;
42
- forcePathStyle?: boolean;
43
- cache?: Cache;
44
- }
45
- //#endregion
46
- //#region src/s3Driver.d.ts
47
- export declare const s3Driver: StorageDriver;
48
- //#endregion
49
- export { type DocumentVersion, type File, type GetUploadParams, type GetUploadResponse, type S3Address, type StorageClient, s3Url_d_exports as s3Url };
50
- //# sourceMappingURL=aws.d.cts.map
1
+ import { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient } from "./StorageClient-COs9SOsU.cjs";
2
+ import { AmazonCannedAccessControlList, AmazonStorageClient } from "./AmazonStorageClient-BNoY2PL6.cjs";
3
+ export { AmazonCannedAccessControlList, AmazonStorageClient, DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient };
package/dist/aws.d.mts CHANGED
@@ -1,50 +1,3 @@
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-ClEvHGpM.mjs";
2
- import { S3Address, t as s3Url_d_exports } from "./s3Url.mjs";
3
- import { S3Client } from "@aws-sdk/client-s3";
4
- import { Cache } from "@geekmidas/cache";
5
- //#region src/AmazonStorageClient.d.ts
6
- export declare class AmazonStorageClient implements StorageClient {
7
- private readonly client;
8
- private readonly bucket;
9
- private readonly acl;
10
- readonly cache?: Cache;
11
- readonly provider = StorageProvider.AWSS3;
12
- static create(options: AmazonStorageClientCreateOptions): AmazonStorageClient;
13
- constructor(client: S3Client, bucket: string, acl?: AmazonCannedAccessControlList, cache?: Cache);
14
- /** A storage span on this bucket. */
15
- private traced;
16
- private createGetObjectCommand;
17
- getVersionDownloadURL(file: File, versionId: string): Promise<string>;
18
- getVersions(key: string): Promise<DocumentVersion[]>;
19
- getDownloadURL(file: File, expiresIn?: number): Promise<string>;
20
- getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
21
- getUpload(params: GetUploadParams, expiresIn?: number): Promise<GetUploadResponse>;
22
- upload(key: string, data: string | Buffer, contentType: string): Promise<void>;
23
- delete(key: string): Promise<void>;
24
- }
25
- export declare enum AmazonCannedAccessControlList {
26
- AuthenticatedRead = "authenticated-read",
27
- Private = "private",
28
- PublicRead = "public-read",
29
- PublicReadWrite = "public-read-write",
30
- AwsExecRead = "aws-exec-read",
31
- BucketOwnerRead = "bucket-owner-read",
32
- BucketOwnerFullControl = "bucket-owner-full-control",
33
- LogDeliveryWrite = "log-delivery-write"
34
- }
35
- interface AmazonStorageClientCreateOptions {
36
- bucket: string;
37
- region?: string;
38
- acl?: AmazonCannedAccessControlList;
39
- accessKeyId?: string;
40
- secretAccessKey?: string;
41
- endpoint?: string;
42
- forcePathStyle?: boolean;
43
- cache?: Cache;
44
- }
45
- //#endregion
46
- //#region src/s3Driver.d.ts
47
- export declare const s3Driver: StorageDriver;
48
- //#endregion
49
- export { type DocumentVersion, type File, type GetUploadParams, type GetUploadResponse, type S3Address, type StorageClient, s3Url_d_exports as s3Url };
50
- //# sourceMappingURL=aws.d.mts.map
1
+ import { DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient } from "./StorageClient-DH4tP-Mf.mjs";
2
+ import { AmazonCannedAccessControlList, AmazonStorageClient } from "./AmazonStorageClient-BHpWawGD.mjs";
3
+ export { AmazonCannedAccessControlList, AmazonStorageClient, DocumentVersion, File, GetUploadParams, GetUploadResponse, StorageClient };
package/dist/aws.mjs CHANGED
@@ -1,190 +1,4 @@
1
- import { parse, t as s3Url_exports } from "./s3Url.mjs";
2
- import { DeleteObjectCommand, GetObjectCommand, ListObjectVersionsCommand, PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
3
- import { createPresignedPost } from "@aws-sdk/s3-presigned-post";
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
36
- //#region src/AmazonStorageClient.ts
37
- var AmazonStorageClient = class AmazonStorageClient {
38
- client;
39
- bucket;
40
- acl;
41
- cache;
42
- provider = "geekimdas.toolbox.storage.aws.s3";
43
- static create(options) {
44
- const { bucket, region, accessKeyId, acl, endpoint, secretAccessKey, forcePathStyle = false, cache } = options;
45
- const client = new S3Client({
46
- region,
47
- credentials: accessKeyId && secretAccessKey ? {
48
- accessKeyId,
49
- secretAccessKey
50
- } : void 0,
51
- endpoint,
52
- forcePathStyle
53
- });
54
- return new AmazonStorageClient(client, bucket, acl, cache);
55
- }
56
- constructor(client, bucket, acl = "authenticated-read", cache) {
57
- this.client = client;
58
- this.bucket = bucket;
59
- this.acl = acl;
60
- this.cache = cache;
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
- }
70
- createGetObjectCommand(file, overrides) {
71
- const disposition = file.disposition ?? (file.name ? "attachment" : void 0);
72
- const ResponseContentDisposition = disposition ? file.name ? `${disposition}; filename=${encodeURIComponent(file.name)}` : disposition : void 0;
73
- return new GetObjectCommand({
74
- Bucket: this.bucket,
75
- Key: file.path,
76
- ResponseContentDisposition,
77
- ResponseContentType: file.responseContentType,
78
- ...overrides
79
- });
80
- }
81
- getVersionDownloadURL(file, versionId) {
82
- const command = this.createGetObjectCommand(file, { VersionId: versionId });
83
- return this.traced("presign", () => getSignedUrl(this.client, command, { expiresIn: 86400 }), { "storage.presign.method": "GET" });
84
- }
85
- async getVersions(key) {
86
- const command = new ListObjectVersionsCommand({
87
- Bucket: this.bucket,
88
- Prefix: key
89
- });
90
- const { Versions = [] } = await this.traced("list", () => this.client.send(command));
91
- return Versions.map((version) => ({
92
- id: version.VersionId || "",
93
- createdAt: version.LastModified || /* @__PURE__ */ new Date()
94
- }));
95
- }
96
- async getDownloadURL(file, expiresIn = 3600) {
97
- const cacheKey = `download-url:${file.path}`;
98
- const cachedURL = await this.cache?.get(cacheKey);
99
- if (cachedURL) return cachedURL;
100
- const command = this.createGetObjectCommand(file);
101
- const url = await this.traced("presign", () => getSignedUrl(this.client, command, { expiresIn }), { "storage.presign.method": "GET" });
102
- const cacheExpiration = Math.max(expiresIn - 60, 0);
103
- if (cacheExpiration) await this.cache?.set(cacheKey, url, cacheExpiration);
104
- return url;
105
- }
106
- async getUploadURL(params, expiresIn = 3600) {
107
- const command = new PutObjectCommand({
108
- Bucket: this.bucket,
109
- Key: params.path,
110
- ContentType: params.contentType,
111
- ContentLength: params.contentLength
112
- });
113
- return this.traced("presign", () => getSignedUrl(this.client, command, { expiresIn }), { "storage.presign.method": "PUT" });
114
- }
115
- async getUpload(params, expiresIn = 5) {
116
- const { path } = params;
117
- const { fields: values, url } = await this.traced("presign", () => createPresignedPost(this.client, {
118
- Expires: expiresIn * 60,
119
- Bucket: this.bucket,
120
- Fields: { acl: this.acl },
121
- Conditions: [],
122
- Key: path
123
- }), { "storage.presign.method": "POST" });
124
- return {
125
- url,
126
- fields: Object.keys(values).map((key) => ({
127
- key,
128
- value: values[key] || ""
129
- }))
130
- };
131
- }
132
- async upload(key, data, contentType) {
133
- const Body = typeof data === "string" ? Buffer.from(data, "base64") : data;
134
- const params = {
135
- Bucket: this.bucket,
136
- Key: key,
137
- Body,
138
- ContentType: contentType
139
- };
140
- const command = new PutObjectCommand(params);
141
- await this.traced("put", () => this.client.send(command), {
142
- "storage.object.size": Body.length,
143
- "storage.object.content_type": contentType
144
- });
145
- }
146
- async delete(key) {
147
- const command = new DeleteObjectCommand({
148
- Bucket: this.bucket,
149
- Key: key
150
- });
151
- await this.traced("delete", () => this.client.send(command));
152
- }
153
- };
154
- let AmazonCannedAccessControlList = /* @__PURE__ */ function(AmazonCannedAccessControlList) {
155
- AmazonCannedAccessControlList["AuthenticatedRead"] = "authenticated-read";
156
- AmazonCannedAccessControlList["Private"] = "private";
157
- AmazonCannedAccessControlList["PublicRead"] = "public-read";
158
- AmazonCannedAccessControlList["PublicReadWrite"] = "public-read-write";
159
- AmazonCannedAccessControlList["AwsExecRead"] = "aws-exec-read";
160
- AmazonCannedAccessControlList["BucketOwnerRead"] = "bucket-owner-read";
161
- AmazonCannedAccessControlList["BucketOwnerFullControl"] = "bucket-owner-full-control";
162
- AmazonCannedAccessControlList["LogDeliveryWrite"] = "log-delivery-write";
163
- return AmazonCannedAccessControlList;
164
- }({});
165
- //#endregion
166
- //#region src/s3Driver.ts
167
- /**
168
- * The S3 driver — `s3://` URLs to an {@link AmazonStorageClient}.
169
- *
170
- * Registered by whoever assembles the application, so neutral code can build a
171
- * client from a URL without importing this file or the AWS SDK behind it.
172
- */
173
- const s3Driver = {
174
- scheme: "s3:",
175
- create(url) {
176
- const { bucket, region, endpoint, forcePathStyle, accessKeyId, secretAccessKey } = parse(url);
177
- return AmazonStorageClient.create({
178
- bucket,
179
- region,
180
- endpoint,
181
- forcePathStyle,
182
- accessKeyId,
183
- secretAccessKey
184
- });
185
- }
186
- };
187
- //#endregion
188
- export { AmazonCannedAccessControlList, AmazonStorageClient, s3Driver, s3Url_exports as s3Url };
1
+ import "./StorageClient-4ADnTPnJ.mjs";
2
+ import { AmazonCannedAccessControlList, AmazonStorageClient } from "./AmazonStorageClient-Dz-KCJ_4.mjs";
189
3
 
190
- //# sourceMappingURL=aws.mjs.map
4
+ export { AmazonCannedAccessControlList, AmazonStorageClient };