import { getFlowReadResourceProfile } from "./traffic-flow-read-resource-profile.js" import type { QueryResultRow } from "pg" import { trackFlowReadSource } from "./traffic-flow-read-budget.js" export interface FlowBoundedQueryClient { query(sql: string, values: unknown[]): Promise<{ rows: Array>; fields?:Array<{name:string}> }> } export interface FlowDecodedSourceLimits { maxRows: number; maxEncodedBytes: number } /** Scalar preflight reserves cumulative request capacity before any data decode. * The following gated data statement enforces that same reservation on its own * snapshot, so concurrent source growth cannot bypass preflight admission. * Source is evaluated only on * PostgreSQL. An oversized source yields only scalar metadata, no data payload. * Query must be trusted SELECT text using PostgreSQL $n placeholders. * maxEncodedBytes bounds source fields using their JSON representation; V8 amplification still needs a * separately reserved/measured working set. No silent LIMIT or result truncation. */ export async function readBoundedFlowSql(client: FlowBoundedQueryClient, query: string, values: unknown[], limits: FlowDecodedSourceLimits): Promise { if (!Number.isSafeInteger(limits.maxRows) || limits.maxRows < 1 || !Number.isSafeInteger(limits.maxEncodedBytes) || limits.maxEncodedBytes < 1) throw Error("Invalid NetFlow SQL source limit") const profile=getFlowReadResourceProfile() const sourceByteLimit=profile.name==="standard"?limits.maxEncodedBytes:Math.min(limits.maxEncodedBytes,profile.querySourceBytes) return trackFlowReadSource(async admit=>{ const preflight=await client.query(`WITH flow_read_source AS MATERIALIZED (${query}) SELECT count(*)::text AS "__flowRowCount",COALESCE(sum(octet_length(row_to_json(s)::text)),0)::text AS "__flowEncodedBytes" FROM flow_read_source s`,values) const n=String(preflight.rows[0]?.__flowRowCount),b=String(preflight.rows[0]?.__flowEncodedBytes) const measuredBytes=Number(b)+2+Math.max(Number(n)-1,0) if(!/^\d{1,15}$/.test(n)||!/^\d{1,15}$/.test(b)||Number(n)>limits.maxRows||measuredBytes>sourceByteLimit)throw Object.assign(Error("Источник NetFlow превышает допустимый объём; сократите область или окно"),{statusCode:409,code:"STATE_UNAVAILABLE"}) const reserved=admit(Number(n),measuredBytes) const countIndex = values.length + 1, bytesIndex = values.length + 2 const result = await client.query(`WITH flow_read_source AS MATERIALIZED (${query}), flow_read_size AS MATERIALIZED (SELECT count(*) AS n, COALESCE(sum(octet_length(row_to_json(s)::text)),0) AS bytes FROM flow_read_source s) SELECT size.n::text AS "__flowRowCount", size.bytes::text AS "__flowEncodedBytes", admitted.* FROM flow_read_size size LEFT JOIN LATERAL (SELECT TRUE AS "__flowPresent", s.* FROM flow_read_source s WHERE size.n <= $${countIndex} AND size.bytes + 2 + GREATEST(size.n-1,0) <= $${bytesIndex}) admitted ON TRUE`, [...values, reserved.maxRows, reserved.maxEncodedBytes]) if(result.fields && ["__flowRowCount","__flowEncodedBytes","__flowPresent"].some(name=>result.fields!.filter(field=>field.name===name).length!==1))throw Error("Reserved NetFlow source column collision") const row = result.rows[0] const count = String(row?.__flowRowCount), bytes = String(row?.__flowEncodedBytes) if (!row || !/^\d{1,15}$/.test(count) || !/^\d{1,15}$/.test(bytes) || Number(count) > reserved.maxRows || Number(bytes) + 2 + Math.max(Number(count)-1,0) > reserved.maxEncodedBytes) throw Object.assign(Error("Источник NetFlow превышает допустимый объём; сократите область или окно"), { statusCode: 409, code: "STATE_UNAVAILABLE" }) if (Number(count) === 0) return [] if (result.rows.length !== Number(count) || result.rows.some(item => item.__flowPresent !== true)) throw Error("Invalid bounded NetFlow SQL result") // Preserve the normal pg column parsers (not JSONB numeric/timestamp coercion). for (const item of result.rows) { delete item.__flowRowCount; delete item.__flowEncodedBytes; delete item.__flowPresent } return result.rows as T[] }) }