@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.
@@ -1,17 +1,12 @@
1
- import { NormalizedAttachment, ExternalSystemAttachmentStreamingFunction, ProcessAttachmentReturnType } from '../types';
2
- import { WorkerAdapter } from '../workers/worker-adapter';
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
- startPoolWorker(): Promise<void>;
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
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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 with ${this.batchSize} workers.`);
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 workers up to batchSize limit
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.startPoolWorker());
30
+ initialPromises.push(this.startPoolStreaming());
31
31
  }
32
- // Wait for all workers to complete
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 startPoolWorker() {
39
+ async startPoolStreaming() {
40
40
  var _a, _b, _c, _d;
41
- // Worker keeps taking jobs until the attachments array is empty
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 start a new worker
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 startPoolWorker to avoid actual processing
95
- jest.spyOn(pool, 'startPoolWorker').mockResolvedValue(undefined);
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 with 10 workers.');
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('startPoolWorker', () => {
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 = (_a = this.state.toDevRev) === null || _a === void 0 ? void 0 : _a.attachmentsMetadata) === null || _b === void 0 ? void 0 : _b.artifactIds) ||
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 ${this.state.toDevRev.attachmentsMetadata.artifactIds.length} attachments metadata artifact IDs in state.`);
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 (this.state.toDevRev.attachmentsMetadata.artifactIds.length > 0) {
612
- const attachmentsMetadataArtifactId = this.state.toDevRev.attachmentsMetadata.artifactIds[0];
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
- this.state.toDevRev.attachmentsMetadata.artifactIds.shift();
625
- this.state.toDevRev.attachmentsMetadata.lastProcessed = 0;
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
- this.state.toDevRev.attachmentsMetadata.artifactIds.shift();
660
- this.state.toDevRev.attachmentsMetadata.lastProcessed = 0;
661
- if (this.state.toDevRev.attachmentsMetadata.lastProcessedAttachmentsIdsList) {
662
- this.state.toDevRev.attachmentsMetadata.lastProcessedAttachmentsIdsList.length = 0;
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devrev/ts-adaas",
3
- "version": "1.8.0-beta.0",
3
+ "version": "1.9.0-beta.0",
4
4
  "description": "Typescript library containing the ADaaS(AirDrop as a Service) control protocol.",
5
5
  "type": "commonjs",
6
6
  "main": "./dist/index.js",