bl 3.0.1 → 4.0.3
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 +3 -0
- package/BufferList.js +396 -0
- package/LICENSE.md +1 -1
- package/README.md +60 -31
- package/bl.js +30 -337
- package/package.json +10 -6
- package/test/convert.js +21 -0
- package/test/indexOf.js +78 -49
- package/test/isBufferList.js +32 -0
- package/test/test.js +212 -157
- package/.jshintrc +0 -60
package/test/indexOf.js
CHANGED
|
@@ -1,62 +1,83 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
const tape = require('tape')
|
|
4
|
+
const BufferList = require('../')
|
|
5
|
+
const { Buffer } = require('buffer')
|
|
6
6
|
|
|
7
|
-
tape('indexOf single byte needle', t => {
|
|
7
|
+
tape('indexOf single byte needle', (t) => {
|
|
8
8
|
const bl = new BufferList(['abcdefg', 'abcdefg', '12345'])
|
|
9
|
+
|
|
9
10
|
t.equal(bl.indexOf('e'), 4)
|
|
10
11
|
t.equal(bl.indexOf('e', 5), 11)
|
|
11
12
|
t.equal(bl.indexOf('e', 12), -1)
|
|
12
13
|
t.equal(bl.indexOf('5'), 18)
|
|
14
|
+
|
|
13
15
|
t.end()
|
|
14
16
|
})
|
|
15
17
|
|
|
16
|
-
tape('indexOf multiple byte needle', t => {
|
|
18
|
+
tape('indexOf multiple byte needle', (t) => {
|
|
17
19
|
const bl = new BufferList(['abcdefg', 'abcdefg'])
|
|
20
|
+
|
|
18
21
|
t.equal(bl.indexOf('ef'), 4)
|
|
19
22
|
t.equal(bl.indexOf('ef', 5), 11)
|
|
23
|
+
|
|
20
24
|
t.end()
|
|
21
25
|
})
|
|
22
26
|
|
|
23
|
-
tape('indexOf multiple byte needles across buffer boundaries', t => {
|
|
27
|
+
tape('indexOf multiple byte needles across buffer boundaries', (t) => {
|
|
24
28
|
const bl = new BufferList(['abcdefg', 'abcdefg'])
|
|
29
|
+
|
|
25
30
|
t.equal(bl.indexOf('fgabc'), 5)
|
|
31
|
+
|
|
26
32
|
t.end()
|
|
27
33
|
})
|
|
28
34
|
|
|
29
|
-
tape('indexOf takes a
|
|
35
|
+
tape('indexOf takes a Uint8Array search', (t) => {
|
|
36
|
+
const bl = new BufferList(['abcdefg', 'abcdefg'])
|
|
37
|
+
const search = new Uint8Array([102, 103, 97, 98, 99]) // fgabc
|
|
38
|
+
|
|
39
|
+
t.equal(bl.indexOf(search), 5)
|
|
40
|
+
|
|
41
|
+
t.end()
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
tape('indexOf takes a buffer list search', (t) => {
|
|
30
45
|
const bl = new BufferList(['abcdefg', 'abcdefg'])
|
|
31
46
|
const search = new BufferList('fgabc')
|
|
47
|
+
|
|
32
48
|
t.equal(bl.indexOf(search), 5)
|
|
49
|
+
|
|
33
50
|
t.end()
|
|
34
51
|
})
|
|
35
52
|
|
|
36
|
-
tape('indexOf a zero byte needle', t => {
|
|
53
|
+
tape('indexOf a zero byte needle', (t) => {
|
|
37
54
|
const b = new BufferList('abcdef')
|
|
38
|
-
const
|
|
55
|
+
const bufEmpty = Buffer.from('')
|
|
56
|
+
|
|
39
57
|
t.equal(b.indexOf(''), 0)
|
|
40
58
|
t.equal(b.indexOf('', 1), 1)
|
|
41
59
|
t.equal(b.indexOf('', b.length + 1), b.length)
|
|
42
60
|
t.equal(b.indexOf('', Infinity), b.length)
|
|
43
|
-
t.equal(b.indexOf(
|
|
44
|
-
t.equal(b.indexOf(
|
|
45
|
-
t.equal(b.indexOf(
|
|
46
|
-
t.equal(b.indexOf(
|
|
61
|
+
t.equal(b.indexOf(bufEmpty), 0)
|
|
62
|
+
t.equal(b.indexOf(bufEmpty, 1), 1)
|
|
63
|
+
t.equal(b.indexOf(bufEmpty, b.length + 1), b.length)
|
|
64
|
+
t.equal(b.indexOf(bufEmpty, Infinity), b.length)
|
|
65
|
+
|
|
47
66
|
t.end()
|
|
48
67
|
})
|
|
49
68
|
|
|
50
|
-
tape('indexOf buffers smaller and larger than the needle', t => {
|
|
69
|
+
tape('indexOf buffers smaller and larger than the needle', (t) => {
|
|
51
70
|
const bl = new BufferList(['abcdefg', 'a', 'bcdefg', 'a', 'bcfgab'])
|
|
71
|
+
|
|
52
72
|
t.equal(bl.indexOf('fgabc'), 5)
|
|
53
73
|
t.equal(bl.indexOf('fgabc', 6), 12)
|
|
54
74
|
t.equal(bl.indexOf('fgabc', 13), -1)
|
|
75
|
+
|
|
55
76
|
t.end()
|
|
56
77
|
})
|
|
57
78
|
|
|
58
79
|
// only present in node 6+
|
|
59
|
-
;(process.version.substr(1).split('.')[0] >= 6) && tape('indexOf latin1 and binary encoding', t => {
|
|
80
|
+
;(process.version.substr(1).split('.')[0] >= 6) && tape('indexOf latin1 and binary encoding', (t) => {
|
|
60
81
|
const b = new BufferList('abcdef')
|
|
61
82
|
|
|
62
83
|
// test latin1 encoding
|
|
@@ -112,15 +133,16 @@ tape('indexOf buffers smaller and larger than the needle', t => {
|
|
|
112
133
|
.indexOf(Buffer.from('\u00e8', 'binary'), 'binary'),
|
|
113
134
|
0
|
|
114
135
|
)
|
|
136
|
+
|
|
115
137
|
t.end()
|
|
116
138
|
})
|
|
117
139
|
|
|
118
|
-
tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
140
|
+
tape('indexOf the entire nodejs10 buffer test suite', (t) => {
|
|
119
141
|
const b = new BufferList('abcdef')
|
|
120
|
-
const
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
const
|
|
142
|
+
const bufA = Buffer.from('a')
|
|
143
|
+
const bufBc = Buffer.from('bc')
|
|
144
|
+
const bufF = Buffer.from('f')
|
|
145
|
+
const bufZ = Buffer.from('z')
|
|
124
146
|
|
|
125
147
|
const stringComparison = 'abcdef'
|
|
126
148
|
|
|
@@ -142,25 +164,26 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
142
164
|
t.equal(b.indexOf('bc', Infinity), -1)
|
|
143
165
|
t.equal(b.indexOf('f'), b.length - 1)
|
|
144
166
|
t.equal(b.indexOf('z'), -1)
|
|
167
|
+
|
|
145
168
|
// empty search tests
|
|
146
|
-
t.equal(b.indexOf(
|
|
147
|
-
t.equal(b.indexOf(
|
|
148
|
-
t.equal(b.indexOf(
|
|
149
|
-
t.equal(b.indexOf(
|
|
150
|
-
t.equal(b.indexOf(
|
|
151
|
-
t.equal(b.indexOf(
|
|
152
|
-
t.equal(b.indexOf(
|
|
153
|
-
t.equal(b.indexOf(
|
|
154
|
-
t.equal(b.indexOf(
|
|
155
|
-
t.equal(b.indexOf(
|
|
156
|
-
t.equal(b.indexOf(
|
|
157
|
-
t.equal(b.indexOf(
|
|
158
|
-
t.equal(b.indexOf(
|
|
159
|
-
t.equal(b.indexOf(
|
|
160
|
-
t.equal(b.indexOf(
|
|
161
|
-
t.equal(b.indexOf(
|
|
162
|
-
t.equal(b.indexOf(
|
|
163
|
-
t.equal(b.indexOf(
|
|
169
|
+
t.equal(b.indexOf(bufA), 0)
|
|
170
|
+
t.equal(b.indexOf(bufA, 1), -1)
|
|
171
|
+
t.equal(b.indexOf(bufA, -1), -1)
|
|
172
|
+
t.equal(b.indexOf(bufA, -4), -1)
|
|
173
|
+
t.equal(b.indexOf(bufA, -b.length), 0)
|
|
174
|
+
t.equal(b.indexOf(bufA, NaN), 0)
|
|
175
|
+
t.equal(b.indexOf(bufA, -Infinity), 0)
|
|
176
|
+
t.equal(b.indexOf(bufA, Infinity), -1)
|
|
177
|
+
t.equal(b.indexOf(bufBc), 1)
|
|
178
|
+
t.equal(b.indexOf(bufBc, 2), -1)
|
|
179
|
+
t.equal(b.indexOf(bufBc, -1), -1)
|
|
180
|
+
t.equal(b.indexOf(bufBc, -3), -1)
|
|
181
|
+
t.equal(b.indexOf(bufBc, -5), 1)
|
|
182
|
+
t.equal(b.indexOf(bufBc, NaN), 1)
|
|
183
|
+
t.equal(b.indexOf(bufBc, -Infinity), 1)
|
|
184
|
+
t.equal(b.indexOf(bufBc, Infinity), -1)
|
|
185
|
+
t.equal(b.indexOf(bufF), b.length - 1)
|
|
186
|
+
t.equal(b.indexOf(bufZ), -1)
|
|
164
187
|
t.equal(b.indexOf(0x61), 0)
|
|
165
188
|
t.equal(b.indexOf(0x61, 1), -1)
|
|
166
189
|
t.equal(b.indexOf(0x61, -1), -1)
|
|
@@ -244,6 +267,7 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
244
267
|
|
|
245
268
|
const mixedByteStringUcs2 =
|
|
246
269
|
Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395', 'ucs2')
|
|
270
|
+
|
|
247
271
|
t.equal(6, mixedByteStringUcs2.indexOf('bc', 0, 'ucs2'))
|
|
248
272
|
t.equal(10, mixedByteStringUcs2.indexOf('\u03a3', 0, 'ucs2'))
|
|
249
273
|
t.equal(-1, mixedByteStringUcs2.indexOf('\u0396', 0, 'ucs2'))
|
|
@@ -283,17 +307,17 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
283
307
|
}
|
|
284
308
|
|
|
285
309
|
const mixedByteStringUtf8 = Buffer.from('\u039a\u0391abc\u03a3\u03a3\u0395')
|
|
310
|
+
|
|
286
311
|
t.equal(5, mixedByteStringUtf8.indexOf('bc'))
|
|
287
312
|
t.equal(5, mixedByteStringUtf8.indexOf('bc', 5))
|
|
288
313
|
t.equal(5, mixedByteStringUtf8.indexOf('bc', -8))
|
|
289
314
|
t.equal(7, mixedByteStringUtf8.indexOf('\u03a3'))
|
|
290
315
|
t.equal(-1, mixedByteStringUtf8.indexOf('\u0396'))
|
|
291
316
|
|
|
292
|
-
|
|
293
317
|
// Test complex string indexOf algorithms. Only trigger for long strings.
|
|
294
318
|
// Long string that isn't a simple repeat of a shorter string.
|
|
295
319
|
let longString = 'A'
|
|
296
|
-
for (let i = 66; i < 76; i++) {
|
|
320
|
+
for (let i = 66; i < 76; i++) { // from 'B' to 'K'
|
|
297
321
|
longString = longString + String.fromCharCode(i) + longString
|
|
298
322
|
}
|
|
299
323
|
|
|
@@ -304,7 +328,7 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
304
328
|
for (let i = 0; i < longBufferString.length - pattern.length; i += 7) {
|
|
305
329
|
const index = longBufferString.indexOf(pattern, i)
|
|
306
330
|
t.equal((i + 15) & ~0xf, index,
|
|
307
|
-
|
|
331
|
+
`Long ABACABA...-string at index ${i}`)
|
|
308
332
|
}
|
|
309
333
|
|
|
310
334
|
let index = longBufferString.indexOf('AJABACA')
|
|
@@ -321,13 +345,16 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
321
345
|
|
|
322
346
|
// Search for a non-ASCII string in a pure ASCII string.
|
|
323
347
|
const asciiString = Buffer.from(
|
|
324
|
-
'
|
|
348
|
+
'somethingnotatallsinisterwhichalsoworks')
|
|
325
349
|
t.equal(-1, asciiString.indexOf('\x2061'))
|
|
326
|
-
t.equal(3, asciiString.indexOf('
|
|
350
|
+
t.equal(3, asciiString.indexOf('eth', 0))
|
|
327
351
|
|
|
328
352
|
// Search in string containing many non-ASCII chars.
|
|
329
353
|
const allCodePoints = []
|
|
330
|
-
for (let i = 0; i < 65536; i++)
|
|
354
|
+
for (let i = 0; i < 65536; i++) {
|
|
355
|
+
allCodePoints[i] = i
|
|
356
|
+
}
|
|
357
|
+
|
|
331
358
|
const allCharsString = String.fromCharCode.apply(String, allCodePoints)
|
|
332
359
|
const allCharsBufferUtf8 = Buffer.from(allCharsString)
|
|
333
360
|
const allCharsBufferUcs2 = Buffer.from(allCharsString, 'ucs2')
|
|
@@ -348,7 +375,7 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
348
375
|
|
|
349
376
|
{
|
|
350
377
|
// Find substrings in Utf8.
|
|
351
|
-
const lengths = [1, 3, 15]
|
|
378
|
+
const lengths = [1, 3, 15] // Single char, simple and complex.
|
|
352
379
|
const indices = [0x5, 0x60, 0x400, 0x680, 0x7ee, 0xFF02, 0x16610, 0x2f77b]
|
|
353
380
|
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
|
|
354
381
|
for (let i = 0; i < indices.length; i++) {
|
|
@@ -378,8 +405,9 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
378
405
|
|
|
379
406
|
{
|
|
380
407
|
// Find substrings in Usc2.
|
|
381
|
-
const lengths = [2, 4, 16]
|
|
408
|
+
const lengths = [2, 4, 16] // Single char, simple and complex.
|
|
382
409
|
const indices = [0x5, 0x65, 0x105, 0x205, 0x285, 0x2005, 0x2085, 0xfff0]
|
|
410
|
+
|
|
383
411
|
for (let lengthIndex = 0; lengthIndex < lengths.length; lengthIndex++) {
|
|
384
412
|
for (let i = 0; i < indices.length; i++) {
|
|
385
413
|
const index = indices[i] * 2
|
|
@@ -401,8 +429,7 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
401
429
|
() => {},
|
|
402
430
|
{},
|
|
403
431
|
[]
|
|
404
|
-
].forEach(val => {
|
|
405
|
-
debugger
|
|
432
|
+
].forEach((val) => {
|
|
406
433
|
t.throws(() => b.indexOf(val), TypeError, `"${JSON.stringify(val)}" should throw`)
|
|
407
434
|
})
|
|
408
435
|
|
|
@@ -440,6 +467,7 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
440
467
|
// test truncation of Number arguments to uint8
|
|
441
468
|
{
|
|
442
469
|
const buf = Buffer.from('this is a test')
|
|
470
|
+
|
|
443
471
|
t.equal(buf.indexOf(0x6973), 3)
|
|
444
472
|
t.equal(buf.indexOf(0x697320), 4)
|
|
445
473
|
t.equal(buf.indexOf(0x69732069), 2)
|
|
@@ -455,9 +483,10 @@ tape('indexOf the entire nodejs10 buffer test suite', t => {
|
|
|
455
483
|
|
|
456
484
|
// Test that Uint8Array arguments are okay.
|
|
457
485
|
{
|
|
458
|
-
const needle = new Uint8Array([
|
|
486
|
+
const needle = new Uint8Array([0x66, 0x6f, 0x6f])
|
|
459
487
|
const haystack = new BufferList(Buffer.from('a foo b foo'))
|
|
460
488
|
t.equal(haystack.indexOf(needle), 2)
|
|
461
489
|
}
|
|
490
|
+
|
|
462
491
|
t.end()
|
|
463
492
|
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const tape = require('tape')
|
|
4
|
+
const { BufferList, BufferListStream } = require('../')
|
|
5
|
+
const { Buffer } = require('buffer')
|
|
6
|
+
|
|
7
|
+
tape('isBufferList positives', (t) => {
|
|
8
|
+
t.ok(BufferList.isBufferList(new BufferList()))
|
|
9
|
+
t.ok(BufferList.isBufferList(new BufferListStream()))
|
|
10
|
+
|
|
11
|
+
t.end()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
tape('isBufferList negatives', (t) => {
|
|
15
|
+
const types = [
|
|
16
|
+
null,
|
|
17
|
+
undefined,
|
|
18
|
+
NaN,
|
|
19
|
+
true,
|
|
20
|
+
false,
|
|
21
|
+
{},
|
|
22
|
+
[],
|
|
23
|
+
Buffer.alloc(0),
|
|
24
|
+
[Buffer.alloc(0)]
|
|
25
|
+
]
|
|
26
|
+
|
|
27
|
+
for (const obj of types) {
|
|
28
|
+
t.notOk(BufferList.isBufferList(obj))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
t.end()
|
|
32
|
+
})
|