Skip to content

Commit 624c321

Browse files
committed
Refactor duplicated promisified fs wrappers into shared utils
1 parent 09a000c commit 624c321

File tree

14 files changed

+55
-87
lines changed

14 files changed

+55
-87
lines changed

src/api-server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { Request, Response } from 'express';
1010
import { HtkConfig } from './config';
1111
import { reportError, addBreadcrumb } from './error-tracking';
1212
import { buildInterceptors, Interceptor } from './interceptors';
13-
import { ALLOWED_ORIGINS, delay } from './util';
13+
import { ALLOWED_ORIGINS } from './constants';
14+
import { delay } from './util';
1415

1516
const packageJson = require('../package.json');
1617

src/browsers.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import * as getBrowserLauncherCb from '@httptoolkit/browser-launcher';
77
import { LaunchOptions, BrowserInstance, Browser } from '@httptoolkit/browser-launcher';
88

99
import { reportError } from './error-tracking';
10+
import { readFile, statFile, deleteFile } from './util';
1011

11-
const statFile = promisify(fs.stat);
12-
const readFile = promisify(fs.readFile);
13-
const deleteFile = promisify(fs.unlink);
1412
const getBrowserLauncher = promisify(getBrowserLauncherCb);
1513

1614
const browserConfigPath = (configPath: string) => path.join(configPath, 'browsers.json');

src/cert-check-server.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { promisify } from 'util';
2-
import * as fs from 'fs';
3-
41
import { getLocal, Mockttp } from 'mockttp';
52

63
import { HttpsPathOptions } from 'mockttp/dist/util/tls';
7-
8-
const readFile = promisify(fs.readFile);
4+
import { readFile } from './util';
95

