UNPKG

17.2 kBTypeScriptView Raw
1declare module 'buffer' {
2 import { BinaryLike } from 'crypto';
3
4 export const INSPECT_MAX_BYTES: number;
5 export const kMaxLength: number;
6 export const kStringMaxLength: number;
7 export const constants: {
8 MAX_LENGTH: number;
9 MAX_STRING_LENGTH: number;
10 };
11
12 export type TranscodeEncoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "latin1" | "binary";
13
14 export function transcode(source: Uint8Array, fromEnc: TranscodeEncoding, toEnc: TranscodeEncoding): Buffer;
15
16 export const SlowBuffer: {
17 /** @deprecated since v6.0.0, use `Buffer.allocUnsafeSlow()` */
18 new(size: number): Buffer;
19 prototype: Buffer;
20 };
21
22 export { Buffer };
23
24 /**
25 * @experimental
26 */
27 export interface BlobOptions {
28 /**
29 * @default 'utf8'
30 */
31 encoding?: BufferEncoding | undefined;
32
33 /**
34 * The Blob content-type. The intent is for `type` to convey
35 * the MIME media type of the data, however no validation of the type format
36 * is performed.
37 */
38 type?: string | undefined;
39 }
40
41 /**
42 * @experimental
43 */
44 export class Blob {
45 /**
46 * Returns a promise that fulfills with an {ArrayBuffer} containing a copy of the `Blob` data.
47 */
48 readonly size: number;
49
50 /**
51 * The content-type of the `Blob`.
52 */
53 readonly type: string;
54
55 /**
56 * Creates a new `Blob` object containing a concatenation of the given sources.
57 *
58 * {ArrayBuffer}, {TypedArray}, {DataView}, and {Buffer} sources are copied into
59 * the 'Blob' and can therefore be safely modified after the 'Blob' is created.
60 *
61 * String sources are also copied into the `Blob`.
62 */
63 constructor(sources: Array<(BinaryLike | Blob)>, options?: BlobOptions);
64
65 arrayBuffer(): Promise<ArrayBuffer>;
66
67 /**
68 * @param start The starting index.
69 * @param end The ending index.
70 * @param type The content-type for the new `Blob`
71 */
72 slice(start?: number, end?: number, type?: string): Blob;
73
74 /**
75 * Returns a promise that resolves the contents of the `Blob` decoded as a UTF-8 string.
76 */
77 text(): Promise<string>;
78 }
79
80 export import atob = globalThis.atob;
81 export import btoa = globalThis.btoa;
82
83 global {
84 // Buffer class
85 type BufferEncoding = "ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "base64url" | "latin1" | "binary" | "hex";
86
87 type WithImplicitCoercion<T> = T | { valueOf(): T };
88
89 /**
90 * Raw data is stored in instances of the Buffer class.
91 * A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.
92 * Valid string encodings: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
93 */
94 class Buffer extends Uint8Array {
95 /**
96 * Allocates a new buffer containing the given {str}.
97 *
98 * @param str String to store in buffer.
99 * @param encoding encoding to use, optional. Default is 'utf8'
100 * @deprecated since v10.0.0 - Use `Buffer.from(string[, encoding])` instead.
101 */
102 constructor(str: string, encoding?: BufferEncoding);
103 /**
104 * Allocates a new buffer of {size} octets.
105 *
106 * @param size count of octets to allocate.
107 * @deprecated since v10.0.0 - Use `Buffer.alloc()` instead (also see `Buffer.allocUnsafe()`).
108 */
109 constructor(size: number);
110 /**
111 * Allocates a new buffer containing the given {array} of octets.
112 *
113 * @param array The octets to store.
114 * @deprecated since v10.0.0 - Use `Buffer.from(array)` instead.
115 */
116 constructor(array: Uint8Array);
117 /**
118 * Produces a Buffer backed by the same allocated memory as
119 * the given {ArrayBuffer}/{SharedArrayBuffer}.
120 *
121 *
122 * @param arrayBuffer The ArrayBuffer with which to share memory.
123 * @deprecated since v10.0.0 - Use `Buffer.from(arrayBuffer[, byteOffset[, length]])` instead.
124 */
125 constructor(arrayBuffer: ArrayBuffer | SharedArrayBuffer);
126 /**
127 * Allocates a new buffer containing the given {array} of octets.
128 *
129 * @param array The octets to store.
130 * @deprecated since v10.0.0 - Use `Buffer.from(array)` instead.
131 */
132 constructor(array: ReadonlyArray<any>);
133 /**
134 * Copies the passed {buffer} data onto a new {Buffer} instance.
135 *
136 * @param buffer The buffer to copy.
137 * @deprecated since v10.0.0 - Use `Buffer.from(buffer)` instead.
138 */
139 constructor(buffer: Buffer);
140 /**
141 * When passed a reference to the .buffer property of a TypedArray instance,
142 * the newly created Buffer will share the same allocated memory as the TypedArray.
143 * The optional {byteOffset} and {length} arguments specify a memory range
144 * within the {arrayBuffer} that will be shared by the Buffer.
145 *
146 * @param arrayBuffer The .buffer property of any TypedArray or a new ArrayBuffer()
147 */
148 static from(arrayBuffer: WithImplicitCoercion<ArrayBuffer | SharedArrayBuffer>, byteOffset?: number, length?: number): Buffer;
149 /**
150 * Creates a new Buffer using the passed {data}
151 * @param data data to create a new Buffer
152 */
153 static from(data: Uint8Array | ReadonlyArray<number>): Buffer;
154 static from(data: WithImplicitCoercion<Uint8Array | ReadonlyArray<number> | string>): Buffer;
155 /**
156 * Creates a new Buffer containing the given JavaScript string {str}.
157 * If provided, the {encoding} parameter identifies the character encoding.
158 * If not provided, {encoding} defaults to 'utf8'.
159 */
160 static from(str: WithImplicitCoercion<string> | { [Symbol.toPrimitive](hint: 'string'): string }, encoding?: BufferEncoding): Buffer;
161 /**
162 * Creates a new Buffer using the passed {data}
163 * @param values to create a new Buffer
164 */
165 static of(...items: number[]): Buffer;
166 /**
167 * Returns true if {obj} is a Buffer
168 *
169 * @param obj object to test.
170 */
171 static isBuffer(obj: any): obj is Buffer;
172 /**
173 * Returns true if {encoding} is a valid encoding argument.
174 * Valid string encodings in Node 0.12: 'ascii'|'utf8'|'utf16le'|'ucs2'(alias of 'utf16le')|'base64'|'binary'(deprecated)|'hex'
175 *
176 * @param encoding string to test.
177 */
178 static isEncoding(encoding: string): encoding is BufferEncoding;
179 /**
180 * Gives the actual byte length of a string. encoding defaults to 'utf8'.
181 * This is not the same as String.prototype.length since that returns the number of characters in a string.
182 *
183 * @param string string to test.
184 * @param encoding encoding used to evaluate (defaults to 'utf8')
185 */
186 static byteLength(
187 string: string | NodeJS.ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
188 encoding?: BufferEncoding
189 ): number;
190 /**
191 * Returns a buffer which is the result of concatenating all the buffers in the list together.
192 *
193 * If the list has no items, or if the totalLength is 0, then it returns a zero-length buffer.
194 * If the list has exactly one item, then the first item of the list is returned.
195 * If the list has more than one item, then a new Buffer is created.
196 *
197 * @param list An array of Buffer objects to concatenate
198 * @param totalLength Total length of the buffers when concatenated.
199 * If totalLength is not provided, it is read from the buffers in the list. However, this adds an additional loop to the function, so it is faster to provide the length explicitly.
200 */
201 static concat(list: ReadonlyArray<Uint8Array>, totalLength?: number): Buffer;
202 /**
203 * The same as buf1.compare(buf2).
204 */
205 static compare(buf1: Uint8Array, buf2: Uint8Array): number;
206 /**
207 * Allocates a new buffer of {size} octets.
208 *
209 * @param size count of octets to allocate.
210 * @param fill if specified, buffer will be initialized by calling buf.fill(fill).
211 * If parameter is omitted, buffer will be filled with zeros.
212 * @param encoding encoding used for call to buf.fill while initalizing
213 */
214 static alloc(size: number, fill?: string | Buffer | number, encoding?: BufferEncoding): Buffer;
215 /**
216 * Allocates a new buffer of {size} octets, leaving memory not initialized, so the contents
217 * of the newly created Buffer are unknown and may contain sensitive data.
218 *
219 * @param size count of octets to allocate
220 */
221 static allocUnsafe(size: number): Buffer;
222 /**
223 * Allocates a new non-pooled buffer of {size} octets, leaving memory not initialized, so the contents
224 * of the newly created Buffer are unknown and may contain sensitive data.
225 *
226 * @param size count of octets to allocate
227 */
228 static allocUnsafeSlow(size: number): Buffer;
229 /**
230 * This is the number of bytes used to determine the size of pre-allocated, internal Buffer instances used for pooling. This value may be modified.
231 */
232 static poolSize: number;
233
234 write(string: string, encoding?: BufferEncoding): number;
235 write(string: string, offset: number, encoding?: BufferEncoding): number;
236 write(string: string, offset: number, length: number, encoding?: BufferEncoding): number;
237 toString(encoding?: BufferEncoding, start?: number, end?: number): string;
238 toJSON(): { type: 'Buffer'; data: number[] };
239 equals(otherBuffer: Uint8Array): boolean;
240 compare(
241 otherBuffer: Uint8Array,
242 targetStart?: number,
243 targetEnd?: number,
244 sourceStart?: number,
245 sourceEnd?: number
246 ): number;
247 copy(targetBuffer: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): number;
248 /**
249 * Returns a new `Buffer` that references **the same memory as the original**, but offset and cropped by the start and end indices.
250 *
251 * This method is incompatible with `Uint8Array#slice()`, which returns a copy of the original memory.
252 *
253 * @param begin Where the new `Buffer` will start. Default: `0`.
254 * @param end Where the new `Buffer` will end (not inclusive). Default: `buf.length`.
255 */
256 slice(begin?: number, end?: number): Buffer;
257 /**
258 * Returns a new `Buffer` that references **the same memory as the original**, but offset and cropped by the start and end indices.
259 *
260 * This method is compatible with `Uint8Array#subarray()`.
261 *
262 * @param begin Where the new `Buffer` will start. Default: `0`.
263 * @param end Where the new `Buffer` will end (not inclusive). Default: `buf.length`.
264 */
265 subarray(begin?: number, end?: number): Buffer;
266 writeBigInt64BE(value: bigint, offset?: number): number;
267 writeBigInt64LE(value: bigint, offset?: number): number;
268 writeBigUInt64BE(value: bigint, offset?: number): number;
269 writeBigUInt64LE(value: bigint, offset?: number): number;
270 writeUIntLE(value: number, offset: number, byteLength: number): number;
271 writeUIntBE(value: number, offset: number, byteLength: number): number;
272 writeIntLE(value: number, offset: number, byteLength: number): number;
273 writeIntBE(value: number, offset: number, byteLength: number): number;
274 readBigUInt64BE(offset?: number): bigint;
275 readBigUInt64LE(offset?: number): bigint;
276 readBigInt64BE(offset?: number): bigint;
277 readBigInt64LE(offset?: number): bigint;
278 readUIntLE(offset: number, byteLength: number): number;
279 readUIntBE(offset: number, byteLength: number): number;
280 readIntLE(offset: number, byteLength: number): number;
281 readIntBE(offset: number, byteLength: number): number;
282 readUInt8(offset?: number): number;
283 readUInt16LE(offset?: number): number;
284 readUInt16BE(offset?: number): number;
285 readUInt32LE(offset?: number): number;
286 readUInt32BE(offset?: number): number;
287 readInt8(offset?: number): number;
288 readInt16LE(offset?: number): number;
289 readInt16BE(offset?: number): number;
290 readInt32LE(offset?: number): number;
291 readInt32BE(offset?: number): number;
292 readFloatLE(offset?: number): number;
293 readFloatBE(offset?: number): number;
294 readDoubleLE(offset?: number): number;
295 readDoubleBE(offset?: number): number;
296 reverse(): this;
297 swap16(): Buffer;
298 swap32(): Buffer;
299 swap64(): Buffer;
300 writeUInt8(value: number, offset?: number): number;
301 writeUInt16LE(value: number, offset?: number): number;
302 writeUInt16BE(value: number, offset?: number): number;
303 writeUInt32LE(value: number, offset?: number): number;
304 writeUInt32BE(value: number, offset?: number): number;
305 writeInt8(value: number, offset?: number): number;
306 writeInt16LE(value: number, offset?: number): number;
307 writeInt16BE(value: number, offset?: number): number;
308 writeInt32LE(value: number, offset?: number): number;
309 writeInt32BE(value: number, offset?: number): number;
310 writeFloatLE(value: number, offset?: number): number;
311 writeFloatBE(value: number, offset?: number): number;
312 writeDoubleLE(value: number, offset?: number): number;
313 writeDoubleBE(value: number, offset?: number): number;
314
315 fill(value: string | Uint8Array | number, offset?: number, end?: number, encoding?: BufferEncoding): this;
316
317 indexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number;
318 lastIndexOf(value: string | number | Uint8Array, byteOffset?: number, encoding?: BufferEncoding): number;
319 entries(): IterableIterator<[number, number]>;
320 includes(value: string | number | Buffer, byteOffset?: number, encoding?: BufferEncoding): boolean;
321 keys(): IterableIterator<number>;
322 values(): IterableIterator<number>;
323 }
324
325 /**
326 * Decodes a string of Base64-encoded data into bytes, and encodes those bytes into a string using Latin-1 (ISO-8859-1).
327 *
328 * This function is only provided for compatibility with legacy web platform APIs
329 * and should never be used in new code, because they use strings to represent
330 * binary data and predate the introduction of typed arrays in JavaScript.
331 * For code running using Node.js APIs, converting between base64-encoded strings
332 * and binary data should be performed using `Buffer.from(str, 'base64')` and
333 * `buf.toString('base64')`.
334 *
335 * @deprecated dom compatibility
336 */
337 function atob(input: string): string;
338
339 /**
340 * Decodes a string into bytes using Latin-1 (ISO-8859), and encodes those bytes into a string using Base64.
341 *
342 * This function is only provided for compatibility with legacy web platform APIs
343 * and should never be used in new code, because they use strings to represent
344 * binary data and predate the introduction of typed arrays in JavaScript.
345 * For code running using Node.js APIs, converting between base64-encoded strings
346 * and binary data should be performed using `Buffer.from(str, 'base64')` and
347 * `buf.toString('base64')`.
348 *
349 * @deprecated dom compatibility
350 */
351 function btoa(input: string): string;
352 }
353}
354
355declare module 'node:buffer' {
356 export * from 'buffer';
357}