summaryrefslogtreecommitdiffstats
path: root/third_party/python/aiohttp/vendor/llhttp/bin
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/python/aiohttp/vendor/llhttp/bin')
-rw-r--r--third_party/python/aiohttp/vendor/llhttp/bin/build_wasm.ts95
-rwxr-xr-xthird_party/python/aiohttp/vendor/llhttp/bin/generate.ts101
2 files changed, 196 insertions, 0 deletions
diff --git a/third_party/python/aiohttp/vendor/llhttp/bin/build_wasm.ts b/third_party/python/aiohttp/vendor/llhttp/bin/build_wasm.ts
new file mode 100644
index 0000000000..a8857037fe
--- /dev/null
+++ b/third_party/python/aiohttp/vendor/llhttp/bin/build_wasm.ts
@@ -0,0 +1,95 @@
+import { execSync } from 'child_process';
+import { copyFileSync, mkdirSync } from 'fs';
+import { join, resolve } from 'path';
+
+let platform = process.env.WASM_PLATFORM ?? '';
+const WASM_OUT = resolve(__dirname, '../build/wasm');
+const WASM_SRC = resolve(__dirname, '../');
+
+if (!platform && process.argv[2]) {
+ platform = execSync('docker info -f "{{.OSType}}/{{.Architecture}}"').toString().trim();
+}
+
+if (process.argv[2] === '--prebuild') {
+ const cmd = `docker build --platform=${platform.toString().trim()} -t llhttp_wasm_builder .`;
+
+ /* tslint:disable-next-line no-console */
+ console.log(`> ${cmd}\n\n`);
+ execSync(cmd, { stdio: 'inherit' });
+
+ process.exit(0);
+}
+
+if (process.argv[2] === '--setup') {
+ try {
+ mkdirSync(join(WASM_SRC, '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 --platform=${platform.toString().trim()}`;
+ // Try to avoid root permission problems on compiled assets
+ // when running on linux.
+ // It will work flawessly if uid === gid === 1000
+ // there will be some warnings otherwise.
+ if (process.platform === 'linux') {
+ cmd += ` --user ${process.getuid()}:${process.getegid()}`;
+ }
+ cmd += ` --mount type=bind,source=${WASM_SRC}/build,target=/home/node/llhttp/build llhttp_wasm_builder npm run wasm`;
+
+ /* tslint:disable-next-line no-console */
+ console.log(`> ${cmd}\n\n`);
+ execSync(cmd, { cwd: WASM_SRC, stdio: 'inherit' });
+ process.exit(0);
+}
+
+try {
+ mkdirSync(WASM_OUT);
+} catch (error) {
+ if (error.code !== 'EEXIST') {
+ throw error;
+ }
+}
+
+// Build ts
+execSync('npm run build', { cwd: WASM_SRC, stdio: 'inherit' });
+
+// Build wasm binary
+execSync(
+ `clang \
+ --sysroot=/usr/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 \
+ -Wl,--no-entry \
+ ${join(WASM_SRC, 'build', 'c')}/*.c \
+ ${join(WASM_SRC, 'src', 'native')}/*.c \
+ -I${join(WASM_SRC, 'build')} \
+ -o ${join(WASM_OUT, 'llhttp.wasm')}`,
+ { stdio: 'inherit' },
+);
+
+// Copy constants for `.js` and `.ts` users.
+copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js'), join(WASM_OUT, 'constants.js'));
+copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.js.map'), join(WASM_OUT, 'constants.js.map'));
+copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'constants.d.ts'), join(WASM_OUT, 'constants.d.ts'));
+copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js'), join(WASM_OUT, 'utils.js'));
+copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.js.map'), join(WASM_OUT, 'utils.js.map'));
+copyFileSync(join(WASM_SRC, 'lib', 'llhttp', 'utils.d.ts'), join(WASM_OUT, 'utils.d.ts'));
diff --git a/third_party/python/aiohttp/vendor/llhttp/bin/generate.ts b/third_party/python/aiohttp/vendor/llhttp/bin/generate.ts
new file mode 100755
index 0000000000..cb0d8c74dd
--- /dev/null
+++ b/third_party/python/aiohttp/vendor/llhttp/bin/generate.ts
@@ -0,0 +1,101 @@
+#!/usr/bin/env -S npx ts-node
+import * as fs from 'fs';
+import { LLParse } from 'llparse';
+import * as path from 'path';
+import * as semver from 'semver';
+
+import * as llhttp from '../src/llhttp';
+
+const pkgFile = path.join(__dirname, '..', 'package.json');
+const pkg = JSON.parse(fs.readFileSync(pkgFile).toString());
+
+const BUILD_DIR = path.join(__dirname, '..', 'build');
+const C_DIR = path.join(BUILD_DIR, 'c');
+const SRC_DIR = path.join(__dirname, '..', 'src');
+
+const C_FILE = path.join(C_DIR, 'llhttp.c');
+const HEADER_FILE = path.join(BUILD_DIR, 'llhttp.h');
+
+for (const dir of [ BUILD_DIR, C_DIR ]) {
+ try {
+ fs.mkdirSync(dir);
+ } catch (e) {
+ // no-op
+ }
+}
+
+function build(mode: 'strict' | 'loose') {
+ const llparse = new LLParse('llhttp__internal');
+ const instance = new llhttp.HTTP(llparse, mode);
+
+ return llparse.build(instance.build().entry, {
+ c: {
+ header: 'llhttp',
+ },
+ debug: process.env.LLPARSE_DEBUG ? 'llhttp__debug' : undefined,
+ headerGuard: 'INCLUDE_LLHTTP_ITSELF_H_',
+ });
+}
+
+function guard(strict: string, loose: string): string {
+ let out = '';
+
+ if (strict === loose) {
+ return strict;
+ }
+
+ out += '#if LLHTTP_STRICT_MODE\n';
+ out += '\n';
+ out += strict + '\n';
+ out += '\n';
+ out += '#else /* !LLHTTP_STRICT_MODE */\n';
+ out += '\n';
+ out += loose + '\n';
+ out += '\n';
+ out += '#endif /* LLHTTP_STRICT_MODE */\n';
+
+ return out;
+}
+
+const artifacts = {
+ loose: build('loose'),
+ strict: build('strict'),
+};
+
+let headers = '';
+
+headers += '#ifndef INCLUDE_LLHTTP_H_\n';
+headers += '#define INCLUDE_LLHTTP_H_\n';
+
+headers += '\n';
+
+const version = semver.parse(pkg.version)!;
+
+headers += `#define LLHTTP_VERSION_MAJOR ${version.major}\n`;
+headers += `#define LLHTTP_VERSION_MINOR ${version.minor}\n`;
+headers += `#define LLHTTP_VERSION_PATCH ${version.patch}\n`;
+headers += '\n';
+
+headers += '#ifndef LLHTTP_STRICT_MODE\n';
+headers += '# define LLHTTP_STRICT_MODE 0\n';
+headers += '#endif\n';
+headers += '\n';
+
+const cHeaders = new llhttp.CHeaders();
+
+headers += guard(artifacts.strict.header, artifacts.loose.header);
+
+headers += '\n';
+
+headers += cHeaders.build();
+
+headers += '\n';
+
+headers += fs.readFileSync(path.join(SRC_DIR, 'native', 'api.h'));
+
+headers += '\n';
+headers += '#endif /* INCLUDE_LLHTTP_H_ */\n';
+
+fs.writeFileSync(C_FILE,
+ guard(artifacts.strict.c || '', artifacts.loose.c || ''));
+fs.writeFileSync(HEADER_FILE, headers);