2
2
3
3
const { execSync } = require ( 'child_process' )
4
4
const { writeFileSync, readFileSync } = require ( 'fs' )
5
- const { join, resolve } = require ( 'path' )
5
+ const { join, resolve, basename } = require ( 'path' )
6
6
7
7
const ROOT = resolve ( __dirname , '../' )
8
8
const WASM_SRC = resolve ( __dirname , '../deps/llhttp' )
@@ -15,6 +15,8 @@ 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
+
18
20
// These are relevant for undici and should not be overridden
19
21
WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor'
20
22
WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all'
@@ -60,18 +62,31 @@ if (hasApk) {
60
62
writeFileSync ( join ( WASM_OUT , 'wasm_build_env.txt' ) , buildInfo )
61
63
}
62
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
+
63
82
// Build wasm binary
64
83
execSync ( `${ WASM_CC } ${ WASM_CFLAGS } ${ WASM_LDFLAGS } \
65
84
${ join ( WASM_SRC , 'src' ) } /*.c \
66
85
-I${ join ( WASM_SRC , 'include' ) } \
67
86
-o ${ join ( WASM_OUT , 'llhttp.wasm' ) } \
68
87
${ WASM_LDLIBS } ` , { stdio : 'inherit' } )
69
88
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' )
75
90
76
91
// Build wasm simd binary
77
92
execSync ( `${ WASM_CC } ${ WASM_CFLAGS } -msimd128 ${ WASM_LDFLAGS } \
@@ -80,8 +95,12 @@ execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \
80
95
-o ${ join ( WASM_OUT , 'llhttp_simd.wasm' ) } \
81
96
${ WASM_LDLIBS } ` , { stdio : 'inherit' } )
82
97
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
+ }
0 commit comments