-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
166 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import type { Data, GraphInputs, ProcessOptions } from '@encrejs/api'; | ||
import type { ProcessStreamEventFilter } from '@encrejs/api/streaming'; | ||
import appService from '../../services/app/index.js'; | ||
import config from '../../config.js'; | ||
|
||
const run = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
const { | ||
userInputs: inputs, | ||
appPath, | ||
context, | ||
filter, | ||
} = req.body as { | ||
userInputs: GraphInputs; | ||
appPath?: string; | ||
context?: Record<string, Data>; | ||
filter?: ProcessStreamEventFilter; | ||
}; | ||
|
||
const filePath: string = | ||
appPath ?? config.encreFilePath ?? config.projectRoot; | ||
const options: ProcessOptions = { inputs, context }; | ||
|
||
res.setHeader('Content-Type', 'text/event-stream'); | ||
res.setHeader('Transfer-Encoding', 'chunked'); | ||
|
||
for await (const chunk of appService.run(filePath, options, filter)) { | ||
const groups = /event: (?<event>.*)\ndata: (?<data>.*)\n\n/.exec(chunk)! | ||
.groups!; | ||
|
||
const eventName = groups.event!; | ||
const data = groups.data!; | ||
|
||
const chunkData = { | ||
['#event']: eventName, | ||
...JSON.parse(data), | ||
}; | ||
|
||
res.write(`data: ${JSON.stringify(chunkData)}\n\n`); | ||
} | ||
|
||
res.write('data: [DONE]\n\n'); | ||
res.end(); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export default { run }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import express from 'express'; | ||
import nodesController from '../../controllers/app/index.js'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/run', nodesController.run); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { | ||
BaseGraph, | ||
createProcessor, | ||
loadGraph, | ||
ProcessOptions, | ||
} from '@encrejs/api'; | ||
import { ProcessStreamEventFilter } from '@encrejs/api/streaming'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
|
||
import { InternalError } from '../../exceptions/internal.js'; | ||
import { getErrorMessage } from '../../utils/getErrorMessage.js'; | ||
|
||
async function _getGraph(filePath: string): Promise<BaseGraph> { | ||
try { | ||
const graph = await loadGraph(filePath); | ||
|
||
return graph; | ||
} catch (error) { | ||
throw new InternalError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`appService.getGraph: ${getErrorMessage(error)}` | ||
); | ||
} | ||
} | ||
|
||
function _getProcessor( | ||
graph: BaseGraph, | ||
options?: ProcessOptions | ||
): ReturnType<typeof createProcessor> { | ||
try { | ||
const processor = createProcessor(graph, options); | ||
|
||
return processor; | ||
} catch (error) { | ||
throw new InternalError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`appService.getProcessor: ${getErrorMessage(error)}` | ||
); | ||
} | ||
} | ||
|
||
async function* run( | ||
filePath: string, | ||
options?: ProcessOptions, | ||
filter?: ProcessStreamEventFilter | ||
) { | ||
try { | ||
const graph = await _getGraph(filePath); | ||
|
||
const processor = _getProcessor(graph, options); | ||
|
||
for await (const chunk of processor.sseStream(filter ?? {})) { | ||
yield chunk; | ||
} | ||
} catch (error) { | ||
throw new InternalError( | ||
StatusCodes.INTERNAL_SERVER_ERROR, | ||
`appService.run: ${getErrorMessage(error)}` | ||
); | ||
} | ||
} | ||
|
||
export default { | ||
run, | ||
}; |
Oops, something went wrong.