What is Nostr?
nakamoto /
npub1g6m…4yk9
2022-12-21 01:02:57
in reply to nevent1q…dghx

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}`
Author Public Key
npub1g6m9kx58s7myyzug9cynft2dhp0zqa6hmw8vadsxywph3ng7wxtsuw4yk9