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

Wasm build #93

Merged
merged 10 commits into from
Apr 4, 2021
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
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.github
bench
test
images
lib
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM node:14.16.0-buster

ARG UID=1000
ARG GID=1000
ARG WASI_SDK_VERSION_MAJOR=12
ARG WASI_SDK_VERSION_MINOR=0

ENV WASI_ROOT=/home/node/wasi-sdk-12.0

RUN wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_SDK_VERSION_MAJOR}/wasi-sdk-${WASI_SDK_VERSION_MAJOR}.${WASI_SDK_VERSION_MINOR}-linux.tar.gz -P /tmp

RUN tar xvf /tmp/wasi-sdk-${WASI_SDK_VERSION_MAJOR}.${WASI_SDK_VERSION_MINOR}-linux.tar.gz --directory /home/node

RUN mkdir /home/node/llhttp

WORKDIR /home/node/llhttp

COPY . .

RUN npm ci

USER node
73 changes: 73 additions & 0 deletions build_wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'
dnlup marked this conversation as resolved.
Show resolved Hide resolved

const { mkdirSync, writeFileSync } = require('fs')
const { execSync } = require('child_process')
const { join } = require('path')
const js = require('javascript-stringify')
const { WASI_ROOT } = process.env

if (process.argv[2] === '--setup') {
try {
mkdirSync(join(__dirname, 'build'))
process.exit(0);
} catch (error) {
if (error.code !== 'EEXIST') {
throw error
}
process.exit(0);
}
}

if (process.argv[2] === '--docker') {
let cmd = 'docker run --rm -it';
if (process.platform === 'linux') {
cmd += ` --user ${process.getuid()}:${process.getegid()}`;
}
cmd += ` --mount type=bind,source=${__dirname}/build,target=/home/node/llhttp/build llhttp_wasm_builder node build_wasm.js`;
execSync(cmd, { stdio: 'inherit' });
process.exit(0);
}

if (!WASI_ROOT) {
throw new Error('Please setup the WASI_ROOT env variable.')
}

const WASM_OUT = join(__dirname, 'build', 'wasm')

try {
mkdirSync(WASM_OUT)
} catch (error) {
if (error.code !== 'EEXIST') {
throw error
}
}

// Build ts
execSync('npm run build', { stdio: 'inherit' })

// Build wasm binary
execSync(`${WASI_ROOT}/bin/clang \
--sysroot=${WASI_ROOT}/share/wasi-sysroot \
-target wasm32-unknown-wasi \
-Ofast \
-fno-exceptions \
-fvisibility=hidden \
-mexec-model=reactor \
-Wl,-error-limit=0 \
-Wl,-O3 \
-Wl,--lto-O3 \
-Wl,--strip-all \
-Wl,--allow-undefined \
-Wl,--export-dynamic \
-Wl,--export-table \
-Wl,--export=malloc \
-Wl,--export=free \
${join(__dirname, 'build', 'c')}/*.c \
${join(__dirname, 'src', 'native')}/*.c \
-I${join(__dirname, 'build')} \
-o ${join(WASM_OUT, 'llhttp.wasm')}`, { stdio: 'inherit' })

// Build `constants.js` file
const { constants } = require('.')
const data = `module.exports = ${js.stringify(constants)}`
writeFileSync(join(WASM_OUT, 'constants.js'), data, 'utf8')
222 changes: 222 additions & 0 deletions examples/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
'use strict';
dnlup marked this conversation as resolved.
Show resolved Hide resolved

/* global WebAssembly */

const { readFileSync } = require('fs');
const { resolve } = require('path');
const constants = require('../build/wasm/constants.js');
const bin = readFileSync(resolve(__dirname, '../build/wasm/llhttp.wasm'));
const mod = new WebAssembly.Module(bin);

const REQUEST = constants.TYPE.REQUEST;
const RESPONSE = constants.TYPE.RESPONSE;
const kOnMessageBegin = 0;
const kOnHeaders = 1;
const kOnHeadersComplete = 2;
const kOnBody = 3;
const kOnMessageComplete = 4;
const kOnExecute = 5;

const kPtr = Symbol('kPrt');
dnlup marked this conversation as resolved.
Show resolved Hide resolved
const kUrl = Symbol('kUrl');
const kStatusMessage = Symbol('kStatusMessage');
const kHeadersFields = Symbol('kHeadersFields');
const kHeadersValues = Symbol('kHeadersValues');
const kBody = Symbol('kBody');
const kReset = Symbol('kReset');
const kCheckErr = Symbol('kCheckErr');

const cstr = (ptr, len) =>
Buffer.from(inst.exports.memory.buffer, ptr, len).toString();

const wasm_on_message_begin = p => {
const i = instMap.get(p);
i[kReset]();
return i[kOnMessageBegin]();
};

const wasm_on_url = (p, at, length) => {
instMap.get(p)[kUrl] = cstr(at, length);
return 0;
};

const wasm_on_status = (p, at, length) => {
instMap.get(p)[kStatusMessage] = cstr(at, length);
return 0;
};

const wasm_on_header_field = (p, at, length) => {
const i= instMap.get(p)
i[kHeadersFields].push(cstr(at, length));
return 0;
};

const wasm_on_header_value = (p, at, length) => {
const i = instMap.get(p);
i[kHeadersValues].push(cstr(at, length));
return 0;
};

