@lodestar/reqresp 1.35.0-dev.fcf8d024ea → 1.35.0-dev.feed916580
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/package.json +12 -12
- package/lib/ReqResp.d.ts.map +0 -1
- package/lib/encoders/requestDecode.d.ts.map +0 -1
- package/lib/encoders/requestEncode.d.ts.map +0 -1
- package/lib/encoders/responseDecode.d.ts.map +0 -1
- package/lib/encoders/responseEncode.d.ts.map +0 -1
- package/lib/encodingStrategies/index.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/decode.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/encode.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/errors.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/index.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/snappyFrames/common.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/snappyFrames/compress.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/snappyFrames/uncompress.d.ts.map +0 -1
- package/lib/encodingStrategies/sszSnappy/utils.d.ts.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/interface.d.ts.map +0 -1
- package/lib/metrics.d.ts.map +0 -1
- package/lib/rate_limiter/ReqRespRateLimiter.d.ts.map +0 -1
- package/lib/rate_limiter/rateLimiterGRCA.d.ts.map +0 -1
- package/lib/rate_limiter/selfRateLimiter.d.ts.map +0 -1
- package/lib/request/errors.d.ts.map +0 -1
- package/lib/request/index.d.ts.map +0 -1
- package/lib/response/errors.d.ts.map +0 -1
- package/lib/response/index.d.ts.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/utils/abortableSource.d.ts.map +0 -1
- package/lib/utils/bufferedSource.d.ts.map +0 -1
- package/lib/utils/collectExactOne.d.ts.map +0 -1
- package/lib/utils/collectMaxResponse.d.ts.map +0 -1
- package/lib/utils/errorMessage.d.ts.map +0 -1
- package/lib/utils/index.d.ts.map +0 -1
- package/lib/utils/onChunk.d.ts.map +0 -1
- package/lib/utils/peerId.d.ts.map +0 -1
- package/lib/utils/protocolId.d.ts.map +0 -1
- package/src/ReqResp.ts +0 -289
- package/src/encoders/requestDecode.ts +0 -29
- package/src/encoders/requestEncode.ts +0 -18
- package/src/encoders/responseDecode.ts +0 -169
- package/src/encoders/responseEncode.ts +0 -81
- package/src/encodingStrategies/index.ts +0 -46
- package/src/encodingStrategies/sszSnappy/decode.ts +0 -111
- package/src/encodingStrategies/sszSnappy/encode.ts +0 -24
- package/src/encodingStrategies/sszSnappy/errors.ts +0 -31
- package/src/encodingStrategies/sszSnappy/index.ts +0 -3
- package/src/encodingStrategies/sszSnappy/snappyFrames/common.ts +0 -36
- package/src/encodingStrategies/sszSnappy/snappyFrames/compress.ts +0 -25
- package/src/encodingStrategies/sszSnappy/snappyFrames/uncompress.ts +0 -114
- package/src/encodingStrategies/sszSnappy/utils.ts +0 -7
- package/src/index.ts +0 -10
- package/src/interface.ts +0 -26
- package/src/metrics.ts +0 -95
- package/src/rate_limiter/ReqRespRateLimiter.ts +0 -107
- package/src/rate_limiter/rateLimiterGRCA.ts +0 -92
- package/src/rate_limiter/selfRateLimiter.ts +0 -112
- package/src/request/errors.ts +0 -119
- package/src/request/index.ts +0 -225
- package/src/response/errors.ts +0 -50
- package/src/response/index.ts +0 -147
- package/src/types.ts +0 -158
- package/src/utils/abortableSource.ts +0 -80
- package/src/utils/bufferedSource.ts +0 -46
- package/src/utils/collectExactOne.ts +0 -15
- package/src/utils/collectMaxResponse.ts +0 -19
- package/src/utils/errorMessage.ts +0 -51
- package/src/utils/index.ts +0 -8
- package/src/utils/onChunk.ts +0 -12
- package/src/utils/peerId.ts +0 -6
- package/src/utils/protocolId.ts +0 -44
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import {Uint8ArrayList} from "uint8arraylist";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Wraps a buffer chunk stream source with another async iterable
|
|
5
|
-
* so it can be reused in multiple for..of statements.
|
|
6
|
-
*
|
|
7
|
-
* Uses a BufferList internally to make sure all chunks are consumed
|
|
8
|
-
* when switching consumers
|
|
9
|
-
*/
|
|
10
|
-
export class BufferedSource {
|
|
11
|
-
isDone = false;
|
|
12
|
-
private buffer: Uint8ArrayList;
|
|
13
|
-
private source: AsyncGenerator<Uint8ArrayList | Uint8Array>;
|
|
14
|
-
|
|
15
|
-
constructor(source: AsyncGenerator<Uint8ArrayList | Uint8Array>) {
|
|
16
|
-
this.buffer = new Uint8ArrayList();
|
|
17
|
-
this.source = source;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
[Symbol.asyncIterator](): AsyncIterator<Uint8ArrayList> {
|
|
21
|
-
const that = this;
|
|
22
|
-
|
|
23
|
-
let firstNext = true;
|
|
24
|
-
|
|
25
|
-
return {
|
|
26
|
-
async next() {
|
|
27
|
-
// Prevent fetching a new chunk if there are pending bytes
|
|
28
|
-
// not processed by a previous consumer of this BufferedSource
|
|
29
|
-
if (firstNext && that.buffer.length > 0) {
|
|
30
|
-
firstNext = false;
|
|
31
|
-
return {done: false, value: that.buffer};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const {done, value: chunk} = await that.source.next();
|
|
35
|
-
if (done === true) {
|
|
36
|
-
that.isDone = true;
|
|
37
|
-
return {done: true, value: undefined};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Concat new chunk and return a reference to its BufferList instance
|
|
41
|
-
that.buffer.append(chunk);
|
|
42
|
-
return {done: false, value: that.buffer};
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import {RequestError, RequestErrorCode} from "../request/errors.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Sink for `<response_chunk>*`, from
|
|
5
|
-
* ```bnf
|
|
6
|
-
* response ::= <response_chunk>*
|
|
7
|
-
* ```
|
|
8
|
-
* Expects exactly one response
|
|
9
|
-
*/
|
|
10
|
-
export async function collectExactOne<T>(source: AsyncIterable<T>): Promise<T> {
|
|
11
|
-
for await (const response of source) {
|
|
12
|
-
return response;
|
|
13
|
-
}
|
|
14
|
-
throw new RequestError({code: RequestErrorCode.EMPTY_RESPONSE});
|
|
15
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sink for `<response_chunk>*`, from
|
|
3
|
-
* ```bnf
|
|
4
|
-
* response ::= <response_chunk>*
|
|
5
|
-
* ```
|
|
6
|
-
* Collects a bounded list of responses up to `maxResponses`
|
|
7
|
-
*/
|
|
8
|
-
export async function collectMaxResponse<T>(source: AsyncIterable<T>, maxResponses: number): Promise<T[]> {
|
|
9
|
-
// else: zero or more responses
|
|
10
|
-
const responses: T[] = [];
|
|
11
|
-
for await (const response of source) {
|
|
12
|
-
responses.push(response);
|
|
13
|
-
|
|
14
|
-
if (maxResponses !== undefined && responses.length >= maxResponses) {
|
|
15
|
-
break;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return responses;
|
|
19
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import {decode as varintDecode, encodingLength as varintEncodingLength} from "uint8-varint";
|
|
2
|
-
import {Uint8ArrayList} from "uint8arraylist";
|
|
3
|
-
import {writeSszSnappyPayload} from "../encodingStrategies/sszSnappy/encode.js";
|
|
4
|
-
import {SnappyFramesUncompress} from "../encodingStrategies/sszSnappy/snappyFrames/uncompress.js";
|
|
5
|
-
import {Encoding} from "../types.js";
|
|
6
|
-
|
|
7
|
-
// ErrorMessage schema:
|
|
8
|
-
//
|
|
9
|
-
// (
|
|
10
|
-
// error_message: List[byte, 256]
|
|
11
|
-
// )
|
|
12
|
-
//
|
|
13
|
-
// By convention, the error_message is a sequence of bytes that MAY be interpreted as a
|
|
14
|
-
// UTF-8 string (for debugging purposes). Clients MUST treat as valid any byte sequences
|
|
15
|
-
//
|
|
16
|
-
// Spec v1.1.10 https://github.com/ethereum/consensus-specs/blob/v1.1.10/specs/phase0/p2p-interface.md#responding-side
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Encodes a UTF-8 string to 256 bytes max
|
|
20
|
-
*/
|
|
21
|
-
export async function* encodeErrorMessage(errorMessage: string, encoding: Encoding): AsyncGenerator<Buffer> {
|
|
22
|
-
const encoder = new TextEncoder();
|
|
23
|
-
const bytes = encoder.encode(errorMessage).slice(0, 256);
|
|
24
|
-
|
|
25
|
-
switch (encoding) {
|
|
26
|
-
case Encoding.SSZ_SNAPPY:
|
|
27
|
-
yield* writeSszSnappyPayload(bytes);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Decodes error message from network bytes and removes non printable, non ascii characters.
|
|
33
|
-
*/
|
|
34
|
-
export async function decodeErrorMessage(encodedErrorMessage: Uint8Array): Promise<string> {
|
|
35
|
-
const encoder = new TextDecoder();
|
|
36
|
-
let sszDataLength: number;
|
|
37
|
-
try {
|
|
38
|
-
sszDataLength = varintDecode(encodedErrorMessage);
|
|
39
|
-
const decompressor = new SnappyFramesUncompress();
|
|
40
|
-
const varintBytes = varintEncodingLength(sszDataLength);
|
|
41
|
-
const errorMessage = decompressor.uncompress(new Uint8ArrayList(encodedErrorMessage.subarray(varintBytes)));
|
|
42
|
-
if (errorMessage == null || errorMessage.length !== sszDataLength) {
|
|
43
|
-
throw new Error("Malformed input: data length mismatch");
|
|
44
|
-
}
|
|
45
|
-
// remove non ascii characters from string
|
|
46
|
-
return encoder.decode(errorMessage.subarray(0)).replace(/[^\x20-\x7F]/g, "");
|
|
47
|
-
} catch (_e) {
|
|
48
|
-
// remove non ascii characters from string
|
|
49
|
-
return encoder.decode(encodedErrorMessage.slice(0, 256)).replace(/[^\x20-\x7F]/g, "");
|
|
50
|
-
}
|
|
51
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * from "./abortableSource.js";
|
|
2
|
-
export * from "./bufferedSource.js";
|
|
3
|
-
export * from "./collectExactOne.js";
|
|
4
|
-
export * from "./collectMaxResponse.js";
|
|
5
|
-
export * from "./errorMessage.js";
|
|
6
|
-
export * from "./onChunk.js";
|
|
7
|
-
export * from "./peerId.js";
|
|
8
|
-
export * from "./protocolId.js";
|
package/src/utils/onChunk.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Calls `callback` with each `chunk` received from the `source` AsyncIterable
|
|
3
|
-
* Useful for logging, or cancelling timeouts
|
|
4
|
-
*/
|
|
5
|
-
export function onChunk<T>(callback: (chunk: T) => void): (source: AsyncIterable<T>) => AsyncIterable<T> {
|
|
6
|
-
return async function* onChunkTransform(source) {
|
|
7
|
-
for await (const chunk of source) {
|
|
8
|
-
callback(chunk);
|
|
9
|
-
yield chunk;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
|
-
}
|
package/src/utils/peerId.ts
DELETED
package/src/utils/protocolId.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import {Encoding, ProtocolAttributes} from "../types.js";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* https://github.com/ethereum/consensus-specs/blob/v1.2.0/specs/phase0/p2p-interface.md#protocol-identification
|
|
5
|
-
*/
|
|
6
|
-
export function formatProtocolID(protocolPrefix: string, method: string, version: number, encoding: Encoding): string {
|
|
7
|
-
return `${protocolPrefix}/${method}/${version}/${encoding}`;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* https://github.com/ethereum/consensus-specs/blob/v1.2.0/specs/phase0/p2p-interface.md#protocol-identification
|
|
12
|
-
*/
|
|
13
|
-
export function parseProtocolID(protocolId: string): ProtocolAttributes {
|
|
14
|
-
const result = protocolId.split("/");
|
|
15
|
-
if (result.length < 4) {
|
|
16
|
-
throw new Error(`Invalid protocol id: ${protocolId}`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const encoding = result.at(-1) as Encoding;
|
|
20
|
-
if (!Object.values(Encoding).includes(encoding)) {
|
|
21
|
-
throw new Error(`Invalid protocol encoding: ${encoding}`);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const versionStr = result.at(-2) as string;
|
|
25
|
-
if (!/^-?[0-9]+$/.test(versionStr)) {
|
|
26
|
-
throw new Error(`Invalid protocol version: ${versionStr}`);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// an ordinal version number (e.g. 1, 2, 3…).
|
|
30
|
-
const version = parseInt(versionStr);
|
|
31
|
-
|
|
32
|
-
// each request is identified by a name consisting of English alphabet, digits and underscores (_).
|
|
33
|
-
const method = result.at(-3) as string;
|
|
34
|
-
|
|
35
|
-
// messages are grouped into families identified by a shared libp2p protocol name prefix
|
|
36
|
-
const protocolPrefix = result.slice(0, result.length - 3).join("/");
|
|
37
|
-
|
|
38
|
-
return {
|
|
39
|
-
protocolPrefix,
|
|
40
|
-
method,
|
|
41
|
-
version,
|
|
42
|
-
encoding,
|
|
43
|
-
};
|
|
44
|
-
}
|