-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
45 lines (38 loc) · 914 Bytes
/
index.js
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
/**!
* contentstream - index.js
*
* Copyright(c) fengmk2 and other contributors.
* MIT Licensed
*
* Authors:
* fengmk2 <[email protected]> (http://fengmk2.github.com)
*/
'use strict';
/**
* Module dependencies.
*/
var Readable = require('readable-stream').Readable;
var util = require('util');
module.exports = ContentStream;
function ContentStream(obj, options) {
if (!(this instanceof ContentStream)) {
return new ContentStream(obj, options);
}
Readable.call(this, options);
if (obj === null || obj === undefined) {
obj = String(obj);
}
this._obj = obj;
}
util.inherits(ContentStream, Readable);
ContentStream.prototype._read = function (n) {
var obj = this._obj;
if (typeof obj === 'string') {
this.push(new Buffer(obj));
} else if (Buffer.isBuffer(obj)) {
this.push(obj);
} else {
this.push(new Buffer(JSON.stringify(obj)));
}
this.push(null);
};