diff --git a/package.json b/package.json index 5581876..06a22a3 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/impl.ts b/src/impl.ts index c6058a5..5d74352 100644 --- a/src/impl.ts +++ b/src/impl.ts @@ -96,7 +96,7 @@ export async function * unixAsyncImpl(env: Record): } } -export function * windowsImpl(): Iterable { +export function * windowsSyncImpl(): Iterable { let exportSystemCertificates; // try/catch helps bundlers deal with optional dependencies // eslint-disable-next-line no-useless-catch @@ -110,7 +110,21 @@ export function * windowsImpl(): Iterable { yield * exportSystemCertificates({ store: 'CA' }); } -export function * macosImpl(): Iterable { +export async function * windowsAsyncImpl(): AsyncIterable { + 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 { let exportSystemCertificates; // try/catch helps bundlers deal with optional dependencies // eslint-disable-next-line no-useless-catch @@ -122,3 +136,16 @@ export function * macosImpl(): Iterable { } yield * exportSystemCertificates(); } + +export async function * macosAsyncImpl(): AsyncIterable { + 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(); +} diff --git a/src/index.ts b/src/index.ts index 51a8a85..f59c1f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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; @@ -20,42 +19,26 @@ function maybeAddNodeCertificates(certs: Set, opts: Options): string[] { export function systemCertsSync(opts: Options = {}): string[] { let certs: Set; 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 { - let certs: Set; - 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(); + if (process.platform === 'win32') { + for await (const cert of windowsAsyncImpl()) { + 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); }