@jsenv/https-local 3.0.7 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +160 -192
  2. package/package.json +10 -30
  3. package/src/certificate_authority.js +111 -110
  4. package/src/certificate_request.js +37 -36
  5. package/src/hosts_file_verif.js +34 -35
  6. package/src/https_local_cli.mjs +74 -0
  7. package/src/internal/authority_file_infos.js +12 -13
  8. package/src/internal/browser_detection.js +4 -4
  9. package/src/internal/certificate_authority_file_urls.js +23 -23
  10. package/src/internal/certificate_data_converter.js +39 -39
  11. package/src/internal/certificate_generator.js +39 -39
  12. package/src/internal/command.js +6 -6
  13. package/src/internal/exec.js +10 -10
  14. package/src/internal/forge.js +3 -3
  15. package/src/internal/hosts/hosts_utils.js +2 -2
  16. package/src/internal/hosts/parse_hosts.js +67 -66
  17. package/src/internal/hosts/read_hosts.js +5 -6
  18. package/src/internal/hosts/write_hosts.js +29 -31
  19. package/src/internal/hosts/write_line_hosts.js +30 -32
  20. package/src/internal/hosts.js +5 -5
  21. package/src/internal/linux/chrome_linux.js +19 -20
  22. package/src/internal/linux/firefox_linux.js +19 -20
  23. package/src/internal/linux/linux.js +8 -8
  24. package/src/internal/linux/linux_trust_store.js +58 -59
  25. package/src/internal/linux/nss_linux.js +20 -21
  26. package/src/internal/mac/chrome_mac.js +15 -16
  27. package/src/internal/mac/firefox_mac.js +20 -21
  28. package/src/internal/mac/mac.js +10 -10
  29. package/src/internal/mac/mac_keychain.js +46 -47
  30. package/src/internal/mac/nss_mac.js +29 -30
  31. package/src/internal/mac/safari.js +2 -2
  32. package/src/internal/memoize.js +14 -14
  33. package/src/internal/nssdb_browser.js +150 -145
  34. package/src/internal/platform.js +6 -6
  35. package/src/internal/search_certificate_in_command_output.js +4 -4
  36. package/src/internal/trust_query.js +4 -4
  37. package/src/internal/unsupported_platform/unsupported_platform.js +5 -5
  38. package/src/internal/validity_formatting.js +32 -32
  39. package/src/internal/windows/chrome_windows.js +26 -27
  40. package/src/internal/windows/edge.js +2 -2
  41. package/src/internal/windows/firefox_windows.js +31 -32
  42. package/src/internal/windows/windows.js +10 -10
  43. package/src/internal/windows/windows_certutil.js +41 -42
  44. package/src/jsenvParameters.js +2 -2
  45. package/src/main.js +5 -8
  46. package/src/validity_duration.js +12 -12
