Repository: MotiaDev/motia
File: packages/core/src/node/node-runner.ts (TypeScript)

Code before:

  try {
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const module = require(path.resolve(filePath))

    // Check if the specified function exists in the module
    if (typeof module.handler !== 'function') {
      throw new Error(`Function handler not found in module ${filePath}`)
    }

    const { traceId, flows, contextInFirstArg } = event

    const logger = new Logger(traceId as string, flows as string[], sender)
    const state = new RpcStateManager(sender)

    const emit = async (data: unknown) => sender.send('emit', data)
    const streamsConfig = event.streams as StreamConfig[]
    const streams = (streamsConfig ?? []).reduce(
      (acc, streams) => {
        acc[streams.name] = {
          get: (groupId: string, id: string) => sender.send(`streams.${streams.name}.get`, { groupId, id }),
          set: (groupId: string, id: string, data: unknown) =>
            sender.send(`streams.${streams.name}.set`, { groupId, id, data }),
          delete: (groupId: string, id: string) => sender.send(`streams.${streams.name}.delete`, { groupId, id }),
          getGroup: (groupId: string) => sender.send(`streams.${streams.name}.getGroup`, { groupId }),
          send: (channel: string, event: unknown) => sender.send(`streams.${streams.name}.send`, { channel, event }),
        }
        return acc
      },
      {} as Record<string, unknown>,
    )

    const context = { traceId, flows, logger, state, emit, streams }

    sender.init()

    const middlewares = Array.isArray(module.config.middleware) ? module.config.middleware : []

    const composedMiddleware = composeMiddleware(...middlewares)
    const handlerFn = () => {
      return contextInFirstArg ? module.handler(context) : module.handler(event.data, context)
    }

    const result = await composedMiddleware(event.data, context, handlerFn)

    await sender.send('result', result)
    await sender.close()

    process.exit(0)
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  } catch (err: any) {

Review comment (suggestion):
does it match with the stream type?

```
  send<T>(channel: StateStreamEventChannel, event: StateStreamEvent<T>): Promise<void>
```

Code after:

  try {
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    const module = require(path.resolve(filePath))

    // Check if the specified function exists in the module
    if (typeof module.handler !== 'function') {
      throw new Error(`Function handler not found in module ${filePath}`)
    }

    const { traceId, flows, contextInFirstArg } = event

    const logger = new Logger(traceId as string, flows as string[], sender)
    const state = new RpcStateManager(sender)

    const emit = async (data: unknown) => sender.send('emit', data)
    const streamsConfig = event.streams as StreamConfig[]
    const streams = (streamsConfig ?? []).reduce(
      (acc, streams) => {
        acc[streams.name] = {
          get: (groupId: string, id: string) => sender.send(`streams.${streams.name}.get`, { groupId, id }),
          set: (groupId: string, id: string, data: unknown) =>
            sender.send(`streams.${streams.name}.set`, { groupId, id, data }),
          delete: (groupId: string, id: string) => sender.send(`streams.${streams.name}.delete`, { groupId, id }),
          getGroup: (groupId: string) => sender.send(`streams.${streams.name}.getGroup`, { groupId }),
          send: (channel: StateStreamEventChannel, event: StateStreamEvent<unknown>) =>
            sender.send(`streams.${streams.name}.send`, { channel, event }),
        }
        return acc
      },
      {} as Record<string, unknown>,
    )

    const context = { traceId, flows, logger, state, emit, streams }

    sender.init()

    const middlewares = Array.isArray(module.config.middleware) ? module.config.middleware : []

    const composedMiddleware = composeMiddleware(...middlewares)
    const handlerFn = () => {
      return contextInFirstArg ? module.handler(context) : module.handler(event.data, context)
    }

    const result = await composedMiddleware(event.data, context, handlerFn)

    await sender.send('result', result)
    await sender.close()

    process.exit(0)
    // eslint-disable-next-line @typescript-eslint/no-explicit-any