@waku/core 0.0.36-b0a2e39.0 → 0.0.36-b133417.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.
Files changed (40) hide show
  1. package/bundle/{base_protocol-DEDdl6tx.js → base_protocol-DvQrudwy.js} +1 -1
  2. package/bundle/{index-DckUzRoN.js → index-CTo1my9M.js} +1 -80
  3. package/bundle/index.js +68 -330
  4. package/bundle/lib/base_protocol.js +2 -2
  5. package/bundle/lib/message/version_0.js +2 -2
  6. package/bundle/{version_0-DyRL7WVV.js → version_0-CyeTW0Vr.js} +32 -370
  7. package/dist/.tsbuildinfo +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.js +1 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/lib/connection_manager/connection_manager.d.ts +1 -1
  12. package/dist/lib/connection_manager/connection_manager.js +1 -1
  13. package/dist/lib/light_push/index.d.ts +1 -2
  14. package/dist/lib/light_push/index.js +1 -2
  15. package/dist/lib/light_push/index.js.map +1 -1
  16. package/dist/lib/light_push/light_push.d.ts +7 -2
  17. package/dist/lib/light_push/light_push.js +35 -19
  18. package/dist/lib/light_push/light_push.js.map +1 -1
  19. package/dist/lib/light_push/{push_rpc_v2.d.ts → push_rpc.d.ts} +3 -3
  20. package/dist/lib/light_push/{push_rpc_v2.js → push_rpc.js} +4 -4
  21. package/dist/lib/light_push/push_rpc.js.map +1 -0
  22. package/dist/lib/light_push/utils.d.ts +0 -24
  23. package/dist/lib/light_push/utils.js +0 -70
  24. package/dist/lib/light_push/utils.js.map +1 -1
  25. package/package.json +1 -1
  26. package/src/index.ts +1 -5
  27. package/src/lib/connection_manager/connection_manager.ts +1 -1
  28. package/src/lib/light_push/index.ts +1 -2
  29. package/src/lib/light_push/light_push.ts +43 -26
  30. package/src/lib/light_push/{push_rpc_v2.ts → push_rpc.ts} +5 -5
  31. package/src/lib/light_push/utils.ts +0 -95
  32. package/dist/lib/light_push/light_push_v3.d.ts +0 -14
  33. package/dist/lib/light_push/light_push_v3.js +0 -187
  34. package/dist/lib/light_push/light_push_v3.js.map +0 -1
  35. package/dist/lib/light_push/push_rpc_v2.js.map +0 -1
  36. package/dist/lib/light_push/push_rpc_v3.d.ts +0 -11
  37. package/dist/lib/light_push/push_rpc_v3.js +0 -32
  38. package/dist/lib/light_push/push_rpc_v3.js.map +0 -1
  39. package/src/lib/light_push/light_push_v3.ts +0 -319
  40. package/src/lib/light_push/push_rpc_v3.ts +0 -45
