Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

45 lines (40 sloc) 1.489 kb
{EventEmitter} = require 'events'
bufInsert = (buf, i, buf2) ->
newbuf = new Buffer buf.length + buf2.length
buf.copy newbuf, 0, 0, i
buf2.copy newbuf, i
buf.copy newbuf, i+buf2.length, i
exports.InsertingStream = class InsertingStream extends EventEmitter
constructor: (searchString, appendString, callback) ->
@searchBuffer = new Buffer searchString
console.error " searching for '#{searchString}'"
@appendBuffer = new Buffer appendString
# This is the next expected character.
# Position 0 means: Expect the first character.
@searchPositions = [0]
if callback?
@on 'data', callback
write: (chunk) ->
i = 0
#console.log "chunktype: #{}"
while i < chunk.length
byte = chunk[i]
# can't write this as a comprehension because of a CS bug
# (see
oldSearchPositions = @searchPositions
@searchPositions = [0]
for searchPosition in oldSearchPositions
expectedByte = @searchBuffer[searchPosition]
if byte is expectedByte
if searchPosition is @searchBuffer.length
chunk = bufInsert chunk, i+1, @appendBuffer
console.error ' inserting...'
@searchPositions.push searchPosition
@_write chunk
_write: (chunk) ->
#console.error "inserting stream writes data"
@emit 'data', chunk
Jump to Line
Something went wrong with that request. Please try again.