-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy patharray.d.ts
More file actions
89 lines (81 loc) · 3.45 KB
/
array.d.ts
File metadata and controls
89 lines (81 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* TypedArray utils and conversions.
*
* ```js
* import { typedCopyBytes, typedView } from '@exodus/bytes/array.js'
* ```
*
* @module @exodus/bytes/array.js
*/
/// <reference types="node" />
// >= TypeScript 5.9 made Uint8Array templated with <> and defaulted to ArrayBufferLike
// which would incorrectly accept SharedArrayBuffer instances.
// < TypeScript 5.7 doesn't support templates for Uint8Array.
// So this type is defined as a workaround to evaluate to Uint8Array<ArrayBuffer> on all versions of TypeScript.
/**
* This is `Uint8Array<ArrayBuffer>`
* (as opposed to `Uint8Array<SharedArrayBuffer>` and `Uint8Array<ArrayBufferLike>`)
* on TypeScript versions that support that distinction.
*
* On TypeScript < 5.7, this is just `Uint8Array`, as it's not a template there.
*/
export type Uint8ArrayBuffer = ReturnType<typeof Uint8Array.from>;
/**
* This is `Uint16Array<ArrayBuffer>`
* (as opposed to `Uint16Array<SharedArrayBuffer>` and `Uint16Array<ArrayBufferLike>`)
* on TypeScript versions that support that distinction.
*
* On TypeScript < 5.7, this is just `Uint16Array`, as it's not a template there.
*/
export type Uint16ArrayBuffer = ReturnType<typeof Uint16Array.from>;
/**
* This is `Uint32Array<ArrayBuffer>`
* (as opposed to `Uint32Array<SharedArrayBuffer>` and `Uint32Array<ArrayBufferLike>`)
* on TypeScript versions that support that distinction.
*
* On TypeScript < 5.7, this is just `Uint32Array`, as it's not a template there.
*/
export type Uint32ArrayBuffer = ReturnType<typeof Uint32Array.from>;
/**
* Output format for typed array conversions
*/
export type OutputFormat = 'uint8' | 'arraybuffer' | 'buffer';
/**
* Create a view of a TypedArray in the specified format (`'uint8'` or `'buffer'`)
*
* > [!IMPORTANT]
* > Does not copy data, returns a view on the same underlying buffer
*
* > [!WARNING]
* > Viewing `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`) as bytes
* > is platform endianness-dependent.
*
* @param arr - The input TypedArray
* @param format - The desired output format (`'uint8'` or `'buffer'`)
* @returns A view on the same underlying buffer
*/
export function typedView(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
export function typedView(arr: ArrayBufferView, format: 'buffer'): Buffer;
export function typedView(arr: ArrayBufferView, format: 'uint8' | 'buffer'): Uint8Array | Buffer;
/**
* Create a copy of TypedArray underlying bytes in the specified format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
*
* This does not copy _values_, but copies the underlying bytes.
* The result is similar to that of `typedView()`, but this function provides a copy, not a view of the same memory.
*
* > [!WARNING]
* > Copying underlying bytes from `Uint16Array` (or other with `BYTES_PER_ELEMENT > 1`)
* > is platform endianness-dependent.
*
* > [!NOTE]
* > Buffer might be pooled.
* > Uint8Array return values are not pooled and match their underlying ArrayBuffer.
*
* @param arr - The input TypedArray
* @param format - The desired output format (`'uint8'`, `'buffer'`, or `'arraybuffer'`)
* @returns A copy of the underlying buffer
*/
export function typedCopyBytes(arr: ArrayBufferView, format: 'uint8'): Uint8Array;
export function typedCopyBytes(arr: ArrayBufferView, format: 'arraybuffer'): ArrayBuffer;
export function typedCopyBytes(arr: ArrayBufferView, format: 'buffer'): Buffer;
export function typedCopyBytes(arr: ArrayBufferView, format: OutputFormat): Uint8Array | ArrayBuffer | Buffer;