@@ -1,187 +0,0 @@
1
- import { getLightPushStatusDescriptionV3, isSuccessStatusCodeV3, LightPushCodecV3, lightPushStatusCodeToProtocolErrorV3, LightPushStatusCodeV3, ProtocolError } from "@waku/interfaces";
2
- import { proto_lightpush_v3 } from "@waku/proto";
3
- import { Logger } from "@waku/utils";
4
- import all from "it-all";
5
- import * as lp from "it-length-prefixed";
6
- import { pipe } from "it-pipe";
7
- import { Uint8ArrayList } from "uint8arraylist";
8
- import { BaseProtocol } from "../base_protocol.js";
9
- import { PushRpcV3 } from "./push_rpc_v3.js";
10
- import { isRLNResponseError, isValidRequestId, validateMessage } from "./utils.js";
11
- const log = new Logger("light-push-v3");
12
- const STREAM_TIMEOUT_MS = 30000;
13
- class LightPushError extends Error {
14
- protocol;
15
- peerId;
16
- response;
17
- constructor(protocol, peerId, response, message) {
18
- super(message || protocol);
19
- this.protocol = protocol;
20
- this.peerId = peerId;
21
- this.response = response;
22
- this.name = "LightPushError";
23
- }
24
- }
25
- function withTimeout(promise, timeoutMs) {
26
- return Promise.race([
27
- promise,
28
- new Promise((_, reject) => setTimeout(() => reject(new Error("Stream operation timed out")), timeoutMs))
29
- ]);
30
- }
31
- function createSuccessResult(peerId, response) {
32
- return {
33
- success: peerId,
34
- failure: null,
35
- requestId: response.requestId,
36
- statusCode: response.statusCode,
37
- statusDesc: response.statusDesc,
38
- relayPeerCount: response.relayPeerCount
39
- };
40
- }
41
- function createFailureResult(error) {
42
- const result = {
43
- success: null,
44
- failure: {
45
- error: error.protocol,
46
- peerId: error.peerId
47
- }
48
- };
49
- if (error.response) {
50
- result.requestId = error.response.requestId;
51
- result.statusCode = error.response.statusCode;
52
- result.statusDesc = error.response.statusDesc;
53
- result.relayPeerCount = error.response.relayPeerCount;
54
- }
55
- return result;
56
- }
57
- function validateResponseSemantics(response, query, peerId) {
58
- if (!isValidRequestId(response.requestId)) {
59
- log.error("Invalid request ID format", { received: response.requestId });
60
- throw new LightPushError(ProtocolError.DECODE_FAILED, peerId, response, "Invalid request ID format");
61
- }
62
- if (response.requestId !== query.query?.requestId &&
63
- response.statusCode !== LightPushStatusCodeV3.TOO_MANY_REQUESTS) {
64
- log.error("Request ID mismatch", {
65
- sent: query.query?.requestId,
66
- received: response.requestId
67
- });
68
- throw new LightPushError(ProtocolError.GENERIC_FAIL, peerId, response, "Request ID mismatch");
69
- }
70
- if (response.statusDesc && isRLNResponseError(response.statusDesc)) {
71
- log.error("Remote peer fault: RLN generation");
72
- throw new LightPushError(ProtocolError.RLN_PROOF_GENERATION, peerId, response, "RLN proof generation failed");
73
- }
74
- }
75
- function ensureSuccessStatus(response, peerId) {
76
- if (!isSuccessStatusCodeV3(response.statusCode)) {
77
- const errorMessage = getLightPushStatusDescriptionV3(response.statusCode, response.statusDesc);
78
- log.error("Remote peer rejected the message:", errorMessage);
79
- const protocolError = lightPushStatusCodeToProtocolErrorV3(response.statusCode);
80
- throw new LightPushError(protocolError, peerId, response, errorMessage);
81
- }
82
- }
83
- function logResponseInfo(response) {
84
- if (response.statusCode === LightPushStatusCodeV3.TOO_MANY_REQUESTS) {
85
- if (response.requestId === "N/A") {
86
- log.warn("Rate limited by nwaku node", {
87
- statusDesc: response.statusDesc || "Request rejected due to too many requests"
88
- });
89
- }
90
- }
91
- if (response.relayPeerCount !== undefined) {
92
- log.info(`Message relayed to ${response.relayPeerCount} peers`);
93
- }
94
- }
95
- export class LightPushCoreV3 extends BaseProtocol {
96
- pubsubTopics;
97
- constructor(pubsubTopics, libp2p) {
98
- super(LightPushCodecV3, libp2p.components, pubsubTopics);
99
- this.pubsubTopics = pubsubTopics;
100
- }
101
- async send(encoder, message, peerId) {
102
- try {
103
- const validated = await this.validateAndPrepareMessage(encoder, message, peerId);
104
- const streamResponse = await this.executeRequest(validated, peerId);
105
- const finalResponse = this.processResponse(streamResponse, peerId);
106
- logResponseInfo(finalResponse);
107
- return createSuccessResult(peerId, finalResponse);
108
- }
109
- catch (error) {
110
- if (error instanceof LightPushError) {
111
- return createFailureResult(error);
112
- }
113
- log.error("Unexpected error in send", error);
114
- return createFailureResult(new LightPushError(ProtocolError.GENERIC_FAIL, peerId));
115
- }
116
- }
117
- async validateAndPrepareMessage(encoder, message, peerId) {
118
- const validationError = await validateMessage(message, encoder);
119
- if (validationError) {
120
- throw new LightPushError(validationError, peerId);
121
- }
122
- const protoMessage = await encoder.toProtoObj(message);
123
- if (!protoMessage) {
124
- log.error("Failed to encode to protoMessage, aborting push");
125
- throw new LightPushError(ProtocolError.ENCODE_FAILED, peerId);
126
- }
127
- const query = PushRpcV3.createRequest(protoMessage, encoder.pubsubTopic);
128
- return { protoMessage: protoMessage, query };
129
- }
130
- async executeRequest(validated, peerId) {
131
- const stream = await this.acquireStream(peerId);
132
- try {
133
- const rawResponse = await this.sendAndReceive(validated.query, stream);
134
- const response = this.decodeResponse(rawResponse, peerId);
135
- return { response, query: validated.query };
136
- }
137
- finally {
138
- void stream.close();
139
- }
140
- }
141
- processResponse(streamResponse, peerId) {
142
- const { response, query } = streamResponse;
143
- validateResponseSemantics(response, query, peerId);
144
- ensureSuccessStatus(response, peerId);
145
- return response;
146
- }
147
- async acquireStream(peerId) {
148
- try {
149
- return await this.getStream(peerId);
150
- }
151
- catch (error) {
152
- log.error("Failed to get stream", error);
153
- throw new LightPushError(ProtocolError.NO_STREAM_AVAILABLE, peerId);
154
- }
155
- }
156
- async sendAndReceive(query, stream) {
157
- try {
158
- return await withTimeout(pipe([query.encode()], lp.encode, stream, lp.decode, async (source) => await all(source)), STREAM_TIMEOUT_MS);
159
- }
160
- catch (err) {
161
- log.error("Failed to send waku light push request", err);
162
- throw new Error("Stream operation failed");
163
- }
164
- }
165
- decodeResponse(rawResponse, peerId) {
166
- const bytes = rawResponse.reduce((acc, chunk) => {
167
- acc.append(chunk);
168
- return acc;
169
- }, new Uint8ArrayList());
170
- try {
171
- const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
172
- if (!response) {
173
- log.error("Remote peer fault: No response received");
174
- throw new LightPushError(ProtocolError.NO_RESPONSE, peerId);
175
- }
176
- return response;
177
- }
178
- catch (err) {
179
- if (err instanceof LightPushError) {
180
- throw err;
181
- }
182
- log.error("Failed to decode push response", err);
183
- throw new LightPushError(ProtocolError.DECODE_FAILED, peerId);
184
- }
185
- }
186
- }
187
- //# sourceMappingURL=light_push_v3.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"light_push_v3.js","sourceRoot":"","sources":["../../../src/lib/light_push/light_push_v3.ts"],"names":[],"mappings":"AACA,OAAO,EACL,+BAA+B,EAI/B,qBAAqB,EAErB,gBAAgB,EAEhB,oCAAoC,EACpC,qBAAqB,EACrB,aAAa,EAEd,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,GAAG,MAAM,QAAQ,CAAC;AACzB,OAAO,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC/B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB,MAAM,GAAG,GAAG,IAAI,MAAM,CAAC,e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2.js","sourceRoot":"","sources":["../../../src/lib/light_push/push_rpc_v2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,IAAI,KAAK,EAAE,MAAM,aAAa,CAAC;AAEvD,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,MAAM,OAAO,SAAS;IACM;IAA1B,YAA0B,KAAoB;QAApB,UAAK,GAAL,KAAK,CAAe;IAAG,CAAC;IAE3C,MAAM,CAAC,aAAa,CACzB,OAA0B,EAC1B,WAAmB;QAEnB,OAAO,IAAI,SAAS,CAAC;YACnB,SAAS,EAAE,IAAI,EAAE;YACjB,OAAO,EAAE;gBACP,OAAO,EAAE,OAAO;gBAChB,WAAW,EAAE,WAAW;aACzB;YACD,QAAQ,EAAE,SAAS;SACpB,CAAC,CAAC;IACL,CAAC;IAEM,MAAM,CAAC,MAAM,CAAC,KAAqB;QACxC,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACxC,OAAO,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;IAC5B,CAAC;IAEM,MAAM;QACX,OAAO,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC;IAC5B,CAAC;IAED,IAAW,QAAQ;QACjB,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;IAC7B,CAAC;CACF"}
@@ -1,11 +0,0 @@
1
- import { proto_lightpush_v3, WakuMessage } from "@waku/proto";
2
- import type { Uint8ArrayList } from "uint8arraylist";
3
- export declare class PushRpcV3 {
4
- request?: proto_lightpush_v3.LightpushRequest;
5
- response?: proto_lightpush_v3.LightpushResponse;
6
- private constructor();
7
- static createRequest(message: WakuMessage, pubsubTopic?: string): PushRpcV3;
8
- static decode(bytes: Uint8ArrayList | Uint8Array): PushRpcV3;
9
- encode(): Uint8Array;
10
- get query(): proto_lightpush_v3.LightpushRequest | undefined;
11
- }
@@ -1,32 +0,0 @@
1
- import { proto_lightpush_v3 } from "@waku/proto";
2
- import { v4 as uuid } from "uuid";
3
- export class PushRpcV3 {
4
- request;
5
- response;
6
- constructor(request, response) {
7
- this.request = request;
8
- this.response = response;
9
- }
10
- static createRequest(message, pubsubTopic) {
11
- const request = {
12
- requestId: uuid(),
13
- message: message,
14
- ...(pubsubTopic && { pubsubTopic })
15
- };
16
- return new PushRpcV3(request, undefined);
17
- }
18
- static decode(bytes) {
19
- const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
20
- return new PushRpcV3(undefined, response);
21
- }
22
- encode() {
23
- if (!this.request) {
24
- throw new Error("Cannot encode without a request");
25
- }
26
- return proto_lightpush_v3.LightpushRequest.encode(this.request);
27
- }
28
- get query() {
29
- return this.request;
30
- }
31
- }
32
- //# sourceMappingURL=push_rpc_v3.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"push_rpc_v3.js","sourceRoot":"","sources":["../../../src/lib/light_push/push_rpc_v3.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAe,MAAM,aAAa,CAAC;AAE9D,OAAO,EAAE,EAAE,IAAI,IAAI,EAAE,MAAM,MAAM,CAAC;AAElC,MAAM,OAAO,SAAS;IACb,OAAO,CAAuC;IAC9C,QAAQ,CAAwC;IAEvD,YACE,OAA6C,EAC7C,QAA+C;QAE/C,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEM,MAAM,CAAC,aAAa,CACzB,OAAoB,EACpB,WAAoB;QAEpB,MAAM,OAAO,GAAwC;YACnD,SAAS,EAAE,IAAI,EAAE;YACjB,OAAO,EAAE,OAAO;YAChB,GAAG,CAAC,WAAW,IAAI,EAAE,WAAW,EAAE,CAAC;SACpC,CAAC;QAEF,OAAO,IAAI,SAAS,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC;IAC3C,CAAC;IAEM,MAAM,CAAC,MAAM,CAAC,KAAkC;QACrD,MAAM,QAAQ,GAAG,kBAAkB,CAAC,iBAAiB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACpE,OAAO,IAAI,SAAS,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IAC5C,CAAC;IAEM,MAAM;QACX,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;QACrD,CAAC;QACD,OAAO,kBAAkB,CAAC,gBAAgB,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAClE,CAAC;IAED,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF"}
@@ -1,319 +0,0 @@
1
- import type { PeerId, Stream } from "@libp2p/interface";
2
- import {
3
- getLightPushStatusDescriptionV3,
4
- type IBaseProtocolCore,
5
- type IEncoder,
6
- type IMessage,
7
- isSuccessStatusCodeV3,
8
- type Libp2p,
9
- LightPushCodecV3,
10
- type LightPushResult,
11
- lightPushStatusCodeToProtocolErrorV3,
12
- LightPushStatusCodeV3,
13
- ProtocolError,
14
- PubsubTopic
15
- } from "@waku/interfaces";
16
- import { proto_lightpush_v3, WakuMessage } from "@waku/proto";
17
- import { Logger } from "@waku/utils";
18
- import all from "it-all";
19
- import * as lp from "it-length-prefixed";
20
- import { pipe } from "it-pipe";
21
- import { Uint8ArrayList } from "uint8arraylist";
22
-
23
- import { BaseProtocol } from "../base_protocol.js";
24
-
25
- import { PushRpcV3 } from "./push_rpc_v3.js";
26
- import {
27
- isRLNResponseError,
28
- isValidRequestId,
29
- validateMessage
30
- } from "./utils.js";
31
-
32
- const log = new Logger("light-push-v3");
33
- const STREAM_TIMEOUT_MS = 30000;
34
-
35
- class LightPushError extends Error {
36
- public constructor(
37
- public readonly protocol: ProtocolError,
38
- public readonly peerId: PeerId,
39
- public readonly response?: proto_lightpush_v3.LightpushResponse,
40
- message?: string
41
- ) {
42
- super(message || protocol);
43
- this.name = "LightPushError";
44
- }
45
- }
46
-
47
- type ValidatedMessage = {
48
- protoMessage: WakuMessage;
49
- query: PushRpcV3;
50
- };
51
-
52
- type StreamResponse = {
53
- response: proto_lightpush_v3.LightpushResponse;
54
- query: PushRpcV3;
55
- };
56
-
57
- function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
58
- return Promise.race([
59
- promise,
60
- new Promise<T>((_, reject) =>
61
- setTimeout(
62
- () => reject(new Error("Stream operation timed out")),
63
- timeoutMs
64
- )
65
- )
66
- ]);
67
- }
68
-
69
- function createSuccessResult(
70
- peerId: PeerId,
71
- response: proto_lightpush_v3.LightpushResponse
72
- ): LightPushResult {
73
- return {
74
- success: peerId,
75
- failure: null,
76
- requestId: response.requestId,
77
- statusCode: response.statusCode,
78
- statusDesc: response.statusDesc,
79
- relayPeerCount: response.relayPeerCount
80
- };
81
- }
82
-
83
- function createFailureResult(error: LightPushError): LightPushResult {
84
- const result: LightPushResult = {
85
- success: null,
86
- failure: {
87
- error: error.protocol,
88
- peerId: error.peerId
89
- }
90
- };
91
-
92
- if (error.response) {
93
- result.requestId = error.response.requestId;
94
- result.statusCode = error.response.statusCode;
95
- result.statusDesc = error.response.statusDesc;
96
- result.relayPeerCount = error.response.relayPeerCount;
97
- }
98
-
99
- return result;
100
- }
101
-
102
- function validateResponseSemantics(
103
- response: proto_lightpush_v3.LightpushResponse,
104
- query: PushRpcV3,
105
- peerId: PeerId
106
- ): void {
107
- if (!isValidRequestId(response.requestId)) {
108
- log.error("Invalid request ID format", { received: response.requestId });
109
- throw new LightPushError(
110
- ProtocolError.DECODE_FAILED,
111
- peerId,
112
- response,
113
- "Invalid request ID format"
114
- );
115
- }
116
-
117
- if (
118
- response.requestId !== query.query?.requestId &&
119
- response.statusCode !== LightPushStatusCodeV3.TOO_MANY_REQUESTS
120
- ) {
121
- log.error("Request ID mismatch", {
122
- sent: query.query?.requestId,
123
- received: response.requestId
124
- });
125
- throw new LightPushError(
126
- ProtocolError.GENERIC_FAIL,
127
- peerId,
128
- response,
129
- "Request ID mismatch"
130
- );
131
- }
132
-
133
- if (response.statusDesc && isRLNResponseError(response.statusDesc)) {
134
- log.error("Remote peer fault: RLN generation");
135
- throw new LightPushError(
136
- ProtocolError.RLN_PROOF_GENERATION,
137
- peerId,
138
- response,
139
- "RLN proof generation failed"
140
- );
141
- }
142
- }
143
-
144
- function ensureSuccessStatus(
145
- response: proto_lightpush_v3.LightpushResponse,
146
- peerId: PeerId
147
- ): void {
148
- if (!isSuccessStatusCodeV3(response.statusCode)) {
149
- const errorMessage = getLightPushStatusDescriptionV3(
150
- response.statusCode,
151
- response.statusDesc
152
- );
153
- log.error("Remote peer rejected the message:", errorMessage);
154
-
155
- const protocolError = lightPushStatusCodeToProtocolErrorV3(
156
- response.statusCode
157
- );
158
- throw new LightPushError(protocolError, peerId, response, errorMessage);
159
- }
160
- }
161
-
162
- function logResponseInfo(response: proto_lightpush_v3.LightpushResponse): void {
163
- if (response.statusCode === LightPushStatusCodeV3.TOO_MANY_REQUESTS) {
164
- if (response.requestId === "N/A") {
165
- log.warn("Rate limited by nwaku node", {
166
- statusDesc:
167
- response.statusDesc || "Request rejected due to too many requests"
168
- });
169
- }
170
- }
171
-
172
- if (response.relayPeerCount !== undefined) {
173
- log.info(`Message relayed to ${response.relayPeerCount} peers`);
174
- }
175
- }
176
-
177
- export class LightPushCoreV3 extends BaseProtocol implements IBaseProtocolCore {
178
- public constructor(
179
- public readonly pubsubTopics: PubsubTopic[],
180
- libp2p: Libp2p
181
- ) {
182
- super(LightPushCodecV3, libp2p.components, pubsubTopics);
183
- }
184
-
185
- public async send(
186
- encoder: IEncoder,
187
- message: IMessage,
188
- peerId: PeerId
189
- ): Promise<LightPushResult> {
190
- try {
191
- const validated = await this.validateAndPrepareMessage(
192
- encoder,
193
- message,
194
- peerId
195
- );
196
- const streamResponse = await this.executeRequest(validated, peerId);
197
- const finalResponse = this.processResponse(streamResponse, peerId);
198
-
199
- logResponseInfo(finalResponse);
200
- return createSuccessResult(peerId, finalResponse);
201
- } catch (error) {
202
- if (error instanceof LightPushError) {
203
- return createFailureResult(error);
204
- }
205
-
206
- log.error("Unexpected error in send", error);
207
- return createFailureResult(
208
- new LightPushError(ProtocolError.GENERIC_FAIL, peerId)
209
- );
210
- }
211
- }
212
-
213
- private async validateAndPrepareMessage(
214
- encoder: IEncoder,
215
- message: IMessage,
216
- peerId: PeerId
217
- ): Promise<ValidatedMessage> {
218
- const validationError = await validateMessage(message, encoder);
219
- if (validationError) {
220
- throw new LightPushError(validationError, peerId);
221
- }
222
-
223
- const protoMessage = await encoder.toProtoObj(message);
224
- if (!protoMessage) {
225
- log.error("Failed to encode to protoMessage, aborting push");
226
- throw new LightPushError(ProtocolError.ENCODE_FAILED, peerId);
227
- }
228
-
229
- const query = PushRpcV3.createRequest(
230
- protoMessage as WakuMessage,
231
- encoder.pubsubTopic
232
- );
233
-
234
- return { protoMessage: protoMessage as WakuMessage, query };
235
- }
236
-
237
- private async executeRequest(
238
- validated: ValidatedMessage,
239
- peerId: PeerId
240
- ): Promise<StreamResponse> {
241
- const stream = await this.acquireStream(peerId);
242
-
243
- try {
244
- const rawResponse = await this.sendAndReceive(validated.query, stream);
245
- const response = this.decodeResponse(rawResponse, peerId);
246
-
247
- return { response, query: validated.query };
248
- } finally {
249
- void stream.close();
250
- }
251
- }
252
-
253
- private processResponse(
254
- streamResponse: StreamResponse,
255
- peerId: PeerId
256
- ): proto_lightpush_v3.LightpushResponse {
257
- const { response, query } = streamResponse;
258
-
259
- validateResponseSemantics(response, query, peerId);
260
- ensureSuccessStatus(response, peerId);
261
-
262
- return response;
263
- }
264
-
265
- private async acquireStream(peerId: PeerId): Promise<Stream> {
266
- try {
267
- return await this.getStream(peerId);
268
- } catch (error) {
269
- log.error("Failed to get stream", error);
270
- throw new LightPushError(ProtocolError.NO_STREAM_AVAILABLE, peerId);
271
- }
272
- }
273
-
274
- private async sendAndReceive(
275
- query: PushRpcV3,
276
- stream: Stream
277
- ): Promise<Uint8ArrayList[]> {
278
- try {
279
- return await withTimeout(
280
- pipe(
281
- [query.encode()],
282
- lp.encode,
283
- stream,
284
- lp.decode,
285
- async (source) => await all(source)
286
- ),
287
- STREAM_TIMEOUT_MS
288
- );
289
- } catch (err) {
290
- log.error("Failed to send waku light push request", err);
291
- throw new Error("Stream operation failed");
292
- }
293
- }
294
-
295
- private decodeResponse(
296
- rawResponse: Uint8ArrayList[],
297
- peerId: PeerId
298
- ): proto_lightpush_v3.LightpushResponse {
299
- const bytes = rawResponse.reduce((acc, chunk) => {
300
- acc.append(chunk);
301
- return acc;
302
- }, new Uint8ArrayList());
303
-
304
- try {
305
- const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
306
- if (!response) {
307
- log.error("Remote peer fault: No response received");
308
- throw new LightPushError(ProtocolError.NO_RESPONSE, peerId);
309
- }
310
- return response;
311
- } catch (err) {
312
- if (err instanceof LightPushError) {
313
- throw err;
314
- }
315
- log.error("Failed to decode push response", err);
316
- throw new LightPushError(ProtocolError.DECODE_FAILED, peerId);
317
- }
318
- }
319
- }
@@ -1,45 +0,0 @@
1
- import { proto_lightpush_v3, WakuMessage } from "@waku/proto";
2
- import type { Uint8ArrayList } from "uint8arraylist";
3
- import { v4 as uuid } from "uuid";
4
-
5
- export class PushRpcV3 {
6
- public request?: proto_lightpush_v3.LightpushRequest;
7
- public response?: proto_lightpush_v3.LightpushResponse;
8
-
9
- private constructor(
10
- request?: proto_lightpush_v3.LightpushRequest,
11
- response?: proto_lightpush_v3.LightpushResponse
12
- ) {
13
- this.request = request;
14
- this.response = response;
15
- }
16
-
17
- public static createRequest(
18
- message: WakuMessage,
19
- pubsubTopic?: string
20
- ): PushRpcV3 {
21
- const request: proto_lightpush_v3.LightpushRequest = {
22
- requestId: uuid(),
23
- message: message,
24
- ...(pubsubTopic && { pubsubTopic })
25
- };
26
-
27
- return new PushRpcV3(request, undefined);
28
- }
29
-
30
- public static decode(bytes: Uint8ArrayList | Uint8Array): PushRpcV3 {
31
- const response = proto_lightpush_v3.LightpushResponse.decode(bytes);
32
- return new PushRpcV3(undefined, response);
33
- }
34
-
35
- public encode(): Uint8Array {
36
- if (!this.request) {
37
- throw new Error("Cannot encode without a request");
38
- }
39
- return proto_lightpush_v3.LightpushRequest.encode(this.request);
40
- }
41
-
42
- public get query(): proto_lightpush_v3.LightpushRequest | undefined {
43
- return this.request;
44
- }
45
- }