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