Skip to content

Commit

Permalink
Merging 41ad9dc into trunk-temp/pr-865/71ea0ecb-1eac-4149-a539-249b1c…
Browse files Browse the repository at this point in the history
…1ac021
  • Loading branch information
trunk-io[bot] authored Jun 4, 2024
2 parents edd99a1 + 41ad9dc commit 0ba53a9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 67 deletions.
74 changes: 44 additions & 30 deletions decorate/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logger from "@arcjet/logger";
import format from "@arcjet/sprintf";
import {
ArcjetDecision,
ArcjetRateLimitReason,
Expand Down Expand Up @@ -182,7 +182,7 @@ export function setRateLimitHeaders(
const policies = new Map<number, number>();
for (const reason of rateLimitReasons) {
if (policies.has(reason.max)) {
logger.error(
console.error(
"Invalid rate limit policy—two policies should not share the same limit",
);
return;
Expand All @@ -194,7 +194,7 @@ export function setRateLimitHeaders(
typeof reason.remaining !== "number" ||
typeof reason.reset !== "number"
) {
logger.error("Invalid rate limit encountered: %s", reason);
console.error(format("Invalid rate limit encountered: %o", reason));
return;
}

Expand All @@ -218,7 +218,9 @@ export function setRateLimitHeaders(
typeof decision.reason.remaining !== "number" ||
typeof decision.reason.reset !== "number"
) {
logger.error("Invalid rate limit encountered: %s", decision.reason);
console.error(
format("Invalid rate limit encountered: %o", decision.reason),
);
return;
}

Expand All @@ -231,17 +233,21 @@ export function setRateLimitHeaders(

if (isHeaderLike(value)) {
if (value.has("RateLimit")) {
logger.warn(
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
value.get("RateLimit"),
limit,
console.warn(
format(
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
value.get("RateLimit"),
limit,
),
);
}
if (value.has("RateLimit-Policy")) {
logger.warn(
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
value.get("RateLimit-Policy"),
limit,
console.warn(
format(
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
value.get("RateLimit-Policy"),
limit,
),
);
}

Expand All @@ -254,17 +260,21 @@ export function setRateLimitHeaders(

if (isResponseLike(value)) {
if (value.headers.has("RateLimit")) {
logger.warn(
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
value.headers.get("RateLimit"),
limit,
console.warn(
format(
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
value.headers.get("RateLimit"),
limit,
),
);
}
if (value.headers.has("RateLimit-Policy")) {
logger.warn(
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
value.headers.get("RateLimit-Policy"),
limit,
console.warn(
format(
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
value.headers.get("RateLimit-Policy"),
limit,
),
);
}

Expand All @@ -277,25 +287,29 @@ export function setRateLimitHeaders(

if (isOutgoingMessageLike(value)) {
if (value.headersSent) {
logger.error(
console.error(
"Headers have already been sent—cannot set RateLimit header",
);
return;
}

if (value.hasHeader("RateLimit")) {
logger.warn(
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
value.getHeader("RateLimit"),
limit,
console.warn(
format(
"Response already contains `RateLimit` header\n Original: %s\n New: %s",
value.getHeader("RateLimit"),
limit,
),
);
}

if (value.hasHeader("RateLimit-Policy")) {
logger.warn(
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
value.getHeader("RateLimit-Policy"),
limit,
console.warn(
format(
"Response already contains `RateLimit-Policy` header\n Original: %s\n New: %s",
value.getHeader("RateLimit-Policy"),
limit,
),
);
}

Expand All @@ -306,7 +320,7 @@ export function setRateLimitHeaders(
return;
}

logger.debug(
console.debug(
"Cannot determine if response is Response or OutgoingMessage type",
);
}
4 changes: 2 additions & 2 deletions decorate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
},
"dependencies": {
"@arcjet/logger": "1.0.0-alpha.13",
"@arcjet/protocol": "1.0.0-alpha.13"
"@arcjet/protocol": "1.0.0-alpha.13",
"@arcjet/sprintf": "1.0.0-alpha.13"
},
"devDependencies": {
"@arcjet/eslint-config": "1.0.0-alpha.13",
Expand Down
Loading

0 comments on commit 0ba53a9

Please sign in to comment.