333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
# buffer-writer
 
[![Build Status](https://secure.travis-ci.org/brianc/node-buffer-writer.png?branch=master)](http://travis-ci.org/brianc/node-buffer-writer)
 
Fast & efficient buffer writer used to keep memory usage low by internally recycling a single large buffer.
 
Used as the binary protocol writer in [node-postgres](https://github.com/brianc/node-postgres)
 
Since postgres requires big endian encoding, this only writes big endian numbers for now, but can & probably will easily be extended to write little endian as well.
 
I'll admit this has a few postgres specific things I might need to take out in the future, such as `addHeader`
 
## api
 
`var writer = new (require('buffer-writer')());`
 
### writer.addInt32(num)
 
Writes a 4-byte big endian binary encoded number to the end of the buffer.
 
### writer.addInt16(num)
 
Writes a 2-byte big endian binary encoded number to the end of the buffer.
 
### writer.addCString(string)
 
Writes a string to the buffer `utf8` encoded and adds a null character (`\0`) at the end.
 
### var buffer = writer.addHeader(char)
 
Writes the 5 byte PostgreSQL required header to the beginning of the buffer. (1 byte for character, 1 BE Int32 for length of the buffer)
 
### var buffer = writer.join()
 
Collects all data in the writer and joins it into a single, new buffer.
 
### var buffer = writer.flush(char)
 
Writes the 5 byte postgres required message header, collects all data in the writer and joins it into a single, new buffer, and then resets the writer.
 
## thoughts
 
This is kind of node-postgres specific.  If you're interested in using this for a more general purpose thing, lemme know.
I would love to work with you on getting this more reusable for your needs.
 
## license
 
MIT