@easy-cms/storage-s3 0.2.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.
- package/LICENSE +21 -0
- package/README.md +19 -0
- package/dist/index.d.ts +52 -0
- package/dist/index.js +95 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kanawoot K.
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @easy-cms/storage-s3
|
|
2
|
+
|
|
3
|
+
Upload storage for [Easy CMS](https://maritonx.github.io/easy-crm/) on AWS S3, Cloudflare R2,
|
|
4
|
+
MinIO or any S3-compatible service. Use it where there is no persistent disk: Vercel, Netlify,
|
|
5
|
+
containers.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { s3Storage } from '@easy-cms/storage-s3'
|
|
9
|
+
|
|
10
|
+
export default defineConfig({
|
|
11
|
+
// …
|
|
12
|
+
upload: { storage: s3Storage({ bucket: 'my-site-media', region: 'eu-central-1' }) },
|
|
13
|
+
})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Credentials default to `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY`. Files are served through the
|
|
17
|
+
CMS API (private bucket) unless you set `publicUrl`.
|
|
18
|
+
|
|
19
|
+
See [Uploads & media](https://maritonx.github.io/easy-crm/guide/uploads#s3-cloudflare-r2-and-minio).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { StorageAdapter } from '@easy-cms/core';
|
|
2
|
+
|
|
3
|
+
interface S3StorageOptions {
|
|
4
|
+
/** Bucket name. */
|
|
5
|
+
readonly bucket: string;
|
|
6
|
+
/**
|
|
7
|
+
* Region. Default `AWS_REGION`, then `us-east-1`. Use `auto` for Cloudflare R2.
|
|
8
|
+
*/
|
|
9
|
+
readonly region?: string;
|
|
10
|
+
/**
|
|
11
|
+
* Endpoint for S3-compatible services, e.g. `https://<account>.r2.cloudflarestorage.com` or
|
|
12
|
+
* `http://localhost:9000` (MinIO). Default: AWS S3 for the region.
|
|
13
|
+
*/
|
|
14
|
+
readonly endpoint?: string;
|
|
15
|
+
/** Default `AWS_ACCESS_KEY_ID`. */
|
|
16
|
+
readonly accessKeyId?: string;
|
|
17
|
+
/** Default `AWS_SECRET_ACCESS_KEY`. */
|
|
18
|
+
readonly secretAccessKey?: string;
|
|
19
|
+
/** Default `AWS_SESSION_TOKEN`. */
|
|
20
|
+
readonly sessionToken?: string;
|
|
21
|
+
/** Prepended to every object key, e.g. `media/`. Default none. */
|
|
22
|
+
readonly prefix?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Base URL where the bucket is publicly readable (a CDN, R2 custom domain, or bucket website).
|
|
25
|
+
* When set, media URLs point there. When not set, files are served through the REST API
|
|
26
|
+
* (`<api>/media/file/<key>`), which works with private buckets.
|
|
27
|
+
*/
|
|
28
|
+
readonly publicUrl?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Address the bucket as `<endpoint>/<bucket>/<key>` instead of `<bucket>.<endpoint>/<key>`.
|
|
31
|
+
* Default: `true` when `endpoint` is set (MinIO needs it, R2 accepts it), otherwise `false`.
|
|
32
|
+
*/
|
|
33
|
+
readonly forcePathStyle?: boolean;
|
|
34
|
+
/** Fetch implementation. Default the global `fetch`. */
|
|
35
|
+
readonly fetch?: typeof fetch;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Stores uploads in an S3-compatible bucket, for hosts without a persistent disk (Vercel,
|
|
39
|
+
* Netlify, containers):
|
|
40
|
+
*
|
|
41
|
+
* ```ts
|
|
42
|
+
* import { s3Storage } from '@easy-cms/storage-s3'
|
|
43
|
+
*
|
|
44
|
+
* upload: { storage: s3Storage({ bucket: 'my-site-media', region: 'eu-central-1' }) }
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* Credentials are read when the CMS starts, not when the config is imported, so builds work
|
|
48
|
+
* without them.
|
|
49
|
+
*/
|
|
50
|
+
declare function s3Storage(options: S3StorageOptions): StorageAdapter;
|
|
51
|
+
|
|
52
|
+
export { type S3StorageOptions, s3Storage };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { AwsClient } from "aws4fetch";
|
|
3
|
+
function s3Storage(options) {
|
|
4
|
+
const prefix = normalizePrefix(options.prefix);
|
|
5
|
+
let client;
|
|
6
|
+
let objectURL = () => {
|
|
7
|
+
throw new Error("s3Storage used before init()");
|
|
8
|
+
};
|
|
9
|
+
const request = async (key, init) => {
|
|
10
|
+
if (!client) throw new Error("s3Storage used before init()");
|
|
11
|
+
const signed = await client.sign(objectURL(prefix + key), init);
|
|
12
|
+
return (options.fetch ?? fetch)(signed);
|
|
13
|
+
};
|
|
14
|
+
return {
|
|
15
|
+
name: "s3",
|
|
16
|
+
init() {
|
|
17
|
+
const env = process.env;
|
|
18
|
+
const accessKeyId = options.accessKeyId ?? env.AWS_ACCESS_KEY_ID;
|
|
19
|
+
const secretAccessKey = options.secretAccessKey ?? env.AWS_SECRET_ACCESS_KEY;
|
|
20
|
+
const missing = [
|
|
21
|
+
!options.bucket && "`bucket`",
|
|
22
|
+
!accessKeyId && "`accessKeyId` (or AWS_ACCESS_KEY_ID)",
|
|
23
|
+
!secretAccessKey && "`secretAccessKey` (or AWS_SECRET_ACCESS_KEY)"
|
|
24
|
+
].filter(Boolean);
|
|
25
|
+
if (missing.length > 0) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`s3Storage is missing ${missing.join(", ")}.
|
|
28
|
+
\u2192 set them in the server's environment or pass them to s3Storage()`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
const region = options.region ?? env.AWS_REGION ?? env.AWS_DEFAULT_REGION ?? "us-east-1";
|
|
32
|
+
const sessionToken = options.sessionToken ?? env.AWS_SESSION_TOKEN;
|
|
33
|
+
client = new AwsClient({
|
|
34
|
+
accessKeyId,
|
|
35
|
+
secretAccessKey,
|
|
36
|
+
...sessionToken ? { sessionToken } : {},
|
|
37
|
+
service: "s3",
|
|
38
|
+
region
|
|
39
|
+
});
|
|
40
|
+
objectURL = bucketURL(options, region);
|
|
41
|
+
},
|
|
42
|
+
async put(key, data, { contentType }) {
|
|
43
|
+
const response = await request(key, {
|
|
44
|
+
method: "PUT",
|
|
45
|
+
body: data,
|
|
46
|
+
headers: { "content-type": contentType, "content-length": String(data.byteLength) }
|
|
47
|
+
});
|
|
48
|
+
if (!response.ok) throw await s3Error("PUT", key, response);
|
|
49
|
+
},
|
|
50
|
+
async get(key) {
|
|
51
|
+
const response = await request(key, { method: "GET" });
|
|
52
|
+
if (response.status === 404) return null;
|
|
53
|
+
if (!response.ok) throw await s3Error("GET", key, response);
|
|
54
|
+
const body = new Uint8Array(await response.arrayBuffer());
|
|
55
|
+
return { body, size: body.byteLength };
|
|
56
|
+
},
|
|
57
|
+
async delete(key) {
|
|
58
|
+
const response = await request(key, { method: "DELETE" });
|
|
59
|
+
if (!response.ok && response.status !== 404) throw await s3Error("DELETE", key, response);
|
|
60
|
+
},
|
|
61
|
+
url(key) {
|
|
62
|
+
if (!options.publicUrl) return void 0;
|
|
63
|
+
return `${options.publicUrl.replace(/\/+$/, "")}/${encodeKey(prefix + key)}`;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function normalizePrefix(prefix) {
|
|
68
|
+
if (!prefix) return "";
|
|
69
|
+
const trimmed = prefix.replace(/^\/+|\/+$/g, "");
|
|
70
|
+
return trimmed ? `${trimmed}/` : "";
|
|
71
|
+
}
|
|
72
|
+
function encodeKey(key) {
|
|
73
|
+
return key.split("/").map(encodeURIComponent).join("/");
|
|
74
|
+
}
|
|
75
|
+
function bucketURL(options, region) {
|
|
76
|
+
const pathStyle = options.forcePathStyle ?? options.endpoint !== void 0;
|
|
77
|
+
const endpoint = new URL(options.endpoint ?? `https://s3.${region}.amazonaws.com`);
|
|
78
|
+
const base = endpoint.origin + endpoint.pathname.replace(/\/+$/, "");
|
|
79
|
+
if (pathStyle) {
|
|
80
|
+
const root2 = `${base}/${encodeURIComponent(options.bucket)}`;
|
|
81
|
+
return (key) => `${root2}/${encodeKey(key)}`;
|
|
82
|
+
}
|
|
83
|
+
const root = `${endpoint.protocol}//${options.bucket}.${endpoint.host}${endpoint.pathname.replace(/\/+$/, "")}`;
|
|
84
|
+
return (key) => `${root}/${encodeKey(key)}`;
|
|
85
|
+
}
|
|
86
|
+
async function s3Error(method, key, response) {
|
|
87
|
+
const text = await response.text().catch(() => "");
|
|
88
|
+
const code = /<Code>([^<]+)<\/Code>/.exec(text)?.[1];
|
|
89
|
+
const message = /<Message>([^<]+)<\/Message>/.exec(text)?.[1];
|
|
90
|
+
const detail = [code, message].filter(Boolean).join(": ") || response.statusText;
|
|
91
|
+
return new Error(`S3 ${method} ${key} failed (${response.status}): ${detail}`);
|
|
92
|
+
}
|
|
93
|
+
export {
|
|
94
|
+
s3Storage
|
|
95
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@easy-cms/storage-s3",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "S3-compatible upload storage for Easy CMS: AWS S3, Cloudflare R2, MinIO",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"aws4fetch": "^1.0.20"
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@easy-cms/core": "^0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"sharp": "^0.35.4",
|
|
26
|
+
"@easy-cms/db-sqlite": "^0.2.0",
|
|
27
|
+
"@easy-cms/core": "^0.2.0"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=22.12"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "git+https://github.com/maritonx/easy-crm.git",
|
|
38
|
+
"directory": "packages/storage-s3"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"easy-cms",
|
|
42
|
+
"s3",
|
|
43
|
+
"r2",
|
|
44
|
+
"minio",
|
|
45
|
+
"storage"
|
|
46
|
+
],
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsup",
|
|
49
|
+
"typecheck": "tsc --noEmit",
|
|
50
|
+
"test": "vitest run"
|
|
51
|
+
}
|
|
52
|
+
}
|