create-signkit 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +661 -0
- package/README.md +14 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.d.ts.map +1 -0
- package/dist/bin.js +10 -0
- package/dist/bin.js.map +1 -0
- package/dist/cli/effective-config.d.ts +31 -0
- package/dist/cli/effective-config.d.ts.map +1 -0
- package/dist/cli/effective-config.js +91 -0
- package/dist/cli/effective-config.js.map +1 -0
- package/dist/cli/errors.d.ts +13 -0
- package/dist/cli/errors.d.ts.map +1 -0
- package/dist/cli/errors.js +29 -0
- package/dist/cli/errors.js.map +1 -0
- package/dist/cli/parse.d.ts +46 -0
- package/dist/cli/parse.d.ts.map +1 -0
- package/dist/cli/parse.js +303 -0
- package/dist/cli/parse.js.map +1 -0
- package/dist/cli/run.d.ts +14 -0
- package/dist/cli/run.d.ts.map +1 -0
- package/dist/cli/run.js +71 -0
- package/dist/cli/run.js.map +1 -0
- package/dist/cli/urls.d.ts +12 -0
- package/dist/cli/urls.d.ts.map +1 -0
- package/dist/cli/urls.js +85 -0
- package/dist/cli/urls.js.map +1 -0
- package/dist/constants.d.ts +44 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +74 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/cloudflare/config.d.ts +19 -0
- package/dist/providers/cloudflare/config.d.ts.map +1 -0
- package/dist/providers/cloudflare/config.js +51 -0
- package/dist/providers/cloudflare/config.js.map +1 -0
- package/dist/providers/cloudflare/reconciler.d.ts +52 -0
- package/dist/providers/cloudflare/reconciler.d.ts.map +1 -0
- package/dist/providers/cloudflare/reconciler.js +675 -0
- package/dist/providers/cloudflare/reconciler.js.map +1 -0
- package/dist/providers/cloudflare/smoke.d.ts +24 -0
- package/dist/providers/cloudflare/smoke.d.ts.map +1 -0
- package/dist/providers/cloudflare/smoke.js +145 -0
- package/dist/providers/cloudflare/smoke.js.map +1 -0
- package/dist/providers/cloudflare/wrangler.d.ts +84 -0
- package/dist/providers/cloudflare/wrangler.d.ts.map +1 -0
- package/dist/providers/cloudflare/wrangler.js +331 -0
- package/dist/providers/cloudflare/wrangler.js.map +1 -0
- package/dist/providers/registry.d.ts +4 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +9 -0
- package/dist/providers/registry.js.map +1 -0
- package/dist/release/extract.d.ts +32 -0
- package/dist/release/extract.d.ts.map +1 -0
- package/dist/release/extract.js +185 -0
- package/dist/release/extract.js.map +1 -0
- package/dist/release/github.d.ts +39 -0
- package/dist/release/github.d.ts.map +1 -0
- package/dist/release/github.js +219 -0
- package/dist/release/github.js.map +1 -0
- package/dist/release/manifest.d.ts +43 -0
- package/dist/release/manifest.d.ts.map +1 -0
- package/dist/release/manifest.js +186 -0
- package/dist/release/manifest.js.map +1 -0
- package/dist/release/paths.d.ts +8 -0
- package/dist/release/paths.d.ts.map +1 -0
- package/dist/release/paths.js +55 -0
- package/dist/release/paths.js.map +1 -0
- package/dist/release/semver.d.ts +17 -0
- package/dist/release/semver.d.ts.map +1 -0
- package/dist/release/semver.js +99 -0
- package/dist/release/semver.js.map +1 -0
- package/dist/runtime/fs.d.ts +17 -0
- package/dist/runtime/fs.d.ts.map +1 -0
- package/dist/runtime/fs.js +57 -0
- package/dist/runtime/fs.js.map +1 -0
- package/dist/runtime/http.d.ts +24 -0
- package/dist/runtime/http.d.ts.map +1 -0
- package/dist/runtime/http.js +118 -0
- package/dist/runtime/http.js.map +1 -0
- package/dist/runtime/process.d.ts +22 -0
- package/dist/runtime/process.d.ts.map +1 -0
- package/dist/runtime/process.js +146 -0
- package/dist/runtime/process.js.map +1 -0
- package/dist/runtime.d.ts +25 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +33 -0
- package/dist/runtime.js.map +1 -0
- package/dist/state/store.d.ts +51 -0
- package/dist/state/store.d.ts.map +1 -0
- package/dist/state/store.js +110 -0
- package/dist/state/store.js.map +1 -0
- package/package.json +58 -0
- package/schema/release-manifest.v1.json +108 -0
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { BACKUP_DIR_MODE, BACKUP_FILE_MODE, MIGRATION_POLICY_NOTES, PACKAGE_NAME, REQUIRED_WORKER_SECRETS } from '../../constants.js';
|
|
3
|
+
import { resolveEffectiveConfig } from '../../cli/effective-config.js';
|
|
4
|
+
import { conflict, generic, preflight, usage } from '../../cli/errors.js';
|
|
5
|
+
import { isValidEmailAddress } from '../../cli/urls.js';
|
|
6
|
+
import { createStateStore, d1BackupFileName, resolveBackupsDir, resolveStatePath } from '../../state/store.js';
|
|
7
|
+
import { renderWranglerConfig } from './config.js';
|
|
8
|
+
import { selectProductionWorkersDevOrigin, smokeCheck } from './smoke.js';
|
|
9
|
+
import { posixRelativeToRoot } from '../../release/paths.js';
|
|
10
|
+
import { assertSelectedAccountAuthorized, assertSuccessfulUpload, sortWorkerVersions } from './wrangler.js';
|
|
11
|
+
const BOOTSTRAP_WARNING_CONFIGURED = 'The bootstrap owner email is configured as a non-secret Worker var. Claim the initial owner immediately via POST /api/v1/instance/bootstrap before advertising this URL: only the matching verified identity can claim it, and the window stays open until then.';
|
|
12
|
+
const BOOTSTRAP_WARNING_MISSING = 'No bootstrap owner email is configured: an uninitialized instance fails closed and cannot be claimed. Pass --bootstrap-owner-email <email> to deploy or upgrade; create-signkit applies it as a non-secret Worker var.';
|
|
13
|
+
function bootstrapWarningFor(target) {
|
|
14
|
+
return target.bootstrapOwnerEmail ? BOOTSTRAP_WARNING_CONFIGURED : BOOTSTRAP_WARNING_MISSING;
|
|
15
|
+
}
|
|
16
|
+
export async function reconcileCloudflare(input, runtime) {
|
|
17
|
+
if (input.command !== 'plan' && !input.yes) {
|
|
18
|
+
throw usage('mutating commands require --yes');
|
|
19
|
+
}
|
|
20
|
+
const stateStore = createStateStore(runtime.fs, resolvePath(runtime, input));
|
|
21
|
+
const existing = await stateStore.load();
|
|
22
|
+
assertCloudflareState(existing);
|
|
23
|
+
if (input.command === 'upgrade' && !existing) {
|
|
24
|
+
throw conflict('upgrade requires recorded local state; adopt the existing Worker before upgrading rather than taking it over');
|
|
25
|
+
}
|
|
26
|
+
const target = resolveEffectiveConfig(input, existing);
|
|
27
|
+
assertSelectedAccountAuthorized(await runtime.wrangler.whoami(), target.accountId);
|
|
28
|
+
const remote = await inspectRemote(runtime.wrangler, target);
|
|
29
|
+
assertUpgradeRouteUnchanged(input, existing);
|
|
30
|
+
const drift = detectDrift(existing, target, remote);
|
|
31
|
+
if (drift.length > 0 && input.command !== 'adopt' && input.command !== 'plan') {
|
|
32
|
+
throw conflict(`remote deployment drifted from local state (${drift.join('; ')}). Re-run with --cloudflare adopt and the intended --worker-name/--d1/--r2 values to record the existing resources.`);
|
|
33
|
+
}
|
|
34
|
+
if (input.command === 'upgrade' && !remote.workerExists) {
|
|
35
|
+
throw conflict('upgrade requires an existing Worker; adopt the resources first');
|
|
36
|
+
}
|
|
37
|
+
if (input.command === 'adopt') {
|
|
38
|
+
return adopt(input, target, runtime, stateStore, existing, remote);
|
|
39
|
+
}
|
|
40
|
+
const release = await runtime.releases.resolve({
|
|
41
|
+
version: input.version,
|
|
42
|
+
channel: input.channel
|
|
43
|
+
});
|
|
44
|
+
const plan = buildPlan(input, existing, target, remote, release);
|
|
45
|
+
if (input.command === 'plan') {
|
|
46
|
+
return {
|
|
47
|
+
exitCode: 0,
|
|
48
|
+
command: 'plan',
|
|
49
|
+
provider: 'cloudflare',
|
|
50
|
+
version: release.tag,
|
|
51
|
+
commit: release.commit,
|
|
52
|
+
plan,
|
|
53
|
+
mutations: [],
|
|
54
|
+
statePath: stateStore.path,
|
|
55
|
+
missingSecrets: missingSecrets(remote.secretNames, uniqueSecrets(release.manifest.requiredSecrets)),
|
|
56
|
+
drift,
|
|
57
|
+
message: 'plan only; no Cloudflare resources were created or changed',
|
|
58
|
+
bootstrapWarning: bootstrapWarningFor(target),
|
|
59
|
+
migrationPolicy: MIGRATION_POLICY_NOTES
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
return deployOrUpgrade(input, target, runtime, stateStore, existing, remote, release, plan);
|
|
63
|
+
}
|
|
64
|
+
async function adopt(input, target, runtime, stateStore, existing, remote) {
|
|
65
|
+
if (!remote.d1) {
|
|
66
|
+
throw conflict(`adopt requires an existing D1 database matching --d1 ${target.d1}`);
|
|
67
|
+
}
|
|
68
|
+
if (!remote.r2Exists) {
|
|
69
|
+
throw conflict(`adopt requires an existing R2 bucket matching --r2 ${target.r2}`);
|
|
70
|
+
}
|
|
71
|
+
if (!remote.workerExists) {
|
|
72
|
+
throw conflict(`adopt requires an existing Worker matching --worker-name ${target.workerName}`);
|
|
73
|
+
}
|
|
74
|
+
const state = adoptState(input, target, existing, remote, runtime.now());
|
|
75
|
+
await stateStore.save(state);
|
|
76
|
+
return {
|
|
77
|
+
exitCode: 0,
|
|
78
|
+
command: 'adopt',
|
|
79
|
+
provider: 'cloudflare',
|
|
80
|
+
plan: [
|
|
81
|
+
{
|
|
82
|
+
id: 'adopt',
|
|
83
|
+
summary: `record Worker ${target.workerName}, D1 ${remote.d1.name} (${remote.d1.uuid}), R2 ${target.r2}`,
|
|
84
|
+
mutating: true
|
|
85
|
+
}
|
|
86
|
+
],
|
|
87
|
+
mutations: ['state'],
|
|
88
|
+
statePath: stateStore.path,
|
|
89
|
+
missingSecrets: missingSecrets(remote.secretNames, [...REQUIRED_WORKER_SECRETS]),
|
|
90
|
+
drift: [],
|
|
91
|
+
message: 'recorded existing Cloudflare resources in local state; no resources were created or deleted',
|
|
92
|
+
bootstrapWarning: bootstrapWarningFor(target),
|
|
93
|
+
migrationPolicy: MIGRATION_POLICY_NOTES
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
async function deployOrUpgrade(input, target, runtime, stateStore, existing, remote, release, plan) {
|
|
97
|
+
const mutations = [];
|
|
98
|
+
const current = { ...remote };
|
|
99
|
+
const initialManagedDeploy = input.command === 'deploy' && existing === undefined;
|
|
100
|
+
assertNoTakeover(input, existing, current);
|
|
101
|
+
assertInitialManagedDeployVars(target, release, initialManagedDeploy);
|
|
102
|
+
if (input.command === 'deploy' && !initialManagedDeploy) {
|
|
103
|
+
assertValidBootstrapOwnerEmail(target.bootstrapOwnerEmail, true);
|
|
104
|
+
}
|
|
105
|
+
if (input.command === 'upgrade') {
|
|
106
|
+
assertUpgradeBootstrapOwnerEmail(target);
|
|
107
|
+
}
|
|
108
|
+
if (input.command === 'upgrade') {
|
|
109
|
+
if (!current.d1 || !current.r2Exists || !current.workerExists) {
|
|
110
|
+
throw conflict('upgrade requires existing Worker, D1, and R2; use deploy to create missing resources or adopt to record them');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
const requiredSecrets = uniqueSecrets(release.manifest.requiredSecrets);
|
|
114
|
+
const missing = missingSecrets(current.secretNames, requiredSecrets);
|
|
115
|
+
if (missing.length > 0) {
|
|
116
|
+
const where = current.workerExists
|
|
117
|
+
? `missing required Worker secrets: ${missing.join(', ')}`
|
|
118
|
+
: `Worker ${target.workerName} does not exist yet, so these required secrets cannot be verified: ${missing.join(', ')}. This command stopped before creating Cloudflare resources or uploading a Worker`;
|
|
119
|
+
throw preflight(`${where}. Set them with wrangler secret put <NAME> --name ${target.workerName} (interactive stdin) or documented environment/stdin channels, then re-run. Secrets are never accepted on argv.`);
|
|
120
|
+
}
|
|
121
|
+
if (!current.d1) {
|
|
122
|
+
if (input.command !== 'deploy' || existing) {
|
|
123
|
+
throw conflict(`D1 ${target.d1} does not exist`);
|
|
124
|
+
}
|
|
125
|
+
if (isUuid(target.d1)) {
|
|
126
|
+
throw conflict(`D1 id ${target.d1} was not found; pass a D1 name to create it, or adopt an existing database`);
|
|
127
|
+
}
|
|
128
|
+
current.d1 = await runtime.wrangler.createD1(target.d1);
|
|
129
|
+
mutations.push('create-d1');
|
|
130
|
+
}
|
|
131
|
+
if (!current.r2Exists) {
|
|
132
|
+
if (input.command !== 'deploy' || existing) {
|
|
133
|
+
throw conflict(`R2 bucket ${target.r2} does not exist`);
|
|
134
|
+
}
|
|
135
|
+
await runtime.wrangler.createR2(target.r2);
|
|
136
|
+
current.r2Exists = true;
|
|
137
|
+
mutations.push('create-r2');
|
|
138
|
+
}
|
|
139
|
+
const workDir = await runtime.fs.mkdtemp(join(runtime.fs.tmpdir(), 'create-signkit-'));
|
|
140
|
+
let backupPath;
|
|
141
|
+
try {
|
|
142
|
+
const bundleBytes = await runtime.releases.downloadBundle(release);
|
|
143
|
+
const extracted = await runtime.extractor.extract(bundleBytes, workDir, release.manifest);
|
|
144
|
+
const generatedVars = generatedWorkerVars(target, initialManagedDeploy);
|
|
145
|
+
assertRequiredVarsPresent(release.manifest.requiredVars, generatedVars, initialManagedDeploy);
|
|
146
|
+
await writeGeneratedConfig(runtime.fs, extracted, target, current, release, generatedVars);
|
|
147
|
+
if (!current.d1) {
|
|
148
|
+
throw generic('D1 id is required before exporting a backup');
|
|
149
|
+
}
|
|
150
|
+
backupPath = await persistD1Backup(runtime, stateStore.path, current.d1.name);
|
|
151
|
+
mutations.push('d1-export');
|
|
152
|
+
const migrationOptions = {
|
|
153
|
+
cwd: extracted.root,
|
|
154
|
+
configPath: extracted.configPath,
|
|
155
|
+
database: current.d1.name
|
|
156
|
+
};
|
|
157
|
+
const migrations = await runtime.wrangler.listMigrations(migrationOptions);
|
|
158
|
+
const pending = [...migrations.pending];
|
|
159
|
+
if (pending.length > 0) {
|
|
160
|
+
await runtime.wrangler.applyMigrations(migrationOptions);
|
|
161
|
+
mutations.push('d1-migrations');
|
|
162
|
+
const remaining = await runtime.wrangler.listMigrations(migrationOptions);
|
|
163
|
+
if (remaining.pending.length > 0) {
|
|
164
|
+
throw generic(`D1 migrations remain pending after apply: ${remaining.pending.join(', ')}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
const newlyApplied = pending;
|
|
168
|
+
const appliedMigrations = unionAppliedMigrations(existing?.appliedMigrations, newlyApplied);
|
|
169
|
+
const previousVersionId = existing?.lastWorkerVersionId ?? current.versions[0]?.id;
|
|
170
|
+
const deployOptions = {
|
|
171
|
+
cwd: extracted.root,
|
|
172
|
+
configPath: extracted.configPath,
|
|
173
|
+
workerName: target.workerName,
|
|
174
|
+
domain: target.domain,
|
|
175
|
+
keepVars: true,
|
|
176
|
+
noBundle: true
|
|
177
|
+
};
|
|
178
|
+
const uploaded = assertSuccessfulUpload(input.command === 'upgrade'
|
|
179
|
+
? await runtime.wrangler.uploadVersion(deployOptions)
|
|
180
|
+
: await runtime.wrangler.deploy(deployOptions));
|
|
181
|
+
assertNewWorkerVersion(uploaded.workerVersionId, previousVersionId);
|
|
182
|
+
mutations.push(input.command === 'upgrade' ? 'versions-upload' : 'deploy');
|
|
183
|
+
if (input.command === 'upgrade') {
|
|
184
|
+
await runtime.wrangler.deployVersion(target.workerName, uploaded.workerVersionId);
|
|
185
|
+
mutations.push('versions-deploy');
|
|
186
|
+
}
|
|
187
|
+
const workerUrl = resolveProductionSmokeOrigin(target, uploaded, target.workerName);
|
|
188
|
+
const attachedCustomDomainThisDeploy = input.command === 'deploy' && Boolean(deployOptions.domain);
|
|
189
|
+
const fallbackUrl = attachedCustomDomainThisDeploy
|
|
190
|
+
? selectProductionWorkersDevOrigin(uploaded.stdout, target.workerName)
|
|
191
|
+
: undefined;
|
|
192
|
+
let rollback;
|
|
193
|
+
if (!workerUrl) {
|
|
194
|
+
rollback = await maybeRollback(runtime.wrangler, target.workerName, previousVersionId, 'HTTPS smoke check skipped: no production origin is known');
|
|
195
|
+
const state = nextState(input, target, existing, current, release, runtime.now(), {
|
|
196
|
+
lastWorkerVersionId: rollback.performed ? previousVersionId : uploaded.workerVersionId,
|
|
197
|
+
previousWorkerVersionId: previousVersionId,
|
|
198
|
+
appliedMigrations,
|
|
199
|
+
lastD1BackupPath: backupPath
|
|
200
|
+
});
|
|
201
|
+
await stateStore.save(state);
|
|
202
|
+
return {
|
|
203
|
+
exitCode: 1,
|
|
204
|
+
command: input.command,
|
|
205
|
+
provider: 'cloudflare',
|
|
206
|
+
version: release.tag,
|
|
207
|
+
commit: release.commit,
|
|
208
|
+
plan,
|
|
209
|
+
mutations,
|
|
210
|
+
statePath: stateStore.path,
|
|
211
|
+
workerUrl: undefined,
|
|
212
|
+
d1BackupPath: backupPath,
|
|
213
|
+
missingSecrets: [],
|
|
214
|
+
drift: [],
|
|
215
|
+
rollback,
|
|
216
|
+
message: `HTTPS smoke check skipped: no production origin is known; refusing to use a version-preview URL as production verification. Pass --public-origin or --domain. ${rollback.guidance}`,
|
|
217
|
+
bootstrapWarning: bootstrapWarningFor(target),
|
|
218
|
+
migrationPolicy: MIGRATION_POLICY_NOTES
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
const smoke = await smokeCheck({
|
|
222
|
+
url: workerUrl,
|
|
223
|
+
fallbackUrl: fallbackUrl && fallbackUrl !== workerUrl ? fallbackUrl : undefined,
|
|
224
|
+
http: runtime.http,
|
|
225
|
+
attempts: runtime.smokeAttempts,
|
|
226
|
+
backoffMs: runtime.smokeBackoffMs,
|
|
227
|
+
timeoutMs: runtime.smokeTimeoutMs,
|
|
228
|
+
sleep: runtime.sleep
|
|
229
|
+
});
|
|
230
|
+
if (!smoke.ok) {
|
|
231
|
+
rollback = await maybeRollback(runtime.wrangler, target.workerName, previousVersionId, smoke.detail);
|
|
232
|
+
const state = nextState(input, target, existing, current, release, runtime.now(), {
|
|
233
|
+
lastWorkerVersionId: rollback.performed ? previousVersionId : uploaded.workerVersionId,
|
|
234
|
+
previousWorkerVersionId: previousVersionId,
|
|
235
|
+
appliedMigrations,
|
|
236
|
+
lastD1BackupPath: backupPath
|
|
237
|
+
});
|
|
238
|
+
await stateStore.save(state);
|
|
239
|
+
return {
|
|
240
|
+
exitCode: 1,
|
|
241
|
+
command: input.command,
|
|
242
|
+
provider: 'cloudflare',
|
|
243
|
+
version: release.tag,
|
|
244
|
+
commit: release.commit,
|
|
245
|
+
plan,
|
|
246
|
+
mutations,
|
|
247
|
+
statePath: stateStore.path,
|
|
248
|
+
workerUrl,
|
|
249
|
+
d1BackupPath: backupPath,
|
|
250
|
+
missingSecrets: [],
|
|
251
|
+
drift: [],
|
|
252
|
+
rollback,
|
|
253
|
+
message: `HTTPS smoke check failed: ${smoke.detail}. ${rollback.guidance}`,
|
|
254
|
+
bootstrapWarning: bootstrapWarningFor(target),
|
|
255
|
+
migrationPolicy: MIGRATION_POLICY_NOTES
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
const state = nextState(input, target, existing, current, release, runtime.now(), {
|
|
259
|
+
lastWorkerVersionId: uploaded.workerVersionId,
|
|
260
|
+
previousWorkerVersionId: previousVersionId,
|
|
261
|
+
appliedMigrations,
|
|
262
|
+
lastD1BackupPath: backupPath
|
|
263
|
+
});
|
|
264
|
+
await stateStore.save(state);
|
|
265
|
+
return {
|
|
266
|
+
exitCode: 0,
|
|
267
|
+
command: input.command,
|
|
268
|
+
provider: 'cloudflare',
|
|
269
|
+
version: release.tag,
|
|
270
|
+
commit: release.commit,
|
|
271
|
+
plan,
|
|
272
|
+
mutations,
|
|
273
|
+
statePath: stateStore.path,
|
|
274
|
+
workerUrl,
|
|
275
|
+
d1BackupPath: backupPath,
|
|
276
|
+
missingSecrets: [],
|
|
277
|
+
drift: [],
|
|
278
|
+
message: input.command === 'upgrade'
|
|
279
|
+
? `upgraded ${target.workerName} to ${release.tag}`
|
|
280
|
+
: `deployed ${target.workerName} at ${release.tag}`,
|
|
281
|
+
bootstrapWarning: bootstrapWarningFor(target),
|
|
282
|
+
migrationPolicy: MIGRATION_POLICY_NOTES
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
await runtime.fs.rm(workDir);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
async function persistD1Backup(runtime, statePath, d1Name) {
|
|
290
|
+
const dir = resolveBackupsDir(statePath);
|
|
291
|
+
await runtime.fs.mkdir(dir, { mode: BACKUP_DIR_MODE });
|
|
292
|
+
await runtime.fs.chmod(dir, BACKUP_DIR_MODE);
|
|
293
|
+
const backupPath = await uniqueD1BackupPath(runtime, dir, d1Name);
|
|
294
|
+
await runtime.wrangler.exportD1(d1Name, backupPath);
|
|
295
|
+
await runtime.fs.chmod(backupPath, BACKUP_FILE_MODE);
|
|
296
|
+
return backupPath;
|
|
297
|
+
}
|
|
298
|
+
async function uniqueD1BackupPath(runtime, dir, d1Name) {
|
|
299
|
+
const base = d1BackupFileName(d1Name, runtime.now());
|
|
300
|
+
let candidate = join(dir, base);
|
|
301
|
+
let suffix = 2;
|
|
302
|
+
while (await runtime.fs.exists(candidate)) {
|
|
303
|
+
candidate = join(dir, base.replace(/\.sql$/, `-${suffix}.sql`));
|
|
304
|
+
suffix += 1;
|
|
305
|
+
}
|
|
306
|
+
return candidate;
|
|
307
|
+
}
|
|
308
|
+
async function maybeRollback(wrangler, workerName, previousVersionId, smokeDetail) {
|
|
309
|
+
const guidanceBase = 'D1 was not rolled back and cannot be rolled back by rolling back the Worker. Restore SQL with D1 Time Travel if needed. Worker rollback cannot undo applied migrations.';
|
|
310
|
+
if (!previousVersionId) {
|
|
311
|
+
return {
|
|
312
|
+
attempted: false,
|
|
313
|
+
performed: false,
|
|
314
|
+
workerRolledBack: false,
|
|
315
|
+
d1RolledBack: false,
|
|
316
|
+
guidance: `No previous Worker version ID is recorded, so Worker rollback was not performed. ${guidanceBase} Smoke failure: ${smokeDetail}`
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
try {
|
|
320
|
+
await wrangler.rollback(workerName, previousVersionId, `${PACKAGE_NAME} smoke check failed; restoring previous Worker version`);
|
|
321
|
+
return {
|
|
322
|
+
attempted: true,
|
|
323
|
+
performed: true,
|
|
324
|
+
workerRolledBack: true,
|
|
325
|
+
d1RolledBack: false,
|
|
326
|
+
previousWorkerVersionId: previousVersionId,
|
|
327
|
+
guidance: `Rolled the Worker back to version ${previousVersionId}. ${guidanceBase}`
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
catch (error) {
|
|
331
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
332
|
+
return {
|
|
333
|
+
attempted: true,
|
|
334
|
+
performed: false,
|
|
335
|
+
workerRolledBack: false,
|
|
336
|
+
d1RolledBack: false,
|
|
337
|
+
previousWorkerVersionId: previousVersionId,
|
|
338
|
+
guidance: `Worker rollback to ${previousVersionId} was attempted and failed (${detail}). ${guidanceBase}`
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async function inspectRemote(wrangler, target) {
|
|
343
|
+
const [databases, r2Exists, versions, secretNames] = await Promise.all([
|
|
344
|
+
wrangler.listD1(),
|
|
345
|
+
wrangler.r2Exists(target.r2),
|
|
346
|
+
wrangler.listVersions(target.workerName),
|
|
347
|
+
wrangler.listSecrets(target.workerName)
|
|
348
|
+
]);
|
|
349
|
+
const d1 = databases.find((database) => database.name === target.d1 || database.uuid === target.d1);
|
|
350
|
+
return {
|
|
351
|
+
d1,
|
|
352
|
+
r2Exists,
|
|
353
|
+
workerExists: versions.length > 0 || secretNames.length > 0,
|
|
354
|
+
secretNames,
|
|
355
|
+
versions: sortWorkerVersions(versions)
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function detectDrift(existing, target, remote) {
|
|
359
|
+
if (!existing) {
|
|
360
|
+
return [];
|
|
361
|
+
}
|
|
362
|
+
const drift = [];
|
|
363
|
+
if (existing.accountId !== target.accountId) {
|
|
364
|
+
drift.push('account-id mismatch');
|
|
365
|
+
}
|
|
366
|
+
if (existing.workerName !== target.workerName) {
|
|
367
|
+
drift.push(`worker-name local=${existing.workerName} requested=${target.workerName}`);
|
|
368
|
+
}
|
|
369
|
+
if (existing.d1.name !== target.d1 && existing.d1.id !== target.d1) {
|
|
370
|
+
drift.push(`d1 local=${existing.d1.name}/${existing.d1.id} requested=${target.d1}`);
|
|
371
|
+
}
|
|
372
|
+
if (existing.r2.name !== target.r2) {
|
|
373
|
+
drift.push(`r2 local=${existing.r2.name} requested=${target.r2}`);
|
|
374
|
+
}
|
|
375
|
+
if (remote.d1 && existing.d1.id && existing.d1.id !== remote.d1.uuid) {
|
|
376
|
+
drift.push(`d1 id local=${existing.d1.id} remote=${remote.d1.uuid}`);
|
|
377
|
+
}
|
|
378
|
+
if (!remote.d1) {
|
|
379
|
+
drift.push(`d1 ${existing.d1.name} is missing remotely`);
|
|
380
|
+
}
|
|
381
|
+
if (!remote.r2Exists) {
|
|
382
|
+
drift.push(`r2 ${existing.r2.name} is missing remotely`);
|
|
383
|
+
}
|
|
384
|
+
if (remote.workerExists === false) {
|
|
385
|
+
drift.push(`worker ${existing.workerName} is missing remotely`);
|
|
386
|
+
}
|
|
387
|
+
return drift;
|
|
388
|
+
}
|
|
389
|
+
function buildPlan(input, existing, target, remote, release) {
|
|
390
|
+
const steps = [
|
|
391
|
+
{
|
|
392
|
+
id: 'resolve-version',
|
|
393
|
+
summary: `use GitHub release ${release.tag} (${release.commit ?? 'commit unknown'})`,
|
|
394
|
+
mutating: false
|
|
395
|
+
},
|
|
396
|
+
{
|
|
397
|
+
id: 'validate-manifest',
|
|
398
|
+
summary: `validate ${release.manifest.bundle.assetName} size ${release.manifest.bundle.size} sha256 ${release.manifest.bundle.sha256}`,
|
|
399
|
+
mutating: false
|
|
400
|
+
}
|
|
401
|
+
];
|
|
402
|
+
if (input.command === 'adopt') {
|
|
403
|
+
return [
|
|
404
|
+
{
|
|
405
|
+
id: 'adopt',
|
|
406
|
+
summary: `record Worker ${target.workerName}, D1 ${target.d1}, R2 ${target.r2} in local state`,
|
|
407
|
+
mutating: true
|
|
408
|
+
}
|
|
409
|
+
];
|
|
410
|
+
}
|
|
411
|
+
if (!remote.d1 && !existing) {
|
|
412
|
+
steps.push({
|
|
413
|
+
id: 'create-d1',
|
|
414
|
+
summary: `create D1 ${d1Name(target, remote)}`,
|
|
415
|
+
mutating: true
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
if (!remote.r2Exists && !existing) {
|
|
419
|
+
steps.push({ id: 'create-r2', summary: `create R2 ${target.r2}`, mutating: true });
|
|
420
|
+
}
|
|
421
|
+
steps.push({
|
|
422
|
+
id: 'd1-export',
|
|
423
|
+
summary: `export D1 ${target.d1} to a retained local backup`,
|
|
424
|
+
mutating: true
|
|
425
|
+
});
|
|
426
|
+
steps.push({
|
|
427
|
+
id: 'd1-migrations',
|
|
428
|
+
summary: 'apply pending D1 migrations only',
|
|
429
|
+
mutating: true
|
|
430
|
+
});
|
|
431
|
+
steps.push({
|
|
432
|
+
id: input.command === 'upgrade' ? 'versions-upload' : 'deploy',
|
|
433
|
+
summary: input.command === 'upgrade'
|
|
434
|
+
? `upload Worker version for ${target.workerName} with --keep-vars`
|
|
435
|
+
: `deploy Worker ${target.workerName} with --keep-vars`,
|
|
436
|
+
mutating: true
|
|
437
|
+
});
|
|
438
|
+
steps.push({
|
|
439
|
+
id: 'smoke',
|
|
440
|
+
summary: `HTTPS smoke check ${target.publicOrigin ?? 'workers.dev URL'}/api/v1/system/capabilities`,
|
|
441
|
+
mutating: false
|
|
442
|
+
});
|
|
443
|
+
steps.push({
|
|
444
|
+
id: 'state',
|
|
445
|
+
summary: `write local state at the configured path (no secrets)`,
|
|
446
|
+
mutating: true
|
|
447
|
+
});
|
|
448
|
+
return steps;
|
|
449
|
+
}
|
|
450
|
+
function nextState(input, target, existing, remote, release, now, extra) {
|
|
451
|
+
if (!remote.d1) {
|
|
452
|
+
throw generic('cannot write state without a D1 id');
|
|
453
|
+
}
|
|
454
|
+
return {
|
|
455
|
+
schemaVersion: 1,
|
|
456
|
+
provider: 'cloudflare',
|
|
457
|
+
accountId: target.accountId,
|
|
458
|
+
workerName: target.workerName,
|
|
459
|
+
d1: { name: remote.d1.name, id: remote.d1.uuid },
|
|
460
|
+
r2: { name: target.r2 },
|
|
461
|
+
domain: target.domain ?? existing?.domain,
|
|
462
|
+
publicOrigin: target.publicOrigin ?? existing?.publicOrigin,
|
|
463
|
+
d6eAuthBaseUrl: target.d6eAuthBaseUrl,
|
|
464
|
+
emailFrom: target.emailFrom ?? existing?.emailFrom,
|
|
465
|
+
emailFromName: target.emailFromName,
|
|
466
|
+
bootstrapOwnerEmail: target.bootstrapOwnerEmail ?? existing?.bootstrapOwnerEmail,
|
|
467
|
+
lastD1BackupPath: extra.lastD1BackupPath ?? existing?.lastD1BackupPath,
|
|
468
|
+
channel: input.channel,
|
|
469
|
+
version: release.tag,
|
|
470
|
+
commit: release.commit,
|
|
471
|
+
lastCommand: input.command,
|
|
472
|
+
updatedAt: now.toISOString(),
|
|
473
|
+
lastWorkerVersionId: extra.lastWorkerVersionId ?? existing?.lastWorkerVersionId,
|
|
474
|
+
previousWorkerVersionId: extra.previousWorkerVersionId ?? existing?.previousWorkerVersionId,
|
|
475
|
+
appliedMigrations: extra.appliedMigrations ?? existing?.appliedMigrations,
|
|
476
|
+
adopted: extra.adopted ?? existing?.adopted
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
function generatedWorkerVars(target, initialManagedDeploy) {
|
|
480
|
+
const vars = {
|
|
481
|
+
SIGNKIT_MAIL_PROVIDER: 'cloudflare'
|
|
482
|
+
};
|
|
483
|
+
if (target.publicOrigin) {
|
|
484
|
+
vars.SIGNKIT_PUBLIC_ORIGIN = target.publicOrigin;
|
|
485
|
+
}
|
|
486
|
+
if (initialManagedDeploy || !target.appliedDefaults.d6eAuthBaseUrl) {
|
|
487
|
+
vars.D6E_AUTH_BASE_URL = target.d6eAuthBaseUrl;
|
|
488
|
+
}
|
|
489
|
+
if (target.emailFrom) {
|
|
490
|
+
vars.SIGNKIT_EMAIL_FROM = target.emailFrom;
|
|
491
|
+
}
|
|
492
|
+
if (initialManagedDeploy || !target.appliedDefaults.emailFromName) {
|
|
493
|
+
vars.SIGNKIT_EMAIL_FROM_NAME = target.emailFromName;
|
|
494
|
+
}
|
|
495
|
+
if (target.bootstrapOwnerEmail) {
|
|
496
|
+
vars.SIGNKIT_BOOTSTRAP_OWNER_EMAIL = target.bootstrapOwnerEmail;
|
|
497
|
+
}
|
|
498
|
+
return vars;
|
|
499
|
+
}
|
|
500
|
+
function assertInitialManagedDeployVars(target, release, initialManagedDeploy) {
|
|
501
|
+
if (!initialManagedDeploy) {
|
|
502
|
+
return;
|
|
503
|
+
}
|
|
504
|
+
if (!target.emailFrom) {
|
|
505
|
+
throw preflight('--email-from is required for the initial managed deploy (SIGNKIT_EMAIL_FROM). Secrets are never accepted on argv.');
|
|
506
|
+
}
|
|
507
|
+
if (!target.publicOrigin) {
|
|
508
|
+
throw preflight('--public-origin or --domain is required for the initial managed deploy (SIGNKIT_PUBLIC_ORIGIN)');
|
|
509
|
+
}
|
|
510
|
+
assertValidBootstrapOwnerEmail(target.bootstrapOwnerEmail, false);
|
|
511
|
+
assertRequiredVarsPresent(release.manifest.requiredVars, generatedWorkerVars(target, true), true);
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* A fresh uninitialized deployment must not be claimable without a bootstrap
|
|
515
|
+
* owner: deploy and upgrade both require the effective
|
|
516
|
+
* `--bootstrap-owner-email` (explicit flag or inherited state). State files
|
|
517
|
+
* written before this requirement stay loadable — the field is optional in
|
|
518
|
+
* the schema — but upgrading or redeploying with one requires passing the
|
|
519
|
+
* flag once, after which the value is recorded in state and inherited.
|
|
520
|
+
* Already-bootstrapped instances are unaffected by the value itself since
|
|
521
|
+
* bootstrap never runs again. Errors name the flag, never the address.
|
|
522
|
+
*/
|
|
523
|
+
function assertUpgradeBootstrapOwnerEmail(target) {
|
|
524
|
+
assertValidBootstrapOwnerEmail(target.bootstrapOwnerEmail, true);
|
|
525
|
+
}
|
|
526
|
+
function assertValidBootstrapOwnerEmail(value, hasState) {
|
|
527
|
+
if (value === undefined || !isValidEmailAddress(value)) {
|
|
528
|
+
throw preflight(hasState
|
|
529
|
+
? '--bootstrap-owner-email is required for deploy/upgrade (SIGNKIT_BOOTSTRAP_OWNER_EMAIL): no valid address is recorded in state, so pass the flag once to record it'
|
|
530
|
+
: '--bootstrap-owner-email is required for the initial managed deploy (SIGNKIT_BOOTSTRAP_OWNER_EMAIL): an uninitialized instance fails closed without it. Secrets are never accepted on argv.');
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
function assertRequiredVarsPresent(required, generated, initialManagedDeploy) {
|
|
534
|
+
if (!initialManagedDeploy) {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
const missing = required.filter((name) => !generated[name]);
|
|
538
|
+
if (missing.length > 0) {
|
|
539
|
+
throw preflight(`generated initial-managed-deploy Worker config is missing required vars: ${missing.join(', ')}`);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function assertCloudflareState(existing) {
|
|
543
|
+
if (existing && existing.provider !== 'cloudflare') {
|
|
544
|
+
throw conflict('deployment state provider is not cloudflare; refusing to inherit or mutate it');
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
function adoptState(input, target, existing, remote, now) {
|
|
548
|
+
if (!remote.d1) {
|
|
549
|
+
throw generic('cannot write state without a D1 id');
|
|
550
|
+
}
|
|
551
|
+
const retarget = existing !== undefined &&
|
|
552
|
+
(existing.workerName !== target.workerName ||
|
|
553
|
+
existing.d1.id !== remote.d1.uuid ||
|
|
554
|
+
existing.d1.name !== remote.d1.name ||
|
|
555
|
+
existing.r2.name !== target.r2);
|
|
556
|
+
return {
|
|
557
|
+
schemaVersion: 1,
|
|
558
|
+
provider: 'cloudflare',
|
|
559
|
+
accountId: target.accountId,
|
|
560
|
+
workerName: target.workerName,
|
|
561
|
+
d1: { name: remote.d1.name, id: remote.d1.uuid },
|
|
562
|
+
r2: { name: target.r2 },
|
|
563
|
+
domain: target.domain ?? existing?.domain,
|
|
564
|
+
publicOrigin: target.publicOrigin ?? existing?.publicOrigin,
|
|
565
|
+
d6eAuthBaseUrl: target.d6eAuthBaseUrl,
|
|
566
|
+
emailFrom: target.emailFrom ?? existing?.emailFrom,
|
|
567
|
+
emailFromName: target.emailFromName,
|
|
568
|
+
bootstrapOwnerEmail: target.bootstrapOwnerEmail ?? existing?.bootstrapOwnerEmail,
|
|
569
|
+
lastD1BackupPath: retarget ? undefined : existing?.lastD1BackupPath,
|
|
570
|
+
channel: input.channel,
|
|
571
|
+
version: retarget ? undefined : existing?.version,
|
|
572
|
+
commit: retarget ? undefined : existing?.commit,
|
|
573
|
+
lastCommand: 'adopt',
|
|
574
|
+
updatedAt: now.toISOString(),
|
|
575
|
+
lastWorkerVersionId: retarget ? undefined : existing?.lastWorkerVersionId,
|
|
576
|
+
previousWorkerVersionId: retarget ? undefined : existing?.previousWorkerVersionId,
|
|
577
|
+
appliedMigrations: retarget ? undefined : existing?.appliedMigrations,
|
|
578
|
+
adopted: true
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function assertNoTakeover(input, existing, remote) {
|
|
582
|
+
if (existing) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
if (input.command === 'deploy' && remote.versions.length > 0) {
|
|
586
|
+
throw conflict('this Worker already has published versions; adopt the existing deployment before deploy rather than taking it over');
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function resolveProductionSmokeOrigin(target, uploaded, workerName) {
|
|
590
|
+
if (target.publicOrigin) {
|
|
591
|
+
return target.publicOrigin;
|
|
592
|
+
}
|
|
593
|
+
return (selectProductionWorkersDevOrigin(uploaded.stdout, workerName) ??
|
|
594
|
+
(uploaded.workerUrl
|
|
595
|
+
? selectProductionWorkersDevOrigin(uploaded.workerUrl, workerName)
|
|
596
|
+
: undefined));
|
|
597
|
+
}
|
|
598
|
+
function assertUpgradeRouteUnchanged(input, existing) {
|
|
599
|
+
if (input.command !== 'upgrade' || !existing) {
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
602
|
+
if (input.overrides.domain && (input.domain ?? '') !== (existing.domain ?? '')) {
|
|
603
|
+
throw conflict('upgrade refuses an explicit --domain that differs from stored state; use deploy or adopt to change routing');
|
|
604
|
+
}
|
|
605
|
+
if (input.overrides.publicOrigin &&
|
|
606
|
+
(input.publicOrigin ?? '') !== (existing.publicOrigin ?? '')) {
|
|
607
|
+
throw conflict('upgrade refuses an explicit --public-origin that differs from stored state; use deploy or adopt to change routing');
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function assertNewWorkerVersion(workerVersionId, previousVersionId) {
|
|
611
|
+
if (!workerVersionId) {
|
|
612
|
+
throw generic('Wrangler did not report a labelled Worker Version ID; refusing to treat another UUID as the Worker version');
|
|
613
|
+
}
|
|
614
|
+
if (previousVersionId && workerVersionId === previousVersionId) {
|
|
615
|
+
throw generic('Wrangler reported the same Worker Version ID as the previous version; refusing to treat this as a successful upload');
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function unionAppliedMigrations(existing, newlyApplied) {
|
|
619
|
+
const seen = new Set();
|
|
620
|
+
const result = [];
|
|
621
|
+
for (const name of [...(existing ?? []), ...newlyApplied]) {
|
|
622
|
+
if (seen.has(name))
|
|
623
|
+
continue;
|
|
624
|
+
seen.add(name);
|
|
625
|
+
result.push(name);
|
|
626
|
+
}
|
|
627
|
+
return result;
|
|
628
|
+
}
|
|
629
|
+
function missingSecrets(present, required) {
|
|
630
|
+
const have = new Set(present);
|
|
631
|
+
return required.filter((name) => !have.has(name));
|
|
632
|
+
}
|
|
633
|
+
function uniqueSecrets(names) {
|
|
634
|
+
return [...new Set([...REQUIRED_WORKER_SECRETS, ...names])];
|
|
635
|
+
}
|
|
636
|
+
function d1Name(target, remote) {
|
|
637
|
+
return remote.d1?.name ?? (isUuid(target.d1) ? target.d1 : target.d1);
|
|
638
|
+
}
|
|
639
|
+
function isUuid(value) {
|
|
640
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
|
|
641
|
+
}
|
|
642
|
+
function posixPath(root, absolute, label) {
|
|
643
|
+
try {
|
|
644
|
+
return posixRelativeToRoot(root, absolute, label);
|
|
645
|
+
}
|
|
646
|
+
catch (error) {
|
|
647
|
+
throw generic(error instanceof Error ? error.message : String(error));
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function resolvePath(runtime, input) {
|
|
651
|
+
return resolveStatePath(runtime.fs, runtime.env, input.statePath);
|
|
652
|
+
}
|
|
653
|
+
async function writeGeneratedConfig(fs, extracted, target, remote, release, vars) {
|
|
654
|
+
if (!remote.d1) {
|
|
655
|
+
throw generic('D1 id is required before generating wrangler.jsonc');
|
|
656
|
+
}
|
|
657
|
+
const contents = renderWranglerConfig({
|
|
658
|
+
workerName: target.workerName,
|
|
659
|
+
d1Name: remote.d1.name,
|
|
660
|
+
d1Id: remote.d1.uuid,
|
|
661
|
+
r2Name: target.r2,
|
|
662
|
+
domain: target.domain,
|
|
663
|
+
publicOrigin: vars.SIGNKIT_PUBLIC_ORIGIN,
|
|
664
|
+
d6eAuthBaseUrl: vars.D6E_AUTH_BASE_URL,
|
|
665
|
+
emailFrom: vars.SIGNKIT_EMAIL_FROM,
|
|
666
|
+
emailFromName: vars.SIGNKIT_EMAIL_FROM_NAME,
|
|
667
|
+
bootstrapOwnerEmail: vars.SIGNKIT_BOOTSTRAP_OWNER_EMAIL,
|
|
668
|
+
manifest: release.manifest,
|
|
669
|
+
main: posixPath(extracted.root, extracted.main, 'generated Worker main'),
|
|
670
|
+
assetsDirectory: posixPath(extracted.root, extracted.assetsDirectory, 'generated assets directory'),
|
|
671
|
+
migrationsDirectory: posixPath(extracted.root, extracted.migrationsDirectory, 'generated migrations directory')
|
|
672
|
+
});
|
|
673
|
+
await fs.writeFile(extracted.configPath, contents);
|
|
674
|
+
}
|
|
675
|
+
//# sourceMappingURL=reconciler.js.map
|