UNPKG

3.59 kBJavaScriptView Raw
1/**
2 * Buffer Writer
3 * =============
4 *
5 * This is the writing complement of the Br. You can easily write
6 * VarInts and other basic number types. The way to use it is: buf =
7 * new Bw().write(buf1).write(buf2).toBuffer()
8 */
9'use strict'
10
11class Bw {
12 constructor (bufs) {
13 this.fromObject({ bufs })
14 }
15
16 fromObject (obj) {
17 this.bufs = obj.bufs || this.bufs || []
18 return this
19 }
20
21 getLength () {
22 let len = 0
23 for (const i in this.bufs) {
24 const buf = this.bufs[i]
25 len = len + buf.length
26 }
27 return len
28 }
29
30 toBuffer () {
31 return Buffer.concat(this.bufs)
32 }
33
34 write (buf) {
35 this.bufs.push(buf)
36 return this
37 }
38
39 writeReverse (buf) {
40 const buf2 = Buffer.alloc(buf.length)
41 for (let i = 0; i < buf2.length; i++) {
42 buf2[i] = buf[buf.length - 1 - i]
43 }
44 this.bufs.push(buf2)
45 return this
46 }
47
48 writeUInt8 (n) {
49 const buf = Buffer.alloc(1)
50 buf.writeUInt8(n, 0)
51 this.write(buf)
52 return this
53 }
54
55 writeInt8 (n) {
56 const buf = Buffer.alloc(1)
57 buf.writeInt8(n, 0)
58 this.write(buf)
59 return this
60 }
61
62 writeUInt16BE (n) {
63 const buf = Buffer.alloc(2)
64 buf.writeUInt16BE(n, 0)
65 this.write(buf)
66 return this
67 }
68
69 writeInt16BE (n) {
70 const buf = Buffer.alloc(2)
71 buf.writeInt16BE(n, 0)
72 this.write(buf)
73 return this
74 }
75
76 writeUInt16LE (n) {
77 const buf = Buffer.alloc(2)
78 buf.writeUInt16LE(n, 0)
79 this.write(buf)
80 return this
81 }
82
83 writeInt16LE (n) {
84 const buf = Buffer.alloc(2)
85 buf.writeInt16LE(n, 0)
86 this.write(buf)
87 return this
88 }
89
90 writeUInt32BE (n) {
91 const buf = Buffer.alloc(4)
92 buf.writeUInt32BE(n, 0)
93 this.write(buf)
94 return this
95 }
96
97 writeInt32BE (n) {
98 const buf = Buffer.alloc(4)
99 buf.writeInt32BE(n, 0)
100 this.write(buf)
101 return this
102 }
103
104 writeUInt32LE (n) {
105 const buf = Buffer.alloc(4)
106 buf.writeUInt32LE(n, 0)
107 this.write(buf)
108 return this
109 }
110
111 writeInt32LE (n) {
112 const buf = Buffer.alloc(4)
113 buf.writeInt32LE(n, 0)
114 this.write(buf)
115 return this
116 }
117
118 writeUInt64BEBn (bn) {
119 const buf = bn.toBuffer({ size: 8 })
120 this.write(buf)
121 return this
122 }
123
124 writeUInt64LEBn (bn) {
125 const buf = bn.toBuffer({ size: 8 })
126 this.writeReverse(buf)
127 return this
128 }
129
130 writeVarIntNum (n) {
131 const buf = Bw.varIntBufNum(n)
132 this.write(buf)
133 return this
134 }
135
136 writeVarIntBn (bn) {
137 const buf = Bw.varIntBufBn(bn)
138 this.write(buf)
139 return this
140 }
141
142 static varIntBufNum (n) {
143 let buf
144 if (n < 253) {
145 buf = Buffer.alloc(1)
146 buf.writeUInt8(n, 0)
147 } else if (n < 0x10000) {
148 buf = Buffer.alloc(1 + 2)
149 buf.writeUInt8(253, 0)
150 buf.writeUInt16LE(n, 1)
151 } else if (n < 0x100000000) {
152 buf = Buffer.alloc(1 + 4)
153 buf.writeUInt8(254, 0)
154 buf.writeUInt32LE(n, 1)
155 } else {
156 buf = Buffer.alloc(1 + 8)
157 buf.writeUInt8(255, 0)
158 buf.writeInt32LE(n & -1, 1)
159 buf.writeUInt32LE(Math.floor(n / 0x100000000), 5)
160 }
161 return buf
162 }
163
164 static varIntBufBn (bn) {
165 let buf
166 const n = bn.toNumber()
167 if (n < 253) {
168 buf = Buffer.alloc(1)
169 buf.writeUInt8(n, 0)
170 } else if (n < 0x10000) {
171 buf = Buffer.alloc(1 + 2)
172 buf.writeUInt8(253, 0)
173 buf.writeUInt16LE(n, 1)
174 } else if (n < 0x100000000) {
175 buf = Buffer.alloc(1 + 4)
176 buf.writeUInt8(254, 0)
177 buf.writeUInt32LE(n, 1)
178 } else {
179 const bw = new Bw()
180 bw.writeUInt8(255)
181 bw.writeUInt64LEBn(bn)
182 buf = bw.toBuffer()
183 }
184 return buf
185 }
186}
187
188export { Bw }