i haven't looked at all the edge cases, but can buffer.length - end ever be less than 0? if so, i suspect this could throw. is it worth wrapping it in a Math.max(0, ...)?