svelte-effect-runtime 1.0.0
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 +40 -0
- package/dist/chunks/magic-string.es-CHYFped_.js +1013 -0
- package/dist/chunks/magic-string.es-CHYFped_.js.map +1 -0
- package/dist/client.d.ts +64 -0
- package/dist/client.js +169 -0
- package/dist/client.js.map +1 -0
- package/dist/effect.d.ts +7 -0
- package/dist/effect.js +28 -0
- package/dist/effect.js.map +1 -0
- package/dist/internal/app-server-shim.d.ts +8 -0
- package/dist/internal/markup.d.ts +11 -0
- package/dist/internal/markup.js +697 -0
- package/dist/internal/markup.js.map +1 -0
- package/dist/internal/remote-client.d.ts +36 -0
- package/dist/internal/remote-client.js +454 -0
- package/dist/internal/remote-client.js.map +1 -0
- package/dist/internal/remote-shared.d.ts +53 -0
- package/dist/internal/remote-shared.js +58 -0
- package/dist/internal/remote-shared.js.map +1 -0
- package/dist/internal/transform.d.ts +11 -0
- package/dist/internal/transform.js +262 -0
- package/dist/internal/transform.js.map +1 -0
- package/dist/language-server.d.ts +3 -0
- package/dist/language-server.js +4 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +5 -0
- package/dist/preprocess.d.ts +7 -0
- package/dist/preprocess.js +36 -0
- package/dist/preprocess.js.map +1 -0
- package/dist/root-node.d.ts +14 -0
- package/dist/root-node.js +8 -0
- package/dist/server.d.ts +129 -0
- package/dist/server.js +494 -0
- package/dist/server.js.map +1 -0
- package/dist/vite.d.ts +16 -0
- package/dist/vite.js +107 -0
- package/dist/vite.js.map +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type * as Server_module from "./server.ts";
|
|
2
|
+
export * from "./mod.ts";
|
|
3
|
+
export * from "./vite.ts";
|
|
4
|
+
export type * from "./server.ts";
|
|
5
|
+
export type { EffectPluginOptions } from "./effect.ts";
|
|
6
|
+
export { effect } from "./effect.ts";
|
|
7
|
+
export declare const Query: typeof Server_module.Query;
|
|
8
|
+
export declare const Command: typeof Server_module.Command;
|
|
9
|
+
export declare const Form: typeof Server_module.Form;
|
|
10
|
+
export declare const Prerender: typeof Server_module.Prerender;
|
|
11
|
+
export declare const RequestEvent: typeof Server_module.RequestEvent;
|
|
12
|
+
export declare const ServerRuntime: typeof Server_module.ServerRuntime;
|
|
13
|
+
export declare const create_effect_transport: typeof Server_module.create_effect_transport;
|
|
14
|
+
export declare const get_server_runtime_or_throw: typeof Server_module.get_server_runtime_or_throw;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { transformEffectMarkup } from "./internal/markup.js";
|
|
2
|
+
import { transformEffectScript } from "./internal/transform.js";
|
|
3
|
+
import { effectPreprocess } from "./preprocess.js";
|
|
4
|
+
import { ClientRuntime, ClientRuntimeTag, getEffectRuntimeOrThrow, provideEffectRuntime, registerHotDispose, runComponentEffect, runInlineEffect, to_effect, to_native } from "./client.js";
|
|
5
|
+
import "./mod.js";
|
|
6
|
+
import { svelteEffectRuntime, sveltekitEffectRuntime } from "./vite.js";
|
|
7
|
+
import { effect } from "./effect.js";
|
|
8
|
+
export { ClientRuntime, ClientRuntimeTag, effect, effectPreprocess, getEffectRuntimeOrThrow, provideEffectRuntime, registerHotDispose, runComponentEffect, runInlineEffect, svelteEffectRuntime, sveltekitEffectRuntime, to_effect, to_native, transformEffectMarkup, transformEffectScript };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { getRequestEvent as get_native_request_event } from "$app/server";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
import * as ManagedRuntime from "effect/ManagedRuntime";
|
|
6
|
+
import * as Schema from "effect/Schema";
|
|
7
|
+
import { type FormError } from "./internal/remote-shared.ts";
|
|
8
|
+
type EffectSchema = Schema.Schema.Any;
|
|
9
|
+
export interface Transporter<T = unknown, U = {
|
|
10
|
+
value: unknown;
|
|
11
|
+
}> {
|
|
12
|
+
decode: (data: U) => T;
|
|
13
|
+
encode: (value: T) => false | U;
|
|
14
|
+
}
|
|
15
|
+
export type Transport = Record<string, Transporter>;
|
|
16
|
+
export interface RemoteFormInput {
|
|
17
|
+
[key: string]: string | number | boolean | File | RemoteFormInput | Array<string | number | boolean | File | RemoteFormInput>;
|
|
18
|
+
}
|
|
19
|
+
export type RemoteQueryFunction<Input, Output> = (arg: OptionalArgument<Input>) => Promise<Output>;
|
|
20
|
+
export type RemoteCommand<Input, Output> = ((arg: OptionalArgument<Input>) => Promise<Output> & {
|
|
21
|
+
updates(...updates: Array<unknown>): Promise<Output>;
|
|
22
|
+
}) & {
|
|
23
|
+
readonly pending: number;
|
|
24
|
+
};
|
|
25
|
+
export type RemotePrerenderFunction<Input, Output> = (arg: OptionalArgument<Input>) => Promise<Output>;
|
|
26
|
+
export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
|
|
27
|
+
readonly action: string;
|
|
28
|
+
enhance: (...args: Array<unknown>) => {
|
|
29
|
+
readonly action: string;
|
|
30
|
+
readonly method: "POST";
|
|
31
|
+
};
|
|
32
|
+
fields: Record<string, unknown>;
|
|
33
|
+
for: (id: string | number) => RemoteForm<Input, Output>;
|
|
34
|
+
readonly method: "POST";
|
|
35
|
+
readonly pending: number;
|
|
36
|
+
preflight: (schema: unknown) => RemoteForm<Input, Output>;
|
|
37
|
+
readonly result: Output | undefined;
|
|
38
|
+
validate: (options?: {
|
|
39
|
+
includeUntouched?: boolean;
|
|
40
|
+
preflightOnly?: boolean;
|
|
41
|
+
}) => Promise<void>;
|
|
42
|
+
[attachment: symbol]: (node: HTMLFormElement) => void;
|
|
43
|
+
};
|
|
44
|
+
type RuntimeOperator = (self: Layer.Layer<unknown, unknown, unknown>) => Layer.Layer<unknown, unknown, unknown>;
|
|
45
|
+
type RuntimeSeedLayer<Requirements> = Layer.Layer<Requirements, never, Requirements>;
|
|
46
|
+
type ProvidedBy<Op> = Op extends (self: RuntimeSeedLayer<infer Requirements>) => Layer.Layer<unknown, unknown, infer Incoming> ? Exclude<Requirements, Incoming> : never;
|
|
47
|
+
type ProvidedByAll<Ops extends ReadonlyArray<RuntimeOperator>> = Ops extends readonly [infer Head, ...infer Tail] ? ProvidedBy<Head> | ProvidedByAll<Extract<Tail, ReadonlyArray<RuntimeOperator>>> : never;
|
|
48
|
+
type ApplyOperator<Seed, Op> = Op extends (self: Seed) => infer Out ? Out : never;
|
|
49
|
+
type ApplyOperators<Seed, Ops extends ReadonlyArray<RuntimeOperator>> = Ops extends readonly [infer Head, ...infer Tail] ? ApplyOperators<ApplyOperator<Seed, Head>, Extract<Tail, ReadonlyArray<RuntimeOperator>>> : Seed;
|
|
50
|
+
type FinalRuntimeLayer<Ops extends ReadonlyArray<RuntimeOperator>> = ApplyOperators<RuntimeSeedLayer<ProvidedByAll<Ops>>, Ops> extends infer Out ? Out extends Layer.Layer<unknown, unknown, infer Incoming> ? [Incoming] extends [never] ? Out : never : never : never;
|
|
51
|
+
type OptionalArgument<Input> = undefined extends Input ? Input | void : Input;
|
|
52
|
+
type SchemaInput<SchemaType extends EffectSchema> = Schema.Schema.Encoded<SchemaType>;
|
|
53
|
+
type SchemaOutput<SchemaType extends EffectSchema> = Schema.Schema.Type<SchemaType>;
|
|
54
|
+
type FieldHelpers<FormShape, SchemaType> = FormShape extends Record<string, unknown> ? {
|
|
55
|
+
[Key in keyof FormShape as Key extends string ? Key : never]: (message: string) => Effect.Effect<never, FormError<SchemaType>, never>;
|
|
56
|
+
} : Record<PropertyKey, never>;
|
|
57
|
+
export type Invalid<SchemaType = unknown> = {
|
|
58
|
+
form: (message: string) => Effect.Effect<never, FormError<SchemaType>, never>;
|
|
59
|
+
} & FieldHelpers<SchemaType extends EffectSchema ? SchemaOutput<SchemaType> : unknown, SchemaType>;
|
|
60
|
+
export type EffectQueryFunction<Input, Output, Error = never> = ((arg: OptionalArgument<Input>) => Effect.Effect<Output, import("./internal/remote-shared.ts").RemoteFailure<Error>, never>) & {
|
|
61
|
+
native: RemoteQueryFunction<Input, Output>;
|
|
62
|
+
};
|
|
63
|
+
export type EffectCommand<Input, Output, Error = never> = ((arg: OptionalArgument<Input>) => Effect.Effect<Output, import("./internal/remote-shared.ts").RemoteFailure<Error>, never>) & {
|
|
64
|
+
native: RemoteCommand<Input, Output>;
|
|
65
|
+
readonly pending: number;
|
|
66
|
+
};
|
|
67
|
+
export type EffectPrerenderFunction<Input, Output, Error = never> = ((arg: OptionalArgument<Input>) => Effect.Effect<Output, import("./internal/remote-shared.ts").RemoteFailure<Error>, never>) & {
|
|
68
|
+
native: RemotePrerenderFunction<Input, Output>;
|
|
69
|
+
};
|
|
70
|
+
export type EffectForm<Input extends RemoteFormInput | void, Output, Error = never> = RemoteForm<Input, Output> & {
|
|
71
|
+
native: RemoteForm<Input, Output>;
|
|
72
|
+
submit(data: OptionalArgument<Input>): Effect.Effect<Output, import("./internal/remote-shared.ts").RemoteFailure<Error>, never>;
|
|
73
|
+
for: RemoteForm<Input, Output>["for"] extends (...args: infer Args) => infer Result ? (...args: Args) => EffectForm<Input, Output, Error> : never;
|
|
74
|
+
};
|
|
75
|
+
export type RequestEventService = ReturnType<typeof get_native_request_event>;
|
|
76
|
+
export declare const RequestEvent: Context.Tag<import("@sveltejs/kit").RequestEvent<Record<string, string>, string | null>, import("@sveltejs/kit").RequestEvent<Record<string, string>, string | null>>;
|
|
77
|
+
interface ServerRuntimeSeed {
|
|
78
|
+
pipe(): Layer.Layer<never>;
|
|
79
|
+
pipe<const Ops extends readonly [RuntimeOperator, ...Array<RuntimeOperator>]>(...ops: Ops): FinalRuntimeLayer<Ops>;
|
|
80
|
+
make(): void;
|
|
81
|
+
make<const Ops extends readonly [RuntimeOperator, ...Array<RuntimeOperator>]>(...ops: Ops): void;
|
|
82
|
+
}
|
|
83
|
+
type ServerManagedRuntime = ManagedRuntime.ManagedRuntime<unknown, unknown>;
|
|
84
|
+
export declare const ServerRuntime: ServerRuntimeSeed;
|
|
85
|
+
export declare function get_server_runtime_or_throw(): ServerManagedRuntime;
|
|
86
|
+
export declare function create_effect_transport<const Schemas extends Record<string, EffectSchema>>(schemas: Schemas): Transport;
|
|
87
|
+
export interface EffectQueryFactory {
|
|
88
|
+
<Output, ErrorType, Requirements>(fn: () => Effect.Effect<Output, ErrorType, Requirements>): EffectQueryFunction<void, Output, ErrorType>;
|
|
89
|
+
<Input, Output, ErrorType, Requirements>(validate: "unchecked", fn: (arg: Input) => Effect.Effect<Output, ErrorType, Requirements>): EffectQueryFunction<Input, Output, ErrorType>;
|
|
90
|
+
<SchemaType extends EffectSchema, Output, ErrorType, Requirements>(validate: SchemaType, fn: (arg: SchemaOutput<SchemaType>) => Effect.Effect<Output, ErrorType, Requirements>): EffectQueryFunction<SchemaInput<SchemaType>, Output, ErrorType>;
|
|
91
|
+
batch: typeof query_batch_factory;
|
|
92
|
+
}
|
|
93
|
+
type RemotePrerenderInputsGenerator<Input> = (event: RequestEventService) => AsyncIterable<Input> | Iterable<Input>;
|
|
94
|
+
declare function query_batch_factory(validate_or_fn: unknown, maybe_fn?: unknown): unknown;
|
|
95
|
+
export declare const Query: EffectQueryFactory;
|
|
96
|
+
export interface EffectCommandFactory {
|
|
97
|
+
<Output, ErrorType, Requirements>(fn: () => Effect.Effect<Output, ErrorType, Requirements>): EffectCommand<void, Output, ErrorType>;
|
|
98
|
+
<Input, Output, ErrorType, Requirements>(validate: "unchecked", fn: (arg: Input) => Effect.Effect<Output, ErrorType, Requirements>): EffectCommand<Input, Output, ErrorType>;
|
|
99
|
+
<SchemaType extends EffectSchema, Output, ErrorType, Requirements>(validate: SchemaType, fn: (arg: SchemaOutput<SchemaType>) => Effect.Effect<Output, ErrorType, Requirements>): EffectCommand<SchemaInput<SchemaType>, Output, ErrorType>;
|
|
100
|
+
}
|
|
101
|
+
export declare const Command: EffectCommandFactory;
|
|
102
|
+
export interface EffectFormFactory {
|
|
103
|
+
<Output, ErrorType, Requirements>(fn: () => Effect.Effect<Output, ErrorType, Requirements>): EffectForm<void, Output, ErrorType>;
|
|
104
|
+
<Input extends RemoteFormInput, Output, ErrorType, Requirements>(validate: "unchecked", fn: (args: {
|
|
105
|
+
data: Input;
|
|
106
|
+
invalid: Invalid;
|
|
107
|
+
}) => Effect.Effect<Output, ErrorType | FormError, Requirements>): EffectForm<Input, Output, ErrorType>;
|
|
108
|
+
<SchemaType extends EffectSchema, Output, ErrorType, Requirements>(validate: SchemaType, fn: (args: {
|
|
109
|
+
data: SchemaOutput<SchemaType>;
|
|
110
|
+
invalid: Invalid<SchemaType>;
|
|
111
|
+
}) => Effect.Effect<Output, ErrorType | FormError<SchemaType>, Requirements>): EffectForm<SchemaInput<SchemaType> & RemoteFormInput, Output, ErrorType>;
|
|
112
|
+
}
|
|
113
|
+
export declare const Form: EffectFormFactory;
|
|
114
|
+
export interface EffectPrerenderFactory {
|
|
115
|
+
<Output, ErrorType, Requirements>(fn: () => Effect.Effect<Output, ErrorType, Requirements>, options?: {
|
|
116
|
+
inputs?: RemotePrerenderInputsGenerator<void>;
|
|
117
|
+
dynamic?: boolean;
|
|
118
|
+
}): EffectPrerenderFunction<void, Output, ErrorType>;
|
|
119
|
+
<Input, Output, ErrorType, Requirements>(validate: "unchecked", fn: (arg: Input) => Effect.Effect<Output, ErrorType, Requirements>, options?: {
|
|
120
|
+
inputs?: RemotePrerenderInputsGenerator<Input>;
|
|
121
|
+
dynamic?: boolean;
|
|
122
|
+
}): EffectPrerenderFunction<Input, Output, ErrorType>;
|
|
123
|
+
<SchemaType extends EffectSchema, Output, ErrorType, Requirements>(validate: SchemaType, fn: (arg: SchemaOutput<SchemaType>) => Effect.Effect<Output, ErrorType, Requirements>, options?: {
|
|
124
|
+
inputs?: RemotePrerenderInputsGenerator<SchemaInput<SchemaType>>;
|
|
125
|
+
dynamic?: boolean;
|
|
126
|
+
}): EffectPrerenderFunction<SchemaInput<SchemaType>, Output, ErrorType>;
|
|
127
|
+
}
|
|
128
|
+
export declare const Prerender: EffectPrerenderFactory;
|
|
129
|
+
export {};
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
import { create_form_error, create_serialized_remote_failure_envelope, is_form_error } from "./internal/remote-shared.js";
|
|
2
|
+
import * as Context from "effect/Context";
|
|
3
|
+
import * as Effect from "effect/Effect";
|
|
4
|
+
import * as Layer from "effect/Layer";
|
|
5
|
+
import * as ManagedRuntime from "effect/ManagedRuntime";
|
|
6
|
+
import * as Cause from "effect/Cause";
|
|
7
|
+
import * as Exit from "effect/Exit";
|
|
8
|
+
import { error, invalid } from "@sveltejs/kit";
|
|
9
|
+
import { command, form, getRequestEvent, prerender, query } from "$app/server";
|
|
10
|
+
import { get_request_store } from "@sveltejs/kit/internal/server";
|
|
11
|
+
import * as Option from "effect/Option";
|
|
12
|
+
import * as Schema from "effect/Schema";
|
|
13
|
+
import * as Schema_ast from "effect/SchemaAST";
|
|
14
|
+
//#region node_modules/devalue/src/utils.js
|
|
15
|
+
var DevalueError = class extends Error {
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} message
|
|
18
|
+
* @param {string[]} keys
|
|
19
|
+
* @param {any} [value] - The value that failed to be serialized
|
|
20
|
+
* @param {any} [root] - The root value being serialized
|
|
21
|
+
*/
|
|
22
|
+
constructor(message, keys, value, root) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "DevalueError";
|
|
25
|
+
this.path = keys.join("");
|
|
26
|
+
this.value = value;
|
|
27
|
+
this.root = root;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/** @param {any} thing */
|
|
31
|
+
function is_primitive(thing) {
|
|
32
|
+
return thing === null || typeof thing !== "object" && typeof thing !== "function";
|
|
33
|
+
}
|
|
34
|
+
const object_proto_names = /* @__PURE__ */ Object.getOwnPropertyNames(Object.prototype).sort().join("\0");
|
|
35
|
+
/** @param {any} thing */
|
|
36
|
+
function is_plain_object(thing) {
|
|
37
|
+
const proto = Object.getPrototypeOf(thing);
|
|
38
|
+
return proto === Object.prototype || proto === null || Object.getPrototypeOf(proto) === null || Object.getOwnPropertyNames(proto).sort().join("\0") === object_proto_names;
|
|
39
|
+
}
|
|
40
|
+
/** @param {any} thing */
|
|
41
|
+
function get_type(thing) {
|
|
42
|
+
return Object.prototype.toString.call(thing).slice(8, -1);
|
|
43
|
+
}
|
|
44
|
+
/** @param {string} char */
|
|
45
|
+
function get_escaped_char(char) {
|
|
46
|
+
switch (char) {
|
|
47
|
+
case "\"": return "\\\"";
|
|
48
|
+
case "<": return "\\u003C";
|
|
49
|
+
case "\\": return "\\\\";
|
|
50
|
+
case "\n": return "\\n";
|
|
51
|
+
case "\r": return "\\r";
|
|
52
|
+
case " ": return "\\t";
|
|
53
|
+
case "\b": return "\\b";
|
|
54
|
+
case "\f": return "\\f";
|
|
55
|
+
case "\u2028": return "\\u2028";
|
|
56
|
+
case "\u2029": return "\\u2029";
|
|
57
|
+
default: return char < " " ? `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}` : "";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/** @param {string} str */
|
|
61
|
+
function stringify_string(str) {
|
|
62
|
+
let result = "";
|
|
63
|
+
let last_pos = 0;
|
|
64
|
+
const len = str.length;
|
|
65
|
+
for (let i = 0; i < len; i += 1) {
|
|
66
|
+
const char = str[i];
|
|
67
|
+
const replacement = get_escaped_char(char);
|
|
68
|
+
if (replacement) {
|
|
69
|
+
result += str.slice(last_pos, i) + replacement;
|
|
70
|
+
last_pos = i + 1;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return `"${last_pos === 0 ? str : result + str.slice(last_pos)}"`;
|
|
74
|
+
}
|
|
75
|
+
/** @param {Record<string | symbol, any>} object */
|
|
76
|
+
function enumerable_symbols(object) {
|
|
77
|
+
return Object.getOwnPropertySymbols(object).filter((symbol) => Object.getOwnPropertyDescriptor(object, symbol).enumerable);
|
|
78
|
+
}
|
|
79
|
+
const is_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
|
|
80
|
+
/** @param {string} key */
|
|
81
|
+
function stringify_key(key) {
|
|
82
|
+
return is_identifier.test(key) ? "." + key : "[" + JSON.stringify(key) + "]";
|
|
83
|
+
}
|
|
84
|
+
/** @param {string} s */
|
|
85
|
+
function is_valid_array_index(s) {
|
|
86
|
+
if (s.length === 0) return false;
|
|
87
|
+
if (s.length > 1 && s.charCodeAt(0) === 48) return false;
|
|
88
|
+
for (let i = 0; i < s.length; i++) {
|
|
89
|
+
const c = s.charCodeAt(i);
|
|
90
|
+
if (c < 48 || c > 57) return false;
|
|
91
|
+
}
|
|
92
|
+
const n = +s;
|
|
93
|
+
if (n >= 2 ** 32 - 1) return false;
|
|
94
|
+
if (n < 0) return false;
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Finds the populated indices of an array.
|
|
99
|
+
* @param {unknown[]} array
|
|
100
|
+
*/
|
|
101
|
+
function valid_array_indices(array) {
|
|
102
|
+
const keys = Object.keys(array);
|
|
103
|
+
for (var i = keys.length - 1; i >= 0; i--) if (is_valid_array_index(keys[i])) break;
|
|
104
|
+
keys.length = i + 1;
|
|
105
|
+
return keys;
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region node_modules/devalue/src/base64.js
|
|
109
|
+
/** @type {(array_buffer: ArrayBuffer) => string} */
|
|
110
|
+
function encode_native(array_buffer) {
|
|
111
|
+
return new Uint8Array(array_buffer).toBase64();
|
|
112
|
+
}
|
|
113
|
+
/** @type {(array_buffer: ArrayBuffer) => string} */
|
|
114
|
+
function encode_buffer(array_buffer) {
|
|
115
|
+
return Buffer.from(array_buffer).toString("base64");
|
|
116
|
+
}
|
|
117
|
+
/** @type {(array_buffer: ArrayBuffer) => string} */
|
|
118
|
+
function encode_legacy(array_buffer) {
|
|
119
|
+
const array = new Uint8Array(array_buffer);
|
|
120
|
+
let binary = "";
|
|
121
|
+
const chunk_size = 32768;
|
|
122
|
+
for (let i = 0; i < array.length; i += chunk_size) {
|
|
123
|
+
const chunk = array.subarray(i, i + chunk_size);
|
|
124
|
+
binary += String.fromCharCode.apply(null, chunk);
|
|
125
|
+
}
|
|
126
|
+
return btoa(binary);
|
|
127
|
+
}
|
|
128
|
+
const native = typeof Uint8Array.fromBase64 === "function";
|
|
129
|
+
const buffer = typeof process === "object" && process.versions?.node !== void 0;
|
|
130
|
+
const encode64 = native ? encode_native : buffer ? encode_buffer : encode_legacy;
|
|
131
|
+
//#endregion
|
|
132
|
+
//#region node_modules/devalue/src/stringify.js
|
|
133
|
+
/**
|
|
134
|
+
* Turn a value into a JSON string that can be parsed with `devalue.parse`
|
|
135
|
+
* @param {any} value
|
|
136
|
+
* @param {Record<string, (value: any) => any>} [reducers]
|
|
137
|
+
*/
|
|
138
|
+
function stringify(value, reducers) {
|
|
139
|
+
/** @type {any[]} */
|
|
140
|
+
const stringified = [];
|
|
141
|
+
/** @type {Map<any, number>} */
|
|
142
|
+
const indexes = /* @__PURE__ */ new Map();
|
|
143
|
+
/** @type {Array<{ key: string, fn: (value: any) => any }>} */
|
|
144
|
+
const custom = [];
|
|
145
|
+
if (reducers) for (const key of Object.getOwnPropertyNames(reducers)) custom.push({
|
|
146
|
+
key,
|
|
147
|
+
fn: reducers[key]
|
|
148
|
+
});
|
|
149
|
+
/** @type {string[]} */
|
|
150
|
+
const keys = [];
|
|
151
|
+
let p = 0;
|
|
152
|
+
/** @param {any} thing */
|
|
153
|
+
function flatten(thing) {
|
|
154
|
+
if (thing === void 0) return -1;
|
|
155
|
+
if (Number.isNaN(thing)) return -3;
|
|
156
|
+
if (thing === Infinity) return -4;
|
|
157
|
+
if (thing === -Infinity) return -5;
|
|
158
|
+
if (thing === 0 && 1 / thing < 0) return -6;
|
|
159
|
+
if (indexes.has(thing)) return indexes.get(thing);
|
|
160
|
+
const index = p++;
|
|
161
|
+
indexes.set(thing, index);
|
|
162
|
+
for (const { key, fn } of custom) {
|
|
163
|
+
const value = fn(thing);
|
|
164
|
+
if (value) {
|
|
165
|
+
stringified[index] = `["${key}",${flatten(value)}]`;
|
|
166
|
+
return index;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (typeof thing === "function") throw new DevalueError(`Cannot stringify a function`, keys, thing, value);
|
|
170
|
+
else if (typeof thing === "symbol") throw new DevalueError(`Cannot stringify a Symbol primitive`, keys, thing, value);
|
|
171
|
+
let str = "";
|
|
172
|
+
if (is_primitive(thing)) str = stringify_primitive(thing);
|
|
173
|
+
else {
|
|
174
|
+
const type = get_type(thing);
|
|
175
|
+
switch (type) {
|
|
176
|
+
case "Number":
|
|
177
|
+
case "String":
|
|
178
|
+
case "Boolean":
|
|
179
|
+
case "BigInt":
|
|
180
|
+
str = `["Object",${flatten(thing.valueOf())}]`;
|
|
181
|
+
break;
|
|
182
|
+
case "Date":
|
|
183
|
+
str = `["Date","${!isNaN(thing.getDate()) ? thing.toISOString() : ""}"]`;
|
|
184
|
+
break;
|
|
185
|
+
case "URL":
|
|
186
|
+
str = `["URL",${stringify_string(thing.toString())}]`;
|
|
187
|
+
break;
|
|
188
|
+
case "URLSearchParams":
|
|
189
|
+
str = `["URLSearchParams",${stringify_string(thing.toString())}]`;
|
|
190
|
+
break;
|
|
191
|
+
case "RegExp":
|
|
192
|
+
const { source, flags } = thing;
|
|
193
|
+
str = flags ? `["RegExp",${stringify_string(source)},"${flags}"]` : `["RegExp",${stringify_string(source)}]`;
|
|
194
|
+
break;
|
|
195
|
+
case "Array": {
|
|
196
|
+
let mostly_dense = false;
|
|
197
|
+
str = "[";
|
|
198
|
+
for (let i = 0; i < thing.length; i += 1) {
|
|
199
|
+
if (i > 0) str += ",";
|
|
200
|
+
if (Object.hasOwn(thing, i)) {
|
|
201
|
+
keys.push(`[${i}]`);
|
|
202
|
+
str += flatten(thing[i]);
|
|
203
|
+
keys.pop();
|
|
204
|
+
} else if (mostly_dense) str += -2;
|
|
205
|
+
else {
|
|
206
|
+
const populated_keys = valid_array_indices(thing);
|
|
207
|
+
const population = populated_keys.length;
|
|
208
|
+
const d = String(thing.length).length;
|
|
209
|
+
if ((thing.length - population) * 3 > 4 + d + population * (d + 1)) {
|
|
210
|
+
str = "[-7," + thing.length;
|
|
211
|
+
for (let j = 0; j < populated_keys.length; j++) {
|
|
212
|
+
const key = populated_keys[j];
|
|
213
|
+
keys.push(`[${key}]`);
|
|
214
|
+
str += "," + key + "," + flatten(thing[key]);
|
|
215
|
+
keys.pop();
|
|
216
|
+
}
|
|
217
|
+
break;
|
|
218
|
+
} else {
|
|
219
|
+
mostly_dense = true;
|
|
220
|
+
str += -2;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
str += "]";
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case "Set":
|
|
228
|
+
str = "[\"Set\"";
|
|
229
|
+
for (const value of thing) str += `,${flatten(value)}`;
|
|
230
|
+
str += "]";
|
|
231
|
+
break;
|
|
232
|
+
case "Map":
|
|
233
|
+
str = "[\"Map\"";
|
|
234
|
+
for (const [key, value] of thing) {
|
|
235
|
+
keys.push(`.get(${is_primitive(key) ? stringify_primitive(key) : "..."})`);
|
|
236
|
+
str += `,${flatten(key)},${flatten(value)}`;
|
|
237
|
+
keys.pop();
|
|
238
|
+
}
|
|
239
|
+
str += "]";
|
|
240
|
+
break;
|
|
241
|
+
case "Int8Array":
|
|
242
|
+
case "Uint8Array":
|
|
243
|
+
case "Uint8ClampedArray":
|
|
244
|
+
case "Int16Array":
|
|
245
|
+
case "Uint16Array":
|
|
246
|
+
case "Float16Array":
|
|
247
|
+
case "Int32Array":
|
|
248
|
+
case "Uint32Array":
|
|
249
|
+
case "Float32Array":
|
|
250
|
+
case "Float64Array":
|
|
251
|
+
case "BigInt64Array":
|
|
252
|
+
case "BigUint64Array":
|
|
253
|
+
case "DataView": {
|
|
254
|
+
/** @type {import("./types.js").TypedArray} */
|
|
255
|
+
const typedArray = thing;
|
|
256
|
+
str = "[\"" + type + "\"," + flatten(typedArray.buffer);
|
|
257
|
+
if (typedArray.byteLength !== typedArray.buffer.byteLength) str += `,${typedArray.byteOffset},${typedArray.length}`;
|
|
258
|
+
str += "]";
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case "ArrayBuffer":
|
|
262
|
+
str = `["ArrayBuffer","${encode64(thing)}"]`;
|
|
263
|
+
break;
|
|
264
|
+
case "Temporal.Duration":
|
|
265
|
+
case "Temporal.Instant":
|
|
266
|
+
case "Temporal.PlainDate":
|
|
267
|
+
case "Temporal.PlainTime":
|
|
268
|
+
case "Temporal.PlainDateTime":
|
|
269
|
+
case "Temporal.PlainMonthDay":
|
|
270
|
+
case "Temporal.PlainYearMonth":
|
|
271
|
+
case "Temporal.ZonedDateTime":
|
|
272
|
+
str = `["${type}",${stringify_string(thing.toString())}]`;
|
|
273
|
+
break;
|
|
274
|
+
default:
|
|
275
|
+
if (!is_plain_object(thing)) throw new DevalueError(`Cannot stringify arbitrary non-POJOs`, keys, thing, value);
|
|
276
|
+
if (enumerable_symbols(thing).length > 0) throw new DevalueError(`Cannot stringify POJOs with symbolic keys`, keys, thing, value);
|
|
277
|
+
if (Object.getPrototypeOf(thing) === null) {
|
|
278
|
+
str = "[\"null\"";
|
|
279
|
+
for (const key of Object.keys(thing)) {
|
|
280
|
+
if (key === "__proto__") throw new DevalueError(`Cannot stringify objects with __proto__ keys`, keys, thing, value);
|
|
281
|
+
keys.push(stringify_key(key));
|
|
282
|
+
str += `,${stringify_string(key)},${flatten(thing[key])}`;
|
|
283
|
+
keys.pop();
|
|
284
|
+
}
|
|
285
|
+
str += "]";
|
|
286
|
+
} else {
|
|
287
|
+
str = "{";
|
|
288
|
+
let started = false;
|
|
289
|
+
for (const key of Object.keys(thing)) {
|
|
290
|
+
if (key === "__proto__") throw new DevalueError(`Cannot stringify objects with __proto__ keys`, keys, thing, value);
|
|
291
|
+
if (started) str += ",";
|
|
292
|
+
started = true;
|
|
293
|
+
keys.push(stringify_key(key));
|
|
294
|
+
str += `${stringify_string(key)}:${flatten(thing[key])}`;
|
|
295
|
+
keys.pop();
|
|
296
|
+
}
|
|
297
|
+
str += "}";
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
stringified[index] = str;
|
|
302
|
+
return index;
|
|
303
|
+
}
|
|
304
|
+
const index = flatten(value);
|
|
305
|
+
if (index < 0) return `${index}`;
|
|
306
|
+
return `[${stringified.join(",")}]`;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* @param {any} thing
|
|
310
|
+
* @returns {string}
|
|
311
|
+
*/
|
|
312
|
+
function stringify_primitive(thing) {
|
|
313
|
+
const type = typeof thing;
|
|
314
|
+
if (type === "string") return stringify_string(thing);
|
|
315
|
+
if (thing === void 0) return (-1).toString();
|
|
316
|
+
if (thing === 0 && 1 / thing < 0) return (-6).toString();
|
|
317
|
+
if (type === "bigint") return `["BigInt","${thing}"]`;
|
|
318
|
+
return String(thing);
|
|
319
|
+
}
|
|
320
|
+
//#endregion
|
|
321
|
+
//#region server.ts
|
|
322
|
+
const RequestEvent = Context.GenericTag("svelte-effect-runtime/RequestEvent");
|
|
323
|
+
let current_server_runtime = null;
|
|
324
|
+
function runtime_seed() {
|
|
325
|
+
return Layer.effectContext(Effect.context());
|
|
326
|
+
}
|
|
327
|
+
function pipe_server_runtime(...ops) {
|
|
328
|
+
if (ops.length === 0) return Layer.empty;
|
|
329
|
+
return ops.reduce((layer, op) => op(layer), runtime_seed());
|
|
330
|
+
}
|
|
331
|
+
function make_server_runtime(...ops) {
|
|
332
|
+
const layer = ops.length === 0 ? pipe_server_runtime() : pipe_server_runtime(...ops);
|
|
333
|
+
const previous_runtime = current_server_runtime;
|
|
334
|
+
current_server_runtime = ManagedRuntime.make(layer);
|
|
335
|
+
previous_runtime?.dispose().catch(() => void 0);
|
|
336
|
+
}
|
|
337
|
+
const ServerRuntime = {
|
|
338
|
+
pipe: pipe_server_runtime,
|
|
339
|
+
make: make_server_runtime
|
|
340
|
+
};
|
|
341
|
+
function get_server_runtime_or_throw() {
|
|
342
|
+
if (current_server_runtime === null) throw new Error("No server Effect runtime found. Call ServerRuntime.make(...) from src/hooks.server.ts via `export const init = () => { ... }` before executing remote functions.");
|
|
343
|
+
return current_server_runtime;
|
|
344
|
+
}
|
|
345
|
+
function is_effect_schema(value) {
|
|
346
|
+
return Schema.isSchema(value);
|
|
347
|
+
}
|
|
348
|
+
function get_effect_schema_validator(schema) {
|
|
349
|
+
return Schema.standardSchemaV1(schema);
|
|
350
|
+
}
|
|
351
|
+
function get_effect_form_validator(schema) {
|
|
352
|
+
return Schema.standardSchemaV1(schema);
|
|
353
|
+
}
|
|
354
|
+
function resolve_remote_args(validate_or_fn, maybe_fn) {
|
|
355
|
+
if (maybe_fn === void 0) return {
|
|
356
|
+
fn: validate_or_fn,
|
|
357
|
+
validate: void 0
|
|
358
|
+
};
|
|
359
|
+
if (validate_or_fn === "unchecked") return {
|
|
360
|
+
fn: maybe_fn,
|
|
361
|
+
validate: "unchecked"
|
|
362
|
+
};
|
|
363
|
+
if (!is_effect_schema(validate_or_fn)) throw new Error("Invalid schema passed to a remote function. Use Effect.Schema, 'unchecked', or omit the validator entirely.");
|
|
364
|
+
return {
|
|
365
|
+
fn: maybe_fn,
|
|
366
|
+
validate: validate_or_fn
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
function get_remote_failure_status(failure) {
|
|
370
|
+
if (failure && typeof failure === "object" && typeof failure.status === "number") return failure.status;
|
|
371
|
+
return 500;
|
|
372
|
+
}
|
|
373
|
+
function log_remote_server_step(step, details) {
|
|
374
|
+
console.log("[svelte-effect-runtime][server]", step, details ?? {});
|
|
375
|
+
}
|
|
376
|
+
function encode_remote_failure(failure) {
|
|
377
|
+
log_remote_server_step("encode_remote_failure:start", { failure });
|
|
378
|
+
const { state } = get_request_store();
|
|
379
|
+
const encoded = stringify(failure, Object.fromEntries(Object.entries(state.transport).map(([name, transporter]) => [name, transporter.encode])));
|
|
380
|
+
throw error(get_remote_failure_status(failure), {
|
|
381
|
+
message: "Effect remote failure",
|
|
382
|
+
...create_serialized_remote_failure_envelope(encoded)
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
function throw_form_error(form_error) {
|
|
386
|
+
log_remote_server_step("throw_form_error", { issues: form_error.issues });
|
|
387
|
+
invalid(...form_error.issues.map((issue) => ({
|
|
388
|
+
message: issue.message,
|
|
389
|
+
path: [...issue.path]
|
|
390
|
+
})));
|
|
391
|
+
}
|
|
392
|
+
async function run_remote_effect(program) {
|
|
393
|
+
const request_event = getRequestEvent();
|
|
394
|
+
const runtime = get_server_runtime_or_throw();
|
|
395
|
+
log_remote_server_step("run_remote_effect:start", {
|
|
396
|
+
method: request_event.request.method,
|
|
397
|
+
url: request_event.url.toString()
|
|
398
|
+
});
|
|
399
|
+
const provided_program = Effect.provideService(program, RequestEvent, request_event);
|
|
400
|
+
const exit = await runtime.runPromiseExit(provided_program);
|
|
401
|
+
if (Exit.isSuccess(exit)) {
|
|
402
|
+
log_remote_server_step("run_remote_effect:success", { value: exit.value });
|
|
403
|
+
return exit.value;
|
|
404
|
+
}
|
|
405
|
+
const failure = Cause.failureOption(exit.cause);
|
|
406
|
+
if (Option.isSome(failure)) {
|
|
407
|
+
log_remote_server_step("run_remote_effect:failure", { failure: failure.value });
|
|
408
|
+
if (is_form_error(failure.value)) throw_form_error(failure.value);
|
|
409
|
+
encode_remote_failure(failure.value);
|
|
410
|
+
}
|
|
411
|
+
log_remote_server_step("run_remote_effect:defect", { cause: exit.cause });
|
|
412
|
+
throw Cause.squash(exit.cause);
|
|
413
|
+
}
|
|
414
|
+
function get_schema_field_names(schema) {
|
|
415
|
+
return Schema_ast.getPropertySignatures(schema.ast).map((property_signature) => property_signature.name).filter((name) => typeof name === "string");
|
|
416
|
+
}
|
|
417
|
+
function create_invalid_helper(options = {}) {
|
|
418
|
+
const field_names = options.schema ? new Set(get_schema_field_names(options.schema)) : null;
|
|
419
|
+
const create_issue_effect = (message, path) => Effect.fail(create_form_error({
|
|
420
|
+
message,
|
|
421
|
+
path
|
|
422
|
+
}));
|
|
423
|
+
return new Proxy({ form(message) {
|
|
424
|
+
return create_issue_effect(message, []);
|
|
425
|
+
} }, { get(target, property) {
|
|
426
|
+
if (typeof property !== "string") return Reflect.get(target, property);
|
|
427
|
+
if (property in target) return target[property];
|
|
428
|
+
if (field_names !== null && !field_names.has(property)) throw new Error(`Unknown form field '${property}' in invalid helper. v1 only supports top-level fields declared in the Effect.Schema.`);
|
|
429
|
+
return (message) => create_issue_effect(message, [property]);
|
|
430
|
+
} });
|
|
431
|
+
}
|
|
432
|
+
function define_native_property(value, native) {
|
|
433
|
+
Object.defineProperty(value, "native", {
|
|
434
|
+
value: native,
|
|
435
|
+
enumerable: false
|
|
436
|
+
});
|
|
437
|
+
return value;
|
|
438
|
+
}
|
|
439
|
+
function create_effect_transport(schemas) {
|
|
440
|
+
return Object.fromEntries(Object.entries(schemas).map(([name, schema]) => {
|
|
441
|
+
return [name, {
|
|
442
|
+
encode(value) {
|
|
443
|
+
try {
|
|
444
|
+
return Schema.is(schema)(value) ? { value: Schema.encodeSync(schema)(value) } : false;
|
|
445
|
+
} catch {
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
decode(data) {
|
|
450
|
+
return Schema.decodeSync(schema)(data.value);
|
|
451
|
+
}
|
|
452
|
+
}];
|
|
453
|
+
}));
|
|
454
|
+
}
|
|
455
|
+
function create_native_wrapper(value) {
|
|
456
|
+
return define_native_property(value, value);
|
|
457
|
+
}
|
|
458
|
+
function query_batch_factory(validate_or_fn, maybe_fn) {
|
|
459
|
+
const resolved = resolve_remote_args(validate_or_fn, maybe_fn);
|
|
460
|
+
if (resolved.validate === void 0) throw new Error("Query.batch requires either 'unchecked' or an Effect.Schema validator to match SvelteKit's native API.");
|
|
461
|
+
return create_native_wrapper(resolved.validate === "unchecked" ? query.batch("unchecked", (args) => run_remote_effect(resolved.fn(args))) : query.batch(get_effect_schema_validator(resolved.validate), (args) => run_remote_effect(resolved.fn(args))));
|
|
462
|
+
}
|
|
463
|
+
const query_impl = (validate_or_fn, maybe_fn) => {
|
|
464
|
+
const resolved = resolve_remote_args(validate_or_fn, maybe_fn);
|
|
465
|
+
return create_native_wrapper(resolved.validate === void 0 ? query(() => run_remote_effect(resolved.fn())) : resolved.validate === "unchecked" ? query("unchecked", (arg) => run_remote_effect(resolved.fn(arg))) : query(get_effect_schema_validator(resolved.validate), (arg) => run_remote_effect(resolved.fn(arg))));
|
|
466
|
+
};
|
|
467
|
+
const Query = Object.assign(query_impl, { batch: query_batch_factory });
|
|
468
|
+
const command_impl = (validate_or_fn, maybe_fn) => {
|
|
469
|
+
const resolved = resolve_remote_args(validate_or_fn, maybe_fn);
|
|
470
|
+
return create_native_wrapper(resolved.validate === void 0 ? command(() => run_remote_effect(resolved.fn())) : resolved.validate === "unchecked" ? command("unchecked", (arg) => run_remote_effect(resolved.fn(arg))) : command(get_effect_schema_validator(resolved.validate), (arg) => run_remote_effect(resolved.fn(arg))));
|
|
471
|
+
};
|
|
472
|
+
const Command = command_impl;
|
|
473
|
+
const form_impl = (validate_or_fn, maybe_fn) => {
|
|
474
|
+
const resolved = resolve_remote_args(validate_or_fn, maybe_fn);
|
|
475
|
+
return create_native_wrapper(resolved.validate === void 0 ? form(() => run_remote_effect(resolved.fn())) : resolved.validate === "unchecked" ? form("unchecked", (data) => run_remote_effect(resolved.fn({
|
|
476
|
+
data,
|
|
477
|
+
invalid: create_invalid_helper()
|
|
478
|
+
}))) : form(get_effect_form_validator(resolved.validate), (data) => run_remote_effect(resolved.fn({
|
|
479
|
+
data,
|
|
480
|
+
invalid: create_invalid_helper({ schema: resolved.validate })
|
|
481
|
+
}))));
|
|
482
|
+
};
|
|
483
|
+
const Form = form_impl;
|
|
484
|
+
const native_prerender_fn = prerender;
|
|
485
|
+
const prerender_impl = (validate_or_fn, fn_or_options, maybe_options) => {
|
|
486
|
+
if (typeof validate_or_fn === "function" && typeof fn_or_options !== "function") return create_native_wrapper(native_prerender_fn(() => run_remote_effect(validate_or_fn()), fn_or_options));
|
|
487
|
+
const resolved = resolve_remote_args(validate_or_fn, fn_or_options);
|
|
488
|
+
return create_native_wrapper(resolved.validate === "unchecked" ? native_prerender_fn("unchecked", (arg) => run_remote_effect(resolved.fn(arg)), maybe_options) : native_prerender_fn(get_effect_schema_validator(resolved.validate), (arg) => run_remote_effect(resolved.fn(arg)), maybe_options));
|
|
489
|
+
};
|
|
490
|
+
const Prerender = prerender_impl;
|
|
491
|
+
//#endregion
|
|
492
|
+
export { Command, Form, Prerender, Query, RequestEvent, ServerRuntime, create_effect_transport, get_server_runtime_or_throw };
|
|
493
|
+
|
|
494
|
+
//# sourceMappingURL=server.js.map
|