Skip to content

Commit c632bd5

Browse files
Limit batch size for attachments streaming to 50 (#37)
1 parent 097a0c3 commit c632bd5

File tree

4 files changed

+97
-59
lines changed

4 files changed

+97
-59
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devrev/ts-adaas",
3-
"version": "1.4.1",
3+
"version": "1.4.2",
44
"description": "Typescript library containing the ADaaS(AirDrop as a Service) control protocol.",
55
"type": "commonjs",
66
"main": "./dist/index.js",

src/workers/worker-adapter.test.ts

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -173,50 +173,6 @@ describe('WorkerAdapter', () => {
173173
expect(result[0]).toHaveLength(1);
174174
expect(result[1]).toHaveLength(1);
175175
});
176-
177-
it('should handle invalid (0) batch size', async () => {
178-
// Arrange
179-
const mockStream = jest.fn();
180-
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
181-
182-
// Act
183-
const result = await adapter.streamAttachments({
184-
stream: mockStream,
185-
batchSize: 0,
186-
});
187-
188-
// Assert
189-
expect(consoleErrorSpy).toHaveBeenCalled();
190-
expect(result).toEqual({
191-
error: expect.any(Error),
192-
});
193-
expect(result?.error?.message).toContain('Invalid attachments batch size');
194-
195-
// Restore console.error
196-
consoleErrorSpy.mockRestore();
197-
});
198-
199-
it('should handle invalid (negative) batch size', async () => {
200-
// Arrange
201-
const mockStream = jest.fn();
202-
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
203-
204-
// Act
205-
const result = await adapter.streamAttachments({
206-
stream: mockStream,
207-
batchSize: -1,
208-
});
209-
210-
// Assert
211-
expect(consoleErrorSpy).toHaveBeenCalled();
212-
expect(result).toEqual({
213-
error: expect.any(Error),
214-
});
215-
expect(result?.error?.message).toContain('Invalid attachments batch size');
216-
217-
// Restore console.error
218-
consoleErrorSpy.mockRestore();
219-
});
220176
});
221177

222178
describe('defaultAttachmentsIterator', () => {
@@ -480,7 +436,29 @@ describe('WorkerAdapter', () => {
480436
it('should handle invalid batch size', async () => {
481437
// Arrange
482438
const mockStream = jest.fn();
483-
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
439+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
440+
441+
// Set up adapter state with artifact IDs
442+
adapter.state.toDevRev = {
443+
attachmentsMetadata: {
444+
artifactIds: ['artifact1'],
445+
lastProcessed: 0,
446+
lastProcessedAttachmentsIdsList: [],
447+
},
448+
};
449+
450+
// Mock getting attachments
451+
adapter['uploader'].getAttachmentsFromArtifactId = jest.fn().mockResolvedValue({
452+
attachments: [
453+
{ url: 'http://example.com/file1.pdf', id: 'attachment1', file_name: 'file1.pdf', parent_id: 'parent1' },
454+
],
455+
});
456+
457+
// Mock the required methods
458+
adapter.initializeRepos = jest.fn();
459+
const mockReducedAttachments = [['batch1']];
460+
adapter['defaultAttachmentsReducer'] = jest.fn().mockReturnValue(mockReducedAttachments);
461+
adapter['defaultAttachmentsIterator'] = jest.fn().mockResolvedValue({});
484462

485463
// Act
486464
const result = await adapter.streamAttachments({
@@ -489,14 +467,72 @@ describe('WorkerAdapter', () => {
489467
});
490468

491469
// Assert
492-
expect(consoleErrorSpy).toHaveBeenCalled();
493-
expect(result).toEqual({
494-
error: expect.any(Error),
470+
expect(consoleWarnSpy).toHaveBeenCalledWith(
471+
'The specified batch size (0) is invalid. Using 1 instead.'
472+
);
473+
474+
// Verify that the reducer was called with batchSize 50 (not 100)
475+
expect(adapter['defaultAttachmentsReducer']).toHaveBeenCalledWith({
476+
attachments: expect.any(Array),
477+
adapter: adapter,
478+
batchSize: 1,
495479
});
496-
expect(result?.error?.message).toContain('Invalid attachments batch size');
497480

498-
// Restore console.error
499-
consoleErrorSpy.mockRestore();
481+
expect(result).toBeUndefined();
482+
483+
// Restore console.warn
484+
consoleWarnSpy.mockRestore();
485+
});
486+
487+
it('should cap batch size to 50 when batchSize is greater than 50', async () => {
488+
// Arrange
489+
const mockStream = jest.fn();
490+
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
491+
492+
// Set up adapter state with artifact IDs
493+
adapter.state.toDevRev = {
494+
attachmentsMetadata: {
495+
artifactIds: ['artifact1'],
496+
lastProcessed: 0,
497+
lastProcessedAttachmentsIdsList: [],
498+
},
499+
};
500+
501+
// Mock getting attachments
502+
adapter['uploader'].getAttachmentsFromArtifactId = jest.fn().mockResolvedValue({
503+
attachments: [
504+
{ url: 'http://example.com/file1.pdf', id: 'attachment1', file_name: 'file1.pdf', parent_id: 'parent1' },
505+
],
506+
});
507+
508+
// Mock the required methods
509+
adapter.initializeRepos = jest.fn();
510+
const mockReducedAttachments = [['batch1']];
511+
adapter['defaultAttachmentsReducer'] = jest.fn().mockReturnValue(mockReducedAttachments);
512+
adapter['defaultAttachmentsIterator'] = jest.fn().mockResolvedValue({});
513+
514+
// Act
515+
const result = await adapter.streamAttachments({
516+
stream: mockStream,
517+
batchSize: 100, // Set batch size greater than 50
518+
});
519+
520+
// Assert
521+
expect(consoleWarnSpy).toHaveBeenCalledWith(
522+
'The specified batch size (100) is too large. Using 50 instead.'
523+
);
524+
525+
// Verify that the reducer was called with batchSize 50 (not 100)
526+
expect(adapter['defaultAttachmentsReducer']).toHaveBeenCalledWith({
527+
attachments: expect.any(Array),
528+
adapter: adapter,
529+
batchSize: 50, // Should be capped at 50
530+
});
531+
532+
expect(result).toBeUndefined();
533+
534+
// Restore console.warn
535+
consoleWarnSpy.mockRestore();
500536
});
501537

502538
it('should handle empty attachments metadata artifact IDs', async () => {

src/workers/worker-adapter.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -952,11 +952,13 @@ export class WorkerAdapter<ConnectorState> {
952952
batchSize?: number;
953953
}): Promise<StreamAttachmentsReturnType> {
954954
if (batchSize <= 0) {
955-
const error = new Error(
956-
`Invalid attachments batch size: ${batchSize}. Batch size must be greater than 0.`
957-
);
958-
console.error(error.message);
959-
return { error };
955+
console.warn(`The specified batch size (${batchSize}) is invalid. Using 1 instead.`);
956+
batchSize = 1;
957+
}
958+
959+
if (batchSize > 50) {
960+
console.warn(`The specified batch size (${batchSize}) is too large. Using 50 instead.`);
961+
batchSize = 50;
960962
}
961963

962964
const repos = [

0 commit comments

Comments
 (0)