Home / Function/ hookRunnerApplication() — fastify Function Reference

hookRunnerApplication() — fastify Function Reference

Architecture documentation for the hookRunnerApplication() function in hooks.js from the fastify codebase.

Entity Profile

Dependency Diagram

graph TD
  04d6fcfa_3f65_7192_30d1_f3b0af80751e["hookRunnerApplication()"]
  4861b031_e088_b0f3_c8b9_e454f9f56793["hooks.js"]
  04d6fcfa_3f65_7192_30d1_f3b0af80751e -->|defined in| 4861b031_e088_b0f3_c8b9_e454f9f56793
  style 04d6fcfa_3f65_7192_30d1_f3b0af80751e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

lib/hooks.js lines 93–181

function hookRunnerApplication (hookName, boot, server, cb) {
  const hooks = server[kHooks][hookName]
  let i = 0
  let c = 0

  next()

  function exit (err) {
    const hookFnName = hooks[i - 1]?.name
    const hookFnFragment = hookFnName ? ` "${hookFnName}"` : ''

    if (err) {
      if (err.code === 'AVV_ERR_READY_TIMEOUT') {
        err = appendStackTrace(err, new FST_ERR_HOOK_TIMEOUT(hookName, hookFnFragment))
      } else {
        err = AVVIO_ERRORS_MAP[err.code] != null
          ? appendStackTrace(err, new AVVIO_ERRORS_MAP[err.code](err.message))
          : err
      }

      cb(err)
      return
    }
    cb()
  }

  function next (err) {
    if (err) {
      exit(err)
      return
    }

    if (i === hooks.length && c === server[kChildren].length) {
      if (i === 0 && c === 0) { // speed up start
        exit()
      } else {
        // This is the last function executed for every fastify instance
        boot(function manageTimeout (err, done) {
          // this callback is needed by fastify to provide an hook interface without the error
          // as first parameter and managing it on behalf the user
          exit(err)

          // this callback is needed by avvio to continue the loading of the next `register` plugins
          done(err)
        })
      }
      return
    }

    if (i === hooks.length && c < server[kChildren].length) {
      const child = server[kChildren][c++]
      hookRunnerApplication(hookName, boot, child, next)
      return
    }

    boot(wrap(hooks[i++], server))
    next()
  }

  function wrap (fn, server) {
    return function (err, done) {
      if (err) {
        done(err)
        return
      }

      if (fn.length === 1) {
        try {
          fn.call(server, done)
        } catch (error) {
          done(error)
        }
        return
      }

      try {
        const ret = fn.call(server)
        if (ret && typeof ret.then === 'function') {
          ret.then(done, done)
          return
        }

Domain

Subdomains

Defined In

Frequently Asked Questions

What does hookRunnerApplication() do?
hookRunnerApplication() is a function in the fastify codebase, defined in lib/hooks.js.
Where is hookRunnerApplication() defined?
hookRunnerApplication() is defined in lib/hooks.js at line 93.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free