summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts
blob: 1a26d777a45cf3002ce00f7565865a346d9a5fbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type { Crypto } from "../../../deps.ts";

let webCrypto: Crypto | undefined = undefined;

/**
 * Try to get an instance of the Crypto API from the current runtime. Should support Node,
 * as well as others, like Deno, that implement Web APIs.
 */
export async function getWebCrypto(): Promise<Crypto> {
  if (webCrypto) {
    return webCrypto;
  }

  try {
    /**
     * Naively attempt a Node import...
     */
    // @ts-ignore: We'll handle any errors...
    // dnt-shim-ignore
    const _crypto = await require("node:crypto");
    webCrypto = _crypto as unknown as Crypto;
  } catch (_err) {
    /**
     * Naively attempt to access Crypto as a global object, which popular alternative run-times
     * support.
     */
    // @ts-ignore: ...right here.
    const _crypto: Crypto = globalThis.crypto;

    if (!_crypto) {
      // We tried to access it both in Node and
      throw new MissingWebCrypto();
    }

    webCrypto = _crypto;
  }

  return webCrypto;
}

class MissingWebCrypto extends Error {
  constructor() {
    const message = "An instance of the Crypto API could not be located";
    super(message);
    this.name = "MissingWebCrypto";
  }
}