@sveltejs/kit 3.0.0-next.0 → 3.0.0-next.10

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.
Files changed (127) hide show
  1. package/package.json +33 -21
  2. package/src/core/adapt/builder.js +29 -43
  3. package/src/core/adapt/index.js +1 -4
  4. package/src/core/config/index.js +132 -71
  5. package/src/core/config/options.js +294 -244
  6. package/src/core/config/types.d.ts +1 -1
  7. package/src/core/env.js +121 -5
  8. package/src/core/generate_manifest/index.js +12 -15
  9. package/src/core/postbuild/analyse.js +7 -19
  10. package/src/core/postbuild/crawl.js +22 -6
  11. package/src/core/postbuild/prerender.js +42 -23
  12. package/src/core/sync/create_manifest_data/index.js +53 -50
  13. package/src/core/sync/sync.js +0 -2
  14. package/src/core/sync/write_client_manifest.js +8 -15
  15. package/src/core/sync/write_env.js +2 -1
  16. package/src/core/sync/write_non_ambient.js +12 -9
  17. package/src/core/sync/write_server.js +13 -14
  18. package/src/core/sync/write_tsconfig.js +16 -8
  19. package/src/core/sync/write_tsconfig_test/package.json +7 -0
  20. package/src/core/sync/write_types/index.js +28 -24
  21. package/src/core/utils.js +2 -2
  22. package/src/exports/env/index.js +12 -0
  23. package/src/exports/hooks/index.js +3 -9
  24. package/src/exports/hooks/sequence.js +3 -2
  25. package/src/exports/index.js +35 -13
  26. package/src/exports/internal/client.js +5 -0
  27. package/src/exports/internal/env.js +2 -2
  28. package/src/exports/internal/index.js +1 -90
  29. package/src/exports/internal/{event.js → server/event.js} +1 -2
  30. package/src/exports/internal/server/index.js +33 -0
  31. package/src/exports/internal/shared.js +89 -0
  32. package/src/exports/node/index.js +62 -15
  33. package/src/exports/params.js +63 -0
  34. package/src/exports/public.d.ts +323 -170
  35. package/src/exports/url.js +86 -0
  36. package/src/exports/vite/build/build_server.js +53 -59
  37. package/src/exports/vite/build/remote.js +18 -11
  38. package/src/exports/vite/build/utils.js +0 -8
  39. package/src/exports/vite/dev/index.js +42 -44
  40. package/src/exports/vite/index.js +810 -593
  41. package/src/exports/vite/preview/index.js +29 -22
  42. package/src/exports/vite/static_analysis/index.js +2 -4
  43. package/src/exports/vite/static_analysis/types.d.ts +14 -0
  44. package/src/exports/vite/utils.js +21 -36
  45. package/src/runtime/app/env/internal.js +4 -4
  46. package/src/runtime/app/env/types.d.ts +1 -1
  47. package/src/runtime/app/environment/index.js +6 -0
  48. package/src/runtime/app/forms.js +24 -7
  49. package/src/runtime/app/paths/client.js +4 -10
  50. package/src/runtime/app/paths/internal/client.js +4 -2
  51. package/src/runtime/app/paths/internal/server.js +2 -23
  52. package/src/runtime/app/paths/public.d.ts +0 -28
  53. package/src/runtime/app/paths/server.js +9 -5
  54. package/src/runtime/app/server/remote/command.js +0 -3
  55. package/src/runtime/app/server/remote/form.js +27 -15
  56. package/src/runtime/app/server/remote/prerender.js +29 -36
  57. package/src/runtime/app/server/remote/query.js +101 -99
  58. package/src/runtime/app/server/remote/requested.js +22 -14
  59. package/src/runtime/app/server/remote/shared.js +30 -26
  60. package/src/runtime/app/state/client.js +1 -2
  61. package/src/runtime/app/stores.js +13 -76
  62. package/src/runtime/client/bundle.js +1 -1
  63. package/src/runtime/client/client-entry.js +3 -0
  64. package/src/runtime/client/client.js +411 -318
  65. package/src/runtime/client/entry.js +24 -3
  66. package/src/runtime/client/fetcher.js +3 -2
  67. package/src/runtime/client/ndjson.js +6 -33
  68. package/src/runtime/client/payload.js +17 -0
  69. package/src/runtime/client/remote-functions/cache.svelte.js +3 -1
  70. package/src/runtime/client/remote-functions/command.svelte.js +7 -32
  71. package/src/runtime/client/remote-functions/form.svelte.js +183 -112
  72. package/src/runtime/client/remote-functions/prerender.svelte.js +29 -8
  73. package/src/runtime/client/remote-functions/query/index.js +7 -14
  74. package/src/runtime/client/remote-functions/query/instance.svelte.js +63 -18
  75. package/src/runtime/client/remote-functions/query/proxy.js +3 -3
  76. package/src/runtime/client/remote-functions/query-batch.svelte.js +60 -68
  77. package/src/runtime/client/remote-functions/query-live/instance.svelte.js +60 -18
  78. package/src/runtime/client/remote-functions/query-live/iterator.js +36 -55
  79. package/src/runtime/client/remote-functions/shared.svelte.js +88 -62
  80. package/src/runtime/client/sse.js +32 -0
  81. package/src/runtime/client/state.svelte.js +65 -49
  82. package/src/runtime/client/stream.js +38 -0
  83. package/src/runtime/client/types.d.ts +11 -7
  84. package/src/runtime/client/utils.js +0 -96
  85. package/src/runtime/components/root.svelte +66 -0
  86. package/src/runtime/env/dynamic/private.js +7 -0
  87. package/src/runtime/env/dynamic/public.js +7 -0
  88. package/src/runtime/env/static/private.js +6 -0
  89. package/src/runtime/env/static/public.js +6 -0
  90. package/src/runtime/form-utils.js +100 -22
  91. package/src/runtime/server/cookie.js +69 -52
  92. package/src/runtime/server/csrf.js +65 -0
  93. package/src/runtime/server/data/index.js +14 -18
  94. package/src/runtime/server/env_module.js +0 -5
  95. package/src/runtime/server/index.js +2 -2
  96. package/src/runtime/server/page/actions.js +41 -26
  97. package/src/runtime/server/page/index.js +9 -15
  98. package/src/runtime/server/page/load_data.js +1 -1
  99. package/src/runtime/server/page/render.js +112 -187
  100. package/src/runtime/server/page/respond_with_error.js +7 -8
  101. package/src/runtime/server/page/server_routing.js +27 -18
  102. package/src/runtime/server/remote.js +355 -216
  103. package/src/runtime/server/respond.js +97 -61
  104. package/src/runtime/server/utils.js +32 -9
  105. package/src/runtime/shared.js +83 -13
  106. package/src/runtime/telemetry/otel.js +1 -1
  107. package/src/runtime/types.d.ts +8 -0
  108. package/src/types/ambient.d.ts +10 -5
  109. package/src/types/global-private.d.ts +15 -22
  110. package/src/types/internal.d.ts +89 -74
  111. package/src/types/private.d.ts +33 -1
  112. package/src/utils/error.js +24 -4
  113. package/src/utils/import.js +6 -1
  114. package/src/utils/imports.js +83 -0
  115. package/src/utils/mime.js +9 -0
  116. package/src/utils/params.js +66 -0
  117. package/src/utils/routing.js +90 -44
  118. package/src/utils/shared-iterator.js +5 -0
  119. package/src/utils/streaming.js +14 -4
  120. package/src/utils/url.js +20 -2
  121. package/src/version.js +1 -1
  122. package/types/index.d.ts +467 -537
  123. package/types/index.d.ts.map +22 -39
  124. package/src/core/sync/write_root.js +0 -148
  125. package/src/exports/internal/server.js +0 -22
  126. package/src/types/synthetic/$lib.md +0 -5
  127. /package/src/exports/internal/{remote-functions.js → server/remote-functions.js} +0 -0
