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

Fetch timeout #330

Open
xtrinch opened this issue Sep 2, 2024 · 4 comments
Open

Fetch timeout #330

xtrinch opened this issue Sep 2, 2024 · 4 comments

Comments

@xtrinch
Copy link

xtrinch commented Sep 2, 2024

What is the current behavior?

Speech to text is returning DeepgramUnknownError: Failed to construct 'Request': member signal is not of type AbortSignal
when attempting to use signal to abort the request after some time

Steps to reproduce

  const client = createClient(
    "API_KEY",
    {
          global: {
            fetch: {
              options: {
                url: "SOME_URL",
                signal: AbortSignal.timeout(config.timeoutSeconds * 1000),
              },
            },
          }
    },
  );

Expected behavior

I would have presumed it wouldn't throw an error, unless I'm doing something wrong, but the types seem to allow this?
Is there another way to add the timeout?

Environment

Node 18

@lukeocodes
Copy link
Contributor

Can you give me a little more info about your environment?

@xtrinch
Copy link
Author

xtrinch commented Sep 2, 2024

Node 18-alpine docker image, "@deepgram/sdk": "^3.5.0", anything else that could be relevant?

@lukeocodes
Copy link
Contributor

lukeocodes commented Sep 2, 2024

We use cross-fetch... I've not seen your method of timeout, but this is what someone else had been using.

  const controller = new AbortController();
  const { signal } = controller;

  // set up your timeout

  const client = createClient(
    "API_KEY",
    {
      global: {
        fetch: {
          options: {
            url: "SOME_URL",
            signal,
          },
        },
      }
    },
  );

You can also provide an entirely custom fetch and configure your requirements that way

@xtrinch
Copy link
Author

xtrinch commented Sep 2, 2024

Thanks for the tips, I converted it to a custom fetch and it now works. It most likely appears to be a lifetime issue because if I pass it into global options I guess the same signal tries to be used multiple times. For anyone interested in the future, this worked:

const customFetch = async (
  timeoutSeconds: number,
  input: string | URL | globalThis.Request,
  init?: RequestInit,
): Promise<Response> => {
  return fetch(input, {
    ...init,
    signal: AbortSignal.timeout(timeoutSeconds * 1000),
  });
};

export const configureDeepgram = (config: DeepgramConfig): DeepgramClient => {
  const client = createClient(
    config.apiKey,
     {
          global: {
            fetch: {
              client: (
                input: string | URL | globalThis.Request,
                init?: RequestInit,
              ): Promise<Response> =>
                customFetch(config.timeoutSeconds, input, init),
              options: {
                url: config.url,
              },
            },
          },
        }
  );

  return client;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants