summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/iso/isoCrypto/getWebCrypto.ts
blob: f07d4a2097ac10712118e3dc9719ae6d2c8fed27 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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;
  }

  /**
   * Naively attempt to access Crypto as a global object, which popular alternative run-times
   * support.
   */
  const _globalThisCrypto = _getWebCryptoInternals.stubThisGlobalThisCrypto();

  if (_globalThisCrypto) {
    webCrypto = _globalThisCrypto;
    return webCrypto;
  }

  /**
   * `globalThis.crypto` isn't available, so attempt a Node import...
   */
  const _nodeCrypto = await _getWebCryptoInternals.stubThisImportNodeCrypto();

  if (_nodeCrypto?.webcrypto) {
    webCrypto = _nodeCrypto.webcrypto as Crypto;
    return webCrypto;
  }

  // We tried to access it both in Node and globally, so bail out
  throw new MissingWebCrypto();
}

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

// Make it possible to stub return values during testing
export const _getWebCryptoInternals = {
  stubThisImportNodeCrypto: async () => {
    try {
      // dnt-shim-ignore
      /**
       * The `webpackIgnore` here is to help support Next.js' Edge runtime.
       * See https://github.com/MasterKale/SimpleWebAuthn/issues/517 for more info.
       */
      const _nodeCrypto = await import(/* webpackIgnore: true */ 'node:crypto');
      return _nodeCrypto;
    } catch (_err) {
      /**
       * Intentionally declaring webcrypto as undefined because we're assuming the Node import
       * failed due to either:
       * - `import()` isn't supported
       * - `node:crypto` is unavailable.
       */
      return { webcrypto: undefined };
    }
  },
  stubThisGlobalThisCrypto: () => globalThis.crypto,
  // Make it possible to reset the `webCrypto` at the top of the file
  setCachedCrypto: (newCrypto: Crypto | undefined) => {
    webCrypto = newCrypto;
  },
};