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

(feature) Return CA Certificate path/data #41

Merged
merged 10 commits into from
Nov 21, 2019
33 changes: 22 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export interface Options /* extends Partial<ICaBufferOpts & ICaPathOpts> */{
ui?: UserInterface
}

interface ICaBuffer {
ca: Buffer;
}
interface ICaPath {
caPath: string;
}
interface IDomainData {
key: Buffer;
cert: Buffer;
}
type IReturnCa<O extends Options> = O['getCaBuffer'] extends true ? ICaBuffer : false;
type IReturnCaPath<O extends Options> = O['getCaPath'] extends true ? ICaPath : false;
type IReturnData<O extends Options = {}> = (IDomainData) & (IReturnCa<O>) & (IReturnCaPath<O>);

Comment on lines +34 to +47
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zetlen Because of the additional flexibility of the API, I wrote these types that will cause the return value to be more explicit, depending on what flags you set when you call certificateFor().

Let me know if you think this is too much/too complicated. The alternative is to limit it to two different return types, but then you wind up with Partial properties:

type ReturnData = {
  key: Buffer,
  cert: Buffer
} | {
  ca?: Buffer,
  caPath?: string,
  key: Buffer,
  cert: Buffer
}

This probably wouldn't be an issue, ultimately. I just wanted it to be more clear what you're getting for what you give, and also get the benefits of strong types.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the self-documenting types!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve of the types; there are lot of ways to type this, but if it populates autosuggest correctly, then that's all that matters. Thanks for thinking about it carefully.

/**
* Request an SSL certificate for the given app name signed by the devcert root
* certificate authority. If devcert has previously generated a certificate for
Expand All @@ -43,7 +57,7 @@ export interface Options /* extends Partial<ICaBufferOpts & ICaPathOpts> */{
* are Buffers with the contents of the certificate private key and certificate
* file, respectively
*/
export async function certificateFor(domain: string, options: Options = {}) {
export async function certificateFor<O extends Options>(domain: string, options: O = {} as O): Promise<IReturnData<O>> {
debug(`Certificate requested for ${ domain }. Skipping certutil install: ${ Boolean(options.skipCertutilInstall) }. Skipping hosts file: ${ Boolean(options.skipHostsFile) }`);

if (options.ui) {
Expand Down Expand Up @@ -76,18 +90,15 @@ export async function certificateFor(domain: string, options: Options = {}) {
}

debug(`Returning domain certificate`);
if (options.returnCa) {
return {
ca: options.getCaBuffer && readFile(rootCACertPath),
caPath: options.getCaPath && rootCACertPath,
key: readFile(domainKeyPath),
cert: readFile(domainCertPath)
}
}
return {

const ret = {
key: readFile(domainKeyPath),
cert: readFile(domainCertPath)
};
} as IReturnData<O>;
if (options.getCaBuffer) (ret as ICaBuffer).ca = readFile(rootCACertPath);
if (options.getCaPath) (ret as ICaPath).caPath = rootCACertPath;

return ret;
}

export function hasCertificateFor(domain: string) {
Expand Down