2
2
3
3
const { execSync } = require ( 'node:child_process' )
4
4
const { writeFileSync, readFileSync } = require ( 'node:fs' )
5
- const { join, resolve, basename } = require ( 'node:path' )
5
+ const { join, resolve } = require ( 'node:path' )
6
6
7
7
const ROOT = resolve ( __dirname , '../' )
8
8
const WASM_SRC = resolve ( __dirname , '../deps/llhttp' )
@@ -15,19 +15,40 @@ let WASM_CFLAGS = process.env.WASM_CFLAGS || '--sysroot=/usr/share/wasi-sysroot
15
15
let WASM_LDFLAGS = process . env . WASM_LDFLAGS || ''
16
16
const WASM_LDLIBS = process . env . WASM_LDLIBS || ''
17
17
18
- const EXTERNAL_PATH = process . env . EXTERNAL_PATH
19
-
20
18
// These are relevant for undici and should not be overridden
21
19
WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor'
22
20
WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all'
23
21
WASM_LDFLAGS += ' -Wl,--allow-undefined -Wl,--export-dynamic -Wl,--export-table'
24
22
WASM_LDFLAGS += ' -Wl,--export=malloc -Wl,--export=free -Wl,--no-entry'
25
23
24
+ const WASM_OPT_FLAGS = '-O4 --converge --strip-debug --strip-dwarf --strip-producers'
25
+
26
+ const writeWasmChunk = ( path , dest ) => {
27
+ const base64 = readFileSync ( join ( WASM_OUT , path ) ) . toString ( 'base64' )
28
+ writeFileSync ( join ( WASM_OUT , dest ) , `'use strict'
29
+
30
+ const { Buffer } = require('node:buffer')
31
+
32
+ module.exports = Buffer.from('${ base64 } ', 'base64')
33
+ ` )
34
+ }
35
+
26
36
let platform = process . env . WASM_PLATFORM
27
37
if ( ! platform && process . argv [ 2 ] ) {
28
38
platform = execSync ( 'docker info -f "{{.OSType}}/{{.Architecture}}"' ) . toString ( ) . trim ( )
29
39
}
30
40
41
+ if ( process . argv [ 2 ] === '--rm' ) {
42
+ const cmd = 'docker image rm llhttp_wasm_builder'
43
+
44
+ console . log ( `> ${ cmd } \n\n` )
45
+ try {
46
+ execSync ( cmd , { stdio : 'inherit' } )
47
+ } catch ( e ) { }
48
+
49
+ process . exit ( 0 )
50
+ }
51
+
31
52
if ( process . argv [ 2 ] === '--prebuild' ) {
32
53
const cmd = `docker build --platform=${ platform . toString ( ) . trim ( ) } -t llhttp_wasm_builder -f ${ DOCKERFILE } ${ ROOT } `
33
54
@@ -59,50 +80,25 @@ if (hasApk) {
59
80
console . log ( 'Failed to generate build environment information' )
60
81
process . exit ( - 1 )
61
82
}
62
- writeFileSync ( join ( WASM_OUT , 'wasm_build_env.txt' ) , buildInfo )
63
- }
64
-
65
- const writeWasmChunk = EXTERNAL_PATH
66
- ? ( path , dest ) => {
67
- const base64 = readFileSync ( join ( WASM_OUT , path ) ) . toString ( 'base64' )
68
- writeFileSync ( join ( WASM_OUT , dest ) , `
69
- const { Buffer } = require('node:buffer')
70
-
71
- module.exports = Buffer.from('${ base64 } ', 'base64')
72
- ` )
73
- }
74
- : ( path , dest ) => {
75
- writeFileSync ( join ( WASM_OUT , dest ) , `
76
- const { fs } = require('node:fs')
77
-
78
- module.exports = fs.readFileSync(require.resolve('./${ basename ( path ) } '))
79
- ` )
80
- }
81
-
82
- // Build wasm binary
83
- execSync ( `${ WASM_CC } ${ WASM_CFLAGS } ${ WASM_LDFLAGS } \
84
- ${ join ( WASM_SRC , 'src' ) } /*.c \
85
- -I${ join ( WASM_SRC , 'include' ) } \
86
- -o ${ join ( WASM_OUT , 'llhttp.wasm' ) } \
87
- ${ WASM_LDLIBS } ` , { stdio : 'inherit' } )
88
-
89
- writeWasmChunk ( 'llhttp.wasm' , 'llhttp-wasm.js' )
90
-
91
- // Build wasm simd binary
92
- execSync ( `${ WASM_CC } ${ WASM_CFLAGS } -msimd128 ${ WASM_LDFLAGS } \
93
- ${ join ( WASM_SRC , 'src' ) } /*.c \
94
- -I${ join ( WASM_SRC , 'include' ) } \
95
- -o ${ join ( WASM_OUT , 'llhttp_simd.wasm' ) } \
96
- ${ WASM_LDLIBS } ` , { stdio : 'inherit' } )
97
-
98
- writeWasmChunk ( 'llhttp_simd.wasm' , 'llhttp_simd-wasm.js' )
99
-
100
- if ( EXTERNAL_PATH ) {
101
- writeFileSync ( join ( ROOT , 'loader.js' ) , `
102
- 'use strict'
103
-
104
- globalThis.__UNDICI_IS_NODE__ = true
105
- module.exports = require('node:module').createRequire('${ EXTERNAL_PATH } /loader.js')('./index-fetch.js')
106
- delete globalThis.__UNDICI_IS_NODE__
107
- ` )
83
+ console . log ( buildInfo )
84
+
85
+ // Build wasm binary
86
+ execSync ( `${ WASM_CC } ${ WASM_CFLAGS } ${ WASM_LDFLAGS } \
87
+ ${ join ( WASM_SRC , 'src' ) } /*.c \
88
+ -I${ join ( WASM_SRC , 'include' ) } \
89
+ -o ${ join ( WASM_OUT , 'llhttp.wasm' ) } \
90
+ ${ WASM_LDLIBS } ` , { stdio : 'inherit' } )
91
+
92
+ execSync ( `./wasm-opt ${ WASM_OPT_FLAGS } -o ${ join ( WASM_OUT , 'llhttp.wasm' ) } ${ join ( WASM_OUT , 'llhttp.wasm' ) } ` , { stdio : 'inherit' } )
93
+ writeWasmChunk ( 'llhttp.wasm' , 'llhttp-wasm.js' )
94
+
95
+ // Build wasm simd binary
96
+ execSync ( `${ WASM_CC } ${ WASM_CFLAGS } -msimd128 ${ WASM_LDFLAGS } \
97
+ ${ join ( WASM_SRC , 'src' ) } /*.c \
98
+ -I${ join ( WASM_SRC , 'include' ) } \
99
+ -o ${ join ( WASM_OUT , 'llhttp_simd.wasm' ) } \
100
+ ${ WASM_LDLIBS } ` , { stdio : 'inherit' } )
101
+
102
+ execSync ( `./wasm-opt ${ WASM_OPT_FLAGS } --enable-simd -o ${ join ( WASM_OUT , 'llhttp_simd.wasm' ) } ${ join ( WASM_OUT , 'llhttp_simd.wasm' ) } ` , { stdio : 'inherit' } )
103
+ writeWasmChunk ( 'llhttp_simd.wasm' , 'llhttp_simd-wasm.js' )
108
104
}
0 commit comments