| 1 |
'use strict' |
| 2 |
|
| 3 |
const WASM_BUILDER_CONTAINER = 'ghcr.io/nodejs/wasm-builder@sha256:975f391d907e42a75b8c72eb77c782181e941608687d4d8694c3e9df415a0970' |
| 4 |
|
| 5 |
const { execSync, execFileSync } = require('node:child_process') |
| 6 |
const { writeFileSync, readFileSync } = require('node:fs') |
| 7 |
const { join, resolve } = require('node:path') |
| 8 |
|
| 9 |
const ROOT = resolve(__dirname, '../') |
| 10 |
const WASM_SRC = resolve(__dirname, '../deps/llhttp') |
| 11 |
const WASM_OUT = resolve(__dirname, '../lib/llhttp') |
| 12 |
|
| 13 |
|
| 14 |
const WASM_CC = process.env.WASM_CC || 'clang' |
| 15 |
let WASM_CFLAGS = process.env.WASM_CFLAGS || '--sysroot=/usr/share/wasi-sysroot -target wasm32-unknown-wasi' |
| 16 |
let WASM_LDFLAGS = process.env.WASM_LDFLAGS || '' |
| 17 |
const WASM_LDLIBS = process.env.WASM_LDLIBS || '' |
| 18 |
const WASM_OPT = process.env.WASM_OPT || 'wasm-opt' |
| 19 |
|
| 20 |
|
| 21 |
const EXTERNAL_PATH = process.env.EXTERNAL_PATH |
| 22 |
|
| 23 |
|
| 24 |
WASM_CFLAGS += ' -Ofast -fno-exceptions -fvisibility=hidden -mexec-model=reactor' |
| 25 |
WASM_LDFLAGS += ' -Wl,-error-limit=0 -Wl,-O3 -Wl,--lto-O3 -Wl,--strip-all' |
| 26 |
WASM_LDFLAGS += ' -Wl,--allow-undefined -Wl,--export-dynamic -Wl,--export-table' |
| 27 |
WASM_LDFLAGS += ' -Wl,--export=malloc -Wl,--export=free -Wl,--no-entry' |
| 28 |
|
| 29 |
const WASM_OPT_FLAGS = '-O4 --converge --strip-debug --strip-dwarf --strip-producers' |
| 30 |
|
| 31 |
const writeWasmChunk = (path, dest) => { |
| 32 |
const base64 = readFileSync(join(WASM_OUT, path)).toString('base64') |
| 33 |
writeFileSync(join(WASM_OUT, dest), `'use strict' |
| 34 |
|
| 35 |
const { Buffer } = require('node:buffer') |
| 36 |
|
| 37 |
const wasmBase64 = '${base64}' |
| 38 |
|
| 39 |
let wasmBuffer |
| 40 |
|
| 41 |
Object.defineProperty(module, 'exports', { |
| 42 |
get: () => { |
| 43 |
return wasmBuffer |
| 44 |
? wasmBuffer |
| 45 |
: (wasmBuffer = Buffer.from(wasmBase64, 'base64')) |
| 46 |
} |
| 47 |
}) |
| 48 |
`) |
| 49 |
} |
| 50 |
|
| 51 |
let platform = process.env.WASM_PLATFORM |
| 52 |
if (!platform && process.argv[2]) { |
| 53 |
platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim() |
| 54 |
} |
| 55 |
|
| 56 |
if (process.argv[2] === '--docker') { |
| 57 |
let cmd = `docker run --rm --platform=${platform.toString().trim()} ` |
| 58 |
if (process.platform === 'linux') { |
| 59 |
cmd += ` --user ${process.getuid()}:${process.getegid()}` |
| 60 |
} |
| 61 |
|
| 62 |
cmd += ` --mount type=bind,source=${ROOT}/lib/llhttp,target=/home/node/build/lib/llhttp \ |
| 63 |
--mount type=bind,source=${ROOT}/build,target=/home/node/build/build \ |
| 64 |
--mount type=bind,source=${ROOT}/deps,target=/home/node/build/deps \ |
| 65 |
-t ${WASM_BUILDER_CONTAINER} node build/wasm.js` |
| 66 |
console.log(`> ${cmd}\n\n`) |
| 67 |
execSync(cmd, { stdio: 'inherit' }) |
| 68 |
process.exit(0) |
| 69 |
} |
| 70 |
|
| 71 |
const hasApk = (function () { |
| 72 |
try { execSync('command -v apk'); return true } catch { return false } |
| 73 |
})() |
| 74 |
const hasOptimizer = (function () { |
| 75 |
try { execSync(`${WASM_OPT} --version`); return true } catch { return false } |
| 76 |
})() |
| 77 |
if (hasApk) { |
| 78 |
|
| 79 |
const buildInfo = execSync('apk info -v').toString() |
| 80 |
if (!buildInfo.includes('wasi-sdk')) { |
| 81 |
throw new Error('Failed to generate build environment information') |
| 82 |
} |
| 83 |
console.log(buildInfo) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
execSync(`${WASM_CC} ${WASM_CFLAGS} ${WASM_LDFLAGS} \ |
| 88 |
${join(WASM_SRC, 'src')}/*.c \ |
| 89 |
-I${join(WASM_SRC, 'include')} \ |
| 90 |
-o ${join(WASM_OUT, 'llhttp.wasm')} \ |
| 91 |
${WASM_LDLIBS}`, { stdio: 'inherit' }) |
| 92 |
|
| 93 |
if (hasOptimizer) { |
| 94 |
execSync(`${WASM_OPT} ${WASM_OPT_FLAGS} -o ${join(WASM_OUT, 'llhttp.wasm')} ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' }) |
| 95 |
} |
| 96 |
writeWasmChunk('llhttp.wasm', 'llhttp-wasm.js') |
| 97 |
|
| 98 |
|
| 99 |
execSync(`${WASM_CC} ${WASM_CFLAGS} -msimd128 ${WASM_LDFLAGS} \ |
| 100 |
${join(WASM_SRC, 'src')}/*.c \ |
| 101 |
-I${join(WASM_SRC, 'include')} \ |
| 102 |
-o ${join(WASM_OUT, 'llhttp_simd.wasm')} \ |
| 103 |
${WASM_LDLIBS}`, { stdio: 'inherit' }) |
| 104 |
|
| 105 |
if (hasOptimizer) { |
| 106 |
|
| 107 |
const wasmOptFlagsArray = WASM_OPT_FLAGS ? WASM_OPT_FLAGS.split(/\s+/).filter(Boolean) : [] |
| 108 |
execFileSync( |
| 109 |
WASM_OPT, |
| 110 |
[ |
| 111 |
...wasmOptFlagsArray, |
| 112 |
'--enable-simd', |
| 113 |
'-o', |
| 114 |
join(WASM_OUT, 'llhttp_simd.wasm'), |
| 115 |
join(WASM_OUT, 'llhttp_simd.wasm') |
| 116 |
], |
| 117 |
{ stdio: 'inherit' } |
| 118 |
) |
| 119 |
} |
| 120 |
writeWasmChunk('llhttp_simd.wasm', 'llhttp_simd-wasm.js') |
| 121 |
|
| 122 |
|
| 123 |
if (EXTERNAL_PATH) { |
| 124 |
writeFileSync(join(ROOT, 'loader.js'), ` |
| 125 |
'use strict' |
| 126 |
globalThis.__UNDICI_IS_NODE__ = true |
| 127 |
module.exports = require('node:module').createRequire('${EXTERNAL_PATH}/loader.js')('./index-fetch.js') |
| 128 |
delete globalThis.__UNDICI_IS_NODE__ |
| 129 |
`) |
| 130 |
} |