UNPKG

@fal-ai/server-proxy

Version:

The fal.ai server proxy adapter for JavaScript and TypeScript Web frameworks

89 lines 3.61 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_PROXY_CONFIG = exports.DEFAULT_ALLOWED_URL_PATTERNS = exports.FAL_REST_API_URL = void 0; exports.createUrlMatcher = createUrlMatcher; exports.useEnvironmentFalKey = useEnvironmentFalKey; exports.fallbackToFalKey = fallbackToFalKey; exports.isAuthorizationHeaderPresent = isAuthorizationHeaderPresent; exports.applyProxyConfig = applyProxyConfig; exports.resolveProxyConfig = resolveProxyConfig; const picomatch_1 = __importDefault(require("picomatch")); const utils_1 = require("./utils"); /** * The fal REST API URL used for storage operations. */ exports.FAL_REST_API_URL = "rest.fal.ai"; /** * The default allowed URL patterns. Only `fal.run` and `queue.fal.run` are allowed by default. * You can add patterns like `fal.ai/**` or `fal.dev/**` if needed for your use case. * * Note: Uses picomatch glob syntax. The `?` character is escaped with `\\?` for literal matching. */ exports.DEFAULT_ALLOWED_URL_PATTERNS = [ "fal.run/**", "queue.fal.run/**", // Storage upload endpoints (exact matches, ? is escaped for literal match) `${exports.FAL_REST_API_URL}/storage/upload/initiate\\?storage_type=fal-cdn-v3`, `${exports.FAL_REST_API_URL}/storage/upload/complete-multipart\\?storage_type=fal-cdn-v3`, ]; /** * Creates a matcher function for URL patterns using picomatch. * Supports glob patterns like `*` (single segment) and `**` (any path). * * @param patterns the URL patterns to match against. * @returns a function that checks if a URL matches any of the patterns. */ function createUrlMatcher(patterns) { return (0, picomatch_1.default)(patterns, { dot: true }); } async function useEnvironmentFalKey() { return `Key ${process.env.FAL_KEY}`; } async function fallbackToFalKey(behavior) { return ((0, utils_1.singleHeaderValue)(behavior.getHeader("authorization")) ?? (await useEnvironmentFalKey())); } async function isAuthorizationHeaderPresent(behavior) { return behavior.getHeader("authorization") !== null; } exports.DEFAULT_PROXY_CONFIG = { allowedUrlPatterns: exports.DEFAULT_ALLOWED_URL_PATTERNS, allowedEndpoints: [], allowUnauthorizedRequests: true, isAuthenticated: isAuthorizationHeaderPresent, resolveFalAuth: fallbackToFalKey, }; /** * Merges the default proxy configuration with the provided configuration. * * @param config the proxy configuration to apply. * @returns the resolved proxy configuration. */ function applyProxyConfig(config) { const resolvedConfig = { ...exports.DEFAULT_PROXY_CONFIG, ...config, }; if (!Array.isArray(resolvedConfig.allowedEndpoints) || resolvedConfig.allowedEndpoints.length === 0) { console.warn("No allowed endpoints specified, all endpoints will be allowed. This is not recommended for production use cases."); } if (resolvedConfig.allowUnauthorizedRequests) { console.warn("Allowing unauthenticated requests. Make sure you protect your proxy endpoint or handle unauthenticated users appropriately."); } return resolvedConfig; } /** * Resolves the proxy configuration once. Use this at handler creation time * to avoid logging warnings on every request. * * @param config the proxy configuration to apply. * @returns the resolved proxy configuration. */ function resolveProxyConfig(config) { return applyProxyConfig(config); } //# sourceMappingURL=config.js.map