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.
50 lines (49 loc) • 1.64 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 });
};
// Add message handler to config node
if (this.config._ws) {
this.config._ws.on('message', handleMessage);
}
// Clean up on close
this.on('close', (done) => {
// Remove message handler
if (this.config._ws) {
this.config._ws.off('message', handleMessage);
}
done();
});
// Handle input messages
this.on('input', (msg, send, done) => {
if (this.config._ws) {
try {
this.config._ws.send(msg.payload);
done();
}
catch (err) {
done(err);
}
}
else {
done(new Error("WebSocket not connected"));
}
});
}
}
RED.nodes.registerType("nostr-relay", NostrRelayNode);
}