@sveltejs/kit 1.2.4 → 1.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -364,10 +364,13 @@ export async function dev(vite, vite_config, svelte_config) {
364
364
  /** @type {function} */ (middleware.handle).name === 'viteServeStaticMiddleware'
365
365
  );
366
366
 
367
+ // Vite will give a 403 on URLs like /test, /static, and /package.json preventing us from
368
+ // serving routes with those names. See https://github.com/vitejs/vite/issues/7363
367
369
  remove_static_middlewares(vite.middlewares);
368
370
 
369
371
  vite.middlewares.use(async (req, res) => {
370
372
  // Vite's base middleware strips out the base path. Restore it
373
+ const original_url = req.url;
371
374
  req.url = req.originalUrl;
372
375
  try {
373
376
  const base = `${vite.config.server.https ? 'https' : 'http'}://${
@@ -375,13 +378,14 @@ export async function dev(vite, vite_config, svelte_config) {
375
378
  }`;
376
379
 
377
380
  const decoded = decodeURI(new URL(base + req.url).pathname);
378
- const file = posixify(path.resolve(decoded.slice(1)));
381
+ const file = posixify(path.resolve(decoded.slice(svelte_config.kit.paths.base.length + 1)));
379
382
  const is_file = fs.existsSync(file) && !fs.statSync(file).isDirectory();
380
383
  const allowed =
381
384
  !vite_config.server.fs.strict ||
382
385
  vite_config.server.fs.allow.some((dir) => file.startsWith(dir));
383
386
 
384
387
  if (is_file && allowed) {
388
+ req.url = original_url;
385
389
  // @ts-expect-error
386
390
  serve_static_middleware.handle(req, res);
387
391
  return;
@@ -24,9 +24,6 @@ export async function handle_action_json_request(event, options, server) {
24
24
  const actions = server?.actions;
25
25
 
26
26
  if (!actions) {
27
- if (server) {
28
- maybe_throw_migration_error(server);
29
- }
30
27
  // TODO should this be a different error altogether?
31
28
  const no_actions_error = error(405, 'POST method not allowed. No actions exist for this page');
32
29
  return action_json(
@@ -50,6 +47,10 @@ export async function handle_action_json_request(event, options, server) {
50
47
  try {
51
48
  const data = await call_action(event, actions);
52
49
 
50
+ if (__SVELTEKIT_DEV__) {
51
+ validate_action_return(data);
52
+ }
53
+
53
54
  if (data instanceof ActionFailure) {
54
55
  return action_json({
55
56
  type: 'failure',
@@ -124,7 +125,6 @@ export async function handle_action_request(event, server) {
124
125
  const actions = server.actions;
125
126
 
126
127
  if (!actions) {
127
- maybe_throw_migration_error(server);
128
128
  // TODO should this be a different error altogether?
129
129
  event.setHeaders({
130
130
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
@@ -142,13 +142,22 @@ export async function handle_action_request(event, server) {
142
142
  try {
143
143
  const data = await call_action(event, actions);
144
144
 
145
+ if (__SVELTEKIT_DEV__) {
146
+ validate_action_return(data);
147
+ }
148
+
145
149
  if (data instanceof ActionFailure) {
146
- return { type: 'failure', status: data.status, data: data.data };
150
+ return {
151
+ type: 'failure',
152
+ status: data.status,
153
+ data: data.data
154
+ };
147
155
  } else {
148
156
  return {
149
157
  type: 'success',
150
158
  status: 200,
151
- data: /** @type {Record<string, any> | undefined} */ (data)
159
+ // @ts-expect-error this will be removed upon serialization, so `undefined` is the same as omission
160
+ data
152
161
  };
153
162
  }
154
163
  } catch (e) {
@@ -185,7 +194,7 @@ function check_named_default_separate(actions) {
185
194
  * @param {NonNullable<import('types').SSRNode['server']['actions']>} actions
186
195
  * @throws {Redirect | ActionFailure | HttpError | Error}
187
196
  */
188
- export async function call_action(event, actions) {
197
+ async function call_action(event, actions) {
189
198
  const url = new URL(event.request.url);
190
199
 
191
200
  let name = 'default';
@@ -213,16 +222,16 @@ export async function call_action(event, actions) {
213
222
  return action(event);
214
223
  }
215
224
 
216
- /**
217
- * @param {import('types').SSRNode['server']} server
218
- */
219
- function maybe_throw_migration_error(server) {
220
- for (const method of ['POST', 'PUT', 'PATCH', 'DELETE']) {
221
- if (/** @type {any} */ (server)[method]) {
222
- throw new Error(
223
- `${method} method no longer allowed in +page.server, use actions instead. See the PR for more info: https://github.com/sveltejs/kit/pull/6469`
224
- );
225
- }
225
+ /** @param {any} data */
226
+ function validate_action_return(data) {
227
+ if (data instanceof Redirect) {
228
+ throw new Error(`Cannot \`return redirect(...)\` — use \`throw redirect(...)\` instead`);
229
+ }
230
+
231
+ if (data instanceof HttpError) {
232
+ throw new Error(
233
+ `Cannot \`return error(...)\` — use \`throw error(...)\` or \`return fail(...)\` instead`
234
+ );
226
235
  }
227
236
  }
228
237