generated from fastify/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
125 lines (116 loc) · 4.07 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
'use strict'
const fp = require('fastify-plugin')
const { isAsyncFunction } = require('./lib/is-async-function')
const { ThrottleStream } = require('./lib/throttle-stream')
const { Readable, Stream, pipeline } = require('node:stream')
function fastifyThrottle (fastify, options, done) {
options = Object.assign({}, options)
options.streamPayloads = options.streamPayloads ?? true
options.bufferPayloads = options.bufferPayloads || false
options.stringPayloads = options.stringPayloads || false
options.async = options.async || false
fastify.addHook('onRoute', (routeOptions) => {
const opts = Object.assign({}, options, routeOptions.config?.throttle)
if (opts.bytesPerSecond) {
addRouteThrottleHook(fastify, routeOptions, opts)
}
})
done()
}
async function addRouteThrottleHook (fastify, routeOptions, throttleOptions) {
const hook = 'onSend'
const hookHandler = throttleOnSendHandler(fastify, throttleOptions)
if (Array.isArray(routeOptions[hook])) {
routeOptions[hook].push(hookHandler)
} else if (typeof routeOptions[hook] === 'function') {
routeOptions[hook] = [routeOptions[hook], hookHandler]
} else {
routeOptions[hook] = [hookHandler]
}
}
function throttleOnSendHandler (fastify, throttleOpts) {
const bytesPerSecond = throttleOpts.bytesPerSecond
const pipelineCallback = err => err && fastify.log.error(err)
if (typeof bytesPerSecond === 'number') {
return async function onSendHandler (_request, _reply, payload) {
if (throttleOpts.streamPayloads && payload instanceof Stream) {
return pipeline(
payload,
new ThrottleStream({ bytesPerSecond }),
pipelineCallback
)
}
if (throttleOpts.bufferPayloads && Buffer.isBuffer(payload)) {
return pipeline(
Readable.from(payload),
new ThrottleStream({ bytesPerSecond }),
pipelineCallback
)
}
if (throttleOpts.stringPayloads && typeof payload === 'string') {
return pipeline(
Readable.from(Buffer.from(payload)),
new ThrottleStream({ bytesPerSecond }),
pipelineCallback
)
}
return payload
}
} else if (throttleOpts.async || isAsyncFunction(bytesPerSecond)) {
return async function onSendHandler (request, _reply, payload) {
if (throttleOpts.streamPayloads && payload instanceof Stream) {
return pipeline(
payload,
new ThrottleStream({ bytesPerSecond: await bytesPerSecond(request) }),
pipelineCallback
)
}
if (throttleOpts.bufferPayloads && Buffer.isBuffer(payload)) {
return pipeline(
Readable.from(payload),
new ThrottleStream({ bytesPerSecond: await bytesPerSecond(request) }),
pipelineCallback
)
}
if (throttleOpts.stringPayloads && typeof payload === 'string') {
return pipeline(
Readable.from(Buffer.from(payload)),
new ThrottleStream({ bytesPerSecond: await bytesPerSecond(request) }),
pipelineCallback
)
}
return payload
}
} else {
return async function onSendHandler (request, _reply, payload) {
if (throttleOpts.streamPayloads && payload instanceof Stream) {
return pipeline(
payload,
new ThrottleStream({ bytesPerSecond: bytesPerSecond(request) }),
pipelineCallback
)
}
if (throttleOpts.bufferPayloads && Buffer.isBuffer(payload)) {
return pipeline(
Readable.from(payload),
new ThrottleStream({ bytesPerSecond: bytesPerSecond(request) }),
pipelineCallback
)
}
if (throttleOpts.stringPayloads && typeof payload === 'string') {
return pipeline(
Readable.from(Buffer.from(payload)),
new ThrottleStream({ bytesPerSecond: bytesPerSecond(request) }),
pipelineCallback
)
}
return payload
}
}
}
module.exports = fp(fastifyThrottle, {
fastify: '5.x',
name: '@fastify/throttle'
})
module.exports.default = fastifyThrottle
module.exports.fastifyThrottle = fastifyThrottle