Skip to content

Commit 3f9e147

Browse files
authored
improve: wasm build (#3074)
1 parent 79c76c5 commit 3f9e147

12 files changed

+61
-291
lines changed

build/Dockerfile

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
FROM node:21-alpine@sha256:6d0f18a1c67dc218c4af50c21256616286a53c09e500fadf025b6d342e1c90ae
1+
FROM node:21-alpine3.19@sha256:ad255c65652e8e99ce0b9d9fc52eee3eae85f445b192f6f9e49a1305c77b2ba6
22

33
ARG UID=1000
44
ARG GID=1000
5+
ARG BINARYEN_VERSION=116
56

67
RUN apk add -U clang lld wasi-sdk
78
RUN mkdir /home/node/undici
89

910
WORKDIR /home/node/undici
1011

12+
RUN wget https://github.com/WebAssembly/binaryen/releases/download/version_$BINARYEN_VERSION/binaryen-version_$BINARYEN_VERSION-x86_64-linux.tar.gz && \
13+
tar -zxvf binaryen-version_$BINARYEN_VERSION-x86_64-linux.tar.gz binaryen-version_$BINARYEN_VERSION/bin/wasm-opt && \
14+
mv binaryen-version_$BINARYEN_VERSION/bin/wasm-opt ./ && \
15+
rm binaryen-version_$BINARYEN_VERSION-x86_64-linux.tar.gz && \
16+
rm -rf binaryen-version_$BINARYEN_VERSION && \
17+
chmod +x ./wasm-opt
18+
1119
COPY package.json .
20+
1221
COPY build build
1322
COPY deps deps
1423
COPY lib lib
1524

16-
RUN npm i
17-
1825
USER node

build/wasm.js

+45-49
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { execSync } = require('node:child_process')
44
const { writeFileSync, readFileSync } = require('node:fs')
5-
const { join, resolve, basename } = require('node:path')
5+
const { join, resolve } = require('node:path')
66

77
const ROOT = resolve(__dirname, '../')
88
const WASM_SRC = resolve(__dirname, '../deps/llhttp')
@@ -15,19 +15,40 @@ let WASM_CFLAGS = process.env.WASM_CFLAGS || '--sysroot=/usr/share/wasi-sysroot
1515
let WASM_LDFLAGS = process.env.WASM_LDFLAGS || ''
1616
const WASM_LDLIBS = process.env.WASM_LDLIBS || ''
1717

18-
const EXTERNAL_PATH = process.env.EXTERNAL_PATH
19-
2018
// These are relevant for undici and should not be overridden
2119
WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor'
2220
WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all'
2321
WASM_LDFLAGS += ' -Wl,--allow-undefined -Wl,--export-dynamic -Wl,--export-table'
2422
WASM_LDFLAGS += ' -Wl,--export=malloc -Wl,--export=free -Wl,--no-entry'
2523

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+
2636
let platform = process.env.WASM_PLATFORM
2737
if (!platform && process.argv[2]) {
2838
platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim()
2939
}
3040

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+
3152
if (process.argv[2] === '--prebuild') {
3253
const cmd = `docker build --platform=${platform.toString().trim()} -t llhttp_wasm_builder -f ${DOCKERFILE} ${ROOT}`
3354

@@ -59,50 +80,25 @@ if (hasApk) {
5980
console.log('Failed to generate build environment information')
6081
process.exit(-1)
6182
}
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')
108104
}

lib/llhttp/.gitkeep

Whitespace-only changes.

lib/llhttp/constants.d.ts

-199
This file was deleted.

lib/llhttp/constants.js.map

-1
This file was deleted.

lib/llhttp/llhttp-wasm.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/llhttp/llhttp.wasm

-6.69 KB
Binary file not shown.

lib/llhttp/llhttp_simd-wasm.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/llhttp/llhttp_simd.wasm

-6.65 KB
Binary file not shown.

lib/llhttp/utils.d.ts

-4
This file was deleted.

lib/llhttp/utils.js.map

-1
This file was deleted.

lib/llhttp/wasm_build_env.txt

-32
This file was deleted.

0 commit comments

Comments
 (0)