Skip to content

Commit b7bfc99

Browse files
committed
Support building for externally shared js builtins
Initial support for loading unbundled module in `AddExternalizedBuiltin`. - Reduces downstream distribution package size (by not shipping wasm twice and not base64-encoding it) - Provides a cleaner stacktrace - Easier to patch To enable this, pass `EXTERNAL_PATH=/path/to/global/node_modules/undici` to `build/wasm.js`. You shall also pass this path to `--shared-builtin-undici/undici-path` in Node.js's `configure.py`. Reference: nodejs/node@ca5be26b318 nodejs/node#44376 Signed-off-by: Zephyr Lykos <[email protected]>
1 parent ba70685 commit b7bfc99

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

build/wasm.js

+30-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
const ROOT = resolve(__dirname, '../')
88
const WASM_SRC = resolve(__dirname, '../deps/llhttp')
@@ -15,6 +15,8 @@ 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+
1820
// These are relevant for undici and should not be overridden
1921
WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor'
2022
WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all'
@@ -60,18 +62,31 @@ if (hasApk) {
6062
writeFileSync(join(WASM_OUT, 'wasm_build_env.txt'), buildInfo)
6163
}
6264

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+
6382
// Build wasm binary
6483
execSync(`${WASM_CC} ${WASM_CFLAGS} ${WASM_LDFLAGS} \
6584
${join(WASM_SRC, 'src')}/*.c \
6685
-I${join(WASM_SRC, 'include')} \
6786
-o ${join(WASM_OUT, 'llhttp.wasm')} \
6887
${WASM_LDLIBS}`, { stdio: 'inherit' })
6988

70-
const base64Wasm = readFileSync(join(WASM_OUT, 'llhttp.wasm')).toString('base64')
71-
writeFileSync(
72-
join(WASM_OUT, 'llhttp-wasm.js'),
73-
`module.exports = '${base64Wasm}'\n`
74-
)
89+
writeWasmChunk('llhttp.wasm', 'llhttp-wasm.js')
7590

7691
// Build wasm simd binary
7792
execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \
@@ -80,8 +95,12 @@ execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \
8095
-o ${join(WASM_OUT, 'llhttp_simd.wasm')} \
8196
${WASM_LDLIBS}`, { stdio: 'inherit' })
8297

83-
const base64WasmSimd = readFileSync(join(WASM_OUT, 'llhttp_simd.wasm')).toString('base64')
84-
writeFileSync(
85-
join(WASM_OUT, 'llhttp_simd-wasm.js'),
86-
`module.exports = '${base64WasmSimd}'\n`
87-
)
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+
module.exports = require('node:module').createRequire('${EXTERNAL_PATH}/loader.js')('./index-fetch.js')
105+
`)
106+
}

lib/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,15 @@ async function lazyllhttp () {
479479

480480
let mod
481481
try {
482-
mod = await WebAssembly.compile(Buffer.from(require('./llhttp/llhttp_simd-wasm.js'), 'base64'))
482+
mod = await WebAssembly.compile(require('./llhttp/llhttp_simd-wasm.js'))
483483
} catch (e) {
484484
/* istanbul ignore next */
485485

486486
// We could check if the error was caused by the simd option not
487487
// being enabled, but the occurring of this other error
488488
// * https://github.com/emscripten-core/emscripten/issues/11495
489489
// got me to remove that check to avoid breaking Node 12.
490-
mod = await WebAssembly.compile(Buffer.from(llhttpWasmData || require('./llhttp/llhttp-wasm.js'), 'base64'))
490+
mod = await WebAssembly.compile(llhttpWasmData || require('./llhttp/llhttp-wasm.js'))
491491
}
492492

493493
return await WebAssembly.instantiate(mod, {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"*.d.ts",
6666
"index.js",
6767
"index-fetch.js",
68+
"loader.js",
6869
"lib",
6970
"types",
7071
"docs"

0 commit comments

Comments
 (0)