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