Skip to content

Commit 3ecb7b4

Browse files
authored
fix: use KB instead of bytes in config file size warning (#973) (#990)
1 parent 2b9debb commit 3ecb7b4

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/core/utils/user-config.spec.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe("userConfig", () => {
2828
{
2929
[convertToNativePaths(currentDir)]: {},
3030
},
31-
currentDir
31+
currentDir,
3232
);
3333
const entry = await asyncGeneratorToArray(traverseFolder("/"));
3434
expect(entry).toEqual([]);
@@ -129,7 +129,7 @@ describe("userConfig", () => {
129129
{
130130
[convertToNativePaths(currentDir)]: {},
131131
},
132-
currentDir
132+
currentDir,
133133
);
134134
const file = await findSWAConfigFile("/");
135135
expect(file).toBe(null);
@@ -159,7 +159,7 @@ describe("userConfig", () => {
159159
expect(config).toBeNull();
160160
expect(logger.warn).toHaveBeenLastCalledWith(
161161
` WARNING: Functionality defined in the routes.json file is now deprecated. File will be ignored!\n` +
162-
` Read more: https://docs.microsoft.com/azure/static-web-apps/configuration#routes`
162+
` Read more: https://docs.microsoft.com/azure/static-web-apps/configuration#routes`,
163163
);
164164
});
165165

@@ -178,10 +178,29 @@ describe("userConfig", () => {
178178
expect(config).toBeNull();
179179
expect(logger.warn).toHaveBeenLastCalledWith(
180180
` WARNING: Functionality defined in the routes.json file is now deprecated. File will be ignored!\n` +
181-
` Read more: https://docs.microsoft.com/azure/static-web-apps/configuration#routes`
181+
` Read more: https://docs.microsoft.com/azure/static-web-apps/configuration#routes`,
182182
);
183183
});
184184

185+
it("should warn with KB unit when config file exceeds max size", async () => {
186+
// Create a valid config file larger than 20KB (SWA_RUNTIME_CONFIG_MAX_SIZE_IN_KB)
187+
// Use many routes to inflate file size while keeping schema valid
188+
const routes = [];
189+
for (let i = 0; i < 500; i++) {
190+
routes.push({ route: `/route-${i}-${"a".repeat(30)}`, rewrite: `/index-${i}.html` });
191+
}
192+
const largeContent = JSON.stringify({ routes });
193+
vol.fromJSON({
194+
"staticwebapp.config.json": largeContent,
195+
});
196+
197+
vi.spyOn(logger, "log").mockImplementation(() => {});
198+
199+
await findSWAConfigFile("/");
200+
expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining("KB"));
201+
expect(logger.warn).not.toHaveBeenCalledWith(expect.stringContaining("bytes"));
202+
});
203+
185204
it("should ignore routes.json if a staticwebapp.config.json exists", async () => {
186205
vol.fromNestedJSON({
187206
s: {

src/core/utils/user-config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export async function findSWAConfigFile(folder: string): Promise<{ filepath: str
6262
const fileSize = (await fs.stat(file.filepath)).size;
6363
const fileSizeInKb = Math.round(fileSize / 1024);
6464
if (fileSizeInKb > SWA_RUNTIME_CONFIG_MAX_SIZE_IN_KB) {
65-
logger.warn(`WARNING: ${SWA_CONFIG_FILENAME} is ${fileSizeInKb} bytes. The maximum size is ${SWA_RUNTIME_CONFIG_MAX_SIZE_IN_KB} bytes.`);
65+
logger.warn(`WARNING: ${SWA_CONFIG_FILENAME} is ${fileSizeInKb} KB. The maximum size is ${SWA_RUNTIME_CONFIG_MAX_SIZE_IN_KB} KB.`);
6666
}
6767

6868
logger.silly(`Content parsed successfully`);
@@ -85,7 +85,7 @@ export async function findSWAConfigFile(folder: string): Promise<{ filepath: str
8585
logger.warn(`Found legacy configuration file: ${file?.filepath}.`);
8686
logger.warn(
8787
` WARNING: Functionality defined in the routes.json file is now deprecated. File will be ignored!\n` +
88-
` Read more: https://docs.microsoft.com/azure/static-web-apps/configuration#routes`
88+
` Read more: https://docs.microsoft.com/azure/static-web-apps/configuration#routes`,
8989
);
9090
return null;
9191
}

0 commit comments

Comments
 (0)