Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

ByteSequence

import { ByteSequence } from "https://raw.githubusercontent.com/i-xi-dev/bytes.es/4.1.10/mod.ts";

Byte sequence

class ByteSequence {
private constructor(bytes: ArrayBuffer);
get byteLength(): number;
get size(): BytesSize;
get buffer(): ArrayBuffer;
get sha256Integrity(): Promise<string>;
get sha384Integrity(): Promise<string>;
get sha512Integrity(): Promise<string>;
 
at(index: number): number | undefined;
duplicate(): ByteSequence;
equals(otherBytes: Bytes): boolean;
format(options?: BytesFormat.Options): string;
getDataView(byteOffset?: number, byteLength?: number): DataView;
getUint8View(byteOffset?: number, byteLength?: number): Uint8Array;
getView<T extends ArrayBufferView>(
byteOffset?,
byteLength?: number,
): T;
segment(segmentByteLength: number): IterableIterator<ByteSequence>;
startsWith(otherBytes: Bytes): boolean;
subsequence(start: number, end?: number): ByteSequence;
toArray(): Array<number>;
toArrayBuffer(): ArrayBuffer;
toArrayBufferView<T extends ArrayBufferView>(ctor?: BufferUtils.ArrayBufferViewConstructor<T>): T;
toBase64Encoded(options?: Base64.Options): string;
toBinaryString(): string;
toBlob(options?: BlobPropertyBag): Blob;
toDataURL(options?: BlobPropertyBag): URL;
toDataView(): DataView;
async toDigest(algorithm: Digest.Algorithm): Promise<ByteSequence>;
toFile(fileName: string, options?: FilePropertyBag): File;
toJSON(): Array<number>;
toPercentEncoded(options?: Percent.Options): string;
toRequest(url: string, options: RequestInit): Request;
toResponse(options: ResponseInit): Response;
toSha256Digest(): Promise<ByteSequence>;
toSha384Digest(): Promise<ByteSequence>;
toSha512Digest(): Promise<ByteSequence>;
toStream(): ReadableStream<Uint8Array>;
toString(): string;
toText(decoder?: {
decode: (input?: Uint8Array) => string;
}
): string;
toUint8Array(): Uint8Array;
[Symbol.iterator](): IterableIterator<number>;
 
static allocate(byteLength: number): ByteSequence;
static from(sourceBytes: Bytes): ByteSequence;
static fromArray(byteArray: Array<number>): ByteSequence;
static fromArrayBuffer(buffer: ArrayBuffer): ByteSequence;
static fromArrayBufferView(bufferView: ArrayBufferView): ByteSequence;
static fromBase64Encoded(base64Encoded: string, options?: Base64.Options): ByteSequence;
static fromBinaryString(binaryString: string): ByteSequence;
static async fromBlob(blob: Blob): Promise<ByteSequence>;
static fromBufferSource(bufferSource: BufferSource): ByteSequence;
static fromDataURL(dataUrl: URL | string): ByteSequence;
static fromPercentEncoded(percentEncoded: string, options?: Percent.Options): ByteSequence;
static async fromRequestOrResponse(reqOrRes: Request | Response, options?: RequestOrResponseReadingOptions): Promise<ByteSequence>;
static async fromStream(
options?: Reading.Options,
onProgressChange?: (event: ProgressEvent) => void,
): Promise<ByteSequence>;
static fromText(text: string, encoder?: {
encode: (input?: string) => Uint8Array;
}
): ByteSequence;
static generateRandom(byteLength: number): ByteSequence;
static of(...bytes: Array<number>): ByteSequence;
static parse(formattedBytes: string, options?: BytesFormat.Options): ByteSequence;
static async withMetadataFromBlob(blob: Blob): Promise<{
data: ByteSequence;
options?: BlobPropertyBag | FilePropertyBag;
fileName?: string;
}
>
;
static withMetadataFromDataURL(dataUrl: URL | string): {
data: ByteSequence;
options?: BlobPropertyBag | FilePropertyBag;
}
;
static async withMetadataFromRequestOrResponse(reqOrRes: Request | Response, options?: RequestOrResponseReadingOptions): Promise<{
data: ByteSequence;
options?: BlobPropertyBag | FilePropertyBag;
fileName?: string;
}
>
;
static wrapArrayBuffer(buffer: ArrayBuffer): ByteSequence;
}

