MCPcopy Create free account
hub / github.com/Effect-TS/effect / decodeDataRow

Function decodeDataRow

packages/sql/pg/src/PgProtocol.ts:1824–1868  ·  view source on GitHub ↗
(
  bytes: Uint8Array,
  store: ArrayBufferLike,
  base: number,
  offset: number,
  limit: number,
  readField: FieldReader<A> | undefined
)

Source from the content-addressed store, hash-verified

1822// whole frame is buffered, which turns every field read into one bounds check
1823// against `limit`.
1824const decodeDataRow = <A>(
1825 bytes: Uint8Array,
1826 store: ArrayBufferLike,
1827 base: number,
1828 offset: number,
1829 limit: number,
1830 readField: FieldReader<A> | undefined
1831): DataRow<A> => {
1832 if (offset + 2 > limit) {
1833 throw new ParseError({ message: "Truncated message: expected 2 more byte(s)" })
1834 }
1835 const count = ((bytes[offset] << 8) | bytes[offset + 1]) << 16 >> 16
1836 if (count < 0) {
1837 throw new ParseError({ message: `Invalid DataRow field count: ${count}` })
1838 }
1839 const values: Array<any> = new Array(count)
1840 let position = offset + 2
1841 for (let i = 0; i < count; i++) {
1842 if (position + 4 > limit) {
1843 throw new ParseError({ message: "Truncated message: expected 4 more byte(s)" })
1844 }
1845 const size = (bytes[position] << 24) | (bytes[position + 1] << 16) | (bytes[position + 2] << 8) |
1846 bytes[position + 3]
1847 position += 4
1848 if (size < 0) {
1849 if (size < -1) {
1850 throw new ParseError({ message: `Invalid DataRow field length: ${size}` })
1851 }
1852 values[i] = readField === undefined ? null : readField(bytes, position, -1, i)
1853 continue
1854 }
1855 const next = position + size
1856 if (next > limit) {
1857 throw new ParseError({ message: `Truncated message: expected ${size} more byte(s)` })
1858 }
1859 values[i] = readField === undefined
1860 ? view(store, base + position, size)
1861 : readField(bytes, position, size, i)
1862 position = next
1863 }
1864 if (position !== limit) {
1865 throw new ParseError({ message: `DataRow has ${limit - position} trailing byte(s)` })
1866 }
1867 return { _tag: "DataRow", values }
1868}
1869
1870const decodeBackend = (type: number, reader: Reader): BackendMessage => {
1871 switch (type) {

Callers 2

pushEachFunction · 0.85
decodeBackendFunction · 0.85

Calls 2

readFieldFunction · 0.85
viewFunction · 0.70

Tested by

no test coverage detected