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

Support for SSL client certificate in Nodejs undici #549

Closed
snird opened this issue Dec 13, 2023 · 4 comments
Closed

Support for SSL client certificate in Nodejs undici #549

snird opened this issue Dec 13, 2023 · 4 comments

Comments

@snird
Copy link

snird commented Dec 13, 2023

I know this library is intended for use in browser & nodejs, but one of the strong use cases for using it in node is the massive performance boost the underlying undici has over the http module.

So given the benefit of using a library that utilizes undici, and that ky is currently the best wrapper for a more convenient use - I ask about a specific implementation for nodejs.

Undici can utilize client certificates: https://github.com/nodejs/undici/blob/main/docs/best-practices/client-certificate.md
If I'm not mistaken, the implementation on ky side is to allow some way of passing the tls details to the connect method (which is standardized, only the tls parameters aren't)

Is it possible?

@sindresorhus
Copy link
Owner

ky depends on fetch, which is built on Unidici in Node.js. You will have to get Node.js to expose additional options on fetch for this to be possible. Ky has no access to Unidici through fetch.

@sholladay
Copy link
Collaborator

I found this example that reportedly works:
nodejs/node#48977

From what I can tell, that should work with Ky.

I'm closing this issue as I don't think there is anything for us to do here.

@alansikora
Copy link

alansikora commented Aug 1, 2024

Hey, sorry to comment on a closed issue but I tried this using fetch:

const response = await fetch(
      `${domain}/request`,
      {
        method: "GET",
        headers: {
          Authorization: `Bearer ${token}`,
        },
        dispatcher: agent,
      }
    );

And it works fine, but:

const response = await ky
    .get(`${domain}/request`, {
      headers: {
        Authorization: `Bearer ${token}`,
      },
      dispatcher: agent,
    })
    .json();

Won't work. It ignores the dispatcher option altogether.

In both cases, for this to work with Typescript, the following is needed:

import { Agent } from "undici";

declare global {
  interface RequestInit {
    dispatcher?: Agent | undefined;
  }
}

So it might be related to that. Is it possible to force a property to be passed down to fetch?

@sholladay
Copy link
Collaborator

My best guess would be that Ky is only passing dispatcher to new Request() and not to fetch(), but that Node only supports the latter.

Ky already has a mechanism designed to handle cases like this but it's error-prone and perhaps this case isn't being handled properly.

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

4 participants