Merge pull request #21417 from backstage/blam/fix-stream-not-readable

Use `Readable.from` for streams
This commit is contained in:
Ben Lambert
2023-11-21 16:07:53 +01:00
committed by GitHub
4 changed files with 9 additions and 19 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Use `Readable.from` to fix some of the stream issues
@@ -139,7 +139,7 @@ export class GerritUrlReader implements UrlReader {
return Buffer.from(responseBody, 'base64');
},
stream: () => {
const readable = new Readable().wrap(response.body);
const readable = Readable.from(response.body);
return readable.pipe(new Base64Decode());
},
};
@@ -16,7 +16,7 @@
import { ConflictError } from '@backstage/errors';
import getRawBody from 'raw-body';
import { Readable, Stream } from 'stream';
import { Readable } from 'stream';
import { ReadUrlResponseFactory } from './ReadUrlResponseFactory';
describe('ReadUrlResponseFactory', () => {
@@ -74,25 +74,10 @@ describe('ReadUrlResponseFactory', () => {
describe("fromNodeJSReadable's", () => {
const expectedText = 'expected text';
const timeouts: NodeJS.Timeout[] = [];
let readable: NodeJS.ReadableStream;
beforeEach(() => {
readable = new Stream({
encoding: 'utf-8',
}) as unknown as NodeJS.ReadableStream;
readable.readable = true;
// Write data asynchronously, as soon as possible.
timeouts[0] = setTimeout(() => {
timeouts[1] = setTimeout(readable.emit.bind(readable, 'end'), 0);
readable.emit('data', expectedText);
}, 0);
});
afterEach(() => {
// Clear out timeouts so we don't emit data across tests.
timeouts.forEach(clearTimeout);
readable = Readable.from(Buffer.from(expectedText));
});
it('etag is passed through', async () => {
@@ -72,7 +72,7 @@ export class ReadUrlResponseFactory {
oldStyleStream: NodeJS.ReadableStream,
options?: ReadUrlResponseFactoryFromStreamOptions,
): Promise<ReadUrlResponse> {
const readable = new Readable().wrap(oldStyleStream);
const readable = Readable.from(oldStyleStream);
return ReadUrlResponseFactory.fromReadable(readable, options);
}
}