@astroscope/boot 0.1.0 → 0.1.1
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/README.md +2 -2
- package/dist/index.cjs +11 -13
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +11 -13
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @astroscope/boot
|
|
2
2
|
|
|
3
|
-
Run initialization and cleanup code
|
|
3
|
+
Startup and graceful shutdown hooks for Astro SSR. Run initialization code before the server starts and cleanup code when it shuts down.
|
|
4
4
|
|
|
5
5
|
## Examples
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ See the [demo/boot](../../demo/boot) directory for a working example.
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
11
11
|
```bash
|
|
12
|
-
|
|
12
|
+
npm install @astroscope/boot
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
## Usage
|
package/dist/index.cjs
CHANGED
|
@@ -61,9 +61,7 @@ function boot(options = {}) {
|
|
|
61
61
|
if (isBuild) return;
|
|
62
62
|
server.httpServer?.once("listening", async () => {
|
|
63
63
|
try {
|
|
64
|
-
const module2 = await server.ssrLoadModule(
|
|
65
|
-
`/${entry}`
|
|
66
|
-
);
|
|
64
|
+
const module2 = await server.ssrLoadModule(`/${entry}`);
|
|
67
65
|
if (module2.onStartup) {
|
|
68
66
|
await module2.onStartup();
|
|
69
67
|
}
|
|
@@ -73,9 +71,7 @@ function boot(options = {}) {
|
|
|
73
71
|
});
|
|
74
72
|
server.httpServer?.once("close", async () => {
|
|
75
73
|
try {
|
|
76
|
-
const module2 = await server.ssrLoadModule(
|
|
77
|
-
`/${entry}`
|
|
78
|
-
);
|
|
74
|
+
const module2 = await server.ssrLoadModule(`/${entry}`);
|
|
79
75
|
if (module2.onShutdown) {
|
|
80
76
|
await module2.onShutdown();
|
|
81
77
|
}
|
|
@@ -89,9 +85,7 @@ function boot(options = {}) {
|
|
|
89
85
|
logger.info("boot file changed, re-running onStartup...");
|
|
90
86
|
try {
|
|
91
87
|
server.moduleGraph.invalidateAll();
|
|
92
|
-
const module2 = await server.ssrLoadModule(
|
|
93
|
-
`/${entry}`
|
|
94
|
-
);
|
|
88
|
+
const module2 = await server.ssrLoadModule(`/${entry}`);
|
|
95
89
|
if (module2.onStartup) {
|
|
96
90
|
await module2.onStartup();
|
|
97
91
|
}
|
|
@@ -116,10 +110,14 @@ function boot(options = {}) {
|
|
|
116
110
|
}
|
|
117
111
|
},
|
|
118
112
|
writeBundle(outputOptions) {
|
|
113
|
+
if (!isSSR) return;
|
|
119
114
|
const outDir = outputOptions.dir;
|
|
120
115
|
if (!outDir || !bootChunkRef) return;
|
|
121
116
|
const entryPath = import_node_path.default.join(outDir, "entry.mjs");
|
|
122
|
-
if (!import_node_fs.default.existsSync(entryPath))
|
|
117
|
+
if (!import_node_fs.default.existsSync(entryPath)) {
|
|
118
|
+
logger.warn("entry.mjs not found - boot injection skipped");
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
123
121
|
const bootChunkName = this.getFileName(bootChunkRef);
|
|
124
122
|
if (!bootChunkName) {
|
|
125
123
|
logger.warn("boot chunk not found");
|
|
@@ -132,9 +130,9 @@ function boot(options = {}) {
|
|
|
132
130
|
);
|
|
133
131
|
}
|
|
134
132
|
let content = import_node_fs.default.readFileSync(entryPath, "utf-8");
|
|
135
|
-
const bootImport = `import
|
|
136
|
-
await onStartup?.();
|
|
137
|
-
if (onShutdown) process.on('SIGTERM', async () => { await onShutdown(); process.exit(0); });
|
|
133
|
+
const bootImport = `import * as __boot from './${bootChunkName}';
|
|
134
|
+
await __boot.onStartup?.();
|
|
135
|
+
if (__boot.onShutdown) process.on('SIGTERM', async () => { await __boot.onShutdown(); process.exit(0); });
|
|
138
136
|
`;
|
|
139
137
|
content = bootImport + content;
|
|
140
138
|
import_node_fs.default.writeFileSync(entryPath, content);
|
package/dist/index.d.cts
CHANGED
|
@@ -12,6 +12,35 @@ interface BootOptions {
|
|
|
12
12
|
*/
|
|
13
13
|
hmr?: boolean;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Astro integration for application lifecycle hooks.
|
|
17
|
+
*
|
|
18
|
+
* Runs `onStartup` and `onShutdown` functions exported from your boot file
|
|
19
|
+
* during server startup and shutdown.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // astro.config.ts
|
|
24
|
+
* import { defineConfig } from "astro/config";
|
|
25
|
+
* import boot from "@astroscope/boot";
|
|
26
|
+
*
|
|
27
|
+
* export default defineConfig({
|
|
28
|
+
* integrations: [boot()],
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // src/boot.ts
|
|
35
|
+
* export async function onStartup() {
|
|
36
|
+
* console.log("Server starting...");
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* export async function onShutdown() {
|
|
40
|
+
* console.log("Server shutting down...");
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
15
44
|
declare function boot(options?: BootOptions): AstroIntegration;
|
|
16
45
|
|
|
17
46
|
export { type BootOptions, boot as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,35 @@ interface BootOptions {
|
|
|
12
12
|
*/
|
|
13
13
|
hmr?: boolean;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Astro integration for application lifecycle hooks.
|
|
17
|
+
*
|
|
18
|
+
* Runs `onStartup` and `onShutdown` functions exported from your boot file
|
|
19
|
+
* during server startup and shutdown.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* // astro.config.ts
|
|
24
|
+
* import { defineConfig } from "astro/config";
|
|
25
|
+
* import boot from "@astroscope/boot";
|
|
26
|
+
*
|
|
27
|
+
* export default defineConfig({
|
|
28
|
+
* integrations: [boot()],
|
|
29
|
+
* });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* // src/boot.ts
|
|
35
|
+
* export async function onStartup() {
|
|
36
|
+
* console.log("Server starting...");
|
|
37
|
+
* }
|
|
38
|
+
*
|
|
39
|
+
* export async function onShutdown() {
|
|
40
|
+
* console.log("Server shutting down...");
|
|
41
|
+
* }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
15
44
|
declare function boot(options?: BootOptions): AstroIntegration;
|
|
16
45
|
|
|
17
46
|
export { type BootOptions, boot as default };
|
package/dist/index.js
CHANGED
|
@@ -27,9 +27,7 @@ function boot(options = {}) {
|
|
|
27
27
|
if (isBuild) return;
|
|
28
28
|
server.httpServer?.once("listening", async () => {
|
|
29
29
|
try {
|
|
30
|
-
const module = await server.ssrLoadModule(
|
|
31
|
-
`/${entry}`
|
|
32
|
-
);
|
|
30
|
+
const module = await server.ssrLoadModule(`/${entry}`);
|
|
33
31
|
if (module.onStartup) {
|
|
34
32
|
await module.onStartup();
|
|
35
33
|
}
|
|
@@ -39,9 +37,7 @@ function boot(options = {}) {
|
|
|
39
37
|
});
|
|
40
38
|
server.httpServer?.once("close", async () => {
|
|
41
39
|
try {
|
|
42
|
-
const module = await server.ssrLoadModule(
|
|
43
|
-
`/${entry}`
|
|
44
|
-
);
|
|
40
|
+
const module = await server.ssrLoadModule(`/${entry}`);
|
|
45
41
|
if (module.onShutdown) {
|
|
46
42
|
await module.onShutdown();
|
|
47
43
|
}
|
|
@@ -55,9 +51,7 @@ function boot(options = {}) {
|
|
|
55
51
|
logger.info("boot file changed, re-running onStartup...");
|
|
56
52
|
try {
|
|
57
53
|
server.moduleGraph.invalidateAll();
|
|
58
|
-
const module = await server.ssrLoadModule(
|
|
59
|
-
`/${entry}`
|
|
60
|
-
);
|
|
54
|
+
const module = await server.ssrLoadModule(`/${entry}`);
|
|
61
55
|
if (module.onStartup) {
|
|
62
56
|
await module.onStartup();
|
|
63
57
|
}
|
|
@@ -82,10 +76,14 @@ function boot(options = {}) {
|
|
|
82
76
|
}
|
|
83
77
|
},
|
|
84
78
|
writeBundle(outputOptions) {
|
|
79
|
+
if (!isSSR) return;
|
|
85
80
|
const outDir = outputOptions.dir;
|
|
86
81
|
if (!outDir || !bootChunkRef) return;
|
|
87
82
|
const entryPath = path.join(outDir, "entry.mjs");
|
|
88
|
-
if (!fs.existsSync(entryPath))
|
|
83
|
+
if (!fs.existsSync(entryPath)) {
|
|
84
|
+
logger.warn("entry.mjs not found - boot injection skipped");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
89
87
|
const bootChunkName = this.getFileName(bootChunkRef);
|
|
90
88
|
if (!bootChunkName) {
|
|
91
89
|
logger.warn("boot chunk not found");
|
|
@@ -98,9 +96,9 @@ function boot(options = {}) {
|
|
|
98
96
|
);
|
|
99
97
|
}
|
|
100
98
|
let content = fs.readFileSync(entryPath, "utf-8");
|
|
101
|
-
const bootImport = `import
|
|
102
|
-
await onStartup?.();
|
|
103
|
-
if (onShutdown) process.on('SIGTERM', async () => { await onShutdown(); process.exit(0); });
|
|
99
|
+
const bootImport = `import * as __boot from './${bootChunkName}';
|
|
100
|
+
await __boot.onStartup?.();
|
|
101
|
+
if (__boot.onShutdown) process.on('SIGTERM', async () => { await __boot.onShutdown(); process.exit(0); });
|
|
104
102
|
`;
|
|
105
103
|
content = bootImport + content;
|
|
106
104
|
fs.writeFileSync(entryPath, content);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astroscope/boot",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Startup and graceful shutdown hooks for Astro SSR",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|