nakamoto on Nostr: ``` import * as WebSocket from "ws"; if (process.argv.length < 3) { ...
```
import * as WebSocket from "ws";
if (process.argv.length < 3) {
console.log("Source server required");
process.exit(1);
}
const sourceServerUrl = process.argv[2];
const destinationServerUrl = "wss://satstacker.cloud";
const sourceServer = new WebSocket(sourceServerUrl);
const destinationServer = new WebSocket(destinationServerUrl);
(async () => {
await Promise.all([
new Promise((res) => {
sourceServer.on("open", res);
}),
new Promise((res) => {
destinationServer.on("open", res);
}),
]);
sourceServer.send(JSON.stringify(["REQ", "cid", { limit: 1 }]));
sourceServer.on("message", (data: WebSocket.Data) => {
const rawString = data.toString("utf-8");
try {
const parsed = JSON.parse(rawString) as any[];
if (!(parsed[2].kind < 50)) return;
parsed.splice(1, 1);
console.log("Forwarding: ", parsed);
destinationServer.send(JSON.stringify(parsed));
} catch (e) {
console.error("Failed to parse.", e);
}
});
destinationServer.on("error", (error: Error) => {
console.error(`Error forwarding message: ${error.message}`);
});
})();
```
`ts-node index.ts {SOURCE_RELAY_HERE}`
import * as WebSocket from "ws";
if (process.argv.length < 3) {
console.log("Source server required");
process.exit(1);
}
const sourceServerUrl = process.argv[2];
const destinationServerUrl = "wss://satstacker.cloud";
const sourceServer = new WebSocket(sourceServerUrl);
const destinationServer = new WebSocket(destinationServerUrl);
(async () => {
await Promise.all([
new Promise((res) => {
sourceServer.on("open", res);
}),
new Promise((res) => {
destinationServer.on("open", res);
}),
]);
sourceServer.send(JSON.stringify(["REQ", "cid", { limit: 1 }]));
sourceServer.on("message", (data: WebSocket.Data) => {
const rawString = data.toString("utf-8");
try {
const parsed = JSON.parse(rawString) as any[];
if (!(parsed[2].kind < 50)) return;
parsed.splice(1, 1);
console.log("Forwarding: ", parsed);
destinationServer.send(JSON.stringify(parsed));
} catch (e) {
console.error("Failed to parse.", e);
}
});
destinationServer.on("error", (error: Error) => {
console.error(`Error forwarding message: ${error.message}`);
});
})();
```
`ts-node index.ts {SOURCE_RELAY_HERE}`