@expo/devcert 1.1.2 → 1.2.0

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.
package/src/constants.ts CHANGED
@@ -1,11 +1,23 @@
1
1
  import path from 'path';
2
- import { unlinkSync as rm, writeFileSync as writeFile, readFileSync as readFile } from 'fs';
3
- import { sync as mkdirp } from 'mkdirp';
4
- import { template as makeTemplate } from 'lodash';
5
- import applicationConfigPath = require('application-config-path');
6
- import eol from 'eol';
2
+ import fs from 'fs';
7
3
  import { mktmp } from './utils';
8
4
 
5
+ function applicationConfigPath(name: string): string {
6
+ switch (process.platform) {
7
+ case 'darwin':
8
+ return path.join(process.env.HOME, 'Library', 'Application Support', name);
9
+ case 'win32':
10
+ return process.env.LOCALAPPDATA
11
+ ? path.join(process.env.LOCALAPPDATA, name)
12
+ : path.join(process.env.USERPROFILE, 'Local Settings', 'Application Data', name);
13
+ case 'linux':
14
+ default:
15
+ return process.env.XDG_CONFIG_HOME
16
+ ? path.join(process.env.XDG_CONFIG_HOME, name)
17
+ : path.join(process.env.HOME, '.config', name);
18
+ }
19
+ }
20
+
9
21
  export const VALID_IP = /(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}/;
10
22
  export const VALID_DOMAIN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.?)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9]$/i;
11
23
 
