@nerest/nerest 0.0.6 → 0.0.7

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 CHANGED
@@ -86,7 +86,17 @@ Here `react` and `react-dom` imports will be replaced with accessing the respect
86
86
 
87
87
  ### Runtime Hooks
88
88
 
89
- TODO
89
+ If the module `nerest-runtime.ts` exists in the root of the micro frontend and exports a default function, this function will be executed when the server starts, and the fastify app instance will be passed to it as its only argument. Example of `nerest-runtime.ts`:
90
+
91
+ ```typescript
92
+ import type { FastifyInstance } from 'fastify';
93
+
94
+ export default function (app: FastifyInstance) {
95
+ console.log('Hello from nerest-runtime.ts');
96
+ }
97
+ ```
98
+
99
+ This runtime hook can be used to adjust fastify settings, register additional plugins or add custom routes.
90
100
 
91
101
  ## Development
92
102
 
@@ -16,6 +16,8 @@ const preview_1 = require("./parts/preview");
16
16
  const validator_1 = require("./parts/validator");
17
17
  const swagger_1 = require("./parts/swagger");
18
18
  const k8s_probes_1 = require("./parts/k8s-probes");
19
+ const runtime_hook_1 = require("./parts/runtime-hook");
20
+ // eslint-disable-next-line max-statements
19
21
  async function runDevelopmentServer() {
20
22
  const root = process.cwd();
21
23
  // TODO: move build config into a separate file
@@ -136,6 +138,8 @@ async function runDevelopmentServer() {
136
138
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
137
139
  },
138
140
  });
141
+ // Execute runtime hook in nerest-runtime.ts if it exists
142
+ await (0, runtime_hook_1.runRuntimeHook)(app, () => viteSsr.ssrLoadModule('/nerest-runtime.ts'));
139
143
  // TODO: remove hardcoded port
