@hyperframes/gcp-cloud-run 0.6.79
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/Dockerfile +118 -0
- package/README.md +128 -0
- package/dist/chromium.d.ts +39 -0
- package/dist/chromium.d.ts.map +1 -0
- package/dist/events.d.ts +130 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/formatExtension.d.ts +11 -0
- package/dist/formatExtension.d.ts.map +1 -0
- package/dist/gcsTransport.d.ts +53 -0
- package/dist/gcsTransport.d.ts.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +855 -0
- package/dist/index.js.map +7 -0
- package/dist/sdk/costAccounting.d.ts +67 -0
- package/dist/sdk/costAccounting.d.ts.map +1 -0
- package/dist/sdk/deploySite.d.ts +54 -0
- package/dist/sdk/deploySite.d.ts.map +1 -0
- package/dist/sdk/getRenderProgress.d.ts +91 -0
- package/dist/sdk/getRenderProgress.d.ts.map +1 -0
- package/dist/sdk/index.d.ts +17 -0
- package/dist/sdk/index.d.ts.map +1 -0
- package/dist/sdk/index.js +390 -0
- package/dist/sdk/index.js.map +7 -0
- package/dist/sdk/renderToCloudRun.d.ts +97 -0
- package/dist/sdk/renderToCloudRun.d.ts.map +1 -0
- package/dist/sdk/validateConfig.d.ts +36 -0
- package/dist/sdk/validateConfig.d.ts.map +1 -0
- package/dist/server.d.ts +58 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +517 -0
- package/dist/server.js.map +7 -0
- package/package.json +62 -0
- package/terraform/main.tf +197 -0
- package/terraform/outputs.tf +34 -0
- package/terraform/providers.tf +12 -0
- package/terraform/variables.tf +75 -0
- package/terraform/versions.tf +9 -0
- package/terraform/workflow.yaml +179 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// src/sdk/deploySite.ts
|
|
2
|
+
import { mkdtempSync, rmSync as rmSync2, statSync as statSync2 } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { Storage } from "@google-cloud/storage";
|
|
6
|
+
import { hashProjectDir } from "@hyperframes/producer/distributed";
|
|
7
|
+
|
|
8
|
+
// src/gcsTransport.ts
|
|
9
|
+
import { createWriteStream, existsSync, mkdirSync, rmSync, statSync } from "node:fs";
|
|
10
|
+
import { dirname } from "node:path";
|
|
11
|
+
import * as tar from "tar";
|
|
12
|
+
function parseGcsUri(uri) {
|
|
13
|
+
if (!uri.startsWith("gs://")) {
|
|
14
|
+
throw new Error(`[gcsTransport] expected gs:// URI, got: ${JSON.stringify(uri)}`);
|
|
15
|
+
}
|
|
16
|
+
const rest = uri.slice("gs://".length);
|
|
17
|
+
const slash = rest.indexOf("/");
|
|
18
|
+
if (slash === -1) {
|
|
19
|
+
throw new Error(`[gcsTransport] missing key in gs URI: ${JSON.stringify(uri)}`);
|
|
20
|
+
}
|
|
21
|
+
const bucket = rest.slice(0, slash);
|
|
22
|
+
const key = rest.slice(slash + 1);
|
|
23
|
+
if (!bucket || !key) {
|
|
24
|
+
throw new Error(`[gcsTransport] empty bucket or key in gs URI: ${JSON.stringify(uri)}`);
|
|
25
|
+
}
|
|
26
|
+
return { bucket, key };
|
|
27
|
+
}
|
|
28
|
+
function formatGcsUri(loc) {
|
|
29
|
+
return `gs://${loc.bucket}/${loc.key}`;
|
|
30
|
+
}
|
|
31
|
+
async function uploadFileToGcs(storage, localPath, uri, contentType) {
|
|
32
|
+
if (!existsSync(localPath)) {
|
|
33
|
+
throw new Error(`[gcsTransport] upload source missing: ${localPath}`);
|
|
34
|
+
}
|
|
35
|
+
const { bucket, key } = parseGcsUri(uri);
|
|
36
|
+
await storage.bucket(bucket).upload(localPath, {
|
|
37
|
+
destination: key,
|
|
38
|
+
// `resumable: false` (simple upload) is faster for the small-to-medium
|
|
39
|
+
// objects this adapter moves and avoids the extra round-trip a resumable
|
|
40
|
+
// session start costs; GCS recommends resumable only past ~8 MB but our
|
|
41
|
+
// chunks are reliably above that, so let the client pick by default.
|
|
42
|
+
contentType
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
async function tarDirectory(sourceDir, destTarball) {
|
|
46
|
+
if (!existsSync(sourceDir) || !statSync(sourceDir).isDirectory()) {
|
|
47
|
+
throw new Error(`[gcsTransport] tar source must be an existing directory: ${sourceDir}`);
|
|
48
|
+
}
|
|
49
|
+
mkdirSync(dirname(destTarball), { recursive: true });
|
|
50
|
+
await tar.create({ gzip: true, file: destTarball, cwd: sourceDir }, ["."]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/sdk/deploySite.ts
|
|
54
|
+
async function deploySite(opts) {
|
|
55
|
+
if (!statSync2(opts.projectDir).isDirectory()) {
|
|
56
|
+
throw new Error(`[deploySite] projectDir is not a directory: ${opts.projectDir}`);
|
|
57
|
+
}
|
|
58
|
+
const siteId = opts.siteId ?? hashProjectDir(opts.projectDir);
|
|
59
|
+
const key = `sites/${siteId}/project.tar.gz`;
|
|
60
|
+
const projectGcsUri = formatGcsUri({ bucket: opts.bucketName, key });
|
|
61
|
+
const storage = opts.storage ?? new Storage();
|
|
62
|
+
const file = storage.bucket(opts.bucketName).file(key);
|
|
63
|
+
const existing = await headObject(file);
|
|
64
|
+
if (existing) {
|
|
65
|
+
return {
|
|
66
|
+
siteId,
|
|
67
|
+
bucketName: opts.bucketName,
|
|
68
|
+
projectGcsUri,
|
|
69
|
+
bytes: existing.bytes,
|
|
70
|
+
uploadedAt: existing.lastModified,
|
|
71
|
+
uploaded: false
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const workdir = mkdtempSync(join(tmpdir(), "hf-deploy-site-"));
|
|
75
|
+
try {
|
|
76
|
+
const tarball = join(workdir, "project.tar.gz");
|
|
77
|
+
await tarDirectory(opts.projectDir, tarball);
|
|
78
|
+
const size = statSync2(tarball).size;
|
|
79
|
+
await uploadFileToGcs(storage, tarball, projectGcsUri, "application/gzip");
|
|
80
|
+
return {
|
|
81
|
+
siteId,
|
|
82
|
+
bucketName: opts.bucketName,
|
|
83
|
+
projectGcsUri,
|
|
84
|
+
bytes: size,
|
|
85
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
86
|
+
uploaded: true
|
|
87
|
+
};
|
|
88
|
+
} finally {
|
|
89
|
+
rmSync2(workdir, { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async function headObject(file) {
|
|
93
|
+
const [exists] = await file.exists();
|
|
94
|
+
if (!exists) return null;
|
|
95
|
+
const [meta] = await file.getMetadata();
|
|
96
|
+
const sizeRaw = meta.size;
|
|
97
|
+
const bytes = typeof sizeRaw === "string" ? Number(sizeRaw) : typeof sizeRaw === "number" ? sizeRaw : 0;
|
|
98
|
+
return {
|
|
99
|
+
bytes: Number.isFinite(bytes) ? bytes : 0,
|
|
100
|
+
lastModified: meta.updated ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/sdk/renderToCloudRun.ts
|
|
105
|
+
import { randomUUID } from "node:crypto";
|
|
106
|
+
|
|
107
|
+
// src/formatExtension.ts
|
|
108
|
+
var FORMAT_EXTENSIONS = {
|
|
109
|
+
mp4: ".mp4",
|
|
110
|
+
mov: ".mov",
|
|
111
|
+
webm: ".webm",
|
|
112
|
+
"png-sequence": ""
|
|
113
|
+
};
|
|
114
|
+
function formatExtension(format) {
|
|
115
|
+
return FORMAT_EXTENSIONS[format];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// src/sdk/validateConfig.ts
|
|
119
|
+
import { InvalidConfigError } from "@hyperframes/producer/distributed";
|
|
120
|
+
import {
|
|
121
|
+
InvalidConfigError as InvalidConfigError2,
|
|
122
|
+
validateDistributedRenderConfig,
|
|
123
|
+
validateVariablesPayload
|
|
124
|
+
} from "@hyperframes/producer/distributed";
|
|
125
|
+
var MAX_WORKFLOWS_INPUT_BYTES = 512 * 1024;
|
|
126
|
+
var LARGE_VARIABLES_DOCS_URL = "https://hyperframes.heygen.com/deploy/templates-on-lambda#working-with-large-variables";
|
|
127
|
+
function validateWorkflowsInputSize(input) {
|
|
128
|
+
let serialized;
|
|
129
|
+
try {
|
|
130
|
+
serialized = JSON.stringify(input);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
throw new InvalidConfigError(
|
|
133
|
+
"config",
|
|
134
|
+
`Cloud Workflows execution argument is not JSON-serializable: ${err instanceof Error ? err.message : String(err)}`
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
if (serialized === void 0) {
|
|
138
|
+
throw new InvalidConfigError(
|
|
139
|
+
"config",
|
|
140
|
+
"Cloud Workflows execution argument is not JSON-serializable (JSON.stringify returned undefined). Check that all fields, including config.variables, are plain JSON values."
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
const byteLength = Buffer.byteLength(serialized, "utf8");
|
|
144
|
+
if (byteLength > MAX_WORKFLOWS_INPUT_BYTES) {
|
|
145
|
+
throw new InvalidConfigError(
|
|
146
|
+
"config",
|
|
147
|
+
`Cloud Workflows execution argument is ${byteLength} bytes, which exceeds the ${MAX_WORKFLOWS_INPUT_BYTES}-byte (512 KiB) limit. Variables are for typed data (strings, numbers, structured records); media assets (images, audio, video) should be passed as URL references the composition resolves at render time, not inlined as base64. See ${LARGE_VARIABLES_DOCS_URL} for the URL-your-assets convention.`
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/sdk/renderToCloudRun.ts
|
|
153
|
+
async function renderToCloudRun(opts) {
|
|
154
|
+
validateDistributedRenderConfig(opts.config);
|
|
155
|
+
if (!opts.bucketName) throw new Error("[renderToCloudRun] bucketName is required");
|
|
156
|
+
if (!opts.projectId) throw new Error("[renderToCloudRun] projectId is required");
|
|
157
|
+
if (!opts.location) throw new Error("[renderToCloudRun] location is required");
|
|
158
|
+
if (!opts.workflowId) throw new Error("[renderToCloudRun] workflowId is required");
|
|
159
|
+
if (!opts.serviceUrl) throw new Error("[renderToCloudRun] serviceUrl is required");
|
|
160
|
+
if (!opts.siteHandle && !opts.projectDir) {
|
|
161
|
+
throw new Error("[renderToCloudRun] either siteHandle or projectDir must be supplied");
|
|
162
|
+
}
|
|
163
|
+
const renderId = opts.renderId ?? `hf-render-${randomUUID()}`;
|
|
164
|
+
if (!/^[A-Za-z0-9._-]+$/.test(renderId) || renderId.includes("..")) {
|
|
165
|
+
throw new Error(
|
|
166
|
+
`[renderToCloudRun] renderId must match [A-Za-z0-9._-]+ and not contain "..": ${JSON.stringify(renderId)}`
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const ext = formatExtension(opts.config.format);
|
|
170
|
+
const outputKey = opts.outputKey ?? `renders/${renderId}/output${ext}`;
|
|
171
|
+
const planOutputGcsPrefix = formatGcsUri({
|
|
172
|
+
bucket: opts.bucketName,
|
|
173
|
+
key: `renders/${renderId}/`
|
|
174
|
+
});
|
|
175
|
+
const outputGcsUri = formatGcsUri({ bucket: opts.bucketName, key: outputKey });
|
|
176
|
+
const site = opts.siteHandle ?? await deploySite({
|
|
177
|
+
projectDir: opts.projectDir,
|
|
178
|
+
bucketName: opts.bucketName,
|
|
179
|
+
storage: opts.storage
|
|
180
|
+
});
|
|
181
|
+
const argument = {
|
|
182
|
+
RenderId: renderId,
|
|
183
|
+
ProjectGcsUri: site.projectGcsUri,
|
|
184
|
+
PlanOutputGcsPrefix: planOutputGcsPrefix,
|
|
185
|
+
OutputGcsUri: outputGcsUri,
|
|
186
|
+
ServiceUrl: opts.serviceUrl,
|
|
187
|
+
Config: opts.config
|
|
188
|
+
};
|
|
189
|
+
validateWorkflowsInputSize(argument);
|
|
190
|
+
const executions = opts.executions ?? await defaultExecutionsClient();
|
|
191
|
+
const parent = executions.workflowPath(opts.projectId, opts.location, opts.workflowId);
|
|
192
|
+
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
193
|
+
const [execution] = await executions.createExecution({
|
|
194
|
+
parent,
|
|
195
|
+
execution: { argument: JSON.stringify(argument) }
|
|
196
|
+
});
|
|
197
|
+
if (!execution.name) {
|
|
198
|
+
throw new Error("[renderToCloudRun] CreateExecution returned no execution name");
|
|
199
|
+
}
|
|
200
|
+
return {
|
|
201
|
+
renderId,
|
|
202
|
+
executionName: execution.name,
|
|
203
|
+
bucketName: opts.bucketName,
|
|
204
|
+
workflowId: opts.workflowId,
|
|
205
|
+
outputGcsUri,
|
|
206
|
+
projectGcsUri: site.projectGcsUri,
|
|
207
|
+
startedAt
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
async function defaultExecutionsClient() {
|
|
211
|
+
const mod = await import("@google-cloud/workflows");
|
|
212
|
+
const client = new mod.ExecutionsClient();
|
|
213
|
+
return client;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/sdk/costAccounting.ts
|
|
217
|
+
var CLOUD_RUN_USD_PER_VCPU_SECOND = 24e-6;
|
|
218
|
+
var CLOUD_RUN_USD_PER_GIB_SECOND = 25e-7;
|
|
219
|
+
var CLOUD_RUN_USD_PER_REQUEST = 4e-7;
|
|
220
|
+
var WORKFLOWS_USD_PER_STEP = 1e-5;
|
|
221
|
+
function computeRenderCost(invocations, workflowSteps) {
|
|
222
|
+
let cloudRunUsd = 0;
|
|
223
|
+
let anyEstimated = false;
|
|
224
|
+
for (const inv of invocations) {
|
|
225
|
+
const seconds = inv.durationMs / 1e3;
|
|
226
|
+
cloudRunUsd += seconds * inv.vcpu * CLOUD_RUN_USD_PER_VCPU_SECOND;
|
|
227
|
+
cloudRunUsd += seconds * inv.memoryGib * CLOUD_RUN_USD_PER_GIB_SECOND;
|
|
228
|
+
cloudRunUsd += CLOUD_RUN_USD_PER_REQUEST;
|
|
229
|
+
if (inv.estimated) anyEstimated = true;
|
|
230
|
+
}
|
|
231
|
+
const workflowsUsd = workflowSteps * WORKFLOWS_USD_PER_STEP;
|
|
232
|
+
const accruedSoFarUsd = roundUsd(cloudRunUsd + workflowsUsd);
|
|
233
|
+
return {
|
|
234
|
+
accruedSoFarUsd,
|
|
235
|
+
displayCost: formatUsd(accruedSoFarUsd),
|
|
236
|
+
breakdown: {
|
|
237
|
+
cloudRunUsd: roundUsd(cloudRunUsd),
|
|
238
|
+
workflowsUsd: roundUsd(workflowsUsd),
|
|
239
|
+
gcsEstimate: "not-included",
|
|
240
|
+
estimated: anyEstimated
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function roundUsd(usd) {
|
|
245
|
+
return Math.round(usd * 1e4) / 1e4;
|
|
246
|
+
}
|
|
247
|
+
function formatUsd(usd) {
|
|
248
|
+
return `$${usd.toFixed(4)}`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/sdk/getRenderProgress.ts
|
|
252
|
+
var DEFAULT_VCPU = 4;
|
|
253
|
+
var DEFAULT_MEMORY_GIB = 16;
|
|
254
|
+
async function getRenderProgress(opts) {
|
|
255
|
+
if (!opts.executionName) {
|
|
256
|
+
throw new Error("[getRenderProgress] executionName is required");
|
|
257
|
+
}
|
|
258
|
+
const executions = opts.executions ?? await defaultExecutionsClient2();
|
|
259
|
+
const vcpu = opts.vcpu ?? DEFAULT_VCPU;
|
|
260
|
+
const memoryGib = opts.memoryGib ?? DEFAULT_MEMORY_GIB;
|
|
261
|
+
const [execution] = await executions.getExecution({ name: opts.executionName });
|
|
262
|
+
const status = mapState(execution.state);
|
|
263
|
+
const startedAt = toIso(execution.startTime) ?? (/* @__PURE__ */ new Date(0)).toISOString();
|
|
264
|
+
const endedAt = toIso(execution.endTime);
|
|
265
|
+
const errors = [];
|
|
266
|
+
if (execution.error) {
|
|
267
|
+
errors.push({
|
|
268
|
+
state: execution.error.context ?? "<execution>",
|
|
269
|
+
error: extractErrorName(execution.error.payload) ?? "ExecutionError",
|
|
270
|
+
cause: execution.error.payload ?? ""
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
if (status !== "succeeded") {
|
|
274
|
+
return {
|
|
275
|
+
status,
|
|
276
|
+
overallProgress: 0,
|
|
277
|
+
framesRendered: 0,
|
|
278
|
+
totalFrames: null,
|
|
279
|
+
invocationsObserved: 0,
|
|
280
|
+
costs: computeRenderCost([], 0),
|
|
281
|
+
outputFile: null,
|
|
282
|
+
errors,
|
|
283
|
+
fatalErrorEncountered: status === "failed" || status === "cancelled",
|
|
284
|
+
startedAt,
|
|
285
|
+
endedAt
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const acc = parseAccumulated(execution.result);
|
|
289
|
+
const chunks = acc.Chunks?.filter((c) => c != null) ?? [];
|
|
290
|
+
const framesRendered = chunks.reduce((sum, c) => sum + (c.FramesEncoded ?? 0), 0);
|
|
291
|
+
const totalFrames = typeof acc.Plan?.TotalFrames === "number" ? acc.Plan.TotalFrames : null;
|
|
292
|
+
const invocations = [];
|
|
293
|
+
const pushInv = (durationMs) => {
|
|
294
|
+
invocations.push({
|
|
295
|
+
durationMs: typeof durationMs === "number" ? durationMs : 0,
|
|
296
|
+
vcpu,
|
|
297
|
+
memoryGib,
|
|
298
|
+
estimated: typeof durationMs !== "number"
|
|
299
|
+
});
|
|
300
|
+
};
|
|
301
|
+
if (acc.Plan) pushInv(acc.Plan.DurationMs);
|
|
302
|
+
for (const c of chunks) pushInv(c.DurationMs);
|
|
303
|
+
if (acc.Assemble) pushInv(acc.Assemble.DurationMs);
|
|
304
|
+
const workflowSteps = invocations.length + 4;
|
|
305
|
+
const costs = computeRenderCost(invocations, workflowSteps);
|
|
306
|
+
const outputGcsUri = acc.Assemble?.OutputGcsUri;
|
|
307
|
+
const outputFile = outputGcsUri ? {
|
|
308
|
+
gcsUri: outputGcsUri,
|
|
309
|
+
bytes: typeof acc.Assemble?.FileSize === "number" ? acc.Assemble.FileSize : null
|
|
310
|
+
} : null;
|
|
311
|
+
return {
|
|
312
|
+
status,
|
|
313
|
+
overallProgress: 1,
|
|
314
|
+
framesRendered,
|
|
315
|
+
totalFrames,
|
|
316
|
+
invocationsObserved: invocations.length,
|
|
317
|
+
costs,
|
|
318
|
+
outputFile,
|
|
319
|
+
errors,
|
|
320
|
+
fatalErrorEncountered: false,
|
|
321
|
+
startedAt,
|
|
322
|
+
endedAt
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
function mapState(state) {
|
|
326
|
+
switch (state) {
|
|
327
|
+
case "ACTIVE":
|
|
328
|
+
case "QUEUED":
|
|
329
|
+
return "running";
|
|
330
|
+
case "SUCCEEDED":
|
|
331
|
+
return "succeeded";
|
|
332
|
+
case "FAILED":
|
|
333
|
+
case "UNAVAILABLE":
|
|
334
|
+
return "failed";
|
|
335
|
+
case "CANCELLED":
|
|
336
|
+
return "cancelled";
|
|
337
|
+
default:
|
|
338
|
+
return "unknown";
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function parseAccumulated(result) {
|
|
342
|
+
if (!result) return {};
|
|
343
|
+
try {
|
|
344
|
+
const parsed = JSON.parse(result);
|
|
345
|
+
if (parsed && typeof parsed === "object") return parsed;
|
|
346
|
+
} catch {
|
|
347
|
+
}
|
|
348
|
+
return {};
|
|
349
|
+
}
|
|
350
|
+
function extractErrorName(payload) {
|
|
351
|
+
if (!payload) return void 0;
|
|
352
|
+
try {
|
|
353
|
+
const outer = JSON.parse(payload);
|
|
354
|
+
if (typeof outer.error === "string") return outer.error;
|
|
355
|
+
if (typeof outer.body === "string") {
|
|
356
|
+
const inner = JSON.parse(outer.body);
|
|
357
|
+
if (typeof inner.error === "string") return inner.error;
|
|
358
|
+
} else if (outer.body && typeof outer.body === "object") {
|
|
359
|
+
const inner = outer.body;
|
|
360
|
+
if (typeof inner.error === "string") return inner.error;
|
|
361
|
+
}
|
|
362
|
+
} catch {
|
|
363
|
+
}
|
|
364
|
+
return void 0;
|
|
365
|
+
}
|
|
366
|
+
function toIso(ts) {
|
|
367
|
+
if (ts == null) return null;
|
|
368
|
+
if (typeof ts === "string") return ts;
|
|
369
|
+
const seconds = ts.seconds == null ? null : Number(ts.seconds);
|
|
370
|
+
if (seconds == null || !Number.isFinite(seconds)) return null;
|
|
371
|
+
const ms = seconds * 1e3 + (ts.nanos ?? 0) / 1e6;
|
|
372
|
+
return new Date(ms).toISOString();
|
|
373
|
+
}
|
|
374
|
+
async function defaultExecutionsClient2() {
|
|
375
|
+
const mod = await import("@google-cloud/workflows");
|
|
376
|
+
const client = new mod.ExecutionsClient();
|
|
377
|
+
return client;
|
|
378
|
+
}
|
|
379
|
+
export {
|
|
380
|
+
InvalidConfigError2 as InvalidConfigError,
|
|
381
|
+
MAX_WORKFLOWS_INPUT_BYTES,
|
|
382
|
+
computeRenderCost,
|
|
383
|
+
deploySite,
|
|
384
|
+
getRenderProgress,
|
|
385
|
+
renderToCloudRun,
|
|
386
|
+
validateDistributedRenderConfig,
|
|
387
|
+
validateVariablesPayload,
|
|
388
|
+
validateWorkflowsInputSize
|
|
389
|
+
};
|
|
390
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/sdk/deploySite.ts", "../../src/gcsTransport.ts", "../../src/sdk/renderToCloudRun.ts", "../../src/formatExtension.ts", "../../src/sdk/validateConfig.ts", "../../src/sdk/costAccounting.ts", "../../src/sdk/getRenderProgress.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * `deploySite` \u2014 upload a project directory to GCS once per content hash\n * and return a reusable handle.\n *\n * `renderToCloudRun` calls this implicitly when no `siteHandle` is passed,\n * but exposing it as a standalone verb lets adopters bundle a project ahead\n * of time and reuse the handle across many renders without re-tarring the\n * project tree on every call.\n *\n * The handle is **content-addressed**: `siteId` is derived from a SHA-256\n * over the project files. Two `deploySite` calls on an unchanged tree\n * produce the same `siteId` and short-circuit the upload after a single\n * existence check.\n */\n\nimport { mkdtempSync, rmSync, statSync } from \"node:fs\";\nimport { tmpdir } from \"node:os\";\nimport { join } from \"node:path\";\nimport { Storage } from \"@google-cloud/storage\";\nimport { hashProjectDir } from \"@hyperframes/producer/distributed\";\nimport { formatGcsUri, tarDirectory, uploadFileToGcs } from \"../gcsTransport.js\";\n\n/** Options for {@link deploySite}. */\nexport interface DeploySiteOptions {\n /** Local project directory containing `index.html` (and any composition assets). */\n projectDir: string;\n /** GCS bucket the Terraform module provisioned. */\n bucketName: string;\n /**\n * Override the content-addressed site id. Useful when the caller has a\n * stable external identifier they want to use (e.g. a git SHA); if unset,\n * the hash of the project tree picks it.\n */\n siteId?: string;\n /** Injection seam for tests. Production callers leave unset. */\n storage?: Storage;\n}\n\n/** Stable handle returned by {@link deploySite}. Pass back to {@link renderToCloudRun}. */\nexport interface SiteHandle {\n /** Content-addressed (or caller-supplied) identifier; stable across re-uploads of the same tree. */\n siteId: string;\n /** Bucket the site landed in. Surfaced separately so callers don't have to re-parse `projectGcsUri`. */\n bucketName: string;\n /** Full `gs://bucket/sites/<siteId>/project.tar.gz` URI; pass through to `renderToCloudRun`. */\n projectGcsUri: string;\n /** Tarball size in bytes; useful for \"did we actually skip the upload?\" assertions. */\n bytes: number;\n /** ISO timestamp of the most recent upload OR the existing object the short-circuit found. */\n uploadedAt: string;\n /** `false` if the object already existed and we skipped the upload. */\n uploaded: boolean;\n}\n\n/**\n * Upload `projectDir` to `gs://bucketName/sites/<siteId>/project.tar.gz`.\n *\n * Short-circuits when an object with the same key already exists in the\n * bucket \u2014 `siteId` derives from the project's content hash, so the same\n * bytes produce the same key, and re-uploading would be redundant.\n */\n// fallow-ignore-next-line complexity\nexport async function deploySite(opts: DeploySiteOptions): Promise<SiteHandle> {\n if (!statSync(opts.projectDir).isDirectory()) {\n throw new Error(`[deploySite] projectDir is not a directory: ${opts.projectDir}`);\n }\n\n const siteId = opts.siteId ?? hashProjectDir(opts.projectDir);\n const key = `sites/${siteId}/project.tar.gz`;\n const projectGcsUri = formatGcsUri({ bucket: opts.bucketName, key });\n const storage = opts.storage ?? new Storage();\n const file = storage.bucket(opts.bucketName).file(key);\n\n // Existence short-circuit. Adopters re-rendering the same project on a\n // tight inner loop (CI smoke, demo flows) save the tar+gzip+upload pass\n // on every iteration.\n const existing = await headObject(file);\n if (existing) {\n return {\n siteId,\n bucketName: opts.bucketName,\n projectGcsUri,\n bytes: existing.bytes,\n uploadedAt: existing.lastModified,\n uploaded: false,\n };\n }\n\n const workdir = mkdtempSync(join(tmpdir(), \"hf-deploy-site-\"));\n try {\n const tarball = join(workdir, \"project.tar.gz\");\n await tarDirectory(opts.projectDir, tarball);\n const size = statSync(tarball).size;\n await uploadFileToGcs(storage, tarball, projectGcsUri, \"application/gzip\");\n return {\n siteId,\n bucketName: opts.bucketName,\n projectGcsUri,\n bytes: size,\n uploadedAt: new Date().toISOString(),\n uploaded: true,\n };\n } finally {\n rmSync(workdir, { recursive: true, force: true });\n }\n}\n\n/**\n * Narrow surface of the `@google-cloud/storage` `File` this module uses \u2014\n * lets the test double implement just `exists()` + `getMetadata()` without\n * pulling the full client type.\n */\ninterface FileLike {\n exists(): Promise<[boolean, ...unknown[]]>;\n getMetadata(): Promise<[{ size?: string | number; updated?: string }, ...unknown[]]>;\n}\n\n// fallow-ignore-next-line complexity\nasync function headObject(file: FileLike): Promise<{ bytes: number; lastModified: string } | null> {\n const [exists] = await file.exists();\n if (!exists) return null;\n const [meta] = await file.getMetadata();\n const sizeRaw = meta.size;\n const bytes =\n typeof sizeRaw === \"string\" ? Number(sizeRaw) : typeof sizeRaw === \"number\" ? sizeRaw : 0;\n return {\n bytes: Number.isFinite(bytes) ? bytes : 0,\n lastModified: meta.updated ?? new Date().toISOString(),\n };\n}\n", "/**\n * Thin GCS transport for the Cloud Run handler.\n *\n * The OSS distributed primitives are pure functions over local file paths;\n * the handler bridges GCS \u2194 the container's writable `/tmp` filesystem on\n * each request. Functions here are intentionally narrow: parse a URI,\n * download an object to a local path, upload a path, tar-pack a planDir,\n * tar-extract a planDir back out.\n *\n * Tar (not zip) for planDir transit:\n * - planDirs contain symlinks (the extract stage materializes them but\n * the compiled/ subtree may include linked assets); tar preserves them,\n * zip does not.\n * - We use the `tar` npm package (pure JS over `node:zlib`) so the\n * archive format doesn't depend on a system `tar`/`unzip` being present\n * in the container image.\n *\n * Apart from the `gs://` scheme and the `@google-cloud/storage` client this\n * is the same shape as `@hyperframes/aws-lambda`'s `s3Transport.ts`.\n */\n\nimport { createWriteStream, existsSync, mkdirSync, rmSync, statSync } from \"node:fs\";\nimport { dirname } from \"node:path\";\nimport { pipeline } from \"node:stream/promises\";\nimport type { Storage } from \"@google-cloud/storage\";\nimport * as tar from \"tar\";\n\n/** Parsed `gs://bucket/key` URI. */\nexport interface GcsLocation {\n bucket: string;\n key: string;\n}\n\n/** Parse `gs://bucket/key/path` \u2192 `{ bucket, key }`. Throws on malformed input. */\n// fallow-ignore-next-line complexity\nexport function parseGcsUri(uri: string): GcsLocation {\n if (!uri.startsWith(\"gs://\")) {\n throw new Error(`[gcsTransport] expected gs:// URI, got: ${JSON.stringify(uri)}`);\n }\n const rest = uri.slice(\"gs://\".length);\n const slash = rest.indexOf(\"/\");\n if (slash === -1) {\n throw new Error(`[gcsTransport] missing key in gs URI: ${JSON.stringify(uri)}`);\n }\n const bucket = rest.slice(0, slash);\n const key = rest.slice(slash + 1);\n if (!bucket || !key) {\n throw new Error(`[gcsTransport] empty bucket or key in gs URI: ${JSON.stringify(uri)}`);\n }\n return { bucket, key };\n}\n\n/** Build `gs://bucket/key` from a location. */\nexport function formatGcsUri(loc: GcsLocation): string {\n return `gs://${loc.bucket}/${loc.key}`;\n}\n\n/** Stream a GCS object to a local file path. */\nexport async function downloadGcsObjectToFile(\n storage: Storage,\n uri: string,\n destPath: string,\n): Promise<void> {\n const { bucket, key } = parseGcsUri(uri);\n mkdirSync(dirname(destPath), { recursive: true });\n const file = storage.bucket(bucket).file(key);\n // `createReadStream` streams the object body; piping into a write stream\n // keeps memory flat for large plan tarballs / chunk files rather than\n // buffering the whole object the way `file.download()` would.\n await pipeline(file.createReadStream(), createWriteStream(destPath));\n}\n\n/**\n * Upload a local file's contents to a GCS URI using a resumable upload.\n * GCS objects have no practical size ceiling for the artifacts this adapter\n * handles (plan tarballs \u2264 2 GB, chunks \u2264 a few hundred MB), so a single\n * upload call works for every case.\n */\nexport async function uploadFileToGcs(\n storage: Storage,\n localPath: string,\n uri: string,\n contentType?: string,\n): Promise<void> {\n if (!existsSync(localPath)) {\n throw new Error(`[gcsTransport] upload source missing: ${localPath}`);\n }\n const { bucket, key } = parseGcsUri(uri);\n await storage.bucket(bucket).upload(localPath, {\n destination: key,\n // `resumable: false` (simple upload) is faster for the small-to-medium\n // objects this adapter moves and avoids the extra round-trip a resumable\n // session start costs; GCS recommends resumable only past ~8 MB but our\n // chunks are reliably above that, so let the client pick by default.\n contentType,\n });\n}\n\n/**\n * Pack a directory into a `.tar.gz` at `destTarball`. Uses the `tar` npm\n * package (pure JS over `node:zlib`) rather than spawning a system tar\n * binary so the archive format is independent of the container's userland.\n */\nexport async function tarDirectory(sourceDir: string, destTarball: string): Promise<void> {\n if (!existsSync(sourceDir) || !statSync(sourceDir).isDirectory()) {\n throw new Error(`[gcsTransport] tar source must be an existing directory: ${sourceDir}`);\n }\n mkdirSync(dirname(destTarball), { recursive: true });\n await tar.create({ gzip: true, file: destTarball, cwd: sourceDir }, [\".\"]);\n}\n\n/**\n * Extract a `.tar.gz` produced by {@link tarDirectory} into `destDir`.\n * The directory is created (or cleared) before extraction so a retried\n * request doesn't observe stale files from a prior run on the same warm\n * container instance.\n */\nexport async function untarDirectory(tarballPath: string, destDir: string): Promise<void> {\n if (!existsSync(tarballPath)) {\n throw new Error(`[gcsTransport] tarball missing: ${tarballPath}`);\n }\n // Wipe target so a warm container instance's prior planDir doesn't bleed\n // into the new request. Cloud Run re-uses the instance filesystem across\n // requests served by the same instance.\n if (existsSync(destDir)) {\n rmSync(destDir, { recursive: true, force: true });\n }\n mkdirSync(destDir, { recursive: true });\n await tar.extract({ file: tarballPath, cwd: destDir });\n}\n", "/**\n * `renderToCloudRun` \u2014 start a distributed render against an already-deployed\n * Cloud Run service + Cloud Workflows definition and return a handle the\n * caller can poll with {@link getRenderProgress}.\n *\n * The function does *not* wait for the render to finish. Cloud Workflows\n * executions can run for hours; blocking the caller's process on the\n * execution is the wrong default. The returned `RenderHandle` carries\n * everything the progress / cost / download paths need.\n *\n * Wire order:\n * 1. Validate config (typed throw before any GCP call).\n * 2. `deploySite` if no `siteHandle` was provided.\n * 3. `CreateExecution` against the workflow with the argument shape the\n * `packages/gcp-cloud-run/terraform/workflow.yaml` definition expects.\n * 4. Return handle. The GCS `outputKey` is deterministic from the\n * client-generated `renderId` so the caller can predict the final\n * object URL before the (server-assigned) execution id exists.\n *\n * Unlike Step Functions, Cloud Workflows assigns the execution id\n * server-side, so we cannot use it as the GCS prefix. We mint a `renderId`\n * (uuid) client-side, use it for every GCS path, and pass it into the\n * workflow argument; the server-assigned execution resource name is tracked\n * separately for polling.\n */\n\nimport { randomUUID } from \"node:crypto\";\nimport type { Storage } from \"@google-cloud/storage\";\nimport type { SerializableDistributedRenderConfig } from \"../events.js\";\nimport { formatExtension } from \"../formatExtension.js\";\nimport { formatGcsUri } from \"../gcsTransport.js\";\nimport { deploySite, type SiteHandle } from \"./deploySite.js\";\nimport { validateDistributedRenderConfig, validateWorkflowsInputSize } from \"./validateConfig.js\";\n\n/**\n * Minimal surface of `@google-cloud/workflows`' `ExecutionsClient` that\n * this module needs. The real client satisfies this; tests inject a double.\n */\nexport interface ExecutionsClientLike {\n workflowPath(project: string, location: string, workflow: string): string;\n createExecution(req: {\n parent: string;\n execution: { argument: string };\n }): Promise<[{ name?: string | null; state?: string | null }, ...unknown[]]>;\n}\n\n/** Options for {@link renderToCloudRun}. */\nexport interface RenderToCloudRunOptions {\n /** Local project directory. Required when `siteHandle` is not supplied. */\n projectDir?: string;\n /** Re-use an existing `deploySite` upload (skips tar+GCS upload). */\n siteHandle?: SiteHandle;\n /** Validated `SerializableDistributedRenderConfig` (no logger / abortSignal). */\n config: SerializableDistributedRenderConfig;\n /** GCS bucket from the Terraform output (`render_bucket_name`). */\n bucketName: string;\n /** GCP project id hosting the workflow. */\n projectId: string;\n /** Workflow location, e.g. `us-central1`. */\n location: string;\n /** Workflow id from the Terraform output (`workflow_name`). */\n workflowId: string;\n /**\n * HTTPS URL of the deployed Cloud Run render service (Terraform output\n * `service_url`). The workflow POSTs every step (plan / renderChunk /\n * assemble) to this URL; passed as an execution argument so the workflow\n * definition stays free of hard-coded URLs.\n */\n serviceUrl: string;\n /**\n * Final output GCS key. Defaults to `renders/<renderId>/output.<ext>`\n * where `<ext>` is derived from `config.format`.\n */\n outputKey?: string;\n /**\n * Client-generated render id. Defaults to `hf-render-<uuid>`. Used as the\n * GCS key prefix and echoed into the workflow argument; not the same as\n * the server-assigned execution id.\n */\n renderId?: string;\n /** Test injection seam \u2014 production callers leave unset. */\n executions?: ExecutionsClientLike;\n /** Test injection seam \u2014 propagated to `deploySite` when applicable. */\n storage?: Storage;\n}\n\n/** Stable identifier + every URL/name the caller needs to follow the render. */\nexport interface RenderHandle {\n /** Client-generated render id; the GCS prefix everything lands under. */\n renderId: string;\n /** Server-assigned execution resource name; pass to {@link getRenderProgress}. */\n executionName: string;\n bucketName: string;\n workflowId: string;\n outputGcsUri: string;\n projectGcsUri: string;\n startedAt: string;\n}\n\n// fallow-ignore-next-line complexity\nexport async function renderToCloudRun(opts: RenderToCloudRunOptions): Promise<RenderHandle> {\n validateDistributedRenderConfig(opts.config);\n\n if (!opts.bucketName) throw new Error(\"[renderToCloudRun] bucketName is required\");\n if (!opts.projectId) throw new Error(\"[renderToCloudRun] projectId is required\");\n if (!opts.location) throw new Error(\"[renderToCloudRun] location is required\");\n if (!opts.workflowId) throw new Error(\"[renderToCloudRun] workflowId is required\");\n if (!opts.serviceUrl) throw new Error(\"[renderToCloudRun] serviceUrl is required\");\n if (!opts.siteHandle && !opts.projectDir) {\n throw new Error(\"[renderToCloudRun] either siteHandle or projectDir must be supplied\");\n }\n\n const renderId = opts.renderId ?? `hf-render-${randomUUID()}`;\n // `renderId` is interpolated directly into GCS object keys\n // (`renders/<renderId>/\u2026`). Reject anything that could escape that prefix\n // or build a malformed key \u2014 `..`, slashes, or other path metacharacters \u2014\n // so a caller-supplied id can't collide with or overwrite another render's\n // artifacts elsewhere in the bucket.\n if (!/^[A-Za-z0-9._-]+$/.test(renderId) || renderId.includes(\"..\")) {\n throw new Error(\n `[renderToCloudRun] renderId must match [A-Za-z0-9._-]+ and not contain \"..\": ${JSON.stringify(renderId)}`,\n );\n }\n const ext = formatExtension(opts.config.format);\n const outputKey = opts.outputKey ?? `renders/${renderId}/output${ext}`;\n const planOutputGcsPrefix = formatGcsUri({\n bucket: opts.bucketName,\n key: `renders/${renderId}/`,\n });\n const outputGcsUri = formatGcsUri({ bucket: opts.bucketName, key: outputKey });\n\n const site =\n opts.siteHandle ??\n (await deploySite({\n projectDir: opts.projectDir as string,\n bucketName: opts.bucketName,\n storage: opts.storage,\n }));\n\n const argument = {\n RenderId: renderId,\n ProjectGcsUri: site.projectGcsUri,\n PlanOutputGcsPrefix: planOutputGcsPrefix,\n OutputGcsUri: outputGcsUri,\n ServiceUrl: opts.serviceUrl,\n Config: opts.config,\n };\n\n // Reject oversize input client-side. Cloud Workflows caps the execution\n // argument at 512 KiB; without this check, input bloat (typically from\n // `config.variables` containing inlined media) surfaces as an opaque\n // server-side error after the execution starts, far from the caller's\n // stack frame.\n validateWorkflowsInputSize(argument);\n\n const executions = opts.executions ?? (await defaultExecutionsClient());\n const parent = executions.workflowPath(opts.projectId, opts.location, opts.workflowId);\n const startedAt = new Date().toISOString();\n const [execution] = await executions.createExecution({\n parent,\n execution: { argument: JSON.stringify(argument) },\n });\n\n if (!execution.name) {\n throw new Error(\"[renderToCloudRun] CreateExecution returned no execution name\");\n }\n\n return {\n renderId,\n executionName: execution.name,\n bucketName: opts.bucketName,\n workflowId: opts.workflowId,\n outputGcsUri,\n projectGcsUri: site.projectGcsUri,\n startedAt,\n };\n}\n\n/**\n * Lazily import the real `@google-cloud/workflows` ExecutionsClient. Dynamic\n * so SDK consumers that only call `validateDistributedRenderConfig` (or\n * inject their own client) don't pay the import cost.\n */\nasync function defaultExecutionsClient(): Promise<ExecutionsClientLike> {\n const mod = await import(\"@google-cloud/workflows\");\n const client = new mod.ExecutionsClient();\n return client as unknown as ExecutionsClientLike;\n}\n", "/**\n * Map a distributed `format` to the file extension the assembled output\n * should carry on disk + in GCS. Shared by `src/server.ts` (chunk +\n * assemble output paths) and `src/sdk/renderToCloudRun.ts` (final\n * output key construction) so the two sides agree on what an mp4\n * looks like vs a png-sequence.\n */\n\nimport type { DistributedFormat } from \"@hyperframes/producer/distributed\";\n\nexport type { DistributedFormat } from \"@hyperframes/producer/distributed\";\n\n// Closed-enum lookup table. TS enforces exhaustiveness via the\n// `Record<DistributedFormat, string>` annotation \u2014 adding a format to\n// `DistributedFormat` without adding the matching key here fails to\n// typecheck, which is the same exhaustiveness guarantee a switch +\n// `_exhaustive: never` arm provides but at lower complexity.\nconst FORMAT_EXTENSIONS: Record<DistributedFormat, string> = {\n mp4: \".mp4\",\n mov: \".mov\",\n webm: \".webm\",\n \"png-sequence\": \"\",\n};\n\nexport function formatExtension(format: DistributedFormat): string {\n return FORMAT_EXTENSIONS[format];\n}\n", "/**\n * Client-side validation for the Cloud Run adapter.\n *\n * The cloud-agnostic config-shape validation (`validateDistributedRenderConfig`,\n * `validateVariablesPayload`, `InvalidConfigError`) lives in\n * `@hyperframes/producer/distributed` and is shared with the other adapters.\n * This module re-exports those and adds the one piece that is specific to\n * Cloud Workflows: the 512 KiB execution-argument size cap.\n */\n\nimport { InvalidConfigError } from \"@hyperframes/producer/distributed\";\n\nexport {\n InvalidConfigError,\n validateDistributedRenderConfig,\n validateVariablesPayload,\n} from \"@hyperframes/producer/distributed\";\n\n/**\n * Hard cap on Cloud Workflows execution arguments \u2014 512 KiB per the Workflows\n * quotas page (maximum size of arguments passed when an execution starts).\n * The cap is on the entire serialized argument, not just the variables,\n * because users hit it at the wire boundary regardless of which field caused\n * the bloat.\n *\n * Specific to Cloud Workflows. Other runtimes (Lambda + Step Functions,\n * Temporal) have different caps; don't reuse this constant for those without\n * confirming the limit.\n */\nexport const MAX_WORKFLOWS_INPUT_BYTES = 512 * 1024;\n\n/** Pointer to the docs section that explains the URL-your-assets convention. */\nconst LARGE_VARIABLES_DOCS_URL =\n \"https://hyperframes.heygen.com/deploy/templates-on-lambda#working-with-large-variables\";\n\n/**\n * Validate that the serialized Cloud Workflows execution argument fits inside\n * the 512 KiB cap. Measured in UTF-8 bytes (the format the API uses on the\n * wire) \u2014 JS strings count UTF-16 code units, which under-reports for any\n * multi-byte character.\n *\n * Throws {@link InvalidConfigError} with a clear message naming the actual\n * byte count, the cap, and a pointer to the \"working with large variables\"\n * docs section, so users hit the limit at the SDK boundary with actionable\n * guidance instead of as an opaque argument-too-large error after the\n * execution starts.\n */\n// fallow-ignore-next-line complexity\nexport function validateWorkflowsInputSize(input: unknown): void {\n let serialized: string | undefined;\n try {\n serialized = JSON.stringify(input);\n } catch (err) {\n throw new InvalidConfigError(\n \"config\",\n `Cloud Workflows execution argument is not JSON-serializable: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n if (serialized === undefined) {\n throw new InvalidConfigError(\n \"config\",\n \"Cloud Workflows execution argument is not JSON-serializable (JSON.stringify returned undefined). \" +\n \"Check that all fields, including config.variables, are plain JSON values.\",\n );\n }\n const byteLength = Buffer.byteLength(serialized, \"utf8\");\n if (byteLength > MAX_WORKFLOWS_INPUT_BYTES) {\n throw new InvalidConfigError(\n \"config\",\n `Cloud Workflows execution argument is ${byteLength} bytes, which exceeds the ` +\n `${MAX_WORKFLOWS_INPUT_BYTES}-byte (512 KiB) limit. Variables are for typed data ` +\n `(strings, numbers, structured records); media assets (images, audio, video) should ` +\n `be passed as URL references the composition resolves at render time, not inlined as ` +\n `base64. See ${LARGE_VARIABLES_DOCS_URL} for the URL-your-assets convention.`,\n );\n }\n}\n", "/**\n * Per-render cost accounting for {@link getRenderProgress}.\n *\n * Google bills the render service two ways:\n *\n * - **Cloud Run** by **vCPU-seconds** and **GiB-seconds** of request\n * processing time, plus a flat per-request charge. Each handler\n * invocation returns its own `DurationMs` in the result body, so the\n * progress reader can recover billed time per step without a separate\n * Cloud Monitoring query \u2014 multiply by the service's configured vCPU /\n * memory to get the resource-seconds.\n * - **Cloud Workflows** by **steps executed**. The orchestration is a\n * fixed shape (Plan + N\u00D7RenderChunk + Assemble + a handful of control\n * steps), so the step count scales with chunk count.\n *\n * The math is documented inline so the constants stay close to the pricing\n * source they came from. Cost is **best-effort**: GCP pricing varies by\n * region + committed-use discounts; we use on-demand `us-central1` (Tier 1)\n * rates as of 2026-06 and label the result `displayCost` so callers see the\n * dollar value but downstream automation can also read the raw number.\n */\n\n/** Cloud Run request-based billing, us-central1 Tier 1: USD per vCPU-second. */\nconst CLOUD_RUN_USD_PER_VCPU_SECOND = 0.000024;\n/** Cloud Run request-based billing, us-central1 Tier 1: USD per GiB-second. */\nconst CLOUD_RUN_USD_PER_GIB_SECOND = 0.0000025;\n/** Cloud Run: USD per request ($0.40 per million). */\nconst CLOUD_RUN_USD_PER_REQUEST = 0.0000004;\n/** Cloud Workflows: USD per internal step ($0.01 per 1,000, after a free tier). */\nconst WORKFLOWS_USD_PER_STEP = 0.00001;\n\n/** Per-invocation billed slice the cost calc cares about. */\nexport interface BilledCloudRunInvocation {\n /** Wall-clock the handler reported via `DurationMs` in its result body. */\n durationMs: number;\n /** vCPU the Cloud Run service was configured with at invocation time. */\n vcpu: number;\n /** Memory in GiB the Cloud Run service was configured with. */\n memoryGib: number;\n /** `true` if the duration was inferred (step result missing) rather than read from the handler payload. */\n estimated: boolean;\n}\n\n/**\n * Result of {@link computeRenderCost}.\n *\n * NOTE: `displayCost` / `accruedSoFarUsd` cover Cloud Run compute + Cloud\n * Workflows steps only. They EXCLUDE GCS storage + network egress for the\n * plan tarball (which can be ~100 MB), chunk artifacts, and the final output\n * \u2014 see `breakdown.gcsEstimate`. Treat the figure as a compute-cost floor,\n * not the authoritative total bill.\n */\nexport interface RenderCost {\n /** USD accrued to date (Cloud Run + Workflows only; excludes GCS \u2014 see note above). */\n accruedSoFarUsd: number;\n /** Human-readable USD string, e.g. `\"$0.0214\"`. Excludes GCS storage/egress. */\n displayCost: string;\n breakdown: {\n cloudRunUsd: number;\n workflowsUsd: number;\n /** GCS transfer + storage cost varies by tier; we don't try to compute it here. */\n gcsEstimate: \"not-included\";\n /** `true` if any invocation fell back to estimated billing. */\n estimated: boolean;\n };\n}\n\n/**\n * Sum Cloud Run vCPU-seconds + GiB-seconds + per-request charges and Cloud\n * Workflows steps into an aggregate USD figure.\n *\n * `workflowSteps` is the count of Workflows steps executed so far \u2014 Plan\n * (1) + RenderChunk (chunkCount) + Assemble (1) + the control steps\n * (BuildChunkList, AssertChunkCount, \u2026). Pass the count the progress reader\n * derived from the execution; a rough constant overhead is fine since the\n * step charge is a rounding error next to Cloud Run compute.\n */\nexport function computeRenderCost(\n invocations: BilledCloudRunInvocation[],\n workflowSteps: number,\n): RenderCost {\n let cloudRunUsd = 0;\n let anyEstimated = false;\n for (const inv of invocations) {\n const seconds = inv.durationMs / 1000;\n cloudRunUsd += seconds * inv.vcpu * CLOUD_RUN_USD_PER_VCPU_SECOND;\n cloudRunUsd += seconds * inv.memoryGib * CLOUD_RUN_USD_PER_GIB_SECOND;\n cloudRunUsd += CLOUD_RUN_USD_PER_REQUEST;\n if (inv.estimated) anyEstimated = true;\n }\n const workflowsUsd = workflowSteps * WORKFLOWS_USD_PER_STEP;\n const accruedSoFarUsd = roundUsd(cloudRunUsd + workflowsUsd);\n return {\n accruedSoFarUsd,\n displayCost: formatUsd(accruedSoFarUsd),\n breakdown: {\n cloudRunUsd: roundUsd(cloudRunUsd),\n workflowsUsd: roundUsd(workflowsUsd),\n gcsEstimate: \"not-included\",\n estimated: anyEstimated,\n },\n };\n}\n\nfunction roundUsd(usd: number): number {\n // Four decimal places \u2014 enough resolution for per-chunk granularity.\n // Anything finer is noise vs GCP's own rounding.\n return Math.round(usd * 10_000) / 10_000;\n}\n\nfunction formatUsd(usd: number): string {\n return `$${usd.toFixed(4)}`;\n}\n", "/**\n * `getRenderProgress` \u2014 read-only progress + cost snapshot for a single\n * render started by {@link renderToCloudRun}.\n *\n * Pulls one `GetExecution` per call. Cloud Workflows does not surface\n * per-step payloads through the basic Executions API the way Step Functions\n * exposes its history, so this reader takes a different tack than the AWS\n * adapter: the workflow definition **accumulates** each step's result body\n * (Plan + every RenderChunk + Assemble) and returns them as one structured\n * object. On success we parse that object for frame totals, the output\n * file, and per-step `DurationMs` (which the handler stamps into every\n * result), then compute cost against the service's configured vCPU/memory.\n *\n * Progress is therefore coarse while the execution is ACTIVE (we report\n * `running` with `overallProgress = 0`) and exact once it SUCCEEDS\n * (`overallProgress = 1`, real frame + cost numbers). Mid-flight per-chunk\n * progress would require the Workflows step-entries API; that's a tracked\n * follow-up, not part of the first version.\n */\n\nimport {\n type BilledCloudRunInvocation,\n computeRenderCost,\n type RenderCost,\n} from \"./costAccounting.js\";\n\n/** Normalised render status. Maps from Cloud Workflows execution states. */\nexport type RenderStatus = \"running\" | \"succeeded\" | \"failed\" | \"cancelled\" | \"unknown\";\n\n/** One error surfaced by the execution. */\nexport interface RenderError {\n /** Step the failure surfaced in, when recoverable from the error context; else `<execution>`. */\n state: string;\n /** Error class / type. */\n error: string;\n /** Cause string (often a stringified JSON payload from the handler). */\n cause: string;\n}\n\n/** Snapshot of a single render's progress + cost + errors at one point in time. */\nexport interface RenderProgress {\n status: RenderStatus;\n /** `[0, 1]`; coarse while running, exact on success. */\n overallProgress: number;\n framesRendered: number;\n /** `null` until the execution succeeds and the accumulated plan result is read. */\n totalFrames: number | null;\n /** Cloud Run invocations the workflow scheduled (Plan + chunks + Assemble), when known. */\n invocationsObserved: number;\n costs: RenderCost;\n /** Final output object if Assemble succeeded; `null` otherwise. */\n outputFile: { gcsUri: string; bytes: number | null } | null;\n errors: RenderError[];\n /** `true` once the execution has terminated in a non-success state. */\n fatalErrorEncountered: boolean;\n startedAt: string;\n endedAt: string | null;\n}\n\n/** Protobuf Timestamp shape the gapic client returns for start/end times. */\ninterface ProtoTimestamp {\n seconds?: number | string | null;\n nanos?: number | null;\n}\n\n/** Subset of a Cloud Workflows Execution this reader consumes. */\nexport interface ExecutionRecord {\n name?: string | null;\n state?: string | null;\n result?: string | null;\n error?: { payload?: string | null; context?: string | null } | null;\n startTime?: ProtoTimestamp | string | null;\n endTime?: ProtoTimestamp | string | null;\n}\n\n/** Minimal surface of `@google-cloud/workflows`' `ExecutionsClient` for reads. */\nexport interface ExecutionsGetClientLike {\n getExecution(req: { name: string }): Promise<[ExecutionRecord, ...unknown[]]>;\n}\n\n/** Options for {@link getRenderProgress}. */\nexport interface GetRenderProgressOptions {\n /** Server-assigned execution resource name from a {@link renderToCloudRun} call. */\n executionName: string;\n /** vCPU the Cloud Run service is configured with (for cost). Default 4. */\n vcpu?: number;\n /** Memory in GiB the Cloud Run service is configured with (for cost). Default 16. */\n memoryGib?: number;\n /** Test injection seam \u2014 production callers leave unset. */\n executions?: ExecutionsGetClientLike;\n}\n\nconst DEFAULT_VCPU = 4;\nconst DEFAULT_MEMORY_GIB = 16;\n\n/** Result body the handler returns for each action; the workflow accumulates these. */\ninterface AccumulatedResult {\n Plan?: { TotalFrames?: number; DurationMs?: number } | null;\n Chunks?: Array<{ FramesEncoded?: number; DurationMs?: number } | null> | null;\n Assemble?: {\n OutputGcsUri?: string;\n FileSize?: number;\n FramesEncoded?: number;\n DurationMs?: number;\n } | null;\n}\n\n/** Pull a current progress snapshot for one render. */\n// fallow-ignore-next-line complexity\nexport async function getRenderProgress(opts: GetRenderProgressOptions): Promise<RenderProgress> {\n if (!opts.executionName) {\n throw new Error(\"[getRenderProgress] executionName is required\");\n }\n const executions = opts.executions ?? (await defaultExecutionsClient());\n const vcpu = opts.vcpu ?? DEFAULT_VCPU;\n const memoryGib = opts.memoryGib ?? DEFAULT_MEMORY_GIB;\n\n const [execution] = await executions.getExecution({ name: opts.executionName });\n const status = mapState(execution.state);\n const startedAt = toIso(execution.startTime) ?? new Date(0).toISOString();\n const endedAt = toIso(execution.endTime);\n\n const errors: RenderError[] = [];\n if (execution.error) {\n errors.push({\n state: execution.error.context ?? \"<execution>\",\n error: extractErrorName(execution.error.payload) ?? \"ExecutionError\",\n cause: execution.error.payload ?? \"\",\n });\n }\n\n // Default snapshot: running / unknown \u2014 no frame or cost data until the\n // accumulated result is available on success.\n if (status !== \"succeeded\") {\n return {\n status,\n overallProgress: 0,\n framesRendered: 0,\n totalFrames: null,\n invocationsObserved: 0,\n costs: computeRenderCost([], 0),\n outputFile: null,\n errors,\n fatalErrorEncountered: status === \"failed\" || status === \"cancelled\",\n startedAt,\n endedAt,\n };\n }\n\n const acc = parseAccumulated(execution.result);\n const chunks = acc.Chunks?.filter((c): c is NonNullable<typeof c> => c != null) ?? [];\n const framesRendered = chunks.reduce((sum, c) => sum + (c.FramesEncoded ?? 0), 0);\n const totalFrames = typeof acc.Plan?.TotalFrames === \"number\" ? acc.Plan.TotalFrames : null;\n\n const invocations: BilledCloudRunInvocation[] = [];\n const pushInv = (durationMs: number | undefined): void => {\n invocations.push({\n durationMs: typeof durationMs === \"number\" ? durationMs : 0,\n vcpu,\n memoryGib,\n estimated: typeof durationMs !== \"number\",\n });\n };\n if (acc.Plan) pushInv(acc.Plan.DurationMs);\n for (const c of chunks) pushInv(c.DurationMs);\n if (acc.Assemble) pushInv(acc.Assemble.DurationMs);\n\n // Workflow step count: Plan + N chunks + Assemble + a small constant of\n // control steps (BuildChunkList, AssertChunkCount, the map scaffold).\n const workflowSteps = invocations.length + 4;\n const costs = computeRenderCost(invocations, workflowSteps);\n\n const outputGcsUri = acc.Assemble?.OutputGcsUri;\n const outputFile = outputGcsUri\n ? {\n gcsUri: outputGcsUri,\n bytes: typeof acc.Assemble?.FileSize === \"number\" ? acc.Assemble.FileSize : null,\n }\n : null;\n\n return {\n status,\n overallProgress: 1,\n framesRendered,\n totalFrames,\n invocationsObserved: invocations.length,\n costs,\n outputFile,\n errors,\n fatalErrorEncountered: false,\n startedAt,\n endedAt,\n };\n}\n\n// fallow-ignore-next-line complexity\nfunction mapState(state: string | null | undefined): RenderStatus {\n switch (state) {\n case \"ACTIVE\":\n case \"QUEUED\":\n return \"running\";\n case \"SUCCEEDED\":\n return \"succeeded\";\n case \"FAILED\":\n case \"UNAVAILABLE\":\n return \"failed\";\n case \"CANCELLED\":\n return \"cancelled\";\n default:\n return \"unknown\";\n }\n}\n\n// fallow-ignore-next-line complexity\nfunction parseAccumulated(result: string | null | undefined): AccumulatedResult {\n if (!result) return {};\n try {\n const parsed = JSON.parse(result) as unknown;\n if (parsed && typeof parsed === \"object\") return parsed as AccumulatedResult;\n } catch {\n // Non-JSON result \u2014 treat as empty so cost/frames degrade to zero\n // rather than throwing on a snapshot read.\n }\n return {};\n}\n\n/**\n * Best-effort pull of the handler's error name out of a Workflows failure\n * payload. On an http step failure, Workflows wraps the response as\n * `{ code, message, body, ... }` where `body` is the handler's JSON\n * `{ error, message }`. We dig out `error` (the typed name like\n * `PLAN_HASH_MISMATCH`) so triage sees the real cause, not a generic label.\n * Returns undefined for any shape we don't recognise \u2014 never throws.\n */\n// fallow-ignore-next-line complexity\nfunction extractErrorName(payload: string | null | undefined): string | undefined {\n if (!payload) return undefined;\n try {\n const outer = JSON.parse(payload) as { error?: unknown; body?: unknown };\n if (typeof outer.error === \"string\") return outer.error;\n if (typeof outer.body === \"string\") {\n const inner = JSON.parse(outer.body) as { error?: unknown };\n if (typeof inner.error === \"string\") return inner.error;\n } else if (outer.body && typeof outer.body === \"object\") {\n const inner = outer.body as { error?: unknown };\n if (typeof inner.error === \"string\") return inner.error;\n }\n } catch {\n // Non-JSON / unexpected shape \u2014 fall through to the generic label.\n }\n return undefined;\n}\n\n// fallow-ignore-next-line complexity\nfunction toIso(ts: ProtoTimestamp | string | null | undefined): string | null {\n if (ts == null) return null;\n if (typeof ts === \"string\") return ts;\n const seconds = ts.seconds == null ? null : Number(ts.seconds);\n if (seconds == null || !Number.isFinite(seconds)) return null;\n const ms = seconds * 1000 + (ts.nanos ?? 0) / 1e6;\n return new Date(ms).toISOString();\n}\n\nasync function defaultExecutionsClient(): Promise<ExecutionsGetClientLike> {\n const mod = await import(\"@google-cloud/workflows\");\n const client = new mod.ExecutionsClient();\n return client as unknown as ExecutionsGetClientLike;\n}\n"],
|
|
5
|
+
"mappings": ";AAeA,SAAS,aAAa,UAAAA,SAAQ,YAAAC,iBAAgB;AAC9C,SAAS,cAAc;AACvB,SAAS,YAAY;AACrB,SAAS,eAAe;AACxB,SAAS,sBAAsB;;;ACE/B,SAAS,mBAAmB,YAAY,WAAW,QAAQ,gBAAgB;AAC3E,SAAS,eAAe;AAGxB,YAAY,SAAS;AAUd,SAAS,YAAY,KAA0B;AACpD,MAAI,CAAC,IAAI,WAAW,OAAO,GAAG;AAC5B,UAAM,IAAI,MAAM,2CAA2C,KAAK,UAAU,GAAG,CAAC,EAAE;AAAA,EAClF;AACA,QAAM,OAAO,IAAI,MAAM,QAAQ,MAAM;AACrC,QAAM,QAAQ,KAAK,QAAQ,GAAG;AAC9B,MAAI,UAAU,IAAI;AAChB,UAAM,IAAI,MAAM,yCAAyC,KAAK,UAAU,GAAG,CAAC,EAAE;AAAA,EAChF;AACA,QAAM,SAAS,KAAK,MAAM,GAAG,KAAK;AAClC,QAAM,MAAM,KAAK,MAAM,QAAQ,CAAC;AAChC,MAAI,CAAC,UAAU,CAAC,KAAK;AACnB,UAAM,IAAI,MAAM,iDAAiD,KAAK,UAAU,GAAG,CAAC,EAAE;AAAA,EACxF;AACA,SAAO,EAAE,QAAQ,IAAI;AACvB;AAGO,SAAS,aAAa,KAA0B;AACrD,SAAO,QAAQ,IAAI,MAAM,IAAI,IAAI,GAAG;AACtC;AAuBA,eAAsB,gBACpB,SACA,WACA,KACA,aACe;AACf,MAAI,CAAC,WAAW,SAAS,GAAG;AAC1B,UAAM,IAAI,MAAM,yCAAyC,SAAS,EAAE;AAA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AyB;AAClD,QAAM,SAAS,IAAI,IAAI,iBAAiB;AACxC,SAAO;AACT;",
|
|
6
|
+
"names": ["rmSync", "statSync", "statSync", "rmSync", "InvalidConfigError", "defaultExecutionsClient"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `renderToCloudRun` — start a distributed render against an already-deployed
|
|
3
|
+
* Cloud Run service + Cloud Workflows definition and return a handle the
|
|
4
|
+
* caller can poll with {@link getRenderProgress}.
|
|
5
|
+
*
|
|
6
|
+
* The function does *not* wait for the render to finish. Cloud Workflows
|
|
7
|
+
* executions can run for hours; blocking the caller's process on the
|
|
8
|
+
* execution is the wrong default. The returned `RenderHandle` carries
|
|
9
|
+
* everything the progress / cost / download paths need.
|
|
10
|
+
*
|
|
11
|
+
* Wire order:
|
|
12
|
+
* 1. Validate config (typed throw before any GCP call).
|
|
13
|
+
* 2. `deploySite` if no `siteHandle` was provided.
|
|
14
|
+
* 3. `CreateExecution` against the workflow with the argument shape the
|
|
15
|
+
* `packages/gcp-cloud-run/terraform/workflow.yaml` definition expects.
|
|
16
|
+
* 4. Return handle. The GCS `outputKey` is deterministic from the
|
|
17
|
+
* client-generated `renderId` so the caller can predict the final
|
|
18
|
+
* object URL before the (server-assigned) execution id exists.
|
|
19
|
+
*
|
|
20
|
+
* Unlike Step Functions, Cloud Workflows assigns the execution id
|
|
21
|
+
* server-side, so we cannot use it as the GCS prefix. We mint a `renderId`
|
|
22
|
+
* (uuid) client-side, use it for every GCS path, and pass it into the
|
|
23
|
+
* workflow argument; the server-assigned execution resource name is tracked
|
|
24
|
+
* separately for polling.
|
|
25
|
+
*/
|
|
26
|
+
import type { Storage } from "@google-cloud/storage";
|
|
27
|
+
import type { SerializableDistributedRenderConfig } from "../events.js";
|
|
28
|
+
import { type SiteHandle } from "./deploySite.js";
|
|
29
|
+
/**
|
|
30
|
+
* Minimal surface of `@google-cloud/workflows`' `ExecutionsClient` that
|
|
31
|
+
* this module needs. The real client satisfies this; tests inject a double.
|
|
32
|
+
*/
|
|
33
|
+
export interface ExecutionsClientLike {
|
|
34
|
+
workflowPath(project: string, location: string, workflow: string): string;
|
|
35
|
+
createExecution(req: {
|
|
36
|
+
parent: string;
|
|
37
|
+
execution: {
|
|
38
|
+
argument: string;
|
|
39
|
+
};
|
|
40
|
+
}): Promise<[{
|
|
41
|
+
name?: string | null;
|
|
42
|
+
state?: string | null;
|
|
43
|
+
}, ...unknown[]]>;
|
|
44
|
+
}
|
|
45
|
+
/** Options for {@link renderToCloudRun}. */
|
|
46
|
+
export interface RenderToCloudRunOptions {
|
|
47
|
+
/** Local project directory. Required when `siteHandle` is not supplied. */
|
|
48
|
+
projectDir?: string;
|
|
49
|
+
/** Re-use an existing `deploySite` upload (skips tar+GCS upload). */
|
|
50
|
+
siteHandle?: SiteHandle;
|
|
51
|
+
/** Validated `SerializableDistributedRenderConfig` (no logger / abortSignal). */
|
|
52
|
+
config: SerializableDistributedRenderConfig;
|
|
53
|
+
/** GCS bucket from the Terraform output (`render_bucket_name`). */
|
|
54
|
+
bucketName: string;
|
|
55
|
+
/** GCP project id hosting the workflow. */
|
|
56
|
+
projectId: string;
|
|
57
|
+
/** Workflow location, e.g. `us-central1`. */
|
|
58
|
+
location: string;
|
|
59
|
+
/** Workflow id from the Terraform output (`workflow_name`). */
|
|
60
|
+
workflowId: string;
|
|
61
|
+
/**
|
|
62
|
+
* HTTPS URL of the deployed Cloud Run render service (Terraform output
|
|
63
|
+
* `service_url`). The workflow POSTs every step (plan / renderChunk /
|
|
64
|
+
* assemble) to this URL; passed as an execution argument so the workflow
|
|
65
|
+
* definition stays free of hard-coded URLs.
|
|
66
|
+
*/
|
|
67
|
+
serviceUrl: string;
|
|
68
|
+
/**
|
|
69
|
+
* Final output GCS key. Defaults to `renders/<renderId>/output.<ext>`
|
|
70
|
+
* where `<ext>` is derived from `config.format`.
|
|
71
|
+
*/
|
|
72
|
+
outputKey?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Client-generated render id. Defaults to `hf-render-<uuid>`. Used as the
|
|
75
|
+
* GCS key prefix and echoed into the workflow argument; not the same as
|
|
76
|
+
* the server-assigned execution id.
|
|
77
|
+
*/
|
|
78
|
+
renderId?: string;
|
|
79
|
+
/** Test injection seam — production callers leave unset. */
|
|
80
|
+
executions?: ExecutionsClientLike;
|
|
81
|
+
/** Test injection seam — propagated to `deploySite` when applicable. */
|
|
82
|
+
storage?: Storage;
|
|
83
|
+
}
|
|
84
|
+
/** Stable identifier + every URL/name the caller needs to follow the render. */
|
|
85
|
+
export interface RenderHandle {
|
|
86
|
+
/** Client-generated render id; the GCS prefix everything lands under. */
|
|
87
|
+
renderId: string;
|
|
88
|
+
/** Server-assigned execution resource name; pass to {@link getRenderProgress}. */
|
|
89
|
+
executionName: string;
|
|
90
|
+
bucketName: string;
|
|
91
|
+
workflowId: string;
|
|
92
|
+
outputGcsUri: string;
|
|
93
|
+
projectGcsUri: string;
|
|
94
|
+
startedAt: string;
|
|
95
|
+
}
|
|
96
|
+
export declare function renderToCloudRun(opts: RenderToCloudRunOptions): Promise<RenderHandle>;
|
|
97
|
+
//# sourceMappingURL=renderToCloudRun.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderToCloudRun.d.ts","sourceRoot":"","sources":["../../src/sdk/renderToCloudRun.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,KAAK,EAAE,mCAAmC,EAAE,MAAM,cAAc,CAAC;AAGxE,OAAO,EAAc,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAG9D;;;GAGG;AACH,MAAM,WAAW,oBAAoB;IACnC,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1E,eAAe,CAAC,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AI,EAAE,uBAAuB,GAAG,OAAO,CAAC,YAAY,CAAC,CA4E3F"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side validation for the Cloud Run adapter.
|
|
3
|
+
*
|
|
4
|
+
* The cloud-agnostic config-shape validation (`validateDistributedRenderConfig`,
|
|
5
|
+
* `validateVariablesPayload`, `InvalidConfigError`) lives in
|
|
6
|
+
* `@hyperframes/producer/distributed` and is shared with the other adapters.
|
|
7
|
+
* This module re-exports those and adds the one piece that is specific to
|
|
8
|
+
* Cloud Workflows: the 512 KiB execution-argument size cap.
|
|
9
|
+
*/
|
|
10
|
+
export { InvalidConfigError, validateDistributedRenderConfig, validateVariablesPayload, } from "@hyperframes/producer/distributed";
|
|
11
|
+
/**
|
|
12
|
+
* Hard cap on Cloud Workflows execution arguments — 512 KiB per the Workflows
|
|
13
|
+
* quotas page (maximum size of arguments passed when an execution starts).
|
|
14
|
+
* The cap is on the entire serialized argument, not just the variables,
|
|
15
|
+
* because users hit it at the wire boundary regardless of which field caused
|
|
16
|
+
* the bloat.
|
|
17
|
+
*
|
|
18
|
+
* Specific to Cloud Workflows. Other runtimes (Lambda + Step Functions,
|
|
19
|
+
* Temporal) have different caps; don't reuse this constant for those without
|
|
20
|
+
* confirming the limit.
|
|
21
|
+
*/
|
|
22
|
+
export declare const MAX_WORKFLOWS_INPUT_BYTES: number;
|
|
23
|
+
/**
|
|
24
|
+
* Validate that the serialized Cloud Workflows execution argument fits inside
|
|
25
|
+
* the 512 KiB cap. Measured in UTF-8 bytes (the format the API uses on the
|
|
26
|
+
* wire) — JS strings count UTF-16 code units, which under-reports for any
|
|
27
|
+
* multi-byte character.
|
|
28
|
+
*
|
|
29
|
+
* Throws {@link InvalidConfigError} with a clear message naming the actual
|
|
30
|
+
* byte count, the cap, and a pointer to the "working with large variables"
|
|
31
|
+
* docs section, so users hit the limit at the SDK boundary with actionable
|
|
32
|
+
* guidance instead of as an opaque argument-too-large error after the
|
|
33
|
+
* execution starts.
|
|
34
|
+
*/
|
|
35
|
+
export declare function validateWorkflowsInputSize(input: unknown): void;
|
|
36
|
+
//# sourceMappingURL=validateConfig.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateConfig.d.ts","sourceRoot":"","sources":["../../src/sdk/validateConfig.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,EACL,kBAAkB,EAClB,+BAA+B,EAC/B,wBAAwB,GACzB,MAAM,mCAAmC,CAAC;AAE3C;;;;;;;;;;GAUG;AACH,eAAO,MAAM,yBAAyB,QAAa,CAAC;AAMpD;;;;;;;;;;;GAWG;AAEH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CA4B/D"}
|