Skip to content

Commit a1ec671

Browse files
authored
improve codebase, fix QDTEXT lookup table (#682)
* use as const instead of IntDict * more const * use Uppercase and Lowercase * make entries in IntDict readonly * reduce use of enumToMap * well, we just need the HEAD method... let typescript take care of ensuring it being correct * implement createNumberRange * simplify * precompute * remove STATUSES_HTTP * restructure and remove enumToMapm simplify * fix wasm build * delete llhttp.c * simplify * simplify
1 parent 76e579e commit a1ec671

File tree

6 files changed

+307
-396
lines changed

6 files changed

+307
-396
lines changed

bin/build_wasm.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ execSync(
8888
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js'), join(WASM_OUT, 'constants.js'));
8989
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js.map'), join(WASM_OUT, 'constants.js.map'));
9090
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.d.ts'), join(WASM_OUT, 'constants.d.ts'));
91-
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js'), join(WASM_OUT, 'utils.js'));
92-
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js.map'), join(WASM_OUT, 'utils.js.map'));
93-
copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.d.ts'), join(WASM_OUT, 'utils.d.ts'));
9491

9592
function isErrorWithCode(error: unknown): error is Error & { code: string } {
9693
return typeof error === 'object' && error !== null && 'code' in error;

src/llhttp/c-headers.ts

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import constants from './constants';
22
import type { IntDict } from './constants';
3-
import { enumToMap } from './utils';
43

54
type Encoding = 'none' | 'hex';
65

@@ -17,43 +16,33 @@ export class CHeaders {
1716

1817
res += '\n';
1918

20-
const errorMap = enumToMap(constants.ERROR);
21-
const methodMap = enumToMap(constants.METHODS);
22-
const httpMethodMap = enumToMap(constants.METHODS, constants.METHODS_HTTP, [
23-
constants.METHODS.PRI,
24-
]);
25-
const rtspMethodMap = enumToMap(constants.METHODS, constants.METHODS_RTSP);
26-
const statusMap = enumToMap(constants.STATUSES, constants.STATUSES_HTTP);
27-
28-
res += this.buildEnum('llhttp_errno', 'HPE', errorMap);
19+
res += this.buildEnum('llhttp_errno', 'HPE', constants.ERROR);
2920
res += '\n';
30-
res += this.buildEnum('llhttp_flags', 'F', enumToMap(constants.FLAGS),
21+
res += this.buildEnum('llhttp_flags', 'F', constants.FLAGS,
3122
'hex');
3223
res += '\n';
3324
res += this.buildEnum('llhttp_lenient_flags', 'LENIENT',
34-
enumToMap(constants.LENIENT_FLAGS), 'hex');
25+
constants.LENIENT_FLAGS, 'hex');
3526
res += '\n';
36-
res += this.buildEnum('llhttp_type', 'HTTP',
37-
enumToMap(constants.TYPE));
27+
res += this.buildEnum('llhttp_type', 'HTTP', constants.TYPE);
3828
res += '\n';
39-
res += this.buildEnum('llhttp_finish', 'HTTP_FINISH',
40-
enumToMap(constants.FINISH));
29+
res += this.buildEnum('llhttp_finish', 'HTTP_FINISH', constants.FINISH);
4130
res += '\n';
42-
res += this.buildEnum('llhttp_method', 'HTTP', methodMap);
31+
res += this.buildEnum('llhttp_method', 'HTTP', constants.METHODS);
4332
res += '\n';
44-
res += this.buildEnum('llhttp_status', 'HTTP_STATUS', statusMap);
33+
res += this.buildEnum('llhttp_status', 'HTTP_STATUS', constants.STATUSES);
4534

4635
res += '\n';
4736

48-
res += this.buildMap('HTTP_ERRNO', errorMap);
37+
res += this.buildMap('HTTP_ERRNO', constants.ERROR);
4938
res += '\n';
50-
res += this.buildMap('HTTP_METHOD', httpMethodMap);
39+
res += this.buildMap('HTTP_METHOD', constants.METHODS_HTTP1);
5140
res += '\n';
52-
res += this.buildMap('RTSP_METHOD', rtspMethodMap);
41+
res += this.buildMap('RTSP_METHOD', constants.METHODS_RTSP);
5342
res += '\n';
54-
res += this.buildMap('HTTP_ALL_METHOD', methodMap);
43+
res += this.buildMap('HTTP_ALL_METHOD', constants.METHODS);
5544
res += '\n';
56-
res += this.buildMap('HTTP_STATUS', statusMap);
45+
res += this.buildMap('HTTP_STATUS', constants.STATUSES);
5746

5847
res += '\n';
5948

@@ -65,39 +54,32 @@ export class CHeaders {
6554
return res;
6655
}
6756

68-
private buildEnum(name: string, prefix: string, map: IntDict,
57+
private buildEnum(name: Lowercase<string>, prefix: string, map: IntDict,
6958
encoding: Encoding = 'none'): string {
7059
let res = '';
7160

72-
res += `enum ${name} {\n`;
73-
const keys = Object.keys(map);
74-
const keysLength = keys.length;
75-
for (let i = 0; i < keysLength; i++) {
76-
const key = keys[i];
77-
const isLast = i === keysLength - 1;
61+
for (const [ key, value ] of Object.entries(map).sort((a,b) => a[1] - b[1])) {
62+
if (res !== "") {
63+
res += ',\n';
64+
}
7865

79-
let value: number | string = map[key];
66+
res += ` ${prefix}_${key.replace(/-/g, '')} = `
8067

8168
if (encoding === 'hex') {
82-
value = `0x${value.toString(16)}`;
83-
}
84-
85-
res += ` ${prefix}_${key.replace(/-/g, '')} = ${value}`;
86-
if (!isLast) {
87-
res += ',\n';
69+
res += `0x${value.toString(16)}`;
70+
} else {
71+
res += value;
8872
}
8973
}
90-
res += '\n};\n';
91-
res += `typedef enum ${name} ${name}_t;\n`;
9274

93-
return res;
75+
return `enum ${name} {\n${res}\n};\ntypedef enum ${name} ${name}_t;\n`;
9476
}
9577

96-
private buildMap(name: string, map: IntDict): string {
78+
private buildMap(name: Uppercase<string>, map: IntDict): string {
9779
let res = '';
9880

9981
res += `#define ${name}_MAP(XX) \\\n`;
100-
for (const [ key, value ] of Object.entries(map)) {
82+
for (const [ key, value ] of Object.entries(map).sort((a,b) => a[1] - b[1])) {
10183
res += ` XX(${value}, ${key.replace(/-/g, '')}, ${key}) \\\n`;
10284
}
10385
res += '\n';

0 commit comments

Comments
 (0)