Skip to content

Commit d4e4800

Browse files
committed
fixed tests, noise reduction (debug)
1 parent b82058a commit d4e4800

File tree

4 files changed

+16
-15
lines changed

4 files changed

+16
-15
lines changed

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = function(needle, replace, options) {
2727
var fifo = null;
2828

2929
function queueReplacement(stream) {
30+
debug('queueing replacement %s %s', typeof replace, replace);
3031
stream.queue(replace);
3132
occurances++;
3233
if (maxOccurances && occurances >= maxOccurances) {
@@ -66,7 +67,7 @@ module.exports = function(needle, replace, options) {
6667
// Is the rest of the chunk identical to
6768
// the rest of the needle?
6869
var nl = compare(needle, chunk, pin, pic);
69-
debug('compare', needle, chunk, pin, pic, 'nl', nl);
70+
//debug('compare', needle, chunk, pin, pic, 'nl', nl);
7071
// is the whole needle contained in the chunk?
7172
if (nl === 0) {
7273
// YES! This is the chunk containing the needle's tail.
@@ -96,7 +97,9 @@ module.exports = function(needle, replace, options) {
9697
}
9798

9899
function write(chunk) {
100+
debug('client writes %s of len %d', typeof chunk, chunk.length);
99101
if (forwarding) {
102+
debug('forwarding %s', typeof chunk);
100103
this.queue(chunk);
101104
return;
102105
}
@@ -112,8 +115,10 @@ module.exports = function(needle, replace, options) {
112115
for(;;) {
113116
if (forwarding) {
114117
if (pic===0) {
115-
this.queue(chunk);
118+
debug('forwarding %s', typeof chunk);
119+
this.queue(chunk);
116120
} else {
121+
debug('forwarding %s', typeof chunk);
117122
this.queue(chunk.slice(pic));
118123
}
119124
pic = 0;

lib/fifo.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ function fifo(passDownstream) {
5959
var len = ch.length;
6060
var skip = seq[0][0];
6161
var fwd = seq[0][1];
62-
debug(skip, fwd);
6362
var skipBytes = Math.min(len, skip);
6463
if (skipBytes === len) {
6564
chunks.shift();
@@ -70,20 +69,19 @@ function fifo(passDownstream) {
7069
}
7170
skip -= skipBytes;
7271
len -= skipBytes;
73-
debug('len', len);
7472
var fwdBytes = Math.min(len, fwd);
7573
if (fwdBytes) {
7674
if (fwdBytes === len) {
77-
debug('forwarding', ch);
75+
debug('forwarding %s %o', typeof ch, ch);
7876
passDownstream(ch);
7977
chunks.shift();
8078
} else {
81-
debug('forwarding slice', ch.slice(0, fwdBytes));
79+
debug('forwarding slice %s %o', typeof ch, ch.slice(0, fwdBytes));
8280
passDownstream(ch.slice(0, fwdBytes));
8381
chunks[0] = ch = ch.slice(fwdBytes);
8482
}
8583
}
86-
debug('forwarded', fwdBytes, 'bytes');
84+
debug('forwarded %d bytes', fwdBytes);
8785
fwd -= fwdBytes;
8886
if (skip === 0 && fwd === 0) {
8987
seq.shift();
@@ -132,7 +130,7 @@ function fifo(passDownstream) {
132130
sum -= s[0] + s[1];
133131
});
134132
f(sum);
135-
}
133+
};
136134
}
137135

138136
// return bytes for which we don't know

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "binary-stream-replace",
33
"version": "1.0.0",
4-
"description": "efficiently replace a sequence of bytes in a stream with a different sequence of bytes, either n times or all occurances.",
4+
"description": "efficiently replace a sequence of bytes in a stream with a different sequence of bytes, either first n occurances or all occurances.",
55
"main": "index.js",
66
"scripts": {
77
"test": "tap-prettify test"
@@ -12,12 +12,11 @@
1212
"browserify",
1313
"replace"
1414
],
15-
"author": "Jan Bölsche",
15+
"author": "Jan Bölsche <[email protected]>",
1616
"license": "ISC",
1717
"devDependencies": {
1818
"tape": "^3.2.0",
1919
"tap-prettify": "0.0.2",
20-
"from": "^0.1.3",
2120
"concat-stream": "^1.4.7"
2221
},
2322
"dependencies": {

test/long.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ function compare(t, encoding, file1, file2, n, r, o) {
88
var src = fs.createReadStream(path.join(__dirname, 'fixtures', file1), {encoding:encoding});
99
var rs = Rs(n, r, o);
1010
var cs = Cs(function(data) {
11-
console.log(typeof data);
1211
var d2 = fs.readFileSync(path.join(__dirname, 'fixtures', file2), encoding);
1312
t.deepEqual(data, d2);
1413
});
@@ -30,12 +29,12 @@ test('should change first 6 occurances of "of" to "0ffer"', function(t) {
3029
compare(t, 'utf-8', 'lorem.txt', '6x0ffer.txt', 'of', '0ffer', {maxOccurances: 6});
3130
});
3231

33-
test('hould not alter a binary stream', function(t) {
32+
test('should not alter a binary stream', function(t) {
3433
t.plan(1);
35-
compare(t, 'binary', 'random.bin', 'random.bin', new Buffer([0xff,0xff,0xff,0xff]), new Buffer([0x00]));
34+
compare(t, null, 'random.bin', 'random.bin', new Buffer([0xff,0xff,0xff,0xff]), new Buffer([0x00]));
3635
});
3736

3837
test('should replace all occurances of 0xff with 0x00 in a binary file', function(t) {
3938
t.plan(1);
40-
compare(t, 'binary', 'random.bin', 'ffto00.bin', new Buffer([0xff]), new Buffer([0x00]));
39+
compare(t, null, 'random.bin', 'ffto00.bin', new Buffer([0xff]), new Buffer([0x00]));
4140
});

0 commit comments

Comments
 (0)