@devrev/ts-adaas 1.8.0-beta.0 → 1.9.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/attachments-streaming/attachments-streaming-pool.d.ts +4 -9
- package/dist/attachments-streaming/attachments-streaming-pool.interfaces.d.ts +8 -0
- package/dist/attachments-streaming/attachments-streaming-pool.interfaces.js +2 -0
- package/dist/attachments-streaming/attachments-streaming-pool.js +7 -7
- package/dist/attachments-streaming/attachments-streaming-pool.test.js +4 -4
- package/dist/workers/worker-adapter.js +11 -11
- package/package.json +1 -1
|
@@ -1,17 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ProcessAttachmentReturnType } from '../types';
|
|
2
|
+
import { AttachmentsStreamingPoolParams } from './attachments-streaming-pool.interfaces';
|
|
3
3
|
export declare class AttachmentsStreamingPool<ConnectorState> {
|
|
4
4
|
private adapter;
|
|
5
5
|
private attachments;
|
|
6
6
|
private batchSize;
|
|
7
7
|
private delay;
|
|
8
8
|
private stream;
|
|
9
|
-
constructor({ adapter, attachments, batchSize, stream, }:
|
|
10
|
-
adapter: WorkerAdapter<ConnectorState>;
|
|
11
|
-
attachments: NormalizedAttachment[];
|
|
12
|
-
batchSize?: number;
|
|
13
|
-
stream: ExternalSystemAttachmentStreamingFunction;
|
|
14
|
-
});
|
|
9
|
+
constructor({ adapter, attachments, batchSize, stream, }: AttachmentsStreamingPoolParams<ConnectorState>);
|
|
15
10
|
streamAll(): Promise<ProcessAttachmentReturnType>;
|
|
16
|
-
|
|
11
|
+
startPoolStreaming(): Promise<void>;
|
|
17
12
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NormalizedAttachment, ExternalSystemAttachmentStreamingFunction } from '../types';
|
|
2
|
+
import { WorkerAdapter } from '../workers/worker-adapter';
|
|
3
|
+
export interface AttachmentsStreamingPoolParams<ConnectorState> {
|
|
4
|
+
adapter: WorkerAdapter<ConnectorState>;
|
|
5
|
+
attachments: NormalizedAttachment[];
|
|
6
|
+
batchSize?: number;
|
|
7
|
+
stream: ExternalSystemAttachmentStreamingFunction;
|
|
8
|
+
}
|
|
@@ -10,7 +10,7 @@ class AttachmentsStreamingPool {
|
|
|
10
10
|
this.stream = stream;
|
|
11
11
|
}
|
|
12
12
|
async streamAll() {
|
|
13
|
-
console.log(`Starting download of ${this.attachments.length} attachments
|
|
13
|
+
console.log(`Starting download of ${this.attachments.length} attachments, streaming ${this.batchSize} at once.`);
|
|
14
14
|
if (!this.adapter.state.toDevRev) {
|
|
15
15
|
const error = new Error('toDevRev state is not initialized');
|
|
16
16
|
console.error(error);
|
|
@@ -23,27 +23,27 @@ class AttachmentsStreamingPool {
|
|
|
23
23
|
this.adapter.state.toDevRev.attachmentsMetadata.lastProcessedAttachmentsIdsList =
|
|
24
24
|
[];
|
|
25
25
|
}
|
|
26
|
-
// Start initial batch of
|
|
26
|
+
// Start initial batch of promises up to batchSize limit
|
|
27
27
|
const initialBatchSize = Math.min(this.batchSize, this.attachments.length);
|
|
28
28
|
const initialPromises = [];
|
|
29
29
|
for (let i = 0; i < initialBatchSize; i++) {
|
|
30
|
-
initialPromises.push(this.
|
|
30
|
+
initialPromises.push(this.startPoolStreaming());
|
|
31
31
|
}
|
|
32
|
-
// Wait for all
|
|
32
|
+
// Wait for all promises to complete
|
|
33
33
|
await Promise.all(initialPromises);
|
|
34
34
|
if (this.delay) {
|
|
35
35
|
return { delay: this.delay };
|
|
36
36
|
}
|
|
37
37
|
return {};
|
|
38
38
|
}
|
|
39
|
-
async
|
|
39
|
+
async startPoolStreaming() {
|
|
40
40
|
var _a, _b, _c, _d;
|
|
41
|
-
//
|
|
41
|
+
// Process attachments until the attachments array is empty
|
|
42
42
|
while (this.attachments.length > 0) {
|
|
43
43
|
if (this.delay) {
|
|
44
44
|
break; // Exit if we have a delay
|
|
45
45
|
}
|
|
46
|
-
// Check if we can
|
|
46
|
+
// Check if we can process next attachment
|
|
47
47
|
const attachment = this.attachments.shift();
|
|
48
48
|
if (!attachment) {
|
|
49
49
|
break; // Exit if no more attachments
|
|
@@ -91,8 +91,8 @@ describe('AttachmentsStreamingPool', () => {
|
|
|
91
91
|
attachments: mockAttachments,
|
|
92
92
|
stream: mockStream
|
|
93
93
|
});
|
|
94
|
-
// Mock
|
|
95
|
-
jest.spyOn(pool, '
|
|
94
|
+
// Mock startPoolStreaming to avoid actual processing
|
|
95
|
+
jest.spyOn(pool, 'startPoolStreaming').mockResolvedValue(undefined);
|
|
96
96
|
await pool.streamAll();
|
|
97
97
|
expect(mockAdapter.state.toDevRev.attachmentsMetadata.lastProcessedAttachmentsIdsList).toEqual([]);
|
|
98
98
|
});
|
|
@@ -116,7 +116,7 @@ describe('AttachmentsStreamingPool', () => {
|
|
|
116
116
|
const result = await pool.streamAll();
|
|
117
117
|
expect(result).toEqual({});
|
|
118
118
|
expect(mockAdapter.processAttachment).not.toHaveBeenCalled();
|
|
119
|
-
expect(console.log).toHaveBeenCalledWith('Starting download of 0 attachments
|
|
119
|
+
expect(console.log).toHaveBeenCalledWith('Starting download of 0 attachments, streaming 10 at once.');
|
|
120
120
|
});
|
|
121
121
|
it('should return delay when rate limit is hit', async () => {
|
|
122
122
|
const delayResponse = { delay: 5000 };
|
|
@@ -130,7 +130,7 @@ describe('AttachmentsStreamingPool', () => {
|
|
|
130
130
|
expect(result).toEqual({ delay: 5000 });
|
|
131
131
|
});
|
|
132
132
|
});
|
|
133
|
-
describe('
|
|
133
|
+
describe('startPoolStreaming', () => {
|
|
134
134
|
it('should skip already processed attachments', async () => {
|
|
135
135
|
mockAdapter.state.toDevRev.attachmentsMetadata.lastProcessedAttachmentsIdsList = ['attachment-1'];
|
|
136
136
|
mockAdapter.processAttachment.mockResolvedValue({});
|
|
@@ -598,18 +598,18 @@ class WorkerAdapter {
|
|
|
598
598
|
},
|
|
599
599
|
];
|
|
600
600
|
this.initializeRepos(repos);
|
|
601
|
+
const attachmentsMetadata = (_a = this.state.toDevRev) === null || _a === void 0 ? void 0 : _a.attachmentsMetadata;
|
|
601
602
|
// If there are no attachments metadata artifact IDs in state, finish here
|
|
602
|
-
if (!((_b =
|
|
603
|
-
this.state.toDevRev.attachmentsMetadata.artifactIds.length === 0) {
|
|
603
|
+
if (!((_b = attachmentsMetadata === null || attachmentsMetadata === void 0 ? void 0 : attachmentsMetadata.artifactIds) === null || _b === void 0 ? void 0 : _b.length)) {
|
|
604
604
|
console.log(`No attachments metadata artifact IDs found in state.`);
|
|
605
605
|
return;
|
|
606
606
|
}
|
|
607
607
|
else {
|
|
608
|
-
console.log(`Found ${
|
|
608
|
+
console.log(`Found ${attachmentsMetadata.artifactIds.length} attachments metadata artifact IDs in state.`);
|
|
609
609
|
}
|
|
610
610
|
// Loop through the attachments metadata artifact IDs
|
|
611
|
-
while (
|
|
612
|
-
const attachmentsMetadataArtifactId =
|
|
611
|
+
while (attachmentsMetadata.artifactIds.length > 0) {
|
|
612
|
+
const attachmentsMetadataArtifactId = attachmentsMetadata.artifactIds[0];
|
|
613
613
|
console.log(`Started processing attachments for attachments metadata artifact ID: ${attachmentsMetadataArtifactId}.`);
|
|
614
614
|
const { attachments, error } = await this.uploader.getAttachmentsFromArtifactId({
|
|
615
615
|
artifact: attachmentsMetadataArtifactId,
|
|
@@ -621,8 +621,8 @@ class WorkerAdapter {
|
|
|
621
621
|
if (!attachments || attachments.length === 0) {
|
|
622
622
|
console.warn(`No attachments found for artifact ID: ${attachmentsMetadataArtifactId}.`);
|
|
623
623
|
// Remove empty artifact and reset lastProcessed
|
|
624
|
-
|
|
625
|
-
|
|
624
|
+
attachmentsMetadata.artifactIds.shift();
|
|
625
|
+
attachmentsMetadata.lastProcessed = 0;
|
|
626
626
|
continue;
|
|
627
627
|
}
|
|
628
628
|
console.log(`Found ${attachments.length} attachments for artifact ID: ${attachmentsMetadataArtifactId}.`);
|
|
@@ -656,10 +656,10 @@ class WorkerAdapter {
|
|
|
656
656
|
return response;
|
|
657
657
|
}
|
|
658
658
|
console.log(`Finished processing all attachments for artifact ID: ${attachmentsMetadataArtifactId}.`);
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
if (
|
|
662
|
-
|
|
659
|
+
attachmentsMetadata.artifactIds.shift();
|
|
660
|
+
attachmentsMetadata.lastProcessed = 0;
|
|
661
|
+
if (attachmentsMetadata.lastProcessedAttachmentsIdsList) {
|
|
662
|
+
attachmentsMetadata.lastProcessedAttachmentsIdsList.length = 0;
|
|
663
663
|
}
|
|
664
664
|
}
|
|
665
665
|
return;
|
package/package.json
CHANGED