const wasm_on_headers_complete = p => {
const i = instMap.get(p);
const type = inst.exports.llhttp_get_type(p);
const versionMajor = inst.exports.llhttp_get_http_major(p);
const versionMinor = inst.exports.llhttp_get_http_minor(p);
const rawHeaders = [];
let method;
let url;
let statusCode;
let statusMessage;
const upgrade = inst.exports.llhttp_get_upgrade(p);
const shouldKeepAlive = inst.exports.llhttp_should_keep_alive(p);

for (let c = 0; c < i[kHeadersFields].length; c++) {
rawHeaders.push(i[kHeadersFields][c], i[kHeadersValues][c])
}
i[kOnHeaders](rawHeaders);

if (type === HTTPParser.REQUEST) {
method = constants.METHODS[inst.exports.llhttp_get_method(p)];
url = i[kUrl];
} else if (type === HTTPParser.RESPONSE) {
statusCode = inst.exports.llhttp_get_status_code(p);
statusMessage = i[kStatusMessage];
}
return i[kOnHeadersComplete](versionMajor, versionMinor, rawHeaders, method,
url, statusCode, statusMessage, upgrade, shouldKeepAlive);
};

const wasm_on_body = (p, at, length) => {
const i = instMap.get(p);
const body = Buffer.from(inst.exports.memory.buffer, at, length);
return i[kOnBody](body);
};

const wasm_on_message_complete = (p) => {
return instMap.get(p)[kOnMessageComplete]();
};

const instMap = new Map();

const inst = new WebAssembly.Instance(mod, {
env: {
wasm_on_message_begin,
wasm_on_url,
wasm_on_status,
wasm_on_header_field,
wasm_on_header_value,
wasm_on_headers_complete,
wasm_on_body,
wasm_on_message_complete,
},
});

inst.exports._initialize(); // wasi reactor

class HTTPParser {
constructor(type) {
this[kPtr] = inst.exports.llhttp_alloc(constants.TYPE[type]);
instMap.set(this[kPtr], this);

this[kUrl] = '';
this[kStatusMessage] = null;
this[kHeadersFields] = [];
this[kHeadersValues] = [];
this[kBody] = null;
}

[kReset]() {
this[kUrl] = '';
this[kStatusMessage] = null;
this[kHeadersFields] = [];
this[kHeadersValues] = [];
this[kBody] = null;
}

[kOnMessageBegin]() {
return 0;
}

[kOnHeaders](rawHeaders) {}

[kOnHeadersComplete](versionMajor, versionMinor, rawHeaders, method,
url, statusCode, statusMessage, upgrade, shouldKeepAlive) {
return 0;
}

[kOnBody](body) {
this[kBody] = body;
return 0;
}

[kOnMessageComplete]() {
return 0;
}

destroy() {
instMap.delete(this[kPtr]);
inst.exports.llhttp_free(this[kPtr]);
}

execute(data) {
// TODO(devsnek): could probably use static alloc and chunk but i'm lazy
const ptr = inst.exports.malloc(data.byteLength);
const u8 = new Uint8Array(inst.exports.memory.buffer);
u8.set(data, ptr);
const ret = inst.exports.llhttp_execute(this[kPtr], ptr, data.length);
inst.exports.free(ptr);
this[kCheckErr](ret);
return ret;
}

[kCheckErr](n) {
if (n === constants.ERROR.OK) {
return;
}
const ptr = inst.exports.llhttp_get_error_reason(this[kPtr]);
const u8 = new Uint8Array(inst.exports.memory.buffer);
const len = u8.indexOf(0, ptr) - ptr;
throw new Error(cstr(ptr, len));
}
}

HTTPParser.REQUEST = REQUEST;
HTTPParser.RESPONSE = RESPONSE;
HTTPParser.kOnMessageBegin = kOnMessageBegin;
HTTPParser.kOnHeaders = kOnHeaders;
HTTPParser.kOnHeadersComplete = kOnHeadersComplete;
HTTPParser.kOnBody = kOnBody;
HTTPParser.kOnMessageComplete = kOnMessageComplete;
HTTPParser.kOnExecute = kOnExecute;

{
const p = new HTTPParser(HTTPParser.REQUEST);

p.execute(Buffer.from(`\
POST /owo HTTP/1.1\r
X: Y\r
Content-Length: 9\r
\r
uh, meow?\r
`));
dnlup marked this conversation as resolved.
Show resolved Hide resolved

console.log(p);

p.destroy();
}

{
const p = new HTTPParser(HTTPParser.RESPONSE);

p.execute(Buffer.from(`\
dnlup marked this conversation as resolved.
Show resolved Hide resolved
HTTP/1.1 200 OK\r
X: Y\r
Content-Length: 9\r
\r
uh, meow?\r
`));

console.log(p);

p.destroy();
}
6 changes: 6 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"bench": "ts-node bench/",
"build": "ts-node bin/generate.ts",
"build-ts": "tsc",
"prebuild-wasm": "docker build -t llhttp_wasm_builder . && node build_wasm.js --setup",
"build-wasm": "node build_wasm.js --docker",
"clean": "rm -rf lib && rm -rf test/tmp",
"prepare": "npm run clean && npm run build-ts",
"lint": "tslint -c tslint.json bin/*.ts src/*.ts src/**/*.ts test/*.ts test/**/*.ts",
Expand All @@ -38,6 +40,7 @@
"devDependencies": {
"@types/mocha": "^5.2.7",
"@types/node": "^10.17.52",
"javascript-stringify": "^2.0.1",
"llparse-dot": "^1.0.1",
"llparse-test-fixture": "^5.0.1",
"mdgator": "^1.1.2",
Expand Down
Loading