Represents a raw buffer of binary data, which is used to store data for the different typed arrays. ArrayBuffers cannot be read from or written to directly, but can be passed to a typed array or DataView Object to interpret the raw buffer as needed.
type
worker_threads.Transferable
Referenced types
class ArrayBuffer
interface MessagePort
The NodeEventTarget is a Node.js-specific extension to EventTarget that emulates a subset of the EventEmitter API.
- type: K,): void;
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
type: string,): void;Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
- event: 'close',): this;
Node.js-specific extension to the
EventTargetclass that emulates the equivalentEventEmitterAPI. The only difference betweenaddListener()andaddEventListener()is thataddListener()will return a reference to theEventTarget.event: 'message',listener: (value: any) => void): this;Node.js-specific extension to the
EventTargetclass that emulates the equivalentEventEmitterAPI. The only difference betweenaddListener()andaddEventListener()is thataddListener()will return a reference to theEventTarget.event: 'messageerror',): this;Node.js-specific extension to the
EventTargetclass that emulates the equivalentEventEmitterAPI. The only difference betweenaddListener()andaddEventListener()is thataddListener()will return a reference to theEventTarget.event: string,listener: (arg: any) => void): this;Node.js-specific extension to the
EventTargetclass that emulates the equivalentEventEmitterAPI. The only difference betweenaddListener()andaddEventListener()is thataddListener()will return a reference to theEventTarget. - ): boolean;
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
- emit(event: 'close',): boolean;
Node.js-specific extension to the
EventTargetclass that dispatches theargto the list of handlers fortype.@returnstrueif event listeners registered for thetypeexist, otherwisefalse.emit(event: 'message',value: any): boolean;Node.js-specific extension to the
EventTargetclass that dispatches theargto the list of handlers fortype.@returnstrueif event listeners registered for thetypeexist, otherwisefalse.emit(event: 'messageerror',): boolean;Node.js-specific extension to the
EventTargetclass that dispatches theargto the list of handlers fortype.@returnstrueif event listeners registered for thetypeexist, otherwisefalse.emit(event: string,arg: any): boolean;Node.js-specific extension to the
EventTargetclass that dispatches theargto the list of handlers fortype.@returnstrueif event listeners registered for thetypeexist, otherwisefalse. Node.js-specific extension to the
EventTargetclass that returns an array of eventtypenames for which event listeners are registered.Node.js-specific extension to the
EventTargetclass that returns the number of max event listeners.- type: string): number;
Node.js-specific extension to the
EventTargetclass that returns the number of event listeners registered for thetype. - once(event: 'message',listener: (value: any) => void): this;
Node.js-specific extension to the
EventTargetclass that adds aoncelistener for the given eventtype. This is equivalent to callingonwith theonceoption set totrue.once(event: string,listener: (arg: any) => void): this;Node.js-specific extension to the
EventTargetclass that adds aoncelistener for the given eventtype. This is equivalent to callingonwith theonceoption set totrue. - type?: string): this;
Node.js-specific extension to the
EventTargetclass. Iftypeis specified, removes all registered listeners fortype, otherwise removes all registered listeners. - type: K,options?: boolean | EventListenerOptions): void;
Removes the event listener in target's event listener list with the same type, callback, and options.
type: string,options?: boolean | EventListenerOptions): void;Removes the event listener in target's event listener list with the same type, callback, and options.
- event: 'close',options?: EventListenerOptions): this;
Node.js-specific extension to the
EventTargetclass that removes thelistenerfor the giventype. The only difference betweenremoveListener()andremoveEventListener()is thatremoveListener()will return a reference to theEventTarget.event: 'message',listener: (value: any) => void,options?: EventListenerOptions): this;Node.js-specific extension to the
EventTargetclass that removes thelistenerfor the giventype. The only difference betweenremoveListener()andremoveEventListener()is thatremoveListener()will return a reference to theEventTarget.event: 'messageerror',options?: EventListenerOptions): this;Node.js-specific extension to the
EventTargetclass that removes thelistenerfor the giventype. The only difference betweenremoveListener()andremoveEventListener()is thatremoveListener()will return a reference to theEventTarget.event: string,listener: (arg: any) => void,options?: EventListenerOptions): this;Node.js-specific extension to the
EventTargetclass that removes thelistenerfor the giventype. The only difference betweenremoveListener()andremoveEventListener()is thatremoveListener()will return a reference to theEventTarget. - n: number): void;
Node.js-specific extension to the
EventTargetclass that sets the number of max event listeners asn.
class AbortSignal
A signal object that allows you to communicate with a DOM request (such as a Fetch) and abort it if required via an AbortController object.
- readonly aborted: boolean
Returns true if this AbortSignal's AbortController has signaled to abort, and false otherwise.
- type: K,): void;
Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.
The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.
When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.
When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.
When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.
If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.
The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.
- ): boolean;
Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
- type: K,options?: boolean | EventListenerOptions): void;
Removes the event listener in target's event listener list with the same type, callback, and options.
interface FileHandle
Calls
filehandle.close()and returns a promise that fulfills when the filehandle is closed.- ): Promise<void>;
Alias of
filehandle.writeFile().When operating on file handles, the mode cannot be changed from what it was set to with
fsPromises.open(). Therefore, this is equivalent tofilehandle.writeFile().@returnsFulfills with
undefinedupon success. Closes the file handle after waiting for any pending operation on the handle to complete.
import { open } from 'node:fs/promises'; let filehandle; try { filehandle = await open('thefile.txt', 'r'); } finally { await filehandle?.close(); }@returnsFulfills with
undefinedupon success.Unlike the 16 KiB default
highWaterMarkfor astream.Readable, the stream returned by this method has a defaulthighWaterMarkof 64 KiB.optionscan includestartandendvalues to read a range of bytes from the file instead of the entire file. Bothstartandendare inclusive and start counting at 0, allowed values are in the [0,Number.MAX_SAFE_INTEGER] range. Ifstartis omitted orundefined,filehandle.createReadStream()reads sequentially from the current file position. Theencodingcan be any one of those accepted byBuffer.If the
FileHandlepoints to a character device that only supports blocking reads (such as keyboard or sound card), read operations do not finish until data is available. This can prevent the process from exiting and the stream from closing naturally.By default, the stream will emit a
'close'event after it has been destroyed. Set theemitCloseoption tofalseto change this behavior.import { open } from 'node:fs/promises'; const fd = await open('/dev/input/event0'); // Create a stream from some character device. const stream = fd.createReadStream(); setTimeout(() => { stream.close(); // This may not close the stream. // Artificially marking end-of-stream, as if the underlying resource had // indicated end-of-file by itself, allows the stream to close. // This does not cancel pending read operations, and if there is such an // operation, the process may still not be able to exit successfully // until it finishes. stream.push(null); stream.read(0); }, 100);If
autoCloseis false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak. IfautoCloseis set to true (default behavior), on'error'or'end'the file descriptor will be closed automatically.An example to read the last 10 bytes of a file which is 100 bytes long:
import { open } from 'node:fs/promises'; const fd = await open('sample.txt'); fd.createReadStream({ start: 90, end: 99 });optionsmay also include astartoption to allow writing data at some position past the beginning of the file, allowed values are in the [0,Number.MAX_SAFE_INTEGER] range. Modifying a file rather than replacing it may require theflagsopenoption to be set tor+rather than the defaultr. Theencodingcan be any one of those accepted byBuffer.If
autoCloseis set to true (default behavior) on'error'or'finish'the file descriptor will be closed automatically. IfautoCloseis false, then the file descriptor won't be closed, even if there's an error. It is the application's responsibility to close it and make sure there's no file descriptor leak.By default, the stream will emit a
'close'event after it has been destroyed. Set theemitCloseoption tofalseto change this behavior.Forces all currently queued I/O operations associated with the file to the operating system's synchronized I/O completion state. Refer to the POSIX
fdatasync(2)documentation for details.Unlike
filehandle.syncthis method does not flush modified metadata.@returnsFulfills with
undefinedupon success.- buffer: T,offset?: null | number,length?: null | number,
Reads data from the file and stores that in the given buffer.
If the file is not modified concurrently, the end-of-file is reached when the number of bytes read is zero.
@param bufferA buffer that will be filled with the file data read.
@param offsetThe location in the buffer at which to start filling.
@param lengthThe number of bytes to read.
@param positionThe location where to begin reading data from the file. If
null, data will be read from the current file position, and the position will be updated. Ifpositionis an integer, the current file position will remain unchanged.@returnsFulfills upon success with an object with two properties:
buffer: T, Returns a byte-oriented
ReadableStreamthat may be used to read the file's contents.An error will be thrown if this method is called more than once or is called after the
FileHandleis closed or closing.import { open, } from 'node:fs/promises'; const file = await open('./some/file/to/read'); for await (const chunk of file.readableWebStream()) console.log(chunk); await file.close();While the
ReadableStreamwill read the file to completion, it will not close theFileHandleautomatically. User code must still call thefileHandle.close()method.- ): Promise<NonSharedBuffer>;
Asynchronously reads the entire contents of a file.
If
optionsis a string, then it specifies theencoding.The
FileHandlehas to support reading.If one or more
filehandle.read()calls are made on a file handle and then afilehandle.readFile()call is made, the data will be read from the current position till the end of the file. It doesn't always read from the beginning of the file.@returnsFulfills upon a successful read with the contents of the file. If no encoding is specified (using
options.encoding), the data is returned as a {Buffer} object. Otherwise, the data will be a string.): Promise<string | NonSharedBuffer>;Asynchronously reads the entire contents of a file. The underlying file will not be closed automatically. The
FileHandlemust have been opened for reading. Convenience method to create a
readlineinterface and stream over the file. Seefilehandle.createReadStream()for the options.import { open } from 'node:fs/promises'; const file = await open('./some/file/to/read'); for await (const line of file.readLines()) { console.log(line); }- @param position
The offset from the beginning of the file where the data should be read from. If
positionis not anumber, the data will be read from the current position.@returnsFulfills upon success an object containing two properties:
- @returns
Fulfills with an {fs.Stats} for the file.
- len?: number): Promise<void>;
Truncates the file.
If the file was larger than
lenbytes, only the firstlenbytes will be retained in the file.The following example retains only the first four bytes of the file:
import { open } from 'node:fs/promises'; let filehandle = null; try { filehandle = await open('temp.txt', 'r+'); await filehandle.truncate(4); } finally { await filehandle?.close(); }If the file previously was shorter than
lenbytes, it is extended, and the extended part is filled with null bytes ('\0'):If
lenis negative then0will be used.@returnsFulfills with
undefinedupon success. - buffer: TBuffer,offset?: null | number,length?: null | number,position?: null | number): Promise<{ buffer: TBuffer; bytesWritten: number }>;
Write
bufferto the file.The promise is fulfilled with an object containing two properties:
It is unsafe to use
filehandle.write()multiple times on the same file without waiting for the promise to be fulfilled (or rejected). For this scenario, usefilehandle.createWriteStream().On Linux, positional writes do not work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
@param offsetThe start position from within
bufferwhere the data to write begins.@param lengthThe number of bytes from
bufferto write.@param positionThe offset from the beginning of the file where the data from
buffershould be written. Ifpositionis not anumber, the data will be written at the current position. See the POSIX pwrite(2) documentation for more detail.buffer: TBuffer,options?: { length: number; offset: number; position: number }): Promise<{ buffer: TBuffer; bytesWritten: number }>;data: string,position?: null | number,encoding?: null | BufferEncoding): Promise<{ buffer: string; bytesWritten: number }>; - ): Promise<void>;
Asynchronously writes data to a file, replacing the file if it already exists.
datacan be a string, a buffer, an AsyncIterable, or an Iterable object. The promise is fulfilled with no arguments upon success.If
optionsis a string, then it specifies theencoding.The
FileHandlehas to support writing.It is unsafe to use
filehandle.writeFile()multiple times on the same file without waiting for the promise to be fulfilled (or rejected).If one or more
filehandle.write()calls are made on a file handle and then afilehandle.writeFile()call is made, the data will be written from the current position till the end of the file. It doesn't always write from the beginning of the file. - buffers: TBuffers,position?: number
Write an array of ArrayBufferView s to the file.
The promise is fulfilled with an object containing a two properties:
It is unsafe to call
writev()multiple times on the same file without waiting for the promise to be fulfilled (or rejected).On Linux, positional writes don't work when the file is opened in append mode. The kernel ignores the position argument and always appends the data to the end of the file.
@param positionThe offset from the beginning of the file where the data from
buffersshould be written. Ifpositionis not anumber, the data will be written at the current position.