mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-13 08:04:06 -08:00
027dfb2f0a
* ⚡ Enable `esModuleInterop` for /core * ⚡ Adjust imports in /core * ⚡ Enable `esModuleInterop` for /cli * ⚡ Adjust imports in /cli * ⚡ Enable `esModuleInterop` for /nodes-base * ⚡ Adjust imports in /nodes-base * ⚡ Make imports consistent * ⬆️ Upgrade TypeScript to 4.6 (#3109) * ⬆️ Upgrade TypeScript to 4.6 * 📦 Update package-lock.json * 🔧 Avoid erroring on untyped errors * 📘 Fix type error Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
import EventSource from 'eventsource';
|
|
import { ITriggerFunctions } from 'n8n-core';
|
|
import {
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
ITriggerResponse,
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
export class SseTrigger implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'SSE Trigger',
|
|
name: 'sseTrigger',
|
|
icon: 'fa:cloud-download-alt',
|
|
group: ['trigger'],
|
|
version: 1,
|
|
description: 'Triggers the workflow when Server-Sent Events occur',
|
|
eventTriggerDescription: '',
|
|
activationMessage: 'You can now make calls to your SSE URL to trigger executions.',
|
|
defaults: {
|
|
name: 'SSE Trigger',
|
|
color: '#225577',
|
|
},
|
|
inputs: [],
|
|
outputs: ['main'],
|
|
properties: [
|
|
{
|
|
displayName: 'URL',
|
|
name: 'url',
|
|
type: 'string',
|
|
default: '',
|
|
placeholder: 'http://example.com',
|
|
description: 'The URL to receive the SSE from.',
|
|
required: true,
|
|
},
|
|
],
|
|
};
|
|
|
|
|
|
async trigger(this: ITriggerFunctions): Promise<ITriggerResponse> {
|
|
const url = this.getNodeParameter('url') as string;
|
|
|
|
const eventSource = new EventSource(url);
|
|
|
|
eventSource.onmessage = (event) => {
|
|
const eventData = JSON.parse(event.data);
|
|
this.emit([this.helpers.returnJsonArray([eventData])]);
|
|
};
|
|
|
|
async function closeFunction() {
|
|
eventSource.close();
|
|
}
|
|
|
|
return {
|
|
closeFunction,
|
|
};
|
|
|
|
}
|
|
}
|