bl 0.9.4 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.travis.yml +5 -3
- package/README.md +2 -0
- package/bl.js +5 -0
- package/package.json +4 -6
- package/test/test.js +554 -6
- package/test/basic-test.js +0 -541
- package/test/sauce.js +0 -38
package/.travis.yml
CHANGED
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# bl *(BufferList)*
|
|
2
2
|
|
|
3
|
+
[](https://travis-ci.org/rvagg/bl)
|
|
4
|
+
|
|
3
5
|
**A Node.js Buffer list collector, reader and streamer thingy.**
|
|
4
6
|
|
|
5
7
|
[](https://nodei.co/npm/bl/)
|
package/bl.js
CHANGED
|
@@ -52,6 +52,11 @@ BufferList.prototype.append = function (buf) {
|
|
|
52
52
|
var isBuffer = Buffer.isBuffer(buf) ||
|
|
53
53
|
buf instanceof BufferList
|
|
54
54
|
|
|
55
|
+
// coerce number arguments to strings, since Buffer(number) does
|
|
56
|
+
// uninitialized memory allocation
|
|
57
|
+
if (typeof buf == 'number')
|
|
58
|
+
buf = buf.toString()
|
|
59
|
+
|
|
55
60
|
this._bufs.push(isBuffer ? buf : new Buffer(buf))
|
|
56
61
|
this.length += buf.length
|
|
57
62
|
return this
|
package/package.json
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bl",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!",
|
|
5
5
|
"main": "bl.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node test/test.js | faucet"
|
|
8
|
-
"test-local": "brtapsauce-local test/basic-test.js"
|
|
7
|
+
"test": "node test/test.js | faucet"
|
|
9
8
|
},
|
|
10
9
|
"repository": {
|
|
11
10
|
"type": "git",
|
|
@@ -25,12 +24,11 @@
|
|
|
25
24
|
],
|
|
26
25
|
"license": "MIT",
|
|
27
26
|
"dependencies": {
|
|
28
|
-
"readable-stream": "~
|
|
27
|
+
"readable-stream": "~2.0.5"
|
|
29
28
|
},
|
|
30
29
|
"devDependencies": {
|
|
31
30
|
"tape": "~2.12.3",
|
|
32
31
|
"hash_file": "~0.1.1",
|
|
33
|
-
"faucet": "~0.0.1"
|
|
34
|
-
"brtapsauce": "~0.3.0"
|
|
32
|
+
"faucet": "~0.0.1"
|
|
35
33
|
}
|
|
36
34
|
}
|
package/test/test.js
CHANGED
|
@@ -1,9 +1,557 @@
|
|
|
1
|
-
require('
|
|
1
|
+
var tape = require('tape')
|
|
2
|
+
, crypto = require('crypto')
|
|
3
|
+
, fs = require('fs')
|
|
4
|
+
, hash = require('hash_file')
|
|
5
|
+
, BufferList = require('../')
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
|
|
7
|
+
, encodings =
|
|
8
|
+
('hex utf8 utf-8 ascii binary base64'
|
|
9
|
+
+ (process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')
|
|
5
10
|
|
|
6
|
-
|
|
7
|
-
|
|
11
|
+
tape('single bytes from single buffer', function (t) {
|
|
12
|
+
var bl = new BufferList()
|
|
13
|
+
bl.append(new Buffer('abcd'))
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
t.equal(bl.length, 4)
|
|
16
|
+
|
|
17
|
+
t.equal(bl.get(0), 97)
|
|
18
|
+
t.equal(bl.get(1), 98)
|
|
19
|
+
t.equal(bl.get(2), 99)
|
|
20
|
+
t.equal(bl.get(3), 100)
|
|
21
|
+
|
|
22
|
+
t.end()
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
tape('single bytes from multiple buffers', function (t) {
|
|
26
|
+
var bl = new BufferList()
|
|
27
|
+
bl.append(new Buffer('abcd'))
|
|
28
|
+
bl.append(new Buffer('efg'))
|
|
29
|
+
bl.append(new Buffer('hi'))
|
|
30
|
+
bl.append(new Buffer('j'))
|
|
31
|
+
|
|
32
|
+
t.equal(bl.length, 10)
|
|
33
|
+
|
|
34
|
+
t.equal(bl.get(0), 97)
|
|
35
|
+
t.equal(bl.get(1), 98)
|
|
36
|
+
t.equal(bl.get(2), 99)
|
|
37
|
+
t.equal(bl.get(3), 100)
|
|
38
|
+
t.equal(bl.get(4), 101)
|
|
39
|
+
t.equal(bl.get(5), 102)
|
|
40
|
+
t.equal(bl.get(6), 103)
|
|
41
|
+
t.equal(bl.get(7), 104)
|
|
42
|
+
t.equal(bl.get(8), 105)
|
|
43
|
+
t.equal(bl.get(9), 106)
|
|
44
|
+
t.end()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
tape('multi bytes from single buffer', function (t) {
|
|
48
|
+
var bl = new BufferList()
|
|
49
|
+
bl.append(new Buffer('abcd'))
|
|
50
|
+
|
|
51
|
+
t.equal(bl.length, 4)
|
|
52
|
+
|
|
53
|
+
t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
|
|
54
|
+
t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
|
|
55
|
+
t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
|
|
56
|
+
|
|
57
|
+
t.end()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
tape('multiple bytes from multiple buffers', function (t) {
|
|
61
|
+
var bl = new BufferList()
|
|
62
|
+
|
|
63
|
+
bl.append(new Buffer('abcd'))
|
|
64
|
+
bl.append(new Buffer('efg'))
|
|
65
|
+
bl.append(new Buffer('hi'))
|
|
66
|
+
bl.append(new Buffer('j'))
|
|
67
|
+
|
|
68
|
+
t.equal(bl.length, 10)
|
|
69
|
+
|
|
70
|
+
t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
|
|
71
|
+
t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
|
|
72
|
+
t.equal(bl.slice(3, 6).toString('ascii'), 'def')
|
|
73
|
+
t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
|
|
74
|
+
t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
|
|
75
|
+
|
|
76
|
+
t.end()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
tape('multiple bytes from multiple buffer lists', function (t) {
|
|
80
|
+
var bl = new BufferList()
|
|
81
|
+
|
|
82
|
+
bl.append(new BufferList([new Buffer('abcd'), new Buffer('efg')]))
|
|
83
|
+
bl.append(new BufferList([new Buffer('hi'), new Buffer('j')]))
|
|
84
|
+
|
|
85
|
+
t.equal(bl.length, 10)
|
|
86
|
+
|
|
87
|
+
t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
|
|
88
|
+
t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
|
|
89
|
+
t.equal(bl.slice(3, 6).toString('ascii'), 'def')
|
|
90
|
+
t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
|
|
91
|
+
t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
|
|
92
|
+
|
|
93
|
+
t.end()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
tape('consuming from multiple buffers', function (t) {
|
|
97
|
+
var bl = new BufferList()
|
|
98
|
+
|
|
99
|
+
bl.append(new Buffer('abcd'))
|
|
100
|
+
bl.append(new Buffer('efg'))
|
|
101
|
+
bl.append(new Buffer('hi'))
|
|
102
|
+
bl.append(new Buffer('j'))
|
|
103
|
+
|
|
104
|
+
t.equal(bl.length, 10)
|
|
105
|
+
|
|
106
|
+
t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
|
|
107
|
+
|
|
108
|
+
bl.consume(3)
|
|
109
|
+
t.equal(bl.length, 7)
|
|
110
|
+
t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')
|
|
111
|
+
|
|
112
|
+
bl.consume(2)
|
|
113
|
+
t.equal(bl.length, 5)
|
|
114
|
+
t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')
|
|
115
|
+
|
|
116
|
+
bl.consume(1)
|
|
117
|
+
t.equal(bl.length, 4)
|
|
118
|
+
t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')
|
|
119
|
+
|
|
120
|
+
bl.consume(1)
|
|
121
|
+
t.equal(bl.length, 3)
|
|
122
|
+
t.equal(bl.slice(0, 3).toString('ascii'), 'hij')
|
|
123
|
+
|
|
124
|
+
bl.consume(2)
|
|
125
|
+
t.equal(bl.length, 1)
|
|
126
|
+
t.equal(bl.slice(0, 1).toString('ascii'), 'j')
|
|
127
|
+
|
|
128
|
+
t.end()
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
tape('test readUInt8 / readInt8', function (t) {
|
|
132
|
+
var buf1 = new Buffer(1)
|
|
133
|
+
, buf2 = new Buffer(3)
|
|
134
|
+
, buf3 = new Buffer(3)
|
|
135
|
+
, bl = new BufferList()
|
|
136
|
+
|
|
137
|
+
buf2[1] = 0x3
|
|
138
|
+
buf2[2] = 0x4
|
|
139
|
+
buf3[0] = 0x23
|
|
140
|
+
buf3[1] = 0x42
|
|
141
|
+
|
|
142
|
+
bl.append(buf1)
|
|
143
|
+
bl.append(buf2)
|
|
144
|
+
bl.append(buf3)
|
|
145
|
+
|
|
146
|
+
t.equal(bl.readUInt8(2), 0x3)
|
|
147
|
+
t.equal(bl.readInt8(2), 0x3)
|
|
148
|
+
t.equal(bl.readUInt8(3), 0x4)
|
|
149
|
+
t.equal(bl.readInt8(3), 0x4)
|
|
150
|
+
t.equal(bl.readUInt8(4), 0x23)
|
|
151
|
+
t.equal(bl.readInt8(4), 0x23)
|
|
152
|
+
t.equal(bl.readUInt8(5), 0x42)
|
|
153
|
+
t.equal(bl.readInt8(5), 0x42)
|
|
154
|
+
t.end()
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
|
|
158
|
+
var buf1 = new Buffer(1)
|
|
159
|
+
, buf2 = new Buffer(3)
|
|
160
|
+
, buf3 = new Buffer(3)
|
|
161
|
+
, bl = new BufferList()
|
|
162
|
+
|
|
163
|
+
buf2[1] = 0x3
|
|
164
|
+
buf2[2] = 0x4
|
|
165
|
+
buf3[0] = 0x23
|
|
166
|
+
buf3[1] = 0x42
|
|
167
|
+
|
|
168
|
+
bl.append(buf1)
|
|
169
|
+
bl.append(buf2)
|
|
170
|
+
bl.append(buf3)
|
|
171
|
+
|
|
172
|
+
t.equal(bl.readUInt16BE(2), 0x0304)
|
|
173
|
+
t.equal(bl.readUInt16LE(2), 0x0403)
|
|
174
|
+
t.equal(bl.readInt16BE(2), 0x0304)
|
|
175
|
+
t.equal(bl.readInt16LE(2), 0x0403)
|
|
176
|
+
t.equal(bl.readUInt16BE(3), 0x0423)
|
|
177
|
+
t.equal(bl.readUInt16LE(3), 0x2304)
|
|
178
|
+
t.equal(bl.readInt16BE(3), 0x0423)
|
|
179
|
+
t.equal(bl.readInt16LE(3), 0x2304)
|
|
180
|
+
t.equal(bl.readUInt16BE(4), 0x2342)
|
|
181
|
+
t.equal(bl.readUInt16LE(4), 0x4223)
|
|
182
|
+
t.equal(bl.readInt16BE(4), 0x2342)
|
|
183
|
+
t.equal(bl.readInt16LE(4), 0x4223)
|
|
184
|
+
t.end()
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
|
|
188
|
+
var buf1 = new Buffer(1)
|
|
189
|
+
, buf2 = new Buffer(3)
|
|
190
|
+
, buf3 = new Buffer(3)
|
|
191
|
+
, bl = new BufferList()
|
|
192
|
+
|
|
193
|
+
buf2[1] = 0x3
|
|
194
|
+
buf2[2] = 0x4
|
|
195
|
+
buf3[0] = 0x23
|
|
196
|
+
buf3[1] = 0x42
|
|
197
|
+
|
|
198
|
+
bl.append(buf1)
|
|
199
|
+
bl.append(buf2)
|
|
200
|
+
bl.append(buf3)
|
|
201
|
+
|
|
202
|
+
t.equal(bl.readUInt32BE(2), 0x03042342)
|
|
203
|
+
t.equal(bl.readUInt32LE(2), 0x42230403)
|
|
204
|
+
t.equal(bl.readInt32BE(2), 0x03042342)
|
|
205
|
+
t.equal(bl.readInt32LE(2), 0x42230403)
|
|
206
|
+
t.end()
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
tape('test readFloatLE / readFloatBE', function (t) {
|
|
210
|
+
var buf1 = new Buffer(1)
|
|
211
|
+
, buf2 = new Buffer(3)
|
|
212
|
+
, buf3 = new Buffer(3)
|
|
213
|
+
, bl = new BufferList()
|
|
214
|
+
|
|
215
|
+
buf2[1] = 0x00
|
|
216
|
+
buf2[2] = 0x00
|
|
217
|
+
buf3[0] = 0x80
|
|
218
|
+
buf3[1] = 0x3f
|
|
219
|
+
|
|
220
|
+
bl.append(buf1)
|
|
221
|
+
bl.append(buf2)
|
|
222
|
+
bl.append(buf3)
|
|
223
|
+
|
|
224
|
+
t.equal(bl.readFloatLE(2), 0x01)
|
|
225
|
+
t.end()
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
229
|
+
var buf1 = new Buffer(1)
|
|
230
|
+
, buf2 = new Buffer(3)
|
|
231
|
+
, buf3 = new Buffer(10)
|
|
232
|
+
, bl = new BufferList()
|
|
233
|
+
|
|
234
|
+
buf2[1] = 0x55
|
|
235
|
+
buf2[2] = 0x55
|
|
236
|
+
buf3[0] = 0x55
|
|
237
|
+
buf3[1] = 0x55
|
|
238
|
+
buf3[2] = 0x55
|
|
239
|
+
buf3[3] = 0x55
|
|
240
|
+
buf3[4] = 0xd5
|
|
241
|
+
buf3[5] = 0x3f
|
|
242
|
+
|
|
243
|
+
bl.append(buf1)
|
|
244
|
+
bl.append(buf2)
|
|
245
|
+
bl.append(buf3)
|
|
246
|
+
|
|
247
|
+
t.equal(bl.readDoubleLE(2), 0.3333333333333333)
|
|
248
|
+
t.end()
|
|
249
|
+
})
|
|
250
|
+
|
|
251
|
+
tape('test toString', function (t) {
|
|
252
|
+
var bl = new BufferList()
|
|
253
|
+
|
|
254
|
+
bl.append(new Buffer('abcd'))
|
|
255
|
+
bl.append(new Buffer('efg'))
|
|
256
|
+
bl.append(new Buffer('hi'))
|
|
257
|
+
bl.append(new Buffer('j'))
|
|
258
|
+
|
|
259
|
+
t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
|
|
260
|
+
t.equal(bl.toString('ascii', 3, 10), 'defghij')
|
|
261
|
+
t.equal(bl.toString('ascii', 3, 6), 'def')
|
|
262
|
+
t.equal(bl.toString('ascii', 3, 8), 'defgh')
|
|
263
|
+
t.equal(bl.toString('ascii', 5, 10), 'fghij')
|
|
264
|
+
|
|
265
|
+
t.end()
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
tape('test toString encoding', function (t) {
|
|
269
|
+
var bl = new BufferList()
|
|
270
|
+
, b = new Buffer('abcdefghij\xff\x00')
|
|
271
|
+
|
|
272
|
+
bl.append(new Buffer('abcd'))
|
|
273
|
+
bl.append(new Buffer('efg'))
|
|
274
|
+
bl.append(new Buffer('hi'))
|
|
275
|
+
bl.append(new Buffer('j'))
|
|
276
|
+
bl.append(new Buffer('\xff\x00'))
|
|
277
|
+
|
|
278
|
+
encodings.forEach(function (enc) {
|
|
279
|
+
t.equal(bl.toString(enc), b.toString(enc), enc)
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
t.end()
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
!process.browser && tape('test stream', function (t) {
|
|
286
|
+
var random = crypto.randomBytes(65534)
|
|
287
|
+
, rndhash = hash(random, 'md5')
|
|
288
|
+
, md5sum = crypto.createHash('md5')
|
|
289
|
+
, bl = new BufferList(function (err, buf) {
|
|
290
|
+
t.ok(Buffer.isBuffer(buf))
|
|
291
|
+
t.ok(err === null)
|
|
292
|
+
t.equal(rndhash, hash(bl.slice(), 'md5'))
|
|
293
|
+
t.equal(rndhash, hash(buf, 'md5'))
|
|
294
|
+
|
|
295
|
+
bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
|
|
296
|
+
.on('close', function () {
|
|
297
|
+
var s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
|
|
298
|
+
s.on('data', md5sum.update.bind(md5sum))
|
|
299
|
+
s.on('end', function() {
|
|
300
|
+
t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
|
|
301
|
+
t.end()
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
|
|
308
|
+
fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
tape('instantiation with Buffer', function (t) {
|
|
312
|
+
var buf = crypto.randomBytes(1024)
|
|
313
|
+
, buf2 = crypto.randomBytes(1024)
|
|
314
|
+
, b = BufferList(buf)
|
|
315
|
+
|
|
316
|
+
t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
|
|
317
|
+
b = BufferList([ buf, buf2 ])
|
|
318
|
+
t.equal(b.slice().toString('hex'), Buffer.concat([ buf, buf2 ]).toString('hex'), 'same buffer')
|
|
319
|
+
t.end()
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
tape('test String appendage', function (t) {
|
|
323
|
+
var bl = new BufferList()
|
|
324
|
+
, b = new Buffer('abcdefghij\xff\x00')
|
|
325
|
+
|
|
326
|
+
bl.append('abcd')
|
|
327
|
+
bl.append('efg')
|
|
328
|
+
bl.append('hi')
|
|
329
|
+
bl.append('j')
|
|
330
|
+
bl.append('\xff\x00')
|
|
331
|
+
|
|
332
|
+
encodings.forEach(function (enc) {
|
|
333
|
+
t.equal(bl.toString(enc), b.toString(enc))
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
t.end()
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
tape('test Number appendage', function (t) {
|
|
340
|
+
var bl = new BufferList()
|
|
341
|
+
, b = new Buffer('1234567890')
|
|
342
|
+
|
|
343
|
+
bl.append(1234)
|
|
344
|
+
bl.append(567)
|
|
345
|
+
bl.append(89)
|
|
346
|
+
bl.append(0)
|
|
347
|
+
|
|
348
|
+
encodings.forEach(function (enc) {
|
|
349
|
+
t.equal(bl.toString(enc), b.toString(enc))
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
t.end()
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
tape('write nothing, should get empty buffer', function (t) {
|
|
356
|
+
t.plan(3)
|
|
357
|
+
BufferList(function (err, data) {
|
|
358
|
+
t.notOk(err, 'no error')
|
|
359
|
+
t.ok(Buffer.isBuffer(data), 'got a buffer')
|
|
360
|
+
t.equal(0, data.length, 'got a zero-length buffer')
|
|
361
|
+
t.end()
|
|
362
|
+
}).end()
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
tape('unicode string', function (t) {
|
|
366
|
+
t.plan(2)
|
|
367
|
+
var inp1 = '\u2600'
|
|
368
|
+
, inp2 = '\u2603'
|
|
369
|
+
, exp = inp1 + ' and ' + inp2
|
|
370
|
+
, bl = BufferList()
|
|
371
|
+
bl.write(inp1)
|
|
372
|
+
bl.write(' and ')
|
|
373
|
+
bl.write(inp2)
|
|
374
|
+
t.equal(exp, bl.toString())
|
|
375
|
+
t.equal(new Buffer(exp).toString('hex'), bl.toString('hex'))
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
tape('should emit finish', function (t) {
|
|
379
|
+
var source = BufferList()
|
|
380
|
+
, dest = BufferList()
|
|
381
|
+
|
|
382
|
+
source.write('hello')
|
|
383
|
+
source.pipe(dest)
|
|
384
|
+
|
|
385
|
+
dest.on('finish', function () {
|
|
386
|
+
t.equal(dest.toString('utf8'), 'hello')
|
|
387
|
+
t.end()
|
|
388
|
+
})
|
|
389
|
+
})
|
|
390
|
+
|
|
391
|
+
tape('basic copy', function (t) {
|
|
392
|
+
var buf = crypto.randomBytes(1024)
|
|
393
|
+
, buf2 = new Buffer(1024)
|
|
394
|
+
, b = BufferList(buf)
|
|
395
|
+
|
|
396
|
+
b.copy(buf2)
|
|
397
|
+
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
398
|
+
t.end()
|
|
399
|
+
})
|
|
400
|
+
|
|
401
|
+
tape('copy after many appends', function (t) {
|
|
402
|
+
var buf = crypto.randomBytes(512)
|
|
403
|
+
, buf2 = new Buffer(1024)
|
|
404
|
+
, b = BufferList(buf)
|
|
405
|
+
|
|
406
|
+
b.append(buf)
|
|
407
|
+
b.copy(buf2)
|
|
408
|
+
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
409
|
+
t.end()
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
tape('copy at a precise position', function (t) {
|
|
413
|
+
var buf = crypto.randomBytes(1004)
|
|
414
|
+
, buf2 = new Buffer(1024)
|
|
415
|
+
, b = BufferList(buf)
|
|
416
|
+
|
|
417
|
+
b.copy(buf2, 20)
|
|
418
|
+
t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
|
|
419
|
+
t.end()
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
tape('copy starting from a precise location', function (t) {
|
|
423
|
+
var buf = crypto.randomBytes(10)
|
|
424
|
+
, buf2 = new Buffer(5)
|
|
425
|
+
, b = BufferList(buf)
|
|
426
|
+
|
|
427
|
+
b.copy(buf2, 0, 5)
|
|
428
|
+
t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
429
|
+
t.end()
|
|
430
|
+
})
|
|
431
|
+
|
|
432
|
+
tape('copy in an interval', function (t) {
|
|
433
|
+
var rnd = crypto.randomBytes(10)
|
|
434
|
+
, b = BufferList(rnd) // put the random bytes there
|
|
435
|
+
, actual = new Buffer(3)
|
|
436
|
+
, expected = new Buffer(3)
|
|
437
|
+
|
|
438
|
+
rnd.copy(expected, 0, 5, 8)
|
|
439
|
+
b.copy(actual, 0, 5, 8)
|
|
440
|
+
|
|
441
|
+
t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
|
|
442
|
+
t.end()
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
tape('copy an interval between two buffers', function (t) {
|
|
446
|
+
var buf = crypto.randomBytes(10)
|
|
447
|
+
, buf2 = new Buffer(10)
|
|
448
|
+
, b = BufferList(buf)
|
|
449
|
+
|
|
450
|
+
b.append(buf)
|
|
451
|
+
b.copy(buf2, 0, 5, 15)
|
|
452
|
+
|
|
453
|
+
t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
454
|
+
t.end()
|
|
455
|
+
})
|
|
456
|
+
|
|
457
|
+
tape('duplicate', function (t) {
|
|
458
|
+
t.plan(2)
|
|
459
|
+
|
|
460
|
+
var bl = new BufferList('abcdefghij\xff\x00')
|
|
461
|
+
, dup = bl.duplicate()
|
|
462
|
+
|
|
463
|
+
t.equal(bl.prototype, dup.prototype)
|
|
464
|
+
t.equal(bl.toString('hex'), dup.toString('hex'))
|
|
465
|
+
})
|
|
466
|
+
|
|
467
|
+
tape('destroy no pipe', function (t) {
|
|
468
|
+
t.plan(2)
|
|
469
|
+
|
|
470
|
+
var bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
471
|
+
bl.destroy()
|
|
472
|
+
|
|
473
|
+
t.equal(bl._bufs.length, 0)
|
|
474
|
+
t.equal(bl.length, 0)
|
|
475
|
+
})
|
|
476
|
+
|
|
477
|
+
!process.browser && tape('destroy with pipe before read end', function (t) {
|
|
478
|
+
t.plan(2)
|
|
479
|
+
|
|
480
|
+
var bl = new BufferList()
|
|
481
|
+
fs.createReadStream(__dirname + '/test.js')
|
|
482
|
+
.pipe(bl)
|
|
483
|
+
|
|
484
|
+
bl.destroy()
|
|
485
|
+
|
|
486
|
+
t.equal(bl._bufs.length, 0)
|
|
487
|
+
t.equal(bl.length, 0)
|
|
488
|
+
|
|
489
|
+
})
|
|
490
|
+
|
|
491
|
+
!process.browser && tape('destroy with pipe before read end with race', function (t) {
|
|
492
|
+
t.plan(2)
|
|
493
|
+
|
|
494
|
+
var bl = new BufferList()
|
|
495
|
+
fs.createReadStream(__dirname + '/test.js')
|
|
496
|
+
.pipe(bl)
|
|
497
|
+
|
|
498
|
+
setTimeout(function () {
|
|
499
|
+
bl.destroy()
|
|
500
|
+
setTimeout(function () {
|
|
501
|
+
t.equal(bl._bufs.length, 0)
|
|
502
|
+
t.equal(bl.length, 0)
|
|
503
|
+
}, 500)
|
|
504
|
+
}, 500)
|
|
505
|
+
})
|
|
506
|
+
|
|
507
|
+
!process.browser && tape('destroy with pipe after read end', function (t) {
|
|
508
|
+
t.plan(2)
|
|
509
|
+
|
|
510
|
+
var bl = new BufferList()
|
|
511
|
+
fs.createReadStream(__dirname + '/test.js')
|
|
512
|
+
.on('end', onEnd)
|
|
513
|
+
.pipe(bl)
|
|
514
|
+
|
|
515
|
+
function onEnd () {
|
|
516
|
+
bl.destroy()
|
|
517
|
+
|
|
518
|
+
t.equal(bl._bufs.length, 0)
|
|
519
|
+
t.equal(bl.length, 0)
|
|
520
|
+
}
|
|
521
|
+
})
|
|
522
|
+
|
|
523
|
+
!process.browser && tape('destroy with pipe while writing to a destination', function (t) {
|
|
524
|
+
t.plan(4)
|
|
525
|
+
|
|
526
|
+
var bl = new BufferList()
|
|
527
|
+
, ds = new BufferList()
|
|
528
|
+
|
|
529
|
+
fs.createReadStream(__dirname + '/test.js')
|
|
530
|
+
.on('end', onEnd)
|
|
531
|
+
.pipe(bl)
|
|
532
|
+
|
|
533
|
+
function onEnd () {
|
|
534
|
+
bl.pipe(ds)
|
|
535
|
+
|
|
536
|
+
setTimeout(function () {
|
|
537
|
+
bl.destroy()
|
|
538
|
+
|
|
539
|
+
t.equals(bl._bufs.length, 0)
|
|
540
|
+
t.equals(bl.length, 0)
|
|
541
|
+
|
|
542
|
+
ds.destroy()
|
|
543
|
+
|
|
544
|
+
t.equals(bl._bufs.length, 0)
|
|
545
|
+
t.equals(bl.length, 0)
|
|
546
|
+
|
|
547
|
+
}, 100)
|
|
548
|
+
}
|
|
549
|
+
})
|
|
550
|
+
|
|
551
|
+
!process.browser && tape('handle error', function (t) {
|
|
552
|
+
t.plan(2)
|
|
553
|
+
fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
|
|
554
|
+
t.ok(err instanceof Error, 'has error')
|
|
555
|
+
t.notOk(data, 'no data')
|
|
556
|
+
}))
|
|
557
|
+
})
|
package/test/basic-test.js
DELETED
|
@@ -1,541 +0,0 @@
|
|
|
1
|
-
var tape = require('tape')
|
|
2
|
-
, crypto = require('crypto')
|
|
3
|
-
, fs = require('fs')
|
|
4
|
-
, hash = require('hash_file')
|
|
5
|
-
, BufferList = require('../')
|
|
6
|
-
|
|
7
|
-
, encodings =
|
|
8
|
-
('hex utf8 utf-8 ascii binary base64'
|
|
9
|
-
+ (process.browser ? '' : ' ucs2 ucs-2 utf16le utf-16le')).split(' ')
|
|
10
|
-
|
|
11
|
-
tape('single bytes from single buffer', function (t) {
|
|
12
|
-
var bl = new BufferList()
|
|
13
|
-
bl.append(new Buffer('abcd'))
|
|
14
|
-
|
|
15
|
-
t.equal(bl.length, 4)
|
|
16
|
-
|
|
17
|
-
t.equal(bl.get(0), 97)
|
|
18
|
-
t.equal(bl.get(1), 98)
|
|
19
|
-
t.equal(bl.get(2), 99)
|
|
20
|
-
t.equal(bl.get(3), 100)
|
|
21
|
-
|
|
22
|
-
t.end()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
tape('single bytes from multiple buffers', function (t) {
|
|
26
|
-
var bl = new BufferList()
|
|
27
|
-
bl.append(new Buffer('abcd'))
|
|
28
|
-
bl.append(new Buffer('efg'))
|
|
29
|
-
bl.append(new Buffer('hi'))
|
|
30
|
-
bl.append(new Buffer('j'))
|
|
31
|
-
|
|
32
|
-
t.equal(bl.length, 10)
|
|
33
|
-
|
|
34
|
-
t.equal(bl.get(0), 97)
|
|
35
|
-
t.equal(bl.get(1), 98)
|
|
36
|
-
t.equal(bl.get(2), 99)
|
|
37
|
-
t.equal(bl.get(3), 100)
|
|
38
|
-
t.equal(bl.get(4), 101)
|
|
39
|
-
t.equal(bl.get(5), 102)
|
|
40
|
-
t.equal(bl.get(6), 103)
|
|
41
|
-
t.equal(bl.get(7), 104)
|
|
42
|
-
t.equal(bl.get(8), 105)
|
|
43
|
-
t.equal(bl.get(9), 106)
|
|
44
|
-
t.end()
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
tape('multi bytes from single buffer', function (t) {
|
|
48
|
-
var bl = new BufferList()
|
|
49
|
-
bl.append(new Buffer('abcd'))
|
|
50
|
-
|
|
51
|
-
t.equal(bl.length, 4)
|
|
52
|
-
|
|
53
|
-
t.equal(bl.slice(0, 4).toString('ascii'), 'abcd')
|
|
54
|
-
t.equal(bl.slice(0, 3).toString('ascii'), 'abc')
|
|
55
|
-
t.equal(bl.slice(1, 4).toString('ascii'), 'bcd')
|
|
56
|
-
|
|
57
|
-
t.end()
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
tape('multiple bytes from multiple buffers', function (t) {
|
|
61
|
-
var bl = new BufferList()
|
|
62
|
-
|
|
63
|
-
bl.append(new Buffer('abcd'))
|
|
64
|
-
bl.append(new Buffer('efg'))
|
|
65
|
-
bl.append(new Buffer('hi'))
|
|
66
|
-
bl.append(new Buffer('j'))
|
|
67
|
-
|
|
68
|
-
t.equal(bl.length, 10)
|
|
69
|
-
|
|
70
|
-
t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
|
|
71
|
-
t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
|
|
72
|
-
t.equal(bl.slice(3, 6).toString('ascii'), 'def')
|
|
73
|
-
t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
|
|
74
|
-
t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
|
|
75
|
-
|
|
76
|
-
t.end()
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
tape('multiple bytes from multiple buffer lists', function (t) {
|
|
80
|
-
var bl = new BufferList()
|
|
81
|
-
|
|
82
|
-
bl.append(new BufferList([new Buffer('abcd'), new Buffer('efg')]))
|
|
83
|
-
bl.append(new BufferList([new Buffer('hi'), new Buffer('j')]))
|
|
84
|
-
|
|
85
|
-
t.equal(bl.length, 10)
|
|
86
|
-
|
|
87
|
-
t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
|
|
88
|
-
t.equal(bl.slice(3, 10).toString('ascii'), 'defghij')
|
|
89
|
-
t.equal(bl.slice(3, 6).toString('ascii'), 'def')
|
|
90
|
-
t.equal(bl.slice(3, 8).toString('ascii'), 'defgh')
|
|
91
|
-
t.equal(bl.slice(5, 10).toString('ascii'), 'fghij')
|
|
92
|
-
|
|
93
|
-
t.end()
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
tape('consuming from multiple buffers', function (t) {
|
|
97
|
-
var bl = new BufferList()
|
|
98
|
-
|
|
99
|
-
bl.append(new Buffer('abcd'))
|
|
100
|
-
bl.append(new Buffer('efg'))
|
|
101
|
-
bl.append(new Buffer('hi'))
|
|
102
|
-
bl.append(new Buffer('j'))
|
|
103
|
-
|
|
104
|
-
t.equal(bl.length, 10)
|
|
105
|
-
|
|
106
|
-
t.equal(bl.slice(0, 10).toString('ascii'), 'abcdefghij')
|
|
107
|
-
|
|
108
|
-
bl.consume(3)
|
|
109
|
-
t.equal(bl.length, 7)
|
|
110
|
-
t.equal(bl.slice(0, 7).toString('ascii'), 'defghij')
|
|
111
|
-
|
|
112
|
-
bl.consume(2)
|
|
113
|
-
t.equal(bl.length, 5)
|
|
114
|
-
t.equal(bl.slice(0, 5).toString('ascii'), 'fghij')
|
|
115
|
-
|
|
116
|
-
bl.consume(1)
|
|
117
|
-
t.equal(bl.length, 4)
|
|
118
|
-
t.equal(bl.slice(0, 4).toString('ascii'), 'ghij')
|
|
119
|
-
|
|
120
|
-
bl.consume(1)
|
|
121
|
-
t.equal(bl.length, 3)
|
|
122
|
-
t.equal(bl.slice(0, 3).toString('ascii'), 'hij')
|
|
123
|
-
|
|
124
|
-
bl.consume(2)
|
|
125
|
-
t.equal(bl.length, 1)
|
|
126
|
-
t.equal(bl.slice(0, 1).toString('ascii'), 'j')
|
|
127
|
-
|
|
128
|
-
t.end()
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
tape('test readUInt8 / readInt8', function (t) {
|
|
132
|
-
var buf1 = new Buffer(1)
|
|
133
|
-
, buf2 = new Buffer(3)
|
|
134
|
-
, buf3 = new Buffer(3)
|
|
135
|
-
, bl = new BufferList()
|
|
136
|
-
|
|
137
|
-
buf2[1] = 0x3
|
|
138
|
-
buf2[2] = 0x4
|
|
139
|
-
buf3[0] = 0x23
|
|
140
|
-
buf3[1] = 0x42
|
|
141
|
-
|
|
142
|
-
bl.append(buf1)
|
|
143
|
-
bl.append(buf2)
|
|
144
|
-
bl.append(buf3)
|
|
145
|
-
|
|
146
|
-
t.equal(bl.readUInt8(2), 0x3)
|
|
147
|
-
t.equal(bl.readInt8(2), 0x3)
|
|
148
|
-
t.equal(bl.readUInt8(3), 0x4)
|
|
149
|
-
t.equal(bl.readInt8(3), 0x4)
|
|
150
|
-
t.equal(bl.readUInt8(4), 0x23)
|
|
151
|
-
t.equal(bl.readInt8(4), 0x23)
|
|
152
|
-
t.equal(bl.readUInt8(5), 0x42)
|
|
153
|
-
t.equal(bl.readInt8(5), 0x42)
|
|
154
|
-
t.end()
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
tape('test readUInt16LE / readUInt16BE / readInt16LE / readInt16BE', function (t) {
|
|
158
|
-
var buf1 = new Buffer(1)
|
|
159
|
-
, buf2 = new Buffer(3)
|
|
160
|
-
, buf3 = new Buffer(3)
|
|
161
|
-
, bl = new BufferList()
|
|
162
|
-
|
|
163
|
-
buf2[1] = 0x3
|
|
164
|
-
buf2[2] = 0x4
|
|
165
|
-
buf3[0] = 0x23
|
|
166
|
-
buf3[1] = 0x42
|
|
167
|
-
|
|
168
|
-
bl.append(buf1)
|
|
169
|
-
bl.append(buf2)
|
|
170
|
-
bl.append(buf3)
|
|
171
|
-
|
|
172
|
-
t.equal(bl.readUInt16BE(2), 0x0304)
|
|
173
|
-
t.equal(bl.readUInt16LE(2), 0x0403)
|
|
174
|
-
t.equal(bl.readInt16BE(2), 0x0304)
|
|
175
|
-
t.equal(bl.readInt16LE(2), 0x0403)
|
|
176
|
-
t.equal(bl.readUInt16BE(3), 0x0423)
|
|
177
|
-
t.equal(bl.readUInt16LE(3), 0x2304)
|
|
178
|
-
t.equal(bl.readInt16BE(3), 0x0423)
|
|
179
|
-
t.equal(bl.readInt16LE(3), 0x2304)
|
|
180
|
-
t.equal(bl.readUInt16BE(4), 0x2342)
|
|
181
|
-
t.equal(bl.readUInt16LE(4), 0x4223)
|
|
182
|
-
t.equal(bl.readInt16BE(4), 0x2342)
|
|
183
|
-
t.equal(bl.readInt16LE(4), 0x4223)
|
|
184
|
-
t.end()
|
|
185
|
-
})
|
|
186
|
-
|
|
187
|
-
tape('test readUInt32LE / readUInt32BE / readInt32LE / readInt32BE', function (t) {
|
|
188
|
-
var buf1 = new Buffer(1)
|
|
189
|
-
, buf2 = new Buffer(3)
|
|
190
|
-
, buf3 = new Buffer(3)
|
|
191
|
-
, bl = new BufferList()
|
|
192
|
-
|
|
193
|
-
buf2[1] = 0x3
|
|
194
|
-
buf2[2] = 0x4
|
|
195
|
-
buf3[0] = 0x23
|
|
196
|
-
buf3[1] = 0x42
|
|
197
|
-
|
|
198
|
-
bl.append(buf1)
|
|
199
|
-
bl.append(buf2)
|
|
200
|
-
bl.append(buf3)
|
|
201
|
-
|
|
202
|
-
t.equal(bl.readUInt32BE(2), 0x03042342)
|
|
203
|
-
t.equal(bl.readUInt32LE(2), 0x42230403)
|
|
204
|
-
t.equal(bl.readInt32BE(2), 0x03042342)
|
|
205
|
-
t.equal(bl.readInt32LE(2), 0x42230403)
|
|
206
|
-
t.end()
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
tape('test readFloatLE / readFloatBE', function (t) {
|
|
210
|
-
var buf1 = new Buffer(1)
|
|
211
|
-
, buf2 = new Buffer(3)
|
|
212
|
-
, buf3 = new Buffer(3)
|
|
213
|
-
, bl = new BufferList()
|
|
214
|
-
|
|
215
|
-
buf2[1] = 0x00
|
|
216
|
-
buf2[2] = 0x00
|
|
217
|
-
buf3[0] = 0x80
|
|
218
|
-
buf3[1] = 0x3f
|
|
219
|
-
|
|
220
|
-
bl.append(buf1)
|
|
221
|
-
bl.append(buf2)
|
|
222
|
-
bl.append(buf3)
|
|
223
|
-
|
|
224
|
-
t.equal(bl.readFloatLE(2), 0x01)
|
|
225
|
-
t.end()
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
tape('test readDoubleLE / readDoubleBE', function (t) {
|
|
229
|
-
var buf1 = new Buffer(1)
|
|
230
|
-
, buf2 = new Buffer(3)
|
|
231
|
-
, buf3 = new Buffer(10)
|
|
232
|
-
, bl = new BufferList()
|
|
233
|
-
|
|
234
|
-
buf2[1] = 0x55
|
|
235
|
-
buf2[2] = 0x55
|
|
236
|
-
buf3[0] = 0x55
|
|
237
|
-
buf3[1] = 0x55
|
|
238
|
-
buf3[2] = 0x55
|
|
239
|
-
buf3[3] = 0x55
|
|
240
|
-
buf3[4] = 0xd5
|
|
241
|
-
buf3[5] = 0x3f
|
|
242
|
-
|
|
243
|
-
bl.append(buf1)
|
|
244
|
-
bl.append(buf2)
|
|
245
|
-
bl.append(buf3)
|
|
246
|
-
|
|
247
|
-
t.equal(bl.readDoubleLE(2), 0.3333333333333333)
|
|
248
|
-
t.end()
|
|
249
|
-
})
|
|
250
|
-
|
|
251
|
-
tape('test toString', function (t) {
|
|
252
|
-
var bl = new BufferList()
|
|
253
|
-
|
|
254
|
-
bl.append(new Buffer('abcd'))
|
|
255
|
-
bl.append(new Buffer('efg'))
|
|
256
|
-
bl.append(new Buffer('hi'))
|
|
257
|
-
bl.append(new Buffer('j'))
|
|
258
|
-
|
|
259
|
-
t.equal(bl.toString('ascii', 0, 10), 'abcdefghij')
|
|
260
|
-
t.equal(bl.toString('ascii', 3, 10), 'defghij')
|
|
261
|
-
t.equal(bl.toString('ascii', 3, 6), 'def')
|
|
262
|
-
t.equal(bl.toString('ascii', 3, 8), 'defgh')
|
|
263
|
-
t.equal(bl.toString('ascii', 5, 10), 'fghij')
|
|
264
|
-
|
|
265
|
-
t.end()
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
tape('test toString encoding', function (t) {
|
|
269
|
-
var bl = new BufferList()
|
|
270
|
-
, b = new Buffer('abcdefghij\xff\x00')
|
|
271
|
-
|
|
272
|
-
bl.append(new Buffer('abcd'))
|
|
273
|
-
bl.append(new Buffer('efg'))
|
|
274
|
-
bl.append(new Buffer('hi'))
|
|
275
|
-
bl.append(new Buffer('j'))
|
|
276
|
-
bl.append(new Buffer('\xff\x00'))
|
|
277
|
-
|
|
278
|
-
encodings.forEach(function (enc) {
|
|
279
|
-
t.equal(bl.toString(enc), b.toString(enc), enc)
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
t.end()
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
!process.browser && tape('test stream', function (t) {
|
|
286
|
-
var random = crypto.randomBytes(65534)
|
|
287
|
-
, rndhash = hash(random, 'md5')
|
|
288
|
-
, md5sum = crypto.createHash('md5')
|
|
289
|
-
, bl = new BufferList(function (err, buf) {
|
|
290
|
-
t.ok(Buffer.isBuffer(buf))
|
|
291
|
-
t.ok(err === null)
|
|
292
|
-
t.equal(rndhash, hash(bl.slice(), 'md5'))
|
|
293
|
-
t.equal(rndhash, hash(buf, 'md5'))
|
|
294
|
-
|
|
295
|
-
bl.pipe(fs.createWriteStream('/tmp/bl_test_rnd_out.dat'))
|
|
296
|
-
.on('close', function () {
|
|
297
|
-
var s = fs.createReadStream('/tmp/bl_test_rnd_out.dat')
|
|
298
|
-
s.on('data', md5sum.update.bind(md5sum))
|
|
299
|
-
s.on('end', function() {
|
|
300
|
-
t.equal(rndhash, md5sum.digest('hex'), 'woohoo! correct hash!')
|
|
301
|
-
t.end()
|
|
302
|
-
})
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
})
|
|
306
|
-
|
|
307
|
-
fs.writeFileSync('/tmp/bl_test_rnd.dat', random)
|
|
308
|
-
fs.createReadStream('/tmp/bl_test_rnd.dat').pipe(bl)
|
|
309
|
-
})
|
|
310
|
-
|
|
311
|
-
tape('instantiation with Buffer', function (t) {
|
|
312
|
-
var buf = crypto.randomBytes(1024)
|
|
313
|
-
, buf2 = crypto.randomBytes(1024)
|
|
314
|
-
, b = BufferList(buf)
|
|
315
|
-
|
|
316
|
-
t.equal(buf.toString('hex'), b.slice().toString('hex'), 'same buffer')
|
|
317
|
-
b = BufferList([ buf, buf2 ])
|
|
318
|
-
t.equal(b.slice().toString('hex'), Buffer.concat([ buf, buf2 ]).toString('hex'), 'same buffer')
|
|
319
|
-
t.end()
|
|
320
|
-
})
|
|
321
|
-
|
|
322
|
-
tape('test String appendage', function (t) {
|
|
323
|
-
var bl = new BufferList()
|
|
324
|
-
, b = new Buffer('abcdefghij\xff\x00')
|
|
325
|
-
|
|
326
|
-
bl.append('abcd')
|
|
327
|
-
bl.append('efg')
|
|
328
|
-
bl.append('hi')
|
|
329
|
-
bl.append('j')
|
|
330
|
-
bl.append('\xff\x00')
|
|
331
|
-
|
|
332
|
-
encodings.forEach(function (enc) {
|
|
333
|
-
t.equal(bl.toString(enc), b.toString(enc))
|
|
334
|
-
})
|
|
335
|
-
|
|
336
|
-
t.end()
|
|
337
|
-
})
|
|
338
|
-
|
|
339
|
-
tape('write nothing, should get empty buffer', function (t) {
|
|
340
|
-
t.plan(3)
|
|
341
|
-
BufferList(function (err, data) {
|
|
342
|
-
t.notOk(err, 'no error')
|
|
343
|
-
t.ok(Buffer.isBuffer(data), 'got a buffer')
|
|
344
|
-
t.equal(0, data.length, 'got a zero-length buffer')
|
|
345
|
-
t.end()
|
|
346
|
-
}).end()
|
|
347
|
-
})
|
|
348
|
-
|
|
349
|
-
tape('unicode string', function (t) {
|
|
350
|
-
t.plan(2)
|
|
351
|
-
var inp1 = '\u2600'
|
|
352
|
-
, inp2 = '\u2603'
|
|
353
|
-
, exp = inp1 + ' and ' + inp2
|
|
354
|
-
, bl = BufferList()
|
|
355
|
-
bl.write(inp1)
|
|
356
|
-
bl.write(' and ')
|
|
357
|
-
bl.write(inp2)
|
|
358
|
-
t.equal(exp, bl.toString())
|
|
359
|
-
t.equal(new Buffer(exp).toString('hex'), bl.toString('hex'))
|
|
360
|
-
})
|
|
361
|
-
|
|
362
|
-
tape('should emit finish', function (t) {
|
|
363
|
-
var source = BufferList()
|
|
364
|
-
, dest = BufferList()
|
|
365
|
-
|
|
366
|
-
source.write('hello')
|
|
367
|
-
source.pipe(dest)
|
|
368
|
-
|
|
369
|
-
dest.on('finish', function () {
|
|
370
|
-
t.equal(dest.toString('utf8'), 'hello')
|
|
371
|
-
t.end()
|
|
372
|
-
})
|
|
373
|
-
})
|
|
374
|
-
|
|
375
|
-
tape('basic copy', function (t) {
|
|
376
|
-
var buf = crypto.randomBytes(1024)
|
|
377
|
-
, buf2 = new Buffer(1024)
|
|
378
|
-
, b = BufferList(buf)
|
|
379
|
-
|
|
380
|
-
b.copy(buf2)
|
|
381
|
-
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
382
|
-
t.end()
|
|
383
|
-
})
|
|
384
|
-
|
|
385
|
-
tape('copy after many appends', function (t) {
|
|
386
|
-
var buf = crypto.randomBytes(512)
|
|
387
|
-
, buf2 = new Buffer(1024)
|
|
388
|
-
, b = BufferList(buf)
|
|
389
|
-
|
|
390
|
-
b.append(buf)
|
|
391
|
-
b.copy(buf2)
|
|
392
|
-
t.equal(b.slice().toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
393
|
-
t.end()
|
|
394
|
-
})
|
|
395
|
-
|
|
396
|
-
tape('copy at a precise position', function (t) {
|
|
397
|
-
var buf = crypto.randomBytes(1004)
|
|
398
|
-
, buf2 = new Buffer(1024)
|
|
399
|
-
, b = BufferList(buf)
|
|
400
|
-
|
|
401
|
-
b.copy(buf2, 20)
|
|
402
|
-
t.equal(b.slice().toString('hex'), buf2.slice(20).toString('hex'), 'same buffer')
|
|
403
|
-
t.end()
|
|
404
|
-
})
|
|
405
|
-
|
|
406
|
-
tape('copy starting from a precise location', function (t) {
|
|
407
|
-
var buf = crypto.randomBytes(10)
|
|
408
|
-
, buf2 = new Buffer(5)
|
|
409
|
-
, b = BufferList(buf)
|
|
410
|
-
|
|
411
|
-
b.copy(buf2, 0, 5)
|
|
412
|
-
t.equal(b.slice(5).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
413
|
-
t.end()
|
|
414
|
-
})
|
|
415
|
-
|
|
416
|
-
tape('copy in an interval', function (t) {
|
|
417
|
-
var rnd = crypto.randomBytes(10)
|
|
418
|
-
, b = BufferList(rnd) // put the random bytes there
|
|
419
|
-
, actual = new Buffer(3)
|
|
420
|
-
, expected = new Buffer(3)
|
|
421
|
-
|
|
422
|
-
rnd.copy(expected, 0, 5, 8)
|
|
423
|
-
b.copy(actual, 0, 5, 8)
|
|
424
|
-
|
|
425
|
-
t.equal(actual.toString('hex'), expected.toString('hex'), 'same buffer')
|
|
426
|
-
t.end()
|
|
427
|
-
})
|
|
428
|
-
|
|
429
|
-
tape('copy an interval between two buffers', function (t) {
|
|
430
|
-
var buf = crypto.randomBytes(10)
|
|
431
|
-
, buf2 = new Buffer(10)
|
|
432
|
-
, b = BufferList(buf)
|
|
433
|
-
|
|
434
|
-
b.append(buf)
|
|
435
|
-
b.copy(buf2, 0, 5, 15)
|
|
436
|
-
|
|
437
|
-
t.equal(b.slice(5, 15).toString('hex'), buf2.toString('hex'), 'same buffer')
|
|
438
|
-
t.end()
|
|
439
|
-
})
|
|
440
|
-
|
|
441
|
-
tape('duplicate', function (t) {
|
|
442
|
-
t.plan(2)
|
|
443
|
-
|
|
444
|
-
var bl = new BufferList('abcdefghij\xff\x00')
|
|
445
|
-
, dup = bl.duplicate()
|
|
446
|
-
|
|
447
|
-
t.equal(bl.prototype, dup.prototype)
|
|
448
|
-
t.equal(bl.toString('hex'), dup.toString('hex'))
|
|
449
|
-
})
|
|
450
|
-
|
|
451
|
-
tape('destroy no pipe', function (t) {
|
|
452
|
-
t.plan(2)
|
|
453
|
-
|
|
454
|
-
var bl = new BufferList('alsdkfja;lsdkfja;lsdk')
|
|
455
|
-
bl.destroy()
|
|
456
|
-
|
|
457
|
-
t.equal(bl._bufs.length, 0)
|
|
458
|
-
t.equal(bl.length, 0)
|
|
459
|
-
})
|
|
460
|
-
|
|
461
|
-
!process.browser && tape('destroy with pipe before read end', function (t) {
|
|
462
|
-
t.plan(2)
|
|
463
|
-
|
|
464
|
-
var bl = new BufferList()
|
|
465
|
-
fs.createReadStream(__dirname + '/sauce.js')
|
|
466
|
-
.pipe(bl)
|
|
467
|
-
|
|
468
|
-
bl.destroy()
|
|
469
|
-
|
|
470
|
-
t.equal(bl._bufs.length, 0)
|
|
471
|
-
t.equal(bl.length, 0)
|
|
472
|
-
|
|
473
|
-
})
|
|
474
|
-
|
|
475
|
-
!process.browser && tape('destroy with pipe before read end with race', function (t) {
|
|
476
|
-
t.plan(2)
|
|
477
|
-
|
|
478
|
-
var bl = new BufferList()
|
|
479
|
-
fs.createReadStream(__dirname + '/sauce.js')
|
|
480
|
-
.pipe(bl)
|
|
481
|
-
|
|
482
|
-
setTimeout(function () {
|
|
483
|
-
bl.destroy()
|
|
484
|
-
setTimeout(function () {
|
|
485
|
-
t.equal(bl._bufs.length, 0)
|
|
486
|
-
t.equal(bl.length, 0)
|
|
487
|
-
}, 500)
|
|
488
|
-
}, 500)
|
|
489
|
-
})
|
|
490
|
-
|
|
491
|
-
!process.browser && tape('destroy with pipe after read end', function (t) {
|
|
492
|
-
t.plan(2)
|
|
493
|
-
|
|
494
|
-
var bl = new BufferList()
|
|
495
|
-
fs.createReadStream(__dirname + '/sauce.js')
|
|
496
|
-
.on('end', onEnd)
|
|
497
|
-
.pipe(bl)
|
|
498
|
-
|
|
499
|
-
function onEnd () {
|
|
500
|
-
bl.destroy()
|
|
501
|
-
|
|
502
|
-
t.equal(bl._bufs.length, 0)
|
|
503
|
-
t.equal(bl.length, 0)
|
|
504
|
-
}
|
|
505
|
-
})
|
|
506
|
-
|
|
507
|
-
!process.browser && tape('destroy with pipe while writing to a destination', function (t) {
|
|
508
|
-
t.plan(4)
|
|
509
|
-
|
|
510
|
-
var bl = new BufferList()
|
|
511
|
-
, ds = new BufferList()
|
|
512
|
-
|
|
513
|
-
fs.createReadStream(__dirname + '/sauce.js')
|
|
514
|
-
.on('end', onEnd)
|
|
515
|
-
.pipe(bl)
|
|
516
|
-
|
|
517
|
-
function onEnd () {
|
|
518
|
-
bl.pipe(ds)
|
|
519
|
-
|
|
520
|
-
setTimeout(function () {
|
|
521
|
-
bl.destroy()
|
|
522
|
-
|
|
523
|
-
t.equals(bl._bufs.length, 0)
|
|
524
|
-
t.equals(bl.length, 0)
|
|
525
|
-
|
|
526
|
-
ds.destroy()
|
|
527
|
-
|
|
528
|
-
t.equals(bl._bufs.length, 0)
|
|
529
|
-
t.equals(bl.length, 0)
|
|
530
|
-
|
|
531
|
-
}, 100)
|
|
532
|
-
}
|
|
533
|
-
})
|
|
534
|
-
|
|
535
|
-
!process.browser && tape('handle error', function (t) {
|
|
536
|
-
t.plan(2)
|
|
537
|
-
fs.createReadStream('/does/not/exist').pipe(BufferList(function (err, data) {
|
|
538
|
-
t.ok(err instanceof Error, 'has error')
|
|
539
|
-
t.notOk(data, 'no data')
|
|
540
|
-
}))
|
|
541
|
-
})
|
package/test/sauce.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const user = process.env.SAUCE_USER
|
|
4
|
-
, key = process.env.SAUCE_KEY
|
|
5
|
-
, path = require('path')
|
|
6
|
-
, brtapsauce = require('brtapsauce')
|
|
7
|
-
, testFile = path.join(__dirname, 'basic-test.js')
|
|
8
|
-
|
|
9
|
-
, capabilities = [
|
|
10
|
-
{ browserName: 'chrome' , platform: 'Windows XP', version: '' }
|
|
11
|
-
, { browserName: 'firefox' , platform: 'Windows 8' , version: '' }
|
|
12
|
-
, { browserName: 'firefox' , platform: 'Windows XP', version: '4' }
|
|
13
|
-
, { browserName: 'internet explorer' , platform: 'Windows 8' , version: '10' }
|
|
14
|
-
, { browserName: 'internet explorer' , platform: 'Windows 7' , version: '9' }
|
|
15
|
-
, { browserName: 'internet explorer' , platform: 'Windows 7' , version: '8' }
|
|
16
|
-
, { browserName: 'internet explorer' , platform: 'Windows XP', version: '7' }
|
|
17
|
-
, { browserName: 'internet explorer' , platform: 'Windows XP', version: '6' }
|
|
18
|
-
, { browserName: 'safari' , platform: 'Windows 7' , version: '5' }
|
|
19
|
-
, { browserName: 'safari' , platform: 'OS X 10.8' , version: '6' }
|
|
20
|
-
, { browserName: 'opera' , platform: 'Windows 7' , version: '' }
|
|
21
|
-
, { browserName: 'opera' , platform: 'Windows 7' , version: '11' }
|
|
22
|
-
, { browserName: 'ipad' , platform: 'OS X 10.8' , version: '6' }
|
|
23
|
-
, { browserName: 'android' , platform: 'Linux' , version: '4.0', 'device-type': 'tablet' }
|
|
24
|
-
]
|
|
25
|
-
|
|
26
|
-
if (!user)
|
|
27
|
-
throw new Error('Must set a SAUCE_USER env var')
|
|
28
|
-
if (!key)
|
|
29
|
-
throw new Error('Must set a SAUCE_KEY env var')
|
|
30
|
-
|
|
31
|
-
brtapsauce({
|
|
32
|
-
name : 'Traversty'
|
|
33
|
-
, user : user
|
|
34
|
-
, key : key
|
|
35
|
-
, brsrc : testFile
|
|
36
|
-
, capabilities : capabilities
|
|
37
|
-
, options : { timeout: 60 * 6 }
|
|
38
|
-
})
|