@astroscope/boot 0.6.2 → 0.6.3

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
@@ -165,11 +165,16 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
165
165
  const p = filePath.toLowerCase();
166
166
  return ignoredSuffixes.some((suffix) => p.endsWith(suffix));
167
167
  };
168
- server.watcher.on("change", async (changedPath) => {
169
- if (shouldIgnore(changedPath)) return;
170
- const bootDeps = getBootDependencies();
171
- if (bootDeps.has(changedPath)) {
172
- logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
168
+ let running = false;
169
+ let pendingReason;
170
+ const rerunBoot = async (reason) => {
171
+ if (running) {
172
+ pendingReason = reason;
173
+ return;
174
+ }
175
+ running = true;
176
+ try {
177
+ logger.info(`${reason}, rerunning hooks...`);
173
178
  const bootContext = getBootContext2();
174
179
  try {
175
180
  const oldModule = await ssrImport(server, bootModuleId);
@@ -184,7 +189,24 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
184
189
  } catch (error) {
185
190
  logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
186
191
  }
192
+ } finally {
193
+ running = false;
194
+ }
195
+ if (pendingReason) {
196
+ const nextReason = pendingReason;
197
+ pendingReason = void 0;
198
+ await rerunBoot(nextReason);
187
199
  }
200
+ };
201
+ server.watcher.on("change", async (changedPath) => {
202
+ if (shouldIgnore(changedPath)) return;
203
+ const bootDeps = getBootDependencies();
204
+ if (bootDeps.has(changedPath)) {
205
+ await rerunBoot(`boot dependency changed: ${changedPath}`);
206
+ }
207
+ });
208
+ server.hot.on("vite:beforeFullReload", async () => {
209
+ await rerunBoot("full reload detected");
188
210
  });
189
211
  }
190
212
 
package/dist/index.js CHANGED
@@ -102,11 +102,16 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
102
102
  const p = filePath.toLowerCase();
103
103
  return ignoredSuffixes.some((suffix) => p.endsWith(suffix));
104
104
  };
105
- server.watcher.on("change", async (changedPath) => {
106
- if (shouldIgnore(changedPath)) return;
107
- const bootDeps = getBootDependencies();
108
- if (bootDeps.has(changedPath)) {
109
- logger.info(`boot dependency changed: ${changedPath}, rerunning hooks...`);
105
+ let running = false;
106
+ let pendingReason;
107
+ const rerunBoot = async (reason) => {
108
+ if (running) {
109
+ pendingReason = reason;
110
+ return;
111
+ }
112
+ running = true;
113
+ try {
114
+ logger.info(`${reason}, rerunning hooks...`);
110
115
  const bootContext = getBootContext2();
111
116
  try {
112
117
  const oldModule = await ssrImport(server, bootModuleId);
@@ -121,7 +126,24 @@ function setupBootHmr(server, entry, logger, getBootContext2) {
121
126
  } catch (error) {
122
127
  logger.error(`Error during boot HMR startup: ${serializeError(error)}`);
123
128
  }
129
+ } finally {
130
+ running = false;
131
+ }
132
+ if (pendingReason) {
133
+ const nextReason = pendingReason;
134
+ pendingReason = void 0;
135
+ await rerunBoot(nextReason);
124
136
  }
137
+ };
138
+ server.watcher.on("change", async (changedPath) => {
139
+ if (shouldIgnore(changedPath)) return;
140
+ const bootDeps = getBootDependencies();
141
+ if (bootDeps.has(changedPath)) {
142
+ await rerunBoot(`boot dependency changed: ${changedPath}`);
143
+ }
144
+ });
145
+ server.hot.on("vite:beforeFullReload", async () => {
146
+ await rerunBoot("full reload detected");
125
147
  });
126
148
  }
127
149
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astroscope/boot",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "Startup and graceful shutdown hooks for Astro SSR",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,57 +0,0 @@
1
- // src/warmup.ts
2
- import { readdirSync } from "fs";
3
- import { join, resolve } from "path";
4
- import { pathToFileURL } from "url";
5
- var SKIP_PATTERNS = [/^noop-/, /server-island/, /session-driver/];
6
- function setServerDir(dir) {
7
- globalThis.__astroscope_boot_server_dir = dir;
8
- }
9
- function isDevMode() {
10
- return Boolean(import.meta.env?.["DEV"]);
11
- }
12
- function discoverChunks(chunksDir) {
13
- let entries;
14
- try {
15
- entries = readdirSync(chunksDir);
16
- } catch {
17
- return [];
18
- }
19
- return entries.filter((name) => {
20
- if (!name.endsWith(".mjs")) return false;
21
- return !SKIP_PATTERNS.some((pattern) => pattern.test(name));
22
- });
23
- }
24
- async function warmup() {
25
- const dir = globalThis.__astroscope_boot_server_dir;
26
- if (isDevMode() || !dir) {
27
- return { success: [], failed: [], duration: 0 };
28
- }
29
- const chunksDir = join(dir, "chunks");
30
- const chunks = discoverChunks(chunksDir);
31
- if (chunks.length === 0) {
32
- return { success: [], failed: [], duration: 0 };
33
- }
34
- const start = Date.now();
35
- const resolvedModules = chunks.map((name) => pathToFileURL(resolve(chunksDir, name)).href);
36
- const results = await Promise.allSettled(resolvedModules.map((mod) => import(
37
- /* @vite-ignore */
38
- mod
39
- )));
40
- const success = [];
41
- const failed = [];
42
- for (let i = 0; i < results.length; i++) {
43
- const result = results[i];
44
- if (result.status === "fulfilled") {
45
- success.push(chunks[i]);
46
- } else {
47
- const reason = result.reason instanceof Error ? result.reason.message : String(result.reason);
48
- failed.push(`${chunks[i]} (${reason})`);
49
- }
50
- }
51
- return { success, failed, duration: Date.now() - start };
52
- }
53
-
54
- export {
55
- setServerDir,
56
- warmup
57
- };
@@ -1,38 +0,0 @@
1
- // src/events.ts
2
- var STORE_KEY = /* @__PURE__ */ Symbol.for("@astroscope/boot/events");
3
- function getStore() {
4
- const existing = globalThis[STORE_KEY];
5
- if (existing) return existing;
6
- const store = {
7
- listeners: /* @__PURE__ */ new Map()
8
- };
9
- globalThis[STORE_KEY] = store;
10
- return store;
11
- }
12
- function on(event, handler) {
13
- const store = getStore();
14
- let handlers = store.listeners.get(event);
15
- if (!handlers) {
16
- handlers = /* @__PURE__ */ new Set();
17
- store.listeners.set(event, handlers);
18
- }
19
- handlers.add(handler);
20
- }
21
- function off(event, handler) {
22
- const store = getStore();
23
- store.listeners.get(event)?.delete(handler);
24
- }
25
- async function emit(event, context) {
26
- const store = getStore();
27
- const handlers = store.listeners.get(event);
28
- if (!handlers) return;
29
- for (const handler of handlers) {
30
- await handler(context);
31
- }
32
- }
33
-
34
- export {
35
- on,
36
- off,
37
- emit
38
- };
@@ -1,57 +0,0 @@
1
- // src/warmup.ts
2
- import { readdirSync } from "fs";
3
- import { join, resolve } from "path";
4
- import { pathToFileURL } from "url";
5
- var SKIP_PATTERNS = [/^noop-/, /server-island/, /session-driver/];
6
- var serverDir;
7
- function setServerDir(dir) {
8
- serverDir = dir;
9
- }
10
- function isDevMode() {
11
- return Boolean(import.meta.env?.["DEV"]);
12
- }
13
- function discoverChunks(chunksDir) {
14
- let entries;
15
- try {
16
- entries = readdirSync(chunksDir);
17
- } catch {
18
- return [];
19
- }
20
- return entries.filter((name) => {
21
- if (!name.endsWith(".mjs")) return false;
22
- return !SKIP_PATTERNS.some((pattern) => pattern.test(name));
23
- });
24
- }
25
- async function warmup() {
26
- if (isDevMode() || !serverDir) {
27
- return { success: [], failed: [], duration: 0 };
28
- }
29
- const chunksDir = join(serverDir, "chunks");
30
- const chunks = discoverChunks(chunksDir);
31
- if (chunks.length === 0) {
32
- return { success: [], failed: [], duration: 0 };
33
- }
34
- const start = Date.now();
35
- const resolvedModules = chunks.map((name) => pathToFileURL(resolve(chunksDir, name)).href);
36
- const results = await Promise.allSettled(resolvedModules.map((mod) => import(
37
- /* @vite-ignore */
38
- mod
39
- )));
40
- const success = [];
41
- const failed = [];
42
- for (let i = 0; i < results.length; i++) {
43
- const result = results[i];
44
- if (result.status === "fulfilled") {
45
- success.push(chunks[i]);
46
- } else {
47
- const reason = result.reason instanceof Error ? result.reason.message : String(result.reason);
48
- failed.push(`${chunks[i]} (${reason})`);
49
- }
50
- }
51
- return { success, failed, duration: Date.now() - start };
52
- }
53
-
54
- export {
55
- setServerDir,
56
- warmup
57
- };
@@ -1,57 +0,0 @@
1
- // src/warmup.ts
2
- import { existsSync, readFileSync } from "fs";
3
- import { dirname, join, resolve } from "path";
4
- import { fileURLToPath, pathToFileURL } from "url";
5
- var WARMUP_MANIFEST_FILE = "warmup-manifest.json";
6
- function isDevMode() {
7
- return Boolean(import.meta.env?.["DEV"]);
8
- }
9
- function loadManifest() {
10
- if (isDevMode()) {
11
- return null;
12
- }
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
- };
27
- }
28
- async function warmup() {
29
- const manifest = loadManifest();
30
- if (!manifest || manifest.modules.length === 0) {
31
- return { success: [], failed: [], duration: 0 };
32
- }
33
- const { modules, serverDir } = manifest;
34
- const start = Date.now();
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(
40
- /* @vite-ignore */
41
- mod
42
- )));
43
- const success = [];
44
- const failed = [];
45
- for (let i = 0; i < results.length; i++) {
46
- if (results[i].status === "fulfilled") {
47
- success.push(modules[i]);
48
- } else {
49
- failed.push(modules[i]);
50
- }
51
- }
52
- return { success, failed, duration: Date.now() - start };
53
- }
54
-
55
- export {
56
- warmup
57
- };
@@ -1,45 +0,0 @@
1
- // src/lifecycle.ts
2
- var STATE_KEY = /* @__PURE__ */ Symbol.for("@astroscope/boot/lifecycle");
3
- function getState() {
4
- const existing = globalThis[STATE_KEY];
5
- if (existing) {
6
- return existing;
7
- }
8
- const state = {
9
- listeners: /* @__PURE__ */ new Map()
10
- };
11
- globalThis[STATE_KEY] = state;
12
- return state;
13
- }
14
- function onLifecycle(event, listener) {
15
- const state = getState();
16
- let listeners = state.listeners.get(event);
17
- if (!listeners) {
18
- listeners = /* @__PURE__ */ new Set();
19
- state.listeners.set(event, listeners);
20
- }
21
- listeners.add(listener);
22
- return () => {
23
- listeners.delete(listener);
24
- };
25
- }
26
- async function emitLifecycle(event) {
27
- const state = getState();
28
- const listeners = state.listeners.get(event);
29
- if (!listeners || listeners.size === 0) {
30
- return;
31
- }
32
- const promises = [];
33
- for (const listener of listeners) {
34
- const result = listener();
35
- if (result instanceof Promise) {
36
- promises.push(result);
37
- }
38
- }
39
- await Promise.all(promises);
40
- }
41
-
42
- export {
43
- onLifecycle,
44
- emitLifecycle
45
- };
@@ -1,16 +0,0 @@
1
- interface BootContext {
2
- /** Whether running in development mode (vite dev server) */
3
- dev: boolean;
4
- /** Server host from Astro config */
5
- host: string;
6
- /** Server port from Astro config */
7
- port: number;
8
- }
9
- interface WarmupResult {
10
- /** Modules that were loaded (page paths + middleware) */
11
- modules: string[];
12
- /** Time taken in milliseconds */
13
- duration: number;
14
- }
15
-
16
- export type { BootContext as B, WarmupResult as W };
@@ -1,16 +0,0 @@
1
- interface BootContext {
2
- /** Whether running in development mode (vite dev server) */
3
- dev: boolean;
4
- /** Server host from Astro config */
5
- host: string;
6
- /** Server port from Astro config */
7
- port: number;
8
- }
9
- interface WarmupResult {
10
- /** Modules that were loaded (page paths + middleware) */
11
- modules: string[];
12
- /** Time taken in milliseconds */
13
- duration: number;
14
- }
15
-
16
- export type { BootContext as B, WarmupResult as W };
@@ -1,18 +0,0 @@
1
- interface BootContext {
2
- /** Whether running in development mode (vite dev server) */
3
- dev: boolean;
4
- /** Server host from Astro config */
5
- host: string;
6
- /** Server port from Astro config */
7
- port: number;
8
- }
9
- interface WarmupResult {
10
- /** Modules that were successfully loaded */
11
- success: string[];
12
- /** Modules that failed to load */
13
- failed: string[];
14
- /** Time taken in milliseconds */
15
- duration: number;
16
- }
17
-
18
- export type { BootContext as B, WarmupResult as W };
@@ -1,18 +0,0 @@
1
- interface BootContext {
2
- /** Whether running in development mode (vite dev server) */
3
- dev: boolean;
4
- /** Server host from Astro config */
5
- host: string;
6
- /** Server port from Astro config */
7
- port: number;
8
- }
9
- interface WarmupResult {
10
- /** Modules that were successfully loaded */
11
- success: string[];
12
- /** Modules that failed to load */
13
- failed: string[];
14
- /** Time taken in milliseconds */
15
- duration: number;
16
- }
17
-
18
- export type { BootContext as B, WarmupResult as W };
package/dist/warmup.cjs DELETED
@@ -1,83 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/warmup.ts
21
- var warmup_exports = {};
22
- __export(warmup_exports, {
23
- setServerDir: () => setServerDir,
24
- warmup: () => warmup
25
- });
26
- module.exports = __toCommonJS(warmup_exports);
27
- var import_node_fs = require("fs");
28
- var import_node_path = require("path");
29
- var import_node_url = require("url");
30
- var import_meta = {};
31
- var SKIP_PATTERNS = [/^noop-/, /server-island/, /session-driver/];
32
- function setServerDir(dir) {
33
- globalThis.__astroscope_boot_server_dir = dir;
34
- }
35
- function isDevMode() {
36
- return Boolean(import_meta.env?.["DEV"]);
37
- }
38
- function discoverChunks(chunksDir) {
39
- let entries;
40
- try {
41
- entries = (0, import_node_fs.readdirSync)(chunksDir);
42
- } catch {
43
- return [];
44
- }
45
- return entries.filter((name) => {
46
- if (!name.endsWith(".mjs")) return false;
47
- return !SKIP_PATTERNS.some((pattern) => pattern.test(name));
48
- });
49
- }
50
- async function warmup() {
51
- const dir = globalThis.__astroscope_boot_server_dir;
52
- if (isDevMode() || !dir) {
53
- return { success: [], failed: [], duration: 0 };
54
- }
55
- const chunksDir = (0, import_node_path.join)(dir, "chunks");
56
- const chunks = discoverChunks(chunksDir);
57
- if (chunks.length === 0) {
58
- return { success: [], failed: [], duration: 0 };
59
- }
60
- const start = Date.now();
61
- const resolvedModules = chunks.map((name) => (0, import_node_url.pathToFileURL)((0, import_node_path.resolve)(chunksDir, name)).href);
62
- const results = await Promise.allSettled(resolvedModules.map((mod) => import(
63
- /* @vite-ignore */
64
- mod
65
- )));
66
- const success = [];
67
- const failed = [];
68
- for (let i = 0; i < results.length; i++) {
69
- const result = results[i];
70
- if (result.status === "fulfilled") {
71
- success.push(chunks[i]);
72
- } else {
73
- const reason = result.reason instanceof Error ? result.reason.message : String(result.reason);
74
- failed.push(`${chunks[i]} (${reason})`);
75
- }
76
- }
77
- return { success, failed, duration: Date.now() - start };
78
- }
79
- // Annotate the CommonJS export names for ESM import in node:
80
- 0 && (module.exports = {
81
- setServerDir,
82
- warmup
83
- });
package/dist/warmup.d.cts DELETED
@@ -1,28 +0,0 @@
1
- import { W as WarmupResult } from './types-CxpusND2.cjs';
2
-
3
- declare global {
4
- var __astroscope_boot_server_dir: string | undefined;
5
- }
6
- /**
7
- * Set the server directory for warmup module discovery.
8
- * Called internally by `setup()` — users do not need to call this.
9
- */
10
- declare function setServerDir(dir: string): void;
11
- /**
12
- * Warms up V8 by importing all server chunks.
13
- *
14
- * In development mode, this is a no-op that returns empty results.
15
- * In production, discovers all `.mjs` chunks in the server output directory
16
- * and imports them to eliminate cold-start latency on first request.
17
- *
18
- * @example
19
- * ```ts
20
- * import { warmup } from '@astroscope/boot/warmup';
21
- *
22
- * const result = await warmup();
23
- * console.log(`warmed up ${result.success.length} modules`);
24
- * ```
25
- */
26
- declare function warmup(): Promise<WarmupResult>;
27
-
28
- export { WarmupResult, setServerDir, warmup };
package/dist/warmup.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import { W as WarmupResult } from './types-CxpusND2.js';
2
-
3
- declare global {
4
- var __astroscope_boot_server_dir: string | undefined;
5
- }
6
- /**
7
- * Set the server directory for warmup module discovery.
8
- * Called internally by `setup()` — users do not need to call this.
9
- */
10
- declare function setServerDir(dir: string): void;
11
- /**
12
- * Warms up V8 by importing all server chunks.
13
- *
14
- * In development mode, this is a no-op that returns empty results.
15
- * In production, discovers all `.mjs` chunks in the server output directory
16
- * and imports them to eliminate cold-start latency on first request.
17
- *
18
- * @example
19
- * ```ts
20
- * import { warmup } from '@astroscope/boot/warmup';
21
- *
22
- * const result = await warmup();
23
- * console.log(`warmed up ${result.success.length} modules`);
24
- * ```
25
- */
26
- declare function warmup(): Promise<WarmupResult>;
27
-
28
- export { WarmupResult, setServerDir, warmup };
package/dist/warmup.js DELETED
@@ -1,8 +0,0 @@
1
- import {
2
- setServerDir,
3
- warmup
4
- } from "./chunk-AL4QFQEW.js";
5
- export {
6
- setServerDir,
7
- warmup
8
- };