@geekmidas/storage 0.0.5 → 1.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 (51) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/{AmazonStorageClient-tyGekctm.cjs → AmazonStorageClient-BKzXhCX0.cjs} +28 -5
  3. package/dist/AmazonStorageClient-BKzXhCX0.cjs.map +1 -0
  4. package/dist/{AmazonStorageClient-DaktozmE.d.mts → AmazonStorageClient-Bc_LP3K6.d.mts} +6 -5
  5. package/dist/AmazonStorageClient-Bc_LP3K6.d.mts.map +1 -0
  6. package/dist/{AmazonStorageClient-Bkw-x2o2.mjs → AmazonStorageClient-CwUey48q.mjs} +3 -2
  7. package/dist/AmazonStorageClient-CwUey48q.mjs.map +1 -0
  8. package/dist/{AmazonStorageClient-aduVU3_p.d.cts → AmazonStorageClient-DxT8_gTZ.d.cts} +6 -5
  9. package/dist/AmazonStorageClient-DxT8_gTZ.d.cts.map +1 -0
  10. package/dist/AmazonStorageClient.cjs +1 -1
  11. package/dist/AmazonStorageClient.d.cts +2 -2
  12. package/dist/AmazonStorageClient.d.mts +2 -2
  13. package/dist/AmazonStorageClient.mjs +2 -2
  14. package/dist/{StorageClient-CZGIC_fz.mjs → StorageClient-4ADnTPnJ.mjs} +2 -1
  15. package/dist/StorageClient-4ADnTPnJ.mjs.map +1 -0
  16. package/dist/StorageClient-BDRVrMJj.cjs +2 -1
  17. package/dist/StorageClient-BDRVrMJj.cjs.map +1 -0
  18. package/dist/{StorageClient-CveQoxJa.d.mts → StorageClient-BWS26EEf.d.cts} +4 -2
  19. package/dist/StorageClient-BWS26EEf.d.cts.map +1 -0
  20. package/dist/{StorageClient-CcOAW3YV.d.cts → StorageClient-VG7Thpke.d.mts} +4 -2
  21. package/dist/StorageClient-VG7Thpke.d.mts.map +1 -0
  22. package/dist/StorageClient.d.cts +1 -1
  23. package/dist/StorageClient.d.mts +1 -1
  24. package/dist/StorageClient.mjs +1 -1
  25. package/dist/aws.cjs +1 -1
  26. package/dist/aws.d.cts +2 -2
  27. package/dist/aws.d.mts +2 -2
  28. package/dist/aws.mjs +2 -2
  29. package/dist/index.d.cts +1 -1
  30. package/dist/index.d.mts +1 -1
  31. package/package.json +29 -6
  32. package/src/AmazonStorageClient.ts +178 -178
  33. package/src/StorageClient.ts +68 -68
  34. package/src/__tests__/AmazonStorageClient.spec.ts +501 -501
  35. package/src/__tests__/StorageClient.spec.ts +71 -71
  36. package/src/aws.ts +7 -7
  37. package/tsconfig.json +9 -0
  38. package/tsdown.config.ts +5 -5
  39. package/dist/__tests__/AmazonStorageClient.spec.cjs +0 -348
  40. package/dist/__tests__/AmazonStorageClient.spec.d.cts +0 -1
  41. package/dist/__tests__/AmazonStorageClient.spec.d.mts +0 -1
  42. package/dist/__tests__/AmazonStorageClient.spec.mjs +0 -347
  43. package/dist/__tests__/StorageClient.spec.cjs +0 -81
  44. package/dist/__tests__/StorageClient.spec.d.cts +0 -1
  45. package/dist/__tests__/StorageClient.spec.d.mts +0 -1
  46. package/dist/__tests__/StorageClient.spec.mjs +0 -81
  47. package/dist/chunk-DWy1uDak.cjs +0 -39
  48. package/dist/magic-string.es-BvJNBYzY.mjs +0 -1014
  49. package/dist/magic-string.es-CKiDiYoS.cjs +0 -1015
  50. package/dist/vi.bdSIJ99Y-CucmVasy.mjs +0 -14928
  51. package/dist/vi.bdSIJ99Y-D0m6qImj.cjs +0 -14926
@@ -1,195 +1,195 @@
1
1
  import {
2
- GetObjectCommand,
3
- ListObjectVersionsCommand,
4
- PutObjectCommand,
5
- S3Client,
2
+ GetObjectCommand,
3
+ ListObjectVersionsCommand,
4
+ PutObjectCommand,
5
+ S3Client,
6
6
  } from '@aws-sdk/client-s3';
