@facilis.dev/nitro 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +18 -2
- package/dist/config.d.ts +30 -0
- package/dist/config.js +112 -0
- package/dist/constants.d.ts +24 -0
- package/dist/constants.js +24 -0
- package/dist/facade.d.ts +37 -0
- package/dist/facade.js +121 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +14 -0
- package/dist/middleware/context.d.ts +5 -0
- package/dist/middleware/context.js +86 -0
- package/dist/middleware/cors.d.ts +10 -0
- package/dist/middleware/cors.js +34 -0
- package/dist/middleware/session.d.ts +9 -0
- package/dist/middleware/session.js +18 -0
- package/dist/module.d.ts +7 -0
- package/dist/module.js +234 -0
- package/dist/rolldown-parser.d.ts +3 -0
- package/dist/rolldown-parser.js +66 -0
- package/dist/route-metadata.d.ts +22 -0
- package/dist/route-metadata.js +161 -0
- package/dist/routes/dev-query.post.d.ts +10 -0
- package/dist/routes/dev-query.post.js +49 -0
- package/dist/routes/dev-schema.get.d.ts +9 -0
- package/dist/routes/dev-schema.get.js +34 -0
- package/dist/routes/dev-view.post.d.ts +11 -0
- package/dist/routes/dev-view.post.js +61 -0
- package/dist/routes/health.get.d.ts +8 -0
- package/dist/routes/health.get.js +18 -0
- package/dist/routes/manifest-revision.get.d.ts +8 -0
- package/dist/routes/manifest-revision.get.js +26 -0
- package/dist/routes/manifest.get.d.ts +8 -0
- package/dist/routes/manifest.get.js +68 -0
- package/dist/routes/openapi.get.d.ts +8 -0
- package/dist/routes/openapi.get.js +29 -0
- package/dist/routes/sso.d.ts +2 -0
- package/dist/routes/sso.js +216 -0
- package/dist/routes/token.post.d.ts +10 -0
- package/dist/routes/token.post.js +75 -0
- package/dist/runtime/boot-runtimes.d.ts +21 -0
- package/dist/runtime/boot-runtimes.js +38 -0
- package/dist/runtime/bootstrap.d.ts +47 -0
- package/dist/runtime/bootstrap.js +214 -0
- package/dist/runtime/config.d.ts +27 -0
- package/dist/runtime/config.js +44 -0
- package/dist/runtime/cors-policy.d.ts +16 -0
- package/dist/runtime/cors-policy.js +88 -0
- package/dist/runtime/db-bootstrap.d.ts +9 -0
- package/dist/runtime/db-bootstrap.js +87 -0
- package/dist/runtime/db-target.d.ts +21 -0
- package/dist/runtime/db-target.js +52 -0
- package/dist/runtime/dev-db-persistence.d.ts +39 -0
- package/dist/runtime/dev-db-persistence.js +124 -0
- package/dist/runtime/dev-query.d.ts +38 -0
- package/dist/runtime/dev-query.js +87 -0
- package/dist/runtime/env.d.ts +9 -0
- package/dist/runtime/env.js +14 -0
- package/dist/runtime/http-guard.d.ts +16 -0
- package/dist/runtime/http-guard.js +42 -0
- package/dist/runtime/http.d.ts +11 -0
- package/dist/runtime/http.js +18 -0
- package/dist/runtime/jobs/boot.d.ts +20 -0
- package/dist/runtime/jobs/boot.js +77 -0
- package/dist/runtime/jobs/contracts.d.ts +161 -0
- package/dist/runtime/jobs/contracts.js +1 -0
- package/dist/runtime/jobs/db-store.d.ts +21 -0
- package/dist/runtime/jobs/db-store.js +210 -0
- package/dist/runtime/jobs/definitions.d.ts +14 -0
- package/dist/runtime/jobs/definitions.js +31 -0
- package/dist/runtime/jobs/engine-support.d.ts +22 -0
- package/dist/runtime/jobs/engine-support.js +32 -0
- package/dist/runtime/jobs/engine.d.ts +6 -0
- package/dist/runtime/jobs/engine.js +261 -0
- package/dist/runtime/jobs/executor.d.ts +11 -0
- package/dist/runtime/jobs/executor.js +27 -0
- package/dist/runtime/jobs/memory-store.d.ts +7 -0
- package/dist/runtime/jobs/memory-store.js +183 -0
- package/dist/runtime/jobs/row-mapping.d.ts +28 -0
- package/dist/runtime/jobs/row-mapping.js +61 -0
- package/dist/runtime/jobs/runtime.d.ts +24 -0
- package/dist/runtime/jobs/runtime.js +86 -0
- package/dist/runtime/logging/audit-chain.d.ts +54 -0
- package/dist/runtime/logging/audit-chain.js +102 -0
- package/dist/runtime/logging/bootstrap-logging.d.ts +28 -0
- package/dist/runtime/logging/bootstrap-logging.js +26 -0
- package/dist/runtime/logging/entry.d.ts +14 -0
- package/dist/runtime/logging/entry.js +85 -0
- package/dist/runtime/logging/file-sink.d.ts +24 -0
- package/dist/runtime/logging/file-sink.js +128 -0
- package/dist/runtime/logging/log-settings.d.ts +52 -0
- package/dist/runtime/logging/log-settings.js +93 -0
- package/dist/runtime/logging/log-sink.d.ts +18 -0
- package/dist/runtime/logging/log-sink.js +22 -0
- package/dist/runtime/logging/redact.d.ts +31 -0
- package/dist/runtime/logging/redact.js +106 -0
- package/dist/runtime/logging/runtime.d.ts +25 -0
- package/dist/runtime/logging/runtime.js +114 -0
- package/dist/runtime/logging/sudo-bridge.d.ts +13 -0
- package/dist/runtime/logging/sudo-bridge.js +38 -0
- package/dist/runtime/manifest-data.d.ts +28 -0
- package/dist/runtime/manifest-data.js +70 -0
- package/dist/runtime/manifest-response.d.ts +45 -0
- package/dist/runtime/manifest-response.js +50 -0
- package/dist/runtime/manifest-sections.d.ts +20 -0
- package/dist/runtime/manifest-sections.js +59 -0
- package/dist/runtime/manifest-service.d.ts +16 -0
- package/dist/runtime/manifest-service.js +66 -0
- package/dist/runtime/project-config.d.ts +8 -0
- package/dist/runtime/project-config.js +22 -0
- package/dist/runtime/project-files.d.ts +12 -0
- package/dist/runtime/project-files.js +99 -0
- package/dist/runtime/rate-limit.d.ts +6 -0
- package/dist/runtime/rate-limit.js +6 -0
- package/dist/runtime/revision-service.d.ts +15 -0
- package/dist/runtime/revision-service.js +26 -0
- package/dist/runtime/sql.d.ts +8 -0
- package/dist/runtime/sql.js +23 -0
- package/dist/runtime/sso.d.ts +40 -0
- package/dist/runtime/sso.js +98 -0
- package/dist/runtime/state.d.ts +7 -0
- package/dist/runtime/state.js +7 -0
- package/dist/runtime/storage-keys.d.ts +9 -0
- package/dist/runtime/storage-keys.js +82 -0
- package/dist/runtime/storage-target.d.ts +25 -0
- package/dist/runtime/storage-target.js +155 -0
- package/dist/runtime/system-role.d.ts +9 -0
- package/dist/runtime/system-role.js +21 -0
- package/dist/runtime/tasks/boot.d.ts +20 -0
- package/dist/runtime/tasks/boot.js +32 -0
- package/dist/runtime/tasks/contracts.d.ts +70 -0
- package/dist/runtime/tasks/contracts.js +1 -0
- package/dist/runtime/tasks/db-store.d.ts +18 -0
- package/dist/runtime/tasks/db-store.js +139 -0
- package/dist/runtime/tasks/row-mapping.d.ts +11 -0
- package/dist/runtime/tasks/row-mapping.js +69 -0
- package/dist/runtime/tasks/runtime.d.ts +7 -0
- package/dist/runtime/tasks/runtime.js +162 -0
- package/dist/runtime/tasks/tasks-test-harness.d.ts +16 -0
- package/dist/runtime/tasks/tasks-test-harness.js +70 -0
- package/dist/runtime/tenant.d.ts +10 -0
- package/dist/runtime/tenant.js +13 -0
- package/dist/runtime/workflows/boot.d.ts +25 -0
- package/dist/runtime/workflows/boot.js +75 -0
- package/dist/runtime/workflows/contracts.d.ts +96 -0
- package/dist/runtime/workflows/contracts.js +1 -0
- package/dist/runtime/workflows/db-store.d.ts +18 -0
- package/dist/runtime/workflows/db-store.js +128 -0
- package/dist/runtime/workflows/definitions.d.ts +14 -0
- package/dist/runtime/workflows/definitions.js +50 -0
- package/dist/runtime/workflows/dispatch.d.ts +4 -0
- package/dist/runtime/workflows/dispatch.js +119 -0
- package/dist/runtime/workflows/row-mapping.d.ts +5 -0
- package/dist/runtime/workflows/row-mapping.js +38 -0
- package/dist/runtime/workflows/runner.d.ts +15 -0
- package/dist/runtime/workflows/runner.js +89 -0
- package/dist/runtime/workflows/runtime.d.ts +14 -0
- package/dist/runtime/workflows/runtime.js +129 -0
- package/dist/runtime/workflows/steps.d.ts +14 -0
- package/dist/runtime/workflows/steps.js +49 -0
- package/dist/runtime/workflows/support.d.ts +20 -0
- package/dist/runtime/workflows/support.js +36 -0
- package/dist/runtime/workflows/transitions.d.ts +8 -0
- package/dist/runtime/workflows/transitions.js +184 -0
- package/dist/runtime/workflows/workflows-test-harness.d.ts +62 -0
- package/dist/runtime/workflows/workflows-test-harness.js +178 -0
- package/dist/session-cookie.d.ts +13 -0
- package/dist/session-cookie.js +48 -0
- package/dist/storage.d.ts +50 -0
- package/dist/storage.js +118 -0
- package/dist/types.d.ts +44 -0
- package/dist/types.js +1 -0
- package/dist/virtual-ids.d.ts +17 -0
- package/dist/virtual-ids.js +17 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.js +135 -0
- package/package.json +61 -4
- package/src/config.ts +120 -0
- package/src/constants.ts +24 -0
- package/src/facade.ts +153 -0
- package/src/index.ts +44 -0
- package/src/middleware/context.ts +106 -0
- package/src/middleware/cors.ts +42 -0
- package/src/middleware/session.ts +23 -0
- package/src/module.ts +278 -0
- package/src/rolldown-parser.ts +79 -0
- package/src/route-metadata-test-harness.ts +62 -0
- package/src/route-metadata.ts +194 -0
- package/src/routes/dev-query.post.ts +53 -0
- package/src/routes/dev-schema.get.ts +43 -0
- package/src/routes/dev-view.post.ts +66 -0
- package/src/routes/health.get.ts +26 -0
- package/src/routes/manifest-revision.get.ts +38 -0
- package/src/routes/manifest.get.ts +111 -0
- package/src/routes/openapi.get.ts +34 -0
- package/src/routes/sso.ts +247 -0
- package/src/routes/token.post.ts +89 -0
- package/src/runtime/boot-runtimes.ts +69 -0
- package/src/runtime/bootstrap.ts +300 -0
- package/src/runtime/config.ts +60 -0
- package/src/runtime/cors-policy.ts +107 -0
- package/src/runtime/db-bootstrap.ts +99 -0
- package/src/runtime/db-target.ts +87 -0
- package/src/runtime/dev-db-persistence.ts +146 -0
- package/src/runtime/dev-query.ts +124 -0
- package/src/runtime/env.ts +18 -0
- package/src/runtime/http-guard.ts +50 -0
- package/src/runtime/http.ts +30 -0
- package/src/runtime/jobs/boot.ts +97 -0
- package/src/runtime/jobs/contracts.ts +165 -0
- package/src/runtime/jobs/db-store-test-harness.ts +98 -0
- package/src/runtime/jobs/db-store.ts +273 -0
- package/src/runtime/jobs/definitions.ts +42 -0
- package/src/runtime/jobs/engine-support.ts +44 -0
- package/src/runtime/jobs/engine.ts +296 -0
- package/src/runtime/jobs/executor.ts +37 -0
- package/src/runtime/jobs/memory-store.ts +222 -0
- package/src/runtime/jobs/row-mapping.ts +67 -0
- package/src/runtime/jobs/runtime.ts +110 -0
- package/src/runtime/logging/audit-chain.ts +135 -0
- package/src/runtime/logging/bootstrap-logging.ts +62 -0
- package/src/runtime/logging/entry.ts +99 -0
- package/src/runtime/logging/file-sink.ts +157 -0
- package/src/runtime/logging/log-settings.ts +166 -0
- package/src/runtime/logging/log-sink.ts +31 -0
- package/src/runtime/logging/redact.ts +108 -0
- package/src/runtime/logging/runtime.ts +163 -0
- package/src/runtime/logging/sudo-bridge.ts +41 -0
- package/src/runtime/manifest-data.ts +103 -0
- package/src/runtime/manifest-response.ts +95 -0
- package/src/runtime/manifest-sections.ts +79 -0
- package/src/runtime/manifest-service.ts +83 -0
- package/src/runtime/project-config.ts +25 -0
- package/src/runtime/project-files.ts +103 -0
- package/src/runtime/rate-limit.ts +12 -0
- package/src/runtime/revision-service.ts +38 -0
- package/src/runtime/sql.ts +24 -0
- package/src/runtime/sso.ts +153 -0
- package/src/runtime/state.ts +7 -0
- package/src/runtime/storage-keys.ts +95 -0
- package/src/runtime/storage-target.ts +238 -0
- package/src/runtime/system-role.ts +26 -0
- package/src/runtime/tasks/boot.ts +49 -0
- package/src/runtime/tasks/contracts.ts +77 -0
- package/src/runtime/tasks/db-store.ts +191 -0
- package/src/runtime/tasks/row-mapping.ts +75 -0
- package/src/runtime/tasks/runtime.ts +192 -0
- package/src/runtime/tasks/tasks-test-harness.ts +92 -0
- package/src/runtime/tenant.ts +15 -0
- package/src/runtime/workflows/boot.ts +97 -0
- package/src/runtime/workflows/contracts.ts +114 -0
- package/src/runtime/workflows/db-store.ts +156 -0
- package/src/runtime/workflows/definitions.ts +75 -0
- package/src/runtime/workflows/dispatch.ts +148 -0
- package/src/runtime/workflows/row-mapping.ts +54 -0
- package/src/runtime/workflows/runner.ts +105 -0
- package/src/runtime/workflows/runtime.ts +161 -0
- package/src/runtime/workflows/steps.ts +74 -0
- package/src/runtime/workflows/support.ts +60 -0
- package/src/runtime/workflows/transitions.ts +259 -0
- package/src/runtime/workflows/workflows-test-harness.ts +238 -0
- package/src/session-cookie.ts +69 -0
- package/src/storage.ts +173 -0
- package/src/types.ts +51 -0
- package/src/virtual-ids.ts +18 -0
- package/src/virtual-modules.d.ts +53 -0
- package/src/vite.ts +175 -0
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@facilis.dev/nitro",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
6
|
-
|
|
3
|
+
"version": "0.1.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Facilis Nitro module and its Vite plugin wrapper.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./dist/index.js",
|
|
9
|
+
"./module": "./dist/module.js",
|
|
10
|
+
"./session": "./dist/session-cookie.js",
|
|
11
|
+
"./storage": "./dist/storage.js",
|
|
12
|
+
"./vite": "./dist/vite.js",
|
|
13
|
+
"./package.json": "./package.json"
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22.15.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"nitro": "3.0.260903-beta",
|
|
26
|
+
"vite": "8.3.5"
|
|
27
|
+
},
|
|
28
|
+
"peerDependenciesMeta": {
|
|
29
|
+
"vite": {
|
|
30
|
+
"optional": true
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@facilis.dev/analyze": "0.1.2",
|
|
35
|
+
"@facilis.dev/auth": "0.1.2",
|
|
36
|
+
"@facilis.dev/core": "0.1.2",
|
|
37
|
+
"@facilis.dev/db": "0.1.2",
|
|
38
|
+
"@facilis.dev/storage": "0.1.2",
|
|
39
|
+
"drizzle-orm": "0.45.4",
|
|
40
|
+
"zod": "4.6.7"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "git+https://github.com/facilis-dev/facilis.git",
|
|
48
|
+
"directory": "packages/nitro"
|
|
49
|
+
},
|
|
50
|
+
"homepage": "https://github.com/facilis-dev/facilis/tree/main/packages/nitro#readme",
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/facilis-dev/facilis/issues"
|
|
53
|
+
},
|
|
54
|
+
"keywords": [
|
|
55
|
+
"facilis",
|
|
56
|
+
"framework",
|
|
57
|
+
"vite",
|
|
58
|
+
"nitro",
|
|
59
|
+
"low-code"
|
|
60
|
+
],
|
|
61
|
+
"main": "./dist/index.js",
|
|
62
|
+
"types": "./dist/index.d.ts"
|
|
63
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
// The /config subpath keeps the Vite config phase from loading @facilis.dev/core's entry, which
|
|
5
|
+
// re-exports the route/db runtime: the plugin only needs the schema and its types here.
|
|
6
|
+
import {
|
|
7
|
+
facilisConfigSchema,
|
|
8
|
+
type FacilisConfig,
|
|
9
|
+
type FacilisNitroOptions,
|
|
10
|
+
} from "@facilis.dev/core/config";
|
|
11
|
+
import type { NitroConfig } from "nitro/types";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The project config file the Vite plugin reads (SPEC §2.3, D-034).
|
|
15
|
+
*
|
|
16
|
+
* A Facilis project commits ONE config: Nitro options live in its `nitro` section, so
|
|
17
|
+
* there is no `nitro.config.ts` and the project's `vite.config.ts` never names Nitro.
|
|
18
|
+
*/
|
|
19
|
+
export const PROJECT_CONFIG_FILE = "facilis.config.ts";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Facilis defaults for Nitro (SPEC §4.1/§4.2).
|
|
23
|
+
*
|
|
24
|
+
* The framework owns these two: the server directory is always `./server` and the native
|
|
25
|
+
* OpenAPI document is always generated (the `/_openapi.json` facade augments it). A
|
|
26
|
+
* project only declares what it changes, in `facilis.config.ts`'s `nitro` section.
|
|
27
|
+
*/
|
|
28
|
+
// `satisfies` keeps this list honest against Nitro's own option type, while the exported
|
|
29
|
+
// value stays an indexable record for the merge.
|
|
30
|
+
const FACILIS_NITRO_DEFAULTS = {
|
|
31
|
+
serverDir: "./server",
|
|
32
|
+
experimental: { openAPI: true },
|
|
33
|
+
} as const satisfies NitroConfig;
|
|
34
|
+
|
|
35
|
+
/** The defaults as the merge sees them; tests assert against this value. */
|
|
36
|
+
export const DEFAULT_NITRO_OPTIONS: Readonly<Record<string, unknown>> = FACILIS_NITRO_DEFAULTS;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Merge the project's `nitro` section over the Facilis defaults.
|
|
40
|
+
*
|
|
41
|
+
* Deep for plain objects so `nitro.experimental.openAPI: false` only flips that switch
|
|
42
|
+
* instead of replacing the whole `experimental` block; arrays are Nitro lists authored
|
|
43
|
+
* whole, so they replace rather than concatenate.
|
|
44
|
+
*/
|
|
45
|
+
export function mergeNitroOptions(project: FacilisNitroOptions | undefined): NitroConfig {
|
|
46
|
+
// The merged record is exactly the shape Nitro's own config loader accepted before D-034
|
|
47
|
+
// (it originates from facilis.config.ts, typed NitroConfig at the authoring site).
|
|
48
|
+
return mergePlainObjects(DEFAULT_NITRO_OPTIONS, project ?? {}) as NitroConfig;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Load `<root>/facilis.config.ts` with the project's native ESM loader.
|
|
53
|
+
*
|
|
54
|
+
* `null` when the file is absent: the framework degrades to its defaults and the Nitro
|
|
55
|
+
* runtime reports the missing config as a manifest warning (it must not brick the dev
|
|
56
|
+
* server). A file that exists but cannot be imported or validated is an authoring error
|
|
57
|
+
* and fails the start with the file path in the message.
|
|
58
|
+
*/
|
|
59
|
+
export async function loadFacilisConfig(root: string): Promise<FacilisConfig | null> {
|
|
60
|
+
const configPath = join(root, PROJECT_CONFIG_FILE);
|
|
61
|
+
if (!existsSync(configPath)) return null;
|
|
62
|
+
|
|
63
|
+
let module: unknown;
|
|
64
|
+
try {
|
|
65
|
+
module = await import(pathToFileURL(configPath).href);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
// The import itself can be the authoring error: a module-level `defineConfig(...)` that
|
|
68
|
+
// fails validation throws here, and the real reason must reach the terminal — a bare
|
|
69
|
+
// "failed to load" sends the author hunting for resolution problems that do not exist.
|
|
70
|
+
throw new Error(`[facilis] failed to load ${configPath}: ${describeError(error)}`, {
|
|
71
|
+
cause: error,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// The file usually default-exports defineConfig(...), which has already validated and
|
|
76
|
+
// filled the defaults; parsing again also accepts a bare object and reports the same
|
|
77
|
+
// schema error a runtime `defineConfig` would.
|
|
78
|
+
const parsed = facilisConfigSchema.safeParse(defaultExport(module));
|
|
79
|
+
if (!parsed.success) {
|
|
80
|
+
throw new Error(`[facilis] invalid ${configPath}: ${parsed.error.message}`);
|
|
81
|
+
}
|
|
82
|
+
return parsed.data;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The Nitro options Nitro should boot with, given the project root. */
|
|
86
|
+
export async function loadNitroOptions(root: string): Promise<NitroConfig> {
|
|
87
|
+
const config = await loadFacilisConfig(root);
|
|
88
|
+
return mergeNitroOptions(config?.nitro);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function mergePlainObjects(
|
|
92
|
+
base: Readonly<Record<string, unknown>>,
|
|
93
|
+
override: Readonly<Record<string, unknown>>,
|
|
94
|
+
): Record<string, unknown> {
|
|
95
|
+
const merged: Record<string, unknown> = {};
|
|
96
|
+
for (const source of [base, override]) {
|
|
97
|
+
for (const [key, value] of Object.entries(source)) {
|
|
98
|
+
const existing = merged[key];
|
|
99
|
+
merged[key] = isPlainObject(value)
|
|
100
|
+
? mergePlainObjects(isPlainObject(existing) ? existing : {}, value)
|
|
101
|
+
: value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return merged;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
108
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The message of a thrown value, for wrapping it into a one-line loader error. */
|
|
112
|
+
function describeError(error: unknown): string {
|
|
113
|
+
if (error instanceof Error) return error.message;
|
|
114
|
+
return String(error);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function defaultExport(module: unknown): unknown {
|
|
118
|
+
if (typeof module !== "object" || module === null || !("default" in module)) return undefined;
|
|
119
|
+
return module.default;
|
|
120
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Paths served by @facilis.dev/nitro; shared by the Nitro module and the runtime routes. */
|
|
2
|
+
export const OPENAPI_FACADE_ROUTE = "/_openapi.json";
|
|
3
|
+
/** Where Nitro's own generated spec is kept intact (dev-only internal route). */
|
|
4
|
+
export const NATIVE_OPENAPI_ROUTE = "/_facilis/openapi.json";
|
|
5
|
+
export const MANIFEST_ROUTE = "/__facilis/manifest";
|
|
6
|
+
export const MANIFEST_REVISION_ROUTE = "/__facilis/manifest/revision";
|
|
7
|
+
export const HEALTH_ROUTE = "/__facilis/health";
|
|
8
|
+
/** Live schema introspection for the Entity Explorer (SPEC §6.3/§11.4). */
|
|
9
|
+
export const DEV_SCHEMA_ROUTE = "/__facilis/dev/schema";
|
|
10
|
+
/** Read-only SQL console for the Data Explorer (SPEC §11.2, D-097). */
|
|
11
|
+
export const DEV_QUERY_ROUTE = "/__facilis/dev/query";
|
|
12
|
+
/** Create/replace a security_invoker view from the query console (D-097). */
|
|
13
|
+
export const DEV_VIEW_ROUTE = "/__facilis/dev/view";
|
|
14
|
+
/**
|
|
15
|
+
* Framework-owned SSO namespace (D-120): providers, start and callback (GET/POST for the SAML
|
|
16
|
+
* ACS) are one handler, registered in dev and production. Reserved to the framework; an app
|
|
17
|
+
* keeps its own login/register routes.
|
|
18
|
+
*/
|
|
19
|
+
export const SSO_ROUTE = "/api/auth/sso/**";
|
|
20
|
+
/**
|
|
21
|
+
* Framework-owned OAuth2 client-credentials token endpoint (v3a), registered in every
|
|
22
|
+
* environment; it answers not_found when auth.apiClients is disabled.
|
|
23
|
+
*/
|
|
24
|
+
export const TOKEN_ROUTE = "/api/auth/token";
|
package/src/facade.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { NATIVE_OPENAPI_ROUTE } from "./constants.ts";
|
|
2
|
+
import { isProductionEnvironment } from "./runtime/env.ts";
|
|
3
|
+
import { jsonResult, type HttpResult } from "./runtime/http.ts";
|
|
4
|
+
import type { RouteMetadata, RouteMetadataMap } from "./types.ts";
|
|
5
|
+
|
|
6
|
+
export { NATIVE_OPENAPI_ROUTE };
|
|
7
|
+
|
|
8
|
+
export interface OpenApiOperation {
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface OpenApiPathItem {
|
|
13
|
+
[method: string]: OpenApiOperation;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface OpenApiDocument {
|
|
17
|
+
openapi?: string;
|
|
18
|
+
info?: Record<string, unknown>;
|
|
19
|
+
paths?: Record<string, OpenApiPathItem>;
|
|
20
|
+
components?: OpenApiComponents;
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface OpenApiComponents {
|
|
25
|
+
securitySchemes?: Record<string, unknown>;
|
|
26
|
+
[key: string]: unknown;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const BEARER_AUTH_SCHEME = { type: "http", scheme: "bearer" } as const;
|
|
30
|
+
|
|
31
|
+
export interface OpenApiFacadeDependencies {
|
|
32
|
+
metadata: RouteMetadataMap;
|
|
33
|
+
fetchNativeSpec: () => Promise<OpenApiDocument | null>;
|
|
34
|
+
isProduction?: () => boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const EMPTY_SPEC: OpenApiDocument = {
|
|
38
|
+
openapi: "3.1.0",
|
|
39
|
+
info: { title: "Facilis API", version: "1.0.0" },
|
|
40
|
+
paths: {},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Copy `spec` and merge the AST-pass metadata into each operation (SPEC §6.9).
|
|
45
|
+
*
|
|
46
|
+
* Nitro cannot see the defineAuthorizedHandler config (Phase 0 A16), so the facade
|
|
47
|
+
* is what puts x-db-tables / x-permission / description on `/_openapi.json`.
|
|
48
|
+
* Fields the scanner could not read are omitted rather than guessed; they are
|
|
49
|
+
* published as `x-facilis-warning` instead.
|
|
50
|
+
*/
|
|
51
|
+
export function mergeOpenApiMetadata(
|
|
52
|
+
spec: OpenApiDocument,
|
|
53
|
+
metadata: RouteMetadataMap,
|
|
54
|
+
): OpenApiDocument {
|
|
55
|
+
const paths: Record<string, OpenApiPathItem> = { ...spec.paths };
|
|
56
|
+
let usesBearerAuth = false;
|
|
57
|
+
|
|
58
|
+
for (const [path, operations] of Object.entries(metadata)) {
|
|
59
|
+
const pathItem = paths[path];
|
|
60
|
+
if (!pathItem) continue;
|
|
61
|
+
let mergedPath: OpenApiPathItem | undefined;
|
|
62
|
+
for (const [method, route] of Object.entries(operations)) {
|
|
63
|
+
const operation = pathItem[method];
|
|
64
|
+
if (!operation) continue;
|
|
65
|
+
mergedPath ??= { ...pathItem };
|
|
66
|
+
mergedPath[method] = mergeOperation(operation, route);
|
|
67
|
+
usesBearerAuth ||= needsBearerAuth(route);
|
|
68
|
+
}
|
|
69
|
+
if (mergedPath) paths[path] = mergedPath;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!usesBearerAuth) return { ...spec, paths };
|
|
73
|
+
// The scheme is declared once, only when an operation actually references it, so a
|
|
74
|
+
// project without token-authenticated cross-site routes publishes no dangling scheme.
|
|
75
|
+
return { ...spec, paths, components: withBearerAuth(spec.components) };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A cross-site route with a permission is reachable only with a Bearer token (v3a). */
|
|
79
|
+
function needsBearerAuth(route: RouteMetadata): boolean {
|
|
80
|
+
if (route.crossSite !== true) return false;
|
|
81
|
+
if (route.permission === undefined || route.unresolved.includes("permission")) return false;
|
|
82
|
+
return route.permission.length > 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Merge the bearer scheme into the spec's components without dropping the native ones. */
|
|
86
|
+
function withBearerAuth(components: OpenApiComponents | undefined): OpenApiComponents {
|
|
87
|
+
return {
|
|
88
|
+
...components,
|
|
89
|
+
securitySchemes: {
|
|
90
|
+
...components?.securitySchemes,
|
|
91
|
+
bearerAuth: BEARER_AUTH_SCHEME,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function mergeOperation(operation: OpenApiOperation, route: RouteMetadata): OpenApiOperation {
|
|
97
|
+
const merged: OpenApiOperation = { ...operation };
|
|
98
|
+
if (route.description !== undefined) merged["description"] = route.description;
|
|
99
|
+
if (route.permission !== undefined && !route.unresolved.includes("permission")) {
|
|
100
|
+
merged["x-permission"] = route.permission;
|
|
101
|
+
}
|
|
102
|
+
if (!route.unresolved.includes("dbTables")) merged["x-db-tables"] = [...route.dbTables];
|
|
103
|
+
if (!route.unresolved.includes("dbColumns")) merged["x-db-columns"] = [...route.dbColumns];
|
|
104
|
+
// An unreadable derogation is omitted, never published as "false" (fail closed, D-157).
|
|
105
|
+
if (!route.unresolved.includes("rawSql")) merged["x-raw-sql"] = route.rawSql === true;
|
|
106
|
+
if (route.crossSite === true) merged["x-cross-site"] = true;
|
|
107
|
+
if (needsBearerAuth(route)) merged["security"] = [{ bearerAuth: [] }];
|
|
108
|
+
if (route.sourceFile !== undefined) merged["x-facilis-source"] = route.sourceFile;
|
|
109
|
+
if (route.warnings.length > 0) {
|
|
110
|
+
merged["x-facilis-warning"] = route.warnings.map((warning) => ({
|
|
111
|
+
code: warning.code,
|
|
112
|
+
message: warning.message,
|
|
113
|
+
}));
|
|
114
|
+
}
|
|
115
|
+
if (merged["summary"] === undefined && route.description !== undefined) {
|
|
116
|
+
merged["summary"] = route.description;
|
|
117
|
+
}
|
|
118
|
+
// Nitro tags scanned handlers "API Routes"; a facilis tag keeps augmented
|
|
119
|
+
// operations identifiable in the visualizer without overriding user tags.
|
|
120
|
+
merged["tags"] ??= ["facilis"];
|
|
121
|
+
return merged;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Build the `/_openapi.json` response: dev-only facade over Nitro's native spec. */
|
|
125
|
+
export async function buildOpenApiFacadeResponse(
|
|
126
|
+
dependencies: OpenApiFacadeDependencies,
|
|
127
|
+
): Promise<HttpResult> {
|
|
128
|
+
const production = (dependencies.isProduction ?? isProductionEnvironment)();
|
|
129
|
+
if (production) {
|
|
130
|
+
return jsonResult(404, { code: "not_found" }, { "cache-control": "no-cache" });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
let native: OpenApiDocument | null = null;
|
|
134
|
+
try {
|
|
135
|
+
native = await dependencies.fetchNativeSpec();
|
|
136
|
+
} catch {
|
|
137
|
+
native = null;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const warnings: Array<{ code: string; message: string }> = [];
|
|
141
|
+
if (!native) {
|
|
142
|
+
warnings.push({
|
|
143
|
+
code: "openapi-unavailable",
|
|
144
|
+
message:
|
|
145
|
+
"Nitro's OpenAPI document is unavailable. Enable experimental.openAPI in nitro.config.ts " +
|
|
146
|
+
`(native route: ${NATIVE_OPENAPI_ROUTE}).`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const merged = mergeOpenApiMetadata(native ?? EMPTY_SPEC, dependencies.metadata);
|
|
151
|
+
if (warnings.length > 0) merged["x-facilis-warning"] = warnings;
|
|
152
|
+
return jsonResult(200, merged, { "cache-control": "no-cache" });
|
|
153
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @facilis.dev/nitro — the `facilis()` Vite plugin, its Nitro module, the framework route
|
|
3
|
+
* metadata pass and the OpenAPI facade (SPEC §5.6, §6.9, §13 risk 3).
|
|
4
|
+
*
|
|
5
|
+
* Entry points: `@facilis.dev/nitro` (everything), `@facilis.dev/nitro/vite` (plugin),
|
|
6
|
+
* `@facilis.dev/nitro/module` (Nitro module), `@facilis.dev/nitro/session` (route-facing server
|
|
7
|
+
* cookie helpers; imports h3, so it is a separate entry point).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
DEV_SCHEMA_ROUTE,
|
|
12
|
+
HEALTH_ROUTE,
|
|
13
|
+
MANIFEST_REVISION_ROUTE,
|
|
14
|
+
MANIFEST_ROUTE,
|
|
15
|
+
NATIVE_OPENAPI_ROUTE,
|
|
16
|
+
OPENAPI_FACADE_ROUTE,
|
|
17
|
+
} from "./constants.ts";
|
|
18
|
+
export { buildOpenApiFacadeResponse, mergeOpenApiMetadata } from "./facade.ts";
|
|
19
|
+
export type {
|
|
20
|
+
OpenApiDocument,
|
|
21
|
+
OpenApiFacadeDependencies,
|
|
22
|
+
OpenApiOperation,
|
|
23
|
+
OpenApiPathItem,
|
|
24
|
+
} from "./facade.ts";
|
|
25
|
+
export { createFacilisNitroModule, facilisNitroModule } from "./module.ts";
|
|
26
|
+
export { scanRouteMetadata } from "./route-metadata.ts";
|
|
27
|
+
export type {
|
|
28
|
+
FacilisOptions,
|
|
29
|
+
HttpMethod,
|
|
30
|
+
MetadataField,
|
|
31
|
+
RouteMetadata,
|
|
32
|
+
RouteMetadataMap,
|
|
33
|
+
ScanOptions,
|
|
34
|
+
} from "./types.ts";
|
|
35
|
+
export { facilis } from "./vite.ts";
|
|
36
|
+
export type { FacilisPlugins, FacilisVitePlugin } from "./vite.ts";
|
|
37
|
+
export {
|
|
38
|
+
RESOLVED_OPENAPI_META_ID,
|
|
39
|
+
VIRTUAL_CONFIG_ID,
|
|
40
|
+
VIRTUAL_DB_SCHEMA_ID,
|
|
41
|
+
VIRTUAL_JOBS_ID,
|
|
42
|
+
VIRTUAL_OPENAPI_META_ID,
|
|
43
|
+
VIRTUAL_WORKFLOWS_ID,
|
|
44
|
+
} from "./virtual-ids.ts";
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { H3Event } from "nitro";
|
|
2
|
+
import { getRequestHeader, getRequestIP, setResponseHeader } from "nitro/h3";
|
|
3
|
+
import {
|
|
4
|
+
acceptRequestId,
|
|
5
|
+
newLogRequestId,
|
|
6
|
+
type FacilisEventContext,
|
|
7
|
+
type FacilisLoggingConfig,
|
|
8
|
+
} from "@facilis.dev/core";
|
|
9
|
+
|
|
10
|
+
import { getBootstrap } from "../runtime/bootstrap.ts";
|
|
11
|
+
import { readDefaultTenantId } from "../runtime/tenant.ts";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* facilis-context middleware (contracts §4.1/§8, D-119).
|
|
15
|
+
*
|
|
16
|
+
* Boots the runtime lazily and exposes event.context.facilis — the db-backed routes, the
|
|
17
|
+
* single-tenant fallback id and the request logging runtime. It also owns the correlation id:
|
|
18
|
+
* an inbound x-request-id is accepted only when it is a safe token, and the resolved id is
|
|
19
|
+
* returned in the response so a client can correlate its request with the server log.
|
|
20
|
+
*
|
|
21
|
+
* When the db is unavailable the facade stays unset, so authorized handlers answer
|
|
22
|
+
* db_unavailable instead of running unscoped; the response still carries the request id.
|
|
23
|
+
*/
|
|
24
|
+
const REQUEST_ID_HEADER = "x-request-id";
|
|
25
|
+
const FORWARDED_FOR_HEADER = "x-forwarded-for";
|
|
26
|
+
const USER_AGENT_HEADER = "user-agent";
|
|
27
|
+
|
|
28
|
+
const handler = async (event: H3Event): Promise<void> => {
|
|
29
|
+
const state = await getBootstrap();
|
|
30
|
+
const context = event.context as FacilisEventContext;
|
|
31
|
+
|
|
32
|
+
// Logging is available even when the db never booted; the id is generated in that case so
|
|
33
|
+
// the failed-boot response is still correlatable with the process log.
|
|
34
|
+
const requestId =
|
|
35
|
+
acceptRequestId(getRequestHeader(event, REQUEST_ID_HEADER)) ?? newLogRequestId();
|
|
36
|
+
setResponseHeader(event, REQUEST_ID_HEADER, requestId);
|
|
37
|
+
|
|
38
|
+
// The CSRF gate is structural, so it needs a value even when the db never booted: the
|
|
39
|
+
// config default (same-origin only) applies when the project config is unavailable.
|
|
40
|
+
context.security = { trustedOrigins: state.config?.security?.trustedOrigins ?? [] };
|
|
41
|
+
|
|
42
|
+
if (state.db === null) return;
|
|
43
|
+
|
|
44
|
+
const client = resolveClient(event, state.loggingConfig);
|
|
45
|
+
// The wrapper's rate-limit key is resolved for every request, independently of the logging
|
|
46
|
+
// opt-ins (D-152): proxy trust decides whether the address identifies the caller, and leaving
|
|
47
|
+
// it unresolved means "no per-client throttle", never one shared window for every user.
|
|
48
|
+
const clientAddress = getRequestIP(event, {
|
|
49
|
+
xForwardedFor: state.loggingConfig.trustProxy,
|
|
50
|
+
});
|
|
51
|
+
context.facilis = {
|
|
52
|
+
db: state.db,
|
|
53
|
+
defaultTenantId: readDefaultTenantId(),
|
|
54
|
+
...(clientAddress === undefined ? {} : { clientAddress }),
|
|
55
|
+
// ctx.log and the access/error streams are structural for every authorized handler
|
|
56
|
+
// (D-119): attach the runtime and the one id all entries of this request share.
|
|
57
|
+
logging: state.logging,
|
|
58
|
+
requestId,
|
|
59
|
+
...(client === undefined ? {} : { client }),
|
|
60
|
+
// ctx.jobs is required by the handler contract: attach it whenever the runtime
|
|
61
|
+
// booted. Without it the wrapper substitutes a job_unavailable fallback (D-107).
|
|
62
|
+
...(state.jobs === null ? {} : { jobs: state.jobs.api }),
|
|
63
|
+
// Same contract for ctx.tasks (D-135): a missing runtime becomes a task_unavailable
|
|
64
|
+
// fallback in the wrapper, never a broken route.
|
|
65
|
+
...(state.tasks === null ? {} : { tasks: state.tasks.api }),
|
|
66
|
+
// Same contract for ctx.workflows (D-135): a missing runtime becomes a
|
|
67
|
+
// workflow_unavailable fallback in the wrapper, never a broken route.
|
|
68
|
+
...(state.workflows === null ? {} : { workflows: state.workflows.api }),
|
|
69
|
+
// Same contract for ctx.storage (D-053): missing runtime => route-level fallback.
|
|
70
|
+
...(state.storage === null ? {} : { storage: state.storage }),
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
interface ClientMetadata {
|
|
75
|
+
readonly ip?: string;
|
|
76
|
+
readonly userAgent?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Client metadata is opt-in: proxy trust and User-Agent collection are deployment decisions. */
|
|
80
|
+
function resolveClient(event: H3Event, config: FacilisLoggingConfig): ClientMetadata | undefined {
|
|
81
|
+
const userAgent = config.includeUserAgent
|
|
82
|
+
? readNonEmptyHeader(event, USER_AGENT_HEADER)
|
|
83
|
+
: undefined;
|
|
84
|
+
const ip = config.trustProxy
|
|
85
|
+
? firstForwardedFor(readNonEmptyHeader(event, FORWARDED_FOR_HEADER))
|
|
86
|
+
: undefined;
|
|
87
|
+
if (userAgent === undefined && ip === undefined) return undefined;
|
|
88
|
+
return {
|
|
89
|
+
...(ip === undefined ? {} : { ip }),
|
|
90
|
+
...(userAgent === undefined ? {} : { userAgent }),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The first address of x-forwarded-for: the client, before any proxy appends its own. */
|
|
95
|
+
export function firstForwardedFor(value: string | undefined): string | undefined {
|
|
96
|
+
if (value === undefined) return undefined;
|
|
97
|
+
const first = value.split(",")[0]?.trim();
|
|
98
|
+
return first === undefined || first.length === 0 ? undefined : first;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function readNonEmptyHeader(event: H3Event, name: string): string | undefined {
|
|
102
|
+
const value = getRequestHeader(event, name);
|
|
103
|
+
return value === undefined || value.trim().length === 0 ? undefined : value;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default handler;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { H3Event } from "nitro";
|
|
2
|
+
import { getRequestHeader, setResponseHeader } from "nitro/h3";
|
|
3
|
+
import config from "#facilis/config";
|
|
4
|
+
import routeMetadata from "#facilis/openapi-meta";
|
|
5
|
+
|
|
6
|
+
import { corsHeadersFor, matchRoute, preflightResponse } from "../runtime/cors-policy.ts";
|
|
7
|
+
|
|
8
|
+
/** Trusted origins come from security.trustedOrigins; anything malformed is ignored. */
|
|
9
|
+
function readTrustedOrigins(source: unknown): readonly string[] {
|
|
10
|
+
const security = (source as { security?: { trustedOrigins?: unknown } }).security;
|
|
11
|
+
const origins = security?.trustedOrigins;
|
|
12
|
+
if (!Array.isArray(origins)) return [];
|
|
13
|
+
return origins.filter((origin): origin is string => typeof origin === "string");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const TRUSTED_ORIGINS = readTrustedOrigins(config);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* facilis-cors middleware (SPEC §8).
|
|
20
|
+
*
|
|
21
|
+
* Runs before facilis-context so a preflight is answered without booting the db or auth.
|
|
22
|
+
* A returned Response short-circuits the request; otherwise the CORS headers are set on the
|
|
23
|
+
* response the route will produce.
|
|
24
|
+
*/
|
|
25
|
+
const handler = (event: H3Event): Response | undefined => {
|
|
26
|
+
const pathname = new URL(event.req.url, "http://localhost").pathname;
|
|
27
|
+
const method = (event.req.method ?? "GET").toUpperCase();
|
|
28
|
+
|
|
29
|
+
if (method === "OPTIONS") {
|
|
30
|
+
return (
|
|
31
|
+
preflightResponse(routeMetadata, pathname, event.req.headers, TRUSTED_ORIGINS) ?? undefined
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const origin = getRequestHeader(event, "origin") ?? null;
|
|
36
|
+
const trusted = origin !== null && TRUSTED_ORIGINS.includes(origin);
|
|
37
|
+
const headers = corsHeadersFor(origin, trusted, matchRoute(routeMetadata, pathname, method));
|
|
38
|
+
for (const [name, value] of headers) setResponseHeader(event, name, value);
|
|
39
|
+
return undefined;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export default handler;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { H3Event } from "nitro";
|
|
2
|
+
import type { FacilisEventContext } from "@facilis.dev/core";
|
|
3
|
+
|
|
4
|
+
import { getBootstrap, getSessionHandler } from "../runtime/bootstrap.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* facilis-session middleware (contracts §4.1).
|
|
8
|
+
*
|
|
9
|
+
* Runs after facilis-context: validates the session cookie on the root client and
|
|
10
|
+
* sets `event.context.auth` (`null` when anonymous). No db means no session.
|
|
11
|
+
*/
|
|
12
|
+
const handler = async (event: H3Event): Promise<void> => {
|
|
13
|
+
const state = await getBootstrap();
|
|
14
|
+
const context = event.context as FacilisEventContext;
|
|
15
|
+
const sessionHandler = getSessionHandler(state);
|
|
16
|
+
if (sessionHandler === null) {
|
|
17
|
+
context.auth = null;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
await sessionHandler(event);
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default handler;
|