Skip to content

Commit

Permalink
Merge pull request #98 from legobeat/crypto-node-compat
Browse files Browse the repository at this point in the history
cryptoNode: fall back to Node.js crypto when webcrypto not available
  • Loading branch information
paulmillr authored Aug 30, 2024
2 parents cefaf79 + 6d3aa0a commit b505154
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cryptoNode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
// We prefer WebCrypto aka globalThis.crypto, which exists in node.js 16+.
// Falls back to Node.js built-in crypto for Node.js <=v14
// See utils.ts for details.
// The file will throw on node.js 14 and earlier.
// @ts-ignore
import * as nc from 'node:crypto';
export const crypto =
nc && typeof nc === 'object' && 'webcrypto' in nc ? (nc.webcrypto as any) : undefined;
nc && typeof nc === 'object' && 'webcrypto' in nc
? (nc.webcrypto as any)
: nc && typeof nc === 'object' && 'randomBytes' in nc
? nc
: undefined;

0 comments on commit b505154

Please sign in to comment.