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