forked from stripe/sync-engine
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathengine.ts
More file actions
297 lines (276 loc) · 9.01 KB
/
engine.ts
File metadata and controls
297 lines (276 loc) · 9.01 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { z } from 'zod'
import {
DestinationOutput,
Message,
PipelineConfig,
Stream,
ConfiguredStream,
ConfiguredCatalog,
CheckResult,
} from '@stripe/sync-protocol'
import type { Destination, Source } from '@stripe/sync-protocol'
import { enforceCatalog, filterType, log, persistState, pipe } from './pipeline.js'
import { applySelection } from './destination-filter.js'
import type { StateStore } from './state-store.js'
import type { ConnectorResolver } from './resolver.js'
import { logger } from '../logger.js'
// MARK: - Engine interface
export interface SetupResult {
source?: Record<string, unknown>
destination?: Record<string, unknown>
}
export interface Engine {
setup(): Promise<SetupResult>
teardown(): Promise<void>
check(): Promise<{ source: CheckResult; destination: CheckResult }>
read(input?: AsyncIterable<unknown>): AsyncIterable<Message>
write(messages: AsyncIterable<Message>): AsyncIterable<DestinationOutput>
sync(input?: AsyncIterable<unknown>): AsyncIterable<DestinationOutput>
}
function engineLogContext(config: PipelineConfig): Record<string, unknown> {
return {
sourceName: config.source.type,
destinationName: config.destination.type,
configuredStreamCount: config.streams?.length ?? 0,
configuredStreams: config.streams?.map((stream) => stream.name) ?? [],
}
}
async function withLoggedStep<T>(
label: string,
context: Record<string, unknown>,
fn: () => Promise<T>
): Promise<T> {
const startedAt = Date.now()
logger.info(context, `${label} started`)
try {
const result = await fn()
logger.info({ ...context, durationMs: Date.now() - startedAt }, `${label} completed`)
return result
} catch (error) {
logger.error({ ...context, durationMs: Date.now() - startedAt, err: error }, `${label} failed`)
throw error
}
}
async function* withLoggedStream<T>(
label: string,
context: Record<string, unknown>,
iter: AsyncIterable<T>
): AsyncIterable<T> {
const startedAt = Date.now()
let itemCount = 0
logger.info(context, `${label} started`)
try {
for await (const item of iter) {
itemCount++
yield item
}
logger.info({ ...context, itemCount, durationMs: Date.now() - startedAt }, `${label} completed`)
} catch (error) {
logger.error(
{ ...context, itemCount, durationMs: Date.now() - startedAt, err: error },
`${label} failed`
)
throw error
}
}
/**
* Build a ConfiguredCatalog from discovered streams, optionally filtered
* by the streams listed in config.
*/
export function buildCatalog(
discovered: Stream[],
configStreams?: PipelineConfig['streams']
): ConfiguredCatalog {
let streams: ConfiguredStream[]
if (configStreams && configStreams.length > 0) {
const wanted = new Map(configStreams.map((s) => [s.name, s]))
streams = discovered
.filter((s) => wanted.has(s.name))
.map((s) => {
const cfg = wanted.get(s.name)!
return {
stream: s,
sync_mode: cfg.sync_mode ?? 'full_refresh',
destination_sync_mode: 'append' as const,
fields: cfg.fields,
backfill_limit: cfg.backfill_limit,
}
})
} else {
streams = discovered.map((s) => ({
stream: s,
sync_mode: 'full_refresh' as const,
destination_sync_mode: 'append' as const,
}))
}
return { streams }
}
// MARK: - Factory
export function createEngine(
config: PipelineConfig,
connectors: { source: Source; destination: Destination },
stateStore: StateStore
): Engine {
// Validate configs using connector JSON Schemas (fail-fast)
const sourceSpec = connectors.source.spec()
const destSpec = connectors.destination.spec()
const { type: _sn, ...rawSourceConfig } = config.source
const { type: _dn, ...rawDestConfig } = config.destination
const sourceConfig = z.fromJSONSchema(sourceSpec.config).parse(rawSourceConfig) as Record<
string,
unknown
>
const destConfig = z.fromJSONSchema(destSpec.config).parse(rawDestConfig) as Record<
string,
unknown
>
const baseContext = engineLogContext(config)
// Lazy-cached catalog — discover is called at most once per engine instance.
let _catalog: ConfiguredCatalog | null = null
async function getCatalog(): Promise<ConfiguredCatalog> {
if (!_catalog) {
const startedAt = Date.now()
logger.info(baseContext, 'Engine source discover started')
try {
const msg = await connectors.source.discover({ config: sourceConfig })
_catalog = buildCatalog(msg.streams, config.streams)
logger.info(
{
...baseContext,
durationMs: Date.now() - startedAt,
discoveredStreamCount: msg.streams.length,
catalogStreamCount: _catalog.streams.length,
catalogStreams: _catalog.streams.map((stream) => stream.stream.name),
},
'Engine source discover completed'
)
} catch (error) {
logger.error(
{ ...baseContext, durationMs: Date.now() - startedAt, err: error },
'Engine source discover failed'
)
throw error
}
}
return _catalog
}
async function setupSource(catalog: ConfiguredCatalog): Promise<void> {
if (connectors.source.setup) {
await withLoggedStep('Engine source setup', baseContext, () =>
connectors.source.setup!({ config: sourceConfig, catalog })
)
}
}
async function setupDestination(
catalog: ConfiguredCatalog
): Promise<Record<string, unknown> | undefined> {
if (connectors.destination.setup) {
const result = await withLoggedStep('Engine destination setup', baseContext, () =>
connectors.destination.setup!({ config: destConfig, catalog })
)
return result ?? undefined
}
}
return {
async setup() {
const catalog = await getCatalog()
const filteredCatalog = applySelection(catalog)
const [sourceUpdates, destUpdates] = await Promise.all([
connectors.source.setup
? withLoggedStep('Engine source setup', baseContext, () =>
connectors.source.setup!({ config: sourceConfig, catalog })
)
: Promise.resolve(undefined),
connectors.destination.setup
? withLoggedStep('Engine destination setup', baseContext, () =>
connectors.destination.setup!({ config: destConfig, catalog: filteredCatalog })
)
: Promise.resolve(undefined),
])
const result: SetupResult = {}
if (sourceUpdates) {
Object.assign(sourceConfig, sourceUpdates)
result.source = sourceUpdates
}
if (destUpdates) {
Object.assign(destConfig, destUpdates)
result.destination = destUpdates
}
return result
},
async teardown() {
await Promise.all([
connectors.source.teardown?.({ config: sourceConfig }),
connectors.destination.teardown?.({ config: destConfig }),
])
},
async check() {
const [source, destination] = await Promise.all([
connectors.source.check({ config: sourceConfig }),
connectors.destination.check({ config: destConfig }),
])
return { source, destination }
},
async *read(input?: AsyncIterable<unknown>) {
const state = await stateStore.get()
const raw = connectors.source.read(
{ config: sourceConfig, catalog: await getCatalog(), state },
input
)
for await (const msg of withLoggedStream(
'Engine source read',
{
...baseContext,
inputProvided: input !== undefined,
stateProvided: state !== undefined,
},
raw
)) {
yield Message.parse(msg)
}
},
async *write(messages: AsyncIterable<Message>) {
const catalog = await getCatalog()
const filteredCatalog = applySelection(catalog)
const destInput = pipe(
messages,
enforceCatalog(filteredCatalog),
log,
filterType('record', 'state')
)
const destOutput = connectors.destination.write(
{ config: destConfig, catalog: filteredCatalog },
destInput
)
for await (const msg of withLoggedStream(
'Engine destination write',
baseContext,
destOutput
)) {
yield DestinationOutput.parse(msg)
}
},
async *sync(input?: AsyncIterable<unknown>) {
if (connectors.destination.skipAutoSetup) {
// Destination manages its own setup externally (e.g. via /setup endpoint).
// Only set up the source here.
const catalog = await getCatalog()
await setupSource(catalog)
} else {
await this.setup()
}
yield* pipe(this.read(input), this.write, persistState(stateStore))
},
}
}
export async function createEngineFromParams(
params: PipelineConfig,
resolver: ConnectorResolver,
stateStore: StateStore
): Promise<Engine> {
const [source, destination] = await Promise.all([
resolver.resolveSource(params.source.type),
resolver.resolveDestination(params.destination.type),
])
return createEngine(params, { source, destination }, stateStore)
}