@libp2p/mplex 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +4 -0
- package/README.md +186 -0
- package/dist/src/decode.d.ts +7 -0
- package/dist/src/decode.d.ts.map +1 -0
- package/dist/src/decode.js +93 -0
- package/dist/src/decode.js.map +1 -0
- package/dist/src/encode.d.ts +7 -0
- package/dist/src/encode.d.ts.map +1 -0
- package/dist/src/encode.js +65 -0
- package/dist/src/encode.js.map +1 -0
- package/dist/src/index.d.ts +66 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +218 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/message-types.d.ts +51 -0
- package/dist/src/message-types.d.ts.map +1 -0
- package/dist/src/message-types.js +31 -0
- package/dist/src/message-types.js.map +1 -0
- package/dist/src/restrict-size.d.ts +9 -0
- package/dist/src/restrict-size.d.ts.map +1 -0
- package/dist/src/restrict-size.js +32 -0
- package/dist/src/restrict-size.js.map +1 -0
- package/dist/src/stream.d.ts +12 -0
- package/dist/src/stream.d.ts.map +1 -0
- package/dist/src/stream.js +146 -0
- package/dist/src/stream.js.map +1 -0
- package/package.json +169 -0
- package/src/decode.ts +132 -0
- package/src/encode.ts +79 -0
- package/src/index.ts +281 -0
- package/src/message-types.ts +79 -0
- package/src/restrict-size.ts +36 -0
- package/src/stream.ts +182 -0
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAY,SAAS,EAAE,MAAM,aAAa,CAAA;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,YAAY,EAA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AAY,CAAC,eAAe,CAAC;YAClC,KAAK,YAAY,CAAC,cAAc;gBAC9B,MAAM,CAAC,KAAK,EAAE,CAAA;gBACd,MAAK;YACP;gBACE,GAAG,CAAC,yBAAyB,EAAE,IAAI,CAAC,CAAA;SACvC;IACH,CAAC;;AAtOM,gBAAU,GAAG,cAAc,CAAA"}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
2
|
+
declare type INITIATOR_NAME = 'NEW_STREAM' | 'MESSAGE' | 'CLOSE' | 'RESET';
|
3
|
+
declare type RECEIVER_NAME = 'MESSAGE' | 'CLOSE' | 'RESET';
|
4
|
+
declare type NAME = 'NEW_STREAM' | 'MESSAGE_INITIATOR' | 'CLOSE_INITIATOR' | 'RESET_INITIATOR' | 'MESSAGE_RECEIVER' | 'CLOSE_RECEIVER' | 'RESET_RECEIVER';
|
5
|
+
declare type CODE = 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
6
|
+
export declare enum MessageTypes {
|
7
|
+
NEW_STREAM = 0,
|
8
|
+
MESSAGE_RECEIVER = 1,
|
9
|
+
MESSAGE_INITIATOR = 2,
|
10
|
+
CLOSE_RECEIVER = 3,
|
11
|
+
CLOSE_INITIATOR = 4,
|
12
|
+
RESET_RECEIVER = 5,
|
13
|
+
RESET_INITIATOR = 6
|
14
|
+
}
|
15
|
+
export declare const MessageTypeNames: Record<CODE, NAME>;
|
16
|
+
export declare const InitiatorMessageTypes: Record<INITIATOR_NAME, CODE>;
|
17
|
+
export declare const ReceiverMessageTypes: Record<RECEIVER_NAME, CODE>;
|
18
|
+
export interface NewStreamMessage {
|
19
|
+
id: number;
|
20
|
+
type: MessageTypes.NEW_STREAM;
|
21
|
+
data: Uint8Array | Uint8ArrayList;
|
22
|
+
}
|
23
|
+
export interface MessageReceiverMessage {
|
24
|
+
id: number;
|
25
|
+
type: MessageTypes.MESSAGE_RECEIVER;
|
26
|
+
data: Uint8Array | Uint8ArrayList;
|
27
|
+
}
|
28
|
+
export interface MessageInitiatorMessage {
|
29
|
+
id: number;
|
30
|
+
type: MessageTypes.MESSAGE_INITIATOR;
|
31
|
+
data: Uint8Array | Uint8ArrayList;
|
32
|
+
}
|
33
|
+
export interface CloseReceiverMessage {
|
34
|
+
id: number;
|
35
|
+
type: MessageTypes.CLOSE_RECEIVER;
|
36
|
+
}
|
37
|
+
export interface CloseInitiatorMessage {
|
38
|
+
id: number;
|
39
|
+
type: MessageTypes.CLOSE_INITIATOR;
|
40
|
+
}
|
41
|
+
export interface ResetReceiverMessage {
|
42
|
+
id: number;
|
43
|
+
type: MessageTypes.RESET_RECEIVER;
|
44
|
+
}
|
45
|
+
export interface ResetInitiatorMessage {
|
46
|
+
id: number;
|
47
|
+
type: MessageTypes.RESET_INITIATOR;
|
48
|
+
}
|
49
|
+
export declare type Message = NewStreamMessage | MessageReceiverMessage | MessageInitiatorMessage | CloseReceiverMessage | CloseInitiatorMessage | ResetReceiverMessage | ResetInitiatorMessage;
|
50
|
+
export {};
|
51
|
+
//# sourceMappingURL=message-types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"message-types.d.ts","sourceRoot":"","sources":["../../src/message-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD,aAAK,cAAc,GAAG,YAAY,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,CAAA;AAClE,aAAK,aAAa,GAAG,SAAS,GAAG,OAAO,GAAG,OAAO,CAAA;AAClD,aAAK,IAAI,GAAG,YAAY,GAAG,mBAAmB,GAAG,iBAAiB,GAAG,iBAAiB,GAAG,kBAAkB,GAAG,gBAAgB,GAAG,gBAAgB,CAAA;AACjJ,aAAK,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AAErC,oBAAY,YAAY;IACtB,UAAU,IAAI;IACd,gBAAg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GAAG,uBAAuB,GAAG,oBAAoB,GAAG,qBAAqB,GAAG,oBAAoB,GAAG,qBAAqB,CAAA"}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
export var MessageTypes;
|
2
|
+
(function (MessageTypes) {
|
3
|
+
MessageTypes[MessageTypes["NEW_STREAM"] = 0] = "NEW_STREAM";
|
4
|
+
MessageTypes[MessageTypes["MESSAGE_RECEIVER"] = 1] = "MESSAGE_RECEIVER";
|
5
|
+
MessageTypes[MessageTypes["MESSAGE_INITIATOR"] = 2] = "MESSAGE_INITIATOR";
|
6
|
+
MessageTypes[MessageTypes["CLOSE_RECEIVER"] = 3] = "CLOSE_RECEIVER";
|
7
|
+
MessageTypes[MessageTypes["CLOSE_INITIATOR"] = 4] = "CLOSE_INITIATOR";
|
8
|
+
MessageTypes[MessageTypes["RESET_RECEIVER"] = 5] = "RESET_RECEIVER";
|
9
|
+
MessageTypes[MessageTypes["RESET_INITIATOR"] = 6] = "RESET_INITIATOR";
|
10
|
+
})(MessageTypes || (MessageTypes = {}));
|
11
|
+
export const MessageTypeNames = Object.freeze({
|
12
|
+
0: 'NEW_STREAM',
|
13
|
+
1: 'MESSAGE_RECEIVER',
|
14
|
+
2: 'MESSAGE_INITIATOR',
|
15
|
+
3: 'CLOSE_RECEIVER',
|
16
|
+
4: 'CLOSE_INITIATOR',
|
17
|
+
5: 'RESET_RECEIVER',
|
18
|
+
6: 'RESET_INITIATOR'
|
19
|
+
});
|
20
|
+
export const InitiatorMessageTypes = Object.freeze({
|
21
|
+
NEW_STREAM: MessageTypes.NEW_STREAM,
|
22
|
+
MESSAGE: MessageTypes.MESSAGE_INITIATOR,
|
23
|
+
CLOSE: MessageTypes.CLOSE_INITIATOR,
|
24
|
+
RESET: MessageTypes.RESET_INITIATOR
|
25
|
+
});
|
26
|
+
export const ReceiverMessageTypes = Object.freeze({
|
27
|
+
MESSAGE: MessageTypes.MESSAGE_RECEIVER,
|
28
|
+
CLOSE: MessageTypes.CLOSE_RECEIVER,
|
29
|
+
RESET: MessageTypes.RESET_RECEIVER
|
30
|
+
});
|
31
|
+
//# sourceMappingURL=message-types.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"message-types.js","sourceRoot":"","sources":["../../src/message-types.ts"],"names":[],"mappings":"AAOA,MAAM,CAAN,IAAY,YAQX;AARD,WAAY,YAAY;IACtB,2DAAc,CAAA;IACd,uEAAoB,CAAA;IACpB,yEAAqB,CAAA;IACrB,mEAAkB,CAAA;IAClB,qEAAmB,CAAA;IACnB,mEAAkB,CAAA;IAClB,qEAAmB,CAAA;AACrB,CAAC,EARW,YAAY,KAAZ,YAAY,QAQvB;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAuB,MAAM,CAAC,MAAM,CAAC;IAChE,CAAC,EAAE,YAAY;IACf,CAAC,EAAE,kBAAkB;IACrB,CAAC,EAAE,mBAAmB;IACtB,CAAC,EAAE,gBAAgB;IACnB,CAAC,EAAE,iBAAiB;IACpB,CAAC,EAAE,gBAAgB;IACnB,CAAC,EAAE,iBAAiB;CACrB,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAiC,MAAM,CAAC,MAAM,CAAC;IAC/E,UAAU,EAAE,YAAY,CAAC,UAAU;IACnC,OAAO,EAAE,YAAY,CAAC,iBAAiB;IACvC,KAAK,EAAE,YAAY,CAAC,eAAe;IACnC,KAAK,EAAE,YAAY,CAAC,eAAe;CACpC,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAgC,MAAM,CAAC,MAAM,CAAC;IAC7E,OAAO,EAAE,YAAY,CAAC,gBAAgB;IACtC,KAAK,EAAE,YAAY,CAAC,cAAc;IAClC,KAAK,EAAE,YAAY,CAAC,cAAc;CACnC,CAAC,CAAA"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { Message } from './message-types.js';
|
2
|
+
import type { Transform } from 'it-stream-types';
|
3
|
+
export declare const MAX_MSG_SIZE: number;
|
4
|
+
/**
|
5
|
+
* Creates an iterable transform that restricts message sizes to
|
6
|
+
* the given maximum size.
|
7
|
+
*/
|
8
|
+
export declare function restrictSize(max?: number): Transform<Message | Message[], Message>;
|
9
|
+
//# sourceMappingURL=restrict-size.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"restrict-size.d.ts","sourceRoot":"","sources":["../../src/restrict-size.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAgB,MAAM,oBAAoB,CAAA;AAC1D,OAAO,KAAK,EAAU,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAExD,eAAO,MAAM,YAAY,QAAU,CAAA;AAEnC;;;GAGG;AACH,wBAAgB,YAAY,CAAE,GAAG,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,OAAO,GAAG,OAAO,EAAE,EAAE,OAAO,CAAC,CA0BnF"}
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { MessageTypes } from './message-types.js';
|
2
|
+
export const MAX_MSG_SIZE = 1 << 20; // 1MB
|
3
|
+
/**
|
4
|
+
* Creates an iterable transform that restricts message sizes to
|
5
|
+
* the given maximum size.
|
6
|
+
*/
|
7
|
+
export function restrictSize(max) {
|
8
|
+
const maxSize = max ?? MAX_MSG_SIZE;
|
9
|
+
const checkSize = (msg) => {
|
10
|
+
if (msg.type !== MessageTypes.NEW_STREAM && msg.type !== MessageTypes.MESSAGE_INITIATOR && msg.type !== MessageTypes.MESSAGE_RECEIVER) {
|
11
|
+
return;
|
12
|
+
}
|
13
|
+
if (msg.data.byteLength > maxSize) {
|
14
|
+
throw Object.assign(new Error('message size too large!'), { code: 'ERR_MSG_TOO_BIG' });
|
15
|
+
}
|
16
|
+
};
|
17
|
+
return (source) => {
|
18
|
+
return (async function* restrictSize() {
|
19
|
+
for await (const msg of source) {
|
20
|
+
if (Array.isArray(msg)) {
|
21
|
+
msg.forEach(checkSize);
|
22
|
+
yield* msg;
|
23
|
+
}
|
24
|
+
else {
|
25
|
+
checkSize(msg);
|
26
|
+
yield msg;
|
27
|
+
}
|
28
|
+
}
|
29
|
+
})();
|
30
|
+
};
|
31
|
+
}
|
32
|
+
//# sourceMappingURL=restrict-size.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"restrict-size.js","sourceRoot":"","sources":["../../src/restrict-size.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAG1D,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,CAAA,CAAC,MAAM;AAE1C;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAE,GAAY;IACxC,MAAM,OAAO,GAAG,GAAG,IAAI,YAAY,CAAA;IAEnC,MAAM,SAAS,GAAG,CAAC,GAAY,EAAE,EAAE;QACjC,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,CAAC,UAAU,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,CAAC,iBAAiB,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,CAAC,gBAAgB,EAAE;YACrI,OAAM;SACP;QAED,IAAI,GAAG,CAAC,IAAI,CAAC,UAAU,GAAG,OAAO,EAAE;YACjC,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,yBAAyB,CAAC,EAAE,EAAE,IAAI,EAAE,iBAAiB,EAAE,CAAC,CAAA;SACvF;IACH,CAAC,CAAA;IAED,OAAO,CAAC,MAAmC,EAAE,EAAE;QAC7C,OAAO,CAAC,KAAK,SAAU,CAAC,CAAC,YAAY;YACnC,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,MAAM,EAAE;gBAC9B,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;oBACtB,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAA;oBACtB,KAAM,CAAC,CAAC,GAAG,CAAA;iBACZ;qBAAM;oBACL,SAAS,CAAC,GAAG,CAAC,CAAA;oBACd,MAAM,GAAG,CAAA;iBACV;aACF;QACH,CAAC,CAAC,EAAE,CAAA;IACN,CAAC,CAAA;AACH,CAAC"}
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { Message } from './message-types.js';
|
2
|
+
import type { MplexStream } from './index.js';
|
3
|
+
export interface Options {
|
4
|
+
id: number;
|
5
|
+
send: (msg: Message) => void;
|
6
|
+
name?: string;
|
7
|
+
onEnd?: (err?: Error) => void;
|
8
|
+
type?: 'initiator' | 'receiver';
|
9
|
+
maxMsgSize?: number;
|
10
|
+
}
|
11
|
+
export declare function createStream(options: Options): MplexStream;
|
12
|
+
//# sourceMappingURL=stream.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"stream.d.ts","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAGjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAO7C,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,wBAAgB,YAAY,CAAE,OAAO,EAAE,OAAO,GAAG,WAAW,CAyJ3D"}
|
@@ -0,0 +1,146 @@
|
|
1
|
+
import { abortableSource } from 'abortable-iterator';
|
2
|
+
import { pushable } from 'it-pushable';
|
3
|
+
import errCode from 'err-code';
|
4
|
+
import { MAX_MSG_SIZE } from './restrict-size.js';
|
5
|
+
import { anySignal } from 'any-signal';
|
6
|
+
import { InitiatorMessageTypes, ReceiverMessageTypes } from './message-types.js';
|
7
|
+
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
8
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
9
|
+
import { logger } from '@libp2p/logger';
|
10
|
+
const log = logger('libp2p:mplex:stream');
|
11
|
+
const ERR_MPLEX_STREAM_RESET = 'ERR_MPLEX_STREAM_RESET';
|
12
|
+
const ERR_MPLEX_STREAM_ABORT = 'ERR_MPLEX_STREAM_ABORT';
|
13
|
+
export function createStream(options) {
|
14
|
+
const { id, name, send, onEnd, type = 'initiator', maxMsgSize = MAX_MSG_SIZE } = options;
|
15
|
+
const abortController = new AbortController();
|
16
|
+
const resetController = new AbortController();
|
17
|
+
const Types = type === 'initiator' ? InitiatorMessageTypes : ReceiverMessageTypes;
|
18
|
+
const externalId = type === 'initiator' ? (`i${id}`) : `r${id}`;
|
19
|
+
const streamName = `${name == null ? id : name}`;
|
20
|
+
let sourceEnded = false;
|
21
|
+
let sinkEnded = false;
|
22
|
+
let endErr;
|
23
|
+
const timeline = {
|
24
|
+
open: Date.now()
|
25
|
+
};
|
26
|
+
const onSourceEnd = (err) => {
|
27
|
+
if (sourceEnded) {
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
sourceEnded = true;
|
31
|
+
log('%s stream %s source end', type, streamName, err);
|
32
|
+
if (err != null && endErr == null) {
|
33
|
+
endErr = err;
|
34
|
+
}
|
35
|
+
if (sinkEnded) {
|
36
|
+
stream.timeline.close = Date.now();
|
37
|
+
if (onEnd != null) {
|
38
|
+
onEnd(endErr);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
};
|
42
|
+
const onSinkEnd = (err) => {
|
43
|
+
if (sinkEnded) {
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
sinkEnded = true;
|
47
|
+
log('%s stream %s sink end - err: %o', type, streamName, err);
|
48
|
+
if (err != null && endErr == null) {
|
49
|
+
endErr = err;
|
50
|
+
}
|
51
|
+
if (sourceEnded) {
|
52
|
+
timeline.close = Date.now();
|
53
|
+
if (onEnd != null) {
|
54
|
+
onEnd(endErr);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
};
|
58
|
+
const stream = {
|
59
|
+
// Close for reading
|
60
|
+
close: () => {
|
61
|
+
stream.source.end();
|
62
|
+
},
|
63
|
+
// Close for reading and writing (local error)
|
64
|
+
abort: (err) => {
|
65
|
+
log('%s stream %s abort', type, streamName, err);
|
66
|
+
// End the source with the passed error
|
67
|
+
stream.source.end(err);
|
68
|
+
abortController.abort();
|
69
|
+
onSinkEnd(err);
|
70
|
+
},
|
71
|
+
// Close immediately for reading and writing (remote error)
|
72
|
+
reset: () => {
|
73
|
+
const err = errCode(new Error('stream reset'), ERR_MPLEX_STREAM_RESET);
|
74
|
+
resetController.abort();
|
75
|
+
stream.source.end(err);
|
76
|
+
onSinkEnd(err);
|
77
|
+
},
|
78
|
+
sink: async (source) => {
|
79
|
+
source = abortableSource(source, anySignal([
|
80
|
+
abortController.signal,
|
81
|
+
resetController.signal
|
82
|
+
]));
|
83
|
+
try {
|
84
|
+
if (type === 'initiator') { // If initiator, open a new stream
|
85
|
+
send({ id, type: InitiatorMessageTypes.NEW_STREAM, data: uint8ArrayFromString(streamName) });
|
86
|
+
}
|
87
|
+
const uint8ArrayList = new Uint8ArrayList();
|
88
|
+
for await (const data of source) {
|
89
|
+
uint8ArrayList.append(data);
|
90
|
+
while (uint8ArrayList.length !== 0) {
|
91
|
+
if (uint8ArrayList.length <= maxMsgSize) {
|
92
|
+
send({ id, type: Types.MESSAGE, data: uint8ArrayList.subarray() });
|
93
|
+
uint8ArrayList.consume(uint8ArrayList.length);
|
94
|
+
break;
|
95
|
+
}
|
96
|
+
const toSend = uint8ArrayList.length - maxMsgSize;
|
97
|
+
send({ id, type: Types.MESSAGE, data: uint8ArrayList.subarray(0, toSend) });
|
98
|
+
uint8ArrayList.consume(toSend);
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
catch (err) {
|
103
|
+
if (err.type === 'aborted' && err.message === 'The operation was aborted') {
|
104
|
+
if (resetController.signal.aborted) {
|
105
|
+
err.message = 'stream reset';
|
106
|
+
err.code = ERR_MPLEX_STREAM_RESET;
|
107
|
+
}
|
108
|
+
if (abortController.signal.aborted) {
|
109
|
+
err.message = 'stream aborted';
|
110
|
+
err.code = ERR_MPLEX_STREAM_ABORT;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
// Send no more data if this stream was remotely reset
|
114
|
+
if (err.code === ERR_MPLEX_STREAM_RESET) {
|
115
|
+
log('%s stream %s reset', type, name);
|
116
|
+
}
|
117
|
+
else {
|
118
|
+
log('%s stream %s error', type, name, err);
|
119
|
+
try {
|
120
|
+
send({ id, type: Types.RESET });
|
121
|
+
}
|
122
|
+
catch (err) {
|
123
|
+
log('%s stream %s error sending reset', type, name, err);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
stream.source.end(err);
|
127
|
+
onSinkEnd(err);
|
128
|
+
return;
|
129
|
+
}
|
130
|
+
try {
|
131
|
+
send({ id, type: Types.CLOSE });
|
132
|
+
}
|
133
|
+
catch (err) {
|
134
|
+
log('%s stream %s error sending close', type, name, err);
|
135
|
+
}
|
136
|
+
onSinkEnd();
|
137
|
+
},
|
138
|
+
source: pushable({
|
139
|
+
onEnd: onSourceEnd
|
140
|
+
}),
|
141
|
+
timeline,
|
142
|
+
id: externalId
|
143
|
+
};
|
144
|
+
return stream;
|
145
|
+
}
|
146
|
+
//# sourceMappingURL=stream.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"stream.js","sourceRoot":"","sources":["../../src/stream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AACtC,OAAO,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,oBAA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AK,EAAE,CAAC,CAAA;aAChC;YAAC,OAAO,GAAG,EAAE;gBACZ,GAAG,CAAC,kCAAkC,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,CAAC,CAAA;aACzD;YAED,SAAS,EAAE,CAAA;QACb,CAAC;QACD,MAAM,EAAE,QAAQ,CAAa;YAC3B,KAAK,EAAE,WAAW;SACnB,CAAC;QACF,QAAQ;QACR,EAAE,EAAE,UAAU;KACf,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/package.json
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
{
|
2
|
+
"name": "@libp2p/mplex",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "JavaScript implementation of https://github.com/libp2p/mplex",
|
5
|
+
"license": "Apache-2.0 OR MIT",
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p-mplex#readme",
|
7
|
+
"repository": {
|
8
|
+
"type": "git",
|
9
|
+
"url": "git+https://github.com/libp2p/js-libp2p-mplex.git"
|
10
|
+
},
|
11
|
+
"bugs": {
|
12
|
+
"url": "https://github.com/libp2p/js-libp2p-mplex/issues"
|
13
|
+
},
|
14
|
+
"keywords": [
|
15
|
+
"IPFS",
|
16
|
+
"connection",
|
17
|
+
"duplex",
|
18
|
+
"libp2p",
|
19
|
+
"mplex",
|
20
|
+
"multiplex",
|
21
|
+
"muxer",
|
22
|
+
"stream"
|
23
|
+
],
|
24
|
+
"engines": {
|
25
|
+
"node": ">=16.0.0",
|
26
|
+
"npm": ">=7.0.0"
|
27
|
+
},
|
28
|
+
"type": "module",
|
29
|
+
"types": "./dist/src/index.d.ts",
|
30
|
+
"files": [
|
31
|
+
"src",
|
32
|
+
"dist/src",
|
33
|
+
"!dist/test",
|
34
|
+
"!**/*.tsbuildinfo"
|
35
|
+
],
|
36
|
+
"exports": {
|
37
|
+
".": {
|
38
|
+
"import": "./dist/src/index.js"
|
39
|
+
}
|
40
|
+
},
|
41
|
+
"eslintConfig": {
|
42
|
+
"extends": "ipfs",
|
43
|
+
"parserOptions": {
|
44
|
+
"sourceType": "module"
|
45
|
+
}
|
46
|
+
},
|
47
|
+
"release": {
|
48
|
+
"branches": [
|
49
|
+
"master"
|
50
|
+
],
|
51
|
+
"plugins": [
|
52
|
+
[
|
53
|
+
"@semantic-release/commit-analyzer",
|
54
|
+
{
|
55
|
+
"preset": "conventionalcommits",
|
56
|
+
"releaseRules": [
|
57
|
+
{
|
58
|
+
"breaking": true,
|
59
|
+
"release": "major"
|
60
|
+
},
|
61
|
+
{
|
62
|
+
"revert": true,
|
63
|
+
"release": "patch"
|
64
|
+
},
|
65
|
+
{
|
66
|
+
"type": "feat",
|
67
|
+
"release": "minor"
|
68
|
+
},
|
69
|
+
{
|
70
|
+
"type": "fix",
|
71
|
+
"release": "patch"
|
72
|
+
},
|
73
|
+
{
|
74
|
+
"type": "chore",
|
75
|
+
"release": "patch"
|
76
|
+
},
|
77
|
+
{
|
78
|
+
"type": "docs",
|
79
|
+
"release": "patch"
|
80
|
+
},
|
81
|
+
{
|
82
|
+
"type": "test",
|
83
|
+
"release": "patch"
|
84
|
+
},
|
85
|
+
{
|
86
|
+
"scope": "no-release",
|
87
|
+
"release": false
|
88
|
+
}
|
89
|
+
]
|
90
|
+
}
|
91
|
+
],
|
92
|
+
[
|
93
|
+
"@semantic-release/release-notes-generator",
|
94
|
+
{
|
95
|
+
"preset": "conventionalcommits",
|
96
|
+
"presetConfig": {
|
97
|
+
"types": [
|
98
|
+
{
|
99
|
+
"type": "feat",
|
100
|
+
"section": "Features"
|
101
|
+
},
|
102
|
+
{
|
103
|
+
"type": "fix",
|
104
|
+
"section": "Bug Fixes"
|
105
|
+
},
|
106
|
+
{
|
107
|
+
"type": "chore",
|
108
|
+
"section": "Trivial Changes"
|
109
|
+
},
|
110
|
+
{
|
111
|
+
"type": "docs",
|
112
|
+
"section": "Trivial Changes"
|
113
|
+
},
|
114
|
+
{
|
115
|
+
"type": "test",
|
116
|
+
"section": "Tests"
|
117
|
+
}
|
118
|
+
]
|
119
|
+
}
|
120
|
+
}
|
121
|
+
],
|
122
|
+
"@semantic-release/changelog",
|
123
|
+
"@semantic-release/npm",
|
124
|
+
"@semantic-release/github",
|
125
|
+
"@semantic-release/git"
|
126
|
+
]
|
127
|
+
},
|
128
|
+
"scripts": {
|
129
|
+
"lint": "aegir lint",
|
130
|
+
"dep-check": "aegir dep-check",
|
131
|
+
"build": "tsc",
|
132
|
+
"pretest": "npm run build",
|
133
|
+
"test": "aegir test -f ./dist/test",
|
134
|
+
"test:chrome": "npm run test -- -t browser --cov",
|
135
|
+
"test:chrome-webworker": "npm run test -- -t webworker",
|
136
|
+
"test:firefox": "npm run test -- -t browser -- --browser firefox",
|
137
|
+
"test:firefox-webworker": "npm run test -- -t webworker -- --browser firefox",
|
138
|
+
"test:node": "npm run test -- -t node --cov",
|
139
|
+
"test:electron-main": "npm run test -- -t electron-main",
|
140
|
+
"release": "semantic-release"
|
141
|
+
},
|
142
|
+
"dependencies": {
|
143
|
+
"@libp2p/logger": "^1.0.3",
|
144
|
+
"@libp2p/tracked-map": "^1.0.1",
|
145
|
+
"abortable-iterator": "^4.0.2",
|
146
|
+
"any-signal": "^3.0.0",
|
147
|
+
"err-code": "^3.0.1",
|
148
|
+
"it-pipe": "^2.0.3",
|
149
|
+
"it-pushable": "^2.0.1",
|
150
|
+
"it-stream-types": "^1.0.4",
|
151
|
+
"uint8arraylist": "^1.2.0",
|
152
|
+
"varint": "^6.0.0"
|
153
|
+
},
|
154
|
+
"devDependencies": {
|
155
|
+
"@libp2p/interface-compliance-tests": "^1.0.7",
|
156
|
+
"@libp2p/interfaces": "^1.1.1",
|
157
|
+
"@types/varint": "^6.0.0",
|
158
|
+
"aegir": "^36.1.3",
|
159
|
+
"cborg": "^1.2.1",
|
160
|
+
"iso-random-stream": "^2.0.0",
|
161
|
+
"it-all": "^1.0.6",
|
162
|
+
"it-drain": "^1.0.5",
|
163
|
+
"it-foreach": "^0.1.1",
|
164
|
+
"it-map": "^1.0.6",
|
165
|
+
"p-defer": "^4.0.0",
|
166
|
+
"random-int": "^3.0.0",
|
167
|
+
"uint8arrays": "^3.0.0"
|
168
|
+
}
|
169
|
+
}
|
package/src/decode.ts
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
import { MessageTypeNames, MessageTypes } from './message-types.js'
|
2
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
3
|
+
import type { Source } from 'it-stream-types'
|
4
|
+
import type { Message } from './message-types.js'
|
5
|
+
|
6
|
+
interface MessageHeader {
|
7
|
+
id: number
|
8
|
+
type: keyof typeof MessageTypeNames
|
9
|
+
offset: number
|
10
|
+
length: number
|
11
|
+
}
|
12
|
+
|
13
|
+
class Decoder {
|
14
|
+
private readonly _buffer: Uint8ArrayList
|
15
|
+
private _headerInfo: MessageHeader | null
|
16
|
+
|
17
|
+
constructor () {
|
18
|
+
this._buffer = new Uint8ArrayList()
|
19
|
+
this._headerInfo = null
|
20
|
+
}
|
21
|
+
|
22
|
+
write (chunk: Uint8Array) {
|
23
|
+
if (chunk == null || chunk.length === 0) {
|
24
|
+
return []
|
25
|
+
}
|
26
|
+
|
27
|
+
this._buffer.append(chunk)
|
28
|
+
const msgs: Message[] = []
|
29
|
+
|
30
|
+
while (this._buffer.length !== 0) {
|
31
|
+
if (this._headerInfo == null) {
|
32
|
+
try {
|
33
|
+
this._headerInfo = this._decodeHeader(this._buffer)
|
34
|
+
} catch (_) {
|
35
|
+
break // We haven't received enough data yet
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
const { id, type, length, offset } = this._headerInfo
|
40
|
+
const bufferedDataLength = this._buffer.length - offset
|
41
|
+
|
42
|
+
if (bufferedDataLength < length) {
|
43
|
+
break // not enough data yet
|
44
|
+
}
|
45
|
+
|
46
|
+
const msg: any = {
|
47
|
+
id,
|
48
|
+
type
|
49
|
+
}
|
50
|
+
|
51
|
+
if (type === MessageTypes.NEW_STREAM || type === MessageTypes.MESSAGE_INITIATOR || type === MessageTypes.MESSAGE_RECEIVER) {
|
52
|
+
msg.data = this._buffer.slice(offset, offset + length)
|
53
|
+
}
|
54
|
+
|
55
|
+
msgs.push(msg)
|
56
|
+
|
57
|
+
this._buffer.consume(offset + length)
|
58
|
+
this._headerInfo = null
|
59
|
+
}
|
60
|
+
|
61
|
+
return msgs
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Attempts to decode the message header from the buffer
|
66
|
+
*/
|
67
|
+
_decodeHeader (data: Uint8ArrayList): MessageHeader {
|
68
|
+
const {
|
69
|
+
value: h,
|
70
|
+
offset
|
71
|
+
} = readVarInt(data)
|
72
|
+
const {
|
73
|
+
value: length,
|
74
|
+
offset: end
|
75
|
+
} = readVarInt(data, offset)
|
76
|
+
|
77
|
+
const type = h & 7
|
78
|
+
|
79
|
+
// @ts-expect-error h is a number not a CODE
|
80
|
+
if (MessageTypeNames[type] == null) {
|
81
|
+
throw new Error(`Invalid type received: ${type}`)
|
82
|
+
}
|
83
|
+
|
84
|
+
// @ts-expect-error h is a number not a CODE
|
85
|
+
return { id: h >> 3, type, offset: offset + end, length }
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
const MSB = 0x80
|
90
|
+
const REST = 0x7F
|
91
|
+
|
92
|
+
function readVarInt (buf: Uint8ArrayList, offset: number = 0) {
|
93
|
+
let res = 0
|
94
|
+
let shift = 0
|
95
|
+
let counter = offset
|
96
|
+
let b: number
|
97
|
+
const l = buf.length
|
98
|
+
|
99
|
+
do {
|
100
|
+
if (counter >= l || shift > 49) {
|
101
|
+
offset = 0
|
102
|
+
throw new RangeError('Could not decode varint')
|
103
|
+
}
|
104
|
+
b = buf.get(counter++)
|
105
|
+
res += shift < 28
|
106
|
+
? (b & REST) << shift
|
107
|
+
: (b & REST) * Math.pow(2, shift)
|
108
|
+
shift += 7
|
109
|
+
} while (b >= MSB)
|
110
|
+
|
111
|
+
offset = counter - offset
|
112
|
+
|
113
|
+
return {
|
114
|
+
value: res,
|
115
|
+
offset
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Decode a chunk and yield an _array_ of decoded messages
|
121
|
+
*/
|
122
|
+
export async function * decode (source: Source<Uint8Array>) {
|
123
|
+
const decoder = new Decoder()
|
124
|
+
|
125
|
+
for await (const chunk of source) {
|
126
|
+
const msgs = decoder.write(chunk)
|
127
|
+
|
128
|
+
if (msgs.length > 0) {
|
129
|
+
yield msgs
|
130
|
+
}
|
131
|
+
}
|
132
|
+
}
|