7
7
  import { createPresignedPost } from '@aws-sdk/s3-presigned-post';
8
8
  import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
9
9
 
10
10
  import type { Cache } from '@geekmidas/cache';
11
11
  import {
12
- type DocumentVersion,
13
- type File,
14
- type GetUploadParams,
15
- type GetUploadResponse,
16
- type StorageClient,
17
- StorageProvider,
12
+ type DocumentVersion,
13
+ type File,
14
+ type GetUploadParams,
15
+ type GetUploadResponse,
16
+ type StorageClient,
17
+ StorageProvider,
18
18
  } from './StorageClient';
19
19
 
20
20
  export class AmazonStorageClient implements StorageClient {
21
- readonly provider = StorageProvider.AWSS3;
22
-
23
- static create(
24
- options: AmazonStorageClientCreateOptions,
25
- ): AmazonStorageClient {
26
- const {
27
- bucket,
28
- region,
29
- accessKeyId,
30
- acl,
31
- endpoint,
32
- secretAccessKey,
33
- forcePathStyle = false,
34
- cache,
35
- } = options;
36
- const hasCredentials = accessKeyId && secretAccessKey;
37
- const credentials = hasCredentials
38
- ? { accessKeyId, secretAccessKey }
39
- : undefined;
40
-
41
- const client = new S3Client({
42
- region,
43
- credentials,
44
- endpoint,
45
- forcePathStyle,
46
- });
47
-
48
- return new AmazonStorageClient(client, bucket, acl, cache);
49
- }
50
- constructor(
51
- private readonly client: S3Client,
52
- private readonly bucket: string,
53
- private readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,
54
- readonly cache?: Cache<string>,
55
- ) {}
56
-
57
- getVersionDownloadURL(file: File, versionId: string): Promise<string> {
58
- const ResponseContentDisposition = file.name
59
- ? `attachment; filename=${encodeURIComponent(file.name)}`
60
- : undefined;
61
-
62
- const command = new GetObjectCommand({
63
- Bucket: this.bucket,
64
- Key: file.path,
65
- ResponseContentDisposition,
66
- VersionId: versionId,
67
- });
68
-
69
- return getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 });
70
- }
71
-
72
- async getVersions(key: string): Promise<DocumentVersion[]> {
73
- const command = new ListObjectVersionsCommand({
74
- Bucket: this.bucket,
75
- Prefix: key,
76
- });
77
-
78
- const { Versions = [] } = await this.client.send(command);
79
-
80
- return Versions.map((version) => ({
81
- id: version.VersionId || '',
82
- createdAt: version.LastModified || new Date(),
83
- }));
84
- }
85
-
86
- async getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {
87
- const cacheKey = `download-url:${file.path}`;
88
- const cachedURL = await this.cache?.get(cacheKey);
89
-
90
- if (cachedURL) {
91
- return cachedURL;
92
- }
93
-
94
- const ResponseContentDisposition = file.name
95
- ? `attachment; filename=${encodeURIComponent(file.name)}`
96
- : undefined;
97
-
98
- const command = new GetObjectCommand({
99
- Bucket: this.bucket,
100
- Key: file.path,
101
- ResponseContentDisposition,
102
- });
103
-
104
- const url = await getSignedUrl(this.client, command, { expiresIn });
105
- const cacheExpiration = Math.max(expiresIn - 60, 0);
106
-
107
- if (cacheExpiration) {
108
- await this.cache?.set(cacheKey, url, cacheExpiration);
109
- }
110
-
111
- return url;
112
- }
113
-
114
- async getUploadURL(
115
- params: GetUploadParams,
116
- expiresIn = 60 * 60,
117
- ): Promise<string> {
118
- const command = new PutObjectCommand({
119
- Bucket: this.bucket,
120
- Key: params.path,
121
- ContentType: params.contentType,
122
- ContentLength: params.contentLength,
123
- });
124
-
125
- return getSignedUrl(this.client, command, { expiresIn });
126
- }
127
-
128
- async getUpload(
129
- params: GetUploadParams,
130
- expiresIn = 5,
131
- ): Promise<GetUploadResponse> {
132
- const { path } = params;
133
- const { fields: values, url } = await createPresignedPost(this.client, {
134
- Expires: expiresIn * 60,
135
- Bucket: this.bucket,
136
- Fields: {
137
- acl: this.acl,
138
- },
139
- Conditions: [
140
- // content length restrictions: 0-1MB]
141
- // ['content-length-range', 0, contentLength],
142
- // specify content-type to be more generic- images only
143
- // ['starts-with', '$Content-Type', 'image/'],
144
- // ['starts-with', '$Content-Type', contentType],
145
- ],
146
- Key: path,
147
- });
148
-
149
- const keys = Object.keys(values);
150
- const fields = keys.map((key) => ({ key, value: values[key] || '' }));
151
-
152
- return { url, fields };
153
- }
154
-
155
- async upload(
156
- key: string,
157
- data: string | Buffer,
158
- contentType: string,
159
- ): Promise<void> {
160
- const Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;
161
-
162
- const params = {
163
- Bucket: this.bucket,
164
- Key: key,
165
- Body,
166
- ContentType: contentType,
167
- };
168
-
169
- const command = new PutObjectCommand(params);
170
-
171
- await this.client.send(command);
172
- }
21
+ readonly provider = StorageProvider.AWSS3;
22
+
23
+ static create(
24
+ options: AmazonStorageClientCreateOptions,
25
+ ): AmazonStorageClient {
26
+ const {
27
+ bucket,
28
+ region,
29
+ accessKeyId,
30
+ acl,
31
+ endpoint,
32
+ secretAccessKey,
33
+ forcePathStyle = false,
34
+ cache,
35
+ } = options;
36
+ const hasCredentials = accessKeyId && secretAccessKey;
37
+ const credentials = hasCredentials
38
+ ? { accessKeyId, secretAccessKey }
39
+ : undefined;
40
+
41
+ const client = new S3Client({
42
+ region,
43
+ credentials,
44
+ endpoint,
45
+ forcePathStyle,
46
+ });
47
+
48
+ return new AmazonStorageClient(client, bucket, acl, cache);
49
+ }
50
+ constructor(
51
+ private readonly client: S3Client,
52
+ private readonly bucket: string,
53
+ private readonly acl = AmazonCannedAccessControlList.AuthenticatedRead,
54
+ readonly cache?: Cache,
55
+ ) {}
56
+
57
+ getVersionDownloadURL(file: File, versionId: string): Promise<string> {
58
+ const ResponseContentDisposition = file.name
59
+ ? `attachment; filename=${encodeURIComponent(file.name)}`
60
+ : undefined;
61
+
62
+ const command = new GetObjectCommand({
63
+ Bucket: this.bucket,
64
+ Key: file.path,
65
+ ResponseContentDisposition,
66
+ VersionId: versionId,
67
+ });
68
+
69
+ return getSignedUrl(this.client, command, { expiresIn: 60 * 60 * 24 });
70
+ }
71
+
72
+ async getVersions(key: string): Promise<DocumentVersion[]> {
73
+ const command = new ListObjectVersionsCommand({
74
+ Bucket: this.bucket,
75
+ Prefix: key,
76
+ });
77
+
78
+ const { Versions = [] } = await this.client.send(command);
79
+
80
+ return Versions.map((version) => ({
81
+ id: version.VersionId || '',
82
+ createdAt: version.LastModified || new Date(),
83
+ }));
84
+ }
85
+
86
+ async getDownloadURL(file: File, expiresIn = 60 * 60): Promise<string> {
87
+ const cacheKey = `download-url:${file.path}`;
88
+ const cachedURL = await this.cache?.get<string>(cacheKey);
89
+
90
+ if (cachedURL) {
91
+ return cachedURL;
92
+ }
93
+
94
+ const ResponseContentDisposition = file.name
95
+ ? `attachment; filename=${encodeURIComponent(file.name)}`
96
+ : undefined;
97
+
98
+ const command = new GetObjectCommand({
99
+ Bucket: this.bucket,
100
+ Key: file.path,
101
+ ResponseContentDisposition,
102
+ });
103
+
104
+ const url = await getSignedUrl(this.client, command, { expiresIn });
105
+ const cacheExpiration = Math.max(expiresIn - 60, 0);
106
+
107
+ if (cacheExpiration) {
108
+ await this.cache?.set(cacheKey, url, cacheExpiration);
109
+ }
110
+
111
+ return url;
112
+ }
113
+
114
+ async getUploadURL(
115
+ params: GetUploadParams,
116
+ expiresIn = 60 * 60,
117
+ ): Promise<string> {
118
+ const command = new PutObjectCommand({
119
+ Bucket: this.bucket,
120
+ Key: params.path,
121
+ ContentType: params.contentType,
122
+ ContentLength: params.contentLength,
123
+ });
124
+
125
+ return getSignedUrl(this.client, command, { expiresIn });
126
+ }
127
+
128
+ async getUpload(
129
+ params: GetUploadParams,
130
+ expiresIn = 5,
131
+ ): Promise<GetUploadResponse> {
132
+ const { path } = params;
133
+ const { fields: values, url } = await createPresignedPost(this.client, {
134
+ Expires: expiresIn * 60,
135
+ Bucket: this.bucket,
136
+ Fields: {
137
+ acl: this.acl,
138
+ },
139
+ Conditions: [
140
+ // content length restrictions: 0-1MB]
141
+ // ['content-length-range', 0, contentLength],
142
+ // specify content-type to be more generic- images only
143
+ // ['starts-with', '$Content-Type', 'image/'],
144
+ // ['starts-with', '$Content-Type', contentType],
145
+ ],
146
+ Key: path,
147
+ });
148
+
149
+ const keys = Object.keys(values);
150
+ const fields = keys.map((key) => ({ key, value: values[key] || '' }));
151
+
152
+ return { url, fields };
153
+ }
154
+
155
+ async upload(
156
+ key: string,
157
+ data: string | Buffer,
158
+ contentType: string,
159
+ ): Promise<void> {
160
+ const Body = typeof data === 'string' ? Buffer.from(data, 'base64') : data;
161
+
162
+ const params = {
163
+ Bucket: this.bucket,
164
+ Key: key,
165
+ Body,
166
+ ContentType: contentType,
167
+ };
168
+
169
+ const command = new PutObjectCommand(params);
170
+
171
+ await this.client.send(command);
172
+ }
173
173
  }