§Constructors

§
new ByteSequence(bytes: ArrayBuffer) private
[src]
@param bytes
  • An ArrayBuffer object.

§Properties

§
byteLength: number readonly
[src]

Gets the number of bytes.

@example
const bytes = ByteSequence.allocate(1024);
const byteCount = bytes.byteLength;
// byteCount
//   → 1024
@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const byteCount = bytes.byteLength;
// byteCount
//   → 8
§
size: BytesSize readonly
[src]

Gets the number of bytes as BytesSize.

@example
const bytes = ByteSequence.allocate(1024);
const kib = bytes.size.to("kibibyte");
// kib
//   → 1
@example
const bytes = ByteSequence.allocate(5_120_000);
const kib = bytes.size.to("kibibyte");
// (new Intl.NumberFormat("en")).format(kib) + " KiB"
//   → "5,000 KiB"
@example
const bytes = ByteSequence.allocate(5_000_000);
const kb = bytes.size.to("kilobyte");
// (new Intl.NumberFormat("en", { style: "unit", unit: "kilobyte" })).format(kb)
//   → "5,000 kB"
§
buffer: ArrayBuffer readonly
[src]

Gets the underlying ArrayBuffer.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const bufferRef = new Uint8Array(bytes.buffer);
bufferRef[0] = 0x0;
// bufferRef
//   → Uint8Array[ 0x0, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]

// new Uint8Array(bytes.buffer)
//   → Uint8Array[ 0x0, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
sha256Integrity: Promise<string> readonly
[src]

Returns the Promise that fulfills with a SRI integrity string with Base64-encoded SHA-256 digest for this byte sequence.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const integrity = await bytes.sha256Integrity;
// integrity
//   → "sha256-4pSrnUKfmpomeNmW5dvUDL9iNjpe1Bf2VMXwuoYeQgA="
§
sha384Integrity: Promise<string> readonly
[src]

Returns the Promise that fulfills with a SRI integrity string with Base64-encoded SHA-384 digest for this byte sequence.

§
sha512Integrity: Promise<string> readonly
[src]

Returns the Promise that fulfills with a SRI integrity string with Base64-encoded SHA-512 digest for this byte sequence.

§Methods

§
at(index: number): number | undefined
[src]
§
duplicate(): ByteSequence
[src]

Returns a new instance of ByteSequence with new underlying ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.

@return

A new ByteSequence object.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const clone = bytes.duplicate();

clone.getUint8View()[0] = 0;
// clone.toArray()
//   → Uint8Array[ 0x0, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]

// bytes.toArray()
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
equals(otherBytes: Bytes): boolean
[src]

Determines whether this byte sequence is equal to the byte sequence represented by another object.

@param otherBytes
  • The object that represents a byte sequence.
@return

If this is equal to the specified byte sequence, true; otherwise, false.

§
format(options?: BytesFormat.Options): string
[src]

Returns the string contains formatted bytes.

@param options
  • The BytesFormat.Options dictionary.
@return

The string contains formatted bytes.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const str = bytes.format();
// str
//   → "E5AF8CE5A3ABE5B1B1"
@example
const options = {
  lowerCase: true,
};
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const str = bytes.format(options);
// str
//   → "e5af8ce5a3abe5b1b1"
§
getDataView(byteOffset?: number, byteLength?: number): DataView
[src]

Returns the DataView that views the underlying ArrayBuffer of this instance.

@param byteOffset
  • The offset, in bytes.
