Skip to content

Commit

Permalink
feat: add build for porting fetch to Node.js (#1183)
Browse files Browse the repository at this point in the history
* feat: add build for porting to Node.js

* inline wasm

* Update package.json
  • Loading branch information
targos committed Jan 30, 2022
1 parent 2d03e77 commit 19b5789
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
13 changes: 13 additions & 0 deletions build/wasm.js
@@ -1,6 +1,7 @@
'use strict'

const { execSync } = require('child_process')
const { writeFileSync, readFileSync } = require('fs');
const { join, resolve } = require('path')
const { WASI_ROOT } = process.env

Expand Down Expand Up @@ -43,6 +44,12 @@ execSync(`${WASI_ROOT}/bin/clang \
-I${join(WASM_SRC, 'include')} \
-o ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' })

const base64Wasm = readFileSync(join(WASM_OUT, 'llhttp.wasm')).toString('base64')
writeFileSync(
join(WASM_OUT, 'llhttp.wasm.js'),
`module.exports = "${base64Wasm}";\n`
)

// Build wasm simd binary
execSync(`${WASI_ROOT}/bin/clang \
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
Expand All @@ -64,3 +71,9 @@ execSync(`${WASI_ROOT}/bin/clang \
${join(WASM_SRC, 'src')}/*.c \
-I${join(WASM_SRC, 'include')} \
-o ${join(WASM_OUT, 'llhttp_simd.wasm')}`, { stdio: 'inherit' })

const base64WasmSimd = readFileSync(join(WASM_OUT, 'llhttp_simd.wasm')).toString('base64')
writeFileSync(
join(WASM_OUT, 'llhttp_simd.wasm.js'),
`module.exports = "${base64WasmSimd}";\n`
)
9 changes: 4 additions & 5 deletions lib/client.js
Expand Up @@ -24,6 +24,8 @@ const {
HTTPParserError
} = require('./core/errors')
const buildConnector = require('./core/connect')
const llhttpWasmData = require('./llhttp/llhttp.wasm.js')
const llhttpSimdWasmData = require('./llhttp/llhttp_simd.wasm.js')

const {
kUrl,
Expand Down Expand Up @@ -409,20 +411,17 @@ const constants = require('./llhttp/constants')
const EMPTY_BUF = Buffer.alloc(0)

async function lazyllhttp () {
const { resolve } = require('path')
const { readFile } = require('fs').promises

let mod
try {
mod = await WebAssembly.compile(await readFile(resolve(__dirname, './llhttp/llhttp_simd.wasm')))
mod = await WebAssembly.compile(Buffer.from(llhttpWasmData, 'base64'))
} catch (e) {
/* istanbul ignore next */

// We could check if the error was caused by the simd option not
// being enabled, but the occurring of this other error
// * https://github.com/emscripten-core/emscripten/issues/11495
// got me to remove that check to avoid breaking Node 12.
mod = await WebAssembly.compile(await readFile(resolve(__dirname, './llhttp/llhttp.wasm')))
mod = await WebAssembly.compile(Buffer.from(llhttpSimdWasmData, 'base64'))
}

return await WebAssembly.instantiate(mod, {
Expand Down
1 change: 1 addition & 0 deletions lib/llhttp/llhttp.wasm.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/llhttp/llhttp_simd.wasm.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -40,6 +40,7 @@
"docs"
],
"scripts": {
"build:node": "esbuild index.js --bundle --platform=node --outfile=undici.js",
"prebuild:wasm": "docker build -t llhttp_wasm_builder -f build/Dockerfile .",
"build:wasm": "node build/wasm.js --docker",
"lint": "standard | snazzy",
Expand Down Expand Up @@ -74,6 +75,7 @@
"cronometro": "^0.8.0",
"delay": "^5.0.0",
"docsify-cli": "^4.4.3",
"esbuild": "^0.14.14",
"formdata-node": "^4.3.1",
"https-pem": "^2.0.0",
"husky": "^7.0.2",
Expand Down

0 comments on commit 19b5789

Please sign in to comment.