@zabaca/zbc 0.17.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -86,6 +86,52 @@ function wrangler(workdir: string, args: string[], env: NodeJS.ProcessEnv, input
86
86
  return combined
87
87
  }
88
88
 
89
+ /**
90
+ * Which deploy this is, as a value the deployed code can be told.
91
+ *
92
+ * `GITHUB_SHA` in CI, `git rev-parse HEAD` locally. A dirty working tree gets a
93
+ * distinct id rather than the bare commit: a local apply builds the image from
94
+ * the working tree, so the same commit with edits is a different image, and
95
+ * handing it the commit's id would be the staleness this exists to prevent. The
96
+ * timestamp makes each such apply its own deploy, which is the honest answer
97
+ * when the content is whatever happened to be on disk.
98
+ *
99
+ * Absence is a hard error rather than a silent skip — see `deployIdVar`.
100
+ */
101
+ function resolveDeployId(projectRoot: string, varName: string): string {
102
+ const fromCi = process.env.GITHUB_SHA?.trim()
103
+ if (fromCi) return fromCi
104
+
105
+ let head: string
106
+ try {
107
+ head = execSync('git rev-parse HEAD', {
108
+ cwd: projectRoot,
109
+ encoding: 'utf8',
110
+ stdio: ['ignore', 'pipe', 'ignore'],
111
+ }).trim()
112
+ } catch {
113
+ throw new Error(
114
+ `deployIdVar is set (${varName}) but this deploy has no id: GITHUB_SHA is unset and \`git rev-parse HEAD\` failed in ${projectRoot}. ` +
115
+ `Deploy from CI, from a git checkout, or drop deployIdVar.`,
116
+ )
117
+ }
118
+
119
+ let dirty = false
120
+ try {
121
+ dirty =
122
+ execSync('git status --porcelain', {
123
+ cwd: projectRoot,
124
+ encoding: 'utf8',
125
+ stdio: ['ignore', 'pipe', 'ignore'],
126
+ }).trim() !== ''
127
+ } catch {
128
+ // A repository whose status cannot be read still has a HEAD worth
129
+ // reporting; treating that as clean is the same answer as before this
130
+ // check existed.
131
+ }
132
+ return dirty ? `${head}-dirty-${Date.now()}` : head
133
+ }
134
+
89
135
  const buildSchema = z.object({
90
136
  command: z.string(),
91
137
  cwd: z.string().optional(),
@@ -475,12 +521,55 @@ export const cloudflareModule = defineModule({
475
521
  */
476
522
  bindings: z.array(bindingSchema).default([]),
477
523
  /**
478
- * For container-backed Workers: roll the running container to the new image
479
- * IMMEDIATELY on deploy (`--containers-rollout immediate`). The wrangler
480
- * default is a GRADUAL rollout, which never drains a single always-warm,
481
- * DO-bound container — so a redeployed image silently never takes effect
482
- * until the container idle-sleeps. Set true whenever the image matters at
483
- * deploy time. No-op for asset-only Workers.
524
+ * Name a Worker var after the deploy itself: the deployed commit
525
+ * (`GITHUB_SHA` in CI, `git rev-parse HEAD` locally, `<sha>-dirty-<ms>` on
526
+ * a working tree with changes), passed as `--var <name>:<id>` like any
527
+ * other var. Unset — the default — injects nothing.
528
+ *
529
+ * It is worth a key of its own because a var is the ONLY channel a Worker
530
+ * has for telling a container-backed payload that a deploy happened at all.
531
+ * `immediateContainerRollout` below rolls the container application to the
532
+ * new image and does not replace a running instance; a payload that reacts
533
+ * to its environment (walgit fingerprints it in a Durable Object and
534
+ * destroys the container when it moves) has nothing to react to when a
535
+ * deploy changed only code. This gives it something: every deploy from a
536
+ * new commit changes this value, and no deploy from the same commit does.
537
+ *
538
+ * Also the only place the deployed commit is written down — Cloudflare
539
+ * numbers container versions itself (v50, v51) and nothing else in a deploy
540
+ * names the code inside one.
541
+ *
542
+ * A var, so never a secret: it is a commit id, visible in the dashboard
543
+ * beside every other var, which is half the point.
544
+ *
545
+ * A HARD ERROR when no id can be resolved (not CI, not a git checkout).
546
+ * Silently omitting it would deploy the payload with no deploy marker,
547
+ * which is exactly the invisible staleness the key exists to prevent. An
548
+ * entry of the same name in `workerVars` takes precedence — the instance
549
+ * pinning a value outranks the module deriving one.
550
+ */
551
+ deployIdVar: z.string().optional(),
552
+ /**
553
+ * For container-backed Workers: roll the CONTAINER APPLICATION to the new
554
+ * image on deploy (`--containers-rollout immediate`) instead of wrangler's
555
+ * gradual default. Set true whenever the image matters at deploy time.
556
+ * No-op for asset-only Workers.
557
+ *
558
+ * WHAT IT DOES NOT DO — and the reason this paragraph exists — is replace a
559
+ * RUNNING instance. It rolls the application to a new version; a single
560
+ * always-warm, DO-bound instance is not drained by that rollout, and keeps
561
+ * serving the old image until it idle-sleeps, which under sustained traffic
562
+ * is never. Observed on walgit's 0.16.1 deploy (2026-09-14): the
563
+ * application moved to v51 with a new image, the rollout reported
564
+ * `completed`, and the instance answering git was the one started an hour
565
+ * earlier — the fix in that release was not live.
566
+ *
567
+ * It also has nothing to roll for a deploy that changes only vars: no new
568
+ * image, no new container version.
569
+ *
570
+ * So it is necessary and not sufficient. Replacing a running instance is
571
+ * the payload's own job, and `deployIdVar` above is what lets it know it
572
+ * has one to do.
484
573
  */
485
574
  immediateContainerRollout: z.boolean().default(false),
486
575
  /**
@@ -572,6 +661,17 @@ export const cloudflareModule = defineModule({
572
661
  const resolvedVars = config.workerVars.map((entry) =>
573
662
  resolveWorkerValue(entry, ctx, 'workerVars'),
574
663
  )
664
+ // The deploy's own id, resolved here so a deploy with no id fails with
665
+ // every other unresolvable reference rather than at wrangler. Last,
666
+ // and skipped when `workerVars` already names it: a `--var` repeated
667
+ // twice is a value wrangler picks between, and the instance's own entry
668
+ // is the one that should win.
669
+ if (config.deployIdVar && !resolvedVars.some((v) => v.name === config.deployIdVar)) {
670
+ resolvedVars.push({
671
+ name: config.deployIdVar,
672
+ value: resolveDeployId(ctx.projectRoot, config.deployIdVar),
673
+ })
674
+ }
575
675
  const resolvedSecrets = config.workerSecrets.map((entry) =>
576
676
  resolveWorkerValue(entry, ctx, 'workerSecrets'),
577
677
  )