-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.ts
140 lines (124 loc) · 4.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
export const TARGET_URL_HEADER = 'x-fal-target-url';
export const DEFAULT_PROXY_ROUTE = '/api/fal/proxy';
const FAL_KEY = process.env.FAL_KEY;
const FAL_KEY_ID = process.env.FAL_KEY_ID;
const FAL_KEY_SECRET = process.env.FAL_KEY_SECRET;
export type HeaderValue = string | string[] | undefined | null;
const FAL_URL_REG_EXP = /(\.|^)fal\.(run|ai)$/;
/**
* The proxy behavior that is passed to the proxy handler. This is a subset of
* request objects that are used by different frameworks, like Express and NextJS.
*/
export interface ProxyBehavior<ResponseType> {
id: string;
method: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
respondWith(status: number, data: string | any): ResponseType;
getHeaders(): Record<string, HeaderValue>;
getHeader(name: string): HeaderValue;
sendHeader(name: string, value: string): void;
getBody(): Promise<string | undefined>;
resolveApiKey?: () => Promise<string | undefined>;
}
/**
* Utility to get a header value as `string` from a Headers object.
*
* @private
* @param request the header value.
* @returns the header value as `string` or `undefined` if the header is not set.
*/
function singleHeaderValue(value: HeaderValue): string | undefined {
if (!value) {
return undefined;
}
if (Array.isArray(value)) {
return value[0];
}
return value;
}
function getFalKey(): string | undefined {
if (FAL_KEY) {
return FAL_KEY;
}
if (FAL_KEY_ID && FAL_KEY_SECRET) {
return `${FAL_KEY_ID}:${FAL_KEY_SECRET}`;
}
return undefined;
}
const EXCLUDED_HEADERS = ['content-length', 'content-encoding'];
/**
* A request handler that proxies the request to the fal-serverless
* endpoint. This is useful so client-side calls to the fal-serverless endpoint
* can be made without CORS issues and the correct credentials can be added
* effortlessly.
*
* @param behavior the request proxy behavior.
* @returns Promise<any> the promise that will be resolved once the request is done.
*/
export async function handleRequest<ResponseType>(
behavior: ProxyBehavior<ResponseType>
) {
const targetUrl = singleHeaderValue(behavior.getHeader(TARGET_URL_HEADER));
if (!targetUrl) {
return behavior.respondWith(400, `Missing the ${TARGET_URL_HEADER} header`);
}
const urlHost = new URL(targetUrl).host;
if (!FAL_URL_REG_EXP.test(urlHost)) {
return behavior.respondWith(412, `Invalid ${TARGET_URL_HEADER} header`);
}
const falKey = behavior.resolveApiKey
? await behavior.resolveApiKey()
: getFalKey();
if (!falKey) {
return behavior.respondWith(401, 'Missing fal.ai credentials');
}
// pass over headers prefixed with x-fal-*
const headers: Record<string, HeaderValue> = {};
Object.keys(behavior.getHeaders()).forEach((key) => {
if (key.toLowerCase().startsWith('x-fal-')) {
headers[key.toLowerCase()] = behavior.getHeader(key);
}
});
const proxyUserAgent = `@fal-ai/serverless-proxy/${behavior.id}`;
const userAgent = singleHeaderValue(behavior.getHeader('user-agent'));
const res = await fetch(targetUrl, {
method: behavior.method,
headers: {
...headers,
authorization:
singleHeaderValue(behavior.getHeader('authorization')) ??
`Key ${falKey}`,
accept: 'application/json',
'content-type': 'application/json',
'user-agent': userAgent,
'x-fal-client-proxy': proxyUserAgent,
} as HeadersInit,
body:
behavior.method?.toUpperCase() === 'GET'
? undefined
: await behavior.getBody(),
});
// copy headers from fal to the proxied response
res.headers.forEach((value, key) => {
if (!EXCLUDED_HEADERS.includes(key.toLowerCase())) {
behavior.sendHeader(key, value);
}
});
if (res.headers.get('content-type')?.includes('application/json')) {
const data = await res.json();
return behavior.respondWith(res.status, data);
}
const data = await res.text();
return behavior.respondWith(res.status, data);
}
export function fromHeaders(
headers: Headers
): Record<string, string | string[]> {
// TODO once Header.entries() is available, use that instead
// Object.fromEntries(headers.entries());
const result: Record<string, string | string[]> = {};
headers.forEach((value, key) => {
result[key] = value;
});
return result;
}