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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
- const assert = require('assert');
- const stream = require('stream');
- const crypto = require('crypto');
- const expect = require('expect.js');
- const miss = require('mississippi');
- const fromValue = require('stream-from-value');
- const io = require('../lib/io');
- function randomStream(bytes) {
- let total = 0;
-
- return miss.from(function(size, next) {
- let max = Math.min(size, bytes - total);
- let n = Math.random() * 412 + 100;
- n = Math.min(n, max);
- if (n == 0) {
- return next(null, null);
- }
- crypto.randomBytes(n, (err, buf) => {
- total += buf.length;
- next(err, buf);
- });
- });
- }
- function b(s) {
- return Buffer.from(s);
- }
- function makeArray(done) {
- let array = [];
- return new stream.Writable({
- objectMode: true,
- write(chunk, enc, cb) {
- array.push(chunk);
- cb();
- },
- final(cb) {
- process.nextTick(done, null, array);
- cb();
- }
- });
- }
- describe('decodeLines()', () => {
- it('should be empty on an empty string', (done) => {
- fromValue('').pipe(io.decodeLines())
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.equal(arr.length, 0);
- done();
- }));
- });
- it('should split a line', (done) => {
- fromValue('foo\n').pipe(io.decodeLines())
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.equal(arr.length, 1);
- assert.equal(arr[0], 'foo');
- done();
- }));
- });
- it('should pass a partial line', (done) => {
- fromValue('bar').pipe(io.decodeLines())
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.equal(arr.length, 1);
- assert.equal(arr[0], 'bar');
- done();
- }));
- });
- it('should pass a couple lines', (done) => {
- fromValue('wumpus\nsplintercat\n').pipe(io.decodeLines())
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.deepEqual(arr, ['wumpus', 'splintercat']);
- done();
- }));
- });
- it('should pass full & partial', (done) => {
- fromValue('wumpus\nsplintercat').pipe(io.decodeLines())
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.deepEqual(arr, ['wumpus', 'splintercat']);
- done();
- }));
- });
- it('should pass chunks', (done) => {
- let lines = io.decodeLines();
- lines.pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.deepEqual(arr, ['wumpus', 'splintercat']);
- done();
- }));
- lines.write(b('wum'));
- lines.write(b('pus\nsplint'));
- lines.write(b('ercat\n'));
- lines.end();
- });
- it('should handle a partial chunk', (done) => {
- let lines = io.decodeLines();
- lines.pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.deepEqual(arr, ['wumpus', 'splintercat']);
- done();
- }));
- lines.write(b('wum'));
- lines.write(b('pus\nsplint'));
- lines.write(b('ercat'));
- lines.end();
- });
- it('should handle a chunks and a write', (done) => {
- let lines = io.decodeLines();
- lines.pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.deepEqual(arr, ['wumpus', 'splintercat']);
- done();
- }));
- lines.write(b('wum'));
- lines.write(b('pus\nsplint'));
- lines.write(b('ercat\n'));
- lines.write(b(''));
- lines.end();
- });
-
- it('should handle some json', (done) => {
- fromValue(b('"wumpus"\n"splintercat"\n')).pipe(io.decodeLines(JSON.parse))
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- assert.deepEqual(arr, ['wumpus', 'splintercat']);
- done();
- }));
- });
-
- it('should handle many random bytes', (done) => {
- let nbytes = 100000;
- randomStream(nbytes).pipe(io.decodeLines((b) => b.length))
- .pipe(makeArray((err, arr) => {
- if (err) return done(err);
- let len = 0;
- for (let e of arr) {
- len += e + 1
- }
- // allow for missing/present final newline
- // if we have a final newline: then each thing has a newline
- // if we do not: then we are short one newline
- expect(len).to.be.below(nbytes+2);
- expect(len).to.be.above(nbytes-1);
- done();
- }));
- });
- })
|