| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820 |
- import { B as BROWSER } from "./chunks/false.js";
- import { json, text, error } from "@sveltejs/kit";
- import { Redirect, SvelteKitError, ActionFailure, HttpError } from "@sveltejs/kit/internal";
- import { with_request_store, merge_tracing, try_get_request_store } from "@sveltejs/kit/internal/server";
- import { a as assets, b as base, c as app_dir, r as relative, o as override, d as reset } from "./chunks/environment.js";
- import { E as ENDPOINT_METHODS, P as PAGE_METHODS, n as negotiate, m as method_not_allowed, h as handle_error_and_jsonify, g as get_status, i as is_form_content_type, a as normalize_error, b as get_global_name, s as serialize_uses, c as clarify_devalue_error, d as get_node_type, e as escape_html, S as SVELTE_KIT_ASSETS, f as create_remote_key, j as static_error_page, r as redirect_response, p as parse_remote_arg, k as stringify, l as deserialize_binary_form, o as has_prerendered_path, T as TRAILING_SLASH_PARAM, I as INVALIDATED_PARAM, q as handle_fatal_error, M as MUTATIVE_METHODS, t as format_server_error } from "./chunks/shared.js";
- import * as devalue from "devalue";
- import { m as make_trackable, d as disable_search, a as decode_params, S as SCHEME, w as writable, r as readable, v as validate_layout_server_exports, b as validate_layout_exports, c as validate_page_server_exports, e as validate_page_exports, n as normalize_path, f as resolve, g as decode_pathname, h as validate_server_exports } from "./chunks/exports.js";
- import { b as base64_encode, t as text_decoder, a as text_encoder, g as get_relative_path } from "./chunks/utils.js";
- import { p as public_env, r as read_implementation, o as options, s as set_private_env, a as set_public_env, g as get_hooks, b as set_read_implementation } from "./chunks/internal.js";
- import { parse, serialize } from "cookie";
- import * as set_cookie_parser from "set-cookie-parser";
- function with_resolvers() {
- let resolve2;
- let reject;
- const promise = new Promise((res, rej) => {
- resolve2 = res;
- reject = rej;
- });
- return { promise, resolve: resolve2, reject };
- }
- const NULL_BODY_STATUS = [101, 103, 204, 205, 304];
- const IN_WEBCONTAINER = !!globalThis.process?.versions?.webcontainer;
- async function render_endpoint(event, event_state, mod, state) {
- const method = (
- /** @type {import('types').HttpMethod} */
- event.request.method
- );
- let handler = mod[method] || mod.fallback;
- if (method === "HEAD" && !mod.HEAD && mod.GET) {
- handler = mod.GET;
- }
- if (!handler) {
- return method_not_allowed(mod, method);
- }
- const prerender = mod.prerender ?? state.prerender_default;
- if (prerender && (mod.POST || mod.PATCH || mod.PUT || mod.DELETE)) {
- throw new Error("Cannot prerender endpoints that have mutative methods");
- }
- if (state.prerendering && !state.prerendering.inside_reroute && !prerender) {
- if (state.depth > 0) {
- throw new Error(`${event.route.id} is not prerenderable`);
- } else {
- return new Response(void 0, { status: 204 });
- }
- }
- try {
- event_state.allows_commands = true;
- const response = await with_request_store(
- { event, state: event_state },
- () => handler(
- /** @type {import('@sveltejs/kit').RequestEvent<Record<string, any>>} */
- event
- )
- );
- if (!(response instanceof Response)) {
- throw new Error(
- `Invalid response from route ${event.url.pathname}: handler should return a Response object`
- );
- }
- if (state.prerendering && (!state.prerendering.inside_reroute || prerender)) {
- const cloned = new Response(response.clone().body, {
- status: response.status,
- statusText: response.statusText,
- headers: new Headers(response.headers)
- });
- cloned.headers.set("x-sveltekit-prerender", String(prerender));
- if (state.prerendering.inside_reroute && prerender) {
- cloned.headers.set(
- "x-sveltekit-routeid",
- encodeURI(
- /** @type {string} */
- event.route.id
- )
- );
- state.prerendering.dependencies.set(event.url.pathname, { response: cloned, body: null });
- } else {
- return cloned;
- }
- }
- return response;
- } catch (e) {
- if (e instanceof Redirect) {
- return new Response(void 0, {
- status: e.status,
- headers: { location: e.location }
- });
- }
- throw e;
- }
- }
- function is_endpoint_request(event) {
- const { method, headers: headers2 } = event.request;
- if (ENDPOINT_METHODS.includes(method) && !PAGE_METHODS.includes(method)) {
- return true;
- }
- if (method === "POST" && headers2.get("x-sveltekit-action") === "true") return false;
- const accept = event.request.headers.get("accept") ?? "*/*";
- return negotiate(accept, ["*", "text/html"]) !== "text/html";
- }
- function compact(arr) {
- return arr.filter(
- /** @returns {val is NonNullable<T>} */
- (val) => val != null
- );
- }
- const DATA_SUFFIX = "/__data.json";
- const HTML_DATA_SUFFIX = ".html__data.json";
- function has_data_suffix(pathname) {
- return pathname.endsWith(DATA_SUFFIX) || pathname.endsWith(HTML_DATA_SUFFIX);
- }
- function add_data_suffix(pathname) {
- if (pathname.endsWith(".html")) return pathname.replace(/\.html$/, HTML_DATA_SUFFIX);
- return pathname.replace(/\/$/, "") + DATA_SUFFIX;
- }
- function strip_data_suffix(pathname) {
- if (pathname.endsWith(HTML_DATA_SUFFIX)) {
- return pathname.slice(0, -HTML_DATA_SUFFIX.length) + ".html";
- }
- return pathname.slice(0, -DATA_SUFFIX.length);
- }
- const ROUTE_SUFFIX = "/__route.js";
- function has_resolution_suffix(pathname) {
- return pathname.endsWith(ROUTE_SUFFIX);
- }
- function add_resolution_suffix(pathname) {
- return pathname.replace(/\/$/, "") + ROUTE_SUFFIX;
- }
- function strip_resolution_suffix(pathname) {
- return pathname.slice(0, -ROUTE_SUFFIX.length);
- }
- const noop_span = {
- spanContext() {
- return noop_span_context;
- },
- setAttribute() {
- return this;
- },
- setAttributes() {
- return this;
- },
- addEvent() {
- return this;
- },
- setStatus() {
- return this;
- },
- updateName() {
- return this;
- },
- end() {
- return this;
- },
- isRecording() {
- return false;
- },
- recordException() {
- return this;
- },
- addLink() {
- return this;
- },
- addLinks() {
- return this;
- }
- };
- const noop_span_context = {
- traceId: "",
- spanId: "",
- traceFlags: 0
- };
- async function record_span({ name, attributes, fn }) {
- {
- return fn(noop_span);
- }
- }
- function is_action_json_request(event) {
- const accept = negotiate(event.request.headers.get("accept") ?? "*/*", [
- "application/json",
- "text/html"
- ]);
- return accept === "application/json" && event.request.method === "POST";
- }
- async function handle_action_json_request(event, event_state, options2, server) {
- const actions = server?.actions;
- if (!actions) {
- const no_actions_error = new SvelteKitError(
- 405,
- "Method Not Allowed",
- `POST method not allowed. No form actions exist for ${"this page"}`
- );
- return action_json(
- {
- type: "error",
- error: await handle_error_and_jsonify(event, event_state, options2, no_actions_error)
- },
- {
- status: no_actions_error.status,
- headers: {
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
- // "The server must generate an Allow header field in a 405 status code response"
- allow: "GET"
- }
- }
- );
- }
- check_named_default_separate(actions);
- try {
- const data = await call_action(event, event_state, actions);
- if (BROWSER) ;
- if (data instanceof ActionFailure) {
- return action_json({
- type: "failure",
- status: data.status,
- // @ts-expect-error we assign a string to what is supposed to be an object. That's ok
- // because we don't use the object outside, and this way we have better code navigation
- // through knowing where the related interface is used.
- data: stringify_action_response(
- data.data,
- /** @type {string} */
- event.route.id,
- options2.hooks.transport
- )
- });
- } else {
- return action_json({
- type: "success",
- status: data ? 200 : 204,
- // @ts-expect-error see comment above
- data: stringify_action_response(
- data,
- /** @type {string} */
- event.route.id,
- options2.hooks.transport
- )
- });
- }
- } catch (e) {
- const err = normalize_error(e);
- if (err instanceof Redirect) {
- return action_json_redirect(err);
- }
- return action_json(
- {
- type: "error",
- error: await handle_error_and_jsonify(
- event,
- event_state,
- options2,
- check_incorrect_fail_use(err)
- )
- },
- {
- status: get_status(err)
- }
- );
- }
- }
- function check_incorrect_fail_use(error2) {
- return error2 instanceof ActionFailure ? new Error('Cannot "throw fail()". Use "return fail()"') : error2;
- }
- function action_json_redirect(redirect) {
- return action_json({
- type: "redirect",
- status: redirect.status,
- location: redirect.location
- });
- }
- function action_json(data, init2) {
- return json(data, init2);
- }
- function is_action_request(event) {
- return event.request.method === "POST";
- }
- async function handle_action_request(event, event_state, server) {
- const actions = server?.actions;
- if (!actions) {
- event.setHeaders({
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
- // "The server must generate an Allow header field in a 405 status code response"
- allow: "GET"
- });
- return {
- type: "error",
- error: new SvelteKitError(
- 405,
- "Method Not Allowed",
- `POST method not allowed. No form actions exist for ${"this page"}`
- )
- };
- }
- check_named_default_separate(actions);
- try {
- const data = await call_action(event, event_state, actions);
- if (BROWSER) ;
- if (data instanceof ActionFailure) {
- return {
- type: "failure",
- status: data.status,
- data: data.data
- };
- } else {
- return {
- type: "success",
- status: 200,
- // @ts-expect-error this will be removed upon serialization, so `undefined` is the same as omission
- data
- };
- }
- } catch (e) {
- const err = normalize_error(e);
- if (err instanceof Redirect) {
- return {
- type: "redirect",
- status: err.status,
- location: err.location
- };
- }
- return {
- type: "error",
- error: check_incorrect_fail_use(err)
- };
- }
- }
- function check_named_default_separate(actions) {
- if (actions.default && Object.keys(actions).length > 1) {
- throw new Error(
- "When using named actions, the default action cannot be used. See the docs for more info: https://svelte.dev/docs/kit/form-actions#named-actions"
- );
- }
- }
- async function call_action(event, event_state, actions) {
- const url = new URL(event.request.url);
- let name = "default";
- for (const param of url.searchParams) {
- if (param[0].startsWith("/")) {
- name = param[0].slice(1);
- if (name === "default") {
- throw new Error('Cannot use reserved action name "default"');
- }
- break;
- }
- }
- const action = actions[name];
- if (!action) {
- throw new SvelteKitError(404, "Not Found", `No action with name '${name}' found`);
- }
- if (!is_form_content_type(event.request)) {
- throw new SvelteKitError(
- 415,
- "Unsupported Media Type",
- `Form actions expect form-encoded data — received ${event.request.headers.get(
- "content-type"
- )}`
- );
- }
- return record_span({
- name: "sveltekit.form_action",
- attributes: {
- "http.route": event.route.id || "unknown"
- },
- fn: async (current2) => {
- const traced_event = merge_tracing(event, current2);
- event_state.allows_commands = true;
- const result = await with_request_store(
- { event: traced_event, state: event_state },
- () => action(traced_event)
- );
- if (result instanceof ActionFailure) {
- current2.setAttributes({
- "sveltekit.form_action.result.type": "failure",
- "sveltekit.form_action.result.status": result.status
- });
- }
- return result;
- }
- });
- }
- function validate_action_return(data) {
- if (data instanceof Redirect) {
- throw new Error("Cannot `return redirect(...)` — use `redirect(...)` instead");
- }
- if (data instanceof HttpError) {
- throw new Error("Cannot `return error(...)` — use `error(...)` or `return fail(...)` instead");
- }
- }
- function uneval_action_response(data, route_id, transport) {
- const replacer = (thing) => {
- for (const key2 in transport) {
- const encoded = transport[key2].encode(thing);
- if (encoded) {
- return `app.decode('${key2}', ${devalue.uneval(encoded, replacer)})`;
- }
- }
- };
- return try_serialize(data, (value) => devalue.uneval(value, replacer), route_id);
- }
- function stringify_action_response(data, route_id, transport) {
- const encoders = Object.fromEntries(
- Object.entries(transport).map(([key2, value]) => [key2, value.encode])
- );
- return try_serialize(data, (value) => devalue.stringify(value, encoders), route_id);
- }
- function try_serialize(data, fn, route_id) {
- try {
- return fn(data);
- } catch (e) {
- const error2 = (
- /** @type {any} */
- e
- );
- if (data instanceof Response) {
- throw new Error(
- `Data returned from action inside ${route_id} is not serializable. Form actions need to return plain objects or fail(). E.g. return { success: true } or return fail(400, { message: "invalid" });`,
- { cause: e }
- );
- }
- if ("path" in error2) {
- let message = `Data returned from action inside ${route_id} is not serializable: ${error2.message}`;
- if (error2.path !== "") message += ` (data.${error2.path})`;
- throw new Error(message, { cause: e });
- }
- throw error2;
- }
- }
- function create_async_iterator() {
- let resolved = -1;
- let returned = -1;
- const deferred = [];
- return {
- iterate: (transform = (x) => x) => {
- return {
- [Symbol.asyncIterator]() {
- return {
- next: async () => {
- const next = deferred[++returned];
- if (!next) return { value: null, done: true };
- const value = await next.promise;
- return { value: transform(value), done: false };
- }
- };
- }
- };
- },
- add: (promise) => {
- deferred.push(with_resolvers());
- void promise.then((value) => {
- deferred[++resolved].resolve(value);
- });
- }
- };
- }
- function server_data_serializer(event, event_state, options2) {
- let promise_id = 1;
- let max_nodes = -1;
- const iterator = create_async_iterator();
- const global = get_global_name(options2);
- function get_replacer(index) {
- return function replacer(thing) {
- if (typeof thing?.then === "function") {
- const id = promise_id++;
- const promise = thing.then(
- /** @param {any} data */
- (data) => ({ data })
- ).catch(
- /** @param {any} error */
- async (error2) => ({
- error: await handle_error_and_jsonify(event, event_state, options2, error2)
- })
- ).then(
- /**
- * @param {{data: any; error: any}} result
- */
- async ({ data, error: error2 }) => {
- let str;
- try {
- str = devalue.uneval(error2 ? [, error2] : [data], replacer);
- } catch {
- error2 = await handle_error_and_jsonify(
- event,
- event_state,
- options2,
- new Error(`Failed to serialize promise while rendering ${event.route.id}`)
- );
- str = devalue.uneval([, error2], replacer);
- }
- return {
- index,
- str: `${global}.resolve(${id}, ${str.includes("app.decode") ? `(app) => ${str}` : `() => ${str}`})`
- };
- }
- );
- iterator.add(promise);
- return `${global}.defer(${id})`;
- } else {
- for (const key2 in options2.hooks.transport) {
- const encoded = options2.hooks.transport[key2].encode(thing);
- if (encoded) {
- return `app.decode('${key2}', ${devalue.uneval(encoded, replacer)})`;
- }
- }
- }
- };
- }
- const strings = (
- /** @type {string[]} */
- []
- );
- return {
- set_max_nodes(i) {
- max_nodes = i;
- },
- add_node(i, node) {
- try {
- if (!node) {
- strings[i] = "null";
- return;
- }
- const payload = { type: "data", data: node.data, uses: serialize_uses(node) };
- if (node.slash) payload.slash = node.slash;
- strings[i] = devalue.uneval(payload, get_replacer(i));
- } catch (e) {
- e.path = e.path.slice(1);
- throw new Error(clarify_devalue_error(
- event,
- /** @type {any} */
- e
- ), { cause: e });
- }
- },
- get_data(csp) {
- const open = `<script${csp.script_needs_nonce ? ` nonce="${csp.nonce}"` : ""}>`;
- const close = `<\/script>
- `;
- return {
- data: `[${compact(max_nodes > -1 ? strings.slice(0, max_nodes) : strings).join(",")}]`,
- chunks: promise_id > 1 ? iterator.iterate(({ index, str }) => {
- if (max_nodes > -1 && index >= max_nodes) {
- return "";
- }
- return open + str + close;
- }) : null
- };
- }
- };
- }
- function server_data_serializer_json(event, event_state, options2) {
- let promise_id = 1;
- const iterator = create_async_iterator();
- const reducers = {
- ...Object.fromEntries(
- Object.entries(options2.hooks.transport).map(([key2, value]) => [key2, value.encode])
- ),
- /** @param {any} thing */
- Promise: (thing) => {
- if (typeof thing?.then !== "function") {
- return;
- }
- const id = promise_id++;
- let key2 = "data";
- const promise = thing.catch(
- /** @param {any} e */
- async (e) => {
- key2 = "error";
- return handle_error_and_jsonify(
- event,
- event_state,
- options2,
- /** @type {any} */
- e
- );
- }
- ).then(
- /** @param {any} value */
- async (value) => {
- let str;
- try {
- str = devalue.stringify(value, reducers);
- } catch {
- const error2 = await handle_error_and_jsonify(
- event,
- event_state,
- options2,
- new Error(`Failed to serialize promise while rendering ${event.route.id}`)
- );
- key2 = "error";
- str = devalue.stringify(error2, reducers);
- }
- return `{"type":"chunk","id":${id},"${key2}":${str}}
- `;
- }
- );
- iterator.add(promise);
- return id;
- }
- };
- const strings = (
- /** @type {string[]} */
- []
- );
- return {
- add_node(i, node) {
- try {
- if (!node) {
- strings[i] = "null";
- return;
- }
- if (node.type === "error" || node.type === "skip") {
- strings[i] = JSON.stringify(node);
- return;
- }
- strings[i] = `{"type":"data","data":${devalue.stringify(node.data, reducers)},"uses":${JSON.stringify(
- serialize_uses(node)
- )}${node.slash ? `,"slash":${JSON.stringify(node.slash)}` : ""}}`;
- } catch (e) {
- e.path = "data" + e.path;
- throw new Error(clarify_devalue_error(
- event,
- /** @type {any} */
- e
- ), { cause: e });
- }
- },
- get_data() {
- return {
- data: `{"type":"data","nodes":[${strings.join(",")}]}
- `,
- chunks: promise_id > 1 ? iterator.iterate() : null
- };
- }
- };
- }
- async function load_server_data({ event, event_state, state, node, parent }) {
- if (!node?.server) return null;
- let is_tracking = true;
- const uses = {
- dependencies: /* @__PURE__ */ new Set(),
- params: /* @__PURE__ */ new Set(),
- parent: false,
- route: false,
- url: false,
- search_params: /* @__PURE__ */ new Set()
- };
- const load = node.server.load;
- const slash = node.server.trailingSlash;
- if (!load) {
- return { type: "data", data: null, uses, slash };
- }
- const url = make_trackable(
- event.url,
- () => {
- if (is_tracking) {
- uses.url = true;
- }
- },
- (param) => {
- if (is_tracking) {
- uses.search_params.add(param);
- }
- }
- );
- if (state.prerendering) {
- disable_search(url);
- }
- const result = await record_span({
- name: "sveltekit.load",
- attributes: {
- "sveltekit.load.node_id": node.server_id || "unknown",
- "sveltekit.load.node_type": get_node_type(node.server_id),
- "http.route": event.route.id || "unknown"
- },
- fn: async (current2) => {
- const traced_event = merge_tracing(event, current2);
- const result2 = await with_request_store(
- { event: traced_event, state: event_state },
- () => load.call(null, {
- ...traced_event,
- fetch: (info, init2) => {
- new URL(info instanceof Request ? info.url : info, event.url);
- return event.fetch(info, init2);
- },
- /** @param {string[]} deps */
- depends: (...deps) => {
- for (const dep of deps) {
- const { href } = new URL(dep, event.url);
- uses.dependencies.add(href);
- }
- },
- params: new Proxy(event.params, {
- get: (target, key2) => {
- if (is_tracking) {
- uses.params.add(key2);
- }
- return target[
- /** @type {string} */
- key2
- ];
- }
- }),
- parent: async () => {
- if (is_tracking) {
- uses.parent = true;
- }
- return parent();
- },
- route: new Proxy(event.route, {
- get: (target, key2) => {
- if (is_tracking) {
- uses.route = true;
- }
- return target[
- /** @type {'id'} */
- key2
- ];
- }
- }),
- url,
- untrack(fn) {
- is_tracking = false;
- try {
- return fn();
- } finally {
- is_tracking = true;
- }
- }
- })
- );
- return result2;
- }
- });
- return {
- type: "data",
- data: result ?? null,
- uses,
- slash
- };
- }
- async function load_data({
- event,
- event_state,
- fetched,
- node,
- parent,
- server_data_promise,
- state,
- resolve_opts,
- csr
- }) {
- const server_data_node = await server_data_promise;
- const load = node?.universal?.load;
- if (!load) {
- return server_data_node?.data ?? null;
- }
- const result = await record_span({
- name: "sveltekit.load",
- attributes: {
- "sveltekit.load.node_id": node.universal_id || "unknown",
- "sveltekit.load.node_type": get_node_type(node.universal_id),
- "http.route": event.route.id || "unknown"
- },
- fn: async (current2) => {
- const traced_event = merge_tracing(event, current2);
- return await with_request_store(
- { event: traced_event, state: event_state },
- () => load.call(null, {
- url: event.url,
- params: event.params,
- data: server_data_node?.data ?? null,
- route: event.route,
- fetch: create_universal_fetch(event, state, fetched, csr, resolve_opts),
- setHeaders: event.setHeaders,
- depends: () => {
- },
- parent,
- untrack: (fn) => fn(),
- tracing: traced_event.tracing
- })
- );
- }
- });
- return result ?? null;
- }
- function create_universal_fetch(event, state, fetched, csr, resolve_opts) {
- const universal_fetch = async (input, init2) => {
- const cloned_body = input instanceof Request && input.body ? input.clone().body : null;
- const cloned_headers = input instanceof Request && [...input.headers].length ? new Headers(input.headers) : init2?.headers;
- let response = await event.fetch(input, init2);
- const url = new URL(input instanceof Request ? input.url : input, event.url);
- const same_origin = url.origin === event.url.origin;
- let dependency;
- if (same_origin) {
- if (state.prerendering) {
- dependency = { response, body: null };
- state.prerendering.dependencies.set(url.pathname, dependency);
- }
- } else if (url.protocol === "https:" || url.protocol === "http:") {
- const mode = input instanceof Request ? input.mode : init2?.mode ?? "cors";
- if (mode === "no-cors") {
- response = new Response("", {
- status: response.status,
- statusText: response.statusText,
- headers: response.headers
- });
- } else {
- const acao = response.headers.get("access-control-allow-origin");
- if (!acao || acao !== event.url.origin && acao !== "*") {
- throw new Error(
- `CORS error: ${acao ? "Incorrect" : "No"} 'Access-Control-Allow-Origin' header is present on the requested resource`
- );
- }
- }
- }
- let teed_body;
- const proxy = new Proxy(response, {
- get(response2, key2, receiver) {
- async function push_fetched(body2, is_b64) {
- const status_number = Number(response2.status);
- if (isNaN(status_number)) {
- throw new Error(
- `response.status is not a number. value: "${response2.status}" type: ${typeof response2.status}`
- );
- }
- fetched.push({
- url: same_origin ? url.href.slice(event.url.origin.length) : url.href,
- method: event.request.method,
- request_body: (
- /** @type {string | ArrayBufferView | undefined} */
- input instanceof Request && cloned_body ? await stream_to_string(cloned_body) : init2?.body
- ),
- request_headers: cloned_headers,
- response_body: body2,
- response: response2,
- is_b64
- });
- }
- if (key2 === "body") {
- if (response2.body === null) {
- return null;
- }
- if (teed_body) {
- return teed_body;
- }
- const [a, b] = response2.body.tee();
- void (async () => {
- let result = new Uint8Array();
- for await (const chunk of a) {
- const combined = new Uint8Array(result.length + chunk.length);
- combined.set(result, 0);
- combined.set(chunk, result.length);
- result = combined;
- }
- if (dependency) {
- dependency.body = new Uint8Array(result);
- }
- void push_fetched(base64_encode(result), true);
- })();
- return teed_body = b;
- }
- if (key2 === "arrayBuffer") {
- return async () => {
- const buffer = await response2.arrayBuffer();
- const bytes = new Uint8Array(buffer);
- if (dependency) {
- dependency.body = bytes;
- }
- if (buffer instanceof ArrayBuffer) {
- await push_fetched(base64_encode(bytes), true);
- }
- return buffer;
- };
- }
- async function text2() {
- const body2 = await response2.text();
- if (body2 === "" && NULL_BODY_STATUS.includes(response2.status)) {
- await push_fetched(void 0, false);
- return void 0;
- }
- if (!body2 || typeof body2 === "string") {
- await push_fetched(body2, false);
- }
- if (dependency) {
- dependency.body = body2;
- }
- return body2;
- }
- if (key2 === "text") {
- return text2;
- }
- if (key2 === "json") {
- return async () => {
- const body2 = await text2();
- return body2 ? JSON.parse(body2) : void 0;
- };
- }
- const value = Reflect.get(response2, key2, response2);
- if (value instanceof Function) {
- return Object.defineProperties(
- /**
- * @this {any}
- */
- function() {
- return Reflect.apply(value, this === receiver ? response2 : this, arguments);
- },
- {
- name: { value: value.name },
- length: { value: value.length }
- }
- );
- }
- return value;
- }
- });
- if (csr) {
- const get = response.headers.get;
- response.headers.get = (key2) => {
- const lower = key2.toLowerCase();
- const value = get.call(response.headers, lower);
- if (value && !lower.startsWith("x-sveltekit-")) {
- const included = resolve_opts.filterSerializedResponseHeaders(lower, value);
- if (!included) {
- throw new Error(
- `Failed to get response header "${lower}" — it must be included by the \`filterSerializedResponseHeaders\` option: https://svelte.dev/docs/kit/hooks#Server-hooks-handle (at ${event.route.id})`
- );
- }
- }
- return value;
- };
- }
- return proxy;
- };
- return (input, init2) => {
- const response = universal_fetch(input, init2);
- response.catch(() => {
- });
- return response;
- };
- }
- async function stream_to_string(stream) {
- let result = "";
- const reader = stream.getReader();
- while (true) {
- const { done, value } = await reader.read();
- if (done) {
- break;
- }
- result += text_decoder.decode(value);
- }
- return result;
- }
- function hash(...values) {
- let hash2 = 5381;
- for (const value of values) {
- if (typeof value === "string") {
- let i = value.length;
- while (i) hash2 = hash2 * 33 ^ value.charCodeAt(--i);
- } else if (ArrayBuffer.isView(value)) {
- const buffer = new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
- let i = buffer.length;
- while (i) hash2 = hash2 * 33 ^ buffer[--i];
- } else {
- throw new TypeError("value must be a string or TypedArray");
- }
- }
- return (hash2 >>> 0).toString(36);
- }
- const replacements = {
- "<": "\\u003C",
- "\u2028": "\\u2028",
- "\u2029": "\\u2029"
- };
- const pattern = new RegExp(`[${Object.keys(replacements).join("")}]`, "g");
- function serialize_data(fetched, filter, prerendering = false) {
- const headers2 = {};
- let cache_control = null;
- let age = null;
- let varyAny = false;
- for (const [key2, value] of fetched.response.headers) {
- if (filter(key2, value)) {
- headers2[key2] = value;
- }
- if (key2 === "cache-control") cache_control = value;
- else if (key2 === "age") age = value;
- else if (key2 === "vary" && value.trim() === "*") varyAny = true;
- }
- const payload = {
- status: fetched.response.status,
- statusText: fetched.response.statusText,
- headers: headers2,
- body: fetched.response_body
- };
- const safe_payload = JSON.stringify(payload).replace(pattern, (match) => replacements[match]);
- const attrs = [
- 'type="application/json"',
- "data-sveltekit-fetched",
- `data-url="${escape_html(fetched.url, true)}"`
- ];
- if (fetched.is_b64) {
- attrs.push("data-b64");
- }
- if (fetched.request_headers || fetched.request_body) {
- const values = [];
- if (fetched.request_headers) {
- values.push([...new Headers(fetched.request_headers)].join(","));
- }
- if (fetched.request_body) {
- values.push(fetched.request_body);
- }
- attrs.push(`data-hash="${hash(...values)}"`);
- }
- if (!prerendering && fetched.method === "GET" && cache_control && !varyAny) {
- const match = /s-maxage=(\d+)/g.exec(cache_control) ?? /max-age=(\d+)/g.exec(cache_control);
- if (match) {
- const ttl = +match[1] - +(age ?? "0");
- attrs.push(`data-ttl="${ttl}"`);
- }
- }
- return `<script ${attrs.join(" ")}>${safe_payload}<\/script>`;
- }
- const s = JSON.stringify;
- function sha256(data) {
- if (!key[0]) precompute();
- const out = init.slice(0);
- const array2 = encode(data);
- for (let i = 0; i < array2.length; i += 16) {
- const w = array2.subarray(i, i + 16);
- let tmp;
- let a;
- let b;
- let out0 = out[0];
- let out1 = out[1];
- let out2 = out[2];
- let out3 = out[3];
- let out4 = out[4];
- let out5 = out[5];
- let out6 = out[6];
- let out7 = out[7];
- for (let i2 = 0; i2 < 64; i2++) {
- if (i2 < 16) {
- tmp = w[i2];
- } else {
- a = w[i2 + 1 & 15];
- b = w[i2 + 14 & 15];
- tmp = w[i2 & 15] = (a >>> 7 ^ a >>> 18 ^ a >>> 3 ^ a << 25 ^ a << 14) + (b >>> 17 ^ b >>> 19 ^ b >>> 10 ^ b << 15 ^ b << 13) + w[i2 & 15] + w[i2 + 9 & 15] | 0;
- }
- tmp = tmp + out7 + (out4 >>> 6 ^ out4 >>> 11 ^ out4 >>> 25 ^ out4 << 26 ^ out4 << 21 ^ out4 << 7) + (out6 ^ out4 & (out5 ^ out6)) + key[i2];
- out7 = out6;
- out6 = out5;
- out5 = out4;
- out4 = out3 + tmp | 0;
- out3 = out2;
- out2 = out1;
- out1 = out0;
- out0 = tmp + (out1 & out2 ^ out3 & (out1 ^ out2)) + (out1 >>> 2 ^ out1 >>> 13 ^ out1 >>> 22 ^ out1 << 30 ^ out1 << 19 ^ out1 << 10) | 0;
- }
- out[0] = out[0] + out0 | 0;
- out[1] = out[1] + out1 | 0;
- out[2] = out[2] + out2 | 0;
- out[3] = out[3] + out3 | 0;
- out[4] = out[4] + out4 | 0;
- out[5] = out[5] + out5 | 0;
- out[6] = out[6] + out6 | 0;
- out[7] = out[7] + out7 | 0;
- }
- const bytes = new Uint8Array(out.buffer);
- reverse_endianness(bytes);
- return btoa(String.fromCharCode(...bytes));
- }
- const init = new Uint32Array(8);
- const key = new Uint32Array(64);
- function precompute() {
- function frac(x) {
- return (x - Math.floor(x)) * 4294967296;
- }
- let prime = 2;
- for (let i = 0; i < 64; prime++) {
- let is_prime = true;
- for (let factor = 2; factor * factor <= prime; factor++) {
- if (prime % factor === 0) {
- is_prime = false;
- break;
- }
- }
- if (is_prime) {
- if (i < 8) {
- init[i] = frac(prime ** (1 / 2));
- }
- key[i] = frac(prime ** (1 / 3));
- i++;
- }
- }
- }
- function reverse_endianness(bytes) {
- for (let i = 0; i < bytes.length; i += 4) {
- const a = bytes[i + 0];
- const b = bytes[i + 1];
- const c = bytes[i + 2];
- const d = bytes[i + 3];
- bytes[i + 0] = d;
- bytes[i + 1] = c;
- bytes[i + 2] = b;
- bytes[i + 3] = a;
- }
- }
- function encode(str) {
- const encoded = text_encoder.encode(str);
- const length = encoded.length * 8;
- const size = 512 * Math.ceil((length + 65) / 512);
- const bytes = new Uint8Array(size / 8);
- bytes.set(encoded);
- bytes[encoded.length] = 128;
- reverse_endianness(bytes);
- const words = new Uint32Array(bytes.buffer);
- words[words.length - 2] = Math.floor(length / 4294967296);
- words[words.length - 1] = length;
- return words;
- }
- const array = new Uint8Array(16);
- function generate_nonce() {
- crypto.getRandomValues(array);
- return btoa(String.fromCharCode(...array));
- }
- const quoted = /* @__PURE__ */ new Set([
- "self",
- "unsafe-eval",
- "unsafe-hashes",
- "unsafe-inline",
- "none",
- "strict-dynamic",
- "report-sample",
- "wasm-unsafe-eval",
- "script"
- ]);
- const crypto_pattern = /^(nonce|sha\d\d\d)-/;
- class BaseProvider {
- /** @type {boolean} */
- #use_hashes;
- /** @type {boolean} */
- #script_needs_csp;
- /** @type {boolean} */
- #script_src_needs_csp;
- /** @type {boolean} */
- #script_src_elem_needs_csp;
- /** @type {boolean} */
- #style_needs_csp;
- /** @type {boolean} */
- #style_src_needs_csp;
- /** @type {boolean} */
- #style_src_attr_needs_csp;
- /** @type {boolean} */
- #style_src_elem_needs_csp;
- /** @type {import('types').CspDirectives} */
- #directives;
- /** @type {Set<import('types').Csp.Source>} */
- #script_src;
- /** @type {Set<import('types').Csp.Source>} */
- #script_src_elem;
- /** @type {Set<import('types').Csp.Source>} */
- #style_src;
- /** @type {Set<import('types').Csp.Source>} */
- #style_src_attr;
- /** @type {Set<import('types').Csp.Source>} */
- #style_src_elem;
- /** @type {boolean} */
- script_needs_nonce;
- /** @type {boolean} */
- style_needs_nonce;
- /** @type {boolean} */
- script_needs_hash;
- /** @type {string} */
- #nonce;
- /**
- * @param {boolean} use_hashes
- * @param {import('types').CspDirectives} directives
- * @param {string} nonce
- */
- constructor(use_hashes, directives, nonce) {
- this.#use_hashes = use_hashes;
- this.#directives = directives;
- const d = this.#directives;
- this.#script_src = /* @__PURE__ */ new Set();
- this.#script_src_elem = /* @__PURE__ */ new Set();
- this.#style_src = /* @__PURE__ */ new Set();
- this.#style_src_attr = /* @__PURE__ */ new Set();
- this.#style_src_elem = /* @__PURE__ */ new Set();
- const effective_script_src = d["script-src"] || d["default-src"];
- const script_src_elem = d["script-src-elem"];
- const effective_style_src = d["style-src"] || d["default-src"];
- const style_src_attr = d["style-src-attr"];
- const style_src_elem = d["style-src-elem"];
- const style_needs_csp = (directive) => !!directive && !directive.some((value) => value === "unsafe-inline");
- const script_needs_csp = (directive) => !!directive && (!directive.some((value) => value === "unsafe-inline") || directive.some((value) => value === "strict-dynamic"));
- this.#script_src_needs_csp = script_needs_csp(effective_script_src);
- this.#script_src_elem_needs_csp = script_needs_csp(script_src_elem);
- this.#style_src_needs_csp = style_needs_csp(effective_style_src);
- this.#style_src_attr_needs_csp = style_needs_csp(style_src_attr);
- this.#style_src_elem_needs_csp = style_needs_csp(style_src_elem);
- this.#script_needs_csp = this.#script_src_needs_csp || this.#script_src_elem_needs_csp;
- this.#style_needs_csp = this.#style_src_needs_csp || this.#style_src_attr_needs_csp || this.#style_src_elem_needs_csp;
- this.script_needs_nonce = this.#script_needs_csp && !this.#use_hashes;
- this.style_needs_nonce = this.#style_needs_csp && !this.#use_hashes;
- this.script_needs_hash = this.#script_needs_csp && this.#use_hashes;
- this.#nonce = nonce;
- }
- /** @param {string} content */
- add_script(content) {
- if (!this.#script_needs_csp) return;
- const source = this.#use_hashes ? `sha256-${sha256(content)}` : `nonce-${this.#nonce}`;
- if (this.#script_src_needs_csp) {
- this.#script_src.add(source);
- }
- if (this.#script_src_elem_needs_csp) {
- this.#script_src_elem.add(source);
- }
- }
- /** @param {`sha256-${string}`[]} hashes */
- add_script_hashes(hashes) {
- for (const hash2 of hashes) {
- if (this.#script_src_needs_csp) {
- this.#script_src.add(hash2);
- }
- if (this.#script_src_elem_needs_csp) {
- this.#script_src_elem.add(hash2);
- }
- }
- }
- /** @param {string} content */
- add_style(content) {
- if (!this.#style_needs_csp) return;
- const source = this.#use_hashes ? `sha256-${sha256(content)}` : `nonce-${this.#nonce}`;
- if (this.#style_src_needs_csp) {
- this.#style_src.add(source);
- }
- if (this.#style_src_attr_needs_csp) {
- this.#style_src_attr.add(source);
- }
- if (this.#style_src_elem_needs_csp) {
- const sha256_empty_comment_hash = "sha256-9OlNO0DNEeaVzHL4RZwCLsBHA8WBQ8toBp/4F5XV2nc=";
- const d = this.#directives;
- if (d["style-src-elem"] && !d["style-src-elem"].includes(sha256_empty_comment_hash) && !this.#style_src_elem.has(sha256_empty_comment_hash)) {
- this.#style_src_elem.add(sha256_empty_comment_hash);
- }
- if (source !== sha256_empty_comment_hash) {
- this.#style_src_elem.add(source);
- }
- }
- }
- /**
- * @param {boolean} [is_meta]
- */
- get_header(is_meta = false) {
- const header = [];
- const directives = { ...this.#directives };
- if (this.#style_src.size > 0) {
- directives["style-src"] = [
- ...directives["style-src"] || directives["default-src"] || [],
- ...this.#style_src
- ];
- }
- if (this.#style_src_attr.size > 0) {
- directives["style-src-attr"] = [
- ...directives["style-src-attr"] || [],
- ...this.#style_src_attr
- ];
- }
- if (this.#style_src_elem.size > 0) {
- directives["style-src-elem"] = [
- ...directives["style-src-elem"] || [],
- ...this.#style_src_elem
- ];
- }
- if (this.#script_src.size > 0) {
- directives["script-src"] = [
- ...directives["script-src"] || directives["default-src"] || [],
- ...this.#script_src
- ];
- }
- if (this.#script_src_elem.size > 0) {
- directives["script-src-elem"] = [
- ...directives["script-src-elem"] || [],
- ...this.#script_src_elem
- ];
- }
- for (const key2 in directives) {
- if (is_meta && (key2 === "frame-ancestors" || key2 === "report-uri" || key2 === "sandbox")) {
- continue;
- }
- const value = (
- /** @type {string[] | true} */
- directives[key2]
- );
- if (!value) continue;
- const directive = [key2];
- if (Array.isArray(value)) {
- value.forEach((value2) => {
- if (quoted.has(value2) || crypto_pattern.test(value2)) {
- directive.push(`'${value2}'`);
- } else {
- directive.push(value2);
- }
- });
- }
- header.push(directive.join(" "));
- }
- return header.join("; ");
- }
- }
- class CspProvider extends BaseProvider {
- get_meta() {
- const content = this.get_header(true);
- if (!content) {
- return;
- }
- return `<meta http-equiv="content-security-policy" content="${escape_html(content, true)}">`;
- }
- }
- class CspReportOnlyProvider extends BaseProvider {
- /**
- * @param {boolean} use_hashes
- * @param {import('types').CspDirectives} directives
- * @param {string} nonce
- */
- constructor(use_hashes, directives, nonce) {
- super(use_hashes, directives, nonce);
- if (Object.values(directives).filter((v) => !!v).length > 0) {
- const has_report_to = directives["report-to"]?.length ?? 0 > 0;
- const has_report_uri = directives["report-uri"]?.length ?? 0 > 0;
- if (!has_report_to && !has_report_uri) {
- throw Error(
- "`content-security-policy-report-only` must be specified with either the `report-to` or `report-uri` directives, or both"
- );
- }
- }
- }
- }
- class Csp {
- /** @readonly */
- nonce = generate_nonce();
- /** @type {CspProvider} */
- csp_provider;
- /** @type {CspReportOnlyProvider} */
- report_only_provider;
- /**
- * @param {import('./types.js').CspConfig} config
- * @param {import('./types.js').CspOpts} opts
- */
- constructor({ mode, directives, reportOnly }, { prerender }) {
- const use_hashes = mode === "hash" || mode === "auto" && prerender;
- this.csp_provider = new CspProvider(use_hashes, directives, this.nonce);
- this.report_only_provider = new CspReportOnlyProvider(use_hashes, reportOnly, this.nonce);
- }
- get script_needs_hash() {
- return this.csp_provider.script_needs_hash || this.report_only_provider.script_needs_hash;
- }
- get script_needs_nonce() {
- return this.csp_provider.script_needs_nonce || this.report_only_provider.script_needs_nonce;
- }
- get style_needs_nonce() {
- return this.csp_provider.style_needs_nonce || this.report_only_provider.style_needs_nonce;
- }
- /** @param {string} content */
- add_script(content) {
- this.csp_provider.add_script(content);
- this.report_only_provider.add_script(content);
- }
- /** @param {`sha256-${string}`[]} hashes */
- add_script_hashes(hashes) {
- this.csp_provider.add_script_hashes(hashes);
- this.report_only_provider.add_script_hashes(hashes);
- }
- /** @param {string} content */
- add_style(content) {
- this.csp_provider.add_style(content);
- this.report_only_provider.add_style(content);
- }
- }
- function exec(match, params, matchers) {
- const result = {};
- const values = match.slice(1);
- const values_needing_match = values.filter((value) => value !== void 0);
- let buffered = 0;
- for (let i = 0; i < params.length; i += 1) {
- const param = params[i];
- let value = values[i - buffered];
- if (param.chained && param.rest && buffered) {
- value = values.slice(i - buffered, i + 1).filter((s2) => s2).join("/");
- buffered = 0;
- }
- if (value === void 0) {
- if (param.rest) {
- value = "";
- } else {
- continue;
- }
- }
- if (!param.matcher || matchers[param.matcher](value)) {
- result[param.name] = value;
- const next_param = params[i + 1];
- const next_value = values[i + 1];
- if (next_param && !next_param.rest && next_param.optional && next_value && param.chained) {
- buffered = 0;
- }
- if (!next_param && !next_value && Object.keys(result).length === values_needing_match.length) {
- buffered = 0;
- }
- continue;
- }
- if (param.optional && param.chained) {
- buffered++;
- continue;
- }
- return;
- }
- if (buffered) return;
- return result;
- }
- function find_route(path, routes, matchers) {
- for (const route of routes) {
- const match = route.pattern.exec(path);
- if (!match) continue;
- const matched = exec(match, route.params, matchers);
- if (matched) {
- return {
- route,
- params: decode_params(matched)
- };
- }
- }
- return null;
- }
- function generate_route_object(route, url, manifest) {
- const { errors, layouts, leaf } = route;
- const nodes = [...errors, ...layouts.map((l) => l?.[1]), leaf[1]].filter((n) => typeof n === "number").map((n) => `'${n}': () => ${create_client_import(manifest._.client.nodes?.[n], url)}`).join(",\n ");
- return [
- `{
- id: ${s(route.id)}`,
- `errors: ${s(route.errors)}`,
- `layouts: ${s(route.layouts)}`,
- `leaf: ${s(route.leaf)}`,
- `nodes: {
- ${nodes}
- }
- }`
- ].join(",\n ");
- }
- function create_client_import(import_path, url) {
- if (!import_path) return "Promise.resolve({})";
- if (import_path[0] === "/") {
- return `import('${import_path}')`;
- }
- if (assets !== "") {
- return `import('${assets}/${import_path}')`;
- }
- let path = get_relative_path(url.pathname, `${base}/${import_path}`);
- if (path[0] !== ".") path = `./${path}`;
- return `import('${path}')`;
- }
- async function resolve_route(resolved_path, url, manifest) {
- if (!manifest._.client.routes) {
- return text("Server-side route resolution disabled", { status: 400 });
- }
- const matchers = await manifest._.matchers();
- const result = find_route(resolved_path, manifest._.client.routes, matchers);
- return create_server_routing_response(result?.route ?? null, result?.params ?? {}, url, manifest).response;
- }
- function create_server_routing_response(route, params, url, manifest) {
- const headers2 = new Headers({
- "content-type": "application/javascript; charset=utf-8"
- });
- if (route) {
- const csr_route = generate_route_object(route, url, manifest);
- const body2 = `${create_css_import(route, url, manifest)}
- export const route = ${csr_route}; export const params = ${JSON.stringify(params)};`;
- return { response: text(body2, { headers: headers2 }), body: body2 };
- } else {
- return { response: text("", { headers: headers2 }), body: "" };
- }
- }
- function create_css_import(route, url, manifest) {
- const { errors, layouts, leaf } = route;
- let css = "";
- for (const node of [...errors, ...layouts.map((l) => l?.[1]), leaf[1]]) {
- if (typeof node !== "number") continue;
- const node_css = manifest._.client.css?.[node];
- for (const css_path of node_css ?? []) {
- css += `'${assets || base}/${css_path}',`;
- }
- }
- if (!css) return "";
- return `${create_client_import(
- /** @type {string} */
- manifest._.client.start,
- url
- )}.then(x => x.load_css([${css}]));`;
- }
- const updated = {
- ...readable(false),
- check: () => false
- };
- async function render_response({
- branch,
- fetched,
- options: options2,
- manifest,
- state,
- page_config,
- status,
- error: error2 = null,
- event,
- event_state,
- resolve_opts,
- action_result,
- data_serializer,
- error_components
- }) {
- if (state.prerendering) {
- if (options2.csp.mode === "nonce") {
- throw new Error('Cannot use prerendering if config.kit.csp.mode === "nonce"');
- }
- if (options2.app_template_contains_nonce) {
- throw new Error("Cannot use prerendering if page template contains %sveltekit.nonce%");
- }
- }
- const { client } = manifest._;
- const modulepreloads = new Set(client.imports);
- const stylesheets = new Set(client.stylesheets);
- const fonts = new Set(client.fonts);
- const link_headers = /* @__PURE__ */ new Set();
- const inline_styles = /* @__PURE__ */ new Map();
- let rendered;
- const form_value = action_result?.type === "success" || action_result?.type === "failure" ? action_result.data ?? null : null;
- let base$1 = base;
- let assets$1 = assets;
- let base_expression = s(base);
- const csp = new Csp(options2.csp, {
- prerender: !!state.prerendering
- });
- {
- if (!state.prerendering?.fallback) {
- const segments = event.url.pathname.slice(base.length).split("/").slice(2);
- base$1 = segments.map(() => "..").join("/") || ".";
- base_expression = `new URL(${s(base$1)}, location).pathname.slice(0, -1)`;
- if (!assets || assets[0] === "/" && assets !== SVELTE_KIT_ASSETS) {
- assets$1 = base$1;
- }
- } else if (options2.hash_routing) {
- base_expression = "new URL('.', location).pathname.slice(0, -1)";
- }
- }
- if (page_config.ssr) {
- const props = {
- stores: {
- page: writable(null),
- navigating: writable(null),
- updated
- },
- constructors: await Promise.all(
- branch.map(({ node }) => {
- if (!node.component) {
- throw new Error(`Missing +page.svelte component for route ${event.route.id}`);
- }
- return node.component();
- })
- ),
- form: form_value
- };
- if (error_components) {
- if (error2) {
- props.error = error2;
- }
- props.errors = error_components;
- }
- let data2 = {};
- for (let i = 0; i < branch.length; i += 1) {
- data2 = { ...data2, ...branch[i].data };
- props[`data_${i}`] = data2;
- }
- props.page = {
- error: error2,
- params: (
- /** @type {Record<string, any>} */
- event.params
- ),
- route: event.route,
- status,
- url: event.url,
- data: data2,
- form: form_value,
- state: {}
- };
- const render_opts = {
- context: /* @__PURE__ */ new Map([
- [
- "__request__",
- {
- page: props.page
- }
- ]
- ]),
- csp: csp.script_needs_nonce ? { nonce: csp.nonce } : { hash: csp.script_needs_hash },
- transformError: error_components ? (
- /** @param {unknown} e */
- async (e) => {
- const transformed2 = await handle_error_and_jsonify(event, event_state, options2, e);
- props.page.error = props.error = error2 = transformed2;
- props.page.status = status = get_status(e);
- return transformed2;
- }
- ) : void 0
- };
- const fetch2 = globalThis.fetch;
- try {
- if (BROWSER) ;
- event_state.allows_commands = false;
- rendered = await with_request_store({ event, state: event_state }, async () => {
- if (relative) override({ base: base$1, assets: assets$1 });
- const maybe_promise = options2.root.render(props, render_opts);
- const rendered2 = options2.async && "then" in maybe_promise ? (
- /** @type {ReturnType<typeof options.root.render> & Promise<any>} */
- maybe_promise.then((r) => r)
- ) : maybe_promise;
- if (options2.async) {
- reset();
- }
- const { head: head2, html: html2, css, hashes } = (
- /** @type {ReturnType<typeof options.root.render>} */
- options2.async ? await rendered2 : rendered2
- );
- if (hashes) {
- csp.add_script_hashes(hashes.script);
- }
- return { head: head2, html: html2, css, hashes };
- });
- } finally {
- reset();
- }
- for (const { node } of branch) {
- for (const url of node.imports) modulepreloads.add(url);
- for (const url of node.stylesheets) stylesheets.add(url);
- for (const url of node.fonts) fonts.add(url);
- if (node.inline_styles && !client.inline) {
- Object.entries(await node.inline_styles()).forEach(([filename, css]) => {
- if (typeof css === "string") {
- inline_styles.set(filename, css);
- return;
- }
- inline_styles.set(filename, css(`${assets$1}/${app_dir}/immutable/assets`, assets$1));
- });
- }
- }
- } else {
- rendered = { head: "", html: "", css: { code: "", map: null }, hashes: { script: [] } };
- }
- const head = new Head(rendered.head, !!state.prerendering);
- let body2 = rendered.html;
- const prefixed = (path) => {
- if (path.startsWith("/")) {
- return base + path;
- }
- return `${assets$1}/${path}`;
- };
- const style = client.inline ? client.inline?.style : Array.from(inline_styles.values()).join("\n");
- if (style) {
- const attributes = [];
- if (csp.style_needs_nonce) attributes.push(`nonce="${csp.nonce}"`);
- csp.add_style(style);
- head.add_style(style, attributes);
- }
- for (const dep of stylesheets) {
- const path = prefixed(dep);
- const attributes = ['rel="stylesheet"'];
- if (inline_styles.has(dep)) {
- attributes.push("disabled", 'media="(max-width: 0)"');
- } else {
- if (resolve_opts.preload({ type: "css", path })) {
- link_headers.add(`<${encodeURI(path)}>; rel="preload"; as="style"; nopush`);
- }
- }
- head.add_stylesheet(path, attributes);
- }
- for (const dep of fonts) {
- const path = prefixed(dep);
- if (resolve_opts.preload({ type: "font", path })) {
- const ext = dep.slice(dep.lastIndexOf(".") + 1);
- head.add_link_tag(path, ['rel="preload"', 'as="font"', `type="font/${ext}"`, "crossorigin"]);
- link_headers.add(
- `<${encodeURI(path)}>; rel="preload"; as="font"; type="font/${ext}"; crossorigin; nopush`
- );
- }
- }
- const global = get_global_name(options2);
- const { data, chunks } = data_serializer.get_data(csp);
- if (page_config.ssr && page_config.csr) {
- body2 += `
- ${fetched.map(
- (item) => serialize_data(item, resolve_opts.filterSerializedResponseHeaders, !!state.prerendering)
- ).join("\n ")}`;
- }
- if (page_config.csr) {
- const route = manifest._.client.routes?.find((r) => r.id === event.route.id) ?? null;
- if (client.uses_env_dynamic_public && state.prerendering) {
- modulepreloads.add(`${app_dir}/env.js`);
- }
- if (!client.inline) {
- const included_modulepreloads = Array.from(modulepreloads, (dep) => prefixed(dep)).filter(
- (path) => resolve_opts.preload({ type: "js", path })
- );
- for (const path of included_modulepreloads) {
- link_headers.add(`<${encodeURI(path)}>; rel="modulepreload"; nopush`);
- if (options2.preload_strategy !== "modulepreload") {
- head.add_script_preload(path);
- } else {
- head.add_link_tag(path, ['rel="modulepreload"']);
- }
- }
- }
- if (manifest._.client.routes && state.prerendering && !state.prerendering.fallback) {
- const pathname = add_resolution_suffix(event.url.pathname);
- state.prerendering.dependencies.set(
- pathname,
- create_server_routing_response(route, event.params, new URL(pathname, event.url), manifest)
- );
- }
- const blocks = [];
- const load_env_eagerly = client.uses_env_dynamic_public && state.prerendering;
- const properties = [`base: ${base_expression}`];
- if (assets) {
- properties.push(`assets: ${s(assets)}`);
- }
- if (client.uses_env_dynamic_public) {
- properties.push(`env: ${load_env_eagerly ? "null" : s(public_env)}`);
- }
- if (chunks) {
- blocks.push("const deferred = new Map();");
- properties.push(`defer: (id) => new Promise((fulfil, reject) => {
- deferred.set(id, { fulfil, reject });
- })`);
- let app_declaration = "";
- if (Object.keys(options2.hooks.transport).length > 0) {
- if (client.inline) {
- app_declaration = `const app = __sveltekit_${options2.version_hash}.app.app;`;
- } else if (client.app) {
- app_declaration = `const app = await import(${s(prefixed(client.app))});`;
- } else {
- app_declaration = `const { app } = await import(${s(prefixed(client.start))});`;
- }
- }
- const prelude = app_declaration ? `${app_declaration}
- const [data, error] = fn(app);` : `const [data, error] = fn();`;
- properties.push(`resolve: async (id, fn) => {
- ${prelude}
- const try_to_resolve = () => {
- if (!deferred.has(id)) {
- setTimeout(try_to_resolve, 0);
- return;
- }
- const { fulfil, reject } = deferred.get(id);
- deferred.delete(id);
- if (error) reject(error);
- else fulfil(data);
- }
- try_to_resolve();
- }`);
- }
- blocks.push(`${global} = {
- ${properties.join(",\n ")}
- };`);
- const args = ["element"];
- blocks.push("const element = document.currentScript.parentElement;");
- if (page_config.ssr) {
- const serialized = { form: "null", error: "null" };
- if (form_value) {
- serialized.form = uneval_action_response(
- form_value,
- /** @type {string} */
- event.route.id,
- options2.hooks.transport
- );
- }
- if (error2) {
- serialized.error = devalue.uneval(error2);
- }
- const hydrate = [
- `node_ids: [${branch.map(({ node }) => node.index).join(", ")}]`,
- `data: ${data}`,
- `form: ${serialized.form}`,
- `error: ${serialized.error}`
- ];
- if (status !== 200) {
- hydrate.push(`status: ${status}`);
- }
- if (manifest._.client.routes) {
- if (route) {
- const stringified = generate_route_object(route, event.url, manifest).replaceAll(
- "\n",
- "\n "
- );
- hydrate.push(`params: ${devalue.uneval(event.params)}`, `server_route: ${stringified}`);
- }
- } else if (options2.embedded) {
- hydrate.push(`params: ${devalue.uneval(event.params)}`, `route: ${s(event.route)}`);
- }
- const indent = " ".repeat(load_env_eagerly ? 7 : 6);
- args.push(`{
- ${indent} ${hydrate.join(`,
- ${indent} `)}
- ${indent}}`);
- }
- const { remote_data: remote_cache } = event_state;
- let serialized_remote_data = "";
- if (remote_cache) {
- const remote = {};
- for (const [info, cache] of remote_cache) {
- if (!info.id) continue;
- for (const key2 in cache) {
- const remote_key = create_remote_key(info.id, key2);
- if (event_state.refreshes?.[remote_key] !== void 0) {
- remote[remote_key] = await cache[key2];
- } else {
- const result = await Promise.race([
- Promise.resolve(cache[key2]).then(
- (v) => (
- /** @type {const} */
- { settled: true, value: v }
- ),
- (e) => (
- /** @type {const} */
- { settled: true, error: e }
- )
- ),
- new Promise((resolve2) => {
- queueMicrotask(() => resolve2(
- /** @type {const} */
- { settled: false }
- ));
- })
- ]);
- if (result.settled) {
- if ("error" in result) throw result.error;
- remote[remote_key] = result.value;
- }
- }
- }
- }
- const replacer = (thing) => {
- for (const key2 in options2.hooks.transport) {
- const encoded = options2.hooks.transport[key2].encode(thing);
- if (encoded) {
- return `app.decode('${key2}', ${devalue.uneval(encoded, replacer)})`;
- }
- }
- };
- serialized_remote_data = `${global}.data = ${devalue.uneval(remote, replacer)};
- `;
- }
- const boot = client.inline ? `${client.inline.script}
- ${serialized_remote_data}${global}.app.start(${args.join(", ")});` : client.app ? `Promise.all([
- import(${s(prefixed(client.start))}),
- import(${s(prefixed(client.app))})
- ]).then(([kit, app]) => {
- ${serialized_remote_data}kit.start(app, ${args.join(", ")});
- });` : `import(${s(prefixed(client.start))}).then((app) => {
- ${serialized_remote_data}app.start(${args.join(", ")})
- });`;
- if (load_env_eagerly) {
- blocks.push(`import(${s(`${base$1}/${app_dir}/env.js`)}).then(({ env }) => {
- ${global}.env = env;
- ${boot.replace(/\n/g, "\n ")}
- });`);
- } else {
- blocks.push(boot);
- }
- if (options2.service_worker) {
- let opts = "";
- if (options2.service_worker_options != null) {
- const service_worker_options = { ...options2.service_worker_options };
- opts = `, ${s(service_worker_options)}`;
- }
- blocks.push(`if ('serviceWorker' in navigator) {
- addEventListener('load', function () {
- navigator.serviceWorker.register('${prefixed("service-worker.js")}'${opts});
- });
- }`);
- }
- const init_app = `
- {
- ${blocks.join("\n\n ")}
- }
- `;
- csp.add_script(init_app);
- body2 += `
- <script${csp.script_needs_nonce ? ` nonce="${csp.nonce}"` : ""}>${init_app}<\/script>
- `;
- }
- const headers2 = new Headers({
- "x-sveltekit-page": "true",
- "content-type": "text/html"
- });
- if (state.prerendering) {
- const csp_headers = csp.csp_provider.get_meta();
- if (csp_headers) {
- head.add_http_equiv(csp_headers);
- }
- if (state.prerendering.cache) {
- head.add_http_equiv(
- `<meta http-equiv="cache-control" content="${state.prerendering.cache}">`
- );
- }
- } else {
- const csp_header = csp.csp_provider.get_header();
- if (csp_header) {
- headers2.set("content-security-policy", csp_header);
- }
- const report_only_header = csp.report_only_provider.get_header();
- if (report_only_header) {
- headers2.set("content-security-policy-report-only", report_only_header);
- }
- if (link_headers.size) {
- headers2.set("link", Array.from(link_headers).join(", "));
- }
- }
- const html = options2.templates.app({
- head: head.build(),
- body: body2,
- assets: assets$1,
- nonce: (
- /** @type {string} */
- csp.nonce
- ),
- env: public_env
- });
- const transformed = await resolve_opts.transformPageChunk({
- html,
- done: true
- }) || "";
- if (!chunks) {
- headers2.set("etag", `"${hash(transformed)}"`);
- }
- return !chunks ? text(transformed, {
- status,
- headers: headers2
- }) : new Response(
- new ReadableStream({
- async start(controller) {
- controller.enqueue(text_encoder.encode(transformed + "\n"));
- for await (const chunk of chunks) {
- if (chunk.length) controller.enqueue(text_encoder.encode(chunk));
- }
- controller.close();
- },
- type: "bytes"
- }),
- {
- headers: headers2
- }
- );
- }
- class Head {
- #rendered;
- #prerendering;
- /** @type {string[]} */
- #http_equiv = [];
- /** @type {string[]} */
- #link_tags = [];
- /** @type {string[]} */
- #script_preloads = [];
- /** @type {string[]} */
- #style_tags = [];
- /** @type {string[]} */
- #stylesheet_links = [];
- /**
- * @param {string} rendered
- * @param {boolean} prerendering
- */
- constructor(rendered, prerendering) {
- this.#rendered = rendered;
- this.#prerendering = prerendering;
- }
- build() {
- return [
- ...this.#http_equiv,
- ...this.#link_tags,
- ...this.#script_preloads,
- this.#rendered,
- ...this.#style_tags,
- ...this.#stylesheet_links
- ].join("\n ");
- }
- /**
- * @param {string} style
- * @param {string[]} attributes
- */
- add_style(style, attributes) {
- this.#style_tags.push(
- `<style${attributes.length ? " " + attributes.join(" ") : ""}>${style}</style>`
- );
- }
- /**
- * @param {string} href
- * @param {string[]} attributes
- */
- add_stylesheet(href, attributes) {
- this.#stylesheet_links.push(`<link href="${href}" ${attributes.join(" ")}>`);
- }
- /** @param {string} href */
- add_script_preload(href) {
- this.#script_preloads.push(
- `<link rel="preload" as="script" crossorigin="anonymous" href="${href}">`
- );
- }
- /**
- * @param {string} href
- * @param {string[]} attributes
- */
- add_link_tag(href, attributes) {
- if (!this.#prerendering) return;
- this.#link_tags.push(`<link href="${href}" ${attributes.join(" ")}>`);
- }
- /** @param {string} tag */
- add_http_equiv(tag) {
- if (!this.#prerendering) return;
- this.#http_equiv.push(tag);
- }
- }
- class PageNodes {
- data;
- /**
- * @param {Array<import('types').SSRNode | undefined>} nodes
- */
- constructor(nodes) {
- this.data = nodes;
- }
- layouts() {
- return this.data.slice(0, -1);
- }
- page() {
- return this.data.at(-1);
- }
- validate() {
- for (const layout of this.layouts()) {
- if (layout) {
- validate_layout_server_exports(
- layout.server,
- /** @type {string} */
- layout.server_id
- );
- validate_layout_exports(
- layout.universal,
- /** @type {string} */
- layout.universal_id
- );
- }
- }
- const page = this.page();
- if (page) {
- validate_page_server_exports(
- page.server,
- /** @type {string} */
- page.server_id
- );
- validate_page_exports(
- page.universal,
- /** @type {string} */
- page.universal_id
- );
- }
- }
- /**
- * @template {'prerender' | 'ssr' | 'csr' | 'trailingSlash'} Option
- * @param {Option} option
- * @returns {Value | undefined}
- */
- #get_option(option) {
- return this.data.reduce(
- (value, node) => {
- return node?.universal?.[option] ?? node?.server?.[option] ?? value;
- },
- /** @type {Value | undefined} */
- void 0
- );
- }
- csr() {
- return this.#get_option("csr") ?? true;
- }
- ssr() {
- return this.#get_option("ssr") ?? true;
- }
- prerender() {
- return this.#get_option("prerender") ?? false;
- }
- trailing_slash() {
- return this.#get_option("trailingSlash") ?? "never";
- }
- get_config() {
- let current2 = {};
- for (const node of this.data) {
- if (!node?.universal?.config && !node?.server?.config) continue;
- current2 = {
- ...current2,
- // TODO: should we override the server config value with the universal value similar to other page options?
- ...node?.universal?.config,
- ...node?.server?.config
- };
- }
- return Object.keys(current2).length ? current2 : void 0;
- }
- should_prerender_data() {
- return this.data.some(
- // prerender in case of trailingSlash because the client retrieves that value from the server
- (node) => node?.server?.load || node?.server?.trailingSlash !== void 0
- );
- }
- }
- async function respond_with_error({
- event,
- event_state,
- options: options2,
- manifest,
- state,
- status,
- error: error2,
- resolve_opts
- }) {
- if (event.request.headers.get("x-sveltekit-error")) {
- return static_error_page(
- options2,
- status,
- /** @type {Error} */
- error2.message
- );
- }
- const fetched = [];
- try {
- const branch = [];
- const default_layout = await manifest._.nodes[0]();
- const nodes = new PageNodes([default_layout]);
- const ssr = nodes.ssr();
- const csr = nodes.csr();
- const data_serializer = server_data_serializer(event, event_state, options2);
- if (ssr) {
- state.error = true;
- const server_data_promise = load_server_data({
- event,
- event_state,
- state,
- node: default_layout,
- // eslint-disable-next-line @typescript-eslint/require-await
- parent: async () => ({})
- });
- const server_data = await server_data_promise;
- data_serializer.add_node(0, server_data);
- const data = await load_data({
- event,
- event_state,
- fetched,
- node: default_layout,
- // eslint-disable-next-line @typescript-eslint/require-await
- parent: async () => ({}),
- resolve_opts,
- server_data_promise,
- state,
- csr
- });
- branch.push(
- {
- node: default_layout,
- server_data,
- data
- },
- {
- node: await manifest._.nodes[1](),
- // 1 is always the root error
- data: null,
- server_data: null
- }
- );
- }
- return await render_response({
- options: options2,
- manifest,
- state,
- page_config: {
- ssr,
- csr
- },
- status,
- error: await handle_error_and_jsonify(event, event_state, options2, error2),
- branch,
- error_components: [],
- fetched,
- event,
- event_state,
- resolve_opts,
- data_serializer
- });
- } catch (e) {
- if (e instanceof Redirect) {
- return redirect_response(e.status, e.location);
- }
- return static_error_page(
- options2,
- get_status(e),
- (await handle_error_and_jsonify(event, event_state, options2, e)).message
- );
- }
- }
- async function handle_remote_call(event, state, options2, manifest, id) {
- return record_span({
- name: "sveltekit.remote.call",
- attributes: {},
- fn: (current2) => {
- const traced_event = merge_tracing(event, current2);
- return with_request_store(
- { event: traced_event, state },
- () => handle_remote_call_internal(traced_event, state, options2, manifest, id)
- );
- }
- });
- }
- async function handle_remote_call_internal(event, state, options2, manifest, id) {
- const [hash2, name, additional_args] = id.split("/");
- const remotes = manifest._.remotes;
- if (!remotes[hash2]) error(404);
- const module = await remotes[hash2]();
- const fn = module.default[name];
- if (!fn) error(404);
- const info = fn.__;
- const transport = options2.hooks.transport;
- event.tracing.current.setAttributes({
- "sveltekit.remote.call.type": info.type,
- "sveltekit.remote.call.name": info.name
- });
- let form_client_refreshes;
- try {
- if (info.type === "query_batch") {
- if (event.request.method !== "POST") {
- throw new SvelteKitError(
- 405,
- "Method Not Allowed",
- `\`query.batch\` functions must be invoked via POST request, not ${event.request.method}`
- );
- }
- const { payloads } = await event.request.json();
- const args = await Promise.all(
- payloads.map((payload2) => parse_remote_arg(payload2, transport))
- );
- const results = await with_request_store({ event, state }, () => info.run(args, options2));
- return json(
- /** @type {RemoteFunctionResponse} */
- {
- type: "result",
- result: stringify(results, transport)
- }
- );
- }
- if (info.type === "form") {
- if (event.request.method !== "POST") {
- throw new SvelteKitError(
- 405,
- "Method Not Allowed",
- `\`form\` functions must be invoked via POST request, not ${event.request.method}`
- );
- }
- if (!is_form_content_type(event.request)) {
- throw new SvelteKitError(
- 415,
- "Unsupported Media Type",
- `\`form\` functions expect form-encoded data — received ${event.request.headers.get(
- "content-type"
- )}`
- );
- }
- const { data: data2, meta, form_data } = await deserialize_binary_form(event.request);
- form_client_refreshes = meta.remote_refreshes;
- if (additional_args && !("id" in data2)) {
- data2.id = JSON.parse(decodeURIComponent(additional_args));
- }
- const fn2 = info.fn;
- const result = await with_request_store({ event, state }, () => fn2(data2, meta, form_data));
- return json(
- /** @type {RemoteFunctionResponse} */
- {
- type: "result",
- result: stringify(result, transport),
- refreshes: result.issues ? void 0 : await serialize_refreshes(meta.remote_refreshes)
- }
- );
- }
- if (info.type === "command") {
- const { payload: payload2, refreshes } = await event.request.json();
- const arg = parse_remote_arg(payload2, transport);
- const data2 = await with_request_store({ event, state }, () => fn(arg));
- return json(
- /** @type {RemoteFunctionResponse} */
- {
- type: "result",
- result: stringify(data2, transport),
- refreshes: await serialize_refreshes(refreshes)
- }
- );
- }
- const payload = info.type === "prerender" ? additional_args : (
- /** @type {string} */
- // new URL(...) necessary because we're hiding the URL from the user in the event object
- new URL(event.request.url).searchParams.get("payload")
- );
- const data = await with_request_store(
- { event, state },
- () => fn(parse_remote_arg(payload, transport))
- );
- return json(
- /** @type {RemoteFunctionResponse} */
- {
- type: "result",
- result: stringify(data, transport)
- }
- );
- } catch (error2) {
- if (error2 instanceof Redirect) {
- return json(
- /** @type {RemoteFunctionResponse} */
- {
- type: "redirect",
- location: error2.location,
- refreshes: await serialize_refreshes(form_client_refreshes)
- }
- );
- }
- const status = error2 instanceof HttpError || error2 instanceof SvelteKitError ? error2.status : 500;
- return json(
- /** @type {RemoteFunctionResponse} */
- {
- type: "error",
- error: await handle_error_and_jsonify(event, state, options2, error2),
- status
- },
- {
- // By setting a non-200 during prerendering we fail the prerender process (unless handleHttpError handles it).
- // Errors at runtime will be passed to the client and are handled there
- status: state.prerendering ? status : void 0,
- headers: {
- "cache-control": "private, no-store"
- }
- }
- );
- }
- async function serialize_refreshes(client_refreshes) {
- const refreshes = state.refreshes ?? {};
- if (client_refreshes) {
- for (const key2 of client_refreshes) {
- if (refreshes[key2] !== void 0) continue;
- const [hash3, name2, payload] = key2.split("/");
- const loader = manifest._.remotes[hash3];
- const fn2 = (await loader?.())?.default?.[name2];
- if (!fn2) error(400, "Bad Request");
- refreshes[key2] = with_request_store(
- { event, state },
- () => fn2(parse_remote_arg(payload, transport))
- );
- }
- }
- if (Object.keys(refreshes).length === 0) {
- return void 0;
- }
- return stringify(
- Object.fromEntries(
- await Promise.all(
- Object.entries(refreshes).map(async ([key2, promise]) => [key2, await promise])
- )
- ),
- transport
- );
- }
- }
- async function handle_remote_form_post(event, state, manifest, id) {
- return record_span({
- name: "sveltekit.remote.form.post",
- attributes: {},
- fn: (current2) => {
- const traced_event = merge_tracing(event, current2);
- return with_request_store(
- { event: traced_event, state },
- () => handle_remote_form_post_internal(traced_event, state, manifest, id)
- );
- }
- });
- }
- async function handle_remote_form_post_internal(event, state, manifest, id) {
- const [hash2, name, action_id] = id.split("/");
- const remotes = manifest._.remotes;
- const module = await remotes[hash2]?.();
- let form = (
- /** @type {RemoteForm<any, any>} */
- module?.default[name]
- );
- if (!form) {
- event.setHeaders({
- // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
- // "The server must generate an Allow header field in a 405 status code response"
- allow: "GET"
- });
- return {
- type: "error",
- error: new SvelteKitError(
- 405,
- "Method Not Allowed",
- `POST method not allowed. No form actions exist for ${"this page"}`
- )
- };
- }
- if (action_id) {
- form = with_request_store({ event, state }, () => form.for(JSON.parse(action_id)));
- }
- try {
- const fn = (
- /** @type {RemoteInfo & { type: 'form' }} */
- /** @type {any} */
- form.__.fn
- );
- const { data, meta, form_data } = await deserialize_binary_form(event.request);
- if (action_id && !("id" in data)) {
- data.id = JSON.parse(decodeURIComponent(action_id));
- }
- await with_request_store({ event, state }, () => fn(data, meta, form_data));
- return {
- type: "success",
- status: 200
- };
- } catch (e) {
- const err = normalize_error(e);
- if (err instanceof Redirect) {
- return {
- type: "redirect",
- status: err.status,
- location: err.location
- };
- }
- return {
- type: "error",
- error: check_incorrect_fail_use(err)
- };
- }
- }
- function get_remote_id(url) {
- return url.pathname.startsWith(`${base}/${app_dir}/remote/`) && url.pathname.replace(`${base}/${app_dir}/remote/`, "");
- }
- function get_remote_action(url) {
- return url.searchParams.get("/remote");
- }
- const MAX_DEPTH = 10;
- async function render_page(event, event_state, page, options2, manifest, state, nodes, resolve_opts) {
- if (state.depth > MAX_DEPTH) {
- return text(`Not found: ${event.url.pathname}`, {
- status: 404
- // TODO in some cases this should be 500. not sure how to differentiate
- });
- }
- if (is_action_json_request(event)) {
- const node = await manifest._.nodes[page.leaf]();
- return handle_action_json_request(event, event_state, options2, node?.server);
- }
- try {
- const leaf_node = (
- /** @type {import('types').SSRNode} */
- nodes.page()
- );
- let status = 200;
- let action_result = void 0;
- if (is_action_request(event)) {
- const remote_id = get_remote_action(event.url);
- if (remote_id) {
- action_result = await handle_remote_form_post(event, event_state, manifest, remote_id);
- } else {
- action_result = await handle_action_request(event, event_state, leaf_node.server);
- }
- if (action_result?.type === "redirect") {
- return redirect_response(action_result.status, action_result.location);
- }
- if (action_result?.type === "error") {
- status = get_status(action_result.error);
- }
- if (action_result?.type === "failure") {
- status = action_result.status;
- }
- }
- const should_prerender = nodes.prerender();
- if (should_prerender) {
- const mod = leaf_node.server;
- if (mod?.actions) {
- throw new Error("Cannot prerender pages with actions");
- }
- } else if (state.prerendering) {
- return new Response(void 0, {
- status: 204
- });
- }
- state.prerender_default = should_prerender;
- const should_prerender_data = nodes.should_prerender_data();
- const data_pathname = add_data_suffix(event.url.pathname);
- const fetched = [];
- const ssr = nodes.ssr();
- const csr = nodes.csr();
- if (ssr === false && !(state.prerendering && should_prerender_data)) {
- if (BROWSER && action_result && !event.request.headers.has("x-sveltekit-action")) ;
- return await render_response({
- branch: [],
- fetched,
- page_config: {
- ssr: false,
- csr
- },
- status,
- error: null,
- event,
- event_state,
- options: options2,
- manifest,
- state,
- resolve_opts,
- data_serializer: server_data_serializer(event, event_state, options2)
- });
- }
- const branch = [];
- let load_error = null;
- const data_serializer = server_data_serializer(event, event_state, options2);
- const data_serializer_json = state.prerendering && should_prerender_data ? server_data_serializer_json(event, event_state, options2) : null;
- const server_promises = nodes.data.map((node, i) => {
- if (load_error) {
- throw load_error;
- }
- return Promise.resolve().then(async () => {
- try {
- if (node === leaf_node && action_result?.type === "error") {
- throw action_result.error;
- }
- const server_data = await load_server_data({
- event,
- event_state,
- state,
- node,
- parent: async () => {
- const data = {};
- for (let j = 0; j < i; j += 1) {
- const parent = await server_promises[j];
- if (parent) Object.assign(data, parent.data);
- }
- return data;
- }
- });
- if (node) {
- data_serializer.add_node(i, server_data);
- }
- data_serializer_json?.add_node(i, server_data);
- return server_data;
- } catch (e) {
- load_error = /** @type {Error} */
- e;
- throw load_error;
- }
- });
- });
- const load_promises = nodes.data.map((node, i) => {
- if (load_error) throw load_error;
- return Promise.resolve().then(async () => {
- try {
- return await load_data({
- event,
- event_state,
- fetched,
- node,
- parent: async () => {
- const data = {};
- for (let j = 0; j < i; j += 1) {
- Object.assign(data, await load_promises[j]);
- }
- return data;
- },
- resolve_opts,
- server_data_promise: server_promises[i],
- state,
- csr
- });
- } catch (e) {
- load_error = /** @type {Error} */
- e;
- throw load_error;
- }
- });
- });
- for (const p of server_promises) p.catch(() => {
- });
- for (const p of load_promises) p.catch(() => {
- });
- for (let i = 0; i < nodes.data.length; i += 1) {
- const node = nodes.data[i];
- if (node) {
- try {
- const server_data = await server_promises[i];
- const data = await load_promises[i];
- branch.push({ node, server_data, data });
- } catch (e) {
- const err = normalize_error(e);
- if (err instanceof Redirect) {
- if (state.prerendering && should_prerender_data) {
- const body2 = JSON.stringify({
- type: "redirect",
- location: err.location
- });
- state.prerendering.dependencies.set(data_pathname, {
- response: text(body2),
- body: body2
- });
- }
- return redirect_response(err.status, err.location);
- }
- const status2 = get_status(err);
- const error2 = await handle_error_and_jsonify(event, event_state, options2, err);
- while (i--) {
- if (page.errors[i]) {
- const index = (
- /** @type {number} */
- page.errors[i]
- );
- const node2 = await manifest._.nodes[index]();
- let j = i;
- while (!branch[j]) j -= 1;
- data_serializer.set_max_nodes(j + 1);
- const layouts = compact(branch.slice(0, j + 1));
- const nodes2 = new PageNodes(layouts.map((layout) => layout.node));
- const error_branch = layouts.concat({
- node: node2,
- data: null,
- server_data: null
- });
- return await render_response({
- event,
- event_state,
- options: options2,
- manifest,
- state,
- resolve_opts,
- page_config: {
- ssr: nodes2.ssr(),
- csr: nodes2.csr()
- },
- status: status2,
- error: error2,
- error_components: await load_error_components(
- options2,
- ssr,
- error_branch,
- page,
- manifest
- ),
- branch: error_branch,
- fetched,
- data_serializer
- });
- }
- }
- return static_error_page(options2, status2, error2.message);
- }
- } else {
- branch.push(null);
- }
- }
- if (state.prerendering && data_serializer_json) {
- let { data, chunks } = data_serializer_json.get_data();
- if (chunks) {
- for await (const chunk of chunks) {
- data += chunk;
- }
- }
- state.prerendering.dependencies.set(data_pathname, {
- response: text(data),
- body: data
- });
- }
- return await render_response({
- event,
- event_state,
- options: options2,
- manifest,
- state,
- resolve_opts,
- page_config: {
- csr,
- ssr
- },
- status,
- error: null,
- branch: !ssr ? [] : compact(branch),
- action_result,
- fetched,
- data_serializer: !ssr ? server_data_serializer(event, event_state, options2) : data_serializer,
- error_components: await load_error_components(options2, ssr, branch, page, manifest)
- });
- } catch (e) {
- if (e instanceof Redirect) {
- return redirect_response(e.status, e.location);
- }
- return await respond_with_error({
- event,
- event_state,
- options: options2,
- manifest,
- state,
- status: e instanceof HttpError ? e.status : 500,
- error: e,
- resolve_opts
- });
- }
- }
- async function load_error_components(options2, ssr, branch, page, manifest) {
- let error_components;
- if (options2.server_error_boundaries && ssr) {
- let last_idx = -1;
- error_components = await Promise.all(
- // eslint-disable-next-line @typescript-eslint/await-thenable
- branch.map((b, i) => {
- if (i === 0) return void 0;
- if (!b) return null;
- i--;
- while (i > last_idx + 1 && page.errors[i] === void 0) i -= 1;
- last_idx = i;
- const idx = page.errors[i];
- if (idx == null) return void 0;
- return manifest._.nodes[idx]?.().then((e) => e.component?.()).catch(() => void 0);
- }).filter((e) => e !== null)
- );
- }
- return error_components;
- }
- function once(fn) {
- let done = false;
- let result;
- return () => {
- if (done) return result;
- done = true;
- return result = fn();
- };
- }
- async function render_data(event, event_state, route, options2, manifest, state, invalidated_data_nodes, trailing_slash) {
- if (!route.page) {
- return new Response(void 0, {
- status: 404
- });
- }
- try {
- const node_ids = [...route.page.layouts, route.page.leaf];
- const invalidated = invalidated_data_nodes ?? node_ids.map(() => true);
- let aborted = false;
- const url = new URL(event.url);
- url.pathname = normalize_path(url.pathname, trailing_slash);
- const new_event = { ...event, url };
- const functions = node_ids.map((n, i) => {
- return once(async () => {
- try {
- if (aborted) {
- return (
- /** @type {import('types').ServerDataSkippedNode} */
- {
- type: "skip"
- }
- );
- }
- const node = n == void 0 ? n : await manifest._.nodes[n]();
- return load_server_data({
- event: new_event,
- event_state,
- state,
- node,
- parent: async () => {
- const data2 = {};
- for (let j = 0; j < i; j += 1) {
- const parent = (
- /** @type {import('types').ServerDataNode | null} */
- await functions[j]()
- );
- if (parent) {
- Object.assign(data2, parent.data);
- }
- }
- return data2;
- }
- });
- } catch (e) {
- aborted = true;
- throw e;
- }
- });
- });
- const promises = functions.map(async (fn, i) => {
- if (!invalidated[i]) {
- return (
- /** @type {import('types').ServerDataSkippedNode} */
- {
- type: "skip"
- }
- );
- }
- return fn();
- });
- let length = promises.length;
- const nodes = await Promise.all(
- promises.map(
- (p, i) => p.catch(async (error2) => {
- if (error2 instanceof Redirect) {
- throw error2;
- }
- length = Math.min(length, i + 1);
- return (
- /** @type {import('types').ServerErrorNode} */
- {
- type: "error",
- error: await handle_error_and_jsonify(event, event_state, options2, error2),
- status: error2 instanceof HttpError || error2 instanceof SvelteKitError ? error2.status : void 0
- }
- );
- })
- )
- );
- const data_serializer = server_data_serializer_json(event, event_state, options2);
- for (let i = 0; i < nodes.length; i++) data_serializer.add_node(i, nodes[i]);
- const { data, chunks } = data_serializer.get_data();
- if (!chunks) {
- return json_response(data);
- }
- return new Response(
- new ReadableStream({
- async start(controller) {
- controller.enqueue(text_encoder.encode(data));
- for await (const chunk of chunks) {
- controller.enqueue(text_encoder.encode(chunk));
- }
- controller.close();
- },
- type: "bytes"
- }),
- {
- headers: {
- // we use a proprietary content type to prevent buffering.
- // the `text` prefix makes it inspectable
- "content-type": "text/sveltekit-data",
- "cache-control": "private, no-store"
- }
- }
- );
- } catch (e) {
- const error2 = normalize_error(e);
- if (error2 instanceof Redirect) {
- return redirect_json_response(error2);
- } else {
- return json_response(await handle_error_and_jsonify(event, event_state, options2, error2), 500);
- }
- }
- }
- function json_response(json2, status = 200) {
- return text(typeof json2 === "string" ? json2 : JSON.stringify(json2), {
- status,
- headers: {
- "content-type": "application/json",
- "cache-control": "private, no-store"
- }
- });
- }
- function redirect_json_response(redirect) {
- return json_response(
- /** @type {import('types').ServerRedirectNode} */
- {
- type: "redirect",
- location: redirect.location
- }
- );
- }
- const INVALID_COOKIE_CHARACTER_REGEX = /[\x00-\x1F\x7F()<>@,;:"/[\]?={} \t]/;
- function validate_options(options2) {
- if (options2?.path === void 0) {
- throw new Error("You must specify a `path` when setting, deleting or serializing cookies");
- }
- }
- function generate_cookie_key(domain, path, name) {
- return `${domain || ""}${path}?${encodeURIComponent(name)}`;
- }
- function get_cookies(request, url) {
- const header = request.headers.get("cookie") ?? "";
- const initial_cookies = parse(header, { decode: (value) => value });
- let normalized_url;
- const new_cookies = /* @__PURE__ */ new Map();
- const defaults = {
- httpOnly: true,
- sameSite: "lax",
- secure: url.hostname === "localhost" && url.protocol === "http:" ? false : true
- };
- const cookies = {
- // The JSDoc param annotations appearing below for get, set and delete
- // are necessary to expose the `cookie` library types to
- // typescript users. `@type {import('@sveltejs/kit').Cookies}` above is not
- // sufficient to do so.
- /**
- * @param {string} name
- * @param {import('cookie').CookieParseOptions} [opts]
- */
- get(name, opts) {
- const best_match = Array.from(new_cookies.values()).filter((c) => {
- return c.name === name && domain_matches(url.hostname, c.options.domain) && path_matches(url.pathname, c.options.path);
- }).sort((a, b) => b.options.path.length - a.options.path.length)[0];
- if (best_match) {
- return best_match.options.maxAge === 0 ? void 0 : best_match.value;
- }
- const req_cookies = parse(header, { decode: opts?.decode });
- const cookie = req_cookies[name];
- return cookie;
- },
- /**
- * @param {import('cookie').CookieParseOptions} [opts]
- */
- getAll(opts) {
- const cookies2 = parse(header, { decode: opts?.decode });
- const lookup = /* @__PURE__ */ new Map();
- for (const c of new_cookies.values()) {
- if (domain_matches(url.hostname, c.options.domain) && path_matches(url.pathname, c.options.path)) {
- const existing = lookup.get(c.name);
- if (!existing || c.options.path.length > existing.options.path.length) {
- lookup.set(c.name, c);
- }
- }
- }
- for (const c of lookup.values()) {
- cookies2[c.name] = c.value;
- }
- return Object.entries(cookies2).map(([name, value]) => ({ name, value }));
- },
- /**
- * @param {string} name
- * @param {string} value
- * @param {import('./page/types.js').Cookie['options']} options
- */
- set(name, value, options2) {
- const illegal_characters = name.match(INVALID_COOKIE_CHARACTER_REGEX);
- if (illegal_characters) {
- console.warn(
- `The cookie name "${name}" will be invalid in SvelteKit 3.0 as it contains ${illegal_characters.join(
- " and "
- )}. See RFC 2616 for more details https://datatracker.ietf.org/doc/html/rfc2616#section-2.2`
- );
- }
- validate_options(options2);
- set_internal(name, value, { ...defaults, ...options2 });
- },
- /**
- * @param {string} name
- * @param {import('./page/types.js').Cookie['options']} options
- */
- delete(name, options2) {
- validate_options(options2);
- cookies.set(name, "", { ...options2, maxAge: 0 });
- },
- /**
- * @param {string} name
- * @param {string} value
- * @param {import('./page/types.js').Cookie['options']} options
- */
- serialize(name, value, options2) {
- validate_options(options2);
- let path = options2.path;
- if (!options2.domain || options2.domain === url.hostname) {
- if (!normalized_url) {
- throw new Error("Cannot serialize cookies until after the route is determined");
- }
- path = resolve(normalized_url, path);
- }
- return serialize(name, value, { ...defaults, ...options2, path });
- }
- };
- function get_cookie_header(destination, header2) {
- const combined_cookies = {
- // cookies sent by the user agent have lowest precedence
- ...initial_cookies
- };
- for (const cookie of new_cookies.values()) {
- if (!domain_matches(destination.hostname, cookie.options.domain)) continue;
- if (!path_matches(destination.pathname, cookie.options.path)) continue;
- const encoder = cookie.options.encode || encodeURIComponent;
- combined_cookies[cookie.name] = encoder(cookie.value);
- }
- if (header2) {
- const parsed = parse(header2, { decode: (value) => value });
- for (const name in parsed) {
- combined_cookies[name] = parsed[name];
- }
- }
- return Object.entries(combined_cookies).map(([name, value]) => `${name}=${value}`).join("; ");
- }
- const internal_queue = [];
- function set_internal(name, value, options2) {
- if (!normalized_url) {
- internal_queue.push(() => set_internal(name, value, options2));
- return;
- }
- let path = options2.path;
- if (!options2.domain || options2.domain === url.hostname) {
- path = resolve(normalized_url, path);
- }
- const cookie_key = generate_cookie_key(options2.domain, path, name);
- const cookie = { name, value, options: { ...options2, path } };
- new_cookies.set(cookie_key, cookie);
- }
- function set_trailing_slash(trailing_slash) {
- normalized_url = normalize_path(url.pathname, trailing_slash);
- internal_queue.forEach((fn) => fn());
- }
- return { cookies, new_cookies, get_cookie_header, set_internal, set_trailing_slash };
- }
- function domain_matches(hostname, constraint) {
- if (!constraint) return true;
- const normalized = constraint[0] === "." ? constraint.slice(1) : constraint;
- if (hostname === normalized) return true;
- return hostname.endsWith("." + normalized);
- }
- function path_matches(path, constraint) {
- if (!constraint) return true;
- const normalized = constraint.endsWith("/") ? constraint.slice(0, -1) : constraint;
- if (path === normalized) return true;
- return path.startsWith(normalized + "/");
- }
- function add_cookies_to_headers(headers2, cookies) {
- for (const new_cookie of cookies) {
- const { name, value, options: options2 } = new_cookie;
- headers2.append("set-cookie", serialize(name, value, options2));
- if (options2.path.endsWith(".html")) {
- const path = add_data_suffix(options2.path);
- headers2.append("set-cookie", serialize(name, value, { ...options2, path }));
- }
- }
- }
- function create_fetch({ event, options: options2, manifest, state, get_cookie_header, set_internal }) {
- const server_fetch = async (info, init2) => {
- const original_request = normalize_fetch_input(info, init2, event.url);
- let mode = (info instanceof Request ? info.mode : init2?.mode) ?? "cors";
- let credentials = (info instanceof Request ? info.credentials : init2?.credentials) ?? "same-origin";
- return options2.hooks.handleFetch({
- event,
- request: original_request,
- fetch: async (info2, init3) => {
- const request = normalize_fetch_input(info2, init3, event.url);
- const url = new URL(request.url);
- if (!request.headers.has("origin")) {
- request.headers.set("origin", event.url.origin);
- }
- if (info2 !== original_request) {
- mode = (info2 instanceof Request ? info2.mode : init3?.mode) ?? "cors";
- credentials = (info2 instanceof Request ? info2.credentials : init3?.credentials) ?? "same-origin";
- }
- if ((request.method === "GET" || request.method === "HEAD") && (mode === "no-cors" && url.origin !== event.url.origin || url.origin === event.url.origin)) {
- request.headers.delete("origin");
- }
- const decoded = decodeURIComponent(url.pathname);
- if (url.origin !== event.url.origin || base && decoded !== base && !decoded.startsWith(`${base}/`)) {
- if (`.${url.hostname}`.endsWith(`.${event.url.hostname}`) && credentials !== "omit") {
- const cookie = get_cookie_header(url, request.headers.get("cookie"));
- if (cookie) request.headers.set("cookie", cookie);
- }
- return fetch(request);
- }
- const prefix = assets || base;
- const filename = (decoded.startsWith(prefix) ? decoded.slice(prefix.length) : decoded).slice(1);
- const filename_html = `${filename}/index.html`;
- const is_asset = manifest.assets.has(filename) || filename in manifest._.server_assets;
- const is_asset_html = manifest.assets.has(filename_html) || filename_html in manifest._.server_assets;
- if (is_asset || is_asset_html) {
- const file = is_asset ? filename : filename_html;
- if (state.read) {
- const type = is_asset ? manifest.mimeTypes[filename.slice(filename.lastIndexOf("."))] : "text/html";
- return new Response(state.read(file), {
- headers: type ? { "content-type": type } : {}
- });
- } else if (read_implementation && file in manifest._.server_assets) {
- const length = manifest._.server_assets[file];
- const type = manifest.mimeTypes[file.slice(file.lastIndexOf("."))];
- return new Response(read_implementation(file), {
- headers: {
- "Content-Length": "" + length,
- "Content-Type": type
- }
- });
- }
- return await fetch(request);
- }
- if (has_prerendered_path(manifest, base + decoded)) {
- return await fetch(request);
- }
- if (credentials !== "omit") {
- const cookie = get_cookie_header(url, request.headers.get("cookie"));
- if (cookie) {
- request.headers.set("cookie", cookie);
- }
- const authorization = event.request.headers.get("authorization");
- if (authorization && !request.headers.has("authorization")) {
- request.headers.set("authorization", authorization);
- }
- }
- if (!request.headers.has("accept")) {
- request.headers.set("accept", "*/*");
- }
- if (!request.headers.has("accept-language")) {
- request.headers.set(
- "accept-language",
- /** @type {string} */
- event.request.headers.get("accept-language")
- );
- }
- const response = await internal_fetch(request, options2, manifest, state);
- const set_cookie = response.headers.get("set-cookie");
- if (set_cookie) {
- for (const str of set_cookie_parser.splitCookiesString(set_cookie)) {
- const { name, value, ...options3 } = set_cookie_parser.parseString(str, {
- decodeValues: false
- });
- const path = options3.path ?? (url.pathname.split("/").slice(0, -1).join("/") || "/");
- set_internal(name, value, {
- path,
- encode: (value2) => value2,
- .../** @type {import('cookie').CookieSerializeOptions} */
- options3
- });
- }
- }
- return response;
- }
- });
- };
- return (input, init2) => {
- const response = server_fetch(input, init2);
- response.catch(() => {
- });
- return response;
- };
- }
- function normalize_fetch_input(info, init2, url) {
- if (info instanceof Request) {
- return info;
- }
- return new Request(typeof info === "string" ? new URL(info, url) : info, init2);
- }
- async function internal_fetch(request, options2, manifest, state) {
- if (request.signal) {
- if (request.signal.aborted) {
- throw new DOMException("The operation was aborted.", "AbortError");
- }
- let remove_abort_listener = () => {
- };
- const abort_promise = new Promise((_, reject) => {
- const on_abort = () => {
- reject(new DOMException("The operation was aborted.", "AbortError"));
- };
- request.signal.addEventListener("abort", on_abort, { once: true });
- remove_abort_listener = () => request.signal.removeEventListener("abort", on_abort);
- });
- const result = await Promise.race([
- respond(request, options2, manifest, {
- ...state,
- depth: state.depth + 1
- }),
- abort_promise
- ]);
- remove_abort_listener();
- return result;
- } else {
- return await respond(request, options2, manifest, {
- ...state,
- depth: state.depth + 1
- });
- }
- }
- let body;
- let etag;
- let headers;
- function get_public_env(request) {
- body ??= `export const env=${JSON.stringify(public_env)}`;
- etag ??= `W/${Date.now()}`;
- headers ??= new Headers({
- "content-type": "application/javascript; charset=utf-8",
- etag
- });
- if (request.headers.get("if-none-match") === etag) {
- return new Response(void 0, { status: 304, headers });
- }
- return new Response(body, { headers });
- }
- const default_transform = ({ html }) => html;
- const default_filter = () => false;
- const default_preload = ({ type }) => type === "js" || type === "css";
- const page_methods = /* @__PURE__ */ new Set(["GET", "HEAD", "POST"]);
- const allowed_page_methods = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
- let warned_on_devtools_json_request = false;
- const respond = propagate_context(internal_respond);
- async function internal_respond(request, options2, manifest, state) {
- const url = new URL(request.url);
- const is_route_resolution_request = has_resolution_suffix(url.pathname);
- const is_data_request = has_data_suffix(url.pathname);
- const remote_id = get_remote_id(url);
- {
- const request_origin = request.headers.get("origin");
- if (remote_id) {
- if (request.method !== "GET" && request_origin !== url.origin) {
- const message = "Cross-site remote requests are forbidden";
- return json({ message }, { status: 403 });
- }
- } else if (options2.csrf_check_origin) {
- const forbidden = is_form_content_type(request) && (request.method === "POST" || request.method === "PUT" || request.method === "PATCH" || request.method === "DELETE") && request_origin !== url.origin && (!request_origin || !options2.csrf_trusted_origins.includes(request_origin));
- if (forbidden) {
- const message = `Cross-site ${request.method} form submissions are forbidden`;
- const opts = { status: 403 };
- if (request.headers.get("accept") === "application/json") {
- return json({ message }, opts);
- }
- return text(message, opts);
- }
- }
- }
- if (options2.hash_routing && url.pathname !== base + "/" && url.pathname !== "/[fallback]") {
- return text("Not found", { status: 404 });
- }
- let invalidated_data_nodes;
- if (is_route_resolution_request) {
- url.pathname = strip_resolution_suffix(url.pathname);
- } else if (is_data_request) {
- url.pathname = strip_data_suffix(url.pathname) + (url.searchParams.get(TRAILING_SLASH_PARAM) === "1" ? "/" : "") || "/";
- url.searchParams.delete(TRAILING_SLASH_PARAM);
- invalidated_data_nodes = url.searchParams.get(INVALIDATED_PARAM)?.split("").map((node) => node === "1");
- url.searchParams.delete(INVALIDATED_PARAM);
- } else if (remote_id) {
- url.pathname = request.headers.get("x-sveltekit-pathname") ?? base;
- url.search = request.headers.get("x-sveltekit-search") ?? "";
- }
- const headers2 = {};
- const { cookies, new_cookies, get_cookie_header, set_internal, set_trailing_slash } = get_cookies(
- request,
- url
- );
- const event_state = {
- prerendering: state.prerendering,
- transport: options2.hooks.transport,
- handleValidationError: options2.hooks.handleValidationError,
- tracing: {
- record_span
- },
- is_in_remote_function: false
- };
- const event = {
- cookies,
- // @ts-expect-error `fetch` needs to be created after the `event` itself
- fetch: null,
- getClientAddress: state.getClientAddress || (() => {
- throw new Error(
- `${"@sveltejs/adapter-static"} does not specify getClientAddress. Please raise an issue`
- );
- }),
- locals: {},
- params: {},
- platform: state.platform,
- request,
- route: { id: null },
- setHeaders: (new_headers) => {
- for (const key2 in new_headers) {
- const lower = key2.toLowerCase();
- const value = new_headers[key2];
- if (lower === "set-cookie") {
- throw new Error(
- "Use `event.cookies.set(name, value, options)` instead of `event.setHeaders` to set cookies"
- );
- } else if (lower in headers2) {
- if (lower === "server-timing") {
- headers2[lower] += ", " + value;
- } else {
- throw new Error(`"${key2}" header is already set`);
- }
- } else {
- headers2[lower] = value;
- if (state.prerendering && lower === "cache-control") {
- state.prerendering.cache = /** @type {string} */
- value;
- }
- }
- }
- },
- url,
- isDataRequest: is_data_request,
- isSubRequest: state.depth > 0,
- isRemoteRequest: !!remote_id
- };
- event.fetch = create_fetch({
- event,
- options: options2,
- manifest,
- state,
- get_cookie_header,
- set_internal
- });
- if (state.emulator?.platform) {
- event.platform = await state.emulator.platform({
- config: {},
- prerender: !!state.prerendering?.fallback
- });
- }
- let resolved_path = url.pathname;
- if (!remote_id) {
- const prerendering_reroute_state = state.prerendering?.inside_reroute;
- try {
- if (state.prerendering) state.prerendering.inside_reroute = true;
- resolved_path = await options2.hooks.reroute({ url: new URL(url), fetch: event.fetch }) ?? url.pathname;
- } catch {
- return text("Internal Server Error", {
- status: 500
- });
- } finally {
- if (state.prerendering) state.prerendering.inside_reroute = prerendering_reroute_state;
- }
- }
- try {
- resolved_path = decode_pathname(resolved_path);
- } catch {
- return text("Malformed URI", { status: 400 });
- }
- if (
- // the resolved path has been decoded so it should be compared to the decoded url pathname
- resolved_path !== decode_pathname(url.pathname) && !state.prerendering?.fallback && has_prerendered_path(manifest, resolved_path)
- ) {
- const url2 = new URL(request.url);
- url2.pathname = is_data_request ? add_data_suffix(resolved_path) : is_route_resolution_request ? add_resolution_suffix(resolved_path) : resolved_path;
- try {
- const response = await fetch(url2, request);
- const headers22 = new Headers(response.headers);
- if (headers22.has("content-encoding")) {
- headers22.delete("content-encoding");
- headers22.delete("content-length");
- }
- return new Response(response.body, {
- headers: headers22,
- status: response.status,
- statusText: response.statusText
- });
- } catch (error2) {
- return await handle_fatal_error(event, event_state, options2, error2);
- }
- }
- let route = null;
- if (base && !state.prerendering?.fallback) {
- if (!resolved_path.startsWith(base)) {
- return text("Not found", { status: 404 });
- }
- resolved_path = resolved_path.slice(base.length) || "/";
- }
- if (is_route_resolution_request) {
- return resolve_route(resolved_path, new URL(request.url), manifest);
- }
- if (resolved_path === `/${app_dir}/env.js`) {
- return get_public_env(request);
- }
- if (!remote_id && resolved_path.startsWith(`/${app_dir}`)) {
- const headers22 = new Headers();
- headers22.set("cache-control", "public, max-age=0, must-revalidate");
- return text("Not found", { status: 404, headers: headers22 });
- }
- if (!state.prerendering?.fallback) {
- const matchers = await manifest._.matchers();
- const result = find_route(resolved_path, manifest._.routes, matchers);
- if (result) {
- route = result.route;
- event.route = { id: route.id };
- event.params = result.params;
- }
- }
- let resolve_opts = {
- transformPageChunk: default_transform,
- filterSerializedResponseHeaders: default_filter,
- preload: default_preload
- };
- let trailing_slash = "never";
- try {
- const page_nodes = route?.page ? new PageNodes(await load_page_nodes(route.page, manifest)) : void 0;
- if (route && !remote_id) {
- if (url.pathname === base || url.pathname === base + "/") {
- trailing_slash = "always";
- } else if (page_nodes) {
- if (BROWSER) ;
- trailing_slash = page_nodes.trailing_slash();
- } else if (route.endpoint) {
- const node = await route.endpoint();
- trailing_slash = node.trailingSlash ?? "never";
- if (BROWSER) ;
- }
- if (!is_data_request) {
- const normalized = normalize_path(url.pathname, trailing_slash);
- if (normalized !== url.pathname && !state.prerendering?.fallback) {
- return new Response(void 0, {
- status: 308,
- headers: {
- "x-sveltekit-normalize": "1",
- location: (
- // ensure paths starting with '//' are not treated as protocol-relative
- (normalized.startsWith("//") ? url.origin + normalized : normalized) + (url.search === "?" ? "" : url.search)
- )
- }
- });
- }
- }
- if (state.before_handle || state.emulator?.platform) {
- let config = {};
- let prerender = false;
- if (route.endpoint) {
- const node = await route.endpoint();
- config = node.config ?? config;
- prerender = node.prerender ?? prerender;
- } else if (page_nodes) {
- config = page_nodes.get_config() ?? config;
- prerender = page_nodes.prerender();
- }
- if (state.before_handle) {
- state.before_handle(event, config, prerender);
- }
- if (state.emulator?.platform) {
- event.platform = await state.emulator.platform({ config, prerender });
- }
- }
- }
- set_trailing_slash(trailing_slash);
- if (state.prerendering && !state.prerendering.fallback && !state.prerendering.inside_reroute) {
- disable_search(url);
- }
- const response = await record_span({
- name: "sveltekit.handle.root",
- attributes: {
- "http.route": event.route.id || "unknown",
- "http.method": event.request.method,
- "http.url": event.url.href,
- "sveltekit.is_data_request": is_data_request,
- "sveltekit.is_sub_request": event.isSubRequest
- },
- fn: async (root_span) => {
- const traced_event = {
- ...event,
- tracing: {
- enabled: false,
- root: root_span,
- current: root_span
- }
- };
- event_state.allows_commands = MUTATIVE_METHODS.includes(request.method);
- return await with_request_store(
- { event: traced_event, state: event_state },
- () => options2.hooks.handle({
- event: traced_event,
- resolve: (event2, opts) => {
- return record_span({
- name: "sveltekit.resolve",
- attributes: {
- "http.route": event2.route.id || "unknown"
- },
- fn: (resolve_span) => {
- return with_request_store(
- null,
- () => resolve2(merge_tracing(event2, resolve_span), page_nodes, opts).then(
- (response2) => {
- for (const key2 in headers2) {
- const value = headers2[key2];
- response2.headers.set(
- key2,
- /** @type {string} */
- value
- );
- }
- add_cookies_to_headers(response2.headers, new_cookies.values());
- if (state.prerendering && event2.route.id !== null) {
- response2.headers.set("x-sveltekit-routeid", encodeURI(event2.route.id));
- }
- resolve_span.setAttributes({
- "http.response.status_code": response2.status,
- "http.response.body.size": response2.headers.get("content-length") || "unknown"
- });
- return response2;
- }
- )
- );
- }
- });
- }
- })
- );
- }
- });
- if (response.status === 200 && response.headers.has("etag")) {
- let if_none_match_value = request.headers.get("if-none-match");
- if (if_none_match_value?.startsWith('W/"')) {
- if_none_match_value = if_none_match_value.substring(2);
- }
- const etag2 = (
- /** @type {string} */
- response.headers.get("etag")
- );
- if (if_none_match_value === etag2) {
- const headers22 = new Headers({ etag: etag2 });
- for (const key2 of [
- "cache-control",
- "content-location",
- "date",
- "expires",
- "vary",
- "set-cookie"
- ]) {
- const value = response.headers.get(key2);
- if (value) headers22.set(key2, value);
- }
- return new Response(void 0, {
- status: 304,
- headers: headers22
- });
- }
- }
- if (is_data_request && response.status >= 300 && response.status <= 308) {
- const location = response.headers.get("location");
- if (location) {
- return redirect_json_response(new Redirect(
- /** @type {any} */
- response.status,
- location
- ));
- }
- }
- return response;
- } catch (e) {
- if (e instanceof Redirect) {
- const response = is_data_request || remote_id ? redirect_json_response(e) : route?.page && is_action_json_request(event) ? action_json_redirect(e) : redirect_response(e.status, e.location);
- add_cookies_to_headers(response.headers, new_cookies.values());
- return response;
- }
- return await handle_fatal_error(event, event_state, options2, e);
- }
- async function resolve2(event2, page_nodes, opts) {
- try {
- if (opts) {
- resolve_opts = {
- transformPageChunk: opts.transformPageChunk || default_transform,
- filterSerializedResponseHeaders: opts.filterSerializedResponseHeaders || default_filter,
- preload: opts.preload || default_preload
- };
- }
- if (options2.hash_routing || state.prerendering?.fallback) {
- return await render_response({
- event: event2,
- event_state,
- options: options2,
- manifest,
- state,
- page_config: { ssr: false, csr: true },
- status: 200,
- error: null,
- branch: [],
- fetched: [],
- resolve_opts,
- data_serializer: server_data_serializer(event2, event_state, options2)
- });
- }
- if (remote_id) {
- return await handle_remote_call(event2, event_state, options2, manifest, remote_id);
- }
- if (route) {
- const method = (
- /** @type {import('types').HttpMethod} */
- event2.request.method
- );
- let response2;
- if (is_data_request) {
- response2 = await render_data(
- event2,
- event_state,
- route,
- options2,
- manifest,
- state,
- invalidated_data_nodes,
- trailing_slash
- );
- } else if (route.endpoint && (!route.page || is_endpoint_request(event2))) {
- response2 = await render_endpoint(event2, event_state, await route.endpoint(), state);
- } else if (route.page) {
- if (!page_nodes) {
- throw new Error("page_nodes not found. This should never happen");
- } else if (page_methods.has(method)) {
- response2 = await render_page(
- event2,
- event_state,
- route.page,
- options2,
- manifest,
- state,
- page_nodes,
- resolve_opts
- );
- } else {
- const allowed_methods = new Set(allowed_page_methods);
- const node = await manifest._.nodes[route.page.leaf]();
- if (node?.server?.actions) {
- allowed_methods.add("POST");
- }
- if (method === "OPTIONS") {
- response2 = new Response(null, {
- status: 204,
- headers: {
- allow: Array.from(allowed_methods.values()).join(", ")
- }
- });
- } else {
- const mod = [...allowed_methods].reduce(
- (acc, curr) => {
- acc[curr] = true;
- return acc;
- },
- /** @type {Record<string, any>} */
- {}
- );
- response2 = method_not_allowed(mod, method);
- }
- }
- } else {
- throw new Error("Route is neither page nor endpoint. This should never happen");
- }
- if (request.method === "GET" && route.page && route.endpoint) {
- const vary = response2.headers.get("vary")?.split(",")?.map((v) => v.trim().toLowerCase());
- if (!(vary?.includes("accept") || vary?.includes("*"))) {
- response2 = new Response(response2.body, {
- status: response2.status,
- statusText: response2.statusText,
- headers: new Headers(response2.headers)
- });
- response2.headers.append("Vary", "Accept");
- }
- }
- return response2;
- }
- if (state.error && event2.isSubRequest) {
- const headers22 = new Headers(request.headers);
- headers22.set("x-sveltekit-error", "true");
- return await fetch(request, { headers: headers22 });
- }
- if (state.error) {
- return text("Internal Server Error", {
- status: 500
- });
- }
- if (state.depth === 0) {
- if (BROWSER && event2.url.pathname === "/.well-known/appspecific/com.chrome.devtools.json") ;
- return await respond_with_error({
- event: event2,
- event_state,
- options: options2,
- manifest,
- state,
- status: 404,
- error: new SvelteKitError(404, "Not Found", `Not found: ${event2.url.pathname}`),
- resolve_opts
- });
- }
- if (state.prerendering) {
- return text("not found", { status: 404 });
- }
- const response = await fetch(request);
- return new Response(response.body, response);
- } catch (e) {
- return await handle_fatal_error(event2, event_state, options2, e);
- } finally {
- event2.cookies.set = () => {
- throw new Error("Cannot use `cookies.set(...)` after the response has been generated");
- };
- event2.setHeaders = () => {
- throw new Error("Cannot use `setHeaders(...)` after the response has been generated");
- };
- }
- }
- }
- function load_page_nodes(page, manifest) {
- return Promise.all([
- // we use == here rather than === because [undefined] serializes as "[null]"
- ...page.layouts.map((n) => n == void 0 ? n : manifest._.nodes[n]()),
- manifest._.nodes[page.leaf]()
- ]);
- }
- function propagate_context(fn) {
- return async (req, ...rest) => {
- {
- return fn(req, ...rest);
- }
- };
- }
- function filter_env(env, allowed, disallowed) {
- return Object.fromEntries(
- Object.entries(env).filter(
- ([k]) => k.startsWith(allowed) && (disallowed === "" || !k.startsWith(disallowed))
- )
- );
- }
- function set_app(value) {
- }
- let init_promise;
- let current = null;
- class Server {
- /** @type {import('types').SSROptions} */
- #options;
- /** @type {import('@sveltejs/kit').SSRManifest} */
- #manifest;
- /** @param {import('@sveltejs/kit').SSRManifest} manifest */
- constructor(manifest) {
- this.#options = options;
- this.#manifest = manifest;
- if (IN_WEBCONTAINER) {
- const respond2 = this.respond.bind(this);
- this.respond = async (...args) => {
- const { promise, resolve: resolve2 } = (
- /** @type {PromiseWithResolvers<void>} */
- with_resolvers()
- );
- const previous = current;
- current = promise;
- await previous;
- return respond2(...args).finally(resolve2);
- };
- }
- }
- /**
- * @param {import('@sveltejs/kit').ServerInitOptions} opts
- */
- async init({ env, read }) {
- const { env_public_prefix, env_private_prefix } = this.#options;
- set_private_env(filter_env(env, env_private_prefix, env_public_prefix));
- set_public_env(filter_env(env, env_public_prefix, env_private_prefix));
- if (read) {
- const wrapped_read = (file) => {
- const result = read(file);
- if (result instanceof ReadableStream) {
- return result;
- } else {
- return new ReadableStream({
- async start(controller) {
- try {
- const stream = await Promise.resolve(result);
- if (!stream) {
- controller.close();
- return;
- }
- const reader = stream.getReader();
- while (true) {
- const { done, value } = await reader.read();
- if (done) break;
- controller.enqueue(value);
- }
- controller.close();
- } catch (error2) {
- controller.error(error2);
- }
- }
- });
- }
- };
- set_read_implementation(wrapped_read);
- }
- await (init_promise ??= (async () => {
- try {
- const module = await get_hooks();
- this.#options.hooks = {
- handle: module.handle || (({ event, resolve: resolve2 }) => resolve2(event)),
- handleError: module.handleError || (({ status, error: error2, event }) => {
- const error_message = format_server_error(
- status,
- /** @type {Error} */
- error2,
- event
- );
- console.error(error_message);
- }),
- handleFetch: module.handleFetch || (({ request, fetch: fetch2 }) => fetch2(request)),
- handleValidationError: module.handleValidationError || (({ issues }) => {
- console.error("Remote function schema validation failed:", issues);
- return { message: "Bad Request" };
- }),
- reroute: module.reroute || (() => {
- }),
- transport: module.transport || {}
- };
- set_app({
- decoders: module.transport ? Object.fromEntries(Object.entries(module.transport).map(([k, v]) => [k, v.decode])) : {}
- });
- if (module.init) {
- await module.init();
- }
- } catch (e) {
- {
- throw e;
- }
- }
- })());
- }
- /**
- * @param {Request} request
- * @param {import('types').RequestOptions} options
- */
- async respond(request, options2) {
- return respond(request, this.#options, this.#manifest, {
- ...options2,
- error: false,
- depth: 0
- });
- }
- }
- export {
- Server
- };
|