@hyperfrontend/immutable-api-utils/built-in-copy/typed-arrays

typed-arrays

Locked, prototype-pollution-resistant copies of the global ArrayBuffer, SharedArrayBuffer, DataView, and TypedArray constructors.

Factory wrappers for Uint8Array, Uint8ClampedArray, Uint16Array, Uint32Array, Int8Array, Int16Array, Int32Array, Float32Array, Float64Array, BigInt64Array, BigUint64Array, plus ArrayBuffer, SharedArrayBuffer, and DataView, are captured at module-load time and frozen into a tamper-proof namespace, so binary buffer construction stays trustworthy even if the globals are later patched. Effective only when imported before any untrusted code has had a chance to mutate the prototype chain.

API Reference

ƒ Functions

§function

createArrayBuffer(byteLength: number): ArrayBuffer

(Safe copy) Creates a new ArrayBuffer using the captured ArrayBuffer constructor. Use this instead of new ArrayBuffer().

Parameters

NameTypeDescription
§byteLength
number
The size, in bytes, of the array buffer to create.

Returns

ArrayBuffer
A new ArrayBuffer instance.

Example

Creating ArrayBuffer

const buffer = createArrayBuffer(16)
// => ArrayBuffer { byteLength: 16 }
§function

createBigInt64Array(length: number): BigInt64Array

(Safe copy) Creates a new BigInt64Array using the captured BigInt64Array constructor. Use this instead of new BigInt64Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

BigInt64Array
A new BigInt64Array instance.

Example

Creating BigInt64Array

const bigInts = createBigInt64Array(2)
bigInts[0] = -9223372036854775808n // Min value
bigInts[1] = 9223372036854775807n  // Max value
§function

createBigUint64Array(length: number): BigUint64Array

(Safe copy) Creates a new BigUint64Array using the captured BigUint64Array constructor. Use this instead of new BigUint64Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

BigUint64Array
A new BigUint64Array instance.

Example

Creating BigUint64Array

const bigUints = createBigUint64Array(2)
bigUints[0] = 0n
bigUints[1] = 18446744073709551615n // Max value
§function

createDataView(buffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, byteLength?: number): DataView

(Safe copy) Creates a new DataView using the captured DataView constructor. Use this instead of new DataView().

Parameters

NameTypeDescription
§buffer
ArrayBuffer | SharedArrayBuffer
The ArrayBuffer or SharedArrayBuffer to view.
§byteOffset?
number
Optional byte offset to start the view.
§byteLength?
number
Optional byte length of the view.

Returns

DataView
A new DataView instance.

Example

Creating DataView for binary data

const buffer = createArrayBuffer(8)
const view = createDataView(buffer)
view.setInt32(0, 42, true) // little-endian
view.getInt32(0, true) // => 42
§function

createFloat32Array(length: number): Float32Array

(Safe copy) Creates a new Float32Array using the captured Float32Array constructor. Use this instead of new Float32Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Float32Array
A new Float32Array instance.

Example

Creating Float32Array

const floats = createFloat32Array(3)
floats[0] = 3.14
floats[1] = -273.15
§function

createFloat64Array(length: number): Float64Array

(Safe copy) Creates a new Float64Array using the captured Float64Array constructor. Use this instead of new Float64Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Float64Array
A new Float64Array instance.

Example

Creating Float64Array

const doubles = createFloat64Array(2)
doubles[0] = Math.PI
doubles[1] = Number.MAX_VALUE
§function

createInt16Array(length: number): Int16Array

(Safe copy) Creates a new Int16Array using the captured Int16Array constructor. Use this instead of new Int16Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Int16Array
A new Int16Array instance.

Example

Creating Int16Array

const shorts = createInt16Array(2)
shorts[0] = -32768 // Min value
shorts[1] = 32767  // Max value
§function

createInt32Array(length: number): Int32Array

(Safe copy) Creates a new Int32Array using the captured Int32Array constructor. Use this instead of new Int32Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Int32Array
A new Int32Array instance.

Example

Creating Int32Array

const ints = createInt32Array(2)
ints[0] = -2147483648 // Min value
ints[1] = 2147483647  // Max value
§function

createInt8Array(length: number): Int8Array

(Safe copy) Creates a new Int8Array using the captured Int8Array constructor. Use this instead of new Int8Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Int8Array
A new Int8Array instance.

Example

Creating Int8Array

const signed = createInt8Array(2)
signed[0] = -128 // Min value
signed[1] = 127  // Max value
§function

createSharedArrayBuffer(byteLength: number): SharedArrayBuffer

(Safe copy) Creates a new SharedArrayBuffer using the captured SharedArrayBuffer constructor. Use this instead of new SharedArrayBuffer().

Parameters

NameTypeDescription
§byteLength
number
The size, in bytes, of the shared array buffer to create.

Returns

SharedArrayBuffer
A new SharedArrayBuffer instance.

Example

