Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add build for porting fetch to Node.js #1183

Merged
merged 3 commits into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions build/wasm.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"docs"
],
"scripts": {
"build:node": "esbuild index.js --bundle --platform=node --outfile=out.js",
targos marked this conversation as resolved.
Show resolved Hide resolved
"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