mirror of
https://github.com/denoland/deno.git
synced 2025-09-30 06:04:48 +00:00
feat(cli): add copy argument to Buffer.bytes (#6697)
This commit is contained in:
parent
63edeb1c36
commit
1a96a96e10
3 changed files with 47 additions and 8 deletions
|
@ -39,7 +39,8 @@ export class Buffer implements Reader, ReaderSync, Writer, WriterSync {
|
||||||
this.#buf = new Uint8Array(ab);
|
this.#buf = new Uint8Array(ab);
|
||||||
}
|
}
|
||||||
|
|
||||||
bytes(): Uint8Array {
|
bytes(options: { copy?: boolean } = { copy: true }): Uint8Array {
|
||||||
|
if (options.copy === false) return this.#buf.subarray(this.#off);
|
||||||
return this.#buf.slice(this.#off);
|
return this.#buf.slice(this.#off);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
8
cli/js/lib.deno.ns.d.ts
vendored
8
cli/js/lib.deno.ns.d.ts
vendored
|
@ -820,10 +820,12 @@ declare namespace Deno {
|
||||||
*
|
*
|
||||||
* The slice is valid for use only until the next buffer modification (that
|
* The slice is valid for use only until the next buffer modification (that
|
||||||
* is, only until the next call to a method like `read()`, `write()`,
|
* is, only until the next call to a method like `read()`, `write()`,
|
||||||
* `reset()`, or `truncate()`). The slice aliases the buffer content at
|
* `reset()`, or `truncate()`). If `options.copy` is false the slice aliases the buffer content at
|
||||||
* least until the next buffer modification, so immediate changes to the
|
* least until the next buffer modification, so immediate changes to the
|
||||||
* slice will affect the result of future reads. */
|
* slice will affect the result of future reads.
|
||||||
bytes(): Uint8Array;
|
* @param options Defaults to `{ copy: true }`
|
||||||
|
*/
|
||||||
|
bytes(options?: { copy?: boolean }): Uint8Array;
|
||||||
/** Returns whether the unread portion of the buffer is empty. */
|
/** Returns whether the unread portion of the buffer is empty. */
|
||||||
empty(): boolean;
|
empty(): boolean;
|
||||||
/** A read only number of bytes of the unread portion of the buffer. */
|
/** A read only number of bytes of the unread portion of the buffer. */
|
||||||
|
|
|
@ -402,14 +402,50 @@ unitTest(function testWriteAllSync(): void {
|
||||||
});
|
});
|
||||||
|
|
||||||
unitTest(function testBufferBytesArrayBufferLength(): void {
|
unitTest(function testBufferBytesArrayBufferLength(): void {
|
||||||
const bytes = new TextEncoder().encode("a");
|
// defaults to copy
|
||||||
|
const args = [{}, { copy: undefined }, undefined, { copy: true }];
|
||||||
|
for (const arg of args) {
|
||||||
|
const bufSize = 64 * 1024;
|
||||||
|
const bytes = new TextEncoder().encode("a".repeat(bufSize));
|
||||||
|
const reader = new Deno.Buffer();
|
||||||
|
Deno.writeAllSync(reader, bytes);
|
||||||
|
|
||||||
|
const writer = new Deno.Buffer();
|
||||||
|
writer.readFromSync(reader);
|
||||||
|
const actualBytes = writer.bytes(arg);
|
||||||
|
|
||||||
|
assertEquals(actualBytes.byteLength, bufSize);
|
||||||
|
assert(actualBytes.buffer !== writer.bytes(arg).buffer);
|
||||||
|
assertEquals(actualBytes.byteLength, actualBytes.buffer.byteLength);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
unitTest(function testBufferBytesCopyFalse(): void {
|
||||||
|
const bufSize = 64 * 1024;
|
||||||
|
const bytes = new TextEncoder().encode("a".repeat(bufSize));
|
||||||
const reader = new Deno.Buffer();
|
const reader = new Deno.Buffer();
|
||||||
Deno.writeAllSync(reader, bytes);
|
Deno.writeAllSync(reader, bytes);
|
||||||
|
|
||||||
const writer = new Deno.Buffer();
|
const writer = new Deno.Buffer();
|
||||||
writer.readFromSync(reader);
|
writer.readFromSync(reader);
|
||||||
const actualBytes = writer.bytes();
|
const actualBytes = writer.bytes({ copy: false });
|
||||||
|
|
||||||
assertEquals(bytes.byteLength, 1);
|
assertEquals(actualBytes.byteLength, bufSize);
|
||||||
assertEquals(bytes.byteLength, actualBytes.buffer.byteLength);
|
assertEquals(actualBytes.buffer, writer.bytes({ copy: false }).buffer);
|
||||||
|
assert(actualBytes.buffer.byteLength > actualBytes.byteLength);
|
||||||
|
});
|
||||||
|
|
||||||
|
unitTest(function testBufferBytesCopyFalseGrowExactBytes(): void {
|
||||||
|
const bufSize = 64 * 1024;
|
||||||
|
const bytes = new TextEncoder().encode("a".repeat(bufSize));
|
||||||
|
const reader = new Deno.Buffer();
|
||||||
|
Deno.writeAllSync(reader, bytes);
|
||||||
|
|
||||||
|
const writer = new Deno.Buffer();
|
||||||
|
writer.grow(bufSize);
|
||||||
|
writer.readFromSync(reader);
|
||||||
|
const actualBytes = writer.bytes({ copy: false });
|
||||||
|
|
||||||
|
assertEquals(actualBytes.byteLength, bufSize);
|
||||||
|
assertEquals(actualBytes.buffer.byteLength, actualBytes.byteLength);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue