@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 facilis-dev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @facilis.dev/nitro
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Facilis Nitro module and its Vite plugin wrapper.
|
|
4
|
+
|
|
5
|
+
Part of [Facilis](https://github.com/facilis-dev/facilis), a browser-first low-code framework: the Studio
|
|
6
|
+
runs a generated project in a WebContainer where vite dev is the only dev server
|
|
7
|
+
and Nitro v3 runs as a Vite plugin. Framework contracts live in
|
|
8
|
+
[docs/SPEC.md](https://github.com/facilis-dev/facilis/blob/main/docs/SPEC.md) and architectural decisions in
|
|
9
|
+
[docs/DECISIONS.md](https://github.com/facilis-dev/facilis/blob/main/docs/DECISIONS.md).
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @facilis.dev/nitro
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The package publishes its TypeScript sources and its compiled dist/; the exports map
|
|
16
|
+
of the published manifest points at dist/, so Node and bundlers load JavaScript and
|
|
17
|
+
the bundled .d.ts files.
|
|
18
|
+
|
|
19
|
+
Licensed under the MIT License.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type FacilisConfig, type FacilisNitroOptions } from "@facilis.dev/core/config";
|
|
2
|
+
import type { NitroConfig } from "nitro/types";
|
|
3
|
+
/**
|
|
4
|
+
* The project config file the Vite plugin reads (SPEC §2.3, D-034).
|
|
5
|
+
*
|
|
6
|
+
* A Facilis project commits ONE config: Nitro options live in its `nitro` section, so
|
|
7
|
+
* there is no `nitro.config.ts` and the project's `vite.config.ts` never names Nitro.
|
|
8
|
+
*/
|
|
9
|
+
export declare const PROJECT_CONFIG_FILE = "facilis.config.ts";
|
|
10
|
+
/** The defaults as the merge sees them; tests assert against this value. */
|
|
11
|
+
export declare const DEFAULT_NITRO_OPTIONS: Readonly<Record<string, unknown>>;
|
|
12
|
+
/**
|
|
13
|
+
* Merge the project's `nitro` section over the Facilis defaults.
|
|
14
|
+
*
|
|
15
|
+
* Deep for plain objects so `nitro.experimental.openAPI: false` only flips that switch
|
|
16
|
+
* instead of replacing the whole `experimental` block; arrays are Nitro lists authored
|
|
17
|
+
* whole, so they replace rather than concatenate.
|
|
18
|
+
*/
|
|
19
|
+
export declare function mergeNitroOptions(project: FacilisNitroOptions | undefined): NitroConfig;
|
|
20
|
+
/**
|
|
21
|
+
* Load `<root>/facilis.config.ts` with the project's native ESM loader.
|
|
22
|
+
*
|
|
23
|
+
* `null` when the file is absent: the framework degrades to its defaults and the Nitro
|
|
24
|
+
* runtime reports the missing config as a manifest warning (it must not brick the dev
|
|
25
|
+
* server). A file that exists but cannot be imported or validated is an authoring error
|
|
26
|
+
* and fails the start with the file path in the message.
|
|
27
|
+
*/
|
|
28
|
+
export declare function loadFacilisConfig(root: string): Promise<FacilisConfig | null>;
|
|
29
|
+
/** The Nitro options Nitro should boot with, given the project root. */
|
|
30
|
+
export declare function loadNitroOptions(root: string): Promise<NitroConfig>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { existsSync } from "node:fs";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { pathToFileURL } from "node:url";
|
|
12
|
+
// The /config subpath keeps the Vite config phase from loading @facilis.dev/core's entry, which
|
|
13
|
+
// re-exports the route/db runtime: the plugin only needs the schema and its types here.
|
|
14
|
+
import { facilisConfigSchema, } from "@facilis.dev/core/config";
|
|
15
|
+
/**
|
|
16
|
+
* The project config file the Vite plugin reads (SPEC §2.3, D-034).
|
|
17
|
+
*
|
|
18
|
+
* A Facilis project commits ONE config: Nitro options live in its `nitro` section, so
|
|
19
|
+
* there is no `nitro.config.ts` and the project's `vite.config.ts` never names Nitro.
|
|
20
|
+
*/
|
|
21
|
+
export const PROJECT_CONFIG_FILE = "facilis.config.ts";
|
|
22
|
+
/**
|
|
23
|
+
* Facilis defaults for Nitro (SPEC §4.1/§4.2).
|
|
24
|
+
*
|
|
25
|
+
* The framework owns these two: the server directory is always `./server` and the native
|
|
26
|
+
* OpenAPI document is always generated (the `/_openapi.json` facade augments it). A
|
|
27
|
+
* project only declares what it changes, in `facilis.config.ts`'s `nitro` section.
|
|
28
|
+
*/
|
|
29
|
+
// `satisfies` keeps this list honest against Nitro's own option type, while the exported
|
|
30
|
+
// value stays an indexable record for the merge.
|
|
31
|
+
const FACILIS_NITRO_DEFAULTS = {
|
|
32
|
+
serverDir: "./server",
|
|
33
|
+
experimental: { openAPI: true },
|
|
34
|
+
};
|
|
35
|
+
/** The defaults as the merge sees them; tests assert against this value. */
|
|
36
|
+
export const DEFAULT_NITRO_OPTIONS = FACILIS_NITRO_DEFAULTS;
|
|
37
|
+
/**
|
|
38
|
+
* Merge the project's `nitro` section over the Facilis defaults.
|
|
39
|
+
*
|
|
40
|
+
* Deep for plain objects so `nitro.experimental.openAPI: false` only flips that switch
|
|
41
|
+
* instead of replacing the whole `experimental` block; arrays are Nitro lists authored
|
|
42
|
+
* whole, so they replace rather than concatenate.
|
|
43
|
+
*/
|
|
44
|
+
export function mergeNitroOptions(project) {
|
|
45
|
+
// The merged record is exactly the shape Nitro's own config loader accepted before D-034
|
|
46
|
+
// (it originates from facilis.config.ts, typed NitroConfig at the authoring site).
|
|
47
|
+
return mergePlainObjects(DEFAULT_NITRO_OPTIONS, project ?? {});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Load `<root>/facilis.config.ts` with the project's native ESM loader.
|
|
51
|
+
*
|
|
52
|
+
* `null` when the file is absent: the framework degrades to its defaults and the Nitro
|
|
53
|
+
* runtime reports the missing config as a manifest warning (it must not brick the dev
|
|
54
|
+
* server). A file that exists but cannot be imported or validated is an authoring error
|
|
55
|
+
* and fails the start with the file path in the message.
|
|
56
|
+
*/
|
|
57
|
+
export async function loadFacilisConfig(root) {
|
|
58
|
+
const configPath = join(root, PROJECT_CONFIG_FILE);
|
|
59
|
+
if (!existsSync(configPath))
|
|
60
|
+
return null;
|
|
61
|
+
let module;
|
|
62
|
+
try {
|
|
63
|
+
module = await import(__rewriteRelativeImportExtension(pathToFileURL(configPath).href));
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
// The import itself can be the authoring error: a module-level `defineConfig(...)` that
|
|
67
|
+
// fails validation throws here, and the real reason must reach the terminal — a bare
|
|
68
|
+
// "failed to load" sends the author hunting for resolution problems that do not exist.
|
|
69
|
+
throw new Error(`[facilis] failed to load ${configPath}: ${describeError(error)}`, {
|
|
70
|
+
cause: error,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// The file usually default-exports defineConfig(...), which has already validated and
|
|
74
|
+
// filled the defaults; parsing again also accepts a bare object and reports the same
|
|
75
|
+
// schema error a runtime `defineConfig` would.
|
|
76
|
+
const parsed = facilisConfigSchema.safeParse(defaultExport(module));
|
|
77
|
+
if (!parsed.success) {
|
|
78
|
+
throw new Error(`[facilis] invalid ${configPath}: ${parsed.error.message}`);
|
|
79
|
+
}
|
|
80
|
+
return parsed.data;
|
|
81
|
+
}
|
|
82
|
+
/** The Nitro options Nitro should boot with, given the project root. */
|
|
83
|
+
export async function loadNitroOptions(root) {
|
|
84
|
+
const config = await loadFacilisConfig(root);
|
|
85
|
+
return mergeNitroOptions(config?.nitro);
|
|
86
|
+
}
|
|
87
|
+
function mergePlainObjects(base, override) {
|
|
88
|
+
const merged = {};
|
|
89
|
+
for (const source of [base, override]) {
|
|
90
|
+
for (const [key, value] of Object.entries(source)) {
|
|
91
|
+
const existing = merged[key];
|
|
92
|
+
merged[key] = isPlainObject(value)
|
|
93
|
+
? mergePlainObjects(isPlainObject(existing) ? existing : {}, value)
|
|
94
|
+
: value;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return merged;
|
|
98
|
+
}
|
|
99
|
+
function isPlainObject(value) {
|
|
100
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
101
|
+
}
|
|
102
|
+
/** The message of a thrown value, for wrapping it into a one-line loader error. */
|
|
103
|
+
function describeError(error) {
|
|
104
|
+
if (error instanceof Error)
|
|
105
|
+
return error.message;
|
|
106
|
+
return String(error);
|
|
107
|
+
}
|
|
108
|
+
function defaultExport(module) {
|
|
109
|
+
if (typeof module !== "object" || module === null || !("default" in module))
|
|
110
|
+
return undefined;
|
|
111
|
+
return module.default;
|
|
112
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Paths served by @facilis.dev/nitro; shared by the Nitro module and the runtime routes. */
|
|
2
|
+
export declare const OPENAPI_FACADE_ROUTE = "/_openapi.json";
|
|
3
|
+
/** Where Nitro's own generated spec is kept intact (dev-only internal route). */
|
|
4
|
+
export declare const NATIVE_OPENAPI_ROUTE = "/_facilis/openapi.json";
|
|
5
|
+
export declare const MANIFEST_ROUTE = "/__facilis/manifest";
|
|
6
|
+
export declare const MANIFEST_REVISION_ROUTE = "/__facilis/manifest/revision";
|
|
7
|
+
export declare const HEALTH_ROUTE = "/__facilis/health";
|
|
8
|
+
/** Live schema introspection for the Entity Explorer (SPEC §6.3/§11.4). */
|
|
9
|
+
export declare const DEV_SCHEMA_ROUTE = "/__facilis/dev/schema";
|
|
10
|
+
/** Read-only SQL console for the Data Explorer (SPEC §11.2, D-097). */
|
|
11
|
+
export declare const DEV_QUERY_ROUTE = "/__facilis/dev/query";
|
|
12
|
+
/** Create/replace a security_invoker view from the query console (D-097). */
|
|
13
|
+
export declare 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 declare 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 declare const TOKEN_ROUTE = "/api/auth/token";
|
|
@@ -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/dist/facade.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { NATIVE_OPENAPI_ROUTE } from "./constants.js";
|
|
2
|
+
import { type HttpResult } from "./runtime/http.js";
|
|
3
|
+
import type { RouteMetadataMap } from "./types.js";
|
|
4
|
+
export { NATIVE_OPENAPI_ROUTE };
|
|
5
|
+
export interface OpenApiOperation {
|
|
6
|
+
[key: string]: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface OpenApiPathItem {
|
|
9
|
+
[method: string]: OpenApiOperation;
|
|
10
|
+
}
|
|
11
|
+
export interface OpenApiDocument {
|
|
12
|
+
openapi?: string;
|
|
13
|
+
info?: Record<string, unknown>;
|
|
14
|
+
paths?: Record<string, OpenApiPathItem>;
|
|
15
|
+
components?: OpenApiComponents;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
export interface OpenApiComponents {
|
|
19
|
+
securitySchemes?: Record<string, unknown>;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
}
|
|
22
|
+
export interface OpenApiFacadeDependencies {
|
|
23
|
+
metadata: RouteMetadataMap;
|
|
24
|
+
fetchNativeSpec: () => Promise<OpenApiDocument | null>;
|
|
25
|
+
isProduction?: () => boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Copy `spec` and merge the AST-pass metadata into each operation (SPEC §6.9).
|
|
29
|
+
*
|
|
30
|
+
* Nitro cannot see the defineAuthorizedHandler config (Phase 0 A16), so the facade
|
|
31
|
+
* is what puts x-db-tables / x-permission / description on `/_openapi.json`.
|
|
32
|
+
* Fields the scanner could not read are omitted rather than guessed; they are
|
|
33
|
+
* published as `x-facilis-warning` instead.
|
|
34
|
+
*/
|
|
35
|
+
export declare function mergeOpenApiMetadata(spec: OpenApiDocument, metadata: RouteMetadataMap): OpenApiDocument;
|
|
36
|
+
/** Build the `/_openapi.json` response: dev-only facade over Nitro's native spec. */
|
|
37
|
+
export declare function buildOpenApiFacadeResponse(dependencies: OpenApiFacadeDependencies): Promise<HttpResult>;
|
package/dist/facade.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { NATIVE_OPENAPI_ROUTE } from "./constants.js";
|
|
2
|
+
import { isProductionEnvironment } from "./runtime/env.js";
|
|
3
|
+
import { jsonResult } from "./runtime/http.js";
|
|
4
|
+
export { NATIVE_OPENAPI_ROUTE };
|
|
5
|
+
const BEARER_AUTH_SCHEME = { type: "http", scheme: "bearer" };
|
|
6
|
+
const EMPTY_SPEC = {
|
|
7
|
+
openapi: "3.1.0",
|
|
8
|
+
info: { title: "Facilis API", version: "1.0.0" },
|
|
9
|
+
paths: {},
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Copy `spec` and merge the AST-pass metadata into each operation (SPEC §6.9).
|
|
13
|
+
*
|
|
14
|
+
* Nitro cannot see the defineAuthorizedHandler config (Phase 0 A16), so the facade
|
|
15
|
+
* is what puts x-db-tables / x-permission / description on `/_openapi.json`.
|
|
16
|
+
* Fields the scanner could not read are omitted rather than guessed; they are
|
|
17
|
+
* published as `x-facilis-warning` instead.
|
|
18
|
+
*/
|
|
19
|
+
export function mergeOpenApiMetadata(spec, metadata) {
|
|
20
|
+
const paths = { ...spec.paths };
|
|
21
|
+
let usesBearerAuth = false;
|
|
22
|
+
for (const [path, operations] of Object.entries(metadata)) {
|
|
23
|
+
const pathItem = paths[path];
|
|
24
|
+
if (!pathItem)
|
|
25
|
+
continue;
|
|
26
|
+
let mergedPath;
|
|
27
|
+
for (const [method, route] of Object.entries(operations)) {
|
|
28
|
+
const operation = pathItem[method];
|
|
29
|
+
if (!operation)
|
|
30
|
+
continue;
|
|
31
|
+
mergedPath ??= { ...pathItem };
|
|
32
|
+
mergedPath[method] = mergeOperation(operation, route);
|
|
33
|
+
usesBearerAuth ||= needsBearerAuth(route);
|
|
34
|
+
}
|
|
35
|
+
if (mergedPath)
|
|
36
|
+
paths[path] = mergedPath;
|
|
37
|
+
}
|
|
38
|
+
if (!usesBearerAuth)
|
|
39
|
+
return { ...spec, paths };
|
|
40
|
+
// The scheme is declared once, only when an operation actually references it, so a
|
|
41
|
+
// project without token-authenticated cross-site routes publishes no dangling scheme.
|
|
42
|
+
return { ...spec, paths, components: withBearerAuth(spec.components) };
|
|
43
|
+
}
|
|
44
|
+
/** A cross-site route with a permission is reachable only with a Bearer token (v3a). */
|
|
45
|
+
function needsBearerAuth(route) {
|
|
46
|
+
if (route.crossSite !== true)
|
|
47
|
+
return false;
|
|
48
|
+
if (route.permission === undefined || route.unresolved.includes("permission"))
|
|
49
|
+
return false;
|
|
50
|
+
return route.permission.length > 0;
|
|
51
|
+
}
|
|
52
|
+
/** Merge the bearer scheme into the spec's components without dropping the native ones. */
|
|
53
|
+
function withBearerAuth(components) {
|
|
54
|
+
return {
|
|
55
|
+
...components,
|
|
56
|
+
securitySchemes: {
|
|
57
|
+
...components?.securitySchemes,
|
|
58
|
+
bearerAuth: BEARER_AUTH_SCHEME,
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function mergeOperation(operation, route) {
|
|
63
|
+
const merged = { ...operation };
|
|
64
|
+
if (route.description !== undefined)
|
|
65
|
+
merged["description"] = route.description;
|
|
66
|
+
if (route.permission !== undefined && !route.unresolved.includes("permission")) {
|
|
67
|
+
merged["x-permission"] = route.permission;
|
|
68
|
+
}
|
|
69
|
+
if (!route.unresolved.includes("dbTables"))
|
|
70
|
+
merged["x-db-tables"] = [...route.dbTables];
|
|
71
|
+
if (!route.unresolved.includes("dbColumns"))
|
|
72
|
+
merged["x-db-columns"] = [...route.dbColumns];
|
|
73
|
+
// An unreadable derogation is omitted, never published as "false" (fail closed, D-157).
|
|
74
|
+
if (!route.unresolved.includes("rawSql"))
|
|
75
|
+
merged["x-raw-sql"] = route.rawSql === true;
|
|
76
|
+
if (route.crossSite === true)
|
|
77
|
+
merged["x-cross-site"] = true;
|
|
78
|
+
if (needsBearerAuth(route))
|
|
79
|
+
merged["security"] = [{ bearerAuth: [] }];
|
|
80
|
+
if (route.sourceFile !== undefined)
|
|
81
|
+
merged["x-facilis-source"] = route.sourceFile;
|
|
82
|
+
if (route.warnings.length > 0) {
|
|
83
|
+
merged["x-facilis-warning"] = route.warnings.map((warning) => ({
|
|
84
|
+
code: warning.code,
|
|
85
|
+
message: warning.message,
|
|
86
|
+
}));
|
|
87
|
+
}
|
|
88
|
+
if (merged["summary"] === undefined && route.description !== undefined) {
|
|
89
|
+
merged["summary"] = route.description;
|
|
90
|
+
}
|
|
91
|
+
// Nitro tags scanned handlers "API Routes"; a facilis tag keeps augmented
|
|
92
|
+
// operations identifiable in the visualizer without overriding user tags.
|
|
93
|
+
merged["tags"] ??= ["facilis"];
|
|
94
|
+
return merged;
|
|
95
|
+
}
|
|
96
|
+
/** Build the `/_openapi.json` response: dev-only facade over Nitro's native spec. */
|
|
97
|
+
export async function buildOpenApiFacadeResponse(dependencies) {
|
|
98
|
+
const production = (dependencies.isProduction ?? isProductionEnvironment)();
|
|
99
|
+
if (production) {
|
|
100
|
+
return jsonResult(404, { code: "not_found" }, { "cache-control": "no-cache" });
|
|
101
|
+
}
|
|
102
|
+
let native = null;
|
|
103
|
+
try {
|
|
104
|
+
native = await dependencies.fetchNativeSpec();
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
native = null;
|
|
108
|
+
}
|
|
109
|
+
const warnings = [];
|
|
110
|
+
if (!native) {
|
|
111
|
+
warnings.push({
|
|
112
|
+
code: "openapi-unavailable",
|
|
113
|
+
message: "Nitro's OpenAPI document is unavailable. Enable experimental.openAPI in nitro.config.ts " +
|
|
114
|
+
`(native route: ${NATIVE_OPENAPI_ROUTE}).`,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const merged = mergeOpenApiMetadata(native ?? EMPTY_SPEC, dependencies.metadata);
|
|
118
|
+
if (warnings.length > 0)
|
|
119
|
+
merged["x-facilis-warning"] = warnings;
|
|
120
|
+
return jsonResult(200, merged, { "cache-control": "no-cache" });
|
|
121
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
export { DEV_SCHEMA_ROUTE, HEALTH_ROUTE, MANIFEST_REVISION_ROUTE, MANIFEST_ROUTE, NATIVE_OPENAPI_ROUTE, OPENAPI_FACADE_ROUTE, } from "./constants.js";
|
|
10
|
+
export { buildOpenApiFacadeResponse, mergeOpenApiMetadata } from "./facade.js";
|
|
11
|
+
export type { OpenApiDocument, OpenApiFacadeDependencies, OpenApiOperation, OpenApiPathItem, } from "./facade.js";
|
|
12
|
+
export { createFacilisNitroModule, facilisNitroModule } from "./module.js";
|
|
13
|
+
export { scanRouteMetadata } from "./route-metadata.js";
|
|
14
|
+
export type { FacilisOptions, HttpMethod, MetadataField, RouteMetadata, RouteMetadataMap, ScanOptions, } from "./types.js";
|
|
15
|
+
export { facilis } from "./vite.js";
|
|
16
|
+
export type { FacilisPlugins, FacilisVitePlugin } from "./vite.js";
|
|
17
|
+
export { RESOLVED_OPENAPI_META_ID, VIRTUAL_CONFIG_ID, VIRTUAL_DB_SCHEMA_ID, VIRTUAL_JOBS_ID, VIRTUAL_OPENAPI_META_ID, VIRTUAL_WORKFLOWS_ID, } from "./virtual-ids.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
export { DEV_SCHEMA_ROUTE, HEALTH_ROUTE, MANIFEST_REVISION_ROUTE, MANIFEST_ROUTE, NATIVE_OPENAPI_ROUTE, OPENAPI_FACADE_ROUTE, } from "./constants.js";
|
|
10
|
+
export { buildOpenApiFacadeResponse, mergeOpenApiMetadata } from "./facade.js";
|
|
11
|
+
export { createFacilisNitroModule, facilisNitroModule } from "./module.js";
|
|
12
|
+
export { scanRouteMetadata } from "./route-metadata.js";
|
|
13
|
+
export { facilis } from "./vite.js";
|
|
14
|
+
export { RESOLVED_OPENAPI_META_ID, VIRTUAL_CONFIG_ID, VIRTUAL_DB_SCHEMA_ID, VIRTUAL_JOBS_ID, VIRTUAL_OPENAPI_META_ID, VIRTUAL_WORKFLOWS_ID, } from "./virtual-ids.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { H3Event } from "nitro";
|
|
2
|
+
declare const handler: (event: H3Event) => Promise<void>;
|
|
3
|
+
/** The first address of x-forwarded-for: the client, before any proxy appends its own. */
|
|
4
|
+
export declare function firstForwardedFor(value: string | undefined): string | undefined;
|
|
5
|
+
export default handler;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { getRequestHeader, getRequestIP, setResponseHeader } from "nitro/h3";
|
|
2
|
+
import { acceptRequestId, newLogRequestId, } from "@facilis.dev/core";
|
|
3
|
+
import { getBootstrap } from "../runtime/bootstrap.js";
|
|
4
|
+
import { readDefaultTenantId } from "../runtime/tenant.js";
|
|
5
|
+
/**
|
|
6
|
+
* facilis-context middleware (contracts §4.1/§8, D-119).
|
|
7
|
+
*
|
|
8
|
+
* Boots the runtime lazily and exposes event.context.facilis — the db-backed routes, the
|
|
9
|
+
* single-tenant fallback id and the request logging runtime. It also owns the correlation id:
|
|
10
|
+
* an inbound x-request-id is accepted only when it is a safe token, and the resolved id is
|
|
11
|
+
* returned in the response so a client can correlate its request with the server log.
|
|
12
|
+
*
|
|
13
|
+
* When the db is unavailable the facade stays unset, so authorized handlers answer
|
|
14
|
+
* db_unavailable instead of running unscoped; the response still carries the request id.
|
|
15
|
+
*/
|
|
16
|
+
const REQUEST_ID_HEADER = "x-request-id";
|
|
17
|
+
const FORWARDED_FOR_HEADER = "x-forwarded-for";
|
|
18
|
+
const USER_AGENT_HEADER = "user-agent";
|
|
19
|
+
const handler = async (event) => {
|
|
20
|
+
const state = await getBootstrap();
|
|
21
|
+
const context = event.context;
|
|
22
|
+
// Logging is available even when the db never booted; the id is generated in that case so
|
|
23
|
+
// the failed-boot response is still correlatable with the process log.
|
|
24
|
+
const requestId = acceptRequestId(getRequestHeader(event, REQUEST_ID_HEADER)) ?? newLogRequestId();
|
|
25
|
+
setResponseHeader(event, REQUEST_ID_HEADER, requestId);
|
|
26
|
+
// The CSRF gate is structural, so it needs a value even when the db never booted: the
|
|
27
|
+
// config default (same-origin only) applies when the project config is unavailable.
|
|
28
|
+
context.security = { trustedOrigins: state.config?.security?.trustedOrigins ?? [] };
|
|
29
|
+
if (state.db === null)
|
|
30
|
+
return;
|
|
31
|
+
const client = resolveClient(event, state.loggingConfig);
|
|
32
|
+
// The wrapper's rate-limit key is resolved for every request, independently of the logging
|
|
33
|
+
// opt-ins (D-152): proxy trust decides whether the address identifies the caller, and leaving
|
|
34
|
+
// it unresolved means "no per-client throttle", never one shared window for every user.
|
|
35
|
+
const clientAddress = getRequestIP(event, {
|
|
36
|
+
xForwardedFor: state.loggingConfig.trustProxy,
|
|
37
|
+
});
|
|
38
|
+
context.facilis = {
|
|
39
|
+
db: state.db,
|
|
40
|
+
defaultTenantId: readDefaultTenantId(),
|
|
41
|
+
...(clientAddress === undefined ? {} : { clientAddress }),
|
|
42
|
+
// ctx.log and the access/error streams are structural for every authorized handler
|
|
43
|
+
// (D-119): attach the runtime and the one id all entries of this request share.
|
|
44
|
+
logging: state.logging,
|
|
45
|
+
requestId,
|
|
46
|
+
...(client === undefined ? {} : { client }),
|
|
47
|
+
// ctx.jobs is required by the handler contract: attach it whenever the runtime
|
|
48
|
+
// booted. Without it the wrapper substitutes a job_unavailable fallback (D-107).
|
|
49
|
+
...(state.jobs === null ? {} : { jobs: state.jobs.api }),
|
|
50
|
+
// Same contract for ctx.tasks (D-135): a missing runtime becomes a task_unavailable
|
|
51
|
+
// fallback in the wrapper, never a broken route.
|
|
52
|
+
...(state.tasks === null ? {} : { tasks: state.tasks.api }),
|
|
53
|
+
// Same contract for ctx.workflows (D-135): a missing runtime becomes a
|
|
54
|
+
// workflow_unavailable fallback in the wrapper, never a broken route.
|
|
55
|
+
...(state.workflows === null ? {} : { workflows: state.workflows.api }),
|
|
56
|
+
// Same contract for ctx.storage (D-053): missing runtime => route-level fallback.
|
|
57
|
+
...(state.storage === null ? {} : { storage: state.storage }),
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
/** Client metadata is opt-in: proxy trust and User-Agent collection are deployment decisions. */
|
|
61
|
+
function resolveClient(event, config) {
|
|
62
|
+
const userAgent = config.includeUserAgent
|
|
63
|
+
? readNonEmptyHeader(event, USER_AGENT_HEADER)
|
|
64
|
+
: undefined;
|
|
65
|
+
const ip = config.trustProxy
|
|
66
|
+
? firstForwardedFor(readNonEmptyHeader(event, FORWARDED_FOR_HEADER))
|
|
67
|
+
: undefined;
|
|
68
|
+
if (userAgent === undefined && ip === undefined)
|
|
69
|
+
return undefined;
|
|
70
|
+
return {
|
|
71
|
+
...(ip === undefined ? {} : { ip }),
|
|
72
|
+
...(userAgent === undefined ? {} : { userAgent }),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** The first address of x-forwarded-for: the client, before any proxy appends its own. */
|
|
76
|
+
export function firstForwardedFor(value) {
|
|
77
|
+
if (value === undefined)
|
|
78
|
+
return undefined;
|
|
79
|
+
const first = value.split(",")[0]?.trim();
|
|
80
|
+
return first === undefined || first.length === 0 ? undefined : first;
|
|
81
|
+
}
|
|
82
|
+
function readNonEmptyHeader(event, name) {
|
|
83
|
+
const value = getRequestHeader(event, name);
|
|
84
|
+
return value === undefined || value.trim().length === 0 ? undefined : value;
|
|
85
|
+
}
|
|
86
|
+
export default handler;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { H3Event } from "nitro";
|
|
2
|
+
/**
|
|
3
|
+
* facilis-cors middleware (SPEC §8).
|
|
4
|
+
*
|
|
5
|
+
* Runs before facilis-context so a preflight is answered without booting the db or auth.
|
|
6
|
+
* A returned Response short-circuits the request; otherwise the CORS headers are set on the
|
|
7
|
+
* response the route will produce.
|
|
8
|
+
*/
|
|
9
|
+
declare const handler: (event: H3Event) => Response | undefined;
|
|
10
|
+
export default handler;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getRequestHeader, setResponseHeader } from "nitro/h3";
|
|
2
|
+
import config from "#facilis/config";
|
|
3
|
+
import routeMetadata from "#facilis/openapi-meta";
|
|
4
|
+
import { corsHeadersFor, matchRoute, preflightResponse } from "../runtime/cors-policy.js";
|
|
5
|
+
/** Trusted origins come from security.trustedOrigins; anything malformed is ignored. */
|
|
6
|
+
function readTrustedOrigins(source) {
|
|
7
|
+
const security = source.security;
|
|
8
|
+
const origins = security?.trustedOrigins;
|
|
9
|
+
if (!Array.isArray(origins))
|
|
10
|
+
return [];
|
|
11
|
+
return origins.filter((origin) => typeof origin === "string");
|
|
12
|
+
}
|
|
13
|
+
const TRUSTED_ORIGINS = readTrustedOrigins(config);
|
|
14
|
+
/**
|
|
15
|
+
* facilis-cors middleware (SPEC §8).
|
|
16
|
+
*
|
|
17
|
+
* Runs before facilis-context so a preflight is answered without booting the db or auth.
|
|
18
|
+
* A returned Response short-circuits the request; otherwise the CORS headers are set on the
|
|
19
|
+
* response the route will produce.
|
|
20
|
+
*/
|
|
21
|
+
const handler = (event) => {
|
|
22
|
+
const pathname = new URL(event.req.url, "http://localhost").pathname;
|
|
23
|
+
const method = (event.req.method ?? "GET").toUpperCase();
|
|
24
|
+
if (method === "OPTIONS") {
|
|
25
|
+
return (preflightResponse(routeMetadata, pathname, event.req.headers, TRUSTED_ORIGINS) ?? undefined);
|
|
26
|
+
}
|
|
27
|
+
const origin = getRequestHeader(event, "origin") ?? null;
|
|
28
|
+
const trusted = origin !== null && TRUSTED_ORIGINS.includes(origin);
|
|
29
|
+
const headers = corsHeadersFor(origin, trusted, matchRoute(routeMetadata, pathname, method));
|
|
30
|
+
for (const [name, value] of headers)
|
|
31
|
+
setResponseHeader(event, name, value);
|
|
32
|
+
return undefined;
|
|
33
|
+
};
|
|
34
|
+
export default handler;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { H3Event } from "nitro";
|
|
2
|
+
/**
|
|
3
|
+
* facilis-session middleware (contracts §4.1).
|
|
4
|
+
*
|
|
5
|
+
* Runs after facilis-context: validates the session cookie on the root client and
|
|
6
|
+
* sets `event.context.auth` (`null` when anonymous). No db means no session.
|
|
7
|
+
*/
|
|
8
|
+
declare const handler: (event: H3Event) => Promise<void>;
|
|
9
|
+
export default handler;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getBootstrap, getSessionHandler } from "../runtime/bootstrap.js";
|
|
2
|
+
/**
|
|
3
|
+
* facilis-session middleware (contracts §4.1).
|
|
4
|
+
*
|
|
5
|
+
* Runs after facilis-context: validates the session cookie on the root client and
|
|
6
|
+
* sets `event.context.auth` (`null` when anonymous). No db means no session.
|
|
7
|
+
*/
|
|
8
|
+
const handler = async (event) => {
|
|
9
|
+
const state = await getBootstrap();
|
|
10
|
+
const context = event.context;
|
|
11
|
+
const sessionHandler = getSessionHandler(state);
|
|
12
|
+
if (sessionHandler === null) {
|
|
13
|
+
context.auth = null;
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
await sessionHandler(event);
|
|
17
|
+
};
|
|
18
|
+
export default handler;
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { NitroModule } from "nitro/types";
|
|
2
|
+
/**
|
|
3
|
+
* Facilis Nitro module (SPEC §5.6; nitro v3 has no defineNitroModule/kit, so it is a
|
|
4
|
+
* plain object). Carried by the `nitro` key of the `facilis()` Vite plugin.
|
|
5
|
+
*/
|
|
6
|
+
export declare function createFacilisNitroModule(): NitroModule;
|
|
7
|
+
export declare const facilisNitroModule: NitroModule;
|