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

fix: use async methods from macos and windows native addons #7

Merged
merged 1 commit into from
Aug 8, 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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"typescript": "^4.0.3"
},
"optionalDependencies": {
"macos-export-certificate-and-key": "^1.1.1",
"win-export-certificate-and-key": "^2.0.0"
"macos-export-certificate-and-key": "^1.2.0",
"win-export-certificate-and-key": "^2.1.0"
}
}
31 changes: 29 additions & 2 deletions src/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export async function * unixAsyncImpl(env: Record<string, string | undefined>):
}
}

export function * windowsImpl(): Iterable<string> {
export function * windowsSyncImpl(): Iterable<string> {
let exportSystemCertificates;
// try/catch helps bundlers deal with optional dependencies
// eslint-disable-next-line no-useless-catch
Expand All @@ -110,7 +110,21 @@ export function * windowsImpl(): Iterable<string> {
yield * exportSystemCertificates({ store: 'CA' });
}

export function * macosImpl(): Iterable<string> {
export async function * windowsAsyncImpl(): AsyncIterable<string> {
let exportSystemCertificatesAsync;
// try/catch helps bundlers deal with optional dependencies
// eslint-disable-next-line no-useless-catch
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
({ exportSystemCertificatesAsync } = require('win-export-certificate-and-key'));
} catch (err) {
throw err;
}
yield * await exportSystemCertificatesAsync({ store: 'ROOT' });
yield * await exportSystemCertificatesAsync({ store: 'CA' });
}

export function * macosSyncImpl(): Iterable<string> {
let exportSystemCertificates;
// try/catch helps bundlers deal with optional dependencies
// eslint-disable-next-line no-useless-catch
Expand All @@ -122,3 +136,16 @@ export function * macosImpl(): Iterable<string> {
}
yield * exportSystemCertificates();
}

export async function * macosAsyncImpl(): AsyncIterable<string> {
let exportSystemCertificatesAsync;
// try/catch helps bundlers deal with optional dependencies
// eslint-disable-next-line no-useless-catch
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
({ exportSystemCertificatesAsync } = require('macos-export-certificate-and-key'));
} catch (err) {
throw err;
}
yield * await exportSystemCertificatesAsync();
}
39 changes: 11 additions & 28 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { unixSyncImpl, unixAsyncImpl, windowsImpl, macosImpl } from './impl';
import { unixSyncImpl, unixAsyncImpl, windowsSyncImpl, macosSyncImpl, windowsAsyncImpl, macosAsyncImpl } from './impl';
import { rootCertificates } from 'tls';
import { once } from 'events';

export interface Options {
env?: Record<string, string | undefined>;
Expand All @@ -20,42 +19,26 @@ function maybeAddNodeCertificates(certs: Set<string>, opts: Options): string[] {
export function systemCertsSync(opts: Options = {}): string[] {
let certs: Set<string>;
if (process.platform === 'win32') {
certs = new Set(windowsImpl());
certs = new Set(windowsSyncImpl());
} else if (process.platform === 'darwin') {
certs = new Set(macosImpl());
certs = new Set(macosSyncImpl());
} else {
certs = new Set(unixSyncImpl(opts.env ?? process.env));
}
return maybeAddNodeCertificates(certs, opts);
}

// eslint-disable-next-line camelcase
declare const __webpack_require__: unknown;

export async function systemCertsAsync(opts: Options = {}): Promise<string[]> {
let certs: Set<string>;
if (process.platform === 'win32' || process.platform === 'darwin') {
const script = `
const { parentPort } = require('worker_threads');
const iterable = require(${JSON.stringify(__filename)}).systemCertsSync(${JSON.stringify(opts)});
parentPort.postMessage(new Set(iterable));
`;
try {
// eslint-disable-next-line camelcase
if (typeof __webpack_require__ !== 'undefined') {
throw new Error('Not attempting to start worker thread from bundled application');
}

const { Worker } = await import('worker_threads');
const worker = new Worker(script, { eval: true });
const [result] = await once(worker, 'message');
certs = result;
} catch (err: any) {
opts.asyncFallbackError = err;
return systemCertsSync();
const certs = new Set<string>();
if (process.platform === 'win32') {
for await (const cert of windowsAsyncImpl()) {
kmruiz marked this conversation as resolved.
Show resolved Hide resolved
certs.add(cert);
}
} else if (process.platform === 'darwin') {
for await (const cert of macosAsyncImpl()) {
certs.add(cert);
}
} else {
certs = new Set();
for await (const cert of unixAsyncImpl(opts.env ?? process.env)) {
certs.add(cert);
}
Expand Down
Loading