node-red-contrib-nostr
Version:
Node-RED nodes for seamless Nostr protocol integration. Features robust WebSocket handling, event filtering, and NPUB-based routing. Built with TypeScript for type safety and extensive testing. Perfect for Nostr automation flows.
45 lines (44 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
function default_1(RED) {
class NostrRelayNode {
constructor(config) {
// Initialize the node
RED.nodes.createNode(this, config);
// Get config node
this.config = RED.nodes.getNode(config.config);
if (!this.config) {
this.error("No relay config");
return;
}
// Set up message handler
const handleMessage = (msg) => {
this.send({ payload: msg });
};
// Listen on the config node's event emitter (relayed from WS callbacks)
this.config.on('message', handleMessage);
// Clean up on close
this.on('close', (done) => {
this.config.removeListener('message', handleMessage);
done();
});
// Handle input messages
this.on('input', async (msg, send, done) => {
if (this.config._ws) {
try {
await this.config._ws.sendMessage(msg.payload);
done();
}
catch (err) {
done(err);
}
}
else {
done(new Error("WebSocket not connected"));
}
});
}
}
RED.nodes.registerType("nostr-relay", NostrRelayNode);
}