174
174
 
175
175
  export enum AmazonCannedAccessControlList {
176
- AuthenticatedRead = 'authenticated-read',
177
- Private = 'private',
178
- PublicRead = 'public-read',
179
- PublicReadWrite = 'public-read-write',
180
- AwsExecRead = 'aws-exec-read',
181
- BucketOwnerRead = 'bucket-owner-read',
182
- BucketOwnerFullControl = 'bucket-owner-full-control',
183
- LogDeliveryWrite = 'log-delivery-write',
176
+ AuthenticatedRead = 'authenticated-read',
177
+ Private = 'private',
178
+ PublicRead = 'public-read',
179
+ PublicReadWrite = 'public-read-write',
180
+ AwsExecRead = 'aws-exec-read',
181
+ BucketOwnerRead = 'bucket-owner-read',
182
+ BucketOwnerFullControl = 'bucket-owner-full-control',
183
+ LogDeliveryWrite = 'log-delivery-write',
184
184
  }
185
185
 
186
186
  interface AmazonStorageClientCreateOptions {
187
- bucket: string;
188
- region?: string;
189
- acl?: AmazonCannedAccessControlList;
190
- accessKeyId?: string;
191
- secretAccessKey?: string;
192
- endpoint?: string;
193
- forcePathStyle?: boolean;
194
- cache?: Cache<string>;
187
+ bucket: string;
188
+ region?: string;
189
+ acl?: AmazonCannedAccessControlList;
190
+ accessKeyId?: string;
191
+ secretAccessKey?: string;
192
+ endpoint?: string;
193
+ forcePathStyle?: boolean;
194
+ cache?: Cache;
195
195
  }
