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

Function decodeArray

packages/sql/pg/src/PgTypes.ts:1760–1809  ·  view source on GitHub ↗
(
  bytes: Uint8Array,
  start: number,
  size: number,
  elementOid: number,
  lookup: Lookup
)

Source from the content-addressed store, hash-verified

1758 * thousand lookups and a thousand views.
1759 */
1760const decodeArray = (
1761 bytes: Uint8Array,
1762 start: number,
1763 size: number,
1764 elementOid: number,
1765 lookup: Lookup
1766): ReadonlyArray<unknown> => {
1767 if (size < 12) return fail("Truncated array value")
1768 const dimensions = readInt32(bytes, start)
1769 const wireElementOid = readUint32(bytes, start + 8)
1770 if (wireElementOid !== elementOid) {
1771 return fail(`Array element OID ${wireElementOid} does not match expected OID ${elementOid}`)
1772 }
1773 if (dimensions === 0) {
1774 if (size !== 12) return fail("Zero-dimensional array has trailing bytes")
1775 return []
1776 }
1777 if (dimensions !== 1) {
1778 return fail(`Only 1-dimensional arrays are supported, received ${dimensions} dimensions`)
1779 }
1780 if (size < 20) return fail("Truncated array value")
1781 const length = readInt32(bytes, start + 12)
1782 if (length < 0) return fail(`Invalid array length: ${length}`)
1783 const lowerBound = readInt32(bytes, start + 16)
1784 if (lowerBound !== 1) return fail(`Only arrays with a lower bound of 1 are supported, received ${lowerBound}`)
1785 const codec = lookup(elementOid)
1786 const read = codec?.read
1787 const values: Array<unknown> = new Array(length)
1788 const limit = start + size
1789 let offset = start + 20
1790 for (let i = 0; i < length; i++) {
1791 if (offset + 4 > limit) return fail("Truncated array element")
1792 const elementSize = readInt32(bytes, offset)
1793 offset += 4
1794 if (elementSize < -1) return fail(`Invalid array element length: ${elementSize}`)
1795 if (elementSize === -1) {
1796 values[i] = null
1797 } else {
1798 if (offset + elementSize > limit) return fail("Truncated array element")
1799 values[i] = read !== undefined
1800 ? read(bytes, offset, elementSize)
1801 : codec === undefined
1802 ? region(bytes, offset, elementSize)
1803 : codec.decode(region(bytes, offset, elementSize))
1804 offset += elementSize
1805 }
1806 }
1807 if (offset !== limit) return fail("Array value has trailing bytes")
1808 return values
1809}
1810
1811/**
1812 * PostgreSQL record_send: an int32 field count, then for each field its type

Callers 1

makeArrayCodecFunction · 0.70

Calls 7

readInt32Function · 0.85
readUint32Function · 0.85
failFunction · 0.70
lookupFunction · 0.70
readFunction · 0.70
regionFunction · 0.70
decodeMethod · 0.45

Tested by

no test coverage detected