To avoid this error, you can limit the number of bytes with the "length" parameter.
buf.write(string[, offset[, length]][, encoding])