140
144
  await app.listen({
141
145
  host: '0.0.0.0',
@@ -0,0 +1,2 @@
1
+ import type { FastifyInstance } from 'fastify';
2
+ export declare function runRuntimeHook(app: FastifyInstance, loader: () => Promise<Record<string, any>>): Promise<void>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runRuntimeHook = void 0;
4
+ // Load the runtime hook module and run it if it exists, passing down our
5
+ // fastify instance. This hook can be used to modify fastify settings, add
6
+ // plugins or routes on an individual app level.
7
+ async function runRuntimeHook(app, loader) {
8
+ let module;
9
+ try {
10
+ module = (await loader());
11
+ }
12
+ catch { }
13
+ if (typeof module?.default === 'function') {
14
+ // If module exists and exports a default function, execute it and
15
+ // pass down the fastify instance
16
+ try {
17
+ await module.default(app);
18
+ }
19
+ catch (e) {
20
+ console.error('Failed to execute runtime hook', e);
21
+ process.exit(1);
22
+ }
23
+ }
24
+ else if (module) {
25
+ console.error("Runtime hook found, but doesn't export default function!");
26
+ process.exit(1);
27
+ }
28
+ else {
29
+ console.log('Runtime hook not found, skipping...');
30
+ }
31
+ }
32
+ exports.runRuntimeHook = runRuntimeHook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nerest/nerest",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "React micro frontend framework",
5
5
  "homepage": "https://github.com/nerestjs/nerest#readme",
6
6
  "repository": {
@@ -52,13 +52,13 @@
52
52
  ]
53
53
  },
54
54
  "dependencies": {
55
- "@fastify/static": "^6.11.2",
55
+ "@fastify/static": "^6.12.0",
56
56
  "@fastify/swagger": "^8.12.0",
57
57
  "@fastify/swagger-ui": "^1.10.1",
58
58
  "ajv": "^8.12.0",
59
59
  "ajv-formats": "^2.1.1",
60
60
  "dotenv": "^16.3.1",
61
- "fast-uri": "^2.2.0",
61
+ "fast-uri": "^2.3.0",
62
62
  "fastify": "^4.24.3",
63
63
  "fastify-graceful-shutdown": "^3.5.1",
64
64
  "nanoid": "^3.3.6",
@@ -69,7 +69,7 @@
69
69
  "@tinkoff/eslint-config": "^1.54.4",
70
70
  "@tinkoff/eslint-config-react": "^1.54.4",
71
71
  "@tinkoff/prettier-config": "^1.52.1",
72
- "@types/react": "^18.2.29",
72
+ "@types/react": "^18.2.33",
73
73
  "@types/react-dom": "^18.2.14",
74
74
  "jest": "^29.7.0",
75
75
  "json-schema-to-typescript": "^13.1.1",
@@ -17,7 +17,9 @@ import { renderPreviewPage } from './parts/preview';
17
17
  import { validator } from './parts/validator';
18
18
  import { setupSwagger } from './parts/swagger';
19
19
  import { setupK8SProbes } from './parts/k8s-probes';
20
+ import { runRuntimeHook } from './parts/runtime-hook';
20
21
 
22
+ // eslint-disable-next-line max-statements
21
23
  export async function runDevelopmentServer() {
22
24
  const root = process.cwd();
23
25
 
@@ -171,6 +173,9 @@ export async function runDevelopmentServer() {
171
173
  },
172
174
  });
173
175
 
176
+ // Execute runtime hook in nerest-runtime.ts if it exists
177
+ await runRuntimeHook(app, () => viteSsr.ssrLoadModule('/nerest-runtime.ts'));
178
+
174
179
  // TODO: remove hardcoded port
175
180
  await app.listen({
176
181
  host: '0.0.0.0',
@@ -0,0 +1,35 @@
1
+ import type { FastifyInstance } from 'fastify';
2
+
3
+ type RuntimeHookModule = {
4
+ default: (app: FastifyInstance) => unknown;
5
+ };
6
+
7
+ // Load the runtime hook module and run it if it exists, passing down our
8
+ // fastify instance. This hook can be used to modify fastify settings, add
9
+ // plugins or routes on an individual app level.
10
+ export async function runRuntimeHook(
11
+ app: FastifyInstance,
12
+ loader: () => Promise<Record<string, any>>
13
+ ) {
14
+ let module: RuntimeHookModule | undefined;
15
+
16
+ try {
17
+ module = (await loader()) as RuntimeHookModule;
18
+ } catch {}
19
+
20
+ if (typeof module?.default === 'function') {
21
+ // If module exists and exports a default function, execute it and
22
+ // pass down the fastify instance
23
+ try {
24
+ await module.default(app);
25
+ } catch (e) {
26
+ console.error('Failed to execute runtime hook', e);
27
+ process.exit(1);
28
+ }
29
+ } else if (module) {
30
+ console.error("Runtime hook found, but doesn't export default function!");
31
+ process.exit(1);
32
+ } else {
33
+ console.log('Runtime hook not found, skipping...');
34
+ }
35
+ }
@@ -12,6 +12,7 @@ import { setupSwagger } from './parts/swagger';
12
12
  import { validator } from './parts/validator';
13
13
  import { renderPreviewPage } from './parts/preview';
14
14
  import { setupK8SProbes } from './parts/k8s-probes';
15
+ import { runRuntimeHook } from './parts/runtime-hook';
15
16
 
16
17
  // TODO: refactor to merge the similar parts between production and development server?
17
18
  async function runProductionServer() {
@@ -103,6 +104,12 @@ async function runProductionServer() {
103
104
  await setupK8SProbes(app);
104
105
  }
105
106
 
107
+ // Execute runtime hook in nerest-runtime.ts if it exists
108
+ await runRuntimeHook(app, async () => {
109
+ const glob = import.meta.glob('/nerest-runtime.ts', { eager: true });
110
+ return glob['/nerest-runtime.ts'];
111
+ });
112
+
106
113
  // TODO: remove hardcoded port
107
114
  await app.listen({
108
115
  host: '0.0.0.0',