UNPKG

@arturwojnar/hermes-postgresql

Version:

Production-Ready TypeScript Outbox Pattern for PostgreSQL

36 lines 1.33 kB
const LSN_REGEXP = new RegExp(`^[0-9a-f]+/[0-9a-f]+$`, 'i'); const getUpperAndLowerWAL = (lsn) => { return lsn.split('/').map((x) => parseInt(x, 16)); }; const convertLsnToBigInt = (lsn) => { const [upperWal, lowerWal] = getUpperAndLowerWAL(lsn); return (BigInt(upperWal) << 32n) | BigInt(lowerWal); }; const convertLsnToBuffer = (lsn) => { const buffer = Buffer.alloc(8); const value = convertLsnToBigInt(lsn); buffer.writeBigUInt64BE(value); return buffer; }; const convertBigIntToLsn = (lsn) => { const upperWal = lsn >> 32n; const lowerWal = lsn & 0xffffffffn; return `${upperWal.toString(16).toUpperCase()}/${lowerWal.toString(16).toUpperCase()}`; }; const incrementWAL = (lsn) => { return convertLsnToBigInt(lsn) + BigInt(1); }; const constructLsn = (lsn) => { const upperWal = lsn.readUInt32BE(0).toString(16).toUpperCase(); const lowerWal = lsn.readUInt32BE(4).toString(16).toUpperCase(); return `${upperWal}/${lowerWal}`; }; const isLsn = (value) => LSN_REGEXP.test(value); const toLsn = (value) => { if (isLsn(value)) { return value; } throw new Error(`not LSN ${value}`); }; export { constructLsn, convertBigIntToLsn, convertLsnToBigInt, convertLsnToBuffer, getUpperAndLowerWAL, incrementWAL, isLsn, toLsn, }; //# sourceMappingURL=lsn.js.map