package/README.md CHANGED
@@ -1,4 +1,6 @@
1
- # https local [![npm package](https://img.shields.io/npm/v/@jsenv/https-local.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/https-local)
1
+ # https local
2
+
3
+ [![npm package](https://img.shields.io/npm/v/@jsenv/https-local.svg?logo=npm&label=package)](https://www.npmjs.com/package/@jsenv/https-local)
2
4
 
3
5
  A programmatic way to generate locally trusted certificates.
4
6
 
@@ -8,88 +10,52 @@ Works on mac, linux and windows.
8
10
 
9
11
  # How to use
10
12
 
11
- 1 - Install _@jsenv/https-local_
12
-
13
- ```console
14
- npm install --save-dev @jsenv/https-local
15
- ```
13
+ The following steps can be taken to start a local server in https.
16
14
 
17
- 2 - Create _install_certificate_authority.mjs_
15
+ 1. Install the root certificate using https-local
16
+ 2. Request certificate for your server
17
+ 3. Start that server
18
18
 
19
- ```js
20
- /*
21
- * This file needs to be executed once.
22
- * After that the root certificate is valid for 20 years.
23
- * Re-executing this file will log the current root certificate validity and trust status.
24
- * Re-executing this file 20 years later would reinstall a root certificate and re-trust it.
25
- *
26
- * Read more in https://github.com/jsenv/https-local#installCertificateAuthority
27
- */
28
-
29
- import {
30
- installCertificateAuthority,
31
- verifyHostsFile,
32
- } from "@jsenv/https-local"
19
+ ## 1. Install the root certificate
33
20
 
34
- await installCertificateAuthority({
35
- tryToTrust: true,
36
- NSSDynamicInstall: true,
37
- })
38
- await verifyHostsFile({
39
- ipMappings: {
40
- "127.0.0.1": ["localhost"],
41
- },
42
- tryToUpdatesHostsFile: true,
43
- })
21
+ ```console
22
+ npx @jsenv/https-local install --trust
44
23
  ```
45
24
 
46
- 3 - Run with node
25
+ This will install a root certificate valid for 20 years.
47
26
 
48
- ```console
49
- node ./install_certificate_authority.mjs
50
- ```
27
+ - Re-executing this command will log the current root certificate validity and trust status.
28
+ - Re-executing this command 20 years later would reinstall a root certificate and re-trust it
29
+
30
+ ## 2. Request certificate for your server
51
31
 
52
- 4 - Create _start_dev_server.mjs_
32
+ _start_dev_server.mjs_
53
33
 
54
34
  ```js
55
- /*
56
- * This file uses "@jsenv/https-local" to obtain a certificate used to start a server in https.
57
- * The certificate is valid for 1 year (396 days) and is issued by a certificate authority trusted on this machine.
58
- * If the certificate authority was not installed before executing this file, an error is thrown
59
- * explaining that certificate authority must be installed first.
60
- *
61
- * To install the certificate authority, you can use the following command
62
- *
63
- * > node ./install_certificate_authority.mjs
64
- *
65
- * Read more in https://github.com/jsenv/https-local#requestCertificate
66
- */
67
-
68
- import { createServer } from "node:https"
69
- import { requestCertificate } from "@jsenv/https-local"
70
-
71
- const { certificate, privateKey } = requestCertificate()
35
+ import { createServer } from "node:https";
36
+ import { requestCertificate } from "@jsenv/https-local";
72
37
 
38
+ const { certificate, privateKey } = requestCertificate();
73
39
  const server = createServer(
74
40
  {
75
41
  cert: certificate,
76
42
  key: privateKey,
77
43
  },
78
44
  (request, response) => {
79
- const body = "Hello world"
45
+ const body = "Hello world";
80
46
  response.writeHead(200, {
81
47
  "content-type": "text/plain",
82
48
  "content-length": Buffer.byteLength(body),
83
- })
84
- response.write(body)
85
- response.end()
49
+ });
50
+ response.write(body);
51
+ response.end();
86
52
  },
87
- )
88
- server.listen(8080)
89
- console.log(`Server listening at https://local.example:8080`)
53
+ );
54
+ server.listen(8080);
55
+ console.log(`Server listening at https://local.example:8080`);
90
56
  ```
91
57
 
92
- 5 - Start server with node
58
+ ## 3. Start the server
93
59
 
94
60
  ```console
95
61
  node ./start_dev_server.mjs
@@ -111,15 +77,142 @@ In the unlikely scenario where a local server is running for more than a year wi
111
77
  The **authority root certificate** expires after 20 years which is close to the maximum allowed duration.
