@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,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,188 @@
1
+ import { createPostgresAdapter } from "./postgres-adapter.js";
2
+ /**
3
+ * Implementation of PathwayState that uses PostgreSQL for storage
4
+ *
5
+ * This class provides persistent storage of pathway state using a PostgreSQL database,
6
+ * which allows for state to be shared across multiple instances of the application.
7
+ *
8
+ * @implements {PathwayState}
9
+ */
10
+ export class PostgresPathwayState {
11
+ /**
12
+ * Creates a new PostgresPathwayState instance
13
+ *
14
+ * @param {PostgresPathwayStateConfig} config The PostgreSQL configuration
15
+ */
16
+ constructor(config) {
17
+ Object.defineProperty(this, "config", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: config
22
+ });
23
+ /**
24
+ * The PostgreSQL adapter instance
25
+ * @private
26
+ */
27
+ Object.defineProperty(this, "postgres", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ /**
34
+ * The table name for storing pathway state
35
+ * @private
36
+ */
37
+ Object.defineProperty(this, "tableName", {
38
+ enumerable: true,
39
+ configurable: true,
40
+ writable: true,
41
+ value: void 0
42
+ });
43
+ /**
44
+ * Time-to-live in milliseconds for processed events
45
+ * @private
46
+ */
47
+ Object.defineProperty(this, "ttlMs", {
48
+ enumerable: true,
49
+ configurable: true,
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ /**
54
+ * Whether the database has been initialized
55
+ * @private
56
+ */
57
+ Object.defineProperty(this, "initialized", {
58
+ enumerable: true,
59
+ configurable: true,
60
+ writable: true,
61
+ value: false
62
+ });
63
+ this.tableName = config.tableName || PostgresPathwayState.DEFAULT_TABLE_NAME;
64
+ this.ttlMs = config.ttlMs || PostgresPathwayState.DEFAULT_TTL_MS;
65
+ this.postgres = null;
66
+ }
67
+ /**
68
+ * Initializes the PostgreSQL connection and creates the necessary table and index
69
+ *
70
+ * @private
71
+ * @returns {Promise<void>}
72
+ */
73
+ async initialize() {
74
+ if (this.initialized) {
75
+ return;
76
+ }
77
+ this.postgres = await createPostgresAdapter({
78
+ host: this.config.host,
79
+ port: this.config.port,
80
+ user: this.config.user,
81
+ password: this.config.password,
82
+ database: this.config.database,
83
+ ssl: this.config.ssl,
84
+ });
85
+ // Create table if it doesn't exist
86
+ await this.postgres.execute(`
87
+ CREATE TABLE IF NOT EXISTS ${this.tableName} (
88
+ event_id TEXT PRIMARY KEY,
89
+ processed BOOLEAN NOT NULL,
90
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
91
+ expires_at TIMESTAMP WITH TIME ZONE NOT NULL
92
+ )
93
+ `);
94
+ // Create index on expires_at to help with cleanup
95
+ await this.postgres.execute(`
96
+ CREATE INDEX IF NOT EXISTS ${this.tableName}_expires_at_idx ON ${this.tableName} (expires_at)
97
+ `);
98
+ this.initialized = true;
99
+ }
100
+ /**
101
+ * Checks if an event has already been processed
102
+ *
103
+ * @param {string} eventId - The ID of the event to check
104
+ * @returns {Promise<boolean>} True if the event has been processed, false otherwise
105
+ */
106
+ async isProcessed(eventId) {
107
+ await this.initialize();
108
+ // Clean up expired entries
109
+ await this.cleanupExpired();
110
+ const result = await this.postgres.query(`
111
+ SELECT processed FROM ${this.tableName}
112
+ WHERE event_id = $1 AND expires_at > NOW()
113
+ `, [eventId]);
114
+ return result.length > 0 && result[0].processed;
115
+ }
116
+ /**
117
+ * Marks an event as processed
118
+ *
119
+ * @param {string} eventId - The ID of the event to mark as processed
120
+ * @returns {Promise<void>}
121
+ */
122
+ async setProcessed(eventId) {
123
+ await this.initialize();
124
+ // Insert or update the event state
125
+ // Using ON CONFLICT to handle the case where the event is already in the table
126
+ await this.postgres.execute(`
127
+ INSERT INTO ${this.tableName} (event_id, processed, expires_at)
128
+ VALUES ($1, TRUE, NOW() + interval '${Math.floor(this.ttlMs / 1000)} seconds')
129
+ ON CONFLICT (event_id)
130
+ DO UPDATE SET
131
+ processed = TRUE,
132
+ expires_at = NOW() + interval '${Math.floor(this.ttlMs / 1000)} seconds'
133
+ `, [eventId]);
134
+ }
135
+ /**
136
+ * Removes expired event records from the database
137
+ *
138
+ * @private
139
+ * @returns {Promise<void>}
140
+ */
141
+ async cleanupExpired() {
142
+ // Delete expired entries
143
+ await this.postgres.execute(`
144
+ DELETE FROM ${this.tableName}
145
+ WHERE expires_at < NOW()
146
+ `);
147
+ }
148
+ /**
149
+ * Closes the PostgreSQL connection
150
+ *
151
+ * @returns {Promise<void>}
152
+ */
153
+ async close() {
154
+ if (this.postgres) {
155
+ await this.postgres.disconnect();
156
+ }
157
+ }
158
+ }
159
+ /**
160
+ * Default time-to-live for processed event records (5 minutes)
161
+ * @private
162
+ */
163
+ Object.defineProperty(PostgresPathwayState, "DEFAULT_TTL_MS", {
164
+ enumerable: true,
165
+ configurable: true,
166
+ writable: true,
167
+ value: 5 * 60 * 1000
168
+ }); // 5 minutes
169
+ /**
170
+ * Default table name for storing pathway state
171
+ * @private
172
+ */
173
+ Object.defineProperty(PostgresPathwayState, "DEFAULT_TABLE_NAME", {
174
+ enumerable: true,
175
+ configurable: true,
176
+ writable: true,
177
+ value: "pathway_state"
178
+ });
179
+ /**
180
+ * Creates a new PostgreSQL pathway state instance
181
+ *
182
+ * @param {PostgresPathwayStateConfig} config The PostgreSQL configuration
183
+ * @returns {PostgresPathwayState} A new PostgresPathwayState instance
184
+ */
185
+ export function createPostgresPathwayState(config) {
186
+ const state = new PostgresPathwayState(config);
187
+ return state;
188
+ }
@@ -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 @@
1
+ export {};
@@ -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,96 @@
1
+ import { NoopLogger } from "../pathways/logger.js";
2
+ /**
3
+ * Router class that handles directing events to the appropriate pathway handlers
4
+ *
5
+ * @class PathwayRouter
6
+ */
7
+ export class PathwayRouter {
8
+ /**
9
+ * Creates a new instance of PathwayRouter
10
+ *
11
+ * @param {PathwaysBuilder<Record<string, any>>} pathways - The pathways builder instance that contains all registered pathways
12
+ * @param {string} secretKey - Secret key used for authentication when processing events
13
+ * @param {Logger} [logger] - Optional logger instance (defaults to NoopLogger if not provided)
14
+ * @throws {Error} Will throw an error if secretKey is empty or not provided
15
+ */
16
+ constructor(
17
+ // deno-lint-ignore no-explicit-any
18
+ pathways, secretKey, logger) {
19
+ Object.defineProperty(this, "pathways", {
20
+ enumerable: true,
21
+ configurable: true,
22
+ writable: true,
23
+ value: pathways
24
+ });
25
+ Object.defineProperty(this, "secretKey", {
26
+ enumerable: true,
27
+ configurable: true,
28
+ writable: true,
29
+ value: secretKey
30
+ });
31
+ Object.defineProperty(this, "logger", {
32
+ enumerable: true,
33
+ configurable: true,
34
+ writable: true,
35
+ value: void 0
36
+ });
37
+ this.logger = logger ?? new NoopLogger();
38
+ if (!secretKey || secretKey.trim() === "") {
39
+ this.logger.error("Secret key is required for PathwayRouter");
40
+ throw new Error("Secret key is required for PathwayRouter");
41
+ }
42
+ this.logger.debug("PathwayRouter initialized");
43
+ }
44
+ /**
45
+ * Processes an incoming event by routing it to the appropriate pathway
46
+ *
47
+ * @param {FlowcoreLegacyEvent} event - The event to process
48
+ * @param {string} providedSecret - The secret key provided for authentication
49
+ * @returns {Promise<{ success: boolean; message: string }>} Result of the event processing
50
+ * @throws {Error} Will throw an error if authentication fails, pathway is not found, or processing fails
51
+ */
52
+ async processEvent(event, providedSecret) {
53
+ // Validate secret key
54
+ if (!providedSecret || providedSecret !== this.secretKey) {
55
+ this.logger.error("Invalid secret key provided");
56
+ throw new Error("Invalid secret key");
57
+ }
58
+ const compatibleEvent = {
59
+ ...event,
60
+ ...(event.aggregator ? { flowType: event.aggregator } : {}),
61
+ };
62
+ const pathwayKey = `${compatibleEvent.flowType}/${compatibleEvent.eventType}`;
63
+ this.logger.debug(`Processing event for pathway: ${pathwayKey}`, {
64
+ eventId: compatibleEvent.eventId
65
+ });
66
+ const pathway = this.pathways.get(pathwayKey);
67
+ if (!pathway) {
68
+ const error = `Pathway ${pathwayKey} not found`;
69
+ this.logger.error(error);
70
+ throw new Error(error);
71
+ }
72
+ try {
73
+ this.logger.debug(`Delegating event processing to pathway handler`, {
74
+ pathwayKey,
75
+ eventId: compatibleEvent.eventId
76
+ });
77
+ await this.pathways.process(pathwayKey, compatibleEvent);
78
+ this.logger.debug(`Event successfully processed through pathway`, {
79
+ pathwayKey,
80
+ eventId: compatibleEvent.eventId
81
+ });
82
+ return { success: true, message: `Event processed through pathway ${pathwayKey}` };
83
+ }
84
+ catch (error) {
85
+ const errorMessage = error instanceof Error
86
+ ? error.message
87
+ : String(error);
88
+ this.logger.error(`Error processing pathway ${pathwayKey}`, {
89
+ error: errorMessage,
90
+ eventId: compatibleEvent.eventId
91
+ });
92
+ // Rethrow the error with additional context
93
+ throw new Error(`Failed to process event in pathway ${pathwayKey}: ${errorMessage}`);
94
+ }
95
+ }
96
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@flowcore/pathways",
3
+ "version": "0.2.1",
4
+ "description": "A Deno Library for creating Flowcore Pathways, simplifying the integration with the flowcore platform",
5
+ "homepage": "https://github.com/flowcore-io/flowcore-sdk#readme",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/flowcore-io/flowcore-sdk.git"
9
+ },
10
+ "license": "MIT",
11
+ "bugs": {
12
+ "url": "https://github.com/flowcore-io/flowcore-sdk/issues"
13
+ },
14
+ "main": "./script/mod.js",
15
+ "module": "./esm/mod.js",
16
+ "exports": {
17
+ ".": {
18
+ "import": "./esm/mod.js",
19
+ "require": "./script/mod.js"
20
+ }
21
+ },
22
+ "optionalDependencies": {
23
+ "bun-sqlite-key-value": "1.13.1"
24
+ },
25
+ "dependencies": {
26
+ "@flowcore/sdk-transformer-core": "^2.3.6",
27
+ "@sinclair/typebox": "^0.34.27",
28
+ "bun-sqlite-key-value": "1.13.1",
29
+ "node-cache": "5.1.2",
30
+ "postgres": "^3.4.3",
31
+ "rxjs": "^7.8.1"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^20.9.0",
35
+ "@types/ws": "^8.5.10"
36
+ },
37
+ "_generatedBy": "dnt@dev"
38
+ }
@@ -0,0 +1,11 @@
1
+ import type { FlowcoreEvent } from "../contracts/event.js";
2
+ /**
3
+ * Extends the FlowcoreEvent with legacy fields
4
+ *
5
+ * @typedef {Object} FlowcoreLegacyEvent
6
+ * @property {string} [aggregator] - Optional aggregator information for legacy support
7
+ */
8
+ export type FlowcoreLegacyEvent = FlowcoreEvent & {
9
+ aggregator?: string;
10
+ };
11
+ //# sourceMappingURL=flowcore.type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flowcore.type.d.ts","sourceRoot":"","sources":["../../src/common/flowcore.type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AAE1D;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,aAAa,GAAG;IAChD,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,CAAA"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Common types and utilities for Flowcore Pathways
3
+ *
4
+ * This module exports common types and utilities that are used across the Flowcore Pathways library.
5
+ */
6
+ export * from "./flowcore.type.js";
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,22 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /**
18
+ * Common types and utilities for Flowcore Pathways
19
+ *
20
+ * This module exports common types and utilities that are used across the Flowcore Pathways library.
21
+ */
22
+ __exportStar(require("./flowcore.type.js"), exports);
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Compatibility layer for the Flowcore Transformer Core SDK
3
+ *
4
+ * This module re-exports components from the @flowcore/sdk-transformer-core package
5
+ * to provide backwards compatibility with existing transformer implementations.
6
+ */
7
+ import transformerCore from "@flowcore/sdk-transformer-core";
8
+ /**
9
+ * WebhookBuilder from the transformer core SDK for sending webhook events
10
+ */
11
+ export declare const WebhookBuilder: typeof transformerCore.WebhookBuilder;
12
+ /**
13
+ * Options for configuring webhook send operations
14
+ */
15
+ export type { WebhookSendOptions } from "@flowcore/sdk-transformer-core";
16
+ //# sourceMappingURL=flowcore-transformer-core.sdk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flowcore-transformer-core.sdk.d.ts","sourceRoot":"","sources":["../../src/compatibility/flowcore-transformer-core.sdk.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,eAAe,MAAM,gCAAgC,CAAA;AAE5D;;GAEG;AACH,eAAO,MAAQ,cAAc,uCAAoB,CAAA;AAEjD;;GAEG;AACH,YAAY,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAA"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.WebhookBuilder = void 0;
7
+ /**
8
+ * Compatibility layer for the Flowcore Transformer Core SDK
9
+ *
10
+ * This module re-exports components from the @flowcore/sdk-transformer-core package
11
+ * to provide backwards compatibility with existing transformer implementations.
12
+ */
13
+ const sdk_transformer_core_1 = __importDefault(require("@flowcore/sdk-transformer-core"));
14
+ /**
15
+ * WebhookBuilder from the transformer core SDK for sending webhook events
16
+ */
17
+ exports.WebhookBuilder = sdk_transformer_core_1.default.WebhookBuilder;