@param byteLength
  • The length of the ArrayBufferView, in bytes.
@return

The DataView.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const dataViewPart = bytes.getDataView(6, 3);
// dataViewPart
//   → Uint8Array[ 0xE5, 0xB1, 0xB1 ]

dataViewPart.setUint8(0, 0);
dataViewPart.setUint8(1, 0);
dataViewPart.setUint8(2, 0);

// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0x0, 0x0, 0x0 ]
§
getUint8View(byteOffset?: number, byteLength?: number): Uint8Array
[src]

Returns the Uint8Array that views the underlying ArrayBuffer of this instance.

@param byteOffset
  • The offset, in bytes.
@param byteLength
  • The length of the ArrayBufferView, in bytes.
@return

The Uint8Array.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8ViewPart = bytes.getUint8View(6, 3);
// uint8ViewPart
//   → Uint8Array[ 0xE5, 0xB1, 0xB1 ]

uint8ViewPart.fill(0);

// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0x0, 0x0, 0x0 ]
§
getView<T extends ArrayBufferView>(ctor?: BufferUtils.ArrayBufferViewConstructor<T>, byteOffset?, byteLength?: number): T
[src]

Returns the ArrayBufferView that views the underlying ArrayBuffer of this instance.

@param ctor
  • The constructor of ArrayBufferView. The default is Uint8Array.
@param byteOffset
  • The offset, in bytes.
@param byteLength
  • The length of the ArrayBufferView, in bytes.
@return

The ArrayBufferView.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8ViewPart = bytes.getView(Uint8Array, 6, 3);
// uint8ViewPart
//   → Uint8Array[ 0xE5, 0xB1, 0xB1 ]

uint8ViewPart.fill(0);

// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0x0, 0x0, 0x0 ]
§
segment(segmentByteLength: number): IterableIterator<ByteSequence>
[src]

Returns a new iterator that contains byte sequences divided by the specified length.

@param segmentByteLength
  • The segment length, in bytes.
@return

A new iterator.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const subsequenceClones = [ ...bytes.segment(3) ];
// subsequenceClones[0]
//   → Uint8Array[ 0xE5, 0xAF, 0x8C ]
// subsequenceClones[1]
//   → Uint8Array[ 0xE5, 0xA3, 0xAB ]
// subsequenceClones[2]
//   → Uint8Array[ 0xE5, 0xB1, 0xB1 ]
§
startsWith(otherBytes: Bytes): boolean
[src]

Determines whether this byte sequence starts with the specified byte sequence.

@param otherBytes
  • The object that represents a byte sequence.
@return

If this starts with the specified byte sequence, true; otherwise, false.

§
subsequence(start: number, end?: number): ByteSequence
[src]

Returns a new instance of ByteSequence with new underlying ArrayBuffer duplicated from a subsequence of the underlying ArrayBuffer of this instance.

@param start
  • The subsequence start index.
@param end
  • The subsequence end index.
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const subsequenceClone = bytes.subsequence(6, 9);

subsequenceClone.getUint8View()[0] = 0;
// subsequenceClone.toArray()
//   → Uint8Array[ 0x0, 0xB1, 0xB1 ]

// bytes.toArray()
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
toArray(): Array<number>
[src]

Returns the 8-bit unsigned integer Array representing this byte sequence.

@return

The Array of 8-bit unsigned integers.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const array = bytes.toArray();
// array
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
toArrayBuffer(): ArrayBuffer
[src]

Returns the ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.

@return

The ArrayBuffer.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const dstBuffer = bytes.toArrayBuffer();
dstBuffer[0] = 0x0;
const dstView = new Uint8Array(dstBuffer);
// dstView
//   → Uint8Array[ 0x0, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]

const srcView = new Uint8Array(bytes.buffer);
// srcView
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
toArrayBufferView<T extends ArrayBufferView>(ctor?: BufferUtils.ArrayBufferViewConstructor<T>): T
[src]

