@sveltejs/kit 1.0.0-next.204 → 1.0.0-next.205

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.
@@ -17,6 +17,85 @@ import 'node:stream';
17
17
  import 'node:util';
18
18
  import 'node:url';
19
19
 
20
+ /** @typedef {{
21
+ * fn: () => Promise<any>,
22
+ * fulfil: (value: any) => void,
23
+ * reject: (error: Error) => void
24
+ * }} Task */
25
+
26
+ /** @param {number} concurrency */
27
+ function queue(concurrency) {
28
+ /** @type {Task[]} */
29
+ const tasks = [];
30
+
31
+ let current = 0;
32
+
33
+ /** @type {(value?: any) => void} */
34
+ let fulfil;
35
+
36
+ /** @type {(error: Error) => void} */
37
+ let reject;
38
+
39
+ let closed = false;
40
+
41
+ const done = new Promise((f, r) => {
42
+ fulfil = f;
43
+ reject = r;
44
+ });
45
+
46
+ done.catch(() => {
47
+ // this is necessary in case a catch handler is never added
48
+ // to the done promise by the user
49
+ });
50
+
51
+ function dequeue() {
52
+ if (current < concurrency) {
53
+ const task = tasks.shift();
54
+
55
+ if (task) {
56
+ current += 1;
57
+ const promise = Promise.resolve(task.fn());
58
+
59
+ promise
60
+ .then(task.fulfil, (err) => {
61
+ task.reject(err);
62
+ reject(err);
63
+ })
64
+ .then(() => {
65
+ current -= 1;
66
+ dequeue();
67
+ });
68
+ } else if (current === 0) {
69
+ closed = true;
70
+ fulfil();
71
+ }
72
+ }
73
+ }
74
+
75
+ return {
76
+ /** @param {() => any} fn */
77
+ add: (fn) => {
78
+ if (closed) throw new Error('Cannot add tasks to a queue that has ended');
79
+
80
+ const promise = new Promise((fulfil, reject) => {
81
+ tasks.push({ fn, fulfil, reject });
82
+ });
83
+
84
+ dequeue();
85
+ return promise;
86
+ },
87
+
88
+ done: () => {
89
+ if (current === 0) {
90
+ closed = true;
91
+ fulfil();
92
+ }
93
+
94
+ return done;
95
+ }
96
+ };
97
+ }
98
+
20
99
  /**
21
100
  * @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
22
101
  * @typedef {import('types/config').PrerenderOnErrorValue} OnError
@@ -151,16 +230,27 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
151
230
  return path;
152
231
  }
153
232
 
233
+ const q = queue(config.kit.prerender.concurrency);
234
+
154
235
  /**
155
236
  * @param {string} decoded_path
156
237
  * @param {string?} referrer
157
238
  */
158
- async function visit(decoded_path, referrer) {
239
+ function enqueue(decoded_path, referrer) {
159
240
  const path = encodeURI(normalize(decoded_path));
160
241
 
161
242
  if (seen.has(path)) return;
162
243
  seen.add(path);
163
244
 
245
+ return q.add(() => visit(path, decoded_path, referrer));
246
+ }
247
+
248
+ /**
249
+ * @param {string} path
250
+ * @param {string} decoded_path
251
+ * @param {string?} referrer
252
+ */
253
+ async function visit(path, decoded_path, referrer) {
164
254
  /** @type {Map<string, import('types/hooks').ServerResponse>} */
165
255
  const dependencies = new Map();
166
256
 
@@ -205,7 +295,7 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
205
295
 
206
296
  const resolved = resolve$1(path, location);
207
297
  if (is_root_relative(resolved)) {
208
- await visit(resolved, path);
298
+ enqueue(resolved, path);
209
299
  }
210
300
  } else {
211
301
  log.warn(`location header missing on redirect received from ${decoded_path}`);
@@ -290,7 +380,7 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
290
380
 
291
381
  if (parsed.search) ;
292
382
 
293
- await visit(pathname, path);
383
+ enqueue(pathname, path);
294
384
  }
295
385
  }
296
386
  }
@@ -300,12 +390,14 @@ async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
300
390
  for (const entry of config.kit.prerender.entries) {
301
391
  if (entry === '*') {
302
392
  for (const entry of build_data.entries) {
303
- await visit(entry, null);
393
+ enqueue(entry, null);
304
394
  }
305
395
  } else {
306
- await visit(entry, null);
396
+ enqueue(entry, null);
307
397
  }
308
398
  }
399
+
400
+ await q.done();
309
401
  }
310
402
 
311
403
  if (fallback) {
package/dist/cli.js CHANGED
@@ -471,6 +471,7 @@ const options = object(
471
471
  }),
472
472
 
473
473
  prerender: object({
474
+ concurrency: number(1),
474
475
  crawl: boolean(true),
475
476
  enabled: boolean(true),
476
477
  entries: validate(['*'], (input, keypath) => {
@@ -617,6 +618,19 @@ function string(fallback, allow_empty = true) {
617
618
  });
618
619
  }
619
620
 
621
+ /**
622
+ * @param {number} fallback
623
+ * @returns {Validator}
624
+ */
625
+ function number(fallback) {
626
+ return validate(fallback, (input, keypath) => {
627
+ if (typeof input !== 'number') {
628
+ throw new Error(`${keypath} should be a number, if specified`);
629
+ }
630
+ return input;
631
+ });
632
+ }
633
+
620
634
  /**
621
635
  * @param {boolean} fallback
622
636
  * @returns {Validator}
@@ -822,7 +836,7 @@ async function launch(port, https) {
822
836
  exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}`);
823
837
  }
824
838
 
825
- const prog = sade('svelte-kit').version('1.0.0-next.204');
839
+ const prog = sade('svelte-kit').version('1.0.0-next.205');
826
840
 
827
841
  prog
828
842
  .command('dev')
@@ -987,7 +1001,7 @@ async function check_port(port) {
987
1001
  function welcome({ port, host, https, open, loose, allow, cwd }) {
988
1002
  if (open) launch(port, https);
989
1003
 
990
- console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.204'}\n`));
1004
+ console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.205'}\n`));
991
1005
 
992
1006
  const protocol = https ? 'https:' : 'http:';
993
1007
  const exposed = typeof host !== 'undefined' && host !== 'localhost' && host !== '127.0.0.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.204",
3
+ "version": "1.0.0-next.205",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
package/types/config.d.ts CHANGED
@@ -76,6 +76,7 @@ export interface Config {
76
76
  base?: string;
77
77
  };
78
78
  prerender?: {
79
+ concurrency?: number;
79
80
  crawl?: boolean;
80
81
  enabled?: boolean;
81
82
  entries?: string[];