File size: 3,674 Bytes
25d91a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Covers automatic NODE_EXTRA_CA_CERTS discovery and validation.
import { describe, expect, it } from "vitest";
import { resolveAutoNodeExtraCaCerts } from "./node-extra-ca-certs.js";

const DEBIAN_CA_BUNDLE_PATH = "/etc/ssl/certs/ca-certificates.crt";
const FEDORA_CA_BUNDLE_PATH = "/etc/pki/tls/certs/ca-bundle.crt";
const GENERIC_CA_BUNDLE_PATH = "/etc/ssl/ca-bundle.pem";
const VERSION_MANAGER_EXEC_PATHS = [
  ["nvm", "/home/test/.nvm/versions/node/v22/bin/node"],
  ["fnm", "/home/test/.fnm/node-versions/v22/installation/bin/node"],
  ["fnm XDG data", "/home/test/.local/share/fnm/node-versions/v22/installation/bin/node"],
  ["nvs dotted home", "/home/test/.nvs/node/22.14.0/x64/bin/node"],
  ["volta", "/home/test/.volta/tools/image/node/22.14.0/bin/node"],
  ["asdf", "/home/test/.asdf/installs/nodejs/22.14.0/bin/node"],
  ["mise", "/home/test/.local/share/mise/installs/node/22.14.0/bin/node"],
  ["n", "/home/test/.n/bin/node"],
  ["nodenv", "/home/test/.nodenv/versions/22.14.0/bin/node"],
  ["nodebrew", "/home/test/.nodebrew/node/v22.14.0/bin/node"],
  ["nvs", "/home/test/nvs/node/22.14.0/x64/bin/node"],
  ["raw path before normalization", "/home/test/.nvm/../system/bin/node"],
] as const;

function allowOnly(path: string) {
  return (candidate: string) => {
    if (candidate !== path) {
      throw new Error("ENOENT");
    }
  };
}

describe("resolveAutoNodeExtraCaCerts", () => {
  it("returns undefined on non-linux platforms", () => {
    expect(
      resolveAutoNodeExtraCaCerts({
        env: { NVM_DIR: "/home/test/.nvm" },
        platform: "darwin",
        accessSync: allowOnly(DEBIAN_CA_BUNDLE_PATH),
      }),
    ).toBeUndefined();
  });

  it("returns the first readable Linux CA bundle", () => {
    expect(
      resolveAutoNodeExtraCaCerts({
        env: { NVM_DIR: "/home/test/.nvm" },
        platform: "linux",
        execPath: "/usr/bin/node",
        accessSync: allowOnly(FEDORA_CA_BUNDLE_PATH),
      }),
    ).toBe(FEDORA_CA_BUNDLE_PATH);
  });

  it.each(VERSION_MANAGER_EXEC_PATHS)("detects %s via execPath", (_manager, execPath) => {
    expect(
      resolveAutoNodeExtraCaCerts({
        env: {},
        platform: "linux",
        execPath,
        accessSync: allowOnly(GENERIC_CA_BUNDLE_PATH),
      }),
    ).toBe(GENERIC_CA_BUNDLE_PATH);
  });

  it("returns undefined when NODE_EXTRA_CA_CERTS is already set", () => {
    expect(
      resolveAutoNodeExtraCaCerts({
        env: {
          NVM_DIR: "/home/test/.nvm",
          NODE_EXTRA_CA_CERTS: "/custom/ca.pem",
        },
        platform: "linux",
        accessSync: allowOnly(DEBIAN_CA_BUNDLE_PATH),
      }),
    ).toBeUndefined();
  });

  it.each([
    ["/usr/bin/node", undefined],
    ["/usr/bin/node", ""],
    ["/usr/bin/node", " \t "],
    ["/home/test/.NVM/versions/node/v22/bin/node", undefined],
    ["/home/test/library/application support/fnm/bin/node", undefined],
    ["/home/test/.local/share/pnpm/node", undefined],
    ["/home/test/.nvm\\versions\\node\\v22\\bin\\node", undefined],
  ] as const)("does not infer Linux CA settings for %s with NVM_DIR=%j", (execPath, NVM_DIR) => {
    expect(
      resolveAutoNodeExtraCaCerts({
        env: { NVM_DIR },
        platform: "linux",
        execPath,
        accessSync: allowOnly(DEBIAN_CA_BUNDLE_PATH),
      }),
    ).toBeUndefined();
  });

  it("returns the readable Linux CA bundle for nvm-managed node", () => {
    expect(
      resolveAutoNodeExtraCaCerts({
        env: { NVM_DIR: "/home/test/.nvm" },
        platform: "linux",
        execPath: "/usr/bin/node",
        accessSync: allowOnly(GENERIC_CA_BUNDLE_PATH),
      }),
    ).toBe(GENERIC_CA_BUNDLE_PATH);
  });
});