@fefade/common 1.0.6 → 1.0.7
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/dist/index.d.mts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +25 -14
- package/dist/index.mjs +25 -14
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -34,7 +34,9 @@ type MaybePromise<T> = T | Promise<T>;
|
|
|
34
34
|
type Handler<E extends HasRequest> = (event: E) => MaybePromise<Response>;
|
|
35
35
|
|
|
36
36
|
type RateLimiterLike = {
|
|
37
|
-
consume
|
|
37
|
+
consume(key: string | number, pointsToConsume?: number, options?: {
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
}): Promise<unknown>;
|
|
38
40
|
};
|
|
39
41
|
declare function export_default$1<E extends HasRequest>(handler: Handler<E>, limiter: RateLimiterLike): Handler<E>;
|
|
40
42
|
|
package/dist/index.d.ts
CHANGED
|
@@ -34,7 +34,9 @@ type MaybePromise<T> = T | Promise<T>;
|
|
|
34
34
|
type Handler<E extends HasRequest> = (event: E) => MaybePromise<Response>;
|
|
35
35
|
|
|
36
36
|
type RateLimiterLike = {
|
|
37
|
-
consume
|
|
37
|
+
consume(key: string | number, pointsToConsume?: number, options?: {
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
}): Promise<unknown>;
|
|
38
40
|
};
|
|
39
41
|
declare function export_default$1<E extends HasRequest>(handler: Handler<E>, limiter: RateLimiterLike): Handler<E>;
|
|
40
42
|
|
package/dist/index.js
CHANGED
|
@@ -14042,16 +14042,16 @@ function getIp_default(request) {
|
|
|
14042
14042
|
|
|
14043
14043
|
// src/withRateLimit.ts
|
|
14044
14044
|
function isRateLimiterRes(err) {
|
|
14045
|
-
return
|
|
14045
|
+
return typeof err === "object" && err !== null && "msBeforeNext" in err && "remainingPoints" in err;
|
|
14046
14046
|
}
|
|
14047
14047
|
function withRateLimit_default(handler, limiter) {
|
|
14048
14048
|
return async (event) => {
|
|
14049
|
-
const
|
|
14049
|
+
const ipAddress = getIp_default(event.request);
|
|
14050
14050
|
try {
|
|
14051
|
-
await limiter.consume(
|
|
14051
|
+
await limiter.consume(ipAddress);
|
|
14052
14052
|
} catch (err) {
|
|
14053
14053
|
if (isRateLimiterRes(err)) {
|
|
14054
|
-
new Response(JSON.stringify({ error: "Too many requests" }), {
|
|
14054
|
+
return new Response(JSON.stringify({ error: "Too many requests" }), {
|
|
14055
14055
|
status: 429,
|
|
14056
14056
|
headers: {
|
|
14057
14057
|
"Retry-After": "1800",
|
|
@@ -14059,11 +14059,9 @@ function withRateLimit_default(handler, limiter) {
|
|
|
14059
14059
|
}
|
|
14060
14060
|
});
|
|
14061
14061
|
}
|
|
14062
|
-
|
|
14063
|
-
status: 500
|
|
14064
|
-
});
|
|
14062
|
+
throw err;
|
|
14065
14063
|
}
|
|
14066
|
-
return handler(
|
|
14064
|
+
return handler(event);
|
|
14067
14065
|
};
|
|
14068
14066
|
}
|
|
14069
14067
|
|
|
@@ -14071,13 +14069,23 @@ function withRateLimit_default(handler, limiter) {
|
|
|
14071
14069
|
function validateTurnstile_default(handler, secretKey) {
|
|
14072
14070
|
return async (event) => {
|
|
14073
14071
|
if (!secretKey) {
|
|
14074
|
-
return new Response(JSON.stringify({ error: "Invalid secret key" })
|
|
14072
|
+
return new Response(JSON.stringify({ error: "Invalid secret key" }), {
|
|
14073
|
+
status: 500,
|
|
14074
|
+
headers: {
|
|
14075
|
+
"Content-Type": "application/json"
|
|
14076
|
+
}
|
|
14077
|
+
});
|
|
14075
14078
|
}
|
|
14076
14079
|
const body = await event.request.json();
|
|
14077
14080
|
const token = body["cf-turnstile-response"];
|
|
14078
|
-
const
|
|
14081
|
+
const ipAddress = getIp_default(event.request);
|
|
14079
14082
|
if (!token) {
|
|
14080
|
-
return new Response(JSON.stringify({ error: "Captcha not sent" })
|
|
14083
|
+
return new Response(JSON.stringify({ error: "Captcha not sent" }), {
|
|
14084
|
+
status: 400,
|
|
14085
|
+
headers: {
|
|
14086
|
+
"Content-Type": "application/json"
|
|
14087
|
+
}
|
|
14088
|
+
});
|
|
14081
14089
|
}
|
|
14082
14090
|
const response = await fetch(
|
|
14083
14091
|
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
@@ -14089,16 +14097,19 @@ function validateTurnstile_default(handler, secretKey) {
|
|
|
14089
14097
|
body: JSON.stringify({
|
|
14090
14098
|
secret: secretKey,
|
|
14091
14099
|
response: token.toString(),
|
|
14092
|
-
remoteip:
|
|
14100
|
+
remoteip: ipAddress
|
|
14093
14101
|
})
|
|
14094
14102
|
}
|
|
14095
14103
|
);
|
|
14096
14104
|
if (!response.ok) {
|
|
14097
14105
|
return new Response(JSON.stringify({ error: "Invalid verification" }), {
|
|
14098
|
-
status: 400
|
|
14106
|
+
status: 400,
|
|
14107
|
+
headers: {
|
|
14108
|
+
"Content-Type": "application/json"
|
|
14109
|
+
}
|
|
14099
14110
|
});
|
|
14100
14111
|
}
|
|
14101
|
-
return handler(
|
|
14112
|
+
return handler(event);
|
|
14102
14113
|
};
|
|
14103
14114
|
}
|
|
14104
14115
|
|
package/dist/index.mjs
CHANGED
|
@@ -14014,16 +14014,16 @@ function getIp_default(request) {
|
|
|
14014
14014
|
|
|
14015
14015
|
// src/withRateLimit.ts
|
|
14016
14016
|
function isRateLimiterRes(err) {
|
|
14017
|
-
return
|
|
14017
|
+
return typeof err === "object" && err !== null && "msBeforeNext" in err && "remainingPoints" in err;
|
|
14018
14018
|
}
|
|
14019
14019
|
function withRateLimit_default(handler, limiter) {
|
|
14020
14020
|
return async (event) => {
|
|
14021
|
-
const
|
|
14021
|
+
const ipAddress = getIp_default(event.request);
|
|
14022
14022
|
try {
|
|
14023
|
-
await limiter.consume(
|
|
14023
|
+
await limiter.consume(ipAddress);
|
|
14024
14024
|
} catch (err) {
|
|
14025
14025
|
if (isRateLimiterRes(err)) {
|
|
14026
|
-
new Response(JSON.stringify({ error: "Too many requests" }), {
|
|
14026
|
+
return new Response(JSON.stringify({ error: "Too many requests" }), {
|
|
14027
14027
|
status: 429,
|
|
14028
14028
|
headers: {
|
|
14029
14029
|
"Retry-After": "1800",
|
|
@@ -14031,11 +14031,9 @@ function withRateLimit_default(handler, limiter) {
|
|
|
14031
14031
|
}
|
|
14032
14032
|
});
|
|
14033
14033
|
}
|
|
14034
|
-
|
|
14035
|
-
status: 500
|
|
14036
|
-
});
|
|
14034
|
+
throw err;
|
|
14037
14035
|
}
|
|
14038
|
-
return handler(
|
|
14036
|
+
return handler(event);
|
|
14039
14037
|
};
|
|
14040
14038
|
}
|
|
14041
14039
|
|
|
@@ -14043,13 +14041,23 @@ function withRateLimit_default(handler, limiter) {
|
|
|
14043
14041
|
function validateTurnstile_default(handler, secretKey) {
|
|
14044
14042
|
return async (event) => {
|
|
14045
14043
|
if (!secretKey) {
|
|
14046
|
-
return new Response(JSON.stringify({ error: "Invalid secret key" })
|
|
14044
|
+
return new Response(JSON.stringify({ error: "Invalid secret key" }), {
|
|
14045
|
+
status: 500,
|
|
14046
|
+
headers: {
|
|
14047
|
+
"Content-Type": "application/json"
|
|
14048
|
+
}
|
|
14049
|
+
});
|
|
14047
14050
|
}
|
|
14048
14051
|
const body = await event.request.json();
|
|
14049
14052
|
const token = body["cf-turnstile-response"];
|
|
14050
|
-
const
|
|
14053
|
+
const ipAddress = getIp_default(event.request);
|
|
14051
14054
|
if (!token) {
|
|
14052
|
-
return new Response(JSON.stringify({ error: "Captcha not sent" })
|
|
14055
|
+
return new Response(JSON.stringify({ error: "Captcha not sent" }), {
|
|
14056
|
+
status: 400,
|
|
14057
|
+
headers: {
|
|
14058
|
+
"Content-Type": "application/json"
|
|
14059
|
+
}
|
|
14060
|
+
});
|
|
14053
14061
|
}
|
|
14054
14062
|
const response = await fetch(
|
|
14055
14063
|
"https://challenges.cloudflare.com/turnstile/v0/siteverify",
|
|
@@ -14061,16 +14069,19 @@ function validateTurnstile_default(handler, secretKey) {
|
|
|
14061
14069
|
body: JSON.stringify({
|
|
14062
14070
|
secret: secretKey,
|
|
14063
14071
|
response: token.toString(),
|
|
14064
|
-
remoteip:
|
|
14072
|
+
remoteip: ipAddress
|
|
14065
14073
|
})
|
|
14066
14074
|
}
|
|
14067
14075
|
);
|
|
14068
14076
|
if (!response.ok) {
|
|
14069
14077
|
return new Response(JSON.stringify({ error: "Invalid verification" }), {
|
|
14070
|
-
status: 400
|
|
14078
|
+
status: 400,
|
|
14079
|
+
headers: {
|
|
14080
|
+
"Content-Type": "application/json"
|
|
14081
|
+
}
|
|
14071
14082
|
});
|
|
14072
14083
|
}
|
|
14073
|
-
return handler(
|
|
14084
|
+
return handler(event);
|
|
14074
14085
|
};
|
|
14075
14086
|
}
|
|
14076
14087
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fefade/common",
|
|
3
3
|
"description": "Containing common resources, constants, images, and utility functions that are used across various projects. This package aims to promote reusability and maintain consistency across different applications by centralizing commonly used assets and code.",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.7",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|