Returns the ArrayBufferView that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.

@param ctor
  • The ArrayBufferViews constructor. The default is Uint8Array.
@return

The ArrayBufferView.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8Array = bytes.toArrayBufferView(Uint8ClampedArray);
// uint8Array
//   → Uint8ClampedArray[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]

uint8Array.fill(0);
// uint8Array
//   → Uint8ClampedArray[ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

// bytes.toArrayBufferView(Uint8ClampedArray)
//   → Uint8ClampedArray[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
toBase64Encoded(options?: Base64.Options): string
[src]

Returns the string contains Base64-encoded bytes of this byte sequence.

@param options
@return

The string contains Base64-encoded bytes.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const encoded = bytes.toBase64Encoded();
// encoded
//   → "5a+M5aOr5bGx"
@example
// Base64 URL (https://datatracker.ietf.org/doc/html/rfc4648#section-5) encoding

const base64Url = {
  table: [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_" ],
  noPadding: true,
};
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const encoded = bytes.toBase64Encoded(base64Url);
// encoded
//   → "5a-M5aOr5bGx"
§
toBinaryString(): string
[src]

Returns the isomorphic decoded string of this byte sequence.

@return
@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const binaryString = bytes.toBinaryString();
// binaryString
//   → "å¯\u{8C}士山"
§
toBlob(options?: BlobPropertyBag): Blob
[src]

Returns the Blob object representing this byte sequence.

@param options
@return

The Blob object.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const blob = bytes.toBlob({ type: "application/octet-stream" });
// new Uint8Array(await blob.arrayBuffer())
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// blob.type
//   → "application/octet-stream"
§
toDataURL(options?: BlobPropertyBag): URL
[src]

Returns the data URL representing this byte sequence.

@param options
@return

The data URL.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const dataUrl = bytes.toDataURL({ type: "application/octet-stream" });
// dataUrl.toString()
//   → "data:application/octet-stream;base64,5a+M5aOr5bGx"
§
toDataView(): DataView
[src]

Returns the DataView that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.

@return

The DataView.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const dataView = bytes.toDataView();
// new Uint8Array(dataView.buffer)
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]

dataView.setUint8(0, 0);
dataView.setUint8(1, 0);
dataView.setUint8(2, 0);
dataView.setUint8(3, 0);
// new Uint8Array(dataView.buffer)
//   → Uint8Array[ 0, 0, 0, 0, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]

// new Uint8Array(bytes.toDataView().buffer)
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
toDigest(algorithm: Digest.Algorithm): Promise<ByteSequence>
[src]

Computes the digest for this byte sequence.

@param algorithm
  • The digest algorithm.
@return

The Promise that fulfills with a ByteSequence object of the digest.

@example
// Node.js

import { createHash } from "node:crypto";
const md5 = {
  // compute: (input: Uint8Array) => Promise<Uint8Array>
  async compute(input) {
    const hash = createHash("md5");
    hash.update(input);
    return hash.digest();
  }
};
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const digestBytes = await bytes.toDigest(md5);
// digestBytes.format()
//   → "52A6AD27415BD86EC64B57EFBEA27F98"
§
toFile(fileName: string, options?: FilePropertyBag): File
[src]

Returns the File object representing this byte sequence.

@param fileName
  • The file name.
@param options
  • The FilePropertyBag object, but endings property is ignored.
@return

The File object.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const file = bytes.toFile("samp.dat", {
  type: "application/octet-stream",
  lastModified: 1640995200000,
});
// new Uint8Array(await file.arrayBuffer())
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// file.name
//   → "samp.dat"
// file.type
//   → "application/octet-stream"
// file.lastModified
//   → 1640995200000
§
toJSON(): Array<number>
[src]

The alias for the toArray method.

@return

The Array of 8-bit unsigned integers.

§
toPercentEncoded(options?: Percent.Options): string
[src]

Returns the string contains Percent-encoded bytes of this byte sequence.

