Skip to content

Commit c6ee839

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
1 parent ba70685 commit c6ee839

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

build/wasm.js

Lines changed: 26 additions & 11 deletions
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,27 @@ 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), `module.exports = '${base64}'\n`)
69+
}
70+
: (path, dest) => {
71+
writeFileSync(join(WASM_OUT, dest), `
72+
'use strict'
73+
74+
module.exports = fs.readFileSync(require.resolve('./${basename(path)}'))
75+
`)
76+
}
77+
6378
// Build wasm binary
6479
execSync(`${WASM_CC} ${WASM_CFLAGS} ${WASM_LDFLAGS} \
6580
${join(WASM_SRC, 'src')}/*.c \
6681
-I${join(WASM_SRC, 'include')} \
6782
-o ${join(WASM_OUT, 'llhttp.wasm')} \
6883
${WASM_LDLIBS}`, { stdio: 'inherit' })
6984

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-
)
85+
writeWasmChunk('llhttp.wasm', 'llhttp-wasm.js')
7586

7687
// Build wasm simd binary
7788
execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \
@@ -80,8 +91,12 @@ execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \
8091
-o ${join(WASM_OUT, 'llhttp_simd.wasm')} \
8192
${WASM_LDLIBS}`, { stdio: 'inherit' })
8293

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-
)
94+
writeWasmChunk('llhttp_simd.wasm', 'llhttp_simd-wasm.js')
95+
96+
if (EXTERNAL_PATH) {
97+
writeFileSync(join(ROOT, 'loader.js'), `
98+
'use strict'
99+
100+
module.exports = require('node:module').createRequire('${EXTERNAL_PATH}/loader.js')('./index-fetch.js')
101+
`)
102+
}

package.json

Lines changed: 1 addition & 0 deletions
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)