Skip to content
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

2 changes: 1 addition & 1 deletion eslint.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import globals from 'globals';
import tseslint from 'typescript-eslint';

export default defineConfig([
{ ignores: ['dist/**', 'test/**', 'src/deprecated/**'] },
{ ignores: ['dist/**', 'test/**', 'src/deprecated/**', 'coverage'] },

// JavaScript files (CommonJS) - for worker.js and config files
{
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devrev/ts-adaas",
"version": "1.12.2",
"version": "1.12.3-beta.1",
"description": "Typescript library containing the ADaaS(AirDrop as a Service) control protocol.",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
22 changes: 11 additions & 11 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { EventType } from '../types/extraction';
import { getLibraryVersion } from './helpers';

export const ALLOWED_EXTRACTION_EVENT_TYPES = [
EventType.ExtractionExternalSyncUnitsStart,
EventType.ExtractionMetadataStart,
EventType.ExtractionDataStart,
EventType.ExtractionDataContinue,
EventType.ExtractionDataDelete,
EventType.ExtractionAttachmentsStart,
EventType.ExtractionAttachmentsContinue,
EventType.ExtractionAttachmentsDelete,
EventType.StartExtractingExternalSyncUnits,
EventType.StartExtractingMetadata,
EventType.StartExtractingData,
EventType.ContinueExtractingData,
EventType.StartDeletingExtractorState,
EventType.StartExtractingAttachments,
EventType.ContinueExtractingAttachments,
EventType.StartDeletingExtractorAttachmentsState,
];

export const ALLOWED_LOADING_EVENT_TYPES = [
Expand All @@ -25,9 +25,9 @@ export const ALLOWED_EVENT_TYPES = [
];

export const STATELESS_EXTRACTION_EVENT_TYPES = [
EventType.ExtractionExternalSyncUnitsStart,
EventType.ExtractionDataDelete,
EventType.ExtractionAttachmentsDelete,
EventType.StartExtractingExternalSyncUnits,
EventType.StartDeletingExtractorState,
EventType.StartDeletingExtractorAttachmentsState,
];

export const STATELESS_LOADING_EVENT_TYPES = [
Expand Down
7 changes: 6 additions & 1 deletion src/common/control-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '../types/extraction';
import { LoaderEventType } from '../types/loading';
import { LIBRARY_VERSION } from './constants';
import { translateOutgoingEventType } from './event-type-translation';

export interface EmitInterface {
event: AirdropEvent;
Expand All @@ -21,8 +22,12 @@ export const emit = async ({
eventType,
data,
}: EmitInterface): Promise<AxiosResponse> => {
// Normalize outgoing event type to ensure we always send new event types
// TODO: Remove when the old types are completely phased out
const translatedEventType = translateOutgoingEventType(eventType);

const newEvent: ExtractorEvent | LoaderEvent = {
event_type: eventType,
event_type: translatedEventType,
event_context: event.payload.event_context,
event_data: {
...data,
Expand Down
163 changes: 163 additions & 0 deletions src/common/event-type-translation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { EventType, ExtractorEventType } from '../types/extraction';
import { LoaderEventType } from '../types/loading';

/**
* Maps old incoming event type strings to new EventType enum values.
* This ensures backwards compatibility when the platform sends old event types.
* @param eventTypeString The raw event type string from the platform
* @returns The normalized EventType enum value
*/
export function translateIncomingEventType(eventTypeString: string): EventType {
// Create a reverse mapping from OLD string values to NEW enum member names
const eventTypeMap: Record<string, EventType> = {
// Old extraction event types from platform -> New enum members
[EventType.ExtractionExternalSyncUnitsStart]:
EventType.StartExtractingExternalSyncUnits,
[EventType.ExtractionMetadataStart]: EventType.StartExtractingMetadata,
[EventType.ExtractionDataStart]: EventType.StartExtractingData,
[EventType.ExtractionDataContinue]: EventType.ContinueExtractingData,
[EventType.ExtractionDataDelete]: EventType.StartDeletingExtractorState,
[EventType.ExtractionAttachmentsStart]:
EventType.StartExtractingAttachments,
[EventType.ExtractionAttachmentsContinue]:
EventType.ContinueExtractingAttachments,
[EventType.ExtractionAttachmentsDelete]:
EventType.StartDeletingExtractorAttachmentsState,

// New extraction event types (already correct, map to new enum members)
[EventType.StartExtractingExternalSyncUnits]:
EventType.StartExtractingExternalSyncUnits,
[EventType.StartExtractingMetadata]: EventType.StartExtractingMetadata,
[EventType.StartExtractingData]: EventType.StartExtractingData,
[EventType.ContinueExtractingData]: EventType.ContinueExtractingData,
[EventType.StartDeletingExtractorState]:
EventType.StartDeletingExtractorState,
[EventType.StartExtractingAttachments]:
EventType.StartExtractingAttachments,
[EventType.ContinueExtractingAttachments]:
EventType.ContinueExtractingAttachments,
[EventType.StartDeletingExtractorAttachmentsState]:
EventType.StartDeletingExtractorAttachmentsState,

// Loading events
[EventType.StartLoadingData]: EventType.StartLoadingData,
[EventType.ContinueLoadingData]: EventType.ContinueLoadingData,
[EventType.StartLoadingAttachments]: EventType.StartLoadingAttachments,
[EventType.ContinueLoadingAttachments]:
EventType.ContinueLoadingAttachments,
[EventType.StartDeletingLoaderState]: EventType.StartDeletingLoaderState,
[EventType.StartDeletingLoaderAttachmentState]:
EventType.StartDeletingLoaderAttachmentState,

// Unknown
[EventType.UnknownEventType]: EventType.UnknownEventType,
};

const normalized = eventTypeMap[eventTypeString];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use translated instead normalized all over the code

if (!normalized) {
console.warn(
`Unknown event type received: ${eventTypeString}. This may indicate a new event type or a typo.`
);
// Return the original string cast as EventType as a fallback
return eventTypeString as EventType;
}

return normalized;
}

/**
* Normalizes ExtractorEventType enum values by converting old enum members to new ones.
* Old enum members are deprecated and should be replaced with new ones.
*/
export function translateExtractorEventType(
eventType: ExtractorEventType
): ExtractorEventType {
// Map old enum members to new enum members
const stringValue = eventType as string;

const mapping: Record<string, ExtractorEventType> = {
// Old string values -> New enum members
[ExtractorEventType.ExtractionExternalSyncUnitsDone]:
ExtractorEventType.ExternalSyncUnitExtractionDone,
[ExtractorEventType.ExtractionExternalSyncUnitsError]:
ExtractorEventType.ExternalSyncUnitExtractionError,
[ExtractorEventType.ExtractionMetadataDone]:
ExtractorEventType.MetadataExtractionDone,
[ExtractorEventType.ExtractionMetadataError]:
ExtractorEventType.MetadataExtractionError,
[ExtractorEventType.ExtractionDataProgress]:
ExtractorEventType.DataExtractionProgress,
[ExtractorEventType.ExtractionDataDelay]:
ExtractorEventType.DataExtractionDelayed,
[ExtractorEventType.ExtractionDataDone]:
ExtractorEventType.DataExtractionDone,
[ExtractorEventType.ExtractionDataError]:
ExtractorEventType.DataExtractionError,
[ExtractorEventType.ExtractionDataDeleteDone]:
ExtractorEventType.ExtractorStateDeletionDone,
[ExtractorEventType.ExtractionDataDeleteError]:
ExtractorEventType.ExtractorStateDeletionError,
[ExtractorEventType.ExtractionAttachmentsProgress]:
ExtractorEventType.AttachmentExtractionProgress,
[ExtractorEventType.ExtractionAttachmentsDelay]:
ExtractorEventType.AttachmentExtractionDelayed,
[ExtractorEventType.ExtractionAttachmentsDone]:
ExtractorEventType.AttachmentExtractionDone,
[ExtractorEventType.ExtractionAttachmentsError]:
ExtractorEventType.AttachmentExtractionError,
[ExtractorEventType.ExtractionAttachmentsDeleteDone]:
ExtractorEventType.ExtractorAttachmentsStateDeletionDone,
[ExtractorEventType.ExtractionAttachmentsDeleteError]:
ExtractorEventType.ExtractorAttachmentsStateDeletionError,
};

// If there's a mapping, use it; otherwise return original (already new)
return mapping[stringValue] ?? eventType;
}

/**
* Normalizes LoaderEventType enum values by converting old enum members to new ones.
* Old enum members are deprecated and should be replaced with new ones.
*/
export function translateLoaderEventType(
eventType: LoaderEventType
): LoaderEventType {
// Map old enum members to new enum members
const stringValue = eventType as string;

const mapping: Record<string, LoaderEventType> = {
// Old string values -> New enum members
[LoaderEventType.DataLoadingDelay]: LoaderEventType.DataLoadingDelayed,
[LoaderEventType.AttachmentLoadingProgress]:
LoaderEventType.AttachmentsLoadingProgress,
[LoaderEventType.AttachmentLoadingDelayed]:
LoaderEventType.AttachmentsLoadingDelayed,
[LoaderEventType.AttachmentLoadingDone]:
LoaderEventType.AttachmentsLoadingDone,
[LoaderEventType.AttachmentLoadingError]:
LoaderEventType.AttachmentsLoadingError,
};

// If there's a mapping, use it; otherwise return original (already new)
return mapping[stringValue] ?? eventType;
}

/**
* Normalizes any outgoing event type (Extractor or Loader) to ensure new event types are used.
*/
export function translateOutgoingEventType(
eventType: ExtractorEventType | LoaderEventType
): ExtractorEventType | LoaderEventType {
// Check if it's an ExtractorEventType by checking if the value exists in ExtractorEventType
if (
Object.values(ExtractorEventType).includes(eventType as ExtractorEventType)
) {
return translateExtractorEventType(eventType as ExtractorEventType);
}
// Otherwise treat as LoaderEventType
if (Object.values(LoaderEventType).includes(eventType as LoaderEventType)) {
return translateLoaderEventType(eventType as LoaderEventType);
}
// If neither, return as-is
return eventType;
}
32 changes: 25 additions & 7 deletions src/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,73 @@ export function getTimeoutErrorEventType(eventType: EventType): {
eventType: ExtractorEventType | LoaderEventType;
} {
switch (eventType) {
// Metadata extraction (handles both old and new enum members)
case EventType.StartExtractingMetadata:
case EventType.ExtractionMetadataStart:
return {
eventType: ExtractorEventType.ExtractionMetadataError,
eventType: ExtractorEventType.MetadataExtractionError,
};

// Data extraction (handles both old and new enum members)
case EventType.StartExtractingData:
case EventType.ContinueExtractingData:
case EventType.ExtractionDataStart:
case EventType.ExtractionDataContinue:
return {
eventType: ExtractorEventType.ExtractionDataError,
eventType: ExtractorEventType.DataExtractionError,
};

// Data deletion (handles both old and new enum members)
case EventType.StartDeletingExtractorState:
case EventType.ExtractionDataDelete:
return {
eventType: ExtractorEventType.ExtractionDataDeleteError,
eventType: ExtractorEventType.ExtractorStateDeletionError,
};

// Attachments extraction (handles both old and new enum members)
case EventType.StartExtractingAttachments:
case EventType.ContinueExtractingAttachments:
case EventType.ExtractionAttachmentsStart:
case EventType.ExtractionAttachmentsContinue:
return {
eventType: ExtractorEventType.ExtractionAttachmentsError,
eventType: ExtractorEventType.AttachmentExtractionError,
};

// Attachments deletion (handles both old and new enum members)
case EventType.StartDeletingExtractorAttachmentsState:
case EventType.ExtractionAttachmentsDelete:
return {
eventType: ExtractorEventType.ExtractionAttachmentsDeleteError,
eventType: ExtractorEventType.ExtractorAttachmentsStateDeletionError,
};

// External sync units (handles both old and new enum members)
case EventType.StartExtractingExternalSyncUnits:
case EventType.ExtractionExternalSyncUnitsStart:
return {
eventType: ExtractorEventType.ExtractionExternalSyncUnitsError,
eventType: ExtractorEventType.ExternalSyncUnitExtractionError,
};

// Loading data
case EventType.StartLoadingData:
case EventType.ContinueLoadingData:
return {
eventType: LoaderEventType.DataLoadingError,
};

// Deleting loader state
case EventType.StartDeletingLoaderState:
return {
eventType: LoaderEventType.LoaderStateDeletionError,
};

// Loading attachments
case EventType.StartLoadingAttachments:
case EventType.ContinueLoadingAttachments:
return {
eventType: LoaderEventType.AttachmentLoadingError,
eventType: LoaderEventType.AttachmentsLoadingError,
};

// Deleting loader attachment state
case EventType.StartDeletingLoaderAttachmentState:
return {
eventType: LoaderEventType.LoaderAttachmentStateDeletionError,
Expand Down
3 changes: 3 additions & 0 deletions src/deprecated/adapter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { STATELESS_EVENT_TYPES } from '../../common/constants';
import { getTimeoutExtractorEventType } from '../common/helpers';
// import { Logger } from '../../logger/logger';
import { State, createAdapterState } from '../../state/state';
import { translateIncomingEventType } from '../../common/event-type-translation';

/**
* Adapter class is used to interact with Airdrop platform. The class provides
Expand Down Expand Up @@ -44,6 +45,8 @@ export async function createAdapter<ConnectorState>(
initialState: ConnectorState,
isLocalDevelopment: boolean = false
) {
event.payload.event_type = translateIncomingEventType(event.payload.event_type);

const newInitialState = structuredClone(initialState);
const adapterState: State<ConnectorState> = await createAdapterState({
event,
Expand Down
6 changes: 3 additions & 3 deletions src/state/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe(State.name, () => {

it.each(
STATEFUL_EVENT_TYPES.filter(
(eventType) => eventType !== EventType.ExtractionDataStart
(eventType) => eventType !== EventType.StartExtractingData
)
)(
'should call post state with full adapter state if fetching returns 404 for event type %s',
Expand Down Expand Up @@ -168,13 +168,13 @@ describe(State.name, () => {
}
);

it(EventType.ExtractionDataStart, async () => {
it(EventType.StartExtractingData, async () => {
// Arrange
const initialState = {
test: 'test',
};
const event = createEvent({
eventType: EventType.ExtractionDataStart,
eventType: EventType.StartExtractingData,
contextOverrides: {
snap_in_version_id: '',
},
Expand Down
4 changes: 2 additions & 2 deletions src/state/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export async function createAdapterState<ConnectorState>({
}
}

// Set lastSyncStarted if the event type is ExtractionDataStart
// Set lastSyncStarted if the event type is StartExtractingData
if (
event.payload.event_type === EventType.ExtractionDataStart &&
event.payload.event_type === EventType.StartExtractingData &&
!as.state.lastSyncStarted
) {
as.state.lastSyncStarted = new Date().toISOString();
Expand Down
Loading