@param options
  • The Percent.Options dictionary.
@return

The string contains Percent-encoded bytes.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const encoded = bytes.toPercentEncoded();
// encoded
//   → "%E5%AF%8C%E5%A3%AB%E5%B1%B1"
@example
// URL component encoding

const urlComponent = {
  encodeSet: [ 0x20, 0x22, 0x23, 0x24, 0x26, 0x2B, 0x2C, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x60, 0x7B, 0x7C, 0x7D ],
};
const bytes = ByteSequence.fromText("富士山");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
const encoded = bytes.toPercentEncoded(urlComponent);
// encoded
//   → "%E5%AF%8C%E5%A3%AB%E5%B1%B1"
@example
// encoding for the value of application/x-www-form-urlencoded

const formUrlEnc = {
  encodeSet: [ 0x20, 0x22, 0x23, 0x24, 0x26, 0x2B, 0x2C, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x60, 0x7B, 0x7C, 0x7D ],
  spaceAsPlus: true,
};
const bytes = ByteSequence.fromText("富士山");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
const encoded = bytes.toPercentEncoded(formUrlEnc);
// encoded
//   → "%E5%AF%8C%E5%A3%AB%E5%B1%B1"
§
toRequest(url: string, options: RequestInit): Request
[src]
@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const request = bytes.toRequest("http://example.com/foo", {
  method: "POST",
  headers: new Headers({
    "Content-Type": "application/octet-stream",
  }),
});
// new Uint8Array(await request.arrayBuffer())
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// request.headers.get("Content-Type")
//   → "application/octet-stream"
§
toResponse(options: ResponseInit): Response
[src]
@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const response = bytes.toResponse({
  headers: new Headers({
    "Content-Type": "application/octet-stream",
  }),
});
// new Uint8Array(await response.arrayBuffer())
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// response.headers.get("Content-Type")
//   → "application/octet-stream"
§
toSha256Digest(): Promise<ByteSequence>
[src]

Computes the SHA-256 digest for this byte sequence.

@return

The Promise that fulfills with a ByteSequence object of the SHA-256 digest.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const digestBytes = await bytes.toSha256Digest();
// digestBytes.format()
//   → "E294AB9D429F9A9A2678D996E5DBD40CBF62363A5ED417F654C5F0BA861E4200"
§
toSha384Digest(): Promise<ByteSequence>
[src]

Computes the SHA-384 digest for this byte sequence.

@return

The Promise that fulfills with a ByteSequence object of the SHA-384 digest.

§
toSha512Digest(): Promise<ByteSequence>
[src]

Computes the SHA-512 digest for this byte sequence.

@return

The Promise that fulfills with a ByteSequence object of the SHA-512 digest.

§
toStream(): ReadableStream<Uint8Array>
[src]
§
toString(): string
[src]

Returns the string contains hexadecimal formatted bytes. Equivalents to the format method with no parameters.

@return

The string contains hexadecimal formatted bytes.

§
toText(decoder?: {
decode: (input?: Uint8Array) => string;
}
): string
[src]

Returns a decoded string by the specified text encoding of this bytes.

@param decoder
  • The text decoder, for example TextDecoder. The default is UTF-8 decoder, which does not add or remove BOM.
@return

A string decoded in the specified text encoding.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const text = bytes.toText();
// text
//   → "富士山"
@example
// EUC-JP decoding (Node.js)

import iconv from "iconv-lite";
const eucJp = {
  // decode: (encoded: Uint8Array) => string
  decode(encoded) {
    return iconv.decode(Buffer.from(encoded), "EUC-JP");
  },
};
const bytes = ByteSequence.of(0xC9, 0xD9, 0xBB, 0xCE, 0xBB, 0xB3);
const text = bytes.toText(eucJp);
// text
//   → "富士山"
@example
// UTF-8 decoding (remove the BOM)

