@meridianjs/storage-s3 0.1.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.
@@ -0,0 +1,36 @@
1
+ import * as _meridianjs_types from '@meridianjs/types';
2
+ import { IStorageProvider, MeridianContainer } from '@meridianjs/types';
3
+
4
+ interface S3StorageOptions {
5
+ bucket: string;
6
+ region: string;
7
+ accessKeyId?: string;
8
+ secretAccessKey?: string;
9
+ /** MinIO / localstack / custom S3-compatible endpoint */
10
+ endpoint?: string;
11
+ /** CloudFront distribution URL, e.g. "https://d1abc.cloudfront.net". Takes precedence over baseUrl. */
12
+ cloudfrontUrl?: string;
13
+ /** Any other CDN base URL. cloudfrontUrl takes precedence if both are provided. */
14
+ baseUrl?: string;
15
+ }
16
+
17
+ declare class S3StorageService implements IStorageProvider {
18
+ private readonly client;
19
+ private readonly options;
20
+ private readonly publicBaseUrl;
21
+ constructor(container: MeridianContainer);
22
+ upload(file: {
23
+ buffer: Buffer;
24
+ originalname: string;
25
+ mimetype: string;
26
+ size: number;
27
+ }, subDir: string): Promise<{
28
+ url: string;
29
+ key: string;
30
+ }>;
31
+ delete(urlOrKey: string): Promise<void>;
32
+ }
33
+
34
+ declare const _default: _meridianjs_types.ModuleDefinition;
35
+
36
+ export { type S3StorageOptions, S3StorageService, _default as default };
@@ -0,0 +1,36 @@
1
+ import * as _meridianjs_types from '@meridianjs/types';
2
+ import { IStorageProvider, MeridianContainer } from '@meridianjs/types';
3
+
4
+ interface S3StorageOptions {
5
+ bucket: string;
6
+ region: string;
7
+ accessKeyId?: string;
8
+ secretAccessKey?: string;
9
+ /** MinIO / localstack / custom S3-compatible endpoint */
10
+ endpoint?: string;
11
+ /** CloudFront distribution URL, e.g. "https://d1abc.cloudfront.net". Takes precedence over baseUrl. */
12
+ cloudfrontUrl?: string;
13
+ /** Any other CDN base URL. cloudfrontUrl takes precedence if both are provided. */
14
+ baseUrl?: string;
15
+ }
16
+
17
+ declare class S3StorageService implements IStorageProvider {
18
+ private readonly client;
19
+ private readonly options;
20
+ private readonly publicBaseUrl;
21
+ constructor(container: MeridianContainer);
22
+ upload(file: {
23
+ buffer: Buffer;
24
+ originalname: string;
25
+ mimetype: string;
26
+ size: number;
27
+ }, subDir: string): Promise<{
28
+ url: string;
29
+ key: string;
30
+ }>;
31
+ delete(urlOrKey: string): Promise<void>;
32
+ }
33
+
34
+ declare const _default: _meridianjs_types.ModuleDefinition;
35
+
36
+ export { type S3StorageOptions, S3StorageService, _default as default };
package/dist/index.js ADDED
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ S3StorageService: () => S3StorageService,
34
+ default: () => index_default
35
+ });
36
+ module.exports = __toCommonJS(index_exports);
37
+ var import_framework_utils = require("@meridianjs/framework-utils");
38
+
39
+ // src/service.ts
40
+ var import_node_crypto = require("crypto");
41
+ var import_node_path = __toESM(require("path"));
42
+ var import_client_s3 = require("@aws-sdk/client-s3");
43
+ var S3StorageService = class {
44
+ client;
45
+ options;
46
+ publicBaseUrl;
47
+ constructor(container) {
48
+ const opts = container.resolve("moduleOptions");
49
+ if (!opts?.bucket) {
50
+ throw new Error(
51
+ "[@meridianjs/storage-s3] Missing required option: 'bucket'. Add bucket and region to your storage-s3 module config."
52
+ );
53
+ }
54
+ if (!opts?.region) {
55
+ throw new Error(
56
+ "[@meridianjs/storage-s3] Missing required option: 'region'. Add region to your storage-s3 module config."
57
+ );
58
+ }
59
+ this.options = opts;
60
+ const clientConfig = {
61
+ region: opts.region
62
+ };
63
+ if (opts.endpoint) {
64
+ clientConfig.endpoint = opts.endpoint;
65
+ clientConfig.forcePathStyle = true;
66
+ }
67
+ if (opts.accessKeyId && opts.secretAccessKey) {
68
+ clientConfig.credentials = {
69
+ accessKeyId: opts.accessKeyId,
70
+ secretAccessKey: opts.secretAccessKey
71
+ };
72
+ }
73
+ this.client = new import_client_s3.S3Client(clientConfig);
74
+ this.publicBaseUrl = (opts.cloudfrontUrl ?? opts.baseUrl ?? `https://${opts.bucket}.s3.${opts.region}.amazonaws.com`).replace(/\/$/, "");
75
+ }
76
+ async upload(file, subDir) {
77
+ const ext = import_node_path.default.extname(file.originalname);
78
+ const key = `${subDir}/${(0, import_node_crypto.randomUUID)()}${ext}`;
79
+ await this.client.send(
80
+ new import_client_s3.PutObjectCommand({
81
+ Bucket: this.options.bucket,
82
+ Key: key,
83
+ Body: file.buffer,
84
+ ContentType: file.mimetype
85
+ })
86
+ );
87
+ return { url: `${this.publicBaseUrl}/${key}`, key };
88
+ }
89
+ async delete(urlOrKey) {
90
+ let key;
91
+ if (urlOrKey.startsWith("http")) {
92
+ key = urlOrKey.replace(`${this.publicBaseUrl}/`, "");
93
+ } else {
94
+ key = urlOrKey;
95
+ }
96
+ await this.client.send(
97
+ new import_client_s3.DeleteObjectCommand({
98
+ Bucket: this.options.bucket,
99
+ Key: key
100
+ })
101
+ );
102
+ }
103
+ };
104
+
105
+ // src/index.ts
106
+ var index_default = (0, import_framework_utils.Module)("storageProvider", {
107
+ service: S3StorageService
108
+ });
109
+ // Annotate the CommonJS export names for ESM import in node:
110
+ 0 && (module.exports = {
111
+ S3StorageService
112
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,77 @@
1
+ // src/index.ts
2
+ import { Module } from "@meridianjs/framework-utils";
3
+
4
+ // src/service.ts
5
+ import { randomUUID } from "crypto";
6
+ import path from "path";
7
+ import { S3Client, PutObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
8
+ var S3StorageService = class {
9
+ client;
10
+ options;
11
+ publicBaseUrl;
12
+ constructor(container) {
13
+ const opts = container.resolve("moduleOptions");
14
+ if (!opts?.bucket) {
15
+ throw new Error(
16
+ "[@meridianjs/storage-s3] Missing required option: 'bucket'. Add bucket and region to your storage-s3 module config."
17
+ );
18
+ }
19
+ if (!opts?.region) {
20
+ throw new Error(
21
+ "[@meridianjs/storage-s3] Missing required option: 'region'. Add region to your storage-s3 module config."
22
+ );
23
+ }
24
+ this.options = opts;
25
+ const clientConfig = {
26
+ region: opts.region
27
+ };
28
+ if (opts.endpoint) {
29
+ clientConfig.endpoint = opts.endpoint;
30
+ clientConfig.forcePathStyle = true;
31
+ }
32
+ if (opts.accessKeyId && opts.secretAccessKey) {
33
+ clientConfig.credentials = {
34
+ accessKeyId: opts.accessKeyId,
35
+ secretAccessKey: opts.secretAccessKey
36
+ };
37
+ }
38
+ this.client = new S3Client(clientConfig);
39
+ this.publicBaseUrl = (opts.cloudfrontUrl ?? opts.baseUrl ?? `https://${opts.bucket}.s3.${opts.region}.amazonaws.com`).replace(/\/$/, "");
40
+ }
41
+ async upload(file, subDir) {
42
+ const ext = path.extname(file.originalname);
43
+ const key = `${subDir}/${randomUUID()}${ext}`;
44
+ await this.client.send(
45
+ new PutObjectCommand({
46
+ Bucket: this.options.bucket,
47
+ Key: key,
48
+ Body: file.buffer,
49
+ ContentType: file.mimetype
50
+ })
51
+ );
52
+ return { url: `${this.publicBaseUrl}/${key}`, key };
53
+ }
54
+ async delete(urlOrKey) {
55
+ let key;
56
+ if (urlOrKey.startsWith("http")) {
57
+ key = urlOrKey.replace(`${this.publicBaseUrl}/`, "");
58
+ } else {
59
+ key = urlOrKey;
60
+ }
61
+ await this.client.send(
62
+ new DeleteObjectCommand({
63
+ Bucket: this.options.bucket,
64
+ Key: key
65
+ })
66
+ );
67
+ }
68
+ };
69
+
70
+ // src/index.ts
71
+ var index_default = Module("storageProvider", {
72
+ service: S3StorageService
73
+ });
74
+ export {
75
+ S3StorageService,
76
+ index_default as default
77
+ };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@meridianjs/storage-s3",
3
+ "version": "0.1.0",
4
+ "description": "S3-compatible storage module for Meridian file uploads",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ }
18
+ }
19
+ },
20
+ "scripts": {
21
+ "build": "tsup src/index.ts --format esm,cjs --dts --clean",
22
+ "dev": "tsup src/index.ts --format esm,cjs --dts --watch",
23
+ "typecheck": "tsc --noEmit",
24
+ "clean": "rm -rf dist",
25
+ "prepublishOnly": "npm run build"
26
+ },
27
+ "dependencies": {
28
+ "@aws-sdk/client-s3": "^3.0.0",
29
+ "@meridianjs/framework-utils": "^0.1.0",
30
+ "@meridianjs/types": "^0.1.0"
31
+ },
32
+ "devDependencies": {
33
+ "tsup": "^8.3.5",
34
+ "typescript": "*"
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ }
42
+ }