| 468 | // If the server is not ready yet, this |
| 469 | // utility will automatically force it. |
| 470 | function inject (opts, cb) { |
| 471 | // lightMyRequest is dynamically loaded as it seems very expensive |
| 472 | // because of Ajv |
| 473 | if (lightMyRequest === undefined) { |
| 474 | lightMyRequest = require('light-my-request') |
| 475 | } |
| 476 | |
| 477 | if (fastify[kState].started) { |
| 478 | if (fastify[kState].closing) { |
| 479 | // Force to return an error |
| 480 | const error = new FST_ERR_REOPENED_CLOSE_SERVER() |
| 481 | if (cb) { |
| 482 | cb(error) |
| 483 | return |
| 484 | } else { |
| 485 | return Promise.reject(error) |
| 486 | } |
| 487 | } |
| 488 | return lightMyRequest(httpHandler, opts, cb) |
| 489 | } |
| 490 | |
| 491 | if (cb) { |
| 492 | this.ready(err => { |
| 493 | if (err) cb(err, null) |
| 494 | else lightMyRequest(httpHandler, opts, cb) |
| 495 | }) |
| 496 | } else { |
| 497 | return lightMyRequest((req, res) => { |
| 498 | this.ready(function (err) { |
| 499 | if (err) { |
| 500 | res.emit('error', err) |
| 501 | return |
| 502 | } |
| 503 | httpHandler(req, res) |
| 504 | }) |
| 505 | }, opts) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | function ready (cb) { |
| 510 | if (this[kState].readyResolver !== null) { |