const decoder = new TextDecoder("utf-8", { ignoreBOM: false });
const utf8 = {
  // decode: (encoded: Uint8Array) => string
  decode(encoded) {
    return decoder.decode(encoded);
  },
};
const bytes = ByteSequence.of(0xEF, 0xBB, 0xBF, 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const text = bytes.toText(utf8);
// text
//   → "富士山"
§
toUint8Array(): Uint8Array
[src]

Returns the Uint8Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.

@return

The Uint8Array.

@example
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8Array = bytes.toUint8Array();
// uint8Array
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
uint8Array.fill(0);
// uint8Array
//   → Uint8Array[ 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

// bytes.toUint8Array()
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
[Symbol.iterator](): IterableIterator<number>
[src]

§Static Methods

§
allocate(byteLength: number): ByteSequence
[src]

Creates a new instance of ByteSequence of the specified size. Its bytes are filled with zeros.

@param byteLength
  • The size, in bytes.
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.allocate(1024);
// bytes.byteLength
//   → 1024
§
from(sourceBytes: Bytes): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified ByteSequence, BufferSource, or 8-bit unsigned integer Array.

@param sourceBytes
  • The Bytes object represents a byte sequence.
@return

A new ByteSequence object.

§
fromArray(byteArray: Array<number>): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified 8-bit unsigned integer Array.

@param byteArray
  • The 8-bit unsigned integer Array represents a byte sequence.
@return

A new ByteSequence object.

@example
const array = [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ];
const bytes = ByteSequence.fromArray(array);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromArrayBuffer(buffer: ArrayBuffer): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer that duplicates the specified ArrayBuffer.

@param buffer
  • The ArrayBuffer.
@return

A new ByteSequence object.

@example
const srcBuffer = Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1).buffer;
const bytes = ByteSequence.fromArrayBuffer(srcBuffer);
const dstBuffer = bytes.buffer;
// (dstBuffer === srcBuffer)
//   → false
§
fromArrayBufferView(bufferView: ArrayBufferView): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer that duplicates the underlying ArrayBuffer of the specified ArrayBufferView.

@param bufferView
  • The object that represents a byte sequence.
@return

A new ByteSequence object.

@example
const uint8Array = Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const bytes = ByteSequence.fromArrayBufferView(uint8Array);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
const buffer = Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1).buffer;
const dataView = new DataView(buffer);
const bytes = ByteSequence.fromArrayBufferView(dataView);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromBase64Encoded(base64Encoded: string, options?: Base64.Options): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the string contains Base64-encoded bytes.

@param base64Encoded
  • The string to decode.
@param options
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.fromBase64Encoded("5a+M5aOr5bGx");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
// Base64 URL (https://datatracker.ietf.org/doc/html/rfc4648#section-5) decoding

const base64Url = {
  table: [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_" ],
  noPadding: true,
};
const bytes = ByteSequence.fromBase64Encoded("5a-M5aOr5bGx", base64Url);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromBinaryString(binaryString: string): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the isomorphic encoded string.

@param binaryString
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.fromBinaryString("å¯\u{8C}士山");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromBlob(blob: Blob): Promise<ByteSequence>
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified Blob object.

@param blob
  • The Blob object (including File object).
@return

The Promise that fulfills with a new ByteSequence object.

@example
const blob = new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ]);
const bytes = await ByteSequence.fromBlob(blob);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
const file = new File([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ], "samp.dat");
const bytes = await ByteSequence.fromBlob(file);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromBufferSource(bufferSource: BufferSource): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer that duplicates the underlying ArrayBuffer of the specified BufferSource.

@param bufferSource
  • The object that represents a byte sequence.
@return

A new ByteSequence object.

@example
const srcBuffer = Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1).buffer;
const bytes = ByteSequence.fromBufferSource(srcBuffer);
const dstBuffer = bytes.buffer;
// (dstBuffer === srcBuffer)
//   → false
// new Uint8Array(dstBuffer)
//   → Uint8Array[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
const uint8Array = Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const bytes = ByteSequence.fromBufferSource(uint8Array);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromDataURL(dataUrl: URL | string): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified data URL.

