Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2661,9 +2661,10 @@ export class Writer {

/**
* Finishes the write operation.
* @param {Uint8Array} [buf] Optional buffer that's 'this.len' bytes long
* @returns Finished buffer
*/
public finish(): Uint8Array;
public finish(buf?: Uint8Array): Uint8Array;
}

/** Wire format writer using node buffers. */
Expand All @@ -2681,9 +2682,10 @@ export class BufferWriter extends Writer {

/**
* Finishes the write operation.
* @param {Buffer} [buf] Optional buffer that's 'this.len' bytes long
* @returns Finished buffer
*/
public finish(): Buffer;
public finish(buf?: Buffer): Buffer;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,20 @@ Writer.prototype.ldelim = function ldelim() {

/**
* Finishes the write operation.
* @param {Uint8Array} [buf] Optional buffer that's 'this.len' bytes long
* @returns {Uint8Array} Finished buffer
*/
Writer.prototype.finish = function finish() {
Writer.prototype.finish = function finish(buf) {
if (buf == null) {
buf = this.constructor.alloc(this.len);
}
var head = this.head.next, // skip noop
buf = this.constructor.alloc(this.len),
pos = 0;
while (head) {
head.fn(head.val, buf, pos);
pos += head.len;
head = head.next;
}
// this.head = this.tail = null;
return buf;
};

Expand Down