parser.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import { TransformOptions } from 'stream'
  2. import {
  3. Mode,
  4. bindComplete,
  5. parseComplete,
  6. closeComplete,
  7. noData,
  8. portalSuspended,
  9. copyDone,
  10. replicationStart,
  11. emptyQuery,
  12. ReadyForQueryMessage,
  13. CommandCompleteMessage,
  14. CopyDataMessage,
  15. CopyResponse,
  16. NotificationResponseMessage,
  17. RowDescriptionMessage,
  18. ParameterDescriptionMessage,
  19. Field,
  20. DataRowMessage,
  21. ParameterStatusMessage,
  22. BackendKeyDataMessage,
  23. DatabaseError,
  24. BackendMessage,
  25. MessageName,
  26. AuthenticationMD5Password,
  27. NoticeMessage,
  28. } from './messages'
  29. import { BufferReader } from './buffer-reader'
  30. // every message is prefixed with a single bye
  31. const CODE_LENGTH = 1
  32. // every message has an int32 length which includes itself but does
  33. // NOT include the code in the length
  34. const LEN_LENGTH = 4
  35. const HEADER_LENGTH = CODE_LENGTH + LEN_LENGTH
  36. export type Packet = {
  37. code: number
  38. packet: Buffer
  39. }
  40. const emptyBuffer = Buffer.allocUnsafe(0)
  41. type StreamOptions = TransformOptions & {
  42. mode: Mode
  43. }
  44. const enum MessageCodes {
  45. DataRow = 0x44, // D
  46. ParseComplete = 0x31, // 1
  47. BindComplete = 0x32, // 2
  48. CloseComplete = 0x33, // 3
  49. CommandComplete = 0x43, // C
  50. ReadyForQuery = 0x5a, // Z
  51. NoData = 0x6e, // n
  52. NotificationResponse = 0x41, // A
  53. AuthenticationResponse = 0x52, // R
  54. ParameterStatus = 0x53, // S
  55. BackendKeyData = 0x4b, // K
  56. ErrorMessage = 0x45, // E
  57. NoticeMessage = 0x4e, // N
  58. RowDescriptionMessage = 0x54, // T
  59. ParameterDescriptionMessage = 0x74, // t
  60. PortalSuspended = 0x73, // s
  61. ReplicationStart = 0x57, // W
  62. EmptyQuery = 0x49, // I
  63. CopyIn = 0x47, // G
  64. CopyOut = 0x48, // H
  65. CopyDone = 0x63, // c
  66. CopyData = 0x64, // d
  67. }
  68. export type MessageCallback = (msg: BackendMessage) => void
  69. export class Parser {
  70. private buffer: Buffer = emptyBuffer
  71. private bufferLength: number = 0
  72. private bufferOffset: number = 0
  73. private reader = new BufferReader()
  74. private mode: Mode
  75. constructor(opts?: StreamOptions) {
  76. if (opts?.mode === 'binary') {
  77. throw new Error('Binary mode not supported yet')
  78. }
  79. this.mode = opts?.mode || 'text'
  80. }
  81. public parse(buffer: Buffer, callback: MessageCallback) {
  82. this.mergeBuffer(buffer)
  83. const bufferFullLength = this.bufferOffset + this.bufferLength
  84. let offset = this.bufferOffset
  85. while (offset + HEADER_LENGTH <= bufferFullLength) {
  86. // code is 1 byte long - it identifies the message type
  87. const code = this.buffer[offset]
  88. // length is 1 Uint32BE - it is the length of the message EXCLUDING the code
  89. const length = this.buffer.readUInt32BE(offset + CODE_LENGTH)
  90. const fullMessageLength = CODE_LENGTH + length
  91. if (fullMessageLength + offset <= bufferFullLength) {
  92. const message = this.handlePacket(offset + HEADER_LENGTH, code, length, this.buffer)
  93. callback(message)
  94. offset += fullMessageLength
  95. } else {
  96. break
  97. }
  98. }
  99. if (offset === bufferFullLength) {
  100. // No more use for the buffer
  101. this.buffer = emptyBuffer
  102. this.bufferLength = 0
  103. this.bufferOffset = 0
  104. } else {
  105. // Adjust the cursors of remainingBuffer
  106. this.bufferLength = bufferFullLength - offset
  107. this.bufferOffset = offset
  108. }
  109. }
  110. private mergeBuffer(buffer: Buffer): void {
  111. if (this.bufferLength > 0) {
  112. const newLength = this.bufferLength + buffer.byteLength
  113. const newFullLength = newLength + this.bufferOffset
  114. if (newFullLength > this.buffer.byteLength) {
  115. // We can't concat the new buffer with the remaining one
  116. let newBuffer: Buffer
  117. if (newLength <= this.buffer.byteLength && this.bufferOffset >= this.bufferLength) {
  118. // We can move the relevant part to the beginning of the buffer instead of allocating a new buffer
  119. newBuffer = this.buffer
  120. } else {
  121. // Allocate a new larger buffer
  122. let newBufferLength = this.buffer.byteLength * 2
  123. while (newLength >= newBufferLength) {
  124. newBufferLength *= 2
  125. }
  126. newBuffer = Buffer.allocUnsafe(newBufferLength)
  127. }
  128. // Move the remaining buffer to the new one
  129. this.buffer.copy(newBuffer, 0, this.bufferOffset, this.bufferOffset + this.bufferLength)
  130. this.buffer = newBuffer
  131. this.bufferOffset = 0
  132. }
  133. // Concat the new buffer with the remaining one
  134. buffer.copy(this.buffer, this.bufferOffset + this.bufferLength)
  135. this.bufferLength = newLength
  136. } else {
  137. this.buffer = buffer
  138. this.bufferOffset = 0
  139. this.bufferLength = buffer.byteLength
  140. }
  141. }
  142. private handlePacket(offset: number, code: number, length: number, bytes: Buffer): BackendMessage {
  143. switch (code) {
  144. case MessageCodes.BindComplete:
  145. return bindComplete
  146. case MessageCodes.ParseComplete:
  147. return parseComplete
  148. case MessageCodes.CloseComplete:
  149. return closeComplete
  150. case MessageCodes.NoData:
  151. return noData
  152. case MessageCodes.PortalSuspended:
  153. return portalSuspended
  154. case MessageCodes.CopyDone:
  155. return copyDone
  156. case MessageCodes.ReplicationStart:
  157. return replicationStart
  158. case MessageCodes.EmptyQuery:
  159. return emptyQuery
  160. case MessageCodes.DataRow:
  161. return this.parseDataRowMessage(offset, length, bytes)
  162. case MessageCodes.CommandComplete:
  163. return this.parseCommandCompleteMessage(offset, length, bytes)
  164. case MessageCodes.ReadyForQuery:
  165. return this.parseReadyForQueryMessage(offset, length, bytes)
  166. case MessageCodes.NotificationResponse:
  167. return this.parseNotificationMessage(offset, length, bytes)
  168. case MessageCodes.AuthenticationResponse:
  169. return this.parseAuthenticationResponse(offset, length, bytes)
  170. case MessageCodes.ParameterStatus:
  171. return this.parseParameterStatusMessage(offset, length, bytes)
  172. case MessageCodes.BackendKeyData:
  173. return this.parseBackendKeyData(offset, length, bytes)
  174. case MessageCodes.ErrorMessage:
  175. return this.parseErrorMessage(offset, length, bytes, 'error')
  176. case MessageCodes.NoticeMessage:
  177. return this.parseErrorMessage(offset, length, bytes, 'notice')
  178. case MessageCodes.RowDescriptionMessage:
  179. return this.parseRowDescriptionMessage(offset, length, bytes)
  180. case MessageCodes.ParameterDescriptionMessage:
  181. return this.parseParameterDescriptionMessage(offset, length, bytes)
  182. case MessageCodes.CopyIn:
  183. return this.parseCopyInMessage(offset, length, bytes)
  184. case MessageCodes.CopyOut:
  185. return this.parseCopyOutMessage(offset, length, bytes)
  186. case MessageCodes.CopyData:
  187. return this.parseCopyData(offset, length, bytes)
  188. default:
  189. return new DatabaseError('received invalid response: ' + code.toString(16), length, 'error')
  190. }
  191. }
  192. private parseReadyForQueryMessage(offset: number, length: number, bytes: Buffer) {
  193. this.reader.setBuffer(offset, bytes)
  194. const status = this.reader.string(1)
  195. return new ReadyForQueryMessage(length, status)
  196. }
  197. private parseCommandCompleteMessage(offset: number, length: number, bytes: Buffer) {
  198. this.reader.setBuffer(offset, bytes)
  199. const text = this.reader.cstring()
  200. return new CommandCompleteMessage(length, text)
  201. }
  202. private parseCopyData(offset: number, length: number, bytes: Buffer) {
  203. const chunk = bytes.slice(offset, offset + (length - 4))
  204. return new CopyDataMessage(length, chunk)
  205. }
  206. private parseCopyInMessage(offset: number, length: number, bytes: Buffer) {
  207. return this.parseCopyMessage(offset, length, bytes, 'copyInResponse')
  208. }
  209. private parseCopyOutMessage(offset: number, length: number, bytes: Buffer) {
  210. return this.parseCopyMessage(offset, length, bytes, 'copyOutResponse')
  211. }
  212. private parseCopyMessage(offset: number, length: number, bytes: Buffer, messageName: MessageName) {
  213. this.reader.setBuffer(offset, bytes)
  214. const isBinary = this.reader.byte() !== 0
  215. const columnCount = this.reader.int16()
  216. const message = new CopyResponse(length, messageName, isBinary, columnCount)
  217. for (let i = 0; i < columnCount; i++) {
  218. message.columnTypes[i] = this.reader.int16()
  219. }
  220. return message
  221. }
  222. private parseNotificationMessage(offset: number, length: number, bytes: Buffer) {
  223. this.reader.setBuffer(offset, bytes)
  224. const processId = this.reader.int32()
  225. const channel = this.reader.cstring()
  226. const payload = this.reader.cstring()
  227. return new NotificationResponseMessage(length, processId, channel, payload)
  228. }
  229. private parseRowDescriptionMessage(offset: number, length: number, bytes: Buffer) {
  230. this.reader.setBuffer(offset, bytes)
  231. const fieldCount = this.reader.int16()
  232. const message = new RowDescriptionMessage(length, fieldCount)
  233. for (let i = 0; i < fieldCount; i++) {
  234. message.fields[i] = this.parseField()
  235. }
  236. return message
  237. }
  238. private parseField(): Field {
  239. const name = this.reader.cstring()
  240. const tableID = this.reader.int32()
  241. const columnID = this.reader.int16()
  242. const dataTypeID = this.reader.int32()
  243. const dataTypeSize = this.reader.int16()
  244. const dataTypeModifier = this.reader.int32()
  245. const mode = this.reader.int16() === 0 ? 'text' : 'binary'
  246. return new Field(name, tableID, columnID, dataTypeID, dataTypeSize, dataTypeModifier, mode)
  247. }
  248. private parseParameterDescriptionMessage(offset: number, length: number, bytes: Buffer) {
  249. this.reader.setBuffer(offset, bytes)
  250. const parameterCount = this.reader.int16()
  251. const message = new ParameterDescriptionMessage(length, parameterCount)
  252. for (let i = 0; i < parameterCount; i++) {
  253. message.dataTypeIDs[i] = this.reader.int32()
  254. }
  255. return message
  256. }
  257. private parseDataRowMessage(offset: number, length: number, bytes: Buffer) {
  258. this.reader.setBuffer(offset, bytes)
  259. const fieldCount = this.reader.int16()
  260. const fields: any[] = new Array(fieldCount)
  261. for (let i = 0; i < fieldCount; i++) {
  262. const len = this.reader.int32()
  263. // a -1 for length means the value of the field is null
  264. fields[i] = len === -1 ? null : this.reader.string(len)
  265. }
  266. return new DataRowMessage(length, fields)
  267. }
  268. private parseParameterStatusMessage(offset: number, length: number, bytes: Buffer) {
  269. this.reader.setBuffer(offset, bytes)
  270. const name = this.reader.cstring()
  271. const value = this.reader.cstring()
  272. return new ParameterStatusMessage(length, name, value)
  273. }
  274. private parseBackendKeyData(offset: number, length: number, bytes: Buffer) {
  275. this.reader.setBuffer(offset, bytes)
  276. const processID = this.reader.int32()
  277. const secretKey = this.reader.int32()
  278. return new BackendKeyDataMessage(length, processID, secretKey)
  279. }
  280. public parseAuthenticationResponse(offset: number, length: number, bytes: Buffer) {
  281. this.reader.setBuffer(offset, bytes)
  282. const code = this.reader.int32()
  283. // TODO(bmc): maybe better types here
  284. const message: BackendMessage & any = {
  285. name: 'authenticationOk',
  286. length,
  287. }
  288. switch (code) {
  289. case 0: // AuthenticationOk
  290. break
  291. case 3: // AuthenticationCleartextPassword
  292. if (message.length === 8) {
  293. message.name = 'authenticationCleartextPassword'
  294. }
  295. break
  296. case 5: // AuthenticationMD5Password
  297. if (message.length === 12) {
  298. message.name = 'authenticationMD5Password'
  299. const salt = this.reader.bytes(4)
  300. return new AuthenticationMD5Password(length, salt)
  301. }
  302. break
  303. case 10: // AuthenticationSASL
  304. message.name = 'authenticationSASL'
  305. message.mechanisms = []
  306. let mechanism: string
  307. do {
  308. mechanism = this.reader.cstring()
  309. if (mechanism) {
  310. message.mechanisms.push(mechanism)
  311. }
  312. } while (mechanism)
  313. break
  314. case 11: // AuthenticationSASLContinue
  315. message.name = 'authenticationSASLContinue'
  316. message.data = this.reader.string(length - 8)
  317. break
  318. case 12: // AuthenticationSASLFinal
  319. message.name = 'authenticationSASLFinal'
  320. message.data = this.reader.string(length - 8)
  321. break
  322. default:
  323. throw new Error('Unknown authenticationOk message type ' + code)
  324. }
  325. return message
  326. }
  327. private parseErrorMessage(offset: number, length: number, bytes: Buffer, name: MessageName) {
  328. this.reader.setBuffer(offset, bytes)
  329. const fields: Record<string, string> = {}
  330. let fieldType = this.reader.string(1)
  331. while (fieldType !== '\0') {
  332. fields[fieldType] = this.reader.cstring()
  333. fieldType = this.reader.string(1)
  334. }
  335. const messageValue = fields.M
  336. const message =
  337. name === 'notice' ? new NoticeMessage(length, messageValue) : new DatabaseError(messageValue, length, name)
  338. message.severity = fields.S
  339. message.code = fields.C
  340. message.detail = fields.D
  341. message.hint = fields.H
  342. message.position = fields.P
  343. message.internalPosition = fields.p
  344. message.internalQuery = fields.q
  345. message.where = fields.W
  346. message.schema = fields.s
  347. message.table = fields.t
  348. message.column = fields.c
  349. message.dataType = fields.d
  350. message.constraint = fields.n
  351. message.file = fields.F
  352. message.line = fields.L
  353. message.routine = fields.R
  354. return message
  355. }
  356. }