Skip to content

Commit dc7115c

Browse files
alexfueltigerchino
andauthored
chore: add test for parsing body size limit (#13437)
* node-adapter: add test for parsing body size limit * slight adjustments --------- Co-authored-by: Chew Tee Ming <[email protected]>
1 parent c7f96c8 commit dc7115c

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

eslint.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export default [
2727
'@typescript-eslint/await-thenable': 'error',
2828
'@typescript-eslint/no-unused-expressions': 'off',
2929
'@typescript-eslint/require-await': 'error',
30-
'@typescript-eslint/no-floating-promises': 'error',
30+
'@typescript-eslint/no-floating-promises': 'error'
3131
},
3232
ignores: [
3333
'packages/adapter-node/rollup.config.js',
34-
'packages/adapter-node/tests/smoke.spec.js',
34+
'packages/adapter-node/tests/smoke.spec_disabled.js',
3535
'packages/adapter-static/test/apps/**/*',
3636
'packages/create-svelte/shared/**/*',
3737
'packages/create-svelte/templates/**/*',

packages/adapter-node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"scripts": {
3636
"dev": "node -e \"fs.rmSync('files', { force: true, recursive: true })\" && rollup -cw",
3737
"build": "node -e \"fs.rmSync('files', { force: true, recursive: true })\" && rollup -c",
38-
"test": "echo \"tests temporarily disabled\" # vitest run",
38+
"test": "vitest run",
3939
"check": "tsc",
4040
"lint": "prettier --check .",
4141
"format": "pnpm lint --write",

packages/adapter-node/src/handler.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getRequest, setResponse, createReadableStream } from '@sveltejs/kit/nod
99
import { Server } from 'SERVER';
1010
import { manifest, prerendered, base } from 'MANIFEST';
1111
import { env } from 'ENV';
12+
import { parse_as_bytes } from '../utils.js';
1213

1314
/* global ENV_PREFIX */
1415

@@ -21,20 +22,7 @@ const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
2122
const host_header = env('HOST_HEADER', 'host').toLowerCase();
2223
const port_header = env('PORT_HEADER', '').toLowerCase();
2324

24-
/**
25-
* @param {string} bytes
26-
*/
27-
function parse_body_size_limit(bytes) {
28-
const multiplier =
29-
{
30-
K: 1024,
31-
M: 1024 * 1024,
32-
G: 1024 * 1024 * 1024
33-
}[bytes[bytes.length - 1]?.toUpperCase()] ?? 1;
34-
return Number(multiplier != 1 ? bytes.substring(0, bytes.length - 1) : bytes) * multiplier;
35-
}
36-
37-
const body_size_limit = parse_body_size_limit(env('BODY_SIZE_LIMIT', '512K'));
25+
const body_size_limit = parse_as_bytes(env('BODY_SIZE_LIMIT', '512K'));
3826

3927
if (isNaN(body_size_limit)) {
4028
throw new Error(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, test, describe } from 'vitest';
2+
import { parse_as_bytes } from '../utils.js';
3+
4+
describe('parse_as_bytes', () => {
5+
test('parses correctly', () => {
6+
const testData = {
7+
200: 200,
8+
'512K': 512 * 1024,
9+
'200M': 200 * 1024 * 1024,
10+
'1G': 1024 * 1024 * 1024,
11+
Infinity,
12+
asdf: NaN
13+
};
14+
15+
Object.keys(testData).forEach((input) => {
16+
const expected = testData[/** @type {keyof typeof testData} */ (input)];
17+
const actual = parse_as_bytes(input);
18+
expect(actual, `Testing input '${input}'`).toBe(expected);
19+
});
20+
});
21+
});

packages/adapter-node/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"@sveltejs/kit": ["../kit/types/index"]
1414
}
1515
},
16-
"include": ["index.js", "src/**/*.js", "internal.d.ts"]
16+
"include": ["index.js", "src/**/*.js", "tests/**/*.js", "internal.d.ts", "utils.js"],
17+
"exclude": ["tests/smoke.spec_disabled.js"]
1718
}

packages/adapter-node/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Parses the given value into number of bytes.
3+
*
4+
* @param {string} value - Size in bytes. Can also be specified with a unit suffix kilobytes (K), megabytes (M), or gigabytes (G).
5+
* @returns {number}
6+
*/
7+
export function parse_as_bytes(value) {
8+
const multiplier =
9+
{
10+
K: 1024,
11+
M: 1024 * 1024,
12+
G: 1024 * 1024 * 1024
13+
}[value[value.length - 1]?.toUpperCase()] ?? 1;
14+
return Number(multiplier != 1 ? value.substring(0, value.length - 1) : value) * multiplier;
15+
}

0 commit comments

Comments
 (0)