@@ -6616,6 +6616,93 @@ describe("experimental_readRawConfig()", () => {
66166616 ) ;
66176617} ) ;
66186618
6619+ describe ( "BOM (Byte Order Marker) handling" , ( ) => {
6620+ runInTempDir ( ) ;
6621+
6622+ it ( "should remove UTF-8 BOM from TOML config files" , ( ) => {
6623+ const configContent = `name = "test-worker"
6624+ compatibility_date = "2022-01-12"` ;
6625+
6626+ fs . writeFileSync (
6627+ "wrangler.toml" ,
6628+ Buffer . concat ( [
6629+ Buffer . from ( [ 0xef , 0xbb , 0xbf ] ) ,
6630+ Buffer . from ( configContent , "utf-8" ) ,
6631+ ] )
6632+ ) ;
6633+
6634+ const config = readConfig ( { config : "wrangler.toml" } ) ;
6635+ expect ( config . name ) . toBe ( "test-worker" ) ;
6636+ expect ( config . compatibility_date ) . toBe ( "2022-01-12" ) ;
6637+ } ) ;
6638+
6639+ it ( "should remove UTF-8 BOM from JSON config files" , ( ) => {
6640+ const configContent = `{
6641+ "name": "test-worker",
6642+ "compatibility_date": "2022-01-12"
6643+ }` ;
6644+
6645+ fs . writeFileSync (
6646+ "wrangler.json" ,
6647+ Buffer . concat ( [
6648+ Buffer . from ( [ 0xef , 0xbb , 0xbf ] ) ,
6649+ Buffer . from ( configContent , "utf-8" ) ,
6650+ ] )
6651+ ) ;
6652+
6653+ const config = readConfig ( { config : "wrangler.json" } ) ;
6654+ expect ( config . name ) . toBe ( "test-worker" ) ;
6655+ expect ( config . compatibility_date ) . toBe ( "2022-01-12" ) ;
6656+ } ) ;
6657+
6658+ it ( "should error on UTF-16 BE BOM" , ( ) => {
6659+ const bomBytes = Buffer . from ( [ 0xfe , 0xff ] ) ;
6660+ const configContent = Buffer . from ( '{"name": "test"}' , "utf-8" ) ;
6661+ fs . writeFileSync ( "wrangler.json" , Buffer . concat ( [ bomBytes , configContent ] ) ) ;
6662+
6663+ expect ( ( ) => readConfig ( { config : "wrangler.json" } ) ) . toThrow (
6664+ "Configuration file contains UTF-16 BE byte order marker"
6665+ ) ;
6666+ } ) ;
6667+
6668+ it ( "should error on UTF-16 LE BOM" , ( ) => {
6669+ const bomBytes = Buffer . from ( [ 0xff , 0xfe ] ) ;
6670+ const configContent = Buffer . from ( '{"name": "test"}' , "utf-8" ) ;
6671+ fs . writeFileSync ( "wrangler.json" , Buffer . concat ( [ bomBytes , configContent ] ) ) ;
6672+
6673+ expect ( ( ) => readConfig ( { config : "wrangler.json" } ) ) . toThrow (
6674+ "Configuration file contains UTF-16 LE byte order marker"
6675+ ) ;
6676+ } ) ;
6677+
6678+ it ( "should error on UTF-32 BE BOM" , ( ) => {
6679+ const bomBytes = Buffer . from ( [ 0x00 , 0x00 , 0xfe , 0xff ] ) ;
6680+ const configContent = Buffer . from ( '{"name": "test"}' , "utf-8" ) ;
6681+ fs . writeFileSync ( "wrangler.json" , Buffer . concat ( [ bomBytes , configContent ] ) ) ;
6682+
6683+ expect ( ( ) => readConfig ( { config : "wrangler.json" } ) ) . toThrow (
6684+ "Configuration file contains UTF-32 BE byte order marker"
6685+ ) ;
6686+ } ) ;
6687+
6688+ it ( "should error on UTF-32 LE BOM" , ( ) => {
6689+ const bomBytes = Buffer . from ( [ 0xff , 0xfe , 0x00 , 0x00 ] ) ;
6690+ const configContent = Buffer . from ( '{"name": "test"}' , "utf-8" ) ;
6691+ fs . writeFileSync ( "wrangler.json" , Buffer . concat ( [ bomBytes , configContent ] ) ) ;
6692+
6693+ expect ( ( ) => readConfig ( { config : "wrangler.json" } ) ) . toThrow (
6694+ "Configuration file contains UTF-32 LE byte order marker"
6695+ ) ;
6696+ } ) ;
6697+
6698+ it ( "should handle files without BOM normally" , ( ) => {
6699+ writeWranglerConfig ( { name : "no-bom-test" } ) ;
6700+
6701+ const config = readConfig ( { config : "wrangler.toml" } ) ;
6702+ expect ( config . name ) . toBe ( "no-bom-test" ) ;
6703+ } ) ;
6704+ } ) ;
6705+
66196706function normalizePath ( text : string ) : string {
66206707 return text
66216708 . replace ( "project\\wrangler.toml" , "project/wrangler.toml" )
0 commit comments