« index

Coverage for /Users/kris/q-io/buffer-stream.js : 89%

57 lines | 51 run | 6 missing | 0 partial | 14 blocks | 9 blocks run | 5 blocks missing

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

  var Q = require("q");
  var Reader = require("./reader");
  
  module.exports = BufferStream;
  function BufferStream(chunks, charset) {
      if (!(this instanceof BufferStream)) {
          return new BufferStream(chunks, charset);
      }
      if (!chunks) {
          chunks = [];
      } else if (!Array.isArray(chunks)) {
          chunks = [chunks];
      }
      this._charset = charset;
      this._chunks = chunks;
      this._close = Q.defer();
      this.closed = this._close.promise;
  }
  
  BufferStream.prototype.forEach = function (write, thisp) {
      var self = this;
      var chunks = self._chunks;
      return Q.fcall(function () {
          chunks.splice(0, chunks.length).forEach(write, thisp);
      });
  };
  
  BufferStream.prototype.read = function () {
      var result;
      result = Reader.join(this._chunks);
      if (this._charset) {
          result = result.toString(this._charset);
      }
      return Q.resolve(result);
  };
  
  BufferStream.prototype.write = function (chunk) {
      if (this._charset) {
          chunk = new Buffer(String(chunk), this._charset);
      } else {
          if (!(chunk instanceof Buffer)) {
              throw new Error("Can't write strings to buffer stream without a charset: " + chunk);
          }
      }
      this._chunks.push(chunk);
      return Q.resolve();
  };
  
  BufferStream.prototype.close = function () {
      this._close.resolve();
      return Q.resolve();
  };
  
  BufferStream.prototype.destroy = function () {
      this._close.resolve();
      return Q.resolve();
  };
« index | cover.io