@astrale-os/adapter-cloudflare 0.5.0-beta.2 → 0.5.0-beta.200
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 +256 -0
- package/dist/adapter.d.ts +11 -0
- package/dist/adapter.js +54 -0
- package/dist/build/bundle/admit.d.ts +3 -0
- package/dist/build/bundle/admit.js +47 -0
- package/dist/build/bundle/compiled-wasm-loader.d.ts +8 -0
- package/dist/build/bundle/compiled-wasm-loader.js +21 -0
- package/dist/build/bundler.d.ts +21 -0
- package/dist/build/bundler.js +96 -0
- package/dist/build/closure.d.ts +5 -0
- package/dist/build/closure.js +28 -0
- package/dist/build/codegen/execution.d.ts +3 -0
- package/dist/build/codegen/execution.js +2 -0
- package/dist/build/codegen/merge.d.ts +0 -1
- package/dist/build/codegen/merge.js +0 -1
- package/dist/build/codegen/wrangler.d.ts +32 -5
- package/dist/build/codegen/wrangler.js +77 -21
- package/dist/build/configuration/invocation-timeout.d.ts +2 -0
- package/dist/build/configuration/invocation-timeout.js +9 -0
- package/dist/build/configuration/request-limit.d.ts +2 -0
- package/dist/build/configuration/request-limit.js +9 -0
- package/dist/build/configuration/router.d.ts +7 -0
- package/dist/build/configuration/router.js +17 -0
- package/dist/build/configuration/settings.d.ts +6 -0
- package/dist/build/configuration/settings.js +40 -0
- package/dist/build/configuration.d.ts +4 -0
- package/dist/build/configuration.js +4 -0
- package/dist/build/frontend/files.d.ts +19 -0
- package/dist/build/frontend/files.js +99 -0
- package/dist/build/frontend/readiness.d.ts +16 -0
- package/dist/build/frontend/readiness.js +21 -0
- package/dist/build/frontend/revision.d.ts +5 -0
- package/dist/build/frontend/revision.js +14 -0
- package/dist/build/frontend/vite.d.ts +14 -0
- package/dist/build/frontend/vite.js +42 -0
- package/dist/build/frontend/vite.process.d.ts +7 -0
- package/dist/build/frontend/vite.process.js +65 -0
- package/dist/build/frontend/vite.runner.d.ts +1 -0
- package/dist/build/frontend/vite.runner.js +155 -0
- package/dist/build/index.d.ts +12 -16
- package/dist/build/index.js +12 -16
- package/dist/build/material.d.ts +10 -0
- package/dist/build/material.js +25 -0
- package/dist/build/metafile.d.ts +5 -0
- package/dist/build/metafile.js +14 -0
- package/dist/build/qualification.d.ts +5 -0
- package/dist/build/qualification.js +7 -0
- package/dist/build/roots.d.ts +2 -0
- package/dist/build/roots.js +34 -0
- package/dist/build/worker/codegen/entry.d.ts +9 -0
- package/dist/build/worker/codegen/entry.js +65 -0
- package/dist/build/worker/codegen/literal.d.ts +28 -0
- package/dist/build/worker/codegen/literal.js +136 -0
- package/dist/build/worker/codegen/service.d.ts +14 -0
- package/dist/build/worker/codegen/service.js +57 -0
- package/dist/build/worker/execution.prepare.d.ts +20 -0
- package/dist/build/worker/execution.prepare.js +129 -0
- package/dist/build/worker/media-type.d.ts +1 -0
- package/dist/build/worker/media-type.js +20 -0
- package/dist/build/worker/modules.d.ts +20 -0
- package/dist/build/worker/modules.js +32 -0
- package/dist/build/worker/specifier.d.ts +5 -0
- package/dist/build/worker/specifier.js +15 -0
- package/dist/build/worker/upload.d.ts +23 -0
- package/dist/build/worker/upload.js +42 -0
- package/dist/cloudflare.d.ts +1 -34
- package/dist/cloudflare.js +1 -205
- package/dist/hosting/activation.d.ts +38 -0
- package/dist/hosting/activation.js +109 -0
- package/dist/hosting/assets.d.ts +14 -0
- package/dist/hosting/assets.js +53 -0
- package/dist/hosting/bounded.d.ts +10 -0
- package/dist/hosting/bounded.js +32 -0
- package/dist/hosting/bytes.d.ts +6 -0
- package/dist/hosting/bytes.js +25 -0
- package/dist/hosting/client.d.ts +144 -0
- package/dist/hosting/client.js +491 -0
- package/dist/hosting/dispatcher/cron.d.mts +10 -0
- package/dist/hosting/dispatcher/cron.mjs +135 -0
- package/dist/hosting/dispatcher/deployments.d.mts +26 -0
- package/dist/hosting/dispatcher/deployments.mjs +158 -0
- package/dist/hosting/dispatcher/dispatcher.d.mts +17 -0
- package/dist/hosting/dispatcher/dispatcher.mjs +76 -0
- package/dist/hosting/dispatcher/env.d.mts +51 -0
- package/dist/hosting/dispatcher/failure.d.mts +4 -0
- package/dist/hosting/dispatcher/failure.mjs +15 -0
- package/dist/hosting/dispatcher/index.d.mts +4 -0
- package/dist/hosting/dispatcher/index.mjs +8 -0
- package/dist/hosting/dispatcher/modules.d.ts +24 -0
- package/dist/hosting/dispatcher/modules.js +55 -0
- package/dist/hosting/dispatcher/services.d.mts +23 -0
- package/dist/hosting/dispatcher/services.mjs +202 -0
- package/dist/hosting/identity.d.ts +15 -0
- package/dist/hosting/identity.js +36 -0
- package/dist/hosting/index.d.ts +34 -0
- package/dist/hosting/index.js +32 -0
- package/dist/hosting/kv.d.ts +90 -0
- package/dist/hosting/kv.js +207 -0
- package/dist/hosting/listing.d.ts +34 -0
- package/dist/hosting/listing.js +50 -0
- package/dist/hosting/names.d.ts +16 -0
- package/dist/hosting/names.js +28 -0
- package/dist/hosting/provision/account.d.ts +11 -0
- package/dist/hosting/provision/account.js +86 -0
- package/dist/hosting/provision/dispatcher.d.ts +42 -0
- package/dist/hosting/provision/dispatcher.js +99 -0
- package/dist/hosting/provision/ingress.d.ts +12 -0
- package/dist/hosting/provision/ingress.js +31 -0
- package/dist/hosting/provision/plan.d.ts +143 -0
- package/dist/hosting/provision/plan.js +246 -0
- package/dist/hosting/provision/routing.d.ts +82 -0
- package/dist/hosting/provision/routing.js +226 -0
- package/dist/hosting/readiness.d.ts +53 -0
- package/dist/hosting/readiness.js +82 -0
- package/dist/hosting/refusal.d.ts +28 -0
- package/dist/hosting/refusal.js +13 -0
- package/dist/hosting/retirement.d.ts +70 -0
- package/dist/hosting/retirement.js +107 -0
- package/dist/hosting/retry-after.d.ts +6 -0
- package/dist/hosting/retry-after.js +55 -0
- package/dist/hosting/script.d.ts +186 -0
- package/dist/hosting/script.js +324 -0
- package/dist/hosting/testing/fake-cloudflare.d.ts +212 -0
- package/dist/hosting/testing/fake-cloudflare.js +757 -0
- package/dist/hosting/testing/index.d.ts +6 -0
- package/dist/hosting/testing/index.js +6 -0
- package/dist/hosting/time.d.ts +4 -0
- package/dist/hosting/time.js +20 -0
- package/dist/identity/binding.d.ts +2 -0
- package/dist/identity/binding.js +2 -0
- package/dist/identity/material.d.ts +6 -0
- package/dist/identity/material.js +29 -0
- package/dist/index.d.ts +14 -13
- package/dist/index.js +13 -12
- package/dist/namespace/configure.d.ts +22 -0
- package/dist/namespace/configure.js +199 -0
- package/dist/namespace/coordinates.d.ts +15 -0
- package/dist/namespace/coordinates.js +49 -0
- package/dist/namespace/deploy-release.d.ts +53 -0
- package/dist/namespace/deploy-release.js +389 -0
- package/dist/namespace/hosting.d.ts +29 -0
- package/dist/namespace/hosting.js +120 -0
- package/dist/namespace/index.d.ts +12 -0
- package/dist/namespace/index.js +12 -0
- package/dist/namespace/list.d.ts +18 -0
- package/dist/namespace/list.js +63 -0
- package/dist/namespace/readiness.d.ts +30 -0
- package/dist/namespace/readiness.js +31 -0
- package/dist/namespace/record.d.ts +21 -0
- package/dist/namespace/record.js +53 -0
- package/dist/params.d.ts +63 -66
- package/dist/params.js +0 -1
- package/dist/release/build.d.ts +35 -0
- package/dist/release/build.js +44 -0
- package/dist/release/deployment.d.ts +44 -0
- package/dist/release/deployment.js +141 -0
- package/dist/release/index.d.ts +2 -0
- package/dist/release/index.js +2 -0
- package/dist/worker/binary.d.ts +21 -0
- package/dist/worker/binary.js +31 -0
- package/dist/worker/build-digest.d.ts +10 -0
- package/dist/worker/build-digest.js +22 -0
- package/dist/worker/cache.d.ts +28 -0
- package/dist/worker/cache.js +70 -0
- package/dist/worker/durable.d.ts +3 -0
- package/dist/worker/durable.js +5 -0
- package/dist/worker/entry.d.ts +47 -0
- package/dist/worker/entry.js +135 -0
- package/dist/worker/fetch.d.ts +11 -0
- package/dist/worker/fetch.js +31 -0
- package/dist/worker/front.d.ts +56 -0
- package/dist/worker/front.js +59 -0
- package/dist/worker/frontend.d.ts +4 -0
- package/dist/worker/frontend.js +83 -0
- package/dist/worker/identity.d.ts +7 -0
- package/dist/worker/identity.js +23 -0
- package/dist/worker/index.d.ts +6 -48
- package/dist/worker/index.js +6 -186
- package/dist/worker/restate.d.ts +6 -0
- package/dist/worker/restate.js +6 -0
- package/dist/worker/settings.d.ts +47 -0
- package/dist/worker/settings.js +163 -0
- package/dist/wrangler/bundle.d.ts +10 -0
- package/dist/wrangler/bundle.js +27 -0
- package/dist/wrangler/index.d.ts +3 -0
- package/dist/wrangler/index.js +3 -0
- package/dist/wrangler/process.d.ts +15 -0
- package/dist/wrangler/process.js +113 -0
- package/dist/wrangler/type-generation.d.ts +6 -0
- package/dist/wrangler/type-generation.js +19 -0
- package/package.json +47 -22
- package/template/astrale.config.ts +43 -29
- package/dist/build/codegen/identity.d.ts +0 -10
- package/dist/build/codegen/identity.d.ts.map +0 -1
- package/dist/build/codegen/identity.js +0 -32
- package/dist/build/codegen/identity.js.map +0 -1
- package/dist/build/codegen/merge.d.ts.map +0 -1
- package/dist/build/codegen/merge.js.map +0 -1
- package/dist/build/codegen/worker.d.ts +0 -12
- package/dist/build/codegen/worker.d.ts.map +0 -1
- package/dist/build/codegen/worker.js +0 -40
- package/dist/build/codegen/worker.js.map +0 -1
- package/dist/build/codegen/wrangler.d.ts.map +0 -1
- package/dist/build/codegen/wrangler.js.map +0 -1
- package/dist/build/frontend.d.ts +0 -26
- package/dist/build/frontend.d.ts.map +0 -1
- package/dist/build/frontend.js +0 -263
- package/dist/build/frontend.js.map +0 -1
- package/dist/build/index.d.ts.map +0 -1
- package/dist/build/index.js.map +0 -1
- package/dist/build/parse-output.d.ts +0 -27
- package/dist/build/parse-output.d.ts.map +0 -1
- package/dist/build/parse-output.js +0 -58
- package/dist/build/parse-output.js.map +0 -1
- package/dist/build/prepare.d.ts +0 -18
- package/dist/build/prepare.d.ts.map +0 -1
- package/dist/build/prepare.js +0 -117
- package/dist/build/prepare.js.map +0 -1
- package/dist/build/worker.d.ts +0 -15
- package/dist/build/worker.d.ts.map +0 -1
- package/dist/build/worker.js +0 -21
- package/dist/build/worker.js.map +0 -1
- package/dist/build/wrangler-cli.d.ts +0 -86
- package/dist/build/wrangler-cli.d.ts.map +0 -1
- package/dist/build/wrangler-cli.js +0 -275
- package/dist/build/wrangler-cli.js.map +0 -1
- package/dist/cloudflare.d.ts.map +0 -1
- package/dist/cloudflare.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/params.d.ts.map +0 -1
- package/dist/params.js.map +0 -1
- package/dist/worker/index.d.ts.map +0 -1
- package/dist/worker/index.js.map +0 -1
- package/src/build/codegen/identity.ts +0 -40
- package/src/build/codegen/merge.ts +0 -92
- package/src/build/codegen/worker.ts +0 -53
- package/src/build/codegen/wrangler.ts +0 -127
- package/src/build/frontend.ts +0 -324
- package/src/build/index.ts +0 -29
- package/src/build/parse-output.ts +0 -60
- package/src/build/prepare.ts +0 -161
- package/src/build/worker.ts +0 -37
- package/src/build/wrangler-cli.ts +0 -362
- package/src/cloudflare.ts +0 -251
- package/src/index.ts +0 -21
- package/src/params.ts +0 -92
- package/src/worker/index.ts +0 -276
- package/src/worker/tsconfig.json +0 -10
package/dist/worker/index.d.ts
CHANGED
|
@@ -1,48 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
export interface Fetcher {
|
|
8
|
-
fetch(request: Request): Response | Promise<Response>;
|
|
9
|
-
}
|
|
10
|
-
/** Adapter-owned bindings generated into every Cloudflare deployment. */
|
|
11
|
-
export interface CloudflareAdapterBindings {
|
|
12
|
-
/** Native Cloudflare deployment metadata (direct Wrangler deployments). */
|
|
13
|
-
readonly ASTRALE_VERSION_METADATA?: {
|
|
14
|
-
readonly id: string;
|
|
15
|
-
readonly tag: string;
|
|
16
|
-
readonly timestamp: string;
|
|
17
|
-
};
|
|
18
|
-
/** Content-derived generation supplied by providers without version metadata. */
|
|
19
|
-
readonly ASTRALE_DEPLOYMENT_VERSION?: string;
|
|
20
|
-
readonly WORKER_URL?: string;
|
|
21
|
-
readonly ASSETS?: Fetcher;
|
|
22
|
-
readonly SELF?: Fetcher;
|
|
23
|
-
readonly FRONTEND_DEV_URL?: string;
|
|
24
|
-
readonly DISPATCHER?: {
|
|
25
|
-
get(name: string): Fetcher;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
export interface CloudflareWorkerConfig<S extends schema.DomainSchema, Bindings extends CloudflareAdapterBindings, Environment> {
|
|
29
|
-
readonly definition: DomainDefinition<S>;
|
|
30
|
-
readonly privateKey: RemoteIdentityInput['privateKey'] | ((bindings: Bindings) => RemoteIdentityInput['privateKey']);
|
|
31
|
-
/** Compose provider bindings into the exact Domain environment once per deployment generation. */
|
|
32
|
-
readonly resolveEnvironment?: (bindings: Bindings) => Environment | Promise<Environment>;
|
|
33
|
-
readonly manifest?: CloudflareManifest;
|
|
34
|
-
readonly resolveUrl?: (bindings: Bindings, requestOrigin: string) => string;
|
|
35
|
-
readonly selfBinding?: (bindings: Bindings) => Fetcher | null | undefined;
|
|
36
|
-
readonly routeSubrequest?: (url: URL, bindings: Bindings) => Fetcher | null | undefined;
|
|
37
|
-
}
|
|
38
|
-
export interface CloudflareWorker<Environment> {
|
|
39
|
-
fetch(request: Request, environment: Environment, executionContext?: ExecutionContext): Response | Promise<Response>;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Compose one Cloudflare module Worker without request-global mutation.
|
|
43
|
-
* Environment bindings are captured once per immutable serving URL/version generation.
|
|
44
|
-
*/
|
|
45
|
-
export declare function cloudflareWorkerEntry<Bindings extends CloudflareAdapterBindings, Environment = Bindings>(): <const S extends schema.DomainSchema>(config: CloudflareWorkerConfig<S, Bindings, Environment>) => CloudflareWorker<Bindings>;
|
|
46
|
-
/** Close an evicted Application before its environment bindings leave the cache. */
|
|
47
|
-
export declare function closeEvictedApplication(application: Pick<Application, 'close'>, now?: () => number): Promise<void>;
|
|
48
|
-
//# sourceMappingURL=index.d.ts.map
|
|
1
|
+
export * from './entry.js';
|
|
2
|
+
export { cloudflareCache, DEPLOYMENT_CACHE_NAME, type CacheApiCache, type CacheApiStorage, type CloudflareCacheOptions, } from './cache.js';
|
|
3
|
+
export { BUILD_DIGEST_BINDING } from './build-digest.js';
|
|
4
|
+
export { decodeSigningIdentity, runtimeBindings } from './identity.js';
|
|
5
|
+
export { decodeWorkerSettings, deploymentSettings, encodeWorkerSettings, SETTINGS_BINDING, type CloudflareWorkerSettings, type WorkerSettings, } from './settings.js';
|
|
6
|
+
export { restateServing } from './durable.js';
|
package/dist/worker/index.js
CHANGED
|
@@ -1,186 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* Compose one Cloudflare module Worker without request-global mutation.
|
|
8
|
-
* Environment bindings are captured once per immutable serving URL/version generation.
|
|
9
|
-
*/
|
|
10
|
-
export function cloudflareWorkerEntry() {
|
|
11
|
-
return function (config) {
|
|
12
|
-
const applications = new Map();
|
|
13
|
-
const applicationFor = async (url, bindings) => {
|
|
14
|
-
const version = deploymentVersion(bindings);
|
|
15
|
-
const key = `${url}\u0000${version}`;
|
|
16
|
-
const cached = applications.get(key);
|
|
17
|
-
if (cached !== undefined)
|
|
18
|
-
return cached;
|
|
19
|
-
const environment = config.resolveEnvironment === undefined
|
|
20
|
-
? bindings
|
|
21
|
-
: await config.resolveEnvironment(bindings);
|
|
22
|
-
const routedFetch = async (input, init) => {
|
|
23
|
-
const request = new Request(input, init);
|
|
24
|
-
const destination = new URL(request.url);
|
|
25
|
-
if (destination.origin === new URL(url).origin) {
|
|
26
|
-
const self = config.selfBinding?.(bindings);
|
|
27
|
-
if (self !== null && self !== undefined)
|
|
28
|
-
return self.fetch(request);
|
|
29
|
-
return serving.app.fetch(request, environment);
|
|
30
|
-
}
|
|
31
|
-
const route = config.routeSubrequest?.(destination, bindings);
|
|
32
|
-
return route === null || route === undefined ? fetch(request) : route.fetch(request);
|
|
33
|
-
};
|
|
34
|
-
const deploymentQuery = `deployment=${encodeURIComponent(version)}`;
|
|
35
|
-
const issuerId = issuer.accept(url);
|
|
36
|
-
const host = new HonoApplication();
|
|
37
|
-
host.get('/meta', (context) => context.json({
|
|
38
|
-
origin: config.definition.schema.origin,
|
|
39
|
-
issuer: issuerId,
|
|
40
|
-
schemaRevision: config.definition.domain.$.revision,
|
|
41
|
-
deploymentVersion: version,
|
|
42
|
-
}));
|
|
43
|
-
const serving = createRemoteApplication({
|
|
44
|
-
definition: config.definition,
|
|
45
|
-
environment,
|
|
46
|
-
identity: {
|
|
47
|
-
issuer: issuerId,
|
|
48
|
-
subject: config.definition.schema.origin,
|
|
49
|
-
privateKey: typeof config.privateKey === 'function'
|
|
50
|
-
? config.privateKey(bindings)
|
|
51
|
-
: config.privateKey,
|
|
52
|
-
fetch: routedFetch,
|
|
53
|
-
...(new URL(url).protocol === 'http:' ? { allowInsecureHttp: true } : {}),
|
|
54
|
-
},
|
|
55
|
-
deployment: {
|
|
56
|
-
bundleHref: `${url}/.well-known/astrale/domain.bundle.json?${deploymentQuery}`,
|
|
57
|
-
endpoint: { http: `${url}/invoke` },
|
|
58
|
-
...(config.definition.frontend === undefined
|
|
59
|
-
? {}
|
|
60
|
-
: { frontendBaseHref: url, frontendCacheKey: version }),
|
|
61
|
-
...(config.manifest === undefined ? {} : { manifest: config.manifest }),
|
|
62
|
-
installation: {
|
|
63
|
-
href: `${url}/install`,
|
|
64
|
-
method: 'POST',
|
|
65
|
-
authentication: { kind: 'public' },
|
|
66
|
-
},
|
|
67
|
-
},
|
|
68
|
-
app: host,
|
|
69
|
-
server: {
|
|
70
|
-
path: '/invoke',
|
|
71
|
-
health: { probe: { check: () => true } },
|
|
72
|
-
limits: {
|
|
73
|
-
maximumRequestBytes: 1024 * 1024,
|
|
74
|
-
maximumPublicationBytes: 1024 * 1024,
|
|
75
|
-
maximumBundleBytes: 8 * 1024 * 1024,
|
|
76
|
-
maximumInstallationBytes: 8 * 1024 * 1024,
|
|
77
|
-
invocationTimeoutMs: 30_000,
|
|
78
|
-
},
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
const created = Object.freeze({
|
|
82
|
-
origin: new URL(url).origin,
|
|
83
|
-
application: serving,
|
|
84
|
-
environment,
|
|
85
|
-
});
|
|
86
|
-
if (applications.size >= MAXIMUM_CACHED_APPLICATIONS) {
|
|
87
|
-
const oldest = applications.keys().next().value;
|
|
88
|
-
if (oldest !== undefined) {
|
|
89
|
-
const evicted = applications.get(oldest);
|
|
90
|
-
applications.delete(oldest);
|
|
91
|
-
if (evicted !== undefined) {
|
|
92
|
-
try {
|
|
93
|
-
await closeEvictedApplication(evicted.application);
|
|
94
|
-
}
|
|
95
|
-
catch (cause) {
|
|
96
|
-
await serving
|
|
97
|
-
.close({ deadline: Date.now() + APPLICATION_CLOSE_TIMEOUT_MS })
|
|
98
|
-
.catch(() => undefined);
|
|
99
|
-
throw cause;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
applications.set(key, created);
|
|
105
|
-
return created;
|
|
106
|
-
};
|
|
107
|
-
return Object.freeze({
|
|
108
|
-
async fetch(request, bindings, executionContext) {
|
|
109
|
-
const requestUrl = new URL(request.url);
|
|
110
|
-
if (config.definition.frontend !== undefined) {
|
|
111
|
-
const asset = await frontendAsset(config.definition, bindings, requestUrl, request);
|
|
112
|
-
if (asset !== undefined)
|
|
113
|
-
return asset;
|
|
114
|
-
}
|
|
115
|
-
const publicUrl = canonicalServingUrl(config.resolveUrl?.(bindings, requestOrigin(requestUrl, request)) ??
|
|
116
|
-
bindings.WORKER_URL ??
|
|
117
|
-
requestOrigin(requestUrl, request));
|
|
118
|
-
const serving = await applicationFor(publicUrl, bindings);
|
|
119
|
-
return serving.application.app.fetch(request, serving.environment, executionContext);
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/** Close an evicted Application before its environment bindings leave the cache. */
|
|
125
|
-
export function closeEvictedApplication(application, now = Date.now) {
|
|
126
|
-
return application.close({ deadline: now() + APPLICATION_CLOSE_TIMEOUT_MS });
|
|
127
|
-
}
|
|
128
|
-
async function frontendAsset(definition, environment, url, request) {
|
|
129
|
-
const frontend = definition.frontend;
|
|
130
|
-
if (frontend === undefined || frontend.source.kind === 'external')
|
|
131
|
-
return undefined;
|
|
132
|
-
const routes = frontend.routes;
|
|
133
|
-
const route = Object.values(routes).find((candidate) => candidate.path === url.pathname);
|
|
134
|
-
const staticAsset = url.pathname.startsWith('/assets/');
|
|
135
|
-
if (route === undefined && !staticAsset)
|
|
136
|
-
return undefined;
|
|
137
|
-
if (request.method !== 'GET' && request.method !== 'HEAD')
|
|
138
|
-
return undefined;
|
|
139
|
-
const binding = environment.ASSETS;
|
|
140
|
-
if (binding === undefined)
|
|
141
|
-
return undefined;
|
|
142
|
-
const dev = environment.FRONTEND_DEV_URL?.replace(/\/+$/u, '');
|
|
143
|
-
if (dev !== undefined && dev.length > 0) {
|
|
144
|
-
return fetch(new Request(`${dev}${url.pathname}${url.search}`, request));
|
|
145
|
-
}
|
|
146
|
-
const path = route === undefined ? `${url.pathname}${url.search}` : `/${route.document}${url.search}`;
|
|
147
|
-
return binding.fetch(new Request(new URL(path, url.origin), request));
|
|
148
|
-
}
|
|
149
|
-
function requestOrigin(url, request) {
|
|
150
|
-
if (url.protocol !== 'http:')
|
|
151
|
-
return url.origin;
|
|
152
|
-
const forwarded = request.headers.get('x-forwarded-proto')?.split(',')[0]?.trim().toLowerCase();
|
|
153
|
-
return forwarded === 'https' ? `https://${url.host}` : url.origin;
|
|
154
|
-
}
|
|
155
|
-
function canonicalServingUrl(input) {
|
|
156
|
-
let url;
|
|
157
|
-
try {
|
|
158
|
-
url = new URL(input);
|
|
159
|
-
}
|
|
160
|
-
catch (cause) {
|
|
161
|
-
throw new TypeError('Cloudflare Worker serving URL is invalid.', { cause });
|
|
162
|
-
}
|
|
163
|
-
if ((url.protocol !== 'https:' && url.protocol !== 'http:') ||
|
|
164
|
-
url.username !== '' ||
|
|
165
|
-
url.password !== '' ||
|
|
166
|
-
url.pathname !== '/' ||
|
|
167
|
-
url.search !== '' ||
|
|
168
|
-
url.hash !== '') {
|
|
169
|
-
throw new TypeError('Cloudflare Worker serving URL must be one HTTP(S) origin.');
|
|
170
|
-
}
|
|
171
|
-
return url.origin;
|
|
172
|
-
}
|
|
173
|
-
function deploymentVersion(environment) {
|
|
174
|
-
const input = environment.ASTRALE_VERSION_METADATA;
|
|
175
|
-
if (input !== null &&
|
|
176
|
-
typeof input === 'object' &&
|
|
177
|
-
typeof input.id === 'string' &&
|
|
178
|
-
input.id.length > 0) {
|
|
179
|
-
return input.id;
|
|
180
|
-
}
|
|
181
|
-
const fallback = environment.ASTRALE_DEPLOYMENT_VERSION;
|
|
182
|
-
if (typeof fallback === 'string' && fallback.length > 0)
|
|
183
|
-
return fallback;
|
|
184
|
-
throw new TypeError('Cloudflare Worker deployment version is unavailable.');
|
|
185
|
-
}
|
|
186
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export * from './entry.js';
|
|
2
|
+
export { cloudflareCache, DEPLOYMENT_CACHE_NAME, } from './cache.js';
|
|
3
|
+
export { BUILD_DIGEST_BINDING } from './build-digest.js';
|
|
4
|
+
export { decodeSigningIdentity, runtimeBindings } from './identity.js';
|
|
5
|
+
export { decodeWorkerSettings, deploymentSettings, encodeWorkerSettings, SETTINGS_BINDING, } from './settings.js';
|
|
6
|
+
export { restateServing } from './durable.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The workflow engine's Workers build. Generated code imports it only for a Domain that declares
|
|
3
|
+
* durable callables and retains it as a separate module, imported on the first engine request.
|
|
4
|
+
* Ordinary requests never evaluate this module or instantiate its WebAssembly core.
|
|
5
|
+
*/
|
|
6
|
+
export * from '@restatedev/restate-sdk-cloudflare-workers/fetch';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The workflow engine's Workers build. Generated code imports it only for a Domain that declares
|
|
3
|
+
* durable callables and retains it as a separate module, imported on the first engine request.
|
|
4
|
+
* Ordinary requests never evaluate this module or instantiate its WebAssembly core.
|
|
5
|
+
*/
|
|
6
|
+
export * from '@restatedev/restate-sdk-cloudflare-workers/fetch';
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { ExchangeOptions } from '@astrale-os/sdk/execution';
|
|
2
|
+
import type { DurableWorkerOptions } from '../params.js';
|
|
3
|
+
import type { CloudflareAdapterBindings, Fetcher } from './front.js';
|
|
4
|
+
/** Plain binding that carries one deployment's execution settings as canonical JSON. */
|
|
5
|
+
export declare const SETTINGS_BINDING = "ASTRALE_SETTINGS";
|
|
6
|
+
export declare const DEFAULT_MAXIMUM_REQUEST_BYTES: number;
|
|
7
|
+
export declare const MAXIMUM_REQUEST_BYTES: number;
|
|
8
|
+
export declare const DEFAULT_INVOCATION_TIMEOUT_MS = 30000;
|
|
9
|
+
export declare const MAXIMUM_INVOCATION_TIMEOUT_MS = 2147483647;
|
|
10
|
+
/** The execution settings one deployment binds as `ASTRALE_SETTINGS`, defaults resolved. */
|
|
11
|
+
export interface WorkerSettings {
|
|
12
|
+
/** Durable policy bound by this deployment; semantic bounds are admitted lazily by SDK serving. */
|
|
13
|
+
readonly durableOptions?: DurableWorkerOptions;
|
|
14
|
+
readonly maximumRequestBytes: number;
|
|
15
|
+
readonly invocationTimeoutMs: number;
|
|
16
|
+
readonly tokenExchange: false | {
|
|
17
|
+
readonly maximumLifetimeSeconds: number;
|
|
18
|
+
};
|
|
19
|
+
readonly router: false | {
|
|
20
|
+
/**
|
|
21
|
+
* Service binding that reaches the instance router. Hosts that deploy immutable Workers
|
|
22
|
+
* bind ROUTER.
|
|
23
|
+
*/
|
|
24
|
+
readonly binding: string;
|
|
25
|
+
readonly hostSuffix: string;
|
|
26
|
+
readonly minLabels: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/** What one isolate executes under, given by a hand-composed Worker or read from its bindings. */
|
|
30
|
+
export interface CloudflareWorkerSettings<Bindings> {
|
|
31
|
+
readonly durableOptions?: DurableWorkerOptions;
|
|
32
|
+
readonly routeSubrequest?: (url: URL, bindings: Bindings) => Fetcher | null | undefined;
|
|
33
|
+
readonly invocationTimeoutMs?: number;
|
|
34
|
+
readonly maximumRequestBytes?: number;
|
|
35
|
+
readonly tokenExchange?: ExchangeOptions | false;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Read the execution settings a deployment bound as `ASTRALE_SETTINGS`. The serving origin, and
|
|
39
|
+
* so the issuer, is the deployment's `WORKER_URL`, which the front door reads.
|
|
40
|
+
*/
|
|
41
|
+
export declare function deploymentSettings<Bindings extends CloudflareAdapterBindings>(bindings: Pick<Bindings, 'ASTRALE_SETTINGS'>): CloudflareWorkerSettings<Bindings>;
|
|
42
|
+
/** Encode settings as the canonical JSON (RFC 8785) text bound as `ASTRALE_SETTINGS`. */
|
|
43
|
+
export declare function encodeWorkerSettings(settings: WorkerSettings): string;
|
|
44
|
+
/** Admit the `ASTRALE_SETTINGS` text one deployment bound; absent or malformed fails closed. */
|
|
45
|
+
export declare function decodeWorkerSettings(input: unknown): WorkerSettings;
|
|
46
|
+
/** Capture the plain binding shape; SDK serving owns retention/retry/deadline semantics. */
|
|
47
|
+
export declare function captureDurableOptions(input: unknown): DurableWorkerOptions;
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// A generated Worker carries no environment value in its bytes: the host that deploys it binds
|
|
2
|
+
// the execution settings of that deployment, so one build serves every environment unchanged.
|
|
3
|
+
/** Plain binding that carries one deployment's execution settings as canonical JSON. */
|
|
4
|
+
export const SETTINGS_BINDING = 'ASTRALE_SETTINGS';
|
|
5
|
+
export const DEFAULT_MAXIMUM_REQUEST_BYTES = 1024 * 1024;
|
|
6
|
+
export const MAXIMUM_REQUEST_BYTES = 16 * 1024 * 1024;
|
|
7
|
+
export const DEFAULT_INVOCATION_TIMEOUT_MS = 30_000;
|
|
8
|
+
export const MAXIMUM_INVOCATION_TIMEOUT_MS = 2_147_483_647;
|
|
9
|
+
const BINDING_NAME = /^[A-Za-z_$][A-Za-z0-9_$]{0,127}$/u;
|
|
10
|
+
/**
|
|
11
|
+
* Read the execution settings a deployment bound as `ASTRALE_SETTINGS`. The serving origin, and
|
|
12
|
+
* so the issuer, is the deployment's `WORKER_URL`, which the front door reads.
|
|
13
|
+
*/
|
|
14
|
+
export function deploymentSettings(bindings) {
|
|
15
|
+
const settings = decodeWorkerSettings(bindings.ASTRALE_SETTINGS);
|
|
16
|
+
return Object.freeze({
|
|
17
|
+
...(settings.durableOptions === undefined ? {} : { durableOptions: settings.durableOptions }),
|
|
18
|
+
maximumRequestBytes: settings.maximumRequestBytes,
|
|
19
|
+
invocationTimeoutMs: settings.invocationTimeoutMs,
|
|
20
|
+
tokenExchange: settings.tokenExchange === false
|
|
21
|
+
? false
|
|
22
|
+
: Object.freeze({ maximumTtlSeconds: settings.tokenExchange.maximumLifetimeSeconds }),
|
|
23
|
+
...(settings.router === false
|
|
24
|
+
? {}
|
|
25
|
+
: { routeSubrequest: instanceRoute(settings.router) }),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** Encode settings as the canonical JSON (RFC 8785) text bound as `ASTRALE_SETTINGS`. */
|
|
29
|
+
export function encodeWorkerSettings(settings) {
|
|
30
|
+
const admitted = acceptWorkerSettings(settings);
|
|
31
|
+
return JSON.stringify({
|
|
32
|
+
...(admitted.durableOptions === undefined ? {} : { durableOptions: admitted.durableOptions }),
|
|
33
|
+
invocationTimeoutMs: admitted.invocationTimeoutMs,
|
|
34
|
+
maximumRequestBytes: admitted.maximumRequestBytes,
|
|
35
|
+
router: admitted.router === false
|
|
36
|
+
? false
|
|
37
|
+
: {
|
|
38
|
+
binding: admitted.router.binding,
|
|
39
|
+
hostSuffix: admitted.router.hostSuffix,
|
|
40
|
+
minLabels: admitted.router.minLabels,
|
|
41
|
+
},
|
|
42
|
+
tokenExchange: admitted.tokenExchange === false
|
|
43
|
+
? false
|
|
44
|
+
: { maximumLifetimeSeconds: admitted.tokenExchange.maximumLifetimeSeconds },
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** Admit the `ASTRALE_SETTINGS` text one deployment bound; absent or malformed fails closed. */
|
|
48
|
+
export function decodeWorkerSettings(input) {
|
|
49
|
+
if (input === undefined) {
|
|
50
|
+
throw new TypeError(`Cloudflare Worker requires the ${SETTINGS_BINDING} binding.`);
|
|
51
|
+
}
|
|
52
|
+
if (typeof input !== 'string')
|
|
53
|
+
invalid();
|
|
54
|
+
let value;
|
|
55
|
+
try {
|
|
56
|
+
value = JSON.parse(input);
|
|
57
|
+
}
|
|
58
|
+
catch (cause) {
|
|
59
|
+
throw new TypeError(`Cloudflare Worker ${SETTINGS_BINDING} binding is invalid JSON.`, {
|
|
60
|
+
cause,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return acceptWorkerSettings(value);
|
|
64
|
+
}
|
|
65
|
+
function acceptWorkerSettings(input) {
|
|
66
|
+
const value = exact(input, ['invocationTimeoutMs', 'maximumRequestBytes', 'router', 'tokenExchange'], ['durableOptions']);
|
|
67
|
+
const durableOptions = value.durableOptions;
|
|
68
|
+
const maximumRequestBytes = integer(value.maximumRequestBytes, 1, MAXIMUM_REQUEST_BYTES);
|
|
69
|
+
const invocationTimeoutMs = integer(value.invocationTimeoutMs, 1, MAXIMUM_INVOCATION_TIMEOUT_MS);
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
...(durableOptions === undefined
|
|
72
|
+
? {}
|
|
73
|
+
: { durableOptions: captureDurableOptions(durableOptions) }),
|
|
74
|
+
maximumRequestBytes,
|
|
75
|
+
invocationTimeoutMs,
|
|
76
|
+
tokenExchange: value.tokenExchange === false ? false : tokenExchange(value.tokenExchange),
|
|
77
|
+
router: value.router === false ? false : router(value.router),
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
function tokenExchange(input) {
|
|
81
|
+
const value = exact(input, ['maximumLifetimeSeconds']);
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
maximumLifetimeSeconds: integer(value.maximumLifetimeSeconds, 1, Number.MAX_SAFE_INTEGER),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
function router(input) {
|
|
87
|
+
const value = exact(input, ['binding', 'hostSuffix', 'minLabels']);
|
|
88
|
+
if (typeof value.binding !== 'string' || !BINDING_NAME.test(value.binding))
|
|
89
|
+
invalid();
|
|
90
|
+
if (typeof value.hostSuffix !== 'string')
|
|
91
|
+
invalid();
|
|
92
|
+
return Object.freeze({
|
|
93
|
+
binding: value.binding,
|
|
94
|
+
hostSuffix: value.hostSuffix,
|
|
95
|
+
minLabels: integer(value.minLabels, 0, Number.MAX_SAFE_INTEGER),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
/** Send instance hosts under the router suffix through its binding; everything else is public. */
|
|
99
|
+
function instanceRoute(router) {
|
|
100
|
+
return (url, bindings) => {
|
|
101
|
+
// Services own their public ingress, including retirement and private-route isolation.
|
|
102
|
+
if (url.hostname.split('.')[1] === 'svc')
|
|
103
|
+
return null;
|
|
104
|
+
const fetcher = Reflect.get(bindings, router.binding);
|
|
105
|
+
if (fetcher &&
|
|
106
|
+
url.hostname.endsWith(router.hostSuffix) &&
|
|
107
|
+
url.hostname.split('.').length >= router.minLabels) {
|
|
108
|
+
return fetcher;
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function exact(input, keys, optional = []) {
|
|
114
|
+
if (input === null ||
|
|
115
|
+
typeof input !== 'object' ||
|
|
116
|
+
Array.isArray(input) ||
|
|
117
|
+
Reflect.ownKeys(input).some((key) => typeof key !== 'string' || (!keys.includes(key) && !optional.includes(key))) ||
|
|
118
|
+
keys.some((key) => !Object.hasOwn(input, key))) {
|
|
119
|
+
invalid();
|
|
120
|
+
}
|
|
121
|
+
return input;
|
|
122
|
+
}
|
|
123
|
+
/** Capture the plain binding shape; SDK serving owns retention/retry/deadline semantics. */
|
|
124
|
+
export function captureDurableOptions(input) {
|
|
125
|
+
const value = exact(input, [], ['retentionMs', 'retry', 'stepTimeoutMs']);
|
|
126
|
+
const retentionMs = optionalNumber(value.retentionMs);
|
|
127
|
+
const retry = value.retry;
|
|
128
|
+
const stepTimeoutMs = optionalNumber(value.stepTimeoutMs);
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
...(retentionMs === undefined ? {} : { retentionMs }),
|
|
131
|
+
...(retry === undefined ? {} : { retry: captureRetry(retry) }),
|
|
132
|
+
...(stepTimeoutMs === undefined ? {} : { stepTimeoutMs }),
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
function captureRetry(input) {
|
|
136
|
+
const value = exact(input, [], ['initialIntervalMs', 'maxAttempts', 'maxDurationMs', 'maxIntervalMs']);
|
|
137
|
+
const initialIntervalMs = optionalNumber(value.initialIntervalMs);
|
|
138
|
+
const maxAttempts = optionalNumber(value.maxAttempts);
|
|
139
|
+
const maxDurationMs = optionalNumber(value.maxDurationMs);
|
|
140
|
+
const maxIntervalMs = optionalNumber(value.maxIntervalMs);
|
|
141
|
+
return Object.freeze({
|
|
142
|
+
...(initialIntervalMs === undefined ? {} : { initialIntervalMs }),
|
|
143
|
+
...(maxAttempts === undefined ? {} : { maxAttempts }),
|
|
144
|
+
...(maxDurationMs === undefined ? {} : { maxDurationMs }),
|
|
145
|
+
...(maxIntervalMs === undefined ? {} : { maxIntervalMs }),
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
function optionalNumber(input) {
|
|
149
|
+
if (input === undefined)
|
|
150
|
+
return undefined;
|
|
151
|
+
if (typeof input !== 'number' || !Number.isFinite(input))
|
|
152
|
+
invalid();
|
|
153
|
+
return input;
|
|
154
|
+
}
|
|
155
|
+
function integer(input, minimum, maximum) {
|
|
156
|
+
if (!Number.isSafeInteger(input) || input < minimum || input > maximum) {
|
|
157
|
+
invalid();
|
|
158
|
+
}
|
|
159
|
+
return input;
|
|
160
|
+
}
|
|
161
|
+
function invalid() {
|
|
162
|
+
throw new TypeError(`Cloudflare Worker ${SETTINGS_BINDING} binding is invalid.`);
|
|
163
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { readdirSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { runCapture, wranglerCommand } from './process.js';
|
|
4
|
+
export async function runWranglerBundle(args) {
|
|
5
|
+
const { cmd, prefix } = wranglerCommand(args.projectDir);
|
|
6
|
+
const { code, out } = await runCapture(cmd, [
|
|
7
|
+
...prefix,
|
|
8
|
+
'deploy',
|
|
9
|
+
'--dry-run',
|
|
10
|
+
'--outdir',
|
|
11
|
+
args.outDir,
|
|
12
|
+
'--metafile',
|
|
13
|
+
'--config',
|
|
14
|
+
args.configPath,
|
|
15
|
+
], args.projectDir, args.onLog, args.signal);
|
|
16
|
+
if (code !== 0)
|
|
17
|
+
throw new Error(`wrangler bundle failed (exit ${code}): ${out.slice(-400)}`);
|
|
18
|
+
const javascript = readdirSync(args.outDir).filter((file) => file.endsWith('.js'));
|
|
19
|
+
if (javascript.length === 0) {
|
|
20
|
+
throw new Error(`wrangler bundle produced no .js in ${args.outDir}`);
|
|
21
|
+
}
|
|
22
|
+
const preferred = javascript.find((file) => file.startsWith('worker')) ?? javascript[0];
|
|
23
|
+
return Object.freeze({
|
|
24
|
+
bundlePath: join(args.outDir, preferred),
|
|
25
|
+
metafilePath: join(args.outDir, 'bundle-meta.json'),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function wranglerCommand(projectDir: string, runtime?: {
|
|
2
|
+
readonly executable: string;
|
|
3
|
+
readonly bun: boolean;
|
|
4
|
+
}): {
|
|
5
|
+
cmd: string;
|
|
6
|
+
prefix: string[];
|
|
7
|
+
};
|
|
8
|
+
/** Keep Wrangler CLI diagnostic logs inside the generated project state. */
|
|
9
|
+
export declare function wranglerProcessEnv(projectDir: string): NodeJS.ProcessEnv;
|
|
10
|
+
export declare function runCapture(cmd: string, args: string[], cwd: string, onLog?: (line: string) => void, signal?: AbortSignal, redact?: (input: string) => string): Promise<{
|
|
11
|
+
code: number;
|
|
12
|
+
out: string;
|
|
13
|
+
stdout: string;
|
|
14
|
+
stderr: string;
|
|
15
|
+
}>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
6
|
+
export function wranglerCommand(projectDir, runtime = {
|
|
7
|
+
executable: process.execPath,
|
|
8
|
+
bun: process.versions.bun !== undefined,
|
|
9
|
+
}) {
|
|
10
|
+
const local = join(projectDir, 'node_modules', '.bin', 'wrangler');
|
|
11
|
+
if (existsSync(local))
|
|
12
|
+
return { cmd: local, prefix: [] };
|
|
13
|
+
const require = createRequire(import.meta.url);
|
|
14
|
+
const packagePath = require.resolve('wrangler/package.json');
|
|
15
|
+
const manifest = JSON.parse(readFileSync(packagePath, 'utf8'));
|
|
16
|
+
const executable = typeof manifest.bin === 'string' ? manifest.bin : manifest.bin?.wrangler;
|
|
17
|
+
if (!executable)
|
|
18
|
+
throw new TypeError('Installed Wrangler package has no wrangler executable.');
|
|
19
|
+
return {
|
|
20
|
+
cmd: runtime.bun ? 'node' : runtime.executable,
|
|
21
|
+
prefix: [resolve(dirname(packagePath), executable)],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Keep Wrangler CLI diagnostic logs inside the generated project state. */
|
|
25
|
+
export function wranglerProcessEnv(projectDir) {
|
|
26
|
+
return {
|
|
27
|
+
...process.env,
|
|
28
|
+
WRANGLER_SEND_METRICS: 'false',
|
|
29
|
+
WRANGLER_LOG_PATH: process.env.WRANGLER_LOG_PATH || resolve(projectDir, '.astrale', 'logs', 'wrangler'),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function runCapture(cmd, args, cwd, onLog, signal, redact = (input) => input) {
|
|
33
|
+
if (signal?.aborted)
|
|
34
|
+
return Promise.reject(new Error('Wrangler process cancelled.'));
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
const child = spawn(cmd, args, {
|
|
37
|
+
cwd,
|
|
38
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
39
|
+
env: wranglerProcessEnv(cwd),
|
|
40
|
+
});
|
|
41
|
+
let out = '';
|
|
42
|
+
let stdout = '';
|
|
43
|
+
let stderr = '';
|
|
44
|
+
let outputBytes = 0;
|
|
45
|
+
// One decoder per stream: a chunk can end inside a multi-byte UTF-8 character.
|
|
46
|
+
const decoders = { stdout: new StringDecoder('utf8'), stderr: new StringDecoder('utf8') };
|
|
47
|
+
let terminating;
|
|
48
|
+
const timeout = setTimeout(() => terminate(new Error('Wrangler process timed out.')), 300_000);
|
|
49
|
+
const abort = () => terminate(new Error('Wrangler process cancelled.'));
|
|
50
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
51
|
+
let escalation;
|
|
52
|
+
const cleanup = () => {
|
|
53
|
+
clearTimeout(timeout);
|
|
54
|
+
if (escalation !== undefined)
|
|
55
|
+
clearTimeout(escalation);
|
|
56
|
+
signal?.removeEventListener('abort', abort);
|
|
57
|
+
};
|
|
58
|
+
function terminate(cause) {
|
|
59
|
+
if (terminating !== undefined)
|
|
60
|
+
return;
|
|
61
|
+
terminating = cause;
|
|
62
|
+
child.kill('SIGTERM');
|
|
63
|
+
escalation = setTimeout(() => {
|
|
64
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
65
|
+
child.kill('SIGKILL');
|
|
66
|
+
}, 1_000);
|
|
67
|
+
}
|
|
68
|
+
const onData = (buffer, stream) => {
|
|
69
|
+
outputBytes += buffer.byteLength;
|
|
70
|
+
if (outputBytes > 1024 * 1024) {
|
|
71
|
+
terminate(new Error('Wrangler process exceeded its output limit.'));
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const chunk = decoders[stream].write(buffer);
|
|
75
|
+
out += chunk;
|
|
76
|
+
if (stream === 'stdout')
|
|
77
|
+
stdout += chunk;
|
|
78
|
+
else
|
|
79
|
+
stderr += chunk;
|
|
80
|
+
};
|
|
81
|
+
child.stdout?.on('data', (buffer) => onData(buffer, 'stdout'));
|
|
82
|
+
child.stderr?.on('data', (buffer) => onData(buffer, 'stderr'));
|
|
83
|
+
child.on('exit', (code) => {
|
|
84
|
+
cleanup();
|
|
85
|
+
if (terminating !== undefined)
|
|
86
|
+
reject(terminating);
|
|
87
|
+
else {
|
|
88
|
+
for (const stream of ['stdout', 'stderr']) {
|
|
89
|
+
const rest = decoders[stream].end();
|
|
90
|
+
out += rest;
|
|
91
|
+
if (stream === 'stdout')
|
|
92
|
+
stdout += rest;
|
|
93
|
+
else
|
|
94
|
+
stderr += rest;
|
|
95
|
+
}
|
|
96
|
+
const safe = redact(out);
|
|
97
|
+
for (const line of safe.split('\n'))
|
|
98
|
+
if (line.trim())
|
|
99
|
+
onLog?.(line);
|
|
100
|
+
resolve({
|
|
101
|
+
code: code ?? 1,
|
|
102
|
+
out: safe,
|
|
103
|
+
stdout: redact(stdout),
|
|
104
|
+
stderr: redact(stderr),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
child.on('error', (cause) => {
|
|
109
|
+
cleanup();
|
|
110
|
+
reject(terminating ?? cause);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
}
|