Skip to content

Commit 64b424d

Browse files
committed
Refactor out a convenient streamToBuffer util method
1 parent 3d47a31 commit 64b424d

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

src/interceptors/android/adb-commands.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { reportError } from '../../error-tracking';
55
import { isErrorLike } from '../../util/error';
66
import { delay, waitUntil } from '../../util/promise';
77
import { getCertificateFingerprint, parseCert } from '../../certificates';
8+
import { streamToBuffer } from '../../util/stream';
89

910
export const ANDROID_TEMP = '/data/local/tmp';
1011
export const SYSTEM_CA_PATH = '/system/etc/security/cacerts';
@@ -239,14 +240,7 @@ export async function hasCertInstalled(
239240
const certStream = await adbClient.pull(certPath);
240241

241242
// Wait until it's clear that the read is successful
242-
const data = await new Promise<Buffer>((resolve, reject) => {
243-
const data: Buffer[] = [];
244-
certStream.on('data', (d: Buffer) => data.push(d));
245-
certStream.on('end', () => resolve(Buffer.concat(data)));
246-
247-
certStream.on('error', reject);
248-
});
249-
243+
const data = await streamToBuffer(certStream);
250244

251245
// The device already has an HTTP Toolkit cert. But is it the right one?
252246
const existingCert = parseCert(data.toString('utf8'));

src/interceptors/docker/docker-proxy.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import * as http from 'http';
77
import Dockerode from 'dockerode';
88
import getRawBody from 'raw-body';
99
import { AbortController } from 'node-abort-controller';
10+
import { makeDestroyable, DestroyableServer } from 'destroyable-server';
1011

1112
import { chmod, deleteFile, readDir } from '../../util/fs';
1213
import { rawHeadersToHeaders } from '../../util/http';
13-
import { makeDestroyable, DestroyableServer } from 'destroyable-server';
14+
import { streamToBuffer } from '../../util/stream';
1415
import { reportError } from '../../error-tracking';
1516
import { addShutdownHandler } from '../../shutdown';
1617

@@ -212,12 +213,7 @@ async function createDockerProxy(
212213
dockerRes.pipe(getBuildOutputPipeline(await extraDockerCommandCount!)).pipe(res);
213214
} else if (shouldRemapContainerData) {
214215
// We need to remap container data, to hook all docker-compose behaviour:
215-
const data = await new Promise<Buffer>((resolve, reject) => {
216-
const dataChunks: Buffer[] = [];
217-
dockerRes.on('data', (d) => dataChunks.push(d));
218-
dockerRes.on('end', () => resolve(Buffer.concat(dataChunks)));
219-
dockerRes.on('error', reject);
220-
});
216+
const data = await streamToBuffer(dockerRes);
221217

222218
try {
223219
if (isComposeContainerQuery) {

src/util/stream.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as stream from 'stream';
2+
3+
export function streamToBuffer(input: stream.Readable) {
4+
return new Promise<Buffer>((resolve, reject) => {
5+
const chunks: Uint8Array[] = [];
6+
input.on('data', (d) => chunks.push(d));
7+
input.on('end', () => resolve(Buffer.concat(chunks)));
8+
input.on('error', reject);
9+
});
10+
};

0 commit comments

Comments
 (0)