From 830808c5650daa1ec57ddc5b7b15b41437da8594 Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Sun, 19 May 2024 11:58:06 +0530 Subject: [PATCH 1/8] feat(js): add path prefix option to startFlowsServer --- js/flow/src/flow.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 5665aed59..f95ef91e5 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -378,7 +378,7 @@ export class Flow< throw new Error( 'Unexpected envelope message case, must set one of: ' + - 'start, schedule, runScheduled, resume, retry, state' + 'start, schedule, runScheduled, resume, retry, state' ); } @@ -710,8 +710,8 @@ export function streamFlow< start(controller) { chunkStreamController = controller; }, - pull() {}, - cancel() {}, + pull() { }, + cancel() { }, }); const operationPromise = flow @@ -835,9 +835,11 @@ export function startFlowsServer(params?: { flows?: Flow[]; port?: number; cors?: CorsOptions; + pathPrefix?: string; }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; + const pathPrefix = params?.pathPrefix ? `/${params?.pathPrefix}` : ""; const app = express(); app.use(bodyParser.json()); app.use(cors(params?.cors)); @@ -845,12 +847,12 @@ export function startFlowsServer(params?: { const flows = params?.flows || createdFlows(); logger.info(`Starting flows server on port ${port}`); flows.forEach((f) => { - logger.info(` - /${f.name}`); + logger.info(` - ${pathPrefix}/${f.name}`); // Add middlware f.middleware?.forEach((m) => { - app.post(`/${f.name}`, m); + app.post(`${pathPrefix}/${f.name}`, m); }); - app.post(`/${f.name}`, f.expressHandler); + app.post(`${pathPrefix}/${f.name}`, f.expressHandler); }); app.listen(port, () => { From 899f2f672d70af8dc319bb996395ae1d44be77cd Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Mon, 20 May 2024 19:50:34 +0530 Subject: [PATCH 2/8] chore: format --- js/flow/src/flow.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index f95ef91e5..f7dadf0f2 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -378,7 +378,7 @@ export class Flow< throw new Error( 'Unexpected envelope message case, must set one of: ' + - 'start, schedule, runScheduled, resume, retry, state' + 'start, schedule, runScheduled, resume, retry, state' ); } @@ -710,8 +710,8 @@ export function streamFlow< start(controller) { chunkStreamController = controller; }, - pull() { }, - cancel() { }, + pull() {}, + cancel() {}, }); const operationPromise = flow @@ -839,7 +839,7 @@ export function startFlowsServer(params?: { }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; - const pathPrefix = params?.pathPrefix ? `/${params?.pathPrefix}` : ""; + const pathPrefix = params?.pathPrefix ? `/${params?.pathPrefix}` : ''; const app = express(); app.use(bodyParser.json()); app.use(cors(params?.cors)); From cc021a93f9c2f0309ecb117883b69de0be7280eb Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Wed, 22 May 2024 21:14:01 +0530 Subject: [PATCH 3/8] add requested changes --- js/flow/src/flow.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index f7dadf0f2..6bac49fe3 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -839,7 +839,7 @@ export function startFlowsServer(params?: { }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; - const pathPrefix = params?.pathPrefix ? `/${params?.pathPrefix}` : ''; + const pathPrefix = params?.pathPrefix ?? ''; const app = express(); app.use(bodyParser.json()); app.use(cors(params?.cors)); @@ -847,12 +847,13 @@ export function startFlowsServer(params?: { const flows = params?.flows || createdFlows(); logger.info(`Starting flows server on port ${port}`); flows.forEach((f) => { - logger.info(` - ${pathPrefix}/${f.name}`); + const flowPath = `${pathPrefix}/${f.name}`; + logger.info(` - ${flowPath}`); // Add middlware f.middleware?.forEach((m) => { - app.post(`${pathPrefix}/${f.name}`, m); + app.post(flowPath, m); }); - app.post(`${pathPrefix}/${f.name}`, f.expressHandler); + app.post(flowPath, f.expressHandler); }); app.listen(port, () => { From 1d9dd17415f9327f30fd0ea7b67051cdf18a55cc Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Sun, 19 May 2024 12:04:27 +0530 Subject: [PATCH 4/8] feat(js): add createFlowsServer --- js/flow/src/flow.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 6bac49fe3..7f5af2bfd 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -831,9 +831,8 @@ function wrapAsAction< ); } -export function startFlowsServer(params?: { +export function createFlowsServer(params?: { flows?: Flow[]; - port?: number; cors?: CorsOptions; pathPrefix?: string; }) { @@ -856,6 +855,23 @@ export function startFlowsServer(params?: { app.post(flowPath, f.expressHandler); }); + return app; +} + +export function startFlowsServer(params?: { + flows?: Flow[]; + port?: number; + cors?: CorsOptions; + pathPrefix?: string; +}) { + const port = + params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; + const app = createFlowsServer({ + flows: params?.flows, + cors: params?.cors, + pathPrefix: params?.pathPrefix, + }); + app.listen(port, () => { console.log(`Flows server listening on port ${port}`); }); From 911f4bf6063568fc76909d8e7c7ea7a27d5039a7 Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Wed, 22 May 2024 21:31:12 +0530 Subject: [PATCH 5/8] refactor: return router by default --- js/flow/src/flow.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 7f5af2bfd..8780ab549 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -831,31 +831,29 @@ function wrapAsAction< ); } -export function createFlowsServer(params?: { +export function createFlowsExpressRouter(params?: { flows?: Flow[]; cors?: CorsOptions; - pathPrefix?: string; }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; - const pathPrefix = params?.pathPrefix ?? ''; - const app = express(); - app.use(bodyParser.json()); - app.use(cors(params?.cors)); + const router = express.Router(); + router.use(bodyParser.json()); + router.use(cors(params?.cors)); const flows = params?.flows || createdFlows(); logger.info(`Starting flows server on port ${port}`); flows.forEach((f) => { - const flowPath = `${pathPrefix}/${f.name}`; + const flowPath = `/${f.name}`; logger.info(` - ${flowPath}`); // Add middlware f.middleware?.forEach((m) => { - app.post(flowPath, m); + router.post(flowPath, m); }); - app.post(flowPath, f.expressHandler); + router.post(flowPath, f.expressHandler); }); - return app; + return router; } export function startFlowsServer(params?: { @@ -866,12 +864,15 @@ export function startFlowsServer(params?: { }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; - const app = createFlowsServer({ + const pathPrefix = params?.pathPrefix ?? ''; + const router = createFlowsExpressRouter({ flows: params?.flows, cors: params?.cors, - pathPrefix: params?.pathPrefix, }); + const app = express(); + app.use(pathPrefix, router); + app.listen(port, () => { console.log(`Flows server listening on port ${port}`); }); From b698f01aaab0d84e315d9921c6b9b9ecee4d6ba1 Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Thu, 23 May 2024 11:10:31 +0530 Subject: [PATCH 6/8] refactor: make leading / default --- js/flow/src/flow.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 6bac49fe3..026c62e5f 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -847,7 +847,7 @@ export function startFlowsServer(params?: { const flows = params?.flows || createdFlows(); logger.info(`Starting flows server on port ${port}`); flows.forEach((f) => { - const flowPath = `${pathPrefix}/${f.name}`; + const flowPath = `/${pathPrefix}${f.name}`; logger.info(` - ${flowPath}`); // Add middlware f.middleware?.forEach((m) => { From 1e87a267ae7041a97bfc3bf87ddbadf3e7670d24 Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Thu, 23 May 2024 19:17:09 +0530 Subject: [PATCH 7/8] shift prefix back into createFlowsExpressRouter --- js/flow/src/flow.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 39744baa9..1ce22edda 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -834,6 +834,7 @@ function wrapAsAction< export function createFlowsExpressRouter(params?: { flows?: Flow[]; cors?: CorsOptions; + pathPrefix?: string; }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; @@ -864,14 +865,14 @@ export function startFlowsServer(params?: { }) { const port = params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; - const pathPrefix = params?.pathPrefix ?? ''; const router = createFlowsExpressRouter({ flows: params?.flows, cors: params?.cors, + pathPrefix: params?.pathPrefix, }); const app = express(); - app.use(pathPrefix, router); + app.use(router); app.listen(port, () => { console.log(`Flows server listening on port ${port}`); From 84ab1f9b8024e9bdc026b14d10824d9a3552280a Mon Sep 17 00:00:00 2001 From: Debkanchan Samadder Date: Thu, 23 May 2024 19:23:07 +0530 Subject: [PATCH 8/8] forgot do handle prefix properly --- js/flow/src/flow.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/js/flow/src/flow.ts b/js/flow/src/flow.ts index 1ce22edda..0073abb21 100644 --- a/js/flow/src/flow.ts +++ b/js/flow/src/flow.ts @@ -836,14 +836,12 @@ export function createFlowsExpressRouter(params?: { cors?: CorsOptions; pathPrefix?: string; }) { - const port = - params?.port || (process.env.PORT ? parseInt(process.env.PORT) : 0) || 3400; const router = express.Router(); router.use(bodyParser.json()); router.use(cors(params?.cors)); + const pathPrefix = params?.pathPrefix ?? ''; const flows = params?.flows || createdFlows(); - logger.info(`Starting flows server on port ${port}`); flows.forEach((f) => { const flowPath = `${pathPrefix}/${f.name}`; logger.info(` - ${flowPath}`); @@ -871,6 +869,7 @@ export function startFlowsServer(params?: { pathPrefix: params?.pathPrefix, }); + logger.info(`Starting flows server on port ${port}`); const app = express(); app.use(router);