Skip to content

Commit 85699d2

Browse files
committed
add wrapper for environment variables with support for fetching from file path
1 parent 5a10ec0 commit 85699d2

File tree

4 files changed

+135
-9
lines changed

4 files changed

+135
-9
lines changed

src/handlers/handlerUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ export function constructConfigFromRequestHeaders(
856856
azureApiVersion: requestHeaders[`x-${POWERED_BY}-azure-api-version`],
857857
azureEndpointName: requestHeaders[`x-${POWERED_BY}-azure-endpoint-name`],
858858
azureFoundryUrl: requestHeaders[`x-${POWERED_BY}-azure-foundry-url`],
859-
azureExtraParams: requestHeaders[`x-${POWERED_BY}-azure-extra-params`],
859+
azureExtraParameters: requestHeaders[`x-${POWERED_BY}-azure-extra-params`],
860860
};
861861

862862
const awsConfig = {

src/providers/azure-ai-inference/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ const AzureAIInferenceAPI: ProviderAPIConfig = {
3939
headers: async ({ providerOptions, fn }) => {
4040
const {
4141
apiKey,
42-
azureExtraParams,
42+
azureExtraParameters,
4343
azureDeploymentName,
4444
azureAdToken,
4545
azureAuthMode,
4646
} = providerOptions;
4747

4848
const headers: Record<string, string> = {
49-
'extra-parameters': azureExtraParams ?? 'drop',
49+
'extra-parameters': azureExtraParameters ?? 'drop',
5050
...(azureDeploymentName && {
5151
'azureml-model-deployment': azureDeploymentName,
5252
}),

src/types/requestBody.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export interface Options {
9595
awsBedrockModel?: string;
9696
awsServerSideEncryption?: string;
9797
awsServerSideEncryptionKMSKeyId?: string;
98+
awsService?: string;
9899
foundationModel?: string;
99100

100101
/** Sagemaker specific */
@@ -131,22 +132,22 @@ export interface Options {
131132
beforeRequestHooks?: HookObject[];
132133
defaultInputGuardrails?: HookObject[];
133134
defaultOutputGuardrails?: HookObject[];
134-
135135
/** OpenAI specific */
136136
openaiProject?: string;
137137
openaiOrganization?: string;
138138
openaiBeta?: string;
139-
140139
/** Azure Inference Specific */
141-
azureDeploymentName?: string;
142140
azureApiVersion?: string;
143-
azureExtraParams?: string;
144141
azureFoundryUrl?: string;
142+
azureExtraParameters?: string;
143+
azureDeploymentName?: string;
145144

146145
/** The parameter to determine if extra non-openai compliant fields should be returned in response */
147146
strictOpenAiCompliance?: boolean;
147+
148148
/** Parameter to determine if fim/completions endpoint is to be used */
149149
mistralFimCompletion?: String;
150+
150151
/** Anthropic specific headers */
151152
anthropicBeta?: string;
152153
anthropicVersion?: string;

src/utils/env.ts

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,128 @@
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+
}
335
}
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

Comments
 (0)