@flowcore/pathways 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +420 -0
  3. package/esm/common/flowcore.type.d.ts +11 -0
  4. package/esm/common/flowcore.type.d.ts.map +1 -0
  5. package/esm/common/flowcore.type.js +1 -0
  6. package/esm/common/index.d.ts +7 -0
  7. package/esm/common/index.d.ts.map +1 -0
  8. package/esm/common/index.js +6 -0
  9. package/esm/compatibility/flowcore-transformer-core.sdk.d.ts +16 -0
  10. package/esm/compatibility/flowcore-transformer-core.sdk.d.ts.map +1 -0
  11. package/esm/compatibility/flowcore-transformer-core.sdk.js +11 -0
  12. package/esm/contracts/event.d.ts +20 -0
  13. package/esm/contracts/event.d.ts.map +1 -0
  14. package/esm/contracts/event.js +15 -0
  15. package/esm/contracts/index.d.ts +5 -0
  16. package/esm/contracts/index.d.ts.map +1 -0
  17. package/esm/contracts/index.js +4 -0
  18. package/esm/mod.d.ts +14 -0
  19. package/esm/mod.d.ts.map +1 -0
  20. package/esm/mod.js +13 -0
  21. package/esm/package.json +3 -0
  22. package/esm/pathways/builder.d.ts +91 -0
  23. package/esm/pathways/builder.d.ts.map +1 -0
  24. package/esm/pathways/builder.js +530 -0
  25. package/esm/pathways/index.d.ts +17 -0
  26. package/esm/pathways/index.d.ts.map +1 -0
  27. package/esm/pathways/index.js +16 -0
  28. package/esm/pathways/internal-pathway.state.d.ts +41 -0
  29. package/esm/pathways/internal-pathway.state.d.ts.map +1 -0
  30. package/esm/pathways/internal-pathway.state.js +64 -0
  31. package/esm/pathways/kv/bun-kv-adapter.d.ts +36 -0
  32. package/esm/pathways/kv/bun-kv-adapter.d.ts.map +1 -0
  33. package/esm/pathways/kv/bun-kv-adapter.js +47 -0
  34. package/esm/pathways/kv/kv-adapter.d.ts +34 -0
  35. package/esm/pathways/kv/kv-adapter.d.ts.map +1 -0
  36. package/esm/pathways/kv/kv-adapter.js +19 -0
  37. package/esm/pathways/kv/node-kv-adapter.d.ts +33 -0
  38. package/esm/pathways/kv/node-kv-adapter.d.ts.map +1 -0
  39. package/esm/pathways/kv/node-kv-adapter.js +44 -0
  40. package/esm/pathways/logger.d.ts +48 -0
  41. package/esm/pathways/logger.d.ts.map +1 -0
  42. package/esm/pathways/logger.js +26 -0
  43. package/esm/pathways/postgres/index.d.ts +9 -0
  44. package/esm/pathways/postgres/index.d.ts.map +1 -0
  45. package/esm/pathways/postgres/index.js +8 -0
  46. package/esm/pathways/postgres/postgres-adapter.d.ts +112 -0
  47. package/esm/pathways/postgres/postgres-adapter.d.ts.map +1 -0
  48. package/esm/pathways/postgres/postgres-adapter.js +113 -0
  49. package/esm/pathways/postgres/postgres-pathway-state.d.ts +113 -0
  50. package/esm/pathways/postgres/postgres-pathway-state.d.ts.map +1 -0
  51. package/esm/pathways/postgres/postgres-pathway-state.js +188 -0
  52. package/esm/pathways/types.d.ts +87 -0
  53. package/esm/pathways/types.d.ts.map +1 -0
  54. package/esm/pathways/types.js +1 -0
  55. package/esm/router/index.d.ts +35 -0
  56. package/esm/router/index.d.ts.map +1 -0
  57. package/esm/router/index.js +96 -0
  58. package/package.json +38 -0
  59. package/script/common/flowcore.type.d.ts +11 -0
  60. package/script/common/flowcore.type.d.ts.map +1 -0
  61. package/script/common/flowcore.type.js +2 -0
  62. package/script/common/index.d.ts +7 -0
  63. package/script/common/index.d.ts.map +1 -0
  64. package/script/common/index.js +22 -0
  65. package/script/compatibility/flowcore-transformer-core.sdk.d.ts +16 -0
  66. package/script/compatibility/flowcore-transformer-core.sdk.d.ts.map +1 -0
  67. package/script/compatibility/flowcore-transformer-core.sdk.js +17 -0
  68. package/script/contracts/event.d.ts +20 -0
  69. package/script/contracts/event.d.ts.map +1 -0
  70. package/script/contracts/event.js +18 -0
  71. package/script/contracts/index.d.ts +5 -0
  72. package/script/contracts/index.d.ts.map +1 -0
  73. package/script/contracts/index.js +20 -0
  74. package/script/mod.d.ts +14 -0
  75. package/script/mod.d.ts.map +1 -0
  76. package/script/mod.js +29 -0
  77. package/script/package.json +3 -0
  78. package/script/pathways/builder.d.ts +91 -0
  79. package/script/pathways/builder.d.ts.map +1 -0
  80. package/script/pathways/builder.js +534 -0
  81. package/script/pathways/index.d.ts +17 -0
  82. package/script/pathways/index.d.ts.map +1 -0
  83. package/script/pathways/index.js +32 -0
  84. package/script/pathways/internal-pathway.state.d.ts +41 -0
  85. package/script/pathways/internal-pathway.state.d.ts.map +1 -0
  86. package/script/pathways/internal-pathway.state.js +68 -0
  87. package/script/pathways/kv/bun-kv-adapter.d.ts +36 -0
  88. package/script/pathways/kv/bun-kv-adapter.d.ts.map +1 -0
  89. package/script/pathways/kv/bun-kv-adapter.js +51 -0
  90. package/script/pathways/kv/kv-adapter.d.ts +34 -0
  91. package/script/pathways/kv/kv-adapter.d.ts.map +1 -0
  92. package/script/pathways/kv/kv-adapter.js +45 -0
  93. package/script/pathways/kv/node-kv-adapter.d.ts +33 -0
  94. package/script/pathways/kv/node-kv-adapter.d.ts.map +1 -0
  95. package/script/pathways/kv/node-kv-adapter.js +51 -0
  96. package/script/pathways/logger.d.ts +48 -0
  97. package/script/pathways/logger.d.ts.map +1 -0
  98. package/script/pathways/logger.js +31 -0
  99. package/script/pathways/postgres/index.d.ts +9 -0
  100. package/script/pathways/postgres/index.d.ts.map +1 -0
  101. package/script/pathways/postgres/index.js +24 -0
  102. package/script/pathways/postgres/postgres-adapter.d.ts +112 -0
  103. package/script/pathways/postgres/postgres-adapter.d.ts.map +1 -0
  104. package/script/pathways/postgres/postgres-adapter.js +141 -0
  105. package/script/pathways/postgres/postgres-pathway-state.d.ts +113 -0
  106. package/script/pathways/postgres/postgres-pathway-state.d.ts.map +1 -0
  107. package/script/pathways/postgres/postgres-pathway-state.js +193 -0
  108. package/script/pathways/types.d.ts +87 -0
  109. package/script/pathways/types.d.ts.map +1 -0
  110. package/script/pathways/types.js +2 -0
  111. package/script/router/index.d.ts +35 -0
  112. package/script/router/index.d.ts.map +1 -0
  113. package/script/router/index.js +100 -0
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.PostgresJsAdapter = void 0;
27
+ exports.createPostgresAdapter = createPostgresAdapter;
28
+ /**
29
+ * Implementation of PostgresAdapter using the postgres.js library
30
+ *
31
+ * @implements {PostgresAdapter}
32
+ */
33
+ class PostgresJsAdapter {
34
+ /**
35
+ * Creates a new PostgresJsAdapter instance
36
+ *
37
+ * @param {PostgresConfig} config The PostgreSQL connection configuration
38
+ */
39
+ constructor(config) {
40
+ /** The postgres.js client factory function */
41
+ Object.defineProperty(this, "postgres", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: null
46
+ });
47
+ /** The active postgres.js client */
48
+ Object.defineProperty(this, "sql", {
49
+ enumerable: true,
50
+ configurable: true,
51
+ writable: true,
52
+ value: null
53
+ });
54
+ /** The PostgreSQL configuration */
55
+ Object.defineProperty(this, "config", {
56
+ enumerable: true,
57
+ configurable: true,
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ /** The connection string built from the configuration */
62
+ Object.defineProperty(this, "connectionString", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: void 0
67
+ });
68
+ this.config = config;
69
+ this.connectionString = `postgres://${config.user}:${config.password}@${config.host}:${config.port}/${config.database}`;
70
+ if (config.ssl) {
71
+ this.connectionString += "?sslmode=require";
72
+ }
73
+ }
74
+ /**
75
+ * Establishes a connection to the PostgreSQL database
76
+ *
77
+ * @returns {Promise<void>}
78
+ * @throws {Error} If connection fails
79
+ */
80
+ async connect() {
81
+ try {
82
+ const module = await Promise.resolve().then(() => __importStar(require("postgres")));
83
+ this.postgres = module.default;
84
+ this.sql = this.postgres(this.connectionString);
85
+ }
86
+ catch (error) {
87
+ console.error("Failed to connect to PostgreSQL:", error);
88
+ throw error;
89
+ }
90
+ }
91
+ /**
92
+ * Closes the connection to the PostgreSQL database
93
+ *
94
+ * @returns {Promise<void>}
95
+ */
96
+ async disconnect() {
97
+ if (this.sql) {
98
+ await this.sql.end();
99
+ this.sql = null;
100
+ }
101
+ }
102
+ /**
103
+ * Executes a SQL query and returns the results
104
+ *
105
+ * @template T The expected result type
106
+ * @param {string} sql The SQL query to execute
107
+ * @param {unknown[]} [params=[]] Optional parameters for the query
108
+ * @returns {Promise<T>} The query results
109
+ */
110
+ async query(sql, params = []) {
111
+ if (!this.sql) {
112
+ await this.connect();
113
+ }
114
+ return await this.sql.unsafe(sql, params);
115
+ }
116
+ /**
117
+ * Executes a SQL statement without returning results
118
+ *
119
+ * @param {string} sql The SQL statement to execute
120
+ * @param {unknown[]} [params=[]] Optional parameters for the statement
121
+ * @returns {Promise<void>}
122
+ */
123
+ async execute(sql, params = []) {
124
+ if (!this.sql) {
125
+ await this.connect();
126
+ }
127
+ await this.sql.unsafe(sql, params);
128
+ }
129
+ }
130
+ exports.PostgresJsAdapter = PostgresJsAdapter;
131
+ /**
132
+ * Creates and initializes a PostgreSQL adapter
133
+ *
134
+ * @param {PostgresConfig} config The PostgreSQL connection configuration
135
+ * @returns {Promise<PostgresAdapter>} An initialized PostgreSQL adapter
136
+ */
137
+ async function createPostgresAdapter(config) {
138
+ const adapter = new PostgresJsAdapter(config);
139
+ await adapter.connect();
140
+ return adapter;
141
+ }
@@ -0,0 +1,113 @@
1
+ import type { PathwayState } from "../types.js";
2
+ /**
3
+ * Configuration options for PostgreSQL pathway state storage
4
+ *
5
+ * @interface PostgresPathwayStateConfig
6
+ */
7
+ export interface PostgresPathwayStateConfig {
8
+ /** PostgreSQL server hostname */
9
+ host: string;
10
+ /** PostgreSQL server port */
11
+ port: number;
12
+ /** PostgreSQL username */
13
+ user: string;
14
+ /** PostgreSQL password */
15
+ password: string;
16
+ /** PostgreSQL database name */
17
+ database: string;
18
+ /** Whether to use SSL for the connection */
19
+ ssl?: boolean;
20
+ /** Table name for storing pathway state (default: "pathway_state") */
21
+ tableName?: string;
22
+ /** Time-to-live in milliseconds for processed events (default: 5 minutes) */
23
+ ttlMs?: number;
24
+ }
25
+ /**
26
+ * Implementation of PathwayState that uses PostgreSQL for storage
27
+ *
28
+ * This class provides persistent storage of pathway state using a PostgreSQL database,
29
+ * which allows for state to be shared across multiple instances of the application.
30
+ *
31
+ * @implements {PathwayState}
32
+ */
33
+ export declare class PostgresPathwayState implements PathwayState {
34
+ private config;
35
+ /**
36
+ * Default time-to-live for processed event records (5 minutes)
37
+ * @private
38
+ */
39
+ private static readonly DEFAULT_TTL_MS;
40
+ /**
41
+ * Default table name for storing pathway state
42
+ * @private
43
+ */
44
+ private static readonly DEFAULT_TABLE_NAME;
45
+ /**
46
+ * The PostgreSQL adapter instance
47
+ * @private
48
+ */
49
+ private postgres;
50
+ /**
51
+ * The table name for storing pathway state
52
+ * @private
53
+ */
54
+ private tableName;
55
+ /**
56
+ * Time-to-live in milliseconds for processed events
57
+ * @private
58
+ */
59
+ private ttlMs;
60
+ /**
61
+ * Whether the database has been initialized
62
+ * @private
63
+ */
64
+ private initialized;
65
+ /**
66
+ * Creates a new PostgresPathwayState instance
67
+ *
68
+ * @param {PostgresPathwayStateConfig} config The PostgreSQL configuration
69
+ */
70
+ constructor(config: PostgresPathwayStateConfig);
71
+ /**
72
+ * Initializes the PostgreSQL connection and creates the necessary table and index
73
+ *
74
+ * @private
75
+ * @returns {Promise<void>}
76
+ */
77
+ private initialize;
78
+ /**
79
+ * Checks if an event has already been processed
80
+ *
81
+ * @param {string} eventId - The ID of the event to check
82
+ * @returns {Promise<boolean>} True if the event has been processed, false otherwise
83
+ */
84
+ isProcessed(eventId: string): Promise<boolean>;
85
+ /**
86
+ * Marks an event as processed
87
+ *
88
+ * @param {string} eventId - The ID of the event to mark as processed
89
+ * @returns {Promise<void>}
90
+ */
91
+ setProcessed(eventId: string): Promise<void>;
92
+ /**
93
+ * Removes expired event records from the database
94
+ *
95
+ * @private
96
+ * @returns {Promise<void>}
97
+ */
98
+ private cleanupExpired;
99
+ /**
100
+ * Closes the PostgreSQL connection
101
+ *
102
+ * @returns {Promise<void>}
103
+ */
104
+ close(): Promise<void>;
105
+ }
106
+ /**
107
+ * Creates a new PostgreSQL pathway state instance
108
+ *
109
+ * @param {PostgresPathwayStateConfig} config The PostgreSQL configuration
110
+ * @returns {PostgresPathwayState} A new PostgresPathwayState instance
111
+ */
112
+ export declare function createPostgresPathwayState(config: PostgresPathwayStateConfig): PostgresPathwayState;
113
+ //# sourceMappingURL=postgres-pathway-state.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postgres-pathway-state.d.ts","sourceRoot":"","sources":["../../../src/pathways/postgres/postgres-pathway-state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIhD;;;;GAIG;AACH,MAAM,WAAW,0BAA0B;IACzC,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,6BAA6B;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,0BAA0B;IAC1B,QAAQ,EAAE,MAAM,CAAC;IACjB,+BAA+B;IAC/B,QAAQ,EAAE,MAAM,CAAC;IACjB,4CAA4C;IAC5C,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,sEAAsE;IACtE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6EAA6E;IAC7E,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;GAOG;AACH,qBAAa,oBAAqB,YAAW,YAAY;IA0C3C,OAAO,CAAC,MAAM;IAzC1B;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAAiB;IAEvD;;;OAGG;IACH,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAAmB;IAE7D;;;OAGG;IACH,OAAO,CAAC,QAAQ,CAAkB;IAElC;;;OAGG;IACH,OAAO,CAAC,SAAS,CAAS;IAE1B;;;OAGG;IACH,OAAO,CAAC,KAAK,CAAS;IAEtB;;;OAGG;IACH,OAAO,CAAC,WAAW,CAAS;IAE5B;;;;OAIG;gBACiB,MAAM,EAAE,0BAA0B;IAMtD;;;;;OAKG;YACW,UAAU;IAgCxB;;;;;OAKG;IACG,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAcpD;;;;;OAKG;IACG,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAelD;;;;;OAKG;YACW,cAAc;IAQ5B;;;;OAIG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAK7B;AAED;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,0BAA0B,GAAG,oBAAoB,CAGnG"}
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PostgresPathwayState = void 0;
4
+ exports.createPostgresPathwayState = createPostgresPathwayState;
5
+ const postgres_adapter_js_1 = require("./postgres-adapter.js");
6
+ /**
7
+ * Implementation of PathwayState that uses PostgreSQL for storage
8
+ *
9
+ * This class provides persistent storage of pathway state using a PostgreSQL database,
10
+ * which allows for state to be shared across multiple instances of the application.
11
+ *
12
+ * @implements {PathwayState}
13
+ */
14
+ class PostgresPathwayState {
15
+ /**
16
+ * Creates a new PostgresPathwayState instance
17
+ *
18
+ * @param {PostgresPathwayStateConfig} config The PostgreSQL configuration
19
+ */
20
+ constructor(config) {
21
+ Object.defineProperty(this, "config", {
22
+ enumerable: true,
23
+ configurable: true,
24
+ writable: true,
25
+ value: config
26
+ });
27
+ /**
28
+ * The PostgreSQL adapter instance
29
+ * @private
30
+ */
31
+ Object.defineProperty(this, "postgres", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ /**
38
+ * The table name for storing pathway state
39
+ * @private
40
+ */
41
+ Object.defineProperty(this, "tableName", {
42
+ enumerable: true,
43
+ configurable: true,
44
+ writable: true,
45
+ value: void 0
46
+ });
47
+ /**
48
+ * Time-to-live in milliseconds for processed events
49
+ * @private
50
+ */
51
+ Object.defineProperty(this, "ttlMs", {
52
+ enumerable: true,
53
+ configurable: true,
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ /**
58
+ * Whether the database has been initialized
59
+ * @private
60
+ */
61
+ Object.defineProperty(this, "initialized", {
62
+ enumerable: true,
63
+ configurable: true,
64
+ writable: true,
65
+ value: false
66
+ });
67
+ this.tableName = config.tableName || PostgresPathwayState.DEFAULT_TABLE_NAME;
68
+ this.ttlMs = config.ttlMs || PostgresPathwayState.DEFAULT_TTL_MS;
69
+ this.postgres = null;
70
+ }
71
+ /**
72
+ * Initializes the PostgreSQL connection and creates the necessary table and index
73
+ *
74
+ * @private
75
+ * @returns {Promise<void>}
76
+ */
77
+ async initialize() {
78
+ if (this.initialized) {
79
+ return;
80
+ }
81
+ this.postgres = await (0, postgres_adapter_js_1.createPostgresAdapter)({
82
+ host: this.config.host,
83
+ port: this.config.port,
84
+ user: this.config.user,
85
+ password: this.config.password,
86
+ database: this.config.database,
87
+ ssl: this.config.ssl,
88
+ });
89
+ // Create table if it doesn't exist
90
+ await this.postgres.execute(`
91
+ CREATE TABLE IF NOT EXISTS ${this.tableName} (
92
+ event_id TEXT PRIMARY KEY,
93
+ processed BOOLEAN NOT NULL,
94
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
95
+ expires_at TIMESTAMP WITH TIME ZONE NOT NULL
96
+ )
97
+ `);
98
+ // Create index on expires_at to help with cleanup
99
+ await this.postgres.execute(`
100
+ CREATE INDEX IF NOT EXISTS ${this.tableName}_expires_at_idx ON ${this.tableName} (expires_at)
101
+ `);
102
+ this.initialized = true;
103
+ }
104
+ /**
105
+ * Checks if an event has already been processed
106
+ *
107
+ * @param {string} eventId - The ID of the event to check
108
+ * @returns {Promise<boolean>} True if the event has been processed, false otherwise
109
+ */
110
+ async isProcessed(eventId) {
111
+ await this.initialize();
112
+ // Clean up expired entries
113
+ await this.cleanupExpired();
114
+ const result = await this.postgres.query(`
115
+ SELECT processed FROM ${this.tableName}
116
+ WHERE event_id = $1 AND expires_at > NOW()
117
+ `, [eventId]);
118
+ return result.length > 0 && result[0].processed;
119
+ }
120
+ /**
121
+ * Marks an event as processed
122
+ *
123
+ * @param {string} eventId - The ID of the event to mark as processed
124
+ * @returns {Promise<void>}
125
+ */
126
+ async setProcessed(eventId) {
127
+ await this.initialize();
128
+ // Insert or update the event state
129
+ // Using ON CONFLICT to handle the case where the event is already in the table
130
+ await this.postgres.execute(`
131
+ INSERT INTO ${this.tableName} (event_id, processed, expires_at)
132
+ VALUES ($1, TRUE, NOW() + interval '${Math.floor(this.ttlMs / 1000)} seconds')
133
+ ON CONFLICT (event_id)
134
+ DO UPDATE SET
135
+ processed = TRUE,
136
+ expires_at = NOW() + interval '${Math.floor(this.ttlMs / 1000)} seconds'
137
+ `, [eventId]);
138
+ }
139
+ /**
140
+ * Removes expired event records from the database
141
+ *
142
+ * @private
143
+ * @returns {Promise<void>}
144
+ */
145
+ async cleanupExpired() {
146
+ // Delete expired entries
147
+ await this.postgres.execute(`
148
+ DELETE FROM ${this.tableName}
149
+ WHERE expires_at < NOW()
150
+ `);
151
+ }
152
+ /**
153
+ * Closes the PostgreSQL connection
154
+ *
155
+ * @returns {Promise<void>}
156
+ */
157
+ async close() {
158
+ if (this.postgres) {
159
+ await this.postgres.disconnect();
160
+ }
161
+ }
162
+ }
163
+ exports.PostgresPathwayState = PostgresPathwayState;
164
+ /**
165
+ * Default time-to-live for processed event records (5 minutes)
166
+ * @private
167
+ */
168
+ Object.defineProperty(PostgresPathwayState, "DEFAULT_TTL_MS", {
169
+ enumerable: true,
170
+ configurable: true,
171
+ writable: true,
172
+ value: 5 * 60 * 1000
173
+ }); // 5 minutes
174
+ /**
175
+ * Default table name for storing pathway state
176
+ * @private
177
+ */
178
+ Object.defineProperty(PostgresPathwayState, "DEFAULT_TABLE_NAME", {
179
+ enumerable: true,
180
+ configurable: true,
181
+ writable: true,
182
+ value: "pathway_state"
183
+ });
184
+ /**
185
+ * Creates a new PostgreSQL pathway state instance
186
+ *
187
+ * @param {PostgresPathwayStateConfig} config The PostgreSQL configuration
188
+ * @returns {PostgresPathwayState} A new PostgresPathwayState instance
189
+ */
190
+ function createPostgresPathwayState(config) {
191
+ const state = new PostgresPathwayState(config);
192
+ return state;
193
+ }
@@ -0,0 +1,87 @@
1
+ import type { TSchema } from "@sinclair/typebox";
2
+ import type { WebhookFileData, WebhookSendOptions } from "@flowcore/sdk-transformer-core";
3
+ /**
4
+ * Helper type to create a custom type error for non-writable pathways
5
+ */
6
+ type NonWritablePathwayError<T extends string> = T & {
7
+ readonly __nonWritablePathwayError: "This pathway is not writable. To make it writable, remove 'writable: false' from the pathway contract.";
8
+ };
9
+ /**
10
+ * Contract for defining a pathway
11
+ * @template F - The flow type
12
+ * @template E - The event type
13
+ * @template T - The schema type
14
+ */
15
+ export interface PathwayContract<F extends string, E extends string, T extends TSchema> {
16
+ flowType: F;
17
+ eventType: E;
18
+ schema: T;
19
+ /**
20
+ * Whether the pathway is writable. Use `false as const` to make the pathway non-writable at compile time.
21
+ * @example
22
+ * ```ts
23
+ * // Non-writable pathway (will not be available in write)
24
+ * register({
25
+ * flowType: "test",
26
+ * eventType: "event",
27
+ * schema: Type.Object({}),
28
+ * writable: false as const
29
+ * })
30
+ *
31
+ * // Writable pathway
32
+ * register({
33
+ * flowType: "test",
34
+ * eventType: "writable",
35
+ * schema: Type.Object({})
36
+ * })
37
+ * ```
38
+ * @default true
39
+ */
40
+ writable?: boolean;
41
+ /**
42
+ * The maximum number of times to retry processing an event if it fails
43
+ * @default 0
44
+ */
45
+ maxRetries?: number;
46
+ /**
47
+ * The delay in milliseconds between retry attempts
48
+ * Used as the base for exponential backoff if retries > 1
49
+ * @default 1000
50
+ */
51
+ retryDelayMs?: number;
52
+ /**
53
+ * HTTP status codes that should trigger a retry
54
+ * @default [500, 502, 503, 504]
55
+ */
56
+ retryStatusCodes?: number[];
57
+ /**
58
+ * Custom timeout for this pathway in milliseconds
59
+ */
60
+ timeoutMs?: number;
61
+ /**
62
+ * Whether this pathway is for file processing
63
+ */
64
+ isFilePathway?: boolean;
65
+ }
66
+ export type PathwayKey<F extends string, E extends string> = `${F}/${E}`;
67
+ export interface EventMetadata extends Record<string, unknown> {
68
+ }
69
+ export type SendWebhook<EventPayload> = (payload: EventPayload, metadata?: EventMetadata, options?: WebhookSendOptions) => Promise<string>;
70
+ export type SendFilehook = (payload: WebhookFileData, metadata?: EventMetadata, options?: WebhookSendOptions) => Promise<string[]>;
71
+ /**
72
+ * Helper type to create a better error message for non-writable pathways
73
+ */
74
+ export type WritablePathway<T extends string, IsWritable extends boolean> = IsWritable extends false ? NonWritablePathwayError<T> : T;
75
+ export type PathwayState = {
76
+ isProcessed: (eventId: string) => (boolean | Promise<boolean>);
77
+ setProcessed: (eventId: string) => (void | Promise<void>);
78
+ };
79
+ /**
80
+ * Options for pathway writes, extending WebhookSendOptions
81
+ */
82
+ export type PathwayWriteOptions = WebhookSendOptions & {
83
+ fireAndForget?: boolean;
84
+ headers?: Record<string, string>;
85
+ };
86
+ export {};
87
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/pathways/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,KAAK,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA;AAEzF;;GAEG;AACH,KAAK,uBAAuB,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,GAAG;IACnD,QAAQ,CAAC,yBAAyB,EAAE,wGAAwG,CAAA;CAC7I,CAAA;AAED;;;;;GAKG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,OAAO;IACpF,QAAQ,EAAE,CAAC,CAAA;IACX,SAAS,EAAE,CAAC,CAAA;IACZ,MAAM,EAAE,CAAC,CAAA;IACT;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;OAIG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAA;IAC3B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED,MAAM,MAAM,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,IAAI,GAAG,CAAC,IAAI,CAAC,EAAE,CAAA;AAExE,MAAM,WAAW,aAAc,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAAG;AAEjE,MAAM,MAAM,WAAW,CAAC,YAAY,IAAI,CAAC,OAAO,EAAE,YAAY,EAAE,QAAQ,CAAC,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,kBAAkB,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;AAC1I,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,EAAE,eAAe,EAAE,QAAQ,CAAC,EAAE,aAAa,EAAE,OAAO,CAAC,EAAE,kBAAkB,KAAK,OAAO,CAAC,MAAM,EAAE,CAAC,CAAA;AAElI;;GAEG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,SAAS,MAAM,EAAE,UAAU,SAAS,OAAO,IAAI,UAAU,SAAS,KAAK,GAAG,uBAAuB,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAErI,MAAM,MAAM,YAAY,GAAG;IACzB,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,CAAA;IAC9D,YAAY,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;CAC1D,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,kBAAkB,GAAG;IACrD,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACjC,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,35 @@
1
+ import type { FlowcoreLegacyEvent } from "../common/flowcore.type.js";
2
+ import type { PathwaysBuilder } from "../pathways/index.js";
3
+ import type { Logger } from "../pathways/logger.js";
4
+ /**
5
+ * Router class that handles directing events to the appropriate pathway handlers
6
+ *
7
+ * @class PathwayRouter
8
+ */
9
+ export declare class PathwayRouter {
10
+ private readonly pathways;
11
+ private readonly secretKey;
12
+ private readonly logger;
13
+ /**
14
+ * Creates a new instance of PathwayRouter
15
+ *
16
+ * @param {PathwaysBuilder<Record<string, any>>} pathways - The pathways builder instance that contains all registered pathways
17
+ * @param {string} secretKey - Secret key used for authentication when processing events
18
+ * @param {Logger} [logger] - Optional logger instance (defaults to NoopLogger if not provided)
19
+ * @throws {Error} Will throw an error if secretKey is empty or not provided
20
+ */
21
+ constructor(pathways: PathwaysBuilder<Record<string, any>>, secretKey: string, logger?: Logger);
22
+ /**
23
+ * Processes an incoming event by routing it to the appropriate pathway
24
+ *
25
+ * @param {FlowcoreLegacyEvent} event - The event to process
26
+ * @param {string} providedSecret - The secret key provided for authentication
27
+ * @returns {Promise<{ success: boolean; message: string }>} Result of the event processing
28
+ * @throws {Error} Will throw an error if authentication fails, pathway is not found, or processing fails
29
+ */
30
+ processEvent(event: FlowcoreLegacyEvent, providedSecret: string): Promise<{
31
+ success: boolean;
32
+ message: string;
33
+ }>;
34
+ }
35
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/router/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAErE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAGnD;;;;GAIG;AACH,qBAAa,aAAa;IAatB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAb5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAEhC;;;;;;;OAOG;gBAGgB,QAAQ,EAAE,eAAe,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,EAC9C,SAAS,EAAE,MAAM,EAClC,MAAM,CAAC,EAAE,MAAM;IAYjB;;;;;;;OAOG;IACG,YAAY,CAAC,KAAK,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAoDvH"}
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PathwayRouter = void 0;
4
+ const logger_js_1 = require("../pathways/logger.js");
5
+ /**
6
+ * Router class that handles directing events to the appropriate pathway handlers
7
+ *
8
+ * @class PathwayRouter
9
+ */
10
+ class PathwayRouter {
11
+ /**
12
+ * Creates a new instance of PathwayRouter
13
+ *
14
+ * @param {PathwaysBuilder<Record<string, any>>} pathways - The pathways builder instance that contains all registered pathways
15
+ * @param {string} secretKey - Secret key used for authentication when processing events
16
+ * @param {Logger} [logger] - Optional logger instance (defaults to NoopLogger if not provided)
17
+ * @throws {Error} Will throw an error if secretKey is empty or not provided
18
+ */
19
+ constructor(
20
+ // deno-lint-ignore no-explicit-any
21
+ pathways, secretKey, logger) {
22
+ Object.defineProperty(this, "pathways", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: pathways
27
+ });
28
+ Object.defineProperty(this, "secretKey", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: secretKey
33
+ });
34
+ Object.defineProperty(this, "logger", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: void 0
39
+ });
40
+ this.logger = logger ?? new logger_js_1.NoopLogger();
41
+ if (!secretKey || secretKey.trim() === "") {
42
+ this.logger.error("Secret key is required for PathwayRouter");
43
+ throw new Error("Secret key is required for PathwayRouter");
44
+ }
45
+ this.logger.debug("PathwayRouter initialized");
46
+ }
47
+ /**
48
+ * Processes an incoming event by routing it to the appropriate pathway
49
+ *
50
+ * @param {FlowcoreLegacyEvent} event - The event to process
51
+ * @param {string} providedSecret - The secret key provided for authentication
52
+ * @returns {Promise<{ success: boolean; message: string }>} Result of the event processing
53
+ * @throws {Error} Will throw an error if authentication fails, pathway is not found, or processing fails
54
+ */
55
+ async processEvent(event, providedSecret) {
56
+ // Validate secret key
57
+ if (!providedSecret || providedSecret !== this.secretKey) {
58
+ this.logger.error("Invalid secret key provided");
59
+ throw new Error("Invalid secret key");
60
+ }
61
+ const compatibleEvent = {
62
+ ...event,
63
+ ...(event.aggregator ? { flowType: event.aggregator } : {}),
64
+ };
65
+ const pathwayKey = `${compatibleEvent.flowType}/${compatibleEvent.eventType}`;
66
+ this.logger.debug(`Processing event for pathway: ${pathwayKey}`, {
67
+ eventId: compatibleEvent.eventId
68
+ });
69
+ const pathway = this.pathways.get(pathwayKey);
70
+ if (!pathway) {
71
+ const error = `Pathway ${pathwayKey} not found`;
72
+ this.logger.error(error);
73
+ throw new Error(error);
74
+ }
75
+ try {
76
+ this.logger.debug(`Delegating event processing to pathway handler`, {
77
+ pathwayKey,
78
+ eventId: compatibleEvent.eventId
79
+ });
80
+ await this.pathways.process(pathwayKey, compatibleEvent);
81
+ this.logger.debug(`Event successfully processed through pathway`, {
82
+ pathwayKey,
83
+ eventId: compatibleEvent.eventId
84
+ });
85
+ return { success: true, message: `Event processed through pathway ${pathwayKey}` };
86
+ }
87
+ catch (error) {
88
+ const errorMessage = error instanceof Error
89
+ ? error.message
90
+ : String(error);
91
+ this.logger.error(`Error processing pathway ${pathwayKey}`, {
92
+ error: errorMessage,
93
+ eventId: compatibleEvent.eventId
94
+ });
95
+ // Rethrow the error with additional context
96
+ throw new Error(`Failed to process event in pathway ${pathwayKey}: ${errorMessage}`);
97
+ }
98
+ }
99
+ }
100
+ exports.PathwayRouter = PathwayRouter;