@param dataUrl
  • The data URL.
@return

A new ByteSequence object.

@example
const bytes = await ByteSequence.fromDataURL("data:application/octet-stream;base64,5a+M5aOr5bGx");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromPercentEncoded(percentEncoded: string, options?: Percent.Options): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the string contains Percent-encoded bytes.

@param percentEncoded
  • The string to decode.
@param options
  • The Percent.Options dictionary.
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.fromPercentEncoded("%E5%AF%8C%E5%A3%AB%E5%B1%B1");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
// URL component decoding

const urlComponent = {
  encodeSet: [ 0x20, 0x22, 0x23, 0x24, 0x26, 0x2B, 0x2C, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x60, 0x7B, 0x7C, 0x7D ],
};
const bytes = ByteSequence.fromPercentEncoded("%E5%AF%8C%E5%A3%AB%E5%B1%B1", urlComponent);
// bytes.toText()
//   → "富士山"
@example
// decoding for the value of application/x-www-form-urlencoded

const formUrlEnc = {
  encodeSet: [ 0x20, 0x22, 0x23, 0x24, 0x26, 0x2B, 0x2C, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x5B, 0x5C, 0x5D, 0x5E, 0x60, 0x7B, 0x7C, 0x7D ],
  spaceAsPlus: true,
};
const bytes = ByteSequence.fromPercentEncoded("%E5%AF%8C%E5%A3%AB%E5%B1%B1", formUrlEnc);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// bytes.toText()
//   → "富士山"
§
fromRequestOrResponse(reqOrRes: Request | Response, options?: RequestOrResponseReadingOptions): Promise<ByteSequence>
[src]
@example
const request = new Request("http://example.com/foo", {
  method: "POST",
  body: new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ]),
});
const bytes = await ByteSequence.fromRequestOrResponse(request);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
const blob = new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ]);
const response = new Response(blob);
const bytes = await ByteSequence.fromRequestOrResponse(response);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromStream(source: BytesStream.Source, options?: Reading.Options, onProgressChange?: (event: ProgressEvent) => void): Promise<ByteSequence>
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified ReadableStream of Uint8Array.

If you want to read Node.js Readable stream of Buffer, you can use stream.Readable.toWeb method (Node.js 17.0.0+)

@param source
  • The ReadableStream of Uint8Array or the async iterator of Uint8Array.
@param options
  • The BytesStream.ReadingOptions object.
@param onProgressChange
  • The ProgressEvent listener. | type | x | | :--- | :--- | | "loadstart", "progress", "loadend" | These will be fired. | | "load" | This will not fire. Instead, the Promise is resolved. | | "abort", "timeout", "error" | These will not fire. Instead, the Promise is rejected. |
@return

The Promise that fulfills with a new ByteSequence object.

@example
const blob = new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ]);
const stream = blob.stream();
const bytes = await ByteSequence.fromStream(stream);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
fromText(text: string, encoder?: {
encode: (input?: string) => Uint8Array;
}
): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer generated from the specified string by the specified text encoding.

@param text
  • The string.
@param encoder
  • The text encoder, for example TextEncoder. The default is UTF-8 encoder, which does not add or remove BOM.
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.fromText("富士山");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
// EUC-JP encoding (Node.js)

import iconv from "iconv-lite";
const eucJp = {
  // encode: (toEncode: string) => Uint8Array
  encode(toEncode) {
    return iconv.encode(toEncode, "EUC-JP");
  },
};
const bytes = ByteSequence.fromText("富士山", eucJp);
// bytes.toArray()
//   → [ 0xC9, 0xD9, 0xBB, 0xCE, 0xBB, 0xB3 ]
@example
// UTF-8 encoding (add the BOM)