Creating SharedArrayBuffer for workers

const sharedBuffer = createSharedArrayBuffer(1024)
// Can be shared between workers via postMessage
§function

createUint16Array(length: number): Uint16Array

(Safe copy) Creates a new Uint16Array using the captured Uint16Array constructor. Use this instead of new Uint16Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Uint16Array
A new Uint16Array instance.

Example

Creating Uint16Array

const shorts = createUint16Array(2)
shorts[0] = 65535 // Max value for 16-bit unsigned
§function

createUint32Array(length: number): Uint32Array

(Safe copy) Creates a new Uint32Array using the captured Uint32Array constructor. Use this instead of new Uint32Array().

Parameters

NameTypeDescription
§length
number
The length of the array.

Returns

Uint32Array
A new Uint32Array instance.

Example

Creating Uint32Array

const ints = createUint32Array(4)
ints[0] = 0xFFFFFFFF // Max 32-bit unsigned value
§function

createUint8Array(length: number): Uint8Array

(Safe copy) Creates a new Uint8Array using the captured Uint8Array constructor. Use this instead of new Uint8Array().
Supports all standard Uint8Array constructor overloads:
  • createUint8Array(length) - Creates array of given length
  • createUint8Array(arrayLike) - Creates from array-like or iterable
  • createUint8Array(buffer, byteOffset?, length?) - Creates view over buffer

Parameters

NameTypeDescription
§length
number
The length of the array to create.

Returns

Uint8Array
A new Uint8Array instance.

Example

Creating Uint8Array

const bytes = createUint8Array(4)
bytes[0] = 255
// From array
const fromArray = createUint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f])
§function

createUint8ClampedArray(length: number): Uint8ClampedArray

(Safe copy) Creates a new Uint8ClampedArray using the captured Uint8ClampedArray constructor. Use this instead of new Uint8ClampedArray().

Parameters

NameTypeDescription
§length
number
Number of elements to allocate in the clamped array.

Returns

Uint8ClampedArray
A new Uint8ClampedArray instance.

Example

Creating Uint8ClampedArray for pixels

const pixels = createUint8ClampedArray(4) // RGBA
pixels[0] = 300 // Clamped to 255
pixels[1] = -10 // Clamped to 0

Variables

§type

bigInt64ArrayFrom

(Safe copy) Creates a new BigInt64Array from an array-like or iterable object.
§type

bigInt64ArrayOf

(Safe copy) Creates a new BigInt64Array from a variable number of arguments.
§type

bigUint64ArrayFrom

(Safe copy) Creates a new BigUint64Array from an array-like or iterable object.
§type

bigUint64ArrayOf

(Safe copy) Creates a new BigUint64Array from a variable number of arguments.
§type

float32ArrayFrom

(Safe copy) Creates a new Float32Array from an array-like or iterable object.
§type

float32ArrayOf

(Safe copy) Creates a new Float32Array from a variable number of arguments.
§type

float64ArrayFrom

(Safe copy) Creates a new Float64Array from an array-like or iterable object.
§type

float64ArrayOf

(Safe copy) Creates a new Float64Array from a variable number of arguments.
§type

int16ArrayFrom

(Safe copy) Creates a new Int16Array from an array-like or iterable object.
§type

int16ArrayOf

(Safe copy) Creates a new Int16Array from a variable number of arguments.
§type

int32ArrayFrom

(Safe copy) Creates a new Int32Array from an array-like or iterable object.
§type

int32ArrayOf

(Safe copy) Creates a new Int32Array from a variable number of arguments.
§type

int8ArrayFrom

(Safe copy) Creates a new Int8Array from an array-like or iterable object.
§type

int8ArrayOf

(Safe copy) Creates a new Int8Array from a variable number of arguments.
§type

isView

(Safe copy) Returns true if arg is an ArrayBuffer or SharedArrayBuffer.
§type

TypedArrays

(Safe copy) Namespace object containing all TypedArray utilities. Note: Importing this imports all methods in this namespace (no tree-shaking).
§type

uint16ArrayFrom

(Safe copy) Creates a new Uint16Array from an array-like or iterable object.
§type

uint16ArrayOf

(Safe copy) Creates a new Uint16Array from a variable number of arguments.
§type

uint32ArrayFrom

(Safe copy) Creates a new Uint32Array from an array-like or iterable object.
§type

uint32ArrayOf

(Safe copy) Creates a new Uint32Array from a variable number of arguments.
§type

uint8ArrayFrom

(Safe copy) Creates a new Uint8Array from an array-like or iterable object.
§type

uint8ArrayOf

(Safe copy) Creates a new Uint8Array from a variable number of arguments.
§type

uint8ClampedArrayFrom

(Safe copy) Creates a new Uint8ClampedArray from an array-like or iterable object.
§type

uint8ClampedArrayOf

(Safe copy) Creates a new Uint8ClampedArray from a variable number of arguments.