@astroscope/boot 0.2.0 → 0.2.2

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/dist/index.cjs CHANGED
@@ -227,10 +227,27 @@ function boot(options = {}) {
227
227
  }
228
228
  }
229
229
  const { host, port } = getServerDefaults(astroConfig);
230
- const bootImport = `import * as __boot from './${bootChunkName}';
230
+ const bootImport = `globalThis.__astroscope_server_url = import.meta.url;
231
+ import * as __boot from './${bootChunkName}';
231
232
  const __bootContext = { dev: false, host: process.env.HOST ?? '${host}', port: process.env.PORT ? Number(process.env.PORT) : ${port} };
232
- await __boot.onStartup?.(__bootContext);
233
- if (__boot.onShutdown) process.on('SIGTERM', async () => { await __boot.onShutdown(__bootContext); process.exit(0); });
233
+ try {
234
+ await __boot.onStartup?.(__bootContext);
235
+ } catch (err) {
236
+ console.error('[boot] startup failed:', err);
237
+ try { await __boot.onShutdown?.(__bootContext); } catch {}
238
+ process.exit(1);
239
+ }
240
+ if (__boot.onShutdown) {
241
+ process.on('SIGTERM', async () => {
242
+ try {
243
+ await __boot.onShutdown(__bootContext);
244
+ process.exit(0);
245
+ } catch (err) {
246
+ console.error('[boot] shutdown failed:', err);
247
+ process.exit(1);
248
+ }
249
+ });
250
+ }
234
251
  `;
235
252
  const s = new import_magic_string.default(entryChunk.code);
236
253
  s.prepend(bootImport);
package/dist/index.js CHANGED
@@ -190,10 +190,27 @@ function boot(options = {}) {
190
190
  }
191
191
  }
192
192
  const { host, port } = getServerDefaults(astroConfig);
193
- const bootImport = `import * as __boot from './${bootChunkName}';
193
+ const bootImport = `globalThis.__astroscope_server_url = import.meta.url;
194
+ import * as __boot from './${bootChunkName}';
194
195
  const __bootContext = { dev: false, host: process.env.HOST ?? '${host}', port: process.env.PORT ? Number(process.env.PORT) : ${port} };
195
- await __boot.onStartup?.(__bootContext);
196
- if (__boot.onShutdown) process.on('SIGTERM', async () => { await __boot.onShutdown(__bootContext); process.exit(0); });
196
+ try {
197
+ await __boot.onStartup?.(__bootContext);
198
+ } catch (err) {
199
+ console.error('[boot] startup failed:', err);
200
+ try { await __boot.onShutdown?.(__bootContext); } catch {}
201
+ process.exit(1);
202
+ }
203
+ if (__boot.onShutdown) {
204
+ process.on('SIGTERM', async () => {
205
+ try {
206
+ await __boot.onShutdown(__bootContext);
207
+ process.exit(0);
208
+ } catch (err) {
209
+ console.error('[boot] shutdown failed:', err);
210
+ process.exit(1);
211
+ }
212
+ });
213
+ }
197
214
  `;
198
215
  const s = new MagicString(entryChunk.code);
199
216
  s.prepend(bootImport);
package/dist/warmup.cjs CHANGED
@@ -31,21 +31,37 @@ var WARMUP_MANIFEST_FILE = "warmup-manifest.json";
31
31
  function isDevMode() {
32
32
  return Boolean(import_meta.env?.["DEV"]);
33
33
  }