@@ -26,34 +38,35 @@ export const opensslSerialFilePath = configPath('certificate-authority', 'serial
26
38
  export const opensslDatabaseFilePath = configPath('certificate-authority', 'index.txt');
27
39
  export const caSelfSignConfig = path.join(__dirname, '../openssl-configurations/certificate-authority-self-signing.conf');
28
40
 
41
+ function eolAuto(str: string): string {
42
+ return str.replace(/\r?\n|\r/g, isWindows ? '\r\n' : '\n');
43
+ }
44
+
29
45
  export function withDomainSigningRequestConfig(domain: string, cb: (filepath: string) => void) {
30
46
  let tmpFile = mktmp();
31
- let source = readFile(path.join(__dirname, '../openssl-configurations/domain-certificate-signing-requests.conf'), 'utf-8');
32
- let template = makeTemplate(source);
33
- let result = template({ domain });
34
- writeFile(tmpFile, eol.auto(result));
47
+ let source = fs.readFileSync(path.join(__dirname, '../openssl-configurations/domain-certificate-signing-requests.conf'), 'utf-8');
48
+ let result = source.replace(/%DOMAIN%/g, domain);
49
+ fs.writeFileSync(tmpFile, eolAuto(result));
35
50
  cb(tmpFile);
36
- rm(tmpFile);
51
+ fs.rmSync(tmpFile);
37
52
  }
38
53
 
39
54
  export function withDomainCertificateConfig(domain: string, cb: (filepath: string) => void) {
40
55
  let tmpFile = mktmp();
41
- let source = readFile(path.join(__dirname, '../openssl-configurations/domain-certificates.conf'), 'utf-8');
42
- let template = makeTemplate(source);
43
- let result = template({
44
- domain,
45
- serialFile: opensslSerialFilePath,
46
- databaseFile: opensslDatabaseFilePath,
47
- domainDir: pathForDomain(domain)
48
- });
49
- writeFile(tmpFile, eol.auto(result));
56
+ let source = fs.readFileSync(path.join(__dirname, '../openssl-configurations/domain-certificates.conf'), 'utf-8');
57
+ let result = source
58
+ .replace(/%DOMAIN%/g, domain)
59
+ .replace(/%SERIALFILE%/g, opensslSerialFilePath.replace(/\\/g, '\\\\'))
60
+ .replace(/%DATABASEFILE%/g, opensslDatabaseFilePath.replace(/\\/g, '\\\\'))
61
+ .replace(/%DOMAINDIR%/g, pathForDomain(domain).replace(/\\/g, '\\\\'))
62
+ fs.writeFileSync(tmpFile, eolAuto(result));
50
63
  cb(tmpFile);
51
- rm(tmpFile);
64
+ fs.rmSync(tmpFile);
52
65
  }
53
66
 
54
67
  // confTemplate = confTemplate.replace(/DATABASE_PATH/, configPath('index.txt').replace(/\\/g, '\\\\'));
55
68
  // confTemplate = confTemplate.replace(/SERIAL_PATH/, configPath('serial').replace(/\\/g, '\\\\'));
56
- // confTemplate = eol.auto(confTemplate);
69
+ // confTemplate = eolAuto(confTemplate);
57
70
 
58
71
  export const rootCADir = configPath('certificate-authority');
59
72
  export const rootCAKeyPath = configPath('certificate-authority', 'private-key.key');
@@ -73,9 +86,9 @@ export function getLegacyConfigDir(): string {
73
86
  }
74
87
 
75
88
  export function ensureConfigDirs() {
76
- mkdirp(configDir);
77
- mkdirp(domainsDir);
78
- mkdirp(rootCADir);
89
+ fs.mkdirSync(configDir, { recursive: true });
90
+ fs.mkdirSync(domainsDir, { recursive: true });
91
+ fs.mkdirSync(rootCADir, { recursive: true });
79
92
  }
80
93
 
81
94
  ensureConfigDirs();
package/src/index.ts CHANGED
@@ -1,7 +1,5 @@
1
- import { readFileSync as readFile, readdirSync as readdir, existsSync as exists } from 'fs';
1
+ import { rmSync as rm, readFileSync as readFile, readdirSync as readdir, existsSync as exists } from 'fs';
2
2
  import createDebug from 'debug';
3
- import { sync as commandExists } from 'command-exists';
4
- import rimraf from 'rimraf';
5
3
  import {
6
4
  isMac,
7
5
  isLinux,
@@ -14,6 +12,7 @@ import {
14
12
  VALID_IP
15
13
  } from './constants';
16
14
  import currentPlatform from './platforms';
15
+ import { commandExists } from './utils';
17
16
  import installCertificateAuthority, { ensureCACertReadable, uninstall } from './certificate-authority';
18
17
  import generateDomainCertificate from './certificates';
19
18
  import UI, { UserInterface } from './user-interface';
@@ -128,5 +127,5 @@ export function configuredDomains() {
128
127
  }
129
128
 
130
129
  export function removeDomain(domain: string) {
131
- return rimraf.sync(pathForDomain(domain));
130
+ return rm(pathForDomain(domain), { force: true, recursive: true });
132
131
  }
@@ -1,8 +1,7 @@
1
1
  import path from 'path';
2
2
  import { writeFileSync as writeFile, existsSync as exists, readFileSync as read } from 'fs';
3
3
  import createDebug from 'debug';
4
- import { sync as commandExists } from 'command-exists';
5
- import { run, sudoAppend } from '../utils';
4
+ import { run, sudoAppend, commandExists } from '../utils';
6
5
  import { Options } from '../index';
7
6
  import { addCertificateToNSSCertDB, assertNotTouchingFiles, openCertificateInFirefox, closeFirefox, removeCertificateFromNSSCertDB } from './shared';
8
7
  import { Platform } from '.';
@@ -1,9 +1,8 @@
1
1
  import path from 'path';
2
2
  import { existsSync as exists, readFileSync as read, writeFileSync as writeFile } from 'fs';
3
3
  import createDebug from 'debug';
4
- import { sync as commandExists } from 'command-exists';
5
4
  import { addCertificateToNSSCertDB, assertNotTouchingFiles, openCertificateInFirefox, closeFirefox, removeCertificateFromNSSCertDB } from './shared';
6
- import { run, sudoAppend } from '../utils';
5
+ import { run, sudoAppend, commandExists } from '../utils';
7
6
  import { Options } from '../index';
8
7
  import UI from '../user-interface';
9
8
  import { Platform } from '.';
@@ -121,4 +120,4 @@ export default class LinuxPlatform implements Platform {
121
120
  return exists(this.CHROME_BIN_PATH);
122
121
  }
123
122
 
124
- }
123
+ }
@@ -1,8 +1,7 @@
1
1
  import path from 'path';
2
- import url from 'url';
3
2
  import createDebug from 'debug';
4
3
  import assert from 'assert';