106
export class CertCheckServer {
117

src/commands/start.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
type ErrorTrackingModule = typeof import('../error-tracking');
33
type IndexTypeModule = typeof import('../index');
44

5-
import { IS_PROD_BUILD } from '../util';
5+
import { IS_PROD_BUILD } from '../constants';
66

77
function maybeBundleImport<T>(moduleName: string): T {
88
if (IS_PROD_BUILD || process.env.OCLIF_TS_NODE === '0') {

src/constants.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const IS_PROD_BUILD = !!process.env.HTTPTOOLKIT_SERVER_BINPATH;
2+
3+
export const ALLOWED_ORIGINS = IS_PROD_BUILD
4+
? [
5+
// Prod builds only allow HTTPS app.httptoolkit.tech usage. This
6+
// ensures that no other sites/apps can communicate with your server
7+
// whilst you have the app open. If they could (requires an HTTP mitm),
8+
// they would be able to start proxies & interceptors.
9+
/^https:\/\/app\.httptoolkit\.tech$/
10+
]
11+
: [
12+
// Dev builds can use the main site, or local sites, even if those
13+
// use HTTP. Note that HTTP here could technically open you to the risk
14+
// above, but it'd require a DNS MitM too (to stop local.httptoolkit.tech
15+
// resolving to localhost and never hitting the network).
16+
/^https?:\/\/localhost(:\d+)?$/,
17+
/^http:\/\/local\.httptoolkit\.tech(:\d+)?$/,
18+
/^https:\/\/app\.httptoolkit\.tech$/
19+
]

src/error-tracking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as path from 'path';
22
import * as child_process from 'child_process';
33
import * as Sentry from '@sentry/node';
44
import { RewriteFrames } from '@sentry/integrations';
5-
import { IS_PROD_BUILD } from './util';
5+
import { IS_PROD_BUILD } from './constants';
66

77
let sentryInitialized = false;
88

src/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ import updateCommand from '@oclif/plugin-update/lib/commands/update';
1111
import { HttpToolkitServerApi } from './api-server';
1212
import { checkBrowserConfig } from './browsers';
1313
import { reportError } from './error-tracking';
14-
import { delay, ALLOWED_ORIGINS } from './util';
14+
import { ALLOWED_ORIGINS } from './constants';
15+
import { delay, readFile, checkAccess, writeFile, ensureDirectoryExists } from './util';
1516
import { registerShutdownHandler } from './shutdown';
1617

17-
const canAccess = util.promisify(fs.access);
18-
const mkDir = util.promisify(fs.mkdir);
19-
const readFile = util.promisify(fs.readFile);
20-
const writeFile = util.promisify(fs.writeFile);
21-
22-
const ensureDirectoryExists = (path: string) =>
23-
canAccess(path).catch(() => mkDir(path, { recursive: true }));
24-
2518
async function generateHTTPSConfig(configPath: string) {
2619
const keyPath = path.join(configPath, 'ca.key');
2720
const certPath = path.join(configPath, 'ca.pem');
@@ -31,7 +24,7 @@ async function generateHTTPSConfig(configPath: string) {
3124
checkCertExpiry(certContent);
3225
return certContent;
3326
}),
34-
canAccess(keyPath, fs.constants.R_OK),
27+
checkAccess(keyPath, fs.constants.R_OK),
3528
]).catch(async () => {
3629
// Cert doesn't exist, or is too close/past expiry. Generate a new one:
3730

src/interceptors/electron.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as _ from 'lodash';
22
import { spawn } from 'child_process';
3-
import * as util from 'util';
43
import * as path from 'path';
5-
import * as fs from 'fs';
64

75
import { getPortPromise as getPort } from 'portfinder';
86
import { generateSPKIFingerprint } from 'mockttp';
@@ -11,13 +9,11 @@ import ChromeRemoteInterface = require('chrome-remote-interface');
119
import { Interceptor } from '.';
1210

1311
import { HtkConfig } from '../config';
14-
import { delay } from '../util';
12+
import { delay, readFile } from '../util';
1513
import { getTerminalEnvVars, OVERRIDES_DIR } from './terminal/terminal-env-overrides';
1614
import { reportError, addBreadcrumb } from '../error-tracking';
1715
import { findExecutableInApp } from '@httptoolkit/osx-find-executable';
1816

19-
const readFile = util.promisify(fs.readFile);
20-
2117
const isAppBundle = (path: string) => {
2218
return process.platform === "darwin" &&
2319
path.endsWith(".app");

src/interceptors/fresh-chrome.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
import { promisify } from 'util';
2-
import * as fs from 'fs';
3-
import * as rimraf from 'rimraf';
4-
51
import * as _ from 'lodash';
62
import { generateSPKIFingerprint } from 'mockttp';
73

84
import { HtkConfig } from '../config';
95

106
import { getAvailableBrowsers, launchBrowser, BrowserInstance, Browser } from '../browsers';
11-
import { delay } from '../util';
7+
import { delay, readFile, deleteFolder } from '../util';
128
import { HideWarningServer } from '../hide-warning-server';
139
import { Interceptor } from '.';
1410
import { reportError } from '../error-tracking';
1511

16-
const readFile = promisify(fs.readFile);
17-
const deleteFolder = promisify(rimraf);
18-
1912
let browsers: _.Dictionary<BrowserInstance> = {};
2013

2114
// Which Chrome variant should we launch, and do we have one available at all?

src/interceptors/fresh-edge.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
import { promisify } from 'util';
2-
import * as fs from 'fs';
3-
import * as rimraf from 'rimraf';
4-
51
import * as _ from 'lodash';
62
import { generateSPKIFingerprint } from 'mockttp';
73

84
import { HtkConfig } from '../config';
95

106
import { getAvailableBrowsers, launchBrowser, BrowserInstance, Browser } from '../browsers';
11-
import { delay } from '../util';
7+
import { delay, deleteFolder, readFile } from '../util';
128
import { HideWarningServer } from '../hide-warning-server';
139
import { Interceptor } from '.';
1410
import { reportError } from '../error-tracking';
1511

16-
const readFile = promisify(fs.readFile);
17-
const deleteFolder = promisify(rimraf);
18-
1912
let browsers: _.Dictionary<BrowserInstance> = {};
2013

2114
// Which Edge variant should we launch, and do we have one available at all?

0 commit comments

Comments
 (0)