messages.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. export type Mode = 'text' | 'binary'
  2. export type MessageName =
  3. | 'parseComplete'
  4. | 'bindComplete'
  5. | 'closeComplete'
  6. | 'noData'
  7. | 'portalSuspended'
  8. | 'replicationStart'
  9. | 'emptyQuery'
  10. | 'copyDone'
  11. | 'copyData'
  12. | 'rowDescription'
  13. | 'parameterDescription'
  14. | 'parameterStatus'
  15. | 'backendKeyData'
  16. | 'notification'
  17. | 'readyForQuery'
  18. | 'commandComplete'
  19. | 'dataRow'
  20. | 'copyInResponse'
  21. | 'copyOutResponse'
  22. | 'authenticationOk'
  23. | 'authenticationMD5Password'
  24. | 'authenticationCleartextPassword'
  25. | 'authenticationSASL'
  26. | 'authenticationSASLContinue'
  27. | 'authenticationSASLFinal'
  28. | 'error'
  29. | 'notice'
  30. export interface BackendMessage {
  31. name: MessageName
  32. length: number
  33. }
  34. export const parseComplete: BackendMessage = {
  35. name: 'parseComplete',
  36. length: 5,
  37. }
  38. export const bindComplete: BackendMessage = {
  39. name: 'bindComplete',
  40. length: 5,
  41. }
  42. export const closeComplete: BackendMessage = {
  43. name: 'closeComplete',
  44. length: 5,
  45. }
  46. export const noData: BackendMessage = {
  47. name: 'noData',
  48. length: 5,
  49. }
  50. export const portalSuspended: BackendMessage = {
  51. name: 'portalSuspended',
  52. length: 5,
  53. }
  54. export const replicationStart: BackendMessage = {
  55. name: 'replicationStart',
  56. length: 4,
  57. }
  58. export const emptyQuery: BackendMessage = {
  59. name: 'emptyQuery',
  60. length: 4,
  61. }
  62. export const copyDone: BackendMessage = {
  63. name: 'copyDone',
  64. length: 4,
  65. }
  66. interface NoticeOrError {
  67. message: string | undefined
  68. severity: string | undefined
  69. code: string | undefined
  70. detail: string | undefined
  71. hint: string | undefined
  72. position: string | undefined
  73. internalPosition: string | undefined
  74. internalQuery: string | undefined
  75. where: string | undefined
  76. schema: string | undefined
  77. table: string | undefined
  78. column: string | undefined
  79. dataType: string | undefined
  80. constraint: string | undefined
  81. file: string | undefined
  82. line: string | undefined
  83. routine: string | undefined
  84. }
  85. export class DatabaseError extends Error implements NoticeOrError {
  86. public severity: string | undefined
  87. public code: string | undefined
  88. public detail: string | undefined
  89. public hint: string | undefined
  90. public position: string | undefined
  91. public internalPosition: string | undefined
  92. public internalQuery: string | undefined
  93. public where: string | undefined
  94. public schema: string | undefined
  95. public table: string | undefined
  96. public column: string | undefined
  97. public dataType: string | undefined
  98. public constraint: string | undefined
  99. public file: string | undefined
  100. public line: string | undefined
  101. public routine: string | undefined
  102. constructor(message: string, public readonly length: number, public readonly name: MessageName) {
  103. super(message)
  104. }
  105. }
  106. export class CopyDataMessage {
  107. public readonly name = 'copyData'
  108. constructor(public readonly length: number, public readonly chunk: Buffer) {}
  109. }
  110. export class CopyResponse {
  111. public readonly columnTypes: number[]
  112. constructor(
  113. public readonly length: number,
  114. public readonly name: MessageName,
  115. public readonly binary: boolean,
  116. columnCount: number
  117. ) {
  118. this.columnTypes = new Array(columnCount)
  119. }
  120. }
  121. export class Field {
  122. constructor(
  123. public readonly name: string,
  124. public readonly tableID: number,
  125. public readonly columnID: number,
  126. public readonly dataTypeID: number,
  127. public readonly dataTypeSize: number,
  128. public readonly dataTypeModifier: number,
  129. public readonly format: Mode
  130. ) {}
  131. }
  132. export class RowDescriptionMessage {
  133. public readonly name: MessageName = 'rowDescription'
  134. public readonly fields: Field[]
  135. constructor(public readonly length: number, public readonly fieldCount: number) {
  136. this.fields = new Array(this.fieldCount)
  137. }
  138. }
  139. export class ParameterDescriptionMessage {
  140. public readonly name: MessageName = 'parameterDescription'
  141. public readonly dataTypeIDs: number[]
  142. constructor(public readonly length: number, public readonly parameterCount: number) {
  143. this.dataTypeIDs = new Array(this.parameterCount)
  144. }
  145. }
  146. export class ParameterStatusMessage {
  147. public readonly name: MessageName = 'parameterStatus'
  148. constructor(
  149. public readonly length: number,
  150. public readonly parameterName: string,
  151. public readonly parameterValue: string
  152. ) {}
  153. }
  154. export class AuthenticationMD5Password implements BackendMessage {
  155. public readonly name: MessageName = 'authenticationMD5Password'
  156. constructor(public readonly length: number, public readonly salt: Buffer) {}
  157. }
  158. export class BackendKeyDataMessage {
  159. public readonly name: MessageName = 'backendKeyData'
  160. constructor(public readonly length: number, public readonly processID: number, public readonly secretKey: number) {}
  161. }
  162. export class NotificationResponseMessage {
  163. public readonly name: MessageName = 'notification'
  164. constructor(
  165. public readonly length: number,
  166. public readonly processId: number,
  167. public readonly channel: string,
  168. public readonly payload: string
  169. ) {}
  170. }
  171. export class ReadyForQueryMessage {
  172. public readonly name: MessageName = 'readyForQuery'
  173. constructor(public readonly length: number, public readonly status: string) {}
  174. }
  175. export class CommandCompleteMessage {
  176. public readonly name: MessageName = 'commandComplete'
  177. constructor(public readonly length: number, public readonly text: string) {}
  178. }
  179. export class DataRowMessage {
  180. public readonly fieldCount: number
  181. public readonly name: MessageName = 'dataRow'
  182. constructor(public length: number, public fields: any[]) {
  183. this.fieldCount = fields.length
  184. }
  185. }
  186. export class NoticeMessage implements BackendMessage, NoticeOrError {
  187. constructor(public readonly length: number, public readonly message: string | undefined) {}
  188. public readonly name = 'notice'
  189. public severity: string | undefined
  190. public code: string | undefined
  191. public detail: string | undefined
  192. public hint: string | undefined
  193. public position: string | undefined
  194. public internalPosition: string | undefined
  195. public internalQuery: string | undefined
  196. public where: string | undefined
  197. public schema: string | undefined
  198. public table: string | undefined
  199. public column: string | undefined
  200. public dataType: string | undefined
  201. public constraint: string | undefined
  202. public file: string | undefined
  203. public line: string | undefined
  204. public routine: string | undefined
  205. }