Skip to content

Instantly share code, notes, and snippets.

@curtkim
Last active August 29, 2015 14:08
Show Gist options
  • Save curtkim/c804bc660afed829a978 to your computer and use it in GitHub Desktop.
Save curtkim/c804bc660afed829a978 to your computer and use it in GitHub Desktop.
nodejs
############################################
# [Stream PassThrough TrafficMeter]
util = require 'util'
PassThrough = require('stream').PassThrough
clientTraffic = {}
class TrafficMeter extends PassThrough
constructor: (opt)->
@client = opt.client
super()
_transform: (chunk, encoding, callback)->
clientTraffic[@client] = 0 unless clientTraffic[@client]
clientTraffic[@client] += chunk.length
@push(chunk)
callback()
process.stdin.pipe(new TrafficMeter({client: 'solmail'})).pipe(process.stdout)
print_count = ()-> console.log clientTraffic
setInterval print_count, 1000
############################################
# [Dummy Readable Stream]
s = new require("stream").Readable()
s._read = ()->
s.push JSON.stringify({name:'123'})
s.push '\n\n'
s.push 'bodybody'
s.push null
s.pipe(parser)
############################################
# [StringArrayStream]
Readable = require("stream").Readable
class StringArrayStream extends Readable
constructor: (@arr) ->
super()
_read: (size) ->
for str in @arr
@push str
@push null
source = new StringArrayStream([
JSON.stringify({name:'123'})
'\n\n'
'bodybody'
])
source.pipe(parser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment