@di-framework/cli-plugin-platform 0.0.1 → 6.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +408 -0
- package/dist/actor-protocol.d.ts +4 -0
- package/dist/actor-protocol.js +162 -0
- package/dist/actors.d.ts +31 -0
- package/dist/actors.js +296 -0
- package/dist/args.d.ts +15 -0
- package/dist/args.js +66 -0
- package/dist/assets/http-adapter.js +876 -0
- package/dist/assets/platform/.gitignore.tmpl +6 -0
- package/dist/assets/platform/Pulumi.yaml.tmpl +6 -0
- package/dist/assets/platform/README.md +208 -0
- package/dist/assets/platform/index.ts.tmpl +2 -0
- package/dist/assets/platform/package.json.tmpl +18 -0
- package/dist/assets/platform/tsconfig.json +11 -0
- package/dist/assets/queue-adapter.js +60 -0
- package/dist/assets/sqlite/BUILD-INFO.txt +13 -0
- package/dist/assets/sqlite/SHA256SUMS +2 -0
- package/dist/assets/sqlite/di-framework-sqlite.wasm +0 -0
- package/dist/assets/sqlite/di-framework-sqlite.wit +337 -0
- package/dist/assets/wit/deps/LICENSE.md +8 -0
- package/dist/assets/wit/deps/di-framework-queues/package.wit +18 -0
- package/dist/assets/wit/deps/di-framework-sqlite/package.wit +211 -0
- package/dist/assets/wit/deps/wasi-cli/package.wit +70 -0
- package/dist/assets/wit/deps/wasi-clocks/package.wit +42 -0
- package/dist/assets/wit/deps/wasi-config/package.wit +15 -0
- package/dist/assets/wit/deps/wasi-filesystem/package.wit +184 -0
- package/dist/assets/wit/deps/wasi-http/package.wit +170 -0
- package/dist/assets/wit/deps/wasi-random/package.wit +17 -0
- package/dist/assets/wit/deps/wasi-sockets/package.wit +105 -0
- package/dist/assets/wit/deps/wasi-tls/package.wit +26 -0
- package/dist/assets/wit/deps/wasmcloud-blobstore/package.wit +153 -0
- package/dist/assets/wit/deps/wasmcloud-keyvalue/package.wit +370 -0
- package/dist/assets/wit/deps/wasmcloud-messaging/package.wit +125 -0
- package/dist/assets/wit/deps/wasmcloud-postgres/package.wit +435 -0
- package/dist/assets/wit/deps/wasmcloud-secrets/package.wit +80 -0
- package/dist/async-transform.d.ts +3 -0
- package/dist/async-transform.js +60 -0
- package/dist/bindings.d.ts +34 -0
- package/dist/bindings.js +307 -0
- package/dist/build.d.ts +29 -0
- package/dist/build.js +325 -0
- package/dist/command.d.ts +3 -0
- package/dist/command.js +141 -0
- package/dist/control/auth.d.ts +18 -0
- package/dist/control/auth.js +89 -0
- package/dist/control/cron.d.ts +7 -0
- package/dist/control/cron.js +86 -0
- package/dist/control/index.d.ts +4 -0
- package/dist/control/index.js +4 -0
- package/dist/control/network.d.ts +12 -0
- package/dist/control/network.js +49 -0
- package/dist/control/queues.d.ts +15 -0
- package/dist/control/queues.js +77 -0
- package/dist/cron.d.ts +26 -0
- package/dist/cron.js +239 -0
- package/dist/deploy.d.ts +29 -0
- package/dist/deploy.js +57 -0
- package/dist/deps.d.ts +96 -0
- package/dist/deps.js +303 -0
- package/dist/destroy.d.ts +3 -0
- package/dist/destroy.js +24 -0
- package/dist/dev-runner.d.ts +24 -0
- package/dist/dev-runner.js +83 -0
- package/dist/dev.d.ts +8 -0
- package/dist/dev.js +76 -0
- package/dist/discovery.d.ts +15 -0
- package/dist/discovery.js +182 -0
- package/dist/doctor.d.ts +8 -0
- package/dist/doctor.js +82 -0
- package/dist/guests.d.ts +9 -0
- package/dist/guests.js +56 -0
- package/dist/host-interface.d.ts +29 -0
- package/dist/host-interface.js +142 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +39 -0
- package/dist/kubernetes.d.ts +9 -0
- package/dist/kubernetes.js +17 -0
- package/dist/managed-bindings.d.ts +9 -0
- package/dist/managed-bindings.js +150 -0
- package/dist/manifest.d.ts +36 -0
- package/dist/manifest.js +257 -0
- package/dist/namespace.d.ts +2 -0
- package/dist/namespace.js +4 -0
- package/dist/node-compat/async-hooks.d.ts +29 -0
- package/dist/node-compat/async-hooks.js +80 -0
- package/dist/node-compat/bootstrap.d.ts +1 -0
- package/dist/node-compat/bootstrap.js +9 -0
- package/dist/node-compat/bytes.d.ts +7 -0
- package/dist/node-compat/bytes.js +41 -0
- package/dist/node-compat/crypto-hash.d.ts +27 -0
- package/dist/node-compat/crypto-hash.js +108 -0
- package/dist/node-compat/crypto-subtle.d.ts +59 -0
- package/dist/node-compat/crypto-subtle.js +288 -0
- package/dist/node-compat/crypto.d.ts +136 -0
- package/dist/node-compat/crypto.js +142 -0
- package/dist/node-compat/dgram.d.ts +49 -0
- package/dist/node-compat/dgram.js +197 -0
- package/dist/node-compat/env.d.ts +6 -0
- package/dist/node-compat/env.js +88 -0
- package/dist/node-compat/fetch-runtime.d.ts +80 -0
- package/dist/node-compat/fetch-runtime.js +314 -0
- package/dist/node-compat/fs.d.ts +84 -0
- package/dist/node-compat/fs.js +285 -0
- package/dist/node-compat/http-parser.d.ts +40 -0
- package/dist/node-compat/http-parser.js +185 -0
- package/dist/node-compat/http.d.ts +130 -0
- package/dist/node-compat/http.js +688 -0
- package/dist/node-compat/https.d.ts +28 -0
- package/dist/node-compat/https.js +88 -0
- package/dist/node-compat/module.d.ts +11 -0
- package/dist/node-compat/module.js +35 -0
- package/dist/node-compat/net.d.ts +119 -0
- package/dist/node-compat/net.js +441 -0
- package/dist/node-compat/process.d.ts +25 -0
- package/dist/node-compat/process.js +63 -0
- package/dist/node-compat/runtime-application.d.ts +4 -0
- package/dist/node-compat/runtime-application.js +24 -0
- package/dist/node-compat/seed-virtual.d.ts +6 -0
- package/dist/node-compat/seed-virtual.js +6 -0
- package/dist/node-compat/seed.d.ts +20 -0
- package/dist/node-compat/seed.js +106 -0
- package/dist/node-compat/socket-address.d.ts +35 -0
- package/dist/node-compat/socket-address.js +162 -0
- package/dist/node-compat/timers.d.ts +32 -0
- package/dist/node-compat/timers.js +96 -0
- package/dist/node-compat/tls.d.ts +57 -0
- package/dist/node-compat/tls.js +290 -0
- package/dist/node-compat/wasi-random.d.ts +2 -0
- package/dist/node-compat/wasi-random.js +25 -0
- package/dist/node-compat/wasi-sockets.d.ts +47 -0
- package/dist/node-compat/wasi-sockets.js +134 -0
- package/dist/oci.d.ts +9 -0
- package/dist/oci.js +9 -0
- package/dist/paths.d.ts +7 -0
- package/dist/paths.js +13 -0
- package/dist/platform-init.d.ts +13 -0
- package/dist/platform-init.js +265 -0
- package/dist/platform.d.ts +22 -0
- package/dist/platform.js +170 -0
- package/dist/project.d.ts +39 -0
- package/dist/project.js +130 -0
- package/dist/publish.d.ts +15 -0
- package/dist/publish.js +57 -0
- package/dist/pulumi.d.ts +4 -0
- package/dist/pulumi.js +27 -0
- package/dist/queues-module.d.ts +4 -0
- package/dist/queues-module.js +89 -0
- package/dist/queues.d.ts +16 -0
- package/dist/queues.js +167 -0
- package/dist/registry.d.ts +16 -0
- package/dist/registry.js +27 -0
- package/dist/service.d.ts +162 -0
- package/dist/service.js +678 -0
- package/dist/support.d.ts +14 -0
- package/dist/support.js +37 -0
- package/dist/target.d.ts +18 -0
- package/dist/target.js +45 -0
- package/dist/toml.d.ts +6 -0
- package/dist/toml.js +245 -0
- package/dist/wash-dev.d.ts +18 -0
- package/dist/wash-dev.js +80 -0
- package/dist/wit.d.ts +76 -0
- package/dist/wit.js +248 -0
- package/dist/workload-members.d.ts +12 -0
- package/dist/workload-members.js +136 -0
- package/dist/workload.d.ts +45 -0
- package/dist/workload.js +457 -0
- package/package.json +55 -8
- package/src/index.ts +0 -2
package/dist/command.js
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { runWasmcloudBuild } from './build.js';
|
|
2
|
+
import { runWasmcloudDeploy } from './deploy.js';
|
|
3
|
+
import { DEFAULT_DEPS } from './deps.js';
|
|
4
|
+
import { runWasmcloudDestroy } from './destroy.js';
|
|
5
|
+
import { runWasmcloudDev } from './dev.js';
|
|
6
|
+
import { runWasmcloudDoctor } from './doctor.js';
|
|
7
|
+
import { runWasmcloudPlatformDeploy, runWasmcloudPlatformDestroy } from './platform.js';
|
|
8
|
+
import { runWasmcloudPlatformInit } from './platform-init.js';
|
|
9
|
+
import { runWasmcloudServiceClasses, runWasmcloudServiceCreate, runWasmcloudServiceDelete, runWasmcloudServiceGet, runWasmcloudServiceList, } from './service.js';
|
|
10
|
+
export function createWasmcloudCommand(deps = DEFAULT_DEPS) {
|
|
11
|
+
return {
|
|
12
|
+
description: 'Build, serve, and deploy DI Framework apps as wasmCloud components',
|
|
13
|
+
children: {
|
|
14
|
+
build: {
|
|
15
|
+
description: 'Bundle the app entry and componentize it for WASI HTTP',
|
|
16
|
+
usage: 'di-framework platform build',
|
|
17
|
+
run: ({ args, io }) => runWasmcloudBuild(args, io, deps),
|
|
18
|
+
},
|
|
19
|
+
dev: {
|
|
20
|
+
description: 'Build, then serve the component locally (wasmtime, wash, or jco)',
|
|
21
|
+
usage: 'di-framework platform dev [--host <address>] [--port <port>]',
|
|
22
|
+
options: [
|
|
23
|
+
'--host <address> Bind address (default: 127.0.0.1)',
|
|
24
|
+
'--port <port> Listen port (default: 8000)',
|
|
25
|
+
],
|
|
26
|
+
run: ({ args, io }) => runWasmcloudDev(args, io, deps),
|
|
27
|
+
},
|
|
28
|
+
deploy: {
|
|
29
|
+
description: 'Build, publish, and apply a wasmCloud WorkloadDeployment for a project in di-framework.deploy.toml',
|
|
30
|
+
usage: 'di-framework platform deploy [name] [--target <name>] [--yes]',
|
|
31
|
+
options: [
|
|
32
|
+
'--target <name> Deployment target from di-framework.deploy.toml (default: default-target)',
|
|
33
|
+
'--yes Accepted for compatibility; application deploy does not prompt',
|
|
34
|
+
],
|
|
35
|
+
run: ({ args, io }) => runWasmcloudDeploy(args, io, deps),
|
|
36
|
+
},
|
|
37
|
+
destroy: {
|
|
38
|
+
description: 'Remove the generated WorkloadDeployment and Service for a project; does not destroy the platform',
|
|
39
|
+
usage: 'di-framework platform destroy [name] [--target <name>] [--yes]',
|
|
40
|
+
options: [
|
|
41
|
+
'--target <name> Deployment target from di-framework.deploy.toml (default: default-target)',
|
|
42
|
+
'--yes Accepted for compatibility; application destroy does not prompt',
|
|
43
|
+
],
|
|
44
|
+
run: ({ args, io }) => runWasmcloudDestroy(args, io, deps),
|
|
45
|
+
},
|
|
46
|
+
cluster: {
|
|
47
|
+
description: 'Initialize, provision, or tear down a managed wasmCloud platform target',
|
|
48
|
+
children: {
|
|
49
|
+
init: {
|
|
50
|
+
description: 'Generate deploy/platform from wasmCloud templates and register it as the default local target',
|
|
51
|
+
usage: 'di-framework platform cluster init [--force]',
|
|
52
|
+
options: [
|
|
53
|
+
'--force, -f Overwrite existing platform files and the local target in di-framework.deploy.toml',
|
|
54
|
+
],
|
|
55
|
+
run: ({ args, io }) => runWasmcloudPlatformInit(args, io, deps),
|
|
56
|
+
},
|
|
57
|
+
up: {
|
|
58
|
+
description: 'Run pulumi up for a managed target in di-framework.deploy.toml (k0s, registry, operator)',
|
|
59
|
+
usage: 'di-framework platform cluster up <target> [--yes]',
|
|
60
|
+
options: ['--yes Skip the Pulumi confirmation prompt'],
|
|
61
|
+
run: ({ args, io }) => runWasmcloudPlatformDeploy(args, io, deps),
|
|
62
|
+
},
|
|
63
|
+
destroy: {
|
|
64
|
+
description: 'Run pulumi destroy for a managed platform target only',
|
|
65
|
+
usage: 'di-framework platform cluster destroy <target> [--yes]',
|
|
66
|
+
options: ['--yes Skip the Pulumi confirmation prompt'],
|
|
67
|
+
run: ({ args, io }) => runWasmcloudPlatformDestroy(args, io, deps),
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
doctor: {
|
|
72
|
+
description: 'Check the project and local toolchain for wasmCloud readiness',
|
|
73
|
+
usage: 'di-framework platform doctor',
|
|
74
|
+
run: ({ args, io }) => runWasmcloudDoctor(args, io, deps),
|
|
75
|
+
},
|
|
76
|
+
service: {
|
|
77
|
+
description: 'Create and manage BackingService custom resources (keyvalue/messaging/postgres) via the cluster API',
|
|
78
|
+
children: {
|
|
79
|
+
create: {
|
|
80
|
+
description: 'Create a BackingService custom resource for an approved capability type',
|
|
81
|
+
usage: 'di-framework platform service create <keyvalue|messaging|postgres> --name=<name> [--class=<class>] [--target <name>] [--namespace <ns>] [--wait]',
|
|
82
|
+
options: [
|
|
83
|
+
'--name <name> Required BackingService metadata.name (DNS label, max 40)',
|
|
84
|
+
'--class <name> Optional approved BackingServiceClass (defaults: keyvalue-redis, messaging-nats, postgres-dedicated)',
|
|
85
|
+
'--memory <qty> Optional sizing parameter (Kubernetes quantity)',
|
|
86
|
+
'--storage <qty> Optional sizing parameter (Kubernetes quantity)',
|
|
87
|
+
'--cpu <qty> Optional sizing parameter (Kubernetes quantity)',
|
|
88
|
+
'--deletion-policy <Retain|Delete> Retention when the CR is deleted (default Retain)',
|
|
89
|
+
'--target <name> Deployment target from di-framework.deploy.toml',
|
|
90
|
+
'--namespace <ns> Override the target namespace',
|
|
91
|
+
'--context <name> Override the kubeconfig context',
|
|
92
|
+
'--wait Wait until the Ready condition is True',
|
|
93
|
+
'--timeout <seconds> Wait timeout in seconds (default: 120)',
|
|
94
|
+
],
|
|
95
|
+
run: ({ args, io }) => runWasmcloudServiceCreate(args, io, deps),
|
|
96
|
+
},
|
|
97
|
+
list: {
|
|
98
|
+
description: 'List BackingService resources in the target namespace',
|
|
99
|
+
usage: 'di-framework platform service list [--target <name>] [--namespace <ns>]',
|
|
100
|
+
options: [
|
|
101
|
+
'--target <name> Deployment target from di-framework.deploy.toml',
|
|
102
|
+
'--namespace <ns> Override the target namespace',
|
|
103
|
+
'--context <name> Override the kubeconfig context',
|
|
104
|
+
],
|
|
105
|
+
run: ({ args, io }) => runWasmcloudServiceList(args, io, deps),
|
|
106
|
+
},
|
|
107
|
+
get: {
|
|
108
|
+
description: 'Show Ready status, type, class, and endpoint summary for a BackingService',
|
|
109
|
+
usage: 'di-framework platform service get <name> [--target <name>] [--namespace <ns>]',
|
|
110
|
+
options: [
|
|
111
|
+
'--target <name> Deployment target from di-framework.deploy.toml',
|
|
112
|
+
'--namespace <ns> Override the target namespace',
|
|
113
|
+
'--context <name> Override the kubeconfig context',
|
|
114
|
+
],
|
|
115
|
+
run: ({ args, io }) => runWasmcloudServiceGet(args, io, deps),
|
|
116
|
+
},
|
|
117
|
+
delete: {
|
|
118
|
+
description: 'Delete a BackingService custom resource (controller retention/in-use rules still apply)',
|
|
119
|
+
usage: 'di-framework platform service delete <name> [--target <name>] [--namespace <ns>]',
|
|
120
|
+
options: [
|
|
121
|
+
'--target <name> Deployment target from di-framework.deploy.toml',
|
|
122
|
+
'--namespace <ns> Override the target namespace',
|
|
123
|
+
'--context <name> Override the kubeconfig context',
|
|
124
|
+
],
|
|
125
|
+
run: ({ args, io }) => runWasmcloudServiceDelete(args, io, deps),
|
|
126
|
+
},
|
|
127
|
+
classes: {
|
|
128
|
+
description: 'Discover approved BackingServiceClass resources (falls back to platform defaults)',
|
|
129
|
+
usage: 'di-framework platform service classes [--target <name>]',
|
|
130
|
+
options: [
|
|
131
|
+
'--target <name> Deployment target from di-framework.deploy.toml',
|
|
132
|
+
'--namespace <ns> Override the target namespace',
|
|
133
|
+
'--context <name> Override the kubeconfig context',
|
|
134
|
+
],
|
|
135
|
+
run: ({ args, io }) => runWasmcloudServiceClasses(args, io, deps),
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Cluster-private control-plane authentication for cron/queue/actor admin APIs. */
|
|
2
|
+
export type ControlIdentity = {
|
|
3
|
+
id: string;
|
|
4
|
+
token: string;
|
|
5
|
+
roles: readonly string[];
|
|
6
|
+
};
|
|
7
|
+
export type ControlAuthResult = {
|
|
8
|
+
ok: true;
|
|
9
|
+
identity: ControlIdentity;
|
|
10
|
+
} | {
|
|
11
|
+
ok: false;
|
|
12
|
+
status: number;
|
|
13
|
+
error: string;
|
|
14
|
+
};
|
|
15
|
+
export declare function authorizeControlRequest(request: Request, requiredRoles?: readonly string[]): ControlAuthResult;
|
|
16
|
+
export declare function unauthorizedResponse(result: Extract<ControlAuthResult, {
|
|
17
|
+
ok: false;
|
|
18
|
+
}>): Response;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/** Cluster-private control-plane authentication for cron/queue/actor admin APIs. */
|
|
2
|
+
/** Unconfigured local/dev only. Never includes `admin`. */
|
|
3
|
+
const ANONYMOUS_IDENTITY = { id: 'anonymous', token: '', roles: ['invoke'] };
|
|
4
|
+
function isIdentityEntry(entry) {
|
|
5
|
+
if (entry === null || typeof entry !== 'object')
|
|
6
|
+
return false;
|
|
7
|
+
const candidate = entry;
|
|
8
|
+
return (typeof candidate.id === 'string' &&
|
|
9
|
+
candidate.id.length > 0 &&
|
|
10
|
+
typeof candidate.token === 'string' &&
|
|
11
|
+
candidate.token.length > 0);
|
|
12
|
+
}
|
|
13
|
+
function singleTokenIdentities() {
|
|
14
|
+
const token = process.env.DI_CONTROL_TOKEN;
|
|
15
|
+
if (typeof token !== 'string' || token.length === 0)
|
|
16
|
+
return [];
|
|
17
|
+
const id = typeof process.env.DI_CONTROL_IDENTITY === 'string' &&
|
|
18
|
+
process.env.DI_CONTROL_IDENTITY.length > 0
|
|
19
|
+
? process.env.DI_CONTROL_IDENTITY
|
|
20
|
+
: 'default';
|
|
21
|
+
return [{ id, token, roles: ['invoke', 'admin'] }];
|
|
22
|
+
}
|
|
23
|
+
function parseIdentities() {
|
|
24
|
+
const raw = process.env.DI_CONTROL_IDENTITIES;
|
|
25
|
+
if (typeof raw === 'string' && raw.trim() !== '') {
|
|
26
|
+
try {
|
|
27
|
+
const parsed = JSON.parse(raw);
|
|
28
|
+
if (Array.isArray(parsed)) {
|
|
29
|
+
const identities = parsed.filter(isIdentityEntry).map((entry) => {
|
|
30
|
+
const roles = Array.isArray(entry.roles)
|
|
31
|
+
? entry.roles.filter((role) => typeof role === 'string')
|
|
32
|
+
: [];
|
|
33
|
+
return {
|
|
34
|
+
id: entry.id,
|
|
35
|
+
token: entry.token,
|
|
36
|
+
roles: roles.length > 0 ? roles : ['invoke'],
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
if (identities.length > 0)
|
|
40
|
+
return { identities, configured: true };
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
// Invalid JSON is still an attempted configuration.
|
|
45
|
+
}
|
|
46
|
+
return { identities: singleTokenIdentities(), configured: true };
|
|
47
|
+
}
|
|
48
|
+
const identities = singleTokenIdentities();
|
|
49
|
+
return { identities, configured: identities.length > 0 };
|
|
50
|
+
}
|
|
51
|
+
function hasRequiredRole(identity, requiredRoles) {
|
|
52
|
+
return !requiredRoles.some((role) => !identity.roles.includes(role) && !identity.roles.includes('admin'));
|
|
53
|
+
}
|
|
54
|
+
export function authorizeControlRequest(request, requiredRoles = ['invoke']) {
|
|
55
|
+
const { identities, configured } = parseIdentities();
|
|
56
|
+
if (identities.length === 0) {
|
|
57
|
+
// Deployed workloads always wire DI_CONTROL_TOKEN. Unconfigured local/dev
|
|
58
|
+
// may invoke, but never administer.
|
|
59
|
+
if (configured || !hasRequiredRole(ANONYMOUS_IDENTITY, requiredRoles)) {
|
|
60
|
+
return {
|
|
61
|
+
ok: false,
|
|
62
|
+
status: configured ? 401 : 403,
|
|
63
|
+
error: configured ? 'Missing control credentials' : 'Insufficient control privileges',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { ok: true, identity: ANONYMOUS_IDENTITY };
|
|
67
|
+
}
|
|
68
|
+
const header = request.headers.get('authorization') ?? request.headers.get('x-di-control-token');
|
|
69
|
+
if (!header) {
|
|
70
|
+
return { ok: false, status: 401, error: 'Missing control credentials' };
|
|
71
|
+
}
|
|
72
|
+
const token = header.toLowerCase().startsWith('bearer ')
|
|
73
|
+
? header.slice('bearer '.length).trim()
|
|
74
|
+
: header.trim();
|
|
75
|
+
const identity = identities.find((entry) => entry.token === token);
|
|
76
|
+
if (!identity) {
|
|
77
|
+
return { ok: false, status: 403, error: 'Unauthorized control credentials' };
|
|
78
|
+
}
|
|
79
|
+
if (!hasRequiredRole(identity, requiredRoles)) {
|
|
80
|
+
return { ok: false, status: 403, error: 'Insufficient control privileges' };
|
|
81
|
+
}
|
|
82
|
+
return { ok: true, identity };
|
|
83
|
+
}
|
|
84
|
+
export function unauthorizedResponse(result) {
|
|
85
|
+
return new Response(JSON.stringify({ success: false, error: result.error }), {
|
|
86
|
+
status: result.status,
|
|
87
|
+
headers: { 'content-type': 'application/json' },
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const CRON_INVOKE_PATH_PREFIX = "/_di/cron/";
|
|
2
|
+
export type CronInvoker = (jobId: string, context?: Record<string, unknown>) => Promise<unknown>;
|
|
3
|
+
export declare function isCronInvokeRequest(request: Request): boolean;
|
|
4
|
+
export declare function cronJobIdFromRequest(request: Request): string | undefined;
|
|
5
|
+
/** CronExecutionResult-shaped failures must not be reported as HTTP success. */
|
|
6
|
+
export declare function isFailedCronResult(result: unknown): boolean;
|
|
7
|
+
export declare function handleCronInvokeRequest(request: Request, invoke: CronInvoker | undefined): Promise<Response>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { authorizeControlRequest, unauthorizedResponse } from './auth.js';
|
|
2
|
+
export const CRON_INVOKE_PATH_PREFIX = '/_di/cron/';
|
|
3
|
+
export function isCronInvokeRequest(request) {
|
|
4
|
+
try {
|
|
5
|
+
const url = new URL(request.url);
|
|
6
|
+
return request.method === 'POST' && url.pathname.startsWith(CRON_INVOKE_PATH_PREFIX);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function cronJobIdFromRequest(request) {
|
|
13
|
+
try {
|
|
14
|
+
const url = new URL(request.url);
|
|
15
|
+
if (!url.pathname.startsWith(CRON_INVOKE_PATH_PREFIX))
|
|
16
|
+
return undefined;
|
|
17
|
+
const rest = url.pathname.slice(CRON_INVOKE_PATH_PREFIX.length);
|
|
18
|
+
const jobId = decodeURIComponent(rest.replace(/\/$/, '').split('/')[0] ?? '');
|
|
19
|
+
return jobId || undefined;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const JSON_HEADERS = { 'content-type': 'application/json' };
|
|
26
|
+
function cronFailureResponse(jobId) {
|
|
27
|
+
return new Response(JSON.stringify({
|
|
28
|
+
success: false,
|
|
29
|
+
completed: false,
|
|
30
|
+
ok: false,
|
|
31
|
+
jobId,
|
|
32
|
+
error: { name: 'CronInvocationError', message: 'Cron job failed' },
|
|
33
|
+
}), { status: 500, headers: JSON_HEADERS });
|
|
34
|
+
}
|
|
35
|
+
/** CronExecutionResult-shaped failures must not be reported as HTTP success. */
|
|
36
|
+
export function isFailedCronResult(result) {
|
|
37
|
+
if (result === null || typeof result !== 'object')
|
|
38
|
+
return false;
|
|
39
|
+
const record = result;
|
|
40
|
+
if (record.success === false || record.ok === false || record.completed === false)
|
|
41
|
+
return true;
|
|
42
|
+
return record.status === 'failure' || record.status === 'skipped';
|
|
43
|
+
}
|
|
44
|
+
export async function handleCronInvokeRequest(request, invoke) {
|
|
45
|
+
const auth = authorizeControlRequest(request, ['invoke']);
|
|
46
|
+
if (!auth.ok)
|
|
47
|
+
return unauthorizedResponse(auth);
|
|
48
|
+
const jobId = cronJobIdFromRequest(request);
|
|
49
|
+
if (!jobId) {
|
|
50
|
+
return new Response(JSON.stringify({ success: false, error: 'Missing cron job id' }), {
|
|
51
|
+
status: 400,
|
|
52
|
+
headers: { 'content-type': 'application/json' },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (typeof invoke !== 'function') {
|
|
56
|
+
return new Response(JSON.stringify({ success: false, error: 'No cron invoker registered' }), {
|
|
57
|
+
status: 404,
|
|
58
|
+
headers: { 'content-type': 'application/json' },
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
let body = {};
|
|
62
|
+
try {
|
|
63
|
+
const parsed = await request.json();
|
|
64
|
+
if (parsed !== null && typeof parsed === 'object' && !Array.isArray(parsed)) {
|
|
65
|
+
body = parsed;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
body = {};
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const result = await invoke(jobId, { ...body, caller: auth.identity.id });
|
|
73
|
+
if (isFailedCronResult(result))
|
|
74
|
+
return cronFailureResponse(jobId);
|
|
75
|
+
return new Response(JSON.stringify({
|
|
76
|
+
success: true,
|
|
77
|
+
completed: true,
|
|
78
|
+
ok: true,
|
|
79
|
+
jobId,
|
|
80
|
+
result,
|
|
81
|
+
}), { status: 200, headers: JSON_HEADERS });
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return cronFailureResponse(jobId);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { authorizeControlRequest, type ControlIdentity, unauthorizedResponse } from './auth.js';
|
|
2
|
+
export { CRON_INVOKE_PATH_PREFIX, type CronInvoker, cronJobIdFromRequest, handleCronInvokeRequest, isCronInvokeRequest, isFailedCronResult, } from './cron.js';
|
|
3
|
+
export { allowControlSurface, controlHttpHostList, controlSurfaceNotFound, isForwardedRequest, requestHostname, } from './network.js';
|
|
4
|
+
export { handleQueueControlRequest, isQueueControlRequest, QUEUE_CONTROL_PATH_PREFIX, } from './queues.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { authorizeControlRequest, unauthorizedResponse } from './auth.js';
|
|
2
|
+
export { CRON_INVOKE_PATH_PREFIX, cronJobIdFromRequest, handleCronInvokeRequest, isCronInvokeRequest, isFailedCronResult, } from './cron.js';
|
|
3
|
+
export { allowControlSurface, controlHttpHostList, controlSurfaceNotFound, isForwardedRequest, requestHostname, } from './network.js';
|
|
4
|
+
export { handleQueueControlRequest, isQueueControlRequest, QUEUE_CONTROL_PATH_PREFIX, } from './queues.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Cluster-only exposure for `/_di/*` and `/_actors/*` on deployed HTTP. */
|
|
2
|
+
export declare function requestHostname(request: Request): string;
|
|
3
|
+
export declare function isForwardedRequest(request: Request): boolean;
|
|
4
|
+
export declare function controlHttpHostList(raw?: string | undefined): string[];
|
|
5
|
+
/**
|
|
6
|
+
* Deployed workloads set `DI_CONTROL_REJECT_FORWARDED=1` and
|
|
7
|
+
* `DI_CONTROL_HTTP_HOST` so control paths are not reachable through public
|
|
8
|
+
* ingress. Unconfigured local/dev leaves both unset and stays open (auth still
|
|
9
|
+
* applies).
|
|
10
|
+
*/
|
|
11
|
+
export declare function allowControlSurface(request: Request): boolean;
|
|
12
|
+
export declare function controlSurfaceNotFound(): Response;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Cluster-only exposure for `/_di/*` and `/_actors/*` on deployed HTTP. */
|
|
2
|
+
const FORWARDED_HEADERS = [
|
|
3
|
+
'x-forwarded-for',
|
|
4
|
+
'x-forwarded-host',
|
|
5
|
+
'x-forwarded-proto',
|
|
6
|
+
'x-forwarded-by',
|
|
7
|
+
];
|
|
8
|
+
export function requestHostname(request) {
|
|
9
|
+
const header = request.headers.get('host');
|
|
10
|
+
const host = header && header.trim() !== '' ? header : new URL(request.url).host;
|
|
11
|
+
return host.trim().split(':')[0] ?? '';
|
|
12
|
+
}
|
|
13
|
+
export function isForwardedRequest(request) {
|
|
14
|
+
return FORWARDED_HEADERS.some((name) => {
|
|
15
|
+
const value = request.headers.get(name);
|
|
16
|
+
return value !== null && value.trim() !== '';
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
export function controlHttpHostList(raw = process.env.DI_CONTROL_HTTP_HOST) {
|
|
20
|
+
if (typeof raw !== 'string')
|
|
21
|
+
return [];
|
|
22
|
+
return raw
|
|
23
|
+
.split(',')
|
|
24
|
+
.map((entry) => entry.trim())
|
|
25
|
+
.filter((entry) => entry.length > 0);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Deployed workloads set `DI_CONTROL_REJECT_FORWARDED=1` and
|
|
29
|
+
* `DI_CONTROL_HTTP_HOST` so control paths are not reachable through public
|
|
30
|
+
* ingress. Unconfigured local/dev leaves both unset and stays open (auth still
|
|
31
|
+
* applies).
|
|
32
|
+
*/
|
|
33
|
+
export function allowControlSurface(request) {
|
|
34
|
+
const rejectForwarded = process.env.DI_CONTROL_REJECT_FORWARDED === '1';
|
|
35
|
+
const allowedHosts = controlHttpHostList();
|
|
36
|
+
if (!rejectForwarded && allowedHosts.length === 0)
|
|
37
|
+
return true;
|
|
38
|
+
if (rejectForwarded && isForwardedRequest(request))
|
|
39
|
+
return false;
|
|
40
|
+
if (allowedHosts.length === 0)
|
|
41
|
+
return true;
|
|
42
|
+
return allowedHosts.includes(requestHostname(request));
|
|
43
|
+
}
|
|
44
|
+
export function controlSurfaceNotFound() {
|
|
45
|
+
return new Response(JSON.stringify({ success: false, error: 'Not found' }), {
|
|
46
|
+
status: 404,
|
|
47
|
+
headers: { 'content-type': 'application/json' },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Structural queue backend surface used by the authenticated control HTTP API. */
|
|
2
|
+
export interface QueueControlBackend {
|
|
3
|
+
enqueue(queueName: string, payload: unknown, options?: Record<string, unknown>): Promise<unknown>;
|
|
4
|
+
listQueues(): Promise<Array<{
|
|
5
|
+
name: string;
|
|
6
|
+
}>>;
|
|
7
|
+
listJobs(queueName: string, filter?: Record<string, unknown>): Promise<unknown[]>;
|
|
8
|
+
getJob(queueName: string, jobId: string): Promise<{
|
|
9
|
+
queueName: string;
|
|
10
|
+
} | null>;
|
|
11
|
+
retryJob(queueName: string, jobId?: string): Promise<unknown[]>;
|
|
12
|
+
}
|
|
13
|
+
export declare const QUEUE_CONTROL_PATH_PREFIX = "/_di/queues/";
|
|
14
|
+
export declare function isQueueControlRequest(request: Request): boolean;
|
|
15
|
+
export declare function handleQueueControlRequest(request: Request, backend: QueueControlBackend | undefined): Promise<Response>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { authorizeControlRequest, unauthorizedResponse } from './auth.js';
|
|
2
|
+
export const QUEUE_CONTROL_PATH_PREFIX = '/_di/queues/';
|
|
3
|
+
export function isQueueControlRequest(request) {
|
|
4
|
+
try {
|
|
5
|
+
const url = new URL(request.url);
|
|
6
|
+
return url.pathname.startsWith(QUEUE_CONTROL_PATH_PREFIX);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
function json(status, body) {
|
|
13
|
+
return new Response(JSON.stringify(body), {
|
|
14
|
+
status,
|
|
15
|
+
headers: { 'content-type': 'application/json' },
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export async function handleQueueControlRequest(request, backend) {
|
|
19
|
+
const auth = authorizeControlRequest(request, ['invoke']);
|
|
20
|
+
if (!auth.ok)
|
|
21
|
+
return unauthorizedResponse(auth);
|
|
22
|
+
if (!backend)
|
|
23
|
+
return json(404, { success: false, error: 'No queue backend registered' });
|
|
24
|
+
const url = new URL(request.url);
|
|
25
|
+
const parts = url.pathname.slice(QUEUE_CONTROL_PATH_PREFIX.length).split('/').filter(Boolean);
|
|
26
|
+
const [queueName, action, jobId] = parts;
|
|
27
|
+
if (!queueName) {
|
|
28
|
+
if (request.method === 'GET') {
|
|
29
|
+
const queues = await backend.listQueues();
|
|
30
|
+
return json(200, { success: true, queues });
|
|
31
|
+
}
|
|
32
|
+
return json(400, { success: false, error: 'Queue name required' });
|
|
33
|
+
}
|
|
34
|
+
if (request.method === 'POST' && (action === undefined || action === 'jobs') && !jobId) {
|
|
35
|
+
const body = (await request.json().catch(() => ({})));
|
|
36
|
+
if (body.payload === undefined) {
|
|
37
|
+
return json(400, { success: false, error: 'payload is required' });
|
|
38
|
+
}
|
|
39
|
+
const job = await backend.enqueue(queueName, body.payload, {
|
|
40
|
+
idempotencyKey: body.idempotencyKey,
|
|
41
|
+
priority: body.priority,
|
|
42
|
+
delayMs: body.delayMs,
|
|
43
|
+
maxRetries: body.maxRetries,
|
|
44
|
+
backoffMs: body.backoffMs,
|
|
45
|
+
timeoutMs: body.timeoutMs,
|
|
46
|
+
});
|
|
47
|
+
return json(200, { success: true, job });
|
|
48
|
+
}
|
|
49
|
+
if (request.method === 'GET' && action === undefined) {
|
|
50
|
+
const status = url.searchParams.get('status') ?? undefined;
|
|
51
|
+
const jobs = await backend.listJobs(queueName, {
|
|
52
|
+
status: status,
|
|
53
|
+
limit: Number(url.searchParams.get('limit') ?? 50),
|
|
54
|
+
});
|
|
55
|
+
return json(200, { success: true, jobs });
|
|
56
|
+
}
|
|
57
|
+
if (request.method === 'GET' && (action === 'jobs' || action === 'inspect') && jobId) {
|
|
58
|
+
const job = await backend.getJob(queueName, jobId);
|
|
59
|
+
if (!job || job.queueName !== queueName) {
|
|
60
|
+
return json(404, { success: false, error: 'Job not found' });
|
|
61
|
+
}
|
|
62
|
+
return json(200, { success: true, job });
|
|
63
|
+
}
|
|
64
|
+
if (request.method === 'POST' && action === 'retry') {
|
|
65
|
+
const admin = authorizeControlRequest(request, ['admin']);
|
|
66
|
+
if (!admin.ok)
|
|
67
|
+
return unauthorizedResponse(admin);
|
|
68
|
+
const jobs = await backend.retryJob(queueName, jobId);
|
|
69
|
+
return json(200, { success: true, jobs });
|
|
70
|
+
}
|
|
71
|
+
if (request.method === 'GET' && action === 'info') {
|
|
72
|
+
const queues = await backend.listQueues();
|
|
73
|
+
const info = queues.find((entry) => entry.name === queueName) ?? null;
|
|
74
|
+
return json(200, { success: true, info });
|
|
75
|
+
}
|
|
76
|
+
return json(404, { success: false, error: 'Unknown queue control route' });
|
|
77
|
+
}
|
package/dist/cron.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type DiscoveredCronJob = {
|
|
2
|
+
jobId: string;
|
|
3
|
+
kebabId: string;
|
|
4
|
+
className: string;
|
|
5
|
+
methodName: string;
|
|
6
|
+
schedule: string | number;
|
|
7
|
+
cronExpression: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
allowConcurrent: boolean;
|
|
10
|
+
description?: string;
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
filePath: string;
|
|
13
|
+
};
|
|
14
|
+
export declare function toKebabCase(str: string): string;
|
|
15
|
+
export declare function normalizeCronExpression(schedule: string | number): string;
|
|
16
|
+
export declare function discoverScheduledJobsInFile(filePath: string): DiscoveredCronJob[];
|
|
17
|
+
export declare function discoverScheduledJobs(projectRoot: string): DiscoveredCronJob[];
|
|
18
|
+
/**
|
|
19
|
+
* Renders the private invocation interface module.
|
|
20
|
+
* Resolves the owning service through DI, awaits the method, and reports completion or failure.
|
|
21
|
+
*/
|
|
22
|
+
export declare function renderCronInvokerModule(jobs: readonly DiscoveredCronJob[]): string;
|
|
23
|
+
/**
|
|
24
|
+
* Adapter module used when a component has ONLY scheduled jobs and no HTTP ingress.
|
|
25
|
+
*/
|
|
26
|
+
export declare function renderCronAdapterModule(jobs: readonly DiscoveredCronJob[]): string;
|