Skip to content

Commit d393f57

Browse files
committed
Add replace fs and fs-extra with fs/promises
1 parent d1eb9c1 commit d393f57

File tree

10 files changed

+22
-38
lines changed

10 files changed

+22
-38
lines changed

karma.conf.js

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ module.exports = function(config) {
2828
"http-proxy-agent": require.resolve('./test/empty-stub.js'),
2929
"https-proxy-agent": require.resolve('./test/empty-stub.js'),
3030
"request-promise-native": require.resolve('./test/empty-stub.js'),
31-
"fs-extra": require.resolve('./test/empty-stub.js'),
3231
"portfinder": require.resolve('./test/empty-stub.js'),
3332
"dns2": require.resolve('./test/empty-stub.js'),
3433
"ws": require.resolve('./test/empty-stub.js')

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
"@types/connect": "3.4.35",
104104
"@types/dns2": "1.4.0",
105105
"@types/express": "4.17.13",
106-
"@types/fs-extra": "8.1.0",
107106
"@types/graphql": "14.5.0",
108107
"@types/lodash": "4.14.178",
109108
"@types/mocha": "8.2.3",
@@ -127,7 +126,6 @@
127126
"dns2": "1.4.2",
128127
"form-data-encoder": "^1.7.2",
129128
"formdata-node": "^4.3.2",
130-
"fs-extra": "^8.1.0",
131129
"http-proxy-agent": "^5.0.0",
132130
"karma": "^6.3.2",
133131
"karma-chai": "^0.1.0",

src/rules/requests/request-handlers.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import net = require('net');
55
import tls = require('tls');
66
import http = require('http');
77
import https = require('https');
8+
import * as fs from 'fs/promises';
89
import * as h2Client from 'http2-wrapper';
910
import CacheableLookup from 'cacheable-lookup';
1011
import { decode as decodeBase64 } from 'base64-arraybuffer';
@@ -20,7 +21,6 @@ import {
2021
} from "../../types";
2122

2223
import { MaybePromise } from '../../util/type-utils';
23-
import { readFile } from '../../util/fs';
2424
import {
2525
waitForCompletedRequest,
2626
buildBodyReader,
@@ -306,7 +306,7 @@ export class StreamHandler extends StreamHandlerDefinition {
306306
export class FileHandler extends FileHandlerDefinition {
307307
async handle(_request: OngoingRequest, response: OngoingResponse) {
308308
// Read the file first, to ensure we error cleanly if it's unavailable
309-
const fileContents = await readFile(this.filePath, null);
309+
const fileContents = await fs.readFile(this.filePath);
310310

311311
if (this.headers) dropDefaultHeaders(response);
312312

@@ -369,7 +369,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
369369
if ('cert' in certObject) {
370370
return certObject.cert.toString('utf8');
371371
} else {
372-
return readFile(certObject.certPath, 'utf8');
372+
return fs.readFile(certObject.certPath, 'utf8');
373373
}
374374
}))
375375
);
@@ -505,7 +505,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
505505
// this can result in sending a request much more quickly!
506506
reqBodyOverride = asBuffer(replaceBody);
507507
} else if (replaceBodyFromFile) {
508-
reqBodyOverride = await readFile(replaceBodyFromFile, null);
508+
reqBodyOverride = await fs.readFile(replaceBodyFromFile);
509509
} else if (updateJsonBody) {
510510
const { body: realBody } = await waitForCompletedRequest(clientReq);
511511
if (await realBody.getJson() === undefined) {
@@ -783,7 +783,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
783783
// this can result in sending a request much more quickly!
784784
resBodyOverride = asBuffer(replaceBody);
785785
} else if (replaceBodyFromFile) {
786-
resBodyOverride = await readFile(replaceBodyFromFile, null);
786+
resBodyOverride = await fs.readFile(replaceBodyFromFile);
787787
} else if (updateJsonBody) {
788788
const rawBody = await streamToBuffer(serverRes);
789789
const realBody = buildBodyReader(rawBody, serverRes.headers);

src/rules/websockets/websocket-handlers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import net = require('net');
33
import * as url from 'url';
44
import * as tls from 'tls';
55
import * as http from 'http';
6+
import * as fs from 'fs/promises';
67
import * as WebSocket from 'ws';
78
import CacheableLookup from 'cacheable-lookup';
89

@@ -30,7 +31,6 @@ import {
3031
import { streamToBuffer } from '../../util/buffer-utils';
3132
import { isLocalhostAddress } from '../../util/socket-util';
3233
import { MaybePromise } from '../../util/type-utils';
33-
import { readFile } from '../../util/fs';
3434

3535
import { getAgent } from '../http-agents';
3636
import { ProxySettingSource } from '../proxy-config';
@@ -200,7 +200,7 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
200200
if ('cert' in certObject) {
201201
return certObject.cert.toString('utf8');
202202
} else {
203-
return readFile(certObject.certPath, 'utf8');
203+
return fs.readFile(certObject.certPath, 'utf8');
204204
}
205205
}))
206206
);

