@etohq/file-s3 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.
package/README.md ADDED
File without changes
@@ -0,0 +1,3 @@
1
+ declare const _default: import("@etohq/types").ModuleProviderExports;
2
+ export default _default;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAKA,wBAEE"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const utils_1 = require("@etohq/framework/utils");
4
+ const s3_file_1 = require("./services/s3-file");
5
+ const services = [s3_file_1.S3FileService];
6
+ exports.default = (0, utils_1.ModuleProvider)(utils_1.Modules.FILE, {
7
+ services,
8
+ });
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,kDAAgE;AAChE,gDAAkD;AAElD,MAAM,QAAQ,GAAG,CAAC,uBAAa,CAAC,CAAA;AAEhC,kBAAe,IAAA,sBAAc,EAAC,eAAO,CAAC,IAAI,EAAE;IAC1C,QAAQ;CACT,CAAC,CAAA"}
@@ -0,0 +1,32 @@
1
+ import { S3Client } from "@aws-sdk/client-s3";
2
+ import { FileTypes, Logger, S3FileServiceOptions } from "@etohq/framework/types";
3
+ import { AbstractFileProviderService } from "@etohq/framework/utils";
4
+ type InjectedDependencies = {
5
+ logger: Logger;
6
+ };
7
+ interface S3FileServiceConfig {
8
+ fileUrl: string;
9
+ accessKeyId?: string;
10
+ secretAccessKey?: string;
11
+ authenticationMethod?: "access-key" | "s3-iam-role";
12
+ region: string;
13
+ bucket: string;
14
+ prefix?: string;
15
+ endpoint?: string;
16
+ cacheControl?: string;
17
+ downloadFileDuration?: number;
18
+ additionalClientConfig?: Record<string, any>;
19
+ }
20
+ export declare class S3FileService extends AbstractFileProviderService {
21
+ static identifier: string;
22
+ protected config_: S3FileServiceConfig;
23
+ protected logger_: Logger;
24
+ protected client_: S3Client;
25
+ constructor({ logger }: InjectedDependencies, options: S3FileServiceOptions);
26
+ protected getClient(): S3Client;
27
+ upload(file: FileTypes.ProviderUploadFileDTO): Promise<FileTypes.ProviderFileResultDTO>;
28
+ delete(file: FileTypes.ProviderDeleteFileDTO): Promise<void>;
29
+ getPresignedDownloadUrl(fileData: FileTypes.ProviderGetFileDTO): Promise<string>;
30
+ }
31
+ export {};
32
+ //# sourceMappingURL=s3-file.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"s3-file.d.ts","sourceRoot":"","sources":["../../src/services/s3-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,QAAQ,EAET,MAAM,oBAAoB,CAAA;AAE3B,OAAO,EACL,SAAS,EACT,MAAM,EACN,oBAAoB,EACrB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EACL,2BAA2B,EAE5B,MAAM,wBAAwB,CAAA;AAI/B,KAAK,oBAAoB,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,UAAU,mBAAmB;IAC3B,OAAO,EAAE,MAAM,CAAA;IACf,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,oBAAoB,CAAC,EAAE,YAAY,GAAG,aAAa,CAAA;IACnD,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,oBAAoB,CAAC,EAAE,MAAM,CAAA;IAC7B,sBAAsB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC7C;AAED,qBAAa,aAAc,SAAQ,2BAA2B;IAC5D,MAAM,CAAC,UAAU,SAAO;IACxB,SAAS,CAAC,OAAO,EAAE,mBAAmB,CAAA;IACtC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAA;IACzB,SAAS,CAAC,OAAO,EAAE,QAAQ,CAAA;gBAEf,EAAE,MAAM,EAAE,EAAE,oBAAoB,EAAE,OAAO,EAAE,oBAAoB;IAgC3E,SAAS,CAAC,SAAS;IAoBb,MAAM,CACV,IAAI,EAAE,SAAS,CAAC,qBAAqB,GACpC,OAAO,CAAC,SAAS,CAAC,qBAAqB,CAAC;IAoDrC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAc5D,uBAAuB,CAC3B,QAAQ,EAAE,SAAS,CAAC,kBAAkB,GACrC,OAAO,CAAC,MAAM,CAAC;CAWnB"}
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.S3FileService = void 0;
7
+ const client_s3_1 = require("@aws-sdk/client-s3");
8
+ const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner");
9
+ const utils_1 = require("@etohq/framework/utils");
10
+ const path_1 = __importDefault(require("path"));
11
+ const ulid_1 = require("ulid");
12
+ class S3FileService extends utils_1.AbstractFileProviderService {
13
+ constructor({ logger }, options) {
14
+ super();
15
+ const authenticationMethod = options.authentication_method ?? "access-key";
16
+ if (authenticationMethod === "access-key" &&
17
+ (!options.access_key_id || !options.secret_access_key)) {
18
+ throw new utils_1.EtoError(utils_1.EtoError.Types.INVALID_DATA, `Access key ID and secret access key are required when using access key authentication`);
19
+ }
20
+ this.config_ = {
21
+ fileUrl: options.file_url,
22
+ accessKeyId: options.access_key_id,
23
+ secretAccessKey: options.secret_access_key,
24
+ authenticationMethod: authenticationMethod,
25
+ region: options.region,
26
+ bucket: options.bucket,
27
+ prefix: options.prefix ?? "",
28
+ endpoint: options.endpoint,
29
+ cacheControl: options.cache_control ?? "public, max-age=31536000",
30
+ downloadFileDuration: options.download_file_duration ?? 60 * 60,
31
+ additionalClientConfig: options.additional_client_config ?? {},
32
+ };
33
+ this.logger_ = logger;
34
+ this.client_ = this.getClient();
35
+ }
36
+ getClient() {
37
+ // If none is provided, the SDK will use the default credentials provider chain, see https://docs.aws.amazon.com/cli/v1/userguide/cli-configure-envvars.html
38
+ const credentials = this.config_.authenticationMethod === "access-key"
39
+ ? {
40
+ accessKeyId: this.config_.accessKeyId,
41
+ secretAccessKey: this.config_.secretAccessKey,
42
+ }
43
+ : undefined;
44
+ const config = {
45
+ credentials,
46
+ region: this.config_.region,
47
+ endpoint: this.config_.endpoint,
48
+ ...this.config_.additionalClientConfig,
49
+ };
50
+ return new client_s3_1.S3Client(config);
51
+ }
52
+ async upload(file) {
53
+ if (!file) {
54
+ throw new utils_1.EtoError(utils_1.EtoError.Types.INVALID_DATA, `No file provided`);
55
+ }
56
+ if (!file.filename) {
57
+ throw new utils_1.EtoError(utils_1.EtoError.Types.INVALID_DATA, `No filename provided`);
58
+ }
59
+ const parsedFilename = path_1.default.parse(file.filename);
60
+ // TODO: Allow passing a full path for storage per request, not as a global config.
61
+ const fileKey = `${this.config_.prefix}${parsedFilename.name}-${(0, ulid_1.ulid)()}${parsedFilename.ext}`;
62
+ const content = Buffer.from(file.content, "binary");
63
+ const command = new client_s3_1.PutObjectCommand({
64
+ // We probably also want to support a separate bucket altogether for private files
65
+ // protected private_bucket_: string
66
+ // protected private_access_key_id_: string
67
+ // protected private_secret_access_key_: string
68
+ ACL: file.access === "public" ? "public-read" : "private",
69
+ Bucket: this.config_.bucket,
70
+ Body: content,
71
+ Key: fileKey,
72
+ ContentType: file.mimeType,
73
+ CacheControl: this.config_.cacheControl,
74
+ // Note: We could potentially set the content disposition when uploading,
75
+ // but storing the original filename as metadata should suffice.
76
+ Metadata: {
77
+ "x-amz-meta-original-filename": file.filename,
78
+ },
79
+ });
80
+ try {
81
+ await this.client_.send(command);
82
+ }
83
+ catch (e) {
84
+ this.logger_.error(e);
85
+ throw e;
86
+ }
87
+ return {
88
+ url: `${this.config_.fileUrl}/${fileKey}`,
89
+ key: fileKey,
90
+ };
91
+ }
92
+ async delete(file) {
93
+ const command = new client_s3_1.DeleteObjectCommand({
94
+ Bucket: this.config_.bucket,
95
+ Key: file.fileKey,
96
+ });
97
+ try {
98
+ await this.client_.send(command);
99
+ }
100
+ catch (e) {
101
+ // TODO: Rethrow depending on the error (eg. a file not found error is fine, but a failed request should be rethrown)
102
+ this.logger_.error(e);
103
+ }
104
+ }
105
+ async getPresignedDownloadUrl(fileData) {
106
+ // TODO: Allow passing content disposition when getting a presigned URL
107
+ const command = new client_s3_1.GetObjectCommand({
108
+ Bucket: this.config_.bucket,
109
+ Key: `${fileData.fileKey}`,
110
+ });
111
+ return await (0, s3_request_presigner_1.getSignedUrl)(this.client_, command, {
112
+ expiresIn: this.config_.downloadFileDuration,
113
+ });
114
+ }
115
+ }
116
+ exports.S3FileService = S3FileService;
117
+ S3FileService.identifier = "s3";
118
+ //# sourceMappingURL=s3-file.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"s3-file.js","sourceRoot":"","sources":["../../src/services/s3-file.ts"],"names":[],"mappings":";;;;;;AAAA,kDAM2B;AAC3B,wEAA4D;AAM5D,kDAG+B;AAC/B,gDAAuB;AACvB,+BAA2B;AAoB3B,MAAa,aAAc,SAAQ,mCAA2B;IAM5D,YAAY,EAAE,MAAM,EAAwB,EAAE,OAA6B;QACzE,KAAK,EAAE,CAAA;QAEP,MAAM,oBAAoB,GAAG,OAAO,CAAC,qBAAqB,IAAI,YAAY,CAAA;QAE1E,IACE,oBAAoB,KAAK,YAAY;YACrC,CAAC,CAAC,OAAO,CAAC,aAAa,IAAI,CAAC,OAAO,CAAC,iBAAiB,CAAC,EACtD,CAAC;YACD,MAAM,IAAI,gBAAQ,CAChB,gBAAQ,CAAC,KAAK,CAAC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CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;YACrB,MAAM,CAAC,CAAA;QACT,CAAC;QAED,OAAO;YACL,GAAG,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,IAAI,OAAO,EAAE;YACzC,GAAG,EAAE,OAAO;SACb,CAAA;IACH,CAAC;IAED,KAAK,CAAC,MAAM,CAAC,IAAqC;QAChD,MAAM,OAAO,GAAG,IAAI,+BAAmB,CAAC;YACtC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;YAC3B,GAAG,EAAE,IAAI,CAAC,OAAO;SAClB,CAAC,CAAA;QAEF,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAClC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,qHAAqH;YACrH,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QACvB,CAAC;IACH,CAAC;IAED,KAAK,CAAC,uBAAuB,CAC3B,QAAsC;QAEtC,uEAAuE;QACvE,MAAM,OAAO,GAAG,IAAI,4BAAgB,CAAC;YACnC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM;YAC3B,GAAG,EAAE,GAAG,QAAQ,CAAC,OAAO,EAAE;SAC3B,CAAC,CAAA;QAEF,OAAO,MAAM,IAAA,mCAAY,EAAC,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE;YAC/C,SAAS,EAAE,IAAI,CAAC,OAAO,CAAC,oBAAoB;SAC7C,CAAC,CAAA;IACJ,CAAC;;AA1IH,sCA2IC;AA1IQ,wBAAU,GAAG,IAAI,CAAA"}
@@ -0,0 +1 @@
1
+ {"root":["../src/index.ts","../src/services/s3-file.ts"],"version":"5.6.2"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@etohq/file-s3",
3
+ "version": "1.0.0",
4
+ "description": "S3 protocol file storage for Eto. Supports any S3-compatible storage provider",
5
+ "main": "dist/index.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/etohq/eto",
9
+ "directory": "packages/file-local"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "!dist/**/__tests__",
14
+ "!dist/**/__mocks__",
15
+ "!dist/**/__fixtures__"
16
+ ],
17
+ "engines": {
18
+ "node": ">=20"
19
+ },
20
+ "author": "Eto",
21
+ "license": "MIT",
22
+ "scripts": {
23
+ "test": "jest --passWithNoTests src",
24
+ "test:integration": "jest --forceExit -- integration-tests/__tests__/*.spec.ts",
25
+ "build": "rimraf dist && tsc --build ./tsconfig.json",
26
+ "watch": "tsc --watch"
27
+ },
28
+ "devDependencies": {
29
+ "@etohq/framework": "^1.0.0",
30
+ "@swc/core": "^1.7.28",
31
+ "@swc/jest": "^0.2.36",
32
+ "axios": "^0.21.4",
33
+ "jest": "^29.7.0",
34
+ "rimraf": "^5.0.2",
35
+ "typescript": "^5.6.2"
36
+ },
37
+ "dependencies": {
38
+ "@aws-sdk/client-s3": "^3.556.0",
39
+ "@aws-sdk/s3-request-presigner": "^3.556.0",
40
+ "awilix": "^8.0.1",
41
+ "ulid": "^2.3.0"
42
+ },
43
+ "peerDependencies": {
44
+ "@etohq/framework": "^1.0.0"
45
+ },
46
+ "keywords": [
47
+ "eto-plugin",
48
+ "eto-plugin-s3"
49
+ ]
50
+ }