@@ -1,91 +1,91 @@
1
1
  import type { Cache } from '@geekmidas/cache';
2
2
  export interface DocumentVersion {
3
- id: string;
4
- createdAt: Date;
3
+ id: string;
4
+ createdAt: Date;
5
5
  }
6
6
 
7
7
  export enum StorageProvider {
8
- AWSS3 = 'geekimdas.toolbox.storage.aws.s3',
9
- GCP = 'geekimdas.toolbox.storage.gcp',
10
- AZURE = 'geekimdas.toolbox.storage.azure',
8
+ AWSS3 = 'geekimdas.toolbox.storage.aws.s3',
9
+ GCP = 'geekimdas.toolbox.storage.gcp',
10
+ AZURE = 'geekimdas.toolbox.storage.azure',
11
11
  }
12
12
 
13
13
  export interface StorageClient {
14
- readonly provider: StorageProvider;
15
- readonly cache?: Cache<string>;
16
- /**
17
- * Get a URL to upload a file to.
18
- *
19
- * @param params - The parameters to get the upload URL for.
20
- * @param expiresIn - The number of minutes the URL should be valid for.
21
- * Defaults to 60 * 60 (1 hour).
22
- * @returns A promise that resolves to the upload URL.
23
- */
24
- getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
14
+ readonly provider: StorageProvider;
15
+ readonly cache?: Cache;
16
+ /**
17
+ * Get a URL to upload a file to.
18
+ *
19
+ * @param params - The parameters to get the upload URL for.
20
+ * @param expiresIn - The number of minutes the URL should be valid for.
21
+ * Defaults to 60 * 60 (1 hour).
22
+ * @returns A promise that resolves to the upload URL.
23
+ */
24
+ getUploadURL(params: GetUploadParams, expiresIn?: number): Promise<string>;
25
25
 
26
- /**
27
- * Get a URL to download a file from.
28
- *
29
- * @param file - The file to get the download URL for.
30
- * @param expiresIn - The number of seconds the URL should be valid for.
31
- * Defaults to 60 * 60 (1 hour).
32
- * @returns A promise that resolves to the download URL.
33
- */
34
- getDownloadURL(file: File, expiresIn?: number): Promise<string>;
35
- /**
36
- * Get the versions for a key.
37
- *
38
- * @param key - The key to get the versions for.
39
- */
40
- getVersions(key: string): Promise<DocumentVersion[]>;
26
+ /**
27
+ * Get a URL to download a file from.
28
+ *
29
+ * @param file - The file to get the download URL for.
30
+ * @param expiresIn - The number of seconds the URL should be valid for.
31
+ * Defaults to 60 * 60 (1 hour).
32
+ * @returns A promise that resolves to the download URL.
33
+ */
34
+ getDownloadURL(file: File, expiresIn?: number): Promise<string>;
35
+ /**
36
+ * Get the versions for a key.
37
+ *
38
+ * @param key - The key to get the versions for.
39
+ */
40
+ getVersions(key: string): Promise<DocumentVersion[]>;
41
41
 
42
- /**
43
- * Get a URL to download a version of a file from.
44
- *
45
- * @param file - The file to get the download URL for.
46
- * @param versionId - The version ID to get the download URL for.
47
- */
48
- getVersionDownloadURL(file: File, versionId: string): Promise<string>;
49
- /**
50
- *
51
- * @param key - The key to upload the file to.
52
- * @param data - The data to upload.
53
- * @param contentType - The content type of the data.
54
- */
55
- upload(
56
- key: string,
57
- data: string | Buffer,
58
- contentType: string,
59
- ): Promise<void>;
60
- /**
61
- * Get a URL to upload a file to.
62
- *
63
- * @param params - The parameters to get the upload URL for.
64
- * @param expiresIn - The number of minutes the URL should be valid for.
65
- */
66
- getUpload(
67
- params: GetUploadParams,
68
- expiresIn?: number,
69
- ): Promise<GetUploadResponse>;
42
+ /**
43
+ * Get a URL to download a version of a file from.
44
+ *
45
+ * @param file - The file to get the download URL for.
46
+ * @param versionId - The version ID to get the download URL for.
47
+ */
48
+ getVersionDownloadURL(file: File, versionId: string): Promise<string>;
49
+ /**
50
+ *
51
+ * @param key - The key to upload the file to.
52
+ * @param data - The data to upload.
53
+ * @param contentType - The content type of the data.
54
+ */
55
+ upload(
56
+ key: string,
57
+ data: string | Buffer,
58
+ contentType: string,
59
+ ): Promise<void>;
60
+ /**
61
+ * Get a URL to upload a file to.
62
+ *
63
+ * @param params - The parameters to get the upload URL for.
64
+ * @param expiresIn - The number of minutes the URL should be valid for.
65
+ */
66
+ getUpload(
67
+ params: GetUploadParams,
68
+ expiresIn?: number,
69
+ ): Promise<GetUploadResponse>;
70
70
  }
71
71
 
72
72
  export interface GetUploadParams {
73
- path: string;
74
- contentType: string;
75
- contentLength: number;
73
+ path: string;
74
+ contentType: string;
75
+ contentLength: number;
76
76
  }
77
77
 
78
78
  export type UploadField = {
79
- key: string;
80
- value: string;
79
+ key: string;
80
+ value: string;
81
81
  };
82
82
 
83
83
  export type GetUploadResponse = {
84
- url: string;
85
- fields: UploadField[];
84
+ url: string;
85
+ fields: UploadField[];
86
86
  };
87
87
 
88
88
  export interface File {
89
- name?: string;
90
- path: string;
89
+ name?: string;
90
+ path: string;
91
91
  }