/
processor-shim.ts
65 lines (58 loc) · 2.25 KB
/
processor-shim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* eslint-disable max-classes-per-file */
import { toString, DataEntity } from '@terascope/utils';
import {
Context,
LegacyProcessor,
SliceRequest,
ProcessorFn,
ValidatedJobConfig
} from '../../interfaces';
import ProcessorCore from '../core/processor-core';
import ConvictSchema from '../convict-schema';
import { ProcessorModule } from '../interfaces';
import { convertResult } from './shim-utils';
export default function processorShim<S extends Record<string, any>>(
legacy: LegacyProcessor
): ProcessorModule {
return {
Processor: class LegacyProcessorShim<T = Record<string, any>> extends ProcessorCore<T> {
private processorFn: ProcessorFn<DataEntity[]>|undefined;
async initialize(): Promise<void> {
this.processorFn = await legacy.newProcessor(
this.context,
this.opConfig,
this.executionConfig
);
}
async handle(input: DataEntity[], sliceRequest: SliceRequest): Promise<DataEntity[]> {
if (this.processorFn != null) {
const result = await this.processorFn(input, this.logger, sliceRequest);
try {
return convertResult(result);
} catch (err) {
throw new Error(`${this.opConfig._op} failed to convert result: ${toString(err)}`);
}
}
throw new Error('Processor has not been initialized');
}
},
Schema: class LegacySchemaShim extends ConvictSchema<S> {
validate(inputConfig: Record<string, any>): any {
const opConfig = super.validate(inputConfig);
if (legacy.selfValidation) {
// @ts-expect-error
legacy.selfValidation(opConfig);
}
return opConfig;
}
validateJob(job: ValidatedJobConfig): void {
if (legacy.crossValidation) {
legacy.crossValidation(job, this.context.sysconfig);
}
}
build(context?: Context): any {
return legacy.schema(context);
}
}
};
}