Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Aug 17, 2023
1 parent 7e9fc52 commit 0772645
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions source/x509/_crypto.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const x509 = require("@peculiar/x509");
import { Crypto } from "@peculiar/webcrypto";
import { Crypto as PeculiarWebCrypto } from "@peculiar/webcrypto";

let _crypto: Crypto | undefined;
let _crypto: PeculiarWebCrypto | undefined;

declare const crypto: any;
declare const window: any;

const ignoreCrypto = process.env.IGNORE_SUBTLE_FROM_CRYPTO;

if (typeof window === "undefined") {
_crypto = require("crypto");
if (!_crypto?.subtle) {
_crypto = new Crypto();
if (!_crypto?.subtle || ignoreCrypto) {
_crypto = new PeculiarWebCrypto();
console.warn("using @peculiar/webcrypto");
} else {
console.warn("using nodejs crypto (native)");
}
x509.cryptoProvider.set(_crypto);
} else {
// using browser crypto
console.warn("using browser crypto (native)");
_crypto = crypto;
x509.cryptoProvider.set(crypto);
}

interface CryptoInterface {}
export function getCrypto(): Crypto {
export function getCrypto(): PeculiarWebCrypto {
return _crypto || crypto || require("crypto");
}
export * as x509 from "@peculiar/x509";

0 comments on commit 0772645

Please sign in to comment.