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

Use llhttp wasm build #575

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions build/llhttp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict'

const { join, resolve } = require('path')
const { copyFileSync, rmSync } = require('fs')
const { execSync } = require('child_process')
const TMP = require('os').tmpdir()

const REPO = '[email protected]:dnlup/llhttp.git'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we used the upstream repository instead and we copy over whatever files we need.

Alternatively, I'd be ok in doing that adds this to llhttp itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if we used the upstream repository instead and we copy over whatever files we need.

Expanding on https://github.com/nodejs/undici/pull/575/files#r589005221 (and thinking out loud), we need to build the c files prior to build the wasm binary (and the source files are the same llhttp is using to build the parser), I think moving that out of llhttp would require us to duplicate a lot of build logic in here. Like, we would need the c src files, the js files to build the parser, add the build scripts.

Alternatively, I'd be ok in doing that adds this to llhttp itself.

I'll open a PR to llhttp and see what happens.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, here's the PR:

nodejs/llhttp#93

const CHECKOUT = 'undici_wasm'
ronag marked this conversation as resolved.
Show resolved Hide resolved
const REPO_PATH = join(TMP, 'llhttp')
ronag marked this conversation as resolved.
Show resolved Hide resolved
const WASM_OUT = resolve(__dirname, '../lib/llhttp')

let code = 0

try {
execSync(`git clone ${REPO}`, { stdio: 'inherit', cwd: TMP })
execSync(`git checkout ${CHECKOUT}`, { stdio: 'inherit', cwd: REPO_PATH })
// https://docs.npmjs.com/cli/v7/commands/npm-ci
// Performs a clean install using the lockfile, this makes the installation faster.
execSync('npm ci', { stdio: 'inherit', cwd: REPO_PATH })
ronag marked this conversation as resolved.
Show resolved Hide resolved
execSync('npm run build-wasm', { stdio: 'inherit', cwd: REPO_PATH })
copyFileSync(join(REPO_PATH, 'build', 'wasm', 'llhttp.wasm'), join(WASM_OUT, 'llhttp.wasm'))
copyFileSync(join(REPO_PATH, 'build', 'wasm', 'constants.js'), join(WASM_OUT, 'constants.js'))
} catch (error) {
console.error(error)
code = 1
} finally {
rmSync(REPO_PATH, { recursive: true, force: true })
process.exit(code)
}
77 changes: 11 additions & 66 deletions lib/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const net = require('net')
const tls = require('tls')
const HTTPParser = require('../node/http-parser')
const HTTPParser = require('../llhttp/parser')
const EventEmitter = require('events')
const assert = require('assert')
const util = require('./util')
Expand Down Expand Up @@ -56,9 +56,6 @@ const {
kStrictContentLength
} = require('./symbols')

const nodeVersions = process.version.split('.')
const nodeMajorVersion = parseInt(nodeVersions[0].slice(1))
const nodeMinorVersion = parseInt(nodeVersions[1])
const insecureHTTPParser = process.execArgv.includes('--insecure-http-parser')

function getServerName (client, host) {
Expand Down Expand Up @@ -357,35 +354,7 @@ class Client extends EventEmitter {

class Parser extends HTTPParser {
constructor (client, socket) {
/* istanbul ignore next */
if (nodeMajorVersion === 12 && nodeMinorVersion < 19) {
super()
this.initialize(
HTTPParser.RESPONSE,
{},
0
)
} else if (nodeMajorVersion === 12 && nodeMinorVersion >= 19) {
super()
this.initialize(
HTTPParser.RESPONSE,
{},
client[kMaxHeadersSize],
0
)
} else if (nodeMajorVersion > 12) {
super()
this.initialize(
HTTPParser.RESPONSE,
{},
client[kMaxHeadersSize],
insecureHTTPParser,
0
)
} else {
super(HTTPParser.RESPONSE, false)
}

super(client[kMaxHeadersSize], insecureHTTPParser)
this.client = client
this.socket = socket
this.timeout = null
Expand Down Expand Up @@ -427,7 +396,7 @@ class Parser extends HTTPParser {
}
this.resuming = false

socketResume(this.socket)
this.socket.resume()
}

this._pause = () => {
Expand All @@ -437,8 +406,10 @@ class Parser extends HTTPParser {

this.paused = true

socketPause(this.socket)
this.socket.pause()
}

socket.on('data', onSocketData)
}

[HTTPParser.kOnHeaders] (rawHeaders) {
Expand Down Expand Up @@ -542,7 +513,6 @@ class Parser extends HTTPParser {
this.request = request

if (request.upgrade) {
this.unconsume()
this.upgrade = true
return
}
Expand Down Expand Up @@ -747,7 +717,6 @@ class Parser extends HTTPParser {
destroy () {
clearTimeout(this.timeout)
this.timeout = null
this.unconsume()
setImmediate((self) => self.close(), this)
}
}
Expand Down Expand Up @@ -793,6 +762,10 @@ function onSocketError (err) {
}
}

function onSocketData (data) {
this[kParser].execute(data)
}

function onSocketEnd () {
util.destroy(this, new SocketError('other side closed'))
}
Expand All @@ -811,6 +784,7 @@ function detachSocket (socket) {
.removeListener('session', onSocketSession)
.removeListener('error', onSocketError)
.removeListener('end', onSocketEnd)
.removeListener('data', onSocketData)
.removeListener('close', onSocketClose)
}

Expand Down Expand Up @@ -885,15 +859,6 @@ function connect (client) {

const parser = new Parser(client, socket)

/* istanbul ignore next */
if (nodeMajorVersion >= 12) {
assert(socket._handle)
parser.consume(socket._handle)
} else {
assert(socket._handle && socket._handle._externalStream)
parser.consume(socket._handle._externalStream)
}

socket[kIdleTimeout] = null
socket[kIdleTimeoutValue] = null
socket[kWriting] = false
Expand All @@ -909,26 +874,6 @@ function connect (client) {
.on('close', onSocketClose)
}

function socketPause (socket) {
if (socket._handle && socket._handle.reading) {
socket._handle.reading = false
const err = socket._handle.readStop()
if (err) {
socket.destroy(util.errnoException(err, 'read'))
}
}
}

function socketResume (socket) {
if (socket._handle && !socket._handle.reading) {
socket._handle.reading = true
const err = socket._handle.readStart()
if (err) {
socket.destroy(util.errnoException(err, 'read'))
}
}
}

function emitDrain (client) {
client[kNeedDrain] = 0
client.emit('drain')
Expand Down
1 change: 1 addition & 0 deletions lib/llhttp/constants.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added lib/llhttp/llhttp.wasm
Binary file not shown.
Loading