Skip to content

Commit

Permalink
wip: Use browser resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
D4nte committed May 5, 2022
1 parent 09189d2 commit 27952e4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
"**/*.spec.js"
]
},
"browser": {
"./lib/crypto_subtle.js": "./lib/crypto_subtle_browser.js"
},
"size-limit": [
{
"path": "build/esm/index.js",
Expand Down
24 changes: 1 addition & 23 deletions src/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
import nodeCrypto from "crypto";

// IE 11
declare global {
interface Window {
msCrypto?: Crypto;
}

interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}

const crypto =
(typeof window !== "undefined" &&
(window as Window) &&
(window.crypto || window.msCrypto)) ||
(nodeCrypto.webcrypto as unknown as Crypto);
const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;

if (subtle === undefined) {
throw new Error("crypto and/or subtle api unavailable");
}
import { crypto, subtle } from "./crypto_subtle";

export { crypto, subtle };

Expand Down
15 changes: 15 additions & 0 deletions src/lib/crypto_subtle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import nodeCrypto from "crypto";

// Types do not seem up-to-date
const crypto: Crypto = nodeCrypto.webcrypto as unknown as Crypto;
if (crypto === undefined) {
throw new Error("node crypto api unavailable");
}

const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;

if (subtle === undefined) {
throw new Error("node subtle api unavailable");
}

export { crypto, subtle };
22 changes: 22 additions & 0 deletions src/lib/crypto_subtle_browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
declare global {
interface Window {
msCrypto?: Crypto;
}

interface Crypto {
webkitSubtle?: SubtleCrypto;
}
}

const crypto = window.crypto || window.msCrypto;
if (crypto === undefined) {
throw new Error("browser crypto api unavailable");
}

const subtle: SubtleCrypto = crypto.subtle || crypto.webkitSubtle;

if (subtle === undefined) {
throw new Error("browser subtle api unavailable");
}

export { crypto, subtle };

0 comments on commit 27952e4

Please sign in to comment.