2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-18 12:36:00 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { copy } from "@std/bytes/copy";
|
2022-03-05 01:20:14 +00:00
|
|
|
|
|
|
|
const MAX_SIZE = 2 ** 32 - 2;
|
|
|
|
const DEFAULT_CHUNK_SIZE = 16_640;
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/** Options for {@linkcode Buffer.bytes}. */
|
|
|
|
export interface BufferBytesOptions {
|
|
|
|
/**
|
|
|
|
* If true, {@linkcode Buffer.bytes} will return a copy of the buffered data.
|
|
|
|
*
|
|
|
|
* If false, it will return a slice to the buffer's data.
|
|
|
|
*
|
|
|
|
* @default {true}
|
|
|
|
*/
|
|
|
|
copy?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A variable-sized buffer of bytes with `readable` and `writable` getters that
|
|
|
|
* allows you to work with {@link https://developer.mozilla.org/en-US/docs/Web/API/Streams_API | Web Streams API}.
|
2022-03-05 01:20:14 +00:00
|
|
|
*
|
|
|
|
* Buffer is almost always used with some I/O like files and sockets. It allows
|
|
|
|
* one to buffer up a download from a socket. Buffer grows and shrinks as
|
|
|
|
* necessary.
|
|
|
|
*
|
|
|
|
* Buffer is NOT the same thing as Node's Buffer. Node's Buffer was created in
|
|
|
|
* 2009 before JavaScript had the concept of ArrayBuffers. It's simply a
|
|
|
|
* non-standard ArrayBuffer.
|
|
|
|
*
|
|
|
|
* ArrayBuffer is a fixed memory allocation. Buffer is implemented on top of
|
|
|
|
* ArrayBuffer.
|
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* Based on {@link https://golang.org/pkg/bytes/#Buffer | Go Buffer}.
|
|
|
|
*
|
|
|
|
* @example Buffer input bytes and convert it to a string
|
|
|
|
* ```ts
|
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
* import { toText } from "@std/streams/to-text";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* // Create a new buffer
|
|
|
|
* const buf = new Buffer();
|
|
|
|
* assertEquals(buf.capacity, 0);
|
|
|
|
* assertEquals(buf.length, 0);
|
|
|
|
*
|
|
|
|
* // Dummy input stream
|
|
|
|
* const inputStream = ReadableStream.from([
|
|
|
|
* "hello, ",
|
|
|
|
* "world",
|
|
|
|
* "!",
|
|
|
|
* ]);
|
|
|
|
*
|
|
|
|
* // Pipe the input stream to the buffer
|
|
|
|
* await inputStream.pipeThrough(new TextEncoderStream()).pipeTo(buf.writable);
|
|
|
|
* assert(buf.capacity > 0);
|
|
|
|
* assert(buf.length > 0);
|
|
|
|
*
|
|
|
|
* // Convert the buffered bytes to a string
|
|
|
|
* const result = await toText(buf.readable);
|
|
|
|
* assertEquals(result, "hello, world!");
|
|
|
|
* assert(buf.empty());
|
|
|
|
* ```
|
|
|
|
*/
|
2022-03-05 01:20:14 +00:00
|
|
|
export class Buffer {
|
|
|
|
#buf: Uint8Array; // contents are the bytes buf[off : len(buf)]
|
|
|
|
#off = 0; // read at buf[off], write at buf[buf.byteLength]
|
|
|
|
#readable: ReadableStream<Uint8Array> = new ReadableStream({
|
|
|
|
type: "bytes",
|
|
|
|
pull: (controller) => {
|
|
|
|
const view = new Uint8Array(controller.byobRequest!.view!.buffer);
|
|
|
|
if (this.empty()) {
|
|
|
|
// Buffer is empty, reset to recover space.
|
|
|
|
this.reset();
|
|
|
|
controller.close();
|
|
|
|
controller.byobRequest!.respond(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const nread = copy(this.#buf.subarray(this.#off), view);
|
|
|
|
this.#off += nread;
|
|
|
|
controller.byobRequest!.respond(nread);
|
|
|
|
},
|
|
|
|
autoAllocateChunkSize: DEFAULT_CHUNK_SIZE,
|
|
|
|
});
|
2023-12-04 06:12:52 +00:00
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Getter returning the instance's {@linkcode ReadableStream}.
|
|
|
|
*
|
|
|
|
* @returns A `ReadableStream` of the buffer.
|
|
|
|
*
|
|
|
|
* @example Read the content out of the buffer to stdout
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const buf = new Buffer();
|
|
|
|
* await buf.readable.pipeTo(Deno.stdout.writable);
|
|
|
|
* ```
|
|
|
|
*/
|
2023-12-04 06:12:52 +00:00
|
|
|
get readable(): ReadableStream<Uint8Array> {
|
2022-03-05 01:20:14 +00:00
|
|
|
return this.#readable;
|
|
|
|
}
|
2023-12-04 06:12:52 +00:00
|
|
|
|
2022-03-05 01:20:14 +00:00
|
|
|
#writable = new WritableStream<Uint8Array>({
|
|
|
|
write: (chunk) => {
|
|
|
|
const m = this.#grow(chunk.byteLength);
|
|
|
|
copy(chunk, this.#buf, m);
|
|
|
|
},
|
|
|
|
});
|
2023-12-04 06:12:52 +00:00
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Getter returning the instance's {@linkcode WritableStream}.
|
|
|
|
*
|
|
|
|
* @returns A `WritableStream` of the buffer.
|
|
|
|
*
|
|
|
|
* @example Write the data from stdin to the buffer
|
2024-06-03 04:10:27 +00:00
|
|
|
* ```ts no-assert
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const buf = new Buffer();
|
|
|
|
* await Deno.stdin.readable.pipeTo(buf.writable);
|
|
|
|
* ```
|
|
|
|
*/
|
2023-12-04 06:12:52 +00:00
|
|
|
get writable(): WritableStream<Uint8Array> {
|
2022-03-05 01:20:14 +00:00
|
|
|
return this.#writable;
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new instance.
|
|
|
|
*
|
|
|
|
* @param ab An optional buffer to use as the initial buffer.
|
|
|
|
*/
|
2022-03-05 01:20:14 +00:00
|
|
|
constructor(ab?: ArrayBufferLike | ArrayLike<number>) {
|
|
|
|
this.#buf = ab === undefined ? new Uint8Array(0) : new Uint8Array(ab);
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Returns a slice holding the unread portion of the buffer.
|
2022-03-05 01:20:14 +00:00
|
|
|
*
|
|
|
|
* The slice is valid for use only until the next buffer modification (that
|
2024-05-28 01:27:40 +00:00
|
|
|
* is, only until the next call to a method that mutates or consumes the
|
|
|
|
* buffer, like reading data out via `readable`, `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 slice will
|
|
|
|
* affect the result of future reads. If `options` is not provided,
|
|
|
|
* `options.copy` defaults to `true`.
|
|
|
|
*
|
|
|
|
* @param options Options for the bytes method.
|
|
|
|
* @returns A copy or a slice of the buffer.
|
|
|
|
*
|
|
|
|
* @example Copy the buffer
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
|
|
|
* import { assertNotEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([0, 1, 2]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* const copied = buf.bytes();
|
|
|
|
* assertEquals(copied.length, array.length);
|
|
|
|
*
|
|
|
|
* // Modify an element in the original array
|
|
|
|
* array[1] = 99;
|
|
|
|
* assertEquals(copied[0], array[0]);
|
|
|
|
* // The copied buffer is not affected by the modification
|
|
|
|
* assertNotEquals(copied[1], array[1]);
|
|
|
|
* assertEquals(copied[2], array[2]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Get a slice to the buffer
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([0, 1, 2]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* const slice = buf.bytes({ copy: false });
|
|
|
|
* assertEquals(slice.length, array.length);
|
|
|
|
*
|
|
|
|
* // Modify an element in the original array
|
|
|
|
* array[1] = 99;
|
|
|
|
* assertEquals(slice[0], array[0]);
|
|
|
|
* // The slice _is_ affected by the modification
|
|
|
|
* assertEquals(slice[1], array[1]);
|
|
|
|
* assertEquals(slice[2], array[2]);
|
|
|
|
* ```
|
2022-03-05 01:20:14 +00:00
|
|
|
*/
|
2024-05-28 01:27:40 +00:00
|
|
|
bytes(options: BufferBytesOptions = { copy: true }): Uint8Array {
|
2022-03-05 01:20:14 +00:00
|
|
|
if (options.copy === false) return this.#buf.subarray(this.#off);
|
|
|
|
return this.#buf.slice(this.#off);
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the unread portion of the buffer is empty.
|
|
|
|
*
|
|
|
|
* @returns Whether the buffer is empty.
|
|
|
|
*
|
|
|
|
* @example Empty buffer
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const buf = new Buffer();
|
|
|
|
* assert(buf.empty());
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Non-empty buffer
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([42]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* assert(!buf.empty());
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Non-empty, but the content was already read
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([42]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* assert(!buf.empty());
|
|
|
|
* // Read the content out of the buffer
|
|
|
|
* await buf.readable.pipeTo(Deno.stdout.writable);
|
|
|
|
* // The buffer is now empty
|
|
|
|
* assert(buf.empty());
|
|
|
|
* ```
|
|
|
|
*/
|
2022-03-05 01:20:14 +00:00
|
|
|
empty(): boolean {
|
|
|
|
return this.#buf.byteLength <= this.#off;
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* A read only number of bytes of the unread portion of the buffer.
|
|
|
|
*
|
|
|
|
* @returns The number of bytes in the unread portion of the buffer.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([0, 1, 2]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* assertEquals(buf.length, 3);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Length becomes 0 after the content is read
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([42]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* assertEquals(buf.length, 1);
|
|
|
|
* // Read the content out of the buffer
|
|
|
|
* await buf.readable.pipeTo(Deno.stdout.writable);
|
|
|
|
* // The length is now 0
|
|
|
|
* assertEquals(buf.length, 0);
|
|
|
|
* ```
|
|
|
|
*/
|
2022-03-05 01:20:14 +00:00
|
|
|
get length(): number {
|
|
|
|
return this.#buf.byteLength - this.#off;
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* The read only capacity of the buffer's underlying byte slice, that is,
|
|
|
|
* the total space allocated for the buffer's data.
|
|
|
|
*
|
|
|
|
* @returns The number of allocated bytes for the buffer.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const arrayBuffer = new ArrayBuffer(256);
|
|
|
|
* const buf = new Buffer(arrayBuffer);
|
|
|
|
* assertEquals(buf.capacity, 256);
|
|
|
|
* ```
|
|
|
|
*/
|
2022-03-05 01:20:14 +00:00
|
|
|
get capacity(): number {
|
|
|
|
return this.#buf.buffer.byteLength;
|
|
|
|
}
|
|
|
|
|
2023-12-04 06:12:52 +00:00
|
|
|
/**
|
|
|
|
* Discards all but the first `n` unread bytes from the buffer but
|
2022-03-05 01:20:14 +00:00
|
|
|
* continues to use the same allocated storage. It throws if `n` is
|
2023-12-04 06:12:52 +00:00
|
|
|
* negative or greater than the length of the buffer.
|
2024-05-28 01:27:40 +00:00
|
|
|
*
|
|
|
|
* @param n The number of bytes to keep.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([0, 1, 2]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* assertEquals(buf.bytes(), array);
|
|
|
|
*
|
|
|
|
* // Discard all but the first 2 bytes
|
|
|
|
* buf.truncate(2);
|
|
|
|
* assertEquals(buf.bytes(), array.slice(0, 2));
|
|
|
|
* ```
|
2023-12-04 06:12:52 +00:00
|
|
|
*/
|
|
|
|
truncate(n: number): void {
|
2022-03-05 01:20:14 +00:00
|
|
|
if (n === 0) {
|
|
|
|
this.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (n < 0 || n > this.length) {
|
2024-08-28 22:55:24 +00:00
|
|
|
throw new Error(
|
2024-08-23 03:31:01 +00:00
|
|
|
`Buffer truncation value "${n}" is not between 0 and ${this.length}`,
|
|
|
|
);
|
2022-03-05 01:20:14 +00:00
|
|
|
}
|
|
|
|
this.#reslice(this.#off + n);
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Resets to an empty buffer.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const array = new Uint8Array([0, 1, 2]);
|
|
|
|
* const buf = new Buffer(array.buffer);
|
|
|
|
* assert(!buf.empty());
|
|
|
|
*
|
|
|
|
* // Reset
|
|
|
|
* buf.reset();
|
|
|
|
* assert(buf.empty());
|
|
|
|
* ```
|
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
reset() {
|
2022-03-05 01:20:14 +00:00
|
|
|
this.#reslice(0);
|
|
|
|
this.#off = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#tryGrowByReslice(n: number) {
|
|
|
|
const l = this.#buf.byteLength;
|
|
|
|
if (n <= this.capacity - l) {
|
|
|
|
this.#reslice(l + n);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#reslice(len: number) {
|
|
|
|
this.#buf = new Uint8Array(this.#buf.buffer, 0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#grow(n: number) {
|
|
|
|
const m = this.length;
|
|
|
|
// If buffer is empty, reset to recover space.
|
|
|
|
if (m === 0 && this.#off !== 0) {
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
// Fast: Try to grow by means of a reslice.
|
|
|
|
const i = this.#tryGrowByReslice(n);
|
|
|
|
if (i >= 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
const c = this.capacity;
|
|
|
|
if (n <= Math.floor(c / 2) - m) {
|
|
|
|
// We can slide things down instead of allocating a new
|
|
|
|
// ArrayBuffer. We only need m+n <= c to slide, but
|
|
|
|
// we instead let capacity get twice as large so we
|
|
|
|
// don't spend all our time copying.
|
|
|
|
copy(this.#buf.subarray(this.#off), this.#buf);
|
|
|
|
} else if (c + n > MAX_SIZE) {
|
2024-08-23 03:31:01 +00:00
|
|
|
throw new Error(
|
|
|
|
`The buffer cannot grow beyond the maximum size of ${MAX_SIZE}`,
|
|
|
|
);
|
2022-03-05 01:20:14 +00:00
|
|
|
} else {
|
|
|
|
// Not enough space anywhere, we need to allocate.
|
|
|
|
const buf = new Uint8Array(Math.min(2 * c + n, MAX_SIZE));
|
|
|
|
copy(this.#buf.subarray(this.#off), buf);
|
|
|
|
this.#buf = buf;
|
|
|
|
}
|
|
|
|
// Restore this.#off and len(this.#buf).
|
|
|
|
this.#off = 0;
|
|
|
|
this.#reslice(Math.min(m + n, MAX_SIZE));
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2024-05-28 01:27:40 +00:00
|
|
|
/**
|
|
|
|
* Grows the buffer's capacity, if necessary, to guarantee space for
|
2022-03-05 01:20:14 +00:00
|
|
|
* another `n` bytes. After `.grow(n)`, at least `n` bytes can be written to
|
|
|
|
* the buffer without another allocation. If `n` is negative, `.grow()` will
|
|
|
|
* throw. If the buffer can't grow it will throw an error.
|
|
|
|
*
|
2024-05-28 01:27:40 +00:00
|
|
|
* @param n The number of bytes to grow the buffer by.
|
|
|
|
*
|
2022-03-05 01:20:14 +00:00
|
|
|
* Based on Go Lang's
|
2024-05-28 01:27:40 +00:00
|
|
|
* {@link https://golang.org/pkg/bytes/#Buffer.Grow | Buffer.Grow}.
|
|
|
|
*
|
|
|
|
* @example Basic usage
|
|
|
|
* ```ts
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assert } from "@std/assert";
|
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-28 01:27:40 +00:00
|
|
|
* import { Buffer } from "@std/streams/buffer";
|
|
|
|
*
|
|
|
|
* const buf = new Buffer();
|
|
|
|
* assertEquals(buf.capacity, 0);
|
|
|
|
*
|
|
|
|
* buf.grow(200);
|
|
|
|
* assert(buf.capacity >= 200);
|
|
|
|
* ```
|
|
|
|
*/
|
2022-08-24 01:21:57 +00:00
|
|
|
grow(n: number) {
|
2022-03-05 01:20:14 +00:00
|
|
|
if (n < 0) {
|
2024-08-28 22:55:24 +00:00
|
|
|
throw new Error(
|
2024-08-23 03:31:01 +00:00
|
|
|
`Cannot grow buffer as growth must be positive: received ${n}`,
|
|
|
|
);
|
2022-03-05 01:20:14 +00:00
|
|
|
}
|
|
|
|
const m = this.#grow(n);
|
|
|
|
this.#reslice(m);
|
|
|
|
}
|
|
|
|
}
|