@@ -1,4 +1,4 @@
1
- import { SvelteComponent } from 'svelte';
1
+ import { Component } from 'svelte';
2
2
  import {
3
3
  Config,
4
4
  ServerLoad,
@@ -17,7 +17,6 @@ import {
17
17
  RequestEvent,
18
18
  SSRManifest,
19
19
  Emulator,
20
- Adapter,
21
20
  ServerInit,
22
21
  ClientInit,
23
22
  Transport,
@@ -34,7 +33,7 @@ import {
34
33
  TrailingSlash
35
34
  } from './private.js';
36
35
  import { Span } from '@opentelemetry/api';
37
- import type { PageOptions } from '../exports/vite/static_analysis/index.js';
36
+ import { PageOptions } from '../exports/vite/static_analysis/types.js';
38
37
  import { SharedIterator } from '../utils/shared-iterator.js';
39
38
 
40
39
  export interface ServerModule {
@@ -44,7 +43,6 @@ export interface ServerModule {
44
43
  export interface ServerInternalModule {
45
44
  set_assets(path: string): void;
46
45
  set_building(): void;
47
- set_env(environment: Record<string, string>): void;
48
46
  set_manifest(manifest: SSRManifest): void;
49
47
  set_prerendering(): void;
50
48
  set_read_implementation(implementation: (path: string) => ReadableStream): void;
@@ -101,6 +99,9 @@ export interface BuildData {
101
99
  routes?: SSRClientRoute[];
102
100
  stylesheets: string[];
103
101
  fonts: string[];
102
+ /**
103
+ * Whether the client uses public dynamic env vars — `$env/dynamic/public` or `$app/env/public`.
104
+ */
104
105
  uses_env_dynamic_public: boolean;
105
106
  /** Only set in case of `bundleStrategy === 'inline'`. */
106
107
  inline?: {
@@ -112,7 +113,7 @@ export interface BuildData {
112
113
  }
113
114
 
114
115
  export interface CSRPageNode {
115
- component: typeof SvelteComponent;
116
+ component: Component;
116
117
  universal: {
117
118
  load?: Load;
118
119
  trailingSlash?: TrailingSlash;
@@ -194,7 +195,7 @@ export class InternalServer extends Server {
194
195
  }
195
196
 
196
197
  export interface ManifestData {
197
- /** Static files from `kit.config.files.assets`. */
198
+ /** Static files from `config.files.assets`. */
198
199
  assets: Asset[];
199
200
  hooks: {
200
201
  client: string | null;
@@ -203,7 +204,7 @@ export interface ManifestData {
203
204
  };
204
205
  nodes: PageNode[];
205
206
  routes: RouteData[];
206
- matchers: Record<string, string>;
207
+ params: string | null;
207
208
  }
208
209
 
209
210
  export interface RemoteChunk {
@@ -236,10 +237,7 @@ export interface PrerenderOptions {
236
237
  cache?: string; // including this here is a bit of a hack, but it makes it easy to add <meta http-equiv>
237
238
  fallback?: boolean;
238
239
  dependencies: Map<string, PrerenderDependency>;
239
- /**
240
- * For each key the (possibly still pending) result of a prerendered remote function.
241
- * Used to deduplicate requests to the same remote function with the same arguments.
242
- */
240
+ /** Results of remote `prerender` functions, shared across the whole prerender run so that each only executes once */
243
241
  remote_responses: Map<string, Promise<any>>;
244
242
  /** True for the duration of a call to the `reroute` hook */
245
243
  inside_reroute?: boolean;
@@ -249,7 +247,7 @@ export type RecursiveRequired<T> = {
249
247
  // Recursive implementation of TypeScript's Required utility type.
250
248
  // Will recursively continue until it reaches a primitive or Function
251
249
  // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
252
- [K in keyof T]-?: Extract<T[K], Function> extends never // If it does not have a Function type
250
+ [K in keyof T]-?: Extract<T[K], Function | (`${string}:` & {})> extends never // If it does not have a Function type
253
251
  ? RecursiveRequired<T[K]> // recursively continue through.
254
252
  : T[K]; // Use the exact type for everything else
255
253
  };
@@ -296,6 +294,7 @@ export interface RouteData {
296
294
 
297
295
  export type ServerRedirectNode = {
298
296
  type: 'redirect';
297
+ status: number;
299
298
  location: string;
300
299
  };
301
300
 
@@ -307,38 +306,42 @@ export type ServerNodesResponse = {
307
306
  nodes: Array<ServerDataNode | ServerDataSkippedNode | ServerErrorNode | null>;
308
307
  };
309
308
 
309
+ export type RemoteFunctionDataNode = {
310
+ /** value */
311
+ v?: any;
312
+ /** error */
313
+ e?: App.Error;
314
+ };
315
+
316
+ export type RemoteFunctionData = {
317
+ /** The result of a command/form invocation */
318
+ _?: any;
319
+ /** `prerender` data that was accessed during the request */
320
+ p?: Record<string, RemoteFunctionDataNode>;
321
+ /** `query` or `query.batch` data that was accessed during the request */
322
+ q?: Record<string, RemoteFunctionDataNode>;
323
+ /** `query.live` data that was accessed during the request */
324
+ l?: Record<string, RemoteFunctionDataNode>;
325
+ /** `form` outputs (result/issues/input) from a non-enhanced submission, keyed by the client-side action id */
326
+ f?: Record<string, RemoteFunctionDataNode>;
327
+ /** Whether there were any refreshes/reconnects during the request */
328
+ r?: true;
329
+ /** The redirect location, if any */
330
+ redirect?: string;
331
+ };
332
+
310
333
  export type RemoteFunctionResponse =
311
334
  | (ServerRedirectNode & {
312
- /** devalue'd Record<string, any> */
313
- refreshes?: string;
314
- /** devalue'd Record<string, any> */
315
- reconnects?: string;
335
+ /** stringified RemoteFunctionData */
336
+ data: string;
316
337
  })
317
338
  | ServerErrorNode
318
339
  | {
319
340
  type: 'result';
320
- result: string;
321
- /** devalue'd Record<string, any> */
322
- refreshes: string | undefined;
323
- /** devalue'd Record<string, any> */
324
- reconnects: string | undefined;
341
+ /** stringified RemoteFunctionData */
342
+ data: string;
325
343
  };
326
344
 
327
- export type RemoteSingleflightResult = {
328
- type: 'result';
329
- data: any;
330
- };
331
-
332
- export type RemoteSingleflightError = {
333
- type: 'error';
334
- status?: number;
335
- error: App.Error;
336
- };
337
-
338
- export type RemoteSingleflightEntry = RemoteSingleflightResult | RemoteSingleflightError;
339
-
340
- export type RemoteSingleflightMap = Record<string, RemoteSingleflightEntry>;
341
-
342
345
  export type RemoteLiveQueryUserFunctionReturnType<Output> = MaybePromise<
343
346
  | AsyncGenerator<Output>
344
347
  | AsyncIterator<Output>
@@ -388,10 +391,6 @@ export interface ServerDataSkippedNode {
388
391
  export interface ServerErrorNode {
389
392
  type: 'error';
390
393
  error: App.Error;
391
- /**
392
- * Only set for HttpErrors.
393
- */
394
- status?: number;
395
394
  }
396
395
 
397
396
  export interface ServerMetadataRoute {
@@ -418,27 +417,7 @@ export interface ServerMetadata {
418
417
  remotes: Map<string, Map<string, { type: RemoteInternals['type']; dynamic: boolean }>>;
419
418
  }
420
419
 
421
- export interface SSRComponent {
422
- default: {
423
- render(
424
- props: Record<string, any>,
425
- opts: { context: Map<any, any>; csp?: { nonce?: string; hash?: boolean } }
426
- ): {
427
- html: string;
428
- head: string;
429
- css: {
430
- code: string;
431
- map: any; // TODO
432
- };
433
- /** Until we require all Svelte versions that support hashes, this might not be defined */
434
- hashes?: {
435
- script: Array<`sha256-${string}`>;
436
- };
437
- };
438
- };
439
- }
440
-
441
- export type SSRComponentLoader = () => Promise<SSRComponent>;
420
+ export type SSRComponentLoader = () => Promise<Component>;
442
421
 
443
422
  export interface UniversalNode {
444
423
  /** Is `null` in case static analysis succeeds but the node is ssr=false */
@@ -477,7 +456,10 @@ export interface SSRNode {
477
456
 
478
457
  /**
479
458
  * During development, all styles are inlined for the page to avoid FOUC.
480
- * But in production, this stores styles that are below the inline threshold
459
+ * But in production, this stores styles that are below the inline threshold.
460
+ * It returns a Promise during development because Vite needs to load the
461
+ * modules on demand. But in production, the contents have been precomputed
462
+ * during the build, so it can return synchronously.
481
463
  */
482
464
  inline_styles?(): MaybePromise<
483
465
  Record<string, string | ((assets: string, base: string) => string)>
@@ -494,7 +476,6 @@ export type SSRNodeLoader = () => Promise<SSRNode>;
494
476
 
495
477
  export interface SSROptions {
496
478
  app_template_contains_nonce: boolean;
497
- async: boolean;
498
479
  csp: ValidatedConfig['kit']['csp'];
499
480
  csrf_check_origin: boolean;
500
481
  csrf_trusted_origins: string[];
@@ -502,10 +483,9 @@ export interface SSROptions {
502
483
  hash_routing: boolean;
503
484
  hooks: ServerHooks;
504
485
  link_header_preload: ValidatedConfig['kit']['output']['linkHeaderPreload'];
505
- root: SSRComponent['default'];
486
+ paths_origin: string | undefined;
506
487
  service_worker: boolean;
507
488
  service_worker_options: RegistrationOptions;
508
- server_error_boundaries: boolean;
509
489
  templates: {
510
490
  app(values: {
511
491
  head: string;
@@ -598,13 +578,12 @@ export interface Uses {
598
578
  search_params: Set<string>;
599
579
  }
600
580
 
601
- export type ValidatedConfig = Config & {
581
+ export type ValidatedConfig = Omit<Config, 'kit'> & {
602
582
  kit: ValidatedKitConfig;
603
583
  extensions: string[];
604
584
  };
605
585
 
606
- // TODO: remove the omit in 4.0
607
- export type ValidatedKitConfig = Omit<RecursiveRequired<KitConfig>, 'adapter'>;
586
+ export type ValidatedKitConfig = RecursiveRequired<KitConfig>;
608
587
 
609
588
  export type BinaryFormMeta = {
610
589
  remote_refreshes?: string[];
@@ -663,6 +642,11 @@ export interface RemoteCommandInternals extends BaseRemoteInternals {
663
642
 
664
643
  export interface RemoteFormInternals extends BaseRemoteInternals {
665
644
  type: 'form';
645
+ /**
646
+ * For keyed (`form.for(key)`) instances: the id as the client computes it
647
+ * (the key is JSON-stringified but not URI-encoded, unlike `id`)
648
+ */
649
+ action_id?: string;
666
650
  fn(body: Record<string, any>, meta: BinaryFormMeta, form_data: FormData | null): Promise<any>;
667
651
  }
668
652
 
@@ -708,15 +692,26 @@ export interface RequestState {
708
692
  record_span: RecordSpan;
709
693
  };
710
694
  readonly remote: {
711
- data: null | Map<
712
- RemoteInternals,
713
- Record<string, { serialize: boolean; data: MaybePromise<any> }>
695
+ /** Resolved query/prerender data, populated by `await myQuery()` or `myQuery.set(...)` */
696
+ data: null | Map<RemoteInternals, Record<string, MaybePromise<any>>>;
697
+ /**
698
+ * Data that is implicitly included because it was awaited during render.
699
+ * This is serialized during SSR if the promise already resolved
700
+ */
701
+ implicit: null | Map<RemoteInternals, Record<string, () => MaybePromise<any>>>;
702
+ /**
703
+ * Data that is explicitly included because of a `set(...)` or `refresh()`.
704
+ * This is always awaited
705
+ */
706
+ explicit: null | Map<
707
+ string,
708
+ {
709
+ internals: RemoteInternals;
710
+ fn: () => Promise<any>;
711
+ }
714
712
  >;
715
713
  /** Instances created via `myForm.for(...)` */
716
714
  forms: null | Map<string, any>;
717
- /** A map of remote function key to corresponding single-flight-mutation promise */
718
- refreshes: null | Map<string, Promise<any>>;
719
- reconnects: null | Map<string, Promise<void>>;
720
715
  /** A map of remote function ID to payloads requested for refreshing by the client */
721
716
  requested: null | Map<string, string[]>;
722
717
  /** A map of query.batch ID to payloads requested for that batch within the same macrotask */
@@ -739,6 +734,8 @@ export interface RequestState {
739
734
  live_iterators: null | Map<string, SharedIterator<any>>;
740
735
  };
741
736
  readonly is_in_remote_function: boolean;
737
+ readonly is_in_remote_form_or_command: boolean;
738
+ readonly is_in_remote_query: boolean;
742
739
  readonly is_in_render: boolean;
743
740
  readonly is_in_universal_load: boolean;
744
741
  }
@@ -748,5 +745,23 @@ export interface RequestStore {
748
745
  state: RequestState;
749
746
  }
750
747
 
748
+ /** Type of the `__sveltekit_abc123` object in the init `<script>` */
749
+ export interface SvelteKitPayload {
750
+ /** The application version */
751
+ version: string;
752
+ /** The basepath, usually relative to the current page */
753
+ base: string;
754
+ /** Path to externally-hosted assets */
755
+ assets?: string;
756
+ /** Public environment variables */
757
+ env?: Record<string, string>;
758
+ /** Serialized data from query/form/command functions */
759
+ data?: RemoteFunctionData;
760
+ /** Create a placeholder promise */
761
+ defer?: (id: number) => Promise<any>;
762
+ /** Resolve a placeholder promise */
763
+ resolve?: (data: { id: number; data: any; error: any }) => void;
764
+ }
765
+
751
766
  export * from '../exports/index.js';
752
767
  export * from './private.js';
@@ -61,7 +61,10 @@ export namespace Csp {
61
61
  | 'unsafe-eval'
62
62
  | 'unsafe-hashes'
63
63
  | 'unsafe-inline'
64
+ | 'unsafe-allow-redirects'
65
+ | 'unsafe-webtransport-hashes'
64
66
  | 'wasm-unsafe-eval'
67
+ | 'trusted-types-eval'
65
68
  | 'none';
66
69
  type CryptoSource = `${'nonce' | 'sha256' | 'sha384' | 'sha512'}-${string}`;
67
70
  type FrameSource = HostSource | SchemeSource | 'self' | 'none';
@@ -70,7 +73,16 @@ export namespace Csp {
70
73
  type HostProtocolSchemes = `${string}://` | '';
71
74
  type HttpDelineator = '/' | '?' | '#' | '\\';
72
75
  type PortScheme = `:${number}` | '' | ':*';
73
- type SchemeSource = 'http:' | 'https:' | 'data:' | 'mediastream:' | 'blob:' | 'filesystem:';
76
+ type SchemeSource =
77
+ | 'http:'
78
+ | 'https:'
79
+ | 'ws:'
80
+ | 'wss:'
81
+ | 'data:'
82
+ | 'mediastream:'
83
+ | 'blob:'
84
+ | 'filesystem:'
85
+ | (`${string}:` & {});
74
86
  type Source = HostSource | SchemeSource | CryptoSource | BaseSource;
75
87
  type Sources = Source[];
76
88
  }
@@ -212,6 +224,10 @@ export interface PrerenderUnseenRoutesHandler {
212
224
  (details: { routes: string[]; message: string }): void;
213
225
  }
214
226
 
227
+ export interface PrerenderInvalidUrlHandler {
228
+ (details: { href: string; referrer: string | null; message: string }): void;
229
+ }
230
+
215
231
  export type PrerenderHttpErrorHandlerValue = 'fail' | 'warn' | 'ignore' | PrerenderHttpErrorHandler;
216
232
  export type PrerenderMissingIdHandlerValue = 'fail' | 'warn' | 'ignore' | PrerenderMissingIdHandler;
217
233
  export type PrerenderUnseenRoutesHandlerValue =
@@ -224,6 +240,11 @@ export type PrerenderEntryGeneratorMismatchHandlerValue =
224
240
  | 'warn'
225
241
  | 'ignore'
226
242
  | PrerenderEntryGeneratorMismatchHandler;
243
+ export type PrerenderInvalidUrlHandlerValue =
244
+ | 'fail'
245
+ | 'warn'
246
+ | 'ignore'
247
+ | PrerenderInvalidUrlHandler;
227
248
 
228
249
  export type PrerenderOption = boolean | 'auto';
229
250
 
@@ -252,3 +273,14 @@ export type DeepPartial<T> = T extends Record<PropertyKey, unknown> | unknown[]
252
273
  : T | undefined;
253
274
 
254
275
  export type IsAny<T> = 0 extends 1 & T ? true : false;
276
+
277
+ export type HasNonOptionalBoolean<T> =
278
+ IsAny<T> extends true
279
+ ? never
280
+ : [T] extends [boolean]
281
+ ? true
282
+ : T extends Array<infer U>
283
+ ? HasNonOptionalBoolean<U>
284
+ : T extends Record<string, any>
285
+ ? { [K in keyof T]: HasNonOptionalBoolean<T[K]> }[keyof T]
286
+ : never;
@@ -1,5 +1,17 @@
1
1
  import { HttpError, SvelteKitError } from '@sveltejs/kit/internal';
2
2
 
3
+ /**
4
+ * For times when you need to throw an error, but without
5
+ * displaying a useless stack trace (since the developer
6
+ * can't do anything useful with it)
7
+ * @param {string} message
8
+ */
9
+ export function stackless(message) {
10
+ const error = new Error(message);
11
+ error.stack = '';
12
+ return error;
13
+ }
14
+
3
15
  /**
4
16
  * @param {unknown} err
5
17
  * @return {Error}
@@ -18,16 +30,24 @@ export function coalesce_to_error(err) {
18
30
  * @param {unknown} error
19
31
  */
20
32
  export function normalize_error(error) {
21
- return /** @type {import('../exports/internal/index.js').Redirect | HttpError | SvelteKitError | Error} */ (
33
+ return /** @type {import('../exports/internal/shared.js').Redirect | HttpError | SvelteKitError | Error} */ (
22
34
  error
23
35
  );
24
36
  }
25
37
 
26
38
  /**
27
- * @param {unknown} error
39
+ * @param {any} transformed
40
+ * @param {any} [error]
28
41
  */
29
- export function get_status(error) {
30
- return error instanceof HttpError || error instanceof SvelteKitError ? error.status : 500;
42
+ export function get_status(transformed, error) {
43
+ const err = error ?? transformed;
44
+ const status = err instanceof HttpError || err instanceof SvelteKitError ? err.status : 500;
45
+
46
+ if (error == null || typeof transformed?.status !== 'number') {
47
+ return status;
48
+ } else {
49
+ return transformed.status;
50
+ }
31
51
  }
32
52
 
33
53
  /**
@@ -2,7 +2,12 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
 
4
4
  /**
5
- * Resolves a peer dependency relative to the current working directory. Duplicated with `packages/adapter-auto`
5
+ * Resolves a peer dependency relative to the current working directory.
6
+ *
7
+ * Mainly used to resolve the correct Vite package when an app's SvelteKit is a
8
+ * linked local repository.
9
+ *
10
+ * Duplicated with `packages/adapter-auto`
6
11
  * @param {string} dependency
7
12
  * @param {string} root
8
13
  */
@@ -0,0 +1,83 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ /**
5
+ * Reads the `imports` field from the package.json at the given directory.
6
+ * @param {string} cwd
7
+ * @returns {Record<string, string | Record<string, string>> | undefined}
8
+ */
9
+ export function read_package_imports(cwd) {
10
+ const pkg_path = path.resolve(cwd, 'package.json');
11
+ if (fs.existsSync(pkg_path)) {
12
+ try {
13
+ return JSON.parse(fs.readFileSync(pkg_path, 'utf-8')).imports;
14
+ } catch {
15
+ // malformed package.json — ignore, the user will see other errors
16
+ }
17
+ }
18
+ }
19
+
20
+ /**
21
+ * Normalizes an import value to a string path, handling both string values
22
+ * and conditional export objects (using the `default` key).
23
+ * @param {string | Record<string, string>} value
24
+ * @returns {string | null}
25
+ */
26
+ export function normalize_import_value(value) {
27
+ if (typeof value === 'string') {
28
+ return value.replace(/^\.\//, '');
29
+ }
30
+ if (value && typeof value === 'object' && typeof value.default === 'string') {
31
+ return value.default.replace(/^\.\//, '');
32
+ }
33
+ return null;
34
+ }
35
+
36
+ /**
37
+ * Returns the `#`-prefixed import keys from the package.json `imports` field.
38
+ * @param {string} cwd
39
+ * @returns {string[]}
40
+ */
41
+ export function get_hash_import_keys(cwd) {
42
+ const imports = read_package_imports(cwd);
43
+ if (!imports) return [];
44
+ return Object.keys(imports).filter((key) => key.startsWith('#'));
45
+ }
46
+
47
+ /**
48
+ * Computes alias entries for `normalize_id` from the package.json `imports` field.
49
+ * Each entry maps a `#xx` alias to its absolute target path. Entries are sorted
50
+ * by path length descending so that longer/more-specific paths are matched first.
51
+ *
52
+ * @param {string} root
53
+ * @param {(p: string) => string} [normalize] - optional path normalizer (e.g. `vite.normalizePath`)
54
+ * @returns {Array<{ alias: string, path: string }>}
55
+ */
56
+ export function get_import_aliases(root, normalize) {
57
+ const imports = read_package_imports(root);
58
+ if (!imports) return [];
59
+
60
+ /** @type {Array<{ alias: string, path: string }>} */
61
+ const aliases = [];
62
+
63
+ for (const [key, raw_value] of Object.entries(imports)) {
64
+ if (!key.startsWith('#')) continue;
65
+
66
+ const value = normalize_import_value(raw_value);
67
+ if (!value) continue;
68
+
69
+ // For `#xx/*` imports, the target directory is the value without `/*`
70
+ // For `#xx` imports, the target is the file itself
71
+ const target = value.endsWith('/*') ? value.slice(0, -2) : value;
72
+ if (target.includes('*')) continue; // not sure if this is even allowed in Node, anyway it's too niche/complex to support here
73
+ const abs = path.resolve(root, target);
74
+ const alias = key.endsWith('/*') ? key.slice(0, -2) : key;
75
+
76
+ aliases.push({ alias, path: normalize ? normalize(abs) : abs });
77
+ }
78
+
79
+ // Sort by path length descending so longer/more-specific paths match first
80
+ aliases.sort((a, b) => b.path.length - a.path.length);
81
+
82
+ return aliases;
83
+ }
@@ -0,0 +1,9 @@
1
+ import { mimes, lookup } from 'mrmime';
2
+
3
+ // `mrmime` does not include `.ico` in its database. The IANA-registered
4
+ // type is `image/vnd.microsoft.icon`, but `image/x-icon` is the
5
+ // semi-official type that is universally supported by browsers and other
6
+ // tools, so we use that.
7
+ mimes['ico'] = 'image/x-icon';
8
+
9
+ export { lookup };
@@ -0,0 +1,66 @@
1
+ import path from 'node:path';
2
+ import { pathToFileURL } from 'node:url';
3
+
4
+ /**
5
+ * @param {import('types').RouteData[]} routes
6
+ * @returns {Set<string>}
7
+ */
8
+ export function collect_matcher_names(routes) {
9
+ /** @type {Set<string>} */
10
+ const names = new Set();
11
+
12
+ for (const route of routes) {
13
+ for (const param of route.params) {
14
+ if (param.matcher) names.add(param.matcher);
15
+ }
16
+ }
17
+
18
+ return names;
19
+ }
20
+
21
+ /**
22
+ * @param {Record<string, unknown>} params
23
+ * @param {Set<string>} names
24
+ * @param {string} [file]
25
+ */
26
+ export function validate_param_matchers(params, names, file) {
27
+ for (const name of names) {
28
+ if (!(name in params)) {
29
+ throw new Error(`No matcher found for parameter '${name}'${file ? ` in ${file}` : ''}`);
30
+ }
31
+ }
32
+ }
33
+
34
+ /**
35
+ * @param {{
36
+ * routes: import('types').RouteData[];
37
+ * params_path: string | null;
38
+ * root: string;
39
+ * load?: (file: string) => Promise<Record<string, unknown>>;
40
+ * }} opts
41
+ * @returns {Promise<Record<string, import('@sveltejs/kit').ParamMatcher> | null>}
42
+ */
43
+ export async function load_and_validate_params({ routes, params_path, root, load }) {
44
+ const names = collect_matcher_names(routes);
45
+
46
+ if (names.size === 0) return null;
47
+
48
+ if (!params_path) {
49
+ throw new Error(`No matcher found for parameter '${names.values().next().value}'`);
50
+ }
51
+
52
+ const file = path.resolve(root, params_path);
53
+ const module = load ? await load(file) : await import(pathToFileURL(file).href);
54
+
55
+ if (!module.params || typeof module.params !== 'object') {
56
+ throw new Error(`${params_path} does not export \`params\` from \`defineParams\``);
57
+ }
58
+
59
+ validate_param_matchers(
60
+ /** @type {Record<string, unknown>} */ (module.params),
61
+ names,
62
+ params_path
63
+ );
64
+
65
+ return /** @type {Record<string, import('@sveltejs/kit').ParamMatcher>} */ (module.params);
66
+ }