basant307/AI_Governance_Project
048
1/*global module, process*/2var Buffer = require('safe-buffer').Buffer;3var Stream = require('stream');4var util = require('util');5 6function DataStream(data) {7 this.buffer = null;8 this.writable = true;9 this.readable = true;10 11 // No input12 if (!data) {13 this.buffer = Buffer.alloc(0);14 return this;15 }16 17 // Stream18 if (typeof data.pipe === 'function') {19 this.buffer = Buffer.alloc(0);20 data.pipe(this);21 return this;22 }23 24 // Buffer or String25 // or Object (assumedly a passworded key)26 if (data.length || typeof data === 'object') {27 this.buffer = data;28 this.writable = false;29 process.nextTick(function () {30 this.emit('end', data);31 this.readable = false;32 this.emit('close');33 }.bind(this));34 return this;35 }36 37 throw new TypeError('Unexpected data type ('+ typeof data + ')');38}39util.inherits(DataStream, Stream);40 41DataStream.prototype.write = function write(data) {42 this.buffer = Buffer.concat([this.buffer, Buffer.from(data)]);43 this.emit('data', data);44};45 46DataStream.prototype.end = function end(data) {47 if (data)48 this.write(data);49 this.emit('end', data);50 this.emit('close');51 this.writable = false;52 this.readable = false;53};54 55module.exports = DataStream;56 