Skip to content

Commit 3597499

Browse files
committed
refactor: use getEnvOptions() CDN defaults instead of inline fallbacks
getEnvOptions() already deep-merges pgpmDefaults.cdn with config files and env vars — no need to re-default every field manually. Just destructure cdn directly from the merged options.
1 parent 405d4a4 commit 3597499

1 file changed

Lines changed: 18 additions & 39 deletions

File tree

graphile/graphile-settings/src/presigned-url-resolver.ts

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
/**
22
* Presigned URL resolver for the Constructive presigned URL plugin.
33
*
4-
* Reads CDN/S3/MinIO configuration from environment variables (via getEnvOptions)
5-
* and lazily initializes an S3Client on first use to avoid requiring
6-
* env vars at module load time.
4+
* Reads CDN/S3/MinIO configuration from the standard env system
5+
* (getEnvOptions → pgpmDefaults + config files + env vars) and lazily
6+
* initializes an S3Client on first use.
77
*
88
* Follows the same lazy-init pattern as upload-resolver.ts.
9-
*
10-
* ENV VARS (via CDNOptions):
11-
* BUCKET_PROVIDER - 'minio' | 's3' (default: 'minio')
12-
* BUCKET_NAME - bucket name (default: 'test-bucket')
13-
* AWS_REGION - AWS region (default: 'us-east-1')
14-
* AWS_ACCESS_KEY - access key (default: 'minioadmin')
15-
* AWS_SECRET_KEY - secret key (default: 'minioadmin')
16-
* MINIO_ENDPOINT - MinIO endpoint (default: 'http://localhost:9000')
179
*/
1810

1911
import { S3Client } from '@aws-sdk/client-s3';
@@ -28,54 +20,41 @@ let s3Config: S3Config | null = null;
2820
/**
2921
* Lazily initialize and return the S3Config for the presigned URL plugin.
3022
*
31-
* Reads CDN env vars on first call, creates an S3Client, and caches the
32-
* config for subsequent calls. Same env vars as upload-resolver.ts.
33-
*
34-
* For MinIO, constructs a publicUrlPrefix from the endpoint + bucket name
35-
* so that public file download URLs resolve correctly.
23+
* Reads CDN config on first call via getEnvOptions() (which already merges
24+
* pgpmDefaults → config file → env vars), creates an S3Client, and caches
25+
* the result. Same CDN config as upload-resolver.ts.
3626
*/
3727
export function getPresignedUrlS3Config(): S3Config {
3828
if (s3Config) return s3Config;
3929

40-
const opts = getEnvOptions();
41-
const cdn = opts.cdn || {};
30+
const { cdn } = getEnvOptions();
4231

43-
const provider = cdn.provider || 'minio';
32+
// cdn is guaranteed populated — pgpmDefaults provides all CDN fields
33+
const { provider, bucketName, awsRegion, awsAccessKey, awsSecretKey, minioEndpoint } = cdn!;
4434
const isMinio = provider === 'minio';
45-
const bucket = cdn.bucketName || 'test-bucket';
46-
const region = cdn.awsRegion || 'us-east-1';
47-
const accessKey = cdn.awsAccessKey || 'minioadmin';
48-
const secretKey = cdn.awsSecretKey || 'minioadmin';
49-
const endpoint = cdn.minioEndpoint || 'http://localhost:9000';
50-
51-
if (process.env.NODE_ENV === 'production') {
52-
if (!cdn.awsAccessKey || !cdn.awsSecretKey) {
53-
log.warn('[presigned-url-resolver] WARNING: Using default credentials in production.');
54-
}
55-
}
5635

5736
log.info(
58-
`[presigned-url-resolver] Initializing: provider=${provider} bucket=${bucket}`,
37+
`[presigned-url-resolver] Initializing: provider=${provider} bucket=${bucketName}`,
5938
);
6039

6140
const client = new S3Client({
62-
region,
63-
credentials: { accessKeyId: accessKey, secretAccessKey: secretKey },
64-
...(isMinio ? { endpoint, forcePathStyle: true } : {}),
41+
region: awsRegion,
42+
credentials: { accessKeyId: awsAccessKey!, secretAccessKey: awsSecretKey! },
43+
...(isMinio ? { endpoint: minioEndpoint, forcePathStyle: true } : {}),
6544
});
6645

6746
// For MinIO (path-style), public URL prefix is endpoint/bucket.
6847
// For S3 (virtual-hosted), it's https://{bucket}.s3.{region}.amazonaws.com
6948
const publicUrlPrefix = isMinio
70-
? `${endpoint}/${bucket}`
71-
: `https://${bucket}.s3.${region}.amazonaws.com`;
49+
? `${minioEndpoint}/${bucketName}`
50+
: `https://${bucketName}.s3.${awsRegion}.amazonaws.com`;
7251

7352
s3Config = {
7453
client,
75-
bucket,
76-
region,
54+
bucket: bucketName!,
55+
region: awsRegion,
7756
publicUrlPrefix,
78-
...(isMinio ? { endpoint, forcePathStyle: true } : {}),
57+
...(isMinio ? { endpoint: minioEndpoint, forcePathStyle: true } : {}),
7958
};
8059

8160
return s3Config;

0 commit comments

Comments
 (0)