const encoder = new TextEncoder();
const utf8 = {
  // encode: (toEncode: string) => Uint8Array
  encode(toEncode) {
    const prepend = toEncode.startsWith("\uFEFF") ? "" : "\uFEFF";
    return encoder.encode(prepend + toEncode);
  },
};
const bytes = ByteSequence.fromText("富士山", utf8);
// bytes.toArray()
//   → [ 0xEF, 0xBB, 0xBF, 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
generateRandom(byteLength: number): ByteSequence
[src]

Creates a new instance of ByteSequence of the specified size. Its bytes are filled with random values.

@param byteLength
  • The size, in bytes.
@return

A new ByteSequence object.

@example
const randomBytes = ByteSequence.generateRandom(1024);
// randomBytes.byteLength
//   → 1024
§
of(...bytes: Array<number>): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified 8-bit unsigned integer iterator.

@param bytes
  • The iterator of 8-bit unsigned integers.
@return

A new ByteSequence object.

§
parse(formattedBytes: string, options?: BytesFormat.Options): ByteSequence
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the string contains formatted bytes.

@param formattedBytes
  • The string to parse.
@param options
  • The BytesFormat.Options dictionary.
@return

A new ByteSequence object.

@example
const bytes = ByteSequence.parse("E5AF8CE5A3ABE5B1B1");
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
@example
const options = {
  lowerCase: true,
};
const bytes = ByteSequence.parse("e5af8ce5a3abe5b1b1", options);
// bytes.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§
withMetadataFromBlob(blob: Blob): Promise<{
data: ByteSequence;
options?: BlobPropertyBag | FilePropertyBag;
fileName?: string;
}
>
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified Blob object.

@param blob
  • The Blob object (including File object).
@return

The Promise that fulfills with a new ByteSequence and a BlobPropertyBag.

@example
const blob = new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ], { type: "application/octet-stream" });
const { data, options } = await ByteSequence.withMetadataFromBlob(blob);
// data.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// options.type
//   → "application/octet-stream"
§
withMetadataFromDataURL(dataUrl: URL | string): {
data: ByteSequence;
options?: BlobPropertyBag | FilePropertyBag;
}
[src]

Creates a new instance of ByteSequence with new underlying ArrayBuffer created from the specified data URL.

@param dataUrl
  • The data URL.
@return

A new ByteSequence object.

@example
const { data, options } = await ByteSequence.withMetadataFromDataURL("data:application/octet-stream;base64,5a+M5aOr5bGx");
// data.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// options.type
//   → "application/octet-stream"
§
withMetadataFromRequestOrResponse(reqOrRes: Request | Response, options?: RequestOrResponseReadingOptions): Promise<{
data: ByteSequence;
options?: BlobPropertyBag | FilePropertyBag;
fileName?: string;
}
>
[src]
@example
const request = new Request("http://example.com/foo", {
  method: "POST",
  headers: new Headers({
    "Content-Type": "application/octet-stream",
  }),
  body: new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ]),
});
const { data, options } = await ByteSequence.withMetadataFromRequestOrResponse(request);
// data.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// options.type
//   → "application/octet-stream"
@example
const blob = new Blob([ Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1) ]);
const response = new Response(blob, {
  headers: new Headers({
    "Content-Type": "application/octet-stream",
  }),
});
const { data, options } = await ByteSequence.withMetadataFromRequestOrResponse(response);
// data.toArray()
//   → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// options.type
//   → "application/octet-stream"
§
wrapArrayBuffer(buffer: ArrayBuffer): ByteSequence
[src]

Creates a new instance of ByteSequence with the specified underlying ArrayBuffer.

@param buffer
  • The ArrayBuffer.
@return

A new ByteSequence object.

@example
const srcBuffer = Uint8Array.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1).buffer;
const bytes = ByteSequence.wrapArrayBuffer(srcBuffer);
const srcBufferRef = bytes.buffer;
// (srcBufferRef === srcBuffer)
//   → true