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