inlaycms 0.0.0-stage → 0.1.0
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 +462 -2
- package/assets/admin/admin.css +2 -0
- package/assets/admin/admin.js +1 -0
- package/assets/admin/editor.js +125 -0
- package/dist/adapters/admin/activity.d.ts +17 -0
- package/dist/adapters/admin/activity.js +59 -0
- package/dist/adapters/admin/assets.d.ts +14 -0
- package/dist/adapters/admin/assets.js +64 -0
- package/dist/adapters/admin/attention.d.ts +15 -0
- package/dist/adapters/admin/attention.js +34 -0
- package/dist/adapters/admin/context.d.ts +40 -0
- package/dist/adapters/admin/context.js +142 -0
- package/dist/adapters/admin/form/walk.d.ts +61 -0
- package/dist/adapters/admin/form/walk.js +565 -0
- package/dist/adapters/admin/pages.d.ts +38 -0
- package/dist/adapters/admin/pages.js +66 -0
- package/dist/adapters/admin/publish.d.ts +12 -0
- package/dist/adapters/admin/publish.js +195 -0
- package/dist/adapters/admin/routes.d.ts +47 -0
- package/dist/adapters/admin/routes.js +255 -0
- package/dist/adapters/admin/screens/backups.d.ts +11 -0
- package/dist/adapters/admin/screens/backups.js +223 -0
- package/dist/adapters/admin/screens/dashboard.d.ts +4 -0
- package/dist/adapters/admin/screens/dashboard.js +160 -0
- package/dist/adapters/admin/screens/editor.d.ts +67 -0
- package/dist/adapters/admin/screens/editor.js +416 -0
- package/dist/adapters/admin/screens/history.d.ts +4 -0
- package/dist/adapters/admin/screens/history.js +105 -0
- package/dist/adapters/admin/screens/lists.d.ts +4 -0
- package/dist/adapters/admin/screens/lists.js +297 -0
- package/dist/adapters/admin/screens/media.d.ts +20 -0
- package/dist/adapters/admin/screens/media.js +186 -0
- package/dist/adapters/admin/screens/pickers.d.ts +29 -0
- package/dist/adapters/admin/screens/pickers.js +134 -0
- package/dist/adapters/admin/screens/preview-pane.d.ts +10 -0
- package/dist/adapters/admin/screens/preview-pane.js +74 -0
- package/dist/adapters/admin/screens/redirects.d.ts +4 -0
- package/dist/adapters/admin/screens/redirects.js +137 -0
- package/dist/adapters/admin/screens/releases.d.ts +7 -0
- package/dist/adapters/admin/screens/releases.js +287 -0
- package/dist/adapters/admin/screens/search.d.ts +22 -0
- package/dist/adapters/admin/screens/search.js +113 -0
- package/dist/adapters/admin/screens/security.d.ts +4 -0
- package/dist/adapters/admin/screens/security.js +85 -0
- package/dist/adapters/admin/screens/users.d.ts +4 -0
- package/dist/adapters/admin/screens/users.js +330 -0
- package/dist/adapters/admin/theme.d.ts +1 -0
- package/dist/adapters/admin/theme.js +1 -0
- package/dist/adapters/admin/translations.d.ts +16 -0
- package/dist/adapters/admin/translations.js +23 -0
- package/dist/adapters/admin/upload.d.ts +8 -0
- package/dist/adapters/admin/upload.js +116 -0
- package/dist/adapters/admin/views/data-table.d.ts +82 -0
- package/dist/adapters/admin/views/data-table.js +117 -0
- package/dist/adapters/admin/views/icons.d.ts +48 -0
- package/dist/adapters/admin/views/icons.js +47 -0
- package/dist/adapters/admin/views/layout.d.ts +51 -0
- package/dist/adapters/admin/views/layout.js +145 -0
- package/dist/adapters/admin/views/sheet.d.ts +15 -0
- package/dist/adapters/admin/views/sheet.js +6 -0
- package/dist/adapters/admin/views/status.d.ts +32 -0
- package/dist/adapters/admin/views/status.js +74 -0
- package/dist/adapters/auth/handler.d.ts +10 -0
- package/dist/adapters/auth/handler.js +63 -0
- package/dist/adapters/auth/request.d.ts +8 -0
- package/dist/adapters/auth/request.js +14 -0
- package/dist/adapters/cli/bin.d.ts +2 -0
- package/dist/adapters/cli/bin.js +42 -0
- package/dist/adapters/cli/main.d.ts +7 -0
- package/dist/adapters/cli/main.js +982 -0
- package/dist/adapters/cli/remote.d.ts +52 -0
- package/dist/adapters/cli/remote.js +161 -0
- package/dist/adapters/mcp/agent-refs.d.ts +9 -0
- package/dist/adapters/mcp/agent-refs.js +25 -0
- package/dist/adapters/mcp/documents.d.ts +15 -0
- package/dist/adapters/mcp/documents.js +26 -0
- package/dist/adapters/mcp/fetch-url.d.ts +19 -0
- package/dist/adapters/mcp/fetch-url.js +244 -0
- package/dist/adapters/mcp/request-state.d.ts +15 -0
- package/dist/adapters/mcp/request-state.js +38 -0
- package/dist/adapters/mcp/results.d.ts +20 -0
- package/dist/adapters/mcp/results.js +61 -0
- package/dist/adapters/mcp/route.d.ts +34 -0
- package/dist/adapters/mcp/route.js +52 -0
- package/dist/adapters/mcp/server.d.ts +44 -0
- package/dist/adapters/mcp/server.js +49 -0
- package/dist/adapters/mcp/tools/history.d.ts +5 -0
- package/dist/adapters/mcp/tools/history.js +84 -0
- package/dist/adapters/mcp/tools/media.d.ts +5 -0
- package/dist/adapters/mcp/tools/media.js +102 -0
- package/dist/adapters/mcp/tools/publish.d.ts +5 -0
- package/dist/adapters/mcp/tools/publish.js +157 -0
- package/dist/adapters/mcp/tools/read.d.ts +5 -0
- package/dist/adapters/mcp/tools/read.js +279 -0
- package/dist/adapters/mcp/tools/redirects.d.ts +5 -0
- package/dist/adapters/mcp/tools/redirects.js +74 -0
- package/dist/adapters/mcp/tools/write.d.ts +5 -0
- package/dist/adapters/mcp/tools/write.js +304 -0
- package/dist/adapters/site/backup-schedule.d.ts +8 -0
- package/dist/adapters/site/backup-schedule.js +31 -0
- package/dist/adapters/site/cache.d.ts +29 -0
- package/dist/adapters/site/cache.js +63 -0
- package/dist/adapters/site/context.d.ts +29 -0
- package/dist/adapters/site/context.js +278 -0
- package/dist/adapters/site/head.d.ts +23 -0
- package/dist/adapters/site/head.js +44 -0
- package/dist/adapters/site/limiter.d.ts +17 -0
- package/dist/adapters/site/limiter.js +39 -0
- package/dist/adapters/site/lock.d.ts +30 -0
- package/dist/adapters/site/lock.js +143 -0
- package/dist/adapters/site/media-routes.d.ts +4 -0
- package/dist/adapters/site/media-routes.js +55 -0
- package/dist/adapters/site/plugin.d.ts +122 -0
- package/dist/adapters/site/plugin.js +436 -0
- package/dist/adapters/site/preview.d.ts +36 -0
- package/dist/adapters/site/preview.js +82 -0
- package/dist/adapters/site/public.d.ts +6 -0
- package/dist/adapters/site/public.js +87 -0
- package/dist/adapters/site/render.d.ts +45 -0
- package/dist/adapters/site/render.js +191 -0
- package/dist/adapters/site/routes.d.ts +17 -0
- package/dist/adapters/site/routes.js +125 -0
- package/dist/adapters/site/sitemap.d.ts +10 -0
- package/dist/adapters/site/sitemap.js +10 -0
- package/dist/adapters/web.d.ts +22 -0
- package/dist/adapters/web.js +75 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +58 -0
- package/dist/core/agent/format.d.ts +62 -0
- package/dist/core/agent/format.js +211 -0
- package/dist/core/agent/schema.d.ts +6 -0
- package/dist/core/agent/schema.js +86 -0
- package/dist/core/archive.d.ts +12 -0
- package/dist/core/archive.js +72 -0
- package/dist/core/auth/auth.d.ts +4911 -0
- package/dist/core/auth/auth.js +61 -0
- package/dist/core/auth/roles.d.ts +10 -0
- package/dist/core/auth/roles.js +38 -0
- package/dist/core/auth/service.d.ts +118 -0
- package/dist/core/auth/service.js +424 -0
- package/dist/core/auth/upload-token.d.ts +8 -0
- package/dist/core/auth/upload-token.js +27 -0
- package/dist/core/backup/backup.d.ts +28 -0
- package/dist/core/backup/backup.js +126 -0
- package/dist/core/backup/restore.d.ts +41 -0
- package/dist/core/backup/restore.js +264 -0
- package/dist/core/backup/schedule.d.ts +12 -0
- package/dist/core/backup/schedule.js +29 -0
- package/dist/core/boot.d.ts +63 -0
- package/dist/core/boot.js +141 -0
- package/dist/core/check.d.ts +32 -0
- package/dist/core/check.js +111 -0
- package/dist/core/content/activity.d.ts +8 -0
- package/dist/core/content/activity.js +101 -0
- package/dist/core/content/delete.d.ts +5 -0
- package/dist/core/content/delete.js +100 -0
- package/dist/core/content/diff.d.ts +6 -0
- package/dist/core/content/diff.js +129 -0
- package/dist/core/content/dry-run.d.ts +24 -0
- package/dist/core/content/dry-run.js +69 -0
- package/dist/core/content/gaps.d.ts +25 -0
- package/dist/core/content/gaps.js +50 -0
- package/dist/core/content/hidden.d.ts +33 -0
- package/dist/core/content/hidden.js +103 -0
- package/dist/core/content/history.d.ts +15 -0
- package/dist/core/content/history.js +62 -0
- package/dist/core/content/patch.d.ts +40 -0
- package/dist/core/content/patch.js +287 -0
- package/dist/core/content/path.d.ts +6 -0
- package/dist/core/content/path.js +38 -0
- package/dist/core/content/publish.d.ts +26 -0
- package/dist/core/content/publish.js +211 -0
- package/dist/core/content/read.d.ts +99 -0
- package/dist/core/content/read.js +141 -0
- package/dist/core/content/reader.d.ts +37 -0
- package/dist/core/content/reader.js +195 -0
- package/dist/core/content/refs.d.ts +44 -0
- package/dist/core/content/refs.js +161 -0
- package/dist/core/content/release-migrations.d.ts +12 -0
- package/dist/core/content/release-migrations.js +154 -0
- package/dist/core/content/save.d.ts +25 -0
- package/dist/core/content/save.js +87 -0
- package/dist/core/content/schema.d.ts +22 -0
- package/dist/core/content/schema.js +127 -0
- package/dist/core/content/search.d.ts +8 -0
- package/dist/core/content/search.js +89 -0
- package/dist/core/content/shipped-input.d.ts +39 -0
- package/dist/core/content/shipped-input.js +221 -0
- package/dist/core/content/shipped.d.ts +112 -0
- package/dist/core/content/shipped.js +437 -0
- package/dist/core/content/slug.d.ts +23 -0
- package/dist/core/content/slug.js +182 -0
- package/dist/core/content/stats.d.ts +11 -0
- package/dist/core/content/stats.js +30 -0
- package/dist/core/content/translate.d.ts +3 -0
- package/dist/core/content/translate.js +68 -0
- package/dist/core/content/usages.d.ts +13 -0
- package/dist/core/content/usages.js +70 -0
- package/dist/core/content/validate.d.ts +21 -0
- package/dist/core/content/validate.js +124 -0
- package/dist/core/content/write.d.ts +10 -0
- package/dist/core/content/write.js +93 -0
- package/dist/core/core.d.ts +227 -0
- package/dist/core/core.js +251 -0
- package/dist/core/db/connection.d.ts +6 -0
- package/dist/core/db/connection.js +12 -0
- package/dist/core/db/migrate.d.ts +9 -0
- package/dist/core/db/migrate.js +42 -0
- package/dist/core/db/migrations/0001_content.d.ts +2 -0
- package/dist/core/db/migrations/0001_content.js +69 -0
- package/dist/core/db/migrations/0002_media.d.ts +2 -0
- package/dist/core/db/migrations/0002_media.js +26 -0
- package/dist/core/db/migrations/0003_redirects.d.ts +2 -0
- package/dist/core/db/migrations/0003_redirects.js +19 -0
- package/dist/core/db/migrations/0004_token_cutoffs.d.ts +3 -0
- package/dist/core/db/migrations/0004_token_cutoffs.js +8 -0
- package/dist/core/db/migrations/0005_site_lock.d.ts +3 -0
- package/dist/core/db/migrations/0005_site_lock.js +10 -0
- package/dist/core/db/migrations/0006_releases.d.ts +3 -0
- package/dist/core/db/migrations/0006_releases.js +34 -0
- package/dist/core/db/schema.d.ts +155 -0
- package/dist/core/db/schema.js +1 -0
- package/dist/core/errors.d.ts +27 -0
- package/dist/core/errors.js +15 -0
- package/dist/core/export/export.d.ts +17 -0
- package/dist/core/export/export.js +185 -0
- package/dist/core/export/format.d.ts +241 -0
- package/dist/core/export/format.js +106 -0
- package/dist/core/export/import.d.ts +54 -0
- package/dist/core/export/import.js +180 -0
- package/dist/core/export/read.d.ts +16 -0
- package/dist/core/export/read.js +234 -0
- package/dist/core/export/replace.d.ts +17 -0
- package/dist/core/export/replace.js +36 -0
- package/dist/core/export/report.d.ts +55 -0
- package/dist/core/export/report.js +50 -0
- package/dist/core/export/schema.d.ts +8 -0
- package/dist/core/export/schema.js +47 -0
- package/dist/core/export/stage.d.ts +25 -0
- package/dist/core/export/stage.js +192 -0
- package/dist/core/fields/builtins.d.ts +70 -0
- package/dist/core/fields/builtins.js +335 -0
- package/dist/core/fields/define.d.ts +86 -0
- package/dist/core/fields/define.js +34 -0
- package/dist/core/fields/walk.d.ts +49 -0
- package/dist/core/fields/walk.js +270 -0
- package/dist/core/fields/widgets.d.ts +27 -0
- package/dist/core/fields/widgets.js +330 -0
- package/dist/core/fields/zod-internals.d.ts +29 -0
- package/dist/core/fields/zod-internals.js +28 -0
- package/dist/core/media/detect.d.ts +5 -0
- package/dist/core/media/detect.js +66 -0
- package/dist/core/media/labels.d.ts +6 -0
- package/dist/core/media/labels.js +17 -0
- package/dist/core/media/library.d.ts +45 -0
- package/dist/core/media/library.js +239 -0
- package/dist/core/media/store.d.ts +11 -0
- package/dist/core/media/store.js +49 -0
- package/dist/core/media/variants.d.ts +10 -0
- package/dist/core/media/variants.js +59 -0
- package/dist/core/messages/en.d.ts +1278 -0
- package/dist/core/messages/en.js +1287 -0
- package/dist/core/messages/index.d.ts +7 -0
- package/dist/core/messages/index.js +14 -0
- package/dist/core/preview.d.ts +16 -0
- package/dist/core/preview.js +42 -0
- package/dist/core/redirects/collapse.d.ts +68 -0
- package/dist/core/redirects/collapse.js +153 -0
- package/dist/core/redirects/csv.d.ts +15 -0
- package/dist/core/redirects/csv.js +68 -0
- package/dist/core/redirects/redirects.d.ts +58 -0
- package/dist/core/redirects/redirects.js +545 -0
- package/dist/core/registry/content.d.ts +26 -0
- package/dist/core/registry/content.js +9 -0
- package/dist/core/registry/define.d.ts +145 -0
- package/dist/core/registry/define.js +41 -0
- package/dist/core/registry/migration.d.ts +44 -0
- package/dist/core/registry/migration.js +4 -0
- package/dist/core/registry/registry.d.ts +18 -0
- package/dist/core/registry/registry.js +255 -0
- package/dist/core/registry/render.d.ts +98 -0
- package/dist/core/registry/render.js +1 -0
- package/dist/core/registry/route.d.ts +42 -0
- package/dist/core/registry/route.js +3 -0
- package/dist/core/release/bundle.d.ts +21 -0
- package/dist/core/release/bundle.js +252 -0
- package/dist/core/release/keys.d.ts +11 -0
- package/dist/core/release/keys.js +60 -0
- package/dist/core/release/manifest.d.ts +103 -0
- package/dist/core/release/manifest.js +138 -0
- package/dist/core/release/pack.d.ts +20 -0
- package/dist/core/release/pack.js +97 -0
- package/dist/core/release/pointer.d.ts +19 -0
- package/dist/core/release/pointer.js +53 -0
- package/dist/core/release/read.d.ts +59 -0
- package/dist/core/release/read.js +256 -0
- package/dist/core/report.d.ts +31 -0
- package/dist/core/report.js +37 -0
- package/dist/core/richtext/html.d.ts +15 -0
- package/dist/core/richtext/html.js +81 -0
- package/dist/core/richtext/markdown.d.ts +19 -0
- package/dist/core/richtext/markdown.js +951 -0
- package/dist/core/richtext/schema.d.ts +98 -0
- package/dist/core/richtext/schema.js +275 -0
- package/dist/core/site/live.d.ts +56 -0
- package/dist/core/site/live.js +145 -0
- package/dist/core/site/paths.d.ts +15 -0
- package/dist/core/site/paths.js +64 -0
- package/dist/core/site/resolve.d.ts +9 -0
- package/dist/core/site/resolve.js +202 -0
- package/dist/core/site/sitemap.d.ts +11 -0
- package/dist/core/site/sitemap.js +24 -0
- package/dist/core/site-lock/site-lock.d.ts +21 -0
- package/dist/core/site-lock/site-lock.js +65 -0
- package/dist/core/site-lock/token.d.ts +7 -0
- package/dist/core/site-lock/token.js +24 -0
- package/dist/core/theme.d.ts +22 -0
- package/dist/core/theme.js +133 -0
- package/dist/core/types.d.ts +253 -0
- package/dist/core/types.js +1 -0
- package/dist/core/version.d.ts +1 -0
- package/dist/core/version.js +5 -0
- package/dist/host/activate.d.ts +53 -0
- package/dist/host/activate.js +162 -0
- package/dist/host/app.d.ts +46 -0
- package/dist/host/app.js +129 -0
- package/dist/host/bin.d.ts +5 -0
- package/dist/host/bin.js +8 -0
- package/dist/host/config.d.ts +36 -0
- package/dist/host/config.js +152 -0
- package/dist/host/dev.d.ts +10 -0
- package/dist/host/dev.js +293 -0
- package/dist/host/diff.d.ts +78 -0
- package/dist/host/diff.js +346 -0
- package/dist/host/hooks.d.ts +41 -0
- package/dist/host/hooks.js +49 -0
- package/dist/host/ipc.d.ts +123 -0
- package/dist/host/ipc.js +61 -0
- package/dist/host/preview-proxy.d.ts +24 -0
- package/dist/host/preview-proxy.js +154 -0
- package/dist/host/preview-worker.d.ts +42 -0
- package/dist/host/preview-worker.js +242 -0
- package/dist/host/primary.d.ts +38 -0
- package/dist/host/primary.js +357 -0
- package/dist/host/push.d.ts +125 -0
- package/dist/host/push.js +420 -0
- package/dist/host/serve.d.ts +16 -0
- package/dist/host/serve.js +150 -0
- package/dist/host/store.d.ts +48 -0
- package/dist/host/store.js +109 -0
- package/dist/host/worker.d.ts +67 -0
- package/dist/host/worker.js +310 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +32 -0
- package/dist/jsx/html.d.ts +16 -0
- package/dist/jsx/html.js +65 -0
- package/dist/jsx/jsx-dev-runtime.d.ts +4 -0
- package/dist/jsx/jsx-dev-runtime.js +5 -0
- package/dist/jsx/jsx-runtime.d.ts +17 -0
- package/dist/jsx/jsx-runtime.js +41 -0
- package/dist/site.d.ts +17 -0
- package/dist/site.js +14 -0
- package/package.json +111 -3
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ActivationSummary } from '../../host/ipc.js';
|
|
2
|
+
import type { ReleaseDiff, ReleaseInfo } from '../../host/push.js';
|
|
3
|
+
/** What the server answers to a push. */
|
|
4
|
+
export interface PushAnswer {
|
|
5
|
+
/** The release kept on the server; null for a dry run. */
|
|
6
|
+
release: ReleaseInfo | null;
|
|
7
|
+
diff: ReleaseDiff;
|
|
8
|
+
/** What making it live did; null without `activate`, or when that failed (see `failed`). */
|
|
9
|
+
activation: ActivationSummary | null;
|
|
10
|
+
warnings: string[];
|
|
11
|
+
/** Why the release, kept on the server, couldn't be made live; null when it was, or wasn't asked to be. */
|
|
12
|
+
failed: string | null;
|
|
13
|
+
/** A signed link that previews the staged release; null when there is none (a dry run, or made live). */
|
|
14
|
+
previewUrl: string | null;
|
|
15
|
+
}
|
|
16
|
+
export interface ReleaseList {
|
|
17
|
+
releases: ReleaseInfo[];
|
|
18
|
+
current: string | null;
|
|
19
|
+
}
|
|
20
|
+
export interface ActivateAnswer {
|
|
21
|
+
release: ReleaseInfo;
|
|
22
|
+
activation: ActivationSummary;
|
|
23
|
+
}
|
|
24
|
+
export interface RemoteOptions {
|
|
25
|
+
/** Lets the token go over plain http:// to a server that isn't this computer. */
|
|
26
|
+
insecure?: boolean;
|
|
27
|
+
/** How long the server may send nothing, once the request is sent, before Inlay gives up: 15 minutes by default. */
|
|
28
|
+
timeoutMs?: number;
|
|
29
|
+
}
|
|
30
|
+
/** A push or an activation waits for the activation, which may take up to 10 minutes on the server. */
|
|
31
|
+
export declare const REMOTE_TIMEOUT_MS: number;
|
|
32
|
+
/** Whether `arg` is a server address rather than a file, an ID or a folder. */
|
|
33
|
+
export declare const isRemote: (arg: string | undefined) => arg is string;
|
|
34
|
+
/**
|
|
35
|
+
* Refuses (`cli.pushInsecure`, a `config` error) to send the push token over plain http:// to anything but this
|
|
36
|
+
* computer, unless `insecure`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function checkTransport(host: string, insecure: boolean): void;
|
|
39
|
+
/**
|
|
40
|
+
* Pushes the release file `file` to `host`. A refusal throws `error.remoteRefused` with the server's message,
|
|
41
|
+
* except a release kept on the server that then couldn't be made live: that answers with `failed` set, so the diff
|
|
42
|
+
* is still shown.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pushFile(host: string, token: string, file: string, opts?: {
|
|
45
|
+
activate?: boolean;
|
|
46
|
+
dryRun?: boolean;
|
|
47
|
+
name?: string;
|
|
48
|
+
} & RemoteOptions): Promise<PushAnswer>;
|
|
49
|
+
/** The releases on `host`, newest first, with the current one's ID. */
|
|
50
|
+
export declare function listRemoteReleases(host: string, token: string, opts?: RemoteOptions): Promise<ReleaseList>;
|
|
51
|
+
/** Makes the release `id` on `host` live. */
|
|
52
|
+
export declare function activateRemote(host: string, token: string, id: string, opts?: RemoteOptions): Promise<ActivateAnswer>;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createReadStream } from 'node:fs';
|
|
2
|
+
import { stat } from 'node:fs/promises';
|
|
3
|
+
import { request as httpRequest } from 'node:http';
|
|
4
|
+
import { request as httpsRequest } from 'node:https';
|
|
5
|
+
import { InlayError } from '../../core/errors.js';
|
|
6
|
+
/** A push or an activation waits for the activation, which may take up to 10 minutes on the server. */
|
|
7
|
+
export const REMOTE_TIMEOUT_MS = 15 * 60_000;
|
|
8
|
+
const isObject = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
9
|
+
/** Whether `arg` is a server address rather than a file, an ID or a folder. */
|
|
10
|
+
export const isRemote = (arg) => arg !== undefined && /^https?:\/\//i.test(arg);
|
|
11
|
+
const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]']);
|
|
12
|
+
/**
|
|
13
|
+
* Refuses (`cli.pushInsecure`, a `config` error) to send the push token over plain http:// to anything but this
|
|
14
|
+
* computer, unless `insecure`.
|
|
15
|
+
*/
|
|
16
|
+
export function checkTransport(host, insecure) {
|
|
17
|
+
let url;
|
|
18
|
+
try {
|
|
19
|
+
url = new URL(host);
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
throw new InlayError('config', 'cli.pushHost', { host });
|
|
23
|
+
}
|
|
24
|
+
if (url.protocol === 'http:' && !LOOPBACK.has(url.hostname.toLowerCase()) && !insecure) {
|
|
25
|
+
throw new InlayError('config', 'cli.pushInsecure', { host });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** The address of the push API on `host`, which may sit under a path. */
|
|
29
|
+
function releasesUrl(host, path = '') {
|
|
30
|
+
return `${host.replace(/\/+$/, '')}/_inlay/releases${path}`;
|
|
31
|
+
}
|
|
32
|
+
/** The server's plain-language error from an `{ error: { message } }` body, if it sent one. */
|
|
33
|
+
function errorOf(body) {
|
|
34
|
+
return isObject(body) && isObject(body.error) && typeof body.error.message === 'string' ? body.error.message : null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Sends one request with node's own client (no redirects followed, no proxy) on a connection of its own, never one
|
|
38
|
+
* kept from an earlier request (whose worker may be shutting down), and reads the whole answer. `sent` turns true
|
|
39
|
+
* once the request, body and all, has gone out.
|
|
40
|
+
*/
|
|
41
|
+
function send(url, method, headers, file, timeoutMs, sent) {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
const request = (url.protocol === 'https:' ? httpsRequest : httpRequest)(url, { method, headers, agent: false }, (res) => {
|
|
44
|
+
const chunks = [];
|
|
45
|
+
res.on('data', (chunk) => chunks.push(chunk));
|
|
46
|
+
res.on('error', reject);
|
|
47
|
+
res.on('end', () => resolve({ status: res.statusCode ?? 0, text: Buffer.concat(chunks).toString('utf8'), location: res.headers.location }));
|
|
48
|
+
});
|
|
49
|
+
request.setTimeout(timeoutMs, () => {
|
|
50
|
+
request.destroy(new InlayError('release', 'error.remoteTimeout', { seconds: Math.round(timeoutMs / 1000) }));
|
|
51
|
+
});
|
|
52
|
+
request.on('error', reject);
|
|
53
|
+
request.on('finish', () => (sent.done = true));
|
|
54
|
+
if (file === undefined)
|
|
55
|
+
return void request.end();
|
|
56
|
+
const body = createReadStream(file);
|
|
57
|
+
body.on('error', (e) => request.destroy(e));
|
|
58
|
+
body.pipe(request);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Sends one request to the push API with the token and answers the status and parsed JSON body (null for none).
|
|
63
|
+
* A server that can't be reached is `error.remoteUnreachable`, a redirect `error.remoteRedirect` (the token is never
|
|
64
|
+
* sent on), and a body that isn't JSON `error.remoteAnswer`. With `mayHaveWorked` (a push or an activation), a
|
|
65
|
+
* request that went out but got no answer in time, lost its connection, or got a 5xx that isn't the server's own
|
|
66
|
+
* error is `cli.pushUnknown`: it may still have gone through.
|
|
67
|
+
*/
|
|
68
|
+
async function call(host, token, path, req, opts) {
|
|
69
|
+
checkTransport(host, opts.insecure === true);
|
|
70
|
+
const headers = { authorization: `Bearer ${token}`, accept: 'application/json' };
|
|
71
|
+
if (req.file !== undefined) {
|
|
72
|
+
headers['content-type'] = 'application/gzip';
|
|
73
|
+
headers['content-length'] = (await stat(req.file)).size;
|
|
74
|
+
}
|
|
75
|
+
else if (req.method === 'POST')
|
|
76
|
+
headers['content-length'] = 0;
|
|
77
|
+
const sent = { done: false };
|
|
78
|
+
const unknown = (problem) => new InlayError('release', 'cli.pushUnknown', { host, problem });
|
|
79
|
+
let res;
|
|
80
|
+
try {
|
|
81
|
+
res = await send(new URL(releasesUrl(host, path)), req.method, headers, req.file, opts.timeoutMs ?? REMOTE_TIMEOUT_MS, sent);
|
|
82
|
+
}
|
|
83
|
+
catch (e) {
|
|
84
|
+
const cause = e instanceof Error && e.cause instanceof Error ? e.cause : e;
|
|
85
|
+
const problem = cause instanceof Error ? cause.message : String(cause);
|
|
86
|
+
if (req.mayHaveWorked && sent.done)
|
|
87
|
+
throw unknown(problem);
|
|
88
|
+
throw new InlayError('release', 'error.remoteUnreachable', { host, problem });
|
|
89
|
+
}
|
|
90
|
+
if (res.status >= 300 && res.status < 400) {
|
|
91
|
+
throw new InlayError('release', 'error.remoteRedirect', { host, location: res.location ?? '' });
|
|
92
|
+
}
|
|
93
|
+
let body = null;
|
|
94
|
+
if (res.text !== '') {
|
|
95
|
+
try {
|
|
96
|
+
body = JSON.parse(res.text);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
body = undefined;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (res.status >= 500 && req.mayHaveWorked && errorOf(body) === null) {
|
|
103
|
+
throw unknown(new InlayError('release', 'error.remoteStatus', { status: res.status }).message);
|
|
104
|
+
}
|
|
105
|
+
if (body === undefined)
|
|
106
|
+
throw new InlayError('release', 'error.remoteAnswer', { host, status: res.status });
|
|
107
|
+
return { status: res.status, body };
|
|
108
|
+
}
|
|
109
|
+
/** The server's refusal as an error: its own message, or `error.remoteAnswer` when it sent none. */
|
|
110
|
+
function refused(host, status, body) {
|
|
111
|
+
const message = errorOf(body);
|
|
112
|
+
return message === null
|
|
113
|
+
? new InlayError('release', 'error.remoteAnswer', { host, status })
|
|
114
|
+
: new InlayError('release', 'error.remoteRefused', { host, message });
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Pushes the release file `file` to `host`. A refusal throws `error.remoteRefused` with the server's message,
|
|
118
|
+
* except a release kept on the server that then couldn't be made live: that answers with `failed` set, so the diff
|
|
119
|
+
* is still shown.
|
|
120
|
+
*/
|
|
121
|
+
export async function pushFile(host, token, file, opts = {}) {
|
|
122
|
+
const query = new URLSearchParams();
|
|
123
|
+
if (opts.activate)
|
|
124
|
+
query.set('activate', '1');
|
|
125
|
+
if (opts.dryRun)
|
|
126
|
+
query.set('dryRun', '1');
|
|
127
|
+
if (opts.name !== undefined)
|
|
128
|
+
query.set('name', opts.name);
|
|
129
|
+
const search = query.size > 0 ? `?${query}` : '';
|
|
130
|
+
const { status, body } = await call(host, token, search, { method: 'POST', file, mayHaveWorked: !opts.dryRun }, opts);
|
|
131
|
+
if (!isObject(body) || !isObject(body.diff))
|
|
132
|
+
throw refused(host, status, body);
|
|
133
|
+
const answer = {
|
|
134
|
+
release: isObject(body.release) ? body.release : null,
|
|
135
|
+
diff: body.diff,
|
|
136
|
+
activation: isObject(body.activation) ? body.activation : null,
|
|
137
|
+
warnings: Array.isArray(body.warnings) ? body.warnings.filter((w) => typeof w === 'string') : [],
|
|
138
|
+
failed: null,
|
|
139
|
+
previewUrl: typeof body.previewUrl === 'string' ? body.previewUrl : null,
|
|
140
|
+
};
|
|
141
|
+
if (status >= 200 && status < 300)
|
|
142
|
+
return answer;
|
|
143
|
+
const failed = errorOf(body);
|
|
144
|
+
if (failed === null || answer.release === null)
|
|
145
|
+
throw refused(host, status, body);
|
|
146
|
+
return { ...answer, activation: null, failed };
|
|
147
|
+
}
|
|
148
|
+
/** The releases on `host`, newest first, with the current one's ID. */
|
|
149
|
+
export async function listRemoteReleases(host, token, opts = {}) {
|
|
150
|
+
const { status, body } = await call(host, token, '', { method: 'GET' }, opts);
|
|
151
|
+
if (status !== 200 || !isObject(body) || !Array.isArray(body.releases))
|
|
152
|
+
throw refused(host, status, body);
|
|
153
|
+
return { releases: body.releases, current: typeof body.current === 'string' ? body.current : null };
|
|
154
|
+
}
|
|
155
|
+
/** Makes the release `id` on `host` live. */
|
|
156
|
+
export async function activateRemote(host, token, id, opts = {}) {
|
|
157
|
+
const { status, body } = await call(host, token, `/${encodeURIComponent(id)}/activate`, { method: 'POST', mayHaveWorked: true }, opts);
|
|
158
|
+
if (status !== 200 || !isObject(body) || !isObject(body.release) || !isObject(body.activation))
|
|
159
|
+
throw refused(host, status, body);
|
|
160
|
+
return { release: body.release, activation: body.activation };
|
|
161
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type AgentRefs } from '../../core/agent/format.js';
|
|
2
|
+
import type { DocumentData } from '../../core/types.js';
|
|
3
|
+
import type { McpDeps } from './server.js';
|
|
4
|
+
/** The absolute URL of a decoded site path, or `null` for a document without a page. */
|
|
5
|
+
export declare function pageUrl(baseUrl: string, path: string | null): string | null;
|
|
6
|
+
/** The absolute URL of a media file. */
|
|
7
|
+
export declare function mediaUrl(baseUrl: string, id: string): string;
|
|
8
|
+
/** Everything `toAgent` and `agentLinks` look up for `data`, read up front in `locale` (drafts included). */
|
|
9
|
+
export declare function loadAgentRefs(deps: Pick<McpDeps, 'core' | 'baseUrl'>, type: string, locale: string, data: DocumentData): Promise<AgentRefs>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { agentRefsNeeded } from '../../core/agent/format.js';
|
|
2
|
+
import { encodePath } from '../../core/site/paths.js';
|
|
3
|
+
/** The absolute URL of a decoded site path, or `null` for a document without a page. */
|
|
4
|
+
export function pageUrl(baseUrl, path) {
|
|
5
|
+
return path === null ? null : `${baseUrl}${encodePath(path)}`;
|
|
6
|
+
}
|
|
7
|
+
/** The absolute URL of a media file. */
|
|
8
|
+
export function mediaUrl(baseUrl, id) {
|
|
9
|
+
return `${baseUrl}/media/${id}`;
|
|
10
|
+
}
|
|
11
|
+
/** Everything `toAgent` and `agentLinks` look up for `data`, read up front in `locale` (drafts included). */
|
|
12
|
+
export async function loadAgentRefs(deps, type, locale, data) {
|
|
13
|
+
const { core, baseUrl } = deps;
|
|
14
|
+
const needed = agentRefsNeeded(core.registry, type, data);
|
|
15
|
+
const documents = await core.liveDocuments(needed.documents, locale, { state: 'preview' });
|
|
16
|
+
const media = await core.lookupMedia(needed.media);
|
|
17
|
+
return {
|
|
18
|
+
document(groupId) {
|
|
19
|
+
const doc = documents.get(groupId);
|
|
20
|
+
return doc ? { title: doc.title, url: pageUrl(baseUrl, doc.path) } : null;
|
|
21
|
+
},
|
|
22
|
+
media: (id) => media.get(id) ?? { status: 'unknown' },
|
|
23
|
+
mediaUrl: (id) => mediaUrl(baseUrl, id),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ToolAnnotations } from '@modelcontextprotocol/server';
|
|
2
|
+
import type { DocumentSummary, DocumentView, LiveState } from '../../core/types.js';
|
|
3
|
+
import type { McpDeps } from './server.js';
|
|
4
|
+
/** Reads only. */
|
|
5
|
+
export declare const READ_ONLY: ToolAnnotations;
|
|
6
|
+
/** Adds or changes something; repeating the call does it again. */
|
|
7
|
+
export declare const EDIT: ToolAnnotations;
|
|
8
|
+
/** Changes something; repeating the call changes nothing more. */
|
|
9
|
+
export declare const IDEMPOTENT_EDIT: ToolAnnotations;
|
|
10
|
+
/** Removes something, or takes it off the site. */
|
|
11
|
+
export declare const DESTRUCTIVE: ToolAnnotations;
|
|
12
|
+
/** The head of document `id`, or a plain `not_found`. */
|
|
13
|
+
export declare function documentOf(deps: Pick<McpDeps, 'core'>, id: string): Promise<DocumentView>;
|
|
14
|
+
/** The document's site path in `state` (preview by default), or `null` when it has no page of its own there. */
|
|
15
|
+
export declare function livePath(deps: Pick<McpDeps, 'core'>, doc: Pick<DocumentSummary, 'id' | 'groupId' | 'locale'>, state?: LiveState): Promise<string | null>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { InlayError } from '../../core/errors.js';
|
|
2
|
+
/** Reads only. */
|
|
3
|
+
export const READ_ONLY = { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false };
|
|
4
|
+
/** Adds or changes something; repeating the call does it again. */
|
|
5
|
+
export const EDIT = { readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false };
|
|
6
|
+
/** Changes something; repeating the call changes nothing more. */
|
|
7
|
+
export const IDEMPOTENT_EDIT = {
|
|
8
|
+
readOnlyHint: false,
|
|
9
|
+
destructiveHint: false,
|
|
10
|
+
idempotentHint: true,
|
|
11
|
+
openWorldHint: false,
|
|
12
|
+
};
|
|
13
|
+
/** Removes something, or takes it off the site. */
|
|
14
|
+
export const DESTRUCTIVE = { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false };
|
|
15
|
+
/** The head of document `id`, or a plain `not_found`. */
|
|
16
|
+
export async function documentOf(deps, id) {
|
|
17
|
+
const doc = await deps.core.getDocument(id);
|
|
18
|
+
if (!doc || doc.deletedAt !== null)
|
|
19
|
+
throw new InlayError('not_found', 'mcp.documentNotFound', { id });
|
|
20
|
+
return doc;
|
|
21
|
+
}
|
|
22
|
+
/** The document's site path in `state` (preview by default), or `null` when it has no page of its own there. */
|
|
23
|
+
export async function livePath(deps, doc, state = 'preview') {
|
|
24
|
+
const live = (await deps.core.liveDocuments([doc.groupId], doc.locale, { state })).get(doc.groupId);
|
|
25
|
+
return live && live.documentId === doc.id ? live.path : null;
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether `address` (an IP address, not a host name) is one the server may fetch from: anything but loopback,
|
|
3
|
+
* private, link-local, CGNAT, unspecified, multicast or reserved addresses, including IPv6 forms that carry one.
|
|
4
|
+
*/
|
|
5
|
+
export declare function isPublicAddress(address: string): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Downloads `url` for the media library, only from addresses `isAllowed` accepts (public ones by default).
|
|
8
|
+
* Every address of each host is checked before connecting, and the connection goes only to a checked
|
|
9
|
+
* address. Follows at most 3 redirects, each checked the same way. Gives up after `timeoutMs` and once
|
|
10
|
+
* the body passes `maxBytes`. Every failure is an `InlayError` with a plain-language message.
|
|
11
|
+
*/
|
|
12
|
+
export declare function fetchRemoteFile(url: string, opts: {
|
|
13
|
+
maxBytes: number;
|
|
14
|
+
timeoutMs?: number;
|
|
15
|
+
isAllowed?: (address: string) => boolean;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
data: Uint8Array;
|
|
18
|
+
filename: string;
|
|
19
|
+
}>;
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { lookup as dnsLookup } from 'node:dns';
|
|
2
|
+
import http from 'node:http';
|
|
3
|
+
import https from 'node:https';
|
|
4
|
+
import { BlockList, isIP } from 'node:net';
|
|
5
|
+
import { posix } from 'node:path';
|
|
6
|
+
import { InlayError } from '../../core/errors.js';
|
|
7
|
+
import { t } from '../../core/messages/index.js';
|
|
8
|
+
const MAX_REDIRECTS = 3;
|
|
9
|
+
const DEFAULT_TIMEOUT_MS = 15_000;
|
|
10
|
+
const REDIRECTS = new Set([301, 302, 303, 307, 308]);
|
|
11
|
+
const blocked = new BlockList();
|
|
12
|
+
for (const [net, prefix] of [
|
|
13
|
+
['0.0.0.0', 8], // unspecified, "this network"
|
|
14
|
+
['10.0.0.0', 8],
|
|
15
|
+
['100.64.0.0', 10], // CGNAT
|
|
16
|
+
['127.0.0.0', 8],
|
|
17
|
+
['169.254.0.0', 16], // link-local, cloud metadata
|
|
18
|
+
['172.16.0.0', 12],
|
|
19
|
+
['192.0.0.0', 24], // IETF protocol assignments
|
|
20
|
+
['192.168.0.0', 16],
|
|
21
|
+
['198.18.0.0', 15], // benchmarking
|
|
22
|
+
['224.0.0.0', 4], // multicast
|
|
23
|
+
['240.0.0.0', 4], // reserved, broadcast
|
|
24
|
+
]) {
|
|
25
|
+
blocked.addSubnet(net, prefix, 'ipv4');
|
|
26
|
+
}
|
|
27
|
+
for (const [net, prefix] of [
|
|
28
|
+
['2001::', 32], // Teredo, which tunnels to an IPv4 address
|
|
29
|
+
['fc00::', 7], // unique local
|
|
30
|
+
['fe80::', 10], // link-local
|
|
31
|
+
['ff00::', 8], // multicast
|
|
32
|
+
]) {
|
|
33
|
+
blocked.addSubnet(net, prefix, 'ipv6');
|
|
34
|
+
}
|
|
35
|
+
/** The eight 16-bit groups of a valid IPv6 address (no zone). */
|
|
36
|
+
function ipv6Groups(address) {
|
|
37
|
+
let text = address.toLowerCase();
|
|
38
|
+
const dotted = /^(.*:)(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(text);
|
|
39
|
+
if (dotted) {
|
|
40
|
+
const [a, b, c, d] = dotted.slice(2).map(Number);
|
|
41
|
+
text = `${dotted[1]}${((a << 8) | b).toString(16)}:${((c << 8) | d).toString(16)}`;
|
|
42
|
+
}
|
|
43
|
+
const [head = '', tail] = text.split('::');
|
|
44
|
+
const left = head === '' ? [] : head.split(':');
|
|
45
|
+
const right = tail === undefined || tail === '' ? [] : tail.split(':');
|
|
46
|
+
const fill = tail === undefined ? [] : new Array(8 - left.length - right.length).fill('0');
|
|
47
|
+
return [...left, ...fill, ...right].map((group) => parseInt(group, 16));
|
|
48
|
+
}
|
|
49
|
+
const ipv4Of = (hi, lo) => `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`;
|
|
50
|
+
/** The IPv4 address an IPv6 address stands for or tunnels to (mapped, compatible, NAT64, 6to4), if any. */
|
|
51
|
+
function embeddedIpv4(groups) {
|
|
52
|
+
const [g0 = 0, g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0] = groups;
|
|
53
|
+
const zeroTo80 = g0 === 0 && g1 === 0 && g2 === 0 && g3 === 0 && g4 === 0;
|
|
54
|
+
if (zeroTo80 && (g5 === 0xffff || g5 === 0))
|
|
55
|
+
return ipv4Of(g6, g7);
|
|
56
|
+
if (g0 === 0x64 && g1 === 0xff9b && g2 === 0 && g3 === 0 && g4 === 0 && g5 === 0)
|
|
57
|
+
return ipv4Of(g6, g7);
|
|
58
|
+
if (g0 === 0x2002)
|
|
59
|
+
return ipv4Of(g1, g2);
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Whether `address` (an IP address, not a host name) is one the server may fetch from: anything but loopback,
|
|
64
|
+
* private, link-local, CGNAT, unspecified, multicast or reserved addresses, including IPv6 forms that carry one.
|
|
65
|
+
*/
|
|
66
|
+
export function isPublicAddress(address) {
|
|
67
|
+
const bare = address.replace(/%.*$/, '');
|
|
68
|
+
const kind = isIP(bare);
|
|
69
|
+
if (kind === 4)
|
|
70
|
+
return !blocked.check(bare, 'ipv4');
|
|
71
|
+
if (kind !== 6)
|
|
72
|
+
return false;
|
|
73
|
+
const groups = ipv6Groups(bare);
|
|
74
|
+
const v4 = embeddedIpv4(groups);
|
|
75
|
+
if (v4 !== null)
|
|
76
|
+
return isPublicAddress(v4);
|
|
77
|
+
// Only global unicast (2000::/3) is public.
|
|
78
|
+
if (((groups[0] ?? 0) & 0xe000) !== 0x2000)
|
|
79
|
+
return false;
|
|
80
|
+
return !blocked.check(bare, 'ipv6');
|
|
81
|
+
}
|
|
82
|
+
const fail = (id, url) => new InlayError('invalid_operation', id, { url });
|
|
83
|
+
/** A `lookup` that resolves every address of the host and fails unless all of them pass `isAllowed`. */
|
|
84
|
+
function checkedLookup(isAllowed, url) {
|
|
85
|
+
return (hostname, options, callback) => {
|
|
86
|
+
dnsLookup(hostname, { all: true, family: 0 }, (err, found) => {
|
|
87
|
+
if (err)
|
|
88
|
+
return callback(err, '', 0);
|
|
89
|
+
if (found.length === 0 || !found.every((a) => isAllowed(a.address))) {
|
|
90
|
+
return callback(fail('error.fetchBlocked', url), '', 0);
|
|
91
|
+
}
|
|
92
|
+
const family = options.family === 4 || options.family === 6 ? options.family : 0;
|
|
93
|
+
const usable = family === 0 ? found : found.filter((a) => a.family === family);
|
|
94
|
+
if (usable.length === 0)
|
|
95
|
+
return callback(fail('error.fetchFailed', url), '', 0);
|
|
96
|
+
if (options.all)
|
|
97
|
+
return callback(null, usable);
|
|
98
|
+
const [first] = usable;
|
|
99
|
+
callback(null, first.address, first.family);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/** `raw` as an http(s) URL whose literal IP host (if it has one) passes `isAllowed`. */
|
|
104
|
+
function checkUrl(raw, isAllowed, url) {
|
|
105
|
+
let parsed;
|
|
106
|
+
try {
|
|
107
|
+
parsed = new URL(raw);
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
throw fail('error.fetchScheme', url);
|
|
111
|
+
}
|
|
112
|
+
if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:')
|
|
113
|
+
throw fail('error.fetchScheme', url);
|
|
114
|
+
// Node connects to a literal address without calling `lookup`, so it is checked here.
|
|
115
|
+
const host = parsed.hostname.replace(/^\[(.*)\]$/, '$1');
|
|
116
|
+
if (isIP(host) !== 0 && !isAllowed(host))
|
|
117
|
+
throw fail('error.fetchBlocked', url);
|
|
118
|
+
return parsed;
|
|
119
|
+
}
|
|
120
|
+
function get(target, lookup, signal) {
|
|
121
|
+
return new Promise((resolve, reject) => {
|
|
122
|
+
const client = target.protocol === 'https:' ? https : http;
|
|
123
|
+
const req = client.request(target, {
|
|
124
|
+
method: 'GET',
|
|
125
|
+
agent: false,
|
|
126
|
+
lookup,
|
|
127
|
+
signal,
|
|
128
|
+
headers: { accept: '*/*', 'accept-encoding': 'identity', 'user-agent': 'Inlay' },
|
|
129
|
+
}, resolve);
|
|
130
|
+
req.on('error', reject);
|
|
131
|
+
req.end();
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async function readBody(res, maxBytes, url, signal) {
|
|
135
|
+
const declared = Number(res.headers['content-length']);
|
|
136
|
+
if (Number.isFinite(declared) && declared > maxBytes) {
|
|
137
|
+
res.destroy();
|
|
138
|
+
throw fail('error.fetchTooLarge', url);
|
|
139
|
+
}
|
|
140
|
+
const stop = () => res.destroy(new Error('aborted'));
|
|
141
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
142
|
+
try {
|
|
143
|
+
const chunks = [];
|
|
144
|
+
let total = 0;
|
|
145
|
+
for await (const chunk of res) {
|
|
146
|
+
total += chunk.length;
|
|
147
|
+
if (total > maxBytes) {
|
|
148
|
+
res.destroy();
|
|
149
|
+
throw fail('error.fetchTooLarge', url);
|
|
150
|
+
}
|
|
151
|
+
chunks.push(chunk);
|
|
152
|
+
}
|
|
153
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
signal.removeEventListener('abort', stop);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** The name in a `Content-Disposition` header (`filename*` first), without any directories. */
|
|
160
|
+
function dispositionName(header) {
|
|
161
|
+
if (header === undefined)
|
|
162
|
+
return null;
|
|
163
|
+
const extended = /filename\*\s*=\s*([^']*)'[^']*'([^;]+)/i.exec(header);
|
|
164
|
+
let name = null;
|
|
165
|
+
if (extended?.[2] !== undefined) {
|
|
166
|
+
try {
|
|
167
|
+
name = decodeURIComponent(extended[2].trim());
|
|
168
|
+
}
|
|
169
|
+
catch {
|
|
170
|
+
name = null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (name === null) {
|
|
174
|
+
const plain = /filename\s*=\s*(?:"((?:[^"\\]|\\.)*)"|([^;\s]+))/i.exec(header);
|
|
175
|
+
name = plain?.[1]?.replace(/\\(.)/g, '$1') ?? plain?.[2] ?? null;
|
|
176
|
+
}
|
|
177
|
+
if (name === null)
|
|
178
|
+
return null;
|
|
179
|
+
const base = posix.basename(name.replace(/\\/g, '/')).trim();
|
|
180
|
+
return base === '' || base === '.' || base === '..' ? null : base;
|
|
181
|
+
}
|
|
182
|
+
function pathName(target) {
|
|
183
|
+
const segment = target.pathname.split('/').pop() ?? '';
|
|
184
|
+
let name;
|
|
185
|
+
try {
|
|
186
|
+
name = decodeURIComponent(segment);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
name = segment;
|
|
190
|
+
}
|
|
191
|
+
name = name.replace(/[/\\]/g, '').trim();
|
|
192
|
+
return name === '' ? null : name;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Downloads `url` for the media library, only from addresses `isAllowed` accepts (public ones by default).
|
|
196
|
+
* Every address of each host is checked before connecting, and the connection goes only to a checked
|
|
197
|
+
* address. Follows at most 3 redirects, each checked the same way. Gives up after `timeoutMs` and once
|
|
198
|
+
* the body passes `maxBytes`. Every failure is an `InlayError` with a plain-language message.
|
|
199
|
+
*/
|
|
200
|
+
export async function fetchRemoteFile(url, opts) {
|
|
201
|
+
const isAllowed = opts.isAllowed ?? isPublicAddress;
|
|
202
|
+
const lookup = checkedLookup(isAllowed, url);
|
|
203
|
+
const controller = new AbortController();
|
|
204
|
+
const timer = setTimeout(() => controller.abort(), opts.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
205
|
+
try {
|
|
206
|
+
let target = checkUrl(url, isAllowed, url);
|
|
207
|
+
for (let redirects = 0;; redirects++) {
|
|
208
|
+
const res = await get(target, lookup, controller.signal);
|
|
209
|
+
const status = res.statusCode ?? 0;
|
|
210
|
+
if (REDIRECTS.has(status)) {
|
|
211
|
+
res.resume();
|
|
212
|
+
const location = res.headers.location;
|
|
213
|
+
if (location === undefined || redirects >= MAX_REDIRECTS)
|
|
214
|
+
throw fail('error.fetchFailed', url);
|
|
215
|
+
let next;
|
|
216
|
+
try {
|
|
217
|
+
next = new URL(location, target).href;
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
throw fail('error.fetchFailed', url);
|
|
221
|
+
}
|
|
222
|
+
target = checkUrl(next, isAllowed, url);
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (status < 200 || status >= 300) {
|
|
226
|
+
res.resume();
|
|
227
|
+
throw fail('error.fetchFailed', url);
|
|
228
|
+
}
|
|
229
|
+
const data = await readBody(res, opts.maxBytes, url, controller.signal);
|
|
230
|
+
const filename = dispositionName(res.headers['content-disposition']) ?? pathName(target) ?? t('media.downloadName');
|
|
231
|
+
return { data, filename };
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
catch (e) {
|
|
235
|
+
if (e instanceof InlayError)
|
|
236
|
+
throw e;
|
|
237
|
+
if (controller.signal.aborted)
|
|
238
|
+
throw fail('error.fetchTimeout', url);
|
|
239
|
+
throw fail('error.fetchFailed', url);
|
|
240
|
+
}
|
|
241
|
+
finally {
|
|
242
|
+
clearTimeout(timer);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type RequestStateCodec } from '@modelcontextprotocol/server';
|
|
2
|
+
/** The codec that signs and checks the request state of multi-round-trip tool calls (publish confirmation). */
|
|
3
|
+
export declare function requestStateCodec<T = unknown>(secret: string): RequestStateCodec<T>;
|
|
4
|
+
/** Remembers which request-state nonces were already used, so a signed state is acted on once. */
|
|
5
|
+
export interface NonceStore {
|
|
6
|
+
/** Marks `nonce` used. `false` when it already was (or the store is full), and the state must not be acted on. */
|
|
7
|
+
consume(nonce: string): boolean;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A single-use store held in memory. It covers one process only, which matches the one-install-per-site model:
|
|
11
|
+
* every confirmation for a site goes through the same `/mcp` handler. A nonce is forgotten once the state
|
|
12
|
+
* carrying it has expired, since the codec refuses that state anyway. Request states expire on the real
|
|
13
|
+
* clock, so this does too.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createNonceStore(): NonceStore;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createHmac } from 'node:crypto';
|
|
2
|
+
import { createRequestStateCodec } from '@modelcontextprotocol/server';
|
|
3
|
+
/** How long a minted request state stays valid. */
|
|
4
|
+
const TTL_SECONDS = 600;
|
|
5
|
+
/** The most nonces a store remembers at once; past that it refuses new ones until old ones expire. */
|
|
6
|
+
const MAX_NONCES = 10_000;
|
|
7
|
+
/** The codec that signs and checks the request state of multi-round-trip tool calls (publish confirmation). */
|
|
8
|
+
export function requestStateCodec(secret) {
|
|
9
|
+
const key = createHmac('sha256', secret).update('inlay-mcp-request-state').digest();
|
|
10
|
+
return createRequestStateCodec({ key, ttlSeconds: TTL_SECONDS });
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A single-use store held in memory. It covers one process only, which matches the one-install-per-site model:
|
|
14
|
+
* every confirmation for a site goes through the same `/mcp` handler. A nonce is forgotten once the state
|
|
15
|
+
* carrying it has expired, since the codec refuses that state anyway. Request states expire on the real
|
|
16
|
+
* clock, so this does too.
|
|
17
|
+
*/
|
|
18
|
+
export function createNonceStore() {
|
|
19
|
+
/** Nonce → when it can be forgotten, in ms since the epoch. Insertion order is expiry order. */
|
|
20
|
+
const used = new Map();
|
|
21
|
+
const purge = (now) => {
|
|
22
|
+
for (const [nonce, expiresAt] of used) {
|
|
23
|
+
if (expiresAt > now)
|
|
24
|
+
break;
|
|
25
|
+
used.delete(nonce);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
consume(nonce) {
|
|
30
|
+
const now = Date.now();
|
|
31
|
+
purge(now);
|
|
32
|
+
if (used.has(nonce) || used.size >= MAX_NONCES)
|
|
33
|
+
return false;
|
|
34
|
+
used.set(nonce, now + TTL_SECONDS * 1000);
|
|
35
|
+
return true;
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CallToolResult, InputRequiredResult } from '@modelcontextprotocol/server';
|
|
2
|
+
import type { ContentIssue } from '../../core/types.js';
|
|
3
|
+
/** Where unexpected tool failures are reported. */
|
|
4
|
+
export type ErrorLog = (err: unknown) => void;
|
|
5
|
+
/** A successful result: `value` as a single JSON text block. */
|
|
6
|
+
export declare function ok(value: unknown): CallToolResult;
|
|
7
|
+
export type IssueJson = {
|
|
8
|
+
field: string;
|
|
9
|
+
path: string;
|
|
10
|
+
message: string;
|
|
11
|
+
};
|
|
12
|
+
/** An issue as tools report it: its field's labels, its dotted path and its message. */
|
|
13
|
+
export declare function issueJson(issue: ContentIssue, prefix?: string[]): IssueJson;
|
|
14
|
+
/**
|
|
15
|
+
* A failed result (`isError`): the catalog message, one line per issue and per usage, then the JSON
|
|
16
|
+
* `{ error: { code, message, issues, usages } }`. Anything but an `InlayError` gives a generic message and is logged.
|
|
17
|
+
*/
|
|
18
|
+
export declare function toolError(e: unknown, log: ErrorLog): CallToolResult;
|
|
19
|
+
/** Wraps a tool handler so anything it throws becomes a `toolError` result. */
|
|
20
|
+
export declare function tool<A, C>(fn: (args: A, ctx: C) => Promise<CallToolResult | InputRequiredResult>, log: ErrorLog): (args: A, ctx: C) => Promise<CallToolResult | InputRequiredResult>;
|