maxogden/concat-stream — pull request #27 by parshap (2014-06-02 19:03:51+00:00)
Title: Correctly infer string encoding from empty string

## Use Case

I have an issue where sometimes the first chunk of my **stream of strings** is an empty string and *concat-stream* then treats the stream as if it were a **stream of buffers**.

```javascript
var concat = require("concat-stream");
var strings = concat(function(data) {
console.log(typeof data === "string");
});
if (Math.random() > 0.5) strings.write("");
strings.end("foo bar");
```

## Changes

This change handles the case where the first chunk written is an empty string (`''`) and correctly infers the encoding to be `'string'` instead of `'buffer'`. All original tests pass without change and I've added new tests for this behavior.

### Details

The bug was with this falsey check in the `inferEncoding` function:

```javascript
if (!firstBuffer) return 'buffer'
```

The empty string was matching this case. I've removed this case which allows the `typeof firstBuffer === 'string'` case to match. Other falsey values will still fall through all other cases and hit `return 'buffer'` at the end of the function.