|
1 | 1 | 'use strict'
|
| 2 | +const fp = require('fastify-plugin') |
| 3 | + |
| 4 | +const { Errors } = require('./lib/index') |
| 5 | + |
| 6 | +module.exports = fp( |
| 7 | + function fastifyRacePlugin (fastify, globalOpts, next) { |
| 8 | + const controllers = new WeakMap() |
| 9 | + let error |
| 10 | + |
| 11 | + if (globalOpts != null && typeof globalOpts !== 'object') { |
| 12 | + return next(new Errors.BAD_PARAMS('object', typeof globalOpts)) |
| 13 | + } |
| 14 | + |
| 15 | + globalOpts = Object.assign( |
| 16 | + {}, |
| 17 | + { handleOnError: true, onRequestClosed: null }, |
| 18 | + globalOpts |
| 19 | + ) |
| 20 | + |
| 21 | + if (typeof globalOpts.handleOnError !== 'boolean') { |
| 22 | + error = new Errors.BAD_PARAMS('boolean', typeof globalOpts.handleOnError) |
| 23 | + } else if ( |
| 24 | + globalOpts.onRequestClosed != null && |
| 25 | + typeof globalOpts.onRequestClosed !== 'function' |
| 26 | + ) { |
| 27 | + error = new Errors.BAD_PARAMS( |
| 28 | + 'function', |
| 29 | + typeof globalOpts.onRequestClosed |
| 30 | + ) |
| 31 | + } |
| 32 | + |
| 33 | + fastify.decorateRequest('race', race) |
| 34 | + fastify.addHook('onResponse', fastifyRacingCleaner) |
| 35 | + |
| 36 | + return next(error) |
| 37 | + |
| 38 | + function fastifyRacingCleaner (request, _reply, done) { |
| 39 | + if (controllers.has(request)) { |
| 40 | + const { controller, cbs } = controllers.get(request) |
| 41 | + |
| 42 | + if (controller.signal.aborted === false) { |
| 43 | + for (const cb of cbs) { |
| 44 | + controller.signal.removeEventListener('abort', cb, { |
| 45 | + once: true |
| 46 | + }) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + controllers.delete(request) |
| 51 | + } |
| 52 | + |
| 53 | + done() |
| 54 | + } |
| 55 | + |
| 56 | + function race (opts = globalOpts) { |
| 57 | + const { raw, id: reqId } = this |
| 58 | + const handleError = typeof opts === 'function' ? true : opts.handleOnError |
| 59 | + const cb = typeof opts === 'function' ? opts : opts.onRequestClosed |
| 60 | + |
| 61 | + if (controllers.has(this)) { |
| 62 | + const { controller: ctrl, cbs } = controllers.get(this) |
| 63 | + |
| 64 | + if (ctrl.signal.aborted === true) { |
| 65 | + throw new Errors.ALREADY_ABORTED(reqId) |
| 66 | + } |
| 67 | + |
| 68 | + if (raw.socket.destroyed === true) { |
| 69 | + throw new Errors.SOCKET_CLOSED(reqId) |
| 70 | + } |
| 71 | + |
| 72 | + if (cb != null) { |
| 73 | + ctrl.signal.addEventListener('abort', cb, { |
| 74 | + once: true |
| 75 | + }) |
| 76 | + |
| 77 | + controllers.set(this, { controller: ctrl, cbs: cbs.concat(cb) }) |
| 78 | + } |
| 79 | + |
| 80 | + return ctrl.signal |
| 81 | + } else { |
| 82 | + // eslint-disable-next-line no-undef |
| 83 | + const controller = new AbortController() |
| 84 | + |
| 85 | + if (cb != null) { |
| 86 | + controller.signal.addEventListener('abort', cb, { |
| 87 | + once: true |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + if (cb == null) controller.signal.then = theneable.bind(this) |
| 92 | + |
| 93 | + if (raw.socket.destroyed) { |
| 94 | + throw new Errors.SOCKET_CLOSED(reqId) |
| 95 | + } else { |
| 96 | + raw.once( |
| 97 | + 'close', |
| 98 | + function () { |
| 99 | + if (controllers.has(this)) { |
| 100 | + const { controller: ctrl } = controllers.get(this) |
| 101 | + if (ctrl.signal.aborted === false) controller.abort() |
| 102 | + } |
| 103 | + }.bind(this) |
| 104 | + ) |
| 105 | + |
| 106 | + if (handleError === true || cb != null) { |
| 107 | + raw.once( |
| 108 | + 'error', |
| 109 | + function (err) { |
| 110 | + if (controllers.has(this)) { |
| 111 | + const { controller: ctrl } = controllers.get(this) |
| 112 | + if (ctrl.signal.aborted === false) controller.abort(err) |
| 113 | + } |
| 114 | + }.bind(this) |
| 115 | + ) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + controllers.set(this, { controller, cbs: cb != null ? [cb] : [] }) |
| 120 | + |
| 121 | + return controller.signal |
| 122 | + } |
| 123 | + |
| 124 | + function theneable (resolve, reject) { |
| 125 | + const { controller, cbs } = controllers.get(this) |
| 126 | + |
| 127 | + if (raw.socket.destroyed === true) { |
| 128 | + return reject(Errors.SOCKET_CLOSED(this.id)) |
| 129 | + } |
| 130 | + |
| 131 | + if (controller.signal.aborted === true) { |
| 132 | + return reject(Errors.ALREADY_ABORTED(this.id)) |
| 133 | + } |
| 134 | + |
| 135 | + try { |
| 136 | + controller.signal.addEventListener('abort', theneableHandler, { |
| 137 | + once: true |
| 138 | + }) |
| 139 | + |
| 140 | + controllers.set(this, { |
| 141 | + controller, |
| 142 | + cbs: cbs.concat(theneableHandler) |
| 143 | + }) |
| 144 | + } catch (err) { |
| 145 | + reject(err) |
| 146 | + } |
| 147 | + |
| 148 | + function theneableHandler (evt) { |
| 149 | + const event = { |
| 150 | + type: evt.type, |
| 151 | + reason: controller.signal?.reason |
| 152 | + } |
| 153 | + |
| 154 | + resolve(event) |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + }, |
| 159 | + { |
| 160 | + fastify: '>=3.24.1', |
| 161 | + name: 'fastify-racing' |
| 162 | + } |
| 163 | +) |
0 commit comments