Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions pm-stack/private-laptop-bridge/smoke-upload.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ function sanitizedBuildMetadata(buildConfig) {
? Object.fromEntries(Object.entries(value).map(([key, item]) => [key, item === null ? 'null' : Array.isArray(item) ? 'array' : typeof item]))
: typeof value;
const context = buildConfig?.buildContext;
const mode = context?.mode;
return JSON.stringify({
rootKeys: buildConfig && typeof buildConfig === 'object' && !Array.isArray(buildConfig) ? Object.keys(buildConfig).sort() : [],
rootTypes: types(buildConfig),
Expand All @@ -171,15 +170,20 @@ function sanitizedBuildMetadata(buildConfig) {
buildContextKeys: context && typeof context === 'object' && !Array.isArray(context) ? Object.keys(context).sort() : [],
buildContextTypes: types(context),
isPreview: typeof context?.isPreview === 'boolean' ? context.isPreview : '<non-boolean>',
mode: mode === '' ? '<empty>' : mode === 'production' || mode === 'development' ? mode : '<other>',
mode: context && Object.hasOwn(context, 'mode') ? (context.mode === '' ? '<empty>' : '<other>') : '<absent>',
});
}

export function assertPrebuiltSmokePayload({ buildConfig, workerConfig, workerNames, bundleFiles }) {
const exactKeys = (value, expected) => value && typeof value === 'object' && !Array.isArray(value) && same(Object.keys(value).sort(), [...expected].sort());
const context = buildConfig?.buildContext;
// Pinned cf emits no mode locally when CF_MODE is unset, but an empty
// mode in the hosted workflow where CF_MODE is explicitly set to ''.
const knownContext = exactKeys(context, ['isPreview']) ||
(exactKeys(context, ['isPreview', 'mode']) && context.mode === '');
if (!exactKeys(buildConfig, ['producer', 'buildContext']) || !exactKeys(buildConfig.producer, ['name', 'version']) ||
buildConfig.producer.name !== 'wrangler' || buildConfig.producer.version !== '4.166.0' ||
!exactKeys(buildConfig.buildContext, ['isPreview', 'mode']) || buildConfig.buildContext.isPreview !== false || buildConfig.buildContext.mode !== '') {
!knownContext || context.isPreview !== false) {
fail(`prebuilt Build Output producer or context is not the pinned non-preview build; sanitized metadata=${sanitizedBuildMetadata(buildConfig)}`);
}
if (!Array.isArray(workerNames) || workerNames.length !== 1 || workerNames[0] !== 'default') fail('prebuilt Build Output must contain only its default Worker');
Expand Down
29 changes: 28 additions & 1 deletion pm-stack/private-laptop-bridge/smoke-upload.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ function builtSmokeFixture() {
END_MS: { type: 'text', value: '' },
};
return {
buildConfig: { producer: { name: 'wrangler', version: '4.166.0' }, buildContext: { isPreview: false, mode: '' } },
// Unset CF_MODE on a local build omits mode from the pinned output.
buildConfig: { producer: { name: 'wrangler', version: '4.166.0' }, buildContext: { isPreview: false } },
workerNames: ['default'],
bundleFiles: ['smoke.js', 'smoke.js.map'],
workerConfig: {
Expand All @@ -186,6 +187,32 @@ function builtSmokeFixture() {
};
}

test('pinned Build Output accepts only the local and CI non-preview context shapes', () => {
// The hosted workflow sets CF_MODE explicitly to the empty string.
for (const context of [{ isPreview: false }, { isPreview: false, mode: '' }]) {
const fixture = builtSmokeFixture();
fixture.buildConfig.buildContext = context;
assert.equal(assertPrebuiltSmokePayload(fixture), true);
}
for (const context of [
null, [], {}, { mode: '' }, { isPreview: 'false' },
{ isPreview: true }, { isPreview: true, mode: '' },
{ isPreview: false, mode: null }, { isPreview: false, mode: false },
{ isPreview: false, mode: 0 }, { isPreview: false, mode: 'production' },
{ isPreview: false, mode: 'synthetic-secret-value' },
{ isPreview: false, extra: 'synthetic-secret-value' },
{ isPreview: false, mode: '', extra: 'synthetic-secret-value' },
]) {
const invalid = builtSmokeFixture();
invalid.buildConfig.buildContext = context;
assert.throws(() => assertPrebuiltSmokePayload(invalid), error => {
assert.match(error.message, /sanitized metadata=/);
assert.doesNotMatch(error.message, /synthetic-secret-value/);
return true;
});
}
});

test('effective Build Output contract excludes routes, domains, triggers, previews, DOs, and extra executable modules', () => {
const fixture = builtSmokeFixture();
assert.equal(assertPrebuiltSmokePayload(fixture), true);
Expand Down