@krantho/media-s3 0.0.0-stage → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Descoped
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,2 @@
1
+ export { createS3FileStore } from './s3-file-store.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createS3FileStore } from './s3-file-store.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1,4 @@
1
+ import type { S3MediaSection } from '@krantho/config';
2
+ import type { IFileStore } from '@krantho/persistence';
3
+ export declare function createS3FileStore(section: S3MediaSection): IFileStore;
4
+ //# sourceMappingURL=s3-file-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"s3-file-store.d.ts","sourceRoot":"","sources":["../src/s3-file-store.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAA;AACrD,OAAO,KAAK,EAAE,UAAU,EAAc,MAAM,sBAAsB,CAAA;AA2FlE,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,cAAc,GAAG,UAAU,CAMrE"}
@@ -0,0 +1,81 @@
1
+ import { CopyObjectCommand, DeleteObjectCommand, GetObjectCommand, NoSuchKey, PutObjectCommand, S3Client, } from '@aws-sdk/client-s3';
2
+ /** Keys never change content, so objects are cached for a year */
3
+ const CACHE_CONTROL = 'public, max-age=31536000, immutable';
4
+ /** File store on an S3-compatible bucket whose objects are public at `publicUrl` */
5
+ class S3FileStore {
6
+ client;
7
+ label = 'S3';
8
+ location;
9
+ publicUrl;
10
+ constructor(client, target) {
11
+ this.client = client;
12
+ this.location = target.bucket;
13
+ this.publicUrl = target.publicUrl.replace(/\/+$/, '');
14
+ }
15
+ async put(key, bytes, contentType) {
16
+ await this.client.send(new PutObjectCommand({
17
+ Bucket: this.location,
18
+ Key: key,
19
+ Body: bytes,
20
+ ContentType: contentType,
21
+ CacheControl: CACHE_CONTROL,
22
+ }));
23
+ return this.stored(key);
24
+ }
25
+ async get(key) {
26
+ try {
27
+ const result = await this.client.send(new GetObjectCommand({ Bucket: this.location, Key: key }));
28
+ if (result.Body === undefined)
29
+ return null;
30
+ return await result.Body.transformToByteArray();
31
+ }
32
+ catch (error) {
33
+ if (error instanceof NoSuchKey)
34
+ return null;
35
+ throw error;
36
+ }
37
+ }
38
+ async copy(fromKey, toKey, contentType) {
39
+ await this.client.send(new CopyObjectCommand({
40
+ Bucket: this.location,
41
+ Key: toKey,
42
+ CopySource: `${this.location}/${encodeURIComponent(fromKey)}`,
43
+ MetadataDirective: 'REPLACE',
44
+ ContentType: contentType,
45
+ CacheControl: CACHE_CONTROL,
46
+ }));
47
+ return this.stored(toKey);
48
+ }
49
+ async delete(key) {
50
+ await this.client.send(new DeleteObjectCommand({ Bucket: this.location, Key: key }));
51
+ }
52
+ stored(key) {
53
+ return { key, url: `${this.publicUrl}/${key}` };
54
+ }
55
+ }
56
+ function required(value, path) {
57
+ if (value === undefined)
58
+ throw new Error(`${path}: required when media.provider is "s3"`);
59
+ return value;
60
+ }
61
+ /** Client options from config; without a key pair the SDK uses its default credential chain */
62
+ function clientConfig(section, region) {
63
+ const config = { region, forcePathStyle: section.forcePathStyle };
64
+ if (section.endpoint !== undefined)
65
+ config.endpoint = section.endpoint;
66
+ if (section.accessKeyId !== undefined && section.secretAccessKey !== undefined) {
67
+ config.credentials = {
68
+ accessKeyId: section.accessKeyId,
69
+ secretAccessKey: section.secretAccessKey,
70
+ };
71
+ }
72
+ return config;
73
+ }
74
+ export function createS3FileStore(section) {
75
+ const bucket = required(section.bucket, 'media.s3.bucket');
76
+ const region = required(section.region, 'media.s3.region');
77
+ const publicUrl = required(section.publicUrl, 'media.s3.publicUrl');
78
+ const client = new S3Client(clientConfig(section, region));
79
+ return new S3FileStore(client, { bucket, publicUrl });
80
+ }
81
+ //# sourceMappingURL=s3-file-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"s3-file-store.js","sourceRoot":"","sources":["../src/s3-file-store.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,SAAS,EACT,gBAAgB,EAChB,QAAQ,GACT,MAAM,oBAAoB,CAAA;AAI3B,kEAAkE;AAClE,MAAM,aAAa,GAAG,qCAAqC,CAAA;AAO3D,o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,MAAM,CAAC,WAAW,GAAG;YACnB,WAAW,EAAE,OAAO,CAAC,WAAW;YAChC,eAAe,EAAE,OAAO,CAAC,eAAe;SACzC,CAAA;IACH,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,OAAuB;IACvD,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAA;IAC1D,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAA;IAC1D,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,SAAS,EAAE,oBAAoB,CAAC,CAAA;IACnE,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC,YAAY,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAA;IAC1D,OAAO,IAAI,WAAW,CAAC,MAAM,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAA;AACvD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,39 @@
1
1
  {
2
2
  "name": "@krantho/media-s3",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.1",
4
+ "description": "The krantho media file store on S3-compatible object storage, such as AWS S3, Cloudflare R2 and MinIO.",
5
+ "license": "MIT",
6
+ "homepage": "https://krantho.com",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/descoped/krantho.git",
10
+ "directory": "packages/media-s3"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/descoped/krantho/issues"
14
+ },
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./dist/index.js",
19
+ "types": "./dist/index.d.ts"
20
+ }
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "dependencies": {
26
+ "@aws-sdk/client-s3": "^3.1168.0",
27
+ "@krantho/config": "1.0.1",
28
+ "@krantho/persistence": "1.0.1"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^25.9.9",
32
+ "typescript": "^6.0.3"
33
+ },
34
+ "scripts": {
35
+ "build": "tsc",
36
+ "typecheck": "tsc --noEmit",
37
+ "clean": "rm -rf dist"
38
+ }
6
39
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.