test-buffers.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // https://www.postgresql.org/docs/current/protocol-message-formats.html
  2. import BufferList from './buffer-list'
  3. const buffers = {
  4. readyForQuery: function () {
  5. return new BufferList().add(Buffer.from('I')).join(true, 'Z')
  6. },
  7. authenticationOk: function () {
  8. return new BufferList().addInt32(0).join(true, 'R')
  9. },
  10. authenticationCleartextPassword: function () {
  11. return new BufferList().addInt32(3).join(true, 'R')
  12. },
  13. authenticationMD5Password: function () {
  14. return new BufferList()
  15. .addInt32(5)
  16. .add(Buffer.from([1, 2, 3, 4]))
  17. .join(true, 'R')
  18. },
  19. authenticationSASL: function () {
  20. return new BufferList().addInt32(10).addCString('SCRAM-SHA-256').addCString('').join(true, 'R')
  21. },
  22. authenticationSASLContinue: function () {
  23. return new BufferList().addInt32(11).addString('data').join(true, 'R')
  24. },
  25. authenticationSASLFinal: function () {
  26. return new BufferList().addInt32(12).addString('data').join(true, 'R')
  27. },
  28. parameterStatus: function (name: string, value: string) {
  29. return new BufferList().addCString(name).addCString(value).join(true, 'S')
  30. },
  31. backendKeyData: function (processID: number, secretKey: number) {
  32. return new BufferList().addInt32(processID).addInt32(secretKey).join(true, 'K')
  33. },
  34. commandComplete: function (string: string) {
  35. return new BufferList().addCString(string).join(true, 'C')
  36. },
  37. rowDescription: function (fields: any[]) {
  38. fields = fields || []
  39. var buf = new BufferList()
  40. buf.addInt16(fields.length)
  41. fields.forEach(function (field) {
  42. buf
  43. .addCString(field.name)
  44. .addInt32(field.tableID || 0)
  45. .addInt16(field.attributeNumber || 0)
  46. .addInt32(field.dataTypeID || 0)
  47. .addInt16(field.dataTypeSize || 0)
  48. .addInt32(field.typeModifier || 0)
  49. .addInt16(field.formatCode || 0)
  50. })
  51. return buf.join(true, 'T')
  52. },
  53. parameterDescription: function (dataTypeIDs: number[]) {
  54. dataTypeIDs = dataTypeIDs || []
  55. var buf = new BufferList()
  56. buf.addInt16(dataTypeIDs.length)
  57. dataTypeIDs.forEach(function (dataTypeID) {
  58. buf.addInt32(dataTypeID)
  59. })
  60. return buf.join(true, 't')
  61. },
  62. dataRow: function (columns: any[]) {
  63. columns = columns || []
  64. var buf = new BufferList()
  65. buf.addInt16(columns.length)
  66. columns.forEach(function (col) {
  67. if (col == null) {
  68. buf.addInt32(-1)
  69. } else {
  70. var strBuf = Buffer.from(col, 'utf8')
  71. buf.addInt32(strBuf.length)
  72. buf.add(strBuf)
  73. }
  74. })
  75. return buf.join(true, 'D')
  76. },
  77. error: function (fields: any) {
  78. return buffers.errorOrNotice(fields).join(true, 'E')
  79. },
  80. notice: function (fields: any) {
  81. return buffers.errorOrNotice(fields).join(true, 'N')
  82. },
  83. errorOrNotice: function (fields: any) {
  84. fields = fields || []
  85. var buf = new BufferList()
  86. fields.forEach(function (field: any) {
  87. buf.addChar(field.type)
  88. buf.addCString(field.value)
  89. })
  90. return buf.add(Buffer.from([0])) // terminator
  91. },
  92. parseComplete: function () {
  93. return new BufferList().join(true, '1')
  94. },
  95. bindComplete: function () {
  96. return new BufferList().join(true, '2')
  97. },
  98. notification: function (id: number, channel: string, payload: string) {
  99. return new BufferList().addInt32(id).addCString(channel).addCString(payload).join(true, 'A')
  100. },
  101. emptyQuery: function () {
  102. return new BufferList().join(true, 'I')
  103. },
  104. portalSuspended: function () {
  105. return new BufferList().join(true, 's')
  106. },
  107. closeComplete: function () {
  108. return new BufferList().join(true, '3')
  109. },
  110. copyIn: function (cols: number) {
  111. const list = new BufferList()
  112. // text mode
  113. .addByte(0)
  114. // column count
  115. .addInt16(cols)
  116. for (let i = 0; i < cols; i++) {
  117. list.addInt16(i)
  118. }
  119. return list.join(true, 'G')
  120. },
  121. copyOut: function (cols: number) {
  122. const list = new BufferList()
  123. // text mode
  124. .addByte(0)
  125. // column count
  126. .addInt16(cols)
  127. for (let i = 0; i < cols; i++) {
  128. list.addInt16(i)
  129. }
  130. return list.join(true, 'H')
  131. },
  132. copyData: function (bytes: Buffer) {
  133. return new BufferList().add(bytes).join(true, 'd')
  134. },
  135. copyDone: function () {
  136. return new BufferList().join(true, 'c')
  137. },
  138. }
  139. export default buffers