CoolFace
Apppublic

AK-21/Graphite-Industrial-Intelligence

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
streams.js106 linesDownload Raw Back to lib
1"use strict"2 3var Buffer = require("safer-buffer").Buffer4 5// NOTE: Due to 'stream' module being pretty large (~100Kb, significant in browser environments),6// we opt to dependency-inject it instead of creating a hard dependency.7module.exports = function (streamModule) {8  var Transform = streamModule.Transform9 10  // == Encoder stream =======================================================11 12  function IconvLiteEncoderStream (conv, options) {13    this.conv = conv14    options = options || {}15    options.decodeStrings = false // We accept only strings, so we don't need to decode them.16    Transform.call(this, options)17  }18 19  IconvLiteEncoderStream.prototype = Object.create(Transform.prototype, {20    constructor: { value: IconvLiteEncoderStream }21  })22 23  IconvLiteEncoderStream.prototype._transform = function (chunk, encoding, done) {24    if (typeof chunk !== "string") {25      return done(new Error("Iconv encoding stream needs strings as its input."))26    }27 28    try {29      var res = this.conv.write(chunk)30      if (res && res.length) this.push(res)31      done()32    } catch (e) {33      done(e)34    }35  }36 37  IconvLiteEncoderStream.prototype._flush = function (done) {38    try {39      var res = this.conv.end()40      if (res && res.length) this.push(res)41      done()42    } catch (e) {43      done(e)44    }45  }46 47  IconvLiteEncoderStream.prototype.collect = function (cb) {48    var chunks = []49    this.on("error", cb)50    this.on("data", function (chunk) { chunks.push(chunk) })51    this.on("end", function () {52      cb(null, Buffer.concat(chunks))53    })54    return this55  }56 57  // == Decoder stream =======================================================58 59  function IconvLiteDecoderStream (conv, options) {60    this.conv = conv61    options = options || {}62    options.encoding = this.encoding = "utf8" // We output strings.63    Transform.call(this, options)64  }65 66  IconvLiteDecoderStream.prototype = Object.create(Transform.prototype, {67    constructor: { value: IconvLiteDecoderStream }68  })69 70  IconvLiteDecoderStream.prototype._transform = function (chunk, encoding, done) {71    if (!Buffer.isBuffer(chunk) && !(chunk instanceof Uint8Array)) { return done(new Error("Iconv decoding stream needs buffers as its input.")) }72    try {73      var res = this.conv.write(chunk)74      if (res && res.length) this.push(res, this.encoding)75      done()76    } catch (e) {77      done(e)78    }79  }80 81  IconvLiteDecoderStream.prototype._flush = function (done) {82    try {83      var res = this.conv.end()84      if (res && res.length) this.push(res, this.encoding)85      done()86    } catch (e) {87      done(e)88    }89  }90 91  IconvLiteDecoderStream.prototype.collect = function (cb) {92    var res = ""93    this.on("error", cb)94    this.on("data", function (chunk) { res += chunk })95    this.on("end", function () {96      cb(null, res)97    })98    return this99  }100 101  return {102    IconvLiteEncoderStream: IconvLiteEncoderStream,103    IconvLiteDecoderStream: IconvLiteDecoderStream104  }105}106