|
1 |
| -export interface Env { |
2 |
| - AWS_ENDPOINT_DOMAIN: string; |
| 1 | +import fs from 'fs'; |
| 2 | +import { Context } from 'hono'; |
| 3 | +import { env, getRuntimeKey } from 'hono/adapter'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +const isNodeInstance = getRuntimeKey() == 'node'; |
| 7 | + |
| 8 | +export function getValueOrFileContents(value?: string, ignore?: boolean) { |
| 9 | + if (!value || ignore) return value; |
| 10 | + |
| 11 | + try { |
| 12 | + // Check if value looks like a file path |
| 13 | + if ( |
| 14 | + value.startsWith('/') || |
| 15 | + value.startsWith('./') || |
| 16 | + value.startsWith('../') |
| 17 | + ) { |
| 18 | + // Resolve the path (handle relative paths) |
| 19 | + const resolvedPath = path.resolve(value); |
| 20 | + |
| 21 | + // Check if file exists |
| 22 | + if (fs.existsSync(resolvedPath)) { |
| 23 | + // File exists, read and return its contents |
| 24 | + return fs.readFileSync(resolvedPath, 'utf8').trim(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + // If not a file path or file doesn't exist, return value as is |
| 29 | + return value; |
| 30 | + } catch (error: any) { |
| 31 | + console.log(`Error reading file at ${value}: ${error.message}`); |
| 32 | + // Return the original value if there's an error |
| 33 | + return value; |
| 34 | + } |
3 | 35 | }
|
| 36 | + |
| 37 | +const nodeEnv = { |
| 38 | + NODE_ENV: getValueOrFileContents(process.env.NODE_ENV, true), |
| 39 | + PORT: getValueOrFileContents(process.env.PORT) || 8787, |
| 40 | + |
| 41 | + TLS_KEY_PATH: getValueOrFileContents(process.env.TLS_KEY_PATH, true), |
| 42 | + TLS_CERT_PATH: getValueOrFileContents(process.env.TLS_CERT_PATH, true), |
| 43 | + TLS_CA_PATH: getValueOrFileContents(process.env.TLS_CA_PATH, true), |
| 44 | + |
| 45 | + AWS_ACCESS_KEY_ID: getValueOrFileContents(process.env.AWS_ACCESS_KEY_ID), |
| 46 | + AWS_SECRET_ACCESS_KEY: getValueOrFileContents( |
| 47 | + process.env.AWS_SECRET_ACCESS_KEY |
| 48 | + ), |
| 49 | + AWS_SESSION_TOKEN: getValueOrFileContents(process.env.AWS_SESSION_TOKEN), |
| 50 | + AWS_ROLE_ARN: getValueOrFileContents(process.env.AWS_ROLE_ARN), |
| 51 | + AWS_PROFILE: getValueOrFileContents(process.env.AWS_PROFILE, true), |
| 52 | + AWS_WEB_IDENTITY_TOKEN_FILE: getValueOrFileContents( |
| 53 | + process.env.AWS_WEB_IDENTITY_TOKEN_FILE, |
| 54 | + true |
| 55 | + ), |
| 56 | + AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: getValueOrFileContents( |
| 57 | + process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI, |
| 58 | + true |
| 59 | + ), |
| 60 | + AWS_ASSUME_ROLE_ACCESS_KEY_ID: getValueOrFileContents( |
| 61 | + process.env.AWS_ASSUME_ROLE_ACCESS_KEY_ID |
| 62 | + ), |
| 63 | + AWS_ASSUME_ROLE_SECRET_ACCESS_KEY: getValueOrFileContents( |
| 64 | + process.env.AWS_ASSUME_ROLE_SECRET_ACCESS_KEY |
| 65 | + ), |
| 66 | + AWS_ASSUME_ROLE_REGION: getValueOrFileContents( |
| 67 | + process.env.AWS_ASSUME_ROLE_REGION |
| 68 | + ), |
| 69 | + AWS_REGION: getValueOrFileContents(process.env.AWS_REGION), |
| 70 | + AWS_ENDPOINT_DOMAIN: getValueOrFileContents(process.env.AWS_ENDPOINT_DOMAIN), |
| 71 | + AWS_IMDS_V1: getValueOrFileContents(process.env.AWS_IMDS_V1), |
| 72 | + |
| 73 | + AZURE_AUTH_MODE: getValueOrFileContents(process.env.AZURE_AUTH_MODE), |
| 74 | + AZURE_ENTRA_CLIENT_ID: getValueOrFileContents( |
| 75 | + process.env.AZURE_ENTRA_CLIENT_ID |
| 76 | + ), |
| 77 | + AZURE_ENTRA_CLIENT_SECRET: getValueOrFileContents( |
| 78 | + process.env.AZURE_ENTRA_CLIENT_SECRET |
| 79 | + ), |
| 80 | + AZURE_ENTRA_TENANT_ID: getValueOrFileContents( |
| 81 | + process.env.AZURE_ENTRA_TENANT_ID |
| 82 | + ), |
| 83 | + AZURE_MANAGED_CLIENT_ID: getValueOrFileContents( |
| 84 | + process.env.AZURE_MANAGED_CLIENT_ID |
| 85 | + ), |
| 86 | + AZURE_MANAGED_VERSION: getValueOrFileContents( |
| 87 | + process.env.AZURE_MANAGED_VERSION |
| 88 | + ), |
| 89 | + AZURE_IDENTITY_ENDPOINT: getValueOrFileContents( |
| 90 | + process.env.IDENTITY_ENDPOINT, |
| 91 | + true |
| 92 | + ), |
| 93 | + AZURE_MANAGED_IDENTITY_HEADER: getValueOrFileContents( |
| 94 | + process.env.IDENTITY_HEADER |
| 95 | + ), |
| 96 | + |
| 97 | + SSE_ENCRYPTION_TYPE: getValueOrFileContents(process.env.SSE_ENCRYPTION_TYPE), |
| 98 | + KMS_KEY_ID: getValueOrFileContents(process.env.KMS_KEY_ID), |
| 99 | + KMS_BUCKET_KEY_ENABLED: getValueOrFileContents( |
| 100 | + process.env.KMS_BUCKET_KEY_ENABLED |
| 101 | + ), |
| 102 | + KMS_ENCRYPTION_CONTEXT: getValueOrFileContents( |
| 103 | + process.env.KMS_ENCRYPTION_CONTEXT |
| 104 | + ), |
| 105 | + KMS_ENCRYPTION_ALGORITHM: getValueOrFileContents( |
| 106 | + process.env.KMS_ENCRYPTION_ALGORITHM |
| 107 | + ), |
| 108 | + KMS_ENCRYPTION_CUSTOMER_KEY: getValueOrFileContents( |
| 109 | + process.env.KMS_ENCRYPTION_CUSTOMER_KEY |
| 110 | + ), |
| 111 | + KMS_ENCRYPTION_CUSTOMER_KEY_MD5: getValueOrFileContents( |
| 112 | + process.env.KMS_ENCRYPTION_CUSTOMER_KEY_MD5 |
| 113 | + ), |
| 114 | + KMS_ROLE_ARN: getValueOrFileContents(process.env.KMS_ROLE_ARN), |
| 115 | + |
| 116 | + HTTP_PROXY: getValueOrFileContents(process.env.HTTP_PROXY), |
| 117 | + HTTPS_PROXY: getValueOrFileContents(process.env.HTTPS_PROXY), |
| 118 | +}; |
| 119 | + |
| 120 | +export const Environment = (c?: Context) => { |
| 121 | + if (isNodeInstance) { |
| 122 | + return nodeEnv; |
| 123 | + } |
| 124 | + if (c) { |
| 125 | + return env(c); |
| 126 | + } |
| 127 | + return {}; |
| 128 | +}; |
0 commit comments