Skip to content

Commit db70c0c

Browse files
committed
feat!: change return type for the container download from blob to ReadableStream
BREAKING CHANGE: buffer property for ContainerDownload is now ReadableStream instead of buffer. response.blob() reads the whole response into ram preventing efficient use of memory to stream the response.
1 parent 0634399 commit db70c0c

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/Client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type FileMakerResponse<T> = {
2323

2424
type ContainerDownload = {
2525
contentType ?: string | null;
26-
buffer : Blob;
26+
buffer : ReadableStream<unknown> | null;
2727
};
2828

2929
export default class Client {
@@ -107,7 +107,7 @@ export default class Client {
107107

108108
return {
109109
contentType: response.headers.get('Content-Type'),
110-
buffer: await response.blob(),
110+
buffer: response.body,
111111
};
112112
}
113113

test/Client.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {Readable} from 'node:stream';
2+
import {text} from 'node:stream/consumers';
13
import fetchMock from '@fetch-mock/jest';
24
import {Client} from '../src';
35
import {FileMakerError} from '../src/Client';
@@ -250,7 +252,18 @@ describe('Client', () => {
250252
});
251253

252254
const response = await client.requestContainer(`https://localhost${containerPath}`);
253-
expect(await response.buffer.text()).toBe('test');
255+
expect(response.buffer).not.toBeNull();
256+
257+
//typescript thinks buffer could still be null if we don't check and throw
258+
if (response.buffer === null) {
259+
throw new Error('streamm is null');
260+
}
261+
262+
const containerReadable = Readable.fromWeb(response.buffer, {
263+
encoding: 'utf-8',
264+
});
265+
const value = await text(containerReadable);
266+
expect(value).toBe('test');
254267
expect(response.contentType).toBe('application/text');
255268
});
256269
});

0 commit comments

Comments
 (0)