svelte-effect-runtime 2.2.0 → 2.2.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/remote/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Cause, Effect } from "effect";
|
|
2
1
|
import type { FormIssue } from "./shared.js";
|
|
2
|
+
import { Cause, Effect } from "effect";
|
|
3
3
|
/**
|
|
4
4
|
* Runs a user-supplied Effect program through a ManagedRuntime, maps its
|
|
5
5
|
* exit into the shape expected by SvelteKit, and returns the result or
|
package/.dist/remote/server.js
CHANGED
|
@@ -46,14 +46,43 @@ function encode_remote_failure(cause) {
|
|
|
46
46
|
const reasons = cause.reasons;
|
|
47
47
|
for (const reason of reasons) if (Cause.isFailReason(reason)) {
|
|
48
48
|
const failure = reason.error;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
49
|
+
const encoded = stringify_failure(failure);
|
|
50
|
+
if (encoded !== void 0) return encoded;
|
|
51
|
+
const serializable_encoded = stringify_failure(to_serializable_failure(failure));
|
|
52
|
+
if (serializable_encoded !== void 0) return serializable_encoded;
|
|
54
53
|
}
|
|
55
54
|
return stringify({ message: "[UNKNOWN_REMOTE_FAILURE]: Unknown error" });
|
|
56
55
|
}
|
|
56
|
+
function stringify_failure(value) {
|
|
57
|
+
try {
|
|
58
|
+
return stringify(value);
|
|
59
|
+
} catch {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function to_serializable_failure(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
64
|
+
if (typeof value === "function" || typeof value === "symbol") return;
|
|
65
|
+
if (!is_object_like(value)) return value;
|
|
66
|
+
if (stringify_failure(value) !== void 0) return value;
|
|
67
|
+
if (seen.has(value)) return;
|
|
68
|
+
seen.add(value);
|
|
69
|
+
const serializable = Array.isArray(value) ? value.map((item) => to_serializable_failure(item, seen)) : to_plain_record(value, seen);
|
|
70
|
+
seen.delete(value);
|
|
71
|
+
return serializable;
|
|
72
|
+
}
|
|
73
|
+
function to_plain_record(value, seen) {
|
|
74
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
75
|
+
const record = {};
|
|
76
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
77
|
+
if (key === "stack" || !("value" in descriptor)) continue;
|
|
78
|
+
record[key] = to_serializable_failure(descriptor.value, seen);
|
|
79
|
+
}
|
|
80
|
+
if (value instanceof Error && !("message" in record)) record.message = value.message;
|
|
81
|
+
return record;
|
|
82
|
+
}
|
|
83
|
+
function is_object_like(value) {
|
|
84
|
+
return typeof value === "object" && value !== null;
|
|
85
|
+
}
|
|
57
86
|
/**
|
|
58
87
|
* Throws a SvelteKit `invalid` response from a {@link FormError}, calling
|
|
59
88
|
* through to the request-scoped `invalid` helper.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","names":[],"sources":["../../../modules/svelte-effect-runtime/src/remote/server.ts"],"sourcesContent":["import { create_serialized_remote_failure_envelope } from \"$/remote/shared.ts\";\nimport {
|
|
1
|
+
{"version":3,"file":"server.js","names":[],"sources":["../../../modules/svelte-effect-runtime/src/remote/server.ts"],"sourcesContent":["import { create_serialized_remote_failure_envelope } from \"$/remote/shared.ts\";\nimport type { FormIssue } from \"$/remote/shared.ts\";\nimport { Cause, Effect, Exit } from \"effect\";\nimport { stringify } from \"devalue\";\n\n/**\n * Runs a user-supplied Effect program through a ManagedRuntime, maps its\n * exit into the shape expected by SvelteKit, and returns the result or\n * throws a SvelteKit-compatible error.\n *\n * @since 2.0.0\n * @param effect - The Effect program to execute.\n * @param runtime - The server-side ManagedRuntime.\n * @param invalid - SvelteKit's `invalid` helper (bound per-request).\n * @param error - SvelteKit's `error` helper.\n * @returns A Promise that resolves with the effect's success value.\n * @internal\n */\nexport async function run_remote_effect<A>(\n effect: Effect.Effect<A, unknown, unknown>,\n runtime: {\n runPromise: (\n e: Effect.Effect<unknown, unknown, unknown>,\n ) => Promise<unknown>;\n },\n invalid: (status: number, body: unknown) => never,\n error: (status: number, body: unknown) => never,\n): Promise<A> {\n const exit: Exit.Exit<A, unknown> = await runtime.runPromise(\n Effect.exit(effect) as Effect.Effect<unknown, unknown, unknown>,\n ) as Exit.Exit<A, unknown>;\n\n if (Exit.isSuccess(exit)) {\n return exit.value;\n }\n\n handle_failure(exit.cause, invalid, error);\n}\n\n/**\n * Inspects the Effect Cause for failures and either throws a form\n * validation error (via SvelteKit's `invalid`) or encodes the error\n * and throws it via SvelteKit's `error`.\n */\nfunction handle_failure(\n cause: Cause.Cause<unknown>,\n invalid: (status: number, body: unknown) => never,\n error: (status: number, body: unknown) => never,\n): never {\n const reasons =\n (cause as unknown as { reasons: Array<{ _tag: string; error?: unknown }> })\n .reasons;\n\n for (const reason of reasons) {\n if (Cause.isFailReason(reason as never)) {\n const failure = reason.error;\n if (\n typeof failure === \"object\" &&\n failure !== null &&\n (failure as Record<string, unknown>)._tag === \"FormError\"\n ) {\n const issues = (failure as { issues?: readonly FormIssue[] }).issues ??\n [];\n invalid(400, { issues });\n }\n }\n }\n\n const encoded = encode_remote_failure(cause);\n const envelope = create_serialized_remote_failure_envelope(encoded);\n\n error(500, envelope);\n}\n\n/**\n * Encodes an Effect Cause into a string that the client-side adapter can\n * decode back into a typed `RemoteFailure`.\n *\n * @since 2.0.0\n * @param cause - The Effect Cause from a failed execution.\n * @returns A devalue-encoded string representing the serialised failure.\n * @internal\n */\nexport function encode_remote_failure(cause: Cause.Cause<unknown>): string {\n const reasons =\n (cause as unknown as { reasons: Array<{ _tag: string; error?: unknown }> })\n .reasons;\n\n for (const reason of reasons) {\n if (Cause.isFailReason(reason as never)) {\n const failure = reason.error;\n\n const encoded = stringify_failure(failure);\n\n if (encoded !== undefined) {\n return encoded;\n }\n\n const serializable_failure = to_serializable_failure(failure);\n const serializable_encoded = stringify_failure(serializable_failure);\n\n if (serializable_encoded !== undefined) {\n return serializable_encoded;\n }\n }\n }\n\n return stringify({ message: \"[UNKNOWN_REMOTE_FAILURE]: Unknown error\" });\n}\n\nfunction stringify_failure(value: unknown): string | undefined {\n try {\n return stringify(value);\n } catch {\n return undefined;\n }\n}\n\nfunction to_serializable_failure(\n value: unknown,\n seen = new WeakSet<object>(),\n): unknown {\n if (typeof value === \"function\" || typeof value === \"symbol\") {\n return undefined;\n }\n\n if (!is_object_like(value)) {\n return value;\n }\n\n if (stringify_failure(value) !== undefined) {\n return value;\n }\n\n if (seen.has(value)) {\n return undefined;\n }\n\n seen.add(value);\n\n const serializable = Array.isArray(value)\n ? value.map((item) => to_serializable_failure(item, seen))\n : to_plain_record(value, seen);\n\n seen.delete(value);\n\n return serializable;\n}\n\nfunction to_plain_record(\n value: object,\n seen: WeakSet<object>,\n): Record<string, unknown> {\n const descriptors = Object.getOwnPropertyDescriptors(value);\n const record: Record<string, unknown> = {};\n\n for (const [key, descriptor] of Object.entries(descriptors)) {\n if (key === \"stack\" || !(\"value\" in descriptor)) {\n continue;\n }\n\n record[key] = to_serializable_failure(descriptor.value, seen);\n }\n\n if (value instanceof Error && !(\"message\" in record)) {\n record.message = value.message;\n }\n\n return record;\n}\n\nfunction is_object_like(value: unknown): value is object {\n return typeof value === \"object\" && value !== null;\n}\n\n/**\n * Throws a SvelteKit `invalid` response from a {@link FormError}, calling\n * through to the request-scoped `invalid` helper.\n *\n * @example\n * ```ts\n * throw_form_error(\n * [{ message: \"Name is required\", path: [\"name\"] }],\n * invalid,\n * );\n * ```\n *\n * @since 2.0.0\n * @param issues - The list of form validation issues.\n * @param invalid - SvelteKit's `invalid` helper bound to the current request.\n * @internal\n */\nexport function throw_form_error(\n issues: readonly FormIssue[],\n invalid: (status: number, body: unknown) => never,\n): never {\n invalid(400, { issues });\n}\n\n/**\n * Remaps low-level SvelteKit errors (e.g. \"Cannot use ___ outside a route\")\n * into clear, actionable messages.\n *\n * @example\n * ```ts\n * try {\n * return native_query(handler);\n * } catch (err) {\n * throw normalize_remote_helper_error(err, \"Query\");\n * }\n * ```\n *\n * @since 2.0.0\n * @param err - The error thrown by SvelteKit's native functions.\n * @param helper_name - Name of the helper that triggered the error.\n * @returns A more descriptive error.\n * @internal\n */\nexport function normalize_remote_helper_error(\n err: unknown,\n helper_name: string,\n): Error {\n const message = err instanceof Error ? err.message : String(err);\n\n if (message.includes(\"Cannot use\") || message.includes(\"outside a route\")) {\n return new Error(\n `[REMOTE_HELPER_CONTEXT]: ${helper_name} was called outside a .remote.ts file. ` +\n `Ensure the file is named \\`*.remote.ts\\` and is located in a route directory.`,\n );\n }\n\n return err instanceof Error\n ? err\n : new Error(`[REMOTE_HELPER_ERROR]: ${message}`);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,eAAsB,kBACpB,QACA,SAKA,SACA,OACY;CACZ,MAAM,OAA8B,MAAM,QAAQ,WAChD,OAAO,KAAK,OAAO,CACpB;AAED,KAAI,KAAK,UAAU,KAAK,CACtB,QAAO,KAAK;AAGd,gBAAe,KAAK,OAAO,SAAS,MAAM;;;;;;;AAQ5C,SAAS,eACP,OACA,SACA,OACO;CACP,MAAM,UACH,MACE;AAEL,MAAK,MAAM,UAAU,QACnB,KAAI,MAAM,aAAa,OAAgB,EAAE;EACvC,MAAM,UAAU,OAAO;AACvB,MACE,OAAO,YAAY,YACnB,YAAY,QACX,QAAoC,SAAS,YAI9C,SAAQ,KAAK,EAAE,QAFC,QAA8C,UAC5D,EAAE,EACmB,CAAC;;AAQ9B,OAAM,KAFW,0CADD,sBAAsB,MAAM,CACuB,CAE/C;;;;;;;;;;;AAYtB,SAAgB,sBAAsB,OAAqC;CACzE,MAAM,UACH,MACE;AAEL,MAAK,MAAM,UAAU,QACnB,KAAI,MAAM,aAAa,OAAgB,EAAE;EACvC,MAAM,UAAU,OAAO;EAEvB,MAAM,UAAU,kBAAkB,QAAQ;AAE1C,MAAI,YAAY,KAAA,EACd,QAAO;EAIT,MAAM,uBAAuB,kBADA,wBAAwB,QAAQ,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