@fefade/common 1.0.3 → 1.0.5

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 CHANGED
@@ -1,13 +1,11 @@
1
- import { IRateLimiterOptions } from 'rate-limiter-flexible';
1
+ declare function export_default$7(lang: string): string;
2
2
 
3
- declare function export_default$5(lang: string): string;
4
-
5
- declare function export_default$4<E extends string>(url: string): {
3
+ declare function export_default$6<E extends string>(url: string): {
6
4
  on: <K extends keyof E>(event: K, handler: (data: E[K]) => void) => void;
7
5
  close: () => void;
8
6
  };
9
7
 
10
- declare function export_default$3(): {
8
+ declare function export_default$5(): {
11
9
  connect: () => Response;
12
10
  broadcast: <E extends string, T>(event: E, data: T) => void;
13
11
  };
@@ -17,7 +15,7 @@ type Props = {
17
15
  domain: string;
18
16
  cookieDomain: string;
19
17
  };
20
- declare function export_default$2({ origin, domain, cookieDomain }: Props): boolean;
18
+ declare function export_default$4({ origin, domain, cookieDomain }: Props): boolean;
21
19
 
22
20
  declare class Errors extends Error {
23
21
  code: string;
@@ -25,9 +23,22 @@ declare class Errors extends Error {
25
23
  constructor(message?: string, code?: string, status?: number);
26
24
  }
27
25
 
28
- declare function export_default$1(err: unknown): Errors;
26
+ declare function export_default$3(err: unknown): Errors;
27
+
28
+ declare function export_default$2<T extends string>(path: string | T, params?: string | string[][] | Record<string, string> | URLSearchParams | undefined): string;
29
+
30
+ type HasRequest<T = {}> = {
31
+ request: Request;
32
+ } & T;
33
+ type MaybePromise<T> = T | Promise<T>;
34
+ type Handler<E extends HasRequest> = (event: E) => MaybePromise<Response>;
35
+
36
+ type RateLimiterLike = {
37
+ consume: (ip: string) => Promise<unknown>;
38
+ };
39
+ declare function export_default$1<E extends HasRequest>(handler: Handler<E>, limiter: RateLimiterLike): Handler<E>;
29
40
 
30
- declare function export_default<T extends string>(path: string | T, params?: string | string[][] | Record<string, string> | URLSearchParams | undefined): string;
41
+ declare function export_default<E extends HasRequest>(handler: Handler<E>, secretKey: string): Handler<E>;
31
42
 
32
43
  declare const ORG_NAME = "Fefade";
33
44
  declare const ACRONYM = "ff";
@@ -42,8 +53,7 @@ declare const URLS: {
42
53
  play_store: string;
43
54
  github: string;
44
55
  };
45
- declare const LANG_STRATEGY: string[];
46
- declare const RATE_LIMIT_DEFAULT_OPTIONS: IRateLimiterOptions;
56
+ declare const LANG_STRATEGY: ("cookie" | "baseLocale" | "globalVariable" | "url" | "preferredLanguage" | "localStorage")[];
47
57
 
48
58
  declare const constants_ACRONYM: typeof ACRONYM;
49
59
  declare const constants_DEFAULT_LOCALE: typeof DEFAULT_LOCALE;
@@ -51,11 +61,10 @@ declare const constants_IS_PROD: typeof IS_PROD;
51
61
  declare const constants_LANG_STRATEGY: typeof LANG_STRATEGY;
52
62
  declare const constants_ORG_NAME: typeof ORG_NAME;
53
63
  declare const constants_PROTOCOL: typeof PROTOCOL;
54
- declare const constants_RATE_LIMIT_DEFAULT_OPTIONS: typeof RATE_LIMIT_DEFAULT_OPTIONS;
55
64
  declare const constants_SUPPORT_EMAIL: typeof SUPPORT_EMAIL;
56
65
  declare const constants_URLS: typeof URLS;
57
66
  declare namespace constants {
58
- export { constants_ACRONYM as ACRONYM, constants_DEFAULT_LOCALE as DEFAULT_LOCALE, constants_IS_PROD as IS_PROD, constants_LANG_STRATEGY as LANG_STRATEGY, constants_ORG_NAME as ORG_NAME, constants_PROTOCOL as PROTOCOL, constants_RATE_LIMIT_DEFAULT_OPTIONS as RATE_LIMIT_DEFAULT_OPTIONS, constants_SUPPORT_EMAIL as SUPPORT_EMAIL, constants_URLS as URLS };
67
+ export { constants_ACRONYM as ACRONYM, constants_DEFAULT_LOCALE as DEFAULT_LOCALE, constants_IS_PROD as IS_PROD, constants_LANG_STRATEGY as LANG_STRATEGY, constants_ORG_NAME as ORG_NAME, constants_PROTOCOL as PROTOCOL, constants_SUPPORT_EMAIL as SUPPORT_EMAIL, constants_URLS as URLS };
59
68
  }
60
69
 
61
- export { constants as Constants, export_default$1 as handleError, export_default$2 as isAllowedOrigin, export_default$5 as normalizeLang, export_default as pathWithParams, export_default$4 as sseClient, export_default$3 as sseServer };
70
+ export { constants as Constants, export_default$3 as handleError, export_default$4 as isAllowedOrigin, export_default$7 as normalizeLang, export_default$2 as pathWithParams, export_default$6 as sseClient, export_default$5 as sseServer, export_default as validateTurnstile, export_default$1 as withRateLimit };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,11 @@
1
- import { IRateLimiterOptions } from 'rate-limiter-flexible';
1
+ declare function export_default$7(lang: string): string;
2
2
 
3
- declare function export_default$5(lang: string): string;
4
-
5
- declare function export_default$4<E extends string>(url: string): {
3
+ declare function export_default$6<E extends string>(url: string): {
6
4
  on: <K extends keyof E>(event: K, handler: (data: E[K]) => void) => void;
7
5
  close: () => void;
8
6
  };
9
7
 
10
- declare function export_default$3(): {
8
+ declare function export_default$5(): {
11
9
  connect: () => Response;
12
10
  broadcast: <E extends string, T>(event: E, data: T) => void;
13
11
  };
@@ -17,7 +15,7 @@ type Props = {
17
15
  domain: string;
18
16
  cookieDomain: string;
19
17
  };
20
- declare function export_default$2({ origin, domain, cookieDomain }: Props): boolean;
18
+ declare function export_default$4({ origin, domain, cookieDomain }: Props): boolean;
21
19
 
22
20
  declare class Errors extends Error {
23
21
  code: string;
@@ -25,9 +23,22 @@ declare class Errors extends Error {
25
23
  constructor(message?: string, code?: string, status?: number);
26
24
  }
27
25
 
28
- declare function export_default$1(err: unknown): Errors;
26
+ declare function export_default$3(err: unknown): Errors;
27
+
28
+ declare function export_default$2<T extends string>(path: string | T, params?: string | string[][] | Record<string, string> | URLSearchParams | undefined): string;
29
+
30
+ type HasRequest<T = {}> = {
31
+ request: Request;
32
+ } & T;
33
+ type MaybePromise<T> = T | Promise<T>;
34
+ type Handler<E extends HasRequest> = (event: E) => MaybePromise<Response>;
35
+
36
+ type RateLimiterLike = {
37
+ consume: (ip: string) => Promise<unknown>;
38
+ };
39
+ declare function export_default$1<E extends HasRequest>(handler: Handler<E>, limiter: RateLimiterLike): Handler<E>;
29
40
 
30
- declare function export_default<T extends string>(path: string | T, params?: string | string[][] | Record<string, string> | URLSearchParams | undefined): string;
41
+ declare function export_default<E extends HasRequest>(handler: Handler<E>, secretKey: string): Handler<E>;
31
42
 
32
43
  declare const ORG_NAME = "Fefade";
33
44
  declare const ACRONYM = "ff";
@@ -42,8 +53,7 @@ declare const URLS: {
42
53
  play_store: string;
43
54
  github: string;
44
55
  };
45
- declare const LANG_STRATEGY: string[];
46
- declare const RATE_LIMIT_DEFAULT_OPTIONS: IRateLimiterOptions;
56
+ declare const LANG_STRATEGY: ("cookie" | "baseLocale" | "globalVariable" | "url" | "preferredLanguage" | "localStorage")[];
47
57
 
48
58
  declare const constants_ACRONYM: typeof ACRONYM;
49
59
  declare const constants_DEFAULT_LOCALE: typeof DEFAULT_LOCALE;
@@ -51,11 +61,10 @@ declare const constants_IS_PROD: typeof IS_PROD;
51
61
  declare const constants_LANG_STRATEGY: typeof LANG_STRATEGY;
52
62
  declare const constants_ORG_NAME: typeof ORG_NAME;
53
63
  declare const constants_PROTOCOL: typeof PROTOCOL;
54
- declare const constants_RATE_LIMIT_DEFAULT_OPTIONS: typeof RATE_LIMIT_DEFAULT_OPTIONS;
55
64
  declare const constants_SUPPORT_EMAIL: typeof SUPPORT_EMAIL;
56
65
  declare const constants_URLS: typeof URLS;
57
66
  declare namespace constants {
58
- export { constants_ACRONYM as ACRONYM, constants_DEFAULT_LOCALE as DEFAULT_LOCALE, constants_IS_PROD as IS_PROD, constants_LANG_STRATEGY as LANG_STRATEGY, constants_ORG_NAME as ORG_NAME, constants_PROTOCOL as PROTOCOL, constants_RATE_LIMIT_DEFAULT_OPTIONS as RATE_LIMIT_DEFAULT_OPTIONS, constants_SUPPORT_EMAIL as SUPPORT_EMAIL, constants_URLS as URLS };
67
+ export { constants_ACRONYM as ACRONYM, constants_DEFAULT_LOCALE as DEFAULT_LOCALE, constants_IS_PROD as IS_PROD, constants_LANG_STRATEGY as LANG_STRATEGY, constants_ORG_NAME as ORG_NAME, constants_PROTOCOL as PROTOCOL, constants_SUPPORT_EMAIL as SUPPORT_EMAIL, constants_URLS as URLS };
59
68
  }
60
69
 
61
- export { constants as Constants, export_default$1 as handleError, export_default$2 as isAllowedOrigin, export_default$5 as normalizeLang, export_default as pathWithParams, export_default$4 as sseClient, export_default$3 as sseServer };
70
+ export { constants as Constants, export_default$3 as handleError, export_default$4 as isAllowedOrigin, export_default$7 as normalizeLang, export_default$2 as pathWithParams, export_default$6 as sseClient, export_default$5 as sseServer, export_default as validateTurnstile, export_default$1 as withRateLimit };
package/dist/index.js CHANGED
@@ -26,7 +26,9 @@ __export(index_exports, {
26
26
  normalizeLang: () => normalizeLang_default,
27
27
  pathWithParams: () => pathWithParams_default,
28
28
  sseClient: () => sseClient_default,
29
- sseServer: () => sseServer_default
29
+ sseServer: () => sseServer_default,
30
+ validateTurnstile: () => validateTurnstile_default,
31
+ withRateLimit: () => withRateLimit_default
30
32
  });
31
33
  module.exports = __toCommonJS(index_exports);
32
34
 
@@ -14033,6 +14035,74 @@ function pathWithParams_default(path, params) {
14033
14035
  return queryString ? `${basePath}?${queryString}` : basePath;
14034
14036
  }
14035
14037
 
14038
+ // src/getIp.ts
14039
+ function getIp_default(request) {
14040
+ return request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || request.headers.get("CF-Connecting-IP") || request.headers.get("X-Forwarded-For") || "unknown";
14041
+ }
14042
+
14043
+ // src/withRateLimit.ts
14044
+ function isRateLimiterRes(err) {
14045
+ return err && typeof err === "object" && "msBeforeNext" in err && "remainingPoints" in err;
14046
+ }
14047
+ function withRateLimit_default(handler, limiter) {
14048
+ return async (event) => {
14049
+ const ip = getIp_default(event.request);
14050
+ try {
14051
+ await limiter.consume(ip);
14052
+ } catch (err) {
14053
+ if (isRateLimiterRes(err)) {
14054
+ Response.json(
14055
+ { error: "Too many requests" },
14056
+ {
14057
+ status: 429,
14058
+ headers: {
14059
+ "Retry-After": "1800",
14060
+ "Content-Type": "application/json"
14061
+ }
14062
+ }
14063
+ );
14064
+ }
14065
+ return Response.json(
14066
+ { error: "Internal server error" },
14067
+ {
14068
+ status: 500
14069
+ }
14070
+ );
14071
+ }
14072
+ return handler({ ...event, ip });
14073
+ };
14074
+ }
14075
+
14076
+ // src/validateTurnstile.ts
14077
+ function validateTurnstile_default(handler, secretKey) {
14078
+ return async (event) => {
14079
+ const body = await event.request.json();
14080
+ const token = body["cf-turnstile-response"];
14081
+ const ip = getIp_default(event.request);
14082
+ if (!token) {
14083
+ return Response.json({ error: "Captcha not sent" });
14084
+ }
14085
+ const response = await fetch(
14086
+ "https://challenges.cloudflare.com/turnstile/v0/siteverify",
14087
+ {
14088
+ method: "POST",
14089
+ headers: {
14090
+ "Content-Type": "application/json"
14091
+ },
14092
+ body: JSON.stringify({
14093
+ secret: secretKey,
14094
+ response: token.toString(),
14095
+ remoteip: ip
14096
+ })
14097
+ }
14098
+ );
14099
+ if (!response.ok) {
14100
+ return Response.json({ error: "Invalid verification" }, { status: 400 });
14101
+ }
14102
+ return handler({ ...event, ip });
14103
+ };
14104
+ }
14105
+
14036
14106
  // src/constants.ts
14037
14107
  var constants_exports = {};
14038
14108
  __export(constants_exports, {
@@ -14042,7 +14112,6 @@ __export(constants_exports, {
14042
14112
  LANG_STRATEGY: () => LANG_STRATEGY,
14043
14113
  ORG_NAME: () => ORG_NAME,
14044
14114
  PROTOCOL: () => PROTOCOL,
14045
- RATE_LIMIT_DEFAULT_OPTIONS: () => RATE_LIMIT_DEFAULT_OPTIONS,
14046
14115
  SUPPORT_EMAIL: () => SUPPORT_EMAIL,
14047
14116
  URLS: () => URLS
14048
14117
  });
@@ -14067,11 +14136,6 @@ var LANG_STRATEGY = [
14067
14136
  "baseLocale",
14068
14137
  "globalVariable"
14069
14138
  ];
14070
- var RATE_LIMIT_DEFAULT_OPTIONS = {
14071
- points: 3,
14072
- duration: 60 * 60,
14073
- blockDuration: 30 * 60
14074
- };
14075
14139
  // Annotate the CommonJS export names for ESM import in node:
14076
14140
  0 && (module.exports = {
14077
14141
  Constants,
@@ -14080,5 +14144,7 @@ var RATE_LIMIT_DEFAULT_OPTIONS = {
14080
14144
  normalizeLang,
14081
14145
  pathWithParams,
14082
14146
  sseClient,
14083
- sseServer
14147
+ sseServer,
14148
+ validateTurnstile,
14149
+ withRateLimit
14084
14150
  });
package/dist/index.mjs CHANGED
@@ -14007,6 +14007,74 @@ function pathWithParams_default(path, params) {
14007
14007
  return queryString ? `${basePath}?${queryString}` : basePath;
14008
14008
  }
14009
14009
 
14010
+ // src/getIp.ts
14011
+ function getIp_default(request) {
14012
+ return request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || request.headers.get("CF-Connecting-IP") || request.headers.get("X-Forwarded-For") || "unknown";
14013
+ }
14014
+
14015
+ // src/withRateLimit.ts
14016
+ function isRateLimiterRes(err) {
14017
+ return err && typeof err === "object" && "msBeforeNext" in err && "remainingPoints" in err;
14018
+ }
14019
+ function withRateLimit_default(handler, limiter) {
14020
+ return async (event) => {
14021
+ const ip = getIp_default(event.request);
14022
+ try {
14023
+ await limiter.consume(ip);
14024
+ } catch (err) {
14025
+ if (isRateLimiterRes(err)) {
14026
+ Response.json(
14027
+ { error: "Too many requests" },
14028
+ {
14029
+ status: 429,
14030
+ headers: {
14031
+ "Retry-After": "1800",
14032
+ "Content-Type": "application/json"
14033
+ }
14034
+ }
14035
+ );
14036
+ }
14037
+ return Response.json(
14038
+ { error: "Internal server error" },
14039
+ {
14040
+ status: 500
14041
+ }
14042
+ );
14043
+ }
14044
+ return handler({ ...event, ip });
14045
+ };
14046
+ }
14047
+
14048
+ // src/validateTurnstile.ts
14049
+ function validateTurnstile_default(handler, secretKey) {
14050
+ return async (event) => {
14051
+ const body = await event.request.json();
14052
+ const token = body["cf-turnstile-response"];
14053
+ const ip = getIp_default(event.request);
14054
+ if (!token) {
14055
+ return Response.json({ error: "Captcha not sent" });
14056
+ }
14057
+ const response = await fetch(
14058
+ "https://challenges.cloudflare.com/turnstile/v0/siteverify",
14059
+ {
14060
+ method: "POST",
14061
+ headers: {
14062
+ "Content-Type": "application/json"
14063
+ },
14064
+ body: JSON.stringify({
14065
+ secret: secretKey,
14066
+ response: token.toString(),
14067
+ remoteip: ip
14068
+ })
14069
+ }
14070
+ );
14071
+ if (!response.ok) {
14072
+ return Response.json({ error: "Invalid verification" }, { status: 400 });
14073
+ }
14074
+ return handler({ ...event, ip });
14075
+ };
14076
+ }
14077
+
14010
14078
  // src/constants.ts
14011
14079
  var constants_exports = {};
14012
14080
  __export(constants_exports, {
@@ -14016,7 +14084,6 @@ __export(constants_exports, {
14016
14084
  LANG_STRATEGY: () => LANG_STRATEGY,
14017
14085
  ORG_NAME: () => ORG_NAME,
14018
14086
  PROTOCOL: () => PROTOCOL,
14019
- RATE_LIMIT_DEFAULT_OPTIONS: () => RATE_LIMIT_DEFAULT_OPTIONS,
14020
14087
  SUPPORT_EMAIL: () => SUPPORT_EMAIL,
14021
14088
  URLS: () => URLS
14022
14089
  });
@@ -14041,11 +14108,6 @@ var LANG_STRATEGY = [
14041
14108
  "baseLocale",
14042
14109
  "globalVariable"
14043
14110
  ];
14044
- var RATE_LIMIT_DEFAULT_OPTIONS = {
14045
- points: 3,
14046
- duration: 60 * 60,
14047
- blockDuration: 30 * 60
14048
- };
14049
14111
  export {
14050
14112
  constants_exports as Constants,
14051
14113
  handleError_default as handleError,
@@ -14053,5 +14115,7 @@ export {
14053
14115
  normalizeLang_default as normalizeLang,
14054
14116
  pathWithParams_default as pathWithParams,
14055
14117
  sseClient_default as sseClient,
14056
- sseServer_default as sseServer
14118
+ sseServer_default as sseServer,
14119
+ validateTurnstile_default as validateTurnstile,
14120
+ withRateLimit_default as withRateLimit
14057
14121
  };
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.3",
4
+ "version": "1.0.5",
5
5
  "private": false,
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -38,7 +38,6 @@
38
38
  "@changesets/cli": "^2.30.0",
39
39
  "@types/node": "^25.5.2",
40
40
  "prettier": "^3.8.1",
41
- "rate-limiter-flexible": "^10.0.1",
42
41
  "tsup": "^8.5.1",
43
42
  "typescript": "^5.9.3",
44
43
  "zod": "^4.3.6"