@arcjet/decorate 1.0.0-alpha.13 → 1.0.0-alpha.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/index.js +12 -12
- package/index.ts +44 -30
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<a href="https://arcjet.com" target="_arcjet-home">
|
|
2
2
|
<picture>
|
|
3
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://arcjet.com/arcjet-
|
|
4
|
-
<img src="https://arcjet.com/arcjet-
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://arcjet.com/logo/arcjet-dark-lockup-voyage-horizontal.svg">
|
|
4
|
+
<img src="https://arcjet.com/logo/arcjet-light-lockup-voyage-horizontal.svg" alt="Arcjet Logo" height="128" width="auto">
|
|
5
5
|
</picture>
|
|
6
6
|
</a>
|
|
7
7
|
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import format from '@arcjet/sprintf';
|
|
2
2
|
|
|
3
3
|
function isHeaderLike(value) {
|
|
4
4
|
if ("has" in value &&
|
|
@@ -89,14 +89,14 @@ function setRateLimitHeaders(value, decision) {
|
|
|
89
89
|
const policies = new Map();
|
|
90
90
|
for (const reason of rateLimitReasons) {
|
|
91
91
|
if (policies.has(reason.max)) {
|
|
92
|
-
|
|
92
|
+
console.error("Invalid rate limit policy—two policies should not share the same limit");
|
|
93
93
|
return;
|
|
94
94
|
}
|
|
95
95
|
if (typeof reason.max !== "number" ||
|
|
96
96
|
typeof reason.window !== "number" ||
|
|
97
97
|
typeof reason.remaining !== "number" ||
|
|
98
98
|
typeof reason.reset !== "number") {
|
|
99
|
-
|
|
99
|
+
console.error(format("Invalid rate limit encountered: %o", reason));
|
|
100
100
|
return;
|
|
101
101
|
}
|
|
102
102
|
policies.set(reason.max, reason.window);
|
|
@@ -116,7 +116,7 @@ function setRateLimitHeaders(value, decision) {
|
|
|
116
116
|
typeof decision.reason.window !== "number" ||
|
|
117
117
|
typeof decision.reason.remaining !== "number" ||
|
|
118
118
|
typeof decision.reason.reset !== "number") {
|
|
119
|
-
|
|
119
|
+
console.error(format("Invalid rate limit encountered: %o", decision.reason));
|
|
120
120
|
return;
|
|
121
121
|
}
|
|
122
122
|
limit = toLimitString(decision.reason);
|
|
@@ -128,10 +128,10 @@ function setRateLimitHeaders(value, decision) {
|
|
|
128
128
|
}
|
|
129
129
|
if (isHeaderLike(value)) {
|
|
130
130
|
if (value.has("RateLimit")) {
|
|
131
|
-
|
|
131
|
+
console.warn(format("Response already contains `RateLimit` header\n Original: %s\n New: %s", value.get("RateLimit"), limit));
|
|
132
132
|
}
|
|
133
133
|
if (value.has("RateLimit-Policy")) {
|
|
134
|
-
|
|
134
|
+
console.warn(format("Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s", value.get("RateLimit-Policy"), limit));
|
|
135
135
|
}
|
|
136
136
|
value.set("RateLimit", limit);
|
|
137
137
|
value.set("RateLimit-Policy", policy);
|
|
@@ -140,10 +140,10 @@ function setRateLimitHeaders(value, decision) {
|
|
|
140
140
|
}
|
|
141
141
|
if (isResponseLike(value)) {
|
|
142
142
|
if (value.headers.has("RateLimit")) {
|
|
143
|
-
|
|
143
|
+
console.warn(format("Response already contains `RateLimit` header\n Original: %s\n New: %s", value.headers.get("RateLimit"), limit));
|
|
144
144
|
}
|
|
145
145
|
if (value.headers.has("RateLimit-Policy")) {
|
|
146
|
-
|
|
146
|
+
console.warn(format("Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s", value.headers.get("RateLimit-Policy"), limit));
|
|
147
147
|
}
|
|
148
148
|
value.headers.set("RateLimit", limit);
|
|
149
149
|
value.headers.set("RateLimit-Policy", policy);
|
|
@@ -152,21 +152,21 @@ function setRateLimitHeaders(value, decision) {
|
|
|
152
152
|
}
|
|
153
153
|
if (isOutgoingMessageLike(value)) {
|
|
154
154
|
if (value.headersSent) {
|
|
155
|
-
|
|
155
|
+
console.error("Headers have already been sent—cannot set RateLimit header");
|
|
156
156
|
return;
|
|
157
157
|
}
|
|
158
158
|
if (value.hasHeader("RateLimit")) {
|
|
159
|
-
|
|
159
|
+
console.warn(format("Response already contains `RateLimit` header\n Original: %s\n New: %s", value.getHeader("RateLimit"), limit));
|
|
160
160
|
}
|
|
161
161
|
if (value.hasHeader("RateLimit-Policy")) {
|
|
162
|
-
|
|
162
|
+
console.warn(format("Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s", value.getHeader("RateLimit-Policy"), limit));
|
|
163
163
|
}
|
|
164
164
|
value.setHeader("RateLimit", limit);
|
|
165
165
|
value.setHeader("RateLimit-Policy", policy);
|
|
166
166
|
// The response was handled
|
|
167
167
|
return;
|
|
168
168
|
}
|
|
169
|
-
|
|
169
|
+
console.debug("Cannot determine if response is Response or OutgoingMessage type");
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
export { setRateLimitHeaders };
|
package/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import format from "@arcjet/sprintf";
|
|
2
2
|
import {
|
|
3
3
|
ArcjetDecision,
|
|
4
4
|
ArcjetRateLimitReason,
|
|
@@ -182,7 +182,7 @@ export function setRateLimitHeaders(
|
|
|
182
182
|
const policies = new Map<number, number>();
|
|
183
183
|
for (const reason of rateLimitReasons) {
|
|
184
184
|
if (policies.has(reason.max)) {
|
|
185
|
-
|
|
185
|
+
console.error(
|
|
186
186
|
"Invalid rate limit policy—two policies should not share the same limit",
|
|
187
187
|
);
|
|
188
188
|
return;
|
|
@@ -194,7 +194,7 @@ export function setRateLimitHeaders(
|
|
|
194
194
|
typeof reason.remaining !== "number" ||
|
|
195
195
|
typeof reason.reset !== "number"
|
|
196
196
|
) {
|
|
197
|
-
|
|
197
|
+
console.error(format("Invalid rate limit encountered: %o", reason));
|
|
198
198
|
return;
|
|
199
199
|
}
|
|
200
200
|
|
|
@@ -218,7 +218,9 @@ export function setRateLimitHeaders(
|
|
|
218
218
|
typeof decision.reason.remaining !== "number" ||
|
|
219
219
|
typeof decision.reason.reset !== "number"
|
|
220
220
|
) {
|
|
221
|
-
|
|
221
|
+
console.error(
|
|
222
|
+
format("Invalid rate limit encountered: %o", decision.reason),
|
|
223
|
+
);
|
|
222
224
|
return;
|
|
223
225
|
}
|
|
224
226
|
|
|
@@ -231,17 +233,21 @@ export function setRateLimitHeaders(
|
|
|
231
233
|
|
|
232
234
|
if (isHeaderLike(value)) {
|
|
233
235
|
if (value.has("RateLimit")) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
236
|
+
console.warn(
|
|
237
|
+
format(
|
|
238
|
+
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
|
|
239
|
+
value.get("RateLimit"),
|
|
240
|
+
limit,
|
|
241
|
+
),
|
|
238
242
|
);
|
|
239
243
|
}
|
|
240
244
|
if (value.has("RateLimit-Policy")) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
+
console.warn(
|
|
246
|
+
format(
|
|
247
|
+
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
|
|
248
|
+
value.get("RateLimit-Policy"),
|
|
249
|
+
limit,
|
|
250
|
+
),
|
|
245
251
|
);
|
|
246
252
|
}
|
|
247
253
|
|
|
@@ -254,17 +260,21 @@ export function setRateLimitHeaders(
|
|
|
254
260
|
|
|
255
261
|
if (isResponseLike(value)) {
|
|
256
262
|
if (value.headers.has("RateLimit")) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
263
|
+
console.warn(
|
|
264
|
+
format(
|
|
265
|
+
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
|
|
266
|
+
value.headers.get("RateLimit"),
|
|
267
|
+
limit,
|
|
268
|
+
),
|
|
261
269
|
);
|
|
262
270
|
}
|
|
263
271
|
if (value.headers.has("RateLimit-Policy")) {
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
272
|
+
console.warn(
|
|
273
|
+
format(
|
|
274
|
+
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
|
|
275
|
+
value.headers.get("RateLimit-Policy"),
|
|
276
|
+
limit,
|
|
277
|
+
),
|
|
268
278
|
);
|
|
269
279
|
}
|
|
270
280
|
|
|
@@ -277,25 +287,29 @@ export function setRateLimitHeaders(
|
|
|
277
287
|
|
|
278
288
|
if (isOutgoingMessageLike(value)) {
|
|
279
289
|
if (value.headersSent) {
|
|
280
|
-
|
|
290
|
+
console.error(
|
|
281
291
|
"Headers have already been sent—cannot set RateLimit header",
|
|
282
292
|
);
|
|
283
293
|
return;
|
|
284
294
|
}
|
|
285
295
|
|
|
286
296
|
if (value.hasHeader("RateLimit")) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
297
|
+
console.warn(
|
|
298
|
+
format(
|
|
299
|
+
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
|
|
300
|
+
value.getHeader("RateLimit"),
|
|
301
|
+
limit,
|
|
302
|
+
),
|
|
291
303
|
);
|
|
292
304
|
}
|
|
293
305
|
|
|
294
306
|
if (value.hasHeader("RateLimit-Policy")) {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
307
|
+
console.warn(
|
|
308
|
+
format(
|
|
309
|
+
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
|
|
310
|
+
value.getHeader("RateLimit-Policy"),
|
|
311
|
+
limit,
|
|
312
|
+
),
|
|
299
313
|
);
|
|
300
314
|
}
|
|
301
315
|
|
|
@@ -306,7 +320,7 @@ export function setRateLimitHeaders(
|
|
|
306
320
|
return;
|
|
307
321
|
}
|
|
308
322
|
|
|
309
|
-
|
|
323
|
+
console.debug(
|
|
310
324
|
"Cannot determine if response is Response or OutgoingMessage type",
|
|
311
325
|
);
|
|
312
326
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcjet/decorate",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.15",
|
|
4
4
|
"description": "Arcjet utilities for decorating responses with information",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://arcjet.com",
|
|
@@ -40,15 +40,15 @@
|
|
|
40
40
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@arcjet/
|
|
44
|
-
"@arcjet/
|
|
43
|
+
"@arcjet/protocol": "1.0.0-alpha.15",
|
|
44
|
+
"@arcjet/sprintf": "1.0.0-alpha.15"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@arcjet/eslint-config": "1.0.0-alpha.
|
|
48
|
-
"@arcjet/rollup-config": "1.0.0-alpha.
|
|
49
|
-
"@arcjet/tsconfig": "1.0.0-alpha.
|
|
47
|
+
"@arcjet/eslint-config": "1.0.0-alpha.15",
|
|
48
|
+
"@arcjet/rollup-config": "1.0.0-alpha.15",
|
|
49
|
+
"@arcjet/tsconfig": "1.0.0-alpha.15",
|
|
50
50
|
"@jest/globals": "29.7.0",
|
|
51
|
-
"@rollup/wasm-node": "4.
|
|
51
|
+
"@rollup/wasm-node": "4.18.0",
|
|
52
52
|
"@types/node": "18.18.0",
|
|
53
53
|
"jest": "29.7.0",
|
|
54
54
|
"typescript": "5.4.5"
|