@supacloud/elysia 0.37.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/README.md +3 -3
- package/compatibility.json +1 -1
- package/dist/effect.d.ts +16 -0
- package/dist/http-policy-postgres.d.ts +1 -1
- package/dist/index.js +37 -6
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -8,11 +8,11 @@ is a separately authorized business command, never an assumed rollback.
|
|
|
8
8
|
|
|
9
9
|
## Compatibility and Acceptance Boundary
|
|
10
10
|
|
|
11
|
-
The Elysia peer and development dependency are pinned to `2.0.0-rc.
|
|
11
|
+
The Elysia peer and development dependency are pinned to `2.0.0-rc.1`.
|
|
12
12
|
The runtime adapter pins Effect `4.0.5` and consumes its `Exit`-based
|
|
13
13
|
execution boundary.
|
|
14
14
|
`compatibility.json` records the acceptance target: Bun 1.4.4, Elysia
|
|
15
|
-
2.0.0-rc.
|
|
15
|
+
2.0.0-rc.1, Effect 4.0.5, `typebox` 1.4.3, `exact-mirror` 1.2.6,
|
|
16
16
|
TypeScript CLI 7.0.2 and the compiler's separate TypeScript 6 semantic API 6.0.2.
|
|
17
17
|
The matrix is a
|
|
18
18
|
required target, not proof of an execution. See the dated, commit-specific
|
|
@@ -327,7 +327,7 @@ Runtime adapter that turns `@supacloud/compiler` output into a production-ready
|
|
|
327
327
|
## Installation
|
|
328
328
|
|
|
329
329
|
```bash
|
|
330
|
-
bun add --exact @supacloud/elysia elysia@2.0.0-rc.
|
|
330
|
+
bun add --exact @supacloud/elysia elysia@2.0.0-rc.1
|
|
331
331
|
```
|
|
332
332
|
|
|
333
333
|
## Usage
|
package/compatibility.json
CHANGED
package/dist/effect.d.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The only production Effect interpreter.
|
|
3
|
+
*
|
|
4
|
+
* Application code returns an Effect program. This adapter owns timeout,
|
|
5
|
+
* the transient-failure allow-list, and the Exit-to-HTTP mapping. It does not
|
|
6
|
+
* replace command receipts, idempotency or durable job retry.
|
|
7
|
+
*/
|
|
1
8
|
import { Effect, Exit } from "effect";
|
|
2
9
|
import type * as Layer from "effect/Layer";
|
|
3
10
|
export interface CompiledEffectError {
|
|
@@ -5,7 +12,16 @@ export interface CompiledEffectError {
|
|
|
5
12
|
readonly status: number;
|
|
6
13
|
readonly code: string;
|
|
7
14
|
readonly message?: string;
|
|
15
|
+
readonly codeFromError?: boolean;
|
|
16
|
+
readonly messageFromError?: boolean;
|
|
17
|
+
readonly statusFromError?: boolean;
|
|
18
|
+
/** Framework may retry this declared 5xx failure. Outcome-unknown failures must not set this. */
|
|
19
|
+
readonly retryable?: boolean;
|
|
20
|
+
/** The write may already have happened. Never retried, even when retry is explicit. */
|
|
21
|
+
readonly outcomeUnknown?: boolean;
|
|
8
22
|
}
|
|
23
|
+
/** An omitted retry policy means no retry; it never enables explicit retry. */
|
|
24
|
+
export declare const DEFAULT_EFFECT_RETRY: "none";
|
|
9
25
|
export interface CompiledEffectDescriptor {
|
|
10
26
|
readonly required: true;
|
|
11
27
|
readonly errors?: readonly CompiledEffectError[];
|
|
@@ -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) };
|
|
@@ -1016,7 +1016,8 @@ function createDocumentationPlugin(options = {}) {
|
|
|
1016
1016
|
}
|
|
1017
1017
|
|
|
1018
1018
|
// src/effect.ts
|
|
1019
|
-
import { Cause, Duration, Effect, Exit, ManagedRuntime
|
|
1019
|
+
import { Cause, Duration, Effect, Exit, ManagedRuntime } from "effect";
|
|
1020
|
+
var DEFAULT_EFFECT_RETRY = "none";
|
|
1020
1021
|
|
|
1021
1022
|
class EffectApplicationError extends Error {
|
|
1022
1023
|
status;
|
|
@@ -1060,11 +1061,36 @@ function effectErrorTag(error) {
|
|
|
1060
1061
|
return error.constructor.name;
|
|
1061
1062
|
return;
|
|
1062
1063
|
}
|
|
1064
|
+
function boundedString(value, key, maxLength) {
|
|
1065
|
+
if (!isRecord(value))
|
|
1066
|
+
return;
|
|
1067
|
+
const candidate = value[key];
|
|
1068
|
+
if (typeof candidate !== "string")
|
|
1069
|
+
return;
|
|
1070
|
+
const trimmed = candidate.trim();
|
|
1071
|
+
return trimmed.length === 0 ? undefined : trimmed.slice(0, maxLength);
|
|
1072
|
+
}
|
|
1073
|
+
function boundedStatus(value) {
|
|
1074
|
+
if (!isRecord(value))
|
|
1075
|
+
return;
|
|
1076
|
+
const candidate = value["status"];
|
|
1077
|
+
return typeof candidate === "number" && Number.isInteger(candidate) && candidate >= 400 && candidate <= 599 ? candidate : undefined;
|
|
1078
|
+
}
|
|
1079
|
+
function isFrameworkRetryable(error, descriptor) {
|
|
1080
|
+
const tag = effectErrorTag(error);
|
|
1081
|
+
if (!tag)
|
|
1082
|
+
return false;
|
|
1083
|
+
const mapping = descriptor.errors?.find((entry) => entry.tag === tag);
|
|
1084
|
+
return mapping?.retryable === true && mapping.outcomeUnknown !== true && mapping.statusFromError !== true && mapping.status >= 500 && mapping.status <= 599;
|
|
1085
|
+
}
|
|
1063
1086
|
function mapEffectFailure(error, descriptor) {
|
|
1064
1087
|
const tag = effectErrorTag(error);
|
|
1065
1088
|
const mapping = tag && descriptor?.errors?.find((entry) => entry.tag === tag);
|
|
1066
1089
|
if (mapping) {
|
|
1067
|
-
|
|
1090
|
+
const instanceCode = mapping.codeFromError ? boundedString(error, "code", 120) : undefined;
|
|
1091
|
+
const instanceMessage = mapping.messageFromError ? boundedString(error, "message", 500) : undefined;
|
|
1092
|
+
const instanceStatus = mapping.statusFromError ? boundedStatus(error) : undefined;
|
|
1093
|
+
return new EffectApplicationError(instanceMessage ?? mapping.message ?? mapping.code, instanceStatus ?? mapping.status, instanceCode ?? mapping.code);
|
|
1068
1094
|
}
|
|
1069
1095
|
if (tag === "TimeoutException" || tag === "TimeoutError") {
|
|
1070
1096
|
return new EffectApplicationError("Effect execution timed out", 504, "EFFECT_TIMEOUT");
|
|
@@ -1082,8 +1108,13 @@ async function runCompiledEffect(value, descriptor, runtime = createDefaultEffec
|
|
|
1082
1108
|
if (descriptor?.timeoutMs !== undefined) {
|
|
1083
1109
|
program = Effect.timeout(program, Duration.millis(descriptor.timeoutMs));
|
|
1084
1110
|
}
|
|
1085
|
-
|
|
1086
|
-
|
|
1111
|
+
const retry = descriptor?.retry ?? DEFAULT_EFFECT_RETRY;
|
|
1112
|
+
const maxAttempts = descriptor?.maxAttempts;
|
|
1113
|
+
if (retry === "explicit" && maxAttempts !== undefined && maxAttempts > 1 && descriptor) {
|
|
1114
|
+
program = Effect.retry(program, {
|
|
1115
|
+
times: maxAttempts - 1,
|
|
1116
|
+
while: (error) => isFrameworkRetryable(error, descriptor)
|
|
1117
|
+
});
|
|
1087
1118
|
}
|
|
1088
1119
|
const exit = await runtime.run(program);
|
|
1089
1120
|
if (Exit.isSuccess(exit))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@supacloud/elysia",
|
|
3
|
-
"version": "0.
|
|
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",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"directory": "packages/elysia"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@supacloud/app": "0.
|
|
52
|
+
"@supacloud/app": "0.36.0",
|
|
53
53
|
"@supacloud/commands": "0.11.2",
|
|
54
54
|
"@supacloud/contracts": "0.15.2",
|
|
55
55
|
"effect": "4.0.5",
|
|
@@ -58,20 +58,20 @@
|
|
|
58
58
|
"typebox": "1.4.3"
|
|
59
59
|
},
|
|
60
60
|
"overrides": {
|
|
61
|
-
"@supacloud/delivery": "0.
|
|
61
|
+
"@supacloud/delivery": "0.24.0",
|
|
62
62
|
"@supacloud/contracts": "0.15.2",
|
|
63
63
|
"@supacloud/commands": "0.11.2",
|
|
64
64
|
"@graphql-tools/utils": "^12.0.4"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"elysia": "2.0.0-rc.
|
|
67
|
+
"elysia": "2.0.0-rc.1"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@supacloud/compiler": "0.
|
|
71
|
-
"@supacloud/delivery": "0.
|
|
72
|
-
"@supacloud/db": "0.20.
|
|
70
|
+
"@supacloud/compiler": "0.55.0",
|
|
71
|
+
"@supacloud/delivery": "0.24.0",
|
|
72
|
+
"@supacloud/db": "0.20.5",
|
|
73
73
|
"@types/bun": "^1.4.4",
|
|
74
|
-
"elysia": "2.0.0-rc.
|
|
74
|
+
"elysia": "2.0.0-rc.1",
|
|
75
75
|
"typescript": "^7.0.2"
|
|
76
76
|
}
|
|
77
77
|
}
|