src/util/fs.ts

-12
This file was deleted.

src/util/tls.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as _ from 'lodash';
2+
import * as fs from 'fs/promises';
23
import { v4 as uuid } from "uuid";
34
import * as forge from 'node-forge';
45

56
const { pki, md, util: { encode64 } } = forge;
67

7-
import * as fs from './fs';
8-
98
export type CAOptions = (CertDataOptions | CertPathOptions);
109

1110
export interface CertDataOptions extends BaseCAOptions {

test/ca.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as https from 'https';
22
import * as path from 'path';
3+
import * as fs from 'fs/promises';
34
import * as forge from 'node-forge';
45

56
import { expect, fetch, ignoreNetworkError, nodeOnly } from "./test-utils";
6-
import * as fs from '../src/util/fs';
77

88
import { CA, generateCACertificate } from '../src/util/tls';
99

test/integration/https.spec.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as fs from 'fs-extra';
21
import * as http from 'http';
32
import * as tls from 'tls';
43
import * as https from 'https';
4+
import * as fs from 'fs/promises';
55

66
import { getLocal } from "../..";
77
import {
@@ -71,9 +71,9 @@ describe("When configured for HTTPS", () => {
7171

7272
it("should use the default domain when no SNI is provided", async () => {
7373
const tlsSocket = tls.connect({
74-
ca: fs.readFileSync('./test/fixtures/test-ca.pem'),
75-
key: fs.readFileSync('./test/fixtures/test-ca.key'),
76-
cert: fs.readFileSync('./test/fixtures/test-ca.pem'),
74+
ca: await fs.readFile('./test/fixtures/test-ca.pem'),
75+
key: await fs.readFile('./test/fixtures/test-ca.key'),
76+
cert: await fs.readFile('./test/fixtures/test-ca.pem'),
7777

7878
host: 'localhost',
7979
port: server.port,
@@ -89,9 +89,9 @@ describe("When configured for HTTPS", () => {
8989

9090
it("should still use the SNI name if one is provided", async () => {
9191
const tlsSocket = tls.connect({
92-
ca: fs.readFileSync('./test/fixtures/test-ca.pem'),
93-
key: fs.readFileSync('./test/fixtures/test-ca.key'),
94-
cert: fs.readFileSync('./test/fixtures/test-ca.pem'),
92+
ca: await fs.readFile('./test/fixtures/test-ca.pem'),
93+
key: await fs.readFile('./test/fixtures/test-ca.key'),
94+
cert: await fs.readFile('./test/fixtures/test-ca.pem'),
9595

9696
host: 'localhost',
9797
servername: 'sni-name.example', // <-- Set a name via SNI

test/integration/proxying/https-proxying.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ = require("lodash");
2-
import * as fs from 'fs-extra';
32
import * as https from 'https';
43
import * as http2 from 'http2';
4+
import * as fs from 'fs/promises';
55
import * as semver from 'semver';
66
import portfinder = require('portfinder');
77
import request = require("request-promise-native");
@@ -107,12 +107,12 @@ nodeOnly(() => {
107107
]);
108108
});
109109

110-
describe("given an untrusted upstream certificate", () => {
110+
describe("given an untrusted upstream certificate", async () => {
111111

112112
let badServer: Mockttp;
113113

114114
const certPath = './test/fixtures/untrusted-ca.pem';
115-
const cert = fs.readFileSync(certPath);
115+
const cert = await fs.readFile(certPath);
116116

117117
beforeEach(async () => {
118118
badServer = getLocal({
@@ -384,8 +384,8 @@ nodeOnly(() => {
384384
beforeEach(async () => {
385385
http2Server = makeDestroyable(http2.createSecureServer({
386386
allowHTTP1: false,
387-
key: fs.readFileSync('./test/fixtures/test-ca.key'),
388-
cert: fs.readFileSync('./test/fixtures/test-ca.pem')
387+
key: await fs.readFile('./test/fixtures/test-ca.key'),
388+
cert: await fs.readFile('./test/fixtures/test-ca.pem')
389389
}, async (req, res) => {
390390
res.writeHead(200, {
391391
"received-url": req.url,

test/integration/proxying/upstream-proxying.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ = require("lodash");
2-
import * as fs from 'fs-extra';
2+
import * as fs from 'fs/promises';
33
import request = require("request-promise-native");
44

55
import { getLocal, Mockttp, MockedEndpoint } from "../../..";

0 commit comments

Comments
 (0)