112
78
  In the very unlikely scenario where you are using the same machine for more than 20 years, re-execute [installCertificateAuthority](#installCertificateAuthority) to update certificate authority then restart your server.
113
79
 
114
- # installCertificateAuthority
80
+ # JavaScript API
81
+
82
+ ## requestCertificate
83
+
84
+ _requestCertificate_ function returns a certificate and private key that can be used to start a server in HTTPS.
85
+
86
+ ```js
87
+ import { createServer } from "node:https";
88
+ import { requestCertificate } from "@jsenv/https-local";
89
+
90
+ const { certificate, privateKey } = requestCertificate({
91
+ altNames: ["localhost", "local.example"],
92
+ });
93
+ ```
94
+
95
+ [installCertificateAuthority](#installCertificateAuthority) must be called before this function.
96
+
97
+ ## verifyHostsFile
98
+
99
+ This function is not mandatory to obtain the https certificates.
100
+ But it is useful to programmatically verify ip mappings that are important for your local server are present in hosts file.
101
+
102
+ ```js
103
+ import { verifyHostsFile } from "@jsenv/https-local";
104
+
105
+ await verifyHostsFile({
106
+ ipMappings: {
107
+ "127.0.0.1": ["localhost", "local.example"],
108
+ },
109
+ });
110
+ ```
111
+
112
+ Find below logs written in terminal when this function is executed.
113
+
114
+ <details>
115
+ <summary>mac and linux</summary>
116
+
117
+ ```console
118
+ > node ./verify_hosts.mjs
119
+
120
+ Check hosts file content...
121
+ ⚠ 1 mapping is missing in hosts file
122
+ --- hosts file path ---
123
+ /etc/hosts
124
+ --- line(s) to add ---
125
+ 127.0.0.1 localhost local.example
126
+ ```
127
+
128
+ </details>
129
+
130
+ <details>
131
+ <summary>windows</summary>
132
+
133
+ ```console
134
+ > node ./verify_hosts.mjs
135
+
136
+ Check hosts file content...
137
+ ⚠ 1 mapping is missing in hosts file
138
+ --- hosts file path ---
139
+ C:\\Windows\\System32\\Drivers\\etc\\hosts
140
+ --- line(s) to add ---
141
+ 127.0.0.1 localhost local.example
142
+ ```
143
+
144
+ </details>
145
+
146
+ ### Auto update hosts
147
+
148
+ It's possible to update hosts file programmatically using _tryToUpdateHostsFile_.
149
+
150
+ ```js
151
+ import { verifyHostsFile } from "@jsenv/https-local";
152
+
153
+ await verifyHostsFile({
154
+ ipMappings: {
155
+ "127.0.0.1": ["localhost", "local.example"],
156
+ },
157
+ tryToUpdateHostsFile: true,
158
+ });
159
+ ```
160
+
161
+ <details>
162
+ <summary>mac and linux</summary>
163
+
164
+ ```console
165
+ Check hosts file content...
166
+ ℹ 1 mapping is missing in hosts file
167
+ Adding 1 mapping(s) in hosts file...
168
+ ❯ echo "127.0.0.1 local.example" | sudo tee -a /etc/hosts
169
+ Password:
170
+ ✔ mappings added to hosts file
171
+ ```
172
+
173
+ _Second execution logs_
174
+
175
+ ```console
176
+ > node ./verify_hosts.mjs
177
+
178
+ Check hosts file content...
179
+ ✔ all ip mappings found in hosts file
180
+ ```
181
+
182
+ </details>
183
+
184
+ <details>
185
+ <summary>windows</summary>
186
+
187
+ ```console
188
+ Check hosts file content...
189
+ ℹ 1 mapping is missing in hosts file
190
+ Adding 1 mapping(s) in hosts file...
191
+ ❯ (echo 127.0.0.1 local.example) >> C:\\Windows\\System32\\Drivers\\etc\\hosts
192
+ Password:
193
+ ✔ mappings added to hosts file
194
+ ```
195
+
196
+ _Second execution logs_
197
+
198
+ ```console
199
+ > node ./verify_hosts.mjs
200
+
201
+ Check hosts file content...
202
+ ✔ all ip mappings found in hosts file
203
+ ```
204
+
205
+ </details>
206
+
207
+ ## installCertificateAuthority
115
208
 
116
209
  _installCertificateAuthority_ function generates a certificate authority valid for 20 years.
117
210
  This certificate authority is needed to generate local certificates that will be trusted by the operating system and web browsers.
118
211
 
119
212
  ```js
120
- import { installCertificateAuthority } from "@jsenv/https-local"
213
+ import { installCertificateAuthority } from "@jsenv/https-local";
121
214
 
122
- await installCertificateAuthority()
215
+ await installCertificateAuthority();
123
216
  ```
124
217
 
125
218
  By default, trusting authority root certificate is a manual process. This manual process is documented in [BenMorel/dev-certificates#Import the CA in your browser](https://github.com/BenMorel/dev-certificates/tree/c10cd68945da772f31815b7a36721ddf848ff3a3#import-the-ca-in-your-browser). This process can be done programmatically as explained in [Auto trust](#Auto-trust).
@@ -222,16 +315,16 @@ Check if certificate is trusted by firefox...
222
315
 
223
316
  </details>
224
317
 
225
- ## Auto trust
318
+ ### Auto trust
226
319
 
227
320
  It's possible to trust root certificate programmatically using _tryToTrust_
228
321
 
229
322
  ```js
230
- import { installCertificateAuthority } from "@jsenv/https-local"
323
+ import { installCertificateAuthority } from "@jsenv/https-local";
231
324
 
232
325
  await installCertificateAuthority({
233
326
  tryToTrust: true,
234
- })
327
+ });
235
328
  ```
236
329
 
237
330
  <details>
@@ -354,128 +447,3 @@ Check if certificate is trusted by firefox...
354
447
  ```
355
448
 
356
449
  </details>
357
-
358
- # requestCertificate
359
-
360
- _requestCertificate_ function returns a certificate and private key that can be used to start a server in HTTPS.
361
-
362
- ```js
363
- import { createServer } from "node:https"
364
- import { requestCertificate } from "@jsenv/https-local"
365
-
366
- const { certificate, privateKey } = requestCertificate({
367
- altNames: ["localhost", "local.example"],
368
- })
369
- ```
370
-
371
- [installCertificateAuthority](#installCertificateAuthority) must be called before this function.
372
-
373
- # verifyHostsFile
374
-
375
- This function is not mandatory to obtain the https certificates.
376
- But it is useful to programmatically verify ip mappings that are important for your local server are present in hosts file.
377
-
378
- ```js
379
- import { verifyHostsFile } from "@jsenv/https-local"
380
-
381
- await verifyHostsFile({
382
- ipMappings: {
383
- "127.0.0.1": ["localhost", "local.example"],
384
- },
385
- })
386
- ```
387
-
388
- Find below logs written in terminal when this function is executed.
389
-
390
- <details>
391
- <summary>mac and linux</summary>
392
-
393
- ```console
394
- > node ./verify_hosts.mjs
395
-
396
- Check hosts file content...
397
- ⚠ 1 mapping is missing in hosts file
398
- --- hosts file path ---
399
- /etc/hosts
400
- --- line(s) to add ---
401
- 127.0.0.1 localhost local.example
402
- ```
403
-
404
- </details>
405
-
406
- <details>
407
- <summary>windows</summary>
408
-
409
- ```console
410
- > node ./verify_hosts.mjs
411
-
412
- Check hosts file content...
413
- ⚠ 1 mapping is missing in hosts file
414
- --- hosts file path ---
415
- C:\\Windows\\System32\\Drivers\\etc\\hosts
416
- --- line(s) to add ---
417
- 127.0.0.1 localhost local.example
418
- ```
419
-
420
- </details>
421
-
422
- ## Auto update hosts
423
-
424
- It's possible to update hosts file programmatically using _tryToUpdateHostsFile_.
425
-
426
- ```js
427
- import { verifyHostsFile } from "@jsenv/https-local"
428
-
429
- await verifyHostsFile({
430
- ipMappings: {
431
- "127.0.0.1": ["localhost", "local.example"],
432
- },
433
- tryToUpdateHostsFile: true,
434
- })
435
- ```
436
-
437
- <details>
438
- <summary>mac and linux</summary>
439
-
440
- ```console
441
- Check hosts file content...
442
- ℹ 1 mapping is missing in hosts file
443
- Adding 1 mapping(s) in hosts file...
444
- ❯ echo "127.0.0.1 local.example" | sudo tee -a /etc/hosts
445
- Password:
446
- ✔ mappings added to hosts file
447
- ```
448
-
449
- _Second execution logs_
450
-
451
- ```console
452
- > node ./verify_hosts.mjs
453
-
454
- Check hosts file content...
455
- ✔ all ip mappings found in hosts file
456
- ```
457
-
458
- </details>
459
-
460
- <details>
461
- <summary>windows</summary>
462
-
463
- ```console
464
- Check hosts file content...
465
- ℹ 1 mapping is missing in hosts file
466
- Adding 1 mapping(s) in hosts file...
467
- ❯ (echo 127.0.0.1 local.example) >> C:\\Windows\\System32\\Drivers\\etc\\hosts
468
- Password:
469
- ✔ mappings added to hosts file
470
- ```
471
-
472
- _Second execution logs_
473
-
474
- ```console
475
- > node ./verify_hosts.mjs
476
-
477
- Check hosts file content...
478
- ✔ all ip mappings found in hosts file
479
- ```
480
-
481
- </details>
package/package.json CHANGED
@@ -1,22 +1,18 @@
1
1
  {
2
2
  "name": "@jsenv/https-local",
3
- "version": "3.0.7",
3
+ "version": "3.1.1",
4
4
  "description": "A programmatic way to generate locally trusted certificates",
5
5
  "license": "MIT",
6
- "author": {
7
- "name": "dmail",
8
- "email": "dmaillard06@gmail.com",
9
- "url": "https://twitter.com/damienmaillard"
10
- },
11
6
  "repository": {
12
7
  "type": "git",
13
- "url": "https://github.com/jsenv/https-local"
8
+ "url": "https://github.com/jsenv/core",
9
+ "directory": "packages/independent/https-local"
14
10
  },
15
11
  "publishConfig": {
16
12
  "access": "public"
17
13
  },
18
14
  "engines": {
19
- "node": ">=16.13.0"
15
+ "node": ">=20.0.0"
20
16
  },
21
17
  "type": "module",
22
18
  "exports": {
@@ -25,15 +21,14 @@
25
21
  },
26
22
  "./*": "./*"
27
23
  },
24
+ "bin": "./src/https_local_cli.mjs",
28
25
  "main": "./src/main.js",
29
26
  "files": [
30
27
  "/src/"
31
28
  ],
32
29
  "scripts": {
33
- "eslint": "npx eslint . --ext=.js,.mjs,.cjs",
34
30
  "test": "node ./scripts/test/test.mjs",
35
31
  "performance": "node --expose-gc ./scripts/performance/performance.mjs --local --log",
36
- "test:coverage": "npm run test -- --coverage",
37
32
  "test:start-node-server": "node ./scripts/certificate/start_node_server.mjs",
38
33
  "ca:install": "node ./scripts/certificate/install_ca.mjs",
39
34
  "ca:log-trust": "node ./scripts/certificate/log_root_certificate_trust.mjs",
@@ -43,30 +38,15 @@
43
38
  "hosts:add-localhost-mappings": "node ./scripts/hosts/add_localhost_mappings.mjs",
44
39
  "hosts:remove-localhost-mappings": "node ./scripts/hosts/remove_localhost_mappings.mjs",
45
40
  "hosts:verify-localhost-mappings": "node ./scripts/hosts/verify_localhost_mappings.mjs",
46
- "hosts:ensure-localhost-mappings": "node ./scripts/hosts/ensure_localhost_mappings.mjs",
47
- "prettier": "prettier --write .",
48
- "playwright:install": "npx playwright install-deps && npx playwright install"
41
+ "hosts:ensure-localhost-mappings": "node ./scripts/hosts/ensure_localhost_mappings.mjs"
49
42
  },
50
43
  "dependencies": {
51
- "@jsenv/filesystem": "4.1.9",
52
- "@jsenv/log": "3.3.2",
53
- "@jsenv/urls": "1.2.8",
44
+ "@jsenv/filesystem": "4.10.2",
45
+ "@jsenv/humanize": "1.2.8",
46
+ "@jsenv/urls": "2.5.2",
54
47
  "command-exists": "1.2.9",
55
48
  "node-forge": "1.3.1",
56
49
  "sudo-prompt": "9.2.1",
57
- "which": "3.0.0"
58
- },
59
- "devDependencies": {
60
- "@jsenv/assert": "2.8.0",
61
- "@jsenv/core": "30.0.4",
62
- "@jsenv/eslint-config": "16.3.2",
63
- "@jsenv/eslint-import-resolver": "0.4.4",
64
- "@jsenv/github-release-package": "1.5.1",
65
- "@jsenv/package-publish": "1.10.1",
66
- "@jsenv/performance-impact": "4.1.0",
67
- "eslint": "8.30.0",
68
- "eslint-plugin-import": "2.26.0",
69
- "playwright": "1.29.1",
70
- "prettier": "2.8.1"
50
+ "which": "4.0.0"
71
51
  }
72
52
  }