5
- import getPort from 'get-port';
4
+ import net from 'net';
6
5
  import http from 'http';
7
6
  import { sync as glob } from 'glob';
8
7
  import { readFileSync as readFile, existsSync as exists } from 'fs';
@@ -109,28 +108,37 @@ async function sleep(ms: number) {
109
108
  */
110
109
  export async function openCertificateInFirefox(firefoxPath: string, certPath: string): Promise<void> {
111
110
  debug('Adding devert to Firefox trust stores manually. Launching a webserver to host our certificate temporarily ...');
112
- let port = await getPort();
113
- let server = http.createServer(async (req, res) => {
114
- let { pathname } = url.parse(req.url);
111
+ let port: number;
112
+ const server = http.createServer(async (req, res) => {
113
+ let { pathname } = new URL(req.url);
115
114
  if (pathname === '/certificate') {
116
115
  res.writeHead(200, { 'Content-type': 'application/x-x509-ca-cert' });
117
116
  res.write(readFile(certPath));
118
117
  res.end();
119
118
  } else {
120
119
  res.writeHead(200);
121
- res.write(await UI.firefoxWizardPromptPage(`http://localhost:${ port }/certificate`));
120
+ res.write(await UI.firefoxWizardPromptPage(`http://localhost:${port}/certificate`));
122
121
  res.end();
123
122
  }
124
- }).listen(port);
125
- debug('Certificate server is up. Printing instructions for user and launching Firefox with hosted certificate URL');
126
- await UI.startFirefoxWizard(`http://localhost:${ port }`);
127
- run(firefoxPath, [`http://localhost:${ port }`]);
128
- await UI.waitForFirefoxWizard();
129
- server.close();
123
+ });
124
+ port = await new Promise((resolve, reject) => {
125
+ server.on('error', reject);
126
+ server.listen(() => {
127
+ resolve((server.address() as net.AddressInfo).port);
128
+ });
129
+ });
130
+ try {
131
+ debug('Certificate server is up. Printing instructions for user and launching Firefox with hosted certificate URL');
132
+ await UI.startFirefoxWizard(`http://localhost:${port}`);
133
+ run(firefoxPath, [`http://localhost:${ port }`]);
134
+ await UI.waitForFirefoxWizard();
135
+ } finally {
136
+ server.close();
137
+ }
130
138
  }
131
139
 
132
140
  export function assertNotTouchingFiles(filepath: string, operation: string): void {
133
141
  if (!filepath.startsWith(configDir) && !filepath.startsWith(getLegacyConfigDir())) {
134
142
  throw new Error(`Devcert cannot ${ operation } ${ filepath }; it is outside known devcert config directories!`);
135
143
  }
136
- }
144
+ }
@@ -1,7 +1,6 @@
1
1
  import createDebug from 'debug';
2
2
  import crypto from 'crypto';
3
- import { writeFileSync as write, readFileSync as read } from 'fs';
4
- import { sync as rimraf } from 'rimraf';
3
+ import { rmSync as rm, writeFileSync as write, readFileSync as read } from 'fs';
5
4
  import { Options } from '../index';
6
5
  import { assertNotTouchingFiles, openCertificateInFirefox } from './shared';
7
6
  import { Platform } from '.';
@@ -64,7 +63,7 @@ export default class WindowsPlatform implements Platform {
64
63
 
65
64
  deleteProtectedFiles(filepath: string) {
66
65
  assertNotTouchingFiles(filepath, 'delete');
67
- rimraf(filepath);
66
+ rm(filepath, { force: true, recursive: true });
68
67
  }
69
68
 
70
69
  async readProtectedFile(filepath: string): Promise<string> {
@@ -104,4 +103,4 @@ export default class WindowsPlatform implements Platform {
104
103
  return decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');
105
104
  }
106
105
 
107
- }
106
+ }
@@ -1,4 +1,4 @@
1
- import passwordPrompt from 'password-prompt';
1
+ import readline from 'node:readline';
2
2
  import { waitForUser } from './utils';
3
3
 
