|
| 1 | +import crypto = require('crypto'); |
| 2 | +import fs = require('fs'); |
| 3 | + |
| 4 | +export interface ExportStream { |
| 5 | + readonly fd?: number; |
| 6 | + write(value: string): unknown; |
| 7 | +} |
| 8 | + |
| 9 | +export interface Destination { |
| 10 | + close(): void; |
| 11 | + write(value: string): void; |
| 12 | +} |
| 13 | + |
| 14 | +export interface RecordWriter { |
| 15 | + readonly records: number; |
| 16 | + finish(record: Record<string, unknown>): void; |
| 17 | + write(record: Record<string, unknown>): void; |
| 18 | +} |
| 19 | + |
| 20 | +export function nullableString(value: unknown): string | null { |
| 21 | + return typeof value === 'string' ? value : null; |
| 22 | +} |
| 23 | + |
| 24 | +export function isRecord(value: unknown): value is Record<string, unknown> { |
| 25 | + return value !== null && typeof value === 'object' && !Array.isArray(value); |
| 26 | +} |
| 27 | + |
| 28 | +export function compareText(left: string, right: string): number { |
| 29 | + if (left < right) return -1; |
| 30 | + return left > right ? 1 : 0; |
| 31 | +} |
| 32 | + |
| 33 | +export function sameFileSnapshot(before: fs.BigIntStats, after: fs.BigIntStats): boolean { |
| 34 | + return ( |
| 35 | + before.dev === after.dev && |
| 36 | + before.ino === after.ino && |
| 37 | + before.size === after.size && |
| 38 | + before.mtimeNs === after.mtimeNs && |
| 39 | + before.ctimeNs === after.ctimeNs |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function writeAll(fd: number, value: string, label: string): void { |
| 44 | + const bytes = Buffer.from(value); |
| 45 | + let offset = 0; |
| 46 | + while (offset < bytes.length) { |
| 47 | + const written = fs.writeSync(fd, bytes, offset, bytes.length - offset); |
| 48 | + if (!Number.isInteger(written) || written <= 0) { |
| 49 | + throw new Error(`${label} export destination stopped accepting bytes`); |
| 50 | + } |
| 51 | + offset += written; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +function streamDestination(stdout: ExportStream, label: string): Destination { |
| 56 | + if (typeof stdout.fd === 'number' && Number.isInteger(stdout.fd)) { |
| 57 | + const fd = stdout.fd; |
| 58 | + return { close(): void {}, write: (value) => writeAll(fd, value, label) }; |
| 59 | + } |
| 60 | + return { |
| 61 | + close(): void {}, |
| 62 | + write(value): void { |
| 63 | + stdout.write(value); |
| 64 | + }, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +export function createExclusiveDestination( |
| 69 | + outputPath: string | null | undefined, |
| 70 | + stdout: ExportStream, |
| 71 | + label: string |
| 72 | +): Destination { |
| 73 | + if (!outputPath) return streamDestination(stdout, label); |
| 74 | + const flags = |
| 75 | + fs.constants.O_WRONLY | |
| 76 | + fs.constants.O_CREAT | |
| 77 | + fs.constants.O_EXCL | |
| 78 | + (fs.constants.O_NOFOLLOW || 0); |
| 79 | + const fd = fs.openSync(outputPath, flags, 0o600); |
| 80 | + try { |
| 81 | + fs.fchmodSync(fd, 0o600); |
| 82 | + } catch (error) { |
| 83 | + fs.closeSync(fd); |
| 84 | + throw error; |
| 85 | + } |
| 86 | + return { close: () => fs.closeSync(fd), write: (value) => writeAll(fd, value, label) }; |
| 87 | +} |
| 88 | + |
| 89 | +export function createReplacingDestination( |
| 90 | + outputPath: string | null | undefined, |
| 91 | + stdout: ExportStream |
| 92 | +): Destination { |
| 93 | + if (!outputPath) return streamDestination(stdout, 'JSON'); |
| 94 | + const fd = fs.openSync(outputPath, 'w'); |
| 95 | + return { close: () => fs.closeSync(fd), write: (value) => writeAll(fd, value, 'JSON') }; |
| 96 | +} |
| 97 | + |
| 98 | +export function createRecordWriter(destination: Destination): RecordWriter { |
| 99 | + const digest = crypto.createHash('sha256'); |
| 100 | + let records = 0; |
| 101 | + const encode = (record: Record<string, unknown>): string => `${JSON.stringify(record)}\n`; |
| 102 | + return { |
| 103 | + get records(): number { |
| 104 | + return records; |
| 105 | + }, |
| 106 | + write(record): void { |
| 107 | + const line = encode(record); |
| 108 | + digest.update(line); |
| 109 | + destination.write(line); |
| 110 | + records += 1; |
| 111 | + }, |
| 112 | + finish(record): void { |
| 113 | + destination.write( |
| 114 | + encode({ ...record, preceding_records: records, records_sha256: digest.digest('hex') }) |
| 115 | + ); |
| 116 | + }, |
| 117 | + }; |
| 118 | +} |
0 commit comments