Skip to content

Commit

Permalink
[Fix] default_stream: do not error on nullish data
Browse files Browse the repository at this point in the history
altho tape itself would never trigger it, this is an entry point
  • Loading branch information
ljharb committed Jan 27, 2024
1 parent bff9dad commit eff3725
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/default_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ module.exports = function () {
return stream;

function write(buf) {
if (
buf == null // eslint-disable-line eqeqeq
|| (Object(buf) !== buf && typeof buf !== 'string')
) {
flush();
return;
}
for (var i = 0; i < buf.length; i++) {
var c = typeof buf === 'string'
? buf.charAt(i)
Expand Down
20 changes: 20 additions & 0 deletions test/default_stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

var tap = require('tap');

var getDefaultStream = require('../lib/default_stream');

tap.test('getDefaultStream', function (tt) {
tt.plan(5);

var stream = getDefaultStream();

tt.doesNotThrow(function () { stream.write('# ok'); }, 'strings are fine');
tt.doesNotThrow(function () { stream.write(123); }, 'numbers are fine');
tt.doesNotThrow(function () { stream.write(undefined); }, 'undefined is fine');
tt.doesNotThrow(function () { stream.write(); }, 'no args is fine');
tt.doesNotThrow(function () { stream.write(null); }, 'null is fine');
// TODO: figure out why writing to the stream after writing null fails tests

tt.end();
});

0 comments on commit eff3725

Please sign in to comment.