4
4
  export interface UserInterface {
@@ -10,6 +10,25 @@ export interface UserInterface {
10
10
  waitForFirefoxWizard(): Promise<void>;
11
11
  }
12
12
 
13
+ async function passwordPrompt(prompt: string): Promise<string> {
14
+ const input = readline.createInterface({
15
+ input: process.stdin,
16
+ output: process.stdout,
17
+ });
18
+ return new Promise((resolve, reject) => {
19
+ input.on('SIGINT', () => {
20
+ reject(new Error('SIGINT'));
21
+ });
22
+ input.question(prompt, (answer) => {
23
+ readline.moveCursor(process.stdout, 0, -1);
24
+ readline.clearLine(process.stdout, 0);
25
+ input.write(prompt + answer.replace(/./g, '*') + '\n');
26
+ input.close();
27
+ resolve(answer);
28
+ });
29
+ });
30
+ }
31
+
13
32
  const DefaultUI: UserInterface = {
14
33
  async getWindowsEncryptionPassword() {
15
34
  return await passwordPrompt('devcert password (http://bit.ly/devcert-what-password?):');
@@ -68,4 +87,4 @@ const DefaultUI: UserInterface = {
68
87
  }
69
88
  }
70
89
 
71
- export default DefaultUI;
90
+ export default DefaultUI;
package/src/utils.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  import { execFileSync, ExecFileSyncOptions } from 'child_process';
2
- import tmp from 'tmp';
2
+ import { randomBytes } from 'crypto';
3
+ import fs from 'fs';
4
+ import os from 'os';
3
5
  import createDebug from 'debug';
4
6
  import path from 'path';
5
- import sudoPrompt from 'sudo-prompt';
7
+ import sudoPrompt from '@expo/sudo-prompt';
6
8
 
7
- import { configPath } from './constants';
9
+ import { configPath, isWindows } from './constants';
8
10
 
9
11
  const debug = createDebug('devcert:util');
10
12
 
@@ -40,9 +42,10 @@ export function reportableError(message: string) {
40
42
  }
41
43
 
42
44
  export function mktmp() {
43
- // discardDescriptor because windows complains the file is in use if we create a tmp file
44
- // and then shell out to a process that tries to use it
45
- return tmp.fileSync({ discardDescriptor: true }).name;
45
+ const random = randomBytes(6).toString('hex');
46
+ const tmppath = path.join(os.tmpdir(), `tmp-${process.pid}${random}`);
47
+ fs.closeSync(fs.openSync(tmppath, 'w'));
48
+ return tmppath;
46
49
  }
47
50
 
48
51
  export function sudo(cmd: string): Promise<string | null> {
@@ -53,3 +56,23 @@ export function sudo(cmd: string): Promise<string | null> {
53
56
  });
54
57
  });
55
58
  }
59
+
60
+ const _commands: Record<string, string | null> = {};
61
+
62
+ export function commandExists(command: string): string | null {
63
+ if (_commands[command] !== undefined) {
64
+ return _commands[command];
65
+ }
66
+ const paths = process.env[isWindows ? 'Path' : 'PATH'].split(path.delimiter);
67
+ const extensions = [...(process.env.PATHEXT || '').split(path.delimiter), ''];
68
+ for (const dir of paths) {
69
+ for (const extension of extensions) {
70
+ const filePath = path.join(dir, command + extension);
71
+ try {
72
+ fs.accessSync(filePath, fs.constants.X_OK);
73
+ return (_commands[command] = filePath);
74
+ } catch {}
75
+ }
76
+ }
77
+ return (_commands[command] = null);
78
+ }
package/tsconfig.json CHANGED
@@ -4,10 +4,10 @@
4
4
  "declaration": true,
5
5
  "module": "commonjs",
6
6
  "moduleResolution": "node",
7
- "target": "ES2016",
7
+ "target": "ES2018",
8
8
  "noImplicitAny": true,
9
9
  "sourceMap": false,
10
- "importHelpers": true,
10
+ "importHelpers": false,
11
11
  "inlineSourceMap": true,
12
12
  "inlineSources": true,
13
13
  "outDir": "dist",
package/src/types.d.ts DELETED
@@ -1,7 +0,0 @@
1
- declare module "command-exists";
2
- declare module "eol";
3
- declare module "sudo-prompt";
4
- declare module "password-prompt";
5
- declare module "application-config-path" {
6
- export = (appName: string) => string;
7
- }