ByteSequence
import { ByteSequence } from "https://raw.githubusercontent.com/i-xi-dev/bytes.es/4.4.2/mod.ts";Byte sequence
§Properties
Gets the number of bytes.
const bytes = ByteSequence.allocate(1024);
const byteCount = bytes.byteLength;
// byteCount
// → 1024
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const byteCount = bytes.byteLength;
// byteCount
// → 8
Gets the number of bytes as BytesSize.
const bytes = ByteSequence.allocate(1024);
const kib = bytes.size.to("kibibyte");
// kib
// → 1
const bytes = ByteSequence.allocate(5_120_000);
const kib = bytes.size.to("kibibyte");
// (new Intl.NumberFormat("en")).format(kib) + " KiB"
// → "5,000 KiB"
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"
Gets the underlying ArrayBuffer.
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 ]
Returns the Promise that fulfills with a SRI integrity string with Base64-encoded SHA-256 digest for this byte sequence.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const integrity = await bytes.sha256Integrity;
// integrity
// → "sha256-4pSrnUKfmpomeNmW5dvUDL9iNjpe1Bf2VMXwuoYeQgA="
Returns the Promise that fulfills with a SRI integrity string with Base64-encoded SHA-384 digest for this byte sequence.
Returns the Promise that fulfills with a SRI integrity string with Base64-encoded SHA-512 digest for this byte sequence.
§Methods
Returns the BigInt64Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
BigInt64Array, in bytes.
The BigInt64Array.
Returns the BigUint64Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
BigUint64Array, in bytes.
The BigUint64Array.
Returns the DataView that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
ArrayBufferView, in bytes.
The DataView.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const dataViewPart = bytes.asDataView(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 ]
Returns the Float32Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Float32Array, in bytes.
The Float32Array.
Returns the Float64Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Float64Array, in bytes.
The Float64Array.
Returns the Int16Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Int16Array, in bytes.
The Int16Array.
Returns the Int32Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Int32Array, in bytes.
The Int32Array.
Returns the Int8Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Int8Array, in bytes.
The Int8Array.
Returns the Uint16Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Uint16Array, in bytes.
The Uint16Array.
Returns the Uint32Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Uint32Array, in bytes.
The Uint32Array.
Returns the Uint8Array that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Uint8Array, in bytes.
The Uint8Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8ViewPart = bytes.asUint8Array(6, 3);
// uint8ViewPart
// → Uint8Array[ 0xE5, 0xB1, 0xB1 ]
uint8ViewPart.fill(0);
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0x0, 0x0, 0x0 ]
Returns the Uint8ClampedArray that views the underlying ArrayBuffer of this instance.
- The offset, in bytes.
- The length of the
Uint8ClampedArray, in bytes.
The Uint8ClampedArray.
Returns a new instance of ByteSequence with new underlying ArrayBuffer
duplicated from the underlying ArrayBuffer of this instance.
A new ByteSequence object.
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 ]
Determines whether this byte sequence is equal to the byte sequence represented by another object.
- The object that represents a byte sequence.
If this is equal to the specified byte sequence, true; otherwise, false.
Returns the string contains formatted bytes.
- The
BytesFormat.Optionsdictionary.
The string contains formatted bytes.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const str = bytes.format();
// str
// → "E5AF8CE5A3ABE5B1B1"
const options = {
lowerCase: true,
};
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const str = bytes.format(options);
// str
// → "e5af8ce5a3abe5b1b1"
The alias for the asDataView method.
The alias for the asUint8Array method.
Returns the ArrayBufferView that views the underlying ArrayBuffer of this instance.
- The constructor of
ArrayBufferView. The default isUint8Array.
- The offset, in bytes.
- The length of the
ArrayBufferView, in bytes.
The ArrayBufferView.
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 ]
Returns a new iterator that contains byte sequences divided by the specified length.
- The segment length, in bytes.
A new iterator.
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 ]
Determines whether this byte sequence starts with the specified byte sequence.
- The object that represents a byte sequence.
If this starts with the specified byte sequence, true; otherwise, false.
Returns a new instance of ByteSequence with new underlying ArrayBuffer
duplicated from a subsequence of the underlying ArrayBuffer of this instance.
- The subsequence start index.
- The subsequence end index.
A new ByteSequence object.
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 ]
Returns the 8-bit unsigned integer Array representing this byte sequence.
The Array of 8-bit unsigned integers.
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 ]
Returns the ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The ArrayBuffer.
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 ]
Returns the ArrayBufferView that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
- The
ArrayBufferViews constructor. The default isUint8Array.
The ArrayBufferView.
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 ]
Returns the string contains Base64-encoded bytes of this byte sequence.
- The
Base64.Optionsdictionary.
The string contains Base64-encoded bytes.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const encoded = bytes.toBase64Encoded();
// encoded
// → "5a+M5aOr5bGx"
// Base64 URL (https://datatracker.ietf.org/doc/html/rfc4648#section-5) encoding
const base64Url = {
tableLastChars: ["-", "_"],
noPadding: true,
};
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const encoded = bytes.toBase64Encoded(base64Url);
// encoded
// → "5a-M5aOr5bGx"
Returns the BigInt64Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The BigInt64Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0, 0, 0, 0, 0);
const int64Array = bytes.toBigInt64Array();
// int64Array
// When the platform byte order is little-endian
// → BigInt64Array[ -5_627_903_438_910_345_243n, 177n ]
// When the platform byte order is big-endian
// → BigInt64Array[ -1_896_142_000_178_338_383n, -5_692_549_928_996_306_944n ]
Returns the BigUint64Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The BigUint64Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0, 0, 0, 0, 0);
const uint64Array = bytes.toBigUint64Array();
// uint64Array
// When the platform byte order is little-endian
// → BigUint64Array[ 0xB1E5ABA3E58CAFE5n, 0x00000000000000B1n ]
// When the platform byte order is big-endian
// → BigUint64Array[ 0xE5AF8CE5A3ABE5B1n, 0xB100000000000000n ]
Returns a 64-bit unsigned integer Iterable represented by this byte sequence.
- The byte order. If omitted, the platform byte order is used.
The Iterable of 64-bit unsigned integers.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1);
const uint64s = bytes.toBigUint64Iterable();
// [...uint64s]
// When the platform byte order is little-endian
// → [ 0xB1E5_ABA3_E58C_AFE5n ]
// When the platform byte order is big-endian
// → [ 0xE5AF_8CE5_A3AB_E5B1n ]
const uint64sLe = bytes.toBigUint64Iterable(ByteOrder.LITTLE_ENDIAN);
// [...uint64sLe]
// → [ 0xB1E5_ABA3_E58C_AFE5n ]
const uint64sBe = bytes.toBigUint64Iterable(ByteOrder.BIG_ENDIAN);
// [...uint64sBe]
// → [ 0xE5AF_8CE5_A3AB_E5B1n ]
Returns the isomorphic decoded string of this byte sequence.
The binary string.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const binaryString = bytes.toBinaryString();
// binaryString
// → "å¯\u{8C}士山"
Returns the Blob object representing this byte sequence.
- The
BlobPropertyBagobject, butendingsproperty is ignored.
The Blob object.
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"
Returns the data URL representing this byte sequence.
- The
BlobPropertyBagobject, butendingsproperty is ignored.
The data URL.
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"
Returns the DataView that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The DataView.
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 ]
Computes the digest for this byte sequence.
- The digest algorithm.
The Promise that fulfills with a ByteSequence object of the digest.
const md5 = {
// compute: (input: BuuferSource) => Promise<ArrayBuffer>
async compute(input) {
// ...
}
};
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const digestBytes = await bytes.toDigest(md5);
// digestBytes.format()
// → "52A6AD27415BD86EC64B57EFBEA27F98"
Returns the File object representing this byte sequence.
- The file name.
- The
FilePropertyBagobject, butendingsproperty is ignored.
The File object.
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
Returns the Float32Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Float32Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0);
const float32Array = bytes.toFloat32Array();
// float32Array
// When the platform byte order is little-endian
// → Float32Array[ -8.304699862546052e+22, -6.68429001038362e-9, 2.4802982818549262e-43 ]
// When the platform byte order is big-endian
// → Float32Array[ -1.0362664748269274e+23, -1.8637135243899587e-17, -1.862645149230957e-9 ]
Returns the Float64Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Float64Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0, 0, 0, 0, 0);
const float64Array = bytes.toFloat64Array();
// float64Array
// When the platform byte order is little-endian
// → Float64Array[ -2.5118824744887084e-68, 8.74e-322 ]
// When the platform byte order is big-endian
// → Float64Array[ -6.54593944702149e+181, -1.131959884853339e-72 ]
Returns the Int16Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Int16Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0);
const int16Array = bytes.toInt16Array();
// int16Array
// When the platform byte order is little-endian
// → Int16Array[ -20_507, -6_772, -21_597, -19_995, 177 ]
// When the platform byte order is big-endian
// → Int16Array[ -6_737, -29_467, -23_637, -6_735, -20_224 ]
Returns the Int32Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Int32Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0);
const int32Array = bytes.toInt32Array();
// int32Array
// When the platform byte order is little-endian
// → Int32Array[ -443_764_763, -1_310_348_381, 177 ]
// When the platform byte order is big-endian
// → Int32Array[ -441_479_963, -1_549_015_631, -1_325_400_064 ]
Returns the Int8Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Int8Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const int8Array = bytes.toInt8Array();
// int8Array
// → Int8Array[ -27, -81, -116, -27, -93, -85, -27, -79, -79 ]
The alias for the toArray method.
The Array of 8-bit unsigned integers.
Computes the MD5 digest for this byte sequence.
The Promise that fulfills with a ByteSequence object of the MD5 digest.
Returns the string contains Percent-encoded bytes of this byte sequence.
- The
Percent.Optionsdictionary.
The string contains Percent-encoded bytes.
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"
// 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"
// 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"
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"
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"
Computes the SHA-1 digest for this byte sequence.
The Promise that fulfills with a ByteSequence object of the SHA-1 digest.
Computes the SHA-256 digest for this byte sequence.
The Promise that fulfills with a ByteSequence object of the SHA-256 digest.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const digestBytes = await bytes.toSha256Digest();
// digestBytes.format()
// → "E294AB9D429F9A9A2678D996E5DBD40CBF62363A5ED417F654C5F0BA861E4200"
Computes the SHA-384 digest for this byte sequence.
The Promise that fulfills with a ByteSequence object of the SHA-384 digest.
Computes the SHA-512 digest for this byte sequence.
The Promise that fulfills with a ByteSequence object of the SHA-512 digest.
Returns the string contains hexadecimal formatted bytes.
Equivalents to the format method with no parameters.
The string contains hexadecimal formatted bytes.
Returns a decoded string by the specified text encoding of this bytes.
- The text decoder, for example
TextDecoder. The default is UTF-8 decoder, which does not add or remove BOM.
A string decoded in the specified text encoding.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const text = bytes.toText();
// text
// → "富士山"
// 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
// → "富士山"
// 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
// → "富士山"
Returns the Uint16Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Uint16Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0);
const uint16Array = bytes.toUint16Array();
// uint16Array
// When the platform byte order is little-endian
// → Uint16Array[ 0xAFE5, 0xE58C, 0xABA3, 0xB1E5, 0x00B1 ]
// When the platform byte order is big-endian
// → Uint16Array[ 0xE5AF, 0x8CE5, 0xA3AB, 0xE5B1, 0xB100 ]
Returns a 16-bit unsigned integer Iterable represented by this byte sequence.
- The byte order. If omitted, the platform byte order is used.
The Iterable of 16-bit unsigned integers.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0);
const uint16s = bytes.toUint16Iterable();
// [...uint16s]
// When the platform byte order is little-endian
// → [ 0xAFE5, 0xE58C, 0xABA3, 0xB1E5, 0x00B1 ]
// When the platform byte order is big-endian
// → [ 0xE5AF, 0x8CE5, 0xA3AB, 0xE5B1, 0xB100 ]
const uint16sLe = bytes.toUint16Iterable(ByteOrder.LITTLE_ENDIAN);
// [...uint16sLe]
// → [ 0xAFE5, 0xE58C, 0xABA3, 0xB1E5, 0x00B1 ]
const uint16sBe = bytes.toUint16Iterable(ByteOrder.BIG_ENDIAN);
// [...uint16sBe]
// → [ 0xE5AF, 0x8CE5, 0xA3AB, 0xE5B1, 0xB100 ]
Returns the Uint32Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Uint32Array.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0);
const uint32Array = bytes.toUint32Array();
// uint32Array
// When the platform byte order is little-endian
// → Uint32Array[ 0xE58CAFE5, 0xB1E5ABA3, 0x000000B1 ]
// When the platform byte order is big-endian
// → Uint32Array[ 0xE5AF8CE5, 0xA3ABE5B1, 0xB1000000 ]
Returns a 32-bit unsigned integer Iterable represented by this byte sequence.
- The byte order. If omitted, the platform byte order is used.
The Iterable of 32-bit unsigned integers.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1, 0, 0, 0);
const uint32s = bytes.toUint32Iterable();
// [...uint32s]
// When the platform byte order is little-endian
// → [ 0xE58C_AFE5, 0xB1E5_ABA3, 0x0000_00B1 ]
// When the platform byte order is big-endian
// → [ 0xE5AF_8CE5, 0xA3AB_E5B1, 0xB100_0000 ]
const uint32sLe = bytes.toUint32Iterable(ByteOrder.LITTLE_ENDIAN);
// [...uint32sLe]
// → [ 0xE58C_AFE5, 0xB1E5_ABA3, 0x0000_00B1 ]
const uint32sBe = bytes.toUint32Iterable(ByteOrder.BIG_ENDIAN);
// [...uint32sBe]
// → [ 0xE5AF_8CE5, 0xA3AB_E5B1, 0xB100_0000 ]
Returns the Uint8Array that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Uint8Array.
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 ]
Returns the Uint8ClampedArray that views a new ArrayBuffer duplicated from the underlying ArrayBuffer of this instance.
The Uint8ClampedArray.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8ClampedArray = bytes.toUint8ClampedArray();
// uint8ClampedArray
// → Uint8ClampedArray[ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
Returns the 8-bit unsigned integer Iterable representing this byte sequence.
The Iterable of 8-bit unsigned integers.
const bytes = ByteSequence.of(0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1);
const uint8s = bytes.toUint8Iterable();
// [...uint8s]
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
§Static Methods
Creates a new instance of ByteSequence of the specified size.
Its bytes are filled with zeros.
- The size, in bytes.
A new ByteSequence object.
const bytes = ByteSequence.allocate(1024);
// bytes.byteLength
// → 1024
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified ByteSequence, BufferSource, or 8-bit unsigned integer Array.
- The
Bytesobject represents a byte sequence.
A new ByteSequence object.
The alias for the fromUint8Iterable method.
- The 8-bit unsigned integer
Arrayrepresents a byte sequence.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
that duplicates the specified ArrayBuffer.
- The
ArrayBuffer.
A new ByteSequence object.
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
Creates a new instance of ByteSequence with new underlying ArrayBuffer
that duplicates the underlying ArrayBuffer of the specified ArrayBufferView.
- The object that represents a byte sequence.
A new ByteSequence object.
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 ]
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 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 64-bit unsigned integer AsyncIterable.
- The 64-bit unsigned integer
AsyncIterablerepresents a byte sequence.
- The byte order. If omitted, write in the platform byte order.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 16-bit unsigned integer AsyncIterable.
- The 16-bit unsigned integer
AsyncIterablerepresents a byte sequence.
- The byte order. If omitted, write in the platform byte order.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 32-bit unsigned integer AsyncIterable.
- The 32-bit unsigned integer
AsyncIterablerepresents a byte sequence.
- The byte order. If omitted, write in the platform byte order.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 8-bit unsigned integer AsyncIterable.
- The 8-bit unsigned integer
AsyncIterablerepresents a byte sequence.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the string contains Base64-encoded bytes.
- The string to decode.
- The
Base64.Optionsdictionary.
A new ByteSequence object.
const bytes = ByteSequence.fromBase64Encoded("5a+M5aOr5bGx");
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// Base64 URL (https://datatracker.ietf.org/doc/html/rfc4648#section-5) decoding
const base64Url = {
tableLastChars: ["-", "_"],
noPadding: true,
};
const bytes = ByteSequence.fromBase64Encoded("5a-M5aOr5bGx", base64Url);
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 64-bit unsigned integer Iterable.
- The 64-bit unsigned integer
Iterablerepresents a byte sequence.
- The byte order. If omitted, write in the platform byte order.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the isomorphic encoded string.
- The binary string.
A new ByteSequence object.
const bytes = ByteSequence.fromBinaryString("å¯\u{8C}士山");
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified Blob object.
- The
Blobobject (includingFileobject).
The Promise that fulfills with a new ByteSequence object.
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 ]
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 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
that duplicates the underlying ArrayBuffer of the specified BufferSource.
- The object that represents a byte sequence.
A new ByteSequence object.
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 ]
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 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified data URL.
- The data URL.
A new ByteSequence object.
const bytes = await ByteSequence.fromDataURL("data:application/octet-stream;base64,5a+M5aOr5bGx");
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the string contains Percent-encoded bytes.
- The string to decode.
- The
Percent.Optionsdictionary.
A new ByteSequence object.
const bytes = ByteSequence.fromPercentEncoded("%E5%AF%8C%E5%A3%AB%E5%B1%B1");
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// 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()
// → "富士山"
// 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()
// → "富士山"
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 ]
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 ]
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+)
- The
ReadableStreamofUint8Arrayor the async iterator ofUint8Array.
- The
BytesStream.ReadingOptionsobject.
- The
ProgressEventlistener. |type| x | | :--- | :--- | |"loadstart","progress","loadend"| These will be fired. | |"load"| This will not fire. Instead, thePromiseis resolved. | |"abort","timeout","error"| These will not fire. Instead, thePromiseis rejected. |
The Promise that fulfills with a new ByteSequence object.
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 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
generated from the specified string by the specified text encoding.
- The string.
- The text encoder, for example
TextEncoder. The default is UTF-8 encoder, which does not add or remove BOM.
A new ByteSequence object.
const bytes = ByteSequence.fromText("富士山");
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
// 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 ]
// 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 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 16-bit unsigned integer Iterable.
- The 16-bit unsigned integer
Iterablerepresents a byte sequence.
- The byte order. If omitted, write in the platform byte order.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 32-bit unsigned integer Iterable.
- The 32-bit unsigned integer
Iterablerepresents a byte sequence.
- The byte order. If omitted, write in the platform byte order.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 8-bit unsigned integer Iterable.
- The 8-bit unsigned integer
Iterablerepresents a byte sequence.
A new ByteSequence object.
const array = [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ];
const bytes = ByteSequence.fromUint8Iterable(array);
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
Creates a new instance of ByteSequence of the specified size.
Its bytes are filled with random values.
- The size, in bytes.
A new ByteSequence object.
const randomBytes = ByteSequence.generateRandom(1024);
// randomBytes.byteLength
// → 1024
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified 8-bit unsigned integer iterator.
- The iterator of 8-bit unsigned integers.
A new ByteSequence object.
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the string contains formatted bytes.
- The string to parse.
- The
BytesFormat.Optionsdictionary.
A new ByteSequence object.
const bytes = ByteSequence.parse("E5AF8CE5A3ABE5B1B1");
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
const options = {
lowerCase: true,
};
const bytes = ByteSequence.parse("e5af8ce5a3abe5b1b1", options);
// bytes.toArray()
// → [ 0xE5, 0xAF, 0x8C, 0xE5, 0xA3, 0xAB, 0xE5, 0xB1, 0xB1 ]
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified Blob object.
- The
Blobobject (includingFileobject).
The Promise that fulfills with a new ByteSequence and a BlobPropertyBag.
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"
Creates a new instance of ByteSequence with new underlying ArrayBuffer
created from the specified data URL.
- The data URL.
A new ByteSequence object.
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"
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"
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"
Creates a new instance of ByteSequence with the specified underlying ArrayBuffer.
- The
ArrayBuffer.
A new ByteSequence object.
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