Skip to content

@caido/quickjs-types / llrt/stream / stream

stream

Classes

DefaultDuplexStream

Extends

Extended by

Implements

Constructors

new DefaultDuplexStream()

new DefaultDuplexStream(): DefaultDuplexStream

Returns

DefaultDuplexStream

Inherited from

DefaultReadableStream.constructor

Methods

addListener()
Call Signature

addListener(event: EventKey, listener: (...args: any[]) => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

DefaultWritableStream.addListener

Overrides

DefaultReadableStream.addListener

Call Signature

addListener(event: "close", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.addListener

Overrides

DefaultReadableStream.addListener

Call Signature

addListener(event: "error", listener: (err: Error) => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

DefaultWritableStream.addListener

Overrides

DefaultReadableStream.addListener

Call Signature

addListener(event: "finish", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.addListener

Overrides

DefaultReadableStream.addListener

destroy()

destroy(error?: Error): this

Destroy the stream. Optionally emit an 'error' event, and emit a 'close' event. After this call, the readable stream will release any internal resources and subsequent calls to push() will be ignored.

Once destroy() has been called any further calls will be a no-op and no further errors except from _destroy() may be emitted as 'error'.

Implementors should not override this method, but instead implement readable._destroy().

Parameters
ParameterTypeDescription
error?ErrorError which will be passed as payload in 'error' event
Returns

this

Inherited from

DefaultReadableStream.destroy

emit()
Call Signature

emit(event: EventKey, ...args: any[]): boolean

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

js
import { EventEmitter } from 'events';
const myEmitter = new EventEmitter();

// First listener
myEmitter.on('event', function firstListener() {
  console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
  const parameters = args.join(', ');
  console.log(`event with parameters ${parameters} in third listener`);
});

myEmitter.emit('event', 1, 2, 3, 4, 5);

// Prints:
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
Parameters
ParameterType
eventEventKey
...argsany[]
Returns

boolean

Implementation of

DefaultWritableStream.emit

Overrides

DefaultReadableStream.emit

Call Signature

emit(event: "close"): boolean

Parameters
ParameterType
event"close"
Returns

boolean

Implementation of

DefaultWritableStream.emit

Overrides

DefaultReadableStream.emit

Call Signature

emit(event: "error", err: Error): boolean

Parameters
ParameterType
event"error"
errError
Returns

boolean

Implementation of

DefaultWritableStream.emit

Overrides

DefaultReadableStream.emit

Call Signature

emit(event: "finish"): boolean

Parameters
ParameterType
event"finish"
Returns

boolean

Implementation of

DefaultWritableStream.emit

Overrides

DefaultReadableStream.emit

end()

end(): this

Calling the writable.end() method signals that no more data will be written to the Writable.

Calling the write method after calling end will raise an error.

Returns

this

Implementation of

DefaultWritableStream.end

eventNames()

eventNames(): EventKey[]

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

js
import { EventEmitter } from 'events';

const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});

const sym = Symbol('symbol');
myEE.on(sym, () => {});

console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns

EventKey[]

Implementation of

DefaultWritableStream.eventNames

Inherited from

DefaultReadableStream.eventNames

off()

off<K>(eventName: EventKey, listener: (...args: any[]) => void): this

Alias for emitter.removeListener().

Type Parameters
Type Parameter
K
Parameters
ParameterType
eventNameEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

DefaultWritableStream.off

Inherited from

DefaultReadableStream.off

on()
Call Signature

on(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

js
server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

DefaultWritableStream.on

Overrides

DefaultReadableStream.on

Call Signature

on(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.on

Overrides

DefaultReadableStream.on

Call Signature

on(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

DefaultWritableStream.on

Overrides

DefaultReadableStream.on

Call Signature

on(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.on

Overrides

DefaultReadableStream.on

once()
Call Signature

once(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Since

v0.3.0

Implementation of

DefaultWritableStream.once

Overrides

DefaultReadableStream.once

Call Signature

once(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.once

Overrides

DefaultReadableStream.once

Call Signature

once(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

DefaultWritableStream.once

Overrides

DefaultReadableStream.once

Call Signature

once(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.once

Overrides

DefaultReadableStream.once

prependListener()
Call Signature

prependListener(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

DefaultWritableStream.prependListener

Overrides

DefaultReadableStream.prependListener

Call Signature

prependListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.prependListener

Overrides

DefaultReadableStream.prependListener

Call Signature

prependListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

DefaultWritableStream.prependListener

Overrides

DefaultReadableStream.prependListener

Call Signature

prependListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.prependListener

Overrides

DefaultReadableStream.prependListener

prependOnceListener()
Call Signature

prependOnceListener(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

DefaultWritableStream.prependOnceListener

Overrides

DefaultReadableStream.prependOnceListener

Call Signature

prependOnceListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.prependOnceListener

Overrides

DefaultReadableStream.prependOnceListener

Call Signature

prependOnceListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

DefaultWritableStream.prependOnceListener

Overrides

DefaultReadableStream.prependOnceListener

Call Signature

prependOnceListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.prependOnceListener

Overrides

DefaultReadableStream.prependOnceListener

read()

read(size?: number): null | Buffer

The readable.read() method reads data out of the internal buffer and returns it. If no data is available to be read, null is returned. By default, the data is returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method or the stream is operating in object mode.

The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned.

If the size argument is not specified, all of the data contained in the internal buffer will be returned.

The size argument must be less than or equal to 1 GiB.

The readable.read() method should only be called on Readable streams operating in paused mode. In flowing mode, readable.read() is called automatically until the internal buffer is fully drained.

js
const readable = getReadableStreamSomehow();

// 'readable' may be triggered multiple times as data is buffered in
readable.on('readable', () => {
  let chunk;
  console.log('Stream is readable (new data received in buffer)');
  // Use a loop to make sure we read all currently available data
  while (null !== (chunk = readable.read())) {
    console.log(`Read ${chunk.length} bytes of data...`);
  }
});

// 'end' will be triggered once when there is no more data available
readable.on('end', () => {
  console.log('Reached end of stream.');
});

Each call to readable.read() returns a chunk of data, or null. The chunks are not concatenated. A while loop is necessary to consume all data currently in the buffer. When reading a large file .read() may return null, having consumed all buffered content so far, but there is still more data to come not yet buffered. In this case a new 'readable' event will be emitted when there is more data in the buffer. Finally the 'end' event will be emitted when there is no more data to come.

Therefore to read a file's whole contents from a readable, it is necessary to collect chunks across multiple 'readable' events:

js
const chunks = [];

readable.on('readable', () => {
  let chunk;
  while (null !== (chunk = readable.read())) {
    chunks.push(chunk);
  }
});

readable.on('end', () => {
  const content = chunks.join('');
});

A Readable stream in object mode will always return a single item from a call to readable.read(size), regardless of the value of the size argument.

If the readable.read() method returns a chunk of data, a 'data' event will also be emitted.

Calling read after the 'end' event has been emitted will return null. No runtime error will be raised.

Parameters
ParameterTypeDescription
size?numberOptional argument to specify how much data to read.
Returns

null | Buffer

Inherited from

DefaultReadableStream.read

removeListener()
Call Signature

removeListener(event: EventKey, listener: (...args: any[]) => void): this

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

js
import { EventEmitter } from 'events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();

const callbackA = () => {
  console.log('A');
  myEmitter.removeListener('event', callbackB);
};

const callbackB = () => {
  console.log('B');
};

myEmitter.on('event', callbackA);

myEmitter.on('event', callbackB);

// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
//   A
//   B

// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
//   A

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

js
import { EventEmitter } from 'events';
const ee = new EventEmitter();

function pong() {
  console.log('pong');
}

ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);

ee.emit('ping');
ee.emit('ping');

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

DefaultWritableStream.removeListener

Overrides

DefaultReadableStream.removeListener

Call Signature

removeListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.removeListener

Overrides

DefaultReadableStream.removeListener

Call Signature

removeListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

DefaultWritableStream.removeListener

Overrides

DefaultReadableStream.removeListener

Call Signature

removeListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

DefaultWritableStream.removeListener

Overrides

DefaultReadableStream.removeListener

write()

write(chunk: string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | Buffer, callback?: (error?: null | Error) => void): void

The writable.write() method writes some data to the stream, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument. The callback is usually called asynchronously and before 'error' is emitted.

js
function write(data, cb) {
  if (!stream.write(data)) {
    stream.once('drain', cb);
  } else {
    process.nextTick(cb);
  }
}

// Wait for cb to be called before doing any other write.
write('hello', () => {
  console.log('Write completed, do more writes now.');
});

A Writable stream in object mode will always ignore the encoding argument.

Parameters
ParameterTypeDescription
chunkstring | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | BufferOptional data to write. chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}.
callback?(error?: null | Error) => voidCallback for when this chunk of data is flushed.
Returns

void

false if the stream wishes for the calling code to wait for the 'drain' event to be emitted before continuing to write additional data; otherwise true.

Since

v0.9.4

Implementation of

DefaultWritableStream.write


DefaultReadableStream

Extends

Extended by

Constructors

new DefaultReadableStream()

new DefaultReadableStream(): DefaultReadableStream

Returns

DefaultReadableStream

Inherited from

ReadableStreamInner.constructor

Methods

addListener()
Call Signature

addListener(event: EventKey, listener: (...args: any[]) => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Inherited from

ReadableStreamInner.addListener

Call Signature

addListener(event: "close", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.addListener

Call Signature

addListener(event: "data", listener: (chunk: Buffer) => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Inherited from

ReadableStreamInner.addListener

Call Signature

addListener(event: "end", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"end"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.addListener

Call Signature

addListener(event: "error", listener: (err: Error) => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

ReadableStreamInner.addListener

Call Signature

addListener(event: "readable", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.addListener

destroy()

destroy(error?: Error): this

Destroy the stream. Optionally emit an 'error' event, and emit a 'close' event. After this call, the readable stream will release any internal resources and subsequent calls to push() will be ignored.

Once destroy() has been called any further calls will be a no-op and no further errors except from _destroy() may be emitted as 'error'.

Implementors should not override this method, but instead implement readable._destroy().

Parameters
ParameterTypeDescription
error?ErrorError which will be passed as payload in 'error' event
Returns

this

Inherited from

ReadableStreamInner.destroy

emit()
Call Signature

emit(event: EventKey, ...args: any[]): boolean

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

js
import { EventEmitter } from 'events';
const myEmitter = new EventEmitter();

// First listener
myEmitter.on('event', function firstListener() {
  console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
  const parameters = args.join(', ');
  console.log(`event with parameters ${parameters} in third listener`);
});

myEmitter.emit('event', 1, 2, 3, 4, 5);

// Prints:
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
Parameters
ParameterType
eventEventKey
...argsany[]
Returns

boolean

Inherited from

ReadableStreamInner.emit

Call Signature

emit(event: "close"): boolean

Parameters
ParameterType
event"close"
Returns

boolean

Inherited from

ReadableStreamInner.emit

Call Signature

emit(event: "data", chunk: Buffer): boolean

Parameters
ParameterType
event"data"
chunkBuffer
Returns

boolean

Inherited from

ReadableStreamInner.emit

Call Signature

emit(event: "end"): boolean

Parameters
ParameterType
event"end"
Returns

boolean

Inherited from

ReadableStreamInner.emit

Call Signature

emit(event: "error", err: Error): boolean

Parameters
ParameterType
event"error"
errError
Returns

boolean

Inherited from

ReadableStreamInner.emit

Call Signature

emit(event: "readable"): boolean

Parameters
ParameterType
event"readable"
Returns

boolean

Inherited from

ReadableStreamInner.emit

eventNames()

eventNames(): EventKey[]

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

js
import { EventEmitter } from 'events';

const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});

const sym = Symbol('symbol');
myEE.on(sym, () => {});

console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns

EventKey[]

Inherited from

ReadableStreamInner.eventNames

off()

off<K>(eventName: EventKey, listener: (...args: any[]) => void): this

Alias for emitter.removeListener().

Type Parameters
Type Parameter
K
Parameters
ParameterType
eventNameEventKey
listener(...args: any[]) => void
Returns

this

Inherited from

ReadableStreamInner.off

on()
Call Signature

on(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

js
server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Inherited from

ReadableStreamInner.on

Call Signature

on(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.on

Call Signature

on(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Inherited from

ReadableStreamInner.on

Call Signature

on(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.on

Call Signature

on(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

ReadableStreamInner.on

Call Signature

on(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.on

once()
Call Signature

once(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Since

v0.3.0

Inherited from

ReadableStreamInner.once

Call Signature

once(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.once

Call Signature

once(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Inherited from

ReadableStreamInner.once

Call Signature

once(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.once

Call Signature

once(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

ReadableStreamInner.once

Call Signature

once(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.once

prependListener()
Call Signature

prependListener(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Inherited from

ReadableStreamInner.prependListener

Call Signature

prependListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.prependListener

Call Signature

prependListener(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Inherited from

ReadableStreamInner.prependListener

Call Signature

prependListener(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.prependListener

Call Signature

prependListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

ReadableStreamInner.prependListener

Call Signature

prependListener(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.prependListener

prependOnceListener()
Call Signature

prependOnceListener(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Inherited from

ReadableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Inherited from

ReadableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

ReadableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.prependOnceListener

read()

read(size?: number): null | Buffer

The readable.read() method reads data out of the internal buffer and returns it. If no data is available to be read, null is returned. By default, the data is returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method or the stream is operating in object mode.

The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned.

If the size argument is not specified, all of the data contained in the internal buffer will be returned.

The size argument must be less than or equal to 1 GiB.

The readable.read() method should only be called on Readable streams operating in paused mode. In flowing mode, readable.read() is called automatically until the internal buffer is fully drained.

js
const readable = getReadableStreamSomehow();

// 'readable' may be triggered multiple times as data is buffered in
readable.on('readable', () => {
  let chunk;
  console.log('Stream is readable (new data received in buffer)');
  // Use a loop to make sure we read all currently available data
  while (null !== (chunk = readable.read())) {
    console.log(`Read ${chunk.length} bytes of data...`);
  }
});

// 'end' will be triggered once when there is no more data available
readable.on('end', () => {
  console.log('Reached end of stream.');
});

Each call to readable.read() returns a chunk of data, or null. The chunks are not concatenated. A while loop is necessary to consume all data currently in the buffer. When reading a large file .read() may return null, having consumed all buffered content so far, but there is still more data to come not yet buffered. In this case a new 'readable' event will be emitted when there is more data in the buffer. Finally the 'end' event will be emitted when there is no more data to come.

Therefore to read a file's whole contents from a readable, it is necessary to collect chunks across multiple 'readable' events:

js
const chunks = [];

readable.on('readable', () => {
  let chunk;
  while (null !== (chunk = readable.read())) {
    chunks.push(chunk);
  }
});

readable.on('end', () => {
  const content = chunks.join('');
});

A Readable stream in object mode will always return a single item from a call to readable.read(size), regardless of the value of the size argument.

If the readable.read() method returns a chunk of data, a 'data' event will also be emitted.

Calling read after the 'end' event has been emitted will return null. No runtime error will be raised.

Parameters
ParameterTypeDescription
size?numberOptional argument to specify how much data to read.
Returns

null | Buffer

Inherited from

ReadableStreamInner.read

removeListener()
Call Signature

removeListener(event: EventKey, listener: (...args: any[]) => void): this

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

js
import { EventEmitter } from 'events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();

const callbackA = () => {
  console.log('A');
  myEmitter.removeListener('event', callbackB);
};

const callbackB = () => {
  console.log('B');
};

myEmitter.on('event', callbackA);

myEmitter.on('event', callbackB);

// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
//   A
//   B

// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
//   A

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

js
import { EventEmitter } from 'events';
const ee = new EventEmitter();

function pong() {
  console.log('pong');
}

ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);

ee.emit('ping');
ee.emit('ping');

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Inherited from

ReadableStreamInner.removeListener

Call Signature

removeListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.removeListener

Call Signature

removeListener(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Inherited from

ReadableStreamInner.removeListener

Call Signature

removeListener(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.removeListener

Call Signature

removeListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

ReadableStreamInner.removeListener

Call Signature

removeListener(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Inherited from

ReadableStreamInner.removeListener


DefaultWritableStream

Extends

Constructors

new DefaultWritableStream()

new DefaultWritableStream(): DefaultWritableStream

Returns

DefaultWritableStream

Inherited from

WritableStreamInner.constructor

Methods

addListener()
Call Signature

addListener(event: EventKey, listener: (...args: any[]) => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Inherited from

WritableStreamInner.addListener

Call Signature

addListener(event: "close", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

WritableStreamInner.addListener

Call Signature

addListener(event: "error", listener: (err: Error) => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

WritableStreamInner.addListener

Call Signature

addListener(event: "finish", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Inherited from

WritableStreamInner.addListener

emit()
Call Signature

emit(event: EventKey, ...args: any[]): boolean

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

js
import { EventEmitter } from 'events';
const myEmitter = new EventEmitter();

// First listener
myEmitter.on('event', function firstListener() {
  console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
  const parameters = args.join(', ');
  console.log(`event with parameters ${parameters} in third listener`);
});

myEmitter.emit('event', 1, 2, 3, 4, 5);

// Prints:
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
Parameters
ParameterType
eventEventKey
...argsany[]
Returns

boolean

Inherited from

WritableStreamInner.emit

Call Signature

emit(event: "close"): boolean

Parameters
ParameterType
event"close"
Returns

boolean

Inherited from

WritableStreamInner.emit

Call Signature

emit(event: "error", err: Error): boolean

Parameters
ParameterType
event"error"
errError
Returns

boolean

Inherited from

WritableStreamInner.emit

Call Signature

emit(event: "finish"): boolean

Parameters
ParameterType
event"finish"
Returns

boolean

Inherited from

WritableStreamInner.emit

end()

end(): this

Calling the writable.end() method signals that no more data will be written to the Writable.

Calling the write method after calling end will raise an error.

Returns

this

Inherited from

WritableStreamInner.end

eventNames()

eventNames(): EventKey[]

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

js
import { EventEmitter } from 'events';

const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});

const sym = Symbol('symbol');
myEE.on(sym, () => {});

console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns

EventKey[]

Inherited from

WritableStreamInner.eventNames

off()

off<K>(eventName: EventKey, listener: (...args: any[]) => void): this

Alias for emitter.removeListener().

Type Parameters
Type Parameter
K
Parameters
ParameterType
eventNameEventKey
listener(...args: any[]) => void
Returns

this

Inherited from

WritableStreamInner.off

on()
Call Signature

on(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

js
server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Inherited from

WritableStreamInner.on

Call Signature

on(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

WritableStreamInner.on

Call Signature

on(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

WritableStreamInner.on

Call Signature

on(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Inherited from

WritableStreamInner.on

once()
Call Signature

once(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Since

v0.3.0

Inherited from

WritableStreamInner.once

Call Signature

once(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

WritableStreamInner.once

Call Signature

once(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

WritableStreamInner.once

Call Signature

once(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Inherited from

WritableStreamInner.once

prependListener()
Call Signature

prependListener(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Inherited from

WritableStreamInner.prependListener

Call Signature

prependListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

WritableStreamInner.prependListener

Call Signature

prependListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

WritableStreamInner.prependListener

Call Signature

prependListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Inherited from

WritableStreamInner.prependListener

prependOnceListener()
Call Signature

prependOnceListener(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Inherited from

WritableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

WritableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

WritableStreamInner.prependOnceListener

Call Signature

prependOnceListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Inherited from

WritableStreamInner.prependOnceListener

removeListener()
Call Signature

removeListener(event: EventKey, listener: (...args: any[]) => void): this

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

js
import { EventEmitter } from 'events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();

const callbackA = () => {
  console.log('A');
  myEmitter.removeListener('event', callbackB);
};

const callbackB = () => {
  console.log('B');
};

myEmitter.on('event', callbackA);

myEmitter.on('event', callbackB);

// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
//   A
//   B

// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
//   A

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

js
import { EventEmitter } from 'events';
const ee = new EventEmitter();

function pong() {
  console.log('pong');
}

ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);

ee.emit('ping');
ee.emit('ping');

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Inherited from

WritableStreamInner.removeListener

Call Signature

removeListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Inherited from

WritableStreamInner.removeListener

Call Signature

removeListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Inherited from

WritableStreamInner.removeListener

Call Signature

removeListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Inherited from

WritableStreamInner.removeListener

write()

write(chunk: string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | Buffer, callback?: (error?: null | Error) => void): void

The writable.write() method writes some data to the stream, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument. The callback is usually called asynchronously and before 'error' is emitted.

js
function write(data, cb) {
  if (!stream.write(data)) {
    stream.once('drain', cb);
  } else {
    process.nextTick(cb);
  }
}

// Wait for cb to be called before doing any other write.
write('hello', () => {
  console.log('Write completed, do more writes now.');
});

A Writable stream in object mode will always ignore the encoding argument.

Parameters
ParameterTypeDescription
chunkstring | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | BufferOptional data to write. chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}.
callback?(error?: null | Error) => voidCallback for when this chunk of data is flushed.
Returns

void

false if the stream wishes for the calling code to wait for the 'drain' event to be emitted before continuing to write additional data; otherwise true.

Since

v0.9.4

Inherited from

WritableStreamInner.write


ReadableStreamInner

Extends

Extended by

Implements

Constructors

new ReadableStreamInner()

new ReadableStreamInner(): ReadableStreamInner

Returns

ReadableStreamInner

Inherited from

EventEmitter.constructor

Methods

addListener()
Call Signature

addListener(event: EventKey, listener: (...args: any[]) => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

ReadableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "close", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "data", listener: (chunk: Buffer) => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Implementation of

QuickJS.ReadableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "end", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"end"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "error", listener: (err: Error) => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.ReadableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "readable", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. data
  3. end
  4. error
  5. readable
Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.addListener

Overrides

EventEmitter.addListener

destroy()

destroy(error?: Error): this

Destroy the stream. Optionally emit an 'error' event, and emit a 'close' event. After this call, the readable stream will release any internal resources and subsequent calls to push() will be ignored.

Once destroy() has been called any further calls will be a no-op and no further errors except from _destroy() may be emitted as 'error'.

Implementors should not override this method, but instead implement readable._destroy().

Parameters
ParameterTypeDescription
error?ErrorError which will be passed as payload in 'error' event
Returns

this

emit()
Call Signature

emit(event: EventKey, ...args: any[]): boolean

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

js
import { EventEmitter } from 'events';
const myEmitter = new EventEmitter();

// First listener
myEmitter.on('event', function firstListener() {
  console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
  const parameters = args.join(', ');
  console.log(`event with parameters ${parameters} in third listener`);
});

myEmitter.emit('event', 1, 2, 3, 4, 5);

// Prints:
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
Parameters
ParameterType
eventEventKey
...argsany[]
Returns

boolean

Implementation of

ReadableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "close"): boolean

Parameters
ParameterType
event"close"
Returns

boolean

Implementation of

QuickJS.ReadableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "data", chunk: Buffer): boolean

Parameters
ParameterType
event"data"
chunkBuffer
Returns

boolean

Implementation of

QuickJS.ReadableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "end"): boolean

Parameters
ParameterType
event"end"
Returns

boolean

Implementation of

QuickJS.ReadableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "error", err: Error): boolean

Parameters
ParameterType
event"error"
errError
Returns

boolean

Implementation of

QuickJS.ReadableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "readable"): boolean

Parameters
ParameterType
event"readable"
Returns

boolean

Implementation of

QuickJS.ReadableStream.emit

Overrides

EventEmitter.emit

eventNames()

eventNames(): EventKey[]

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

js
import { EventEmitter } from 'events';

const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});

const sym = Symbol('symbol');
myEE.on(sym, () => {});

console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns

EventKey[]

Implementation of

ReadableStream.eventNames

Inherited from

EventEmitter.eventNames

off()

off<K>(eventName: EventKey, listener: (...args: any[]) => void): this

Alias for emitter.removeListener().

Type Parameters
Type Parameter
K
Parameters
ParameterType
eventNameEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

ReadableStream.off

Inherited from

EventEmitter.off

on()
Call Signature

on(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

js
server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

ReadableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Implementation of

QuickJS.ReadableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.ReadableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.on

Overrides

EventEmitter.on

once()
Call Signature

once(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Since

v0.3.0

Implementation of

ReadableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Implementation of

QuickJS.ReadableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.ReadableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.once

Overrides

EventEmitter.once

prependListener()
Call Signature

prependListener(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

ReadableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependListener

Overrides

EventEmitter.prependListener

prependOnceListener()
Call Signature

prependOnceListener(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

ReadableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

read()

read(size?: number): null | Buffer

The readable.read() method reads data out of the internal buffer and returns it. If no data is available to be read, null is returned. By default, the data is returned as a Buffer object unless an encoding has been specified using the readable.setEncoding() method or the stream is operating in object mode.

The optional size argument specifies a specific number of bytes to read. If size bytes are not available to be read, null will be returned unless the stream has ended, in which case all of the data remaining in the internal buffer will be returned.

If the size argument is not specified, all of the data contained in the internal buffer will be returned.

The size argument must be less than or equal to 1 GiB.

The readable.read() method should only be called on Readable streams operating in paused mode. In flowing mode, readable.read() is called automatically until the internal buffer is fully drained.

js
const readable = getReadableStreamSomehow();

// 'readable' may be triggered multiple times as data is buffered in
readable.on('readable', () => {
  let chunk;
  console.log('Stream is readable (new data received in buffer)');
  // Use a loop to make sure we read all currently available data
  while (null !== (chunk = readable.read())) {
    console.log(`Read ${chunk.length} bytes of data...`);
  }
});

// 'end' will be triggered once when there is no more data available
readable.on('end', () => {
  console.log('Reached end of stream.');
});

Each call to readable.read() returns a chunk of data, or null. The chunks are not concatenated. A while loop is necessary to consume all data currently in the buffer. When reading a large file .read() may return null, having consumed all buffered content so far, but there is still more data to come not yet buffered. In this case a new 'readable' event will be emitted when there is more data in the buffer. Finally the 'end' event will be emitted when there is no more data to come.

Therefore to read a file's whole contents from a readable, it is necessary to collect chunks across multiple 'readable' events:

js
const chunks = [];

readable.on('readable', () => {
  let chunk;
  while (null !== (chunk = readable.read())) {
    chunks.push(chunk);
  }
});

readable.on('end', () => {
  const content = chunks.join('');
});

A Readable stream in object mode will always return a single item from a call to readable.read(size), regardless of the value of the size argument.

If the readable.read() method returns a chunk of data, a 'data' event will also be emitted.

Calling read after the 'end' event has been emitted will return null. No runtime error will be raised.

Parameters
ParameterTypeDescription
size?numberOptional argument to specify how much data to read.
Returns

null | Buffer

Implementation of

ReadableStream.read

removeListener()
Call Signature

removeListener(event: EventKey, listener: (...args: any[]) => void): this

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

js
import { EventEmitter } from 'events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();

const callbackA = () => {
  console.log('A');
  myEmitter.removeListener('event', callbackB);
};

const callbackB = () => {
  console.log('B');
};

myEmitter.on('event', callbackA);

myEmitter.on('event', callbackB);

// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
//   A
//   B

// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
//   A

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

js
import { EventEmitter } from 'events';
const ee = new EventEmitter();

function pong() {
  console.log('pong');
}

ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);

ee.emit('ping');
ee.emit('ping');

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

ReadableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "data", listener: (chunk: Buffer) => void): this

Parameters
ParameterType
event"data"
listener(chunk: Buffer) => void
Returns

this

Implementation of

QuickJS.ReadableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "end", listener: () => void): this

Parameters
ParameterType
event"end"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.ReadableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "readable", listener: () => void): this

Parameters
ParameterType
event"readable"
listener() => void
Returns

this

Implementation of

QuickJS.ReadableStream.removeListener

Overrides

EventEmitter.removeListener


WritableStreamInner

Extends

Extended by

Implements

Constructors

new WritableStreamInner()

new WritableStreamInner(): WritableStreamInner

Returns

WritableStreamInner

Inherited from

EventEmitter.constructor

Methods

addListener()
Call Signature

addListener(event: EventKey, listener: (...args: any[]) => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

WritableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "close", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "error", listener: (err: Error) => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.WritableStream.addListener

Overrides

EventEmitter.addListener

Call Signature

addListener(event: "finish", listener: () => void): this

Event emitter The defined events on documents including:

  1. close
  2. error
  3. finish
Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.addListener

Overrides

EventEmitter.addListener

emit()
Call Signature

emit(event: EventKey, ...args: any[]): boolean

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

js
import { EventEmitter } from 'events';
const myEmitter = new EventEmitter();

// First listener
myEmitter.on('event', function firstListener() {
  console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
  console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
  const parameters = args.join(', ');
  console.log(`event with parameters ${parameters} in third listener`);
});

myEmitter.emit('event', 1, 2, 3, 4, 5);

// Prints:
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listener
Parameters
ParameterType
eventEventKey
...argsany[]
Returns

boolean

Implementation of

WritableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "close"): boolean

Parameters
ParameterType
event"close"
Returns

boolean

Implementation of

QuickJS.WritableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "error", err: Error): boolean

Parameters
ParameterType
event"error"
errError
Returns

boolean

Implementation of

QuickJS.WritableStream.emit

Overrides

EventEmitter.emit

Call Signature

emit(event: "finish"): boolean

Parameters
ParameterType
event"finish"
Returns

boolean

Implementation of

QuickJS.WritableStream.emit

Overrides

EventEmitter.emit

end()

end(): this

Calling the writable.end() method signals that no more data will be written to the Writable.

Calling the write method after calling end will raise an error.

Returns

this

Implementation of

WritableStream.end

eventNames()

eventNames(): EventKey[]

Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.

js
import { EventEmitter } from 'events';

const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});

const sym = Symbol('symbol');
myEE.on(sym, () => {});

console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns

EventKey[]

Implementation of

WritableStream.eventNames

Inherited from

EventEmitter.eventNames

off()

off<K>(eventName: EventKey, listener: (...args: any[]) => void): this

Alias for emitter.removeListener().

Type Parameters
Type Parameter
K
Parameters
ParameterType
eventNameEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

WritableStream.off

Inherited from

EventEmitter.off

on()
Call Signature

on(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

js
server.on('connection', (stream) => {
  console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

WritableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.WritableStream.on

Overrides

EventEmitter.on

Call Signature

on(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.on

Overrides

EventEmitter.on

once()
Call Signature

once(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

js
import { EventEmitter } from 'events';
const myEE = new EventEmitter();
myEE.once('foo', () => console.log('a'));
myEE.prependOnceListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
//   b
//   a
Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Since

v0.3.0

Implementation of

WritableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.WritableStream.once

Overrides

EventEmitter.once

Call Signature

once(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.once

Overrides

EventEmitter.once

prependListener()
Call Signature

prependListener(event: EventKey, listener: (...args: any[]) => void): this

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

WritableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.WritableStream.prependListener

Overrides

EventEmitter.prependListener

Call Signature

prependListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.prependListener

Overrides

EventEmitter.prependListener

prependOnceListener()
Call Signature

prependOnceListener(event: EventKey, listener: (...args: any[]) => void): this

Adds a one-timelistener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterTypeDescription
eventEventKey-
listener(...args: any[]) => voidThe callback function
Returns

this

Implementation of

WritableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.WritableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

Call Signature

prependOnceListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.prependOnceListener

Overrides

EventEmitter.prependOnceListener

removeListener()
Call Signature

removeListener(event: EventKey, listener: (...args: any[]) => void): this

Removes the specified listener from the listener array for the event named eventName.

removeListener() will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified eventName, then removeListener() must be called multiple times to remove each instance.

Once an event is emitted, all listeners attached to it at the time of emitting are called in order. This implies that any removeListener() calls after emitting and before the last listener finishes execution will not remove them from emit() in progress. Subsequent events behave as expected.

js
import { EventEmitter } from 'events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();

const callbackA = () => {
  console.log('A');
  myEmitter.removeListener('event', callbackB);
};

const callbackB = () => {
  console.log('B');
};

myEmitter.on('event', callbackA);

myEmitter.on('event', callbackB);

// callbackA removes listener callbackB but it will still be called.
// Internal listener array at time of emit [callbackA, callbackB]
myEmitter.emit('event');
// Prints:
//   A
//   B

// callbackB is now removed.
// Internal listener array [callbackA]
myEmitter.emit('event');
// Prints:
//   A

Because listeners are managed using an internal array, calling this will change the position indices of any listener registered after the listener being removed. This will not impact the order in which listeners are called, but it means that any copies of the listener array as returned by the emitter.listeners() method will need to be recreated.

When a single function has been added as a handler multiple times for a single event (as in the example below), removeListener() will remove the most recently added instance. In the example the once('ping') listener is removed:

js
import { EventEmitter } from 'events';
const ee = new EventEmitter();

function pong() {
  console.log('pong');
}

ee.on('ping', pong);
ee.once('ping', pong);
ee.removeListener('ping', pong);

ee.emit('ping');
ee.emit('ping');

Returns a reference to the EventEmitter, so that calls can be chained.

Parameters
ParameterType
eventEventKey
listener(...args: any[]) => void
Returns

this

Implementation of

WritableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "close", listener: () => void): this

Parameters
ParameterType
event"close"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "error", listener: (err: Error) => void): this

Parameters
ParameterType
event"error"
listener(err: Error) => void
Returns

this

Implementation of

QuickJS.WritableStream.removeListener

Overrides

EventEmitter.removeListener

Call Signature

removeListener(event: "finish", listener: () => void): this

Parameters
ParameterType
event"finish"
listener() => void
Returns

this

Implementation of

QuickJS.WritableStream.removeListener

Overrides

EventEmitter.removeListener

write()

write(chunk: string | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | Buffer, callback?: (error?: null | Error) => void): void

The writable.write() method writes some data to the stream, and calls the supplied callback once the data has been fully handled. If an error occurs, the callback will be called with the error as its first argument. The callback is usually called asynchronously and before 'error' is emitted.

js
function write(data, cb) {
  if (!stream.write(data)) {
    stream.once('drain', cb);
  } else {
    process.nextTick(cb);
  }
}

// Wait for cb to be called before doing any other write.
write('hello', () => {
  console.log('Write completed, do more writes now.');
});

A Writable stream in object mode will always ignore the encoding argument.

Parameters
ParameterTypeDescription
chunkstring | ArrayBuffer | SharedArrayBuffer | ArrayBufferView | BufferOptional data to write. chunk must be a {string}, {Buffer}, {TypedArray} or {DataView}.
callback?(error?: null | Error) => voidCallback for when this chunk of data is flushed.
Returns

void

false if the stream wishes for the calling code to wait for the 'drain' event to be emitted before continuing to write additional data; otherwise true.

Since

v0.9.4

Implementation of

WritableStream.write