34
- function loadModules() {
34
+ function loadManifest() {
35
35
  if (isDevMode()) {
36
- return [];
36
+ return null;
37
37
  }
38
- const __dirname = (0, import_node_path.dirname)((0, import_node_url.fileURLToPath)(import_meta.url));
39
- const manifest = JSON.parse((0, import_node_fs.readFileSync)((0, import_node_path.join)(__dirname, WARMUP_MANIFEST_FILE), "utf-8"));
40
- return manifest.modules ?? [];
38
+ const serverUrl = globalThis.__astroscope_server_url;
39
+ if (!serverUrl) {
40
+ return null;
41
+ }
42
+ const serverDir = (0, import_node_path.dirname)((0, import_node_url.fileURLToPath)(serverUrl));
43
+ const manifestPath = (0, import_node_path.join)(serverDir, "chunks", WARMUP_MANIFEST_FILE);
44
+ if (!(0, import_node_fs.existsSync)(manifestPath)) {
45
+ return null;
46
+ }
47
+ const manifest = JSON.parse((0, import_node_fs.readFileSync)(manifestPath, "utf-8"));
48
+ return {
49
+ modules: manifest.modules ?? [],
50
+ serverDir
51
+ };
41
52
  }
42
53
  async function warmup() {
43
- const modules = loadModules();
44
- if (isDevMode() || modules.length === 0) {
54
+ const manifest = loadManifest();
55
+ if (!manifest || manifest.modules.length === 0) {
45
56
  return { success: [], failed: [], duration: 0 };
46
57
  }
58
+ const { modules, serverDir } = manifest;
47
59
  const start = Date.now();
48
- const results = await Promise.allSettled(modules.map((mod) => import(mod)));
60
+ const resolvedModules = modules.map((mod) => {
61
+ const absolutePath = (0, import_node_path.resolve)(serverDir, mod);
62
+ return (0, import_node_url.pathToFileURL)(absolutePath).href;
63
+ });
64
+ const results = await Promise.allSettled(resolvedModules.map((mod) => import(mod)));
49
65
  const success = [];
50
66
  const failed = [];
51
67
  for (let i = 0; i < results.length; i++) {
package/dist/warmup.d.cts CHANGED
@@ -1,10 +1,21 @@
1
1
  import { W as WarmupResult } from './types-CxpusND2.cjs';
2
2
 
3
+ declare global {
4
+ var __astroscope_server_url: string | undefined;
5
+ }
3
6
  /**
4
7
  * Warms up V8 by importing all page modules and middleware.
5
8
  *
6
9
  * In development mode, this is a no-op that returns empty results.
7
10
  * In production, reads the warmup manifest and imports all discovered modules.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { warmup } from '@astroscope/boot/warmup';
15
+ *
16
+ * const result = await warmup();
17
+ * console.log(`warmed up ${result.success.length} modules`);
18
+ * ```
8
19
  */
9
20
  declare function warmup(): Promise<WarmupResult>;
10
21
 
package/dist/warmup.d.ts CHANGED
@@ -1,10 +1,21 @@
1
1
  import { W as WarmupResult } from './types-CxpusND2.js';
2
2
 
3
+ declare global {
4
+ var __astroscope_server_url: string | undefined;
5
+ }
3
6
  /**
4
7
  * Warms up V8 by importing all page modules and middleware.
5
8
  *
6
9
  * In development mode, this is a no-op that returns empty results.
7
10
  * In production, reads the warmup manifest and imports all discovered modules.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * import { warmup } from '@astroscope/boot/warmup';
15
+ *
16
+ * const result = await warmup();
17
+ * console.log(`warmed up ${result.success.length} modules`);
18
+ * ```
8
19
  */
9
20
  declare function warmup(): Promise<WarmupResult>;
10
21
 
package/dist/warmup.js CHANGED
@@ -1,26 +1,42 @@
1
1
  // src/warmup.ts
2
- import { readFileSync } from "fs";
3
- import { dirname, join } from "path";
4
- import { fileURLToPath } from "url";
2
+ import { existsSync, readFileSync } from "fs";
3
+ import { dirname, join, resolve } from "path";
4
+ import { fileURLToPath, pathToFileURL } from "url";
5
5
  var WARMUP_MANIFEST_FILE = "warmup-manifest.json";
6
6
  function isDevMode() {
7
7
  return Boolean(import.meta.env?.["DEV"]);
8
8
  }
9
- function loadModules() {
9
+ function loadManifest() {
10
10
  if (isDevMode()) {
11
- return [];
11
+ return null;
12
12
  }
13
- const __dirname = dirname(fileURLToPath(import.meta.url));
14
- const manifest = JSON.parse(readFileSync(join(__dirname, WARMUP_MANIFEST_FILE), "utf-8"));
15
- return manifest.modules ?? [];
13
+ const serverUrl = globalThis.__astroscope_server_url;
14
+ if (!serverUrl) {
15
+ return null;
16
+ }
17
+ const serverDir = dirname(fileURLToPath(serverUrl));
18
+ const manifestPath = join(serverDir, "chunks", WARMUP_MANIFEST_FILE);
19
+ if (!existsSync(manifestPath)) {
20
+ return null;
21
+ }
22
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
23
+ return {
24
+ modules: manifest.modules ?? [],
25
+ serverDir
26
+ };
16
27
  }
17
28
  async function warmup() {
18
- const modules = loadModules();
19
- if (isDevMode() || modules.length === 0) {
29
+ const manifest = loadManifest();
30
+ if (!manifest || manifest.modules.length === 0) {
20
31
  return { success: [], failed: [], duration: 0 };
21
32
  }
33
+ const { modules, serverDir } = manifest;
22
34
  const start = Date.now();
23
- const results = await Promise.allSettled(modules.map((mod) => import(mod)));
35
+ const resolvedModules = modules.map((mod) => {
36
+ const absolutePath = resolve(serverDir, mod);
37
+ return pathToFileURL(absolutePath).href;
38
+ });
39
+ const results = await Promise.allSettled(resolvedModules.map((mod) => import(mod)));
24
40
  const success = [];
25
41
  const failed = [];
26
42
  for (let i = 0; i < results.length; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astroscope/boot",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Startup and graceful shutdown hooks for Astro SSR",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -47,7 +47,7 @@
47
47
  "lint:fix": "eslint 'src/**/*.{ts,tsx}' --fix"
48
48
  },
49
49
  "devDependencies": {
50
- "astro": "^5.16.9",
50
+ "astro": "^5.17.1",
51
51
  "tsup": "^8.5.1",
52
52
  "typescript": "^5.9.3"
53
53
  },