-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.ts
More file actions
164 lines (147 loc) · 5.35 KB
/
header.ts
File metadata and controls
164 lines (147 loc) · 5.35 KB
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
164
/**
* Reference code
* https://github.com/express-rate-limit/express-rate-limit/blob/main/source/headers.ts
*/
import { Buffer } from 'node:buffer';
import { createHash } from 'node:crypto';
import { RateLimitInfo } from './types.js';
import { GamanHeader } from '@gaman/core';
export const SUPPORTED_DRAFT_VERSIONS = [
'draft-6',
'draft-7',
'draft-8',
] as const;
/**
* Returns the number of seconds left for the window to reset. Uses `windowMs`
* in case the store doesn't return a `resetTime`.
*
* @param ttl {number | undefined} - The window length.
* @param resetTime {Date | undefined} - The timestamp at which the store window resets.
*/
const getResetSeconds = (ttl: number, resetTime?: Date): number => {
let resetSeconds: number;
if (resetTime) {
const deltaSeconds = Math.ceil((resetTime.getTime() - Date.now()) / 1000);
resetSeconds = Math.max(0, deltaSeconds);
} else {
// This isn't really correct, but the field is required by the spec in `draft-7`,
// so this is the best we can do. The validator should have already logged a
// warning by this point.
resetSeconds = Math.ceil(ttl / 1000);
}
return resetSeconds;
};
/**
* Returns the hash of the identifier, truncated to 12 bytes, and then converted
* to base64 so that it can be used as a 16 byte partition key. The 16-byte limit
* is arbitrary, and follows from the examples given in the 8th draft.
*
* @param key {string} - The identifier to hash.
*/
const getPartitionKey = (key: string): string => {
const hash = createHash('sha256');
hash.update(key);
const partitionKey = hash.digest('hex').slice(0, 12);
return Buffer.from(partitionKey).toString('base64');
};
/**
* Sets `X-RateLimit-*` headers on a response.
*/
export const setLegacyHeaders = (
header: GamanHeader,
info: RateLimitInfo,
): void => {
header.set('X-RateLimit-Limit', info.limit.toString());
header.set('X-RateLimit-Remaining', info.remaining.toString());
// If we have a resetTime, also provide the current date to help avoid
// issues with incorrect clocks.
if (info.reset instanceof Date) {
header.set('Date', new Date().toUTCString());
header.set(
'X-RateLimit-Reset',
Math.ceil(info.reset.getTime() / 1000).toString(),
);
}
};
/**
* Sets `RateLimit-*`` headers based on the sixth draft of the IETF specification.
* See https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-06.
*
* @param header {GamanHeader} - The gaman header object to set headers on.
* @param info {RateLimitInfo} - The rate limit info, used to set the headers.
* @param windowMs {number} - The window length.
*/
export const setDraft6Headers = (
header: GamanHeader,
info: RateLimitInfo,
windowMs: number,
): void => {
const windowSeconds = Math.ceil(windowMs / 1000);
const resetSeconds = getResetSeconds(windowMs, info.reset);
header.set('RateLimit-Policy', `${info.limit};w=${windowSeconds}`);
header.set('RateLimit-Limit', info.limit.toString());
header.set('RateLimit-Remaining', info.remaining.toString());
// Set this header only if the store returns a `resetTime`.
header.set('RateLimit-Reset', resetSeconds.toString());
};
/**
* Sets `RateLimit` & `RateLimit-Policy` headers based on the seventh draft of the spec.
* See https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-07.
*
* @param header {GamanHeader} - The gaman header object to set headers on.
* @param info {RateLimitInfo} - The rate limit info, used to set the headers.
* @param windowMs {number} - The window length.
*/
export const setDraft7Headers = (
header: GamanHeader,
info: RateLimitInfo,
windowMs: number,
): void => {
const windowSeconds = Math.ceil(windowMs / 1000);
const resetSeconds = getResetSeconds(windowMs, info.reset);
header.set('RateLimit-Policy', `${info.limit};w=${windowSeconds}`);
header.set(
'RateLimit',
`limit=${info.limit}, remaining=${info.remaining}, reset=${resetSeconds}`,
);
};
/**
* Sets `RateLimit` & `RateLimit-Policy` headers based on the eighth draft of the spec.
* See https://datatracker.ietf.org/doc/html/draft-ietf-httpapi-ratelimit-headers-08.
*
* @param header {GamanHeader} - The gaman header object to set headers on.
* @param info {RateLimitInfo} - The rate limit info, used to set the headers.
* @param windowMs {number} - The window length.
* @param name {string} - The name of the quota policy.
* @param key {string} - The unique string identifying the client.
*/
export const setDraft8Headers = (
header: GamanHeader,
info: RateLimitInfo,
windowMs: number,
name: string,
key: string,
): void => {
const windowSeconds = Math.ceil(windowMs / 1000);
const resetSeconds = getResetSeconds(windowMs, info.reset);
const partitionKey = getPartitionKey(key);
const _header = `r=${info.remaining}; t=${resetSeconds}`;
const policy = `q=${info.limit}; w=${windowSeconds}; pk=:${partitionKey}:`;
header.set('RateLimit', `"${name}"; ${_header}`);
header.set('RateLimit-Policy', `"${name}"; ${policy}`);
};
/**
* Sets the `Retry-After` header.
*
* @param header {GamanHeader} - The gaman header object to set headers on.
* @param info {RateLimitInfo} - The rate limit info, used to set the headers.
* @param ttl {number} - The window length.
*/
export const setRetryAfterHeader = (
header: GamanHeader,
info: RateLimitInfo,
ttl: number,
): void => {
const resetSeconds = getResetSeconds(ttl, info.reset);
header.set('Retry-After', resetSeconds.toString());
};