You're Invited:Meet the Socket Team at RSAC and BSidesSF 2026, March 23–26.RSVP
Socket
Book a DemoSign in
Socket

@libp2p/mplex

Package Overview
Dependencies
Maintainers
6
Versions
854
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@libp2p/mplex - npm Package Compare versions

Comparing version
8.0.4
to
9.0.4
+4
-3
dist/src/encode.js
import batchedBytes from 'it-batched-bytes';
import * as varint from 'uint8-varint';
import { Uint8ArrayList } from 'uint8arraylist';
import varint from 'varint';
import { allocUnsafe } from './alloc-unsafe.js';

@@ -21,10 +21,11 @@ import { MessageTypes } from './message-types.js';

varint.encode(msg.id << 3 | msg.type, pool, offset);
offset += varint.encode.bytes ?? 0;
offset += varint.encodingLength(msg.id << 3 | msg.type);
if ((msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) && msg.data != null) {
varint.encode(msg.data.length, pool, offset);
offset += varint.encodingLength(msg.data.length);
}
else {
varint.encode(0, pool, offset);
offset += varint.encodingLength(0);
}
offset += varint.encode.bytes ?? 0;
const header = pool.subarray(this._poolOffset, offset);

@@ -31,0 +32,0 @@ if (POOL_SIZE - offset < 100) {

@@ -1,1 +0,1 @@

{"version":3,"file":"encode.js","sourceRoot":"","sources":["../../src/encode.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,kBAAkB,CAAA;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,MAAM,MAAM,QAAQ,CAAA;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,EAAgB,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAG/D,MAAM,SAAS,GAAG,EAAE,GAAG,IAAI,CAAA;AAE3B,MAAM,OAAO;IACH,KAAK,CAAY;IACjB,WAAW,CAAQ;IAE3B;QACE,IAAI,CAAC,KAAK,GAAG,WAAW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AAY,CAAC,MAAM,EAAE;QAC3B,IAAI,EAAE,YAAY;QAClB,SAAS,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;YACvB,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE;gBACnB,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAA;aACvB;QACH,CAAC;KACF,CAAC,CAAA;AACJ,CAAC"}

@@ -1,2 +0,2 @@

import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer';
import type { StreamMuxerFactory } from '@libp2p/interface/stream-muxer';
export interface MplexInit {

@@ -3,0 +3,0 @@ /**

import { type Message } from './message-types.js';
import { type MplexStream } from './stream.js';
import type { MplexInit } from './index.js';
import type { Stream } from '@libp2p/interface-connection';
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface-stream-muxer';
import type { AbortOptions } from '@libp2p/interface';
import type { Stream } from '@libp2p/interface/connection';
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface/stream-muxer';
import type { Sink, Source } from 'it-stream-types';
import type { Uint8ArrayList } from 'uint8arraylist';
export interface MplexStream extends Stream {
sourceReadableLength: () => number;
sourcePush: (data: Uint8ArrayList) => void;
}
interface MplexStreamMuxerInit extends MplexInit, StreamMuxerInit {
/**
* The default timeout to use in ms when shutting down the muxer.
*/
closeTimeout?: number;
}

@@ -23,2 +25,3 @@ export declare class MplexStreamMuxer implements StreamMuxer {

private readonly rateLimiter;
private readonly closeTimeout;
constructor(init?: MplexStreamMuxerInit);

@@ -37,3 +40,4 @@ /**

*/
close(err?: Error | undefined): void;
close(options?: AbortOptions): Promise<void>;
abort(err: Error): void;
/**

@@ -57,7 +61,2 @@ * Called whenever an inbound stream is created

_createSink(): Sink<Source<Uint8ArrayList | Uint8Array>, Promise<void>>;
/**
* Creates a source that restricts outgoing message sizes
* and varint encodes them
*/
_createSource(): any;
_handleIncoming(message: Message): Promise<void>;

@@ -64,0 +63,0 @@ }

@@ -1,1 +0,1 @@

{"version":3,"file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CnI;;;OAGG;IACH,WAAW,IAAK,IAAI,CAAC,MAAM,CAAC,cAAc,GAAG,UAAU,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAyBlE,eAAe,CAAE,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;CAsGxD"}

@@ -1,5 +0,5 @@

import { CodeError } from '@libp2p/interfaces/errors';
import { CodeError } from '@libp2p/interface/errors';
import { logger } from '@libp2p/logger';
import { abortableSource } from 'abortable-iterator';
import { anySignal } from 'any-signal';
import { pipe } from 'it-pipe';
import { pushableV } from 'it-pushable';

@@ -17,2 +17,3 @@ import { RateLimiterMemory } from 'rate-limiter-flexible';

const DISCONNECT_THRESHOLD = 5;
const CLOSE_TIMEOUT = 500;
function printMessage(msg) {

@@ -41,2 +42,3 @@ const output = {

rateLimiter;
closeTimeout;
constructor(init) {

@@ -56,2 +58,3 @@ init = init ?? {};

this._init = init;
this.closeTimeout = init.closeTimeout ?? CLOSE_TIMEOUT;
/**

@@ -64,5 +67,16 @@ * An iterable sink

*/
const source = this._createSource();
this._source = source;
this.source = source;
this._source = pushableV({
objectMode: true,
onEnd: () => {
// the source has ended, we can't write any more messages to gracefully
// close streams so all we can do is destroy them
for (const stream of this._streams.initiators.values()) {
stream.destroy();
}
for (const stream of this._streams.receivers.values()) {
stream.destroy();
}
}
});
this.source = pipe(this._source, source => encode(source, this._init.minSendBytes));
/**

@@ -107,13 +121,30 @@ * Close controller

*/
close(err) {
if (this.closeController.signal.aborted)
async close(options) {
if (this.closeController.signal.aborted) {
return;
if (err != null) {
this.streams.forEach(s => { s.abort(err); });
}
else {
this.streams.forEach(s => { s.close(); });
const signal = options?.signal ?? AbortSignal.timeout(this.closeTimeout);
try {
// try to gracefully close all streams
await Promise.all(this.streams.map(async (s) => s.close({
signal
})));
this._source.end();
// try to gracefully close the muxer
await this._source.onEmpty({
signal
});
this.closeController.abort();
}
this.closeController.abort();
catch (err) {
this.abort(err);
}
}
abort(err) {
if (this.closeController.signal.aborted) {
return;
}
this.streams.forEach(s => { s.abort(err); });
this.closeController.abort(err);
}
/**

@@ -136,3 +167,3 @@ * Called whenever an inbound stream is created

}
const send = (msg) => {
const send = async (msg) => {
if (log.enabled) {

@@ -144,3 +175,3 @@ log.trace('%s stream %s send', type, id, printMessage(msg));

const onEnd = () => {
log('%s stream with id %s and protocol %s ended', type, id, stream.stat.protocol);
log('%s stream with id %s and protocol %s ended', type, id, stream.protocol);
registry.delete(id);

@@ -161,5 +192,6 @@ if (this._init.onStreamEnd != null) {

const sink = async (source) => {
const signal = anySignal([this.closeController.signal, this._init.signal]);
try {
source = abortableSource(source, signal);
source = abortableSource(source, this.closeController.signal, {
returnOnAbort: true
});
const decoder = new Decoder(this._init.maxMsgSize, this._init.maxUnprocessedMessageQueueSize);

@@ -177,26 +209,5 @@ for await (const chunk of source) {

}
finally {
signal.clear();
}
};
return sink;
}
/**
* Creates a source that restricts outgoing message sizes
* and varint encodes them
*/
_createSource() {
const onEnd = (err) => {
this.close(err);
};
const source = pushableV({
objectMode: true,
onEnd
});
return Object.assign(encode(source, this._init.minSendBytes), {
push: source.push,
end: source.end,
return: source.return
});
}
async _handleIncoming(message) {

@@ -226,3 +237,3 @@ const { id, type } = message;

// since there's no backpressure in mplex, the only thing we can really do to protect ourselves is close the connection
this._source.end(new Error('Too many open streams'));
this.abort(new Error('Too many open streams'));
return;

@@ -242,37 +253,53 @@ }

log('missing stream %s for message type %s', id, MessageTypeNames[type]);
// if the remote keeps sending us messages for streams that have been
// closed or were never opened they may be attacking us so if they do
// this very quickly all we can do is close the connection
try {
await this.rateLimiter.consume('missing-stream', 1);
}
catch {
log('rate limit hit when receiving messages for streams that do not exist - closing remote connection');
// since there's no backpressure in mplex, the only thing we can really do to protect ourselves is close the connection
this.abort(new Error('Too many messages for missing streams'));
return;
}
return;
}
const maxBufferSize = this._init.maxStreamBufferSize ?? MAX_STREAM_BUFFER_SIZE;
switch (type) {
case MessageTypes.MESSAGE_INITIATOR:
case MessageTypes.MESSAGE_RECEIVER:
if (stream.sourceReadableLength() > maxBufferSize) {
// Stream buffer has got too large, reset the stream
this._source.push({
id: message.id,
type: type === MessageTypes.MESSAGE_INITIATOR ? MessageTypes.RESET_RECEIVER : MessageTypes.RESET_INITIATOR
});
// Inform the stream consumer they are not fast enough
const error = new CodeError('Input buffer full - increase Mplex maxBufferSize to accommodate slow consumers', 'ERR_STREAM_INPUT_BUFFER_FULL');
stream.abort(error);
return;
}
// We got data from the remote, push it into our local stream
stream.sourcePush(message.data);
break;
case MessageTypes.CLOSE_INITIATOR:
case MessageTypes.CLOSE_RECEIVER:
// We should expect no more data from the remote, stop reading
stream.closeRead();
break;
case MessageTypes.RESET_INITIATOR:
case MessageTypes.RESET_RECEIVER:
// Stop reading and writing to the stream immediately
stream.reset();
break;
default:
log('unknown message type %s', type);
try {
switch (type) {
case MessageTypes.MESSAGE_INITIATOR:
case MessageTypes.MESSAGE_RECEIVER:
if (stream.sourceReadableLength() > maxBufferSize) {
// Stream buffer has got too large, reset the stream
this._source.push({
id: message.id,
type: type === MessageTypes.MESSAGE_INITIATOR ? MessageTypes.RESET_RECEIVER : MessageTypes.RESET_INITIATOR
});
// Inform the stream consumer they are not fast enough
throw new CodeError('Input buffer full - increase Mplex maxBufferSize to accommodate slow consumers', 'ERR_STREAM_INPUT_BUFFER_FULL');
}
// We got data from the remote, push it into our local stream
stream.sourcePush(message.data);
break;
case MessageTypes.CLOSE_INITIATOR:
case MessageTypes.CLOSE_RECEIVER:
// The remote has stopped writing, so we can stop reading
stream.remoteCloseWrite();
break;
case MessageTypes.RESET_INITIATOR:
case MessageTypes.RESET_RECEIVER:
// The remote has errored, stop reading and writing to the stream immediately
stream.reset();
break;
default:
log('unknown message type %s', type);
}
}
catch (err) {
log.error('error while processing message', err);
stream.abort(err);
}
}
}
//# sourceMappingURL=mplex.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"mplex.js","sourceRoot":"","sources":["../../src/mplex.ts"],"names":[],"mappings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KAAK,YAAY,CAAC,iBAAiB,CAAC,CAAC,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC,YAAY,CAAC,eAAe;yBAC3G,CAAC,CAAA;wBAEF,sDAAsD;wBACtD,MAAM,IAAI,SAAS,CAAC,gFAAgF,EAAE,8BAA8B,CAAC,CAAA;qBACtI;oBAED,6DAA6D;oBAC7D,MAAM,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;oBAC/B,MAAK;gBACP,KAAK,YAAY,CAAC,eAAe,CAAC;gBAClC,KAAK,YAAY,CAAC,cAAc;oBAC9B,yDAAyD;oBACzD,MAAM,CAAC,gBAAgB,EAAE,CAAA;oBACzB,MAAK;gBACP,KAAK,YAAY,CAAC,eAAe,CAAC;gBAClC,KAAK,YAAY,CAAC,cAAc;oBAC9B,6EAA6E;oBAC7E,MAAM,CAAC,KAAK,EAAE,CAAA;oBACd,MAAK;gBACP;oBACE,GAAG,CAAC,yBAAyB,EAAE,IAAI,CAAC,CAAA;aACvC;SACF;QAAC,OAAO,GAAQ,EAAE;YACjB,GAAG,CAAC,KAAK,CAAC,gCAAgC,EAAE,GAAG,CAAC,CAAA;YAChD,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;SAClB;IACH,CAAC;CACF"}

@@ -1,2 +0,2 @@

import { AbstractStream, type AbstractStreamInit } from '@libp2p/interface-stream-muxer/stream';
import { AbstractStream, type AbstractStreamInit } from '@libp2p/interface/stream-muxer/stream';
import { Uint8ArrayList } from 'uint8arraylist';

@@ -6,3 +6,3 @@ import type { Message } from './message-types.js';

id: number;
send: (msg: Message) => void;
send: (msg: Message) => Promise<void>;
name?: string;

@@ -16,5 +16,10 @@ onEnd?: (err?: Error) => void;

name: string;
send: (msg: Message) => void;
send: (msg: Message) => Promise<void>;
/**
* The maximum allowable data size, any data larger than this will be
* chunked and sent in multiple data messages
*/
maxDataSize: number;
}
declare class MplexStream extends AbstractStream {
export declare class MplexStream extends AbstractStream {
private readonly name;

@@ -24,8 +29,9 @@ private readonly streamId;

private readonly types;
private readonly maxDataSize;
constructor(init: MplexStreamInit);
sendNewStream(): void;
sendData(data: Uint8ArrayList): void;
sendReset(): void;
sendCloseWrite(): void;
sendCloseRead(): void;
sendNewStream(): Promise<void>;
sendData(data: Uint8ArrayList): Promise<void>;
sendReset(): Promise<void>;
sendCloseWrite(): Promise<void>;
sendCloseRead(): Promise<void>;
}

@@ -32,0 +38,0 @@ export declare function createStream(options: Options): MplexStream;

@@ -1,1 +0,1 @@

{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,KAAK,kBAAkB,EAAE,MAAM,uCAAuC,CAAA;AAC/F,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAI/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAEjD,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAA;IACV,IAAI,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAA;IAC5B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,CAAA;IAC7B,IAAI,CAAC,EAAE,WAAW,GAAG,UAAU,CAAA;IAC/B,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,UAAU,eAAgB,SAAQ,kBAAkB;IAClD,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAA;CAC7B;AAED,cAAM,WAAY,SAAQ,cAAc;IACtC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAQ;IAC7B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAQ;IACjC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAwB;IAC7C,OAAO,CAAC,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I,CAAiC;IACtD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAwB;IAC9C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAQ;gBAEvB,IAAI,EAAE,eAAe;IAU5B,aAAa,IAAK,OAAO,CAAC,IAAI,CAAC;IAI/B,QAAQ,CAAE,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC;IAe9C,SAAS,IAAK,OAAO,CAAC,IAAI,CAAC;IAI3B,cAAc,IAAK,OAAO,CAAC,IAAI,CAAC;IAIhC,aAAa,IAAK,OAAO,CAAC,IAAI,CAAC;CAGtC;AAED,wBAAgB,YAAY,CAAE,OAAO,EAAE,OAAO,GAAG,WAAW,CAa3D"}

@@ -1,2 +0,3 @@

import { AbstractStream } from '@libp2p/interface-stream-muxer/stream';
import { AbstractStream } from '@libp2p/interface/stream-muxer/stream';
import { logger } from '@libp2p/logger';
import { Uint8ArrayList } from 'uint8arraylist';

@@ -6,3 +7,3 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';

import { InitiatorMessageTypes, ReceiverMessageTypes } from './message-types.js';
class MplexStream extends AbstractStream {
export class MplexStream extends AbstractStream {
name;

@@ -12,2 +13,3 @@ streamId;

types;
maxDataSize;
constructor(init) {

@@ -19,16 +21,26 @@ super(init);

this.streamId = init.streamId;
this.maxDataSize = init.maxDataSize;
}
sendNewStream() {
this.send({ id: this.streamId, type: InitiatorMessageTypes.NEW_STREAM, data: new Uint8ArrayList(uint8ArrayFromString(this.name)) });
async sendNewStream() {
await this.send({ id: this.streamId, type: InitiatorMessageTypes.NEW_STREAM, data: new Uint8ArrayList(uint8ArrayFromString(this.name)) });
}
sendData(data) {
this.send({ id: this.streamId, type: this.types.MESSAGE, data });
async sendData(data) {
data = data.sublist();
while (data.byteLength > 0) {
const toSend = Math.min(data.byteLength, this.maxDataSize);
await this.send({
id: this.streamId,
type: this.types.MESSAGE,
data: data.sublist(0, toSend)
});
data.consume(toSend);
}
}
sendReset() {
this.send({ id: this.streamId, type: this.types.RESET });
async sendReset() {
await this.send({ id: this.streamId, type: this.types.RESET });
}
sendCloseWrite() {
this.send({ id: this.streamId, type: this.types.CLOSE });
async sendCloseWrite() {
await this.send({ id: this.streamId, type: this.types.CLOSE });
}
sendCloseRead() {
async sendCloseRead() {
// mplex does not support close read, only close write

@@ -46,5 +58,6 @@ }

onEnd,
send
send,
log: logger(`libp2p:mplex:stream:${type}:${id}`)
});
}
//# sourceMappingURL=stream.js.map

@@ -1,1 +0,1 @@

{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAA2B,MAAM,uCAAuC,CAAA;AAC/F,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAA;AAkBhF,MAAM,WAAY,SAAQ,cAAc;IACrB,IAAI,CAAQ;IACZ,QAAQ,CAAQ;IAChB,IAAI,CAAwB;IAC5B,KAAK,CAAwB;IAE9C,YAAa,IAAqB;QAChC,KAAK,CAAC,IAAI,CAAC,CAAA;QAEX,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,SAAS,KAAK,UAAU,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,oBAAoB,CAAA;QACzF,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACrB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;QACrB,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAA;IAC/B,CAAC;IAED,aAAa;QACX,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,EAAE,qBAAqB,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D,QAAQ,EAAE,EAAE;QACZ,IAAI,EAAE,GAAG,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE;QACnC,SAAS,EAAE,IAAI,KAAK,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS;QACxD,WAAW,EAAE,UAAU;QACvB,KAAK;QACL,IAAI;QACJ,GAAG,EAAE,MAAM,CAAC,uBAAuB,IAAI,IAAI,EAAE,EAAE,CAAC;KACjD,CAAC,CAAA;AACJ,CAAC"}
{
"MplexInit": "https://libp2p.github.io/js-libp2p-mplex/interfaces/MplexInit.html",
"mplex": "https://libp2p.github.io/js-libp2p-mplex/functions/mplex.html"
"MplexInit": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_mplex.MplexInit.html",
"mplex": "https://libp2p.github.io/js-libp2p/functions/_libp2p_mplex.mplex.html"
}
{
"name": "@libp2p/mplex",
"version": "8.0.4",
"version": "9.0.4",
"description": "JavaScript implementation of https://github.com/libp2p/mplex",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/libp2p/js-libp2p-mplex#readme",
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/stream-multiplexer-mplex#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/libp2p/js-libp2p-mplex.git"
"url": "git+https://github.com/libp2p/js-libp2p.git"
},
"bugs": {
"url": "https://github.com/libp2p/js-libp2p-mplex/issues"
"url": "https://github.com/libp2p/js-libp2p/issues"
},

@@ -24,6 +24,2 @@ "keywords": [

],
"engines": {
"node": ">=16.0.0",
"npm": ">=7.0.0"
},
"type": "module",

@@ -49,87 +45,2 @@ "types": "./dist/src/index.d.ts",

},
"release": {
"branches": [
"master"
],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"preset": "conventionalcommits",
"releaseRules": [
{
"breaking": true,
"release": "major"
},
{
"revert": true,
"release": "patch"
},
{
"type": "feat",
"release": "minor"
},
{
"type": "fix",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
},
{
"type": "test",
"release": "patch"
},
{
"type": "deps",
"release": "patch"
},
{
"scope": "no-release",
"release": false
}
]
}
],
[
"@semantic-release/release-notes-generator",
{
"preset": "conventionalcommits",
"presetConfig": {
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Trivial Changes"
},
{
"type": "docs",
"section": "Documentation"
},
{
"type": "deps",
"section": "Dependencies"
},
{
"type": "test",
"section": "Tests"
}
]
}
}
],
"@semantic-release/changelog",
"@semantic-release/npm",
"@semantic-release/github",
"@semantic-release/git"
]
},
"scripts": {

@@ -147,26 +58,20 @@ "clean": "aegir clean",

"test:node": "aegir test -t node --cov",
"test:electron-main": "aegir test -t electron-main",
"release": "aegir release",
"docs": "aegir docs"
"test:electron-main": "aegir test -t electron-main"
},
"dependencies": {
"@libp2p/interface-connection": "^5.0.0",
"@libp2p/interface-stream-muxer": "^4.1.2",
"@libp2p/interfaces": "^3.2.0",
"@libp2p/logger": "^2.0.0",
"abortable-iterator": "^5.0.0",
"any-signal": "^4.0.1",
"@libp2p/interface": "^0.1.2",
"@libp2p/logger": "^3.0.2",
"abortable-iterator": "^5.0.1",
"benchmark": "^2.1.4",
"it-batched-bytes": "^2.0.2",
"it-pushable": "^3.1.0",
"it-pushable": "^3.2.0",
"it-stream-types": "^2.0.1",
"rate-limiter-flexible": "^2.3.9",
"uint8arraylist": "^2.1.1",
"uint8arrays": "^4.0.2",
"varint": "^6.0.0"
"rate-limiter-flexible": "^2.3.11",
"uint8-varint": "^2.0.0",
"uint8arraylist": "^2.4.3",
"uint8arrays": "^4.0.6"
},
"devDependencies": {
"@libp2p/interface-stream-muxer-compliance-tests": "^7.0.3",
"@types/varint": "^6.0.0",
"aegir": "^39.0.7",
"@libp2p/interface-compliance-tests": "^4.0.4",
"aegir": "^40.0.8",
"cborg": "^2.0.1",

@@ -176,5 +81,5 @@ "delay": "^6.0.0",

"it-all": "^3.0.1",
"it-drain": "^3.0.1",
"it-drain": "^3.0.2",
"it-foreach": "^2.0.2",
"it-map": "^3.0.1",
"it-map": "^3.0.3",
"it-pipe": "^3.0.1",

@@ -181,0 +86,0 @@ "it-to-buffer": "^4.0.1",

@@ -5,4 +5,4 @@ # @libp2p/mplex <!-- omit in toc -->

[![Discuss](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg?style=flat-square)](https://discuss.libp2p.io)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p-mplex.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p-mplex)
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p-mplex/js-test-and-release.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p-mplex/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)
[![codecov](https://img.shields.io/codecov/c/github/libp2p/js-libp2p.svg?style=flat-square)](https://codecov.io/gh/libp2p/js-libp2p)
[![CI](https://img.shields.io/github/actions/workflow/status/libp2p/js-libp2p/main.yml?branch=master\&style=flat-square)](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amaster)

@@ -63,3 +63,3 @@ > JavaScript implementation of <https://github.com/libp2p/mplex>

- <https://libp2p.github.io/js-libp2p-mplex>
- <https://libp2p.github.io/js-libp2p/modules/_libp2p_mplex.html>

@@ -66,0 +66,0 @@ ## License

import batchedBytes from 'it-batched-bytes'
import * as varint from 'uint8-varint'
import { Uint8ArrayList } from 'uint8arraylist'
import varint from 'varint'
import { allocUnsafe } from './alloc-unsafe.js'

@@ -27,12 +27,12 @@ import { type Message, MessageTypes } from './message-types.js'

varint.encode(msg.id << 3 | msg.type, pool, offset)
offset += varint.encode.bytes ?? 0
offset += varint.encodingLength(msg.id << 3 | msg.type)
if ((msg.type === MessageTypes.NEW_STREAM || msg.type === MessageTypes.MESSAGE_INITIATOR || msg.type === MessageTypes.MESSAGE_RECEIVER) && msg.data != null) {
varint.encode(msg.data.length, pool, offset)
offset += varint.encodingLength(msg.data.length)
} else {
varint.encode(0, pool, offset)
offset += varint.encodingLength(0)
}
offset += varint.encode.bytes ?? 0
const header = pool.subarray(this._poolOffset, offset)

@@ -39,0 +39,0 @@

import { MplexStreamMuxer } from './mplex.js'
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface/stream-muxer'

@@ -4,0 +4,0 @@ export interface MplexInit {

@@ -1,6 +0,6 @@

import { CodeError } from '@libp2p/interfaces/errors'
import { CodeError } from '@libp2p/interface/errors'
import { logger } from '@libp2p/logger'
import { abortableSource } from 'abortable-iterator'
import { anySignal } from 'any-signal'
import { pushableV } from 'it-pushable'
import { pipe } from 'it-pipe'
import { type PushableV, pushableV } from 'it-pushable'
import { RateLimiterMemory } from 'rate-limiter-flexible'

@@ -11,6 +11,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays'

import { MessageTypes, MessageTypeNames, type Message } from './message-types.js'
import { createStream } from './stream.js'
import { createStream, type MplexStream } from './stream.js'
import type { MplexInit } from './index.js'
import type { Stream } from '@libp2p/interface-connection'
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface-stream-muxer'
import type { AbortOptions } from '@libp2p/interface'
import type { Stream } from '@libp2p/interface/connection'
import type { StreamMuxer, StreamMuxerInit } from '@libp2p/interface/stream-muxer'
import type { Sink, Source } from 'it-stream-types'

@@ -25,2 +26,3 @@ import type { Uint8ArrayList } from 'uint8arraylist'

const DISCONNECT_THRESHOLD = 5
const CLOSE_TIMEOUT = 500

@@ -44,9 +46,9 @@ function printMessage (msg: Message): any {

export interface MplexStream extends Stream {
sourceReadableLength: () => number
sourcePush: (data: Uint8ArrayList) => void
interface MplexStreamMuxerInit extends MplexInit, StreamMuxerInit {
/**
* The default timeout to use in ms when shutting down the muxer.
*/
closeTimeout?: number
}
interface MplexStreamMuxerInit extends MplexInit, StreamMuxerInit {}
export class MplexStreamMuxer implements StreamMuxer {

@@ -61,5 +63,6 @@ public protocol = '/mplex/6.7.0'

private readonly _init: MplexStreamMuxerInit
private readonly _source: { push: (val: Message) => void, end: (err?: Error) => void }
private readonly _source: PushableV<Message>
private readonly closeController: AbortController
private readonly rateLimiter: RateLimiterMemory
private readonly closeTimeout: number

@@ -81,2 +84,3 @@ constructor (init?: MplexStreamMuxerInit) {

this._init = init
this.closeTimeout = init.closeTimeout ?? CLOSE_TIMEOUT

@@ -91,6 +95,21 @@ /**

*/
const source = this._createSource()
this._source = source
this.source = source
this._source = pushableV<Message>({
objectMode: true,
onEnd: (): void => {
// the source has ended, we can't write any more messages to gracefully
// close streams so all we can do is destroy them
for (const stream of this._streams.initiators.values()) {
stream.destroy()
}
for (const stream of this._streams.receivers.values()) {
stream.destroy()
}
}
})
this.source = pipe(
this._source,
source => encode(source, this._init.minSendBytes)
)
/**

@@ -140,13 +159,39 @@ * Close controller

*/
close (err?: Error | undefined): void {
if (this.closeController.signal.aborted) return
async close (options?: AbortOptions): Promise<void> {
if (this.closeController.signal.aborted) {
return
}
if (err != null) {
this.streams.forEach(s => { s.abort(err) })
} else {
this.streams.forEach(s => { s.close() })
const signal = options?.signal ?? AbortSignal.timeout(this.closeTimeout)
try {
// try to gracefully close all streams
await Promise.all(
this.streams.map(async s => s.close({
signal
}))
)
this._source.end()
// try to gracefully close the muxer
await this._source.onEmpty({
signal
})
this.closeController.abort()
} catch (err: any) {
this.abort(err)
}
this.closeController.abort()
}
abort (err: Error): void {
if (this.closeController.signal.aborted) {
return
}
this.streams.forEach(s => { s.abort(err) })
this.closeController.abort(err)
}
/**

@@ -174,3 +219,3 @@ * Called whenever an inbound stream is created

const send = (msg: Message): void => {
const send = async (msg: Message): Promise<void> => {
if (log.enabled) {

@@ -184,3 +229,3 @@ log.trace('%s stream %s send', type, id, printMessage(msg))

const onEnd = (): void => {
log('%s stream with id %s and protocol %s ended', type, id, stream.stat.protocol)
log('%s stream with id %s and protocol %s ended', type, id, stream.protocol)
registry.delete(id)

@@ -204,6 +249,6 @@

const sink: Sink<Source<Uint8ArrayList | Uint8Array>, Promise<void>> = async source => {
const signal = anySignal([this.closeController.signal, this._init.signal])
try {
source = abortableSource(source, signal)
source = abortableSource(source, this.closeController.signal, {
returnOnAbort: true
})

@@ -222,4 +267,2 @@ const decoder = new Decoder(this._init.maxMsgSize, this._init.maxUnprocessedMessageQueueSize)

this._source.end(err) // End the source with an error
} finally {
signal.clear()
}

@@ -231,22 +274,2 @@ }

/**
* Creates a source that restricts outgoing message sizes
* and varint encodes them
*/
_createSource (): any {
const onEnd = (err?: Error): void => {
this.close(err)
}
const source = pushableV<Message>({
objectMode: true,
onEnd
})
return Object.assign(encode(source, this._init.minSendBytes), {
push: source.push,
end: source.end,
return: source.return
})
}
async _handleIncoming (message: Message): Promise<void> {

@@ -279,3 +302,3 @@ const { id, type } = message

// since there's no backpressure in mplex, the only thing we can really do to protect ourselves is close the connection
this._source.end(new Error('Too many open streams'))
this.abort(new Error('Too many open streams'))
return

@@ -302,2 +325,14 @@ }

// if the remote keeps sending us messages for streams that have been
// closed or were never opened they may be attacking us so if they do
// this very quickly all we can do is close the connection
try {
await this.rateLimiter.consume('missing-stream', 1)
} catch {
log('rate limit hit when receiving messages for streams that do not exist - closing remote connection')
// since there's no backpressure in mplex, the only thing we can really do to protect ourselves is close the connection
this.abort(new Error('Too many messages for missing streams'))
return
}
return

@@ -308,36 +343,38 @@ }

switch (type) {
case MessageTypes.MESSAGE_INITIATOR:
case MessageTypes.MESSAGE_RECEIVER:
if (stream.sourceReadableLength() > maxBufferSize) {
// Stream buffer has got too large, reset the stream
this._source.push({
id: message.id,
type: type === MessageTypes.MESSAGE_INITIATOR ? MessageTypes.RESET_RECEIVER : MessageTypes.RESET_INITIATOR
})
try {
switch (type) {
case MessageTypes.MESSAGE_INITIATOR:
case MessageTypes.MESSAGE_RECEIVER:
if (stream.sourceReadableLength() > maxBufferSize) {
// Stream buffer has got too large, reset the stream
this._source.push({
id: message.id,
type: type === MessageTypes.MESSAGE_INITIATOR ? MessageTypes.RESET_RECEIVER : MessageTypes.RESET_INITIATOR
})
// Inform the stream consumer they are not fast enough
const error = new CodeError('Input buffer full - increase Mplex maxBufferSize to accommodate slow consumers', 'ERR_STREAM_INPUT_BUFFER_FULL')
stream.abort(error)
// Inform the stream consumer they are not fast enough
throw new CodeError('Input buffer full - increase Mplex maxBufferSize to accommodate slow consumers', 'ERR_STREAM_INPUT_BUFFER_FULL')
}
return
}
// We got data from the remote, push it into our local stream
stream.sourcePush(message.data)
break
case MessageTypes.CLOSE_INITIATOR:
case MessageTypes.CLOSE_RECEIVER:
// We should expect no more data from the remote, stop reading
stream.closeRead()
break
case MessageTypes.RESET_INITIATOR:
case MessageTypes.RESET_RECEIVER:
// Stop reading and writing to the stream immediately
stream.reset()
break
default:
log('unknown message type %s', type)
// We got data from the remote, push it into our local stream
stream.sourcePush(message.data)
break
case MessageTypes.CLOSE_INITIATOR:
case MessageTypes.CLOSE_RECEIVER:
// The remote has stopped writing, so we can stop reading
stream.remoteCloseWrite()
break
case MessageTypes.RESET_INITIATOR:
case MessageTypes.RESET_RECEIVER:
// The remote has errored, stop reading and writing to the stream immediately
stream.reset()
break
default:
log('unknown message type %s', type)
}
} catch (err: any) {
log.error('error while processing message', err)
stream.abort(err)
}
}
}

@@ -1,2 +0,3 @@

import { AbstractStream, type AbstractStreamInit } from '@libp2p/interface-stream-muxer/stream'
import { AbstractStream, type AbstractStreamInit } from '@libp2p/interface/stream-muxer/stream'
import { logger } from '@libp2p/logger'
import { Uint8ArrayList } from 'uint8arraylist'

@@ -10,3 +11,3 @@ import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'

id: number
send: (msg: Message) => void
send: (msg: Message) => Promise<void>
name?: string

@@ -21,10 +22,17 @@ onEnd?: (err?: Error) => void

name: string
send: (msg: Message) => void
send: (msg: Message) => Promise<void>
/**
* The maximum allowable data size, any data larger than this will be
* chunked and sent in multiple data messages
*/
maxDataSize: number
}
class MplexStream extends AbstractStream {
export class MplexStream extends AbstractStream {
private readonly name: string
private readonly streamId: number
private readonly send: (msg: Message) => void
private readonly send: (msg: Message) => Promise<void>
private readonly types: Record<string, number>
private readonly maxDataSize: number

@@ -38,21 +46,33 @@ constructor (init: MplexStreamInit) {

this.streamId = init.streamId
this.maxDataSize = init.maxDataSize
}
sendNewStream (): void {
this.send({ id: this.streamId, type: InitiatorMessageTypes.NEW_STREAM, data: new Uint8ArrayList(uint8ArrayFromString(this.name)) })
async sendNewStream (): Promise<void> {
await this.send({ id: this.streamId, type: InitiatorMessageTypes.NEW_STREAM, data: new Uint8ArrayList(uint8ArrayFromString(this.name)) })
}
sendData (data: Uint8ArrayList): void {
this.send({ id: this.streamId, type: this.types.MESSAGE, data })
async sendData (data: Uint8ArrayList): Promise<void> {
data = data.sublist()
while (data.byteLength > 0) {
const toSend = Math.min(data.byteLength, this.maxDataSize)
await this.send({
id: this.streamId,
type: this.types.MESSAGE,
data: data.sublist(0, toSend)
})
data.consume(toSend)
}
}
sendReset (): void {
this.send({ id: this.streamId, type: this.types.RESET })
async sendReset (): Promise<void> {
await this.send({ id: this.streamId, type: this.types.RESET })
}
sendCloseWrite (): void {
this.send({ id: this.streamId, type: this.types.CLOSE })
async sendCloseWrite (): Promise<void> {
await this.send({ id: this.streamId, type: this.types.CLOSE })
}
sendCloseRead (): void {
async sendCloseRead (): Promise<void> {
// mplex does not support close read, only close write

@@ -72,4 +92,5 @@ }

onEnd,
send
send,
log: logger(`libp2p:mplex:stream:${type}:${id}`)
})
}

Sorry, the diff of this file is too big to display