|
| 1 | +import type { AuthenticatedEnvironment } from "~/services/apiAuth.server"; |
| 2 | +import { BaseService } from "./baseService.server"; |
| 3 | +import { env } from "~/env.server"; |
| 4 | +import { createPresignedPost } from "@aws-sdk/s3-presigned-post"; |
| 5 | +import { S3Client } from "@aws-sdk/client-s3"; |
| 6 | +import { customAlphabet } from "nanoid"; |
| 7 | +import { errAsync, fromPromise } from "neverthrow"; |
| 8 | + |
| 9 | +const nanoid = customAlphabet("1234567890abcdefghijklmnopqrstuvwxyz", 24); |
| 10 | +const objectStoreClient = |
| 11 | + env.ARTIFACTS_OBJECT_STORE_ACCESS_KEY_ID && |
| 12 | + env.ARTIFACTS_OBJECT_STORE_SECRET_ACCESS_KEY && |
| 13 | + env.ARTIFACTS_OBJECT_STORE_BASE_URL |
| 14 | + ? new S3Client({ |
| 15 | + credentials: { |
| 16 | + accessKeyId: env.ARTIFACTS_OBJECT_STORE_ACCESS_KEY_ID, |
| 17 | + secretAccessKey: env.ARTIFACTS_OBJECT_STORE_SECRET_ACCESS_KEY, |
| 18 | + }, |
| 19 | + region: env.ARTIFACTS_OBJECT_STORE_REGION, |
| 20 | + endpoint: env.ARTIFACTS_OBJECT_STORE_BASE_URL, |
| 21 | + forcePathStyle: true, |
| 22 | + }) |
| 23 | + : new S3Client(); |
| 24 | + |
| 25 | +const artifactKeyPrefixByType = { |
| 26 | + deployment_context: "deployments", |
| 27 | +} as const; |
| 28 | +const artifactBytesSizeLimitByType = { |
| 29 | + deployment_context: 100 * 1024 * 1024, // 100MB |
| 30 | +} as const; |
| 31 | + |
| 32 | +export class ArtifactsService extends BaseService { |
| 33 | + private readonly bucket = env.ARTIFACTS_OBJECT_STORE_BUCKET; |
| 34 | + |
| 35 | + public createArtifact( |
| 36 | + type: "deployment_context", |
| 37 | + authenticatedEnv: AuthenticatedEnvironment, |
| 38 | + contentLength?: number |
| 39 | + ) { |
| 40 | + const limit = artifactBytesSizeLimitByType[type]; |
| 41 | + |
| 42 | + // this is just a validation using client-side data |
| 43 | + // the actual limit will be enforced by S3 |
| 44 | + if (contentLength && contentLength > limit) { |
| 45 | + return errAsync({ |
| 46 | + type: "artifact_size_exceeds_limit" as const, |
| 47 | + contentLength, |
| 48 | + sizeLimit: limit, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + const uniqueId = nanoid(); |
| 53 | + const key = `${artifactKeyPrefixByType[type]}/${authenticatedEnv.project.externalRef}/${authenticatedEnv.slug}/${uniqueId}.tar.gz`; |
| 54 | + |
| 55 | + return this.createPresignedPost(key, limit, contentLength).map((result) => ({ |
| 56 | + artifactKey: key, |
| 57 | + uploadUrl: result.url, |
| 58 | + uploadFields: result.fields, |
| 59 | + expiresAt: result.expiresAt, |
| 60 | + })); |
| 61 | + } |
| 62 | + |
| 63 | + private createPresignedPost(key: string, sizeLimit: number, contentLength?: number) { |
| 64 | + const ttlSeconds = 300; // 5 minutes |
| 65 | + const expiresAt = new Date(Date.now() + ttlSeconds * 1000); |
| 66 | + |
| 67 | + return fromPromise( |
| 68 | + createPresignedPost(objectStoreClient, { |
| 69 | + Bucket: this.bucket, |
| 70 | + Key: key, |
| 71 | + Conditions: [["content-length-range", 0, sizeLimit]], |
| 72 | + Fields: { |
| 73 | + "Content-Type": "application/gzip", |
| 74 | + }, |
| 75 | + Expires: ttlSeconds, |
| 76 | + }), |
| 77 | + (error) => ({ |
| 78 | + type: "failed_to_create_presigned_post" as const, |
| 79 | + cause: error, |
| 80 | + }) |
| 81 | + ).map((result) => ({ |
| 82 | + ...result, |
| 83 | + expiresAt, |
| 84 | + })); |
| 85 | + } |
| 86 | +} |
0 commit comments