Skip to content

Commit

Permalink
add EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Feb 8, 2024
1 parent b06cb0b commit c1f91f4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 2 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ckan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"xmlbuilder2": "^3.1.1"
},
"devDependencies": {
"@types/node": "^20.11.16",
"c8": "^9.1.0",
"chai": "^5.0.3",
"chai-subset": "^1.6.0",
Expand Down
12 changes: 12 additions & 0 deletions packages/core/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @ts-check
import EventEmitter from 'node:events'
import express from 'express'
import { pino } from 'pino'
import cors from 'cors'
Expand Down Expand Up @@ -41,6 +42,7 @@ export {
* }>} Trifid instance.
*/
const trifid = async (config, additionalMiddlewares = {}) => {
const trifidEvents = new EventEmitter()
const fullConfig = await handler(config)
const server = express()
server.disable('x-powered-by')
Expand All @@ -63,6 +65,15 @@ const trifid = async (config, additionalMiddlewares = {}) => {
// Add support for absolute URLs, so that we can use `req.absoluteUrl()` in any middleware to get the absolute URL
server.use(absoluteUrl())

// Forward server events to the Trifid middlewares
server.on('ready', () => {
trifidEvents.emit('ready')
})

server.on('close', () => {
trifidEvents.emit('close')
})

// Configure Express server
if (fullConfig?.server?.express) {
for (const expressSettingKey in fullConfig.server.express) {
Expand Down Expand Up @@ -104,6 +115,7 @@ const trifid = async (config, additionalMiddlewares = {}) => {
logger,
templateEngineInstance,
`http://${host}:${portNumber}/`,
trifidEvents,
)

const start = async () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/core/lib/middlewares/apply.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import merge from 'lodash/merge.js'
import vhost from 'vhost'
import { initQuery } from '../sparql.js'

const apply = async (server, globals, middlewares, logger, templateEngine, instanceHostname) => {
const apply = async (server, globals, middlewares, logger, templateEngine, instanceHostname, trifidEvents) => {
const { query: querySparql } = initQuery(logger, globals.endpoints, instanceHostname)

for (const middleware of middlewares) {
Expand All @@ -28,6 +28,7 @@ const apply = async (server, globals, middlewares, logger, templateEngine, insta
render,
query,
registerTemplateHelper: registerHelper,
trifidEvents,
})

// default path is '/' (see: https://github.com/expressjs/express/blob/d854c43ea177d1faeea56189249fff8c24a764bd/lib/router/index.js#L425)
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
* @property {Object.<string, any>} config The Trifid configuration.
* @property {(templatePath: string, context: Object.<string, any>, options: Object.<string, any>) => Promise<string>} render The render function.
* @property {TrifidQuery} query The SPARQL query function.
* @property {import('node:events').EventEmitter} trifidEvents The Trifid events emitter.
*/

/**
Expand Down
14 changes: 13 additions & 1 deletion packages/handler-fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { waitForVariableToBeTrue } from './lib/utils.js'

/** @type {import('trifid-core/dist/types/index.d.ts').TrifidMiddleware} */
export const factory = async (trifid) => {
const { config, logger } = trifid
const { config, logger, trifidEvents } = trifid
const { contentType, url, baseIri, graphName, unionDefaultGraph } = config

const queryTimeout = 30000
Expand All @@ -17,6 +17,18 @@ export const factory = async (trifid) => {

let ready = false

logger.warn('Listening to Trifid events…')

trifidEvents.on('ready', async () => {
logger.warn('Got "ready" event from Trifid')
})

trifidEvents.on('close', async () => {
logger.warn('Got "close" event from Trifid ; closing worker…')
await worker.terminate()
logger.debug('Worker terminated')
})

worker.on('message', async (message) => {
const { type, data } = message
if (type === 'log') {
Expand Down

0 comments on commit c1f91f4

Please sign in to comment.