Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

io.js 4.4 KB

You have to be logged in to leave a comment. Sign In
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
  1. const assert = require('assert');
  2. const stream = require('stream');
  3. const crypto = require('crypto');
  4. const expect = require('expect.js');
  5. const miss = require('mississippi');
  6. const fromValue = require('stream-from-value');
  7. const io = require('../lib/io');
  8. function randomStream(bytes) {
  9. let total = 0;
  10. return miss.from(function(size, next) {
  11. let max = Math.min(size, bytes - total);
  12. let n = Math.random() * 412 + 100;
  13. n = Math.min(n, max);
  14. if (n == 0) {
  15. return next(null, null);
  16. }
  17. crypto.randomBytes(n, (err, buf) => {
  18. total += buf.length;
  19. next(err, buf);
  20. });
  21. });
  22. }
  23. function b(s) {
  24. return Buffer.from(s);
  25. }
  26. function makeArray(done) {
  27. let array = [];
  28. return new stream.Writable({
  29. objectMode: true,
  30. write(chunk, enc, cb) {
  31. array.push(chunk);
  32. cb();
  33. },
  34. final(cb) {
  35. process.nextTick(done, null, array);
  36. cb();
  37. }
  38. });
  39. }
  40. describe('decodeLines()', () => {
  41. it('should be empty on an empty string', (done) => {
  42. fromValue('').pipe(io.decodeLines())
  43. .pipe(makeArray((err, arr) => {
  44. if (err) return done(err);
  45. assert.equal(arr.length, 0);
  46. done();
  47. }));
  48. });
  49. it('should split a line', (done) => {
  50. fromValue('foo\n').pipe(io.decodeLines())
  51. .pipe(makeArray((err, arr) => {
  52. if (err) return done(err);
  53. assert.equal(arr.length, 1);
  54. assert.equal(arr[0], 'foo');
  55. done();
  56. }));
  57. });
  58. it('should pass a partial line', (done) => {
  59. fromValue('bar').pipe(io.decodeLines())
  60. .pipe(makeArray((err, arr) => {
  61. if (err) return done(err);
  62. assert.equal(arr.length, 1);
  63. assert.equal(arr[0], 'bar');
  64. done();
  65. }));
  66. });
  67. it('should pass a couple lines', (done) => {
  68. fromValue('wumpus\nsplintercat\n').pipe(io.decodeLines())
  69. .pipe(makeArray((err, arr) => {
  70. if (err) return done(err);
  71. assert.deepEqual(arr, ['wumpus', 'splintercat']);
  72. done();
  73. }));
  74. });
  75. it('should pass full & partial', (done) => {
  76. fromValue('wumpus\nsplintercat').pipe(io.decodeLines())
  77. .pipe(makeArray((err, arr) => {
  78. if (err) return done(err);
  79. assert.deepEqual(arr, ['wumpus', 'splintercat']);
  80. done();
  81. }));
  82. });
  83. it('should pass chunks', (done) => {
  84. let lines = io.decodeLines();
  85. lines.pipe(makeArray((err, arr) => {
  86. if (err) return done(err);
  87. assert.deepEqual(arr, ['wumpus', 'splintercat']);
  88. done();
  89. }));
  90. lines.write(b('wum'));
  91. lines.write(b('pus\nsplint'));
  92. lines.write(b('ercat\n'));
  93. lines.end();
  94. });
  95. it('should handle a partial chunk', (done) => {
  96. let lines = io.decodeLines();
  97. lines.pipe(makeArray((err, arr) => {
  98. if (err) return done(err);
  99. assert.deepEqual(arr, ['wumpus', 'splintercat']);
  100. done();
  101. }));
  102. lines.write(b('wum'));
  103. lines.write(b('pus\nsplint'));
  104. lines.write(b('ercat'));
  105. lines.end();
  106. });
  107. it('should handle a chunks and a write', (done) => {
  108. let lines = io.decodeLines();
  109. lines.pipe(makeArray((err, arr) => {
  110. if (err) return done(err);
  111. assert.deepEqual(arr, ['wumpus', 'splintercat']);
  112. done();
  113. }));
  114. lines.write(b('wum'));
  115. lines.write(b('pus\nsplint'));
  116. lines.write(b('ercat\n'));
  117. lines.write(b(''));
  118. lines.end();
  119. });
  120. it('should handle some json', (done) => {
  121. fromValue(b('"wumpus"\n"splintercat"\n')).pipe(io.decodeLines(JSON.parse))
  122. .pipe(makeArray((err, arr) => {
  123. if (err) return done(err);
  124. assert.deepEqual(arr, ['wumpus', 'splintercat']);
  125. done();
  126. }));
  127. });
  128. it('should handle many random bytes', (done) => {
  129. let nbytes = 100000;
  130. randomStream(nbytes).pipe(io.decodeLines((b) => b.length))
  131. .pipe(makeArray((err, arr) => {
  132. if (err) return done(err);
  133. let len = 0;
  134. for (let e of arr) {
  135. len += e + 1
  136. }
  137. // allow for missing/present final newline
  138. // if we have a final newline: then each thing has a newline
  139. // if we do not: then we are short one newline
  140. expect(len).to.be.below(nbytes+2);
  141. expect(len).to.be.above(nbytes-1);
  142. done();
  143. }));
  144. });
  145. })
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...