Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): local credentials warning suppress option #79

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fal-ai/serverless-client",
"description": "The fal serverless JS/TS client",
"version": "0.14.0",
"version": "0.14.1-alpha.0",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
45 changes: 45 additions & 0 deletions libs/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,43 @@
export type CredentialsResolver = () => string | undefined;

export type Config = {
/**
* The credentials to use for the fal serverless client. When using the
* client in the browser, it's recommended to use a proxy server to avoid
* exposing the credentials in the client's environment.
*
* By default it tries to use the `FAL_KEY` environment variable, when
* `process.env` is defined.
*
* @see https://fal.ai/docs/model-endpoints/server-side
* @see #suppressLocalCredentialsWarning
*/
credentials?: undefined | string | CredentialsResolver;
/**
* Suppresses the warning when the fal credentials are exposed in the
* browser's environment. Make sure you understand the security implications
* before enabling this option.
*/
suppressLocalCredentialsWarning?: boolean;
/**
* The URL of the proxy server to use for the client requests. The proxy
* server should forward the requests to the fal serverless rest api.
*/
proxyUrl?: string;
/**
* The request middleware to use for the client requests. By default it
* doesn't apply any middleware.
*/
requestMiddleware?: RequestMiddleware;
/**
* The response handler to use for the client requests. By default it uses
* a built-in response handler that returns the JSON response.
*/
responseHandler?: ResponseHandler<any>;

Check warning on line 44 in libs/client/src/config.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
/**
* The fetch implementation to use for the client requests. By default it uses
* the global `fetch` function.
*/
fetch?: typeof fetch;
};

Expand Down Expand Up @@ -48,6 +81,7 @@

const DEFAULT_CONFIG: Partial<Config> = {
credentials: credentialsFromEnv,
suppressLocalCredentialsWarning: false,
requestMiddleware: (request) => Promise.resolve(request),
responseHandler: defaultResponseHandler,
};
Expand All @@ -70,6 +104,17 @@
),
};
}
const { credentials, suppressLocalCredentialsWarning } = configuration;
if (
typeof window !== 'undefined' &&
credentials &&
!suppressLocalCredentialsWarning
) {
console.warn(
"The fal credentials are exposed in the browser's environment. " +
"That's not recommended for production use cases."
);
}
}

/**
Expand Down
6 changes: 0 additions & 6 deletions libs/client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
navigator?.userAgent === 'Cloudflare-Workers';

type RequestOptions = {
responseHandler?: ResponseHandler<any>;

Check warning on line 10 in libs/client/src/request.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
};

export async function dispatchRequest<Input, Output>(
Expand All @@ -32,12 +32,6 @@
url: targetUrl,
});
const authHeader = credentials ? { Authorization: `Key ${credentials}` } : {};
if (typeof window !== 'undefined' && credentials) {
console.warn(
"The fal credentials are exposed in the browser's environment. " +
"That's not recommended for production use cases."
);
}
const requestHeaders = {
...authHeader,
Accept: 'application/json',
Expand Down
Loading