UNPKG

@energyweb/node-red-contrib-green-proof-worker

Version:

## Peer dependencies

64 lines (63 loc) 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SourceHttpApi = void 0; const zod_1 = require("zod"); const errors_1 = require("../errors"); const source_http_api_http_ack_node_1 = require("../source-http-api-http-ack/source-http-api-http-ack.node"); const Config = zod_1.z.object({ sqliteConfig: zod_1.z.string().optional(), }); /** * Self-acking HTTP source API, using configured SQLite config node or trying to find it in all nodes */ const SourceHttpApi = (api) => class SourceHttpApiSelfAck extends (0, source_http_api_http_ack_node_1.SourceHttpApiHttpAck)(api) { constructor(config) { super(config); // To avoid introducing breaking change // we replaced kafka-proxy-ack node with this self-ack node // because we don't want to change existing flow uploaded online // However, this self-ack logic has dependency to sqliteConfig, and that dependency is in flow.json // We don't want to require flow update, so we need to figure out how to get access to that sqlite node instance. // HOPEFULLY there is only one sqlite-config -> currently we know, that there is rather one // It all wouldn't be an issue, if flow could define marketplace version it depends on (hence version of node-red and this library) const fallbackSqliteNodeId = this.api.findNodeIdByType('sqlite-config'); const parsedConfig = Config.parse(config); const configNode = this.api.getNode(parsedConfig.sqliteConfig || fallbackSqliteNodeId || ''); if (!configNode) { throw new errors_1.GGPError(errors_1.ErrorCode.SqliteConfigNotFound, {}); } this.database = configNode.database; } async buildGetMessageUrl() { const config = await this.config; const url = new URL('/api/message', config.host); url.searchParams.set('namespace', config.namespace); url.searchParams.set('workerId', config.workerId); const lastAckedMessageId = await this.getLastAckedMessageId(); if (lastAckedMessageId) { url.searchParams.set('lastMessageId', lastAckedMessageId); } return url; } async getLastAckedMessageId() { const database = await this.database; const lastAcked = await database .selectFrom('acked_message') .select('messageId') .orderBy('created_at desc') .limit(1) .executeTakeFirst(); return lastAcked ? lastAcked.messageId : null; } async ackMessage(messageId) { const database = await this.database; await database .insertInto('acked_message') .values({ messageId, created_at: Date.now(), }) .execute(); } }; exports.SourceHttpApi = SourceHttpApi;