chore(dogfooding): add deploy folder and remove telemetry (#128)

This commit is contained in:
Sasha 2025-04-18 17:00:18 +02:00 committed by GitHub
parent d51ed4ce55
commit 5c7c09aedf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 14 additions and 171 deletions

View File

@ -2,29 +2,17 @@ import {
createLightNode,
DecodedMessage,
LightNode,
utils,
} from "@waku/sdk";
import { generateKeyPairFromSeed } from "@libp2p/crypto/keys";
import { fromString } from "uint8arrays";
import { Type, Field } from "protobufjs";
import {
TelemetryClient,
TelemetryPushFilter,
TelemetryType,
} from "./telemetry_client";
import {
generateRandomNumber,
sha256,
buildExtraData,
} from "./util";
const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8";
const DEFAULT_PUBSUB_TOPIC = utils.contentTopicToPubsubTopic(
DEFAULT_CONTENT_TOPIC
);
const TELEMETRY_URL =
process.env.TELEMETRY_URL || "http://localhost:8080/waku-metrics";
const ProtoSequencedMessage = new Type("SequencedMessage")
.add(new Field("hash", 1, "string"))
@ -33,7 +21,6 @@ const ProtoSequencedMessage = new Type("SequencedMessage")
.add(new Field("sender", 4, "string"));
const sequenceCompletedEvent = new CustomEvent("sequenceCompleted");
const messageSentEvent = new CustomEvent("messageSent");
const messageReceivedEvent = new CustomEvent("messageReceived");
async function wakuNode(): Promise<LightNode> {
@ -67,7 +54,7 @@ async function wakuNode(): Promise<LightNode> {
return node;
}
export async function app(telemetryClient: TelemetryClient) {
export async function app() {
const node = await wakuNode();
console.log("DEBUG: your peer ID is:", node.libp2p.peerId.toString());
@ -80,31 +67,6 @@ export async function app(telemetryClient: TelemetryClient) {
contentTopic: DEFAULT_CONTENT_TOPIC
});
node.libp2p.addEventListener("peer:discovery", async (event) => {
const discoveredPeerId = event.detail.id.toString();
const timestamp = Math.floor(new Date().getTime() / 1000);
const extraData = await buildExtraData(node, discoveredPeerId);
const hash = await sha256(`${peerId}-${discoveredPeerId}-${timestamp}`);
telemetryClient.push<TelemetryPushFilter>([
{
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "discovery",
timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId,
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: hash,
errorMessage: "",
extraData,
},
]);
});
const startLightPushSequence = async (
numMessages: number,
period: number = 3000
@ -171,8 +133,6 @@ export async function app(telemetryClient: TelemetryClient) {
return;
}
const timestamp = Math.floor(new Date().getTime() / 1000);
const messageElement = document.createElement("div");
messageElement.textContent = `Message: ${decodedMessage.hash}`;
document.dispatchEvent(messageReceivedEvent);
@ -189,10 +149,7 @@ export async function app(telemetryClient: TelemetryClient) {
}
(async () => {
const telemetryClient = new TelemetryClient(TELEMETRY_URL, 5000);
const { node, startLightPushSequence, startFilterSubscription } = await app(
telemetryClient
);
const { startLightPushSequence, startFilterSubscription } = await app();
startFilterSubscription();

View File

@ -1,85 +0,0 @@
export enum TelemetryType {
LIGHT_PUSH_FILTER = "LightPushFilter",
}
interface TelemetryMessage {
type: string;
timestamp: number;
contentTopic: string;
pubsubTopic: string;
peerId: string;
errorMessage: string;
extraData: string;
}
export interface TelemetryPushFilter extends TelemetryMessage {
type: "LightPushFilter",
protocol: string;
ephemeral: boolean;
seenTimestamp: number;
createdAt: number;
messageHash: string;
}
export class TelemetryClient {
constructor(
private readonly url: string,
private intervalPeriod: number = 5000
) {}
private queue: TelemetryMessage[] = [];
private intervalId: NodeJS.Timeout | null = null;
private requestId = 0;
public push<T extends TelemetryMessage>(messages: T[]) {
this.queue.push(...messages);
}
public async start() {
if (!this.intervalId) {
this.intervalId = setInterval(async () => {
if (this.queue.length > 0) {
const success = await this.send(this.queue);
if (success) {
console.log("Sent ", this.queue.length, " telemetry logs");
this.queue = [];
}
}
}, this.intervalPeriod);
}
}
public stop() {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
}
private async send<T extends TelemetryMessage>(messages: T[]) {
if (!window.location.hostname.includes("lab.waku.org")) {
return;
}
const telemetryRequests = messages.map((message) => ({
id: ++this.requestId,
telemetryType: message.type.toString(),
telemetryData: message
}));
try {
const res = await fetch(this.url, {
method: "POST",
body: JSON.stringify(telemetryRequests),
});
if (res.status !== 201) {
console.log("DEBUG: Error sending messages to telemetry service: ", res.status, res.statusText, res.json);
return false
}
return true;
} catch (e) {
console.log("DEBUG: Error sending messages to telemetry service", e);
return false;
}
}
}

View File

@ -1,38 +1,12 @@
import type { LightNode } from "@waku/sdk";
export const generateRandomNumber = (): number => {
return Math.floor(Math.random() * 1000000);
};
return Math.floor(Math.random() * 1000000);
};
export const sha256 = async (number: number | string ): Promise<string> => {
const encoder = new TextEncoder();
const data = encoder.encode(number.toString());
const buffer = await crypto.subtle.digest("SHA-256", data);
return Array.from(new Uint8Array(buffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
};
const DEFAULT_EXTRA_DATA = { sdk: "0.0.29-rc" };
export const DEFAULT_EXTRA_DATA_STR = JSON.stringify(DEFAULT_EXTRA_DATA);
export const buildExtraData = async (node: LightNode, peerId: string): Promise<string> => {
let extraData = { ...DEFAULT_EXTRA_DATA };
const peer = (await node.libp2p.peerStore.all()).find(p => p.id.toString() === peerId);
if (!peer || peerId === node.libp2p.peerId.toString()) {
return JSON.stringify(extraData);
}
const websocket = peer
.addresses
.map(addr => addr.multiaddr.toString())
.some(addr => addr.includes("ws") || addr.includes("wss"));
return JSON.stringify({
...extraData,
peerId,
websocket,
enabledProtocols: peer.protocols,
});
const encoder = new TextEncoder();
const data = encoder.encode(number.toString());
const buffer = await crypto.subtle.digest("SHA-256", data);
return Array.from(new Uint8Array(buffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
};

View File

@ -1,12 +1,12 @@
const CopyWebpackPlugin = require("copy-webpack-plugin");
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: "./src/index.ts", // Changed from index.js to index.ts
output: {
path: path.resolve(__dirname, "build"),
filename: "index.js",
publicPath: process.env.NODE_ENV === 'production' ? '/dogfooding/' : '/'
},
experiments: {
asyncWebAssembly: true,
@ -31,9 +31,6 @@ module.exports = {
plugins: [
new CopyWebpackPlugin({
patterns: ["index.html", "favicon.ico", "favicon.png", "manifest.json"],
}),
new webpack.DefinePlugin({
'process.env.TELEMETRY_URL': JSON.stringify(process.env.TELEMETRY_URL || "https://telemetry.status.im/waku-metrics")
}),
})
],
};
};