|
1 | 1 | import fs from 'node:fs'; |
2 | 2 | import path from 'node:path'; |
3 | | -import type { Decoder, Encoder } from './sink-source.type'; |
| 3 | +import type { Decoder, Encoder, RecoverResult } from './sink-source.type'; |
4 | 4 |
|
5 | 5 | export type AppendOptions = { |
6 | 6 | filePath: string; |
@@ -162,3 +162,60 @@ export const StringCodec = { |
162 | 162 | encode: (v: string) => v, |
163 | 163 | decode: (v: string) => v, |
164 | 164 | }; |
| 165 | + |
| 166 | +export abstract class RecoverableEventSink< |
| 167 | + Raw extends Record<string, unknown>, |
| 168 | + Domain, |
| 169 | +> { |
| 170 | + protected readonly sink: JsonlFile<Raw>; |
| 171 | + private finalized = false; |
| 172 | + |
| 173 | + constructor(sink: JsonlFile<Raw>) { |
| 174 | + this.sink = sink; |
| 175 | + } |
| 176 | + |
| 177 | + open() { |
| 178 | + this.sink.open(); |
| 179 | + } |
| 180 | + |
| 181 | + write(event: Domain) { |
| 182 | + this.sink.write(this.encode(event)); |
| 183 | + } |
| 184 | + |
| 185 | + close() { |
| 186 | + this.finalize(); |
| 187 | + } |
| 188 | + |
| 189 | + recover(): RecoverResult<Domain> { |
| 190 | + const { records, errors, partialTail } = this.sink.recover(); |
| 191 | + const out: Domain[] = []; |
| 192 | + const errs = [...errors]; |
| 193 | + |
| 194 | + records.forEach((r, i) => { |
| 195 | + try { |
| 196 | + out.push(this.decode(r)); |
| 197 | + } catch (error) { |
| 198 | + errs.push({ |
| 199 | + lineNo: i + 1, |
| 200 | + line: JSON.stringify(r), |
| 201 | + error: error as Error, |
| 202 | + }); |
| 203 | + } |
| 204 | + }); |
| 205 | + |
| 206 | + return { records: out, errors: errs, partialTail }; |
| 207 | + } |
| 208 | + |
| 209 | + finalize() { |
| 210 | + if (this.finalized) { |
| 211 | + return; |
| 212 | + } |
| 213 | + this.finalized = true; |
| 214 | + this.sink.close(); |
| 215 | + this.onFinalize(); |
| 216 | + } |
| 217 | + |
| 218 | + protected abstract encode(domain: Domain): Raw; |
| 219 | + protected abstract decode(raw: Raw): Domain; |
| 220 | + protected abstract onFinalize(): void; |
| 221 | +} |
0 commit comments