@supacloud/elysia 0.38.0 → 0.38.1
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/http-policy-postgres.d.ts +1 -1
- package/dist/index.js +2 -2
- package/package.json +3 -3
|
@@ -3,7 +3,7 @@ export interface HttpPolicyDatabase {
|
|
|
3
3
|
query(text: string, parameters: readonly unknown[]): Promise<readonly Record<string, unknown>[]>;
|
|
4
4
|
}
|
|
5
5
|
/** Apply through migrations, never from a request. Credentials must be server-only. */
|
|
6
|
-
export declare const HTTP_POLICY_STORE_SQL = "\nCREATE SCHEMA IF NOT EXISTS supacloud_http;\nREVOKE ALL ON SCHEMA supacloud_http FROM PUBLIC;\nCREATE TABLE IF NOT EXISTS supacloud_http.rate_limits (\n key text PRIMARY KEY CHECK (key ~ '^[a-f0-9]{64}$'),\n used bigint NOT NULL CHECK (used > 0),\n reset_at timestamptz NOT NULL\n);\nCREATE INDEX IF NOT EXISTS http_rate_limit_expiry ON supacloud_http.rate_limits(reset_at);\nCREATE TABLE IF NOT EXISTS supacloud_http.response_cache (\n key text PRIMARY KEY CHECK (key ~ '^[a-f0-9]{64}$'),\n body text NOT NULL CHECK (octet_length(body) <= 16777216),\n expires_at timestamptz NOT NULL,\n generation uuid NOT NULL\n);\nALTER TABLE supacloud_http.response_cache ADD COLUMN IF NOT EXISTS generation uuid\n NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000';\nCREATE TABLE IF NOT EXISTS supacloud_http.cache_generation (\n singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),\n generation uuid NOT NULL DEFAULT gen_random_uuid()\n);\nINSERT INTO supacloud_http.cache_generation(singleton) VALUES (true) ON CONFLICT DO NOTHING;\nCREATE INDEX IF NOT EXISTS http_response_cache_expiry ON supacloud_http.response_cache(expires_at);\nREVOKE ALL ON ALL TABLES IN SCHEMA supacloud_http FROM PUBLIC;\nCREATE OR REPLACE FUNCTION supacloud_http.consume_rate_limit(p_key text, p_limit bigint, p_window_ms integer)\nRETURNS TABLE(allowed boolean, remaining bigint, reset_at_ms numeric)\nLANGUAGE plpgsql SET search_path = pg_catalog, supacloud_http AS $rate$\nDECLARE\n entry supacloud_http.rate_limits%ROWTYPE;\n observed_at timestamptz;\nBEGIN\n IF p_limit < 1 OR p_limit > 1000000000 OR p_window_ms < 1 OR p_window_ms > 86400000 THEN\n RAISE EXCEPTION 'Invalid rate limit window';\n END IF;\n LOOP\n INSERT INTO supacloud_http.rate_limits(key, used, reset_at)\n VALUES (p_key, 1, '-infinity') ON CONFLICT DO NOTHING;\n SELECT
|
|
6
|
+
export declare const HTTP_POLICY_STORE_SQL = "\nCREATE SCHEMA IF NOT EXISTS supacloud_http;\nREVOKE ALL ON SCHEMA supacloud_http FROM PUBLIC;\nCREATE TABLE IF NOT EXISTS supacloud_http.rate_limits (\n key text PRIMARY KEY CHECK (key ~ '^[a-f0-9]{64}$'),\n used bigint NOT NULL CHECK (used > 0),\n reset_at timestamptz NOT NULL\n);\nCREATE INDEX IF NOT EXISTS http_rate_limit_expiry ON supacloud_http.rate_limits(reset_at);\nCREATE TABLE IF NOT EXISTS supacloud_http.response_cache (\n key text PRIMARY KEY CHECK (key ~ '^[a-f0-9]{64}$'),\n body text NOT NULL CHECK (octet_length(body) <= 16777216),\n expires_at timestamptz NOT NULL,\n generation uuid NOT NULL\n);\nALTER TABLE supacloud_http.response_cache ADD COLUMN IF NOT EXISTS generation uuid\n NOT NULL DEFAULT '00000000-0000-0000-0000-000000000000';\nCREATE TABLE IF NOT EXISTS supacloud_http.cache_generation (\n singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),\n generation uuid NOT NULL DEFAULT gen_random_uuid()\n);\nINSERT INTO supacloud_http.cache_generation(singleton) VALUES (true) ON CONFLICT DO NOTHING;\nCREATE INDEX IF NOT EXISTS http_response_cache_expiry ON supacloud_http.response_cache(expires_at);\nREVOKE ALL ON ALL TABLES IN SCHEMA supacloud_http FROM PUBLIC;\nCREATE OR REPLACE FUNCTION supacloud_http.consume_rate_limit(p_key text, p_limit bigint, p_window_ms integer)\nRETURNS TABLE(allowed boolean, remaining bigint, reset_at_ms numeric)\nLANGUAGE plpgsql SET search_path = pg_catalog, supacloud_http AS $rate$\nDECLARE\n entry supacloud_http.rate_limits%ROWTYPE;\n observed_at timestamptz;\nBEGIN\n IF p_limit < 1 OR p_limit > 1000000000 OR p_window_ms < 1 OR p_window_ms > 86400000 THEN\n RAISE EXCEPTION 'Invalid rate limit window';\n END IF;\n LOOP\n INSERT INTO supacloud_http.rate_limits(key, used, reset_at)\n VALUES (p_key, 1, '-infinity') ON CONFLICT DO NOTHING;\n SELECT key, used, reset_at INTO entry FROM supacloud_http.rate_limits WHERE key=p_key FOR UPDATE;\n EXIT WHEN FOUND;\n -- Expiry pruning may remove a conflicting row before we acquire its lock.\n END LOOP;\n observed_at := clock_timestamp();\n IF entry.reset_at <= observed_at THEN\n entry.used := 1;\n entry.reset_at := observed_at + p_window_ms * interval '1 millisecond';\n ELSE\n entry.used := least(entry.used + 1, p_limit + 1);\n END IF;\n UPDATE supacloud_http.rate_limits SET used=entry.used, reset_at=entry.reset_at WHERE key=p_key;\n RETURN QUERY SELECT entry.used <= p_limit, greatest(p_limit-entry.used,0),\n extract(epoch FROM entry.reset_at)*1000;\nEND\n$rate$;\nREVOKE ALL ON FUNCTION supacloud_http.consume_rate_limit(text,bigint,integer) FROM PUBLIC;\n";
|
|
7
7
|
/** Independent instances sharing a database share atomic quotas and private cache entries. */
|
|
8
8
|
export declare function createPostgresHttpPolicyStores(database: HttpPolicyDatabase): {
|
|
9
9
|
rateLimitStore: HttpRateLimitStore;
|
package/dist/index.js
CHANGED
|
@@ -573,7 +573,7 @@ BEGIN
|
|
|
573
573
|
LOOP
|
|
574
574
|
INSERT INTO supacloud_http.rate_limits(key, used, reset_at)
|
|
575
575
|
VALUES (p_key, 1, '-infinity') ON CONFLICT DO NOTHING;
|
|
576
|
-
SELECT
|
|
576
|
+
SELECT key, used, reset_at INTO entry FROM supacloud_http.rate_limits WHERE key=p_key FOR UPDATE;
|
|
577
577
|
EXIT WHEN FOUND;
|
|
578
578
|
-- Expiry pruning may remove a conflicting row before we acquire its lock.
|
|
579
579
|
END LOOP;
|
|
@@ -602,7 +602,7 @@ function createPostgresHttpPolicyStores(database) {
|
|
|
602
602
|
if (!Number.isSafeInteger(limit) || limit <= 0 || limit > 1e9 || !Number.isSafeInteger(windowMs) || windowMs <= 0 || windowMs > 86400000) {
|
|
603
603
|
throw new TypeError("Invalid rate limit window");
|
|
604
604
|
}
|
|
605
|
-
const [row] = await database.query("SELECT
|
|
605
|
+
const [row] = await database.query("SELECT allowed, remaining, reset_at_ms FROM supacloud_http.consume_rate_limit($1,$2,$3)", [key, limit, windowMs]);
|
|
606
606
|
if (!row || typeof row.allowed !== "boolean")
|
|
607
607
|
throw new Error("Invalid rate limit storage result");
|
|
608
608
|
return { allowed: row.allowed, remaining: Number(row.remaining), resetAt: Number(row.reset_at_ms) };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/elysia",
|
|
3
|
-
"version": "0.38.
|
|
3
|
+
"version": "0.38.1",
|
|
4
4
|
"description": "Elysia runtime adapter for SupaCloud compiled modules: application/request scopes, route registration and validation",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -67,9 +67,9 @@
|
|
|
67
67
|
"elysia": "2.0.0-rc.1"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@supacloud/compiler": "0.
|
|
70
|
+
"@supacloud/compiler": "0.55.0",
|
|
71
71
|
"@supacloud/delivery": "0.24.0",
|
|
72
|
-
"@supacloud/db": "0.20.
|
|
72
|
+
"@supacloud/db": "0.20.5",
|
|
73
73
|
"@types/bun": "^1.4.4",
|
|
74
74
|
"elysia": "2.0.0-rc.1",
|
|
75
75
|
"typescript": "^7.0.2"
|