|
1 | 1 | /** @import { RequestEvent } from '@sveltejs/kit' */
|
2 |
| -/** @import { ServerHooks, MaybePromise, RequestState, RemoteInfo } from 'types' */ |
| 2 | +/** @import { ServerHooks, MaybePromise, RequestState, RemoteInfo, RequestStore } from 'types' */ |
3 | 3 | import { parse } from 'devalue';
|
4 | 4 | import { error } from '@sveltejs/kit';
|
5 | 5 | import { with_request_store, get_request_store } from '@sveltejs/kit/internal/server';
|
@@ -103,42 +103,48 @@ export function parse_remote_response(data, transport) {
|
103 | 103 | * @param {(arg?: any) => T} fn
|
104 | 104 | */
|
105 | 105 | export async function run_remote_function(event, state, allow_cookies, arg, validate, fn) {
|
106 |
| - /** @type {RequestEvent} */ |
107 |
| - const cleansed = { |
108 |
| - ...event, |
109 |
| - setHeaders: () => { |
110 |
| - throw new Error('setHeaders is not allowed in remote functions'); |
111 |
| - }, |
112 |
| - cookies: { |
113 |
| - ...event.cookies, |
114 |
| - set: (name, value, opts) => { |
115 |
| - if (!allow_cookies) { |
116 |
| - throw new Error('Cannot set cookies in `query` or `prerender` functions'); |
117 |
| - } |
118 |
| - |
119 |
| - if (opts.path && !opts.path.startsWith('/')) { |
120 |
| - throw new Error('Cookies set in remote functions must have an absolute path'); |
121 |
| - } |
122 |
| - |
123 |
| - return event.cookies.set(name, value, opts); |
| 106 | + /** @type {RequestStore} */ |
| 107 | + const store = { |
| 108 | + event: { |
| 109 | + ...event, |
| 110 | + setHeaders: () => { |
| 111 | + throw new Error('setHeaders is not allowed in remote functions'); |
124 | 112 | },
|
125 |
| - delete: (name, opts) => { |
126 |
| - if (!allow_cookies) { |
127 |
| - throw new Error('Cannot delete cookies in `query` or `prerender` functions'); |
| 113 | + cookies: { |
| 114 | + ...event.cookies, |
| 115 | + set: (name, value, opts) => { |
| 116 | + if (!allow_cookies) { |
| 117 | + throw new Error('Cannot set cookies in `query` or `prerender` functions'); |
| 118 | + } |
| 119 | + |
| 120 | + if (opts.path && !opts.path.startsWith('/')) { |
| 121 | + throw new Error('Cookies set in remote functions must have an absolute path'); |
| 122 | + } |
| 123 | + |
| 124 | + return event.cookies.set(name, value, opts); |
| 125 | + }, |
| 126 | + delete: (name, opts) => { |
| 127 | + if (!allow_cookies) { |
| 128 | + throw new Error('Cannot delete cookies in `query` or `prerender` functions'); |
| 129 | + } |
| 130 | + |
| 131 | + if (opts.path && !opts.path.startsWith('/')) { |
| 132 | + throw new Error('Cookies deleted in remote functions must have an absolute path'); |
| 133 | + } |
| 134 | + |
| 135 | + return event.cookies.delete(name, opts); |
128 | 136 | }
|
129 |
| - |
130 |
| - if (opts.path && !opts.path.startsWith('/')) { |
131 |
| - throw new Error('Cookies deleted in remote functions must have an absolute path'); |
132 |
| - } |
133 |
| - |
134 |
| - return event.cookies.delete(name, opts); |
135 | 137 | }
|
| 138 | + }, |
| 139 | + state: { |
| 140 | + ...state, |
| 141 | + is_in_remote_function: true |
136 | 142 | }
|
137 | 143 | };
|
138 | 144 |
|
139 | 145 | // In two parts, each with_event, so that runtimes without async local storage can still get the event at the start of the function
|
140 |
| - const validated = await with_request_store({ event: cleansed, state }, () => validate(arg)); |
141 |
| - return with_request_store({ event: cleansed, state }, () => fn(validated)); |
| 146 | + const validated = await with_request_store(store, () => validate(arg)); |
| 147 | + return with_request_store(store, () => fn(validated)); |
142 | 148 | }
|
143 | 149 |
|
144 | 150 | /**
|
|
0 commit comments