1+ import { promises as fs } from 'node:fs'
2+ import { dirname , resolve } from 'node:path'
3+
14import { mergeConfigs } from '@netlify/config'
25
36import type { NetlifyConfig } from '../../index.js'
47import { getConfigMutations } from '../../plugins/child/diff.js'
8+ import { DEPLOY_CONFIG_DIST_PATH , FRAMEWORKS_API_SKEW_PROTECTION_PATH } from '../../utils/frameworks_api.js'
59import { CoreStep , CoreStepFunction } from '../types.js'
610
11+ import { loadSkewProtectionConfig } from './skew_protection.js'
712import { filterConfig , loadConfigFile } from './util.js'
813
914// The properties that can be set using this API. Each element represents a
@@ -26,6 +31,30 @@ const ALLOWED_PROPERTIES = [
2631// a special notation where `redirects!` represents "forced redirects", etc.
2732const OVERRIDE_PROPERTIES = new Set ( [ 'redirects!' ] )
2833
34+ // Looks for a skew protection configuration file. If found, the file is loaded
35+ // and validated against the schema, throwing a build error if validation
36+ // fails. If valid, the contents are written to the deploy config file.
37+ const handleSkewProtection = async ( buildDir : string , packagePath ?: string ) => {
38+ const inputPath = resolve ( buildDir , packagePath ?? '' , FRAMEWORKS_API_SKEW_PROTECTION_PATH )
39+ const outputPath = resolve ( buildDir , packagePath ?? '' , DEPLOY_CONFIG_DIST_PATH )
40+
41+ const skewProtectionConfig = await loadSkewProtectionConfig ( inputPath )
42+ if ( ! skewProtectionConfig ) {
43+ return
44+ }
45+
46+ const deployConfig = {
47+ skew_protection : skewProtectionConfig ,
48+ }
49+
50+ try {
51+ await fs . mkdir ( dirname ( outputPath ) , { recursive : true } )
52+ await fs . writeFile ( outputPath , JSON . stringify ( deployConfig ) )
53+ } catch ( error ) {
54+ throw new Error ( 'Failed to process skew protection configuration' , { cause : error } )
55+ }
56+ }
57+
2958const coreStep : CoreStepFunction = async function ( {
3059 buildDir,
3160 netlifyConfig,
@@ -34,6 +63,7 @@ const coreStep: CoreStepFunction = async function ({
3463 // no-op
3564 } ,
3665} ) {
66+ await handleSkewProtection ( buildDir , packagePath )
3767 let config : Partial < NetlifyConfig > | undefined
3868
3969 try {
0 commit comments